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