blob: 5d3b147bf46fadc9ac4e8d1b69b37b7ea578d2c7 [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 Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000018#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000028#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000030using namespace llvm;
31
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000032/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000033enum ShiftType {
34 Lsl,
35 Lsr,
36 Asr,
37 Ror,
38 Rrx
39};
40
Chris Lattner3a697562010-10-28 17:20:03 +000041namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000042
43class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000044
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045class ARMAsmParser : public TargetAsmParser {
46 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000047 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000049 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
51
52 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000053 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
54
Chris Lattnere5658fa2010-10-30 04:09:10 +000055 int TryParseRegister();
Owen Andersone4e5e2a2011-01-13 21:46:02 +000056 bool TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*>&);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Andersone4e5e2a2011-01-13 21:46:02 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, bool isMCR);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
68 enum ShiftType &ShiftType,
69 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000074 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000075 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000084
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000085 /// @name Auto-generated Match Functions
86 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000087
Chris Lattner0692ee62010-09-06 19:11:01 +000088#define GET_ASSEMBLER_HEADER
89#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000091 /// }
92
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000094 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000095 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
96 // Initialize the set of available features.
97 setAvailableFeatures(ComputeAvailableFeatures(
98 &TM.getSubtarget<ARMSubtarget>()));
99 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000100
Benjamin Kramer38e59892010-07-14 22:38:02 +0000101 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000102 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000103 virtual bool ParseDirective(AsmToken DirectiveID);
104};
Jim Grosbach16c74252010-10-29 14:46:02 +0000105} // end anonymous namespace
106
Chris Lattner3a697562010-10-28 17:20:03 +0000107namespace {
108
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000109/// ARMOperand - Instances of this class represent a parsed ARM machine
110/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000111class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000112 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000113 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000114 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000115 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000116 Memory,
117 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000118 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000119 DPRRegisterList,
120 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000122 } Kind;
123
Sean Callanan76264762010-04-02 22:27:05 +0000124 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000125 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000126
127 union {
128 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000129 ARMCC::CondCodes Val;
130 } CC;
131
132 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000133 const char *Data;
134 unsigned Length;
135 } Tok;
136
137 struct {
138 unsigned RegNum;
139 } Reg;
140
Bill Wendling8155e5b2010-11-06 22:19:43 +0000141 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000142 const MCExpr *Val;
143 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000144
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000145 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000146 struct {
147 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000148 unsigned OffsetRegNum; // used when OffsetIsReg is true
149 const MCExpr *Offset; // used when OffsetIsReg is false
150 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
151 enum ShiftType ShiftType; // used when OffsetRegShifted is true
152 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000153 unsigned Preindexed : 1;
154 unsigned Postindexed : 1;
155 unsigned OffsetIsReg : 1;
156 unsigned Negative : 1; // only used when OffsetIsReg is true
157 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000158 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000159 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000160
Bill Wendling146018f2010-11-06 21:42:12 +0000161 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
162public:
Sean Callanan76264762010-04-02 22:27:05 +0000163 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
164 Kind = o.Kind;
165 StartLoc = o.StartLoc;
166 EndLoc = o.EndLoc;
167 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 case CondCode:
169 CC = o.CC;
170 break;
Sean Callanan76264762010-04-02 22:27:05 +0000171 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000172 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000173 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000174 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000175 case Register:
176 Reg = o.Reg;
177 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000178 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000179 case DPRRegisterList:
180 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000181 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000182 break;
Sean Callanan76264762010-04-02 22:27:05 +0000183 case Immediate:
184 Imm = o.Imm;
185 break;
186 case Memory:
187 Mem = o.Mem;
188 break;
189 }
190 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000191
Sean Callanan76264762010-04-02 22:27:05 +0000192 /// getStartLoc - Get the location of the first token of this operand.
193 SMLoc getStartLoc() const { return StartLoc; }
194 /// getEndLoc - Get the location of the last token of this operand.
195 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000196
Daniel Dunbar8462b302010-08-11 06:36:53 +0000197 ARMCC::CondCodes getCondCode() const {
198 assert(Kind == CondCode && "Invalid access!");
199 return CC.Val;
200 }
201
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000202 StringRef getToken() const {
203 assert(Kind == Token && "Invalid access!");
204 return StringRef(Tok.Data, Tok.Length);
205 }
206
207 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000208 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000209 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210 }
211
Bill Wendling5fa22a12010-11-09 23:28:44 +0000212 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000213 assert((Kind == RegisterList || Kind == DPRRegisterList ||
214 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000215 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000216 }
217
Kevin Enderbycfe07242009-10-13 22:19:02 +0000218 const MCExpr *getImm() const {
219 assert(Kind == Immediate && "Invalid access!");
220 return Imm.Val;
221 }
222
Daniel Dunbar8462b302010-08-11 06:36:53 +0000223 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000224 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000225 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000226 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000227 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000228 bool isDPRRegList() const { return Kind == DPRRegisterList; }
229 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000230 bool isToken() const { return Kind == Token; }
231 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000232 bool isMemMode5() const {
233 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
234 Mem.Writeback || Mem.Negative)
235 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000236
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000237 // If there is an offset expression, make sure it's valid.
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000238 if (!Mem.Offset) return true;
239
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000240 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000241 if (!CE) return false;
242
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000243 // The offset must be a multiple of 4 in the range 0-1020.
244 int64_t Value = CE->getValue();
245 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
246 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000247 bool isMemModeRegThumb() const {
248 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
249 return false;
250 return !Mem.Offset || !isa<MCConstantExpr>(Mem.Offset);
251 }
252 bool isMemModeImmThumb() const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000253 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
254 return false;
255
Bill Wendlingf4caf692010-12-14 03:36:38 +0000256 if (!Mem.Offset) return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000257
258 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
259 if (!CE) return false;
260
261 // The offset must be a multiple of 4 in the range 0-124.
262 uint64_t Value = CE->getValue();
263 return ((Value & 0x3) == 0 && Value <= 124);
264 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000265
266 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000267 // Add as immediates when possible. Null MCExpr = 0.
268 if (Expr == 0)
269 Inst.addOperand(MCOperand::CreateImm(0));
270 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000271 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
272 else
273 Inst.addOperand(MCOperand::CreateExpr(Expr));
274 }
275
Daniel Dunbar8462b302010-08-11 06:36:53 +0000276 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000277 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000278 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000279 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
280 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000281 }
282
Jim Grosbachd67641b2010-12-06 18:21:12 +0000283 void addCCOutOperands(MCInst &Inst, unsigned N) const {
284 assert(N == 1 && "Invalid number of operands!");
285 Inst.addOperand(MCOperand::CreateReg(getReg()));
286 }
287
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000288 void addRegOperands(MCInst &Inst, unsigned N) const {
289 assert(N == 1 && "Invalid number of operands!");
290 Inst.addOperand(MCOperand::CreateReg(getReg()));
291 }
292
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000293 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000294 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000295 const SmallVectorImpl<unsigned> &RegList = getRegList();
296 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000297 I = RegList.begin(), E = RegList.end(); I != E; ++I)
298 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000299 }
300
Bill Wendling0f630752010-11-17 04:32:08 +0000301 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
302 addRegListOperands(Inst, N);
303 }
304
305 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
306 addRegListOperands(Inst, N);
307 }
308
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000309 void addImmOperands(MCInst &Inst, unsigned N) const {
310 assert(N == 1 && "Invalid number of operands!");
311 addExpr(Inst, getImm());
312 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000313
Chris Lattner14b93852010-10-29 00:27:31 +0000314 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
315 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000316
Chris Lattner14b93852010-10-29 00:27:31 +0000317 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000318 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000319
Jim Grosbach80eb2332010-10-29 17:41:25 +0000320 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
321 // the difference?
322 if (Mem.Offset) {
323 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000324 assert(CE && "Non-constant mode 5 offset operand!");
325
Jim Grosbach80eb2332010-10-29 17:41:25 +0000326 // The MCInst offset operand doesn't include the low two bits (like
327 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000328 int64_t Offset = CE->getValue() / 4;
329 if (Offset >= 0)
330 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
331 Offset)));
332 else
333 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
334 -Offset)));
335 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000336 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000337 }
Chris Lattner14b93852010-10-29 00:27:31 +0000338 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000339
Bill Wendlingf4caf692010-12-14 03:36:38 +0000340 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
341 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000342 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000343 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
344 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000345
Bill Wendlingf4caf692010-12-14 03:36:38 +0000346 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
347 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
348 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
349 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
350 assert(CE && "Non-constant mode offset operand!");
351 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000352 }
353
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000354 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000355
Chris Lattner3a697562010-10-28 17:20:03 +0000356 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
357 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000358 Op->CC.Val = CC;
359 Op->StartLoc = S;
360 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000361 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000362 }
363
Jim Grosbachd67641b2010-12-06 18:21:12 +0000364 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
365 ARMOperand *Op = new ARMOperand(CCOut);
366 Op->Reg.RegNum = RegNum;
367 Op->StartLoc = S;
368 Op->EndLoc = S;
369 return Op;
370 }
371
Chris Lattner3a697562010-10-28 17:20:03 +0000372 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
373 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000374 Op->Tok.Data = Str.data();
375 Op->Tok.Length = Str.size();
376 Op->StartLoc = S;
377 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000378 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000379 }
380
Bill Wendling50d0f582010-11-18 23:43:05 +0000381 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000382 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000383 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000384 Op->StartLoc = S;
385 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000386 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000387 }
388
Bill Wendling7729e062010-11-09 22:44:22 +0000389 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000390 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000391 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000392 KindTy Kind = RegisterList;
393
394 if (ARM::DPRRegClass.contains(Regs.front().first))
395 Kind = DPRRegisterList;
396 else if (ARM::SPRRegClass.contains(Regs.front().first))
397 Kind = SPRRegisterList;
398
399 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000400 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000401 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000402 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000403 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000404 Op->StartLoc = StartLoc;
405 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000406 return Op;
407 }
408
Chris Lattner3a697562010-10-28 17:20:03 +0000409 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
410 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000411 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000412 Op->StartLoc = S;
413 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000414 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000415 }
416
Chris Lattner3a697562010-10-28 17:20:03 +0000417 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000418 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000419 bool OffsetRegShifted, enum ShiftType ShiftType,
420 const MCExpr *ShiftAmount, bool Preindexed,
421 bool Postindexed, bool Negative, bool Writeback,
422 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000423 assert((OffsetRegNum == -1 || OffsetIsReg) &&
424 "OffsetRegNum must imply OffsetIsReg!");
425 assert((!OffsetRegShifted || OffsetIsReg) &&
426 "OffsetRegShifted must imply OffsetIsReg!");
427 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
428 "Cannot have shift amount without shifted register offset!");
429 assert((!Offset || !OffsetIsReg) &&
430 "Cannot have expression offset and register offset!");
431
Chris Lattner3a697562010-10-28 17:20:03 +0000432 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000433 Op->Mem.BaseRegNum = BaseRegNum;
434 Op->Mem.OffsetIsReg = OffsetIsReg;
435 Op->Mem.Offset = Offset;
436 Op->Mem.OffsetRegNum = OffsetRegNum;
437 Op->Mem.OffsetRegShifted = OffsetRegShifted;
438 Op->Mem.ShiftType = ShiftType;
439 Op->Mem.ShiftAmount = ShiftAmount;
440 Op->Mem.Preindexed = Preindexed;
441 Op->Mem.Postindexed = Postindexed;
442 Op->Mem.Negative = Negative;
443 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000444
Sean Callanan76264762010-04-02 22:27:05 +0000445 Op->StartLoc = S;
446 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000447 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000448 }
449};
450
451} // end anonymous namespace.
452
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000453void ARMOperand::dump(raw_ostream &OS) const {
454 switch (Kind) {
455 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000456 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000457 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000458 case CCOut:
459 OS << "<ccout " << getReg() << ">";
460 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000461 case Immediate:
462 getImm()->print(OS);
463 break;
464 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000465 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000466 break;
467 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000468 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000469 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000470 case RegisterList:
471 case DPRRegisterList:
472 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000473 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000474
Bill Wendling5fa22a12010-11-09 23:28:44 +0000475 const SmallVectorImpl<unsigned> &RegList = getRegList();
476 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000477 I = RegList.begin(), E = RegList.end(); I != E; ) {
478 OS << *I;
479 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000480 }
481
482 OS << ">";
483 break;
484 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000485 case Token:
486 OS << "'" << getToken() << "'";
487 break;
488 }
489}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000490
491/// @name Auto-generated Match Functions
492/// {
493
494static unsigned MatchRegisterName(StringRef Name);
495
496/// }
497
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000498/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000499/// and if it is a register name the token is eaten and the register number is
500/// returned. Otherwise return -1.
501///
502int ARMAsmParser::TryParseRegister() {
503 const AsmToken &Tok = Parser.getTok();
504 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000505
Chris Lattnere5658fa2010-10-30 04:09:10 +0000506 // FIXME: Validate register for the current architecture; we have to do
507 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000508 std::string upperCase = Tok.getString().str();
509 std::string lowerCase = LowercaseString(upperCase);
510 unsigned RegNum = MatchRegisterName(lowerCase);
511 if (!RegNum) {
512 RegNum = StringSwitch<unsigned>(lowerCase)
513 .Case("r13", ARM::SP)
514 .Case("r14", ARM::LR)
515 .Case("r15", ARM::PC)
516 .Case("ip", ARM::R12)
517 .Default(0);
518 }
519 if (!RegNum) return -1;
520
Chris Lattnere5658fa2010-10-30 04:09:10 +0000521 Parser.Lex(); // Eat identifier token.
522 return RegNum;
523}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000524
525
Bill Wendling50d0f582010-11-18 23:43:05 +0000526/// Try to parse a register name. The token must be an Identifier when called.
527/// If it's a register, an AsmOperand is created. Another AsmOperand is created
528/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000529///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000530/// TODO this is likely to change to allow different register types and or to
531/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000532bool ARMAsmParser::
533TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000534 SMLoc S = Parser.getTok().getLoc();
535 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000536 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000537 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000538
Bill Wendling50d0f582010-11-18 23:43:05 +0000539 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000540
Chris Lattnere5658fa2010-10-30 04:09:10 +0000541 const AsmToken &ExclaimTok = Parser.getTok();
542 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000543 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
544 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000545 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000546 }
547
Bill Wendling50d0f582010-11-18 23:43:05 +0000548 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000549}
550
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000551static int MatchMCRName(StringRef Name) {
552 // Use the same layout as the tablegen'erated register name matcher. Ugly,
553 // but efficient.
554 switch (Name.size()) {
555 default: break;
556 case 2:
557 if (Name[0] != 'p' && Name[0] != 'c')
558 return -1;
559 switch (Name[1]) {
560 default: return -1;
561 case '0': return 0;
562 case '1': return 1;
563 case '2': return 2;
564 case '3': return 3;
565 case '4': return 4;
566 case '5': return 5;
567 case '6': return 6;
568 case '7': return 7;
569 case '8': return 8;
570 case '9': return 9;
571 }
572 break;
573 case 3:
574 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
575 return -1;
576 switch (Name[2]) {
577 default: return -1;
578 case '0': return 10;
579 case '1': return 11;
580 case '2': return 12;
581 case '3': return 13;
582 case '4': return 14;
583 case '5': return 15;
584 }
585 break;
586 }
587
588 llvm_unreachable("Unhandled coprocessor operand string!");
589 return -1;
590}
591
592/// TryParseMCRName - Try to parse an MCR/MRC symbolic operand
593/// name. The token must be an Identifier when called, and if it is a MCR
594/// operand name, the token is eaten and the operand is added to the
595/// operand list.
596bool ARMAsmParser::
597TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
598 SMLoc S = Parser.getTok().getLoc();
599 const AsmToken &Tok = Parser.getTok();
600 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
601
602 int Num = MatchMCRName(Tok.getString());
603 if (Num == -1)
604 return true;
605
606 Parser.Lex(); // Eat identifier token.
607 Operands.push_back(ARMOperand::CreateImm(
608 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
609 return false;
610}
611
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000612/// Parse a register list, return it if successful else return null. The first
613/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000614bool ARMAsmParser::
615ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000616 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000617 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000618 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000619
Bill Wendling7729e062010-11-09 22:44:22 +0000620 // Read the rest of the registers in the list.
621 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000622 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000623
Bill Wendling7729e062010-11-09 22:44:22 +0000624 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000625 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000626 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000627
Sean Callanan18b83232010-01-19 21:44:56 +0000628 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000629 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000630 if (RegTok.isNot(AsmToken::Identifier)) {
631 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000632 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000633 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000634
Bill Wendling1d6a2652010-11-06 10:40:24 +0000635 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000636 if (RegNum == -1) {
637 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000638 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000639 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000640
Bill Wendlinge7176102010-11-06 22:36:58 +0000641 if (IsRange) {
642 int Reg = PrevRegNum;
643 do {
644 ++Reg;
645 Registers.push_back(std::make_pair(Reg, RegLoc));
646 } while (Reg != RegNum);
647 } else {
648 Registers.push_back(std::make_pair(RegNum, RegLoc));
649 }
650
651 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000652 } while (Parser.getTok().is(AsmToken::Comma) ||
653 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000654
655 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000656 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000657 if (RCurlyTok.isNot(AsmToken::RCurly)) {
658 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000659 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000660 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000661
Bill Wendlinge7176102010-11-06 22:36:58 +0000662 SMLoc E = RCurlyTok.getLoc();
663 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000664
Bill Wendlinge7176102010-11-06 22:36:58 +0000665 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000666 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000667 RI = Registers.begin(), RE = Registers.end();
668
Bill Wendling7caebff2011-01-12 21:20:59 +0000669 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000670 bool EmittedWarning = false;
671
Bill Wendling7caebff2011-01-12 21:20:59 +0000672 DenseMap<unsigned, bool> RegMap;
673 RegMap[HighRegNum] = true;
674
Bill Wendlinge7176102010-11-06 22:36:58 +0000675 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000676 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000677 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000678
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000679 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000680 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000681 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000682 }
683
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000684 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000685 Warning(RegInfo.second,
686 "register not in ascending order in register list");
687
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000688 RegMap[Reg] = true;
689 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000690 }
691
Bill Wendling50d0f582010-11-18 23:43:05 +0000692 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
693 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000694}
695
Bill Wendlinge7176102010-11-06 22:36:58 +0000696/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000697/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000698///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000699/// TODO Only preindexing and postindexing addressing are started, unindexed
700/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000701bool ARMAsmParser::
702ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000703 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000704 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000705 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000706 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000707 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000708
Sean Callanan18b83232010-01-19 21:44:56 +0000709 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000710 if (BaseRegTok.isNot(AsmToken::Identifier)) {
711 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000712 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000713 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000714 int BaseRegNum = TryParseRegister();
715 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000716 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000717 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000718 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000719
720 bool Preindexed = false;
721 bool Postindexed = false;
722 bool OffsetIsReg = false;
723 bool Negative = false;
724 bool Writeback = false;
725
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000726 // First look for preindexed address forms, that is after the "[Rn" we now
727 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000728 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000729 if (Tok.is(AsmToken::Comma)) {
730 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000731 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000732 int OffsetRegNum;
733 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000734 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000735 const MCExpr *ShiftAmount = 0;
736 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000737 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
738 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000739 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000740 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000741 if (RBracTok.isNot(AsmToken::RBrac)) {
742 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000743 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000744 }
Sean Callanan76264762010-04-02 22:27:05 +0000745 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000746 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000747
Jim Grosbach03f44a02010-11-29 23:18:01 +0000748
Sean Callanan18b83232010-01-19 21:44:56 +0000749 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000750 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000751 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000752 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
753 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000754 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000755 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000756 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000757
758 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
759 OffsetRegNum, OffsetRegShifted,
760 ShiftType, ShiftAmount, Preindexed,
761 Postindexed, Negative, Writeback,
762 S, E));
763 if (WBOp)
764 Operands.push_back(WBOp);
765
766 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000767 }
768 // The "[Rn" we have so far was not followed by a comma.
769 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000770 // If there's anything other than the right brace, this is a post indexing
771 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000772 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000773 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000774
Daniel Dunbar81f453c2011-01-18 05:33:57 +0000775 int OffsetRegNum = -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000776 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000777 enum ShiftType ShiftType = Lsl;
778 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000779 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000780
Sean Callanan18b83232010-01-19 21:44:56 +0000781 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000782
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000783 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000784 Postindexed = true;
785 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000786
Chris Lattner550276e2010-10-28 20:52:15 +0000787 if (NextTok.isNot(AsmToken::Comma)) {
788 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000789 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000790 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000791
Sean Callananb9a25b72010-01-19 20:27:46 +0000792 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000793
Chris Lattner550276e2010-10-28 20:52:15 +0000794 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000795 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000796 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000797 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000798 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000799
Bill Wendling50d0f582010-11-18 23:43:05 +0000800 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
801 OffsetRegNum, OffsetRegShifted,
802 ShiftType, ShiftAmount, Preindexed,
803 Postindexed, Negative, Writeback,
804 S, E));
805 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000806 }
807
Bill Wendling50d0f582010-11-18 23:43:05 +0000808 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000809}
810
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000811/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
812/// we will parse the following (were +/- means that a plus or minus is
813/// optional):
814/// +/-Rm
815/// +/-Rm, shift
816/// #offset
817/// we return false on success or an error otherwise.
818bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000819 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000820 enum ShiftType &ShiftType,
821 const MCExpr *&ShiftAmount,
822 const MCExpr *&Offset,
823 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000824 int &OffsetRegNum,
825 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000826 Negative = false;
827 OffsetRegShifted = false;
828 OffsetIsReg = false;
829 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000830 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000831 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000832 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000833 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000834 else if (NextTok.is(AsmToken::Minus)) {
835 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000836 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000837 }
838 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000839 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000840 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000841 SMLoc CurLoc = OffsetRegTok.getLoc();
842 OffsetRegNum = TryParseRegister();
843 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000844 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000845 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000846 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000847 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000848
Bill Wendling12f40e92010-11-06 10:51:53 +0000849 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000850 if (OffsetRegNum != -1) {
851 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000852 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000853 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000854 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000855
Sean Callanan18b83232010-01-19 21:44:56 +0000856 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000857 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000858 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000859 OffsetRegShifted = true;
860 }
861 }
862 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
863 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000864 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000865 if (HashTok.isNot(AsmToken::Hash))
866 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000867
Sean Callananb9a25b72010-01-19 20:27:46 +0000868 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000869
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000870 if (getParser().ParseExpression(Offset))
871 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000872 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000873 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000874 return false;
875}
876
877/// ParseShift as one of these two:
878/// ( lsl | lsr | asr | ror ) , # shift_amount
879/// rrx
880/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000881bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000882 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000883 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000884 if (Tok.isNot(AsmToken::Identifier))
885 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000886 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000887 if (ShiftName == "lsl" || ShiftName == "LSL")
888 St = Lsl;
889 else if (ShiftName == "lsr" || ShiftName == "LSR")
890 St = Lsr;
891 else if (ShiftName == "asr" || ShiftName == "ASR")
892 St = Asr;
893 else if (ShiftName == "ror" || ShiftName == "ROR")
894 St = Ror;
895 else if (ShiftName == "rrx" || ShiftName == "RRX")
896 St = Rrx;
897 else
898 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000899 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000900
901 // Rrx stands alone.
902 if (St == Rrx)
903 return false;
904
905 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000906 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000907 if (HashTok.isNot(AsmToken::Hash))
908 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000909 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000910
911 if (getParser().ParseExpression(ShiftAmount))
912 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000913
914 return false;
915}
916
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000917/// Parse a arm instruction operand. For now this parses the operand regardless
918/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000919bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
920 bool isMCR){
Sean Callanan76264762010-04-02 22:27:05 +0000921 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000922 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000923 default:
924 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000925 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000926 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000927 if (!TryParseRegisterWithWriteBack(Operands))
928 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000929 if (isMCR && !TryParseMCRName(Operands))
930 return false;
931
932 // Fall though for the Identifier case that is not a register or a
933 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000934 case AsmToken::Integer: // things like 1f and 2b as a branch targets
935 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000936 // This was not a register so parse other operands that start with an
937 // identifier (like labels) as expressions and create them as immediates.
938 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000939 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000940 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000941 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000942 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000943 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
944 return false;
945 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000946 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000947 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000948 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000949 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000950 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000951 // #42 -> immediate.
952 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000953 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000954 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000955 const MCExpr *ImmVal;
956 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000957 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000958 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000959 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
960 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000961 case AsmToken::Colon: {
962 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +0000963 // FIXME: Check it's an expression prefix,
964 // e.g. (FOO - :lower16:BAR) isn't legal.
965 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000966 if (ParsePrefix(RefKind))
967 return true;
968
Evan Cheng75972122011-01-13 07:58:56 +0000969 const MCExpr *SubExprVal;
970 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +0000971 return true;
972
Evan Cheng75972122011-01-13 07:58:56 +0000973 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
974 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +0000975 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +0000976 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +0000977 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000978 }
Jason W Kim9081b4b2011-01-11 23:53:41 +0000979 }
980}
981
Evan Cheng75972122011-01-13 07:58:56 +0000982// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
983// :lower16: and :upper16:.
984bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
985 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000986
987 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +0000988 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +0000989 Parser.Lex(); // Eat ':'
990
991 if (getLexer().isNot(AsmToken::Identifier)) {
992 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
993 return true;
994 }
995
996 StringRef IDVal = Parser.getTok().getIdentifier();
997 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +0000998 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000999 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001000 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001001 } else {
1002 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1003 return true;
1004 }
1005 Parser.Lex();
1006
1007 if (getLexer().isNot(AsmToken::Colon)) {
1008 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1009 return true;
1010 }
1011 Parser.Lex(); // Eat the last ':'
1012 return false;
1013}
1014
1015const MCExpr *
1016ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1017 MCSymbolRefExpr::VariantKind Variant) {
1018 // Recurse over the given expression, rebuilding it to apply the given variant
1019 // to the leftmost symbol.
1020 if (Variant == MCSymbolRefExpr::VK_None)
1021 return E;
1022
1023 switch (E->getKind()) {
1024 case MCExpr::Target:
1025 llvm_unreachable("Can't handle target expr yet");
1026 case MCExpr::Constant:
1027 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1028
1029 case MCExpr::SymbolRef: {
1030 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1031
1032 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1033 return 0;
1034
1035 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1036 }
1037
1038 case MCExpr::Unary:
1039 llvm_unreachable("Can't handle unary expressions yet");
1040
1041 case MCExpr::Binary: {
1042 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1043 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1044 const MCExpr *RHS = BE->getRHS();
1045 if (!LHS)
1046 return 0;
1047
1048 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1049 }
1050 }
1051
1052 assert(0 && "Invalid expression kind!");
1053 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001054}
1055
Daniel Dunbar352e1482011-01-11 15:59:50 +00001056/// \brief Given a mnemonic, split out possible predication code and carry
1057/// setting letters to form a canonical mnemonic and flags.
1058//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001059// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001060static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1061 unsigned &PredicationCode,
1062 bool &CarrySetting) {
1063 PredicationCode = ARMCC::AL;
1064 CarrySetting = false;
1065
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001066 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001067 //
1068 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001069 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1070 Mnemonic == "movs" ||
1071 Mnemonic == "svc" ||
1072 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1073 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1074 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1075 Mnemonic == "vclt" ||
1076 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1077 Mnemonic == "vcle" ||
1078 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1079 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1080 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001081 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001082
Daniel Dunbar352e1482011-01-11 15:59:50 +00001083 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001084 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001085 .Case("eq", ARMCC::EQ)
1086 .Case("ne", ARMCC::NE)
1087 .Case("hs", ARMCC::HS)
1088 .Case("lo", ARMCC::LO)
1089 .Case("mi", ARMCC::MI)
1090 .Case("pl", ARMCC::PL)
1091 .Case("vs", ARMCC::VS)
1092 .Case("vc", ARMCC::VC)
1093 .Case("hi", ARMCC::HI)
1094 .Case("ls", ARMCC::LS)
1095 .Case("ge", ARMCC::GE)
1096 .Case("lt", ARMCC::LT)
1097 .Case("gt", ARMCC::GT)
1098 .Case("le", ARMCC::LE)
1099 .Case("al", ARMCC::AL)
1100 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001101 if (CC != ~0U) {
1102 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001103 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001104 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001105
Daniel Dunbar352e1482011-01-11 15:59:50 +00001106 // Next, determine if we have a carry setting bit. We explicitly ignore all
1107 // the instructions we know end in 's'.
1108 if (Mnemonic.endswith("s") &&
1109 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1110 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1111 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1112 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1113 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1114 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1115 CarrySetting = true;
1116 }
1117
1118 return Mnemonic;
1119}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001120
1121/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1122/// inclusion of carry set or predication code operands.
1123//
1124// FIXME: It would be nice to autogen this.
1125static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1126 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001127 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1128 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1129 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1130 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1131 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1132 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1133 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1134 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1135 CanAcceptCarrySet = true;
1136 } else {
1137 CanAcceptCarrySet = false;
1138 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001139
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001140 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1141 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1142 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1143 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1144 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001145 CanAcceptPredicationCode = false;
1146 } else {
1147 CanAcceptPredicationCode = true;
1148 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001149}
1150
1151/// Parse an arm instruction mnemonic followed by its operands.
1152bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1153 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1154 // Create the leading tokens for the mnemonic, split by '.' characters.
1155 size_t Start = 0, Next = Name.find('.');
1156 StringRef Head = Name.slice(Start, Next);
1157
Daniel Dunbar352e1482011-01-11 15:59:50 +00001158 // Split out the predication code and carry setting flag from the mnemonic.
1159 unsigned PredicationCode;
1160 bool CarrySetting;
1161 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001162
Chris Lattner3a697562010-10-28 17:20:03 +00001163 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001164
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001165 // Next, add the CCOut and ConditionCode operands, if needed.
1166 //
1167 // For mnemonics which can ever incorporate a carry setting bit or predication
1168 // code, our matching model involves us always generating CCOut and
1169 // ConditionCode operands to match the mnemonic "as written" and then we let
1170 // the matcher deal with finding the right instruction or generating an
1171 // appropriate error.
1172 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1173 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1174
1175 // Add the carry setting operand, if necessary.
1176 //
1177 // FIXME: It would be awesome if we could somehow invent a location such that
1178 // match errors on this operand would print a nice diagnostic about how the
1179 // 's' character in the mnemonic resulted in a CCOut operand.
1180 if (CanAcceptCarrySet) {
1181 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1182 NameLoc));
1183 } else {
1184 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1185 // misspelled another mnemonic).
1186
1187 // FIXME: Issue a nice error.
1188 }
1189
1190 // Add the predication code operand, if necessary.
1191 if (CanAcceptPredicationCode) {
1192 Operands.push_back(ARMOperand::CreateCondCode(
1193 ARMCC::CondCodes(PredicationCode), NameLoc));
1194 } else {
1195 // This mnemonic can't ever accept a predication code, but the user wrote
1196 // one (or misspelled another mnemonic).
1197
1198 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001199 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001200
1201 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001202 while (Next != StringRef::npos) {
1203 Start = Next;
1204 Next = Name.find('.', Start + 1);
1205 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001206
Chris Lattner3a697562010-10-28 17:20:03 +00001207 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001208 }
1209
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001210 bool isMCR = (Head == "mcr" || Head == "mcr2" ||
1211 Head == "mcrr" || Head == "mcrr2" ||
1212 Head == "mrc" || Head == "mrc2" ||
1213 Head == "mrrc" || Head == "mrrc2");
1214
Daniel Dunbar5747b132010-08-11 06:37:16 +00001215 // Read the remaining operands.
1216 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001217 // Read the first operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001218 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001219 Parser.EatToEndOfStatement();
1220 return true;
1221 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001222
1223 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001224 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001225
1226 // Parse and remember the operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001227 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001228 Parser.EatToEndOfStatement();
1229 return true;
1230 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001231 }
1232 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001233
Chris Lattnercbf8a982010-09-11 16:18:25 +00001234 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1235 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001236 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001237 }
Bill Wendling146018f2010-11-06 21:42:12 +00001238
Chris Lattner34e53142010-09-08 05:10:46 +00001239 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001240 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001241}
1242
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001243bool ARMAsmParser::
1244MatchAndEmitInstruction(SMLoc IDLoc,
1245 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1246 MCStreamer &Out) {
1247 MCInst Inst;
1248 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001249 MatchResultTy MatchResult, MatchResult2;
1250 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1251 if (MatchResult != Match_Success) {
1252 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1253 // that does not update the condition codes. So try adding a CCOut operand
1254 // with a value of reg0.
1255 if (MatchResult == Match_InvalidOperand) {
1256 Operands.insert(Operands.begin() + 1,
1257 ARMOperand::CreateCCOut(0,
1258 ((ARMOperand*)Operands[0])->getStartLoc()));
1259 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1260 if (MatchResult2 == Match_Success)
1261 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001262 else {
1263 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001264 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001265 delete CCOut;
1266 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001267 }
1268 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1269 // that updates the condition codes if it ends in 's'. So see if the
1270 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1271 // operand with a value of CPSR.
1272 else if(MatchResult == Match_MnemonicFail) {
1273 // Get the instruction mnemonic, which is the first token.
1274 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1275 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1276 // removed the 's' from the mnemonic for matching.
1277 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1278 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001279 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1280 Operands.erase(Operands.begin());
1281 delete OldMnemonic;
1282 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001283 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1284 Operands.insert(Operands.begin() + 1,
1285 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1286 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1287 if (MatchResult2 == Match_Success)
1288 MatchResult = Match_Success;
1289 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001290 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1291 Operands.erase(Operands.begin());
1292 delete OldMnemonic;
1293 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001294 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001295 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1296 Operands.erase(Operands.begin() + 1);
1297 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001298 }
1299 }
1300 }
1301 }
1302 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001303 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001304 Out.EmitInstruction(Inst);
1305 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001306 case Match_MissingFeature:
1307 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1308 return true;
1309 case Match_InvalidOperand: {
1310 SMLoc ErrorLoc = IDLoc;
1311 if (ErrorInfo != ~0U) {
1312 if (ErrorInfo >= Operands.size())
1313 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001314
Chris Lattnere73d4f82010-10-28 21:41:58 +00001315 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1316 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1317 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001318
Chris Lattnere73d4f82010-10-28 21:41:58 +00001319 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001320 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001321 case Match_MnemonicFail:
1322 return Error(IDLoc, "unrecognized instruction mnemonic");
1323 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001324
Eric Christopherc223e2b2010-10-29 09:26:59 +00001325 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001326 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001327}
1328
Kevin Enderby515d5092009-10-15 20:48:48 +00001329/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001330bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1331 StringRef IDVal = DirectiveID.getIdentifier();
1332 if (IDVal == ".word")
1333 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001334 else if (IDVal == ".thumb")
1335 return ParseDirectiveThumb(DirectiveID.getLoc());
1336 else if (IDVal == ".thumb_func")
1337 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1338 else if (IDVal == ".code")
1339 return ParseDirectiveCode(DirectiveID.getLoc());
1340 else if (IDVal == ".syntax")
1341 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001342 return true;
1343}
1344
1345/// ParseDirectiveWord
1346/// ::= .word [ expression (, expression)* ]
1347bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1348 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1349 for (;;) {
1350 const MCExpr *Value;
1351 if (getParser().ParseExpression(Value))
1352 return true;
1353
Chris Lattneraaec2052010-01-19 19:46:13 +00001354 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001355
1356 if (getLexer().is(AsmToken::EndOfStatement))
1357 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001358
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001359 // FIXME: Improve diagnostic.
1360 if (getLexer().isNot(AsmToken::Comma))
1361 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001362 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001363 }
1364 }
1365
Sean Callananb9a25b72010-01-19 20:27:46 +00001366 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001367 return false;
1368}
1369
Kevin Enderby515d5092009-10-15 20:48:48 +00001370/// ParseDirectiveThumb
1371/// ::= .thumb
1372bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1373 if (getLexer().isNot(AsmToken::EndOfStatement))
1374 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001375 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001376
1377 // TODO: set thumb mode
1378 // TODO: tell the MC streamer the mode
1379 // getParser().getStreamer().Emit???();
1380 return false;
1381}
1382
1383/// ParseDirectiveThumbFunc
1384/// ::= .thumbfunc symbol_name
1385bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001386 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001387 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001388 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001389 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001390 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001391 if (getLexer().isNot(AsmToken::EndOfStatement))
1392 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001393 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001394
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001395 // Mark symbol as a thumb symbol.
1396 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1397 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001398 return false;
1399}
1400
1401/// ParseDirectiveSyntax
1402/// ::= .syntax unified | divided
1403bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001404 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001405 if (Tok.isNot(AsmToken::Identifier))
1406 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001407 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001408 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001409 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001410 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001411 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001412 else
1413 return Error(L, "unrecognized syntax mode in .syntax directive");
1414
1415 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001416 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001417 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001418
1419 // TODO tell the MC streamer the mode
1420 // getParser().getStreamer().Emit???();
1421 return false;
1422}
1423
1424/// ParseDirectiveCode
1425/// ::= .code 16 | 32
1426bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001427 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001428 if (Tok.isNot(AsmToken::Integer))
1429 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001430 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001431 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001432 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001433 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001434 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001435 else
1436 return Error(L, "invalid operand to .code directive");
1437
1438 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001439 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001440 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001441
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001442 // FIXME: We need to be able switch subtargets at this point so that
1443 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1444 // includes Feature_IsThumb or not to match the right instructions. This is
1445 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1446 if (Val == 16){
1447 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1448 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001449 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001450 }
1451 else{
1452 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1453 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001454 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001455 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001456
Kevin Enderby515d5092009-10-15 20:48:48 +00001457 return false;
1458}
1459
Sean Callanan90b70972010-04-07 20:29:34 +00001460extern "C" void LLVMInitializeARMAsmLexer();
1461
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001462/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001463extern "C" void LLVMInitializeARMAsmParser() {
1464 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1465 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001466 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001467}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001468
Chris Lattner0692ee62010-09-06 19:11:01 +00001469#define GET_REGISTER_MATCHER
1470#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001471#include "ARMGenAsmMatcher.inc"