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