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