blob: 9836ef598fd6b6f573e02450f99caaeed2c759c2 [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"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000011#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000012#include "llvm/MC/MCParser/MCAsmLexer.h"
13#include "llvm/MC/MCParser/MCAsmParser.h"
14#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000015#include "llvm/MC/MCStreamer.h"
16#include "llvm/MC/MCExpr.h"
17#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/Target/TargetRegistry.h"
19#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000020#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000023#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000025using namespace llvm;
26
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000027// The shift types for register controlled shifts in arm memory addressing
28enum ShiftType {
29 Lsl,
30 Lsr,
31 Asr,
32 Ror,
33 Rrx
34};
35
Chris Lattner3a697562010-10-28 17:20:03 +000036namespace {
37 struct ARMOperand;
38
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039class ARMAsmParser : public TargetAsmParser {
40 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000041 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042
43private:
44 MCAsmParser &getParser() const { return Parser; }
45
46 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
47
48 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
49
50 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattner3a697562010-10-28 17:20:03 +000052 ARMOperand *MaybeParseRegister(bool ParseWriteBack);
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +000053 ARMOperand *ParseRegisterList();
Chris Lattner550276e2010-10-28 20:52:15 +000054 ARMOperand *ParseMemory();
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
Chris Lattner550276e2010-10-28 20:52:15 +000067 ARMOperand *ParseOperand();
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
Chris Lattner7036f8b2010-09-29 01:42:58 +000079 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000080 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000081 MCStreamer &Out);
82
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// @name Auto-generated Match Functions
84 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000085
Chris Lattner0692ee62010-09-06 19:11:01 +000086#define GET_ASSEMBLER_HEADER
87#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000089 /// }
90
91
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000092public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000093 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
94 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {}
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095
Benjamin Kramer38e59892010-07-14 22:38:02 +000096 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000097 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000098
99 virtual bool ParseDirective(AsmToken DirectiveID);
100};
Chris Lattner3a697562010-10-28 17:20:03 +0000101} // end anonymous namespace
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102
Chris Lattner3a697562010-10-28 17:20:03 +0000103namespace {
104
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000105/// ARMOperand - Instances of this class represent a parsed ARM machine
106/// instruction.
Chris Lattner76593892010-01-14 21:21:40 +0000107struct ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000108public:
109 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000110 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000111 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Memory,
113 Register,
114 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000115 } Kind;
116
Sean Callanan76264762010-04-02 22:27:05 +0000117 SMLoc StartLoc, EndLoc;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000118
119 union {
120 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 ARMCC::CondCodes Val;
122 } CC;
123
124 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000125 const char *Data;
126 unsigned Length;
127 } Tok;
128
129 struct {
130 unsigned RegNum;
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000131 bool Writeback;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000132 } Reg;
133
Kevin Enderbycfe07242009-10-13 22:19:02 +0000134 struct {
135 const MCExpr *Val;
136 } Imm;
Sean Callanan76264762010-04-02 22:27:05 +0000137
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 // This is for all forms of ARM address expressions
139 struct {
140 unsigned BaseRegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000141 unsigned OffsetRegNum; // used when OffsetIsReg is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000142 const MCExpr *Offset; // used when OffsetIsReg is false
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned
146 OffsetRegShifted : 1, // only used when OffsetIsReg is true
147 Preindexed : 1,
148 Postindexed : 1,
149 OffsetIsReg : 1,
150 Negative : 1, // only used when OffsetIsReg is true
151 Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 } Mem;
153
154 };
Sean Callanan76264762010-04-02 22:27:05 +0000155
Chris Lattner14ab39e2010-09-01 16:04:34 +0000156 //ARMOperand(KindTy K, SMLoc S, SMLoc E)
157 // : Kind(K), StartLoc(S), EndLoc(E) {}
Sean Callanan76264762010-04-02 22:27:05 +0000158
159 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
160 Kind = o.Kind;
161 StartLoc = o.StartLoc;
162 EndLoc = o.EndLoc;
163 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000164 case CondCode:
165 CC = o.CC;
166 break;
Sean Callanan76264762010-04-02 22:27:05 +0000167 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000169 break;
170 case Register:
171 Reg = o.Reg;
172 break;
173 case Immediate:
174 Imm = o.Imm;
175 break;
176 case Memory:
177 Mem = o.Mem;
178 break;
179 }
180 }
181
182 /// getStartLoc - Get the location of the first token of this operand.
183 SMLoc getStartLoc() const { return StartLoc; }
184 /// getEndLoc - Get the location of the last token of this operand.
185 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000186
Daniel Dunbar8462b302010-08-11 06:36:53 +0000187 ARMCC::CondCodes getCondCode() const {
188 assert(Kind == CondCode && "Invalid access!");
189 return CC.Val;
190 }
191
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000192 StringRef getToken() const {
193 assert(Kind == Token && "Invalid access!");
194 return StringRef(Tok.Data, Tok.Length);
195 }
196
197 unsigned getReg() const {
198 assert(Kind == Register && "Invalid access!");
199 return Reg.RegNum;
200 }
201
Kevin Enderbycfe07242009-10-13 22:19:02 +0000202 const MCExpr *getImm() const {
203 assert(Kind == Immediate && "Invalid access!");
204 return Imm.Val;
205 }
206
Daniel Dunbar8462b302010-08-11 06:36:53 +0000207 bool isCondCode() const { return Kind == CondCode; }
208
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000209 bool isImm() const { return Kind == Immediate; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210
211 bool isReg() const { return Kind == Register; }
212
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000213 bool isToken() const {return Kind == Token; }
214
215 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
216 // Add as immediates when possible.
217 if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
218 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
219 else
220 Inst.addOperand(MCOperand::CreateExpr(Expr));
221 }
222
Daniel Dunbar8462b302010-08-11 06:36:53 +0000223 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000224 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000225 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000226 // FIXME: What belongs here?
227 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000228 }
229
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000230 void addRegOperands(MCInst &Inst, unsigned N) const {
231 assert(N == 1 && "Invalid number of operands!");
232 Inst.addOperand(MCOperand::CreateReg(getReg()));
233 }
234
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000235 void addImmOperands(MCInst &Inst, unsigned N) const {
236 assert(N == 1 && "Invalid number of operands!");
237 addExpr(Inst, getImm());
238 }
239
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000240 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000241
Chris Lattner3a697562010-10-28 17:20:03 +0000242 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
243 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000244 Op->CC.Val = CC;
245 Op->StartLoc = S;
246 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000247 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000248 }
249
Chris Lattner3a697562010-10-28 17:20:03 +0000250 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
251 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000252 Op->Tok.Data = Str.data();
253 Op->Tok.Length = Str.size();
254 Op->StartLoc = S;
255 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000256 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000257 }
258
Chris Lattner3a697562010-10-28 17:20:03 +0000259 static ARMOperand *CreateReg(unsigned RegNum, bool Writeback, SMLoc S,
260 SMLoc E) {
261 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000262 Op->Reg.RegNum = RegNum;
263 Op->Reg.Writeback = Writeback;
Sean Callanan76264762010-04-02 22:27:05 +0000264 Op->StartLoc = S;
265 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000266 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000267 }
268
Chris Lattner3a697562010-10-28 17:20:03 +0000269 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
270 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000271 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000272 Op->StartLoc = S;
273 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000274 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000275 }
276
Chris Lattner3a697562010-10-28 17:20:03 +0000277 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
278 const MCExpr *Offset, unsigned OffsetRegNum,
279 bool OffsetRegShifted, enum ShiftType ShiftType,
280 const MCExpr *ShiftAmount, bool Preindexed,
281 bool Postindexed, bool Negative, bool Writeback,
282 SMLoc S, SMLoc E) {
283 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000284 Op->Mem.BaseRegNum = BaseRegNum;
285 Op->Mem.OffsetIsReg = OffsetIsReg;
286 Op->Mem.Offset = Offset;
287 Op->Mem.OffsetRegNum = OffsetRegNum;
288 Op->Mem.OffsetRegShifted = OffsetRegShifted;
289 Op->Mem.ShiftType = ShiftType;
290 Op->Mem.ShiftAmount = ShiftAmount;
291 Op->Mem.Preindexed = Preindexed;
292 Op->Mem.Postindexed = Postindexed;
293 Op->Mem.Negative = Negative;
294 Op->Mem.Writeback = Writeback;
295
296 Op->StartLoc = S;
297 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000298 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000299 }
Chris Lattner3a697562010-10-28 17:20:03 +0000300
301private:
302 ARMOperand(KindTy K) : Kind(K) {}
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000303};
304
305} // end anonymous namespace.
306
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000307void ARMOperand::dump(raw_ostream &OS) const {
308 switch (Kind) {
309 case CondCode:
310 OS << ARMCondCodeToString(getCondCode());
311 break;
312 case Immediate:
313 getImm()->print(OS);
314 break;
315 case Memory:
316 OS << "<memory>";
317 break;
318 case Register:
319 OS << "<register " << getReg() << ">";
320 break;
321 case Token:
322 OS << "'" << getToken() << "'";
323 break;
324 }
325}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000326
327/// @name Auto-generated Match Functions
328/// {
329
330static unsigned MatchRegisterName(StringRef Name);
331
332/// }
333
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000334/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattner3a697562010-10-28 17:20:03 +0000335/// and if it is a register name the token is eaten and a Reg operand is created
336/// and returned. Otherwise return null.
337///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000338/// TODO this is likely to change to allow different register types and or to
339/// parse for a specific register type.
Chris Lattner3a697562010-10-28 17:20:03 +0000340ARMOperand *ARMAsmParser::MaybeParseRegister(bool ParseWriteBack) {
Sean Callanan76264762010-04-02 22:27:05 +0000341 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000342 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000343 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
344
345 // FIXME: Validate register for the current architecture; we have to do
346 // validation later, so maybe there is no need for this here.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000347 int RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000348
349 RegNum = MatchRegisterName(Tok.getString());
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000350 if (RegNum == -1)
Chris Lattner3a697562010-10-28 17:20:03 +0000351 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000352
353 S = Tok.getLoc();
354
Sean Callananb9a25b72010-01-19 20:27:46 +0000355 Parser.Lex(); // Eat identifier token.
Sean Callanan76264762010-04-02 22:27:05 +0000356
357 E = Parser.getTok().getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000358
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000359 bool Writeback = false;
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000360 if (ParseWriteBack) {
Sean Callanan18b83232010-01-19 21:44:56 +0000361 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000362 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000363 E = ExclaimTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000364 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000365 Parser.Lex(); // Eat exclaim token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000366 }
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000367 }
368
Chris Lattner3a697562010-10-28 17:20:03 +0000369 return ARMOperand::CreateReg(RegNum, Writeback, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000370}
371
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000372/// Parse a register list, return it if successful else return null. The first
373/// token must be a '{' when called.
374ARMOperand *ARMAsmParser::ParseRegisterList() {
Sean Callanan76264762010-04-02 22:27:05 +0000375 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000376 assert(Parser.getTok().is(AsmToken::LCurly) &&
Kevin Enderbycfe07242009-10-13 22:19:02 +0000377 "Token is not an Left Curly Brace");
Sean Callanan76264762010-04-02 22:27:05 +0000378 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000379 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000380
Sean Callanan18b83232010-01-19 21:44:56 +0000381 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000382 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000383 if (RegTok.isNot(AsmToken::Identifier)) {
384 Error(RegLoc, "register expected");
385 return 0;
386 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000387 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000388 if (RegNum == -1) {
389 Error(RegLoc, "register expected");
390 return 0;
391 }
392
Sean Callananb9a25b72010-01-19 20:27:46 +0000393 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000394 unsigned RegList = 1 << RegNum;
395
396 int HighRegNum = RegNum;
397 // TODO ranges like "{Rn-Rm}"
Sean Callanan18b83232010-01-19 21:44:56 +0000398 while (Parser.getTok().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000399 Parser.Lex(); // Eat comma token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000400
Sean Callanan18b83232010-01-19 21:44:56 +0000401 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000402 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000403 if (RegTok.isNot(AsmToken::Identifier)) {
404 Error(RegLoc, "register expected");
405 return 0;
406 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000407 int RegNum = MatchRegisterName(RegTok.getString());
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000408 if (RegNum == -1) {
409 Error(RegLoc, "register expected");
410 return 0;
411 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000412
413 if (RegList & (1 << RegNum))
414 Warning(RegLoc, "register duplicated in register list");
415 else if (RegNum <= HighRegNum)
416 Warning(RegLoc, "register not in ascending order in register list");
417 RegList |= 1 << RegNum;
418 HighRegNum = RegNum;
419
Sean Callananb9a25b72010-01-19 20:27:46 +0000420 Parser.Lex(); // Eat identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000421 }
Sean Callanan18b83232010-01-19 21:44:56 +0000422 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000423 if (RCurlyTok.isNot(AsmToken::RCurly)) {
424 Error(RCurlyTok.getLoc(), "'}' expected");
425 return 0;
426 }
Sean Callanan76264762010-04-02 22:27:05 +0000427 E = RCurlyTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000428 Parser.Lex(); // Eat left curly brace token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000429
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000430 // FIXME: Need to return an operand!
431 Error(E, "FIXME: register list parsing not implemented");
432 return 0;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000433}
434
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000435/// Parse an arm memory expression, return false if successful else return true
436/// or an error. The first token must be a '[' when called.
437/// TODO Only preindexing and postindexing addressing are started, unindexed
438/// with option, etc are still to do.
Chris Lattner550276e2010-10-28 20:52:15 +0000439ARMOperand *ARMAsmParser::ParseMemory() {
Sean Callanan76264762010-04-02 22:27:05 +0000440 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000441 assert(Parser.getTok().is(AsmToken::LBrac) &&
Kevin Enderby6bd266e2009-10-12 22:51:49 +0000442 "Token is not an Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000443 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000444 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000445
Sean Callanan18b83232010-01-19 21:44:56 +0000446 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000447 if (BaseRegTok.isNot(AsmToken::Identifier)) {
448 Error(BaseRegTok.getLoc(), "register expected");
449 return 0;
450 }
451 int BaseRegNum = 0;
452 if (ARMOperand *Op = MaybeParseRegister(false))
453 BaseRegNum = Op->getReg();
454 else {
455 Error(BaseRegTok.getLoc(), "register expected");
456 return 0;
457 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000458
459 bool Preindexed = false;
460 bool Postindexed = false;
461 bool OffsetIsReg = false;
462 bool Negative = false;
463 bool Writeback = false;
464
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000465 // First look for preindexed address forms, that is after the "[Rn" we now
466 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000467 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000468 if (Tok.is(AsmToken::Comma)) {
469 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000470 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000471 int OffsetRegNum;
472 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000473 enum ShiftType ShiftType;
474 const MCExpr *ShiftAmount;
475 const MCExpr *Offset;
Chris Lattner550276e2010-10-28 20:52:15 +0000476 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
477 Offset, OffsetIsReg, OffsetRegNum, E))
478 return 0;
Sean Callanan18b83232010-01-19 21:44:56 +0000479 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000480 if (RBracTok.isNot(AsmToken::RBrac)) {
481 Error(RBracTok.getLoc(), "']' expected");
482 return 0;
483 }
Sean Callanan76264762010-04-02 22:27:05 +0000484 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000485 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000486
Sean Callanan18b83232010-01-19 21:44:56 +0000487 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000488 if (ExclaimTok.is(AsmToken::Exclaim)) {
Sean Callanan76264762010-04-02 22:27:05 +0000489 E = ExclaimTok.getLoc();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000490 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000491 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000492 }
Chris Lattner550276e2010-10-28 20:52:15 +0000493 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
494 OffsetRegShifted, ShiftType, ShiftAmount,
495 Preindexed, Postindexed, Negative, Writeback,
496 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000497 }
498 // The "[Rn" we have so far was not followed by a comma.
499 else if (Tok.is(AsmToken::RBrac)) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000500 // This is a post indexing addressing forms, that is a ']' follows after
501 // the "[Rn".
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000502 Postindexed = true;
503 Writeback = true;
Sean Callanan76264762010-04-02 22:27:05 +0000504 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000505 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000506
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000507 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000508 bool OffsetRegShifted = false;
509 enum ShiftType ShiftType;
510 const MCExpr *ShiftAmount;
511 const MCExpr *Offset;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000512
Sean Callanan18b83232010-01-19 21:44:56 +0000513 const AsmToken &NextTok = Parser.getTok();
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000514 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000515 if (NextTok.isNot(AsmToken::Comma)) {
516 Error(NextTok.getLoc(), "',' expected");
517 return 0;
518 }
Sean Callananb9a25b72010-01-19 20:27:46 +0000519 Parser.Lex(); // Eat comma token.
Chris Lattner550276e2010-10-28 20:52:15 +0000520 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
521 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
522 E))
523 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000524 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000525
Chris Lattner550276e2010-10-28 20:52:15 +0000526 return ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, OffsetRegNum,
527 OffsetRegShifted, ShiftType, ShiftAmount,
528 Preindexed, Postindexed, Negative, Writeback,
529 S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000530 }
531
Chris Lattner550276e2010-10-28 20:52:15 +0000532 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000533}
534
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000535/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
536/// we will parse the following (were +/- means that a plus or minus is
537/// optional):
538/// +/-Rm
539/// +/-Rm, shift
540/// #offset
541/// we return false on success or an error otherwise.
542bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000543 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000544 enum ShiftType &ShiftType,
545 const MCExpr *&ShiftAmount,
546 const MCExpr *&Offset,
547 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000548 int &OffsetRegNum,
549 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000550 Negative = false;
551 OffsetRegShifted = false;
552 OffsetIsReg = false;
553 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000554 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000555 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000556 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000557 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000558 else if (NextTok.is(AsmToken::Minus)) {
559 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000560 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000561 }
562 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000563 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000564 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattner550276e2010-10-28 20:52:15 +0000565 if (ARMOperand *Op = MaybeParseRegister(false)) {
566 OffsetIsReg = true;
Sean Callanan76264762010-04-02 22:27:05 +0000567 E = Op->getEndLoc();
568 OffsetRegNum = Op->getReg();
Chris Lattner550276e2010-10-28 20:52:15 +0000569 delete Op;
Sean Callanan76264762010-04-02 22:27:05 +0000570 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000571 }
572 // If we parsed a register as the offset then their can be a shift after that
573 if (OffsetRegNum != -1) {
574 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000575 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000576 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000577 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000578
Sean Callanan18b83232010-01-19 21:44:56 +0000579 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000580 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000581 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000582 OffsetRegShifted = true;
583 }
584 }
585 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
586 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000587 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000588 if (HashTok.isNot(AsmToken::Hash))
589 return Error(HashTok.getLoc(), "'#' expected");
Sean Callanan76264762010-04-02 22:27:05 +0000590
Sean Callananb9a25b72010-01-19 20:27:46 +0000591 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000592
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000593 if (getParser().ParseExpression(Offset))
594 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000595 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000596 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000597 return false;
598}
599
600/// ParseShift as one of these two:
601/// ( lsl | lsr | asr | ror ) , # shift_amount
602/// rrx
603/// and returns true if it parses a shift otherwise it returns false.
Chris Lattner3a697562010-10-28 17:20:03 +0000604bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000605 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000606 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000607 if (Tok.isNot(AsmToken::Identifier))
608 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000609 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000610 if (ShiftName == "lsl" || ShiftName == "LSL")
611 St = Lsl;
612 else if (ShiftName == "lsr" || ShiftName == "LSR")
613 St = Lsr;
614 else if (ShiftName == "asr" || ShiftName == "ASR")
615 St = Asr;
616 else if (ShiftName == "ror" || ShiftName == "ROR")
617 St = Ror;
618 else if (ShiftName == "rrx" || ShiftName == "RRX")
619 St = Rrx;
620 else
621 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000622 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000623
624 // Rrx stands alone.
625 if (St == Rrx)
626 return false;
627
628 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000629 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000630 if (HashTok.isNot(AsmToken::Hash))
631 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000632 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000633
634 if (getParser().ParseExpression(ShiftAmount))
635 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000636
637 return false;
638}
639
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000640/// Parse a arm instruction operand. For now this parses the operand regardless
641/// of the mnemonic.
Chris Lattner550276e2010-10-28 20:52:15 +0000642ARMOperand *ARMAsmParser::ParseOperand() {
Sean Callanan76264762010-04-02 22:27:05 +0000643 SMLoc S, E;
644
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000645 switch (getLexer().getKind()) {
646 case AsmToken::Identifier:
Chris Lattner550276e2010-10-28 20:52:15 +0000647 if (ARMOperand *Op = MaybeParseRegister(true))
648 return Op;
649
Kevin Enderby515d5092009-10-15 20:48:48 +0000650 // This was not a register so parse other operands that start with an
651 // identifier (like labels) as expressions and create them as immediates.
652 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000653 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000654 if (getParser().ParseExpression(IdVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000655 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000656 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000657 return ARMOperand::CreateImm(IdVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000658 case AsmToken::LBrac:
Chris Lattner550276e2010-10-28 20:52:15 +0000659 return ParseMemory();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000660 case AsmToken::LCurly:
Chris Lattner550276e2010-10-28 20:52:15 +0000661 return ParseRegisterList();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000662 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000663 // #42 -> immediate.
664 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000665 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000666 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000667 const MCExpr *ImmVal;
668 if (getParser().ParseExpression(ImmVal))
Chris Lattner550276e2010-10-28 20:52:15 +0000669 return 0;
Sean Callanan76264762010-04-02 22:27:05 +0000670 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Chris Lattner550276e2010-10-28 20:52:15 +0000671 return ARMOperand::CreateImm(ImmVal, S, E);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000672 default:
Chris Lattner550276e2010-10-28 20:52:15 +0000673 Error(Parser.getTok().getLoc(), "unexpected token in operand");
674 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000675 }
676}
677
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000678/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000679bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000680 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000681 // Create the leading tokens for the mnemonic, split by '.' characters.
682 size_t Start = 0, Next = Name.find('.');
683 StringRef Head = Name.slice(Start, Next);
684
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000685 // Determine the predicate, if any.
686 //
687 // FIXME: We need a way to check whether a prefix supports predication,
688 // otherwise we will end up with an ambiguity for instructions that happen to
689 // end with a predicate name.
690 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
691 .Case("eq", ARMCC::EQ)
692 .Case("ne", ARMCC::NE)
693 .Case("hs", ARMCC::HS)
694 .Case("lo", ARMCC::LO)
695 .Case("mi", ARMCC::MI)
696 .Case("pl", ARMCC::PL)
697 .Case("vs", ARMCC::VS)
698 .Case("vc", ARMCC::VC)
699 .Case("hi", ARMCC::HI)
700 .Case("ls", ARMCC::LS)
701 .Case("ge", ARMCC::GE)
702 .Case("lt", ARMCC::LT)
703 .Case("gt", ARMCC::GT)
704 .Case("le", ARMCC::LE)
705 .Case("al", ARMCC::AL)
706 .Default(~0U);
Chris Lattner3a697562010-10-28 17:20:03 +0000707
708 if (CC != ~0U)
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000709 Head = Head.slice(0, Head.size() - 2);
Chris Lattner3a697562010-10-28 17:20:03 +0000710 else
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000711 CC = ARMCC::AL;
712
Chris Lattner3a697562010-10-28 17:20:03 +0000713 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
714 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC), NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000715
716 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000717 while (Next != StringRef::npos) {
718 Start = Next;
719 Next = Name.find('.', Start + 1);
720 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000721
Chris Lattner3a697562010-10-28 17:20:03 +0000722 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000723 }
724
725 // Read the remaining operands.
726 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000727 // Read the first operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000728 if (ARMOperand *Op = ParseOperand())
729 Operands.push_back(Op);
730 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000731 Parser.EatToEndOfStatement();
732 return true;
733 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000734
735 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000736 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000737
738 // Parse and remember the operand.
Chris Lattner550276e2010-10-28 20:52:15 +0000739 if (ARMOperand *Op = ParseOperand())
740 Operands.push_back(Op);
741 else {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000742 Parser.EatToEndOfStatement();
743 return true;
744 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000745 }
746 }
Chris Lattner34e53142010-09-08 05:10:46 +0000747
Chris Lattnercbf8a982010-09-11 16:18:25 +0000748 if (getLexer().isNot(AsmToken::EndOfStatement)) {
749 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000750 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000751 }
Chris Lattner34e53142010-09-08 05:10:46 +0000752 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000753 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000754}
755
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000756bool ARMAsmParser::
757MatchAndEmitInstruction(SMLoc IDLoc,
758 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
759 MCStreamer &Out) {
760 MCInst Inst;
761 unsigned ErrorInfo;
762 if (MatchInstructionImpl(Operands, Inst, ErrorInfo) == Match_Success) {
763 Out.EmitInstruction(Inst);
764 return false;
765 }
766
767 // FIXME: We should give nicer diagnostics about the exact failure.
768 Error(IDLoc, "unrecognized instruction");
769 return true;
770}
771
772
773
Kevin Enderby515d5092009-10-15 20:48:48 +0000774/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000775bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
776 StringRef IDVal = DirectiveID.getIdentifier();
777 if (IDVal == ".word")
778 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000779 else if (IDVal == ".thumb")
780 return ParseDirectiveThumb(DirectiveID.getLoc());
781 else if (IDVal == ".thumb_func")
782 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
783 else if (IDVal == ".code")
784 return ParseDirectiveCode(DirectiveID.getLoc());
785 else if (IDVal == ".syntax")
786 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000787 return true;
788}
789
790/// ParseDirectiveWord
791/// ::= .word [ expression (, expression)* ]
792bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
793 if (getLexer().isNot(AsmToken::EndOfStatement)) {
794 for (;;) {
795 const MCExpr *Value;
796 if (getParser().ParseExpression(Value))
797 return true;
798
Chris Lattneraaec2052010-01-19 19:46:13 +0000799 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000800
801 if (getLexer().is(AsmToken::EndOfStatement))
802 break;
803
804 // FIXME: Improve diagnostic.
805 if (getLexer().isNot(AsmToken::Comma))
806 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000807 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000808 }
809 }
810
Sean Callananb9a25b72010-01-19 20:27:46 +0000811 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000812 return false;
813}
814
Kevin Enderby515d5092009-10-15 20:48:48 +0000815/// ParseDirectiveThumb
816/// ::= .thumb
817bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
818 if (getLexer().isNot(AsmToken::EndOfStatement))
819 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000820 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000821
822 // TODO: set thumb mode
823 // TODO: tell the MC streamer the mode
824 // getParser().getStreamer().Emit???();
825 return false;
826}
827
828/// ParseDirectiveThumbFunc
829/// ::= .thumbfunc symbol_name
830bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000831 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000832 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
833 return Error(L, "unexpected token in .syntax directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000834 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +0000835
836 if (getLexer().isNot(AsmToken::EndOfStatement))
837 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000838 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000839
840 // TODO: mark symbol as a thumb symbol
841 // getParser().getStreamer().Emit???();
842 return false;
843}
844
845/// ParseDirectiveSyntax
846/// ::= .syntax unified | divided
847bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000848 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000849 if (Tok.isNot(AsmToken::Identifier))
850 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +0000851 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +0000852 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000853 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000854 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +0000855 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000856 else
857 return Error(L, "unrecognized syntax mode in .syntax directive");
858
859 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000860 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000861 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000862
863 // TODO tell the MC streamer the mode
864 // getParser().getStreamer().Emit???();
865 return false;
866}
867
868/// ParseDirectiveCode
869/// ::= .code 16 | 32
870bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +0000871 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +0000872 if (Tok.isNot(AsmToken::Integer))
873 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +0000874 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +0000875 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +0000876 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +0000877 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +0000878 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000879 else
880 return Error(L, "invalid operand to .code directive");
881
882 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +0000883 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000884 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000885
886 // TODO tell the MC streamer the mode
887 // getParser().getStreamer().Emit???();
888 return false;
889}
890
Sean Callanan90b70972010-04-07 20:29:34 +0000891extern "C" void LLVMInitializeARMAsmLexer();
892
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000893/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000894extern "C" void LLVMInitializeARMAsmParser() {
895 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
896 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +0000897 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000898}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000899
Chris Lattner0692ee62010-09-06 19:11:01 +0000900#define GET_REGISTER_MATCHER
901#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000902#include "ARMGenAsmMatcher.inc"