blob: 190be651216ca12cfde34ac97a72858dd6ff6d7f [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 Chengb72d2a92011-01-11 21:46:47 +000012#include "ARMBaseRegisterInfo.h"
Daniel Dunbar3483aca2010-08-11 05:24:50 +000013#include "ARMSubtarget.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000014#include "llvm/MC/MCParser/MCAsmLexer.h"
15#include "llvm/MC/MCParser/MCAsmParser.h"
16#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
Jim Grosbach642fc9c2010-11-05 22:33:53 +000017#include "llvm/MC/MCContext.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000018#include "llvm/MC/MCStreamer.h"
19#include "llvm/MC/MCExpr.h"
20#include "llvm/MC/MCInst.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000021#include "llvm/Target/TargetRegistry.h"
22#include "llvm/Target/TargetAsmParser.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000023#include "llvm/Support/SourceMgr.h"
Daniel Dunbarfa315de2010-08-11 06:37:12 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000025#include "llvm/ADT/SmallVector.h"
Daniel Dunbar345a9a62010-08-11 06:37:20 +000026#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc6ef2772010-01-22 01:44:57 +000027#include "llvm/ADT/Twine.h"
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000028using namespace llvm;
29
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +000030/// Shift types used for register controlled shifts in ARM memory addressing.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000031enum ShiftType {
32 Lsl,
33 Lsr,
34 Asr,
35 Ror,
36 Rrx
37};
38
Chris Lattner3a697562010-10-28 17:20:03 +000039namespace {
Bill Wendling146018f2010-11-06 21:42:12 +000040
41class ARMOperand;
Jim Grosbach16c74252010-10-29 14:46:02 +000042
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000043class ARMAsmParser : public TargetAsmParser {
44 MCAsmParser &Parser;
Daniel Dunbard73ada72010-07-19 00:33:49 +000045 TargetMachine &TM;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000046
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000047 MCAsmParser &getParser() const { return Parser; }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000048 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
49
50 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000051 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
52
Chris Lattnere5658fa2010-10-30 04:09:10 +000053 int TryParseRegister();
Bill Wendling50d0f582010-11-18 23:43:05 +000054 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
55 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
56 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &);
57 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000058
Kevin Enderby9c41fa82009-10-30 22:55:57 +000059 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
61 enum ShiftType &ShiftType,
62 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
64 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +000065 int &OffsetRegNum,
66 SMLoc &E);
Sean Callanan76264762010-04-02 22:27:05 +000067 bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000068 bool ParseDirectiveWord(unsigned Size, SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000069 bool ParseDirectiveThumb(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000070 bool ParseDirectiveThumbFunc(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000071 bool ParseDirectiveCode(SMLoc L);
Kevin Enderby515d5092009-10-15 20:48:48 +000072 bool ParseDirectiveSyntax(SMLoc L);
73
Chris Lattner7036f8b2010-09-29 01:42:58 +000074 bool MatchAndEmitInstruction(SMLoc IDLoc,
Chris Lattner7c51a312010-09-29 01:50:45 +000075 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
Chris Lattnerfa42fad2010-10-28 21:28:01 +000076 MCStreamer &Out);
Jim Grosbach16c74252010-10-29 14:46:02 +000077
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000078 /// @name Auto-generated Match Functions
79 /// {
Daniel Dunbar3483aca2010-08-11 05:24:50 +000080
Chris Lattner0692ee62010-09-06 19:11:01 +000081#define GET_ASSEMBLER_HEADER
82#include "ARMGenAsmMatcher.inc"
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000083
Kevin Enderbya7ba3a82009-10-06 22:26:42 +000084 /// }
85
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000086public:
Daniel Dunbard73ada72010-07-19 00:33:49 +000087 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
Jim Grosbach833c93c2010-11-01 16:59:54 +000088 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
89 // Initialize the set of available features.
90 setAvailableFeatures(ComputeAvailableFeatures(
91 &TM.getSubtarget<ARMSubtarget>()));
92 }
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000093
Benjamin Kramer38e59892010-07-14 22:38:02 +000094 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
Chris Lattner98986712010-01-14 22:21:20 +000095 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +000096 virtual bool ParseDirective(AsmToken DirectiveID);
97};
Jim Grosbach16c74252010-10-29 14:46:02 +000098} // end anonymous namespace
99
Chris Lattner3a697562010-10-28 17:20:03 +0000100namespace {
101
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000102/// ARMOperand - Instances of this class represent a parsed ARM machine
103/// instruction.
Bill Wendling146018f2010-11-06 21:42:12 +0000104class ARMOperand : public MCParsedAsmOperand {
Sean Callanan76264762010-04-02 22:27:05 +0000105 enum KindTy {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000106 CondCode,
Jim Grosbachd67641b2010-12-06 18:21:12 +0000107 CCOut,
Kevin Enderbycfe07242009-10-13 22:19:02 +0000108 Immediate,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000109 Memory,
110 Register,
Bill Wendling8d5acb72010-11-06 19:56:04 +0000111 RegisterList,
Bill Wendling0f630752010-11-17 04:32:08 +0000112 DPRRegisterList,
113 SPRRegisterList,
Daniel Dunbar8462b302010-08-11 06:36:53 +0000114 Token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000115 } Kind;
116
Sean Callanan76264762010-04-02 22:27:05 +0000117 SMLoc StartLoc, EndLoc;
Bill Wendling24d22d22010-11-18 21:50:54 +0000118 SmallVector<unsigned, 8> Registers;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000119
120 union {
121 struct {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000122 ARMCC::CondCodes Val;
123 } CC;
124
125 struct {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000126 const char *Data;
127 unsigned Length;
128 } Tok;
129
130 struct {
131 unsigned RegNum;
132 } Reg;
133
Bill Wendling8155e5b2010-11-06 22:19:43 +0000134 struct {
Kevin Enderbycfe07242009-10-13 22:19:02 +0000135 const MCExpr *Val;
136 } Imm;
Jim Grosbach16c74252010-10-29 14:46:02 +0000137
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000138 /// Combined record for all forms of ARM address expressions.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000139 struct {
140 unsigned BaseRegNum;
Bill Wendling146018f2010-11-06 21:42:12 +0000141 unsigned OffsetRegNum; // used when OffsetIsReg is true
142 const MCExpr *Offset; // used when OffsetIsReg is false
143 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
144 enum ShiftType ShiftType; // used when OffsetRegShifted is true
145 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
Bill Wendling50d0f582010-11-18 23:43:05 +0000146 unsigned Preindexed : 1;
147 unsigned Postindexed : 1;
148 unsigned OffsetIsReg : 1;
149 unsigned Negative : 1; // only used when OffsetIsReg is true
150 unsigned Writeback : 1;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000151 } Mem;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000152 };
Jim Grosbach16c74252010-10-29 14:46:02 +0000153
Bill Wendling146018f2010-11-06 21:42:12 +0000154 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
155public:
Sean Callanan76264762010-04-02 22:27:05 +0000156 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
157 Kind = o.Kind;
158 StartLoc = o.StartLoc;
159 EndLoc = o.EndLoc;
160 switch (Kind) {
Daniel Dunbar8462b302010-08-11 06:36:53 +0000161 case CondCode:
162 CC = o.CC;
163 break;
Sean Callanan76264762010-04-02 22:27:05 +0000164 case Token:
Daniel Dunbar8462b302010-08-11 06:36:53 +0000165 Tok = o.Tok;
Sean Callanan76264762010-04-02 22:27:05 +0000166 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000167 case CCOut:
Sean Callanan76264762010-04-02 22:27:05 +0000168 case Register:
169 Reg = o.Reg;
170 break;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000171 case RegisterList:
Bill Wendling0f630752010-11-17 04:32:08 +0000172 case DPRRegisterList:
173 case SPRRegisterList:
Bill Wendling24d22d22010-11-18 21:50:54 +0000174 Registers = o.Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000175 break;
Sean Callanan76264762010-04-02 22:27:05 +0000176 case Immediate:
177 Imm = o.Imm;
178 break;
179 case Memory:
180 Mem = o.Mem;
181 break;
182 }
183 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000184
Sean Callanan76264762010-04-02 22:27:05 +0000185 /// getStartLoc - Get the location of the first token of this operand.
186 SMLoc getStartLoc() const { return StartLoc; }
187 /// getEndLoc - Get the location of the last token of this operand.
188 SMLoc getEndLoc() const { return EndLoc; }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000189
Daniel Dunbar8462b302010-08-11 06:36:53 +0000190 ARMCC::CondCodes getCondCode() const {
191 assert(Kind == CondCode && "Invalid access!");
192 return CC.Val;
193 }
194
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000195 StringRef getToken() const {
196 assert(Kind == Token && "Invalid access!");
197 return StringRef(Tok.Data, Tok.Length);
198 }
199
200 unsigned getReg() const {
Benjamin Kramer6aa49432010-12-07 15:50:35 +0000201 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
Bill Wendling7729e062010-11-09 22:44:22 +0000202 return Reg.RegNum;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000203 }
204
Bill Wendling5fa22a12010-11-09 23:28:44 +0000205 const SmallVectorImpl<unsigned> &getRegList() const {
Bill Wendling0f630752010-11-17 04:32:08 +0000206 assert((Kind == RegisterList || Kind == DPRRegisterList ||
207 Kind == SPRRegisterList) && "Invalid access!");
Bill Wendling24d22d22010-11-18 21:50:54 +0000208 return Registers;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000209 }
210
Kevin Enderbycfe07242009-10-13 22:19:02 +0000211 const MCExpr *getImm() const {
212 assert(Kind == Immediate && "Invalid access!");
213 return Imm.Val;
214 }
215
Daniel Dunbar8462b302010-08-11 06:36:53 +0000216 bool isCondCode() const { return Kind == CondCode; }
Jim Grosbachd67641b2010-12-06 18:21:12 +0000217 bool isCCOut() const { return Kind == CCOut; }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000218 bool isImm() const { return Kind == Immediate; }
Bill Wendlingb32e7842010-11-08 00:32:40 +0000219 bool isReg() const { return Kind == Register; }
Bill Wendling8d5acb72010-11-06 19:56:04 +0000220 bool isRegList() const { return Kind == RegisterList; }
Bill Wendling0f630752010-11-17 04:32:08 +0000221 bool isDPRRegList() const { return Kind == DPRRegisterList; }
222 bool isSPRRegList() const { return Kind == SPRRegisterList; }
Chris Lattner14b93852010-10-29 00:27:31 +0000223 bool isToken() const { return Kind == Token; }
224 bool isMemory() const { return Kind == Memory; }
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000225 bool isMemMode5() const {
226 if (!isMemory() || Mem.OffsetIsReg || Mem.OffsetRegShifted ||
227 Mem.Writeback || Mem.Negative)
228 return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000229
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000230 // If there is an offset expression, make sure it's valid.
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000231 if (!Mem.Offset) return true;
232
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000233 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000234 if (!CE) return false;
235
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000236 // The offset must be a multiple of 4 in the range 0-1020.
237 int64_t Value = CE->getValue();
238 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
239 }
Bill Wendlingf4caf692010-12-14 03:36:38 +0000240 bool isMemModeRegThumb() const {
241 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
242 return false;
243 return !Mem.Offset || !isa<MCConstantExpr>(Mem.Offset);
244 }
245 bool isMemModeImmThumb() const {
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000246 if (!isMemory() || (!Mem.OffsetIsReg && !Mem.Offset) || Mem.Writeback)
247 return false;
248
Bill Wendlingf4caf692010-12-14 03:36:38 +0000249 if (!Mem.Offset) return false;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000250
251 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
252 if (!CE) return false;
253
254 // The offset must be a multiple of 4 in the range 0-124.
255 uint64_t Value = CE->getValue();
256 return ((Value & 0x3) == 0 && Value <= 124);
257 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000258
259 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
Chris Lattner14b93852010-10-29 00:27:31 +0000260 // Add as immediates when possible. Null MCExpr = 0.
261 if (Expr == 0)
262 Inst.addOperand(MCOperand::CreateImm(0));
263 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000264 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
265 else
266 Inst.addOperand(MCOperand::CreateExpr(Expr));
267 }
268
Daniel Dunbar8462b302010-08-11 06:36:53 +0000269 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000270 assert(N == 2 && "Invalid number of operands!");
Daniel Dunbar8462b302010-08-11 06:36:53 +0000271 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
Jim Grosbach04f74942010-12-06 18:30:57 +0000272 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
273 Inst.addOperand(MCOperand::CreateReg(RegNum));
Daniel Dunbar8462b302010-08-11 06:36:53 +0000274 }
275
Jim Grosbachd67641b2010-12-06 18:21:12 +0000276 void addCCOutOperands(MCInst &Inst, unsigned N) const {
277 assert(N == 1 && "Invalid number of operands!");
278 Inst.addOperand(MCOperand::CreateReg(getReg()));
279 }
280
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000281 void addRegOperands(MCInst &Inst, unsigned N) const {
282 assert(N == 1 && "Invalid number of operands!");
283 Inst.addOperand(MCOperand::CreateReg(getReg()));
284 }
285
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000286 void addRegListOperands(MCInst &Inst, unsigned N) const {
Bill Wendling7729e062010-11-09 22:44:22 +0000287 assert(N == 1 && "Invalid number of operands!");
Bill Wendling5fa22a12010-11-09 23:28:44 +0000288 const SmallVectorImpl<unsigned> &RegList = getRegList();
289 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000290 I = RegList.begin(), E = RegList.end(); I != E; ++I)
291 Inst.addOperand(MCOperand::CreateReg(*I));
Bill Wendling87f4f9a2010-11-08 23:49:57 +0000292 }
293
Bill Wendling0f630752010-11-17 04:32:08 +0000294 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
295 addRegListOperands(Inst, N);
296 }
297
298 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
299 addRegListOperands(Inst, N);
300 }
301
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000302 void addImmOperands(MCInst &Inst, unsigned N) const {
303 assert(N == 1 && "Invalid number of operands!");
304 addExpr(Inst, getImm());
305 }
Jim Grosbach16c74252010-10-29 14:46:02 +0000306
Chris Lattner14b93852010-10-29 00:27:31 +0000307 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
308 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
Jim Grosbach16c74252010-10-29 14:46:02 +0000309
Chris Lattner14b93852010-10-29 00:27:31 +0000310 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlinga60f1572010-11-06 10:48:18 +0000311 assert(!Mem.OffsetIsReg && "Invalid mode 5 operand");
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000312
Jim Grosbach80eb2332010-10-29 17:41:25 +0000313 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
314 // the difference?
315 if (Mem.Offset) {
316 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000317 assert(CE && "Non-constant mode 5 offset operand!");
318
Jim Grosbach80eb2332010-10-29 17:41:25 +0000319 // The MCInst offset operand doesn't include the low two bits (like
320 // the instruction encoding).
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000321 int64_t Offset = CE->getValue() / 4;
322 if (Offset >= 0)
323 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
324 Offset)));
325 else
326 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
327 -Offset)));
328 } else {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000329 Inst.addOperand(MCOperand::CreateImm(0));
Bill Wendling92b5a2e2010-11-03 01:49:29 +0000330 }
Chris Lattner14b93852010-10-29 00:27:31 +0000331 }
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000332
Bill Wendlingf4caf692010-12-14 03:36:38 +0000333 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
334 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000335 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
Bill Wendlingf4caf692010-12-14 03:36:38 +0000336 Inst.addOperand(MCOperand::CreateReg(Mem.OffsetRegNum));
337 }
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000338
Bill Wendlingf4caf692010-12-14 03:36:38 +0000339 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
340 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
341 Inst.addOperand(MCOperand::CreateReg(Mem.BaseRegNum));
342 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Mem.Offset);
343 assert(CE && "Non-constant mode offset operand!");
344 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000345 }
346
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000347 virtual void dump(raw_ostream &OS) const;
Daniel Dunbarb3cb6962010-08-11 06:37:04 +0000348
Chris Lattner3a697562010-10-28 17:20:03 +0000349 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
350 ARMOperand *Op = new ARMOperand(CondCode);
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000351 Op->CC.Val = CC;
352 Op->StartLoc = S;
353 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000354 return Op;
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000355 }
356
Jim Grosbachd67641b2010-12-06 18:21:12 +0000357 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
358 ARMOperand *Op = new ARMOperand(CCOut);
359 Op->Reg.RegNum = RegNum;
360 Op->StartLoc = S;
361 Op->EndLoc = S;
362 return Op;
363 }
364
Chris Lattner3a697562010-10-28 17:20:03 +0000365 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
366 ARMOperand *Op = new ARMOperand(Token);
Sean Callanan76264762010-04-02 22:27:05 +0000367 Op->Tok.Data = Str.data();
368 Op->Tok.Length = Str.size();
369 Op->StartLoc = S;
370 Op->EndLoc = S;
Chris Lattner3a697562010-10-28 17:20:03 +0000371 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000372 }
373
Bill Wendling50d0f582010-11-18 23:43:05 +0000374 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
Chris Lattner3a697562010-10-28 17:20:03 +0000375 ARMOperand *Op = new ARMOperand(Register);
Sean Callanan76264762010-04-02 22:27:05 +0000376 Op->Reg.RegNum = RegNum;
Sean Callanan76264762010-04-02 22:27:05 +0000377 Op->StartLoc = S;
378 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000379 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000380 }
381
Bill Wendling7729e062010-11-09 22:44:22 +0000382 static ARMOperand *
Bill Wendling5fa22a12010-11-09 23:28:44 +0000383 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000384 SMLoc StartLoc, SMLoc EndLoc) {
Bill Wendling0f630752010-11-17 04:32:08 +0000385 KindTy Kind = RegisterList;
386
387 if (ARM::DPRRegClass.contains(Regs.front().first))
388 Kind = DPRRegisterList;
389 else if (ARM::SPRRegClass.contains(Regs.front().first))
390 Kind = SPRRegisterList;
391
392 ARMOperand *Op = new ARMOperand(Kind);
Bill Wendling5fa22a12010-11-09 23:28:44 +0000393 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000394 I = Regs.begin(), E = Regs.end(); I != E; ++I)
Bill Wendling24d22d22010-11-18 21:50:54 +0000395 Op->Registers.push_back(I->first);
Bill Wendlingcb21d1c2010-11-19 00:38:19 +0000396 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
Matt Beaumont-Gaycc8d10e2010-11-10 00:08:58 +0000397 Op->StartLoc = StartLoc;
398 Op->EndLoc = EndLoc;
Bill Wendling8d5acb72010-11-06 19:56:04 +0000399 return Op;
400 }
401
Chris Lattner3a697562010-10-28 17:20:03 +0000402 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
403 ARMOperand *Op = new ARMOperand(Immediate);
Sean Callanan76264762010-04-02 22:27:05 +0000404 Op->Imm.Val = Val;
Sean Callanan76264762010-04-02 22:27:05 +0000405 Op->StartLoc = S;
406 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000407 return Op;
Kevin Enderbycfe07242009-10-13 22:19:02 +0000408 }
409
Chris Lattner3a697562010-10-28 17:20:03 +0000410 static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg,
411 const MCExpr *Offset, unsigned OffsetRegNum,
412 bool OffsetRegShifted, enum ShiftType ShiftType,
413 const MCExpr *ShiftAmount, bool Preindexed,
414 bool Postindexed, bool Negative, bool Writeback,
415 SMLoc S, SMLoc E) {
416 ARMOperand *Op = new ARMOperand(Memory);
Sean Callanan76264762010-04-02 22:27:05 +0000417 Op->Mem.BaseRegNum = BaseRegNum;
418 Op->Mem.OffsetIsReg = OffsetIsReg;
419 Op->Mem.Offset = Offset;
420 Op->Mem.OffsetRegNum = OffsetRegNum;
421 Op->Mem.OffsetRegShifted = OffsetRegShifted;
422 Op->Mem.ShiftType = ShiftType;
423 Op->Mem.ShiftAmount = ShiftAmount;
424 Op->Mem.Preindexed = Preindexed;
425 Op->Mem.Postindexed = Postindexed;
426 Op->Mem.Negative = Negative;
427 Op->Mem.Writeback = Writeback;
Jim Grosbach16c74252010-10-29 14:46:02 +0000428
Sean Callanan76264762010-04-02 22:27:05 +0000429 Op->StartLoc = S;
430 Op->EndLoc = E;
Chris Lattner3a697562010-10-28 17:20:03 +0000431 return Op;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000432 }
433};
434
435} // end anonymous namespace.
436
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000437void ARMOperand::dump(raw_ostream &OS) const {
438 switch (Kind) {
439 case CondCode:
Daniel Dunbar6a5c22e2011-01-10 15:26:21 +0000440 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000441 break;
Jim Grosbachd67641b2010-12-06 18:21:12 +0000442 case CCOut:
443 OS << "<ccout " << getReg() << ">";
444 break;
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000445 case Immediate:
446 getImm()->print(OS);
447 break;
448 case Memory:
Bill Wendling50d0f582010-11-18 23:43:05 +0000449 OS << "<memory>";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000450 break;
451 case Register:
Bill Wendling50d0f582010-11-18 23:43:05 +0000452 OS << "<register " << getReg() << ">";
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000453 break;
Bill Wendling0f630752010-11-17 04:32:08 +0000454 case RegisterList:
455 case DPRRegisterList:
456 case SPRRegisterList: {
Bill Wendling8d5acb72010-11-06 19:56:04 +0000457 OS << "<register_list ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000458
Bill Wendling5fa22a12010-11-09 23:28:44 +0000459 const SmallVectorImpl<unsigned> &RegList = getRegList();
460 for (SmallVectorImpl<unsigned>::const_iterator
Bill Wendling7729e062010-11-09 22:44:22 +0000461 I = RegList.begin(), E = RegList.end(); I != E; ) {
462 OS << *I;
463 if (++I < E) OS << ", ";
Bill Wendling8d5acb72010-11-06 19:56:04 +0000464 }
465
466 OS << ">";
467 break;
468 }
Daniel Dunbarfa315de2010-08-11 06:37:12 +0000469 case Token:
470 OS << "'" << getToken() << "'";
471 break;
472 }
473}
Daniel Dunbar3483aca2010-08-11 05:24:50 +0000474
475/// @name Auto-generated Match Functions
476/// {
477
478static unsigned MatchRegisterName(StringRef Name);
479
480/// }
481
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000482/// Try to parse a register name. The token must be an Identifier when called,
Chris Lattnere5658fa2010-10-30 04:09:10 +0000483/// and if it is a register name the token is eaten and the register number is
484/// returned. Otherwise return -1.
485///
486int ARMAsmParser::TryParseRegister() {
487 const AsmToken &Tok = Parser.getTok();
488 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
Jim Grosbachd4462a52010-11-01 16:44:21 +0000489
Chris Lattnere5658fa2010-10-30 04:09:10 +0000490 // FIXME: Validate register for the current architecture; we have to do
491 // validation later, so maybe there is no need for this here.
Bill Wendlingd68fd9c2010-11-06 10:45:34 +0000492 unsigned RegNum = MatchRegisterName(Tok.getString());
493 if (RegNum == 0)
Chris Lattnere5658fa2010-10-30 04:09:10 +0000494 return -1;
495 Parser.Lex(); // Eat identifier token.
496 return RegNum;
497}
Jim Grosbachd4462a52010-11-01 16:44:21 +0000498
499
Bill Wendling50d0f582010-11-18 23:43:05 +0000500/// Try to parse a register name. The token must be an Identifier when called.
501/// If it's a register, an AsmOperand is created. Another AsmOperand is created
502/// if there is a "writeback". 'true' if it's not a register.
Chris Lattner3a697562010-10-28 17:20:03 +0000503///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000504/// TODO this is likely to change to allow different register types and or to
505/// parse for a specific register type.
Bill Wendling50d0f582010-11-18 23:43:05 +0000506bool ARMAsmParser::
507TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000508 SMLoc S = Parser.getTok().getLoc();
509 int RegNo = TryParseRegister();
Bill Wendlinge7176102010-11-06 22:36:58 +0000510 if (RegNo == -1)
Bill Wendling50d0f582010-11-18 23:43:05 +0000511 return true;
Jim Grosbachd4462a52010-11-01 16:44:21 +0000512
Bill Wendling50d0f582010-11-18 23:43:05 +0000513 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000514
Chris Lattnere5658fa2010-10-30 04:09:10 +0000515 const AsmToken &ExclaimTok = Parser.getTok();
516 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000517 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
518 ExclaimTok.getLoc()));
Chris Lattnere5658fa2010-10-30 04:09:10 +0000519 Parser.Lex(); // Eat exclaim token
Kevin Enderby99e6d4e2009-10-07 18:01:35 +0000520 }
521
Bill Wendling50d0f582010-11-18 23:43:05 +0000522 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000523}
524
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000525/// Parse a register list, return it if successful else return null. The first
526/// token must be a '{' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000527bool ARMAsmParser::
528ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan18b83232010-01-19 21:44:56 +0000529 assert(Parser.getTok().is(AsmToken::LCurly) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000530 "Token is not a Left Curly Brace");
Bill Wendlinge7176102010-11-06 22:36:58 +0000531 SMLoc S = Parser.getTok().getLoc();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000532
Bill Wendling7729e062010-11-09 22:44:22 +0000533 // Read the rest of the registers in the list.
534 unsigned PrevRegNum = 0;
Bill Wendling5fa22a12010-11-09 23:28:44 +0000535 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000536
Bill Wendling7729e062010-11-09 22:44:22 +0000537 do {
Bill Wendlinge7176102010-11-06 22:36:58 +0000538 bool IsRange = Parser.getTok().is(AsmToken::Minus);
Bill Wendling7729e062010-11-09 22:44:22 +0000539 Parser.Lex(); // Eat non-identifier token.
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000540
Sean Callanan18b83232010-01-19 21:44:56 +0000541 const AsmToken &RegTok = Parser.getTok();
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000542 SMLoc RegLoc = RegTok.getLoc();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000543 if (RegTok.isNot(AsmToken::Identifier)) {
544 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000545 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000546 }
Bill Wendlinge7176102010-11-06 22:36:58 +0000547
Bill Wendling1d6a2652010-11-06 10:40:24 +0000548 int RegNum = TryParseRegister();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000549 if (RegNum == -1) {
550 Error(RegLoc, "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000551 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000552 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000553
Bill Wendlinge7176102010-11-06 22:36:58 +0000554 if (IsRange) {
555 int Reg = PrevRegNum;
556 do {
557 ++Reg;
558 Registers.push_back(std::make_pair(Reg, RegLoc));
559 } while (Reg != RegNum);
560 } else {
561 Registers.push_back(std::make_pair(RegNum, RegLoc));
562 }
563
564 PrevRegNum = RegNum;
Bill Wendling7729e062010-11-09 22:44:22 +0000565 } while (Parser.getTok().is(AsmToken::Comma) ||
566 Parser.getTok().is(AsmToken::Minus));
Bill Wendlinge7176102010-11-06 22:36:58 +0000567
568 // Process the right curly brace of the list.
Sean Callanan18b83232010-01-19 21:44:56 +0000569 const AsmToken &RCurlyTok = Parser.getTok();
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000570 if (RCurlyTok.isNot(AsmToken::RCurly)) {
571 Error(RCurlyTok.getLoc(), "'}' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000572 return true;
Chris Lattnerc0ddfaa2010-10-28 17:23:41 +0000573 }
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000574
Bill Wendlinge7176102010-11-06 22:36:58 +0000575 SMLoc E = RCurlyTok.getLoc();
576 Parser.Lex(); // Eat right curly brace token.
Jim Grosbach03f44a02010-11-29 23:18:01 +0000577
Bill Wendlinge7176102010-11-06 22:36:58 +0000578 // Verify the register list.
Bill Wendling5fa22a12010-11-09 23:28:44 +0000579 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
Bill Wendlinge7176102010-11-06 22:36:58 +0000580 RI = Registers.begin(), RE = Registers.end();
581
Bill Wendlinge7176102010-11-06 22:36:58 +0000582 DenseMap<unsigned, bool> RegMap;
583 RegMap[RI->first] = true;
584
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000585 unsigned HighRegNum = RI->first;
586 bool EmittedWarning = false;
587
Bill Wendlinge7176102010-11-06 22:36:58 +0000588 for (++RI; RI != RE; ++RI) {
Bill Wendling7729e062010-11-09 22:44:22 +0000589 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000590 unsigned Reg = RegInfo.first;
Bill Wendlinge7176102010-11-06 22:36:58 +0000591
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000592 if (RegMap[Reg]) {
Bill Wendlinge7176102010-11-06 22:36:58 +0000593 Error(RegInfo.second, "register duplicated in register list");
Bill Wendling50d0f582010-11-18 23:43:05 +0000594 return true;
Bill Wendlinge7176102010-11-06 22:36:58 +0000595 }
596
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000597 if (!EmittedWarning && Reg < HighRegNum)
Bill Wendlinge7176102010-11-06 22:36:58 +0000598 Warning(RegInfo.second,
599 "register not in ascending order in register list");
600
Bill Wendling8e8b18b2010-11-09 23:45:59 +0000601 RegMap[Reg] = true;
602 HighRegNum = std::max(Reg, HighRegNum);
Bill Wendlinge7176102010-11-06 22:36:58 +0000603 }
604
Bill Wendling50d0f582010-11-18 23:43:05 +0000605 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
606 return false;
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000607}
608
Bill Wendlinge7176102010-11-06 22:36:58 +0000609/// Parse an ARM memory expression, return false if successful else return true
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000610/// or an error. The first token must be a '[' when called.
Bill Wendling50d0f582010-11-18 23:43:05 +0000611///
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000612/// TODO Only preindexing and postindexing addressing are started, unindexed
613/// with option, etc are still to do.
Bill Wendling50d0f582010-11-18 23:43:05 +0000614bool ARMAsmParser::
615ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
Sean Callanan76264762010-04-02 22:27:05 +0000616 SMLoc S, E;
Sean Callanan18b83232010-01-19 21:44:56 +0000617 assert(Parser.getTok().is(AsmToken::LBrac) &&
Bill Wendlinga60f1572010-11-06 10:48:18 +0000618 "Token is not a Left Bracket");
Sean Callanan76264762010-04-02 22:27:05 +0000619 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000620 Parser.Lex(); // Eat left bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000621
Sean Callanan18b83232010-01-19 21:44:56 +0000622 const AsmToken &BaseRegTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000623 if (BaseRegTok.isNot(AsmToken::Identifier)) {
624 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000625 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000626 }
Chris Lattnere5658fa2010-10-30 04:09:10 +0000627 int BaseRegNum = TryParseRegister();
628 if (BaseRegNum == -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000629 Error(BaseRegTok.getLoc(), "register expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000630 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000631 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000632
633 bool Preindexed = false;
634 bool Postindexed = false;
635 bool OffsetIsReg = false;
636 bool Negative = false;
637 bool Writeback = false;
638
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000639 // First look for preindexed address forms, that is after the "[Rn" we now
640 // have to see if the next token is a comma.
Sean Callanan18b83232010-01-19 21:44:56 +0000641 const AsmToken &Tok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000642 if (Tok.is(AsmToken::Comma)) {
643 Preindexed = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000644 Parser.Lex(); // Eat comma token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000645 int OffsetRegNum;
646 bool OffsetRegShifted;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000647 enum ShiftType ShiftType;
Bill Wendlingef4a68b2010-11-30 07:44:32 +0000648 const MCExpr *ShiftAmount = 0;
649 const MCExpr *Offset = 0;
Chris Lattner550276e2010-10-28 20:52:15 +0000650 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
651 Offset, OffsetIsReg, OffsetRegNum, E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000652 return true;
Sean Callanan18b83232010-01-19 21:44:56 +0000653 const AsmToken &RBracTok = Parser.getTok();
Chris Lattner550276e2010-10-28 20:52:15 +0000654 if (RBracTok.isNot(AsmToken::RBrac)) {
655 Error(RBracTok.getLoc(), "']' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000656 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000657 }
Sean Callanan76264762010-04-02 22:27:05 +0000658 E = RBracTok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000659 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000660
Jim Grosbach03f44a02010-11-29 23:18:01 +0000661
Sean Callanan18b83232010-01-19 21:44:56 +0000662 const AsmToken &ExclaimTok = Parser.getTok();
Bill Wendling50d0f582010-11-18 23:43:05 +0000663 ARMOperand *WBOp = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000664 if (ExclaimTok.is(AsmToken::Exclaim)) {
Bill Wendling50d0f582010-11-18 23:43:05 +0000665 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
666 ExclaimTok.getLoc());
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000667 Writeback = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000668 Parser.Lex(); // Eat exclaim token
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000669 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000670
671 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
672 OffsetRegNum, OffsetRegShifted,
673 ShiftType, ShiftAmount, Preindexed,
674 Postindexed, Negative, Writeback,
675 S, E));
676 if (WBOp)
677 Operands.push_back(WBOp);
678
679 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000680 }
681 // The "[Rn" we have so far was not followed by a comma.
682 else if (Tok.is(AsmToken::RBrac)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000683 // If there's anything other than the right brace, this is a post indexing
684 // addressing form.
Sean Callanan76264762010-04-02 22:27:05 +0000685 E = Tok.getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000686 Parser.Lex(); // Eat right bracket token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000687
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000688 int OffsetRegNum = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000689 bool OffsetRegShifted = false;
Jim Grosbach00a257a2010-11-29 23:41:10 +0000690 enum ShiftType ShiftType = Lsl;
691 const MCExpr *ShiftAmount = 0;
Chris Lattner14b93852010-10-29 00:27:31 +0000692 const MCExpr *Offset = 0;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000693
Sean Callanan18b83232010-01-19 21:44:56 +0000694 const AsmToken &NextTok = Parser.getTok();
Jim Grosbach03f44a02010-11-29 23:18:01 +0000695
Kevin Enderbye2a98dd2009-10-15 21:42:45 +0000696 if (NextTok.isNot(AsmToken::EndOfStatement)) {
Jim Grosbach80eb2332010-10-29 17:41:25 +0000697 Postindexed = true;
698 Writeback = true;
Bill Wendling50d0f582010-11-18 23:43:05 +0000699
Chris Lattner550276e2010-10-28 20:52:15 +0000700 if (NextTok.isNot(AsmToken::Comma)) {
701 Error(NextTok.getLoc(), "',' expected");
Bill Wendling50d0f582010-11-18 23:43:05 +0000702 return true;
Chris Lattner550276e2010-10-28 20:52:15 +0000703 }
Bill Wendling50d0f582010-11-18 23:43:05 +0000704
Sean Callananb9a25b72010-01-19 20:27:46 +0000705 Parser.Lex(); // Eat comma token.
Bill Wendling50d0f582010-11-18 23:43:05 +0000706
Chris Lattner550276e2010-10-28 20:52:15 +0000707 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
Jim Grosbach16c74252010-10-29 14:46:02 +0000708 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
Chris Lattner550276e2010-10-28 20:52:15 +0000709 E))
Bill Wendling50d0f582010-11-18 23:43:05 +0000710 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000711 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000712
Bill Wendling50d0f582010-11-18 23:43:05 +0000713 Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset,
714 OffsetRegNum, OffsetRegShifted,
715 ShiftType, ShiftAmount, Preindexed,
716 Postindexed, Negative, Writeback,
717 S, E));
718 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000719 }
720
Bill Wendling50d0f582010-11-18 23:43:05 +0000721 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000722}
723
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000724/// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
725/// we will parse the following (were +/- means that a plus or minus is
726/// optional):
727/// +/-Rm
728/// +/-Rm, shift
729/// #offset
730/// we return false on success or an error otherwise.
731bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
Sean Callanan76264762010-04-02 22:27:05 +0000732 bool &OffsetRegShifted,
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000733 enum ShiftType &ShiftType,
734 const MCExpr *&ShiftAmount,
735 const MCExpr *&Offset,
736 bool &OffsetIsReg,
Sean Callanan76264762010-04-02 22:27:05 +0000737 int &OffsetRegNum,
738 SMLoc &E) {
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000739 Negative = false;
740 OffsetRegShifted = false;
741 OffsetIsReg = false;
742 OffsetRegNum = -1;
Sean Callanan18b83232010-01-19 21:44:56 +0000743 const AsmToken &NextTok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000744 E = NextTok.getLoc();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000745 if (NextTok.is(AsmToken::Plus))
Sean Callananb9a25b72010-01-19 20:27:46 +0000746 Parser.Lex(); // Eat plus token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000747 else if (NextTok.is(AsmToken::Minus)) {
748 Negative = true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000749 Parser.Lex(); // Eat minus token
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000750 }
751 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
Sean Callanan18b83232010-01-19 21:44:56 +0000752 const AsmToken &OffsetRegTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000753 if (OffsetRegTok.is(AsmToken::Identifier)) {
Chris Lattnere5658fa2010-10-30 04:09:10 +0000754 SMLoc CurLoc = OffsetRegTok.getLoc();
755 OffsetRegNum = TryParseRegister();
756 if (OffsetRegNum != -1) {
Chris Lattner550276e2010-10-28 20:52:15 +0000757 OffsetIsReg = true;
Chris Lattnere5658fa2010-10-30 04:09:10 +0000758 E = CurLoc;
Sean Callanan76264762010-04-02 22:27:05 +0000759 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000760 }
Jim Grosbachd4462a52010-11-01 16:44:21 +0000761
Bill Wendling12f40e92010-11-06 10:51:53 +0000762 // If we parsed a register as the offset then there can be a shift after that.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000763 if (OffsetRegNum != -1) {
764 // Look for a comma then a shift
Sean Callanan18b83232010-01-19 21:44:56 +0000765 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000766 if (Tok.is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +0000767 Parser.Lex(); // Eat comma token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000768
Sean Callanan18b83232010-01-19 21:44:56 +0000769 const AsmToken &Tok = Parser.getTok();
Sean Callanan76264762010-04-02 22:27:05 +0000770 if (ParseShift(ShiftType, ShiftAmount, E))
Duncan Sands34727662010-07-12 08:16:59 +0000771 return Error(Tok.getLoc(), "shift expected");
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000772 OffsetRegShifted = true;
773 }
774 }
775 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
776 // Look for #offset following the "[Rn," or "[Rn],"
Sean Callanan18b83232010-01-19 21:44:56 +0000777 const AsmToken &HashTok = Parser.getTok();
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000778 if (HashTok.isNot(AsmToken::Hash))
779 return Error(HashTok.getLoc(), "'#' expected");
Jim Grosbach16c74252010-10-29 14:46:02 +0000780
Sean Callananb9a25b72010-01-19 20:27:46 +0000781 Parser.Lex(); // Eat hash token.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000782
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000783 if (getParser().ParseExpression(Offset))
784 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000785 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000786 }
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000787 return false;
788}
789
790/// ParseShift as one of these two:
791/// ( lsl | lsr | asr | ror ) , # shift_amount
792/// rrx
793/// and returns true if it parses a shift otherwise it returns false.
Jim Grosbach16c74252010-10-29 14:46:02 +0000794bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount,
Sean Callanan76264762010-04-02 22:27:05 +0000795 SMLoc &E) {
Sean Callanan18b83232010-01-19 21:44:56 +0000796 const AsmToken &Tok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000797 if (Tok.isNot(AsmToken::Identifier))
798 return true;
Benjamin Kramer38e59892010-07-14 22:38:02 +0000799 StringRef ShiftName = Tok.getString();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000800 if (ShiftName == "lsl" || ShiftName == "LSL")
801 St = Lsl;
802 else if (ShiftName == "lsr" || ShiftName == "LSR")
803 St = Lsr;
804 else if (ShiftName == "asr" || ShiftName == "ASR")
805 St = Asr;
806 else if (ShiftName == "ror" || ShiftName == "ROR")
807 St = Ror;
808 else if (ShiftName == "rrx" || ShiftName == "RRX")
809 St = Rrx;
810 else
811 return true;
Sean Callananb9a25b72010-01-19 20:27:46 +0000812 Parser.Lex(); // Eat shift type token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000813
814 // Rrx stands alone.
815 if (St == Rrx)
816 return false;
817
818 // Otherwise, there must be a '#' and a shift amount.
Sean Callanan18b83232010-01-19 21:44:56 +0000819 const AsmToken &HashTok = Parser.getTok();
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000820 if (HashTok.isNot(AsmToken::Hash))
821 return Error(HashTok.getLoc(), "'#' expected");
Sean Callananb9a25b72010-01-19 20:27:46 +0000822 Parser.Lex(); // Eat hash token.
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000823
824 if (getParser().ParseExpression(ShiftAmount))
825 return true;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000826
827 return false;
828}
829
Kevin Enderby9c41fa82009-10-30 22:55:57 +0000830/// Parse a arm instruction operand. For now this parses the operand regardless
831/// of the mnemonic.
Bill Wendling50d0f582010-11-18 23:43:05 +0000832bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands){
Sean Callanan76264762010-04-02 22:27:05 +0000833 SMLoc S, E;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000834 switch (getLexer().getKind()) {
Bill Wendling146018f2010-11-06 21:42:12 +0000835 default:
836 Error(Parser.getTok().getLoc(), "unexpected token in operand");
Bill Wendling50d0f582010-11-18 23:43:05 +0000837 return true;
838 case AsmToken::Identifier: {
839 if (!TryParseRegisterWithWriteBack(Operands))
840 return false;
Jim Grosbach16c74252010-10-29 14:46:02 +0000841
Kevin Enderby515d5092009-10-15 20:48:48 +0000842 // This was not a register so parse other operands that start with an
843 // identifier (like labels) as expressions and create them as immediates.
844 const MCExpr *IdVal;
Sean Callanan76264762010-04-02 22:27:05 +0000845 S = Parser.getTok().getLoc();
Kevin Enderby515d5092009-10-15 20:48:48 +0000846 if (getParser().ParseExpression(IdVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000847 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000848 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000849 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
850 return false;
851 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000852 case AsmToken::LBrac:
Bill Wendling50d0f582010-11-18 23:43:05 +0000853 return ParseMemory(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000854 case AsmToken::LCurly:
Bill Wendling50d0f582010-11-18 23:43:05 +0000855 return ParseRegisterList(Operands);
Kevin Enderbyd7894f12009-10-09 21:12:28 +0000856 case AsmToken::Hash:
Kevin Enderby079469f2009-10-13 23:33:38 +0000857 // #42 -> immediate.
858 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
Sean Callanan76264762010-04-02 22:27:05 +0000859 S = Parser.getTok().getLoc();
Sean Callananb9a25b72010-01-19 20:27:46 +0000860 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +0000861 const MCExpr *ImmVal;
862 if (getParser().ParseExpression(ImmVal))
Bill Wendling50d0f582010-11-18 23:43:05 +0000863 return true;
Sean Callanan76264762010-04-02 22:27:05 +0000864 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
Bill Wendling50d0f582010-11-18 23:43:05 +0000865 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
866 return false;
Kevin Enderbya7ba3a82009-10-06 22:26:42 +0000867 }
868}
869
Daniel Dunbar352e1482011-01-11 15:59:50 +0000870/// \brief Given a mnemonic, split out possible predication code and carry
871/// setting letters to form a canonical mnemonic and flags.
872//
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000873// FIXME: Would be nice to autogen this.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000874static StringRef SplitMnemonicAndCC(StringRef Mnemonic,
875 unsigned &PredicationCode,
876 bool &CarrySetting) {
877 PredicationCode = ARMCC::AL;
878 CarrySetting = false;
879
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000880 // Ignore some mnemonics we know aren't predicated forms.
Daniel Dunbar352e1482011-01-11 15:59:50 +0000881 //
882 // FIXME: Would be nice to autogen this.
Daniel Dunbar8ab11122011-01-10 21:01:03 +0000883 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
884 Mnemonic == "movs" ||
885 Mnemonic == "svc" ||
886 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
887 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
888 Mnemonic == "vacge" || Mnemonic == "vcge" ||
889 Mnemonic == "vclt" ||
890 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
891 Mnemonic == "vcle" ||
892 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
893 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
894 Mnemonic == "vqdmlal"))
Daniel Dunbar352e1482011-01-11 15:59:50 +0000895 return Mnemonic;
Daniel Dunbar5747b132010-08-11 06:37:16 +0000896
Daniel Dunbar352e1482011-01-11 15:59:50 +0000897 // First, split out any predication code.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000898 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000899 .Case("eq", ARMCC::EQ)
900 .Case("ne", ARMCC::NE)
901 .Case("hs", ARMCC::HS)
902 .Case("lo", ARMCC::LO)
903 .Case("mi", ARMCC::MI)
904 .Case("pl", ARMCC::PL)
905 .Case("vs", ARMCC::VS)
906 .Case("vc", ARMCC::VC)
907 .Case("hi", ARMCC::HI)
908 .Case("ls", ARMCC::LS)
909 .Case("ge", ARMCC::GE)
910 .Case("lt", ARMCC::LT)
911 .Case("gt", ARMCC::GT)
912 .Case("le", ARMCC::LE)
913 .Case("al", ARMCC::AL)
914 .Default(~0U);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000915 if (CC != ~0U) {
916 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
Daniel Dunbar352e1482011-01-11 15:59:50 +0000917 PredicationCode = CC;
Bill Wendling52925b62010-10-29 23:50:21 +0000918 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +0000919
Daniel Dunbar352e1482011-01-11 15:59:50 +0000920 // Next, determine if we have a carry setting bit. We explicitly ignore all
921 // the instructions we know end in 's'.
922 if (Mnemonic.endswith("s") &&
923 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
924 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
925 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
926 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
927 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
928 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
929 CarrySetting = true;
930 }
931
932 return Mnemonic;
933}
Daniel Dunbar3771dd02011-01-11 15:59:53 +0000934
935/// \brief Given a canonical mnemonic, determine if the instruction ever allows
936/// inclusion of carry set or predication code operands.
937//
938// FIXME: It would be nice to autogen this.
939static void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
940 bool &CanAcceptPredicationCode) {
Daniel Dunbareb9f3f92011-01-11 19:06:29 +0000941 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
942 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
943 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
944 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
945 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
946 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
947 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
948 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
949 CanAcceptCarrySet = true;
950 } else {
951 CanAcceptCarrySet = false;
952 }
Daniel Dunbar3771dd02011-01-11 15:59:53 +0000953
Daniel Dunbareb9f3f92011-01-11 19:06:29 +0000954 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
955 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
956 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
957 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
958 Mnemonic == "dsb" || Mnemonic == "movs") {
Daniel Dunbar3771dd02011-01-11 15:59:53 +0000959 CanAcceptPredicationCode = false;
960 } else {
961 CanAcceptPredicationCode = true;
962 }
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000963}
964
965/// Parse an arm instruction mnemonic followed by its operands.
966bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
967 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
968 // Create the leading tokens for the mnemonic, split by '.' characters.
969 size_t Start = 0, Next = Name.find('.');
970 StringRef Head = Name.slice(Start, Next);
971
Daniel Dunbar352e1482011-01-11 15:59:50 +0000972 // Split out the predication code and carry setting flag from the mnemonic.
973 unsigned PredicationCode;
974 bool CarrySetting;
975 Head = SplitMnemonicAndCC(Head, PredicationCode, CarrySetting);
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +0000976
Chris Lattner3a697562010-10-28 17:20:03 +0000977 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Bill Wendling9717fa92010-11-21 10:56:05 +0000978
Daniel Dunbar3771dd02011-01-11 15:59:53 +0000979 // Next, add the CCOut and ConditionCode operands, if needed.
980 //
981 // For mnemonics which can ever incorporate a carry setting bit or predication
982 // code, our matching model involves us always generating CCOut and
983 // ConditionCode operands to match the mnemonic "as written" and then we let
984 // the matcher deal with finding the right instruction or generating an
985 // appropriate error.
986 bool CanAcceptCarrySet, CanAcceptPredicationCode;
987 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
988
989 // Add the carry setting operand, if necessary.
990 //
991 // FIXME: It would be awesome if we could somehow invent a location such that
992 // match errors on this operand would print a nice diagnostic about how the
993 // 's' character in the mnemonic resulted in a CCOut operand.
994 if (CanAcceptCarrySet) {
995 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
996 NameLoc));
997 } else {
998 // This mnemonic can't ever accept a carry set, but the user wrote one (or
999 // misspelled another mnemonic).
1000
1001 // FIXME: Issue a nice error.
1002 }
1003
1004 // Add the predication code operand, if necessary.
1005 if (CanAcceptPredicationCode) {
1006 Operands.push_back(ARMOperand::CreateCondCode(
1007 ARMCC::CondCodes(PredicationCode), NameLoc));
1008 } else {
1009 // This mnemonic can't ever accept a predication code, but the user wrote
1010 // one (or misspelled another mnemonic).
1011
1012 // FIXME: Issue a nice error.
Daniel Dunbarbadbd2f2011-01-10 12:24:52 +00001013 }
Daniel Dunbar345a9a62010-08-11 06:37:20 +00001014
1015 // Add the remaining tokens in the mnemonic.
Daniel Dunbar5747b132010-08-11 06:37:16 +00001016 while (Next != StringRef::npos) {
1017 Start = Next;
1018 Next = Name.find('.', Start + 1);
1019 Head = Name.slice(Start, Next);
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001020
Chris Lattner3a697562010-10-28 17:20:03 +00001021 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
Daniel Dunbar5747b132010-08-11 06:37:16 +00001022 }
1023
1024 // Read the remaining operands.
1025 if (getLexer().isNot(AsmToken::EndOfStatement)) {
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001026 // Read the first operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001027 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001028 Parser.EatToEndOfStatement();
1029 return true;
1030 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001031
1032 while (getLexer().is(AsmToken::Comma)) {
Sean Callananb9a25b72010-01-19 20:27:46 +00001033 Parser.Lex(); // Eat the comma.
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001034
1035 // Parse and remember the operand.
Bill Wendling50d0f582010-11-18 23:43:05 +00001036 if (ParseOperand(Operands)) {
Chris Lattnercbf8a982010-09-11 16:18:25 +00001037 Parser.EatToEndOfStatement();
1038 return true;
1039 }
Kevin Enderbya7ba3a82009-10-06 22:26:42 +00001040 }
1041 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001042
Chris Lattnercbf8a982010-09-11 16:18:25 +00001043 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1044 Parser.EatToEndOfStatement();
Chris Lattner34e53142010-09-08 05:10:46 +00001045 return TokError("unexpected token in argument list");
Chris Lattnercbf8a982010-09-11 16:18:25 +00001046 }
Bill Wendling146018f2010-11-06 21:42:12 +00001047
Chris Lattner34e53142010-09-08 05:10:46 +00001048 Parser.Lex(); // Consume the EndOfStatement
Chris Lattner98986712010-01-14 22:21:20 +00001049 return false;
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001050}
1051
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001052bool ARMAsmParser::
1053MatchAndEmitInstruction(SMLoc IDLoc,
1054 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1055 MCStreamer &Out) {
1056 MCInst Inst;
1057 unsigned ErrorInfo;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001058 MatchResultTy MatchResult, MatchResult2;
1059 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1060 if (MatchResult != Match_Success) {
1061 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1062 // that does not update the condition codes. So try adding a CCOut operand
1063 // with a value of reg0.
1064 if (MatchResult == Match_InvalidOperand) {
1065 Operands.insert(Operands.begin() + 1,
1066 ARMOperand::CreateCCOut(0,
1067 ((ARMOperand*)Operands[0])->getStartLoc()));
1068 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1069 if (MatchResult2 == Match_Success)
1070 MatchResult = Match_Success;
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001071 else {
1072 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001073 Operands.erase(Operands.begin() + 1);
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001074 delete CCOut;
1075 }
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001076 }
1077 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1078 // that updates the condition codes if it ends in 's'. So see if the
1079 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1080 // operand with a value of CPSR.
1081 else if(MatchResult == Match_MnemonicFail) {
1082 // Get the instruction mnemonic, which is the first token.
1083 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1084 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1085 // removed the 's' from the mnemonic for matching.
1086 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1087 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001088 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1089 Operands.erase(Operands.begin());
1090 delete OldMnemonic;
1091 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001092 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1093 Operands.insert(Operands.begin() + 1,
1094 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1095 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1096 if (MatchResult2 == Match_Success)
1097 MatchResult = Match_Success;
1098 else {
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001099 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1100 Operands.erase(Operands.begin());
1101 delete OldMnemonic;
1102 Operands.insert(Operands.begin(),
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001103 ARMOperand::CreateToken(Mnemonic, NameLoc));
Kevin Enderby44a9e8f2010-12-10 01:41:56 +00001104 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1105 Operands.erase(Operands.begin() + 1);
1106 delete CCOut;
Kevin Enderby193c3ac2010-12-09 19:19:43 +00001107 }
1108 }
1109 }
1110 }
1111 switch (MatchResult) {
Chris Lattnere73d4f82010-10-28 21:41:58 +00001112 case Match_Success:
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001113 Out.EmitInstruction(Inst);
1114 return false;
Chris Lattnere73d4f82010-10-28 21:41:58 +00001115 case Match_MissingFeature:
1116 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
1117 return true;
1118 case Match_InvalidOperand: {
1119 SMLoc ErrorLoc = IDLoc;
1120 if (ErrorInfo != ~0U) {
1121 if (ErrorInfo >= Operands.size())
1122 return Error(IDLoc, "too few operands for instruction");
Jim Grosbach16c74252010-10-29 14:46:02 +00001123
Chris Lattnere73d4f82010-10-28 21:41:58 +00001124 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
1125 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
1126 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001127
Chris Lattnere73d4f82010-10-28 21:41:58 +00001128 return Error(ErrorLoc, "invalid operand for instruction");
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001129 }
Chris Lattnere73d4f82010-10-28 21:41:58 +00001130 case Match_MnemonicFail:
1131 return Error(IDLoc, "unrecognized instruction mnemonic");
1132 }
Jim Grosbach16c74252010-10-29 14:46:02 +00001133
Eric Christopherc223e2b2010-10-29 09:26:59 +00001134 llvm_unreachable("Implement any new match types added!");
Bill Wendling146018f2010-11-06 21:42:12 +00001135 return true;
Chris Lattnerfa42fad2010-10-28 21:28:01 +00001136}
1137
Kevin Enderby515d5092009-10-15 20:48:48 +00001138/// ParseDirective parses the arm specific directives
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001139bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
1140 StringRef IDVal = DirectiveID.getIdentifier();
1141 if (IDVal == ".word")
1142 return ParseDirectiveWord(4, DirectiveID.getLoc());
Kevin Enderby515d5092009-10-15 20:48:48 +00001143 else if (IDVal == ".thumb")
1144 return ParseDirectiveThumb(DirectiveID.getLoc());
1145 else if (IDVal == ".thumb_func")
1146 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
1147 else if (IDVal == ".code")
1148 return ParseDirectiveCode(DirectiveID.getLoc());
1149 else if (IDVal == ".syntax")
1150 return ParseDirectiveSyntax(DirectiveID.getLoc());
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001151 return true;
1152}
1153
1154/// ParseDirectiveWord
1155/// ::= .word [ expression (, expression)* ]
1156bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
1157 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1158 for (;;) {
1159 const MCExpr *Value;
1160 if (getParser().ParseExpression(Value))
1161 return true;
1162
Chris Lattneraaec2052010-01-19 19:46:13 +00001163 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001164
1165 if (getLexer().is(AsmToken::EndOfStatement))
1166 break;
Jim Grosbach16c74252010-10-29 14:46:02 +00001167
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001168 // FIXME: Improve diagnostic.
1169 if (getLexer().isNot(AsmToken::Comma))
1170 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001171 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001172 }
1173 }
1174
Sean Callananb9a25b72010-01-19 20:27:46 +00001175 Parser.Lex();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001176 return false;
1177}
1178
Kevin Enderby515d5092009-10-15 20:48:48 +00001179/// ParseDirectiveThumb
1180/// ::= .thumb
1181bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
1182 if (getLexer().isNot(AsmToken::EndOfStatement))
1183 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001184 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001185
1186 // TODO: set thumb mode
1187 // TODO: tell the MC streamer the mode
1188 // getParser().getStreamer().Emit???();
1189 return false;
1190}
1191
1192/// ParseDirectiveThumbFunc
1193/// ::= .thumbfunc symbol_name
1194bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001195 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001196 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
Jim Grosbach83c40182010-11-05 22:11:33 +00001197 return Error(L, "unexpected token in .thumb_func directive");
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001198 StringRef Name = Tok.getString();
Sean Callananb9a25b72010-01-19 20:27:46 +00001199 Parser.Lex(); // Consume the identifier token.
Kevin Enderby515d5092009-10-15 20:48:48 +00001200 if (getLexer().isNot(AsmToken::EndOfStatement))
1201 return Error(L, "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001202 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001203
Jim Grosbach642fc9c2010-11-05 22:33:53 +00001204 // Mark symbol as a thumb symbol.
1205 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
1206 getParser().getStreamer().EmitThumbFunc(Func);
Kevin Enderby515d5092009-10-15 20:48:48 +00001207 return false;
1208}
1209
1210/// ParseDirectiveSyntax
1211/// ::= .syntax unified | divided
1212bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001213 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001214 if (Tok.isNot(AsmToken::Identifier))
1215 return Error(L, "unexpected token in .syntax directive");
Benjamin Kramer38e59892010-07-14 22:38:02 +00001216 StringRef Mode = Tok.getString();
Duncan Sands58c86912010-06-29 13:04:35 +00001217 if (Mode == "unified" || Mode == "UNIFIED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001218 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001219 else if (Mode == "divided" || Mode == "DIVIDED")
Sean Callananb9a25b72010-01-19 20:27:46 +00001220 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001221 else
1222 return Error(L, "unrecognized syntax mode in .syntax directive");
1223
1224 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001225 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001226 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001227
1228 // TODO tell the MC streamer the mode
1229 // getParser().getStreamer().Emit???();
1230 return false;
1231}
1232
1233/// ParseDirectiveCode
1234/// ::= .code 16 | 32
1235bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
Sean Callanan18b83232010-01-19 21:44:56 +00001236 const AsmToken &Tok = Parser.getTok();
Kevin Enderby515d5092009-10-15 20:48:48 +00001237 if (Tok.isNot(AsmToken::Integer))
1238 return Error(L, "unexpected token in .code directive");
Sean Callanan18b83232010-01-19 21:44:56 +00001239 int64_t Val = Parser.getTok().getIntVal();
Duncan Sands58c86912010-06-29 13:04:35 +00001240 if (Val == 16)
Sean Callananb9a25b72010-01-19 20:27:46 +00001241 Parser.Lex();
Duncan Sands58c86912010-06-29 13:04:35 +00001242 else if (Val == 32)
Sean Callananb9a25b72010-01-19 20:27:46 +00001243 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001244 else
1245 return Error(L, "invalid operand to .code directive");
1246
1247 if (getLexer().isNot(AsmToken::EndOfStatement))
Sean Callanan18b83232010-01-19 21:44:56 +00001248 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
Sean Callananb9a25b72010-01-19 20:27:46 +00001249 Parser.Lex();
Kevin Enderby515d5092009-10-15 20:48:48 +00001250
Jim Grosbach2a301702010-11-05 22:40:53 +00001251 if (Val == 16)
1252 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
1253 else
1254 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
1255
Kevin Enderby515d5092009-10-15 20:48:48 +00001256 return false;
1257}
1258
Sean Callanan90b70972010-04-07 20:29:34 +00001259extern "C" void LLVMInitializeARMAsmLexer();
1260
Kevin Enderby9c41fa82009-10-30 22:55:57 +00001261/// Force static initialization.
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001262extern "C" void LLVMInitializeARMAsmParser() {
1263 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
1264 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
Sean Callanan90b70972010-04-07 20:29:34 +00001265 LLVMInitializeARMAsmLexer();
Kevin Enderbyca9c42c2009-09-15 00:27:25 +00001266}
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001267
Chris Lattner0692ee62010-09-06 19:11:01 +00001268#define GET_REGISTER_MATCHER
1269#define GET_MATCHER_IMPLEMENTATION
Daniel Dunbar3483aca2010-08-11 05:24:50 +00001270#include "ARMGenAsmMatcher.inc"