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