blob: 580ef674ca552b6e5b6e98d64fdb05a8ef5c2808 [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;
Daniel Dunbard73ada72010-07-19 00:33:49 +000040 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041
42private:
43 MCAsmParser &getParser() const { return Parser; }
44
45 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
46
47 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
48
49 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
50
Sean Callanan76264762010-04-02 22:27:05 +000051 bool MaybeParseRegister(OwningPtr<ARMOperand> &Op, bool ParseWriteBack);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000052
Sean Callanan76264762010-04-02 22:27:05 +000053 bool ParseRegisterList(OwningPtr<ARMOperand> &Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +000054
Sean Callanan76264762010-04-02 22:27:05 +000055 bool ParseMemory(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000056
Kevin Enderby9c41fa82009-10-30 22:55:57 +000057 bool ParseMemoryOffsetReg(bool &Negative,
58 bool &OffsetRegShifted,
59 enum ShiftType &ShiftType,
60 const MCExpr *&ShiftAmount,
61 const MCExpr *&Offset,
62 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000063 int &OffsetRegNum,
64 SMLoc &E);
Kevin Enderby9c41fa82009-10-30 22:55:57 +000065
Sean Callanan76264762010-04-02 22:27:05 +000066 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000067
Sean Callanan76264762010-04-02 22:27:05 +000068 bool ParseOperand(OwningPtr<ARMOperand> &Op);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000069
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000070 bool ParseDirectiveWord(unsigned Size, SMLoc L);
71
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveThumb(SMLoc L);
73
74 bool ParseDirectiveThumbFunc(SMLoc L);
75
76 bool ParseDirectiveCode(SMLoc L);
77
78 bool ParseDirectiveSyntax(SMLoc L);
79
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000080 // TODO - For now hacked versions of the next two are in here in this file to
81 // allow some parser testing until the table gen versions are implemented.
82
83 /// @name Auto-generated Match Functions
84 /// {
Chris Lattner98986712010-01-14 22:21:20 +000085 bool MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000086 MCInst &Inst);
87
Kevin Enderbyd7894f12009-10-09 21:12:28 +000088 /// MatchRegisterName - Match the given string to a register name and return
89 /// its register number, or -1 if there is no match. To allow return values
90 /// to be used directly in register lists, arm registers have values between
91 /// 0 and 15.
Benjamin Kramer38e59892010-07-14 22:38:02 +000092 int MatchRegisterName(StringRef Name);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000093
94 /// }
95
96
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000097public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000098 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
99 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000100
Benjamin Kramer38e59892010-07-14 22:38:02 +0000101 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000102 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000103
104 virtual bool ParseDirective(AsmToken DirectiveID);
105};
106
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000107/// ARMOperand - Instances of this class represent a parsed ARM machine
108/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000109struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000110private:
111 ARMOperand() {}
112public:
113 enum KindTy {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000114 Token,
115 Register,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000116 Immediate,
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000117 Memory
118 } Kind;
119
Sean Callanan76264762010-04-02 22:27:05 +0000120 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000121
122 union {
123 struct {
124 const char *Data;
125 unsigned Length;
126 } Tok;
127
128 struct {
129 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000130 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000131 } Reg;
132
Kevin Enderbycfe07242009-10-13 22:19:02 +0000133 struct {
134 const MCExpr *Val;
135 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000136
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000137 // This is for all forms of ARM address expressions
138 struct {
139 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000140 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000141 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000142 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000143 enum ShiftType ShiftType; // used when OffsetRegShifted is true
144 unsigned
145 OffsetRegShifted : 1, // only used when OffsetIsReg is true
146 Preindexed : 1,
147 Postindexed : 1,
148 OffsetIsReg : 1,
149 Negative : 1, // only used when OffsetIsReg is true
150 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 } Mem;
152
153 };
Sean Callanan76264762010-04-02 22:27:05 +0000154
155 ARMOperand(KindTy K, SMLoc S, SMLoc E)
156 : Kind(K), StartLoc(S), EndLoc(E) {}
157
158 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
159 Kind = o.Kind;
160 StartLoc = o.StartLoc;
161 EndLoc = o.EndLoc;
162 switch (Kind) {
163 case Token:
164 Tok = o.Tok;
165 break;
166 case Register:
167 Reg = o.Reg;
168 break;
169 case Immediate:
170 Imm = o.Imm;
171 break;
172 case Memory:
173 Mem = o.Mem;
174 break;
175 }
176 }
177
178 /// getStartLoc - Get the location of the first token of this operand.
179 SMLoc getStartLoc() const { return StartLoc; }
180 /// getEndLoc - Get the location of the last token of this operand.
181 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000182
183 StringRef getToken() const {
184 assert(Kind == Token && "Invalid access!");
185 return StringRef(Tok.Data, Tok.Length);
186 }
187
188 unsigned getReg() const {
189 assert(Kind == Register && "Invalid access!");
190 return Reg.RegNum;
191 }
192
Kevin Enderbycfe07242009-10-13 22:19:02 +0000193 const MCExpr *getImm() const {
194 assert(Kind == Immediate && "Invalid access!");
195 return Imm.Val;
196 }
197
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000198 bool isToken() const {return Kind == Token; }
199
200 bool isReg() const { return Kind == Register; }
201
202 void addRegOperands(MCInst &Inst, unsigned N) const {
203 assert(N == 1 && "Invalid number of operands!");
204 Inst.addOperand(MCOperand::CreateReg(getReg()));
205 }
206
Sean Callanan76264762010-04-02 22:27:05 +0000207 static void CreateToken(OwningPtr<ARMOperand> &Op, StringRef Str,
208 SMLoc S) {
209 Op.reset(new ARMOperand);
210 Op->Kind = Token;
211 Op->Tok.Data = Str.data();
212 Op->Tok.Length = Str.size();
213 Op->StartLoc = S;
214 Op->EndLoc = S;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000215 }
216
Sean Callanan76264762010-04-02 22:27:05 +0000217 static void CreateReg(OwningPtr<ARMOperand> &Op, unsigned RegNum,
218 bool Writeback, SMLoc S, SMLoc E) {
219 Op.reset(new ARMOperand);
220 Op->Kind = Register;
221 Op->Reg.RegNum = RegNum;
222 Op->Reg.Writeback = Writeback;
223
224 Op->StartLoc = S;
225 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000226 }
227
Sean Callanan76264762010-04-02 22:27:05 +0000228 static void CreateImm(OwningPtr<ARMOperand> &Op, const MCExpr *Val,
229 SMLoc S, SMLoc E) {
230 Op.reset(new ARMOperand);
231 Op->Kind = Immediate;
232 Op->Imm.Val = Val;
233
234 Op->StartLoc = S;
235 Op->EndLoc = E;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000236 }
237
Sean Callanan76264762010-04-02 22:27:05 +0000238 static void CreateMem(OwningPtr<ARMOperand> &Op,
239 unsigned BaseRegNum, bool OffsetIsReg,
240 const MCExpr *Offset, unsigned OffsetRegNum,
241 bool OffsetRegShifted, enum ShiftType ShiftType,
242 const MCExpr *ShiftAmount, bool Preindexed,
243 bool Postindexed, bool Negative, bool Writeback,
244 SMLoc S, SMLoc E) {
245 Op.reset(new ARMOperand);
246 Op->Kind = Memory;
247 Op->Mem.BaseRegNum = BaseRegNum;
248 Op->Mem.OffsetIsReg = OffsetIsReg;
249 Op->Mem.Offset = Offset;
250 Op->Mem.OffsetRegNum = OffsetRegNum;
251 Op->Mem.OffsetRegShifted = OffsetRegShifted;
252 Op->Mem.ShiftType = ShiftType;
253 Op->Mem.ShiftAmount = ShiftAmount;
254 Op->Mem.Preindexed = Preindexed;
255 Op->Mem.Postindexed = Postindexed;
256 Op->Mem.Negative = Negative;
257 Op->Mem.Writeback = Writeback;
258
259 Op->StartLoc = S;
260 Op->EndLoc = E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000261 }
262};
263
264} // end anonymous namespace.
265
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000266/// Try to parse a register name. The token must be an Identifier when called,
267/// and if it is a register name a Reg operand is created, the token is eaten
268/// and false is returned. Else true is returned and no token is eaten.
269/// TODO this is likely to change to allow different register types and or to
270/// parse for a specific register type.
Sean Callanan76264762010-04-02 22:27:05 +0000271bool ARMAsmParser::MaybeParseRegister
272 (OwningPtr<ARMOperand> &Op, bool ParseWriteBack) {
273 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000274 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000275 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
276
277 // FIXME: Validate register for the current architecture; we have to do
278 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000279 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000280
281 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000282 if (RegNum == -1)
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000283 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000284
285 S = Tok.getLoc();
286
Sean Callananb9a25b72010-01-19 20:27:46 +0000287 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000288
289 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000290
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000291 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000292 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000293 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000294 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000295 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000296 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000297 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000298 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000299 }
300
Sean Callanan76264762010-04-02 22:27:05 +0000301 ARMOperand::CreateReg(Op, RegNum, Writeback, S, E);
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000302
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000303 return false;
304}
305
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000306/// Parse a register list, return false if successful else return true or an
307/// error. The first token must be a '{' when called.
Sean Callanan76264762010-04-02 22:27:05 +0000308bool ARMAsmParser::ParseRegisterList(OwningPtr<ARMOperand> &Op) {
309 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000310 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000311 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000312 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000313 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000314
Sean Callanan18b83232010-01-19 21:44:56 +0000315 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000316 SMLoc RegLoc = RegTok.getLoc();
317 if (RegTok.isNot(AsmToken::Identifier))
318 return Error(RegLoc, "register expected");
319 int RegNum = MatchRegisterName(RegTok.getString());
320 if (RegNum == -1)
321 return Error(RegLoc, "register expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000322 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000323 unsigned RegList = 1 << RegNum;
324
325 int HighRegNum = RegNum;
326 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000327 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000328 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000329
Sean Callanan18b83232010-01-19 21:44:56 +0000330 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000331 SMLoc RegLoc = RegTok.getLoc();
332 if (RegTok.isNot(AsmToken::Identifier))
333 return Error(RegLoc, "register expected");
334 int RegNum = MatchRegisterName(RegTok.getString());
335 if (RegNum == -1)
336 return Error(RegLoc, "register expected");
337
338 if (RegList & (1 << RegNum))
339 Warning(RegLoc, "register duplicated in register list");
340 else if (RegNum <= HighRegNum)
341 Warning(RegLoc, "register not in ascending order in register list");
342 RegList |= 1 << RegNum;
343 HighRegNum = RegNum;
344
Sean Callananb9a25b72010-01-19 20:27:46 +0000345 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000346 }
Sean Callanan18b83232010-01-19 21:44:56 +0000347 const AsmToken &RCurlyTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000348 if (RCurlyTok.isNot(AsmToken::RCurly))
349 return Error(RCurlyTok.getLoc(), "'}' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000350 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000351 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000352
353 return false;
354}
355
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000356/// Parse an arm memory expression, return false if successful else return true
357/// or an error. The first token must be a '[' when called.
358/// TODO Only preindexing and postindexing addressing are started, unindexed
359/// with option, etc are still to do.
Sean Callanan76264762010-04-02 22:27:05 +0000360bool ARMAsmParser::ParseMemory(OwningPtr<ARMOperand> &Op) {
361 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000362 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000363 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000364 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000365 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000366
Sean Callanan18b83232010-01-19 21:44:56 +0000367 const AsmToken &BaseRegTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000368 if (BaseRegTok.isNot(AsmToken::Identifier))
369 return Error(BaseRegTok.getLoc(), "register expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000370 if (MaybeParseRegister(Op, false))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000371 return Error(BaseRegTok.getLoc(), "register expected");
Sean Callanan76264762010-04-02 22:27:05 +0000372 int BaseRegNum = Op->getReg();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000373
374 bool Preindexed = false;
375 bool Postindexed = false;
376 bool OffsetIsReg = false;
377 bool Negative = false;
378 bool Writeback = false;
379
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000380 // First look for preindexed address forms, that is after the "[Rn" we now
381 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000382 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000383 if (Tok.is(AsmToken::Comma)) {
384 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000385 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000386 int OffsetRegNum;
387 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000388 enum ShiftType ShiftType;
389 const MCExpr *ShiftAmount;
390 const MCExpr *Offset;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000391 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000392 Offset, OffsetIsReg, OffsetRegNum, E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000393 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000394 const AsmToken &RBracTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000395 if (RBracTok.isNot(AsmToken::RBrac))
396 return Error(RBracTok.getLoc(), "']' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000397 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000398 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000399
Sean Callanan18b83232010-01-19 21:44:56 +0000400 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000401 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000402 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000403 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000404 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000405 }
Sean Callanan76264762010-04-02 22:27:05 +0000406 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
407 OffsetRegShifted, ShiftType, ShiftAmount,
408 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000409 return false;
410 }
411 // The "[Rn" we have so far was not followed by a comma.
412 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000413 // This is a post indexing addressing forms, that is a ']' follows after
414 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000415 Postindexed = true;
416 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000417 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000418 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000419
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000420 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000421 bool OffsetRegShifted = false;
422 enum ShiftType ShiftType;
423 const MCExpr *ShiftAmount;
424 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000425
Sean Callanan18b83232010-01-19 21:44:56 +0000426 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000427 if (NextTok.isNot(AsmToken::EndOfStatement)) {
428 if (NextTok.isNot(AsmToken::Comma))
Duncan Sands34727662010-07-12 08:16:59 +0000429 return Error(NextTok.getLoc(), "',' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000430 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000431 if(ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Sean Callanan76264762010-04-02 22:27:05 +0000432 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
433 E))
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000434 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000435 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000436
Sean Callanan76264762010-04-02 22:27:05 +0000437 ARMOperand::CreateMem(Op, BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
438 OffsetRegShifted, ShiftType, ShiftAmount,
439 Preindexed, Postindexed, Negative, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000440 return false;
441 }
442
443 return true;
444}
445
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000446/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
447/// we will parse the following (were +/- means that a plus or minus is
448/// optional):
449/// +/-Rm
450/// +/-Rm, shift
451/// #offset
452/// we return false on success or an error otherwise.
453bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000454 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000455 enum ShiftType &ShiftType,
456 const MCExpr *&ShiftAmount,
457 const MCExpr *&Offset,
458 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000459 int &OffsetRegNum,
460 SMLoc &E) {
461 OwningPtr<ARMOperand> Op;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000462 Negative = false;
463 OffsetRegShifted = false;
464 OffsetIsReg = false;
465 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000466 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000467 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000468 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000469 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000470 else if (NextTok.is(AsmToken::Minus)) {
471 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000472 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000473 }
474 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000475 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000476 if (OffsetRegTok.is(AsmToken::Identifier)) {
477 OffsetIsReg = !MaybeParseRegister(Op, false);
Sean Callanan76264762010-04-02 22:27:05 +0000478 if (OffsetIsReg) {
479 E = Op->getEndLoc();
480 OffsetRegNum = Op->getReg();
481 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000482 }
483 // If we parsed a register as the offset then their can be a shift after that
484 if (OffsetRegNum != -1) {
485 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000486 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000487 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000488 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000489
Sean Callanan18b83232010-01-19 21:44:56 +0000490 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000491 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000492 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000493 OffsetRegShifted = true;
494 }
495 }
496 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
497 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000498 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000499 if (HashTok.isNot(AsmToken::Hash))
500 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000501
Sean Callananb9a25b72010-01-19 20:27:46 +0000502 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000503
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000504 if (getParser().ParseExpression(Offset))
505 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000506 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000507 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000508 return false;
509}
510
511/// ParseShift as one of these two:
512/// ( lsl | lsr | asr | ror ) , # shift_amount
513/// rrx
514/// and returns true if it parses a shift otherwise it returns false.
Sean Callanan76264762010-04-02 22:27:05 +0000515bool ARMAsmParser::ParseShift(ShiftType &St,
516 const MCExpr *&ShiftAmount,
517 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000518 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000519 if (Tok.isNot(AsmToken::Identifier))
520 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000521 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000522 if (ShiftName == "lsl" || ShiftName == "LSL")
523 St = Lsl;
524 else if (ShiftName == "lsr" || ShiftName == "LSR")
525 St = Lsr;
526 else if (ShiftName == "asr" || ShiftName == "ASR")
527 St = Asr;
528 else if (ShiftName == "ror" || ShiftName == "ROR")
529 St = Ror;
530 else if (ShiftName == "rrx" || ShiftName == "RRX")
531 St = Rrx;
532 else
533 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000534 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000535
536 // Rrx stands alone.
537 if (St == Rrx)
538 return false;
539
540 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000541 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000542 if (HashTok.isNot(AsmToken::Hash))
543 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000544 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000545
546 if (getParser().ParseExpression(ShiftAmount))
547 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000548
549 return false;
550}
551
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000552/// A hack to allow some testing, to be replaced by a real table gen version.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000553int ARMAsmParser::MatchRegisterName(StringRef Name) {
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000554 if (Name == "r0" || Name == "R0")
555 return 0;
556 else if (Name == "r1" || Name == "R1")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000557 return 1;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000558 else if (Name == "r2" || Name == "R2")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000559 return 2;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000560 else if (Name == "r3" || Name == "R3")
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000561 return 3;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000562 else if (Name == "r3" || Name == "R3")
563 return 3;
564 else if (Name == "r4" || Name == "R4")
565 return 4;
566 else if (Name == "r5" || Name == "R5")
567 return 5;
568 else if (Name == "r6" || Name == "R6")
569 return 6;
570 else if (Name == "r7" || Name == "R7")
571 return 7;
572 else if (Name == "r8" || Name == "R8")
573 return 8;
574 else if (Name == "r9" || Name == "R9")
575 return 9;
576 else if (Name == "r10" || Name == "R10")
577 return 10;
578 else if (Name == "r11" || Name == "R11" || Name == "fp")
579 return 11;
580 else if (Name == "r12" || Name == "R12" || Name == "ip")
581 return 12;
582 else if (Name == "r13" || Name == "R13" || Name == "sp")
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000583 return 13;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000584 else if (Name == "r14" || Name == "R14" || Name == "lr")
585 return 14;
586 else if (Name == "r15" || Name == "R15" || Name == "pc")
587 return 15;
588 return -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000589}
590
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000591/// A hack to allow some testing, to be replaced by a real table gen version.
Chris Lattner98986712010-01-14 22:21:20 +0000592bool ARMAsmParser::
593MatchInstruction(const SmallVectorImpl<MCParsedAsmOperand*> &Operands,
594 MCInst &Inst) {
595 ARMOperand &Op0 = *(ARMOperand*)Operands[0];
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000596 assert(Op0.Kind == ARMOperand::Token && "First operand not a Token");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000597 StringRef Mnemonic = Op0.getToken();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000598 if (Mnemonic == "add" ||
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000599 Mnemonic == "stmfd" ||
600 Mnemonic == "str" ||
601 Mnemonic == "ldmfd" ||
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000602 Mnemonic == "ldr" ||
Kevin Enderbycfe07242009-10-13 22:19:02 +0000603 Mnemonic == "mov" ||
Kevin Enderby515d5092009-10-15 20:48:48 +0000604 Mnemonic == "sub" ||
605 Mnemonic == "bl" ||
606 Mnemonic == "push" ||
607 Mnemonic == "blx" ||
Daniel Dunbar2685a292009-10-20 05:15:36 +0000608 Mnemonic == "pop") {
609 // Hard-coded to a valid instruction, till we have a real matcher.
610 Inst = MCInst();
611 Inst.setOpcode(ARM::MOVr);
612 Inst.addOperand(MCOperand::CreateReg(2));
613 Inst.addOperand(MCOperand::CreateReg(2));
614 Inst.addOperand(MCOperand::CreateImm(0));
615 Inst.addOperand(MCOperand::CreateImm(0));
616 Inst.addOperand(MCOperand::CreateReg(0));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000617 return false;
Daniel Dunbar2685a292009-10-20 05:15:36 +0000618 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000619
620 return true;
621}
622
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000623/// Parse a arm instruction operand. For now this parses the operand regardless
624/// of the mnemonic.
Sean Callanan76264762010-04-02 22:27:05 +0000625bool ARMAsmParser::ParseOperand(OwningPtr<ARMOperand> &Op) {
626 SMLoc S, E;
627
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000628 switch (getLexer().getKind()) {
629 case AsmToken::Identifier:
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000630 if (!MaybeParseRegister(Op, true))
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000631 return false;
Kevin Enderby515d5092009-10-15 20:48:48 +0000632 // This was not a register so parse other operands that start with an
633 // identifier (like labels) as expressions and create them as immediates.
634 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000635 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000636 if (getParser().ParseExpression(IdVal))
637 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000638 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
639 ARMOperand::CreateImm(Op, IdVal, S, E);
Kevin Enderby515d5092009-10-15 20:48:48 +0000640 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000641 case AsmToken::LBrac:
Kevin Enderby515d5092009-10-15 20:48:48 +0000642 return ParseMemory(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000643 case AsmToken::LCurly:
Kevin Enderby515d5092009-10-15 20:48:48 +0000644 return ParseRegisterList(Op);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000645 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000646 // #42 -> immediate.
647 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000648 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000649 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000650 const MCExpr *ImmVal;
651 if (getParser().ParseExpression(ImmVal))
Kevin Enderbycfe07242009-10-13 22:19:02 +0000652 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000653 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
654 ARMOperand::CreateImm(Op, ImmVal, S, E);
Kevin Enderbycfe07242009-10-13 22:19:02 +0000655 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000656 default:
Sean Callanan18b83232010-01-19 21:44:56 +0000657 return Error(Parser.getTok().getLoc(), "unexpected token in operand");
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000658 }
659}
660
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000661/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000662bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000663 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000664 OwningPtr<ARMOperand> Op;
665 ARMOperand::CreateToken(Op, Name, NameLoc);
666
667 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000668
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000669 if (getLexer().isNot(AsmToken::EndOfStatement)) {
670
671 // Read the first operand.
Sean Callanan76264762010-04-02 22:27:05 +0000672 OwningPtr<ARMOperand> Op;
Chris Lattner98986712010-01-14 22:21:20 +0000673 if (ParseOperand(Op)) return true;
Sean Callanan76264762010-04-02 22:27:05 +0000674 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000675
676 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000677 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000678
679 // Parse and remember the operand.
Chris Lattner98986712010-01-14 22:21:20 +0000680 if (ParseOperand(Op)) return true;
Sean Callanan76264762010-04-02 22:27:05 +0000681 Operands.push_back(Op.take());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000682 }
683 }
Chris Lattner98986712010-01-14 22:21:20 +0000684 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000685}
686
Kevin Enderby515d5092009-10-15 20:48:48 +0000687/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000688bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
689 StringRef IDVal = DirectiveID.getIdentifier();
690 if (IDVal == ".word")
691 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000692 else if (IDVal == ".thumb")
693 return ParseDirectiveThumb(DirectiveID.getLoc());
694 else if (IDVal == ".thumb_func")
695 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
696 else if (IDVal == ".code")
697 return ParseDirectiveCode(DirectiveID.getLoc());
698 else if (IDVal == ".syntax")
699 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000700 return true;
701}
702
703/// ParseDirectiveWord
704/// ::= .word [ expression (, expression)* ]
705bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
706 if (getLexer().isNot(AsmToken::EndOfStatement)) {
707 for (;;) {
708 const MCExpr *Value;
709 if (getParser().ParseExpression(Value))
710 return true;
711
Chris Lattneraaec2052010-01-19 19:46:13 +0000712 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000713
714 if (getLexer().is(AsmToken::EndOfStatement))
715 break;
716
717 // FIXME: Improve diagnostic.
718 if (getLexer().isNot(AsmToken::Comma))
719 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000720 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000721 }
722 }
723
Sean Callananb9a25b72010-01-19 20:27:46 +0000724 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000725 return false;
726}
727
Kevin Enderby515d5092009-10-15 20:48:48 +0000728/// ParseDirectiveThumb
729/// ::= .thumb
730bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
731 if (getLexer().isNot(AsmToken::EndOfStatement))
732 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000733 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000734
735 // TODO: set thumb mode
736 // TODO: tell the MC streamer the mode
737 // getParser().getStreamer().Emit???();
738 return false;
739}
740
741/// ParseDirectiveThumbFunc
742/// ::= .thumbfunc symbol_name
743bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000744 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000745 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
746 return Error(L, "unexpected token in .syntax directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000747 StringRef ATTRIBUTE_UNUSED SymbolName = Parser.getTok().getIdentifier();
Sean Callananb9a25b72010-01-19 20:27:46 +0000748 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000749
750 if (getLexer().isNot(AsmToken::EndOfStatement))
751 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000752 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000753
754 // TODO: mark symbol as a thumb symbol
755 // getParser().getStreamer().Emit???();
756 return false;
757}
758
759/// ParseDirectiveSyntax
760/// ::= .syntax unified | divided
761bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000762 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000763 if (Tok.isNot(AsmToken::Identifier))
764 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000765 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000766 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000767 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000768 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000769 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000770 else
771 return Error(L, "unrecognized syntax mode in .syntax directive");
772
773 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000774 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000775 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000776
777 // TODO tell the MC streamer the mode
778 // getParser().getStreamer().Emit???();
779 return false;
780}
781
782/// ParseDirectiveCode
783/// ::= .code 16 | 32
784bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000785 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000786 if (Tok.isNot(AsmToken::Integer))
787 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000788 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000789 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000790 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000791 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000792 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000793 else
794 return Error(L, "invalid operand to .code directive");
795
796 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000797 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000798 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000799
800 // TODO tell the MC streamer the mode
801 // getParser().getStreamer().Emit???();
802 return false;
803}
804
Sean Callanan90b70972010-04-07 20:29:34 +0000805extern "C" void LLVMInitializeARMAsmLexer();
806
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000807/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000808extern "C" void LLVMInitializeARMAsmParser() {
809 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
810 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000811 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000812}