blob: 5346106c58a77dff773584481f31d3286c6381b9 [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
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000029/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000030enum 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
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000137 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138 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:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000439 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000440 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
Daniel Dunbar352e1482011-01-11 15:59:50 +0000869/// \brief Given a mnemonic, split out possible predication code and carry
870/// setting letters to form a canonical mnemonic and flags.
871//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000872// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000873static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
874 unsigned &PredicationCode,
875 bool &CarrySetting) {
876 PredicationCode = ARMCC::AL;
877 CarrySetting = false;
878
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000879 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000880 //
881 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +0000882 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
883 Mnemonic == "movs" ||
884 Mnemonic == "svc" ||
885 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
886 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
887 Mnemonic == "vacge" || Mnemonic == "vcge" ||
888 Mnemonic == "vclt" ||
889 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
890 Mnemonic == "vcle" ||
891 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
892 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
893 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +0000894 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +0000895
Daniel Dunbar352e1482011-01-11 15:59:50 +0000896 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000897 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000898 .Case("eq", ARMCC::EQ)
899 .Case("ne", ARMCC::NE)
900 .Case("hs", ARMCC::HS)
901 .Case("lo", ARMCC::LO)
902 .Case("mi", ARMCC::MI)
903 .Case("pl", ARMCC::PL)
904 .Case("vs", ARMCC::VS)
905 .Case("vc", ARMCC::VC)
906 .Case("hi", ARMCC::HI)
907 .Case("ls", ARMCC::LS)
908 .Case("ge", ARMCC::GE)
909 .Case("lt", ARMCC::LT)
910 .Case("gt", ARMCC::GT)
911 .Case("le", ARMCC::LE)
912 .Case("al", ARMCC::AL)
913 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000914 if (CC != ~0U) {
915 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +0000916 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +0000917 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000918
Daniel Dunbar352e1482011-01-11 15:59:50 +0000919 // Next, determine if we have a carry setting bit. We explicitly ignore all
920 // the instructions we know end in 's'.
921 if (Mnemonic.endswith("s") &&
922 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
923 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
924 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
925 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
926 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
927 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
928 CarrySetting = true;
929 }
930
931 return Mnemonic;
932}
Daniel Dunbar3771dd02011-01-11 15:59:53 +0000933
934/// \brief Given a canonical mnemonic, determine if the instruction ever allows
935/// inclusion of carry set or predication code operands.
936//
937// FIXME: It would be nice to autogen this.
938static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
939 bool &CanAcceptPredicationCode) {
940 CanAcceptCarrySet = false;
941
942 if (Mnemonic == "trap") {
943 CanAcceptPredicationCode = false;
944 } else {
945 CanAcceptPredicationCode = true;
946 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000947}
948
949/// Parse an arm instruction mnemonic followed by its operands.
950bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
951 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
952 // Create the leading tokens for the mnemonic, split by '.' characters.
953 size_t Start = 0, Next = Name.find('.');
954 StringRef Head = Name.slice(Start, Next);
955
Daniel Dunbar352e1482011-01-11 15:59:50 +0000956 // Split out the predication code and carry setting flag from the mnemonic.
957 unsigned PredicationCode;
958 bool CarrySetting;
959 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000960
Chris Lattner3a697562010-10-28 17:20:03 +0000961 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +0000962
Daniel Dunbar3771dd02011-01-11 15:59:53 +0000963 // Next, add the CCOut and ConditionCode operands, if needed.
964 //
965 // For mnemonics which can ever incorporate a carry setting bit or predication
966 // code, our matching model involves us always generating CCOut and
967 // ConditionCode operands to match the mnemonic "as written" and then we let
968 // the matcher deal with finding the right instruction or generating an
969 // appropriate error.
970 bool CanAcceptCarrySet, CanAcceptPredicationCode;
971 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
972
973 // Add the carry setting operand, if necessary.
974 //
975 // FIXME: It would be awesome if we could somehow invent a location such that
976 // match errors on this operand would print a nice diagnostic about how the
977 // 's' character in the mnemonic resulted in a CCOut operand.
978 if (CanAcceptCarrySet) {
979 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
980 NameLoc));
981 } else {
982 // This mnemonic can't ever accept a carry set, but the user wrote one (or
983 // misspelled another mnemonic).
984
985 // FIXME: Issue a nice error.
986 }
987
988 // Add the predication code operand, if necessary.
989 if (CanAcceptPredicationCode) {
990 Operands.push_back(ARMOperand::CreateCondCode(
991 ARMCC::CondCodes(PredicationCode), NameLoc));
992 } else {
993 // This mnemonic can't ever accept a predication code, but the user wrote
994 // one (or misspelled another mnemonic).
995
996 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000997 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000998
999 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001000 while (Next != StringRef::npos) {
1001 Start = Next;
1002 Next = Name.find('.', Start + 1);
1003 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001004
Chris Lattner3a697562010-10-28 17:20:03 +00001005 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001006 }
1007
1008 // Read the remaining operands.
1009 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001010 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001011 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001012 Parser.EatToEndOfStatement();
1013 return true;
1014 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001015
1016 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001017 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001018
1019 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001020 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001021 Parser.EatToEndOfStatement();
1022 return true;
1023 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001024 }
1025 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001026
Chris Lattnercbf8a982010-09-11 16:18:25 +00001027 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1028 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001029 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001030 }
Bill Wendling146018f2010-11-06 21:42:12 +00001031
Chris Lattner34e53142010-09-08 05:10:46 +00001032 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001033 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001034}
1035
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001036bool ARMAsmParser::
1037MatchAndEmitInstruction(SMLoc IDLoc,
1038 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1039 MCStreamer &Out) {
1040 MCInst Inst;
1041 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001042 MatchResultTy MatchResult, MatchResult2;
1043 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1044 if (MatchResult != Match_Success) {
1045 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1046 // that does not update the condition codes. So try adding a CCOut operand
1047 // with a value of reg0.
1048 if (MatchResult == Match_InvalidOperand) {
1049 Operands.insert(Operands.begin() + 1,
1050 ARMOperand::CreateCCOut(0,
1051 ((ARMOperand*)Operands[0])->getStartLoc()));
1052 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1053 if (MatchResult2 == Match_Success)
1054 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001055 else {
1056 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001057 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001058 delete CCOut;
1059 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001060 }
1061 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1062 // that updates the condition codes if it ends in 's'. So see if the
1063 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1064 // operand with a value of CPSR.
1065 else if(MatchResult == Match_MnemonicFail) {
1066 // Get the instruction mnemonic, which is the first token.
1067 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1068 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1069 // removed the 's' from the mnemonic for matching.
1070 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1071 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001072 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1073 Operands.erase(Operands.begin());
1074 delete OldMnemonic;
1075 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001076 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1077 Operands.insert(Operands.begin() + 1,
1078 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1079 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1080 if (MatchResult2 == Match_Success)
1081 MatchResult = Match_Success;
1082 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001083 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1084 Operands.erase(Operands.begin());
1085 delete OldMnemonic;
1086 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001087 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001088 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1089 Operands.erase(Operands.begin() + 1);
1090 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001091 }
1092 }
1093 }
1094 }
1095 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001096 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001097 Out.EmitInstruction(Inst);
1098 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001099 case Match_MissingFeature:
1100 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1101 return true;
1102 case Match_InvalidOperand: {
1103 SMLoc ErrorLoc = IDLoc;
1104 if (ErrorInfo != ~0U) {
1105 if (ErrorInfo >= Operands.size())
1106 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001107
Chris Lattnere73d4f82010-10-28 21:41:58 +00001108 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1109 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1110 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001111
Chris Lattnere73d4f82010-10-28 21:41:58 +00001112 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001113 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001114 case Match_MnemonicFail:
1115 return Error(IDLoc, "unrecognized instruction mnemonic");
1116 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001117
Eric Christopherc223e2b2010-10-29 09:26:59 +00001118 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001119 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001120}
1121
Kevin Enderby515d5092009-10-15 20:48:48 +00001122/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001123bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1124 StringRef IDVal = DirectiveID.getIdentifier();
1125 if (IDVal == ".word")
1126 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001127 else if (IDVal == ".thumb")
1128 return ParseDirectiveThumb(DirectiveID.getLoc());
1129 else if (IDVal == ".thumb_func")
1130 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1131 else if (IDVal == ".code")
1132 return ParseDirectiveCode(DirectiveID.getLoc());
1133 else if (IDVal == ".syntax")
1134 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001135 return true;
1136}
1137
1138/// ParseDirectiveWord
1139/// ::= .word [ expression (, expression)* ]
1140bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1141 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1142 for (;;) {
1143 const MCExpr *Value;
1144 if (getParser().ParseExpression(Value))
1145 return true;
1146
Chris Lattneraaec2052010-01-19 19:46:13 +00001147 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001148
1149 if (getLexer().is(AsmToken::EndOfStatement))
1150 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001151
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001152 // FIXME: Improve diagnostic.
1153 if (getLexer().isNot(AsmToken::Comma))
1154 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001155 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001156 }
1157 }
1158
Sean Callananb9a25b72010-01-19 20:27:46 +00001159 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001160 return false;
1161}
1162
Kevin Enderby515d5092009-10-15 20:48:48 +00001163/// ParseDirectiveThumb
1164/// ::= .thumb
1165bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1166 if (getLexer().isNot(AsmToken::EndOfStatement))
1167 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001168 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001169
1170 // TODO: set thumb mode
1171 // TODO: tell the MC streamer the mode
1172 // getParser().getStreamer().Emit???();
1173 return false;
1174}
1175
1176/// ParseDirectiveThumbFunc
1177/// ::= .thumbfunc symbol_name
1178bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001179 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001180 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001181 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001182 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001183 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001184 if (getLexer().isNot(AsmToken::EndOfStatement))
1185 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001186 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001187
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001188 // Mark symbol as a thumb symbol.
1189 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1190 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001191 return false;
1192}
1193
1194/// ParseDirectiveSyntax
1195/// ::= .syntax unified | divided
1196bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001197 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001198 if (Tok.isNot(AsmToken::Identifier))
1199 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001200 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001201 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001202 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001203 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001204 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001205 else
1206 return Error(L, "unrecognized syntax mode in .syntax directive");
1207
1208 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001209 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001210 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001211
1212 // TODO tell the MC streamer the mode
1213 // getParser().getStreamer().Emit???();
1214 return false;
1215}
1216
1217/// ParseDirectiveCode
1218/// ::= .code 16 | 32
1219bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001220 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001221 if (Tok.isNot(AsmToken::Integer))
1222 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001223 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001224 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001225 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001226 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001227 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001228 else
1229 return Error(L, "invalid operand to .code directive");
1230
1231 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001232 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001233 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001234
Jim Grosbach2a301702010-11-05 22:40:53 +00001235 if (Val == 16)
1236 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1237 else
1238 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1239
Kevin Enderby515d5092009-10-15 20:48:48 +00001240 return false;
1241}
1242
Sean Callanan90b70972010-04-07 20:29:34 +00001243extern "C" void LLVMInitializeARMAsmLexer();
1244
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001245/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001246extern "C" void LLVMInitializeARMAsmParser() {
1247 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1248 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001249 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001250}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001251
Chris Lattner0692ee62010-09-06 19:11:01 +00001252#define GET_REGISTER_MATCHER
1253#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001254#include "ARMGenAsmMatcher.inc"