Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1 | //===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "ARM.h" |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 11 | #include "ARMSubtarget.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 13 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 14 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCStreamer.h" |
| 16 | #include "llvm/MC/MCExpr.h" |
| 17 | #include "llvm/MC/MCInst.h" |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 18 | #include "llvm/Target/TargetRegistry.h" |
| 19 | #include "llvm/Target/TargetAsmParser.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallVector.h" |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Twine.h" |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 27 | // The shift types for register controlled shifts in arm memory addressing |
| 28 | enum ShiftType { |
| 29 | Lsl, |
| 30 | Lsr, |
| 31 | Asr, |
| 32 | Ror, |
| 33 | Rrx |
| 34 | }; |
| 35 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 36 | namespace { |
| 37 | struct ARMOperand; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 38 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 39 | class ARMAsmParser : public TargetAsmParser { |
| 40 | MCAsmParser &Parser; |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 41 | TargetMachine &TM; |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 42 | |
| 43 | private: |
| 44 | MCAsmParser &getParser() const { return Parser; } |
| 45 | |
| 46 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 47 | |
| 48 | void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); } |
| 49 | |
| 50 | bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } |
| 51 | |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 52 | int TryParseRegister(); |
| 53 | ARMOperand *TryParseRegisterWithWriteBack(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 54 | ARMOperand *ParseRegisterList(); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 55 | ARMOperand *ParseMemory(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 56 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 57 | bool ParseMemoryOffsetReg(bool &Negative, |
| 58 | bool &OffsetRegShifted, |
| 59 | enum ShiftType &ShiftType, |
| 60 | const MCExpr *&ShiftAmount, |
| 61 | const MCExpr *&Offset, |
| 62 | bool &OffsetIsReg, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 63 | int &OffsetRegNum, |
| 64 | SMLoc &E); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 65 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 66 | bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 68 | ARMOperand *ParseOperand(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 69 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 70 | bool ParseDirectiveWord(unsigned Size, SMLoc L); |
| 71 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 72 | bool ParseDirectiveThumb(SMLoc L); |
| 73 | |
| 74 | bool ParseDirectiveThumbFunc(SMLoc L); |
| 75 | |
| 76 | bool ParseDirectiveCode(SMLoc L); |
| 77 | |
| 78 | bool ParseDirectiveSyntax(SMLoc L); |
| 79 | |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 80 | bool MatchAndEmitInstruction(SMLoc IDLoc, |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 81 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 82 | MCStreamer &Out); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 83 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 84 | /// @name Auto-generated Match Functions |
| 85 | /// { |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 87 | #define GET_ASSEMBLER_HEADER |
| 88 | #include "ARMGenAsmMatcher.inc" |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 89 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 90 | /// } |
| 91 | |
| 92 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 93 | public: |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 94 | ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM) |
| 95 | : TargetAsmParser(T), Parser(_Parser), TM(_TM) {} |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 96 | |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 97 | virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 98 | SmallVectorImpl<MCParsedAsmOperand*> &Operands); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 99 | |
| 100 | virtual bool ParseDirective(AsmToken DirectiveID); |
| 101 | }; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 102 | } // end anonymous namespace |
| 103 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 104 | namespace { |
| 105 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 106 | /// ARMOperand - Instances of this class represent a parsed ARM machine |
| 107 | /// instruction. |
Chris Lattner | 7659389 | 2010-01-14 21:21:40 +0000 | [diff] [blame] | 108 | struct ARMOperand : public MCParsedAsmOperand { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 109 | public: |
| 110 | enum KindTy { |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 111 | CondCode, |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 112 | Immediate, |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 113 | Memory, |
| 114 | Register, |
| 115 | Token |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 116 | } Kind; |
| 117 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 118 | SMLoc StartLoc, EndLoc; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 119 | |
| 120 | union { |
| 121 | struct { |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 122 | ARMCC::CondCodes Val; |
| 123 | } CC; |
| 124 | |
| 125 | struct { |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 126 | const char *Data; |
| 127 | unsigned Length; |
| 128 | } Tok; |
| 129 | |
| 130 | struct { |
| 131 | unsigned RegNum; |
Kevin Enderby | 99e6d4e | 2009-10-07 18:01:35 +0000 | [diff] [blame] | 132 | bool Writeback; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 133 | } Reg; |
| 134 | |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 135 | struct { |
| 136 | const MCExpr *Val; |
| 137 | } Imm; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 138 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 139 | // This is for all forms of ARM address expressions |
| 140 | struct { |
| 141 | unsigned BaseRegNum; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 142 | unsigned OffsetRegNum; // used when OffsetIsReg is true |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 143 | const MCExpr *Offset; // used when OffsetIsReg is false |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 144 | const MCExpr *ShiftAmount; // used when OffsetRegShifted is true |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 145 | enum ShiftType ShiftType; // used when OffsetRegShifted is true |
| 146 | unsigned |
| 147 | OffsetRegShifted : 1, // only used when OffsetIsReg is true |
| 148 | Preindexed : 1, |
| 149 | Postindexed : 1, |
| 150 | OffsetIsReg : 1, |
| 151 | Negative : 1, // only used when OffsetIsReg is true |
| 152 | Writeback : 1; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 153 | } Mem; |
| 154 | |
| 155 | }; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 156 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 157 | ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() { |
| 158 | Kind = o.Kind; |
| 159 | StartLoc = o.StartLoc; |
| 160 | EndLoc = o.EndLoc; |
| 161 | switch (Kind) { |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 162 | case CondCode: |
| 163 | CC = o.CC; |
| 164 | break; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 165 | case Token: |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 166 | Tok = o.Tok; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 167 | break; |
| 168 | case Register: |
| 169 | Reg = o.Reg; |
| 170 | break; |
| 171 | case Immediate: |
| 172 | Imm = o.Imm; |
| 173 | break; |
| 174 | case Memory: |
| 175 | Mem = o.Mem; |
| 176 | break; |
| 177 | } |
| 178 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 179 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 180 | /// getStartLoc - Get the location of the first token of this operand. |
| 181 | SMLoc getStartLoc() const { return StartLoc; } |
| 182 | /// getEndLoc - Get the location of the last token of this operand. |
| 183 | SMLoc getEndLoc() const { return EndLoc; } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 184 | |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 185 | ARMCC::CondCodes getCondCode() const { |
| 186 | assert(Kind == CondCode && "Invalid access!"); |
| 187 | return CC.Val; |
| 188 | } |
| 189 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 190 | StringRef getToken() const { |
| 191 | assert(Kind == Token && "Invalid access!"); |
| 192 | return StringRef(Tok.Data, Tok.Length); |
| 193 | } |
| 194 | |
| 195 | unsigned getReg() const { |
| 196 | assert(Kind == Register && "Invalid access!"); |
| 197 | return Reg.RegNum; |
| 198 | } |
| 199 | |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 200 | const MCExpr *getImm() const { |
| 201 | assert(Kind == Immediate && "Invalid access!"); |
| 202 | return Imm.Val; |
| 203 | } |
| 204 | |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 205 | bool isCondCode() const { return Kind == CondCode; } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 206 | bool isImm() const { return Kind == Immediate; } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 207 | bool isReg() const { return Kind == Register; } |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 208 | bool isToken() const { return Kind == Token; } |
| 209 | bool isMemory() const { return Kind == Memory; } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 210 | |
| 211 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 212 | // Add as immediates when possible. Null MCExpr = 0. |
| 213 | if (Expr == 0) |
| 214 | Inst.addOperand(MCOperand::CreateImm(0)); |
| 215 | else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 216 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 217 | else |
| 218 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
| 219 | } |
| 220 | |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 221 | void addCondCodeOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 222 | assert(N == 2 && "Invalid number of operands!"); |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 223 | Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode()))); |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 224 | // FIXME: What belongs here? |
| 225 | Inst.addOperand(MCOperand::CreateReg(0)); |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 228 | void addRegOperands(MCInst &Inst, unsigned N) const { |
| 229 | assert(N == 1 && "Invalid number of operands!"); |
| 230 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 231 | } |
| 232 | |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 233 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 234 | assert(N == 1 && "Invalid number of operands!"); |
| 235 | addExpr(Inst, getImm()); |
| 236 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 237 | |
| 238 | |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 239 | bool isMemMode5() const { |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 240 | if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted || |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 241 | Mem.Writeback || Mem.Negative) |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 242 | return false; |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 243 | // If there is an offset expression, make sure it's valid. |
| 244 | if (!Mem.Offset) |
| 245 | return true; |
| 246 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset); |
| 247 | if (!CE) |
| 248 | return false; |
| 249 | // The offset must be a multiple of 4 in the range 0-1020. |
| 250 | int64_t Value = CE->getValue(); |
| 251 | return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020); |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 252 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 254 | void addMemMode5Operands(MCInst &Inst, unsigned N) const { |
| 255 | assert(N == 2 && isMemMode5() && "Invalid number of operands!"); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 256 | |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 257 | Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum)); |
| 258 | assert(!Mem.OffsetIsReg && "invalid mode 5 operand"); |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 259 | // FIXME: #-0 is encoded differently than #0. Does the parser preserve |
| 260 | // the difference? |
| 261 | if (Mem.Offset) { |
| 262 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset); |
| 263 | assert (CE && "non-constant mode 5 offset operand!"); |
| 264 | // The MCInst offset operand doesn't include the low two bits (like |
| 265 | // the instruction encoding). |
| 266 | Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4)); |
| 267 | } else |
| 268 | Inst.addOperand(MCOperand::CreateImm(0)); |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 269 | } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 270 | |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 271 | virtual void dump(raw_ostream &OS) const; |
Daniel Dunbar | b3cb696 | 2010-08-11 06:37:04 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 273 | static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) { |
| 274 | ARMOperand *Op = new ARMOperand(CondCode); |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 275 | Op->CC.Val = CC; |
| 276 | Op->StartLoc = S; |
| 277 | Op->EndLoc = S; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 278 | return Op; |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 281 | static ARMOperand *CreateToken(StringRef Str, SMLoc S) { |
| 282 | ARMOperand *Op = new ARMOperand(Token); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 283 | Op->Tok.Data = Str.data(); |
| 284 | Op->Tok.Length = Str.size(); |
| 285 | Op->StartLoc = S; |
| 286 | Op->EndLoc = S; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 287 | return Op; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 290 | static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S, |
| 291 | SMLoc E) { |
| 292 | ARMOperand *Op = new ARMOperand(Register); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 293 | Op->Reg.RegNum = RegNum; |
| 294 | Op->Reg.Writeback = Writeback; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 295 | Op->StartLoc = S; |
| 296 | Op->EndLoc = E; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 297 | return Op; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 300 | static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) { |
| 301 | ARMOperand *Op = new ARMOperand(Immediate); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 302 | Op->Imm.Val = Val; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 303 | Op->StartLoc = S; |
| 304 | Op->EndLoc = E; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 305 | return Op; |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 308 | static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg, |
| 309 | const MCExpr *Offset, unsigned OffsetRegNum, |
| 310 | bool OffsetRegShifted, enum ShiftType ShiftType, |
| 311 | const MCExpr *ShiftAmount, bool Preindexed, |
| 312 | bool Postindexed, bool Negative, bool Writeback, |
| 313 | SMLoc S, SMLoc E) { |
| 314 | ARMOperand *Op = new ARMOperand(Memory); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 315 | Op->Mem.BaseRegNum = BaseRegNum; |
| 316 | Op->Mem.OffsetIsReg = OffsetIsReg; |
| 317 | Op->Mem.Offset = Offset; |
| 318 | Op->Mem.OffsetRegNum = OffsetRegNum; |
| 319 | Op->Mem.OffsetRegShifted = OffsetRegShifted; |
| 320 | Op->Mem.ShiftType = ShiftType; |
| 321 | Op->Mem.ShiftAmount = ShiftAmount; |
| 322 | Op->Mem.Preindexed = Preindexed; |
| 323 | Op->Mem.Postindexed = Postindexed; |
| 324 | Op->Mem.Negative = Negative; |
| 325 | Op->Mem.Writeback = Writeback; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 326 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 327 | Op->StartLoc = S; |
| 328 | Op->EndLoc = E; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 329 | return Op; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 330 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 332 | private: |
| 333 | ARMOperand(KindTy K) : Kind(K) {} |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 334 | }; |
| 335 | |
| 336 | } // end anonymous namespace. |
| 337 | |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 338 | void ARMOperand::dump(raw_ostream &OS) const { |
| 339 | switch (Kind) { |
| 340 | case CondCode: |
| 341 | OS << ARMCondCodeToString(getCondCode()); |
| 342 | break; |
| 343 | case Immediate: |
| 344 | getImm()->print(OS); |
| 345 | break; |
| 346 | case Memory: |
| 347 | OS << "<memory>"; |
| 348 | break; |
| 349 | case Register: |
| 350 | OS << "<register " << getReg() << ">"; |
| 351 | break; |
| 352 | case Token: |
| 353 | OS << "'" << getToken() << "'"; |
| 354 | break; |
| 355 | } |
| 356 | } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 357 | |
| 358 | /// @name Auto-generated Match Functions |
| 359 | /// { |
| 360 | |
| 361 | static unsigned MatchRegisterName(StringRef Name); |
| 362 | |
| 363 | /// } |
| 364 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 365 | /// Try to parse a register name. The token must be an Identifier when called, |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 366 | /// and if it is a register name the token is eaten and the register number is |
| 367 | /// returned. Otherwise return -1. |
| 368 | /// |
| 369 | int ARMAsmParser::TryParseRegister() { |
| 370 | const AsmToken &Tok = Parser.getTok(); |
| 371 | assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier"); |
| 372 | |
| 373 | // FIXME: Validate register for the current architecture; we have to do |
| 374 | // validation later, so maybe there is no need for this here. |
| 375 | int RegNum = MatchRegisterName(Tok.getString()); |
| 376 | if (RegNum == -1) |
| 377 | return -1; |
| 378 | Parser.Lex(); // Eat identifier token. |
| 379 | return RegNum; |
| 380 | } |
| 381 | |
| 382 | |
| 383 | /// Try to parse a register name. The token must be an Identifier when called, |
| 384 | /// and if it is a register name the token is eaten and the register number is |
| 385 | /// returned. Otherwise return -1. |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 386 | /// |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 387 | /// TODO this is likely to change to allow different register types and or to |
| 388 | /// parse for a specific register type. |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 389 | ARMOperand *ARMAsmParser::TryParseRegisterWithWriteBack() { |
| 390 | SMLoc S = Parser.getTok().getLoc(); |
| 391 | int RegNo = TryParseRegister(); |
| 392 | if (RegNo == -1) return 0; |
| 393 | |
| 394 | SMLoc E = Parser.getTok().getLoc(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 395 | |
Kevin Enderby | 99e6d4e | 2009-10-07 18:01:35 +0000 | [diff] [blame] | 396 | bool Writeback = false; |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 397 | const AsmToken &ExclaimTok = Parser.getTok(); |
| 398 | if (ExclaimTok.is(AsmToken::Exclaim)) { |
| 399 | E = ExclaimTok.getLoc(); |
| 400 | Writeback = true; |
| 401 | Parser.Lex(); // Eat exclaim token |
Kevin Enderby | 99e6d4e | 2009-10-07 18:01:35 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 404 | return ARMOperand::CreateReg(RegNo, Writeback, S, E); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 407 | /// Parse a register list, return it if successful else return null. The first |
| 408 | /// token must be a '{' when called. |
| 409 | ARMOperand *ARMAsmParser::ParseRegisterList() { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 410 | SMLoc S, E; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 411 | assert(Parser.getTok().is(AsmToken::LCurly) && |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 412 | "Token is not an Left Curly Brace"); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 413 | S = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 414 | Parser.Lex(); // Eat left curly brace token. |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 415 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 416 | const AsmToken &RegTok = Parser.getTok(); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 417 | SMLoc RegLoc = RegTok.getLoc(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 418 | if (RegTok.isNot(AsmToken::Identifier)) { |
| 419 | Error(RegLoc, "register expected"); |
| 420 | return 0; |
| 421 | } |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 422 | int RegNum = MatchRegisterName(RegTok.getString()); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 423 | if (RegNum == -1) { |
| 424 | Error(RegLoc, "register expected"); |
| 425 | return 0; |
| 426 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 427 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 428 | Parser.Lex(); // Eat identifier token. |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 429 | unsigned RegList = 1 << RegNum; |
| 430 | |
| 431 | int HighRegNum = RegNum; |
| 432 | // TODO ranges like "{Rn-Rm}" |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 433 | while (Parser.getTok().is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 434 | Parser.Lex(); // Eat comma token. |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 435 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 436 | const AsmToken &RegTok = Parser.getTok(); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 437 | SMLoc RegLoc = RegTok.getLoc(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 438 | if (RegTok.isNot(AsmToken::Identifier)) { |
| 439 | Error(RegLoc, "register expected"); |
| 440 | return 0; |
| 441 | } |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 442 | int RegNum = MatchRegisterName(RegTok.getString()); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 443 | if (RegNum == -1) { |
| 444 | Error(RegLoc, "register expected"); |
| 445 | return 0; |
| 446 | } |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 447 | |
| 448 | if (RegList & (1 << RegNum)) |
| 449 | Warning(RegLoc, "register duplicated in register list"); |
| 450 | else if (RegNum <= HighRegNum) |
| 451 | Warning(RegLoc, "register not in ascending order in register list"); |
| 452 | RegList |= 1 << RegNum; |
| 453 | HighRegNum = RegNum; |
| 454 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 455 | Parser.Lex(); // Eat identifier token. |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 456 | } |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 457 | const AsmToken &RCurlyTok = Parser.getTok(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 458 | if (RCurlyTok.isNot(AsmToken::RCurly)) { |
| 459 | Error(RCurlyTok.getLoc(), "'}' expected"); |
| 460 | return 0; |
| 461 | } |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 462 | E = RCurlyTok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 463 | Parser.Lex(); // Eat left curly brace token. |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 464 | |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 465 | // FIXME: Need to return an operand! |
| 466 | Error(E, "FIXME: register list parsing not implemented"); |
| 467 | return 0; |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 468 | } |
| 469 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 470 | /// Parse an arm memory expression, return false if successful else return true |
| 471 | /// or an error. The first token must be a '[' when called. |
| 472 | /// TODO Only preindexing and postindexing addressing are started, unindexed |
| 473 | /// with option, etc are still to do. |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 474 | ARMOperand *ARMAsmParser::ParseMemory() { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 475 | SMLoc S, E; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 476 | assert(Parser.getTok().is(AsmToken::LBrac) && |
Kevin Enderby | 6bd266e | 2009-10-12 22:51:49 +0000 | [diff] [blame] | 477 | "Token is not an Left Bracket"); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 478 | S = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 479 | Parser.Lex(); // Eat left bracket token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 480 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 481 | const AsmToken &BaseRegTok = Parser.getTok(); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 482 | if (BaseRegTok.isNot(AsmToken::Identifier)) { |
| 483 | Error(BaseRegTok.getLoc(), "register expected"); |
| 484 | return 0; |
| 485 | } |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 486 | int BaseRegNum = TryParseRegister(); |
| 487 | if (BaseRegNum == -1) { |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 488 | Error(BaseRegTok.getLoc(), "register expected"); |
| 489 | return 0; |
| 490 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 491 | |
| 492 | bool Preindexed = false; |
| 493 | bool Postindexed = false; |
| 494 | bool OffsetIsReg = false; |
| 495 | bool Negative = false; |
| 496 | bool Writeback = false; |
| 497 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 498 | // First look for preindexed address forms, that is after the "[Rn" we now |
| 499 | // have to see if the next token is a comma. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 500 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 501 | if (Tok.is(AsmToken::Comma)) { |
| 502 | Preindexed = true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 503 | Parser.Lex(); // Eat comma token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 504 | int OffsetRegNum; |
| 505 | bool OffsetRegShifted; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 506 | enum ShiftType ShiftType; |
| 507 | const MCExpr *ShiftAmount; |
| 508 | const MCExpr *Offset; |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 509 | if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount, |
| 510 | Offset, OffsetIsReg, OffsetRegNum, E)) |
| 511 | return 0; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 512 | const AsmToken &RBracTok = Parser.getTok(); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 513 | if (RBracTok.isNot(AsmToken::RBrac)) { |
| 514 | Error(RBracTok.getLoc(), "']' expected"); |
| 515 | return 0; |
| 516 | } |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 517 | E = RBracTok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 518 | Parser.Lex(); // Eat right bracket token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 519 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 520 | const AsmToken &ExclaimTok = Parser.getTok(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 521 | if (ExclaimTok.is(AsmToken::Exclaim)) { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 522 | E = ExclaimTok.getLoc(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 523 | Writeback = true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 524 | Parser.Lex(); // Eat exclaim token |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 525 | } |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 526 | return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum, |
| 527 | OffsetRegShifted, ShiftType, ShiftAmount, |
| 528 | Preindexed, Postindexed, Negative, Writeback, |
| 529 | S, E); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 530 | } |
| 531 | // The "[Rn" we have so far was not followed by a comma. |
| 532 | else if (Tok.is(AsmToken::RBrac)) { |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 533 | // If there's anything other than the right brace, this is a post indexing |
| 534 | // addressing form. |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 535 | E = Tok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 536 | Parser.Lex(); // Eat right bracket token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 537 | |
Kevin Enderby | e2a98dd | 2009-10-15 21:42:45 +0000 | [diff] [blame] | 538 | int OffsetRegNum = 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 539 | bool OffsetRegShifted = false; |
| 540 | enum ShiftType ShiftType; |
| 541 | const MCExpr *ShiftAmount; |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 542 | const MCExpr *Offset = 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 543 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 544 | const AsmToken &NextTok = Parser.getTok(); |
Kevin Enderby | e2a98dd | 2009-10-15 21:42:45 +0000 | [diff] [blame] | 545 | if (NextTok.isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 546 | Postindexed = true; |
| 547 | Writeback = true; |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 548 | if (NextTok.isNot(AsmToken::Comma)) { |
| 549 | Error(NextTok.getLoc(), "',' expected"); |
| 550 | return 0; |
| 551 | } |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 552 | Parser.Lex(); // Eat comma token. |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 553 | if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 554 | ShiftAmount, Offset, OffsetIsReg, OffsetRegNum, |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 555 | E)) |
| 556 | return 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 557 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 559 | return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum, |
| 560 | OffsetRegShifted, ShiftType, ShiftAmount, |
| 561 | Preindexed, Postindexed, Negative, Writeback, |
| 562 | S, E); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 565 | return 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 568 | /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn]," |
| 569 | /// we will parse the following (were +/- means that a plus or minus is |
| 570 | /// optional): |
| 571 | /// +/-Rm |
| 572 | /// +/-Rm, shift |
| 573 | /// #offset |
| 574 | /// we return false on success or an error otherwise. |
| 575 | bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 576 | bool &OffsetRegShifted, |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 577 | enum ShiftType &ShiftType, |
| 578 | const MCExpr *&ShiftAmount, |
| 579 | const MCExpr *&Offset, |
| 580 | bool &OffsetIsReg, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 581 | int &OffsetRegNum, |
| 582 | SMLoc &E) { |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 583 | Negative = false; |
| 584 | OffsetRegShifted = false; |
| 585 | OffsetIsReg = false; |
| 586 | OffsetRegNum = -1; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 587 | const AsmToken &NextTok = Parser.getTok(); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 588 | E = NextTok.getLoc(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 589 | if (NextTok.is(AsmToken::Plus)) |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 590 | Parser.Lex(); // Eat plus token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 591 | else if (NextTok.is(AsmToken::Minus)) { |
| 592 | Negative = true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 593 | Parser.Lex(); // Eat minus token |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 594 | } |
| 595 | // See if there is a register following the "[Rn," or "[Rn]," we have so far. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 596 | const AsmToken &OffsetRegTok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 597 | if (OffsetRegTok.is(AsmToken::Identifier)) { |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 598 | SMLoc CurLoc = OffsetRegTok.getLoc(); |
| 599 | OffsetRegNum = TryParseRegister(); |
| 600 | if (OffsetRegNum != -1) { |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 601 | OffsetIsReg = true; |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 602 | E = CurLoc; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 603 | } |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 604 | } |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 605 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 606 | // If we parsed a register as the offset then their can be a shift after that |
| 607 | if (OffsetRegNum != -1) { |
| 608 | // Look for a comma then a shift |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 609 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 610 | if (Tok.is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 611 | Parser.Lex(); // Eat comma token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 612 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 613 | const AsmToken &Tok = Parser.getTok(); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 614 | if (ParseShift(ShiftType, ShiftAmount, E)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 615 | return Error(Tok.getLoc(), "shift expected"); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 616 | OffsetRegShifted = true; |
| 617 | } |
| 618 | } |
| 619 | else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm" |
| 620 | // Look for #offset following the "[Rn," or "[Rn]," |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 621 | const AsmToken &HashTok = Parser.getTok(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 622 | if (HashTok.isNot(AsmToken::Hash)) |
| 623 | return Error(HashTok.getLoc(), "'#' expected"); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 624 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 625 | Parser.Lex(); // Eat hash token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 626 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 627 | if (getParser().ParseExpression(Offset)) |
| 628 | return true; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 629 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 630 | } |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | |
| 634 | /// ParseShift as one of these two: |
| 635 | /// ( lsl | lsr | asr | ror ) , # shift_amount |
| 636 | /// rrx |
| 637 | /// and returns true if it parses a shift otherwise it returns false. |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 638 | bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 639 | SMLoc &E) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 640 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 641 | if (Tok.isNot(AsmToken::Identifier)) |
| 642 | return true; |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 643 | StringRef ShiftName = Tok.getString(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 644 | if (ShiftName == "lsl" || ShiftName == "LSL") |
| 645 | St = Lsl; |
| 646 | else if (ShiftName == "lsr" || ShiftName == "LSR") |
| 647 | St = Lsr; |
| 648 | else if (ShiftName == "asr" || ShiftName == "ASR") |
| 649 | St = Asr; |
| 650 | else if (ShiftName == "ror" || ShiftName == "ROR") |
| 651 | St = Ror; |
| 652 | else if (ShiftName == "rrx" || ShiftName == "RRX") |
| 653 | St = Rrx; |
| 654 | else |
| 655 | return true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 656 | Parser.Lex(); // Eat shift type token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 657 | |
| 658 | // Rrx stands alone. |
| 659 | if (St == Rrx) |
| 660 | return false; |
| 661 | |
| 662 | // Otherwise, there must be a '#' and a shift amount. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 663 | const AsmToken &HashTok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 664 | if (HashTok.isNot(AsmToken::Hash)) |
| 665 | return Error(HashTok.getLoc(), "'#' expected"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 666 | Parser.Lex(); // Eat hash token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 667 | |
| 668 | if (getParser().ParseExpression(ShiftAmount)) |
| 669 | return true; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 670 | |
| 671 | return false; |
| 672 | } |
| 673 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 674 | /// Parse a arm instruction operand. For now this parses the operand regardless |
| 675 | /// of the mnemonic. |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 676 | ARMOperand *ARMAsmParser::ParseOperand() { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 677 | SMLoc S, E; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 678 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 679 | switch (getLexer().getKind()) { |
| 680 | case AsmToken::Identifier: |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 681 | if (ARMOperand *Op = TryParseRegisterWithWriteBack()) |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 682 | return Op; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 683 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 684 | // This was not a register so parse other operands that start with an |
| 685 | // identifier (like labels) as expressions and create them as immediates. |
| 686 | const MCExpr *IdVal; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 687 | S = Parser.getTok().getLoc(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 688 | if (getParser().ParseExpression(IdVal)) |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 689 | return 0; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 690 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 691 | return ARMOperand::CreateImm(IdVal, S, E); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 692 | case AsmToken::LBrac: |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 693 | return ParseMemory(); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 694 | case AsmToken::LCurly: |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 695 | return ParseRegisterList(); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 696 | case AsmToken::Hash: |
Kevin Enderby | 079469f | 2009-10-13 23:33:38 +0000 | [diff] [blame] | 697 | // #42 -> immediate. |
| 698 | // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 699 | S = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 700 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 701 | const MCExpr *ImmVal; |
| 702 | if (getParser().ParseExpression(ImmVal)) |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 703 | return 0; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 704 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 705 | return ARMOperand::CreateImm(ImmVal, S, E); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 706 | default: |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 707 | Error(Parser.getTok().getLoc(), "unexpected token in operand"); |
| 708 | return 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 712 | /// Parse an arm instruction mnemonic followed by its operands. |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 713 | bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 714 | SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Daniel Dunbar | 5747b13 | 2010-08-11 06:37:16 +0000 | [diff] [blame] | 715 | // Create the leading tokens for the mnemonic, split by '.' characters. |
| 716 | size_t Start = 0, Next = Name.find('.'); |
| 717 | StringRef Head = Name.slice(Start, Next); |
| 718 | |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 719 | // Determine the predicate, if any. |
| 720 | // |
| 721 | // FIXME: We need a way to check whether a prefix supports predication, |
| 722 | // otherwise we will end up with an ambiguity for instructions that happen to |
| 723 | // end with a predicate name. |
Jim Grosbach | 3df518e | 2010-10-29 21:56:51 +0000 | [diff] [blame] | 724 | // FIXME: Likewise, some arithmetic instructions have an 's' prefix which |
| 725 | // indicates to update the condition codes. Those instructions have an |
| 726 | // additional immediate operand which encodes the prefix as reg0 or CPSR. |
| 727 | // Just checking for a suffix of 's' definitely creates ambiguities; e.g, |
| 728 | // the SMMLS instruction. |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 729 | unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2)) |
| 730 | .Case("eq", ARMCC::EQ) |
| 731 | .Case("ne", ARMCC::NE) |
| 732 | .Case("hs", ARMCC::HS) |
| 733 | .Case("lo", ARMCC::LO) |
| 734 | .Case("mi", ARMCC::MI) |
| 735 | .Case("pl", ARMCC::PL) |
| 736 | .Case("vs", ARMCC::VS) |
| 737 | .Case("vc", ARMCC::VC) |
| 738 | .Case("hi", ARMCC::HI) |
| 739 | .Case("ls", ARMCC::LS) |
| 740 | .Case("ge", ARMCC::GE) |
| 741 | .Case("lt", ARMCC::LT) |
| 742 | .Case("gt", ARMCC::GT) |
| 743 | .Case("le", ARMCC::LE) |
| 744 | .Case("al", ARMCC::AL) |
| 745 | .Default(~0U); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 746 | |
Chris Lattner | dba34d8 | 2010-10-30 04:35:59 +0000 | [diff] [blame] | 747 | if (CC == ~0U || |
| 748 | (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) { |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 749 | CC = ARMCC::AL; |
Chris Lattner | dba34d8 | 2010-10-30 04:35:59 +0000 | [diff] [blame] | 750 | } else { |
| 751 | Head = Head.slice(0, Head.size() - 2); |
Bill Wendling | 52925b6 | 2010-10-29 23:50:21 +0000 | [diff] [blame] | 752 | } |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 754 | Operands.push_back(ARMOperand::CreateToken(Head, NameLoc)); |
| 755 | Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc)); |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 756 | |
| 757 | // Add the remaining tokens in the mnemonic. |
Daniel Dunbar | 5747b13 | 2010-08-11 06:37:16 +0000 | [diff] [blame] | 758 | while (Next != StringRef::npos) { |
| 759 | Start = Next; |
| 760 | Next = Name.find('.', Start + 1); |
| 761 | Head = Name.slice(Start, Next); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 762 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 763 | Operands.push_back(ARMOperand::CreateToken(Head, NameLoc)); |
Daniel Dunbar | 5747b13 | 2010-08-11 06:37:16 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // Read the remaining operands. |
| 767 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 768 | // Read the first operand. |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 769 | if (ARMOperand *Op = ParseOperand()) |
| 770 | Operands.push_back(Op); |
| 771 | else { |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 772 | Parser.EatToEndOfStatement(); |
| 773 | return true; |
| 774 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 775 | |
| 776 | while (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 777 | Parser.Lex(); // Eat the comma. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 778 | |
| 779 | // Parse and remember the operand. |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 780 | if (ARMOperand *Op = ParseOperand()) |
| 781 | Operands.push_back(Op); |
| 782 | else { |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 783 | Parser.EatToEndOfStatement(); |
| 784 | return true; |
| 785 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 786 | } |
| 787 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 788 | |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 789 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 790 | Parser.EatToEndOfStatement(); |
Chris Lattner | 34e5314 | 2010-09-08 05:10:46 +0000 | [diff] [blame] | 791 | return TokError("unexpected token in argument list"); |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 792 | } |
Chris Lattner | 34e5314 | 2010-09-08 05:10:46 +0000 | [diff] [blame] | 793 | Parser.Lex(); // Consume the EndOfStatement |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 794 | return false; |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 795 | } |
| 796 | |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 797 | bool ARMAsmParser:: |
| 798 | MatchAndEmitInstruction(SMLoc IDLoc, |
| 799 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 800 | MCStreamer &Out) { |
| 801 | MCInst Inst; |
| 802 | unsigned ErrorInfo; |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 803 | switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) { |
| 804 | case Match_Success: |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 805 | Out.EmitInstruction(Inst); |
| 806 | return false; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 807 | |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 808 | case Match_MissingFeature: |
| 809 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 810 | return true; |
| 811 | case Match_InvalidOperand: { |
| 812 | SMLoc ErrorLoc = IDLoc; |
| 813 | if (ErrorInfo != ~0U) { |
| 814 | if (ErrorInfo >= Operands.size()) |
| 815 | return Error(IDLoc, "too few operands for instruction"); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 816 | |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 817 | ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc(); |
| 818 | if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc; |
| 819 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 820 | |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 821 | return Error(ErrorLoc, "invalid operand for instruction"); |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 822 | } |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 823 | case Match_MnemonicFail: |
| 824 | return Error(IDLoc, "unrecognized instruction mnemonic"); |
| 825 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 826 | |
Eric Christopher | c223e2b | 2010-10-29 09:26:59 +0000 | [diff] [blame] | 827 | llvm_unreachable("Implement any new match types added!"); |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | |
| 831 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 832 | /// ParseDirective parses the arm specific directives |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 833 | bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 834 | StringRef IDVal = DirectiveID.getIdentifier(); |
| 835 | if (IDVal == ".word") |
| 836 | return ParseDirectiveWord(4, DirectiveID.getLoc()); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 837 | else if (IDVal == ".thumb") |
| 838 | return ParseDirectiveThumb(DirectiveID.getLoc()); |
| 839 | else if (IDVal == ".thumb_func") |
| 840 | return ParseDirectiveThumbFunc(DirectiveID.getLoc()); |
| 841 | else if (IDVal == ".code") |
| 842 | return ParseDirectiveCode(DirectiveID.getLoc()); |
| 843 | else if (IDVal == ".syntax") |
| 844 | return ParseDirectiveSyntax(DirectiveID.getLoc()); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 845 | return true; |
| 846 | } |
| 847 | |
| 848 | /// ParseDirectiveWord |
| 849 | /// ::= .word [ expression (, expression)* ] |
| 850 | bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { |
| 851 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 852 | for (;;) { |
| 853 | const MCExpr *Value; |
| 854 | if (getParser().ParseExpression(Value)) |
| 855 | return true; |
| 856 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 857 | getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 858 | |
| 859 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 860 | break; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 861 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 862 | // FIXME: Improve diagnostic. |
| 863 | if (getLexer().isNot(AsmToken::Comma)) |
| 864 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 865 | Parser.Lex(); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 869 | Parser.Lex(); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 870 | return false; |
| 871 | } |
| 872 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 873 | /// ParseDirectiveThumb |
| 874 | /// ::= .thumb |
| 875 | bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) { |
| 876 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 877 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 878 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 879 | |
| 880 | // TODO: set thumb mode |
| 881 | // TODO: tell the MC streamer the mode |
| 882 | // getParser().getStreamer().Emit???(); |
| 883 | return false; |
| 884 | } |
| 885 | |
| 886 | /// ParseDirectiveThumbFunc |
| 887 | /// ::= .thumbfunc symbol_name |
| 888 | bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 889 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 890 | if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) |
| 891 | return Error(L, "unexpected token in .syntax directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 892 | Parser.Lex(); // Consume the identifier token. |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 893 | |
| 894 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 895 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 896 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 897 | |
| 898 | // TODO: mark symbol as a thumb symbol |
| 899 | // getParser().getStreamer().Emit???(); |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | /// ParseDirectiveSyntax |
| 904 | /// ::= .syntax unified | divided |
| 905 | bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 906 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 907 | if (Tok.isNot(AsmToken::Identifier)) |
| 908 | return Error(L, "unexpected token in .syntax directive"); |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 909 | StringRef Mode = Tok.getString(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 910 | if (Mode == "unified" || Mode == "UNIFIED") |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 911 | Parser.Lex(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 912 | else if (Mode == "divided" || Mode == "DIVIDED") |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 913 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 914 | else |
| 915 | return Error(L, "unrecognized syntax mode in .syntax directive"); |
| 916 | |
| 917 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 918 | return Error(Parser.getTok().getLoc(), "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 919 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 920 | |
| 921 | // TODO tell the MC streamer the mode |
| 922 | // getParser().getStreamer().Emit???(); |
| 923 | return false; |
| 924 | } |
| 925 | |
| 926 | /// ParseDirectiveCode |
| 927 | /// ::= .code 16 | 32 |
| 928 | bool ARMAsmParser::ParseDirectiveCode(SMLoc L) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 929 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 930 | if (Tok.isNot(AsmToken::Integer)) |
| 931 | return Error(L, "unexpected token in .code directive"); |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 932 | int64_t Val = Parser.getTok().getIntVal(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 933 | if (Val == 16) |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 934 | Parser.Lex(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 935 | else if (Val == 32) |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 936 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 937 | else |
| 938 | return Error(L, "invalid operand to .code directive"); |
| 939 | |
| 940 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 941 | return Error(Parser.getTok().getLoc(), "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 942 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 943 | |
| 944 | // TODO tell the MC streamer the mode |
| 945 | // getParser().getStreamer().Emit???(); |
| 946 | return false; |
| 947 | } |
| 948 | |
Sean Callanan | 90b7097 | 2010-04-07 20:29:34 +0000 | [diff] [blame] | 949 | extern "C" void LLVMInitializeARMAsmLexer(); |
| 950 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 951 | /// Force static initialization. |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 952 | extern "C" void LLVMInitializeARMAsmParser() { |
| 953 | RegisterAsmParser<ARMAsmParser> X(TheARMTarget); |
| 954 | RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget); |
Sean Callanan | 90b7097 | 2010-04-07 20:29:34 +0000 | [diff] [blame] | 955 | LLVMInitializeARMAsmLexer(); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 956 | } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 957 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 958 | #define GET_REGISTER_MATCHER |
| 959 | #define GET_MATCHER_IMPLEMENTATION |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 960 | #include "ARMGenAsmMatcher.inc" |