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