blob: d4735966256f1e31069f1327071ad5fb09cebb46 [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*> &);
Bruno Cardoso Lopesb41aaab2011-03-31 15:54:36 +000051 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 /// @{
Bruno Cardoso Lopesb41aaab2011-03-31 15:54:36 +0000296
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000297 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 }
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000353 bool isMemMode7() const {
354 if (!isMemory() ||
355 getMemPreindexed() ||
356 getMemPostindexed() ||
357 getMemOffsetIsReg() ||
358 getMemNegative() ||
359 getMemWriteback())
360 return false;
361
362 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
363 if (!CE) return false;
364
365 if (CE->getValue())
366 return false;
367
368 return true;
369 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000370 bool isMemModeRegThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000371 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingf4caf692010-12-14 03:36:38 +0000372 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000373 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000374 }
375 bool isMemModeImmThumb() const {
Daniel Dunbar4b462672011-01-18 05:55:27 +0000376 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000377 return false;
378
Daniel Dunbar4b462672011-01-18 05:55:27 +0000379 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000380 if (!CE) return false;
381
382 // The offset must be a multiple of 4 in the range 0-124.
383 uint64_t Value = CE->getValue();
384 return ((Value & 0x3) == 0 && Value <= 124);
385 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000386 bool isMSRMask() const { return Kind == MSRMask; }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000387 bool isProcIFlags() const { return Kind == ProcIFlags; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000388
389 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000390 // Add as immediates when possible. Null MCExpr = 0.
391 if (Expr == 0)
392 Inst.addOperand(MCOperand::CreateImm(0));
393 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000394 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
395 else
396 Inst.addOperand(MCOperand::CreateExpr(Expr));
397 }
398
Daniel Dunbar8462b302010-08-11 06:36:53 +0000399 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000400 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000401 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000402 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
403 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000404 }
405
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000406 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
407 assert(N == 1 && "Invalid number of operands!");
408 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
409 }
410
411 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
412 assert(N == 1 && "Invalid number of operands!");
413 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
414 }
415
Jim Grosbachd67641b2010-12-06 18:21:12 +0000416 void addCCOutOperands(MCInst &Inst, unsigned N) const {
417 assert(N == 1 && "Invalid number of operands!");
418 Inst.addOperand(MCOperand::CreateReg(getReg()));
419 }
420
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000421 void addRegOperands(MCInst &Inst, unsigned N) const {
422 assert(N == 1 && "Invalid number of operands!");
423 Inst.addOperand(MCOperand::CreateReg(getReg()));
424 }
425
Owen Anderson00828302011-03-18 22:50:18 +0000426 void addShifterOperands(MCInst &Inst, unsigned N) const {
427 assert(N == 1 && "Invalid number of operands!");
428 Inst.addOperand(MCOperand::CreateImm(
429 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
430 }
431
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000432 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000433 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000434 const SmallVectorImpl<unsigned> &RegList = getRegList();
435 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000436 I = RegList.begin(), E = RegList.end(); I != E; ++I)
437 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000438 }
439
Bill Wendling0f630752010-11-17 04:32:08 +0000440 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
441 addRegListOperands(Inst, N);
442 }
443
444 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
445 addRegListOperands(Inst, N);
446 }
447
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000448 void addImmOperands(MCInst &Inst, unsigned N) const {
449 assert(N == 1 && "Invalid number of operands!");
450 addExpr(Inst, getImm());
451 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000452
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000453 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
454 assert(N == 1 && "Invalid number of operands!");
455 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
456 }
457
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000458 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
459 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
460 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
461
462 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Matt Beaumont-Gay1866af42011-03-24 22:05:48 +0000463 (void)CE;
Bruno Cardoso Lopes505f3cd2011-03-24 21:04:58 +0000464 assert((CE || CE->getValue() == 0) &&
465 "No offset operand support in mode 7");
466 }
467
Chris Lattner14b93852010-10-29 00:27:31 +0000468 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
469 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000470
Daniel Dunbar4b462672011-01-18 05:55:27 +0000471 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
472 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000473
Jim Grosbach80eb2332010-10-29 17:41:25 +0000474 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
475 // the difference?
Daniel Dunbar4b462672011-01-18 05:55:27 +0000476 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000477 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000478
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000479 // The MCInst offset operand doesn't include the low two bits (like
480 // the instruction encoding).
481 int64_t Offset = CE->getValue() / 4;
482 if (Offset >= 0)
483 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
484 Offset)));
485 else
486 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
487 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000488 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000489
Bill Wendlingf4caf692010-12-14 03:36:38 +0000490 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
491 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000492 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
493 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000494 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000495
Bill Wendlingf4caf692010-12-14 03:36:38 +0000496 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
497 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
Daniel Dunbar4b462672011-01-18 05:55:27 +0000498 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
499 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
Bill Wendlingf4caf692010-12-14 03:36:38 +0000500 assert(CE && "Non-constant mode offset operand!");
501 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000502 }
503
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000504 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
505 assert(N == 1 && "Invalid number of operands!");
506 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
507 }
508
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000509 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
510 assert(N == 1 && "Invalid number of operands!");
511 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
512 }
513
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000514 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000515
Chris Lattner3a697562010-10-28 17:20:03 +0000516 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
517 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000518 Op->CC.Val = CC;
519 Op->StartLoc = S;
520 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000521 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000522 }
523
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000524 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
525 ARMOperand *Op = new ARMOperand(CoprocNum);
526 Op->Cop.Val = CopVal;
527 Op->StartLoc = S;
528 Op->EndLoc = S;
529 return Op;
530 }
531
532 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
533 ARMOperand *Op = new ARMOperand(CoprocReg);
534 Op->Cop.Val = CopVal;
535 Op->StartLoc = S;
536 Op->EndLoc = S;
537 return Op;
538 }
539
Jim Grosbachd67641b2010-12-06 18:21:12 +0000540 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
541 ARMOperand *Op = new ARMOperand(CCOut);
542 Op->Reg.RegNum = RegNum;
543 Op->StartLoc = S;
544 Op->EndLoc = S;
545 return Op;
546 }
547
Chris Lattner3a697562010-10-28 17:20:03 +0000548 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
549 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000550 Op->Tok.Data = Str.data();
551 Op->Tok.Length = Str.size();
552 Op->StartLoc = S;
553 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000554 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000555 }
556
Bill Wendling50d0f582010-11-18 23:43:05 +0000557 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000558 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000559 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000560 Op->StartLoc = S;
561 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000562 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000563 }
564
Owen Anderson00828302011-03-18 22:50:18 +0000565 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
566 SMLoc S, SMLoc E) {
567 ARMOperand *Op = new ARMOperand(Shifter);
568 Op->Shift.ShiftTy = ShTy;
569 Op->StartLoc = S;
570 Op->EndLoc = E;
571 return Op;
572 }
573
Bill Wendling7729e062010-11-09 22:44:22 +0000574 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000575 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000576 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000577 KindTy Kind = RegisterList;
578
579 if (ARM::DPRRegClass.contains(Regs.front().first))
580 Kind = DPRRegisterList;
581 else if (ARM::SPRRegClass.contains(Regs.front().first))
582 Kind = SPRRegisterList;
583
584 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000585 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000586 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000587 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000588 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000589 Op->StartLoc = StartLoc;
590 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000591 return Op;
592 }
593
Chris Lattner3a697562010-10-28 17:20:03 +0000594 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
595 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000596 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000597 Op->StartLoc = S;
598 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000599 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000600 }
601
Bruno Cardoso Lopesb41aaab2011-03-31 15:54:36 +0000602 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
603 const MCExpr *Offset, int OffsetRegNum,
604 bool OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +0000605 enum ARM_AM::ShiftOpc ShiftType,
Chris Lattner3a697562010-10-28 17:20:03 +0000606 const MCExpr *ShiftAmount, bool Preindexed,
607 bool Postindexed, bool Negative, bool Writeback,
608 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000609 assert((OffsetRegNum == -1 || OffsetIsReg) &&
610 "OffsetRegNum must imply OffsetIsReg!");
611 assert((!OffsetRegShifted || OffsetIsReg) &&
612 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000613 assert((Offset || OffsetIsReg) &&
614 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000615 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
616 "Cannot have shift amount without shifted register offset!");
617 assert((!Offset || !OffsetIsReg) &&
618 "Cannot have expression offset and register offset!");
619
Chris Lattner3a697562010-10-28 17:20:03 +0000620 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000621 Op->Mem.BaseRegNum = BaseRegNum;
622 Op->Mem.OffsetIsReg = OffsetIsReg;
Daniel Dunbar2637dc92011-01-18 05:55:15 +0000623 if (OffsetIsReg)
624 Op->Mem.Offset.RegNum = OffsetRegNum;
625 else
626 Op->Mem.Offset.Value = Offset;
Sean Callanan76264762010-04-02 22:27:05 +0000627 Op->Mem.OffsetRegShifted = OffsetRegShifted;
628 Op->Mem.ShiftType = ShiftType;
629 Op->Mem.ShiftAmount = ShiftAmount;
630 Op->Mem.Preindexed = Preindexed;
631 Op->Mem.Postindexed = Postindexed;
632 Op->Mem.Negative = Negative;
633 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000634
Sean Callanan76264762010-04-02 22:27:05 +0000635 Op->StartLoc = S;
636 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000637 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000638 }
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000639
640 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
641 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
642 Op->MBOpt.Val = Opt;
643 Op->StartLoc = S;
644 Op->EndLoc = S;
645 return Op;
646 }
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000647
648 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
649 ARMOperand *Op = new ARMOperand(ProcIFlags);
650 Op->IFlags.Val = IFlags;
651 Op->StartLoc = S;
652 Op->EndLoc = S;
653 return Op;
654 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000655
656 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
657 ARMOperand *Op = new ARMOperand(MSRMask);
658 Op->MMask.Val = MMask;
659 Op->StartLoc = S;
660 Op->EndLoc = S;
661 return Op;
662 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000663};
664
665} // end anonymous namespace.
666
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000667void ARMOperand::dump(raw_ostream &OS) const {
668 switch (Kind) {
669 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000670 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000671 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000672 case CCOut:
673 OS << "<ccout " << getReg() << ">";
674 break;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000675 case CoprocNum:
676 OS << "<coprocessor number: " << getCoproc() << ">";
677 break;
678 case CoprocReg:
679 OS << "<coprocessor register: " << getCoproc() << ">";
680 break;
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +0000681 case MSRMask:
682 OS << "<mask: " << getMSRMask() << ">";
683 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000684 case Immediate:
685 getImm()->print(OS);
686 break;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +0000687 case MemBarrierOpt:
688 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
689 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000690 case Memory:
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000691 OS << "<memory "
Bruno Cardoso Lopesb41aaab2011-03-31 15:54:36 +0000692 << "base:" << getMemBaseRegNum();
Daniel Dunbar6ec56202011-01-18 05:55:21 +0000693 if (getMemOffsetIsReg()) {
694 OS << " offset:<register " << getMemOffsetRegNum();
695 if (getMemOffsetRegShifted()) {
696 OS << " offset-shift-type:" << getMemShiftType();
697 OS << " offset-shift-amount:" << *getMemShiftAmount();
698 }
699 } else {
700 OS << " offset:" << *getMemOffset();
701 }
702 if (getMemOffsetIsReg())
703 OS << " (offset-is-reg)";
704 if (getMemPreindexed())
705 OS << " (pre-indexed)";
706 if (getMemPostindexed())
707 OS << " (post-indexed)";
708 if (getMemNegative())
709 OS << " (negative)";
710 if (getMemWriteback())
711 OS << " (writeback)";
712 OS << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000713 break;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +0000714 case ProcIFlags: {
715 OS << "<ARM_PROC::";
716 unsigned IFlags = getProcIFlags();
717 for (int i=2; i >= 0; --i)
718 if (IFlags & (1 << i))
719 OS << ARM_PROC::IFlagsToString(1 << i);
720 OS << ">";
721 break;
722 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000723 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000724 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000725 break;
Owen Anderson00828302011-03-18 22:50:18 +0000726 case Shifter:
727 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
728 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000729 case RegisterList:
730 case DPRRegisterList:
731 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000732 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000733
Bill Wendling5fa22a12010-11-09 23:28:44 +0000734 const SmallVectorImpl<unsigned> &RegList = getRegList();
735 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000736 I = RegList.begin(), E = RegList.end(); I != E; ) {
737 OS << *I;
738 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000739 }
740
741 OS << ">";
742 break;
743 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000744 case Token:
745 OS << "'" << getToken() << "'";
746 break;
747 }
748}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000749
750/// @name Auto-generated Match Functions
751/// {
752
753static unsigned MatchRegisterName(StringRef Name);
754
755/// }
756
Bob Wilson69df7232011-02-03 21:46:10 +0000757bool ARMAsmParser::ParseRegister(unsigned &RegNo,
758 SMLoc &StartLoc, SMLoc &EndLoc) {
Roman Divackybf755322011-01-27 17:14:22 +0000759 RegNo = TryParseRegister();
760
761 return (RegNo == (unsigned)-1);
762}
763
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000764/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000765/// and if it is a register name the token is eaten and the register number is
766/// returned. Otherwise return -1.
767///
768int ARMAsmParser::TryParseRegister() {
769 const AsmToken &Tok = Parser.getTok();
770 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000771
Chris Lattnere5658fa2010-10-30 04:09:10 +0000772 // FIXME: Validate register for the current architecture; we have to do
773 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000774 std::string upperCase = Tok.getString().str();
775 std::string lowerCase = LowercaseString(upperCase);
776 unsigned RegNum = MatchRegisterName(lowerCase);
777 if (!RegNum) {
778 RegNum = StringSwitch<unsigned>(lowerCase)
779 .Case("r13", ARM::SP)
780 .Case("r14", ARM::LR)
781 .Case("r15", ARM::PC)
782 .Case("ip", ARM::R12)
783 .Default(0);
784 }
785 if (!RegNum) return -1;
Bob Wilson69df7232011-02-03 21:46:10 +0000786
Chris Lattnere5658fa2010-10-30 04:09:10 +0000787 Parser.Lex(); // Eat identifier token.
788 return RegNum;
789}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000790
Owen Anderson00828302011-03-18 22:50:18 +0000791/// Try to parse a register name. The token must be an Identifier when called,
792/// and if it is a register name the token is eaten and the register number is
793/// returned. Otherwise return -1.
794///
795bool ARMAsmParser::TryParseShiftRegister(
796 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
797 SMLoc S = Parser.getTok().getLoc();
798 const AsmToken &Tok = Parser.getTok();
799 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
800
801 std::string upperCase = Tok.getString().str();
802 std::string lowerCase = LowercaseString(upperCase);
803 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
804 .Case("lsl", ARM_AM::lsl)
805 .Case("lsr", ARM_AM::lsr)
806 .Case("asr", ARM_AM::asr)
807 .Case("ror", ARM_AM::ror)
808 .Case("rrx", ARM_AM::rrx)
809 .Default(ARM_AM::no_shift);
810
811 if (ShiftTy == ARM_AM::no_shift)
812 return true;
813
814 Parser.Lex(); // Eat shift-type operand;
815 int RegNum = TryParseRegister();
816 if (RegNum == -1)
817 return Error(Parser.getTok().getLoc(), "register expected");
818
819 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
820 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
821 S, Parser.getTok().getLoc()));
822
823 return false;
824}
825
826
Bill Wendling50d0f582010-11-18 23:43:05 +0000827/// Try to parse a register name. The token must be an Identifier when called.
828/// If it's a register, an AsmOperand is created. Another AsmOperand is created
829/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000830///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000831/// TODO this is likely to change to allow different register types and or to
832/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000833bool ARMAsmParser::
834TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000835 SMLoc S = Parser.getTok().getLoc();
836 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000837 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000838 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000839
Bill Wendling50d0f582010-11-18 23:43:05 +0000840 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000841
Chris Lattnere5658fa2010-10-30 04:09:10 +0000842 const AsmToken &ExclaimTok = Parser.getTok();
843 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000844 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
845 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000846 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000847 }
848
Bill Wendling50d0f582010-11-18 23:43:05 +0000849 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000850}
851
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000852/// MatchCoprocessorOperandName - Try to parse an coprocessor related
853/// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
854/// "c5", ...
855static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000856 // Use the same layout as the tablegen'erated register name matcher. Ugly,
857 // but efficient.
858 switch (Name.size()) {
859 default: break;
860 case 2:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000861 if (Name[0] != CoprocOp)
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000862 return -1;
863 switch (Name[1]) {
864 default: return -1;
865 case '0': return 0;
866 case '1': return 1;
867 case '2': return 2;
868 case '3': return 3;
869 case '4': return 4;
870 case '5': return 5;
871 case '6': return 6;
872 case '7': return 7;
873 case '8': return 8;
874 case '9': return 9;
875 }
876 break;
877 case 3:
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000878 if (Name[0] != CoprocOp || Name[1] != '1')
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000879 return -1;
880 switch (Name[2]) {
881 default: return -1;
882 case '0': return 10;
883 case '1': return 11;
884 case '2': return 12;
885 case '3': return 13;
886 case '4': return 14;
887 case '5': return 15;
888 }
889 break;
890 }
891
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000892 return -1;
893}
894
Jim Grosbachf922c472011-02-12 01:34:40 +0000895/// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000896/// token must be an Identifier when called, and if it is a coprocessor
897/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000898ARMAsmParser::OperandMatchResultTy ARMAsmParser::
899tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000900 SMLoc S = Parser.getTok().getLoc();
901 const AsmToken &Tok = Parser.getTok();
902 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
903
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000904 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000905 if (Num == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000906 return MatchOperand_NoMatch;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000907
908 Parser.Lex(); // Eat identifier token.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000909 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000910 return MatchOperand_Success;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000911}
912
Jim Grosbachf922c472011-02-12 01:34:40 +0000913/// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000914/// token must be an Identifier when called, and if it is a coprocessor
915/// number, the token is eaten and the operand is added to the operand list.
Jim Grosbachf922c472011-02-12 01:34:40 +0000916ARMAsmParser::OperandMatchResultTy ARMAsmParser::
917tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000918 SMLoc S = Parser.getTok().getLoc();
919 const AsmToken &Tok = Parser.getTok();
920 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
921
922 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
923 if (Reg == -1)
Jim Grosbachf922c472011-02-12 01:34:40 +0000924 return MatchOperand_NoMatch;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +0000925
926 Parser.Lex(); // Eat identifier token.
927 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
Jim Grosbachf922c472011-02-12 01:34:40 +0000928 return MatchOperand_Success;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000929}
930
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000931/// Parse a register list, return it if successful else return null. The first
932/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000933bool ARMAsmParser::
934ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000935 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000936 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000937 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000938
Bill Wendling7729e062010-11-09 22:44:22 +0000939 // Read the rest of the registers in the list.
940 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000941 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000942
Bill Wendling7729e062010-11-09 22:44:22 +0000943 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000944 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000945 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000946
Sean Callanan18b83232010-01-19 21:44:56 +0000947 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000948 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000949 if (RegTok.isNot(AsmToken::Identifier)) {
950 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000951 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000952 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000953
Bill Wendling1d6a2652010-11-06 10:40:24 +0000954 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000955 if (RegNum == -1) {
956 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000957 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000958 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000959
Bill Wendlinge7176102010-11-06 22:36:58 +0000960 if (IsRange) {
961 int Reg = PrevRegNum;
962 do {
963 ++Reg;
964 Registers.push_back(std::make_pair(Reg, RegLoc));
965 } while (Reg != RegNum);
966 } else {
967 Registers.push_back(std::make_pair(RegNum, RegLoc));
968 }
969
970 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000971 } while (Parser.getTok().is(AsmToken::Comma) ||
972 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000973
974 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000975 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000976 if (RCurlyTok.isNot(AsmToken::RCurly)) {
977 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000978 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000979 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000980
Bill Wendlinge7176102010-11-06 22:36:58 +0000981 SMLoc E = RCurlyTok.getLoc();
982 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000983
Bill Wendlinge7176102010-11-06 22:36:58 +0000984 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000985 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000986 RI = Registers.begin(), RE = Registers.end();
987
Bill Wendling7caebff2011-01-12 21:20:59 +0000988 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000989 bool EmittedWarning = false;
990
Bill Wendling7caebff2011-01-12 21:20:59 +0000991 DenseMap<unsigned, bool> RegMap;
992 RegMap[HighRegNum] = true;
993
Bill Wendlinge7176102010-11-06 22:36:58 +0000994 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000995 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000996 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000997
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000998 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000999 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +00001000 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +00001001 }
1002
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001003 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +00001004 Warning(RegInfo.second,
1005 "register not in ascending order in register list");
1006
Bill Wendling8e8b18b2010-11-09 23:45:59 +00001007 RegMap[Reg] = true;
1008 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +00001009 }
1010
Bill Wendling50d0f582010-11-18 23:43:05 +00001011 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1012 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001013}
1014
Jim Grosbachf922c472011-02-12 01:34:40 +00001015/// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1016ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1017tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001018 SMLoc S = Parser.getTok().getLoc();
1019 const AsmToken &Tok = Parser.getTok();
1020 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1021 StringRef OptStr = Tok.getString();
1022
1023 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1024 .Case("sy", ARM_MB::SY)
1025 .Case("st", ARM_MB::ST)
1026 .Case("ish", ARM_MB::ISH)
1027 .Case("ishst", ARM_MB::ISHST)
1028 .Case("nsh", ARM_MB::NSH)
1029 .Case("nshst", ARM_MB::NSHST)
1030 .Case("osh", ARM_MB::OSH)
1031 .Case("oshst", ARM_MB::OSHST)
1032 .Default(~0U);
1033
1034 if (Opt == ~0U)
Jim Grosbachf922c472011-02-12 01:34:40 +00001035 return MatchOperand_NoMatch;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001036
1037 Parser.Lex(); // Eat identifier token.
1038 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
Jim Grosbachf922c472011-02-12 01:34:40 +00001039 return MatchOperand_Success;
Bruno Cardoso Lopes706d9462011-02-07 22:09:15 +00001040}
1041
Bruno Cardoso Lopes8bba1a52011-02-18 19:49:06 +00001042/// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001043ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1044tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1045 SMLoc S = Parser.getTok().getLoc();
1046 const AsmToken &Tok = Parser.getTok();
1047 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1048 StringRef IFlagsStr = Tok.getString();
1049
1050 unsigned IFlags = 0;
1051 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1052 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1053 .Case("a", ARM_PROC::A)
1054 .Case("i", ARM_PROC::I)
1055 .Case("f", ARM_PROC::F)
1056 .Default(~0U);
1057
1058 // If some specific iflag is already set, it means that some letter is
1059 // present more than once, this is not acceptable.
1060 if (Flag == ~0U || (IFlags & Flag))
1061 return MatchOperand_NoMatch;
1062
1063 IFlags |= Flag;
1064 }
1065
1066 Parser.Lex(); // Eat identifier token.
1067 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1068 return MatchOperand_Success;
1069}
1070
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001071/// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1072ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1073tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1074 SMLoc S = Parser.getTok().getLoc();
1075 const AsmToken &Tok = Parser.getTok();
1076 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1077 StringRef Mask = Tok.getString();
1078
1079 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1080 size_t Start = 0, Next = Mask.find('_');
1081 StringRef Flags = "";
1082 StringRef SpecReg = Mask.slice(Start, Next);
1083 if (Next != StringRef::npos)
1084 Flags = Mask.slice(Next+1, Mask.size());
1085
1086 // FlagsVal contains the complete mask:
1087 // 3-0: Mask
1088 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1089 unsigned FlagsVal = 0;
1090
1091 if (SpecReg == "apsr") {
1092 FlagsVal = StringSwitch<unsigned>(Flags)
1093 .Case("nzcvq", 0x8) // same as CPSR_c
1094 .Case("g", 0x4) // same as CPSR_s
1095 .Case("nzcvqg", 0xc) // same as CPSR_fs
1096 .Default(~0U);
1097
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001098 if (FlagsVal == ~0U) {
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001099 if (!Flags.empty())
1100 return MatchOperand_NoMatch;
1101 else
1102 FlagsVal = 0; // No flag
Joerg Sonnenberger4b19c982011-02-19 00:43:45 +00001103 }
Bruno Cardoso Lopes584bf7b2011-02-18 19:45:59 +00001104 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1105 for (int i = 0, e = Flags.size(); i != e; ++i) {
1106 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1107 .Case("c", 1)
1108 .Case("x", 2)
1109 .Case("s", 4)
1110 .Case("f", 8)
1111 .Default(~0U);
1112
1113 // If some specific flag is already set, it means that some letter is
1114 // present more than once, this is not acceptable.
1115 if (FlagsVal == ~0U || (FlagsVal & Flag))
1116 return MatchOperand_NoMatch;
1117 FlagsVal |= Flag;
1118 }
1119 } else // No match for special register.
1120 return MatchOperand_NoMatch;
1121
1122 // Special register without flags are equivalent to "fc" flags.
1123 if (!FlagsVal)
1124 FlagsVal = 0x9;
1125
1126 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1127 if (SpecReg == "spsr")
1128 FlagsVal |= 16;
1129
1130 Parser.Lex(); // Eat identifier token.
1131 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1132 return MatchOperand_Success;
1133}
1134
Bill Wendlinge7176102010-11-06 22:36:58 +00001135/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001136/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +00001137///
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001138/// TODO Only preindexing and postindexing addressing are started, unindexed
1139/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +00001140bool ARMAsmParser::
Bruno Cardoso Lopesb41aaab2011-03-31 15:54:36 +00001141ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +00001142 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +00001143 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +00001144 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +00001145 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001146 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001147
Sean Callanan18b83232010-01-19 21:44:56 +00001148 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001149 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1150 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001151 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001152 }
Chris Lattnere5658fa2010-10-30 04:09:10 +00001153 int BaseRegNum = TryParseRegister();
1154 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001155 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001156 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001157 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001158
Daniel Dunbar05710932011-01-18 05:34:17 +00001159 // The next token must either be a comma or a closing bracket.
1160 const AsmToken &Tok = Parser.getTok();
1161 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1162 return true;
1163
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001164 bool Preindexed = false;
1165 bool Postindexed = false;
1166 bool OffsetIsReg = false;
1167 bool Negative = false;
1168 bool Writeback = false;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001169 ARMOperand *WBOp = 0;
1170 int OffsetRegNum = -1;
1171 bool OffsetRegShifted = false;
Owen Anderson00828302011-03-18 22:50:18 +00001172 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001173 const MCExpr *ShiftAmount = 0;
1174 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001175
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001176 // First look for preindexed address forms, that is after the "[Rn" we now
1177 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001178 if (Tok.is(AsmToken::Comma)) {
1179 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001180 Parser.Lex(); // Eat comma token.
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001181
Chris Lattner550276e2010-10-28 20:52:15 +00001182 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1183 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001184 return true;
Sean Callanan18b83232010-01-19 21:44:56 +00001185 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +00001186 if (RBracTok.isNot(AsmToken::RBrac)) {
1187 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001188 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001189 }
Sean Callanan76264762010-04-02 22:27:05 +00001190 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001191 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001192
Sean Callanan18b83232010-01-19 21:44:56 +00001193 const AsmToken &ExclaimTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001194 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +00001195 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1196 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001197 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001198 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001199 }
Daniel Dunbar05710932011-01-18 05:34:17 +00001200 } else {
1201 // The "[Rn" we have so far was not followed by a comma.
1202
Jim Grosbach80eb2332010-10-29 17:41:25 +00001203 // If there's anything other than the right brace, this is a post indexing
1204 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +00001205 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001206 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001207
Sean Callanan18b83232010-01-19 21:44:56 +00001208 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +00001209
Kevin Enderbye2a98dd2009-10-15 21:42:45 +00001210 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +00001211 Postindexed = true;
1212 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +00001213
Chris Lattner550276e2010-10-28 20:52:15 +00001214 if (NextTok.isNot(AsmToken::Comma)) {
1215 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +00001216 return true;
Chris Lattner550276e2010-10-28 20:52:15 +00001217 }
Bill Wendling50d0f582010-11-18 23:43:05 +00001218
Sean Callananb9a25b72010-01-19 20:27:46 +00001219 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +00001220
Chris Lattner550276e2010-10-28 20:52:15 +00001221 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +00001222 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +00001223 E))
Bill Wendling50d0f582010-11-18 23:43:05 +00001224 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001225 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001226 }
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001227
1228 // Force Offset to exist if used.
1229 if (!OffsetIsReg) {
1230 if (!Offset)
1231 Offset = MCConstantExpr::Create(0, getContext());
1232 }
1233
Bruno Cardoso Lopesb41aaab2011-03-31 15:54:36 +00001234 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
1235 OffsetRegNum, OffsetRegShifted,
1236 ShiftType, ShiftAmount, Preindexed,
1237 Postindexed, Negative, Writeback,
1238 S, E));
Daniel Dunbar05d8b712011-01-18 05:34:24 +00001239 if (WBOp)
1240 Operands.push_back(WBOp);
1241
1242 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001243}
1244
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001245/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1246/// we will parse the following (were +/- means that a plus or minus is
1247/// optional):
1248/// +/-Rm
1249/// +/-Rm, shift
1250/// #offset
1251/// we return false on success or an error otherwise.
1252bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +00001253 bool &OffsetRegShifted,
Owen Anderson00828302011-03-18 22:50:18 +00001254 enum ARM_AM::ShiftOpc &ShiftType,
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001255 const MCExpr *&ShiftAmount,
1256 const MCExpr *&Offset,
1257 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +00001258 int &OffsetRegNum,
1259 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001260 Negative = false;
1261 OffsetRegShifted = false;
1262 OffsetIsReg = false;
1263 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +00001264 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001265 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001266 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +00001267 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001268 else if (NextTok.is(AsmToken::Minus)) {
1269 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001270 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001271 }
1272 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +00001273 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001274 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +00001275 SMLoc CurLoc = OffsetRegTok.getLoc();
1276 OffsetRegNum = TryParseRegister();
1277 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +00001278 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +00001279 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +00001280 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001281 }
Jim Grosbachd4462a52010-11-01 16:44:21 +00001282
Bill Wendling12f40e92010-11-06 10:51:53 +00001283 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001284 if (OffsetRegNum != -1) {
1285 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +00001286 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001287 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001288 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001289
Sean Callanan18b83232010-01-19 21:44:56 +00001290 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +00001291 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +00001292 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001293 OffsetRegShifted = true;
1294 }
1295 }
1296 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1297 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +00001298 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001299 if (HashTok.isNot(AsmToken::Hash))
1300 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +00001301
Sean Callananb9a25b72010-01-19 20:27:46 +00001302 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001303
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001304 if (getParser().ParseExpression(Offset))
1305 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001306 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001307 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001308 return false;
1309}
1310
1311/// ParseShift as one of these two:
1312/// ( lsl | lsr | asr | ror ) , # shift_amount
1313/// rrx
1314/// and returns true if it parses a shift otherwise it returns false.
Owen Anderson00828302011-03-18 22:50:18 +00001315bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1316 const MCExpr *&ShiftAmount, SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +00001317 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001318 if (Tok.isNot(AsmToken::Identifier))
1319 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +00001320 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001321 if (ShiftName == "lsl" || ShiftName == "LSL")
Owen Anderson00828302011-03-18 22:50:18 +00001322 St = ARM_AM::lsl;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001323 else if (ShiftName == "lsr" || ShiftName == "LSR")
Owen Anderson00828302011-03-18 22:50:18 +00001324 St = ARM_AM::lsr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001325 else if (ShiftName == "asr" || ShiftName == "ASR")
Owen Anderson00828302011-03-18 22:50:18 +00001326 St = ARM_AM::asr;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001327 else if (ShiftName == "ror" || ShiftName == "ROR")
Owen Anderson00828302011-03-18 22:50:18 +00001328 St = ARM_AM::ror;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001329 else if (ShiftName == "rrx" || ShiftName == "RRX")
Owen Anderson00828302011-03-18 22:50:18 +00001330 St = ARM_AM::rrx;
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001331 else
1332 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +00001333 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001334
1335 // Rrx stands alone.
Owen Anderson00828302011-03-18 22:50:18 +00001336 if (St == ARM_AM::rrx)
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001337 return false;
1338
1339 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +00001340 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001341 if (HashTok.isNot(AsmToken::Hash))
1342 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +00001343 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001344
1345 if (getParser().ParseExpression(ShiftAmount))
1346 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001347
1348 return false;
1349}
1350
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001351/// Parse a arm instruction operand. For now this parses the operand regardless
1352/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001353bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001354 StringRef Mnemonic) {
Sean Callanan76264762010-04-02 22:27:05 +00001355 SMLoc S, E;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001356
1357 // Check if the current operand has a custom associated parser, if so, try to
1358 // custom parse the operand, or fallback to the general approach.
Jim Grosbachf922c472011-02-12 01:34:40 +00001359 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1360 if (ResTy == MatchOperand_Success)
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001361 return false;
Jim Grosbachf922c472011-02-12 01:34:40 +00001362 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1363 // there was a match, but an error occurred, in which case, just return that
1364 // the operand parsing failed.
1365 if (ResTy == MatchOperand_ParseFail)
1366 return true;
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001367
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001368 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +00001369 default:
1370 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +00001371 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +00001372 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +00001373 if (!TryParseRegisterWithWriteBack(Operands))
1374 return false;
Owen Anderson00828302011-03-18 22:50:18 +00001375 if (!TryParseShiftRegister(Operands))
1376 return false;
1377
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001378
1379 // Fall though for the Identifier case that is not a register or a
1380 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +00001381 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1382 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +00001383 // This was not a register so parse other operands that start with an
1384 // identifier (like labels) as expressions and create them as immediates.
1385 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +00001386 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +00001387 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001388 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001389 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001390 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1391 return false;
1392 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001393 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +00001394 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001395 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +00001396 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +00001397 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +00001398 // #42 -> immediate.
1399 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +00001400 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +00001401 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001402 const MCExpr *ImmVal;
1403 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +00001404 return true;
Sean Callanan76264762010-04-02 22:27:05 +00001405 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +00001406 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1407 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001408 case AsmToken::Colon: {
1409 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +00001410 // FIXME: Check it's an expression prefix,
1411 // e.g. (FOO - :lower16:BAR) isn't legal.
1412 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001413 if (ParsePrefix(RefKind))
1414 return true;
1415
Evan Cheng75972122011-01-13 07:58:56 +00001416 const MCExpr *SubExprVal;
1417 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +00001418 return true;
1419
Evan Cheng75972122011-01-13 07:58:56 +00001420 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1421 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +00001422 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +00001423 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +00001424 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001425 }
Jason W Kim9081b4b2011-01-11 23:53:41 +00001426 }
1427}
1428
Evan Cheng75972122011-01-13 07:58:56 +00001429// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1430// :lower16: and :upper16:.
1431bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1432 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001433
1434 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +00001435 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +00001436 Parser.Lex(); // Eat ':'
1437
1438 if (getLexer().isNot(AsmToken::Identifier)) {
1439 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1440 return true;
1441 }
1442
1443 StringRef IDVal = Parser.getTok().getIdentifier();
1444 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001445 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001446 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001447 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001448 } else {
1449 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1450 return true;
1451 }
1452 Parser.Lex();
1453
1454 if (getLexer().isNot(AsmToken::Colon)) {
1455 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1456 return true;
1457 }
1458 Parser.Lex(); // Eat the last ':'
1459 return false;
1460}
1461
1462const MCExpr *
1463ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1464 MCSymbolRefExpr::VariantKind Variant) {
1465 // Recurse over the given expression, rebuilding it to apply the given variant
1466 // to the leftmost symbol.
1467 if (Variant == MCSymbolRefExpr::VK_None)
1468 return E;
1469
1470 switch (E->getKind()) {
1471 case MCExpr::Target:
1472 llvm_unreachable("Can't handle target expr yet");
1473 case MCExpr::Constant:
1474 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1475
1476 case MCExpr::SymbolRef: {
1477 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1478
1479 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1480 return 0;
1481
1482 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1483 }
1484
1485 case MCExpr::Unary:
1486 llvm_unreachable("Can't handle unary expressions yet");
1487
1488 case MCExpr::Binary: {
1489 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1490 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1491 const MCExpr *RHS = BE->getRHS();
1492 if (!LHS)
1493 return 0;
1494
1495 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1496 }
1497 }
1498
1499 assert(0 && "Invalid expression kind!");
1500 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001501}
1502
Daniel Dunbar352e1482011-01-11 15:59:50 +00001503/// \brief Given a mnemonic, split out possible predication code and carry
1504/// setting letters to form a canonical mnemonic and flags.
1505//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001506// FIXME: Would be nice to autogen this.
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001507static StringRef SplitMnemonic(StringRef Mnemonic,
1508 unsigned &PredicationCode,
1509 bool &CarrySetting,
1510 unsigned &ProcessorIMod) {
Daniel Dunbar352e1482011-01-11 15:59:50 +00001511 PredicationCode = ARMCC::AL;
1512 CarrySetting = false;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001513 ProcessorIMod = 0;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001514
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001515 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001516 //
1517 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001518 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1519 Mnemonic == "movs" ||
1520 Mnemonic == "svc" ||
1521 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1522 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1523 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1524 Mnemonic == "vclt" ||
1525 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1526 Mnemonic == "vcle" ||
1527 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1528 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1529 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001530 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001531
Daniel Dunbar352e1482011-01-11 15:59:50 +00001532 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001533 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001534 .Case("eq", ARMCC::EQ)
1535 .Case("ne", ARMCC::NE)
1536 .Case("hs", ARMCC::HS)
1537 .Case("lo", ARMCC::LO)
1538 .Case("mi", ARMCC::MI)
1539 .Case("pl", ARMCC::PL)
1540 .Case("vs", ARMCC::VS)
1541 .Case("vc", ARMCC::VC)
1542 .Case("hi", ARMCC::HI)
1543 .Case("ls", ARMCC::LS)
1544 .Case("ge", ARMCC::GE)
1545 .Case("lt", ARMCC::LT)
1546 .Case("gt", ARMCC::GT)
1547 .Case("le", ARMCC::LE)
1548 .Case("al", ARMCC::AL)
1549 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001550 if (CC != ~0U) {
1551 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001552 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001553 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001554
Daniel Dunbar352e1482011-01-11 15:59:50 +00001555 // Next, determine if we have a carry setting bit. We explicitly ignore all
1556 // the instructions we know end in 's'.
1557 if (Mnemonic.endswith("s") &&
1558 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1559 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1560 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1561 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1562 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1563 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1564 CarrySetting = true;
1565 }
1566
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001567 // The "cps" instruction can have a interrupt mode operand which is glued into
1568 // the mnemonic. Check if this is the case, split it and parse the imod op
1569 if (Mnemonic.startswith("cps")) {
1570 // Split out any imod code.
1571 unsigned IMod =
1572 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1573 .Case("ie", ARM_PROC::IE)
1574 .Case("id", ARM_PROC::ID)
1575 .Default(~0U);
1576 if (IMod != ~0U) {
1577 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1578 ProcessorIMod = IMod;
1579 }
1580 }
1581
Daniel Dunbar352e1482011-01-11 15:59:50 +00001582 return Mnemonic;
1583}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001584
1585/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1586/// inclusion of carry set or predication code operands.
1587//
1588// FIXME: It would be nice to autogen this.
Bruno Cardoso Lopesfdcee772011-01-18 20:55:11 +00001589void ARMAsmParser::
1590GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1591 bool &CanAcceptPredicationCode) {
1592 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1593
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001594 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1595 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1596 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1597 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1598 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1599 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1600 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1601 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1602 CanAcceptCarrySet = true;
1603 } else {
1604 CanAcceptCarrySet = false;
1605 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001606
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001607 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1608 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1609 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1610 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
Bruno Cardoso Lopese47f3752011-01-20 19:18:32 +00001611 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001612 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001613 CanAcceptPredicationCode = false;
1614 } else {
1615 CanAcceptPredicationCode = true;
1616 }
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001617
1618 if (isThumb)
1619 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
Bruno Cardoso Lopes8dd37f72011-01-20 18:32:09 +00001620 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
Bruno Cardoso Lopesfa5bd272011-01-20 16:35:57 +00001621 CanAcceptPredicationCode = false;
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001622}
1623
1624/// Parse an arm instruction mnemonic followed by its operands.
1625bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1626 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1627 // Create the leading tokens for the mnemonic, split by '.' characters.
1628 size_t Start = 0, Next = Name.find('.');
1629 StringRef Head = Name.slice(Start, Next);
1630
Daniel Dunbar352e1482011-01-11 15:59:50 +00001631 // Split out the predication code and carry setting flag from the mnemonic.
1632 unsigned PredicationCode;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001633 unsigned ProcessorIMod;
Daniel Dunbar352e1482011-01-11 15:59:50 +00001634 bool CarrySetting;
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001635 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1636 ProcessorIMod);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001637
Chris Lattner3a697562010-10-28 17:20:03 +00001638 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001639
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001640 // Next, add the CCOut and ConditionCode operands, if needed.
1641 //
1642 // For mnemonics which can ever incorporate a carry setting bit or predication
1643 // code, our matching model involves us always generating CCOut and
1644 // ConditionCode operands to match the mnemonic "as written" and then we let
1645 // the matcher deal with finding the right instruction or generating an
1646 // appropriate error.
1647 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1648 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1649
1650 // Add the carry setting operand, if necessary.
1651 //
1652 // FIXME: It would be awesome if we could somehow invent a location such that
1653 // match errors on this operand would print a nice diagnostic about how the
1654 // 's' character in the mnemonic resulted in a CCOut operand.
1655 if (CanAcceptCarrySet) {
1656 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1657 NameLoc));
1658 } else {
1659 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1660 // misspelled another mnemonic).
1661
1662 // FIXME: Issue a nice error.
1663 }
1664
1665 // Add the predication code operand, if necessary.
1666 if (CanAcceptPredicationCode) {
1667 Operands.push_back(ARMOperand::CreateCondCode(
1668 ARMCC::CondCodes(PredicationCode), NameLoc));
1669 } else {
1670 // This mnemonic can't ever accept a predication code, but the user wrote
1671 // one (or misspelled another mnemonic).
1672
1673 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001674 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001675
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001676 // Add the processor imod operand, if necessary.
1677 if (ProcessorIMod) {
1678 Operands.push_back(ARMOperand::CreateImm(
1679 MCConstantExpr::Create(ProcessorIMod, getContext()),
1680 NameLoc, NameLoc));
1681 } else {
1682 // This mnemonic can't ever accept a imod, but the user wrote
1683 // one (or misspelled another mnemonic).
1684
1685 // FIXME: Issue a nice error.
1686 }
1687
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001688 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001689 while (Next != StringRef::npos) {
1690 Start = Next;
1691 Next = Name.find('.', Start + 1);
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001692 StringRef ExtraToken = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001693
Bruno Cardoso Lopesa2b6e412011-02-14 13:09:44 +00001694 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001695 }
1696
1697 // Read the remaining operands.
1698 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001699 // Read the first operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001700 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001701 Parser.EatToEndOfStatement();
1702 return true;
1703 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001704
1705 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001706 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001707
1708 // Parse and remember the operand.
Bruno Cardoso Lopesfafde7f2011-02-07 21:41:25 +00001709 if (ParseOperand(Operands, Head)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001710 Parser.EatToEndOfStatement();
1711 return true;
1712 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001713 }
1714 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001715
Chris Lattnercbf8a982010-09-11 16:18:25 +00001716 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1717 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001718 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001719 }
Bill Wendling146018f2010-11-06 21:42:12 +00001720
Chris Lattner34e53142010-09-08 05:10:46 +00001721 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001722 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001723}
1724
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001725bool ARMAsmParser::
1726MatchAndEmitInstruction(SMLoc IDLoc,
1727 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1728 MCStreamer &Out) {
1729 MCInst Inst;
1730 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001731 MatchResultTy MatchResult, MatchResult2;
1732 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1733 if (MatchResult != Match_Success) {
1734 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1735 // that does not update the condition codes. So try adding a CCOut operand
1736 // with a value of reg0.
1737 if (MatchResult == Match_InvalidOperand) {
1738 Operands.insert(Operands.begin() + 1,
1739 ARMOperand::CreateCCOut(0,
1740 ((ARMOperand*)Operands[0])->getStartLoc()));
1741 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1742 if (MatchResult2 == Match_Success)
1743 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001744 else {
1745 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001746 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001747 delete CCOut;
1748 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001749 }
1750 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1751 // that updates the condition codes if it ends in 's'. So see if the
1752 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1753 // operand with a value of CPSR.
1754 else if(MatchResult == Match_MnemonicFail) {
1755 // Get the instruction mnemonic, which is the first token.
1756 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1757 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1758 // removed the 's' from the mnemonic for matching.
1759 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1760 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001761 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1762 Operands.erase(Operands.begin());
1763 delete OldMnemonic;
1764 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001765 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1766 Operands.insert(Operands.begin() + 1,
1767 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1768 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1769 if (MatchResult2 == Match_Success)
1770 MatchResult = Match_Success;
1771 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001772 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1773 Operands.erase(Operands.begin());
1774 delete OldMnemonic;
1775 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001776 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001777 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1778 Operands.erase(Operands.begin() + 1);
1779 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001780 }
1781 }
1782 }
1783 }
1784 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001785 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001786 Out.EmitInstruction(Inst);
1787 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001788 case Match_MissingFeature:
1789 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1790 return true;
1791 case Match_InvalidOperand: {
1792 SMLoc ErrorLoc = IDLoc;
1793 if (ErrorInfo != ~0U) {
1794 if (ErrorInfo >= Operands.size())
1795 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001796
Chris Lattnere73d4f82010-10-28 21:41:58 +00001797 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1798 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1799 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001800
Chris Lattnere73d4f82010-10-28 21:41:58 +00001801 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001802 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001803 case Match_MnemonicFail:
1804 return Error(IDLoc, "unrecognized instruction mnemonic");
Daniel Dunbarb4129152011-02-04 17:12:23 +00001805 case Match_ConversionFail:
1806 return Error(IDLoc, "unable to convert operands to instruction");
Chris Lattnere73d4f82010-10-28 21:41:58 +00001807 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001808
Eric Christopherc223e2b2010-10-29 09:26:59 +00001809 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001810 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001811}
1812
Kevin Enderby515d5092009-10-15 20:48:48 +00001813/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001814bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1815 StringRef IDVal = DirectiveID.getIdentifier();
1816 if (IDVal == ".word")
1817 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001818 else if (IDVal == ".thumb")
1819 return ParseDirectiveThumb(DirectiveID.getLoc());
1820 else if (IDVal == ".thumb_func")
1821 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1822 else if (IDVal == ".code")
1823 return ParseDirectiveCode(DirectiveID.getLoc());
1824 else if (IDVal == ".syntax")
1825 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001826 return true;
1827}
1828
1829/// ParseDirectiveWord
1830/// ::= .word [ expression (, expression)* ]
1831bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1832 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1833 for (;;) {
1834 const MCExpr *Value;
1835 if (getParser().ParseExpression(Value))
1836 return true;
1837
Chris Lattneraaec2052010-01-19 19:46:13 +00001838 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001839
1840 if (getLexer().is(AsmToken::EndOfStatement))
1841 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001842
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001843 // FIXME: Improve diagnostic.
1844 if (getLexer().isNot(AsmToken::Comma))
1845 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001846 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001847 }
1848 }
1849
Sean Callananb9a25b72010-01-19 20:27:46 +00001850 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001851 return false;
1852}
1853
Kevin Enderby515d5092009-10-15 20:48:48 +00001854/// ParseDirectiveThumb
1855/// ::= .thumb
1856bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1857 if (getLexer().isNot(AsmToken::EndOfStatement))
1858 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001859 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001860
1861 // TODO: set thumb mode
1862 // TODO: tell the MC streamer the mode
1863 // getParser().getStreamer().Emit???();
1864 return false;
1865}
1866
1867/// ParseDirectiveThumbFunc
1868/// ::= .thumbfunc symbol_name
1869bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001870 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001871 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001872 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001873 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001874 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001875 if (getLexer().isNot(AsmToken::EndOfStatement))
1876 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001877 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001878
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001879 // Mark symbol as a thumb symbol.
1880 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1881 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001882 return false;
1883}
1884
1885/// ParseDirectiveSyntax
1886/// ::= .syntax unified | divided
1887bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001888 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001889 if (Tok.isNot(AsmToken::Identifier))
1890 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001891 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001892 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001893 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001894 else if (Mode == "divided" || Mode == "DIVIDED")
Kevin Enderby9e56fb12011-01-27 23:22:36 +00001895 return Error(L, "'.syntax divided' arm asssembly not supported");
Kevin Enderby515d5092009-10-15 20:48:48 +00001896 else
1897 return Error(L, "unrecognized syntax mode in .syntax directive");
1898
1899 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001900 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001901 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001902
1903 // TODO tell the MC streamer the mode
1904 // getParser().getStreamer().Emit???();
1905 return false;
1906}
1907
1908/// ParseDirectiveCode
1909/// ::= .code 16 | 32
1910bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001911 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001912 if (Tok.isNot(AsmToken::Integer))
1913 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001914 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001915 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001916 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001917 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001918 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001919 else
1920 return Error(L, "invalid operand to .code directive");
1921
1922 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001923 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001924 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001925
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001926 // FIXME: We need to be able switch subtargets at this point so that
1927 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1928 // includes Feature_IsThumb or not to match the right instructions. This is
1929 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1930 if (Val == 16){
1931 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1932 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001933 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001934 }
1935 else{
1936 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1937 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001938 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001939 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001940
Kevin Enderby515d5092009-10-15 20:48:48 +00001941 return false;
1942}
1943
Sean Callanan90b70972010-04-07 20:29:34 +00001944extern "C" void LLVMInitializeARMAsmLexer();
1945
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001946/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001947extern "C" void LLVMInitializeARMAsmParser() {
1948 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1949 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001950 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001951}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001952
Chris Lattner0692ee62010-09-06 19:11:01 +00001953#define GET_REGISTER_MATCHER
1954#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001955#include "ARMGenAsmMatcher.inc"