blob: d0d2368aabf20ed13fc00824132e92afc38b36f8 [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,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000106 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000107 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000108 Memory,
109 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000110 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000111 DPRRegisterList,
112 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000113 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000114 } Kind;
115
Sean Callanan76264762010-04-02 22:27:05 +0000116 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000117 SmallVector<unsigned, 8> Registers;
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;
131 } Reg;
132
Bill Wendling8155e5b2010-11-06 22:19:43 +0000133 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000134 const MCExpr *Val;
135 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000136
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000137 // This is for all forms of ARM address expressions
138 struct {
139 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000140 unsigned OffsetRegNum; // used when OffsetIsReg is true
141 const MCExpr *Offset; // used when OffsetIsReg is false
142 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
143 enum ShiftType ShiftType; // used when OffsetRegShifted is true
144 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000145 unsigned Preindexed : 1;
146 unsigned Postindexed : 1;
147 unsigned OffsetIsReg : 1;
148 unsigned Negative : 1; // only used when OffsetIsReg is true
149 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000150 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000152
Bill Wendling146018f2010-11-06 21:42:12 +0000153 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
154public:
Sean Callanan76264762010-04-02 22:27:05 +0000155 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
156 Kind = o.Kind;
157 StartLoc = o.StartLoc;
158 EndLoc = o.EndLoc;
159 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000160 case CondCode:
161 CC = o.CC;
162 break;
Sean Callanan76264762010-04-02 22:27:05 +0000163 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000164 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000165 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000166 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000167 case Register:
168 Reg = o.Reg;
169 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000170 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000171 case DPRRegisterList:
172 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000173 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000174 break;
Sean Callanan76264762010-04-02 22:27:05 +0000175 case Immediate:
176 Imm = o.Imm;
177 break;
178 case Memory:
179 Mem = o.Mem;
180 break;
181 }
182 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000183
Sean Callanan76264762010-04-02 22:27:05 +0000184 /// getStartLoc - Get the location of the first token of this operand.
185 SMLoc getStartLoc() const { return StartLoc; }
186 /// getEndLoc - Get the location of the last token of this operand.
187 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000188
Daniel Dunbar8462b302010-08-11 06:36:53 +0000189 ARMCC::CondCodes getCondCode() const {
190 assert(Kind == CondCode && "Invalid access!");
191 return CC.Val;
192 }
193
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 StringRef getToken() const {
195 assert(Kind == Token && "Invalid access!");
196 return StringRef(Tok.Data, Tok.Length);
197 }
198
199 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000200 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000201 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000202 }
203
Bill Wendling5fa22a12010-11-09 23:28:44 +0000204 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000205 assert((Kind == RegisterList || Kind == DPRRegisterList ||
206 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000207 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000208 }
209
Kevin Enderbycfe07242009-10-13 22:19:02 +0000210 const MCExpr *getImm() const {
211 assert(Kind == Immediate && "Invalid access!");
212 return Imm.Val;
213 }
214
Daniel Dunbar8462b302010-08-11 06:36:53 +0000215 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000216 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000217 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000218 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000219 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000220 bool isDPRRegList() const { return Kind == DPRRegisterList; }
221 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000222 bool isToken() const { return Kind == Token; }
223 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000224 bool isMemMode5() const {
225 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
226 Mem.Writeback || Mem.Negative)
227 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000228
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000229 // If there is an offset expression, make sure it's valid.
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000230 if (!Mem.Offset) return true;
231
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000232 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000233 if (!CE) return false;
234
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000235 // The offset must be a multiple of 4 in the range 0-1020.
236 int64_t Value = CE->getValue();
237 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
238 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000239 bool isMemModeThumb() const {
240 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
241 return false;
242
243 if (!Mem.Offset) return true;
244
245 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
246 if (!CE) return false;
247
248 // The offset must be a multiple of 4 in the range 0-124.
249 uint64_t Value = CE->getValue();
250 return ((Value & 0x3) == 0 && Value <= 124);
251 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000252
253 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000254 // Add as immediates when possible. Null MCExpr = 0.
255 if (Expr == 0)
256 Inst.addOperand(MCOperand::CreateImm(0));
257 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000258 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
259 else
260 Inst.addOperand(MCOperand::CreateExpr(Expr));
261 }
262
Daniel Dunbar8462b302010-08-11 06:36:53 +0000263 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000264 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000265 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000266 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
267 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000268 }
269
Jim Grosbachd67641b2010-12-06 18:21:12 +0000270 void addCCOutOperands(MCInst &Inst, unsigned N) const {
271 assert(N == 1 && "Invalid number of operands!");
272 Inst.addOperand(MCOperand::CreateReg(getReg()));
273 }
274
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000275 void addRegOperands(MCInst &Inst, unsigned N) const {
276 assert(N == 1 && "Invalid number of operands!");
277 Inst.addOperand(MCOperand::CreateReg(getReg()));
278 }
279
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000280 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000281 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000282 const SmallVectorImpl<unsigned> &RegList = getRegList();
283 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000284 I = RegList.begin(), E = RegList.end(); I != E; ++I)
285 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000286 }
287
Bill Wendling0f630752010-11-17 04:32:08 +0000288 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
289 addRegListOperands(Inst, N);
290 }
291
292 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
293 addRegListOperands(Inst, N);
294 }
295
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000296 void addImmOperands(MCInst &Inst, unsigned N) const {
297 assert(N == 1 && "Invalid number of operands!");
298 addExpr(Inst, getImm());
299 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000300
Chris Lattner14b93852010-10-29 00:27:31 +0000301 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
302 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000303
Chris Lattner14b93852010-10-29 00:27:31 +0000304 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000305 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000306
Jim Grosbach80eb2332010-10-29 17:41:25 +0000307 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
308 // the difference?
309 if (Mem.Offset) {
310 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000311 assert(CE && "Non-constant mode 5 offset operand!");
312
Jim Grosbach80eb2332010-10-29 17:41:25 +0000313 // The MCInst offset operand doesn't include the low two bits (like
314 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000315 int64_t Offset = CE->getValue() / 4;
316 if (Offset >= 0)
317 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
318 Offset)));
319 else
320 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
321 -Offset)));
322 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000323 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000324 }
Chris Lattner14b93852010-10-29 00:27:31 +0000325 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000326
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000327 void addMemModeThumbOperands(MCInst &Inst, unsigned N) const {
328 assert(N == 3 && isMemModeThumb() && "Invalid number of operands!");
329 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
330
331 if (Mem.Offset) {
332 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
333 assert(CE && "Non-constant mode offset operand!");
Bill Wendling1fd374e2010-11-30 22:57:21 +0000334 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000335 Inst.addOperand(MCOperand::CreateReg(0));
336 } else {
337 Inst.addOperand(MCOperand::CreateImm(0));
338 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
339 }
340 }
341
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000342 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000343
Chris Lattner3a697562010-10-28 17:20:03 +0000344 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
345 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000346 Op->CC.Val = CC;
347 Op->StartLoc = S;
348 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000349 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000350 }
351
Jim Grosbachd67641b2010-12-06 18:21:12 +0000352 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
353 ARMOperand *Op = new ARMOperand(CCOut);
354 Op->Reg.RegNum = RegNum;
355 Op->StartLoc = S;
356 Op->EndLoc = S;
357 return Op;
358 }
359
Chris Lattner3a697562010-10-28 17:20:03 +0000360 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
361 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000362 Op->Tok.Data = Str.data();
363 Op->Tok.Length = Str.size();
364 Op->StartLoc = S;
365 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000366 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000367 }
368
Bill Wendling50d0f582010-11-18 23:43:05 +0000369 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000370 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000371 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000372 Op->StartLoc = S;
373 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000374 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000375 }
376
Bill Wendling7729e062010-11-09 22:44:22 +0000377 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000378 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000379 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000380 KindTy Kind = RegisterList;
381
382 if (ARM::DPRRegClass.contains(Regs.front().first))
383 Kind = DPRRegisterList;
384 else if (ARM::SPRRegClass.contains(Regs.front().first))
385 Kind = SPRRegisterList;
386
387 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000388 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000389 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000390 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000391 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000392 Op->StartLoc = StartLoc;
393 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000394 return Op;
395 }
396
Chris Lattner3a697562010-10-28 17:20:03 +0000397 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
398 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000399 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000400 Op->StartLoc = S;
401 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000402 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000403 }
404
Chris Lattner3a697562010-10-28 17:20:03 +0000405 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
406 const MCExpr *Offset, unsigned OffsetRegNum,
407 bool OffsetRegShifted, enum ShiftType ShiftType,
408 const MCExpr *ShiftAmount, bool Preindexed,
409 bool Postindexed, bool Negative, bool Writeback,
410 SMLoc S, SMLoc E) {
411 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000412 Op->Mem.BaseRegNum = BaseRegNum;
413 Op->Mem.OffsetIsReg = OffsetIsReg;
414 Op->Mem.Offset = Offset;
415 Op->Mem.OffsetRegNum = OffsetRegNum;
416 Op->Mem.OffsetRegShifted = OffsetRegShifted;
417 Op->Mem.ShiftType = ShiftType;
418 Op->Mem.ShiftAmount = ShiftAmount;
419 Op->Mem.Preindexed = Preindexed;
420 Op->Mem.Postindexed = Postindexed;
421 Op->Mem.Negative = Negative;
422 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000423
Sean Callanan76264762010-04-02 22:27:05 +0000424 Op->StartLoc = S;
425 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000426 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000427 }
428};
429
430} // end anonymous namespace.
431
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000432void ARMOperand::dump(raw_ostream &OS) const {
433 switch (Kind) {
434 case CondCode:
435 OS << ARMCondCodeToString(getCondCode());
436 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000437 case CCOut:
438 OS << "<ccout " << getReg() << ">";
439 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000440 case Immediate:
441 getImm()->print(OS);
442 break;
443 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000444 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000445 break;
446 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000447 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000448 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000449 case RegisterList:
450 case DPRRegisterList:
451 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000452 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000453
Bill Wendling5fa22a12010-11-09 23:28:44 +0000454 const SmallVectorImpl<unsigned> &RegList = getRegList();
455 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000456 I = RegList.begin(), E = RegList.end(); I != E; ) {
457 OS << *I;
458 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000459 }
460
461 OS << ">";
462 break;
463 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000464 case Token:
465 OS << "'" << getToken() << "'";
466 break;
467 }
468}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000469
470/// @name Auto-generated Match Functions
471/// {
472
473static unsigned MatchRegisterName(StringRef Name);
474
475/// }
476
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000477/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000478/// and if it is a register name the token is eaten and the register number is
479/// returned. Otherwise return -1.
480///
481int ARMAsmParser::TryParseRegister() {
482 const AsmToken &Tok = Parser.getTok();
483 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000484
Chris Lattnere5658fa2010-10-30 04:09:10 +0000485 // FIXME: Validate register for the current architecture; we have to do
486 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000487 unsigned RegNum = MatchRegisterName(Tok.getString());
488 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000489 return -1;
490 Parser.Lex(); // Eat identifier token.
491 return RegNum;
492}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000493
494
Bill Wendling50d0f582010-11-18 23:43:05 +0000495/// Try to parse a register name. The token must be an Identifier when called.
496/// If it's a register, an AsmOperand is created. Another AsmOperand is created
497/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000498///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000499/// TODO this is likely to change to allow different register types and or to
500/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000501bool ARMAsmParser::
502TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000503 SMLoc S = Parser.getTok().getLoc();
504 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000505 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000506 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000507
Bill Wendling50d0f582010-11-18 23:43:05 +0000508 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000509
Chris Lattnere5658fa2010-10-30 04:09:10 +0000510 const AsmToken &ExclaimTok = Parser.getTok();
511 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000512 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
513 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000514 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000515 }
516
Bill Wendling50d0f582010-11-18 23:43:05 +0000517 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518}
519
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000520/// Parse a register list, return it if successful else return null. The first
521/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000522bool ARMAsmParser::
523ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000524 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000525 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000526 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000527
Bill Wendling7729e062010-11-09 22:44:22 +0000528 // Read the rest of the registers in the list.
529 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000530 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000531
Bill Wendling7729e062010-11-09 22:44:22 +0000532 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000533 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000534 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000535
Sean Callanan18b83232010-01-19 21:44:56 +0000536 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000537 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000538 if (RegTok.isNot(AsmToken::Identifier)) {
539 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000540 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000541 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000542
Bill Wendling1d6a2652010-11-06 10:40:24 +0000543 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000544 if (RegNum == -1) {
545 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000546 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000547 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000548
Bill Wendlinge7176102010-11-06 22:36:58 +0000549 if (IsRange) {
550 int Reg = PrevRegNum;
551 do {
552 ++Reg;
553 Registers.push_back(std::make_pair(Reg, RegLoc));
554 } while (Reg != RegNum);
555 } else {
556 Registers.push_back(std::make_pair(RegNum, RegLoc));
557 }
558
559 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000560 } while (Parser.getTok().is(AsmToken::Comma) ||
561 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000562
563 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000564 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000565 if (RCurlyTok.isNot(AsmToken::RCurly)) {
566 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000567 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000568 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000569
Bill Wendlinge7176102010-11-06 22:36:58 +0000570 SMLoc E = RCurlyTok.getLoc();
571 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000572
Bill Wendlinge7176102010-11-06 22:36:58 +0000573 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000574 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000575 RI = Registers.begin(), RE = Registers.end();
576
Bill Wendlinge7176102010-11-06 22:36:58 +0000577 DenseMap<unsigned, bool> RegMap;
578 RegMap[RI->first] = true;
579
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000580 unsigned HighRegNum = RI->first;
581 bool EmittedWarning = false;
582
Bill Wendlinge7176102010-11-06 22:36:58 +0000583 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000584 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000585 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000586
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000587 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000588 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000589 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000590 }
591
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000592 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000593 Warning(RegInfo.second,
594 "register not in ascending order in register list");
595
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000596 RegMap[Reg] = true;
597 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000598 }
599
Bill Wendling50d0f582010-11-18 23:43:05 +0000600 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
601 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000602}
603
Bill Wendlinge7176102010-11-06 22:36:58 +0000604/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000605/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000606///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000607/// TODO Only preindexing and postindexing addressing are started, unindexed
608/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000609bool ARMAsmParser::
610ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000611 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000612 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000613 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000614 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000615 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000616
Sean Callanan18b83232010-01-19 21:44:56 +0000617 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000618 if (BaseRegTok.isNot(AsmToken::Identifier)) {
619 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000620 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000621 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000622 int BaseRegNum = TryParseRegister();
623 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000624 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000625 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000626 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000627
628 bool Preindexed = false;
629 bool Postindexed = false;
630 bool OffsetIsReg = false;
631 bool Negative = false;
632 bool Writeback = false;
633
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000634 // First look for preindexed address forms, that is after the "[Rn" we now
635 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000636 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000637 if (Tok.is(AsmToken::Comma)) {
638 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000639 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000640 int OffsetRegNum;
641 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000642 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000643 const MCExpr *ShiftAmount = 0;
644 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000645 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
646 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000647 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000648 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000649 if (RBracTok.isNot(AsmToken::RBrac)) {
650 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000651 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000652 }
Sean Callanan76264762010-04-02 22:27:05 +0000653 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000654 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000655
Jim Grosbach03f44a02010-11-29 23:18:01 +0000656
Sean Callanan18b83232010-01-19 21:44:56 +0000657 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000658 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000659 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000660 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
661 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000662 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000663 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000664 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000665
666 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
667 OffsetRegNum, OffsetRegShifted,
668 ShiftType, ShiftAmount, Preindexed,
669 Postindexed, Negative, Writeback,
670 S, E));
671 if (WBOp)
672 Operands.push_back(WBOp);
673
674 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000675 }
676 // The "[Rn" we have so far was not followed by a comma.
677 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000678 // If there's anything other than the right brace, this is a post indexing
679 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000680 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000681 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000682
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000683 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000684 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000685 enum ShiftType ShiftType = Lsl;
686 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000687 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000688
Sean Callanan18b83232010-01-19 21:44:56 +0000689 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000690
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000691 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000692 Postindexed = true;
693 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000694
Chris Lattner550276e2010-10-28 20:52:15 +0000695 if (NextTok.isNot(AsmToken::Comma)) {
696 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000697 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000698 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000699
Sean Callananb9a25b72010-01-19 20:27:46 +0000700 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000701
Chris Lattner550276e2010-10-28 20:52:15 +0000702 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000703 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000704 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000705 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000706 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000707
Bill Wendling50d0f582010-11-18 23:43:05 +0000708 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
709 OffsetRegNum, OffsetRegShifted,
710 ShiftType, ShiftAmount, Preindexed,
711 Postindexed, Negative, Writeback,
712 S, E));
713 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000714 }
715
Bill Wendling50d0f582010-11-18 23:43:05 +0000716 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000717}
718
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000719/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
720/// we will parse the following (were +/- means that a plus or minus is
721/// optional):
722/// +/-Rm
723/// +/-Rm, shift
724/// #offset
725/// we return false on success or an error otherwise.
726bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000727 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000728 enum ShiftType &ShiftType,
729 const MCExpr *&ShiftAmount,
730 const MCExpr *&Offset,
731 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000732 int &OffsetRegNum,
733 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000734 Negative = false;
735 OffsetRegShifted = false;
736 OffsetIsReg = false;
737 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000738 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000739 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000740 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000741 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000742 else if (NextTok.is(AsmToken::Minus)) {
743 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000744 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000745 }
746 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000747 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000748 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000749 SMLoc CurLoc = OffsetRegTok.getLoc();
750 OffsetRegNum = TryParseRegister();
751 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000752 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000753 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000754 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000755 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000756
Bill Wendling12f40e92010-11-06 10:51:53 +0000757 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000758 if (OffsetRegNum != -1) {
759 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000760 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000761 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000762 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000763
Sean Callanan18b83232010-01-19 21:44:56 +0000764 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000765 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000766 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000767 OffsetRegShifted = true;
768 }
769 }
770 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
771 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000772 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000773 if (HashTok.isNot(AsmToken::Hash))
774 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000775
Sean Callananb9a25b72010-01-19 20:27:46 +0000776 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000778 if (getParser().ParseExpression(Offset))
779 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000780 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000781 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000782 return false;
783}
784
785/// ParseShift as one of these two:
786/// ( lsl | lsr | asr | ror ) , # shift_amount
787/// rrx
788/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000789bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000790 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000791 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000792 if (Tok.isNot(AsmToken::Identifier))
793 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000794 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000795 if (ShiftName == "lsl" || ShiftName == "LSL")
796 St = Lsl;
797 else if (ShiftName == "lsr" || ShiftName == "LSR")
798 St = Lsr;
799 else if (ShiftName == "asr" || ShiftName == "ASR")
800 St = Asr;
801 else if (ShiftName == "ror" || ShiftName == "ROR")
802 St = Ror;
803 else if (ShiftName == "rrx" || ShiftName == "RRX")
804 St = Rrx;
805 else
806 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000807 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000808
809 // Rrx stands alone.
810 if (St == Rrx)
811 return false;
812
813 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000814 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000815 if (HashTok.isNot(AsmToken::Hash))
816 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000817 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000818
819 if (getParser().ParseExpression(ShiftAmount))
820 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000821
822 return false;
823}
824
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000825/// Parse a arm instruction operand. For now this parses the operand regardless
826/// of the mnemonic.
Bill Wendling50d0f582010-11-18 23:43:05 +0000827bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
Sean Callanan76264762010-04-02 22:27:05 +0000828 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000829 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000830 default:
831 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000832 return true;
833 case AsmToken::Identifier: {
834 if (!TryParseRegisterWithWriteBack(Operands))
835 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000836
Kevin Enderby515d5092009-10-15 20:48:48 +0000837 // This was not a register so parse other operands that start with an
838 // identifier (like labels) as expressions and create them as immediates.
839 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000840 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000841 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000842 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000843 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000844 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
845 return false;
846 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000847 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000848 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000849 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000850 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000851 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000852 // #42 -> immediate.
853 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000854 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000855 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000856 const MCExpr *ImmVal;
857 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000858 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000859 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000860 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
861 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000862 }
863}
864
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000865/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000866bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000867 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000868 // Create the leading tokens for the mnemonic, split by '.' characters.
869 size_t Start = 0, Next = Name.find('.');
870 StringRef Head = Name.slice(Start, Next);
871
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000872 // Determine the predicate, if any.
873 //
874 // FIXME: We need a way to check whether a prefix supports predication,
875 // otherwise we will end up with an ambiguity for instructions that happen to
876 // end with a predicate name.
877 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
878 .Case("eq", ARMCC::EQ)
879 .Case("ne", ARMCC::NE)
880 .Case("hs", ARMCC::HS)
881 .Case("lo", ARMCC::LO)
882 .Case("mi", ARMCC::MI)
883 .Case("pl", ARMCC::PL)
884 .Case("vs", ARMCC::VS)
885 .Case("vc", ARMCC::VC)
886 .Case("hi", ARMCC::HI)
887 .Case("ls", ARMCC::LS)
888 .Case("ge", ARMCC::GE)
889 .Case("lt", ARMCC::LT)
890 .Case("gt", ARMCC::GT)
891 .Case("le", ARMCC::LE)
892 .Case("al", ARMCC::AL)
893 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000894
Chris Lattnerdba34d82010-10-30 04:35:59 +0000895 if (CC == ~0U ||
896 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000897 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000898 } else {
899 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000900 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000901
Chris Lattner3a697562010-10-28 17:20:03 +0000902 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +0000903
904 if (Head != "trap")
905 // FIXME: Should only add this operand for predicated instructions
906 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC),
907 NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000908
909 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000910 while (Next != StringRef::npos) {
911 Start = Next;
912 Next = Name.find('.', Start + 1);
913 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000914
Chris Lattner3a697562010-10-28 17:20:03 +0000915 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000916 }
917
918 // Read the remaining operands.
919 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000920 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +0000921 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000922 Parser.EatToEndOfStatement();
923 return true;
924 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000925
926 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000927 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000928
929 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +0000930 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000931 Parser.EatToEndOfStatement();
932 return true;
933 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000934 }
935 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000936
Chris Lattnercbf8a982010-09-11 16:18:25 +0000937 if (getLexer().isNot(AsmToken::EndOfStatement)) {
938 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000939 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000940 }
Bill Wendling146018f2010-11-06 21:42:12 +0000941
Chris Lattner34e53142010-09-08 05:10:46 +0000942 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000943 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000944}
945
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000946bool ARMAsmParser::
947MatchAndEmitInstruction(SMLoc IDLoc,
948 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
949 MCStreamer &Out) {
950 MCInst Inst;
951 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +0000952 MatchResultTy MatchResult, MatchResult2;
953 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
954 if (MatchResult != Match_Success) {
955 // If we get a Match_InvalidOperand it might be some arithmetic instruction
956 // that does not update the condition codes. So try adding a CCOut operand
957 // with a value of reg0.
958 if (MatchResult == Match_InvalidOperand) {
959 Operands.insert(Operands.begin() + 1,
960 ARMOperand::CreateCCOut(0,
961 ((ARMOperand*)Operands[0])->getStartLoc()));
962 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
963 if (MatchResult2 == Match_Success)
964 MatchResult = Match_Success;
965 else
966 Operands.erase(Operands.begin() + 1);
967 }
968 // If we get a Match_MnemonicFail it might be some arithmetic instruction
969 // that updates the condition codes if it ends in 's'. So see if the
970 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
971 // operand with a value of CPSR.
972 else if(MatchResult == Match_MnemonicFail) {
973 // Get the instruction mnemonic, which is the first token.
974 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
975 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
976 // removed the 's' from the mnemonic for matching.
977 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
978 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
979 Operands.erase(Operands.begin());
980 Operands.insert(Operands.begin(),
981 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
982 Operands.insert(Operands.begin() + 1,
983 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
984 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
985 if (MatchResult2 == Match_Success)
986 MatchResult = Match_Success;
987 else {
988 Operands.erase(Operands.begin());
989 Operands.insert(Operands.begin(),
990 ARMOperand::CreateToken(Mnemonic, NameLoc));
991 Operands.erase(Operands.begin() + 1);
992 }
993 }
994 }
995 }
996 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +0000997 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000998 Out.EmitInstruction(Inst);
999 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001000 case Match_MissingFeature:
1001 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1002 return true;
1003 case Match_InvalidOperand: {
1004 SMLoc ErrorLoc = IDLoc;
1005 if (ErrorInfo != ~0U) {
1006 if (ErrorInfo >= Operands.size())
1007 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001008
Chris Lattnere73d4f82010-10-28 21:41:58 +00001009 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1010 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1011 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001012
Chris Lattnere73d4f82010-10-28 21:41:58 +00001013 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001014 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001015 case Match_MnemonicFail:
1016 return Error(IDLoc, "unrecognized instruction mnemonic");
1017 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001018
Eric Christopherc223e2b2010-10-29 09:26:59 +00001019 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001020 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001021}
1022
Kevin Enderby515d5092009-10-15 20:48:48 +00001023/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001024bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1025 StringRef IDVal = DirectiveID.getIdentifier();
1026 if (IDVal == ".word")
1027 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001028 else if (IDVal == ".thumb")
1029 return ParseDirectiveThumb(DirectiveID.getLoc());
1030 else if (IDVal == ".thumb_func")
1031 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1032 else if (IDVal == ".code")
1033 return ParseDirectiveCode(DirectiveID.getLoc());
1034 else if (IDVal == ".syntax")
1035 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001036 return true;
1037}
1038
1039/// ParseDirectiveWord
1040/// ::= .word [ expression (, expression)* ]
1041bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1042 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1043 for (;;) {
1044 const MCExpr *Value;
1045 if (getParser().ParseExpression(Value))
1046 return true;
1047
Chris Lattneraaec2052010-01-19 19:46:13 +00001048 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001049
1050 if (getLexer().is(AsmToken::EndOfStatement))
1051 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001052
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001053 // FIXME: Improve diagnostic.
1054 if (getLexer().isNot(AsmToken::Comma))
1055 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001056 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001057 }
1058 }
1059
Sean Callananb9a25b72010-01-19 20:27:46 +00001060 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001061 return false;
1062}
1063
Kevin Enderby515d5092009-10-15 20:48:48 +00001064/// ParseDirectiveThumb
1065/// ::= .thumb
1066bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1067 if (getLexer().isNot(AsmToken::EndOfStatement))
1068 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001069 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001070
1071 // TODO: set thumb mode
1072 // TODO: tell the MC streamer the mode
1073 // getParser().getStreamer().Emit???();
1074 return false;
1075}
1076
1077/// ParseDirectiveThumbFunc
1078/// ::= .thumbfunc symbol_name
1079bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001080 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001081 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001082 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001083 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001084 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001085 if (getLexer().isNot(AsmToken::EndOfStatement))
1086 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001087 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001088
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001089 // Mark symbol as a thumb symbol.
1090 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1091 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001092 return false;
1093}
1094
1095/// ParseDirectiveSyntax
1096/// ::= .syntax unified | divided
1097bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001098 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001099 if (Tok.isNot(AsmToken::Identifier))
1100 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001101 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001102 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001103 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001104 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001105 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001106 else
1107 return Error(L, "unrecognized syntax mode in .syntax directive");
1108
1109 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001110 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001111 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001112
1113 // TODO tell the MC streamer the mode
1114 // getParser().getStreamer().Emit???();
1115 return false;
1116}
1117
1118/// ParseDirectiveCode
1119/// ::= .code 16 | 32
1120bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001121 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001122 if (Tok.isNot(AsmToken::Integer))
1123 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001124 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001125 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001126 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001127 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001128 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001129 else
1130 return Error(L, "invalid operand to .code directive");
1131
1132 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001133 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001134 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001135
Jim Grosbach2a301702010-11-05 22:40:53 +00001136 if (Val == 16)
1137 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1138 else
1139 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1140
Kevin Enderby515d5092009-10-15 20:48:48 +00001141 return false;
1142}
1143
Sean Callanan90b70972010-04-07 20:29:34 +00001144extern "C" void LLVMInitializeARMAsmLexer();
1145
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001146/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001147extern "C" void LLVMInitializeARMAsmParser() {
1148 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1149 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001150 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001151}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001152
Chris Lattner0692ee62010-09-06 19:11:01 +00001153#define GET_REGISTER_MATCHER
1154#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001155#include "ARMGenAsmMatcher.inc"