blob: 3a7668a8d21ccb68744c38548e3998fe35e9bf17 [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
713 bool Preindexed = false;
714 bool Postindexed = false;
715 bool OffsetIsReg = false;
716 bool Negative = false;
717 bool Writeback = false;
718
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000719 // First look for preindexed address forms, that is after the "[Rn" we now
720 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000721 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000722 if (Tok.is(AsmToken::Comma)) {
723 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000724 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000725 int OffsetRegNum;
726 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000727 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000728 const MCExpr *ShiftAmount = 0;
729 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000730 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
731 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000732 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000733 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000734 if (RBracTok.isNot(AsmToken::RBrac)) {
735 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000736 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000737 }
Sean Callanan76264762010-04-02 22:27:05 +0000738 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000739 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000740
Jim Grosbach03f44a02010-11-29 23:18:01 +0000741
Sean Callanan18b83232010-01-19 21:44:56 +0000742 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000743 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000744 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000745 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
746 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000747 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000748 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000749 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000750
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000751 // Force Offset to exist if used.
752 if (!OffsetIsReg) {
753 if (!Offset)
754 Offset = MCConstantExpr::Create(0, getContext());
755 }
756
Bill Wendling50d0f582010-11-18 23:43:05 +0000757 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
758 OffsetRegNum, OffsetRegShifted,
759 ShiftType, ShiftAmount, Preindexed,
760 Postindexed, Negative, Writeback,
761 S, E));
762 if (WBOp)
763 Operands.push_back(WBOp);
764
765 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000766 }
767 // The "[Rn" we have so far was not followed by a comma.
768 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000769 // If there's anything other than the right brace, this is a post indexing
770 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000771 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000772 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000773
Daniel Dunbar81f453c2011-01-18 05:33:57 +0000774 int OffsetRegNum = -1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000775 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000776 enum ShiftType ShiftType = Lsl;
777 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000778 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000779
Sean Callanan18b83232010-01-19 21:44:56 +0000780 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000781
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000782 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000783 Postindexed = true;
784 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000785
Chris Lattner550276e2010-10-28 20:52:15 +0000786 if (NextTok.isNot(AsmToken::Comma)) {
787 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000788 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000789 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000790
Sean Callananb9a25b72010-01-19 20:27:46 +0000791 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000792
Chris Lattner550276e2010-10-28 20:52:15 +0000793 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000794 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000795 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000796 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000797 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000798
Daniel Dunbard3df5f32011-01-18 05:34:11 +0000799 // Force Offset to exist if used.
800 if (!OffsetIsReg) {
801 if (!Offset)
802 Offset = MCConstantExpr::Create(0, getContext());
803 }
804
Bill Wendling50d0f582010-11-18 23:43:05 +0000805 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
806 OffsetRegNum, OffsetRegShifted,
807 ShiftType, ShiftAmount, Preindexed,
808 Postindexed, Negative, Writeback,
809 S, E));
810 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000811 }
812
Bill Wendling50d0f582010-11-18 23:43:05 +0000813 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000814}
815
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000816/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
817/// we will parse the following (were +/- means that a plus or minus is
818/// optional):
819/// +/-Rm
820/// +/-Rm, shift
821/// #offset
822/// we return false on success or an error otherwise.
823bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000824 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000825 enum ShiftType &ShiftType,
826 const MCExpr *&ShiftAmount,
827 const MCExpr *&Offset,
828 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000829 int &OffsetRegNum,
830 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000831 Negative = false;
832 OffsetRegShifted = false;
833 OffsetIsReg = false;
834 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000835 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000836 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000837 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000838 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000839 else if (NextTok.is(AsmToken::Minus)) {
840 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000841 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000842 }
843 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000844 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000845 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000846 SMLoc CurLoc = OffsetRegTok.getLoc();
847 OffsetRegNum = TryParseRegister();
848 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000849 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000850 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000851 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000852 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000853
Bill Wendling12f40e92010-11-06 10:51:53 +0000854 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000855 if (OffsetRegNum != -1) {
856 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000857 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000858 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000859 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000860
Sean Callanan18b83232010-01-19 21:44:56 +0000861 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000862 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000863 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000864 OffsetRegShifted = true;
865 }
866 }
867 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
868 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000869 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000870 if (HashTok.isNot(AsmToken::Hash))
871 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000872
Sean Callananb9a25b72010-01-19 20:27:46 +0000873 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000874
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000875 if (getParser().ParseExpression(Offset))
876 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000877 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000878 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000879 return false;
880}
881
882/// ParseShift as one of these two:
883/// ( lsl | lsr | asr | ror ) , # shift_amount
884/// rrx
885/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000886bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000887 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000888 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000889 if (Tok.isNot(AsmToken::Identifier))
890 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000891 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000892 if (ShiftName == "lsl" || ShiftName == "LSL")
893 St = Lsl;
894 else if (ShiftName == "lsr" || ShiftName == "LSR")
895 St = Lsr;
896 else if (ShiftName == "asr" || ShiftName == "ASR")
897 St = Asr;
898 else if (ShiftName == "ror" || ShiftName == "ROR")
899 St = Ror;
900 else if (ShiftName == "rrx" || ShiftName == "RRX")
901 St = Rrx;
902 else
903 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000904 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000905
906 // Rrx stands alone.
907 if (St == Rrx)
908 return false;
909
910 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000911 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000912 if (HashTok.isNot(AsmToken::Hash))
913 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000914 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000915
916 if (getParser().ParseExpression(ShiftAmount))
917 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000918
919 return false;
920}
921
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000922/// Parse a arm instruction operand. For now this parses the operand regardless
923/// of the mnemonic.
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000924bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
925 bool isMCR){
Sean Callanan76264762010-04-02 22:27:05 +0000926 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000927 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000928 default:
929 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000930 return true;
Kevin Enderby67b212e2011-01-13 20:32:36 +0000931 case AsmToken::Identifier:
Bill Wendling50d0f582010-11-18 23:43:05 +0000932 if (!TryParseRegisterWithWriteBack(Operands))
933 return false;
Owen Andersone4e5e2a2011-01-13 21:46:02 +0000934 if (isMCR && !TryParseMCRName(Operands))
935 return false;
936
937 // Fall though for the Identifier case that is not a register or a
938 // special name.
Kevin Enderby67b212e2011-01-13 20:32:36 +0000939 case AsmToken::Integer: // things like 1f and 2b as a branch targets
940 case AsmToken::Dot: { // . as a branch target
Kevin Enderby515d5092009-10-15 20:48:48 +0000941 // This was not a register so parse other operands that start with an
942 // identifier (like labels) as expressions and create them as immediates.
943 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000944 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000945 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000946 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000947 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000948 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
949 return false;
950 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000951 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000952 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000953 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000954 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000955 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000956 // #42 -> immediate.
957 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000958 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000959 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000960 const MCExpr *ImmVal;
961 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000962 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000963 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000964 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
965 return false;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000966 case AsmToken::Colon: {
967 // ":lower16:" and ":upper16:" expression prefixes
Evan Cheng75972122011-01-13 07:58:56 +0000968 // FIXME: Check it's an expression prefix,
969 // e.g. (FOO - :lower16:BAR) isn't legal.
970 ARMMCExpr::VariantKind RefKind;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000971 if (ParsePrefix(RefKind))
972 return true;
973
Evan Cheng75972122011-01-13 07:58:56 +0000974 const MCExpr *SubExprVal;
975 if (getParser().ParseExpression(SubExprVal))
Jason W Kim9081b4b2011-01-11 23:53:41 +0000976 return true;
977
Evan Cheng75972122011-01-13 07:58:56 +0000978 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
979 getContext());
Jason W Kim9081b4b2011-01-11 23:53:41 +0000980 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Evan Cheng75972122011-01-13 07:58:56 +0000981 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
Jason W Kim9081b4b2011-01-11 23:53:41 +0000982 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000983 }
Jason W Kim9081b4b2011-01-11 23:53:41 +0000984 }
985}
986
Evan Cheng75972122011-01-13 07:58:56 +0000987// ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
988// :lower16: and :upper16:.
989bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
990 RefKind = ARMMCExpr::VK_ARM_None;
Jason W Kim9081b4b2011-01-11 23:53:41 +0000991
992 // :lower16: and :upper16: modifiers
Jason W Kim8a8696d2011-01-13 00:27:00 +0000993 assert(getLexer().is(AsmToken::Colon) && "expected a :");
Jason W Kim9081b4b2011-01-11 23:53:41 +0000994 Parser.Lex(); // Eat ':'
995
996 if (getLexer().isNot(AsmToken::Identifier)) {
997 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
998 return true;
999 }
1000
1001 StringRef IDVal = Parser.getTok().getIdentifier();
1002 if (IDVal == "lower16") {
Evan Cheng75972122011-01-13 07:58:56 +00001003 RefKind = ARMMCExpr::VK_ARM_LO16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001004 } else if (IDVal == "upper16") {
Evan Cheng75972122011-01-13 07:58:56 +00001005 RefKind = ARMMCExpr::VK_ARM_HI16;
Jason W Kim9081b4b2011-01-11 23:53:41 +00001006 } else {
1007 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1008 return true;
1009 }
1010 Parser.Lex();
1011
1012 if (getLexer().isNot(AsmToken::Colon)) {
1013 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1014 return true;
1015 }
1016 Parser.Lex(); // Eat the last ':'
1017 return false;
1018}
1019
1020const MCExpr *
1021ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1022 MCSymbolRefExpr::VariantKind Variant) {
1023 // Recurse over the given expression, rebuilding it to apply the given variant
1024 // to the leftmost symbol.
1025 if (Variant == MCSymbolRefExpr::VK_None)
1026 return E;
1027
1028 switch (E->getKind()) {
1029 case MCExpr::Target:
1030 llvm_unreachable("Can't handle target expr yet");
1031 case MCExpr::Constant:
1032 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1033
1034 case MCExpr::SymbolRef: {
1035 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1036
1037 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1038 return 0;
1039
1040 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1041 }
1042
1043 case MCExpr::Unary:
1044 llvm_unreachable("Can't handle unary expressions yet");
1045
1046 case MCExpr::Binary: {
1047 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1048 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1049 const MCExpr *RHS = BE->getRHS();
1050 if (!LHS)
1051 return 0;
1052
1053 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1054 }
1055 }
1056
1057 assert(0 && "Invalid expression kind!");
1058 return 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001059}
1060
Daniel Dunbar352e1482011-01-11 15:59:50 +00001061/// \brief Given a mnemonic, split out possible predication code and carry
1062/// setting letters to form a canonical mnemonic and flags.
1063//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001064// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001065static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
1066 unsigned &PredicationCode,
1067 bool &CarrySetting) {
1068 PredicationCode = ARMCC::AL;
1069 CarrySetting = false;
1070
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001071 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +00001072 //
1073 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +00001074 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1075 Mnemonic == "movs" ||
1076 Mnemonic == "svc" ||
1077 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1078 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1079 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1080 Mnemonic == "vclt" ||
1081 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1082 Mnemonic == "vcle" ||
1083 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1084 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1085 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +00001086 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +00001087
Daniel Dunbar352e1482011-01-11 15:59:50 +00001088 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001089 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001090 .Case("eq", ARMCC::EQ)
1091 .Case("ne", ARMCC::NE)
1092 .Case("hs", ARMCC::HS)
1093 .Case("lo", ARMCC::LO)
1094 .Case("mi", ARMCC::MI)
1095 .Case("pl", ARMCC::PL)
1096 .Case("vs", ARMCC::VS)
1097 .Case("vc", ARMCC::VC)
1098 .Case("hi", ARMCC::HI)
1099 .Case("ls", ARMCC::LS)
1100 .Case("ge", ARMCC::GE)
1101 .Case("lt", ARMCC::LT)
1102 .Case("gt", ARMCC::GT)
1103 .Case("le", ARMCC::LE)
1104 .Case("al", ARMCC::AL)
1105 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001106 if (CC != ~0U) {
1107 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +00001108 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +00001109 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001110
Daniel Dunbar352e1482011-01-11 15:59:50 +00001111 // Next, determine if we have a carry setting bit. We explicitly ignore all
1112 // the instructions we know end in 's'.
1113 if (Mnemonic.endswith("s") &&
1114 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1115 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1116 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1117 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1118 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1119 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1120 CarrySetting = true;
1121 }
1122
1123 return Mnemonic;
1124}
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001125
1126/// \brief Given a canonical mnemonic, determine if the instruction ever allows
1127/// inclusion of carry set or predication code operands.
1128//
1129// FIXME: It would be nice to autogen this.
1130static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1131 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001132 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1133 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1134 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1135 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1136 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1137 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1138 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1139 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1140 CanAcceptCarrySet = true;
1141 } else {
1142 CanAcceptCarrySet = false;
1143 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001144
Daniel Dunbareb9f3f92011-01-11 19:06:29 +00001145 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1146 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1147 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1148 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1149 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001150 CanAcceptPredicationCode = false;
1151 } else {
1152 CanAcceptPredicationCode = true;
1153 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001154}
1155
1156/// Parse an arm instruction mnemonic followed by its operands.
1157bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1158 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1159 // Create the leading tokens for the mnemonic, split by '.' characters.
1160 size_t Start = 0, Next = Name.find('.');
1161 StringRef Head = Name.slice(Start, Next);
1162
Daniel Dunbar352e1482011-01-11 15:59:50 +00001163 // Split out the predication code and carry setting flag from the mnemonic.
1164 unsigned PredicationCode;
1165 bool CarrySetting;
1166 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001167
Chris Lattner3a697562010-10-28 17:20:03 +00001168 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +00001169
Daniel Dunbar3771dd02011-01-11 15:59:53 +00001170 // Next, add the CCOut and ConditionCode operands, if needed.
1171 //
1172 // For mnemonics which can ever incorporate a carry setting bit or predication
1173 // code, our matching model involves us always generating CCOut and
1174 // ConditionCode operands to match the mnemonic "as written" and then we let
1175 // the matcher deal with finding the right instruction or generating an
1176 // appropriate error.
1177 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1178 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1179
1180 // Add the carry setting operand, if necessary.
1181 //
1182 // FIXME: It would be awesome if we could somehow invent a location such that
1183 // match errors on this operand would print a nice diagnostic about how the
1184 // 's' character in the mnemonic resulted in a CCOut operand.
1185 if (CanAcceptCarrySet) {
1186 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1187 NameLoc));
1188 } else {
1189 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1190 // misspelled another mnemonic).
1191
1192 // FIXME: Issue a nice error.
1193 }
1194
1195 // Add the predication code operand, if necessary.
1196 if (CanAcceptPredicationCode) {
1197 Operands.push_back(ARMOperand::CreateCondCode(
1198 ARMCC::CondCodes(PredicationCode), NameLoc));
1199 } else {
1200 // This mnemonic can't ever accept a predication code, but the user wrote
1201 // one (or misspelled another mnemonic).
1202
1203 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001204 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001205
1206 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001207 while (Next != StringRef::npos) {
1208 Start = Next;
1209 Next = Name.find('.', Start + 1);
1210 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001211
Chris Lattner3a697562010-10-28 17:20:03 +00001212 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001213 }
1214
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001215 bool isMCR = (Head == "mcr" || Head == "mcr2" ||
1216 Head == "mcrr" || Head == "mcrr2" ||
1217 Head == "mrc" || Head == "mrc2" ||
1218 Head == "mrrc" || Head == "mrrc2");
1219
Daniel Dunbar5747b132010-08-11 06:37:16 +00001220 // Read the remaining operands.
1221 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001222 // Read the first operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001223 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001224 Parser.EatToEndOfStatement();
1225 return true;
1226 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001227
1228 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001229 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001230
1231 // Parse and remember the operand.
Owen Andersone4e5e2a2011-01-13 21:46:02 +00001232 if (ParseOperand(Operands, isMCR)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001233 Parser.EatToEndOfStatement();
1234 return true;
1235 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001236 }
1237 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001238
Chris Lattnercbf8a982010-09-11 16:18:25 +00001239 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1240 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001241 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001242 }
Bill Wendling146018f2010-11-06 21:42:12 +00001243
Chris Lattner34e53142010-09-08 05:10:46 +00001244 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001245 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001246}
1247
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001248bool ARMAsmParser::
1249MatchAndEmitInstruction(SMLoc IDLoc,
1250 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1251 MCStreamer &Out) {
1252 MCInst Inst;
1253 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001254 MatchResultTy MatchResult, MatchResult2;
1255 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1256 if (MatchResult != Match_Success) {
1257 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1258 // that does not update the condition codes. So try adding a CCOut operand
1259 // with a value of reg0.
1260 if (MatchResult == Match_InvalidOperand) {
1261 Operands.insert(Operands.begin() + 1,
1262 ARMOperand::CreateCCOut(0,
1263 ((ARMOperand*)Operands[0])->getStartLoc()));
1264 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1265 if (MatchResult2 == Match_Success)
1266 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001267 else {
1268 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001269 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001270 delete CCOut;
1271 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001272 }
1273 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1274 // that updates the condition codes if it ends in 's'. So see if the
1275 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1276 // operand with a value of CPSR.
1277 else if(MatchResult == Match_MnemonicFail) {
1278 // Get the instruction mnemonic, which is the first token.
1279 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1280 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1281 // removed the 's' from the mnemonic for matching.
1282 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1283 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001284 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1285 Operands.erase(Operands.begin());
1286 delete OldMnemonic;
1287 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001288 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1289 Operands.insert(Operands.begin() + 1,
1290 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1291 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1292 if (MatchResult2 == Match_Success)
1293 MatchResult = Match_Success;
1294 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001295 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1296 Operands.erase(Operands.begin());
1297 delete OldMnemonic;
1298 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001299 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001300 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1301 Operands.erase(Operands.begin() + 1);
1302 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001303 }
1304 }
1305 }
1306 }
1307 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001308 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001309 Out.EmitInstruction(Inst);
1310 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001311 case Match_MissingFeature:
1312 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1313 return true;
1314 case Match_InvalidOperand: {
1315 SMLoc ErrorLoc = IDLoc;
1316 if (ErrorInfo != ~0U) {
1317 if (ErrorInfo >= Operands.size())
1318 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001319
Chris Lattnere73d4f82010-10-28 21:41:58 +00001320 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1321 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1322 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001323
Chris Lattnere73d4f82010-10-28 21:41:58 +00001324 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001325 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001326 case Match_MnemonicFail:
1327 return Error(IDLoc, "unrecognized instruction mnemonic");
1328 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001329
Eric Christopherc223e2b2010-10-29 09:26:59 +00001330 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001331 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001332}
1333
Kevin Enderby515d5092009-10-15 20:48:48 +00001334/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001335bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1336 StringRef IDVal = DirectiveID.getIdentifier();
1337 if (IDVal == ".word")
1338 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001339 else if (IDVal == ".thumb")
1340 return ParseDirectiveThumb(DirectiveID.getLoc());
1341 else if (IDVal == ".thumb_func")
1342 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1343 else if (IDVal == ".code")
1344 return ParseDirectiveCode(DirectiveID.getLoc());
1345 else if (IDVal == ".syntax")
1346 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001347 return true;
1348}
1349
1350/// ParseDirectiveWord
1351/// ::= .word [ expression (, expression)* ]
1352bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1353 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1354 for (;;) {
1355 const MCExpr *Value;
1356 if (getParser().ParseExpression(Value))
1357 return true;
1358
Chris Lattneraaec2052010-01-19 19:46:13 +00001359 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001360
1361 if (getLexer().is(AsmToken::EndOfStatement))
1362 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001363
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001364 // FIXME: Improve diagnostic.
1365 if (getLexer().isNot(AsmToken::Comma))
1366 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001367 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001368 }
1369 }
1370
Sean Callananb9a25b72010-01-19 20:27:46 +00001371 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001372 return false;
1373}
1374
Kevin Enderby515d5092009-10-15 20:48:48 +00001375/// ParseDirectiveThumb
1376/// ::= .thumb
1377bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1378 if (getLexer().isNot(AsmToken::EndOfStatement))
1379 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001380 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001381
1382 // TODO: set thumb mode
1383 // TODO: tell the MC streamer the mode
1384 // getParser().getStreamer().Emit???();
1385 return false;
1386}
1387
1388/// ParseDirectiveThumbFunc
1389/// ::= .thumbfunc symbol_name
1390bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001391 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001392 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001393 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001394 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001395 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001396 if (getLexer().isNot(AsmToken::EndOfStatement))
1397 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001398 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001399
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001400 // Mark symbol as a thumb symbol.
1401 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1402 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001403 return false;
1404}
1405
1406/// ParseDirectiveSyntax
1407/// ::= .syntax unified | divided
1408bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001409 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001410 if (Tok.isNot(AsmToken::Identifier))
1411 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001412 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001413 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001414 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001415 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001416 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001417 else
1418 return Error(L, "unrecognized syntax mode in .syntax directive");
1419
1420 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001421 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001422 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001423
1424 // TODO tell the MC streamer the mode
1425 // getParser().getStreamer().Emit???();
1426 return false;
1427}
1428
1429/// ParseDirectiveCode
1430/// ::= .code 16 | 32
1431bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001432 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001433 if (Tok.isNot(AsmToken::Integer))
1434 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001435 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001436 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001437 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001438 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001439 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001440 else
1441 return Error(L, "invalid operand to .code directive");
1442
1443 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001444 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001445 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001446
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001447 // FIXME: We need to be able switch subtargets at this point so that
1448 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
1449 // includes Feature_IsThumb or not to match the right instructions. This is
1450 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
1451 if (Val == 16){
1452 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
1453 "switching between arm/thumb not yet suppported via .code 16)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001454 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001455 }
1456 else{
1457 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
1458 "switching between thumb/arm not yet suppported via .code 32)");
Jim Grosbach2a301702010-11-05 22:40:53 +00001459 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
Kevin Enderbyfef9ff42011-01-13 01:07:01 +00001460 }
Jim Grosbach2a301702010-11-05 22:40:53 +00001461
Kevin Enderby515d5092009-10-15 20:48:48 +00001462 return false;
1463}
1464
Sean Callanan90b70972010-04-07 20:29:34 +00001465extern "C" void LLVMInitializeARMAsmLexer();
1466
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001467/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001468extern "C" void LLVMInitializeARMAsmParser() {
1469 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1470 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001471 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001472}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001473
Chris Lattner0692ee62010-09-06 19:11:01 +00001474#define GET_REGISTER_MATCHER
1475#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001476#include "ARMGenAsmMatcher.inc"