blob: bd5546dc0fbed1d3b9f0d884294cc3c1d1777bb5 [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
Chris Lattner3a697562010-10-28 17:20:03 +000032namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000033
34class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000035
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000036class ARMAsmParser : public TargetAsmParser {
37 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000038 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000039
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000040 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000041 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
42
43 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000044 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
45
Chris Lattnere5658fa2010-10-30 04:09:10 +000046 int TryParseRegister();
Roman Divackybf755322011-01-27 17:14:22 +000047 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
Bill Wendling50d0f582010-11-18 23:43:05 +000048 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Anderson00828302011-03-18 22:50:18 +000049 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
Bill Wendling50d0f582010-11-18 23:43:05 +000050 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
51 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +000052 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
Evan Cheng75972122011-01-13 07:58:56 +000053 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000054 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
55 MCSymbolRefExpr::VariantKind Variant);
56
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000057
Kevin Enderby9c41fa82009-10-30 22:55:57 +000058 bool ParseMemoryOffsetReg(bool &Negative,
59 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +000060 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +000061 const MCExpr *&ShiftAmount,
62 const MCExpr *&Offset,
63 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000064 int &OffsetRegNum,
65 SMLoc &E);
Owen Anderson00828302011-03-18 22:50:18 +000066 bool ParseShift(enum ARM_AM::ShiftOpc &St,
67 const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000068 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveSyntax(SMLoc L);
73
Chris Lattner7036f8b2010-09-29 01:42:58 +000074 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000075 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000076 MCStreamer &Out);
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +000077 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
78 bool &CanAcceptPredicationCode);
Jim Grosbach16c74252010-10-29 14:46:02 +000079
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000080 /// @name Auto-generated Match Functions
81 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000082
Chris Lattner0692ee62010-09-06 19:11:01 +000083#define GET_ASSEMBLER_HEADER
84#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000085
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000086 /// }
87
Jim Grosbachf922c472011-02-12 01:34:40 +000088 OperandMatchResultTy tryParseCoprocNumOperand(
89 SmallVectorImpl<MCParsedAsmOperand*>&);
90 OperandMatchResultTy tryParseCoprocRegOperand(
91 SmallVectorImpl<MCParsedAsmOperand*>&);
92 OperandMatchResultTy tryParseMemBarrierOptOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000093 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +000094 OperandMatchResultTy tryParseProcIFlagsOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000095 SmallVectorImpl<MCParsedAsmOperand*>&);
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +000096 OperandMatchResultTy tryParseMSRMaskOperand(
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +000097 SmallVectorImpl<MCParsedAsmOperand*>&);
Jim Grosbachf922c472011-02-12 01:34:40 +000098
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000099public:
Daniel Dunbard73ada72010-07-19 00:33:49 +0000100 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +0000101 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
102 // Initialize the set of available features.
103 setAvailableFeatures(ComputeAvailableFeatures(
104 &TM.getSubtarget<ARMSubtarget>()));
105 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000106
Benjamin Kramer38e59892010-07-14 22:38:02 +0000107 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000108 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000109 virtual bool ParseDirective(AsmToken DirectiveID);
110};
Jim Grosbach16c74252010-10-29 14:46:02 +0000111} // end anonymous namespace
112
Chris Lattner3a697562010-10-28 17:20:03 +0000113namespace {
114
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000115/// ARMOperand - Instances of this class represent a parsed ARM machine
116/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000117class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000118 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000119 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000120 CCOut,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000121 CoprocNum,
122 CoprocReg,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000123 Immediate,
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000124 MemBarrierOpt,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000125 Memory,
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000126 MSRMask,
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000127 ProcIFlags,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000128 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000129 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000130 DPRRegisterList,
131 SPRRegisterList,
Owen Anderson00828302011-03-18 22:50:18 +0000132 Shifter,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000133 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000134 } Kind;
135
Sean Callanan76264762010-04-02 22:27:05 +0000136 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000137 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000138
139 union {
140 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000141 ARMCC::CondCodes Val;
142 } CC;
143
144 struct {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000145 ARM_MB::MemBOpt Val;
146 } MBOpt;
147
148 struct {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000149 unsigned Val;
150 } Cop;
151
152 struct {
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000153 ARM_PROC::IFlags Val;
154 } IFlags;
155
156 struct {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000157 unsigned Val;
158 } MMask;
159
160 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000161 const char *Data;
162 unsigned Length;
163 } Tok;
164
165 struct {
166 unsigned RegNum;
167 } Reg;
168
Bill Wendling8155e5b2010-11-06 22:19:43 +0000169 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000170 const MCExpr *Val;
171 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000172
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000173 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000174 struct {
175 unsigned BaseRegNum;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000176 union {
177 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
178 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
179 } Offset;
Bill Wendling146018f2010-11-06 21:42:12 +0000180 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
Owen Anderson00828302011-03-18 22:50:18 +0000181 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
Bill Wendling146018f2010-11-06 21:42:12 +0000182 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000183 unsigned Preindexed : 1;
184 unsigned Postindexed : 1;
185 unsigned OffsetIsReg : 1;
186 unsigned Negative : 1; // only used when OffsetIsReg is true
187 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000188 } Mem;
Owen Anderson00828302011-03-18 22:50:18 +0000189
190 struct {
191 ARM_AM::ShiftOpc ShiftTy;
192 unsigned RegNum;
193 } Shift;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000194 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000195
Bill Wendling146018f2010-11-06 21:42:12 +0000196 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
197public:
Sean Callanan76264762010-04-02 22:27:05 +0000198 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
199 Kind = o.Kind;
200 StartLoc = o.StartLoc;
201 EndLoc = o.EndLoc;
202 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000203 case CondCode:
204 CC = o.CC;
205 break;
Sean Callanan76264762010-04-02 22:27:05 +0000206 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000207 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000208 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000209 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000210 case Register:
211 Reg = o.Reg;
212 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000213 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000214 case DPRRegisterList:
215 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000216 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000217 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000218 case CoprocNum:
219 case CoprocReg:
220 Cop = o.Cop;
221 break;
Sean Callanan76264762010-04-02 22:27:05 +0000222 case Immediate:
223 Imm = o.Imm;
224 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000225 case MemBarrierOpt:
226 MBOpt = o.MBOpt;
227 break;
Sean Callanan76264762010-04-02 22:27:05 +0000228 case Memory:
229 Mem = o.Mem;
230 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000231 case MSRMask:
232 MMask = o.MMask;
233 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000234 case ProcIFlags:
235 IFlags = o.IFlags;
Owen Anderson00828302011-03-18 22:50:18 +0000236 break;
237 case Shifter:
238 Shift = o.Shift;
239 break;
Sean Callanan76264762010-04-02 22:27:05 +0000240 }
241 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000242
Sean Callanan76264762010-04-02 22:27:05 +0000243 /// getStartLoc - Get the location of the first token of this operand.
244 SMLoc getStartLoc() const { return StartLoc; }
245 /// getEndLoc - Get the location of the last token of this operand.
246 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000247
Daniel Dunbar8462b302010-08-11 06:36:53 +0000248 ARMCC::CondCodes getCondCode() const {
249 assert(Kind == CondCode && "Invalid access!");
250 return CC.Val;
251 }
252
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000253 unsigned getCoproc() const {
254 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
255 return Cop.Val;
256 }
257
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000258 StringRef getToken() const {
259 assert(Kind == Token && "Invalid access!");
260 return StringRef(Tok.Data, Tok.Length);
261 }
262
263 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000264 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000265 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000266 }
267
Bill Wendling5fa22a12010-11-09 23:28:44 +0000268 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000269 assert((Kind == RegisterList || Kind == DPRRegisterList ||
270 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000271 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000272 }
273
Kevin Enderbycfe07242009-10-13 22:19:02 +0000274 const MCExpr *getImm() const {
275 assert(Kind == Immediate && "Invalid access!");
276 return Imm.Val;
277 }
278
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000279 ARM_MB::MemBOpt getMemBarrierOpt() const {
280 assert(Kind == MemBarrierOpt && "Invalid access!");
281 return MBOpt.Val;
282 }
283
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000284 ARM_PROC::IFlags getProcIFlags() const {
285 assert(Kind == ProcIFlags && "Invalid access!");
286 return IFlags.Val;
287 }
288
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000289 unsigned getMSRMask() const {
290 assert(Kind == MSRMask && "Invalid access!");
291 return MMask.Val;
292 }
293
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000294 /// @name Memory Operand Accessors
295 /// @{
296
297 unsigned getMemBaseRegNum() const {
298 return Mem.BaseRegNum;
299 }
300 unsigned getMemOffsetRegNum() const {
301 assert(Mem.OffsetIsReg && "Invalid access!");
302 return Mem.Offset.RegNum;
303 }
304 const MCExpr *getMemOffset() const {
305 assert(!Mem.OffsetIsReg && "Invalid access!");
306 return Mem.Offset.Value;
307 }
308 unsigned getMemOffsetRegShifted() const {
309 assert(Mem.OffsetIsReg && "Invalid access!");
310 return Mem.OffsetRegShifted;
311 }
312 const MCExpr *getMemShiftAmount() const {
313 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
314 return Mem.ShiftAmount;
315 }
Owen Anderson00828302011-03-18 22:50:18 +0000316 enum ARM_AM::ShiftOpc getMemShiftType() const {
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000317 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
318 return Mem.ShiftType;
319 }
320 bool getMemPreindexed() const { return Mem.Preindexed; }
321 bool getMemPostindexed() const { return Mem.Postindexed; }
322 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
323 bool getMemNegative() const { return Mem.Negative; }
324 bool getMemWriteback() const { return Mem.Writeback; }
325
326 /// @}
327
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000328 bool isCoprocNum() const { return Kind == CoprocNum; }
329 bool isCoprocReg() const { return Kind == CoprocReg; }
Daniel Dunbar8462b302010-08-11 06:36:53 +0000330 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000331 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000332 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000333 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000334 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000335 bool isDPRRegList() const { return Kind == DPRRegisterList; }
336 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000337 bool isToken() const { return Kind == Token; }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000338 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
Chris Lattner14b93852010-10-29 00:27:31 +0000339 bool isMemory() const { return Kind == Memory; }
Owen Anderson00828302011-03-18 22:50:18 +0000340 bool isShifter() const { return Kind == Shifter; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000341 bool isMemMode5() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000342 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
343 getMemNegative())
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000344 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000345
Daniel Dunbar4b462672011-01-18 05:55:27 +0000346 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000347 if (!CE) return false;
348
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000349 // The offset must be a multiple of 4 in the range 0-1020.
350 int64_t Value = CE->getValue();
351 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
352 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000353 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000354 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000355 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000356 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000357 }
358 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000359 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000360 return false;
361
Daniel Dunbar4b462672011-01-18 05:55:27 +0000362 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000363 if (!CE) return false;
364
365 // The offset must be a multiple of 4 in the range 0-124.
366 uint64_t Value = CE->getValue();
367 return ((Value & 0x3) == 0 && Value <= 124);
368 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000369 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000370 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000371
372 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000373 // Add as immediates when possible. Null MCExpr = 0.
374 if (Expr == 0)
375 Inst.addOperand(MCOperand::CreateImm(0));
376 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000377 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
378 else
379 Inst.addOperand(MCOperand::CreateExpr(Expr));
380 }
381
Daniel Dunbar8462b302010-08-11 06:36:53 +0000382 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000383 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000384 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000385 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
386 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000387 }
388
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000389 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
390 assert(N == 1 && "Invalid number of operands!");
391 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
392 }
393
394 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
395 assert(N == 1 && "Invalid number of operands!");
396 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
397 }
398
Jim Grosbachd67641b2010-12-06 18:21:12 +0000399 void addCCOutOperands(MCInst &Inst, unsigned N) const {
400 assert(N == 1 && "Invalid number of operands!");
401 Inst.addOperand(MCOperand::CreateReg(getReg()));
402 }
403
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000404 void addRegOperands(MCInst &Inst, unsigned N) const {
405 assert(N == 1 && "Invalid number of operands!");
406 Inst.addOperand(MCOperand::CreateReg(getReg()));
407 }
408
Owen Anderson00828302011-03-18 22:50:18 +0000409 void addShifterOperands(MCInst &Inst, unsigned N) const {
410 assert(N == 1 && "Invalid number of operands!");
411 Inst.addOperand(MCOperand::CreateImm(
412 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
413 }
414
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000415 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000416 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000417 const SmallVectorImpl<unsigned> &RegList = getRegList();
418 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000419 I = RegList.begin(), E = RegList.end(); I != E; ++I)
420 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000421 }
422
Bill Wendling0f630752010-11-17 04:32:08 +0000423 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
424 addRegListOperands(Inst, N);
425 }
426
427 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
428 addRegListOperands(Inst, N);
429 }
430
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000431 void addImmOperands(MCInst &Inst, unsigned N) const {
432 assert(N == 1 && "Invalid number of operands!");
433 addExpr(Inst, getImm());
434 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000435
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000436 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
437 assert(N == 1 && "Invalid number of operands!");
438 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
439 }
440
Chris Lattner14b93852010-10-29 00:27:31 +0000441 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
442 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000443
Daniel Dunbar4b462672011-01-18 05:55:27 +0000444 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
445 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000446
Jim Grosbach80eb2332010-10-29 17:41:25 +0000447 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
448 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000449 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000450 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000451
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000452 // The MCInst offset operand doesn't include the low two bits (like
453 // the instruction encoding).
454 int64_t Offset = CE->getValue() / 4;
455 if (Offset >= 0)
456 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
457 Offset)));
458 else
459 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
460 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000461 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000462
Bill Wendlingf4caf692010-12-14 03:36:38 +0000463 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
464 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000465 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
466 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000467 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000468
Bill Wendlingf4caf692010-12-14 03:36:38 +0000469 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
470 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000471 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
472 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000473 assert(CE && "Non-constant mode offset operand!");
474 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000475 }
476
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000477 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
478 assert(N == 1 && "Invalid number of operands!");
479 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
480 }
481
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000482 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
483 assert(N == 1 && "Invalid number of operands!");
484 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
485 }
486
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000487 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000488
Chris Lattner3a697562010-10-28 17:20:03 +0000489 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
490 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000491 Op->CC.Val = CC;
492 Op->StartLoc = S;
493 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000494 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000495 }
496
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000497 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
498 ARMOperand *Op = new ARMOperand(CoprocNum);
499 Op->Cop.Val = CopVal;
500 Op->StartLoc = S;
501 Op->EndLoc = S;
502 return Op;
503 }
504
505 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
506 ARMOperand *Op = new ARMOperand(CoprocReg);
507 Op->Cop.Val = CopVal;
508 Op->StartLoc = S;
509 Op->EndLoc = S;
510 return Op;
511 }
512
Jim Grosbachd67641b2010-12-06 18:21:12 +0000513 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
514 ARMOperand *Op = new ARMOperand(CCOut);
515 Op->Reg.RegNum = RegNum;
516 Op->StartLoc = S;
517 Op->EndLoc = S;
518 return Op;
519 }
520
Chris Lattner3a697562010-10-28 17:20:03 +0000521 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
522 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000523 Op->Tok.Data = Str.data();
524 Op->Tok.Length = Str.size();
525 Op->StartLoc = S;
526 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000527 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000528 }
529
Bill Wendling50d0f582010-11-18 23:43:05 +0000530 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000531 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000532 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000533 Op->StartLoc = S;
534 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000535 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000536 }
537
Owen Anderson00828302011-03-18 22:50:18 +0000538 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
539 SMLoc S, SMLoc E) {
540 ARMOperand *Op = new ARMOperand(Shifter);
541 Op->Shift.ShiftTy = ShTy;
542 Op->StartLoc = S;
543 Op->EndLoc = E;
544 return Op;
545 }
546
Bill Wendling7729e062010-11-09 22:44:22 +0000547 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000548 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000549 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000550 KindTy Kind = RegisterList;
551
552 if (ARM::DPRRegClass.contains(Regs.front().first))
553 Kind = DPRRegisterList;
554 else if (ARM::SPRRegClass.contains(Regs.front().first))
555 Kind = SPRRegisterList;
556
557 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000558 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000559 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000560 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000561 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000562 Op->StartLoc = StartLoc;
563 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000564 return Op;
565 }
566
Chris Lattner3a697562010-10-28 17:20:03 +0000567 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
568 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000569 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000570 Op->StartLoc = S;
571 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000572 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000573 }
574
Chris Lattner3a697562010-10-28 17:20:03 +0000575 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000576 const MCExpr *Offset, int OffsetRegNum,
Owen Anderson00828302011-03-18 22:50:18 +0000577 bool OffsetRegShifted,
578 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000579 const MCExpr *ShiftAmount, bool Preindexed,
580 bool Postindexed, bool Negative, bool Writeback,
581 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000582 assert((OffsetRegNum == -1 || OffsetIsReg) &&
583 "OffsetRegNum must imply OffsetIsReg!");
584 assert((!OffsetRegShifted || OffsetIsReg) &&
585 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000586 assert((Offset || OffsetIsReg) &&
587 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000588 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
589 "Cannot have shift amount without shifted register offset!");
590 assert((!Offset || !OffsetIsReg) &&
591 "Cannot have expression offset and register offset!");
592
Chris Lattner3a697562010-10-28 17:20:03 +0000593 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000594 Op->Mem.BaseRegNum = BaseRegNum;
595 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000596 if (OffsetIsReg)
597 Op->Mem.Offset.RegNum = OffsetRegNum;
598 else
599 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000600 Op->Mem.OffsetRegShifted = OffsetRegShifted;
601 Op->Mem.ShiftType = ShiftType;
602 Op->Mem.ShiftAmount = ShiftAmount;
603 Op->Mem.Preindexed = Preindexed;
604 Op->Mem.Postindexed = Postindexed;
605 Op->Mem.Negative = Negative;
606 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000607
Sean Callanan76264762010-04-02 22:27:05 +0000608 Op->StartLoc = S;
609 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000610 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000611 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000612
613 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
614 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
615 Op->MBOpt.Val = Opt;
616 Op->StartLoc = S;
617 Op->EndLoc = S;
618 return Op;
619 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000620
621 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
622 ARMOperand *Op = new ARMOperand(ProcIFlags);
623 Op->IFlags.Val = IFlags;
624 Op->StartLoc = S;
625 Op->EndLoc = S;
626 return Op;
627 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000628
629 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
630 ARMOperand *Op = new ARMOperand(MSRMask);
631 Op->MMask.Val = MMask;
632 Op->StartLoc = S;
633 Op->EndLoc = S;
634 return Op;
635 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000636};
637
638} // end anonymous namespace.
639
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000640void ARMOperand::dump(raw_ostream &OS) const {
641 switch (Kind) {
642 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000643 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000644 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000645 case CCOut:
646 OS << "<ccout " << getReg() << ">";
647 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000648 case CoprocNum:
649 OS << "<coprocessor number: " << getCoproc() << ">";
650 break;
651 case CoprocReg:
652 OS << "<coprocessor register: " << getCoproc() << ">";
653 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000654 case MSRMask:
655 OS << "<mask: " << getMSRMask() << ">";
656 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000657 case Immediate:
658 getImm()->print(OS);
659 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000660 case MemBarrierOpt:
661 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
662 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000663 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000664 OS << "<memory "
665 << "base:" << getMemBaseRegNum();
666 if (getMemOffsetIsReg()) {
667 OS << " offset:<register " << getMemOffsetRegNum();
668 if (getMemOffsetRegShifted()) {
669 OS << " offset-shift-type:" << getMemShiftType();
670 OS << " offset-shift-amount:" << *getMemShiftAmount();
671 }
672 } else {
673 OS << " offset:" << *getMemOffset();
674 }
675 if (getMemOffsetIsReg())
676 OS << " (offset-is-reg)";
677 if (getMemPreindexed())
678 OS << " (pre-indexed)";
679 if (getMemPostindexed())
680 OS << " (post-indexed)";
681 if (getMemNegative())
682 OS << " (negative)";
683 if (getMemWriteback())
684 OS << " (writeback)";
685 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000686 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000687 case ProcIFlags: {
688 OS << "<ARM_PROC::";
689 unsigned IFlags = getProcIFlags();
690 for (int i=2; i >= 0; --i)
691 if (IFlags & (1 << i))
692 OS << ARM_PROC::IFlagsToString(1 << i);
693 OS << ">";
694 break;
695 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000696 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000697 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000698 break;
Owen Anderson00828302011-03-18 22:50:18 +0000699 case Shifter:
700 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
701 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000702 case RegisterList:
703 case DPRRegisterList:
704 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000705 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000706
Bill Wendling5fa22a12010-11-09 23:28:44 +0000707 const SmallVectorImpl<unsigned> &RegList = getRegList();
708 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000709 I = RegList.begin(), E = RegList.end(); I != E; ) {
710 OS << *I;
711 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000712 }
713
714 OS << ">";
715 break;
716 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000717 case Token:
718 OS << "'" << getToken() << "'";
719 break;
720 }
721}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000722
723/// @name Auto-generated Match Functions
724/// {
725
726static unsigned MatchRegisterName(StringRef Name);
727
728/// }
729
Bob Wilson69df7232011-02-03 21:46:10 +0000730bool ARMAsmParser::ParseRegister(unsigned &RegNo,
731 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000732 RegNo = TryParseRegister();
733
734 return (RegNo == (unsigned)-1);
735}
736
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000737/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000738/// and if it is a register name the token is eaten and the register number is
739/// returned. Otherwise return -1.
740///
741int ARMAsmParser::TryParseRegister() {
742 const AsmToken &Tok = Parser.getTok();
743 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000744
Chris Lattnere5658fa2010-10-30 04:09:10 +0000745 // FIXME: Validate register for the current architecture; we have to do
746 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000747 std::string upperCase = Tok.getString().str();
748 std::string lowerCase = LowercaseString(upperCase);
749 unsigned RegNum = MatchRegisterName(lowerCase);
750 if (!RegNum) {
751 RegNum = StringSwitch<unsigned>(lowerCase)
752 .Case("r13", ARM::SP)
753 .Case("r14", ARM::LR)
754 .Case("r15", ARM::PC)
755 .Case("ip", ARM::R12)
756 .Default(0);
757 }
758 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000759
Chris Lattnere5658fa2010-10-30 04:09:10 +0000760 Parser.Lex(); // Eat identifier token.
761 return RegNum;
762}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000763
Owen Anderson00828302011-03-18 22:50:18 +0000764/// Try to parse a register name. The token must be an Identifier when called,
765/// and if it is a register name the token is eaten and the register number is
766/// returned. Otherwise return -1.
767///
768bool ARMAsmParser::TryParseShiftRegister(
769 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
770 SMLoc S = Parser.getTok().getLoc();
771 const AsmToken &Tok = Parser.getTok();
772 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
773
774 std::string upperCase = Tok.getString().str();
775 std::string lowerCase = LowercaseString(upperCase);
776 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
777 .Case("lsl", ARM_AM::lsl)
778 .Case("lsr", ARM_AM::lsr)
779 .Case("asr", ARM_AM::asr)
780 .Case("ror", ARM_AM::ror)
781 .Case("rrx", ARM_AM::rrx)
782 .Default(ARM_AM::no_shift);
783
784 if (ShiftTy == ARM_AM::no_shift)
785 return true;
786
787 Parser.Lex(); // Eat shift-type operand;
788 int RegNum = TryParseRegister();
789 if (RegNum == -1)
790 return Error(Parser.getTok().getLoc(), "register expected");
791
792 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
793 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
794 S, Parser.getTok().getLoc()));
795
796 return false;
797}
798
799
Bill Wendling50d0f582010-11-18 23:43:05 +0000800/// Try to parse a register name. The token must be an Identifier when called.
801/// If it's a register, an AsmOperand is created. Another AsmOperand is created
802/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000803///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000804/// TODO this is likely to change to allow different register types and or to
805/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000806bool ARMAsmParser::
807TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000808 SMLoc S = Parser.getTok().getLoc();
809 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000810 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000811 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000812
Bill Wendling50d0f582010-11-18 23:43:05 +0000813 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000814
Chris Lattnere5658fa2010-10-30 04:09:10 +0000815 const AsmToken &ExclaimTok = Parser.getTok();
816 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000817 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
818 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000819 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000820 }
821
Bill Wendling50d0f582010-11-18 23:43:05 +0000822 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000823}
824
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000825/// MatchCoprocessorOperandName - Try to parse an coprocessor related
826/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
827/// "c5", ...
828static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000829 // Use the same layout as the tablegen'erated register name matcher. Ugly,
830 // but efficient.
831 switch (Name.size()) {
832 default: break;
833 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000834 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000835 return -1;
836 switch (Name[1]) {
837 default: return -1;
838 case '0': return 0;
839 case '1': return 1;
840 case '2': return 2;
841 case '3': return 3;
842 case '4': return 4;
843 case '5': return 5;
844 case '6': return 6;
845 case '7': return 7;
846 case '8': return 8;
847 case '9': return 9;
848 }
849 break;
850 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000851 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000852 return -1;
853 switch (Name[2]) {
854 default: return -1;
855 case '0': return 10;
856 case '1': return 11;
857 case '2': return 12;
858 case '3': return 13;
859 case '4': return 14;
860 case '5': return 15;
861 }
862 break;
863 }
864
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000865 return -1;
866}
867
Jim Grosbachf922c472011-02-12 01:34:40 +0000868/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000869/// token must be an Identifier when called, and if it is a coprocessor
870/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000871ARMAsmParser::OperandMatchResultTy ARMAsmParser::
872tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000873 SMLoc S = Parser.getTok().getLoc();
874 const AsmToken &Tok = Parser.getTok();
875 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
876
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000877 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000878 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000879 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000880
881 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000882 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000883 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000884}
885
Jim Grosbachf922c472011-02-12 01:34:40 +0000886/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000887/// token must be an Identifier when called, and if it is a coprocessor
888/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000889ARMAsmParser::OperandMatchResultTy ARMAsmParser::
890tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000891 SMLoc S = Parser.getTok().getLoc();
892 const AsmToken &Tok = Parser.getTok();
893 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
894
895 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
896 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000897 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000898
899 Parser.Lex(); // Eat identifier token.
900 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000901 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000902}
903
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000904/// Parse a register list, return it if successful else return null. The first
905/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000906bool ARMAsmParser::
907ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000908 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000909 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000910 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000911
Bill Wendling7729e062010-11-09 22:44:22 +0000912 // Read the rest of the registers in the list.
913 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000914 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000915
Bill Wendling7729e062010-11-09 22:44:22 +0000916 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000917 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000918 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000919
Sean Callanan18b83232010-01-19 21:44:56 +0000920 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000921 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000922 if (RegTok.isNot(AsmToken::Identifier)) {
923 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000924 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000925 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000926
Bill Wendling1d6a2652010-11-06 10:40:24 +0000927 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000928 if (RegNum == -1) {
929 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000930 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000931 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000932
Bill Wendlinge7176102010-11-06 22:36:58 +0000933 if (IsRange) {
934 int Reg = PrevRegNum;
935 do {
936 ++Reg;
937 Registers.push_back(std::make_pair(Reg, RegLoc));
938 } while (Reg != RegNum);
939 } else {
940 Registers.push_back(std::make_pair(RegNum, RegLoc));
941 }
942
943 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000944 } while (Parser.getTok().is(AsmToken::Comma) ||
945 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000946
947 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000948 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000949 if (RCurlyTok.isNot(AsmToken::RCurly)) {
950 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000951 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000952 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000953
Bill Wendlinge7176102010-11-06 22:36:58 +0000954 SMLoc E = RCurlyTok.getLoc();
955 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000956
Bill Wendlinge7176102010-11-06 22:36:58 +0000957 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000958 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000959 RI = Registers.begin(), RE = Registers.end();
960
Bill Wendling7caebff2011-01-12 21:20:59 +0000961 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000962 bool EmittedWarning = false;
963
Bill Wendling7caebff2011-01-12 21:20:59 +0000964 DenseMap<unsigned, bool> RegMap;
965 RegMap[HighRegNum] = true;
966
Bill Wendlinge7176102010-11-06 22:36:58 +0000967 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000968 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000969 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000970
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000971 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000972 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000973 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000974 }
975
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000976 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000977 Warning(RegInfo.second,
978 "register not in ascending order in register list");
979
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000980 RegMap[Reg] = true;
981 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000982 }
983
Bill Wendling50d0f582010-11-18 23:43:05 +0000984 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
985 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000986}
987
Jim Grosbachf922c472011-02-12 01:34:40 +0000988/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
989ARMAsmParser::OperandMatchResultTy ARMAsmParser::
990tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000991 SMLoc S = Parser.getTok().getLoc();
992 const AsmToken &Tok = Parser.getTok();
993 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
994 StringRef OptStr = Tok.getString();
995
996 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
997 .Case("sy", ARM_MB::SY)
998 .Case("st", ARM_MB::ST)
999 .Case("ish", ARM_MB::ISH)
1000 .Case("ishst", ARM_MB::ISHST)
1001 .Case("nsh", ARM_MB::NSH)
1002 .Case("nshst", ARM_MB::NSHST)
1003 .Case("osh", ARM_MB::OSH)
1004 .Case("oshst", ARM_MB::OSHST)
1005 .Default(~0U);
1006
1007 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001008 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001009
1010 Parser.Lex(); // Eat identifier token.
1011 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001012 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001013}
1014
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001015/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001016ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1017tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1018 SMLoc S = Parser.getTok().getLoc();
1019 const AsmToken &Tok = Parser.getTok();
1020 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1021 StringRef IFlagsStr = Tok.getString();
1022
1023 unsigned IFlags = 0;
1024 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1025 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1026 .Case("a", ARM_PROC::A)
1027 .Case("i", ARM_PROC::I)
1028 .Case("f", ARM_PROC::F)
1029 .Default(~0U);
1030
1031 // If some specific iflag is already set, it means that some letter is
1032 // present more than once, this is not acceptable.
1033 if (Flag == ~0U || (IFlags & Flag))
1034 return MatchOperand_NoMatch;
1035
1036 IFlags |= Flag;
1037 }
1038
1039 Parser.Lex(); // Eat identifier token.
1040 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1041 return MatchOperand_Success;
1042}
1043
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001044/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1045ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1046tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1047 SMLoc S = Parser.getTok().getLoc();
1048 const AsmToken &Tok = Parser.getTok();
1049 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1050 StringRef Mask = Tok.getString();
1051
1052 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1053 size_t Start = 0, Next = Mask.find('_');
1054 StringRef Flags = "";
1055 StringRef SpecReg = Mask.slice(Start, Next);
1056 if (Next != StringRef::npos)
1057 Flags = Mask.slice(Next+1, Mask.size());
1058
1059 // FlagsVal contains the complete mask:
1060 // 3-0: Mask
1061 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1062 unsigned FlagsVal = 0;
1063
1064 if (SpecReg == "apsr") {
1065 FlagsVal = StringSwitch<unsigned>(Flags)
1066 .Case("nzcvq", 0x8) // same as CPSR_c
1067 .Case("g", 0x4) // same as CPSR_s
1068 .Case("nzcvqg", 0xc) // same as CPSR_fs
1069 .Default(~0U);
1070
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001071 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001072 if (!Flags.empty())
1073 return MatchOperand_NoMatch;
1074 else
1075 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001076 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001077 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1078 for (int i = 0, e = Flags.size(); i != e; ++i) {
1079 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1080 .Case("c", 1)
1081 .Case("x", 2)
1082 .Case("s", 4)
1083 .Case("f", 8)
1084 .Default(~0U);
1085
1086 // If some specific flag is already set, it means that some letter is
1087 // present more than once, this is not acceptable.
1088 if (FlagsVal == ~0U || (FlagsVal & Flag))
1089 return MatchOperand_NoMatch;
1090 FlagsVal |= Flag;
1091 }
1092 } else // No match for special register.
1093 return MatchOperand_NoMatch;
1094
1095 // Special register without flags are equivalent to "fc" flags.
1096 if (!FlagsVal)
1097 FlagsVal = 0x9;
1098
1099 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1100 if (SpecReg == "spsr")
1101 FlagsVal |= 16;
1102
1103 Parser.Lex(); // Eat identifier token.
1104 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1105 return MatchOperand_Success;
1106}
1107
Bill Wendlinge7176102010-11-06 22:36:58 +00001108/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001109/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001110///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001111/// TODO Only preindexing and postindexing addressing are started, unindexed
1112/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001113bool ARMAsmParser::
1114ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00001115 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001116 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001117 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001118 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001119 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001120
Sean Callanan18b83232010-01-19 21:44:56 +00001121 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001122 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1123 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001124 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001125 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001126 int BaseRegNum = TryParseRegister();
1127 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001128 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001129 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001130 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001131
Daniel Dunbar05710932011-01-18 05:34:17 +00001132 // The next token must either be a comma or a closing bracket.
1133 const AsmToken &Tok = Parser.getTok();
1134 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1135 return true;
1136
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001137 bool Preindexed = false;
1138 bool Postindexed = false;
1139 bool OffsetIsReg = false;
1140 bool Negative = false;
1141 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001142 ARMOperand *WBOp = 0;
1143 int OffsetRegNum = -1;
1144 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001145 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001146 const MCExpr *ShiftAmount = 0;
1147 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001148
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001149 // First look for preindexed address forms, that is after the "[Rn" we now
1150 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001151 if (Tok.is(AsmToken::Comma)) {
1152 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001153 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001154
Chris Lattner550276e2010-10-28 20:52:15 +00001155 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1156 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001157 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001158 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001159 if (RBracTok.isNot(AsmToken::RBrac)) {
1160 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001161 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001162 }
Sean Callanan76264762010-04-02 22:27:05 +00001163 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001164 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001165
Sean Callanan18b83232010-01-19 21:44:56 +00001166 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001167 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001168 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1169 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001170 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001171 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001172 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001173 } else {
1174 // The "[Rn" we have so far was not followed by a comma.
1175
Jim Grosbach80eb2332010-10-29 17:41:25 +00001176 // If there's anything other than the right brace, this is a post indexing
1177 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001178 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001179 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001180
Sean Callanan18b83232010-01-19 21:44:56 +00001181 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001182
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001183 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001184 Postindexed = true;
1185 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001186
Chris Lattner550276e2010-10-28 20:52:15 +00001187 if (NextTok.isNot(AsmToken::Comma)) {
1188 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001189 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001190 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001191
Sean Callananb9a25b72010-01-19 20:27:46 +00001192 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001193
Chris Lattner550276e2010-10-28 20:52:15 +00001194 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001195 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001196 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001197 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001198 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001199 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001200
1201 // Force Offset to exist if used.
1202 if (!OffsetIsReg) {
1203 if (!Offset)
1204 Offset = MCConstantExpr::Create(0, getContext());
1205 }
1206
1207 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
1208 OffsetRegNum, OffsetRegShifted,
1209 ShiftType, ShiftAmount, Preindexed,
1210 Postindexed, Negative, Writeback,
1211 S, E));
1212 if (WBOp)
1213 Operands.push_back(WBOp);
1214
1215 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001216}
1217
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001218/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1219/// we will parse the following (were +/- means that a plus or minus is
1220/// optional):
1221/// +/-Rm
1222/// +/-Rm, shift
1223/// #offset
1224/// we return false on success or an error otherwise.
1225bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001226 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001227 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001228 const MCExpr *&ShiftAmount,
1229 const MCExpr *&Offset,
1230 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001231 int &OffsetRegNum,
1232 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001233 Negative = false;
1234 OffsetRegShifted = false;
1235 OffsetIsReg = false;
1236 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001237 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001238 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001239 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001240 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001241 else if (NextTok.is(AsmToken::Minus)) {
1242 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001243 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001244 }
1245 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001246 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001247 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001248 SMLoc CurLoc = OffsetRegTok.getLoc();
1249 OffsetRegNum = TryParseRegister();
1250 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001251 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001252 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001253 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001254 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001255
Bill Wendling12f40e92010-11-06 10:51:53 +00001256 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001257 if (OffsetRegNum != -1) {
1258 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001259 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001260 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001261 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001262
Sean Callanan18b83232010-01-19 21:44:56 +00001263 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001264 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001265 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001266 OffsetRegShifted = true;
1267 }
1268 }
1269 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1270 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001271 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001272 if (HashTok.isNot(AsmToken::Hash))
1273 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001274
Sean Callananb9a25b72010-01-19 20:27:46 +00001275 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001276
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001277 if (getParser().ParseExpression(Offset))
1278 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001279 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001280 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001281 return false;
1282}
1283
1284/// ParseShift as one of these two:
1285/// ( lsl | lsr | asr | ror ) , # shift_amount
1286/// rrx
1287/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001288bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1289 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001290 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001291 if (Tok.isNot(AsmToken::Identifier))
1292 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001293 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001294 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001295 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001296 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001297 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001298 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001299 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001300 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001301 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001302 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001303 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001304 else
1305 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001306 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001307
1308 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001309 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001310 return false;
1311
1312 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001313 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001314 if (HashTok.isNot(AsmToken::Hash))
1315 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001316 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001317
1318 if (getParser().ParseExpression(ShiftAmount))
1319 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001320
1321 return false;
1322}
1323
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001324/// Parse a arm instruction operand. For now this parses the operand regardless
1325/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001326bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001327 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001328 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001329
1330 // Check if the current operand has a custom associated parser, if so, try to
1331 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001332 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1333 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001334 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001335 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1336 // there was a match, but an error occurred, in which case, just return that
1337 // the operand parsing failed.
1338 if (ResTy == MatchOperand_ParseFail)
1339 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001340
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001341 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001342 default:
1343 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001344 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001345 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001346 if (!TryParseRegisterWithWriteBack(Operands))
1347 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001348 if (!TryParseShiftRegister(Operands))
1349 return false;
1350
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001351
1352 // Fall though for the Identifier case that is not a register or a
1353 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001354 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1355 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001356 // This was not a register so parse other operands that start with an
1357 // identifier (like labels) as expressions and create them as immediates.
1358 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001359 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001360 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001361 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001362 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001363 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1364 return false;
1365 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001366 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001367 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001368 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001369 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001370 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001371 // #42 -> immediate.
1372 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001373 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001374 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001375 const MCExpr *ImmVal;
1376 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001377 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001378 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001379 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1380 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001381 case AsmToken::Colon: {
1382 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001383 // FIXME: Check it's an expression prefix,
1384 // e.g. (FOO - :lower16:BAR) isn't legal.
1385 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001386 if (ParsePrefix(RefKind))
1387 return true;
1388
Evan Cheng75972122011-01-13 07:58:56 +00001389 const MCExpr *SubExprVal;
1390 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001391 return true;
1392
Evan Cheng75972122011-01-13 07:58:56 +00001393 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1394 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001395 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001396 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001397 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001398 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001399 }
1400}
1401
Evan Cheng75972122011-01-13 07:58:56 +00001402// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1403// :lower16: and :upper16:.
1404bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1405 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001406
1407 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001408 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001409 Parser.Lex(); // Eat ':'
1410
1411 if (getLexer().isNot(AsmToken::Identifier)) {
1412 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1413 return true;
1414 }
1415
1416 StringRef IDVal = Parser.getTok().getIdentifier();
1417 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001418 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001419 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001420 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001421 } else {
1422 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1423 return true;
1424 }
1425 Parser.Lex();
1426
1427 if (getLexer().isNot(AsmToken::Colon)) {
1428 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1429 return true;
1430 }
1431 Parser.Lex(); // Eat the last ':'
1432 return false;
1433}
1434
1435const MCExpr *
1436ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1437 MCSymbolRefExpr::VariantKind Variant) {
1438 // Recurse over the given expression, rebuilding it to apply the given variant
1439 // to the leftmost symbol.
1440 if (Variant == MCSymbolRefExpr::VK_None)
1441 return E;
1442
1443 switch (E->getKind()) {
1444 case MCExpr::Target:
1445 llvm_unreachable("Can't handle target expr yet");
1446 case MCExpr::Constant:
1447 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1448
1449 case MCExpr::SymbolRef: {
1450 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1451
1452 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1453 return 0;
1454
1455 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1456 }
1457
1458 case MCExpr::Unary:
1459 llvm_unreachable("Can't handle unary expressions yet");
1460
1461 case MCExpr::Binary: {
1462 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1463 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1464 const MCExpr *RHS = BE->getRHS();
1465 if (!LHS)
1466 return 0;
1467
1468 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1469 }
1470 }
1471
1472 assert(0 && "Invalid expression kind!");
1473 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001474}
1475
Daniel Dunbar352e1482011-01-11 15:59:50 +00001476/// \brief Given a mnemonic, split out possible predication code and carry
1477/// setting letters to form a canonical mnemonic and flags.
1478//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001479// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001480static StringRef SplitMnemonic(StringRef Mnemonic,
1481 unsigned &PredicationCode,
1482 bool &CarrySetting,
1483 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001484 PredicationCode = ARMCC::AL;
1485 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001486 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001487
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001488 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001489 //
1490 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001491 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1492 Mnemonic == "movs" ||
1493 Mnemonic == "svc" ||
1494 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1495 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1496 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1497 Mnemonic == "vclt" ||
1498 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1499 Mnemonic == "vcle" ||
1500 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1501 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1502 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001503 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001504
Daniel Dunbar352e1482011-01-11 15:59:50 +00001505 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001506 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001507 .Case("eq", ARMCC::EQ)
1508 .Case("ne", ARMCC::NE)
1509 .Case("hs", ARMCC::HS)
1510 .Case("lo", ARMCC::LO)
1511 .Case("mi", ARMCC::MI)
1512 .Case("pl", ARMCC::PL)
1513 .Case("vs", ARMCC::VS)
1514 .Case("vc", ARMCC::VC)
1515 .Case("hi", ARMCC::HI)
1516 .Case("ls", ARMCC::LS)
1517 .Case("ge", ARMCC::GE)
1518 .Case("lt", ARMCC::LT)
1519 .Case("gt", ARMCC::GT)
1520 .Case("le", ARMCC::LE)
1521 .Case("al", ARMCC::AL)
1522 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001523 if (CC != ~0U) {
1524 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001525 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001526 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001527
Daniel Dunbar352e1482011-01-11 15:59:50 +00001528 // Next, determine if we have a carry setting bit. We explicitly ignore all
1529 // the instructions we know end in 's'.
1530 if (Mnemonic.endswith("s") &&
1531 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1532 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1533 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1534 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1535 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1536 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1537 CarrySetting = true;
1538 }
1539
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001540 // The "cps" instruction can have a interrupt mode operand which is glued into
1541 // the mnemonic. Check if this is the case, split it and parse the imod op
1542 if (Mnemonic.startswith("cps")) {
1543 // Split out any imod code.
1544 unsigned IMod =
1545 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1546 .Case("ie", ARM_PROC::IE)
1547 .Case("id", ARM_PROC::ID)
1548 .Default(~0U);
1549 if (IMod != ~0U) {
1550 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1551 ProcessorIMod = IMod;
1552 }
1553 }
1554
Daniel Dunbar352e1482011-01-11 15:59:50 +00001555 return Mnemonic;
1556}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001557
1558/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1559/// inclusion of carry set or predication code operands.
1560//
1561// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001562void ARMAsmParser::
1563GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1564 bool &CanAcceptPredicationCode) {
1565 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1566
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001567 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1568 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1569 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1570 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1571 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1572 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1573 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1574 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1575 CanAcceptCarrySet = true;
1576 } else {
1577 CanAcceptCarrySet = false;
1578 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001579
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001580 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1581 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1582 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1583 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001584 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001585 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001586 CanAcceptPredicationCode = false;
1587 } else {
1588 CanAcceptPredicationCode = true;
1589 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001590
1591 if (isThumb)
1592 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001593 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001594 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001595}
1596
1597/// Parse an arm instruction mnemonic followed by its operands.
1598bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1599 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1600 // Create the leading tokens for the mnemonic, split by '.' characters.
1601 size_t Start = 0, Next = Name.find('.');
1602 StringRef Head = Name.slice(Start, Next);
1603
Daniel Dunbar352e1482011-01-11 15:59:50 +00001604 // Split out the predication code and carry setting flag from the mnemonic.
1605 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001606 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001607 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001608 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1609 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001610
Chris Lattner3a697562010-10-28 17:20:03 +00001611 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001612
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001613 // Next, add the CCOut and ConditionCode operands, if needed.
1614 //
1615 // For mnemonics which can ever incorporate a carry setting bit or predication
1616 // code, our matching model involves us always generating CCOut and
1617 // ConditionCode operands to match the mnemonic "as written" and then we let
1618 // the matcher deal with finding the right instruction or generating an
1619 // appropriate error.
1620 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1621 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1622
1623 // Add the carry setting operand, if necessary.
1624 //
1625 // FIXME: It would be awesome if we could somehow invent a location such that
1626 // match errors on this operand would print a nice diagnostic about how the
1627 // 's' character in the mnemonic resulted in a CCOut operand.
1628 if (CanAcceptCarrySet) {
1629 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1630 NameLoc));
1631 } else {
1632 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1633 // misspelled another mnemonic).
1634
1635 // FIXME: Issue a nice error.
1636 }
1637
1638 // Add the predication code operand, if necessary.
1639 if (CanAcceptPredicationCode) {
1640 Operands.push_back(ARMOperand::CreateCondCode(
1641 ARMCC::CondCodes(PredicationCode), NameLoc));
1642 } else {
1643 // This mnemonic can't ever accept a predication code, but the user wrote
1644 // one (or misspelled another mnemonic).
1645
1646 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001647 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001648
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001649 // Add the processor imod operand, if necessary.
1650 if (ProcessorIMod) {
1651 Operands.push_back(ARMOperand::CreateImm(
1652 MCConstantExpr::Create(ProcessorIMod, getContext()),
1653 NameLoc, NameLoc));
1654 } else {
1655 // This mnemonic can't ever accept a imod, but the user wrote
1656 // one (or misspelled another mnemonic).
1657
1658 // FIXME: Issue a nice error.
1659 }
1660
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001661 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001662 while (Next != StringRef::npos) {
1663 Start = Next;
1664 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001665 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001666
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001667 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001668 }
1669
1670 // Read the remaining operands.
1671 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001672 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001673 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001674 Parser.EatToEndOfStatement();
1675 return true;
1676 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001677
1678 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001679 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001680
1681 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001682 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001683 Parser.EatToEndOfStatement();
1684 return true;
1685 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001686 }
1687 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001688
Chris Lattnercbf8a982010-09-11 16:18:25 +00001689 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1690 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001691 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001692 }
Bill Wendling146018f2010-11-06 21:42:12 +00001693
Chris Lattner34e53142010-09-08 05:10:46 +00001694 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001695 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001696}
1697
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001698bool ARMAsmParser::
1699MatchAndEmitInstruction(SMLoc IDLoc,
1700 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1701 MCStreamer &Out) {
1702 MCInst Inst;
1703 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001704 MatchResultTy MatchResult, MatchResult2;
1705 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1706 if (MatchResult != Match_Success) {
1707 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1708 // that does not update the condition codes. So try adding a CCOut operand
1709 // with a value of reg0.
1710 if (MatchResult == Match_InvalidOperand) {
1711 Operands.insert(Operands.begin() + 1,
1712 ARMOperand::CreateCCOut(0,
1713 ((ARMOperand*)Operands[0])->getStartLoc()));
1714 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1715 if (MatchResult2 == Match_Success)
1716 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001717 else {
1718 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001719 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001720 delete CCOut;
1721 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001722 }
1723 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1724 // that updates the condition codes if it ends in 's'. So see if the
1725 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1726 // operand with a value of CPSR.
1727 else if(MatchResult == Match_MnemonicFail) {
1728 // Get the instruction mnemonic, which is the first token.
1729 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1730 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1731 // removed the 's' from the mnemonic for matching.
1732 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1733 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001734 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1735 Operands.erase(Operands.begin());
1736 delete OldMnemonic;
1737 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001738 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1739 Operands.insert(Operands.begin() + 1,
1740 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1741 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1742 if (MatchResult2 == Match_Success)
1743 MatchResult = Match_Success;
1744 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001745 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1746 Operands.erase(Operands.begin());
1747 delete OldMnemonic;
1748 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001749 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001750 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1751 Operands.erase(Operands.begin() + 1);
1752 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001753 }
1754 }
1755 }
1756 }
1757 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001758 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001759 Out.EmitInstruction(Inst);
1760 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001761 case Match_MissingFeature:
1762 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1763 return true;
1764 case Match_InvalidOperand: {
1765 SMLoc ErrorLoc = IDLoc;
1766 if (ErrorInfo != ~0U) {
1767 if (ErrorInfo >= Operands.size())
1768 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001769
Chris Lattnere73d4f82010-10-28 21:41:58 +00001770 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1771 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1772 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001773
Chris Lattnere73d4f82010-10-28 21:41:58 +00001774 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001775 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001776 case Match_MnemonicFail:
1777 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001778 case Match_ConversionFail:
1779 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001780 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001781
Eric Christopherc223e2b2010-10-29 09:26:59 +00001782 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001783 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001784}
1785
Kevin Enderby515d5092009-10-15 20:48:48 +00001786/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001787bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1788 StringRef IDVal = DirectiveID.getIdentifier();
1789 if (IDVal == ".word")
1790 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001791 else if (IDVal == ".thumb")
1792 return ParseDirectiveThumb(DirectiveID.getLoc());
1793 else if (IDVal == ".thumb_func")
1794 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1795 else if (IDVal == ".code")
1796 return ParseDirectiveCode(DirectiveID.getLoc());
1797 else if (IDVal == ".syntax")
1798 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001799 return true;
1800}
1801
1802/// ParseDirectiveWord
1803/// ::= .word [ expression (, expression)* ]
1804bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1805 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1806 for (;;) {
1807 const MCExpr *Value;
1808 if (getParser().ParseExpression(Value))
1809 return true;
1810
Chris Lattneraaec2052010-01-19 19:46:13 +00001811 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001812
1813 if (getLexer().is(AsmToken::EndOfStatement))
1814 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001815
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001816 // FIXME: Improve diagnostic.
1817 if (getLexer().isNot(AsmToken::Comma))
1818 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001819 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001820 }
1821 }
1822
Sean Callananb9a25b72010-01-19 20:27:46 +00001823 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001824 return false;
1825}
1826
Kevin Enderby515d5092009-10-15 20:48:48 +00001827/// ParseDirectiveThumb
1828/// ::= .thumb
1829bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1830 if (getLexer().isNot(AsmToken::EndOfStatement))
1831 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001832 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001833
1834 // TODO: set thumb mode
1835 // TODO: tell the MC streamer the mode
1836 // getParser().getStreamer().Emit???();
1837 return false;
1838}
1839
1840/// ParseDirectiveThumbFunc
1841/// ::= .thumbfunc symbol_name
1842bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001843 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001844 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001845 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001846 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001847 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001848 if (getLexer().isNot(AsmToken::EndOfStatement))
1849 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001850 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001851
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001852 // Mark symbol as a thumb symbol.
1853 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1854 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001855 return false;
1856}
1857
1858/// ParseDirectiveSyntax
1859/// ::= .syntax unified | divided
1860bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001861 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001862 if (Tok.isNot(AsmToken::Identifier))
1863 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001864 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001865 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001866 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001867 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001868 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001869 else
1870 return Error(L, "unrecognized syntax mode in .syntax directive");
1871
1872 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001873 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001874 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001875
1876 // TODO tell the MC streamer the mode
1877 // getParser().getStreamer().Emit???();
1878 return false;
1879}
1880
1881/// ParseDirectiveCode
1882/// ::= .code 16 | 32
1883bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001884 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001885 if (Tok.isNot(AsmToken::Integer))
1886 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001887 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001888 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001889 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001890 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001891 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001892 else
1893 return Error(L, "invalid operand to .code directive");
1894
1895 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001896 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001897 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001898
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001899 // FIXME: We need to be able switch subtargets at this point so that
1900 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1901 // includes Feature_IsThumb or not to match the right instructions. This is
1902 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1903 if (Val == 16){
1904 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1905 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001906 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001907 }
1908 else{
1909 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1910 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001911 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001912 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001913
Kevin Enderby515d5092009-10-15 20:48:48 +00001914 return false;
1915}
1916
Sean Callanan90b70972010-04-07 20:29:34 +00001917extern "C" void LLVMInitializeARMAsmLexer();
1918
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001919/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001920extern "C" void LLVMInitializeARMAsmParser() {
1921 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1922 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001923 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001924}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001925
Chris Lattner0692ee62010-09-06 19:11:01 +00001926#define GET_REGISTER_MATCHER
1927#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001928#include "ARMGenAsmMatcher.inc"