blob: 08f3a90d1cf18664b3431663b13351bc1173571f [file] [log] [blame]
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001//===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "ARM.h"
Bill Wendling92b5a2e2010-11-03 01:49:29 +000011#include "ARMAddressingModes.h"
Evan Cheng75972122011-01-13 07:58:56 +000012#include "ARMMCExpr.h"
Evan Chengb72d2a92011-01-11 21:46:47 +000013#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000014#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000015#include "llvm/MC/MCParser/MCAsmLexer.h"
16#include "llvm/MC/MCParser/MCAsmParser.h"
17#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000018#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000019#include "llvm/MC/MCStreamer.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000022#include "llvm/Target/TargetRegistry.h"
23#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000024#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000026#include "llvm/ADT/SmallVector.h"
Owen Anderson0c9f2502011-01-13 22:50:36 +000027#include "llvm/ADT/StringExtras.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000028#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000029#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000030using namespace llvm;
31
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000032/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000033enum ShiftType {
34 Lsl,
35 Lsr,
36 Asr,
37 Ror,
38 Rrx
39};
40
Chris Lattner3a697562010-10-28 17:20:03 +000041namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000042
43class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000044
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000045class ARMAsmParser : public TargetAsmParser {
46 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000047 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000049 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000050 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
51
52 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000053 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
54
Chris Lattnere5658fa2010-10-30 04:09:10 +000055 int TryParseRegister();
Owen Andersone4e5e2a2011-01-13 21:46:02 +000056 bool TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*>&);
Bill Wendling50d0f582010-11-18 23:43:05 +000057 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
58 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
59 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
Owen Andersone4e5e2a2011-01-13 21:46:02 +000060 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, bool isMCR);
Evan Cheng75972122011-01-13 07:58:56 +000061 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
Jason W Kim9081b4b2011-01-11 23:53:41 +000062 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
63 MCSymbolRefExpr::VariantKind Variant);
64
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000065
Kevin Enderby9c41fa82009-10-30 22:55:57 +000066 bool ParseMemoryOffsetReg(bool &Negative,
67 bool &OffsetRegShifted,
68 enum ShiftType &ShiftType,
69 const MCExpr *&ShiftAmount,
70 const MCExpr *&Offset,
71 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000072 int &OffsetRegNum,
73 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000074 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000075 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000076 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000077 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000078 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000079 bool ParseDirectiveSyntax(SMLoc L);
80
Chris Lattner7036f8b2010-09-29 01:42:58 +000081 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000082 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000083 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000084
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000085 /// @name Auto-generated Match Functions
86 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000087
Chris Lattner0692ee62010-09-06 19:11:01 +000088#define GET_ASSEMBLER_HEADER
89#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000090
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000091 /// }
92
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000094 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000095 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
96 // Initialize the set of available features.
97 setAvailableFeatures(ComputeAvailableFeatures(
98 &TM.getSubtarget<ARMSubtarget>()));
99 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000100
Benjamin Kramer38e59892010-07-14 22:38:02 +0000101 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +0000102 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +0000103 virtual bool ParseDirective(AsmToken DirectiveID);
104};
Jim Grosbach16c74252010-10-29 14:46:02 +0000105} // end anonymous namespace
106
Chris Lattner3a697562010-10-28 17:20:03 +0000107namespace {
108
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000109/// ARMOperand - Instances of this class represent a parsed ARM machine
110/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000111class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000112 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000113 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000114 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000115 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000116 Memory,
117 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000118 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000119 DPRRegisterList,
120 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000121 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000122 } Kind;
123
Sean Callanan76264762010-04-02 22:27:05 +0000124 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000125 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000126
127 union {
128 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000129 ARMCC::CondCodes Val;
130 } CC;
131
132 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000133 const char *Data;
134 unsigned Length;
135 } Tok;
136
137 struct {
138 unsigned RegNum;
139 } Reg;
140
Bill Wendling8155e5b2010-11-06 22:19:43 +0000141 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000142 const MCExpr *Val;
143 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000144
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000145 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000146 struct {
147 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000148 unsigned OffsetRegNum; // used when OffsetIsReg is true
149 const MCExpr *Offset; // used when OffsetIsReg is false
150 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
151 enum ShiftType ShiftType; // used when OffsetRegShifted is true
152 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000153 unsigned Preindexed : 1;
154 unsigned Postindexed : 1;
155 unsigned OffsetIsReg : 1;
156 unsigned Negative : 1; // only used when OffsetIsReg is true
157 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000158 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000159 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000160
Bill Wendling146018f2010-11-06 21:42:12 +0000161 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
162public:
Sean Callanan76264762010-04-02 22:27:05 +0000163 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
164 Kind = o.Kind;
165 StartLoc = o.StartLoc;
166 EndLoc = o.EndLoc;
167 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000168 case CondCode:
169 CC = o.CC;
170 break;
Sean Callanan76264762010-04-02 22:27:05 +0000171 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000172 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000173 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000174 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000175 case Register:
176 Reg = o.Reg;
177 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000178 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000179 case DPRRegisterList:
180 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000181 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000182 break;
Sean Callanan76264762010-04-02 22:27:05 +0000183 case Immediate:
184 Imm = o.Imm;
185 break;
186 case Memory:
187 Mem = o.Mem;
188 break;
189 }
190 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000191
Sean Callanan76264762010-04-02 22:27:05 +0000192 /// getStartLoc - Get the location of the first token of this operand.
193 SMLoc getStartLoc() const { return StartLoc; }
194 /// getEndLoc - Get the location of the last token of this operand.
195 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000196
Daniel Dunbar8462b302010-08-11 06:36:53 +0000197 ARMCC::CondCodes getCondCode() const {
198 assert(Kind == CondCode && "Invalid access!");
199 return CC.Val;
200 }
201
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000202 StringRef getToken() const {
203 assert(Kind == Token && "Invalid access!");
204 return StringRef(Tok.Data, Tok.Length);
205 }
206
207 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000208 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000209 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000210 }
211
Bill Wendling5fa22a12010-11-09 23:28:44 +0000212 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000213 assert((Kind == RegisterList || Kind == DPRRegisterList ||
214 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000215 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000216 }
217
Kevin Enderbycfe07242009-10-13 22:19:02 +0000218 const MCExpr *getImm() const {
219 assert(Kind == Immediate && "Invalid access!");
220 return Imm.Val;
221 }
222
Daniel Dunbar8462b302010-08-11 06:36:53 +0000223 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000224 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000225 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000226 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000227 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000228 bool isDPRRegList() const { return Kind == DPRRegisterList; }
229 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000230 bool isToken() const { return Kind == Token; }
231 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000232 bool isMemMode5() const {
233 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
234 Mem.Writeback || Mem.Negative)
235 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000236
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000237 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000238 if (!CE) return false;
239
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000240 // The offset must be a multiple of 4 in the range 0-1020.
241 int64_t Value = CE->getValue();
242 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
243 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000244 bool isMemModeRegThumb() const {
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000245 if (!isMemory() || !Mem.OffsetIsReg || Mem.Writeback)
Bill Wendlingf4caf692010-12-14 03:36:38 +0000246 return false;
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000247 return true;
Bill Wendlingf4caf692010-12-14 03:36:38 +0000248 }
249 bool isMemModeImmThumb() const {
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000250 if (!isMemory() || Mem.OffsetIsReg || Mem.Writeback)
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000251 return false;
252
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000253 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
254 if (!CE) return false;
255
256 // The offset must be a multiple of 4 in the range 0-124.
257 uint64_t Value = CE->getValue();
258 return ((Value & 0x3) == 0 && Value <= 124);
259 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000260
261 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000262 // Add as immediates when possible. Null MCExpr = 0.
263 if (Expr == 0)
264 Inst.addOperand(MCOperand::CreateImm(0));
265 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000266 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
267 else
268 Inst.addOperand(MCOperand::CreateExpr(Expr));
269 }
270
Daniel Dunbar8462b302010-08-11 06:36:53 +0000271 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000272 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000273 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000274 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
275 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000276 }
277
Jim Grosbachd67641b2010-12-06 18:21:12 +0000278 void addCCOutOperands(MCInst &Inst, unsigned N) const {
279 assert(N == 1 && "Invalid number of operands!");
280 Inst.addOperand(MCOperand::CreateReg(getReg()));
281 }
282
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000283 void addRegOperands(MCInst &Inst, unsigned N) const {
284 assert(N == 1 && "Invalid number of operands!");
285 Inst.addOperand(MCOperand::CreateReg(getReg()));
286 }
287
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000288 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000289 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000290 const SmallVectorImpl<unsigned> &RegList = getRegList();
291 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000292 I = RegList.begin(), E = RegList.end(); I != E; ++I)
293 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000294 }
295
Bill Wendling0f630752010-11-17 04:32:08 +0000296 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
297 addRegListOperands(Inst, N);
298 }
299
300 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
301 addRegListOperands(Inst, N);
302 }
303
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000304 void addImmOperands(MCInst &Inst, unsigned N) const {
305 assert(N == 1 && "Invalid number of operands!");
306 addExpr(Inst, getImm());
307 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000308
Chris Lattner14b93852010-10-29 00:27:31 +0000309 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
310 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000311
Chris Lattner14b93852010-10-29 00:27:31 +0000312 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000313 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000314
Jim Grosbach80eb2332010-10-29 17:41:25 +0000315 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
316 // the difference?
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000317 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
318 assert(CE && "Non-constant mode 5 offset operand!");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000319
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000320 // The MCInst offset operand doesn't include the low two bits (like
321 // the instruction encoding).
322 int64_t Offset = CE->getValue() / 4;
323 if (Offset >= 0)
324 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
325 Offset)));
326 else
327 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
328 -Offset)));
Chris Lattner14b93852010-10-29 00:27:31 +0000329 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000330
Bill Wendlingf4caf692010-12-14 03:36:38 +0000331 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
332 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000333 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000334 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
335 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000336
Bill Wendlingf4caf692010-12-14 03:36:38 +0000337 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
338 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
339 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
340 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
341 assert(CE && "Non-constant mode offset operand!");
342 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000343 }
344
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000345 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000346
Chris Lattner3a697562010-10-28 17:20:03 +0000347 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
348 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000349 Op->CC.Val = CC;
350 Op->StartLoc = S;
351 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000352 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000353 }
354
Jim Grosbachd67641b2010-12-06 18:21:12 +0000355 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
356 ARMOperand *Op = new ARMOperand(CCOut);
357 Op->Reg.RegNum = RegNum;
358 Op->StartLoc = S;
359 Op->EndLoc = S;
360 return Op;
361 }
362
Chris Lattner3a697562010-10-28 17:20:03 +0000363 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
364 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000365 Op->Tok.Data = Str.data();
366 Op->Tok.Length = Str.size();
367 Op->StartLoc = S;
368 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000369 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000370 }
371
Bill Wendling50d0f582010-11-18 23:43:05 +0000372 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000373 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000374 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000375 Op->StartLoc = S;
376 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000377 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000378 }
379
Bill Wendling7729e062010-11-09 22:44:22 +0000380 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000381 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000382 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000383 KindTy Kind = RegisterList;
384
385 if (ARM::DPRRegClass.contains(Regs.front().first))
386 Kind = DPRRegisterList;
387 else if (ARM::SPRRegClass.contains(Regs.front().first))
388 Kind = SPRRegisterList;
389
390 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000391 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000392 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000393 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000394 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000395 Op->StartLoc = StartLoc;
396 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000397 return Op;
398 }
399
Chris Lattner3a697562010-10-28 17:20:03 +0000400 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
401 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000402 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000403 Op->StartLoc = S;
404 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000405 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000406 }
407
Chris Lattner3a697562010-10-28 17:20:03 +0000408 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
Daniel Dunbar023835d2011-01-18 05:34:05 +0000409 const MCExpr *Offset, int OffsetRegNum,
Chris Lattner3a697562010-10-28 17:20:03 +0000410 bool OffsetRegShifted, enum ShiftType ShiftType,
411 const MCExpr *ShiftAmount, bool Preindexed,
412 bool Postindexed, bool Negative, bool Writeback,
413 SMLoc S, SMLoc E) {
Daniel Dunbar023835d2011-01-18 05:34:05 +0000414 assert((OffsetRegNum == -1 || OffsetIsReg) &&
415 "OffsetRegNum must imply OffsetIsReg!");
416 assert((!OffsetRegShifted || OffsetIsReg) &&
417 "OffsetRegShifted must imply OffsetIsReg!");
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000418 assert((Offset || OffsetIsReg) &&
419 "Offset must exists unless register offset is used!");
Daniel Dunbar023835d2011-01-18 05:34:05 +0000420 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
421 "Cannot have shift amount without shifted register offset!");
422 assert((!Offset || !OffsetIsReg) &&
423 "Cannot have expression offset and register offset!");
424
Chris Lattner3a697562010-10-28 17:20:03 +0000425 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000426 Op->Mem.BaseRegNum = BaseRegNum;
427 Op->Mem.OffsetIsReg = OffsetIsReg;
428 Op->Mem.Offset = Offset;
429 Op->Mem.OffsetRegNum = OffsetRegNum;
430 Op->Mem.OffsetRegShifted = OffsetRegShifted;
431 Op->Mem.ShiftType = ShiftType;
432 Op->Mem.ShiftAmount = ShiftAmount;
433 Op->Mem.Preindexed = Preindexed;
434 Op->Mem.Postindexed = Postindexed;
435 Op->Mem.Negative = Negative;
436 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000437
Sean Callanan76264762010-04-02 22:27:05 +0000438 Op->StartLoc = S;
439 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000440 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000441 }
442};
443
444} // end anonymous namespace.
445
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000446void ARMOperand::dump(raw_ostream &OS) const {
447 switch (Kind) {
448 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000449 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000450 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000451 case CCOut:
452 OS << "<ccout " << getReg() << ">";
453 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000454 case Immediate:
455 getImm()->print(OS);
456 break;
457 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000458 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000459 break;
460 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000461 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000462 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000463 case RegisterList:
464 case DPRRegisterList:
465 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000466 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000467
Bill Wendling5fa22a12010-11-09 23:28:44 +0000468 const SmallVectorImpl<unsigned> &RegList = getRegList();
469 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000470 I = RegList.begin(), E = RegList.end(); I != E; ) {
471 OS << *I;
472 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000473 }
474
475 OS << ">";
476 break;
477 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000478 case Token:
479 OS << "'" << getToken() << "'";
480 break;
481 }
482}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000483
484/// @name Auto-generated Match Functions
485/// {
486
487static unsigned MatchRegisterName(StringRef Name);
488
489/// }
490
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000491/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000492/// and if it is a register name the token is eaten and the register number is
493/// returned. Otherwise return -1.
494///
495int ARMAsmParser::TryParseRegister() {
496 const AsmToken &Tok = Parser.getTok();
497 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000498
Chris Lattnere5658fa2010-10-30 04:09:10 +0000499 // FIXME: Validate register for the current architecture; we have to do
500 // validation later, so maybe there is no need for this here.
Owen Anderson0c9f2502011-01-13 22:50:36 +0000501 std::string upperCase = Tok.getString().str();
502 std::string lowerCase = LowercaseString(upperCase);
503 unsigned RegNum = MatchRegisterName(lowerCase);
504 if (!RegNum) {
505 RegNum = StringSwitch<unsigned>(lowerCase)
506 .Case("r13", ARM::SP)
507 .Case("r14", ARM::LR)
508 .Case("r15", ARM::PC)
509 .Case("ip", ARM::R12)
510 .Default(0);
511 }
512 if (!RegNum) return -1;
513
Chris Lattnere5658fa2010-10-30 04:09:10 +0000514 Parser.Lex(); // Eat identifier token.
515 return RegNum;
516}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000517
518
Bill Wendling50d0f582010-11-18 23:43:05 +0000519/// Try to parse a register name. The token must be an Identifier when called.
520/// If it's a register, an AsmOperand is created. Another AsmOperand is created
521/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000522///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000523/// TODO this is likely to change to allow different register types and or to
524/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000525bool ARMAsmParser::
526TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000527 SMLoc S = Parser.getTok().getLoc();
528 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000529 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000530 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000531
Bill Wendling50d0f582010-11-18 23:43:05 +0000532 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000533
Chris Lattnere5658fa2010-10-30 04:09:10 +0000534 const AsmToken &ExclaimTok = Parser.getTok();
535 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000536 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
537 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000538 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000539 }
540
Bill Wendling50d0f582010-11-18 23:43:05 +0000541 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000542}
543
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000544static int MatchMCRName(StringRef Name) {
545 // Use the same layout as the tablegen'erated register name matcher. Ugly,
546 // but efficient.
547 switch (Name.size()) {
548 default: break;
549 case 2:
550 if (Name[0] != 'p' && Name[0] != 'c')
551 return -1;
552 switch (Name[1]) {
553 default: return -1;
554 case '0': return 0;
555 case '1': return 1;
556 case '2': return 2;
557 case '3': return 3;
558 case '4': return 4;
559 case '5': return 5;
560 case '6': return 6;
561 case '7': return 7;
562 case '8': return 8;
563 case '9': return 9;
564 }
565 break;
566 case 3:
567 if ((Name[0] != 'p' && Name[0] != 'c') || Name[1] != '1')
568 return -1;
569 switch (Name[2]) {
570 default: return -1;
571 case '0': return 10;
572 case '1': return 11;
573 case '2': return 12;
574 case '3': return 13;
575 case '4': return 14;
576 case '5': return 15;
577 }
578 break;
579 }
580
581 llvm_unreachable("Unhandled coprocessor operand string!");
582 return -1;
583}
584
585/// TryParseMCRName - Try to parse an MCR/MRC symbolic operand
586/// name. The token must be an Identifier when called, and if it is a MCR
587/// operand name, the token is eaten and the operand is added to the
588/// operand list.
589bool ARMAsmParser::
590TryParseMCRName(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
591 SMLoc S = Parser.getTok().getLoc();
592 const AsmToken &Tok = Parser.getTok();
593 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
594
595 int Num = MatchMCRName(Tok.getString());
596 if (Num == -1)
597 return true;
598
599 Parser.Lex(); // Eat identifier token.
600 Operands.push_back(ARMOperand::CreateImm(
601 MCConstantExpr::Create(Num, getContext()), S, Parser.getTok().getLoc()));
602 return false;
603}
604
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000605/// Parse a register list, return it if successful else return null. The first
606/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000607bool ARMAsmParser::
608ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000609 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000610 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000611 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000612
Bill Wendling7729e062010-11-09 22:44:22 +0000613 // Read the rest of the registers in the list.
614 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000615 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000616
Bill Wendling7729e062010-11-09 22:44:22 +0000617 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000618 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000619 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000620
Sean Callanan18b83232010-01-19 21:44:56 +0000621 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000622 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000623 if (RegTok.isNot(AsmToken::Identifier)) {
624 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000625 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000626 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000627
Bill Wendling1d6a2652010-11-06 10:40:24 +0000628 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000629 if (RegNum == -1) {
630 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000631 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000632 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000633
Bill Wendlinge7176102010-11-06 22:36:58 +0000634 if (IsRange) {
635 int Reg = PrevRegNum;
636 do {
637 ++Reg;
638 Registers.push_back(std::make_pair(Reg, RegLoc));
639 } while (Reg != RegNum);
640 } else {
641 Registers.push_back(std::make_pair(RegNum, RegLoc));
642 }
643
644 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000645 } while (Parser.getTok().is(AsmToken::Comma) ||
646 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000647
648 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000649 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000650 if (RCurlyTok.isNot(AsmToken::RCurly)) {
651 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000652 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000653 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000654
Bill Wendlinge7176102010-11-06 22:36:58 +0000655 SMLoc E = RCurlyTok.getLoc();
656 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000657
Bill Wendlinge7176102010-11-06 22:36:58 +0000658 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000659 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000660 RI = Registers.begin(), RE = Registers.end();
661
Bill Wendling7caebff2011-01-12 21:20:59 +0000662 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000663 bool EmittedWarning = false;
664
Bill Wendling7caebff2011-01-12 21:20:59 +0000665 DenseMap<unsigned, bool> RegMap;
666 RegMap[HighRegNum] = true;
667
Bill Wendlinge7176102010-11-06 22:36:58 +0000668 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000669 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling7caebff2011-01-12 21:20:59 +0000670 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
Bill Wendlinge7176102010-11-06 22:36:58 +0000671
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000672 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000673 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000674 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000675 }
676
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000677 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000678 Warning(RegInfo.second,
679 "register not in ascending order in register list");
680
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000681 RegMap[Reg] = true;
682 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000683 }
684
Bill Wendling50d0f582010-11-18 23:43:05 +0000685 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
686 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000687}
688
Bill Wendlinge7176102010-11-06 22:36:58 +0000689/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000690/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000691///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000692/// TODO Only preindexing and postindexing addressing are started, unindexed
693/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000694bool ARMAsmParser::
695ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000696 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000697 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000698 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000699 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000700 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000701
Sean Callanan18b83232010-01-19 21:44:56 +0000702 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000703 if (BaseRegTok.isNot(AsmToken::Identifier)) {
704 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000705 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000706 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000707 int BaseRegNum = TryParseRegister();
708 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000709 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000710 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000711 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000712
Daniel Dunbar05710932011-01-18 05:34:17 +0000713 // The next token must either be a comma or a closing bracket.
714 const AsmToken &Tok = Parser.getTok();
715 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
716 return true;
717
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000718 bool Preindexed = false;
719 bool Postindexed = false;
720 bool OffsetIsReg = false;
721 bool Negative = false;
722 bool Writeback = false;
723
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000724 // First look for preindexed address forms, that is after the "[Rn" we now
725 // have to see if the next token is a comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000726 if (Tok.is(AsmToken::Comma)) {
727 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000728 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000729 int OffsetRegNum;
730 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000731 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000732 const MCExpr *ShiftAmount = 0;
733 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000734 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
735 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000736 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000737 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000738 if (RBracTok.isNot(AsmToken::RBrac)) {
739 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000740 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000741 }
Sean Callanan76264762010-04-02 22:27:05 +0000742 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000743 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000744
Sean Callanan18b83232010-01-19 21:44:56 +0000745 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000746 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000747 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000748 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
749 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000750 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000751 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000752 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000753
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000754 // Force Offset to exist if used.
755 if (!OffsetIsReg) {
756 if (!Offset)
757 Offset = MCConstantExpr::Create(0, getContext());
758 }
759
Bill Wendling50d0f582010-11-18 23:43:05 +0000760 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
761 OffsetRegNum, OffsetRegShifted,
762 ShiftType, ShiftAmount, Preindexed,
763 Postindexed, Negative, Writeback,
764 S, E));
765 if (WBOp)
766 Operands.push_back(WBOp);
767
768 return false;
Daniel Dunbar05710932011-01-18 05:34:17 +0000769 } else {
770 // The "[Rn" we have so far was not followed by a comma.
771
Jim Grosbach80eb2332010-10-29 17:41:25 +0000772 // If there's anything other than the right brace, this is a post indexing
773 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000774 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000775 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000776
Daniel Dunbar81f453c2011-01-18 05:33:57 +0000777 int OffsetRegNum = -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000778 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000779 enum ShiftType ShiftType = Lsl;
780 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000781 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000782
Sean Callanan18b83232010-01-19 21:44:56 +0000783 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000784
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000785 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000786 Postindexed = true;
787 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000788
Chris Lattner550276e2010-10-28 20:52:15 +0000789 if (NextTok.isNot(AsmToken::Comma)) {
790 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000791 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000792 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000793
Sean Callananb9a25b72010-01-19 20:27:46 +0000794 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000795
Chris Lattner550276e2010-10-28 20:52:15 +0000796 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000797 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000798 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000799 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000800 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000801
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000802 // Force Offset to exist if used.
803 if (!OffsetIsReg) {
804 if (!Offset)
805 Offset = MCConstantExpr::Create(0, getContext());
806 }
807
Bill Wendling50d0f582010-11-18 23:43:05 +0000808 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
809 OffsetRegNum, OffsetRegShifted,
810 ShiftType, ShiftAmount, Preindexed,
811 Postindexed, Negative, Writeback,
812 S, E));
813 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000814 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000815}
816
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000817/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
818/// we will parse the following (were +/- means that a plus or minus is
819/// optional):
820/// +/-Rm
821/// +/-Rm, shift
822/// #offset
823/// we return false on success or an error otherwise.
824bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000825 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000826 enum ShiftType &ShiftType,
827 const MCExpr *&ShiftAmount,
828 const MCExpr *&Offset,
829 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000830 int &OffsetRegNum,
831 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000832 Negative = false;
833 OffsetRegShifted = false;
834 OffsetIsReg = false;
835 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000836 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000837 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000838 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000839 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000840 else if (NextTok.is(AsmToken::Minus)) {
841 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000842 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000843 }
844 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000845 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000846 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000847 SMLoc CurLoc = OffsetRegTok.getLoc();
848 OffsetRegNum = TryParseRegister();
849 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000850 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000851 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000852 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000853 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000854
Bill Wendling12f40e92010-11-06 10:51:53 +0000855 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000856 if (OffsetRegNum != -1) {
857 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000858 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000859 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000861
Sean Callanan18b83232010-01-19 21:44:56 +0000862 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000863 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000864 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000865 OffsetRegShifted = true;
866 }
867 }
868 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
869 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000870 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000871 if (HashTok.isNot(AsmToken::Hash))
872 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000873
Sean Callananb9a25b72010-01-19 20:27:46 +0000874 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000875
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000876 if (getParser().ParseExpression(Offset))
877 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000878 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000879 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000880 return false;
881}
882
883/// ParseShift as one of these two:
884/// ( lsl | lsr | asr | ror ) , # shift_amount
885/// rrx
886/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000887bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000888 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000889 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000890 if (Tok.isNot(AsmToken::Identifier))
891 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000892 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000893 if (ShiftName == "lsl" || ShiftName == "LSL")
894 St = Lsl;
895 else if (ShiftName == "lsr" || ShiftName == "LSR")
896 St = Lsr;
897 else if (ShiftName == "asr" || ShiftName == "ASR")
898 St = Asr;
899 else if (ShiftName == "ror" || ShiftName == "ROR")
900 St = Ror;
901 else if (ShiftName == "rrx" || ShiftName == "RRX")
902 St = Rrx;
903 else
904 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000905 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000906
907 // Rrx stands alone.
908 if (St == Rrx)
909 return false;
910
911 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000912 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000913 if (HashTok.isNot(AsmToken::Hash))
914 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000915 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000916
917 if (getParser().ParseExpression(ShiftAmount))
918 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000919
920 return false;
921}
922
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000923/// Parse a arm instruction operand. For now this parses the operand regardless
924/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000925bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
926 bool isMCR){
Sean Callanan76264762010-04-02 22:27:05 +0000927 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000928 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000929 default:
930 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000931 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000932 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000933 if (!TryParseRegisterWithWriteBack(Operands))
934 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000935 if (isMCR && !TryParseMCRName(Operands))
936 return false;
937
938 // Fall though for the Identifier case that is not a register or a
939 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000940 case AsmToken::Integer: // things like 1f and 2b as a branch targets
941 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000942 // This was not a register so parse other operands that start with an
943 // identifier (like labels) as expressions and create them as immediates.
944 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000945 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000946 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000947 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000948 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000949 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
950 return false;
951 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000952 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000953 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000954 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000955 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000956 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000957 // #42 -> immediate.
958 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000959 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000960 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000961 const MCExpr *ImmVal;
962 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000963 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000964 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000965 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
966 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000967 case AsmToken::Colon: {
968 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +0000969 // FIXME: Check it's an expression prefix,
970 // e.g. (FOO - :lower16:BAR) isn't legal.
971 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000972 if (ParsePrefix(RefKind))
973 return true;
974
Evan Cheng75972122011-01-13 07:58:56 +0000975 const MCExpr *SubExprVal;
976 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +0000977 return true;
978
Evan Cheng75972122011-01-13 07:58:56 +0000979 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
980 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +0000981 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +0000982 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +0000983 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000984 }
Jason W Kim9081b4b2011-01-11 23:53:41 +0000985 }
986}
987
Evan Cheng75972122011-01-13 07:58:56 +0000988// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
989// :lower16: and :upper16:.
990bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
991 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000992
993 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +0000994 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +0000995 Parser.Lex(); // Eat ':'
996
997 if (getLexer().isNot(AsmToken::Identifier)) {
998 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
999 return true;
1000 }
1001
1002 StringRef IDVal = Parser.getTok().getIdentifier();
1003 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001004 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001005 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001006 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001007 } else {
1008 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1009 return true;
1010 }
1011 Parser.Lex();
1012
1013 if (getLexer().isNot(AsmToken::Colon)) {
1014 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1015 return true;
1016 }
1017 Parser.Lex(); // Eat the last ':'
1018 return false;
1019}
1020
1021const MCExpr *
1022ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1023 MCSymbolRefExpr::VariantKind Variant) {
1024 // Recurse over the given expression, rebuilding it to apply the given variant
1025 // to the leftmost symbol.
1026 if (Variant == MCSymbolRefExpr::VK_None)
1027 return E;
1028
1029 switch (E->getKind()) {
1030 case MCExpr::Target:
1031 llvm_unreachable("Can't handle target expr yet");
1032 case MCExpr::Constant:
1033 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1034
1035 case MCExpr::SymbolRef: {
1036 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1037
1038 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1039 return 0;
1040
1041 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1042 }
1043
1044 case MCExpr::Unary:
1045 llvm_unreachable("Can't handle unary expressions yet");
1046
1047 case MCExpr::Binary: {
1048 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1049 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1050 const MCExpr *RHS = BE->getRHS();
1051 if (!LHS)
1052 return 0;
1053
1054 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1055 }
1056 }
1057
1058 assert(0 && "Invalid expression kind!");
1059 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001060}
1061
Daniel Dunbar352e1482011-01-11 15:59:50 +00001062/// \brief Given a mnemonic, split out possible predication code and carry
1063/// setting letters to form a canonical mnemonic and flags.
1064//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001065// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001066static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1067 unsigned &PredicationCode,
1068 bool &CarrySetting) {
1069 PredicationCode = ARMCC::AL;
1070 CarrySetting = false;
1071
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001072 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001073 //
1074 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001075 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1076 Mnemonic == "movs" ||
1077 Mnemonic == "svc" ||
1078 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1079 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1080 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1081 Mnemonic == "vclt" ||
1082 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1083 Mnemonic == "vcle" ||
1084 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1085 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1086 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001087 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001088
Daniel Dunbar352e1482011-01-11 15:59:50 +00001089 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001090 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001091 .Case("eq", ARMCC::EQ)
1092 .Case("ne", ARMCC::NE)
1093 .Case("hs", ARMCC::HS)
1094 .Case("lo", ARMCC::LO)
1095 .Case("mi", ARMCC::MI)
1096 .Case("pl", ARMCC::PL)
1097 .Case("vs", ARMCC::VS)
1098 .Case("vc", ARMCC::VC)
1099 .Case("hi", ARMCC::HI)
1100 .Case("ls", ARMCC::LS)
1101 .Case("ge", ARMCC::GE)
1102 .Case("lt", ARMCC::LT)
1103 .Case("gt", ARMCC::GT)
1104 .Case("le", ARMCC::LE)
1105 .Case("al", ARMCC::AL)
1106 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001107 if (CC != ~0U) {
1108 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001109 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001110 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001111
Daniel Dunbar352e1482011-01-11 15:59:50 +00001112 // Next, determine if we have a carry setting bit. We explicitly ignore all
1113 // the instructions we know end in 's'.
1114 if (Mnemonic.endswith("s") &&
1115 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1116 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1117 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1118 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1119 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1120 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1121 CarrySetting = true;
1122 }
1123
1124 return Mnemonic;
1125}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001126
1127/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1128/// inclusion of carry set or predication code operands.
1129//
1130// FIXME: It would be nice to autogen this.
1131static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1132 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001133 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1134 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1135 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1136 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1137 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1138 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1139 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1140 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1141 CanAcceptCarrySet = true;
1142 } else {
1143 CanAcceptCarrySet = false;
1144 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001145
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001146 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1147 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1148 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1149 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1150 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001151 CanAcceptPredicationCode = false;
1152 } else {
1153 CanAcceptPredicationCode = true;
1154 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001155}
1156
1157/// Parse an arm instruction mnemonic followed by its operands.
1158bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1159 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1160 // Create the leading tokens for the mnemonic, split by '.' characters.
1161 size_t Start = 0, Next = Name.find('.');
1162 StringRef Head = Name.slice(Start, Next);
1163
Daniel Dunbar352e1482011-01-11 15:59:50 +00001164 // Split out the predication code and carry setting flag from the mnemonic.
1165 unsigned PredicationCode;
1166 bool CarrySetting;
1167 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001168
Chris Lattner3a697562010-10-28 17:20:03 +00001169 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001170
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001171 // Next, add the CCOut and ConditionCode operands, if needed.
1172 //
1173 // For mnemonics which can ever incorporate a carry setting bit or predication
1174 // code, our matching model involves us always generating CCOut and
1175 // ConditionCode operands to match the mnemonic "as written" and then we let
1176 // the matcher deal with finding the right instruction or generating an
1177 // appropriate error.
1178 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1179 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1180
1181 // Add the carry setting operand, if necessary.
1182 //
1183 // FIXME: It would be awesome if we could somehow invent a location such that
1184 // match errors on this operand would print a nice diagnostic about how the
1185 // 's' character in the mnemonic resulted in a CCOut operand.
1186 if (CanAcceptCarrySet) {
1187 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1188 NameLoc));
1189 } else {
1190 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1191 // misspelled another mnemonic).
1192
1193 // FIXME: Issue a nice error.
1194 }
1195
1196 // Add the predication code operand, if necessary.
1197 if (CanAcceptPredicationCode) {
1198 Operands.push_back(ARMOperand::CreateCondCode(
1199 ARMCC::CondCodes(PredicationCode), NameLoc));
1200 } else {
1201 // This mnemonic can't ever accept a predication code, but the user wrote
1202 // one (or misspelled another mnemonic).
1203
1204 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001205 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001206
1207 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001208 while (Next != StringRef::npos) {
1209 Start = Next;
1210 Next = Name.find('.', Start + 1);
1211 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001212
Chris Lattner3a697562010-10-28 17:20:03 +00001213 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001214 }
1215
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001216 bool isMCR = (Head == "mcr" || Head == "mcr2" ||
1217 Head == "mcrr" || Head == "mcrr2" ||
1218 Head == "mrc" || Head == "mrc2" ||
1219 Head == "mrrc" || Head == "mrrc2");
1220
Daniel Dunbar5747b132010-08-11 06:37:16 +00001221 // Read the remaining operands.
1222 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001223 // Read the first operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001224 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001225 Parser.EatToEndOfStatement();
1226 return true;
1227 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001228
1229 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001230 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001231
1232 // Parse and remember the operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001233 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001234 Parser.EatToEndOfStatement();
1235 return true;
1236 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001237 }
1238 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001239
Chris Lattnercbf8a982010-09-11 16:18:25 +00001240 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1241 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001242 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001243 }
Bill Wendling146018f2010-11-06 21:42:12 +00001244
Chris Lattner34e53142010-09-08 05:10:46 +00001245 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001246 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001247}
1248
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001249bool ARMAsmParser::
1250MatchAndEmitInstruction(SMLoc IDLoc,
1251 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1252 MCStreamer &Out) {
1253 MCInst Inst;
1254 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001255 MatchResultTy MatchResult, MatchResult2;
1256 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1257 if (MatchResult != Match_Success) {
1258 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1259 // that does not update the condition codes. So try adding a CCOut operand
1260 // with a value of reg0.
1261 if (MatchResult == Match_InvalidOperand) {
1262 Operands.insert(Operands.begin() + 1,
1263 ARMOperand::CreateCCOut(0,
1264 ((ARMOperand*)Operands[0])->getStartLoc()));
1265 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1266 if (MatchResult2 == Match_Success)
1267 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001268 else {
1269 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001270 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001271 delete CCOut;
1272 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001273 }
1274 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1275 // that updates the condition codes if it ends in 's'. So see if the
1276 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1277 // operand with a value of CPSR.
1278 else if(MatchResult == Match_MnemonicFail) {
1279 // Get the instruction mnemonic, which is the first token.
1280 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1281 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1282 // removed the 's' from the mnemonic for matching.
1283 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1284 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001285 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1286 Operands.erase(Operands.begin());
1287 delete OldMnemonic;
1288 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001289 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1290 Operands.insert(Operands.begin() + 1,
1291 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1292 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1293 if (MatchResult2 == Match_Success)
1294 MatchResult = Match_Success;
1295 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001296 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1297 Operands.erase(Operands.begin());
1298 delete OldMnemonic;
1299 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001300 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001301 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1302 Operands.erase(Operands.begin() + 1);
1303 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001304 }
1305 }
1306 }
1307 }
1308 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001309 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001310 Out.EmitInstruction(Inst);
1311 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001312 case Match_MissingFeature:
1313 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1314 return true;
1315 case Match_InvalidOperand: {
1316 SMLoc ErrorLoc = IDLoc;
1317 if (ErrorInfo != ~0U) {
1318 if (ErrorInfo >= Operands.size())
1319 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001320
Chris Lattnere73d4f82010-10-28 21:41:58 +00001321 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1322 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1323 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001324
Chris Lattnere73d4f82010-10-28 21:41:58 +00001325 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001326 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001327 case Match_MnemonicFail:
1328 return Error(IDLoc, "unrecognized instruction mnemonic");
1329 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001330
Eric Christopherc223e2b2010-10-29 09:26:59 +00001331 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001332 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001333}
1334
Kevin Enderby515d5092009-10-15 20:48:48 +00001335/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001336bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1337 StringRef IDVal = DirectiveID.getIdentifier();
1338 if (IDVal == ".word")
1339 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001340 else if (IDVal == ".thumb")
1341 return ParseDirectiveThumb(DirectiveID.getLoc());
1342 else if (IDVal == ".thumb_func")
1343 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1344 else if (IDVal == ".code")
1345 return ParseDirectiveCode(DirectiveID.getLoc());
1346 else if (IDVal == ".syntax")
1347 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001348 return true;
1349}
1350
1351/// ParseDirectiveWord
1352/// ::= .word [ expression (, expression)* ]
1353bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1354 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1355 for (;;) {
1356 const MCExpr *Value;
1357 if (getParser().ParseExpression(Value))
1358 return true;
1359
Chris Lattneraaec2052010-01-19 19:46:13 +00001360 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001361
1362 if (getLexer().is(AsmToken::EndOfStatement))
1363 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001364
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001365 // FIXME: Improve diagnostic.
1366 if (getLexer().isNot(AsmToken::Comma))
1367 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001368 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001369 }
1370 }
1371
Sean Callananb9a25b72010-01-19 20:27:46 +00001372 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001373 return false;
1374}
1375
Kevin Enderby515d5092009-10-15 20:48:48 +00001376/// ParseDirectiveThumb
1377/// ::= .thumb
1378bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1379 if (getLexer().isNot(AsmToken::EndOfStatement))
1380 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001381 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001382
1383 // TODO: set thumb mode
1384 // TODO: tell the MC streamer the mode
1385 // getParser().getStreamer().Emit???();
1386 return false;
1387}
1388
1389/// ParseDirectiveThumbFunc
1390/// ::= .thumbfunc symbol_name
1391bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001392 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001393 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001394 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001395 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001396 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001397 if (getLexer().isNot(AsmToken::EndOfStatement))
1398 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001399 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001400
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001401 // Mark symbol as a thumb symbol.
1402 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1403 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001404 return false;
1405}
1406
1407/// ParseDirectiveSyntax
1408/// ::= .syntax unified | divided
1409bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001410 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001411 if (Tok.isNot(AsmToken::Identifier))
1412 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001413 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001414 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001415 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001416 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001417 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001418 else
1419 return Error(L, "unrecognized syntax mode in .syntax directive");
1420
1421 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001422 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001423 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001424
1425 // TODO tell the MC streamer the mode
1426 // getParser().getStreamer().Emit???();
1427 return false;
1428}
1429
1430/// ParseDirectiveCode
1431/// ::= .code 16 | 32
1432bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001433 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001434 if (Tok.isNot(AsmToken::Integer))
1435 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001436 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001437 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001438 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001439 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001440 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001441 else
1442 return Error(L, "invalid operand to .code directive");
1443
1444 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001445 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001446 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001447
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001448 // FIXME: We need to be able switch subtargets at this point so that
1449 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1450 // includes Feature_IsThumb or not to match the right instructions. This is
1451 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1452 if (Val == 16){
1453 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1454 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001455 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001456 }
1457 else{
1458 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1459 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001460 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001461 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001462
Kevin Enderby515d5092009-10-15 20:48:48 +00001463 return false;
1464}
1465
Sean Callanan90b70972010-04-07 20:29:34 +00001466extern "C" void LLVMInitializeARMAsmLexer();
1467
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001468/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001469extern "C" void LLVMInitializeARMAsmParser() {
1470 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1471 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001472 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001473}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001474
Chris Lattner0692ee62010-09-06 19:11:01 +00001475#define GET_REGISTER_MATCHER
1476#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001477#include "ARMGenAsmMatcher.inc"