[PATCH] D24667: [mips][ias] Handle more complicated expressions for memory operands

Simon Dardis via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 16 07:47:54 PDT 2016


sdardis created this revision.
sdardis added reviewers: vkalintiris, zoran.jovanovic.
sdardis added subscribers: llvm-commits, seanbruno, emaste.
Herald added a subscriber: sdardis.

This patch teaches ias for mips to handle expressions such as
(8*4)+(8*31)($sp). Such expression typically occur from the expansion
of multiple macro definitions.

This partially resolves PR/30383.

https://reviews.llvm.org/D24667

Files:
  lib/Target/Mips/AsmParser/MipsAsmParser.cpp
  test/MC/Mips/memory-offsets.s

Index: test/MC/Mips/memory-offsets.s
===================================================================
--- /dev/null
+++ test/MC/Mips/memory-offsets.s
@@ -0,0 +1,34 @@
+# RUN: llvm-mc -arch=mips -mcpu=mips32 -show-encoding %s | FileCheck %s
+
+# Check that parseMemOperand handles expressions such as <int>, (<int>),
+# <expr>, <expr> op <expr>, (<expr>) op (<expr>).
+
+    .global __start
+    .ent    __start
+__start:
+    lw $31, ($29)                       # CHECK: lw  $ra, 0($sp)      # encoding: [0x8f,0xbf,0x00,0x00]
+    lw $31, 0($29)                      # CHECK: lw  $ra, 0($sp)      # encoding: [0x8f,0xbf,0x00,0x00]
+    lw $31, (8)($29)                    # CHECK: lw  $ra, 8($sp)      # encoding: [0x8f,0xbf,0x00,0x08]
+    lw $31, 3 + (4 * 8)($29)            # CHECK: lw  $ra, 35($sp)     # encoding: [0x8f,0xbf,0x00,0x23]
+    lw $31, (8 + 8)($29)                # CHECK: lw  $ra, 16($sp)     # encoding: [0x8f,0xbf,0x00,0x10]
+    lw $31, (8 << 4)($29)               # CHECK: lw  $ra, 128($sp)    # encoding: [0x8f,0xbf,0x00,0x80]
+    lw $31, (32768 >> 2)($29)           # CHECK: lw  $ra, 8192($sp)   # encoding: [0x8f,0xbf,0x20,0x00]
+    lw $31, 32768 >> 2($29)             # CHECK: lw  $ra, 8192($sp)   # encoding: [0x8f,0xbf,0x20,0x00]
+    lw $31, 2 << 3($29)                 # CHECK: lw  $ra, 16($sp)     # encoding: [0x8f,0xbf,0x00,0x10]
+    lw $31, (2 << 3)($29)               # CHECK: lw  $ra, 16($sp)     # encoding: [0x8f,0xbf,0x00,0x10]
+    lw $31, 4 - (4 * 8)($29)            # CHECK: lw  $ra, -28($sp)    # encoding: [0x8f,0xbf,0xff,0xe4]
+    lw $31, 4 | 8 ($29)                 # CHECK: lw  $ra, 12($sp)     # encoding: [0x8f,0xbf,0x00,0x0c]
+    lw $31, 4 || 8 ($29)                # CHECK: lw  $ra, 1($sp)      # encoding: [0x8f,0xbf,0x00,0x01]
+    lw $31, 8 & 8 ($29)                 # CHECK: lw  $ra, 8($sp)      # encoding: [0x8f,0xbf,0x00,0x08]
+    lw $31, 8 && 8($29)                 # CHECK: lw  $ra, 1($sp)      # encoding: [0x8f,0xbf,0x00,0x01]
+    lw $31, (8 * 4) ^ (8 * 31)($29)     # CHECK: lw  $ra, 216($sp)    # encoding: [0x8f,0xbf,0x00,0xd8]
+    lw $31, (8 * 4) / (8 * 31)($29)     # CHECK: lw  $ra, 0($sp)      # encoding: [0x8f,0xbf,0x00,0x00]
+    lw $31, (8 * 4) % (8 * 31)($29)     # CHECK: lw  $ra, 32($sp)     # encoding: [0x8f,0xbf,0x00,0x20]
+    lw $31, (8 * 4) % (8)($29)          # CHECK: lw  $ra, 0($sp)      # encoding: [0x8f,0xbf,0x00,0x00]
+    lw $31, (8 * 4) + (8 * 31) ($29)    # CHECK: lw  $ra, 280($sp)    # encoding: [0x8f,0xbf,0x01,0x18]
+    lw $31, (8*4) + (8*31) + (8*32 + __start) ($29) # CHECK:  lui $ra, %hi(((+248)+((8*32)+__start))+32) # encoding: [0x3c,0x1f,A,A]
+                                                    # CHECK:                                #   fixup A - offset: 0, value: %hi(((+248)+((8*32)+__start))+32), kind: fixup_Mips_HI16
+                                                    # CHECK:  addu  $ra, $ra, $sp           # encoding: [0x03,0xfd,0xf8,0x21]
+                                                    # CHECK:  lw  $ra, %lo(((+248)+((8*32)+__start))+32)($ra) # encoding: [0x8f,0xff,A,A]
+                                                    # CHECK:                                #   fixup A - offset: 0, value: %lo(((+248)+((8*32)+__start))+32), kind: fixup_Mips_LO16
+    .end __start
Index: lib/Target/Mips/AsmParser/MipsAsmParser.cpp
===================================================================
--- lib/Target/Mips/AsmParser/MipsAsmParser.cpp
+++ lib/Target/Mips/AsmParser/MipsAsmParser.cpp
@@ -4391,6 +4391,7 @@
 
   if (isParenExpr)
     return getParser().parseParenExprOfDepth(0, Res, S);
+
   return getParser().parseExpression(Res);
 }
 
@@ -4435,8 +4436,34 @@
             MipsOperand::CreateMem(std::move(Base), IdVal, S, E, *this));
         return MatchOperand_Success;
       }
-      Error(Parser.getTok().getLoc(), "'(' expected");
-      return MatchOperand_ParseFail;
+      MCBinaryExpr::Opcode Opcode;
+      switch (Tok.getKind()) {
+      case AsmToken::Plus:           Opcode = MCBinaryExpr::Add;  break;
+      case AsmToken::Minus:          Opcode = MCBinaryExpr::Sub;  break;
+      case AsmToken::Star:           Opcode = MCBinaryExpr::Mul;  break;
+      case AsmToken::Pipe:           Opcode = MCBinaryExpr::Or;   break;
+      case AsmToken::PipePipe:       Opcode = MCBinaryExpr::LOr;  break;
+      case AsmToken::Amp:            Opcode = MCBinaryExpr::And;  break;
+      case AsmToken::AmpAmp:         Opcode = MCBinaryExpr::LAnd; break;
+      case AsmToken::EqualEqual:     Opcode = MCBinaryExpr::EQ;   break;
+      case AsmToken::ExclaimEqual:   Opcode = MCBinaryExpr::NE;   break;
+      case AsmToken::Less:           Opcode = MCBinaryExpr::LT;   break;
+      case AsmToken::LessEqual:      Opcode = MCBinaryExpr::LTE;  break;
+      case AsmToken::LessLess:       Opcode = MCBinaryExpr::Shl;  break;
+      case AsmToken::Greater:        Opcode = MCBinaryExpr::GT;   break;
+      case AsmToken::GreaterEqual:   Opcode = MCBinaryExpr::GTE;  break;
+      case AsmToken::GreaterGreater: Opcode = MCBinaryExpr::LShr; break;
+      case AsmToken::Caret:   Opcode = MCBinaryExpr::Xor; Parser.Lex(); break;
+      case AsmToken::Slash:   Opcode = MCBinaryExpr::Div; Parser.Lex(); break;
+      case AsmToken::Percent: Opcode = MCBinaryExpr::Mod; Parser.Lex(); break;
+      default:
+        Error(Parser.getTok().getLoc(), "'(' or expression expected");
+        return MatchOperand_ParseFail;
+      }
+      const MCExpr * Res2;
+      if (getParser().parseExpression(Res2))
+        return MatchOperand_ParseFail;
+      IdVal = MCBinaryExpr::create(Opcode, IdVal, Res2, getContext());
     }
 
     Parser.Lex(); // Eat the '(' token.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24667.71638.patch
Type: text/x-patch
Size: 5704 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160916/da3572e2/attachment.bin>


More information about the llvm-commits mailing list