blob: 31742e36245ab1ca2836c94d215277c9e590a319 [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"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000012#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000013#include "llvm/MC/MCParser/MCAsmLexer.h"
14#include "llvm/MC/MCParser/MCAsmParser.h"
15#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000016#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000017#include "llvm/MC/MCStreamer.h"
18#include "llvm/MC/MCExpr.h"
19#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000020#include "llvm/Target/TargetRegistry.h"
21#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000022#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000027using namespace llvm;
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
Chris Lattner3a697562010-10-28 17:20:03 +000038namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000039
40class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000041
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000042class ARMAsmParser : public TargetAsmParser {
43 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000044 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
48
49 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
51
Chris Lattnere5658fa2010-10-30 04:09:10 +000052 int TryParseRegister();
Bill Wendling50d0f582010-11-18 23:43:05 +000053 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
54 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
55 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
56 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000057
Kevin Enderby9c41fa82009-10-30 22:55:57 +000058 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
60 enum ShiftType &ShiftType,
61 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
63 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000064 int &OffsetRegNum,
65 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000066 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000067 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000068 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveSyntax(SMLoc L);
72
Chris Lattner7036f8b2010-09-29 01:42:58 +000073 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000074 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000075 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000076
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000077 /// @name Auto-generated Match Functions
78 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000079
Chris Lattner0692ee62010-09-06 19:11:01 +000080#define GET_ASSEMBLER_HEADER
81#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083 /// }
84
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000085public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000086 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000087 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
88 // Initialize the set of available features.
89 setAvailableFeatures(ComputeAvailableFeatures(
90 &TM.getSubtarget<ARMSubtarget>()));
91 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000092
Benjamin Kramer38e59892010-07-14 22:38:02 +000093 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000094 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095 virtual bool ParseDirective(AsmToken DirectiveID);
96};
Jim Grosbach16c74252010-10-29 14:46:02 +000097} // end anonymous namespace
98
Chris Lattner3a697562010-10-28 17:20:03 +000099namespace {
100
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000101/// ARMOperand - Instances of this class represent a parsed ARM machine
102/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000103class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000104 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000105 CondCode,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000106 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000107 Memory,
108 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000109 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000110 DPRRegisterList,
111 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000112 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000113 } Kind;
114
Sean Callanan76264762010-04-02 22:27:05 +0000115 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000116 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000117
118 union {
119 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000120 ARMCC::CondCodes Val;
121 } CC;
122
123 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124 const char *Data;
125 unsigned Length;
126 } Tok;
127
128 struct {
129 unsigned RegNum;
130 } Reg;
131
Bill Wendling8155e5b2010-11-06 22:19:43 +0000132 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000133 const MCExpr *Val;
134 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000135
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000136 // This is for all forms of ARM address expressions
137 struct {
138 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000139 unsigned OffsetRegNum; // used when OffsetIsReg is true
140 const MCExpr *Offset; // used when OffsetIsReg is false
141 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
142 enum ShiftType ShiftType; // used when OffsetRegShifted is true
143 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000144 unsigned Preindexed : 1;
145 unsigned Postindexed : 1;
146 unsigned OffsetIsReg : 1;
147 unsigned Negative : 1; // only used when OffsetIsReg is true
148 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000149 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000151
Bill Wendling146018f2010-11-06 21:42:12 +0000152 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
153public:
Sean Callanan76264762010-04-02 22:27:05 +0000154 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
155 Kind = o.Kind;
156 StartLoc = o.StartLoc;
157 EndLoc = o.EndLoc;
158 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000159 case CondCode:
160 CC = o.CC;
161 break;
Sean Callanan76264762010-04-02 22:27:05 +0000162 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000163 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000164 break;
165 case Register:
166 Reg = o.Reg;
167 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000168 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000169 case DPRRegisterList:
170 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000171 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000172 break;
Sean Callanan76264762010-04-02 22:27:05 +0000173 case Immediate:
174 Imm = o.Imm;
175 break;
176 case Memory:
177 Mem = o.Mem;
178 break;
179 }
180 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000181
Sean Callanan76264762010-04-02 22:27:05 +0000182 /// 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 {
Bill Wendling7729e062010-11-09 22:44:22 +0000198 assert(Kind == Register && "Invalid access!");
199 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200 }
201
Bill Wendling5fa22a12010-11-09 23:28:44 +0000202 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000203 assert((Kind == RegisterList || Kind == DPRRegisterList ||
204 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000205 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000206 }
207
Kevin Enderbycfe07242009-10-13 22:19:02 +0000208 const MCExpr *getImm() const {
209 assert(Kind == Immediate && "Invalid access!");
210 return Imm.Val;
211 }
212
Daniel Dunbar8462b302010-08-11 06:36:53 +0000213 bool isCondCode() const { return Kind == CondCode; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000214 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000215 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000216 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000217 bool isDPRRegList() const { return Kind == DPRRegisterList; }
218 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000219 bool isToken() const { return Kind == Token; }
220 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000221 bool isMemMode5() const {
222 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
223 Mem.Writeback || Mem.Negative)
224 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000225
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000226 // If there is an offset expression, make sure it's valid.
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000227 if (!Mem.Offset) return true;
228
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000229 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000230 if (!CE) return false;
231
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000232 // The offset must be a multiple of 4 in the range 0-1020.
233 int64_t Value = CE->getValue();
234 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
235 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000236 bool isMemModeThumb() const {
237 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
238 return false;
239
240 if (!Mem.Offset) return true;
241
242 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
243 if (!CE) return false;
244
245 // The offset must be a multiple of 4 in the range 0-124.
246 uint64_t Value = CE->getValue();
247 return ((Value & 0x3) == 0 && Value <= 124);
248 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000249
250 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000251 // Add as immediates when possible. Null MCExpr = 0.
252 if (Expr == 0)
253 Inst.addOperand(MCOperand::CreateImm(0));
254 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000255 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
256 else
257 Inst.addOperand(MCOperand::CreateExpr(Expr));
258 }
259
Daniel Dunbar8462b302010-08-11 06:36:53 +0000260 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000261 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000262 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000263 // FIXME: What belongs here?
264 Inst.addOperand(MCOperand::CreateReg(0));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000265 }
266
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000267 void addRegOperands(MCInst &Inst, unsigned N) const {
268 assert(N == 1 && "Invalid number of operands!");
269 Inst.addOperand(MCOperand::CreateReg(getReg()));
270 }
271
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000272 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000273 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000274 const SmallVectorImpl<unsigned> &RegList = getRegList();
275 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000276 I = RegList.begin(), E = RegList.end(); I != E; ++I)
277 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000278 }
279
Bill Wendling0f630752010-11-17 04:32:08 +0000280 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
281 addRegListOperands(Inst, N);
282 }
283
284 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
285 addRegListOperands(Inst, N);
286 }
287
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000288 void addImmOperands(MCInst &Inst, unsigned N) const {
289 assert(N == 1 && "Invalid number of operands!");
290 addExpr(Inst, getImm());
291 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000292
Chris Lattner14b93852010-10-29 00:27:31 +0000293 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
294 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000295
Chris Lattner14b93852010-10-29 00:27:31 +0000296 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000297 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000298
Jim Grosbach80eb2332010-10-29 17:41:25 +0000299 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
300 // the difference?
301 if (Mem.Offset) {
302 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000303 assert(CE && "Non-constant mode 5 offset operand!");
304
Jim Grosbach80eb2332010-10-29 17:41:25 +0000305 // The MCInst offset operand doesn't include the low two bits (like
306 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000307 int64_t Offset = CE->getValue() / 4;
308 if (Offset >= 0)
309 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
310 Offset)));
311 else
312 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
313 -Offset)));
314 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000315 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000316 }
Chris Lattner14b93852010-10-29 00:27:31 +0000317 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000318
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000319 void addMemModeThumbOperands(MCInst &Inst, unsigned N) const {
320 assert(N == 3 && isMemModeThumb() && "Invalid number of operands!");
321 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
322
323 if (Mem.Offset) {
324 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
325 assert(CE && "Non-constant mode offset operand!");
326 Inst.addOperand(MCOperand::CreateImm(CE->getValue() / 4));
327 Inst.addOperand(MCOperand::CreateReg(0));
328 } else {
329 Inst.addOperand(MCOperand::CreateImm(0));
330 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
331 }
332 }
333
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000334 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000335
Chris Lattner3a697562010-10-28 17:20:03 +0000336 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
337 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000338 Op->CC.Val = CC;
339 Op->StartLoc = S;
340 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000341 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000342 }
343
Chris Lattner3a697562010-10-28 17:20:03 +0000344 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
345 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000346 Op->Tok.Data = Str.data();
347 Op->Tok.Length = Str.size();
348 Op->StartLoc = S;
349 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000350 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000351 }
352
Bill Wendling50d0f582010-11-18 23:43:05 +0000353 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000354 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000355 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000356 Op->StartLoc = S;
357 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000358 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000359 }
360
Bill Wendling7729e062010-11-09 22:44:22 +0000361 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000362 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000363 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000364 KindTy Kind = RegisterList;
365
366 if (ARM::DPRRegClass.contains(Regs.front().first))
367 Kind = DPRRegisterList;
368 else if (ARM::SPRRegClass.contains(Regs.front().first))
369 Kind = SPRRegisterList;
370
371 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000372 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000373 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000374 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000375 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000376 Op->StartLoc = StartLoc;
377 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000378 return Op;
379 }
380
Chris Lattner3a697562010-10-28 17:20:03 +0000381 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
382 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000383 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000384 Op->StartLoc = S;
385 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000386 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000387 }
388
Chris Lattner3a697562010-10-28 17:20:03 +0000389 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
390 const MCExpr *Offset, unsigned OffsetRegNum,
391 bool OffsetRegShifted, enum ShiftType ShiftType,
392 const MCExpr *ShiftAmount, bool Preindexed,
393 bool Postindexed, bool Negative, bool Writeback,
394 SMLoc S, SMLoc E) {
395 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000396 Op->Mem.BaseRegNum = BaseRegNum;
397 Op->Mem.OffsetIsReg = OffsetIsReg;
398 Op->Mem.Offset = Offset;
399 Op->Mem.OffsetRegNum = OffsetRegNum;
400 Op->Mem.OffsetRegShifted = OffsetRegShifted;
401 Op->Mem.ShiftType = ShiftType;
402 Op->Mem.ShiftAmount = ShiftAmount;
403 Op->Mem.Preindexed = Preindexed;
404 Op->Mem.Postindexed = Postindexed;
405 Op->Mem.Negative = Negative;
406 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000407
Sean Callanan76264762010-04-02 22:27:05 +0000408 Op->StartLoc = S;
409 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000410 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000411 }
412};
413
414} // end anonymous namespace.
415
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000416void ARMOperand::dump(raw_ostream &OS) const {
417 switch (Kind) {
418 case CondCode:
419 OS << ARMCondCodeToString(getCondCode());
420 break;
421 case Immediate:
422 getImm()->print(OS);
423 break;
424 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000425 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000426 break;
427 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000428 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000429 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000430 case RegisterList:
431 case DPRRegisterList:
432 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000433 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000434
Bill Wendling5fa22a12010-11-09 23:28:44 +0000435 const SmallVectorImpl<unsigned> &RegList = getRegList();
436 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000437 I = RegList.begin(), E = RegList.end(); I != E; ) {
438 OS << *I;
439 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000440 }
441
442 OS << ">";
443 break;
444 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000445 case Token:
446 OS << "'" << getToken() << "'";
447 break;
448 }
449}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000450
451/// @name Auto-generated Match Functions
452/// {
453
454static unsigned MatchRegisterName(StringRef Name);
455
456/// }
457
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000458/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000459/// and if it is a register name the token is eaten and the register number is
460/// returned. Otherwise return -1.
461///
462int ARMAsmParser::TryParseRegister() {
463 const AsmToken &Tok = Parser.getTok();
464 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000465
Chris Lattnere5658fa2010-10-30 04:09:10 +0000466 // FIXME: Validate register for the current architecture; we have to do
467 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000468 unsigned RegNum = MatchRegisterName(Tok.getString());
469 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000470 return -1;
471 Parser.Lex(); // Eat identifier token.
472 return RegNum;
473}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000474
475
Bill Wendling50d0f582010-11-18 23:43:05 +0000476/// Try to parse a register name. The token must be an Identifier when called.
477/// If it's a register, an AsmOperand is created. Another AsmOperand is created
478/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000479///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000480/// TODO this is likely to change to allow different register types and or to
481/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000482bool ARMAsmParser::
483TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000484 SMLoc S = Parser.getTok().getLoc();
485 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000486 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000487 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000488
Bill Wendling50d0f582010-11-18 23:43:05 +0000489 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000490
Chris Lattnere5658fa2010-10-30 04:09:10 +0000491 const AsmToken &ExclaimTok = Parser.getTok();
492 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000493 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
494 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000495 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000496 }
497
Bill Wendling50d0f582010-11-18 23:43:05 +0000498 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000499}
500
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000501/// Parse a register list, return it if successful else return null. The first
502/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000503bool ARMAsmParser::
504ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000505 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000506 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000507 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000508
Bill Wendling7729e062010-11-09 22:44:22 +0000509 // Read the rest of the registers in the list.
510 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000511 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000512
Bill Wendling7729e062010-11-09 22:44:22 +0000513 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000514 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000515 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000516
Sean Callanan18b83232010-01-19 21:44:56 +0000517 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000518 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000519 if (RegTok.isNot(AsmToken::Identifier)) {
520 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000521 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000522 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000523
Bill Wendling1d6a2652010-11-06 10:40:24 +0000524 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000525 if (RegNum == -1) {
526 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000527 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000528 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000529
Bill Wendlinge7176102010-11-06 22:36:58 +0000530 if (IsRange) {
531 int Reg = PrevRegNum;
532 do {
533 ++Reg;
534 Registers.push_back(std::make_pair(Reg, RegLoc));
535 } while (Reg != RegNum);
536 } else {
537 Registers.push_back(std::make_pair(RegNum, RegLoc));
538 }
539
540 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000541 } while (Parser.getTok().is(AsmToken::Comma) ||
542 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000543
544 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000545 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000546 if (RCurlyTok.isNot(AsmToken::RCurly)) {
547 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000548 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000549 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000550
Bill Wendlinge7176102010-11-06 22:36:58 +0000551 SMLoc E = RCurlyTok.getLoc();
552 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000553
Bill Wendlinge7176102010-11-06 22:36:58 +0000554 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000555 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000556 RI = Registers.begin(), RE = Registers.end();
557
Bill Wendlinge7176102010-11-06 22:36:58 +0000558 DenseMap<unsigned, bool> RegMap;
559 RegMap[RI->first] = true;
560
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000561 unsigned HighRegNum = RI->first;
562 bool EmittedWarning = false;
563
Bill Wendlinge7176102010-11-06 22:36:58 +0000564 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000565 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000566 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000567
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000568 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000569 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000570 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000571 }
572
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000573 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000574 Warning(RegInfo.second,
575 "register not in ascending order in register list");
576
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000577 RegMap[Reg] = true;
578 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000579 }
580
Bill Wendling50d0f582010-11-18 23:43:05 +0000581 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
582 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000583}
584
Bill Wendlinge7176102010-11-06 22:36:58 +0000585/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000586/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000587///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000588/// TODO Only preindexing and postindexing addressing are started, unindexed
589/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000590bool ARMAsmParser::
591ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000592 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000593 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000594 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000595 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000596 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000597
Sean Callanan18b83232010-01-19 21:44:56 +0000598 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000599 if (BaseRegTok.isNot(AsmToken::Identifier)) {
600 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000601 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000602 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000603 int BaseRegNum = TryParseRegister();
604 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000605 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000606 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000607 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000608
609 bool Preindexed = false;
610 bool Postindexed = false;
611 bool OffsetIsReg = false;
612 bool Negative = false;
613 bool Writeback = false;
614
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000615 // First look for preindexed address forms, that is after the "[Rn" we now
616 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000617 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000618 if (Tok.is(AsmToken::Comma)) {
619 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000620 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000621 int OffsetRegNum;
622 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000623 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000624 const MCExpr *ShiftAmount = 0;
625 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000626 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
627 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000628 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000629 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000630 if (RBracTok.isNot(AsmToken::RBrac)) {
631 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000632 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000633 }
Sean Callanan76264762010-04-02 22:27:05 +0000634 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000635 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000636
Jim Grosbach03f44a02010-11-29 23:18:01 +0000637
Sean Callanan18b83232010-01-19 21:44:56 +0000638 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000639 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000640 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000641 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
642 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000643 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000644 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000645 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000646
647 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
648 OffsetRegNum, OffsetRegShifted,
649 ShiftType, ShiftAmount, Preindexed,
650 Postindexed, Negative, Writeback,
651 S, E));
652 if (WBOp)
653 Operands.push_back(WBOp);
654
655 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000656 }
657 // The "[Rn" we have so far was not followed by a comma.
658 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000659 // If there's anything other than the right brace, this is a post indexing
660 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000661 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000662 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000663
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000664 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000665 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000666 enum ShiftType ShiftType = Lsl;
667 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000668 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000669
Sean Callanan18b83232010-01-19 21:44:56 +0000670 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000671
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000672 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000673 Postindexed = true;
674 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000675
Chris Lattner550276e2010-10-28 20:52:15 +0000676 if (NextTok.isNot(AsmToken::Comma)) {
677 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000678 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000679 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000680
Sean Callananb9a25b72010-01-19 20:27:46 +0000681 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000682
Chris Lattner550276e2010-10-28 20:52:15 +0000683 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000684 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000685 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000686 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000687 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000688
Bill Wendling50d0f582010-11-18 23:43:05 +0000689 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
690 OffsetRegNum, OffsetRegShifted,
691 ShiftType, ShiftAmount, Preindexed,
692 Postindexed, Negative, Writeback,
693 S, E));
694 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000695 }
696
Bill Wendling50d0f582010-11-18 23:43:05 +0000697 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000698}
699
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000700/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
701/// we will parse the following (were +/- means that a plus or minus is
702/// optional):
703/// +/-Rm
704/// +/-Rm, shift
705/// #offset
706/// we return false on success or an error otherwise.
707bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000708 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000709 enum ShiftType &ShiftType,
710 const MCExpr *&ShiftAmount,
711 const MCExpr *&Offset,
712 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000713 int &OffsetRegNum,
714 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000715 Negative = false;
716 OffsetRegShifted = false;
717 OffsetIsReg = false;
718 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000719 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000720 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000721 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000722 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000723 else if (NextTok.is(AsmToken::Minus)) {
724 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000725 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000726 }
727 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000728 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000729 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000730 SMLoc CurLoc = OffsetRegTok.getLoc();
731 OffsetRegNum = TryParseRegister();
732 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000733 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000734 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000735 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000736 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000737
Bill Wendling12f40e92010-11-06 10:51:53 +0000738 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000739 if (OffsetRegNum != -1) {
740 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000741 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000742 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000743 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000744
Sean Callanan18b83232010-01-19 21:44:56 +0000745 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000746 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000747 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000748 OffsetRegShifted = true;
749 }
750 }
751 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
752 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000753 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000754 if (HashTok.isNot(AsmToken::Hash))
755 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000756
Sean Callananb9a25b72010-01-19 20:27:46 +0000757 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000758
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000759 if (getParser().ParseExpression(Offset))
760 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000761 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000762 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000763 return false;
764}
765
766/// ParseShift as one of these two:
767/// ( lsl | lsr | asr | ror ) , # shift_amount
768/// rrx
769/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000770bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000771 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000772 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000773 if (Tok.isNot(AsmToken::Identifier))
774 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000775 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000776 if (ShiftName == "lsl" || ShiftName == "LSL")
777 St = Lsl;
778 else if (ShiftName == "lsr" || ShiftName == "LSR")
779 St = Lsr;
780 else if (ShiftName == "asr" || ShiftName == "ASR")
781 St = Asr;
782 else if (ShiftName == "ror" || ShiftName == "ROR")
783 St = Ror;
784 else if (ShiftName == "rrx" || ShiftName == "RRX")
785 St = Rrx;
786 else
787 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000788 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000789
790 // Rrx stands alone.
791 if (St == Rrx)
792 return false;
793
794 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000795 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000796 if (HashTok.isNot(AsmToken::Hash))
797 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000798 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000799
800 if (getParser().ParseExpression(ShiftAmount))
801 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000802
803 return false;
804}
805
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000806/// Parse a arm instruction operand. For now this parses the operand regardless
807/// of the mnemonic.
Bill Wendling50d0f582010-11-18 23:43:05 +0000808bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
Sean Callanan76264762010-04-02 22:27:05 +0000809 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000810 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000811 default:
812 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000813 return true;
814 case AsmToken::Identifier: {
815 if (!TryParseRegisterWithWriteBack(Operands))
816 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000817
Kevin Enderby515d5092009-10-15 20:48:48 +0000818 // This was not a register so parse other operands that start with an
819 // identifier (like labels) as expressions and create them as immediates.
820 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000821 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000822 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000823 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000824 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000825 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
826 return false;
827 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000828 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000829 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000830 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000831 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000832 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000833 // #42 -> immediate.
834 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000835 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000836 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000837 const MCExpr *ImmVal;
838 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000839 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000840 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000841 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
842 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000843 }
844}
845
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000846/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000847bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000848 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000849 // Create the leading tokens for the mnemonic, split by '.' characters.
850 size_t Start = 0, Next = Name.find('.');
851 StringRef Head = Name.slice(Start, Next);
852
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000853 // Determine the predicate, if any.
854 //
855 // FIXME: We need a way to check whether a prefix supports predication,
856 // otherwise we will end up with an ambiguity for instructions that happen to
857 // end with a predicate name.
Jim Grosbach3df518e2010-10-29 21:56:51 +0000858 // FIXME: Likewise, some arithmetic instructions have an 's' prefix which
859 // indicates to update the condition codes. Those instructions have an
860 // additional immediate operand which encodes the prefix as reg0 or CPSR.
861 // Just checking for a suffix of 's' definitely creates ambiguities; e.g,
862 // the SMMLS instruction.
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000863 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
864 .Case("eq", ARMCC::EQ)
865 .Case("ne", ARMCC::NE)
866 .Case("hs", ARMCC::HS)
867 .Case("lo", ARMCC::LO)
868 .Case("mi", ARMCC::MI)
869 .Case("pl", ARMCC::PL)
870 .Case("vs", ARMCC::VS)
871 .Case("vc", ARMCC::VC)
872 .Case("hi", ARMCC::HI)
873 .Case("ls", ARMCC::LS)
874 .Case("ge", ARMCC::GE)
875 .Case("lt", ARMCC::LT)
876 .Case("gt", ARMCC::GT)
877 .Case("le", ARMCC::LE)
878 .Case("al", ARMCC::AL)
879 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000880
Chris Lattnerdba34d82010-10-30 04:35:59 +0000881 if (CC == ~0U ||
882 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000883 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000884 } else {
885 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000886 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000887
Chris Lattner3a697562010-10-28 17:20:03 +0000888 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +0000889
890 if (Head != "trap")
891 // FIXME: Should only add this operand for predicated instructions
892 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC),
893 NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000894
895 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000896 while (Next != StringRef::npos) {
897 Start = Next;
898 Next = Name.find('.', Start + 1);
899 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000900
Chris Lattner3a697562010-10-28 17:20:03 +0000901 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000902 }
903
904 // Read the remaining operands.
905 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000906 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +0000907 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000908 Parser.EatToEndOfStatement();
909 return true;
910 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000911
912 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000913 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000914
915 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +0000916 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000917 Parser.EatToEndOfStatement();
918 return true;
919 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000920 }
921 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000922
Chris Lattnercbf8a982010-09-11 16:18:25 +0000923 if (getLexer().isNot(AsmToken::EndOfStatement)) {
924 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000925 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000926 }
Bill Wendling146018f2010-11-06 21:42:12 +0000927
Chris Lattner34e53142010-09-08 05:10:46 +0000928 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000929 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000930}
931
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000932bool ARMAsmParser::
933MatchAndEmitInstruction(SMLoc IDLoc,
934 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
935 MCStreamer &Out) {
936 MCInst Inst;
937 unsigned ErrorInfo;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000938 switch (MatchInstructionImpl(Operands, Inst, ErrorInfo)) {
939 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000940 Out.EmitInstruction(Inst);
941 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +0000942 case Match_MissingFeature:
943 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
944 return true;
945 case Match_InvalidOperand: {
946 SMLoc ErrorLoc = IDLoc;
947 if (ErrorInfo != ~0U) {
948 if (ErrorInfo >= Operands.size())
949 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +0000950
Chris Lattnere73d4f82010-10-28 21:41:58 +0000951 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
952 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
953 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000954
Chris Lattnere73d4f82010-10-28 21:41:58 +0000955 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000956 }
Chris Lattnere73d4f82010-10-28 21:41:58 +0000957 case Match_MnemonicFail:
958 return Error(IDLoc, "unrecognized instruction mnemonic");
959 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000960
Eric Christopherc223e2b2010-10-29 09:26:59 +0000961 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +0000962 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000963}
964
Kevin Enderby515d5092009-10-15 20:48:48 +0000965/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000966bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
967 StringRef IDVal = DirectiveID.getIdentifier();
968 if (IDVal == ".word")
969 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +0000970 else if (IDVal == ".thumb")
971 return ParseDirectiveThumb(DirectiveID.getLoc());
972 else if (IDVal == ".thumb_func")
973 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
974 else if (IDVal == ".code")
975 return ParseDirectiveCode(DirectiveID.getLoc());
976 else if (IDVal == ".syntax")
977 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000978 return true;
979}
980
981/// ParseDirectiveWord
982/// ::= .word [ expression (, expression)* ]
983bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
984 if (getLexer().isNot(AsmToken::EndOfStatement)) {
985 for (;;) {
986 const MCExpr *Value;
987 if (getParser().ParseExpression(Value))
988 return true;
989
Chris Lattneraaec2052010-01-19 19:46:13 +0000990 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000991
992 if (getLexer().is(AsmToken::EndOfStatement))
993 break;
Jim Grosbach16c74252010-10-29 14:46:02 +0000994
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000995 // FIXME: Improve diagnostic.
996 if (getLexer().isNot(AsmToken::Comma))
997 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +0000998 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000999 }
1000 }
1001
Sean Callananb9a25b72010-01-19 20:27:46 +00001002 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001003 return false;
1004}
1005
Kevin Enderby515d5092009-10-15 20:48:48 +00001006/// ParseDirectiveThumb
1007/// ::= .thumb
1008bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1009 if (getLexer().isNot(AsmToken::EndOfStatement))
1010 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001011 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001012
1013 // TODO: set thumb mode
1014 // TODO: tell the MC streamer the mode
1015 // getParser().getStreamer().Emit???();
1016 return false;
1017}
1018
1019/// ParseDirectiveThumbFunc
1020/// ::= .thumbfunc symbol_name
1021bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001022 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001023 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001024 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001025 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001026 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001027 if (getLexer().isNot(AsmToken::EndOfStatement))
1028 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001029 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001030
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001031 // Mark symbol as a thumb symbol.
1032 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1033 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001034 return false;
1035}
1036
1037/// ParseDirectiveSyntax
1038/// ::= .syntax unified | divided
1039bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001040 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001041 if (Tok.isNot(AsmToken::Identifier))
1042 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001043 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001044 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001045 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001046 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001047 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001048 else
1049 return Error(L, "unrecognized syntax mode in .syntax directive");
1050
1051 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001052 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001053 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001054
1055 // TODO tell the MC streamer the mode
1056 // getParser().getStreamer().Emit???();
1057 return false;
1058}
1059
1060/// ParseDirectiveCode
1061/// ::= .code 16 | 32
1062bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001063 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001064 if (Tok.isNot(AsmToken::Integer))
1065 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001066 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001067 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001068 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001069 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001070 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001071 else
1072 return Error(L, "invalid operand to .code directive");
1073
1074 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001075 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001076 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001077
Jim Grosbach2a301702010-11-05 22:40:53 +00001078 if (Val == 16)
1079 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1080 else
1081 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1082
Kevin Enderby515d5092009-10-15 20:48:48 +00001083 return false;
1084}
1085
Sean Callanan90b70972010-04-07 20:29:34 +00001086extern "C" void LLVMInitializeARMAsmLexer();
1087
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001088/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001089extern "C" void LLVMInitializeARMAsmParser() {
1090 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1091 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001092 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001093}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001094
Chris Lattner0692ee62010-09-06 19:11:01 +00001095#define GET_REGISTER_MATCHER
1096#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001097#include "ARMGenAsmMatcher.inc"