blob: 1f5f64a3a602ffa5a749af9a00b2d5bb95244993 [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 Dunbar6ec56202011-01-18 05:55:21 +0000225 /// @name Memory Operand Accessors
226 /// @{
227
228 unsigned getMemBaseRegNum() const {
229 return Mem.BaseRegNum;
230 }
231 unsigned getMemOffsetRegNum() const {
232 assert(Mem.OffsetIsReg && "Invalid access!");
233 return Mem.Offset.RegNum;
234 }
235 const MCExpr *getMemOffset() const {
236 assert(!Mem.OffsetIsReg && "Invalid access!");
237 return Mem.Offset.Value;
238 }
239 unsigned getMemOffsetRegShifted() const {
240 assert(Mem.OffsetIsReg && "Invalid access!");
241 return Mem.OffsetRegShifted;
242 }
243 const MCExpr *getMemShiftAmount() const {
244 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
245 return Mem.ShiftAmount;
246 }
247 enum ShiftType getMemShiftType() const {
248 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
249 return Mem.ShiftType;
250 }
251 bool getMemPreindexed() const { return Mem.Preindexed; }
252 bool getMemPostindexed() const { return Mem.Postindexed; }
253 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
254 bool getMemNegative() const { return Mem.Negative; }
255 bool getMemWriteback() const { return Mem.Writeback; }
256
257 /// @}
258
Daniel Dunbar8462b302010-08-11 06:36:53 +0000259 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000260 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000261 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000262 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000263 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000264 bool isDPRRegList() const { return Kind == DPRRegisterList; }
265 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000266 bool isToken() const { return Kind == Token; }
267 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000268 bool isMemMode5() const {
269 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
270 Mem.Writeback || Mem.Negative)
271 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000272
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000273 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000274 if (!CE) return false;
275
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000276 // The offset must be a multiple of 4 in the range 0-1020.
277 int64_t Value = CE->getValue();
278 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
279 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000280 bool isMemModeRegThumb() const {
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000281 if (!isMemory() || !Mem.OffsetIsReg || Mem.Writeback)
Bill Wendlingf4caf692010-12-14 03:36:38 +0000282 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000283 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000284 }
285 bool isMemModeImmThumb() const {
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000286 if (!isMemory() || Mem.OffsetIsReg || Mem.Writeback)
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000287 return false;
288
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000289 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000290 if (!CE) return false;
291
292 // The offset must be a multiple of 4 in the range 0-124.
293 uint64_t Value = CE->getValue();
294 return ((Value & 0x3) == 0 && Value <= 124);
295 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000296
297 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000298 // Add as immediates when possible. Null MCExpr = 0.
299 if (Expr == 0)
300 Inst.addOperand(MCOperand::CreateImm(0));
301 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000302 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
303 else
304 Inst.addOperand(MCOperand::CreateExpr(Expr));
305 }
306
Daniel Dunbar8462b302010-08-11 06:36:53 +0000307 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000308 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000309 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000310 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
311 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000312 }
313
Jim Grosbachd67641b2010-12-06 18:21:12 +0000314 void addCCOutOperands(MCInst &Inst, unsigned N) const {
315 assert(N == 1 && "Invalid number of operands!");
316 Inst.addOperand(MCOperand::CreateReg(getReg()));
317 }
318
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000319 void addRegOperands(MCInst &Inst, unsigned N) const {
320 assert(N == 1 && "Invalid number of operands!");
321 Inst.addOperand(MCOperand::CreateReg(getReg()));
322 }
323
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000324 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000325 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000326 const SmallVectorImpl<unsigned> &RegList = getRegList();
327 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000328 I = RegList.begin(), E = RegList.end(); I != E; ++I)
329 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000330 }
331
Bill Wendling0f630752010-11-17 04:32:08 +0000332 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
333 addRegListOperands(Inst, N);
334 }
335
336 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
337 addRegListOperands(Inst, N);
338 }
339
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000340 void addImmOperands(MCInst &Inst, unsigned N) const {
341 assert(N == 1 && "Invalid number of operands!");
342 addExpr(Inst, getImm());
343 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000344
Chris Lattner14b93852010-10-29 00:27:31 +0000345 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
346 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000347
Chris Lattner14b93852010-10-29 00:27:31 +0000348 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000349 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000350
Jim Grosbach80eb2332010-10-29 17:41:25 +0000351 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
352 // the difference?
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000353 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000354 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000355
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000356 // The MCInst offset operand doesn't include the low two bits (like
357 // the instruction encoding).
358 int64_t Offset = CE->getValue() / 4;
359 if (Offset >= 0)
360 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
361 Offset)));
362 else
363 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
364 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000365 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000366
Bill Wendlingf4caf692010-12-14 03:36:38 +0000367 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
368 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000369 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000370 Inst.addOperand(MCOperand::CreateReg(Mem.Offset.RegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000371 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000372
Bill Wendlingf4caf692010-12-14 03:36:38 +0000373 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
374 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
375 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000376 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset.Value);
Bill Wendlingf4caf692010-12-14 03:36:38 +0000377 assert(CE && "Non-constant mode offset operand!");
378 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000379 }
380
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000381 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000382
Chris Lattner3a697562010-10-28 17:20:03 +0000383 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
384 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000385 Op->CC.Val = CC;
386 Op->StartLoc = S;
387 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000388 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000389 }
390
Jim Grosbachd67641b2010-12-06 18:21:12 +0000391 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
392 ARMOperand *Op = new ARMOperand(CCOut);
393 Op->Reg.RegNum = RegNum;
394 Op->StartLoc = S;
395 Op->EndLoc = S;
396 return Op;
397 }
398
Chris Lattner3a697562010-10-28 17:20:03 +0000399 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
400 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000401 Op->Tok.Data = Str.data();
402 Op->Tok.Length = Str.size();
403 Op->StartLoc = S;
404 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000405 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000406 }
407
Bill Wendling50d0f582010-11-18 23:43:05 +0000408 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000409 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000410 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000411 Op->StartLoc = S;
412 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000413 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000414 }
415
Bill Wendling7729e062010-11-09 22:44:22 +0000416 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000417 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000418 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000419 KindTy Kind = RegisterList;
420
421 if (ARM::DPRRegClass.contains(Regs.front().first))
422 Kind = DPRRegisterList;
423 else if (ARM::SPRRegClass.contains(Regs.front().first))
424 Kind = SPRRegisterList;
425
426 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000427 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000428 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000429 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000430 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000431 Op->StartLoc = StartLoc;
432 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000433 return Op;
434 }
435
Chris Lattner3a697562010-10-28 17:20:03 +0000436 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
437 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000438 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000439 Op->StartLoc = S;
440 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000441 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000442 }
443
Chris Lattner3a697562010-10-28 17:20:03 +0000444 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000445 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000446 bool OffsetRegShifted, enum ShiftType ShiftType,
447 const MCExpr *ShiftAmount, bool Preindexed,
448 bool Postindexed, bool Negative, bool Writeback,
449 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000450 assert((OffsetRegNum == -1 || OffsetIsReg) &&
451 "OffsetRegNum must imply OffsetIsReg!");
452 assert((!OffsetRegShifted || OffsetIsReg) &&
453 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000454 assert((Offset || OffsetIsReg) &&
455 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000456 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
457 "Cannot have shift amount without shifted register offset!");
458 assert((!Offset || !OffsetIsReg) &&
459 "Cannot have expression offset and register offset!");
460
Chris Lattner3a697562010-10-28 17:20:03 +0000461 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000462 Op->Mem.BaseRegNum = BaseRegNum;
463 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000464 if (OffsetIsReg)
465 Op->Mem.Offset.RegNum = OffsetRegNum;
466 else
467 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000468 Op->Mem.OffsetRegShifted = OffsetRegShifted;
469 Op->Mem.ShiftType = ShiftType;
470 Op->Mem.ShiftAmount = ShiftAmount;
471 Op->Mem.Preindexed = Preindexed;
472 Op->Mem.Postindexed = Postindexed;
473 Op->Mem.Negative = Negative;
474 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000475
Sean Callanan76264762010-04-02 22:27:05 +0000476 Op->StartLoc = S;
477 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000478 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000479 }
480};
481
482} // end anonymous namespace.
483
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000484void ARMOperand::dump(raw_ostream &OS) const {
485 switch (Kind) {
486 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000487 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000488 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000489 case CCOut:
490 OS << "<ccout " << getReg() << ">";
491 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000492 case Immediate:
493 getImm()->print(OS);
494 break;
495 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000496 OS << "<memory "
497 << "base:" << getMemBaseRegNum();
498 if (getMemOffsetIsReg()) {
499 OS << " offset:<register " << getMemOffsetRegNum();
500 if (getMemOffsetRegShifted()) {
501 OS << " offset-shift-type:" << getMemShiftType();
502 OS << " offset-shift-amount:" << *getMemShiftAmount();
503 }
504 } else {
505 OS << " offset:" << *getMemOffset();
506 }
507 if (getMemOffsetIsReg())
508 OS << " (offset-is-reg)";
509 if (getMemPreindexed())
510 OS << " (pre-indexed)";
511 if (getMemPostindexed())
512 OS << " (post-indexed)";
513 if (getMemNegative())
514 OS << " (negative)";
515 if (getMemWriteback())
516 OS << " (writeback)";
517 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000518 break;
519 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000520 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000521 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000522 case RegisterList:
523 case DPRRegisterList:
524 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000525 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000526
Bill Wendling5fa22a12010-11-09 23:28:44 +0000527 const SmallVectorImpl<unsigned> &RegList = getRegList();
528 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000529 I = RegList.begin(), E = RegList.end(); I != E; ) {
530 OS << *I;
531 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000532 }
533
534 OS << ">";
535 break;
536 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000537 case Token:
538 OS << "'" << getToken() << "'";
539 break;
540 }
541}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000542
543/// @name Auto-generated Match Functions
544/// {
545
546static unsigned MatchRegisterName(StringRef Name);
547
548/// }
549
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000550/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000551/// and if it is a register name the token is eaten and the register number is
552/// returned. Otherwise return -1.
553///
554int ARMAsmParser::TryParseRegister() {
555 const AsmToken &Tok = Parser.getTok();
556 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000557
Chris Lattnere5658fa2010-10-30 04:09:10 +0000558 // FIXME: Validate register for the current architecture; we have to do
559 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000560 std::string upperCase = Tok.getString().str();
561 std::string lowerCase = LowercaseString(upperCase);
562 unsigned RegNum = MatchRegisterName(lowerCase);
563 if (!RegNum) {
564 RegNum = StringSwitch<unsigned>(lowerCase)
565 .Case("r13", ARM::SP)
566 .Case("r14", ARM::LR)
567 .Case("r15", ARM::PC)
568 .Case("ip", ARM::R12)
569 .Default(0);
570 }
571 if (!RegNum) return -1;
572
Chris Lattnere5658fa2010-10-30 04:09:10 +0000573 Parser.Lex(); // Eat identifier token.
574 return RegNum;
575}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000576
577
Bill Wendling50d0f582010-11-18 23:43:05 +0000578/// Try to parse a register name. The token must be an Identifier when called.
579/// If it's a register, an AsmOperand is created. Another AsmOperand is created
580/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000581///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000582/// TODO this is likely to change to allow different register types and or to
583/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000584bool ARMAsmParser::
585TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000586 SMLoc S = Parser.getTok().getLoc();
587 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000588 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000589 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000590
Bill Wendling50d0f582010-11-18 23:43:05 +0000591 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000592
Chris Lattnere5658fa2010-10-30 04:09:10 +0000593 const AsmToken &ExclaimTok = Parser.getTok();
594 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000595 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
596 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000597 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000598 }
599
Bill Wendling50d0f582010-11-18 23:43:05 +0000600 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000601}
602
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000603static int MatchMCRName(StringRef Name) {
604 // Use the same layout as the tablegen'erated register name matcher. Ugly,
605 // but efficient.
606 switch (Name.size()) {
607 default: break;
608 case 2:
609 if (Name[0] != 'p' && Name[0] != 'c')
610 return -1;
611 switch (Name[1]) {
612 default: return -1;
613 case '0': return 0;
614 case '1': return 1;
615 case '2': return 2;
616 case '3': return 3;
617 case '4': return 4;
618 case '5': return 5;
619 case '6': return 6;
620 case '7': return 7;
621 case '8': return 8;
622 case '9': return 9;
623 }
624 break;
625 case 3:
626 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
627 return -1;
628 switch (Name[2]) {
629 default: return -1;
630 case '0': return 10;
631 case '1': return 11;
632 case '2': return 12;
633 case '3': return 13;
634 case '4': return 14;
635 case '5': return 15;
636 }
637 break;
638 }
639
640 llvm_unreachable("Unhandled coprocessor operand string!");
641 return -1;
642}
643
644/// TryParseMCRName - Try to parse an MCR/MRC symbolic operand
645/// name. The token must be an Identifier when called, and if it is a MCR
646/// operand name, the token is eaten and the operand is added to the
647/// operand list.
648bool ARMAsmParser::
649TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
650 SMLoc S = Parser.getTok().getLoc();
651 const AsmToken &Tok = Parser.getTok();
652 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
653
654 int Num = MatchMCRName(Tok.getString());
655 if (Num == -1)
656 return true;
657
658 Parser.Lex(); // Eat identifier token.
659 Operands.push_back(ARMOperand::CreateImm(
660 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
661 return false;
662}
663
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000664/// Parse a register list, return it if successful else return null. The first
665/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000666bool ARMAsmParser::
667ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000668 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000669 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000670 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000671
Bill Wendling7729e062010-11-09 22:44:22 +0000672 // Read the rest of the registers in the list.
673 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000674 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000675
Bill Wendling7729e062010-11-09 22:44:22 +0000676 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000677 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000678 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000679
Sean Callanan18b83232010-01-19 21:44:56 +0000680 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000681 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000682 if (RegTok.isNot(AsmToken::Identifier)) {
683 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000684 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000685 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000686
Bill Wendling1d6a2652010-11-06 10:40:24 +0000687 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000688 if (RegNum == -1) {
689 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000690 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000691 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000692
Bill Wendlinge7176102010-11-06 22:36:58 +0000693 if (IsRange) {
694 int Reg = PrevRegNum;
695 do {
696 ++Reg;
697 Registers.push_back(std::make_pair(Reg, RegLoc));
698 } while (Reg != RegNum);
699 } else {
700 Registers.push_back(std::make_pair(RegNum, RegLoc));
701 }
702
703 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000704 } while (Parser.getTok().is(AsmToken::Comma) ||
705 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000706
707 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000708 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000709 if (RCurlyTok.isNot(AsmToken::RCurly)) {
710 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000711 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000712 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000713
Bill Wendlinge7176102010-11-06 22:36:58 +0000714 SMLoc E = RCurlyTok.getLoc();
715 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000716
Bill Wendlinge7176102010-11-06 22:36:58 +0000717 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000718 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000719 RI = Registers.begin(), RE = Registers.end();
720
Bill Wendling7caebff2011-01-12 21:20:59 +0000721 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000722 bool EmittedWarning = false;
723
Bill Wendling7caebff2011-01-12 21:20:59 +0000724 DenseMap<unsigned, bool> RegMap;
725 RegMap[HighRegNum] = true;
726
Bill Wendlinge7176102010-11-06 22:36:58 +0000727 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000728 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000729 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000730
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000731 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000732 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000733 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000734 }
735
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000736 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000737 Warning(RegInfo.second,
738 "register not in ascending order in register list");
739
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000740 RegMap[Reg] = true;
741 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000742 }
743
Bill Wendling50d0f582010-11-18 23:43:05 +0000744 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
745 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000746}
747
Bill Wendlinge7176102010-11-06 22:36:58 +0000748/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000749/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000750///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000751/// TODO Only preindexing and postindexing addressing are started, unindexed
752/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000753bool ARMAsmParser::
754ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000755 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000756 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000757 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000758 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000759 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000760
Sean Callanan18b83232010-01-19 21:44:56 +0000761 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000762 if (BaseRegTok.isNot(AsmToken::Identifier)) {
763 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000764 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000765 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000766 int BaseRegNum = TryParseRegister();
767 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000768 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000769 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000770 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000771
Daniel Dunbar05710932011-01-18 05:34:17 +0000772 // The next token must either be a comma or a closing bracket.
773 const AsmToken &Tok = Parser.getTok();
774 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
775 return true;
776
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000777 bool Preindexed = false;
778 bool Postindexed = false;
779 bool OffsetIsReg = false;
780 bool Negative = false;
781 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000782 ARMOperand *WBOp = 0;
783 int OffsetRegNum = -1;
784 bool OffsetRegShifted = false;
785 enum ShiftType ShiftType = Lsl;
786 const MCExpr *ShiftAmount = 0;
787 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000788
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000789 // First look for preindexed address forms, that is after the "[Rn" we now
790 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000791 if (Tok.is(AsmToken::Comma)) {
792 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000793 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000794
Chris Lattner550276e2010-10-28 20:52:15 +0000795 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
796 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000797 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000798 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000799 if (RBracTok.isNot(AsmToken::RBrac)) {
800 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000801 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000802 }
Sean Callanan76264762010-04-02 22:27:05 +0000803 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000804 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000805
Sean Callanan18b83232010-01-19 21:44:56 +0000806 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000807 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000808 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
809 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000810 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000811 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000812 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000813 } else {
814 // The "[Rn" we have so far was not followed by a comma.
815
Jim Grosbach80eb2332010-10-29 17:41:25 +0000816 // If there's anything other than the right brace, this is a post indexing
817 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000818 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000819 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000820
Sean Callanan18b83232010-01-19 21:44:56 +0000821 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000822
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000823 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000824 Postindexed = true;
825 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000826
Chris Lattner550276e2010-10-28 20:52:15 +0000827 if (NextTok.isNot(AsmToken::Comma)) {
828 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000829 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000830 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000831
Sean Callananb9a25b72010-01-19 20:27:46 +0000832 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000833
Chris Lattner550276e2010-10-28 20:52:15 +0000834 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000835 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000836 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000837 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000838 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000839 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000840
841 // Force Offset to exist if used.
842 if (!OffsetIsReg) {
843 if (!Offset)
844 Offset = MCConstantExpr::Create(0, getContext());
845 }
846
847 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
848 OffsetRegNum, OffsetRegShifted,
849 ShiftType, ShiftAmount, Preindexed,
850 Postindexed, Negative, Writeback,
851 S, E));
852 if (WBOp)
853 Operands.push_back(WBOp);
854
855 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000856}
857
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000858/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
859/// we will parse the following (were +/- means that a plus or minus is
860/// optional):
861/// +/-Rm
862/// +/-Rm, shift
863/// #offset
864/// we return false on success or an error otherwise.
865bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000866 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000867 enum ShiftType &ShiftType,
868 const MCExpr *&ShiftAmount,
869 const MCExpr *&Offset,
870 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000871 int &OffsetRegNum,
872 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000873 Negative = false;
874 OffsetRegShifted = false;
875 OffsetIsReg = false;
876 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000877 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000878 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000879 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000880 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000881 else if (NextTok.is(AsmToken::Minus)) {
882 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000883 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000884 }
885 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000886 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000887 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000888 SMLoc CurLoc = OffsetRegTok.getLoc();
889 OffsetRegNum = TryParseRegister();
890 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000891 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000892 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000893 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000894 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000895
Bill Wendling12f40e92010-11-06 10:51:53 +0000896 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000897 if (OffsetRegNum != -1) {
898 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000899 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000900 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000901 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000902
Sean Callanan18b83232010-01-19 21:44:56 +0000903 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000904 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000905 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000906 OffsetRegShifted = true;
907 }
908 }
909 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
910 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000911 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000912 if (HashTok.isNot(AsmToken::Hash))
913 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000914
Sean Callananb9a25b72010-01-19 20:27:46 +0000915 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000916
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000917 if (getParser().ParseExpression(Offset))
918 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000919 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000920 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000921 return false;
922}
923
924/// ParseShift as one of these two:
925/// ( lsl | lsr | asr | ror ) , # shift_amount
926/// rrx
927/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000928bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000929 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000930 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000931 if (Tok.isNot(AsmToken::Identifier))
932 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000933 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000934 if (ShiftName == "lsl" || ShiftName == "LSL")
935 St = Lsl;
936 else if (ShiftName == "lsr" || ShiftName == "LSR")
937 St = Lsr;
938 else if (ShiftName == "asr" || ShiftName == "ASR")
939 St = Asr;
940 else if (ShiftName == "ror" || ShiftName == "ROR")
941 St = Ror;
942 else if (ShiftName == "rrx" || ShiftName == "RRX")
943 St = Rrx;
944 else
945 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000946 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000947
948 // Rrx stands alone.
949 if (St == Rrx)
950 return false;
951
952 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000953 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000954 if (HashTok.isNot(AsmToken::Hash))
955 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000956 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000957
958 if (getParser().ParseExpression(ShiftAmount))
959 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000960
961 return false;
962}
963
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000964/// Parse a arm instruction operand. For now this parses the operand regardless
965/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000966bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
967 bool isMCR){
Sean Callanan76264762010-04-02 22:27:05 +0000968 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000969 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000970 default:
971 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000972 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000973 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000974 if (!TryParseRegisterWithWriteBack(Operands))
975 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000976 if (isMCR && !TryParseMCRName(Operands))
977 return false;
978
979 // Fall though for the Identifier case that is not a register or a
980 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000981 case AsmToken::Integer: // things like 1f and 2b as a branch targets
982 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000983 // This was not a register so parse other operands that start with an
984 // identifier (like labels) as expressions and create them as immediates.
985 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000986 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000987 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000988 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000989 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000990 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
991 return false;
992 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000993 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000994 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000995 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000996 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000997 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000998 // #42 -> immediate.
999 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001000 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001001 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001002 const MCExpr *ImmVal;
1003 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001004 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001005 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001006 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1007 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001008 case AsmToken::Colon: {
1009 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001010 // FIXME: Check it's an expression prefix,
1011 // e.g. (FOO - :lower16:BAR) isn't legal.
1012 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001013 if (ParsePrefix(RefKind))
1014 return true;
1015
Evan Cheng75972122011-01-13 07:58:56 +00001016 const MCExpr *SubExprVal;
1017 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001018 return true;
1019
Evan Cheng75972122011-01-13 07:58:56 +00001020 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1021 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001022 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001023 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001024 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001025 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001026 }
1027}
1028
Evan Cheng75972122011-01-13 07:58:56 +00001029// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1030// :lower16: and :upper16:.
1031bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1032 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001033
1034 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001035 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001036 Parser.Lex(); // Eat ':'
1037
1038 if (getLexer().isNot(AsmToken::Identifier)) {
1039 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1040 return true;
1041 }
1042
1043 StringRef IDVal = Parser.getTok().getIdentifier();
1044 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001045 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001046 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001047 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001048 } else {
1049 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1050 return true;
1051 }
1052 Parser.Lex();
1053
1054 if (getLexer().isNot(AsmToken::Colon)) {
1055 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1056 return true;
1057 }
1058 Parser.Lex(); // Eat the last ':'
1059 return false;
1060}
1061
1062const MCExpr *
1063ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1064 MCSymbolRefExpr::VariantKind Variant) {
1065 // Recurse over the given expression, rebuilding it to apply the given variant
1066 // to the leftmost symbol.
1067 if (Variant == MCSymbolRefExpr::VK_None)
1068 return E;
1069
1070 switch (E->getKind()) {
1071 case MCExpr::Target:
1072 llvm_unreachable("Can't handle target expr yet");
1073 case MCExpr::Constant:
1074 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1075
1076 case MCExpr::SymbolRef: {
1077 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1078
1079 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1080 return 0;
1081
1082 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1083 }
1084
1085 case MCExpr::Unary:
1086 llvm_unreachable("Can't handle unary expressions yet");
1087
1088 case MCExpr::Binary: {
1089 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1090 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1091 const MCExpr *RHS = BE->getRHS();
1092 if (!LHS)
1093 return 0;
1094
1095 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1096 }
1097 }
1098
1099 assert(0 && "Invalid expression kind!");
1100 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001101}
1102
Daniel Dunbar352e1482011-01-11 15:59:50 +00001103/// \brief Given a mnemonic, split out possible predication code and carry
1104/// setting letters to form a canonical mnemonic and flags.
1105//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001106// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001107static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1108 unsigned &PredicationCode,
1109 bool &CarrySetting) {
1110 PredicationCode = ARMCC::AL;
1111 CarrySetting = false;
1112
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001113 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001114 //
1115 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001116 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1117 Mnemonic == "movs" ||
1118 Mnemonic == "svc" ||
1119 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1120 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1121 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1122 Mnemonic == "vclt" ||
1123 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1124 Mnemonic == "vcle" ||
1125 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1126 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1127 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001128 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001129
Daniel Dunbar352e1482011-01-11 15:59:50 +00001130 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001131 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001132 .Case("eq", ARMCC::EQ)
1133 .Case("ne", ARMCC::NE)
1134 .Case("hs", ARMCC::HS)
1135 .Case("lo", ARMCC::LO)
1136 .Case("mi", ARMCC::MI)
1137 .Case("pl", ARMCC::PL)
1138 .Case("vs", ARMCC::VS)
1139 .Case("vc", ARMCC::VC)
1140 .Case("hi", ARMCC::HI)
1141 .Case("ls", ARMCC::LS)
1142 .Case("ge", ARMCC::GE)
1143 .Case("lt", ARMCC::LT)
1144 .Case("gt", ARMCC::GT)
1145 .Case("le", ARMCC::LE)
1146 .Case("al", ARMCC::AL)
1147 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001148 if (CC != ~0U) {
1149 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001150 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001151 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001152
Daniel Dunbar352e1482011-01-11 15:59:50 +00001153 // Next, determine if we have a carry setting bit. We explicitly ignore all
1154 // the instructions we know end in 's'.
1155 if (Mnemonic.endswith("s") &&
1156 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1157 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1158 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1159 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1160 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1161 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1162 CarrySetting = true;
1163 }
1164
1165 return Mnemonic;
1166}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001167
1168/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1169/// inclusion of carry set or predication code operands.
1170//
1171// FIXME: It would be nice to autogen this.
1172static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1173 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001174 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1175 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1176 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1177 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1178 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1179 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1180 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1181 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1182 CanAcceptCarrySet = true;
1183 } else {
1184 CanAcceptCarrySet = false;
1185 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001186
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001187 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1188 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1189 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1190 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1191 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001192 CanAcceptPredicationCode = false;
1193 } else {
1194 CanAcceptPredicationCode = true;
1195 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001196}
1197
1198/// Parse an arm instruction mnemonic followed by its operands.
1199bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1200 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1201 // Create the leading tokens for the mnemonic, split by '.' characters.
1202 size_t Start = 0, Next = Name.find('.');
1203 StringRef Head = Name.slice(Start, Next);
1204
Daniel Dunbar352e1482011-01-11 15:59:50 +00001205 // Split out the predication code and carry setting flag from the mnemonic.
1206 unsigned PredicationCode;
1207 bool CarrySetting;
1208 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001209
Chris Lattner3a697562010-10-28 17:20:03 +00001210 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001211
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001212 // Next, add the CCOut and ConditionCode operands, if needed.
1213 //
1214 // For mnemonics which can ever incorporate a carry setting bit or predication
1215 // code, our matching model involves us always generating CCOut and
1216 // ConditionCode operands to match the mnemonic "as written" and then we let
1217 // the matcher deal with finding the right instruction or generating an
1218 // appropriate error.
1219 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1220 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1221
1222 // Add the carry setting operand, if necessary.
1223 //
1224 // FIXME: It would be awesome if we could somehow invent a location such that
1225 // match errors on this operand would print a nice diagnostic about how the
1226 // 's' character in the mnemonic resulted in a CCOut operand.
1227 if (CanAcceptCarrySet) {
1228 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1229 NameLoc));
1230 } else {
1231 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1232 // misspelled another mnemonic).
1233
1234 // FIXME: Issue a nice error.
1235 }
1236
1237 // Add the predication code operand, if necessary.
1238 if (CanAcceptPredicationCode) {
1239 Operands.push_back(ARMOperand::CreateCondCode(
1240 ARMCC::CondCodes(PredicationCode), NameLoc));
1241 } else {
1242 // This mnemonic can't ever accept a predication code, but the user wrote
1243 // one (or misspelled another mnemonic).
1244
1245 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001246 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001247
1248 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001249 while (Next != StringRef::npos) {
1250 Start = Next;
1251 Next = Name.find('.', Start + 1);
1252 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001253
Chris Lattner3a697562010-10-28 17:20:03 +00001254 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001255 }
1256
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001257 bool isMCR = (Head == "mcr" || Head == "mcr2" ||
1258 Head == "mcrr" || Head == "mcrr2" ||
1259 Head == "mrc" || Head == "mrc2" ||
1260 Head == "mrrc" || Head == "mrrc2");
1261
Daniel Dunbar5747b132010-08-11 06:37:16 +00001262 // Read the remaining operands.
1263 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001264 // Read the first operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001265 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001266 Parser.EatToEndOfStatement();
1267 return true;
1268 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001269
1270 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001271 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001272
1273 // Parse and remember the operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001274 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001275 Parser.EatToEndOfStatement();
1276 return true;
1277 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001278 }
1279 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001280
Chris Lattnercbf8a982010-09-11 16:18:25 +00001281 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1282 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001283 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001284 }
Bill Wendling146018f2010-11-06 21:42:12 +00001285
Chris Lattner34e53142010-09-08 05:10:46 +00001286 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001287 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001288}
1289
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001290bool ARMAsmParser::
1291MatchAndEmitInstruction(SMLoc IDLoc,
1292 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1293 MCStreamer &Out) {
1294 MCInst Inst;
1295 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001296 MatchResultTy MatchResult, MatchResult2;
1297 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1298 if (MatchResult != Match_Success) {
1299 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1300 // that does not update the condition codes. So try adding a CCOut operand
1301 // with a value of reg0.
1302 if (MatchResult == Match_InvalidOperand) {
1303 Operands.insert(Operands.begin() + 1,
1304 ARMOperand::CreateCCOut(0,
1305 ((ARMOperand*)Operands[0])->getStartLoc()));
1306 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1307 if (MatchResult2 == Match_Success)
1308 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001309 else {
1310 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001311 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001312 delete CCOut;
1313 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001314 }
1315 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1316 // that updates the condition codes if it ends in 's'. So see if the
1317 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1318 // operand with a value of CPSR.
1319 else if(MatchResult == Match_MnemonicFail) {
1320 // Get the instruction mnemonic, which is the first token.
1321 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1322 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1323 // removed the 's' from the mnemonic for matching.
1324 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1325 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001326 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1327 Operands.erase(Operands.begin());
1328 delete OldMnemonic;
1329 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001330 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1331 Operands.insert(Operands.begin() + 1,
1332 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1333 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1334 if (MatchResult2 == Match_Success)
1335 MatchResult = Match_Success;
1336 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001337 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1338 Operands.erase(Operands.begin());
1339 delete OldMnemonic;
1340 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001341 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001342 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1343 Operands.erase(Operands.begin() + 1);
1344 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001345 }
1346 }
1347 }
1348 }
1349 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001350 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001351 Out.EmitInstruction(Inst);
1352 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001353 case Match_MissingFeature:
1354 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1355 return true;
1356 case Match_InvalidOperand: {
1357 SMLoc ErrorLoc = IDLoc;
1358 if (ErrorInfo != ~0U) {
1359 if (ErrorInfo >= Operands.size())
1360 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001361
Chris Lattnere73d4f82010-10-28 21:41:58 +00001362 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1363 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1364 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001365
Chris Lattnere73d4f82010-10-28 21:41:58 +00001366 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001367 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001368 case Match_MnemonicFail:
1369 return Error(IDLoc, "unrecognized instruction mnemonic");
1370 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001371
Eric Christopherc223e2b2010-10-29 09:26:59 +00001372 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001373 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001374}
1375
Kevin Enderby515d5092009-10-15 20:48:48 +00001376/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001377bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1378 StringRef IDVal = DirectiveID.getIdentifier();
1379 if (IDVal == ".word")
1380 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001381 else if (IDVal == ".thumb")
1382 return ParseDirectiveThumb(DirectiveID.getLoc());
1383 else if (IDVal == ".thumb_func")
1384 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1385 else if (IDVal == ".code")
1386 return ParseDirectiveCode(DirectiveID.getLoc());
1387 else if (IDVal == ".syntax")
1388 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001389 return true;
1390}
1391
1392/// ParseDirectiveWord
1393/// ::= .word [ expression (, expression)* ]
1394bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1395 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1396 for (;;) {
1397 const MCExpr *Value;
1398 if (getParser().ParseExpression(Value))
1399 return true;
1400
Chris Lattneraaec2052010-01-19 19:46:13 +00001401 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001402
1403 if (getLexer().is(AsmToken::EndOfStatement))
1404 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001405
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001406 // FIXME: Improve diagnostic.
1407 if (getLexer().isNot(AsmToken::Comma))
1408 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001409 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001410 }
1411 }
1412
Sean Callananb9a25b72010-01-19 20:27:46 +00001413 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001414 return false;
1415}
1416
Kevin Enderby515d5092009-10-15 20:48:48 +00001417/// ParseDirectiveThumb
1418/// ::= .thumb
1419bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1420 if (getLexer().isNot(AsmToken::EndOfStatement))
1421 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001422 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001423
1424 // TODO: set thumb mode
1425 // TODO: tell the MC streamer the mode
1426 // getParser().getStreamer().Emit???();
1427 return false;
1428}
1429
1430/// ParseDirectiveThumbFunc
1431/// ::= .thumbfunc symbol_name
1432bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001433 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001434 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001435 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001436 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001437 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001438 if (getLexer().isNot(AsmToken::EndOfStatement))
1439 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001440 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001441
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001442 // Mark symbol as a thumb symbol.
1443 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1444 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001445 return false;
1446}
1447
1448/// ParseDirectiveSyntax
1449/// ::= .syntax unified | divided
1450bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001451 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001452 if (Tok.isNot(AsmToken::Identifier))
1453 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001454 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001455 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001456 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001457 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001458 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001459 else
1460 return Error(L, "unrecognized syntax mode in .syntax directive");
1461
1462 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001463 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001464 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001465
1466 // TODO tell the MC streamer the mode
1467 // getParser().getStreamer().Emit???();
1468 return false;
1469}
1470
1471/// ParseDirectiveCode
1472/// ::= .code 16 | 32
1473bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001474 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001475 if (Tok.isNot(AsmToken::Integer))
1476 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001477 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001478 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001479 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001480 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001481 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001482 else
1483 return Error(L, "invalid operand to .code directive");
1484
1485 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001486 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001487 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001488
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001489 // FIXME: We need to be able switch subtargets at this point so that
1490 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1491 // includes Feature_IsThumb or not to match the right instructions. This is
1492 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1493 if (Val == 16){
1494 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1495 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001496 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001497 }
1498 else{
1499 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1500 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001501 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001502 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001503
Kevin Enderby515d5092009-10-15 20:48:48 +00001504 return false;
1505}
1506
Sean Callanan90b70972010-04-07 20:29:34 +00001507extern "C" void LLVMInitializeARMAsmLexer();
1508
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001509/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001510extern "C" void LLVMInitializeARMAsmParser() {
1511 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1512 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001513 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001514}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001515
Chris Lattner0692ee62010-09-06 19:11:01 +00001516#define GET_REGISTER_MATCHER
1517#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001518#include "ARMGenAsmMatcher.inc"