blob: 8322d70cedbc29db70b60d4320bb8b33e08745d5 [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 Wendlingf4caf692010-12-14 03:36:38 +0000239 bool isMemModeRegThumb() const {
240 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
241 return false;
242 return !Mem.Offset || !isa<MCConstantExpr>(Mem.Offset);
243 }
244 bool isMemModeImmThumb() const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000245 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
246 return false;
247
Bill Wendlingf4caf692010-12-14 03:36:38 +0000248 if (!Mem.Offset) return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000249
250 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
251 if (!CE) return false;
252
253 // The offset must be a multiple of 4 in the range 0-124.
254 uint64_t Value = CE->getValue();
255 return ((Value & 0x3) == 0 && Value <= 124);
256 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000257
258 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000259 // Add as immediates when possible. Null MCExpr = 0.
260 if (Expr == 0)
261 Inst.addOperand(MCOperand::CreateImm(0));
262 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000263 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
264 else
265 Inst.addOperand(MCOperand::CreateExpr(Expr));
266 }
267
Daniel Dunbar8462b302010-08-11 06:36:53 +0000268 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000269 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000270 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000271 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
272 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000273 }
274
Jim Grosbachd67641b2010-12-06 18:21:12 +0000275 void addCCOutOperands(MCInst &Inst, unsigned N) const {
276 assert(N == 1 && "Invalid number of operands!");
277 Inst.addOperand(MCOperand::CreateReg(getReg()));
278 }
279
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000280 void addRegOperands(MCInst &Inst, unsigned N) const {
281 assert(N == 1 && "Invalid number of operands!");
282 Inst.addOperand(MCOperand::CreateReg(getReg()));
283 }
284
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000285 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000286 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000287 const SmallVectorImpl<unsigned> &RegList = getRegList();
288 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000289 I = RegList.begin(), E = RegList.end(); I != E; ++I)
290 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000291 }
292
Bill Wendling0f630752010-11-17 04:32:08 +0000293 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
294 addRegListOperands(Inst, N);
295 }
296
297 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
298 addRegListOperands(Inst, N);
299 }
300
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000301 void addImmOperands(MCInst &Inst, unsigned N) const {
302 assert(N == 1 && "Invalid number of operands!");
303 addExpr(Inst, getImm());
304 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000305
Chris Lattner14b93852010-10-29 00:27:31 +0000306 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
307 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000308
Chris Lattner14b93852010-10-29 00:27:31 +0000309 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000310 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000311
Jim Grosbach80eb2332010-10-29 17:41:25 +0000312 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
313 // the difference?
314 if (Mem.Offset) {
315 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000316 assert(CE && "Non-constant mode 5 offset operand!");
317
Jim Grosbach80eb2332010-10-29 17:41:25 +0000318 // The MCInst offset operand doesn't include the low two bits (like
319 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000320 int64_t Offset = CE->getValue() / 4;
321 if (Offset >= 0)
322 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
323 Offset)));
324 else
325 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
326 -Offset)));
327 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000328 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000329 }
Chris Lattner14b93852010-10-29 00:27:31 +0000330 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000331
Bill Wendlingf4caf692010-12-14 03:36:38 +0000332 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
333 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000334 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000335 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
336 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000337
Bill Wendlingf4caf692010-12-14 03:36:38 +0000338 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
339 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
340 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
341 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
342 assert(CE && "Non-constant mode offset operand!");
343 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000344 }
345
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000346 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000347
Chris Lattner3a697562010-10-28 17:20:03 +0000348 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
349 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000350 Op->CC.Val = CC;
351 Op->StartLoc = S;
352 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000353 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000354 }
355
Jim Grosbachd67641b2010-12-06 18:21:12 +0000356 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
357 ARMOperand *Op = new ARMOperand(CCOut);
358 Op->Reg.RegNum = RegNum;
359 Op->StartLoc = S;
360 Op->EndLoc = S;
361 return Op;
362 }
363
Chris Lattner3a697562010-10-28 17:20:03 +0000364 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
365 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000366 Op->Tok.Data = Str.data();
367 Op->Tok.Length = Str.size();
368 Op->StartLoc = S;
369 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000370 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000371 }
372
Bill Wendling50d0f582010-11-18 23:43:05 +0000373 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000374 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000375 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000376 Op->StartLoc = S;
377 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000378 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000379 }
380
Bill Wendling7729e062010-11-09 22:44:22 +0000381 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000382 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000383 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000384 KindTy Kind = RegisterList;
385
386 if (ARM::DPRRegClass.contains(Regs.front().first))
387 Kind = DPRRegisterList;
388 else if (ARM::SPRRegClass.contains(Regs.front().first))
389 Kind = SPRRegisterList;
390
391 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000392 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000393 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000394 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000395 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000396 Op->StartLoc = StartLoc;
397 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000398 return Op;
399 }
400
Chris Lattner3a697562010-10-28 17:20:03 +0000401 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
402 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000403 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000404 Op->StartLoc = S;
405 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000406 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000407 }
408
Chris Lattner3a697562010-10-28 17:20:03 +0000409 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
410 const MCExpr *Offset, unsigned OffsetRegNum,
411 bool OffsetRegShifted, enum ShiftType ShiftType,
412 const MCExpr *ShiftAmount, bool Preindexed,
413 bool Postindexed, bool Negative, bool Writeback,
414 SMLoc S, SMLoc E) {
415 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000416 Op->Mem.BaseRegNum = BaseRegNum;
417 Op->Mem.OffsetIsReg = OffsetIsReg;
418 Op->Mem.Offset = Offset;
419 Op->Mem.OffsetRegNum = OffsetRegNum;
420 Op->Mem.OffsetRegShifted = OffsetRegShifted;
421 Op->Mem.ShiftType = ShiftType;
422 Op->Mem.ShiftAmount = ShiftAmount;
423 Op->Mem.Preindexed = Preindexed;
424 Op->Mem.Postindexed = Postindexed;
425 Op->Mem.Negative = Negative;
426 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000427
Sean Callanan76264762010-04-02 22:27:05 +0000428 Op->StartLoc = S;
429 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000430 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000431 }
432};
433
434} // end anonymous namespace.
435
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000436void ARMOperand::dump(raw_ostream &OS) const {
437 switch (Kind) {
438 case CondCode:
439 OS << ARMCondCodeToString(getCondCode());
440 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000441 case CCOut:
442 OS << "<ccout " << getReg() << ">";
443 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000444 case Immediate:
445 getImm()->print(OS);
446 break;
447 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000448 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000449 break;
450 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000451 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000452 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000453 case RegisterList:
454 case DPRRegisterList:
455 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000456 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000457
Bill Wendling5fa22a12010-11-09 23:28:44 +0000458 const SmallVectorImpl<unsigned> &RegList = getRegList();
459 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000460 I = RegList.begin(), E = RegList.end(); I != E; ) {
461 OS << *I;
462 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000463 }
464
465 OS << ">";
466 break;
467 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000468 case Token:
469 OS << "'" << getToken() << "'";
470 break;
471 }
472}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000473
474/// @name Auto-generated Match Functions
475/// {
476
477static unsigned MatchRegisterName(StringRef Name);
478
479/// }
480
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000481/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000482/// and if it is a register name the token is eaten and the register number is
483/// returned. Otherwise return -1.
484///
485int ARMAsmParser::TryParseRegister() {
486 const AsmToken &Tok = Parser.getTok();
487 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000488
Chris Lattnere5658fa2010-10-30 04:09:10 +0000489 // FIXME: Validate register for the current architecture; we have to do
490 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000491 unsigned RegNum = MatchRegisterName(Tok.getString());
492 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000493 return -1;
494 Parser.Lex(); // Eat identifier token.
495 return RegNum;
496}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000497
498
Bill Wendling50d0f582010-11-18 23:43:05 +0000499/// Try to parse a register name. The token must be an Identifier when called.
500/// If it's a register, an AsmOperand is created. Another AsmOperand is created
501/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000502///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000503/// TODO this is likely to change to allow different register types and or to
504/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000505bool ARMAsmParser::
506TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000507 SMLoc S = Parser.getTok().getLoc();
508 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000509 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000510 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000511
Bill Wendling50d0f582010-11-18 23:43:05 +0000512 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000513
Chris Lattnere5658fa2010-10-30 04:09:10 +0000514 const AsmToken &ExclaimTok = Parser.getTok();
515 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000516 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
517 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000518 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000519 }
520
Bill Wendling50d0f582010-11-18 23:43:05 +0000521 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000522}
523
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000524/// Parse a register list, return it if successful else return null. The first
525/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000526bool ARMAsmParser::
527ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000528 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000529 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000530 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000531
Bill Wendling7729e062010-11-09 22:44:22 +0000532 // Read the rest of the registers in the list.
533 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000534 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000535
Bill Wendling7729e062010-11-09 22:44:22 +0000536 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000537 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000538 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000539
Sean Callanan18b83232010-01-19 21:44:56 +0000540 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000541 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000542 if (RegTok.isNot(AsmToken::Identifier)) {
543 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000544 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000545 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000546
Bill Wendling1d6a2652010-11-06 10:40:24 +0000547 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000548 if (RegNum == -1) {
549 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000550 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000551 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000552
Bill Wendlinge7176102010-11-06 22:36:58 +0000553 if (IsRange) {
554 int Reg = PrevRegNum;
555 do {
556 ++Reg;
557 Registers.push_back(std::make_pair(Reg, RegLoc));
558 } while (Reg != RegNum);
559 } else {
560 Registers.push_back(std::make_pair(RegNum, RegLoc));
561 }
562
563 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000564 } while (Parser.getTok().is(AsmToken::Comma) ||
565 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000566
567 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000568 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000569 if (RCurlyTok.isNot(AsmToken::RCurly)) {
570 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000571 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000572 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000573
Bill Wendlinge7176102010-11-06 22:36:58 +0000574 SMLoc E = RCurlyTok.getLoc();
575 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000576
Bill Wendlinge7176102010-11-06 22:36:58 +0000577 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000578 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000579 RI = Registers.begin(), RE = Registers.end();
580
Bill Wendlinge7176102010-11-06 22:36:58 +0000581 DenseMap<unsigned, bool> RegMap;
582 RegMap[RI->first] = true;
583
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000584 unsigned HighRegNum = RI->first;
585 bool EmittedWarning = false;
586
Bill Wendlinge7176102010-11-06 22:36:58 +0000587 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000588 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000589 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000590
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000591 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000592 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000593 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000594 }
595
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000596 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000597 Warning(RegInfo.second,
598 "register not in ascending order in register list");
599
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000600 RegMap[Reg] = true;
601 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000602 }
603
Bill Wendling50d0f582010-11-18 23:43:05 +0000604 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
605 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000606}
607
Bill Wendlinge7176102010-11-06 22:36:58 +0000608/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000609/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000610///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000611/// TODO Only preindexing and postindexing addressing are started, unindexed
612/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000613bool ARMAsmParser::
614ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000615 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000616 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000617 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000618 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000619 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000620
Sean Callanan18b83232010-01-19 21:44:56 +0000621 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000622 if (BaseRegTok.isNot(AsmToken::Identifier)) {
623 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000624 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000625 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000626 int BaseRegNum = TryParseRegister();
627 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000628 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000629 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000630 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000631
632 bool Preindexed = false;
633 bool Postindexed = false;
634 bool OffsetIsReg = false;
635 bool Negative = false;
636 bool Writeback = false;
637
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000638 // First look for preindexed address forms, that is after the "[Rn" we now
639 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000640 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000641 if (Tok.is(AsmToken::Comma)) {
642 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000643 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000644 int OffsetRegNum;
645 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000646 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000647 const MCExpr *ShiftAmount = 0;
648 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000649 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
650 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000651 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000652 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000653 if (RBracTok.isNot(AsmToken::RBrac)) {
654 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000655 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000656 }
Sean Callanan76264762010-04-02 22:27:05 +0000657 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000658 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000659
Jim Grosbach03f44a02010-11-29 23:18:01 +0000660
Sean Callanan18b83232010-01-19 21:44:56 +0000661 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000662 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000663 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000664 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
665 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000666 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000667 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000668 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000669
670 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
671 OffsetRegNum, OffsetRegShifted,
672 ShiftType, ShiftAmount, Preindexed,
673 Postindexed, Negative, Writeback,
674 S, E));
675 if (WBOp)
676 Operands.push_back(WBOp);
677
678 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000679 }
680 // The "[Rn" we have so far was not followed by a comma.
681 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000682 // If there's anything other than the right brace, this is a post indexing
683 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000684 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000685 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000686
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000687 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000688 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000689 enum ShiftType ShiftType = Lsl;
690 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000691 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000692
Sean Callanan18b83232010-01-19 21:44:56 +0000693 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000694
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000695 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000696 Postindexed = true;
697 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000698
Chris Lattner550276e2010-10-28 20:52:15 +0000699 if (NextTok.isNot(AsmToken::Comma)) {
700 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000701 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000702 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000703
Sean Callananb9a25b72010-01-19 20:27:46 +0000704 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000705
Chris Lattner550276e2010-10-28 20:52:15 +0000706 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000707 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000708 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000709 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000710 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000711
Bill Wendling50d0f582010-11-18 23:43:05 +0000712 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
713 OffsetRegNum, OffsetRegShifted,
714 ShiftType, ShiftAmount, Preindexed,
715 Postindexed, Negative, Writeback,
716 S, E));
717 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000718 }
719
Bill Wendling50d0f582010-11-18 23:43:05 +0000720 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000721}
722
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000723/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
724/// we will parse the following (were +/- means that a plus or minus is
725/// optional):
726/// +/-Rm
727/// +/-Rm, shift
728/// #offset
729/// we return false on success or an error otherwise.
730bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000731 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000732 enum ShiftType &ShiftType,
733 const MCExpr *&ShiftAmount,
734 const MCExpr *&Offset,
735 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000736 int &OffsetRegNum,
737 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000738 Negative = false;
739 OffsetRegShifted = false;
740 OffsetIsReg = false;
741 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000742 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000743 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000744 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000745 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000746 else if (NextTok.is(AsmToken::Minus)) {
747 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000748 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000749 }
750 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000751 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000752 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000753 SMLoc CurLoc = OffsetRegTok.getLoc();
754 OffsetRegNum = TryParseRegister();
755 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000756 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000757 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000758 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000759 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000760
Bill Wendling12f40e92010-11-06 10:51:53 +0000761 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000762 if (OffsetRegNum != -1) {
763 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000764 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000765 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000766 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000767
Sean Callanan18b83232010-01-19 21:44:56 +0000768 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000769 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000770 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000771 OffsetRegShifted = true;
772 }
773 }
774 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
775 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000776 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777 if (HashTok.isNot(AsmToken::Hash))
778 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000779
Sean Callananb9a25b72010-01-19 20:27:46 +0000780 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000781
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000782 if (getParser().ParseExpression(Offset))
783 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000784 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000785 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000786 return false;
787}
788
789/// ParseShift as one of these two:
790/// ( lsl | lsr | asr | ror ) , # shift_amount
791/// rrx
792/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000793bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000794 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000795 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000796 if (Tok.isNot(AsmToken::Identifier))
797 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000798 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000799 if (ShiftName == "lsl" || ShiftName == "LSL")
800 St = Lsl;
801 else if (ShiftName == "lsr" || ShiftName == "LSR")
802 St = Lsr;
803 else if (ShiftName == "asr" || ShiftName == "ASR")
804 St = Asr;
805 else if (ShiftName == "ror" || ShiftName == "ROR")
806 St = Ror;
807 else if (ShiftName == "rrx" || ShiftName == "RRX")
808 St = Rrx;
809 else
810 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000811 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000812
813 // Rrx stands alone.
814 if (St == Rrx)
815 return false;
816
817 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000818 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000819 if (HashTok.isNot(AsmToken::Hash))
820 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000821 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000822
823 if (getParser().ParseExpression(ShiftAmount))
824 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000825
826 return false;
827}
828
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000829/// Parse a arm instruction operand. For now this parses the operand regardless
830/// of the mnemonic.
Bill Wendling50d0f582010-11-18 23:43:05 +0000831bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
Sean Callanan76264762010-04-02 22:27:05 +0000832 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000833 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000834 default:
835 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000836 return true;
837 case AsmToken::Identifier: {
838 if (!TryParseRegisterWithWriteBack(Operands))
839 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000840
Kevin Enderby515d5092009-10-15 20:48:48 +0000841 // This was not a register so parse other operands that start with an
842 // identifier (like labels) as expressions and create them as immediates.
843 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000844 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000845 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000846 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000847 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000848 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
849 return false;
850 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000851 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000852 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000853 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000854 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000855 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000856 // #42 -> immediate.
857 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000858 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000859 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000860 const MCExpr *ImmVal;
861 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000862 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000863 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000864 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
865 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000866 }
867}
868
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000869/// Parse an arm instruction mnemonic followed by its operands.
Benjamin Kramer38e59892010-07-14 22:38:02 +0000870bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000871 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Daniel Dunbar5747b132010-08-11 06:37:16 +0000872 // Create the leading tokens for the mnemonic, split by '.' characters.
873 size_t Start = 0, Next = Name.find('.');
874 StringRef Head = Name.slice(Start, Next);
875
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000876 // Determine the predicate, if any.
877 //
878 // FIXME: We need a way to check whether a prefix supports predication,
879 // otherwise we will end up with an ambiguity for instructions that happen to
880 // end with a predicate name.
881 unsigned CC = StringSwitch<unsigned>(Head.substr(Head.size()-2))
882 .Case("eq", ARMCC::EQ)
883 .Case("ne", ARMCC::NE)
884 .Case("hs", ARMCC::HS)
885 .Case("lo", ARMCC::LO)
886 .Case("mi", ARMCC::MI)
887 .Case("pl", ARMCC::PL)
888 .Case("vs", ARMCC::VS)
889 .Case("vc", ARMCC::VC)
890 .Case("hi", ARMCC::HI)
891 .Case("ls", ARMCC::LS)
892 .Case("ge", ARMCC::GE)
893 .Case("lt", ARMCC::LT)
894 .Case("gt", ARMCC::GT)
895 .Case("le", ARMCC::LE)
896 .Case("al", ARMCC::AL)
897 .Default(~0U);
Jim Grosbach16c74252010-10-29 14:46:02 +0000898
Chris Lattnerdba34d82010-10-30 04:35:59 +0000899 if (CC == ~0U ||
900 (CC == ARMCC::LS && (Head == "vmls" || Head == "vnmls"))) {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000901 CC = ARMCC::AL;
Chris Lattnerdba34d82010-10-30 04:35:59 +0000902 } else {
903 Head = Head.slice(0, Head.size() - 2);
Bill Wendling52925b62010-10-29 23:50:21 +0000904 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000905
Chris Lattner3a697562010-10-28 17:20:03 +0000906 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +0000907
908 if (Head != "trap")
909 // FIXME: Should only add this operand for predicated instructions
910 Operands.push_back(ARMOperand::CreateCondCode(ARMCC::CondCodes(CC),
911 NameLoc));
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000912
913 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +0000914 while (Next != StringRef::npos) {
915 Start = Next;
916 Next = Name.find('.', Start + 1);
917 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000918
Chris Lattner3a697562010-10-28 17:20:03 +0000919 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +0000920 }
921
922 // Read the remaining operands.
923 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000924 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +0000925 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000926 Parser.EatToEndOfStatement();
927 return true;
928 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000929
930 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000931 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000932
933 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +0000934 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +0000935 Parser.EatToEndOfStatement();
936 return true;
937 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000938 }
939 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000940
Chris Lattnercbf8a982010-09-11 16:18:25 +0000941 if (getLexer().isNot(AsmToken::EndOfStatement)) {
942 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +0000943 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +0000944 }
Bill Wendling146018f2010-11-06 21:42:12 +0000945
Chris Lattner34e53142010-09-08 05:10:46 +0000946 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +0000947 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000948}
949
Chris Lattnerfa42fad2010-10-28 21:28:01 +0000950bool ARMAsmParser::
951MatchAndEmitInstruction(SMLoc IDLoc,
952 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
953 MCStreamer &Out) {
954 MCInst Inst;
955 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +0000956 MatchResultTy MatchResult, MatchResult2;
957 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
958 if (MatchResult != Match_Success) {
959 // If we get a Match_InvalidOperand it might be some arithmetic instruction
960 // that does not update the condition codes. So try adding a CCOut operand
961 // with a value of reg0.
962 if (MatchResult == Match_InvalidOperand) {
963 Operands.insert(Operands.begin() + 1,
964 ARMOperand::CreateCCOut(0,
965 ((ARMOperand*)Operands[0])->getStartLoc()));
966 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
967 if (MatchResult2 == Match_Success)
968 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +0000969 else {
970 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +0000971 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +0000972 delete CCOut;
973 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +0000974 }
975 // If we get a Match_MnemonicFail it might be some arithmetic instruction
976 // that updates the condition codes if it ends in 's'. So see if the
977 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
978 // operand with a value of CPSR.
979 else if(MatchResult == Match_MnemonicFail) {
980 // Get the instruction mnemonic, which is the first token.
981 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
982 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
983 // removed the 's' from the mnemonic for matching.
984 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
985 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +0000986 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
987 Operands.erase(Operands.begin());
988 delete OldMnemonic;
989 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +0000990 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
991 Operands.insert(Operands.begin() + 1,
992 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
993 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
994 if (MatchResult2 == Match_Success)
995 MatchResult = Match_Success;
996 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +0000997 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
998 Operands.erase(Operands.begin());
999 delete OldMnemonic;
1000 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001001 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001002 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1003 Operands.erase(Operands.begin() + 1);
1004 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001005 }
1006 }
1007 }
1008 }
1009 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001010 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001011 Out.EmitInstruction(Inst);
1012 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001013 case Match_MissingFeature:
1014 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1015 return true;
1016 case Match_InvalidOperand: {
1017 SMLoc ErrorLoc = IDLoc;
1018 if (ErrorInfo != ~0U) {
1019 if (ErrorInfo >= Operands.size())
1020 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001021
Chris Lattnere73d4f82010-10-28 21:41:58 +00001022 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1023 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1024 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001025
Chris Lattnere73d4f82010-10-28 21:41:58 +00001026 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001027 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001028 case Match_MnemonicFail:
1029 return Error(IDLoc, "unrecognized instruction mnemonic");
1030 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001031
Eric Christopherc223e2b2010-10-29 09:26:59 +00001032 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001033 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001034}
1035
Kevin Enderby515d5092009-10-15 20:48:48 +00001036/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001037bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1038 StringRef IDVal = DirectiveID.getIdentifier();
1039 if (IDVal == ".word")
1040 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001041 else if (IDVal == ".thumb")
1042 return ParseDirectiveThumb(DirectiveID.getLoc());
1043 else if (IDVal == ".thumb_func")
1044 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1045 else if (IDVal == ".code")
1046 return ParseDirectiveCode(DirectiveID.getLoc());
1047 else if (IDVal == ".syntax")
1048 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001049 return true;
1050}
1051
1052/// ParseDirectiveWord
1053/// ::= .word [ expression (, expression)* ]
1054bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1055 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1056 for (;;) {
1057 const MCExpr *Value;
1058 if (getParser().ParseExpression(Value))
1059 return true;
1060
Chris Lattneraaec2052010-01-19 19:46:13 +00001061 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001062
1063 if (getLexer().is(AsmToken::EndOfStatement))
1064 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001065
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001066 // FIXME: Improve diagnostic.
1067 if (getLexer().isNot(AsmToken::Comma))
1068 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001069 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001070 }
1071 }
1072
Sean Callananb9a25b72010-01-19 20:27:46 +00001073 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001074 return false;
1075}
1076
Kevin Enderby515d5092009-10-15 20:48:48 +00001077/// ParseDirectiveThumb
1078/// ::= .thumb
1079bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1080 if (getLexer().isNot(AsmToken::EndOfStatement))
1081 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001082 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001083
1084 // TODO: set thumb mode
1085 // TODO: tell the MC streamer the mode
1086 // getParser().getStreamer().Emit???();
1087 return false;
1088}
1089
1090/// ParseDirectiveThumbFunc
1091/// ::= .thumbfunc symbol_name
1092bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001093 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001094 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001095 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001096 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001097 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001098 if (getLexer().isNot(AsmToken::EndOfStatement))
1099 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001100 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001101
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001102 // Mark symbol as a thumb symbol.
1103 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1104 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001105 return false;
1106}
1107
1108/// ParseDirectiveSyntax
1109/// ::= .syntax unified | divided
1110bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001111 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001112 if (Tok.isNot(AsmToken::Identifier))
1113 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001114 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001115 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001116 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001117 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001118 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001119 else
1120 return Error(L, "unrecognized syntax mode in .syntax directive");
1121
1122 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001123 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001124 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001125
1126 // TODO tell the MC streamer the mode
1127 // getParser().getStreamer().Emit???();
1128 return false;
1129}
1130
1131/// ParseDirectiveCode
1132/// ::= .code 16 | 32
1133bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001134 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001135 if (Tok.isNot(AsmToken::Integer))
1136 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001137 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001138 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001139 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001140 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001141 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001142 else
1143 return Error(L, "invalid operand to .code directive");
1144
1145 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001146 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001147 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001148
Jim Grosbach2a301702010-11-05 22:40:53 +00001149 if (Val == 16)
1150 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1151 else
1152 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1153
Kevin Enderby515d5092009-10-15 20:48:48 +00001154 return false;
1155}
1156
Sean Callanan90b70972010-04-07 20:29:34 +00001157extern "C" void LLVMInitializeARMAsmLexer();
1158
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001159/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001160extern "C" void LLVMInitializeARMAsmParser() {
1161 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1162 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001163 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001164}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001165
Chris Lattner0692ee62010-09-06 19:11:01 +00001166#define GET_REGISTER_MATCHER
1167#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001168#include "ARMGenAsmMatcher.inc"