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