blob: 2aabd0261993b9f1c76937880845f0ce4937d649 [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"
Evan Chengb72d2a92011-01-11 21:46:47 +000012#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000013#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000014#include "llvm/MC/MCParser/MCAsmLexer.h"
15#include "llvm/MC/MCParser/MCAsmParser.h"
16#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000017#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000021#include "llvm/Target/TargetRegistry.h"
22#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000026#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000027#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000028using namespace llvm;
29
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000030/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000031enum ShiftType {
32 Lsl,
33 Lsr,
34 Asr,
35 Ror,
36 Rrx
37};
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043class ARMAsmParser : public TargetAsmParser {
44 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000045 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000051 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
Bill Wendling50d0f582010-11-18 23:43:05 +000054 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
55 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
56 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
57 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
Jason W Kim9081b4b2011-01-11 23:53:41 +000058 bool ParsePrefix(MCSymbolRefExpr::VariantKind &RefKind);
59 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
60 MCSymbolRefExpr::VariantKind Variant);
61
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000062
Kevin Enderby9c41fa82009-10-30 22:55:57 +000063 bool ParseMemoryOffsetReg(bool &Negative,
64 bool &OffsetRegShifted,
65 enum ShiftType &ShiftType,
66 const MCExpr *&ShiftAmount,
67 const MCExpr *&Offset,
68 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000069 int &OffsetRegNum,
70 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000071 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000072 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000073 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000074 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000075 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveSyntax(SMLoc L);
77
Chris Lattner7036f8b2010-09-29 01:42:58 +000078 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000079 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000080 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000081
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000082 /// @name Auto-generated Match Functions
83 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000084
Chris Lattner0692ee62010-09-06 19:11:01 +000085#define GET_ASSEMBLER_HEADER
86#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000088 /// }
89
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000090public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000091 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000092 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
93 // Initialize the set of available features.
94 setAvailableFeatures(ComputeAvailableFeatures(
95 &TM.getSubtarget<ARMSubtarget>()));
96 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000097
Benjamin Kramer38e59892010-07-14 22:38:02 +000098 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000099 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000100 virtual bool ParseDirective(AsmToken DirectiveID);
101};
Jim Grosbach16c74252010-10-29 14:46:02 +0000102} // end anonymous namespace
103
Chris Lattner3a697562010-10-28 17:20:03 +0000104namespace {
105
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000106/// ARMOperand - Instances of this class represent a parsed ARM machine
107/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000108class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000109 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000110 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000111 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000112 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000113 Memory,
114 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000115 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000116 DPRRegisterList,
117 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000118 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000119 } Kind;
120
Sean Callanan76264762010-04-02 22:27:05 +0000121 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000122 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000123
124 union {
125 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000126 ARMCC::CondCodes Val;
127 } CC;
128
129 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000130 const char *Data;
131 unsigned Length;
132 } Tok;
133
134 struct {
135 unsigned RegNum;
136 } Reg;
137
Bill Wendling8155e5b2010-11-06 22:19:43 +0000138 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000139 const MCExpr *Val;
140 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000141
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000142 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000143 struct {
144 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000145 unsigned OffsetRegNum; // used when OffsetIsReg is true
146 const MCExpr *Offset; // used when OffsetIsReg is false
147 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
148 enum ShiftType ShiftType; // used when OffsetRegShifted is true
149 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000150 unsigned Preindexed : 1;
151 unsigned Postindexed : 1;
152 unsigned OffsetIsReg : 1;
153 unsigned Negative : 1; // only used when OffsetIsReg is true
154 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000155 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000156 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000157
Bill Wendling146018f2010-11-06 21:42:12 +0000158 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
159public:
Sean Callanan76264762010-04-02 22:27:05 +0000160 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
161 Kind = o.Kind;
162 StartLoc = o.StartLoc;
163 EndLoc = o.EndLoc;
164 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000165 case CondCode:
166 CC = o.CC;
167 break;
Sean Callanan76264762010-04-02 22:27:05 +0000168 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000169 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000170 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000171 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000172 case Register:
173 Reg = o.Reg;
174 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000175 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000176 case DPRRegisterList:
177 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000178 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000179 break;
Sean Callanan76264762010-04-02 22:27:05 +0000180 case Immediate:
181 Imm = o.Imm;
182 break;
183 case Memory:
184 Mem = o.Mem;
185 break;
186 }
187 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000188
Sean Callanan76264762010-04-02 22:27:05 +0000189 /// getStartLoc - Get the location of the first token of this operand.
190 SMLoc getStartLoc() const { return StartLoc; }
191 /// getEndLoc - Get the location of the last token of this operand.
192 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000193
Daniel Dunbar8462b302010-08-11 06:36:53 +0000194 ARMCC::CondCodes getCondCode() const {
195 assert(Kind == CondCode && "Invalid access!");
196 return CC.Val;
197 }
198
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000199 StringRef getToken() const {
200 assert(Kind == Token && "Invalid access!");
201 return StringRef(Tok.Data, Tok.Length);
202 }
203
204 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000205 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000206 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000207 }
208
Bill Wendling5fa22a12010-11-09 23:28:44 +0000209 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000210 assert((Kind == RegisterList || Kind == DPRRegisterList ||
211 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000212 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000213 }
214
Kevin Enderbycfe07242009-10-13 22:19:02 +0000215 const MCExpr *getImm() const {
216 assert(Kind == Immediate && "Invalid access!");
217 return Imm.Val;
218 }
219
Daniel Dunbar8462b302010-08-11 06:36:53 +0000220 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000221 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000222 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000223 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000224 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000225 bool isDPRRegList() const { return Kind == DPRRegisterList; }
226 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000227 bool isToken() const { return Kind == Token; }
228 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000229 bool isMemMode5() const {
230 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
231 Mem.Writeback || Mem.Negative)
232 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000233
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000234 // If there is an offset expression, make sure it's valid.
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000235 if (!Mem.Offset) return true;
236
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000237 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000238 if (!CE) return false;
239
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000240 // The offset must be a multiple of 4 in the range 0-1020.
241 int64_t Value = CE->getValue();
242 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
243 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000244 bool isMemModeRegThumb() const {
245 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
246 return false;
247 return !Mem.Offset || !isa<MCConstantExpr>(Mem.Offset);
248 }
249 bool isMemModeImmThumb() const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000250 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
251 return false;
252
Bill Wendlingf4caf692010-12-14 03:36:38 +0000253 if (!Mem.Offset) return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000254
255 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
256 if (!CE) return false;
257
258 // The offset must be a multiple of 4 in the range 0-124.
259 uint64_t Value = CE->getValue();
260 return ((Value & 0x3) == 0 && Value <= 124);
261 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000262
263 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000264 // Add as immediates when possible. Null MCExpr = 0.
265 if (Expr == 0)
266 Inst.addOperand(MCOperand::CreateImm(0));
267 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000268 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
269 else
270 Inst.addOperand(MCOperand::CreateExpr(Expr));
271 }
272
Daniel Dunbar8462b302010-08-11 06:36:53 +0000273 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000274 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000275 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000276 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
277 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000278 }
279
Jim Grosbachd67641b2010-12-06 18:21:12 +0000280 void addCCOutOperands(MCInst &Inst, unsigned N) const {
281 assert(N == 1 && "Invalid number of operands!");
282 Inst.addOperand(MCOperand::CreateReg(getReg()));
283 }
284
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000285 void addRegOperands(MCInst &Inst, unsigned N) const {
286 assert(N == 1 && "Invalid number of operands!");
287 Inst.addOperand(MCOperand::CreateReg(getReg()));
288 }
289
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000290 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000291 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000292 const SmallVectorImpl<unsigned> &RegList = getRegList();
293 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000294 I = RegList.begin(), E = RegList.end(); I != E; ++I)
295 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000296 }
297
Bill Wendling0f630752010-11-17 04:32:08 +0000298 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
299 addRegListOperands(Inst, N);
300 }
301
302 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
303 addRegListOperands(Inst, N);
304 }
305
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000306 void addImmOperands(MCInst &Inst, unsigned N) const {
307 assert(N == 1 && "Invalid number of operands!");
308 addExpr(Inst, getImm());
309 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000310
Chris Lattner14b93852010-10-29 00:27:31 +0000311 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
312 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000313
Chris Lattner14b93852010-10-29 00:27:31 +0000314 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000315 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000316
Jim Grosbach80eb2332010-10-29 17:41:25 +0000317 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
318 // the difference?
319 if (Mem.Offset) {
320 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000321 assert(CE && "Non-constant mode 5 offset operand!");
322
Jim Grosbach80eb2332010-10-29 17:41:25 +0000323 // The MCInst offset operand doesn't include the low two bits (like
324 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000325 int64_t Offset = CE->getValue() / 4;
326 if (Offset >= 0)
327 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
328 Offset)));
329 else
330 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
331 -Offset)));
332 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000333 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000334 }
Chris Lattner14b93852010-10-29 00:27:31 +0000335 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000336
Bill Wendlingf4caf692010-12-14 03:36:38 +0000337 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
338 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000339 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000340 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
341 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000342
Bill Wendlingf4caf692010-12-14 03:36:38 +0000343 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
344 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
345 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
346 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
347 assert(CE && "Non-constant mode offset operand!");
348 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000349 }
350
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000351 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000352
Chris Lattner3a697562010-10-28 17:20:03 +0000353 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
354 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000355 Op->CC.Val = CC;
356 Op->StartLoc = S;
357 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000358 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000359 }
360
Jim Grosbachd67641b2010-12-06 18:21:12 +0000361 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
362 ARMOperand *Op = new ARMOperand(CCOut);
363 Op->Reg.RegNum = RegNum;
364 Op->StartLoc = S;
365 Op->EndLoc = S;
366 return Op;
367 }
368
Chris Lattner3a697562010-10-28 17:20:03 +0000369 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
370 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000371 Op->Tok.Data = Str.data();
372 Op->Tok.Length = Str.size();
373 Op->StartLoc = S;
374 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000375 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000376 }
377
Bill Wendling50d0f582010-11-18 23:43:05 +0000378 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000379 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000380 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000381 Op->StartLoc = S;
382 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000383 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000384 }
385
Bill Wendling7729e062010-11-09 22:44:22 +0000386 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000387 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000388 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000389 KindTy Kind = RegisterList;
390
391 if (ARM::DPRRegClass.contains(Regs.front().first))
392 Kind = DPRRegisterList;
393 else if (ARM::SPRRegClass.contains(Regs.front().first))
394 Kind = SPRRegisterList;
395
396 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000397 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000398 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000399 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000400 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000401 Op->StartLoc = StartLoc;
402 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000403 return Op;
404 }
405
Chris Lattner3a697562010-10-28 17:20:03 +0000406 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
407 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000408 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000409 Op->StartLoc = S;
410 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000411 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000412 }
413
Chris Lattner3a697562010-10-28 17:20:03 +0000414 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
415 const MCExpr *Offset, unsigned OffsetRegNum,
416 bool OffsetRegShifted, enum ShiftType ShiftType,
417 const MCExpr *ShiftAmount, bool Preindexed,
418 bool Postindexed, bool Negative, bool Writeback,
419 SMLoc S, SMLoc E) {
420 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000421 Op->Mem.BaseRegNum = BaseRegNum;
422 Op->Mem.OffsetIsReg = OffsetIsReg;
423 Op->Mem.Offset = Offset;
424 Op->Mem.OffsetRegNum = OffsetRegNum;
425 Op->Mem.OffsetRegShifted = OffsetRegShifted;
426 Op->Mem.ShiftType = ShiftType;
427 Op->Mem.ShiftAmount = ShiftAmount;
428 Op->Mem.Preindexed = Preindexed;
429 Op->Mem.Postindexed = Postindexed;
430 Op->Mem.Negative = Negative;
431 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000432
Sean Callanan76264762010-04-02 22:27:05 +0000433 Op->StartLoc = S;
434 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000435 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000436 }
437};
438
439} // end anonymous namespace.
440
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000441void ARMOperand::dump(raw_ostream &OS) const {
442 switch (Kind) {
443 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000444 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000445 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000446 case CCOut:
447 OS << "<ccout " << getReg() << ">";
448 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000449 case Immediate:
450 getImm()->print(OS);
451 break;
452 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000453 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000454 break;
455 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000456 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000457 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000458 case RegisterList:
459 case DPRRegisterList:
460 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000461 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000462
Bill Wendling5fa22a12010-11-09 23:28:44 +0000463 const SmallVectorImpl<unsigned> &RegList = getRegList();
464 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000465 I = RegList.begin(), E = RegList.end(); I != E; ) {
466 OS << *I;
467 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000468 }
469
470 OS << ">";
471 break;
472 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000473 case Token:
474 OS << "'" << getToken() << "'";
475 break;
476 }
477}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000478
479/// @name Auto-generated Match Functions
480/// {
481
482static unsigned MatchRegisterName(StringRef Name);
483
484/// }
485
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000486/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000487/// and if it is a register name the token is eaten and the register number is
488/// returned. Otherwise return -1.
489///
490int ARMAsmParser::TryParseRegister() {
491 const AsmToken &Tok = Parser.getTok();
492 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000493
Chris Lattnere5658fa2010-10-30 04:09:10 +0000494 // FIXME: Validate register for the current architecture; we have to do
495 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000496 unsigned RegNum = MatchRegisterName(Tok.getString());
497 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000498 return -1;
499 Parser.Lex(); // Eat identifier token.
500 return RegNum;
501}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000502
503
Bill Wendling50d0f582010-11-18 23:43:05 +0000504/// Try to parse a register name. The token must be an Identifier when called.
505/// If it's a register, an AsmOperand is created. Another AsmOperand is created
506/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000507///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000508/// TODO this is likely to change to allow different register types and or to
509/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000510bool ARMAsmParser::
511TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000512 SMLoc S = Parser.getTok().getLoc();
513 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000514 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000515 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000516
Bill Wendling50d0f582010-11-18 23:43:05 +0000517 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000518
Chris Lattnere5658fa2010-10-30 04:09:10 +0000519 const AsmToken &ExclaimTok = Parser.getTok();
520 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000521 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
522 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000523 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000524 }
525
Bill Wendling50d0f582010-11-18 23:43:05 +0000526 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000527}
528
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000529/// Parse a register list, return it if successful else return null. The first
530/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000531bool ARMAsmParser::
532ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000533 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000534 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000535 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000536
Bill Wendling7729e062010-11-09 22:44:22 +0000537 // Read the rest of the registers in the list.
538 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000539 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000540
Bill Wendling7729e062010-11-09 22:44:22 +0000541 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000542 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000543 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000544
Sean Callanan18b83232010-01-19 21:44:56 +0000545 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000546 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000547 if (RegTok.isNot(AsmToken::Identifier)) {
548 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000549 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000550 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000551
Bill Wendling1d6a2652010-11-06 10:40:24 +0000552 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000553 if (RegNum == -1) {
554 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000555 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000556 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000557
Bill Wendlinge7176102010-11-06 22:36:58 +0000558 if (IsRange) {
559 int Reg = PrevRegNum;
560 do {
561 ++Reg;
562 Registers.push_back(std::make_pair(Reg, RegLoc));
563 } while (Reg != RegNum);
564 } else {
565 Registers.push_back(std::make_pair(RegNum, RegLoc));
566 }
567
568 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000569 } while (Parser.getTok().is(AsmToken::Comma) ||
570 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000571
572 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000573 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000574 if (RCurlyTok.isNot(AsmToken::RCurly)) {
575 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000576 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000577 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000578
Bill Wendlinge7176102010-11-06 22:36:58 +0000579 SMLoc E = RCurlyTok.getLoc();
580 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000581
Bill Wendlinge7176102010-11-06 22:36:58 +0000582 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000583 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000584 RI = Registers.begin(), RE = Registers.end();
585
Bill Wendling7caebff2011-01-12 21:20:59 +0000586 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000587 bool EmittedWarning = false;
588
Bill Wendling7caebff2011-01-12 21:20:59 +0000589 DenseMap<unsigned, bool> RegMap;
590 RegMap[HighRegNum] = true;
591
Bill Wendlinge7176102010-11-06 22:36:58 +0000592 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000593 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000594 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000595
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000596 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000597 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000598 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000599 }
600
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000601 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000602 Warning(RegInfo.second,
603 "register not in ascending order in register list");
604
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000605 RegMap[Reg] = true;
606 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000607 }
608
Bill Wendling50d0f582010-11-18 23:43:05 +0000609 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
610 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000611}
612
Bill Wendlinge7176102010-11-06 22:36:58 +0000613/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000614/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000615///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000616/// TODO Only preindexing and postindexing addressing are started, unindexed
617/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000618bool ARMAsmParser::
619ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000620 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000621 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000622 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000623 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000624 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000625
Sean Callanan18b83232010-01-19 21:44:56 +0000626 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000627 if (BaseRegTok.isNot(AsmToken::Identifier)) {
628 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000629 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000630 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000631 int BaseRegNum = TryParseRegister();
632 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000633 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000634 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000635 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000636
637 bool Preindexed = false;
638 bool Postindexed = false;
639 bool OffsetIsReg = false;
640 bool Negative = false;
641 bool Writeback = false;
642
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000643 // First look for preindexed address forms, that is after the "[Rn" we now
644 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000645 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000646 if (Tok.is(AsmToken::Comma)) {
647 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000648 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000649 int OffsetRegNum;
650 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000651 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000652 const MCExpr *ShiftAmount = 0;
653 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000654 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
655 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000656 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000657 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000658 if (RBracTok.isNot(AsmToken::RBrac)) {
659 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000660 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000661 }
Sean Callanan76264762010-04-02 22:27:05 +0000662 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000663 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000664
Jim Grosbach03f44a02010-11-29 23:18:01 +0000665
Sean Callanan18b83232010-01-19 21:44:56 +0000666 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000667 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000668 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000669 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
670 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000671 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000672 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000673 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000674
675 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
676 OffsetRegNum, OffsetRegShifted,
677 ShiftType, ShiftAmount, Preindexed,
678 Postindexed, Negative, Writeback,
679 S, E));
680 if (WBOp)
681 Operands.push_back(WBOp);
682
683 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000684 }
685 // The "[Rn" we have so far was not followed by a comma.
686 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000687 // If there's anything other than the right brace, this is a post indexing
688 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000689 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000690 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000691
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000692 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000693 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000694 enum ShiftType ShiftType = Lsl;
695 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000696 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000697
Sean Callanan18b83232010-01-19 21:44:56 +0000698 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000699
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000700 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000701 Postindexed = true;
702 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000703
Chris Lattner550276e2010-10-28 20:52:15 +0000704 if (NextTok.isNot(AsmToken::Comma)) {
705 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000706 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000707 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000708
Sean Callananb9a25b72010-01-19 20:27:46 +0000709 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000710
Chris Lattner550276e2010-10-28 20:52:15 +0000711 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000712 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000713 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000714 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000715 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000716
Bill Wendling50d0f582010-11-18 23:43:05 +0000717 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
718 OffsetRegNum, OffsetRegShifted,
719 ShiftType, ShiftAmount, Preindexed,
720 Postindexed, Negative, Writeback,
721 S, E));
722 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000723 }
724
Bill Wendling50d0f582010-11-18 23:43:05 +0000725 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000726}
727
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000728/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
729/// we will parse the following (were +/- means that a plus or minus is
730/// optional):
731/// +/-Rm
732/// +/-Rm, shift
733/// #offset
734/// we return false on success or an error otherwise.
735bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000736 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000737 enum ShiftType &ShiftType,
738 const MCExpr *&ShiftAmount,
739 const MCExpr *&Offset,
740 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000741 int &OffsetRegNum,
742 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000743 Negative = false;
744 OffsetRegShifted = false;
745 OffsetIsReg = false;
746 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000747 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000748 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000749 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000750 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000751 else if (NextTok.is(AsmToken::Minus)) {
752 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000753 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000754 }
755 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000756 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000757 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000758 SMLoc CurLoc = OffsetRegTok.getLoc();
759 OffsetRegNum = TryParseRegister();
760 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000761 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000762 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000763 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000764 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000765
Bill Wendling12f40e92010-11-06 10:51:53 +0000766 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000767 if (OffsetRegNum != -1) {
768 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000769 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000770 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000771 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000772
Sean Callanan18b83232010-01-19 21:44:56 +0000773 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000774 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000775 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000776 OffsetRegShifted = true;
777 }
778 }
779 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
780 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000781 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000782 if (HashTok.isNot(AsmToken::Hash))
783 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000784
Sean Callananb9a25b72010-01-19 20:27:46 +0000785 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000786
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000787 if (getParser().ParseExpression(Offset))
788 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000789 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000790 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000791 return false;
792}
793
794/// ParseShift as one of these two:
795/// ( lsl | lsr | asr | ror ) , # shift_amount
796/// rrx
797/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000798bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000799 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000800 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000801 if (Tok.isNot(AsmToken::Identifier))
802 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000803 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000804 if (ShiftName == "lsl" || ShiftName == "LSL")
805 St = Lsl;
806 else if (ShiftName == "lsr" || ShiftName == "LSR")
807 St = Lsr;
808 else if (ShiftName == "asr" || ShiftName == "ASR")
809 St = Asr;
810 else if (ShiftName == "ror" || ShiftName == "ROR")
811 St = Ror;
812 else if (ShiftName == "rrx" || ShiftName == "RRX")
813 St = Rrx;
814 else
815 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000816 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000817
818 // Rrx stands alone.
819 if (St == Rrx)
820 return false;
821
822 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000823 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000824 if (HashTok.isNot(AsmToken::Hash))
825 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000826 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000827
828 if (getParser().ParseExpression(ShiftAmount))
829 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000830
831 return false;
832}
833
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000834/// Parse a arm instruction operand. For now this parses the operand regardless
835/// of the mnemonic.
Bill Wendling50d0f582010-11-18 23:43:05 +0000836bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
Sean Callanan76264762010-04-02 22:27:05 +0000837 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000838 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000839 default:
840 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000841 return true;
842 case AsmToken::Identifier: {
843 if (!TryParseRegisterWithWriteBack(Operands))
844 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000845
Kevin Enderby515d5092009-10-15 20:48:48 +0000846 // This was not a register so parse other operands that start with an
847 // identifier (like labels) as expressions and create them as immediates.
848 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000849 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000850 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000851 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000852 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000853 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
854 return false;
855 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000856 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000857 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000858 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000859 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000860 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000861 // #42 -> immediate.
862 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000863 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000864 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000865 const MCExpr *ImmVal;
866 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000867 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000868 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000869 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
870 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000871 case AsmToken::Colon: {
872 // ":lower16:" and ":upper16:" expression prefixes
873 MCSymbolRefExpr::VariantKind RefKind;
874 if (ParsePrefix(RefKind))
875 return true;
876
877 const MCExpr *ExprVal;
878 if (getParser().ParseExpression(ExprVal))
879 return true;
880
881 // TODO: Attach the prefix to the entire expression
882 // instead of just the first symbol.
883 const MCExpr *ModExprVal = ApplyPrefixToExpr(ExprVal, RefKind);
884 if (!ModExprVal) {
885 return TokError("invalid modifier '" + getTok().getIdentifier() +
886 "' (no symbols present)");
887 }
888
889 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
890 Operands.push_back(ARMOperand::CreateImm(ModExprVal, S, E));
891 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000892 }
Jason W Kim9081b4b2011-01-11 23:53:41 +0000893 }
894}
895
896// FIXME: The next 2 routines are hacks to get ARMAsmParser to understand
897// :lower16: and :upper16:
898// It still attaches VK_ARM_HI/LO16 to MCSymbolRefExpr, but it really
899// should be attached to the entire MCExpr as a whole - perhaps using
900// MCTargetExpr?
901bool ARMAsmParser::ParsePrefix(MCSymbolRefExpr::VariantKind &RefKind) {
902 RefKind = MCSymbolRefExpr::VK_None;
903
904 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +0000905 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +0000906 Parser.Lex(); // Eat ':'
907
908 if (getLexer().isNot(AsmToken::Identifier)) {
909 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
910 return true;
911 }
912
913 StringRef IDVal = Parser.getTok().getIdentifier();
914 if (IDVal == "lower16") {
915 RefKind = MCSymbolRefExpr::VK_ARM_LO16;
916 } else if (IDVal == "upper16") {
917 RefKind = MCSymbolRefExpr::VK_ARM_HI16;
918 } else {
919 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
920 return true;
921 }
922 Parser.Lex();
923
924 if (getLexer().isNot(AsmToken::Colon)) {
925 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
926 return true;
927 }
928 Parser.Lex(); // Eat the last ':'
929 return false;
930}
931
932const MCExpr *
933ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
934 MCSymbolRefExpr::VariantKind Variant) {
935 // Recurse over the given expression, rebuilding it to apply the given variant
936 // to the leftmost symbol.
937 if (Variant == MCSymbolRefExpr::VK_None)
938 return E;
939
940 switch (E->getKind()) {
941 case MCExpr::Target:
942 llvm_unreachable("Can't handle target expr yet");
943 case MCExpr::Constant:
944 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
945
946 case MCExpr::SymbolRef: {
947 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
948
949 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
950 return 0;
951
952 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
953 }
954
955 case MCExpr::Unary:
956 llvm_unreachable("Can't handle unary expressions yet");
957
958 case MCExpr::Binary: {
959 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
960 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
961 const MCExpr *RHS = BE->getRHS();
962 if (!LHS)
963 return 0;
964
965 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
966 }
967 }
968
969 assert(0 && "Invalid expression kind!");
970 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000971}
972
Daniel Dunbar352e1482011-01-11 15:59:50 +0000973/// \brief Given a mnemonic, split out possible predication code and carry
974/// setting letters to form a canonical mnemonic and flags.
975//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000976// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000977static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
978 unsigned &PredicationCode,
979 bool &CarrySetting) {
980 PredicationCode = ARMCC::AL;
981 CarrySetting = false;
982
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000983 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000984 //
985 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +0000986 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
987 Mnemonic == "movs" ||
988 Mnemonic == "svc" ||
989 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
990 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
991 Mnemonic == "vacge" || Mnemonic == "vcge" ||
992 Mnemonic == "vclt" ||
993 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
994 Mnemonic == "vcle" ||
995 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
996 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
997 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +0000998 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +0000999
Daniel Dunbar352e1482011-01-11 15:59:50 +00001000 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001001 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001002 .Case("eq", ARMCC::EQ)
1003 .Case("ne", ARMCC::NE)
1004 .Case("hs", ARMCC::HS)
1005 .Case("lo", ARMCC::LO)
1006 .Case("mi", ARMCC::MI)
1007 .Case("pl", ARMCC::PL)
1008 .Case("vs", ARMCC::VS)
1009 .Case("vc", ARMCC::VC)
1010 .Case("hi", ARMCC::HI)
1011 .Case("ls", ARMCC::LS)
1012 .Case("ge", ARMCC::GE)
1013 .Case("lt", ARMCC::LT)
1014 .Case("gt", ARMCC::GT)
1015 .Case("le", ARMCC::LE)
1016 .Case("al", ARMCC::AL)
1017 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001018 if (CC != ~0U) {
1019 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001020 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001021 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001022
Daniel Dunbar352e1482011-01-11 15:59:50 +00001023 // Next, determine if we have a carry setting bit. We explicitly ignore all
1024 // the instructions we know end in 's'.
1025 if (Mnemonic.endswith("s") &&
1026 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1027 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1028 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1029 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1030 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1031 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1032 CarrySetting = true;
1033 }
1034
1035 return Mnemonic;
1036}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001037
1038/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1039/// inclusion of carry set or predication code operands.
1040//
1041// FIXME: It would be nice to autogen this.
1042static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1043 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001044 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1045 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1046 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1047 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1048 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1049 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1050 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1051 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1052 CanAcceptCarrySet = true;
1053 } else {
1054 CanAcceptCarrySet = false;
1055 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001056
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001057 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1058 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1059 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1060 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1061 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001062 CanAcceptPredicationCode = false;
1063 } else {
1064 CanAcceptPredicationCode = true;
1065 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001066}
1067
1068/// Parse an arm instruction mnemonic followed by its operands.
1069bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1070 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1071 // Create the leading tokens for the mnemonic, split by '.' characters.
1072 size_t Start = 0, Next = Name.find('.');
1073 StringRef Head = Name.slice(Start, Next);
1074
Daniel Dunbar352e1482011-01-11 15:59:50 +00001075 // Split out the predication code and carry setting flag from the mnemonic.
1076 unsigned PredicationCode;
1077 bool CarrySetting;
1078 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001079
Chris Lattner3a697562010-10-28 17:20:03 +00001080 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001081
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001082 // Next, add the CCOut and ConditionCode operands, if needed.
1083 //
1084 // For mnemonics which can ever incorporate a carry setting bit or predication
1085 // code, our matching model involves us always generating CCOut and
1086 // ConditionCode operands to match the mnemonic "as written" and then we let
1087 // the matcher deal with finding the right instruction or generating an
1088 // appropriate error.
1089 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1090 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1091
1092 // Add the carry setting operand, if necessary.
1093 //
1094 // FIXME: It would be awesome if we could somehow invent a location such that
1095 // match errors on this operand would print a nice diagnostic about how the
1096 // 's' character in the mnemonic resulted in a CCOut operand.
1097 if (CanAcceptCarrySet) {
1098 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1099 NameLoc));
1100 } else {
1101 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1102 // misspelled another mnemonic).
1103
1104 // FIXME: Issue a nice error.
1105 }
1106
1107 // Add the predication code operand, if necessary.
1108 if (CanAcceptPredicationCode) {
1109 Operands.push_back(ARMOperand::CreateCondCode(
1110 ARMCC::CondCodes(PredicationCode), NameLoc));
1111 } else {
1112 // This mnemonic can't ever accept a predication code, but the user wrote
1113 // one (or misspelled another mnemonic).
1114
1115 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001116 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001117
1118 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001119 while (Next != StringRef::npos) {
1120 Start = Next;
1121 Next = Name.find('.', Start + 1);
1122 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001123
Chris Lattner3a697562010-10-28 17:20:03 +00001124 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001125 }
1126
1127 // Read the remaining operands.
1128 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001129 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001130 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001131 Parser.EatToEndOfStatement();
1132 return true;
1133 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001134
1135 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001136 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001137
1138 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001139 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001140 Parser.EatToEndOfStatement();
1141 return true;
1142 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001143 }
1144 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001145
Chris Lattnercbf8a982010-09-11 16:18:25 +00001146 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1147 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001148 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001149 }
Bill Wendling146018f2010-11-06 21:42:12 +00001150
Chris Lattner34e53142010-09-08 05:10:46 +00001151 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001152 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001153}
1154
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001155bool ARMAsmParser::
1156MatchAndEmitInstruction(SMLoc IDLoc,
1157 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1158 MCStreamer &Out) {
1159 MCInst Inst;
1160 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001161 MatchResultTy MatchResult, MatchResult2;
1162 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1163 if (MatchResult != Match_Success) {
1164 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1165 // that does not update the condition codes. So try adding a CCOut operand
1166 // with a value of reg0.
1167 if (MatchResult == Match_InvalidOperand) {
1168 Operands.insert(Operands.begin() + 1,
1169 ARMOperand::CreateCCOut(0,
1170 ((ARMOperand*)Operands[0])->getStartLoc()));
1171 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1172 if (MatchResult2 == Match_Success)
1173 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001174 else {
1175 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001176 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001177 delete CCOut;
1178 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001179 }
1180 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1181 // that updates the condition codes if it ends in 's'. So see if the
1182 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1183 // operand with a value of CPSR.
1184 else if(MatchResult == Match_MnemonicFail) {
1185 // Get the instruction mnemonic, which is the first token.
1186 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1187 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1188 // removed the 's' from the mnemonic for matching.
1189 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1190 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001191 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1192 Operands.erase(Operands.begin());
1193 delete OldMnemonic;
1194 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001195 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1196 Operands.insert(Operands.begin() + 1,
1197 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1198 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1199 if (MatchResult2 == Match_Success)
1200 MatchResult = Match_Success;
1201 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001202 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1203 Operands.erase(Operands.begin());
1204 delete OldMnemonic;
1205 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001206 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001207 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1208 Operands.erase(Operands.begin() + 1);
1209 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001210 }
1211 }
1212 }
1213 }
1214 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001215 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001216 Out.EmitInstruction(Inst);
1217 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001218 case Match_MissingFeature:
1219 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1220 return true;
1221 case Match_InvalidOperand: {
1222 SMLoc ErrorLoc = IDLoc;
1223 if (ErrorInfo != ~0U) {
1224 if (ErrorInfo >= Operands.size())
1225 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001226
Chris Lattnere73d4f82010-10-28 21:41:58 +00001227 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1228 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1229 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001230
Chris Lattnere73d4f82010-10-28 21:41:58 +00001231 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001232 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001233 case Match_MnemonicFail:
1234 return Error(IDLoc, "unrecognized instruction mnemonic");
1235 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001236
Eric Christopherc223e2b2010-10-29 09:26:59 +00001237 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001238 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001239}
1240
Kevin Enderby515d5092009-10-15 20:48:48 +00001241/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001242bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1243 StringRef IDVal = DirectiveID.getIdentifier();
1244 if (IDVal == ".word")
1245 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001246 else if (IDVal == ".thumb")
1247 return ParseDirectiveThumb(DirectiveID.getLoc());
1248 else if (IDVal == ".thumb_func")
1249 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1250 else if (IDVal == ".code")
1251 return ParseDirectiveCode(DirectiveID.getLoc());
1252 else if (IDVal == ".syntax")
1253 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001254 return true;
1255}
1256
1257/// ParseDirectiveWord
1258/// ::= .word [ expression (, expression)* ]
1259bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1260 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1261 for (;;) {
1262 const MCExpr *Value;
1263 if (getParser().ParseExpression(Value))
1264 return true;
1265
Chris Lattneraaec2052010-01-19 19:46:13 +00001266 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001267
1268 if (getLexer().is(AsmToken::EndOfStatement))
1269 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001270
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001271 // FIXME: Improve diagnostic.
1272 if (getLexer().isNot(AsmToken::Comma))
1273 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001274 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001275 }
1276 }
1277
Sean Callananb9a25b72010-01-19 20:27:46 +00001278 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001279 return false;
1280}
1281
Kevin Enderby515d5092009-10-15 20:48:48 +00001282/// ParseDirectiveThumb
1283/// ::= .thumb
1284bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1285 if (getLexer().isNot(AsmToken::EndOfStatement))
1286 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001287 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001288
1289 // TODO: set thumb mode
1290 // TODO: tell the MC streamer the mode
1291 // getParser().getStreamer().Emit???();
1292 return false;
1293}
1294
1295/// ParseDirectiveThumbFunc
1296/// ::= .thumbfunc symbol_name
1297bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001298 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001299 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001300 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001301 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001302 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001303 if (getLexer().isNot(AsmToken::EndOfStatement))
1304 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001305 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001306
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001307 // Mark symbol as a thumb symbol.
1308 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1309 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001310 return false;
1311}
1312
1313/// ParseDirectiveSyntax
1314/// ::= .syntax unified | divided
1315bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001316 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001317 if (Tok.isNot(AsmToken::Identifier))
1318 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001319 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001320 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001321 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001322 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001323 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001324 else
1325 return Error(L, "unrecognized syntax mode in .syntax directive");
1326
1327 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001328 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001329 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001330
1331 // TODO tell the MC streamer the mode
1332 // getParser().getStreamer().Emit???();
1333 return false;
1334}
1335
1336/// ParseDirectiveCode
1337/// ::= .code 16 | 32
1338bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001339 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001340 if (Tok.isNot(AsmToken::Integer))
1341 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001342 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001343 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001344 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001345 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001346 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001347 else
1348 return Error(L, "invalid operand to .code directive");
1349
1350 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001351 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001352 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001353
Jim Grosbach2a301702010-11-05 22:40:53 +00001354 if (Val == 16)
1355 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1356 else
1357 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1358
Kevin Enderby515d5092009-10-15 20:48:48 +00001359 return false;
1360}
1361
Sean Callanan90b70972010-04-07 20:29:34 +00001362extern "C" void LLVMInitializeARMAsmLexer();
1363
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001364/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001365extern "C" void LLVMInitializeARMAsmParser() {
1366 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1367 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001368 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001369}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001370
Chris Lattner0692ee62010-09-06 19:11:01 +00001371#define GET_REGISTER_MATCHER
1372#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001373#include "ARMGenAsmMatcher.inc"