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