blob: e010d186f3ac94ad199e08d8034e3d22adc7dbd9 [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;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000148 union {
149 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
150 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
151 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000152 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
153 enum ShiftType ShiftType; // used when OffsetRegShifted is true
154 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000155 unsigned Preindexed : 1;
156 unsigned Postindexed : 1;
157 unsigned OffsetIsReg : 1;
158 unsigned Negative : 1; // only used when OffsetIsReg is true
159 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000160 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000161 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000162
Bill Wendling146018f2010-11-06 21:42:12 +0000163 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
164public:
Sean Callanan76264762010-04-02 22:27:05 +0000165 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
166 Kind = o.Kind;
167 StartLoc = o.StartLoc;
168 EndLoc = o.EndLoc;
169 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000170 case CondCode:
171 CC = o.CC;
172 break;
Sean Callanan76264762010-04-02 22:27:05 +0000173 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000174 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000175 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000176 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000177 case Register:
178 Reg = o.Reg;
179 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000180 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000181 case DPRRegisterList:
182 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000183 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000184 break;
Sean Callanan76264762010-04-02 22:27:05 +0000185 case Immediate:
186 Imm = o.Imm;
187 break;
188 case Memory:
189 Mem = o.Mem;
190 break;
191 }
192 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000193
Sean Callanan76264762010-04-02 22:27:05 +0000194 /// getStartLoc - Get the location of the first token of this operand.
195 SMLoc getStartLoc() const { return StartLoc; }
196 /// getEndLoc - Get the location of the last token of this operand.
197 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000198
Daniel Dunbar8462b302010-08-11 06:36:53 +0000199 ARMCC::CondCodes getCondCode() const {
200 assert(Kind == CondCode && "Invalid access!");
201 return CC.Val;
202 }
203
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000204 StringRef getToken() const {
205 assert(Kind == Token && "Invalid access!");
206 return StringRef(Tok.Data, Tok.Length);
207 }
208
209 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000210 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000211 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000212 }
213
Bill Wendling5fa22a12010-11-09 23:28:44 +0000214 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000215 assert((Kind == RegisterList || Kind == DPRRegisterList ||
216 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000217 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000218 }
219
Kevin Enderbycfe07242009-10-13 22:19:02 +0000220 const MCExpr *getImm() const {
221 assert(Kind == Immediate && "Invalid access!");
222 return Imm.Val;
223 }
224
Daniel Dunbar8462b302010-08-11 06:36:53 +0000225 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000226 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000227 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000228 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000229 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000230 bool isDPRRegList() const { return Kind == DPRRegisterList; }
231 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000232 bool isToken() const { return Kind == Token; }
233 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000234 bool isMemMode5() const {
235 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
236 Mem.Writeback || Mem.Negative)
237 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000238
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000239 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000240 if (!CE) return false;
241
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000242 // The offset must be a multiple of 4 in the range 0-1020.
243 int64_t Value = CE->getValue();
244 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
245 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000246 bool isMemModeRegThumb() const {
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000247 if (!isMemory() || !Mem.OffsetIsReg || Mem.Writeback)
Bill Wendlingf4caf692010-12-14 03:36:38 +0000248 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000249 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000250 }
251 bool isMemModeImmThumb() const {
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000252 if (!isMemory() || Mem.OffsetIsReg || Mem.Writeback)
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000253 return false;
254
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000255 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000256 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?
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000319 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000320 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000321
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000322 // The MCInst offset operand doesn't include the low two bits (like
323 // the instruction encoding).
324 int64_t Offset = CE->getValue() / 4;
325 if (Offset >= 0)
326 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
327 Offset)));
328 else
329 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
330 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000331 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000332
Bill Wendlingf4caf692010-12-14 03:36:38 +0000333 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
334 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000335 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000336 Inst.addOperand(MCOperand::CreateReg(Mem.Offset.RegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000337 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000338
Bill Wendlingf4caf692010-12-14 03:36:38 +0000339 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
340 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
341 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000342 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000343 assert(CE && "Non-constant mode offset operand!");
344 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000345 }
346
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000347 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000348
Chris Lattner3a697562010-10-28 17:20:03 +0000349 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
350 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000351 Op->CC.Val = CC;
352 Op->StartLoc = S;
353 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000354 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000355 }
356
Jim Grosbachd67641b2010-12-06 18:21:12 +0000357 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
358 ARMOperand *Op = new ARMOperand(CCOut);
359 Op->Reg.RegNum = RegNum;
360 Op->StartLoc = S;
361 Op->EndLoc = S;
362 return Op;
363 }
364
Chris Lattner3a697562010-10-28 17:20:03 +0000365 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
366 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000367 Op->Tok.Data = Str.data();
368 Op->Tok.Length = Str.size();
369 Op->StartLoc = S;
370 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000371 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000372 }
373
Bill Wendling50d0f582010-11-18 23:43:05 +0000374 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000375 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000376 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000377 Op->StartLoc = S;
378 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000379 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000380 }
381
Bill Wendling7729e062010-11-09 22:44:22 +0000382 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000383 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000384 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000385 KindTy Kind = RegisterList;
386
387 if (ARM::DPRRegClass.contains(Regs.front().first))
388 Kind = DPRRegisterList;
389 else if (ARM::SPRRegClass.contains(Regs.front().first))
390 Kind = SPRRegisterList;
391
392 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000393 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000394 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000395 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000396 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000397 Op->StartLoc = StartLoc;
398 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000399 return Op;
400 }
401
Chris Lattner3a697562010-10-28 17:20:03 +0000402 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
403 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000404 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000405 Op->StartLoc = S;
406 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000407 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000408 }
409
Chris Lattner3a697562010-10-28 17:20:03 +0000410 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000411 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000412 bool OffsetRegShifted, enum ShiftType ShiftType,
413 const MCExpr *ShiftAmount, bool Preindexed,
414 bool Postindexed, bool Negative, bool Writeback,
415 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000416 assert((OffsetRegNum == -1 || OffsetIsReg) &&
417 "OffsetRegNum must imply OffsetIsReg!");
418 assert((!OffsetRegShifted || OffsetIsReg) &&
419 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000420 assert((Offset || OffsetIsReg) &&
421 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000422 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
423 "Cannot have shift amount without shifted register offset!");
424 assert((!Offset || !OffsetIsReg) &&
425 "Cannot have expression offset and register offset!");
426
Chris Lattner3a697562010-10-28 17:20:03 +0000427 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000428 Op->Mem.BaseRegNum = BaseRegNum;
429 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000430 if (OffsetIsReg)
431 Op->Mem.Offset.RegNum = OffsetRegNum;
432 else
433 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000434 Op->Mem.OffsetRegShifted = OffsetRegShifted;
435 Op->Mem.ShiftType = ShiftType;
436 Op->Mem.ShiftAmount = ShiftAmount;
437 Op->Mem.Preindexed = Preindexed;
438 Op->Mem.Postindexed = Postindexed;
439 Op->Mem.Negative = Negative;
440 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000441
Sean Callanan76264762010-04-02 22:27:05 +0000442 Op->StartLoc = S;
443 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000444 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000445 }
446};
447
448} // end anonymous namespace.
449
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000450void ARMOperand::dump(raw_ostream &OS) const {
451 switch (Kind) {
452 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000453 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000454 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000455 case CCOut:
456 OS << "<ccout " << getReg() << ">";
457 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000458 case Immediate:
459 getImm()->print(OS);
460 break;
461 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000462 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000463 break;
464 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000465 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000466 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000467 case RegisterList:
468 case DPRRegisterList:
469 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000470 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000471
Bill Wendling5fa22a12010-11-09 23:28:44 +0000472 const SmallVectorImpl<unsigned> &RegList = getRegList();
473 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000474 I = RegList.begin(), E = RegList.end(); I != E; ) {
475 OS << *I;
476 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000477 }
478
479 OS << ">";
480 break;
481 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000482 case Token:
483 OS << "'" << getToken() << "'";
484 break;
485 }
486}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000487
488/// @name Auto-generated Match Functions
489/// {
490
491static unsigned MatchRegisterName(StringRef Name);
492
493/// }
494
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000495/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000496/// and if it is a register name the token is eaten and the register number is
497/// returned. Otherwise return -1.
498///
499int ARMAsmParser::TryParseRegister() {
500 const AsmToken &Tok = Parser.getTok();
501 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000502
Chris Lattnere5658fa2010-10-30 04:09:10 +0000503 // FIXME: Validate register for the current architecture; we have to do
504 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000505 std::string upperCase = Tok.getString().str();
506 std::string lowerCase = LowercaseString(upperCase);
507 unsigned RegNum = MatchRegisterName(lowerCase);
508 if (!RegNum) {
509 RegNum = StringSwitch<unsigned>(lowerCase)
510 .Case("r13", ARM::SP)
511 .Case("r14", ARM::LR)
512 .Case("r15", ARM::PC)
513 .Case("ip", ARM::R12)
514 .Default(0);
515 }
516 if (!RegNum) return -1;
517
Chris Lattnere5658fa2010-10-30 04:09:10 +0000518 Parser.Lex(); // Eat identifier token.
519 return RegNum;
520}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000521
522
Bill Wendling50d0f582010-11-18 23:43:05 +0000523/// Try to parse a register name. The token must be an Identifier when called.
524/// If it's a register, an AsmOperand is created. Another AsmOperand is created
525/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000526///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000527/// TODO this is likely to change to allow different register types and or to
528/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000529bool ARMAsmParser::
530TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000531 SMLoc S = Parser.getTok().getLoc();
532 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000533 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000534 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000535
Bill Wendling50d0f582010-11-18 23:43:05 +0000536 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000537
Chris Lattnere5658fa2010-10-30 04:09:10 +0000538 const AsmToken &ExclaimTok = Parser.getTok();
539 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000540 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
541 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000542 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000543 }
544
Bill Wendling50d0f582010-11-18 23:43:05 +0000545 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000546}
547
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000548static int MatchMCRName(StringRef Name) {
549 // Use the same layout as the tablegen'erated register name matcher. Ugly,
550 // but efficient.
551 switch (Name.size()) {
552 default: break;
553 case 2:
554 if (Name[0] != 'p' && Name[0] != 'c')
555 return -1;
556 switch (Name[1]) {
557 default: return -1;
558 case '0': return 0;
559 case '1': return 1;
560 case '2': return 2;
561 case '3': return 3;
562 case '4': return 4;
563 case '5': return 5;
564 case '6': return 6;
565 case '7': return 7;
566 case '8': return 8;
567 case '9': return 9;
568 }
569 break;
570 case 3:
571 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
572 return -1;
573 switch (Name[2]) {
574 default: return -1;
575 case '0': return 10;
576 case '1': return 11;
577 case '2': return 12;
578 case '3': return 13;
579 case '4': return 14;
580 case '5': return 15;
581 }
582 break;
583 }
584
585 llvm_unreachable("Unhandled coprocessor operand string!");
586 return -1;
587}
588
589/// TryParseMCRName - Try to parse an MCR/MRC symbolic operand
590/// name. The token must be an Identifier when called, and if it is a MCR
591/// operand name, the token is eaten and the operand is added to the
592/// operand list.
593bool ARMAsmParser::
594TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
595 SMLoc S = Parser.getTok().getLoc();
596 const AsmToken &Tok = Parser.getTok();
597 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
598
599 int Num = MatchMCRName(Tok.getString());
600 if (Num == -1)
601 return true;
602
603 Parser.Lex(); // Eat identifier token.
604 Operands.push_back(ARMOperand::CreateImm(
605 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
606 return false;
607}
608
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000609/// Parse a register list, return it if successful else return null. The first
610/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000611bool ARMAsmParser::
612ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000613 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000614 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000615 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000616
Bill Wendling7729e062010-11-09 22:44:22 +0000617 // Read the rest of the registers in the list.
618 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000619 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000620
Bill Wendling7729e062010-11-09 22:44:22 +0000621 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000622 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000623 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000624
Sean Callanan18b83232010-01-19 21:44:56 +0000625 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000626 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000627 if (RegTok.isNot(AsmToken::Identifier)) {
628 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000629 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000630 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000631
Bill Wendling1d6a2652010-11-06 10:40:24 +0000632 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000633 if (RegNum == -1) {
634 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000635 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000636 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000637
Bill Wendlinge7176102010-11-06 22:36:58 +0000638 if (IsRange) {
639 int Reg = PrevRegNum;
640 do {
641 ++Reg;
642 Registers.push_back(std::make_pair(Reg, RegLoc));
643 } while (Reg != RegNum);
644 } else {
645 Registers.push_back(std::make_pair(RegNum, RegLoc));
646 }
647
648 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000649 } while (Parser.getTok().is(AsmToken::Comma) ||
650 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000651
652 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000653 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000654 if (RCurlyTok.isNot(AsmToken::RCurly)) {
655 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000656 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000657 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000658
Bill Wendlinge7176102010-11-06 22:36:58 +0000659 SMLoc E = RCurlyTok.getLoc();
660 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000661
Bill Wendlinge7176102010-11-06 22:36:58 +0000662 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000663 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000664 RI = Registers.begin(), RE = Registers.end();
665
Bill Wendling7caebff2011-01-12 21:20:59 +0000666 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000667 bool EmittedWarning = false;
668
Bill Wendling7caebff2011-01-12 21:20:59 +0000669 DenseMap<unsigned, bool> RegMap;
670 RegMap[HighRegNum] = true;
671
Bill Wendlinge7176102010-11-06 22:36:58 +0000672 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000673 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000674 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000675
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000676 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000677 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000678 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000679 }
680
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000681 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000682 Warning(RegInfo.second,
683 "register not in ascending order in register list");
684
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000685 RegMap[Reg] = true;
686 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000687 }
688
Bill Wendling50d0f582010-11-18 23:43:05 +0000689 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
690 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000691}
692
Bill Wendlinge7176102010-11-06 22:36:58 +0000693/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000694/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000695///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000696/// TODO Only preindexing and postindexing addressing are started, unindexed
697/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000698bool ARMAsmParser::
699ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000700 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000701 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000702 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000703 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000704 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000705
Sean Callanan18b83232010-01-19 21:44:56 +0000706 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000707 if (BaseRegTok.isNot(AsmToken::Identifier)) {
708 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000709 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000710 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000711 int BaseRegNum = TryParseRegister();
712 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000713 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000714 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000715 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000716
Daniel Dunbar05710932011-01-18 05:34:17 +0000717 // The next token must either be a comma or a closing bracket.
718 const AsmToken &Tok = Parser.getTok();
719 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
720 return true;
721
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000722 bool Preindexed = false;
723 bool Postindexed = false;
724 bool OffsetIsReg = false;
725 bool Negative = false;
726 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000727 ARMOperand *WBOp = 0;
728 int OffsetRegNum = -1;
729 bool OffsetRegShifted = false;
730 enum ShiftType ShiftType = Lsl;
731 const MCExpr *ShiftAmount = 0;
732 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000733
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000734 // First look for preindexed address forms, that is after the "[Rn" we now
735 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000736 if (Tok.is(AsmToken::Comma)) {
737 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000738 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000739
Chris Lattner550276e2010-10-28 20:52:15 +0000740 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
741 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000742 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000743 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000744 if (RBracTok.isNot(AsmToken::RBrac)) {
745 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000746 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000747 }
Sean Callanan76264762010-04-02 22:27:05 +0000748 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000749 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000750
Sean Callanan18b83232010-01-19 21:44:56 +0000751 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000752 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000753 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
754 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000755 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000756 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000757 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000758 } else {
759 // The "[Rn" we have so far was not followed by a comma.
760
Jim Grosbach80eb2332010-10-29 17:41:25 +0000761 // If there's anything other than the right brace, this is a post indexing
762 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000763 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000764 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000765
Sean Callanan18b83232010-01-19 21:44:56 +0000766 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000767
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000768 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000769 Postindexed = true;
770 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000771
Chris Lattner550276e2010-10-28 20:52:15 +0000772 if (NextTok.isNot(AsmToken::Comma)) {
773 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000774 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000775 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000776
Sean Callananb9a25b72010-01-19 20:27:46 +0000777 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000778
Chris Lattner550276e2010-10-28 20:52:15 +0000779 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000780 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000781 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000782 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000783 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000784 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000785
786 // Force Offset to exist if used.
787 if (!OffsetIsReg) {
788 if (!Offset)
789 Offset = MCConstantExpr::Create(0, getContext());
790 }
791
792 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
793 OffsetRegNum, OffsetRegShifted,
794 ShiftType, ShiftAmount, Preindexed,
795 Postindexed, Negative, Writeback,
796 S, E));
797 if (WBOp)
798 Operands.push_back(WBOp);
799
800 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000801}
802
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000803/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
804/// we will parse the following (were +/- means that a plus or minus is
805/// optional):
806/// +/-Rm
807/// +/-Rm, shift
808/// #offset
809/// we return false on success or an error otherwise.
810bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000811 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000812 enum ShiftType &ShiftType,
813 const MCExpr *&ShiftAmount,
814 const MCExpr *&Offset,
815 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000816 int &OffsetRegNum,
817 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000818 Negative = false;
819 OffsetRegShifted = false;
820 OffsetIsReg = false;
821 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000822 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000823 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000824 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000825 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000826 else if (NextTok.is(AsmToken::Minus)) {
827 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000828 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000829 }
830 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000831 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000832 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000833 SMLoc CurLoc = OffsetRegTok.getLoc();
834 OffsetRegNum = TryParseRegister();
835 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000836 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000837 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000838 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000839 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000840
Bill Wendling12f40e92010-11-06 10:51:53 +0000841 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000842 if (OffsetRegNum != -1) {
843 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000844 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000845 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000846 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000847
Sean Callanan18b83232010-01-19 21:44:56 +0000848 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000849 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000850 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000851 OffsetRegShifted = true;
852 }
853 }
854 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
855 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000856 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000857 if (HashTok.isNot(AsmToken::Hash))
858 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000859
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000861
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000862 if (getParser().ParseExpression(Offset))
863 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000864 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000865 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000866 return false;
867}
868
869/// ParseShift as one of these two:
870/// ( lsl | lsr | asr | ror ) , # shift_amount
871/// rrx
872/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000873bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000874 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000875 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000876 if (Tok.isNot(AsmToken::Identifier))
877 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000878 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000879 if (ShiftName == "lsl" || ShiftName == "LSL")
880 St = Lsl;
881 else if (ShiftName == "lsr" || ShiftName == "LSR")
882 St = Lsr;
883 else if (ShiftName == "asr" || ShiftName == "ASR")
884 St = Asr;
885 else if (ShiftName == "ror" || ShiftName == "ROR")
886 St = Ror;
887 else if (ShiftName == "rrx" || ShiftName == "RRX")
888 St = Rrx;
889 else
890 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000891 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000892
893 // Rrx stands alone.
894 if (St == Rrx)
895 return false;
896
897 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000898 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000899 if (HashTok.isNot(AsmToken::Hash))
900 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000901 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000902
903 if (getParser().ParseExpression(ShiftAmount))
904 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000905
906 return false;
907}
908
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000909/// Parse a arm instruction operand. For now this parses the operand regardless
910/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000911bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
912 bool isMCR){
Sean Callanan76264762010-04-02 22:27:05 +0000913 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000914 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000915 default:
916 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000917 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000918 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000919 if (!TryParseRegisterWithWriteBack(Operands))
920 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000921 if (isMCR && !TryParseMCRName(Operands))
922 return false;
923
924 // Fall though for the Identifier case that is not a register or a
925 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000926 case AsmToken::Integer: // things like 1f and 2b as a branch targets
927 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000928 // This was not a register so parse other operands that start with an
929 // identifier (like labels) as expressions and create them as immediates.
930 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000931 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000932 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000933 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000934 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000935 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
936 return false;
937 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000938 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000939 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000940 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000941 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000942 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000943 // #42 -> immediate.
944 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000945 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000946 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000947 const MCExpr *ImmVal;
948 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000949 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000950 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000951 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
952 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000953 case AsmToken::Colon: {
954 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +0000955 // FIXME: Check it's an expression prefix,
956 // e.g. (FOO - :lower16:BAR) isn't legal.
957 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000958 if (ParsePrefix(RefKind))
959 return true;
960
Evan Cheng75972122011-01-13 07:58:56 +0000961 const MCExpr *SubExprVal;
962 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +0000963 return true;
964
Evan Cheng75972122011-01-13 07:58:56 +0000965 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
966 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +0000967 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +0000968 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +0000969 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000970 }
Jason W Kim9081b4b2011-01-11 23:53:41 +0000971 }
972}
973
Evan Cheng75972122011-01-13 07:58:56 +0000974// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
975// :lower16: and :upper16:.
976bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
977 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000978
979 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +0000980 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +0000981 Parser.Lex(); // Eat ':'
982
983 if (getLexer().isNot(AsmToken::Identifier)) {
984 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
985 return true;
986 }
987
988 StringRef IDVal = Parser.getTok().getIdentifier();
989 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +0000990 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000991 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +0000992 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000993 } else {
994 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
995 return true;
996 }
997 Parser.Lex();
998
999 if (getLexer().isNot(AsmToken::Colon)) {
1000 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1001 return true;
1002 }
1003 Parser.Lex(); // Eat the last ':'
1004 return false;
1005}
1006
1007const MCExpr *
1008ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1009 MCSymbolRefExpr::VariantKind Variant) {
1010 // Recurse over the given expression, rebuilding it to apply the given variant
1011 // to the leftmost symbol.
1012 if (Variant == MCSymbolRefExpr::VK_None)
1013 return E;
1014
1015 switch (E->getKind()) {
1016 case MCExpr::Target:
1017 llvm_unreachable("Can't handle target expr yet");
1018 case MCExpr::Constant:
1019 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1020
1021 case MCExpr::SymbolRef: {
1022 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1023
1024 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1025 return 0;
1026
1027 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1028 }
1029
1030 case MCExpr::Unary:
1031 llvm_unreachable("Can't handle unary expressions yet");
1032
1033 case MCExpr::Binary: {
1034 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1035 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1036 const MCExpr *RHS = BE->getRHS();
1037 if (!LHS)
1038 return 0;
1039
1040 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1041 }
1042 }
1043
1044 assert(0 && "Invalid expression kind!");
1045 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001046}
1047
Daniel Dunbar352e1482011-01-11 15:59:50 +00001048/// \brief Given a mnemonic, split out possible predication code and carry
1049/// setting letters to form a canonical mnemonic and flags.
1050//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001051// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001052static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1053 unsigned &PredicationCode,
1054 bool &CarrySetting) {
1055 PredicationCode = ARMCC::AL;
1056 CarrySetting = false;
1057
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001058 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001059 //
1060 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001061 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1062 Mnemonic == "movs" ||
1063 Mnemonic == "svc" ||
1064 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1065 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1066 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1067 Mnemonic == "vclt" ||
1068 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1069 Mnemonic == "vcle" ||
1070 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1071 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1072 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001073 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001074
Daniel Dunbar352e1482011-01-11 15:59:50 +00001075 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001076 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001077 .Case("eq", ARMCC::EQ)
1078 .Case("ne", ARMCC::NE)
1079 .Case("hs", ARMCC::HS)
1080 .Case("lo", ARMCC::LO)
1081 .Case("mi", ARMCC::MI)
1082 .Case("pl", ARMCC::PL)
1083 .Case("vs", ARMCC::VS)
1084 .Case("vc", ARMCC::VC)
1085 .Case("hi", ARMCC::HI)
1086 .Case("ls", ARMCC::LS)
1087 .Case("ge", ARMCC::GE)
1088 .Case("lt", ARMCC::LT)
1089 .Case("gt", ARMCC::GT)
1090 .Case("le", ARMCC::LE)
1091 .Case("al", ARMCC::AL)
1092 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001093 if (CC != ~0U) {
1094 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001095 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001096 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001097
Daniel Dunbar352e1482011-01-11 15:59:50 +00001098 // Next, determine if we have a carry setting bit. We explicitly ignore all
1099 // the instructions we know end in 's'.
1100 if (Mnemonic.endswith("s") &&
1101 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1102 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1103 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1104 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1105 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1106 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1107 CarrySetting = true;
1108 }
1109
1110 return Mnemonic;
1111}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001112
1113/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1114/// inclusion of carry set or predication code operands.
1115//
1116// FIXME: It would be nice to autogen this.
1117static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1118 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001119 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1120 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1121 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1122 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1123 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1124 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1125 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1126 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1127 CanAcceptCarrySet = true;
1128 } else {
1129 CanAcceptCarrySet = false;
1130 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001131
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001132 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1133 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1134 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1135 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1136 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001137 CanAcceptPredicationCode = false;
1138 } else {
1139 CanAcceptPredicationCode = true;
1140 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001141}
1142
1143/// Parse an arm instruction mnemonic followed by its operands.
1144bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1145 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1146 // Create the leading tokens for the mnemonic, split by '.' characters.
1147 size_t Start = 0, Next = Name.find('.');
1148 StringRef Head = Name.slice(Start, Next);
1149
Daniel Dunbar352e1482011-01-11 15:59:50 +00001150 // Split out the predication code and carry setting flag from the mnemonic.
1151 unsigned PredicationCode;
1152 bool CarrySetting;
1153 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001154
Chris Lattner3a697562010-10-28 17:20:03 +00001155 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001156
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001157 // Next, add the CCOut and ConditionCode operands, if needed.
1158 //
1159 // For mnemonics which can ever incorporate a carry setting bit or predication
1160 // code, our matching model involves us always generating CCOut and
1161 // ConditionCode operands to match the mnemonic "as written" and then we let
1162 // the matcher deal with finding the right instruction or generating an
1163 // appropriate error.
1164 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1165 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1166
1167 // Add the carry setting operand, if necessary.
1168 //
1169 // FIXME: It would be awesome if we could somehow invent a location such that
1170 // match errors on this operand would print a nice diagnostic about how the
1171 // 's' character in the mnemonic resulted in a CCOut operand.
1172 if (CanAcceptCarrySet) {
1173 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1174 NameLoc));
1175 } else {
1176 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1177 // misspelled another mnemonic).
1178
1179 // FIXME: Issue a nice error.
1180 }
1181
1182 // Add the predication code operand, if necessary.
1183 if (CanAcceptPredicationCode) {
1184 Operands.push_back(ARMOperand::CreateCondCode(
1185 ARMCC::CondCodes(PredicationCode), NameLoc));
1186 } else {
1187 // This mnemonic can't ever accept a predication code, but the user wrote
1188 // one (or misspelled another mnemonic).
1189
1190 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001191 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001192
1193 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001194 while (Next != StringRef::npos) {
1195 Start = Next;
1196 Next = Name.find('.', Start + 1);
1197 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001198
Chris Lattner3a697562010-10-28 17:20:03 +00001199 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001200 }
1201
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001202 bool isMCR = (Head == "mcr" || Head == "mcr2" ||
1203 Head == "mcrr" || Head == "mcrr2" ||
1204 Head == "mrc" || Head == "mrc2" ||
1205 Head == "mrrc" || Head == "mrrc2");
1206
Daniel Dunbar5747b132010-08-11 06:37:16 +00001207 // Read the remaining operands.
1208 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001209 // Read the first operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001210 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001211 Parser.EatToEndOfStatement();
1212 return true;
1213 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001214
1215 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001216 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001217
1218 // Parse and remember the operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001219 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001220 Parser.EatToEndOfStatement();
1221 return true;
1222 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001223 }
1224 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001225
Chris Lattnercbf8a982010-09-11 16:18:25 +00001226 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1227 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001228 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001229 }
Bill Wendling146018f2010-11-06 21:42:12 +00001230
Chris Lattner34e53142010-09-08 05:10:46 +00001231 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001232 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001233}
1234
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001235bool ARMAsmParser::
1236MatchAndEmitInstruction(SMLoc IDLoc,
1237 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1238 MCStreamer &Out) {
1239 MCInst Inst;
1240 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001241 MatchResultTy MatchResult, MatchResult2;
1242 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1243 if (MatchResult != Match_Success) {
1244 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1245 // that does not update the condition codes. So try adding a CCOut operand
1246 // with a value of reg0.
1247 if (MatchResult == Match_InvalidOperand) {
1248 Operands.insert(Operands.begin() + 1,
1249 ARMOperand::CreateCCOut(0,
1250 ((ARMOperand*)Operands[0])->getStartLoc()));
1251 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1252 if (MatchResult2 == Match_Success)
1253 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001254 else {
1255 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001256 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001257 delete CCOut;
1258 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001259 }
1260 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1261 // that updates the condition codes if it ends in 's'. So see if the
1262 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1263 // operand with a value of CPSR.
1264 else if(MatchResult == Match_MnemonicFail) {
1265 // Get the instruction mnemonic, which is the first token.
1266 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1267 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1268 // removed the 's' from the mnemonic for matching.
1269 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1270 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001271 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1272 Operands.erase(Operands.begin());
1273 delete OldMnemonic;
1274 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001275 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1276 Operands.insert(Operands.begin() + 1,
1277 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1278 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1279 if (MatchResult2 == Match_Success)
1280 MatchResult = Match_Success;
1281 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001282 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1283 Operands.erase(Operands.begin());
1284 delete OldMnemonic;
1285 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001286 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001287 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1288 Operands.erase(Operands.begin() + 1);
1289 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001290 }
1291 }
1292 }
1293 }
1294 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001295 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001296 Out.EmitInstruction(Inst);
1297 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001298 case Match_MissingFeature:
1299 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1300 return true;
1301 case Match_InvalidOperand: {
1302 SMLoc ErrorLoc = IDLoc;
1303 if (ErrorInfo != ~0U) {
1304 if (ErrorInfo >= Operands.size())
1305 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001306
Chris Lattnere73d4f82010-10-28 21:41:58 +00001307 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1308 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1309 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001310
Chris Lattnere73d4f82010-10-28 21:41:58 +00001311 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001312 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001313 case Match_MnemonicFail:
1314 return Error(IDLoc, "unrecognized instruction mnemonic");
1315 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001316
Eric Christopherc223e2b2010-10-29 09:26:59 +00001317 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001318 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001319}
1320
Kevin Enderby515d5092009-10-15 20:48:48 +00001321/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001322bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1323 StringRef IDVal = DirectiveID.getIdentifier();
1324 if (IDVal == ".word")
1325 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001326 else if (IDVal == ".thumb")
1327 return ParseDirectiveThumb(DirectiveID.getLoc());
1328 else if (IDVal == ".thumb_func")
1329 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1330 else if (IDVal == ".code")
1331 return ParseDirectiveCode(DirectiveID.getLoc());
1332 else if (IDVal == ".syntax")
1333 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001334 return true;
1335}
1336
1337/// ParseDirectiveWord
1338/// ::= .word [ expression (, expression)* ]
1339bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1340 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1341 for (;;) {
1342 const MCExpr *Value;
1343 if (getParser().ParseExpression(Value))
1344 return true;
1345
Chris Lattneraaec2052010-01-19 19:46:13 +00001346 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001347
1348 if (getLexer().is(AsmToken::EndOfStatement))
1349 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001350
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001351 // FIXME: Improve diagnostic.
1352 if (getLexer().isNot(AsmToken::Comma))
1353 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001354 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001355 }
1356 }
1357
Sean Callananb9a25b72010-01-19 20:27:46 +00001358 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001359 return false;
1360}
1361
Kevin Enderby515d5092009-10-15 20:48:48 +00001362/// ParseDirectiveThumb
1363/// ::= .thumb
1364bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1365 if (getLexer().isNot(AsmToken::EndOfStatement))
1366 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001367 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001368
1369 // TODO: set thumb mode
1370 // TODO: tell the MC streamer the mode
1371 // getParser().getStreamer().Emit???();
1372 return false;
1373}
1374
1375/// ParseDirectiveThumbFunc
1376/// ::= .thumbfunc symbol_name
1377bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001378 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001379 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001380 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001381 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001382 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001383 if (getLexer().isNot(AsmToken::EndOfStatement))
1384 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001385 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001386
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001387 // Mark symbol as a thumb symbol.
1388 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1389 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001390 return false;
1391}
1392
1393/// ParseDirectiveSyntax
1394/// ::= .syntax unified | divided
1395bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001396 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001397 if (Tok.isNot(AsmToken::Identifier))
1398 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001399 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001400 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001401 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001402 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001403 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001404 else
1405 return Error(L, "unrecognized syntax mode in .syntax directive");
1406
1407 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001408 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001409 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001410
1411 // TODO tell the MC streamer the mode
1412 // getParser().getStreamer().Emit???();
1413 return false;
1414}
1415
1416/// ParseDirectiveCode
1417/// ::= .code 16 | 32
1418bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001419 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001420 if (Tok.isNot(AsmToken::Integer))
1421 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001422 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001423 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001424 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001425 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001426 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001427 else
1428 return Error(L, "invalid operand to .code directive");
1429
1430 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001431 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001432 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001433
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001434 // FIXME: We need to be able switch subtargets at this point so that
1435 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1436 // includes Feature_IsThumb or not to match the right instructions. This is
1437 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1438 if (Val == 16){
1439 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1440 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001441 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001442 }
1443 else{
1444 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1445 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001446 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001447 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001448
Kevin Enderby515d5092009-10-15 20:48:48 +00001449 return false;
1450}
1451
Sean Callanan90b70972010-04-07 20:29:34 +00001452extern "C" void LLVMInitializeARMAsmLexer();
1453
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001454/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001455extern "C" void LLVMInitializeARMAsmParser() {
1456 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1457 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001458 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001459}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001460
Chris Lattner0692ee62010-09-06 19:11:01 +00001461#define GET_REGISTER_MATCHER
1462#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001463#include "ARMGenAsmMatcher.inc"