blob: ba42b1ec103f86ef970311955bf27fc960fb3c6e [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);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000084 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
85 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000086
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000087 /// @name Auto-generated Match Functions
88 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000089
Chris Lattner0692ee62010-09-06 19:11:01 +000090#define GET_ASSEMBLER_HEADER
91#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000092
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000093 /// }
94
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000095public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000096 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000097 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
98 // Initialize the set of available features.
99 setAvailableFeatures(ComputeAvailableFeatures(
100 &TM.getSubtarget<ARMSubtarget>()));
101 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000102
Benjamin Kramer38e59892010-07-14 22:38:02 +0000103 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000104 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000105 virtual bool ParseDirective(AsmToken DirectiveID);
106};
Jim Grosbach16c74252010-10-29 14:46:02 +0000107} // end anonymous namespace
108
Chris Lattner3a697562010-10-28 17:20:03 +0000109namespace {
110
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000111/// ARMOperand - Instances of this class represent a parsed ARM machine
112/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000113class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000114 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000115 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000116 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000117 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000118 Memory,
119 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000120 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000121 DPRRegisterList,
122 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000123 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000124 } Kind;
125
Sean Callanan76264762010-04-02 22:27:05 +0000126 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000127 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000128
129 union {
130 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000131 ARMCC::CondCodes Val;
132 } CC;
133
134 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000135 const char *Data;
136 unsigned Length;
137 } Tok;
138
139 struct {
140 unsigned RegNum;
141 } Reg;
142
Bill Wendling8155e5b2010-11-06 22:19:43 +0000143 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000144 const MCExpr *Val;
145 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000146
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000147 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000148 struct {
149 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000150 union {
151 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
152 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
153 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000154 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
155 enum ShiftType ShiftType; // used when OffsetRegShifted is true
156 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000157 unsigned Preindexed : 1;
158 unsigned Postindexed : 1;
159 unsigned OffsetIsReg : 1;
160 unsigned Negative : 1; // only used when OffsetIsReg is true
161 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000162 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000163 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000164
Bill Wendling146018f2010-11-06 21:42:12 +0000165 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
166public:
Sean Callanan76264762010-04-02 22:27:05 +0000167 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
168 Kind = o.Kind;
169 StartLoc = o.StartLoc;
170 EndLoc = o.EndLoc;
171 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000172 case CondCode:
173 CC = o.CC;
174 break;
Sean Callanan76264762010-04-02 22:27:05 +0000175 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000176 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000177 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000178 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000179 case Register:
180 Reg = o.Reg;
181 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000182 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000183 case DPRRegisterList:
184 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000185 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000186 break;
Sean Callanan76264762010-04-02 22:27:05 +0000187 case Immediate:
188 Imm = o.Imm;
189 break;
190 case Memory:
191 Mem = o.Mem;
192 break;
193 }
194 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000195
Sean Callanan76264762010-04-02 22:27:05 +0000196 /// getStartLoc - Get the location of the first token of this operand.
197 SMLoc getStartLoc() const { return StartLoc; }
198 /// getEndLoc - Get the location of the last token of this operand.
199 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000200
Daniel Dunbar8462b302010-08-11 06:36:53 +0000201 ARMCC::CondCodes getCondCode() const {
202 assert(Kind == CondCode && "Invalid access!");
203 return CC.Val;
204 }
205
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000206 StringRef getToken() const {
207 assert(Kind == Token && "Invalid access!");
208 return StringRef(Tok.Data, Tok.Length);
209 }
210
211 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000212 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000213 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000214 }
215
Bill Wendling5fa22a12010-11-09 23:28:44 +0000216 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000217 assert((Kind == RegisterList || Kind == DPRRegisterList ||
218 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000219 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000220 }
221
Kevin Enderbycfe07242009-10-13 22:19:02 +0000222 const MCExpr *getImm() const {
223 assert(Kind == Immediate && "Invalid access!");
224 return Imm.Val;
225 }
226
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000227 /// @name Memory Operand Accessors
228 /// @{
229
230 unsigned getMemBaseRegNum() const {
231 return Mem.BaseRegNum;
232 }
233 unsigned getMemOffsetRegNum() const {
234 assert(Mem.OffsetIsReg && "Invalid access!");
235 return Mem.Offset.RegNum;
236 }
237 const MCExpr *getMemOffset() const {
238 assert(!Mem.OffsetIsReg && "Invalid access!");
239 return Mem.Offset.Value;
240 }
241 unsigned getMemOffsetRegShifted() const {
242 assert(Mem.OffsetIsReg && "Invalid access!");
243 return Mem.OffsetRegShifted;
244 }
245 const MCExpr *getMemShiftAmount() const {
246 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
247 return Mem.ShiftAmount;
248 }
249 enum ShiftType getMemShiftType() const {
250 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
251 return Mem.ShiftType;
252 }
253 bool getMemPreindexed() const { return Mem.Preindexed; }
254 bool getMemPostindexed() const { return Mem.Postindexed; }
255 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
256 bool getMemNegative() const { return Mem.Negative; }
257 bool getMemWriteback() const { return Mem.Writeback; }
258
259 /// @}
260
Daniel Dunbar8462b302010-08-11 06:36:53 +0000261 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000262 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000263 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000264 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000265 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000266 bool isDPRRegList() const { return Kind == DPRRegisterList; }
267 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000268 bool isToken() const { return Kind == Token; }
269 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000270 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000271 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
272 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000273 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000274
Daniel Dunbar4b462672011-01-18 05:55:27 +0000275 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000276 if (!CE) return false;
277
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000278 // The offset must be a multiple of 4 in the range 0-1020.
279 int64_t Value = CE->getValue();
280 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
281 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000282 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000283 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000284 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000285 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000286 }
287 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000288 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000289 return false;
290
Daniel Dunbar4b462672011-01-18 05:55:27 +0000291 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000292 if (!CE) return false;
293
294 // The offset must be a multiple of 4 in the range 0-124.
295 uint64_t Value = CE->getValue();
296 return ((Value & 0x3) == 0 && Value <= 124);
297 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000298
299 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000300 // Add as immediates when possible. Null MCExpr = 0.
301 if (Expr == 0)
302 Inst.addOperand(MCOperand::CreateImm(0));
303 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000304 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
305 else
306 Inst.addOperand(MCOperand::CreateExpr(Expr));
307 }
308
Daniel Dunbar8462b302010-08-11 06:36:53 +0000309 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000310 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000311 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000312 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
313 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000314 }
315
Jim Grosbachd67641b2010-12-06 18:21:12 +0000316 void addCCOutOperands(MCInst &Inst, unsigned N) const {
317 assert(N == 1 && "Invalid number of operands!");
318 Inst.addOperand(MCOperand::CreateReg(getReg()));
319 }
320
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000321 void addRegOperands(MCInst &Inst, unsigned N) const {
322 assert(N == 1 && "Invalid number of operands!");
323 Inst.addOperand(MCOperand::CreateReg(getReg()));
324 }
325
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000326 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000327 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000328 const SmallVectorImpl<unsigned> &RegList = getRegList();
329 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000330 I = RegList.begin(), E = RegList.end(); I != E; ++I)
331 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000332 }
333
Bill Wendling0f630752010-11-17 04:32:08 +0000334 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
335 addRegListOperands(Inst, N);
336 }
337
338 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
339 addRegListOperands(Inst, N);
340 }
341
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000342 void addImmOperands(MCInst &Inst, unsigned N) const {
343 assert(N == 1 && "Invalid number of operands!");
344 addExpr(Inst, getImm());
345 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000346
Chris Lattner14b93852010-10-29 00:27:31 +0000347 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
348 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000349
Daniel Dunbar4b462672011-01-18 05:55:27 +0000350 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
351 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000352
Jim Grosbach80eb2332010-10-29 17:41:25 +0000353 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
354 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000355 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000356 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000357
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000358 // The MCInst offset operand doesn't include the low two bits (like
359 // the instruction encoding).
360 int64_t Offset = CE->getValue() / 4;
361 if (Offset >= 0)
362 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
363 Offset)));
364 else
365 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
366 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000367 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000368
Bill Wendlingf4caf692010-12-14 03:36:38 +0000369 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
370 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000371 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
372 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000373 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000374
Bill Wendlingf4caf692010-12-14 03:36:38 +0000375 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
376 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000377 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
378 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000379 assert(CE && "Non-constant mode offset operand!");
380 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000381 }
382
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000383 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000384
Chris Lattner3a697562010-10-28 17:20:03 +0000385 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
386 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000387 Op->CC.Val = CC;
388 Op->StartLoc = S;
389 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000390 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000391 }
392
Jim Grosbachd67641b2010-12-06 18:21:12 +0000393 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
394 ARMOperand *Op = new ARMOperand(CCOut);
395 Op->Reg.RegNum = RegNum;
396 Op->StartLoc = S;
397 Op->EndLoc = S;
398 return Op;
399 }
400
Chris Lattner3a697562010-10-28 17:20:03 +0000401 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
402 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000403 Op->Tok.Data = Str.data();
404 Op->Tok.Length = Str.size();
405 Op->StartLoc = S;
406 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000407 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000408 }
409
Bill Wendling50d0f582010-11-18 23:43:05 +0000410 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000411 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000412 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000413 Op->StartLoc = S;
414 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000415 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000416 }
417
Bill Wendling7729e062010-11-09 22:44:22 +0000418 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000419 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000420 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000421 KindTy Kind = RegisterList;
422
423 if (ARM::DPRRegClass.contains(Regs.front().first))
424 Kind = DPRRegisterList;
425 else if (ARM::SPRRegClass.contains(Regs.front().first))
426 Kind = SPRRegisterList;
427
428 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000429 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000430 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000431 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000432 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000433 Op->StartLoc = StartLoc;
434 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000435 return Op;
436 }
437
Chris Lattner3a697562010-10-28 17:20:03 +0000438 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
439 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000440 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000441 Op->StartLoc = S;
442 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000443 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000444 }
445
Chris Lattner3a697562010-10-28 17:20:03 +0000446 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000447 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000448 bool OffsetRegShifted, enum ShiftType ShiftType,
449 const MCExpr *ShiftAmount, bool Preindexed,
450 bool Postindexed, bool Negative, bool Writeback,
451 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000452 assert((OffsetRegNum == -1 || OffsetIsReg) &&
453 "OffsetRegNum must imply OffsetIsReg!");
454 assert((!OffsetRegShifted || OffsetIsReg) &&
455 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000456 assert((Offset || OffsetIsReg) &&
457 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000458 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
459 "Cannot have shift amount without shifted register offset!");
460 assert((!Offset || !OffsetIsReg) &&
461 "Cannot have expression offset and register offset!");
462
Chris Lattner3a697562010-10-28 17:20:03 +0000463 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000464 Op->Mem.BaseRegNum = BaseRegNum;
465 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000466 if (OffsetIsReg)
467 Op->Mem.Offset.RegNum = OffsetRegNum;
468 else
469 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000470 Op->Mem.OffsetRegShifted = OffsetRegShifted;
471 Op->Mem.ShiftType = ShiftType;
472 Op->Mem.ShiftAmount = ShiftAmount;
473 Op->Mem.Preindexed = Preindexed;
474 Op->Mem.Postindexed = Postindexed;
475 Op->Mem.Negative = Negative;
476 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000477
Sean Callanan76264762010-04-02 22:27:05 +0000478 Op->StartLoc = S;
479 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000480 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000481 }
482};
483
484} // end anonymous namespace.
485
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000486void ARMOperand::dump(raw_ostream &OS) const {
487 switch (Kind) {
488 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000489 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000490 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000491 case CCOut:
492 OS << "<ccout " << getReg() << ">";
493 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000494 case Immediate:
495 getImm()->print(OS);
496 break;
497 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000498 OS << "<memory "
499 << "base:" << getMemBaseRegNum();
500 if (getMemOffsetIsReg()) {
501 OS << " offset:<register " << getMemOffsetRegNum();
502 if (getMemOffsetRegShifted()) {
503 OS << " offset-shift-type:" << getMemShiftType();
504 OS << " offset-shift-amount:" << *getMemShiftAmount();
505 }
506 } else {
507 OS << " offset:" << *getMemOffset();
508 }
509 if (getMemOffsetIsReg())
510 OS << " (offset-is-reg)";
511 if (getMemPreindexed())
512 OS << " (pre-indexed)";
513 if (getMemPostindexed())
514 OS << " (post-indexed)";
515 if (getMemNegative())
516 OS << " (negative)";
517 if (getMemWriteback())
518 OS << " (writeback)";
519 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000520 break;
521 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000522 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000523 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000524 case RegisterList:
525 case DPRRegisterList:
526 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000527 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000528
Bill Wendling5fa22a12010-11-09 23:28:44 +0000529 const SmallVectorImpl<unsigned> &RegList = getRegList();
530 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000531 I = RegList.begin(), E = RegList.end(); I != E; ) {
532 OS << *I;
533 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000534 }
535
536 OS << ">";
537 break;
538 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000539 case Token:
540 OS << "'" << getToken() << "'";
541 break;
542 }
543}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000544
545/// @name Auto-generated Match Functions
546/// {
547
548static unsigned MatchRegisterName(StringRef Name);
549
550/// }
551
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000552/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000553/// and if it is a register name the token is eaten and the register number is
554/// returned. Otherwise return -1.
555///
556int ARMAsmParser::TryParseRegister() {
557 const AsmToken &Tok = Parser.getTok();
558 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000559
Chris Lattnere5658fa2010-10-30 04:09:10 +0000560 // FIXME: Validate register for the current architecture; we have to do
561 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000562 std::string upperCase = Tok.getString().str();
563 std::string lowerCase = LowercaseString(upperCase);
564 unsigned RegNum = MatchRegisterName(lowerCase);
565 if (!RegNum) {
566 RegNum = StringSwitch<unsigned>(lowerCase)
567 .Case("r13", ARM::SP)
568 .Case("r14", ARM::LR)
569 .Case("r15", ARM::PC)
570 .Case("ip", ARM::R12)
571 .Default(0);
572 }
573 if (!RegNum) return -1;
574
Chris Lattnere5658fa2010-10-30 04:09:10 +0000575 Parser.Lex(); // Eat identifier token.
576 return RegNum;
577}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000578
579
Bill Wendling50d0f582010-11-18 23:43:05 +0000580/// Try to parse a register name. The token must be an Identifier when called.
581/// If it's a register, an AsmOperand is created. Another AsmOperand is created
582/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000583///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000584/// TODO this is likely to change to allow different register types and or to
585/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000586bool ARMAsmParser::
587TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000588 SMLoc S = Parser.getTok().getLoc();
589 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000590 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000591 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000592
Bill Wendling50d0f582010-11-18 23:43:05 +0000593 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000594
Chris Lattnere5658fa2010-10-30 04:09:10 +0000595 const AsmToken &ExclaimTok = Parser.getTok();
596 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000597 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
598 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000599 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000600 }
601
Bill Wendling50d0f582010-11-18 23:43:05 +0000602 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000603}
604
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000605static int MatchMCRName(StringRef Name) {
606 // Use the same layout as the tablegen'erated register name matcher. Ugly,
607 // but efficient.
608 switch (Name.size()) {
609 default: break;
610 case 2:
611 if (Name[0] != 'p' && Name[0] != 'c')
612 return -1;
613 switch (Name[1]) {
614 default: return -1;
615 case '0': return 0;
616 case '1': return 1;
617 case '2': return 2;
618 case '3': return 3;
619 case '4': return 4;
620 case '5': return 5;
621 case '6': return 6;
622 case '7': return 7;
623 case '8': return 8;
624 case '9': return 9;
625 }
626 break;
627 case 3:
628 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
629 return -1;
630 switch (Name[2]) {
631 default: return -1;
632 case '0': return 10;
633 case '1': return 11;
634 case '2': return 12;
635 case '3': return 13;
636 case '4': return 14;
637 case '5': return 15;
638 }
639 break;
640 }
641
642 llvm_unreachable("Unhandled coprocessor operand string!");
643 return -1;
644}
645
646/// TryParseMCRName - Try to parse an MCR/MRC symbolic operand
647/// name. The token must be an Identifier when called, and if it is a MCR
648/// operand name, the token is eaten and the operand is added to the
649/// operand list.
650bool ARMAsmParser::
651TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
652 SMLoc S = Parser.getTok().getLoc();
653 const AsmToken &Tok = Parser.getTok();
654 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
655
656 int Num = MatchMCRName(Tok.getString());
657 if (Num == -1)
658 return true;
659
660 Parser.Lex(); // Eat identifier token.
661 Operands.push_back(ARMOperand::CreateImm(
662 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
663 return false;
664}
665
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000666/// Parse a register list, return it if successful else return null. The first
667/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000668bool ARMAsmParser::
669ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000670 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000671 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000672 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000673
Bill Wendling7729e062010-11-09 22:44:22 +0000674 // Read the rest of the registers in the list.
675 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000676 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000677
Bill Wendling7729e062010-11-09 22:44:22 +0000678 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000679 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000680 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000681
Sean Callanan18b83232010-01-19 21:44:56 +0000682 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000683 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000684 if (RegTok.isNot(AsmToken::Identifier)) {
685 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000686 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000687 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000688
Bill Wendling1d6a2652010-11-06 10:40:24 +0000689 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000690 if (RegNum == -1) {
691 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000692 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000693 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000694
Bill Wendlinge7176102010-11-06 22:36:58 +0000695 if (IsRange) {
696 int Reg = PrevRegNum;
697 do {
698 ++Reg;
699 Registers.push_back(std::make_pair(Reg, RegLoc));
700 } while (Reg != RegNum);
701 } else {
702 Registers.push_back(std::make_pair(RegNum, RegLoc));
703 }
704
705 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000706 } while (Parser.getTok().is(AsmToken::Comma) ||
707 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000708
709 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000710 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000711 if (RCurlyTok.isNot(AsmToken::RCurly)) {
712 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000713 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000714 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000715
Bill Wendlinge7176102010-11-06 22:36:58 +0000716 SMLoc E = RCurlyTok.getLoc();
717 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000718
Bill Wendlinge7176102010-11-06 22:36:58 +0000719 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000720 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000721 RI = Registers.begin(), RE = Registers.end();
722
Bill Wendling7caebff2011-01-12 21:20:59 +0000723 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000724 bool EmittedWarning = false;
725
Bill Wendling7caebff2011-01-12 21:20:59 +0000726 DenseMap<unsigned, bool> RegMap;
727 RegMap[HighRegNum] = true;
728
Bill Wendlinge7176102010-11-06 22:36:58 +0000729 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000730 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000731 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000732
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000733 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000734 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000735 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000736 }
737
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000738 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000739 Warning(RegInfo.second,
740 "register not in ascending order in register list");
741
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000742 RegMap[Reg] = true;
743 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000744 }
745
Bill Wendling50d0f582010-11-18 23:43:05 +0000746 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
747 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000748}
749
Bill Wendlinge7176102010-11-06 22:36:58 +0000750/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000751/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000752///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000753/// TODO Only preindexing and postindexing addressing are started, unindexed
754/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000755bool ARMAsmParser::
756ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000757 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000758 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000759 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000760 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000761 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000762
Sean Callanan18b83232010-01-19 21:44:56 +0000763 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000764 if (BaseRegTok.isNot(AsmToken::Identifier)) {
765 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000766 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000767 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000768 int BaseRegNum = TryParseRegister();
769 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000770 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000771 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000772 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000773
Daniel Dunbar05710932011-01-18 05:34:17 +0000774 // The next token must either be a comma or a closing bracket.
775 const AsmToken &Tok = Parser.getTok();
776 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
777 return true;
778
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000779 bool Preindexed = false;
780 bool Postindexed = false;
781 bool OffsetIsReg = false;
782 bool Negative = false;
783 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000784 ARMOperand *WBOp = 0;
785 int OffsetRegNum = -1;
786 bool OffsetRegShifted = false;
787 enum ShiftType ShiftType = Lsl;
788 const MCExpr *ShiftAmount = 0;
789 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000790
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000791 // First look for preindexed address forms, that is after the "[Rn" we now
792 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000793 if (Tok.is(AsmToken::Comma)) {
794 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000795 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000796
Chris Lattner550276e2010-10-28 20:52:15 +0000797 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
798 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000799 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000800 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000801 if (RBracTok.isNot(AsmToken::RBrac)) {
802 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000803 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000804 }
Sean Callanan76264762010-04-02 22:27:05 +0000805 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000806 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000807
Sean Callanan18b83232010-01-19 21:44:56 +0000808 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000809 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000810 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
811 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000812 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000813 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000814 }
Daniel Dunbar05710932011-01-18 05:34:17 +0000815 } else {
816 // The "[Rn" we have so far was not followed by a comma.
817
Jim Grosbach80eb2332010-10-29 17:41:25 +0000818 // If there's anything other than the right brace, this is a post indexing
819 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000820 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000821 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000822
Sean Callanan18b83232010-01-19 21:44:56 +0000823 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000824
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000825 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000826 Postindexed = true;
827 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000828
Chris Lattner550276e2010-10-28 20:52:15 +0000829 if (NextTok.isNot(AsmToken::Comma)) {
830 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000831 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000832 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000833
Sean Callananb9a25b72010-01-19 20:27:46 +0000834 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000835
Chris Lattner550276e2010-10-28 20:52:15 +0000836 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000837 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000838 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000839 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000840 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000841 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +0000842
843 // Force Offset to exist if used.
844 if (!OffsetIsReg) {
845 if (!Offset)
846 Offset = MCConstantExpr::Create(0, getContext());
847 }
848
849 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
850 OffsetRegNum, OffsetRegShifted,
851 ShiftType, ShiftAmount, Preindexed,
852 Postindexed, Negative, Writeback,
853 S, E));
854 if (WBOp)
855 Operands.push_back(WBOp);
856
857 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000858}
859
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000860/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
861/// we will parse the following (were +/- means that a plus or minus is
862/// optional):
863/// +/-Rm
864/// +/-Rm, shift
865/// #offset
866/// we return false on success or an error otherwise.
867bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000868 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000869 enum ShiftType &ShiftType,
870 const MCExpr *&ShiftAmount,
871 const MCExpr *&Offset,
872 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000873 int &OffsetRegNum,
874 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000875 Negative = false;
876 OffsetRegShifted = false;
877 OffsetIsReg = false;
878 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000879 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000880 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000881 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000882 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000883 else if (NextTok.is(AsmToken::Minus)) {
884 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000885 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000886 }
887 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000888 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000889 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000890 SMLoc CurLoc = OffsetRegTok.getLoc();
891 OffsetRegNum = TryParseRegister();
892 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000893 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000894 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000895 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000896 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000897
Bill Wendling12f40e92010-11-06 10:51:53 +0000898 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000899 if (OffsetRegNum != -1) {
900 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000901 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000902 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000903 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000904
Sean Callanan18b83232010-01-19 21:44:56 +0000905 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000906 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000907 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000908 OffsetRegShifted = true;
909 }
910 }
911 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
912 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000913 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000914 if (HashTok.isNot(AsmToken::Hash))
915 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000916
Sean Callananb9a25b72010-01-19 20:27:46 +0000917 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000918
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000919 if (getParser().ParseExpression(Offset))
920 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000921 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000922 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000923 return false;
924}
925
926/// ParseShift as one of these two:
927/// ( lsl | lsr | asr | ror ) , # shift_amount
928/// rrx
929/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000930bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000931 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000932 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000933 if (Tok.isNot(AsmToken::Identifier))
934 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000935 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000936 if (ShiftName == "lsl" || ShiftName == "LSL")
937 St = Lsl;
938 else if (ShiftName == "lsr" || ShiftName == "LSR")
939 St = Lsr;
940 else if (ShiftName == "asr" || ShiftName == "ASR")
941 St = Asr;
942 else if (ShiftName == "ror" || ShiftName == "ROR")
943 St = Ror;
944 else if (ShiftName == "rrx" || ShiftName == "RRX")
945 St = Rrx;
946 else
947 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000948 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000949
950 // Rrx stands alone.
951 if (St == Rrx)
952 return false;
953
954 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000955 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000956 if (HashTok.isNot(AsmToken::Hash))
957 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000958 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000959
960 if (getParser().ParseExpression(ShiftAmount))
961 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000962
963 return false;
964}
965
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000966/// Parse a arm instruction operand. For now this parses the operand regardless
967/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000968bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
969 bool isMCR){
Sean Callanan76264762010-04-02 22:27:05 +0000970 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000971 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000972 default:
973 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000974 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000975 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000976 if (!TryParseRegisterWithWriteBack(Operands))
977 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000978 if (isMCR && !TryParseMCRName(Operands))
979 return false;
980
981 // Fall though for the Identifier case that is not a register or a
982 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000983 case AsmToken::Integer: // things like 1f and 2b as a branch targets
984 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000985 // This was not a register so parse other operands that start with an
986 // identifier (like labels) as expressions and create them as immediates.
987 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000988 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000989 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000990 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000991 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000992 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
993 return false;
994 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000995 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000996 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000997 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000998 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000999 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001000 // #42 -> immediate.
1001 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001002 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001003 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001004 const MCExpr *ImmVal;
1005 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001006 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001007 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001008 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1009 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001010 case AsmToken::Colon: {
1011 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001012 // FIXME: Check it's an expression prefix,
1013 // e.g. (FOO - :lower16:BAR) isn't legal.
1014 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001015 if (ParsePrefix(RefKind))
1016 return true;
1017
Evan Cheng75972122011-01-13 07:58:56 +00001018 const MCExpr *SubExprVal;
1019 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001020 return true;
1021
Evan Cheng75972122011-01-13 07:58:56 +00001022 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1023 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001024 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001025 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001026 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001027 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001028 }
1029}
1030
Evan Cheng75972122011-01-13 07:58:56 +00001031// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1032// :lower16: and :upper16:.
1033bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1034 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001035
1036 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001037 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001038 Parser.Lex(); // Eat ':'
1039
1040 if (getLexer().isNot(AsmToken::Identifier)) {
1041 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1042 return true;
1043 }
1044
1045 StringRef IDVal = Parser.getTok().getIdentifier();
1046 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001047 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001048 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001049 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001050 } else {
1051 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1052 return true;
1053 }
1054 Parser.Lex();
1055
1056 if (getLexer().isNot(AsmToken::Colon)) {
1057 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1058 return true;
1059 }
1060 Parser.Lex(); // Eat the last ':'
1061 return false;
1062}
1063
1064const MCExpr *
1065ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1066 MCSymbolRefExpr::VariantKind Variant) {
1067 // Recurse over the given expression, rebuilding it to apply the given variant
1068 // to the leftmost symbol.
1069 if (Variant == MCSymbolRefExpr::VK_None)
1070 return E;
1071
1072 switch (E->getKind()) {
1073 case MCExpr::Target:
1074 llvm_unreachable("Can't handle target expr yet");
1075 case MCExpr::Constant:
1076 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1077
1078 case MCExpr::SymbolRef: {
1079 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1080
1081 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1082 return 0;
1083
1084 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1085 }
1086
1087 case MCExpr::Unary:
1088 llvm_unreachable("Can't handle unary expressions yet");
1089
1090 case MCExpr::Binary: {
1091 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1092 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1093 const MCExpr *RHS = BE->getRHS();
1094 if (!LHS)
1095 return 0;
1096
1097 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1098 }
1099 }
1100
1101 assert(0 && "Invalid expression kind!");
1102 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001103}
1104
Daniel Dunbar352e1482011-01-11 15:59:50 +00001105/// \brief Given a mnemonic, split out possible predication code and carry
1106/// setting letters to form a canonical mnemonic and flags.
1107//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001108// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001109static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1110 unsigned &PredicationCode,
1111 bool &CarrySetting) {
1112 PredicationCode = ARMCC::AL;
1113 CarrySetting = false;
1114
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001115 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001116 //
1117 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001118 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1119 Mnemonic == "movs" ||
1120 Mnemonic == "svc" ||
1121 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1122 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1123 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1124 Mnemonic == "vclt" ||
1125 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1126 Mnemonic == "vcle" ||
1127 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1128 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1129 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001130 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001131
Daniel Dunbar352e1482011-01-11 15:59:50 +00001132 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001133 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001134 .Case("eq", ARMCC::EQ)
1135 .Case("ne", ARMCC::NE)
1136 .Case("hs", ARMCC::HS)
1137 .Case("lo", ARMCC::LO)
1138 .Case("mi", ARMCC::MI)
1139 .Case("pl", ARMCC::PL)
1140 .Case("vs", ARMCC::VS)
1141 .Case("vc", ARMCC::VC)
1142 .Case("hi", ARMCC::HI)
1143 .Case("ls", ARMCC::LS)
1144 .Case("ge", ARMCC::GE)
1145 .Case("lt", ARMCC::LT)
1146 .Case("gt", ARMCC::GT)
1147 .Case("le", ARMCC::LE)
1148 .Case("al", ARMCC::AL)
1149 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001150 if (CC != ~0U) {
1151 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001152 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001153 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001154
Daniel Dunbar352e1482011-01-11 15:59:50 +00001155 // Next, determine if we have a carry setting bit. We explicitly ignore all
1156 // the instructions we know end in 's'.
1157 if (Mnemonic.endswith("s") &&
1158 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1159 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1160 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1161 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1162 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1163 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1164 CarrySetting = true;
1165 }
1166
1167 return Mnemonic;
1168}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001169
1170/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1171/// inclusion of carry set or predication code operands.
1172//
1173// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001174void ARMAsmParser::
1175GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1176 bool &CanAcceptPredicationCode) {
1177 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1178
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001179 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1180 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1181 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1182 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1183 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1184 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1185 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1186 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1187 CanAcceptCarrySet = true;
1188 } else {
1189 CanAcceptCarrySet = false;
1190 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001191
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001192 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1193 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1194 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1195 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001196 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001197 CanAcceptPredicationCode = false;
1198 } else {
1199 CanAcceptPredicationCode = true;
1200 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001201
1202 if (isThumb)
1203 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
1204 Mnemonic == "mrc" || Mnemonic == "mrrc")
1205 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001206}
1207
1208/// Parse an arm instruction mnemonic followed by its operands.
1209bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1210 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1211 // Create the leading tokens for the mnemonic, split by '.' characters.
1212 size_t Start = 0, Next = Name.find('.');
1213 StringRef Head = Name.slice(Start, Next);
1214
Daniel Dunbar352e1482011-01-11 15:59:50 +00001215 // Split out the predication code and carry setting flag from the mnemonic.
1216 unsigned PredicationCode;
1217 bool CarrySetting;
1218 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001219
Chris Lattner3a697562010-10-28 17:20:03 +00001220 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001221
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001222 // Next, add the CCOut and ConditionCode operands, if needed.
1223 //
1224 // For mnemonics which can ever incorporate a carry setting bit or predication
1225 // code, our matching model involves us always generating CCOut and
1226 // ConditionCode operands to match the mnemonic "as written" and then we let
1227 // the matcher deal with finding the right instruction or generating an
1228 // appropriate error.
1229 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1230 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1231
1232 // Add the carry setting operand, if necessary.
1233 //
1234 // FIXME: It would be awesome if we could somehow invent a location such that
1235 // match errors on this operand would print a nice diagnostic about how the
1236 // 's' character in the mnemonic resulted in a CCOut operand.
1237 if (CanAcceptCarrySet) {
1238 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1239 NameLoc));
1240 } else {
1241 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1242 // misspelled another mnemonic).
1243
1244 // FIXME: Issue a nice error.
1245 }
1246
1247 // Add the predication code operand, if necessary.
1248 if (CanAcceptPredicationCode) {
1249 Operands.push_back(ARMOperand::CreateCondCode(
1250 ARMCC::CondCodes(PredicationCode), NameLoc));
1251 } else {
1252 // This mnemonic can't ever accept a predication code, but the user wrote
1253 // one (or misspelled another mnemonic).
1254
1255 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001256 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001257
1258 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001259 while (Next != StringRef::npos) {
1260 Start = Next;
1261 Next = Name.find('.', Start + 1);
1262 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001263
Chris Lattner3a697562010-10-28 17:20:03 +00001264 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001265 }
1266
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001267 bool isMCR = (Head == "mcr" || Head == "mcr2" ||
1268 Head == "mcrr" || Head == "mcrr2" ||
1269 Head == "mrc" || Head == "mrc2" ||
1270 Head == "mrrc" || Head == "mrrc2");
1271
Daniel Dunbar5747b132010-08-11 06:37:16 +00001272 // Read the remaining operands.
1273 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001274 // Read the first operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001275 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001276 Parser.EatToEndOfStatement();
1277 return true;
1278 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001279
1280 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001281 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001282
1283 // Parse and remember the operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001284 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001285 Parser.EatToEndOfStatement();
1286 return true;
1287 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001288 }
1289 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001290
Chris Lattnercbf8a982010-09-11 16:18:25 +00001291 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1292 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001293 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001294 }
Bill Wendling146018f2010-11-06 21:42:12 +00001295
Chris Lattner34e53142010-09-08 05:10:46 +00001296 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001297 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001298}
1299
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001300bool ARMAsmParser::
1301MatchAndEmitInstruction(SMLoc IDLoc,
1302 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1303 MCStreamer &Out) {
1304 MCInst Inst;
1305 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001306 MatchResultTy MatchResult, MatchResult2;
1307 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1308 if (MatchResult != Match_Success) {
1309 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1310 // that does not update the condition codes. So try adding a CCOut operand
1311 // with a value of reg0.
1312 if (MatchResult == Match_InvalidOperand) {
1313 Operands.insert(Operands.begin() + 1,
1314 ARMOperand::CreateCCOut(0,
1315 ((ARMOperand*)Operands[0])->getStartLoc()));
1316 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1317 if (MatchResult2 == Match_Success)
1318 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001319 else {
1320 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001321 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001322 delete CCOut;
1323 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001324 }
1325 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1326 // that updates the condition codes if it ends in 's'. So see if the
1327 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1328 // operand with a value of CPSR.
1329 else if(MatchResult == Match_MnemonicFail) {
1330 // Get the instruction mnemonic, which is the first token.
1331 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1332 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1333 // removed the 's' from the mnemonic for matching.
1334 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1335 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001336 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1337 Operands.erase(Operands.begin());
1338 delete OldMnemonic;
1339 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001340 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1341 Operands.insert(Operands.begin() + 1,
1342 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1343 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1344 if (MatchResult2 == Match_Success)
1345 MatchResult = Match_Success;
1346 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001347 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1348 Operands.erase(Operands.begin());
1349 delete OldMnemonic;
1350 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001351 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001352 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1353 Operands.erase(Operands.begin() + 1);
1354 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001355 }
1356 }
1357 }
1358 }
1359 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001360 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001361 Out.EmitInstruction(Inst);
1362 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001363 case Match_MissingFeature:
1364 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1365 return true;
1366 case Match_InvalidOperand: {
1367 SMLoc ErrorLoc = IDLoc;
1368 if (ErrorInfo != ~0U) {
1369 if (ErrorInfo >= Operands.size())
1370 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001371
Chris Lattnere73d4f82010-10-28 21:41:58 +00001372 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1373 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1374 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001375
Chris Lattnere73d4f82010-10-28 21:41:58 +00001376 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001377 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001378 case Match_MnemonicFail:
1379 return Error(IDLoc, "unrecognized instruction mnemonic");
1380 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001381
Eric Christopherc223e2b2010-10-29 09:26:59 +00001382 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001383 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001384}
1385
Kevin Enderby515d5092009-10-15 20:48:48 +00001386/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001387bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1388 StringRef IDVal = DirectiveID.getIdentifier();
1389 if (IDVal == ".word")
1390 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001391 else if (IDVal == ".thumb")
1392 return ParseDirectiveThumb(DirectiveID.getLoc());
1393 else if (IDVal == ".thumb_func")
1394 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1395 else if (IDVal == ".code")
1396 return ParseDirectiveCode(DirectiveID.getLoc());
1397 else if (IDVal == ".syntax")
1398 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001399 return true;
1400}
1401
1402/// ParseDirectiveWord
1403/// ::= .word [ expression (, expression)* ]
1404bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1405 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1406 for (;;) {
1407 const MCExpr *Value;
1408 if (getParser().ParseExpression(Value))
1409 return true;
1410
Chris Lattneraaec2052010-01-19 19:46:13 +00001411 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001412
1413 if (getLexer().is(AsmToken::EndOfStatement))
1414 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001415
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001416 // FIXME: Improve diagnostic.
1417 if (getLexer().isNot(AsmToken::Comma))
1418 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001419 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001420 }
1421 }
1422
Sean Callananb9a25b72010-01-19 20:27:46 +00001423 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001424 return false;
1425}
1426
Kevin Enderby515d5092009-10-15 20:48:48 +00001427/// ParseDirectiveThumb
1428/// ::= .thumb
1429bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1430 if (getLexer().isNot(AsmToken::EndOfStatement))
1431 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001432 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001433
1434 // TODO: set thumb mode
1435 // TODO: tell the MC streamer the mode
1436 // getParser().getStreamer().Emit???();
1437 return false;
1438}
1439
1440/// ParseDirectiveThumbFunc
1441/// ::= .thumbfunc symbol_name
1442bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001443 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001444 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001445 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001446 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001447 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001448 if (getLexer().isNot(AsmToken::EndOfStatement))
1449 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001450 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001451
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001452 // Mark symbol as a thumb symbol.
1453 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1454 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001455 return false;
1456}
1457
1458/// ParseDirectiveSyntax
1459/// ::= .syntax unified | divided
1460bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001461 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001462 if (Tok.isNot(AsmToken::Identifier))
1463 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001464 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001465 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001466 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001467 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001468 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001469 else
1470 return Error(L, "unrecognized syntax mode in .syntax directive");
1471
1472 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001473 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001474 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001475
1476 // TODO tell the MC streamer the mode
1477 // getParser().getStreamer().Emit???();
1478 return false;
1479}
1480
1481/// ParseDirectiveCode
1482/// ::= .code 16 | 32
1483bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001484 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001485 if (Tok.isNot(AsmToken::Integer))
1486 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001487 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001488 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001489 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001490 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001491 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001492 else
1493 return Error(L, "invalid operand to .code directive");
1494
1495 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001496 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001497 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001498
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001499 // FIXME: We need to be able switch subtargets at this point so that
1500 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1501 // includes Feature_IsThumb or not to match the right instructions. This is
1502 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1503 if (Val == 16){
1504 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1505 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001506 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001507 }
1508 else{
1509 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1510 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001511 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001512 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001513
Kevin Enderby515d5092009-10-15 20:48:48 +00001514 return false;
1515}
1516
Sean Callanan90b70972010-04-07 20:29:34 +00001517extern "C" void LLVMInitializeARMAsmLexer();
1518
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001519/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001520extern "C" void LLVMInitializeARMAsmParser() {
1521 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1522 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001523 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001524}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001525
Chris Lattner0692ee62010-09-06 19:11:01 +00001526#define GET_REGISTER_MATCHER
1527#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001528#include "ARMGenAsmMatcher.inc"