1 //===-- ARMAsmParser.cpp - Parse ARM assembly to MCInst instructions ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "ARMAddressingModes.h"
12 #include "ARMMCExpr.h"
13 #include "ARMBaseRegisterInfo.h"
14 #include "ARMSubtarget.h"
15 #include "llvm/MC/MCParser/MCAsmLexer.h"
16 #include "llvm/MC/MCParser/MCAsmParser.h"
17 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCStreamer.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/Target/TargetRegistry.h"
23 #include "llvm/Target/TargetAsmParser.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/ADT/Twine.h"
36 class ARMAsmParser : public TargetAsmParser {
40 MCAsmParser &getParser() const { return Parser; }
41 MCAsmLexer &getLexer() const { return Parser.getLexer(); }
43 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); }
44 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); }
46 int TryParseRegister();
47 virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc);
48 bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &);
49 bool TryParseShiftRegister(SmallVectorImpl<MCParsedAsmOperand*> &);
50 bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &);
51 bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &,
52 ARMII::AddrMode AddrMode);
53 bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic);
54 bool ParsePrefix(ARMMCExpr::VariantKind &RefKind);
55 const MCExpr *ApplyPrefixToExpr(const MCExpr *E,
56 MCSymbolRefExpr::VariantKind Variant);
59 bool ParseMemoryOffsetReg(bool &Negative,
60 bool &OffsetRegShifted,
61 enum ARM_AM::ShiftOpc &ShiftType,
62 const MCExpr *&ShiftAmount,
63 const MCExpr *&Offset,
67 bool ParseShift(enum ARM_AM::ShiftOpc &St,
68 const MCExpr *&ShiftAmount, SMLoc &E);
69 bool ParseDirectiveWord(unsigned Size, SMLoc L);
70 bool ParseDirectiveThumb(SMLoc L);
71 bool ParseDirectiveThumbFunc(SMLoc L);
72 bool ParseDirectiveCode(SMLoc L);
73 bool ParseDirectiveSyntax(SMLoc L);
75 bool MatchAndEmitInstruction(SMLoc IDLoc,
76 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
78 void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
79 bool &CanAcceptPredicationCode);
81 /// @name Auto-generated Match Functions
84 #define GET_ASSEMBLER_HEADER
85 #include "ARMGenAsmMatcher.inc"
89 OperandMatchResultTy tryParseCoprocNumOperand(
90 SmallVectorImpl<MCParsedAsmOperand*>&);
91 OperandMatchResultTy tryParseCoprocRegOperand(
92 SmallVectorImpl<MCParsedAsmOperand*>&);
93 OperandMatchResultTy tryParseMemBarrierOptOperand(
94 SmallVectorImpl<MCParsedAsmOperand*>&);
95 OperandMatchResultTy tryParseProcIFlagsOperand(
96 SmallVectorImpl<MCParsedAsmOperand*>&);
97 OperandMatchResultTy tryParseMSRMaskOperand(
98 SmallVectorImpl<MCParsedAsmOperand*>&);
99 OperandMatchResultTy tryParseMemMode2Operand(
100 SmallVectorImpl<MCParsedAsmOperand*>&);
101 OperandMatchResultTy tryParseMemMode3Operand(
102 SmallVectorImpl<MCParsedAsmOperand*>&);
104 // Asm Match Converter Methods
105 bool CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
106 const SmallVectorImpl<MCParsedAsmOperand*> &);
107 bool CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
108 const SmallVectorImpl<MCParsedAsmOperand*> &);
109 bool CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
110 const SmallVectorImpl<MCParsedAsmOperand*> &);
111 bool CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
112 const SmallVectorImpl<MCParsedAsmOperand*> &);
115 ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM)
116 : TargetAsmParser(T), Parser(_Parser), TM(_TM) {
117 // Initialize the set of available features.
118 setAvailableFeatures(ComputeAvailableFeatures(
119 &TM.getSubtarget<ARMSubtarget>()));
122 virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc,
123 SmallVectorImpl<MCParsedAsmOperand*> &Operands);
124 virtual bool ParseDirective(AsmToken DirectiveID);
126 } // end anonymous namespace
130 /// ARMOperand - Instances of this class represent a parsed ARM machine
132 class ARMOperand : public MCParsedAsmOperand {
151 SMLoc StartLoc, EndLoc;
152 SmallVector<unsigned, 8> Registers;
156 ARMCC::CondCodes Val;
168 ARM_PROC::IFlags Val;
188 /// Combined record for all forms of ARM address expressions.
190 ARMII::AddrMode AddrMode;
193 unsigned RegNum; ///< Offset register num, when OffsetIsReg.
194 const MCExpr *Value; ///< Offset value, when !OffsetIsReg.
196 const MCExpr *ShiftAmount; // used when OffsetRegShifted is true
197 enum ARM_AM::ShiftOpc ShiftType; // used when OffsetRegShifted is true
198 unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true
199 unsigned Preindexed : 1;
200 unsigned Postindexed : 1;
201 unsigned OffsetIsReg : 1;
202 unsigned Negative : 1; // only used when OffsetIsReg is true
203 unsigned Writeback : 1;
207 ARM_AM::ShiftOpc ShiftTy;
212 ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {}
214 ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() {
216 StartLoc = o.StartLoc;
230 case DPRRegisterList:
231 case SPRRegisterList:
232 Registers = o.Registers;
259 /// getStartLoc - Get the location of the first token of this operand.
260 SMLoc getStartLoc() const { return StartLoc; }
261 /// getEndLoc - Get the location of the last token of this operand.
262 SMLoc getEndLoc() const { return EndLoc; }
264 ARMCC::CondCodes getCondCode() const {
265 assert(Kind == CondCode && "Invalid access!");
269 unsigned getCoproc() const {
270 assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!");
274 StringRef getToken() const {
275 assert(Kind == Token && "Invalid access!");
276 return StringRef(Tok.Data, Tok.Length);
279 unsigned getReg() const {
280 assert((Kind == Register || Kind == CCOut) && "Invalid access!");
284 const SmallVectorImpl<unsigned> &getRegList() const {
285 assert((Kind == RegisterList || Kind == DPRRegisterList ||
286 Kind == SPRRegisterList) && "Invalid access!");
290 const MCExpr *getImm() const {
291 assert(Kind == Immediate && "Invalid access!");
295 ARM_MB::MemBOpt getMemBarrierOpt() const {
296 assert(Kind == MemBarrierOpt && "Invalid access!");
300 ARM_PROC::IFlags getProcIFlags() const {
301 assert(Kind == ProcIFlags && "Invalid access!");
305 unsigned getMSRMask() const {
306 assert(Kind == MSRMask && "Invalid access!");
310 /// @name Memory Operand Accessors
312 ARMII::AddrMode getMemAddrMode() const {
315 unsigned getMemBaseRegNum() const {
316 return Mem.BaseRegNum;
318 unsigned getMemOffsetRegNum() const {
319 assert(Mem.OffsetIsReg && "Invalid access!");
320 return Mem.Offset.RegNum;
322 const MCExpr *getMemOffset() const {
323 assert(!Mem.OffsetIsReg && "Invalid access!");
324 return Mem.Offset.Value;
326 unsigned getMemOffsetRegShifted() const {
327 assert(Mem.OffsetIsReg && "Invalid access!");
328 return Mem.OffsetRegShifted;
330 const MCExpr *getMemShiftAmount() const {
331 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
332 return Mem.ShiftAmount;
334 enum ARM_AM::ShiftOpc getMemShiftType() const {
335 assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!");
336 return Mem.ShiftType;
338 bool getMemPreindexed() const { return Mem.Preindexed; }
339 bool getMemPostindexed() const { return Mem.Postindexed; }
340 bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; }
341 bool getMemNegative() const { return Mem.Negative; }
342 bool getMemWriteback() const { return Mem.Writeback; }
346 bool isCoprocNum() const { return Kind == CoprocNum; }
347 bool isCoprocReg() const { return Kind == CoprocReg; }
348 bool isCondCode() const { return Kind == CondCode; }
349 bool isCCOut() const { return Kind == CCOut; }
350 bool isImm() const { return Kind == Immediate; }
351 bool isReg() const { return Kind == Register; }
352 bool isRegList() const { return Kind == RegisterList; }
353 bool isDPRRegList() const { return Kind == DPRRegisterList; }
354 bool isSPRRegList() const { return Kind == SPRRegisterList; }
355 bool isToken() const { return Kind == Token; }
356 bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; }
357 bool isMemory() const { return Kind == Memory; }
358 bool isShifter() const { return Kind == Shifter; }
359 bool isMemMode2() const {
360 if (getMemAddrMode() != ARMII::AddrMode2)
363 if (getMemOffsetIsReg())
366 if (getMemNegative() &&
367 !(getMemPostindexed() || getMemPreindexed()))
370 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
371 if (!CE) return false;
372 int64_t Value = CE->getValue();
374 // The offset must be in the range 0-4095 (imm12).
375 if (Value > 4095 || Value < -4095)
380 bool isMemMode3() const {
381 if (getMemAddrMode() != ARMII::AddrMode3)
384 if (getMemOffsetIsReg()) {
385 if (getMemOffsetRegShifted())
386 return false; // No shift with offset reg allowed
390 if (getMemNegative() &&
391 !(getMemPostindexed() || getMemPreindexed()))
394 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
395 if (!CE) return false;
396 int64_t Value = CE->getValue();
398 // The offset must be in the range 0-255 (imm8).
399 if (Value > 255 || Value < -255)
404 bool isMemMode5() const {
405 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() ||
409 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
410 if (!CE) return false;
412 // The offset must be a multiple of 4 in the range 0-1020.
413 int64_t Value = CE->getValue();
414 return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020);
416 bool isMemMode7() const {
418 getMemPreindexed() ||
419 getMemPostindexed() ||
420 getMemOffsetIsReg() ||
425 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
426 if (!CE) return false;
433 bool isMemModeRegThumb() const {
434 if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback())
438 bool isMemModeImmThumb() const {
439 if (!isMemory() || getMemOffsetIsReg() || getMemWriteback())
442 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
443 if (!CE) return false;
445 // The offset must be a multiple of 4 in the range 0-124.
446 uint64_t Value = CE->getValue();
447 return ((Value & 0x3) == 0 && Value <= 124);
449 bool isMSRMask() const { return Kind == MSRMask; }
450 bool isProcIFlags() const { return Kind == ProcIFlags; }
452 void addExpr(MCInst &Inst, const MCExpr *Expr) const {
453 // Add as immediates when possible. Null MCExpr = 0.
455 Inst.addOperand(MCOperand::CreateImm(0));
456 else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr))
457 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
459 Inst.addOperand(MCOperand::CreateExpr(Expr));
462 void addCondCodeOperands(MCInst &Inst, unsigned N) const {
463 assert(N == 2 && "Invalid number of operands!");
464 Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode())));
465 unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR;
466 Inst.addOperand(MCOperand::CreateReg(RegNum));
469 void addCoprocNumOperands(MCInst &Inst, unsigned N) const {
470 assert(N == 1 && "Invalid number of operands!");
471 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
474 void addCoprocRegOperands(MCInst &Inst, unsigned N) const {
475 assert(N == 1 && "Invalid number of operands!");
476 Inst.addOperand(MCOperand::CreateImm(getCoproc()));
479 void addCCOutOperands(MCInst &Inst, unsigned N) const {
480 assert(N == 1 && "Invalid number of operands!");
481 Inst.addOperand(MCOperand::CreateReg(getReg()));
484 void addRegOperands(MCInst &Inst, unsigned N) const {
485 assert(N == 1 && "Invalid number of operands!");
486 Inst.addOperand(MCOperand::CreateReg(getReg()));
489 void addShifterOperands(MCInst &Inst, unsigned N) const {
490 assert(N == 1 && "Invalid number of operands!");
491 Inst.addOperand(MCOperand::CreateImm(
492 ARM_AM::getSORegOpc(Shift.ShiftTy, 0)));
495 void addRegListOperands(MCInst &Inst, unsigned N) const {
496 assert(N == 1 && "Invalid number of operands!");
497 const SmallVectorImpl<unsigned> &RegList = getRegList();
498 for (SmallVectorImpl<unsigned>::const_iterator
499 I = RegList.begin(), E = RegList.end(); I != E; ++I)
500 Inst.addOperand(MCOperand::CreateReg(*I));
503 void addDPRRegListOperands(MCInst &Inst, unsigned N) const {
504 addRegListOperands(Inst, N);
507 void addSPRRegListOperands(MCInst &Inst, unsigned N) const {
508 addRegListOperands(Inst, N);
511 void addImmOperands(MCInst &Inst, unsigned N) const {
512 assert(N == 1 && "Invalid number of operands!");
513 addExpr(Inst, getImm());
516 void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const {
517 assert(N == 1 && "Invalid number of operands!");
518 Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt())));
521 void addMemMode7Operands(MCInst &Inst, unsigned N) const {
522 assert(N == 1 && isMemMode7() && "Invalid number of operands!");
523 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
525 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
527 assert((CE || CE->getValue() == 0) &&
528 "No offset operand support in mode 7");
531 void addMemMode2Operands(MCInst &Inst, unsigned N) const {
532 assert(isMemMode2() && "Invalid mode or number of operands!");
533 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
534 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
536 if (getMemOffsetIsReg()) {
537 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
539 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
540 ARM_AM::ShiftOpc ShOpc = ARM_AM::no_shift;
541 int64_t ShiftAmount = 0;
543 if (getMemOffsetRegShifted()) {
544 ShOpc = getMemShiftType();
545 const MCConstantExpr *CE =
546 dyn_cast<MCConstantExpr>(getMemShiftAmount());
547 ShiftAmount = CE->getValue();
550 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(AMOpc, ShiftAmount,
555 // Create a operand placeholder to always yield the same number of operands.
556 Inst.addOperand(MCOperand::CreateReg(0));
558 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
560 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
561 assert(CE && "Non-constant mode 2 offset operand!");
562 int64_t Offset = CE->getValue();
565 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::add,
566 Offset, ARM_AM::no_shift, IdxMode)));
568 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM2Opc(ARM_AM::sub,
569 -Offset, ARM_AM::no_shift, IdxMode)));
572 void addMemMode3Operands(MCInst &Inst, unsigned N) const {
573 assert(isMemMode3() && "Invalid mode or number of operands!");
574 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
575 unsigned IdxMode = (getMemPreindexed() | getMemPostindexed() << 1);
577 if (getMemOffsetIsReg()) {
578 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
580 ARM_AM::AddrOpc AMOpc = getMemNegative() ? ARM_AM::sub : ARM_AM::add;
581 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(AMOpc, 0,
586 // Create a operand placeholder to always yield the same number of operands.
587 Inst.addOperand(MCOperand::CreateReg(0));
589 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
591 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
592 assert(CE && "Non-constant mode 3 offset operand!");
593 int64_t Offset = CE->getValue();
596 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::add,
599 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM3Opc(ARM_AM::sub,
603 void addMemMode5Operands(MCInst &Inst, unsigned N) const {
604 assert(N == 2 && isMemMode5() && "Invalid number of operands!");
606 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
607 assert(!getMemOffsetIsReg() && "Invalid mode 5 operand");
609 // FIXME: #-0 is encoded differently than #0. Does the parser preserve
611 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
612 assert(CE && "Non-constant mode 5 offset operand!");
614 // The MCInst offset operand doesn't include the low two bits (like
615 // the instruction encoding).
616 int64_t Offset = CE->getValue() / 4;
618 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add,
621 Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub,
625 void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const {
626 assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!");
627 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
628 Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum()));
631 void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const {
632 assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!");
633 Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum()));
634 const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset());
635 assert(CE && "Non-constant mode offset operand!");
636 Inst.addOperand(MCOperand::CreateImm(CE->getValue()));
639 void addMSRMaskOperands(MCInst &Inst, unsigned N) const {
640 assert(N == 1 && "Invalid number of operands!");
641 Inst.addOperand(MCOperand::CreateImm(unsigned(getMSRMask())));
644 void addProcIFlagsOperands(MCInst &Inst, unsigned N) const {
645 assert(N == 1 && "Invalid number of operands!");
646 Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags())));
649 virtual void dump(raw_ostream &OS) const;
651 static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) {
652 ARMOperand *Op = new ARMOperand(CondCode);
659 static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) {
660 ARMOperand *Op = new ARMOperand(CoprocNum);
661 Op->Cop.Val = CopVal;
667 static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) {
668 ARMOperand *Op = new ARMOperand(CoprocReg);
669 Op->Cop.Val = CopVal;
675 static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) {
676 ARMOperand *Op = new ARMOperand(CCOut);
677 Op->Reg.RegNum = RegNum;
683 static ARMOperand *CreateToken(StringRef Str, SMLoc S) {
684 ARMOperand *Op = new ARMOperand(Token);
685 Op->Tok.Data = Str.data();
686 Op->Tok.Length = Str.size();
692 static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) {
693 ARMOperand *Op = new ARMOperand(Register);
694 Op->Reg.RegNum = RegNum;
700 static ARMOperand *CreateShifter(ARM_AM::ShiftOpc ShTy,
702 ARMOperand *Op = new ARMOperand(Shifter);
703 Op->Shift.ShiftTy = ShTy;
710 CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs,
711 SMLoc StartLoc, SMLoc EndLoc) {
712 KindTy Kind = RegisterList;
714 if (ARM::DPRRegClass.contains(Regs.front().first))
715 Kind = DPRRegisterList;
716 else if (ARM::SPRRegClass.contains(Regs.front().first))
717 Kind = SPRRegisterList;
719 ARMOperand *Op = new ARMOperand(Kind);
720 for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
721 I = Regs.begin(), E = Regs.end(); I != E; ++I)
722 Op->Registers.push_back(I->first);
723 array_pod_sort(Op->Registers.begin(), Op->Registers.end());
724 Op->StartLoc = StartLoc;
729 static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) {
730 ARMOperand *Op = new ARMOperand(Immediate);
737 static ARMOperand *CreateMem(ARMII::AddrMode AddrMode, unsigned BaseRegNum,
738 bool OffsetIsReg, const MCExpr *Offset,
739 int OffsetRegNum, bool OffsetRegShifted,
740 enum ARM_AM::ShiftOpc ShiftType,
741 const MCExpr *ShiftAmount, bool Preindexed,
742 bool Postindexed, bool Negative, bool Writeback,
744 assert((OffsetRegNum == -1 || OffsetIsReg) &&
745 "OffsetRegNum must imply OffsetIsReg!");
746 assert((!OffsetRegShifted || OffsetIsReg) &&
747 "OffsetRegShifted must imply OffsetIsReg!");
748 assert((Offset || OffsetIsReg) &&
749 "Offset must exists unless register offset is used!");
750 assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) &&
751 "Cannot have shift amount without shifted register offset!");
752 assert((!Offset || !OffsetIsReg) &&
753 "Cannot have expression offset and register offset!");
755 ARMOperand *Op = new ARMOperand(Memory);
756 Op->Mem.AddrMode = AddrMode;
757 Op->Mem.BaseRegNum = BaseRegNum;
758 Op->Mem.OffsetIsReg = OffsetIsReg;
760 Op->Mem.Offset.RegNum = OffsetRegNum;
762 Op->Mem.Offset.Value = Offset;
763 Op->Mem.OffsetRegShifted = OffsetRegShifted;
764 Op->Mem.ShiftType = ShiftType;
765 Op->Mem.ShiftAmount = ShiftAmount;
766 Op->Mem.Preindexed = Preindexed;
767 Op->Mem.Postindexed = Postindexed;
768 Op->Mem.Negative = Negative;
769 Op->Mem.Writeback = Writeback;
776 static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) {
777 ARMOperand *Op = new ARMOperand(MemBarrierOpt);
784 static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) {
785 ARMOperand *Op = new ARMOperand(ProcIFlags);
786 Op->IFlags.Val = IFlags;
792 static ARMOperand *CreateMSRMask(unsigned MMask, SMLoc S) {
793 ARMOperand *Op = new ARMOperand(MSRMask);
794 Op->MMask.Val = MMask;
801 } // end anonymous namespace.
803 void ARMOperand::dump(raw_ostream &OS) const {
806 OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">";
809 OS << "<ccout " << getReg() << ">";
812 OS << "<coprocessor number: " << getCoproc() << ">";
815 OS << "<coprocessor register: " << getCoproc() << ">";
818 OS << "<mask: " << getMSRMask() << ">";
824 OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">";
828 << "am:" << ARMII::AddrModeToString(getMemAddrMode())
829 << " base:" << getMemBaseRegNum();
830 if (getMemOffsetIsReg()) {
831 OS << " offset:<register " << getMemOffsetRegNum();
832 if (getMemOffsetRegShifted()) {
833 OS << " offset-shift-type:" << getMemShiftType();
834 OS << " offset-shift-amount:" << *getMemShiftAmount();
837 OS << " offset:" << *getMemOffset();
839 if (getMemOffsetIsReg())
840 OS << " (offset-is-reg)";
841 if (getMemPreindexed())
842 OS << " (pre-indexed)";
843 if (getMemPostindexed())
844 OS << " (post-indexed)";
845 if (getMemNegative())
847 if (getMemWriteback())
848 OS << " (writeback)";
853 unsigned IFlags = getProcIFlags();
854 for (int i=2; i >= 0; --i)
855 if (IFlags & (1 << i))
856 OS << ARM_PROC::IFlagsToString(1 << i);
861 OS << "<register " << getReg() << ">";
864 OS << "<shifter " << getShiftOpcStr(Shift.ShiftTy) << ">";
867 case DPRRegisterList:
868 case SPRRegisterList: {
869 OS << "<register_list ";
871 const SmallVectorImpl<unsigned> &RegList = getRegList();
872 for (SmallVectorImpl<unsigned>::const_iterator
873 I = RegList.begin(), E = RegList.end(); I != E; ) {
875 if (++I < E) OS << ", ";
882 OS << "'" << getToken() << "'";
887 /// @name Auto-generated Match Functions
890 static unsigned MatchRegisterName(StringRef Name);
894 bool ARMAsmParser::ParseRegister(unsigned &RegNo,
895 SMLoc &StartLoc, SMLoc &EndLoc) {
896 RegNo = TryParseRegister();
898 return (RegNo == (unsigned)-1);
901 /// Try to parse a register name. The token must be an Identifier when called,
902 /// and if it is a register name the token is eaten and the register number is
903 /// returned. Otherwise return -1.
905 int ARMAsmParser::TryParseRegister() {
906 const AsmToken &Tok = Parser.getTok();
907 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
909 // FIXME: Validate register for the current architecture; we have to do
910 // validation later, so maybe there is no need for this here.
911 std::string upperCase = Tok.getString().str();
912 std::string lowerCase = LowercaseString(upperCase);
913 unsigned RegNum = MatchRegisterName(lowerCase);
915 RegNum = StringSwitch<unsigned>(lowerCase)
916 .Case("r13", ARM::SP)
917 .Case("r14", ARM::LR)
918 .Case("r15", ARM::PC)
919 .Case("ip", ARM::R12)
922 if (!RegNum) return -1;
924 Parser.Lex(); // Eat identifier token.
928 /// Try to parse a register name. The token must be an Identifier when called,
929 /// and if it is a register name the token is eaten and the register number is
930 /// returned. Otherwise return -1.
932 bool ARMAsmParser::TryParseShiftRegister(
933 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
934 SMLoc S = Parser.getTok().getLoc();
935 const AsmToken &Tok = Parser.getTok();
936 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
938 std::string upperCase = Tok.getString().str();
939 std::string lowerCase = LowercaseString(upperCase);
940 ARM_AM::ShiftOpc ShiftTy = StringSwitch<ARM_AM::ShiftOpc>(lowerCase)
941 .Case("lsl", ARM_AM::lsl)
942 .Case("lsr", ARM_AM::lsr)
943 .Case("asr", ARM_AM::asr)
944 .Case("ror", ARM_AM::ror)
945 .Case("rrx", ARM_AM::rrx)
946 .Default(ARM_AM::no_shift);
948 if (ShiftTy == ARM_AM::no_shift)
951 Parser.Lex(); // Eat shift-type operand;
952 int RegNum = TryParseRegister();
954 return Error(Parser.getTok().getLoc(), "register expected");
956 Operands.push_back(ARMOperand::CreateReg(RegNum,S, Parser.getTok().getLoc()));
957 Operands.push_back(ARMOperand::CreateShifter(ShiftTy,
958 S, Parser.getTok().getLoc()));
964 /// Try to parse a register name. The token must be an Identifier when called.
965 /// If it's a register, an AsmOperand is created. Another AsmOperand is created
966 /// if there is a "writeback". 'true' if it's not a register.
968 /// TODO this is likely to change to allow different register types and or to
969 /// parse for a specific register type.
971 TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
972 SMLoc S = Parser.getTok().getLoc();
973 int RegNo = TryParseRegister();
977 Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc()));
979 const AsmToken &ExclaimTok = Parser.getTok();
980 if (ExclaimTok.is(AsmToken::Exclaim)) {
981 Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(),
982 ExclaimTok.getLoc()));
983 Parser.Lex(); // Eat exclaim token
989 /// MatchCoprocessorOperandName - Try to parse an coprocessor related
990 /// instruction with a symbolic operand name. Example: "p1", "p7", "c3",
992 static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) {
993 // Use the same layout as the tablegen'erated register name matcher. Ugly,
995 switch (Name.size()) {
998 if (Name[0] != CoprocOp)
1015 if (Name[0] != CoprocOp || Name[1] != '1')
1019 case '0': return 10;
1020 case '1': return 11;
1021 case '2': return 12;
1022 case '3': return 13;
1023 case '4': return 14;
1024 case '5': return 15;
1032 /// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The
1033 /// token must be an Identifier when called, and if it is a coprocessor
1034 /// number, the token is eaten and the operand is added to the operand list.
1035 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1036 tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1037 SMLoc S = Parser.getTok().getLoc();
1038 const AsmToken &Tok = Parser.getTok();
1039 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1041 int Num = MatchCoprocessorOperandName(Tok.getString(), 'p');
1043 return MatchOperand_NoMatch;
1045 Parser.Lex(); // Eat identifier token.
1046 Operands.push_back(ARMOperand::CreateCoprocNum(Num, S));
1047 return MatchOperand_Success;
1050 /// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The
1051 /// token must be an Identifier when called, and if it is a coprocessor
1052 /// number, the token is eaten and the operand is added to the operand list.
1053 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1054 tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1055 SMLoc S = Parser.getTok().getLoc();
1056 const AsmToken &Tok = Parser.getTok();
1057 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1059 int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c');
1061 return MatchOperand_NoMatch;
1063 Parser.Lex(); // Eat identifier token.
1064 Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S));
1065 return MatchOperand_Success;
1068 /// Parse a register list, return it if successful else return null. The first
1069 /// token must be a '{' when called.
1071 ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1072 assert(Parser.getTok().is(AsmToken::LCurly) &&
1073 "Token is not a Left Curly Brace");
1074 SMLoc S = Parser.getTok().getLoc();
1076 // Read the rest of the registers in the list.
1077 unsigned PrevRegNum = 0;
1078 SmallVector<std::pair<unsigned, SMLoc>, 32> Registers;
1081 bool IsRange = Parser.getTok().is(AsmToken::Minus);
1082 Parser.Lex(); // Eat non-identifier token.
1084 const AsmToken &RegTok = Parser.getTok();
1085 SMLoc RegLoc = RegTok.getLoc();
1086 if (RegTok.isNot(AsmToken::Identifier)) {
1087 Error(RegLoc, "register expected");
1091 int RegNum = TryParseRegister();
1093 Error(RegLoc, "register expected");
1098 int Reg = PrevRegNum;
1101 Registers.push_back(std::make_pair(Reg, RegLoc));
1102 } while (Reg != RegNum);
1104 Registers.push_back(std::make_pair(RegNum, RegLoc));
1107 PrevRegNum = RegNum;
1108 } while (Parser.getTok().is(AsmToken::Comma) ||
1109 Parser.getTok().is(AsmToken::Minus));
1111 // Process the right curly brace of the list.
1112 const AsmToken &RCurlyTok = Parser.getTok();
1113 if (RCurlyTok.isNot(AsmToken::RCurly)) {
1114 Error(RCurlyTok.getLoc(), "'}' expected");
1118 SMLoc E = RCurlyTok.getLoc();
1119 Parser.Lex(); // Eat right curly brace token.
1121 // Verify the register list.
1122 SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator
1123 RI = Registers.begin(), RE = Registers.end();
1125 unsigned HighRegNum = getARMRegisterNumbering(RI->first);
1126 bool EmittedWarning = false;
1128 DenseMap<unsigned, bool> RegMap;
1129 RegMap[HighRegNum] = true;
1131 for (++RI; RI != RE; ++RI) {
1132 const std::pair<unsigned, SMLoc> &RegInfo = *RI;
1133 unsigned Reg = getARMRegisterNumbering(RegInfo.first);
1136 Error(RegInfo.second, "register duplicated in register list");
1140 if (!EmittedWarning && Reg < HighRegNum)
1141 Warning(RegInfo.second,
1142 "register not in ascending order in register list");
1145 HighRegNum = std::max(Reg, HighRegNum);
1148 Operands.push_back(ARMOperand::CreateRegList(Registers, S, E));
1152 /// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options.
1153 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1154 tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1155 SMLoc S = Parser.getTok().getLoc();
1156 const AsmToken &Tok = Parser.getTok();
1157 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1158 StringRef OptStr = Tok.getString();
1160 unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size()))
1161 .Case("sy", ARM_MB::SY)
1162 .Case("st", ARM_MB::ST)
1163 .Case("ish", ARM_MB::ISH)
1164 .Case("ishst", ARM_MB::ISHST)
1165 .Case("nsh", ARM_MB::NSH)
1166 .Case("nshst", ARM_MB::NSHST)
1167 .Case("osh", ARM_MB::OSH)
1168 .Case("oshst", ARM_MB::OSHST)
1172 return MatchOperand_NoMatch;
1174 Parser.Lex(); // Eat identifier token.
1175 Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S));
1176 return MatchOperand_Success;
1179 /// tryParseProcIFlagsOperand - Try to parse iflags from CPS instruction.
1180 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1181 tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1182 SMLoc S = Parser.getTok().getLoc();
1183 const AsmToken &Tok = Parser.getTok();
1184 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1185 StringRef IFlagsStr = Tok.getString();
1187 unsigned IFlags = 0;
1188 for (int i = 0, e = IFlagsStr.size(); i != e; ++i) {
1189 unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1))
1190 .Case("a", ARM_PROC::A)
1191 .Case("i", ARM_PROC::I)
1192 .Case("f", ARM_PROC::F)
1195 // If some specific iflag is already set, it means that some letter is
1196 // present more than once, this is not acceptable.
1197 if (Flag == ~0U || (IFlags & Flag))
1198 return MatchOperand_NoMatch;
1203 Parser.Lex(); // Eat identifier token.
1204 Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S));
1205 return MatchOperand_Success;
1208 /// tryParseMSRMaskOperand - Try to parse mask flags from MSR instruction.
1209 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1210 tryParseMSRMaskOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1211 SMLoc S = Parser.getTok().getLoc();
1212 const AsmToken &Tok = Parser.getTok();
1213 assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier");
1214 StringRef Mask = Tok.getString();
1216 // Split spec_reg from flag, example: CPSR_sxf => "CPSR" and "sxf"
1217 size_t Start = 0, Next = Mask.find('_');
1218 StringRef Flags = "";
1219 StringRef SpecReg = Mask.slice(Start, Next);
1220 if (Next != StringRef::npos)
1221 Flags = Mask.slice(Next+1, Mask.size());
1223 // FlagsVal contains the complete mask:
1225 // 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1226 unsigned FlagsVal = 0;
1228 if (SpecReg == "apsr") {
1229 FlagsVal = StringSwitch<unsigned>(Flags)
1230 .Case("nzcvq", 0x8) // same as CPSR_c
1231 .Case("g", 0x4) // same as CPSR_s
1232 .Case("nzcvqg", 0xc) // same as CPSR_fs
1235 if (FlagsVal == ~0U) {
1237 return MatchOperand_NoMatch;
1239 FlagsVal = 0; // No flag
1241 } else if (SpecReg == "cpsr" || SpecReg == "spsr") {
1242 for (int i = 0, e = Flags.size(); i != e; ++i) {
1243 unsigned Flag = StringSwitch<unsigned>(Flags.substr(i, 1))
1250 // If some specific flag is already set, it means that some letter is
1251 // present more than once, this is not acceptable.
1252 if (FlagsVal == ~0U || (FlagsVal & Flag))
1253 return MatchOperand_NoMatch;
1256 } else // No match for special register.
1257 return MatchOperand_NoMatch;
1259 // Special register without flags are equivalent to "fc" flags.
1263 // Bit 4: Special Reg (cpsr, apsr => 0; spsr => 1)
1264 if (SpecReg == "spsr")
1267 Parser.Lex(); // Eat identifier token.
1268 Operands.push_back(ARMOperand::CreateMSRMask(FlagsVal, S));
1269 return MatchOperand_Success;
1272 /// tryParseMemMode2Operand - Try to parse memory addressing mode 2 operand.
1273 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1274 tryParseMemMode2Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1275 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1277 if (ParseMemory(Operands, ARMII::AddrMode2))
1278 return MatchOperand_NoMatch;
1280 return MatchOperand_Success;
1283 /// tryParseMemMode3Operand - Try to parse memory addressing mode 3 operand.
1284 ARMAsmParser::OperandMatchResultTy ARMAsmParser::
1285 tryParseMemMode3Operand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1286 assert(Parser.getTok().is(AsmToken::LBrac) && "Token is not a \"[\"");
1288 if (ParseMemory(Operands, ARMII::AddrMode3))
1289 return MatchOperand_NoMatch;
1291 return MatchOperand_Success;
1294 /// CvtLdWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1295 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1296 /// when they refer multiple MIOperands inside a single one.
1298 CvtLdWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1299 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1300 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1302 // Create a writeback register dummy placeholder.
1303 Inst.addOperand(MCOperand::CreateImm(0));
1305 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1306 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1310 /// CvtStWriteBackRegAddrMode2 - Convert parsed operands to MCInst.
1311 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1312 /// when they refer multiple MIOperands inside a single one.
1314 CvtStWriteBackRegAddrMode2(MCInst &Inst, unsigned Opcode,
1315 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1316 // Create a writeback register dummy placeholder.
1317 Inst.addOperand(MCOperand::CreateImm(0));
1318 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1319 ((ARMOperand*)Operands[3])->addMemMode2Operands(Inst, 3);
1320 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1324 /// CvtLdWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1325 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1326 /// when they refer multiple MIOperands inside a single one.
1328 CvtLdWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1329 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1330 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1332 // Create a writeback register dummy placeholder.
1333 Inst.addOperand(MCOperand::CreateImm(0));
1335 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1336 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1340 /// CvtStWriteBackRegAddrMode3 - Convert parsed operands to MCInst.
1341 /// Needed here because the Asm Gen Matcher can't handle properly tied operands
1342 /// when they refer multiple MIOperands inside a single one.
1344 CvtStWriteBackRegAddrMode3(MCInst &Inst, unsigned Opcode,
1345 const SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1346 // Create a writeback register dummy placeholder.
1347 Inst.addOperand(MCOperand::CreateImm(0));
1348 ((ARMOperand*)Operands[2])->addRegOperands(Inst, 1);
1349 ((ARMOperand*)Operands[3])->addMemMode3Operands(Inst, 3);
1350 ((ARMOperand*)Operands[1])->addCondCodeOperands(Inst, 2);
1354 /// Parse an ARM memory expression, return false if successful else return true
1355 /// or an error. The first token must be a '[' when called.
1357 /// TODO Only preindexing and postindexing addressing are started, unindexed
1358 /// with option, etc are still to do.
1360 ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1361 ARMII::AddrMode AddrMode = ARMII::AddrModeNone) {
1363 assert(Parser.getTok().is(AsmToken::LBrac) &&
1364 "Token is not a Left Bracket");
1365 S = Parser.getTok().getLoc();
1366 Parser.Lex(); // Eat left bracket token.
1368 const AsmToken &BaseRegTok = Parser.getTok();
1369 if (BaseRegTok.isNot(AsmToken::Identifier)) {
1370 Error(BaseRegTok.getLoc(), "register expected");
1373 int BaseRegNum = TryParseRegister();
1374 if (BaseRegNum == -1) {
1375 Error(BaseRegTok.getLoc(), "register expected");
1379 // The next token must either be a comma or a closing bracket.
1380 const AsmToken &Tok = Parser.getTok();
1381 if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac))
1384 bool Preindexed = false;
1385 bool Postindexed = false;
1386 bool OffsetIsReg = false;
1387 bool Negative = false;
1388 bool Writeback = false;
1389 ARMOperand *WBOp = 0;
1390 int OffsetRegNum = -1;
1391 bool OffsetRegShifted = false;
1392 enum ARM_AM::ShiftOpc ShiftType = ARM_AM::lsl;
1393 const MCExpr *ShiftAmount = 0;
1394 const MCExpr *Offset = 0;
1396 // First look for preindexed address forms, that is after the "[Rn" we now
1397 // have to see if the next token is a comma.
1398 if (Tok.is(AsmToken::Comma)) {
1400 Parser.Lex(); // Eat comma token.
1402 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount,
1403 Offset, OffsetIsReg, OffsetRegNum, E))
1405 const AsmToken &RBracTok = Parser.getTok();
1406 if (RBracTok.isNot(AsmToken::RBrac)) {
1407 Error(RBracTok.getLoc(), "']' expected");
1410 E = RBracTok.getLoc();
1411 Parser.Lex(); // Eat right bracket token.
1413 const AsmToken &ExclaimTok = Parser.getTok();
1414 if (ExclaimTok.is(AsmToken::Exclaim)) {
1415 // None of addrmode3 instruction uses "!"
1416 if (AddrMode == ARMII::AddrMode3)
1419 WBOp = ARMOperand::CreateToken(ExclaimTok.getString(),
1420 ExclaimTok.getLoc());
1422 Parser.Lex(); // Eat exclaim token
1423 } else { // In addressing mode 2, pre-indexed mode always end with "!"
1424 if (AddrMode == ARMII::AddrMode2)
1428 // The "[Rn" we have so far was not followed by a comma.
1430 // If there's anything other than the right brace, this is a post indexing
1433 Parser.Lex(); // Eat right bracket token.
1435 const AsmToken &NextTok = Parser.getTok();
1437 if (NextTok.isNot(AsmToken::EndOfStatement)) {
1441 if (NextTok.isNot(AsmToken::Comma)) {
1442 Error(NextTok.getLoc(), "',' expected");
1446 Parser.Lex(); // Eat comma token.
1448 if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType,
1449 ShiftAmount, Offset, OffsetIsReg, OffsetRegNum,
1455 // Force Offset to exist if used.
1458 Offset = MCConstantExpr::Create(0, getContext());
1460 if (AddrMode == ARMII::AddrMode3 && OffsetRegShifted) {
1461 Error(E, "shift amount not supported");
1466 Operands.push_back(ARMOperand::CreateMem(AddrMode, BaseRegNum, OffsetIsReg,
1467 Offset, OffsetRegNum, OffsetRegShifted,
1468 ShiftType, ShiftAmount, Preindexed,
1469 Postindexed, Negative, Writeback, S, E));
1471 Operands.push_back(WBOp);
1476 /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn],"
1477 /// we will parse the following (were +/- means that a plus or minus is
1482 /// we return false on success or an error otherwise.
1483 bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative,
1484 bool &OffsetRegShifted,
1485 enum ARM_AM::ShiftOpc &ShiftType,
1486 const MCExpr *&ShiftAmount,
1487 const MCExpr *&Offset,
1492 OffsetRegShifted = false;
1493 OffsetIsReg = false;
1495 const AsmToken &NextTok = Parser.getTok();
1496 E = NextTok.getLoc();
1497 if (NextTok.is(AsmToken::Plus))
1498 Parser.Lex(); // Eat plus token.
1499 else if (NextTok.is(AsmToken::Minus)) {
1501 Parser.Lex(); // Eat minus token
1503 // See if there is a register following the "[Rn," or "[Rn]," we have so far.
1504 const AsmToken &OffsetRegTok = Parser.getTok();
1505 if (OffsetRegTok.is(AsmToken::Identifier)) {
1506 SMLoc CurLoc = OffsetRegTok.getLoc();
1507 OffsetRegNum = TryParseRegister();
1508 if (OffsetRegNum != -1) {
1514 // If we parsed a register as the offset then there can be a shift after that.
1515 if (OffsetRegNum != -1) {
1516 // Look for a comma then a shift
1517 const AsmToken &Tok = Parser.getTok();
1518 if (Tok.is(AsmToken::Comma)) {
1519 Parser.Lex(); // Eat comma token.
1521 const AsmToken &Tok = Parser.getTok();
1522 if (ParseShift(ShiftType, ShiftAmount, E))
1523 return Error(Tok.getLoc(), "shift expected");
1524 OffsetRegShifted = true;
1527 else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm"
1528 // Look for #offset following the "[Rn," or "[Rn],"
1529 const AsmToken &HashTok = Parser.getTok();
1530 if (HashTok.isNot(AsmToken::Hash))
1531 return Error(HashTok.getLoc(), "'#' expected");
1533 Parser.Lex(); // Eat hash token.
1535 if (getParser().ParseExpression(Offset))
1537 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1542 /// ParseShift as one of these two:
1543 /// ( lsl | lsr | asr | ror ) , # shift_amount
1545 /// and returns true if it parses a shift otherwise it returns false.
1546 bool ARMAsmParser::ParseShift(ARM_AM::ShiftOpc &St,
1547 const MCExpr *&ShiftAmount, SMLoc &E) {
1548 const AsmToken &Tok = Parser.getTok();
1549 if (Tok.isNot(AsmToken::Identifier))
1551 StringRef ShiftName = Tok.getString();
1552 if (ShiftName == "lsl" || ShiftName == "LSL")
1554 else if (ShiftName == "lsr" || ShiftName == "LSR")
1556 else if (ShiftName == "asr" || ShiftName == "ASR")
1558 else if (ShiftName == "ror" || ShiftName == "ROR")
1560 else if (ShiftName == "rrx" || ShiftName == "RRX")
1564 Parser.Lex(); // Eat shift type token.
1566 // Rrx stands alone.
1567 if (St == ARM_AM::rrx)
1570 // Otherwise, there must be a '#' and a shift amount.
1571 const AsmToken &HashTok = Parser.getTok();
1572 if (HashTok.isNot(AsmToken::Hash))
1573 return Error(HashTok.getLoc(), "'#' expected");
1574 Parser.Lex(); // Eat hash token.
1576 if (getParser().ParseExpression(ShiftAmount))
1582 /// Parse a arm instruction operand. For now this parses the operand regardless
1583 /// of the mnemonic.
1584 bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1585 StringRef Mnemonic) {
1588 // Check if the current operand has a custom associated parser, if so, try to
1589 // custom parse the operand, or fallback to the general approach.
1590 OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic);
1591 if (ResTy == MatchOperand_Success)
1593 // If there wasn't a custom match, try the generic matcher below. Otherwise,
1594 // there was a match, but an error occurred, in which case, just return that
1595 // the operand parsing failed.
1596 if (ResTy == MatchOperand_ParseFail)
1599 switch (getLexer().getKind()) {
1601 Error(Parser.getTok().getLoc(), "unexpected token in operand");
1603 case AsmToken::Identifier:
1604 if (!TryParseRegisterWithWriteBack(Operands))
1606 if (!TryParseShiftRegister(Operands))
1610 // Fall though for the Identifier case that is not a register or a
1612 case AsmToken::Integer: // things like 1f and 2b as a branch targets
1613 case AsmToken::Dot: { // . as a branch target
1614 // This was not a register so parse other operands that start with an
1615 // identifier (like labels) as expressions and create them as immediates.
1616 const MCExpr *IdVal;
1617 S = Parser.getTok().getLoc();
1618 if (getParser().ParseExpression(IdVal))
1620 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1621 Operands.push_back(ARMOperand::CreateImm(IdVal, S, E));
1624 case AsmToken::LBrac:
1625 return ParseMemory(Operands);
1626 case AsmToken::LCurly:
1627 return ParseRegisterList(Operands);
1628 case AsmToken::Hash:
1629 // #42 -> immediate.
1630 // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate
1631 S = Parser.getTok().getLoc();
1633 const MCExpr *ImmVal;
1634 if (getParser().ParseExpression(ImmVal))
1636 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1637 Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E));
1639 case AsmToken::Colon: {
1640 // ":lower16:" and ":upper16:" expression prefixes
1641 // FIXME: Check it's an expression prefix,
1642 // e.g. (FOO - :lower16:BAR) isn't legal.
1643 ARMMCExpr::VariantKind RefKind;
1644 if (ParsePrefix(RefKind))
1647 const MCExpr *SubExprVal;
1648 if (getParser().ParseExpression(SubExprVal))
1651 const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal,
1653 E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1);
1654 Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E));
1660 // ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e.
1661 // :lower16: and :upper16:.
1662 bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) {
1663 RefKind = ARMMCExpr::VK_ARM_None;
1665 // :lower16: and :upper16: modifiers
1666 assert(getLexer().is(AsmToken::Colon) && "expected a :");
1667 Parser.Lex(); // Eat ':'
1669 if (getLexer().isNot(AsmToken::Identifier)) {
1670 Error(Parser.getTok().getLoc(), "expected prefix identifier in operand");
1674 StringRef IDVal = Parser.getTok().getIdentifier();
1675 if (IDVal == "lower16") {
1676 RefKind = ARMMCExpr::VK_ARM_LO16;
1677 } else if (IDVal == "upper16") {
1678 RefKind = ARMMCExpr::VK_ARM_HI16;
1680 Error(Parser.getTok().getLoc(), "unexpected prefix in operand");
1685 if (getLexer().isNot(AsmToken::Colon)) {
1686 Error(Parser.getTok().getLoc(), "unexpected token after prefix");
1689 Parser.Lex(); // Eat the last ':'
1694 ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E,
1695 MCSymbolRefExpr::VariantKind Variant) {
1696 // Recurse over the given expression, rebuilding it to apply the given variant
1697 // to the leftmost symbol.
1698 if (Variant == MCSymbolRefExpr::VK_None)
1701 switch (E->getKind()) {
1702 case MCExpr::Target:
1703 llvm_unreachable("Can't handle target expr yet");
1704 case MCExpr::Constant:
1705 llvm_unreachable("Can't handle lower16/upper16 of constant yet");
1707 case MCExpr::SymbolRef: {
1708 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E);
1710 if (SRE->getKind() != MCSymbolRefExpr::VK_None)
1713 return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext());
1717 llvm_unreachable("Can't handle unary expressions yet");
1719 case MCExpr::Binary: {
1720 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E);
1721 const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant);
1722 const MCExpr *RHS = BE->getRHS();
1726 return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext());
1730 assert(0 && "Invalid expression kind!");
1734 /// \brief Given a mnemonic, split out possible predication code and carry
1735 /// setting letters to form a canonical mnemonic and flags.
1737 // FIXME: Would be nice to autogen this.
1738 static StringRef SplitMnemonic(StringRef Mnemonic,
1739 unsigned &PredicationCode,
1741 unsigned &ProcessorIMod) {
1742 PredicationCode = ARMCC::AL;
1743 CarrySetting = false;
1746 // Ignore some mnemonics we know aren't predicated forms.
1748 // FIXME: Would be nice to autogen this.
1749 if (Mnemonic == "teq" || Mnemonic == "vceq" ||
1750 Mnemonic == "movs" ||
1751 Mnemonic == "svc" ||
1752 (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" ||
1753 Mnemonic == "vmls" || Mnemonic == "vnmls") ||
1754 Mnemonic == "vacge" || Mnemonic == "vcge" ||
1755 Mnemonic == "vclt" ||
1756 Mnemonic == "vacgt" || Mnemonic == "vcgt" ||
1757 Mnemonic == "vcle" ||
1758 (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" ||
1759 Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" ||
1760 Mnemonic == "vqdmlal"))
1763 // First, split out any predication code.
1764 unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2))
1765 .Case("eq", ARMCC::EQ)
1766 .Case("ne", ARMCC::NE)
1767 .Case("hs", ARMCC::HS)
1768 .Case("lo", ARMCC::LO)
1769 .Case("mi", ARMCC::MI)
1770 .Case("pl", ARMCC::PL)
1771 .Case("vs", ARMCC::VS)
1772 .Case("vc", ARMCC::VC)
1773 .Case("hi", ARMCC::HI)
1774 .Case("ls", ARMCC::LS)
1775 .Case("ge", ARMCC::GE)
1776 .Case("lt", ARMCC::LT)
1777 .Case("gt", ARMCC::GT)
1778 .Case("le", ARMCC::LE)
1779 .Case("al", ARMCC::AL)
1782 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2);
1783 PredicationCode = CC;
1786 // Next, determine if we have a carry setting bit. We explicitly ignore all
1787 // the instructions we know end in 's'.
1788 if (Mnemonic.endswith("s") &&
1789 !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" ||
1790 Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" ||
1791 Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" ||
1792 Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" ||
1793 Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) {
1794 Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1);
1795 CarrySetting = true;
1798 // The "cps" instruction can have a interrupt mode operand which is glued into
1799 // the mnemonic. Check if this is the case, split it and parse the imod op
1800 if (Mnemonic.startswith("cps")) {
1801 // Split out any imod code.
1803 StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2))
1804 .Case("ie", ARM_PROC::IE)
1805 .Case("id", ARM_PROC::ID)
1808 Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2);
1809 ProcessorIMod = IMod;
1816 /// \brief Given a canonical mnemonic, determine if the instruction ever allows
1817 /// inclusion of carry set or predication code operands.
1819 // FIXME: It would be nice to autogen this.
1821 GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet,
1822 bool &CanAcceptPredicationCode) {
1823 bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb();
1825 if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" ||
1826 Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" ||
1827 Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" ||
1828 Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" ||
1829 Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" ||
1830 Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" ||
1831 Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" ||
1832 Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") {
1833 CanAcceptCarrySet = true;
1835 CanAcceptCarrySet = false;
1838 if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" ||
1839 Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" ||
1840 Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" ||
1841 Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" ||
1842 Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" ||
1843 Mnemonic == "clrex" || Mnemonic.startswith("cps")) {
1844 CanAcceptPredicationCode = false;
1846 CanAcceptPredicationCode = true;
1850 if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" ||
1851 Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp")
1852 CanAcceptPredicationCode = false;
1855 /// Parse an arm instruction mnemonic followed by its operands.
1856 bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc,
1857 SmallVectorImpl<MCParsedAsmOperand*> &Operands) {
1858 // Create the leading tokens for the mnemonic, split by '.' characters.
1859 size_t Start = 0, Next = Name.find('.');
1860 StringRef Head = Name.slice(Start, Next);
1862 // Split out the predication code and carry setting flag from the mnemonic.
1863 unsigned PredicationCode;
1864 unsigned ProcessorIMod;
1866 Head = SplitMnemonic(Head, PredicationCode, CarrySetting,
1869 Operands.push_back(ARMOperand::CreateToken(Head, NameLoc));
1871 // Next, add the CCOut and ConditionCode operands, if needed.
1873 // For mnemonics which can ever incorporate a carry setting bit or predication
1874 // code, our matching model involves us always generating CCOut and
1875 // ConditionCode operands to match the mnemonic "as written" and then we let
1876 // the matcher deal with finding the right instruction or generating an
1877 // appropriate error.
1878 bool CanAcceptCarrySet, CanAcceptPredicationCode;
1879 GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode);
1881 // Add the carry setting operand, if necessary.
1883 // FIXME: It would be awesome if we could somehow invent a location such that
1884 // match errors on this operand would print a nice diagnostic about how the
1885 // 's' character in the mnemonic resulted in a CCOut operand.
1886 if (CanAcceptCarrySet) {
1887 Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0,
1890 // This mnemonic can't ever accept a carry set, but the user wrote one (or
1891 // misspelled another mnemonic).
1893 // FIXME: Issue a nice error.
1896 // Add the predication code operand, if necessary.
1897 if (CanAcceptPredicationCode) {
1898 Operands.push_back(ARMOperand::CreateCondCode(
1899 ARMCC::CondCodes(PredicationCode), NameLoc));
1901 // This mnemonic can't ever accept a predication code, but the user wrote
1902 // one (or misspelled another mnemonic).
1904 // FIXME: Issue a nice error.
1907 // Add the processor imod operand, if necessary.
1908 if (ProcessorIMod) {
1909 Operands.push_back(ARMOperand::CreateImm(
1910 MCConstantExpr::Create(ProcessorIMod, getContext()),
1913 // This mnemonic can't ever accept a imod, but the user wrote
1914 // one (or misspelled another mnemonic).
1916 // FIXME: Issue a nice error.
1919 // Add the remaining tokens in the mnemonic.
1920 while (Next != StringRef::npos) {
1922 Next = Name.find('.', Start + 1);
1923 StringRef ExtraToken = Name.slice(Start, Next);
1925 Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc));
1928 // Read the remaining operands.
1929 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1930 // Read the first operand.
1931 if (ParseOperand(Operands, Head)) {
1932 Parser.EatToEndOfStatement();
1936 while (getLexer().is(AsmToken::Comma)) {
1937 Parser.Lex(); // Eat the comma.
1939 // Parse and remember the operand.
1940 if (ParseOperand(Operands, Head)) {
1941 Parser.EatToEndOfStatement();
1947 if (getLexer().isNot(AsmToken::EndOfStatement)) {
1948 Parser.EatToEndOfStatement();
1949 return TokError("unexpected token in argument list");
1952 Parser.Lex(); // Consume the EndOfStatement
1957 MatchAndEmitInstruction(SMLoc IDLoc,
1958 SmallVectorImpl<MCParsedAsmOperand*> &Operands,
1962 MatchResultTy MatchResult, MatchResult2;
1963 MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1964 if (MatchResult != Match_Success) {
1965 // If we get a Match_InvalidOperand it might be some arithmetic instruction
1966 // that does not update the condition codes. So try adding a CCOut operand
1967 // with a value of reg0.
1968 if (MatchResult == Match_InvalidOperand) {
1969 Operands.insert(Operands.begin() + 1,
1970 ARMOperand::CreateCCOut(0,
1971 ((ARMOperand*)Operands[0])->getStartLoc()));
1972 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
1973 if (MatchResult2 == Match_Success)
1974 MatchResult = Match_Success;
1976 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
1977 Operands.erase(Operands.begin() + 1);
1981 // If we get a Match_MnemonicFail it might be some arithmetic instruction
1982 // that updates the condition codes if it ends in 's'. So see if the
1983 // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut
1984 // operand with a value of CPSR.
1985 else if(MatchResult == Match_MnemonicFail) {
1986 // Get the instruction mnemonic, which is the first token.
1987 StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken();
1988 if (Mnemonic.substr(Mnemonic.size()-1) == "s") {
1989 // removed the 's' from the mnemonic for matching.
1990 StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1);
1991 SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc();
1992 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
1993 Operands.erase(Operands.begin());
1995 Operands.insert(Operands.begin(),
1996 ARMOperand::CreateToken(MnemonicNoS, NameLoc));
1997 Operands.insert(Operands.begin() + 1,
1998 ARMOperand::CreateCCOut(ARM::CPSR, NameLoc));
1999 MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo);
2000 if (MatchResult2 == Match_Success)
2001 MatchResult = Match_Success;
2003 ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]);
2004 Operands.erase(Operands.begin());
2006 Operands.insert(Operands.begin(),
2007 ARMOperand::CreateToken(Mnemonic, NameLoc));
2008 ARMOperand *CCOut = ((ARMOperand*)Operands[1]);
2009 Operands.erase(Operands.begin() + 1);
2015 switch (MatchResult) {
2017 Out.EmitInstruction(Inst);
2019 case Match_MissingFeature:
2020 Error(IDLoc, "instruction requires a CPU feature not currently enabled");
2022 case Match_InvalidOperand: {
2023 SMLoc ErrorLoc = IDLoc;
2024 if (ErrorInfo != ~0U) {
2025 if (ErrorInfo >= Operands.size())
2026 return Error(IDLoc, "too few operands for instruction");
2028 ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc();
2029 if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc;
2032 return Error(ErrorLoc, "invalid operand for instruction");
2034 case Match_MnemonicFail:
2035 return Error(IDLoc, "unrecognized instruction mnemonic");
2036 case Match_ConversionFail:
2037 return Error(IDLoc, "unable to convert operands to instruction");
2040 llvm_unreachable("Implement any new match types added!");
2044 /// ParseDirective parses the arm specific directives
2045 bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) {
2046 StringRef IDVal = DirectiveID.getIdentifier();
2047 if (IDVal == ".word")
2048 return ParseDirectiveWord(4, DirectiveID.getLoc());
2049 else if (IDVal == ".thumb")
2050 return ParseDirectiveThumb(DirectiveID.getLoc());
2051 else if (IDVal == ".thumb_func")
2052 return ParseDirectiveThumbFunc(DirectiveID.getLoc());
2053 else if (IDVal == ".code")
2054 return ParseDirectiveCode(DirectiveID.getLoc());
2055 else if (IDVal == ".syntax")
2056 return ParseDirectiveSyntax(DirectiveID.getLoc());
2060 /// ParseDirectiveWord
2061 /// ::= .word [ expression (, expression)* ]
2062 bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) {
2063 if (getLexer().isNot(AsmToken::EndOfStatement)) {
2065 const MCExpr *Value;
2066 if (getParser().ParseExpression(Value))
2069 getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/);
2071 if (getLexer().is(AsmToken::EndOfStatement))
2074 // FIXME: Improve diagnostic.
2075 if (getLexer().isNot(AsmToken::Comma))
2076 return Error(L, "unexpected token in directive");
2085 /// ParseDirectiveThumb
2087 bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) {
2088 if (getLexer().isNot(AsmToken::EndOfStatement))
2089 return Error(L, "unexpected token in directive");
2092 // TODO: set thumb mode
2093 // TODO: tell the MC streamer the mode
2094 // getParser().getStreamer().Emit???();
2098 /// ParseDirectiveThumbFunc
2099 /// ::= .thumbfunc symbol_name
2100 bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) {
2101 const AsmToken &Tok = Parser.getTok();
2102 if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String))
2103 return Error(L, "unexpected token in .thumb_func directive");
2104 StringRef Name = Tok.getString();
2105 Parser.Lex(); // Consume the identifier token.
2106 if (getLexer().isNot(AsmToken::EndOfStatement))
2107 return Error(L, "unexpected token in directive");
2110 // Mark symbol as a thumb symbol.
2111 MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name);
2112 getParser().getStreamer().EmitThumbFunc(Func);
2116 /// ParseDirectiveSyntax
2117 /// ::= .syntax unified | divided
2118 bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) {
2119 const AsmToken &Tok = Parser.getTok();
2120 if (Tok.isNot(AsmToken::Identifier))
2121 return Error(L, "unexpected token in .syntax directive");
2122 StringRef Mode = Tok.getString();
2123 if (Mode == "unified" || Mode == "UNIFIED")
2125 else if (Mode == "divided" || Mode == "DIVIDED")
2126 return Error(L, "'.syntax divided' arm asssembly not supported");
2128 return Error(L, "unrecognized syntax mode in .syntax directive");
2130 if (getLexer().isNot(AsmToken::EndOfStatement))
2131 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
2134 // TODO tell the MC streamer the mode
2135 // getParser().getStreamer().Emit???();
2139 /// ParseDirectiveCode
2140 /// ::= .code 16 | 32
2141 bool ARMAsmParser::ParseDirectiveCode(SMLoc L) {
2142 const AsmToken &Tok = Parser.getTok();
2143 if (Tok.isNot(AsmToken::Integer))
2144 return Error(L, "unexpected token in .code directive");
2145 int64_t Val = Parser.getTok().getIntVal();
2151 return Error(L, "invalid operand to .code directive");
2153 if (getLexer().isNot(AsmToken::EndOfStatement))
2154 return Error(Parser.getTok().getLoc(), "unexpected token in directive");
2157 // FIXME: We need to be able switch subtargets at this point so that
2158 // MatchInstructionImpl() will work when it gets the AvailableFeatures which
2159 // includes Feature_IsThumb or not to match the right instructions. This is
2160 // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine.
2162 assert(TM.getSubtarget<ARMSubtarget>().isThumb() &&
2163 "switching between arm/thumb not yet suppported via .code 16)");
2164 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16);
2167 assert(!TM.getSubtarget<ARMSubtarget>().isThumb() &&
2168 "switching between thumb/arm not yet suppported via .code 32)");
2169 getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32);
2175 extern "C" void LLVMInitializeARMAsmLexer();
2177 /// Force static initialization.
2178 extern "C" void LLVMInitializeARMAsmParser() {
2179 RegisterAsmParser<ARMAsmParser> X(TheARMTarget);
2180 RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget);
2181 LLVMInitializeARMAsmLexer();
2184 #define GET_REGISTER_MATCHER
2185 #define GET_MATCHER_IMPLEMENTATION
2186 #include "ARMGenAsmMatcher.inc"