blob: 129c09d2bd463b47cb182c2203826bebc16671b5 [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000011#include "llvm/MC/MCParser/MCAsmLexer.h"
12#include "llvm/MC/MCParser/MCAsmParser.h"
13#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000014#include "llvm/MC/MCStreamer.h"
15#include "llvm/MC/MCExpr.h"
16#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000017#include "llvm/Target/TargetRegistry.h"
18#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000019#include "llvm/Support/Compiler.h"
20#include "llvm/Support/SourceMgr.h"
Sean Callanan76264762010-04-02 22:27:05 +000021#include "llvm/ADT/OwningPtr.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000024using namespace llvm;
25
26namespace {
27struct ARMOperand;
28
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000029// The shift types for register controlled shifts in arm memory addressing
30enum ShiftType {
31 Lsl,
32 Lsr,
33 Asr,
34 Ror,
35 Rrx
36};
37
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000038class ARMAsmParser : public TargetAsmParser {
39 MCAsmParser &Parser;
40
41private:
42 MCAsmParser &getParser() const { return Parser; }
43
44 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
45
46 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
47
48 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
49
Sean Callanan76264762010-04-02 22:27:05 +000050 bool MaybeParseRegister(OwningPtr<ARMOperand> &Op, bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000051
Sean Callanan76264762010-04-02 22:27:05 +000052 bool ParseRegisterList(OwningPtr<ARMOperand> &Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +000053
Sean Callanan76264762010-04-02 22:27:05 +000054 bool ParseMemory(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000055
Kevin Enderby9c41fa82009-10-30 22:55:57 +000056 bool ParseMemoryOffsetReg(bool &Negative,
57 bool &OffsetRegShifted,
58 enum ShiftType &ShiftType,
59 const MCExpr *&ShiftAmount,
60 const MCExpr *&Offset,
61 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000062 int &OffsetRegNum,
63 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000064
Sean Callanan76264762010-04-02 22:27:05 +000065 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000066
Sean Callanan76264762010-04-02 22:27:05 +000067 bool ParseOperand(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000068
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000069 bool ParseDirectiveWord(unsigned Size, SMLoc L);
70
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveThumb(SMLoc L);
72
73 bool ParseDirectiveThumbFunc(SMLoc L);
74
75 bool ParseDirectiveCode(SMLoc L);
76
77 bool ParseDirectiveSyntax(SMLoc L);
78
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000079 // TODO - For now hacked versions of the next two are in here in this file to
80 // allow some parser testing until the table gen versions are implemented.
81
82 /// @name Auto-generated Match Functions
83 /// {
Chris Lattner98986712010-01-14 22:21:20 +000084 bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000085 MCInst &Inst);
86
Kevin Enderbyd7894f12009-10-09 21:12:28 +000087 /// MatchRegisterName - Match the given string to a register name and return
88 /// its register number, or -1 if there is no match. To allow return values
89 /// to be used directly in register lists, arm registers have values between
90 /// 0 and 15.
91 int MatchRegisterName(const StringRef &Name);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000092
93 /// }
94
95
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000096public:
97 ARMAsmParser(const Target &T, MCAsmParser &_Parser)
98 : TargetAsmParser(T), Parser(_Parser) {}
99
Chris Lattnerf007e852010-01-14 21:32:45 +0000100 virtual bool ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000101 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102
103 virtual bool ParseDirective(AsmToken DirectiveID);
104};
105
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106/// ARMOperand - Instances of this class represent a parsed ARM machine
107/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000108struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000109private:
110 ARMOperand() {}
111public:
112 enum KindTy {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000113 Token,
114 Register,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000115 Immediate,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000116 Memory
117 } Kind;
118
Sean Callanan76264762010-04-02 22:27:05 +0000119 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000120
121 union {
122 struct {
123 const char *Data;
124 unsigned Length;
125 } Tok;
126
127 struct {
128 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000129 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 } Reg;
131
Kevin Enderbycfe07242009-10-13 22:19:02 +0000132 struct {
133 const MCExpr *Val;
134 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000135
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000136 // This is for all forms of ARM address expressions
137 struct {
138 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000140 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000142 enum ShiftType ShiftType; // used when OffsetRegShifted is true
143 unsigned
144 OffsetRegShifted : 1, // only used when OffsetIsReg is true
145 Preindexed : 1,
146 Postindexed : 1,
147 OffsetIsReg : 1,
148 Negative : 1, // only used when OffsetIsReg is true
149 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 } Mem;
151
152 };
Sean Callanan76264762010-04-02 22:27:05 +0000153
154 ARMOperand(KindTy K, SMLoc S, SMLoc E)
155 : Kind(K), StartLoc(S), EndLoc(E) {}
156
157 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
158 Kind = o.Kind;
159 StartLoc = o.StartLoc;
160 EndLoc = o.EndLoc;
161 switch (Kind) {
162 case Token:
163 Tok = o.Tok;
164 break;
165 case Register:
166 Reg = o.Reg;
167 break;
168 case Immediate:
169 Imm = o.Imm;
170 break;
171 case Memory:
172 Mem = o.Mem;
173 break;
174 }
175 }
176
177 /// getStartLoc - Get the location of the first token of this operand.
178 SMLoc getStartLoc() const { return StartLoc; }
179 /// getEndLoc - Get the location of the last token of this operand.
180 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000181
182 StringRef getToken() const {
183 assert(Kind == Token && "Invalid access!");
184 return StringRef(Tok.Data, Tok.Length);
185 }
186
187 unsigned getReg() const {
188 assert(Kind == Register && "Invalid access!");
189 return Reg.RegNum;
190 }
191
Kevin Enderbycfe07242009-10-13 22:19:02 +0000192 const MCExpr *getImm() const {
193 assert(Kind == Immediate && "Invalid access!");
194 return Imm.Val;
195 }
196
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000197 bool isToken() const {return Kind == Token; }
198
199 bool isReg() const { return Kind == Register; }
200
201 void addRegOperands(MCInst &Inst, unsigned N) const {
202 assert(N == 1 && "Invalid number of operands!");
203 Inst.addOperand(MCOperand::CreateReg(getReg()));
204 }
205
Sean Callanan76264762010-04-02 22:27:05 +0000206 static void CreateToken(OwningPtr<ARMOperand> &Op, StringRef Str,
207 SMLoc S) {
208 Op.reset(new ARMOperand);
209 Op->Kind = Token;
210 Op->Tok.Data = Str.data();
211 Op->Tok.Length = Str.size();
212 Op->StartLoc = S;
213 Op->EndLoc = S;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000214 }
215
Sean Callanan76264762010-04-02 22:27:05 +0000216 static void CreateReg(OwningPtr<ARMOperand> &Op, unsigned RegNum,
217 bool Writeback, SMLoc S, SMLoc E) {
218 Op.reset(new ARMOperand);
219 Op->Kind = Register;
220 Op->Reg.RegNum = RegNum;
221 Op->Reg.Writeback = Writeback;
222
223 Op->StartLoc = S;
224 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000225 }
226
Sean Callanan76264762010-04-02 22:27:05 +0000227 static void CreateImm(OwningPtr<ARMOperand> &Op, const MCExpr *Val,
228 SMLoc S, SMLoc E) {
229 Op.reset(new ARMOperand);
230 Op->Kind = Immediate;
231 Op->Imm.Val = Val;
232
233 Op->StartLoc = S;
234 Op->EndLoc = E;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000235 }
236
Sean Callanan76264762010-04-02 22:27:05 +0000237 static void CreateMem(OwningPtr<ARMOperand> &Op,
238 unsigned BaseRegNum, bool OffsetIsReg,
239 const MCExpr *Offset, unsigned OffsetRegNum,
240 bool OffsetRegShifted, enum ShiftType ShiftType,
241 const MCExpr *ShiftAmount, bool Preindexed,
242 bool Postindexed, bool Negative, bool Writeback,
243 SMLoc S, SMLoc E) {
244 Op.reset(new ARMOperand);
245 Op->Kind = Memory;
246 Op->Mem.BaseRegNum = BaseRegNum;
247 Op->Mem.OffsetIsReg = OffsetIsReg;
248 Op->Mem.Offset = Offset;
249 Op->Mem.OffsetRegNum = OffsetRegNum;
250 Op->Mem.OffsetRegShifted = OffsetRegShifted;
251 Op->Mem.ShiftType = ShiftType;
252 Op->Mem.ShiftAmount = ShiftAmount;
253 Op->Mem.Preindexed = Preindexed;
254 Op->Mem.Postindexed = Postindexed;
255 Op->Mem.Negative = Negative;
256 Op->Mem.Writeback = Writeback;
257
258 Op->StartLoc = S;
259 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000260 }
261};
262
263} // end anonymous namespace.
264
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000265/// Try to parse a register name. The token must be an Identifier when called,
266/// and if it is a register name a Reg operand is created, the token is eaten
267/// and false is returned. Else true is returned and no token is eaten.
268/// TODO this is likely to change to allow different register types and or to
269/// parse for a specific register type.
Sean Callanan76264762010-04-02 22:27:05 +0000270bool ARMAsmParser::MaybeParseRegister
271 (OwningPtr<ARMOperand> &Op, bool ParseWriteBack) {
272 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000273 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000274 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
275
276 // FIXME: Validate register for the current architecture; we have to do
277 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000278 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000279
280 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000281 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000282 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000283
284 S = Tok.getLoc();
285
Sean Callananb9a25b72010-01-19 20:27:46 +0000286 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000287
288 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000289
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000290 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000291 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000292 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000293 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000294 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000295 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000296 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000297 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000298 }
299
Sean Callanan76264762010-04-02 22:27:05 +0000300 ARMOperand::CreateReg(Op, RegNum, Writeback, S, E);
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000301
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000302 return false;
303}
304
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000305/// Parse a register list, return false if successful else return true or an
306/// error. The first token must be a '{' when called.
Sean Callanan76264762010-04-02 22:27:05 +0000307bool ARMAsmParser::ParseRegisterList(OwningPtr<ARMOperand> &Op) {
308 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000309 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000310 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000311 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000312 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000313
Sean Callanan18b83232010-01-19 21:44:56 +0000314 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000315 SMLoc RegLoc = RegTok.getLoc();
316 if (RegTok.isNot(AsmToken::Identifier))
317 return Error(RegLoc, "register expected");
318 int RegNum = MatchRegisterName(RegTok.getString());
319 if (RegNum == -1)
320 return Error(RegLoc, "register expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000321 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000322 unsigned RegList = 1 << RegNum;
323
324 int HighRegNum = RegNum;
325 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000326 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000327 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000328
Sean Callanan18b83232010-01-19 21:44:56 +0000329 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000330 SMLoc RegLoc = RegTok.getLoc();
331 if (RegTok.isNot(AsmToken::Identifier))
332 return Error(RegLoc, "register expected");
333 int RegNum = MatchRegisterName(RegTok.getString());
334 if (RegNum == -1)
335 return Error(RegLoc, "register expected");
336
337 if (RegList & (1 << RegNum))
338 Warning(RegLoc, "register duplicated in register list");
339 else if (RegNum <= HighRegNum)
340 Warning(RegLoc, "register not in ascending order in register list");
341 RegList |= 1 << RegNum;
342 HighRegNum = RegNum;
343
Sean Callananb9a25b72010-01-19 20:27:46 +0000344 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000345 }
Sean Callanan18b83232010-01-19 21:44:56 +0000346 const AsmToken &RCurlyTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000347 if (RCurlyTok.isNot(AsmToken::RCurly))
348 return Error(RCurlyTok.getLoc(), "'}' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000349 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000350 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000351
352 return false;
353}
354
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000355/// Parse an arm memory expression, return false if successful else return true
356/// or an error. The first token must be a '[' when called.
357/// TODO Only preindexing and postindexing addressing are started, unindexed
358/// with option, etc are still to do.
Sean Callanan76264762010-04-02 22:27:05 +0000359bool ARMAsmParser::ParseMemory(OwningPtr<ARMOperand> &Op) {
360 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000361 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000362 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000363 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000364 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000365
Sean Callanan18b83232010-01-19 21:44:56 +0000366 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000367 if (BaseRegTok.isNot(AsmToken::Identifier))
368 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000369 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000370 return Error(BaseRegTok.getLoc(), "register expected");
Sean Callanan76264762010-04-02 22:27:05 +0000371 int BaseRegNum = Op->getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000372
373 bool Preindexed = false;
374 bool Postindexed = false;
375 bool OffsetIsReg = false;
376 bool Negative = false;
377 bool Writeback = false;
378
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000379 // First look for preindexed address forms, that is after the "[Rn" we now
380 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000381 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000382 if (Tok.is(AsmToken::Comma)) {
383 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000384 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000385 int OffsetRegNum;
386 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000387 enum ShiftType ShiftType;
388 const MCExpr *ShiftAmount;
389 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000390 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000391 Offset, OffsetIsReg, OffsetRegNum, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000392 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000393 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000394 if (RBracTok.isNot(AsmToken::RBrac))
395 return Error(RBracTok.getLoc(), "']' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000396 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000397 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000398
Sean Callanan18b83232010-01-19 21:44:56 +0000399 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000400 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000401 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000402 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000403 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000404 }
Sean Callanan76264762010-04-02 22:27:05 +0000405 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
406 OffsetRegShifted, ShiftType, ShiftAmount,
407 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000408 return false;
409 }
410 // The "[Rn" we have so far was not followed by a comma.
411 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000412 // This is a post indexing addressing forms, that is a ']' follows after
413 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000414 Postindexed = true;
415 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000416 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000417 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000418
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000419 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000420 bool OffsetRegShifted = false;
421 enum ShiftType ShiftType;
422 const MCExpr *ShiftAmount;
423 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000424
Sean Callanan18b83232010-01-19 21:44:56 +0000425 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000426 if (NextTok.isNot(AsmToken::EndOfStatement)) {
427 if (NextTok.isNot(AsmToken::Comma))
428 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000429 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000430 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Sean Callanan76264762010-04-02 22:27:05 +0000431 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
432 E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000433 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000434 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000435
Sean Callanan76264762010-04-02 22:27:05 +0000436 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
437 OffsetRegShifted, ShiftType, ShiftAmount,
438 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000439 return false;
440 }
441
442 return true;
443}
444
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000445/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
446/// we will parse the following (were +/- means that a plus or minus is
447/// optional):
448/// +/-Rm
449/// +/-Rm, shift
450/// #offset
451/// we return false on success or an error otherwise.
452bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000453 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000454 enum ShiftType &ShiftType,
455 const MCExpr *&ShiftAmount,
456 const MCExpr *&Offset,
457 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000458 int &OffsetRegNum,
459 SMLoc &E) {
460 OwningPtr<ARMOperand> Op;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000461 Negative = false;
462 OffsetRegShifted = false;
463 OffsetIsReg = false;
464 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000465 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000466 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000467 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000468 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000469 else if (NextTok.is(AsmToken::Minus)) {
470 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000471 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000472 }
473 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000474 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000475 if (OffsetRegTok.is(AsmToken::Identifier)) {
476 OffsetIsReg = !MaybeParseRegister(Op, false);
Sean Callanan76264762010-04-02 22:27:05 +0000477 if (OffsetIsReg) {
478 E = Op->getEndLoc();
479 OffsetRegNum = Op->getReg();
480 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000481 }
482 // If we parsed a register as the offset then their can be a shift after that
483 if (OffsetRegNum != -1) {
484 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000485 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000486 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000487 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000488
Sean Callanan18b83232010-01-19 21:44:56 +0000489 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000490 if (ParseShift(ShiftType, ShiftAmount, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000491 return Error(Tok.getLoc(), "shift expected");
492 OffsetRegShifted = true;
493 }
494 }
495 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
496 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000497 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000498 if (HashTok.isNot(AsmToken::Hash))
499 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000500
Sean Callananb9a25b72010-01-19 20:27:46 +0000501 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000502
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000503 if (getParser().ParseExpression(Offset))
504 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000505 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000506 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000507 return false;
508}
509
510/// ParseShift as one of these two:
511/// ( lsl | lsr | asr | ror ) , # shift_amount
512/// rrx
513/// and returns true if it parses a shift otherwise it returns false.
Sean Callanan76264762010-04-02 22:27:05 +0000514bool ARMAsmParser::ParseShift(ShiftType &St,
515 const MCExpr *&ShiftAmount,
516 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000517 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000518 if (Tok.isNot(AsmToken::Identifier))
519 return true;
520 const StringRef &ShiftName = Tok.getString();
521 if (ShiftName == "lsl" || ShiftName == "LSL")
522 St = Lsl;
523 else if (ShiftName == "lsr" || ShiftName == "LSR")
524 St = Lsr;
525 else if (ShiftName == "asr" || ShiftName == "ASR")
526 St = Asr;
527 else if (ShiftName == "ror" || ShiftName == "ROR")
528 St = Ror;
529 else if (ShiftName == "rrx" || ShiftName == "RRX")
530 St = Rrx;
531 else
532 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000533 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000534
535 // Rrx stands alone.
536 if (St == Rrx)
537 return false;
538
539 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000540 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000541 if (HashTok.isNot(AsmToken::Hash))
542 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000543 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000544
545 if (getParser().ParseExpression(ShiftAmount))
546 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000547
548 return false;
549}
550
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000551/// A hack to allow some testing, to be replaced by a real table gen version.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000552int ARMAsmParser::MatchRegisterName(const StringRef &Name) {
553 if (Name == "r0" || Name == "R0")
554 return 0;
555 else if (Name == "r1" || Name == "R1")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000556 return 1;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000557 else if (Name == "r2" || Name == "R2")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000558 return 2;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000559 else if (Name == "r3" || Name == "R3")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000560 return 3;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000561 else if (Name == "r3" || Name == "R3")
562 return 3;
563 else if (Name == "r4" || Name == "R4")
564 return 4;
565 else if (Name == "r5" || Name == "R5")
566 return 5;
567 else if (Name == "r6" || Name == "R6")
568 return 6;
569 else if (Name == "r7" || Name == "R7")
570 return 7;
571 else if (Name == "r8" || Name == "R8")
572 return 8;
573 else if (Name == "r9" || Name == "R9")
574 return 9;
575 else if (Name == "r10" || Name == "R10")
576 return 10;
577 else if (Name == "r11" || Name == "R11" || Name == "fp")
578 return 11;
579 else if (Name == "r12" || Name == "R12" || Name == "ip")
580 return 12;
581 else if (Name == "r13" || Name == "R13" || Name == "sp")
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000582 return 13;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000583 else if (Name == "r14" || Name == "R14" || Name == "lr")
584 return 14;
585 else if (Name == "r15" || Name == "R15" || Name == "pc")
586 return 15;
587 return -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588}
589
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000590/// A hack to allow some testing, to be replaced by a real table gen version.
Chris Lattner98986712010-01-14 22:21:20 +0000591bool ARMAsmParser::
592MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
593 MCInst &Inst) {
594 ARMOperand &Op0 = *(ARMOperand*)Operands[0];
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000595 assert(Op0.Kind == ARMOperand::Token && "First operand not a Token");
596 const StringRef &Mnemonic = Op0.getToken();
597 if (Mnemonic == "add" ||
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000598 Mnemonic == "stmfd" ||
599 Mnemonic == "str" ||
600 Mnemonic == "ldmfd" ||
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000601 Mnemonic == "ldr" ||
Kevin Enderbycfe07242009-10-13 22:19:02 +0000602 Mnemonic == "mov" ||
Kevin Enderby515d5092009-10-15 20:48:48 +0000603 Mnemonic == "sub" ||
604 Mnemonic == "bl" ||
605 Mnemonic == "push" ||
606 Mnemonic == "blx" ||
Daniel Dunbar2685a292009-10-20 05:15:36 +0000607 Mnemonic == "pop") {
608 // Hard-coded to a valid instruction, till we have a real matcher.
609 Inst = MCInst();
610 Inst.setOpcode(ARM::MOVr);
611 Inst.addOperand(MCOperand::CreateReg(2));
612 Inst.addOperand(MCOperand::CreateReg(2));
613 Inst.addOperand(MCOperand::CreateImm(0));
614 Inst.addOperand(MCOperand::CreateImm(0));
615 Inst.addOperand(MCOperand::CreateReg(0));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000616 return false;
Daniel Dunbar2685a292009-10-20 05:15:36 +0000617 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000618
619 return true;
620}
621
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000622/// Parse a arm instruction operand. For now this parses the operand regardless
623/// of the mnemonic.
Sean Callanan76264762010-04-02 22:27:05 +0000624bool ARMAsmParser::ParseOperand(OwningPtr<ARMOperand> &Op) {
625 SMLoc S, E;
626
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000627 switch (getLexer().getKind()) {
628 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000629 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000630 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000631 // This was not a register so parse other operands that start with an
632 // identifier (like labels) as expressions and create them as immediates.
633 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000634 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000635 if (getParser().ParseExpression(IdVal))
636 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000637 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
638 ARMOperand::CreateImm(Op, IdVal, S, E);
Kevin Enderby515d5092009-10-15 20:48:48 +0000639 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000640 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000641 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000642 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000643 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000644 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000645 // #42 -> immediate.
646 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000647 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000648 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000649 const MCExpr *ImmVal;
650 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000651 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000652 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
653 ARMOperand::CreateImm(Op, ImmVal, S, E);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000654 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000655 default:
Sean Callanan18b83232010-01-19 21:44:56 +0000656 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000657 }
658}
659
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000660/// Parse an arm instruction mnemonic followed by its operands.
Chris Lattnerf007e852010-01-14 21:32:45 +0000661bool ARMAsmParser::ParseInstruction(const StringRef &Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000662 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000663 OwningPtr<ARMOperand> Op;
664 ARMOperand::CreateToken(Op, Name, NameLoc);
665
666 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000667
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000668 if (getLexer().isNot(AsmToken::EndOfStatement)) {
669
670 // Read the first operand.
Sean Callanan76264762010-04-02 22:27:05 +0000671 OwningPtr<ARMOperand> Op;
Chris Lattner98986712010-01-14 22:21:20 +0000672 if (ParseOperand(Op)) return true;
Sean Callanan76264762010-04-02 22:27:05 +0000673 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000674
675 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000676 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000677
678 // Parse and remember the operand.
Chris Lattner98986712010-01-14 22:21:20 +0000679 if (ParseOperand(Op)) return true;
Sean Callanan76264762010-04-02 22:27:05 +0000680 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000681 }
682 }
Chris Lattner98986712010-01-14 22:21:20 +0000683 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000684}
685
Kevin Enderby515d5092009-10-15 20:48:48 +0000686/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000687bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
688 StringRef IDVal = DirectiveID.getIdentifier();
689 if (IDVal == ".word")
690 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000691 else if (IDVal == ".thumb")
692 return ParseDirectiveThumb(DirectiveID.getLoc());
693 else if (IDVal == ".thumb_func")
694 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
695 else if (IDVal == ".code")
696 return ParseDirectiveCode(DirectiveID.getLoc());
697 else if (IDVal == ".syntax")
698 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000699 return true;
700}
701
702/// ParseDirectiveWord
703/// ::= .word [ expression (, expression)* ]
704bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
705 if (getLexer().isNot(AsmToken::EndOfStatement)) {
706 for (;;) {
707 const MCExpr *Value;
708 if (getParser().ParseExpression(Value))
709 return true;
710
Chris Lattneraaec2052010-01-19 19:46:13 +0000711 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000712
713 if (getLexer().is(AsmToken::EndOfStatement))
714 break;
715
716 // FIXME: Improve diagnostic.
717 if (getLexer().isNot(AsmToken::Comma))
718 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000719 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000720 }
721 }
722
Sean Callananb9a25b72010-01-19 20:27:46 +0000723 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000724 return false;
725}
726
Kevin Enderby515d5092009-10-15 20:48:48 +0000727/// ParseDirectiveThumb
728/// ::= .thumb
729bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
730 if (getLexer().isNot(AsmToken::EndOfStatement))
731 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000732 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000733
734 // TODO: set thumb mode
735 // TODO: tell the MC streamer the mode
736 // getParser().getStreamer().Emit???();
737 return false;
738}
739
740/// ParseDirectiveThumbFunc
741/// ::= .thumbfunc symbol_name
742bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000743 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000744 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
745 return Error(L, "unexpected token in .syntax directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000746 StringRef ATTRIBUTE_UNUSED SymbolName = Parser.getTok().getIdentifier();
Sean Callananb9a25b72010-01-19 20:27:46 +0000747 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000748
749 if (getLexer().isNot(AsmToken::EndOfStatement))
750 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000751 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000752
753 // TODO: mark symbol as a thumb symbol
754 // getParser().getStreamer().Emit???();
755 return false;
756}
757
758/// ParseDirectiveSyntax
759/// ::= .syntax unified | divided
760bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000761 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000762 if (Tok.isNot(AsmToken::Identifier))
763 return Error(L, "unexpected token in .syntax directive");
764 const StringRef &Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000765 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000766 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000767 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000768 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000769 else
770 return Error(L, "unrecognized syntax mode in .syntax directive");
771
772 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000773 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000774 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000775
776 // TODO tell the MC streamer the mode
777 // getParser().getStreamer().Emit???();
778 return false;
779}
780
781/// ParseDirectiveCode
782/// ::= .code 16 | 32
783bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000784 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000785 if (Tok.isNot(AsmToken::Integer))
786 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000787 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000788 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000789 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000790 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000791 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000792 else
793 return Error(L, "invalid operand to .code directive");
794
795 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000796 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000797 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000798
799 // TODO tell the MC streamer the mode
800 // getParser().getStreamer().Emit???();
801 return false;
802}
803
Sean Callanan90b70972010-04-07 20:29:34 +0000804extern "C" void LLVMInitializeARMAsmLexer();
805
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000806/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000807extern "C" void LLVMInitializeARMAsmParser() {
808 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
809 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000810 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000811}