Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1 | //===-- 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 Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 11 | #include "ARMAddressingModes.h" |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 12 | #include "ARMMCExpr.h" |
Evan Cheng | b72d2a9 | 2011-01-11 21:46:47 +0000 | [diff] [blame] | 13 | #include "ARMBaseRegisterInfo.h" |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 14 | #include "ARMSubtarget.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
| 16 | #include "llvm/MC/MCParser/MCAsmParser.h" |
| 17 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
Jim Grosbach | 642fc9c | 2010-11-05 22:33:53 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCStreamer.h" |
| 20 | #include "llvm/MC/MCExpr.h" |
| 21 | #include "llvm/MC/MCInst.h" |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 22 | #include "llvm/Target/TargetRegistry.h" |
| 23 | #include "llvm/Target/TargetAsmParser.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SourceMgr.h" |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallVector.h" |
Owen Anderson | 0c9f250 | 2011-01-13 22:50:36 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringExtras.h" |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | c6ef277 | 2010-01-22 01:44:57 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Twine.h" |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Daniel Dunbar | 6a5c22e | 2011-01-10 15:26:21 +0000 | [diff] [blame] | 32 | /// Shift types used for register controlled shifts in ARM memory addressing. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 33 | enum ShiftType { |
| 34 | Lsl, |
| 35 | Lsr, |
| 36 | Asr, |
| 37 | Ror, |
| 38 | Rrx |
| 39 | }; |
| 40 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 41 | namespace { |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 42 | |
| 43 | class ARMOperand; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 44 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 45 | class ARMAsmParser : public TargetAsmParser { |
| 46 | MCAsmParser &Parser; |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 47 | TargetMachine &TM; |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 48 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 49 | MCAsmParser &getParser() const { return Parser; } |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 50 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 51 | |
| 52 | void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); } |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 53 | bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } |
| 54 | |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 55 | int TryParseRegister(); |
Roman Divacky | bf75532 | 2011-01-27 17:14:22 +0000 | [diff] [blame] | 56 | virtual bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 57 | bool TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &); |
| 58 | bool ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &); |
| 59 | bool ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &); |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 60 | bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, StringRef Mnemonic); |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 61 | bool ParsePrefix(ARMMCExpr::VariantKind &RefKind); |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 62 | const MCExpr *ApplyPrefixToExpr(const MCExpr *E, |
| 63 | MCSymbolRefExpr::VariantKind Variant); |
| 64 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 65 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 66 | bool ParseMemoryOffsetReg(bool &Negative, |
| 67 | bool &OffsetRegShifted, |
| 68 | enum ShiftType &ShiftType, |
| 69 | const MCExpr *&ShiftAmount, |
| 70 | const MCExpr *&Offset, |
| 71 | bool &OffsetIsReg, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 72 | int &OffsetRegNum, |
| 73 | SMLoc &E); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 74 | bool ParseShift(enum ShiftType &St, const MCExpr *&ShiftAmount, SMLoc &E); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 75 | bool ParseDirectiveWord(unsigned Size, SMLoc L); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 76 | bool ParseDirectiveThumb(SMLoc L); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 77 | bool ParseDirectiveThumbFunc(SMLoc L); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 78 | bool ParseDirectiveCode(SMLoc L); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 79 | bool ParseDirectiveSyntax(SMLoc L); |
| 80 | |
Chris Lattner | 7036f8b | 2010-09-29 01:42:58 +0000 | [diff] [blame] | 81 | bool MatchAndEmitInstruction(SMLoc IDLoc, |
Chris Lattner | 7c51a31 | 2010-09-29 01:50:45 +0000 | [diff] [blame] | 82 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 83 | MCStreamer &Out); |
Bruno Cardoso Lopes | fdcee77 | 2011-01-18 20:55:11 +0000 | [diff] [blame] | 84 | void GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet, |
| 85 | bool &CanAcceptPredicationCode); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 86 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 87 | /// @name Auto-generated Match Functions |
| 88 | /// { |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 90 | #define GET_ASSEMBLER_HEADER |
| 91 | #include "ARMGenAsmMatcher.inc" |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 92 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 93 | /// } |
| 94 | |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 95 | OperandMatchResultTy tryParseCoprocNumOperand( |
| 96 | SmallVectorImpl<MCParsedAsmOperand*>&); |
| 97 | OperandMatchResultTy tryParseCoprocRegOperand( |
| 98 | SmallVectorImpl<MCParsedAsmOperand*>&); |
| 99 | OperandMatchResultTy tryParseMemBarrierOptOperand( |
| 100 | SmallVectorImpl<MCParsedAsmOperand*> &); |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 101 | OperandMatchResultTy tryParseProcIFlagsOperand( |
| 102 | SmallVectorImpl<MCParsedAsmOperand*> &); |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 103 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 104 | public: |
Daniel Dunbar | d73ada7 | 2010-07-19 00:33:49 +0000 | [diff] [blame] | 105 | ARMAsmParser(const Target &T, MCAsmParser &_Parser, TargetMachine &_TM) |
Jim Grosbach | 833c93c | 2010-11-01 16:59:54 +0000 | [diff] [blame] | 106 | : TargetAsmParser(T), Parser(_Parser), TM(_TM) { |
| 107 | // Initialize the set of available features. |
| 108 | setAvailableFeatures(ComputeAvailableFeatures( |
| 109 | &TM.getSubtarget<ARMSubtarget>())); |
| 110 | } |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 111 | |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 112 | virtual bool ParseInstruction(StringRef Name, SMLoc NameLoc, |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 113 | SmallVectorImpl<MCParsedAsmOperand*> &Operands); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 114 | virtual bool ParseDirective(AsmToken DirectiveID); |
| 115 | }; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 116 | } // end anonymous namespace |
| 117 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 118 | namespace { |
| 119 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 120 | /// ARMOperand - Instances of this class represent a parsed ARM machine |
| 121 | /// instruction. |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 122 | class ARMOperand : public MCParsedAsmOperand { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 123 | enum KindTy { |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 124 | CondCode, |
Jim Grosbach | d67641b | 2010-12-06 18:21:12 +0000 | [diff] [blame] | 125 | CCOut, |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 126 | CoprocNum, |
| 127 | CoprocReg, |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 128 | Immediate, |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 129 | MemBarrierOpt, |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 130 | Memory, |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 131 | ProcIFlags, |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 132 | Register, |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 133 | RegisterList, |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 134 | DPRRegisterList, |
| 135 | SPRRegisterList, |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 136 | Token |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 137 | } Kind; |
| 138 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 139 | SMLoc StartLoc, EndLoc; |
Bill Wendling | 24d22d2 | 2010-11-18 21:50:54 +0000 | [diff] [blame] | 140 | SmallVector<unsigned, 8> Registers; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 141 | |
| 142 | union { |
| 143 | struct { |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 144 | ARMCC::CondCodes Val; |
| 145 | } CC; |
| 146 | |
| 147 | struct { |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 148 | ARM_MB::MemBOpt Val; |
| 149 | } MBOpt; |
| 150 | |
| 151 | struct { |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 152 | unsigned Val; |
| 153 | } Cop; |
| 154 | |
| 155 | struct { |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 156 | ARM_PROC::IFlags Val; |
| 157 | } IFlags; |
| 158 | |
| 159 | struct { |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 160 | const char *Data; |
| 161 | unsigned Length; |
| 162 | } Tok; |
| 163 | |
| 164 | struct { |
| 165 | unsigned RegNum; |
| 166 | } Reg; |
| 167 | |
Bill Wendling | 8155e5b | 2010-11-06 22:19:43 +0000 | [diff] [blame] | 168 | struct { |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 169 | const MCExpr *Val; |
| 170 | } Imm; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 171 | |
Daniel Dunbar | 6a5c22e | 2011-01-10 15:26:21 +0000 | [diff] [blame] | 172 | /// Combined record for all forms of ARM address expressions. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 173 | struct { |
| 174 | unsigned BaseRegNum; |
Daniel Dunbar | 2637dc9 | 2011-01-18 05:55:15 +0000 | [diff] [blame] | 175 | union { |
| 176 | unsigned RegNum; ///< Offset register num, when OffsetIsReg. |
| 177 | const MCExpr *Value; ///< Offset value, when !OffsetIsReg. |
| 178 | } Offset; |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 179 | const MCExpr *ShiftAmount; // used when OffsetRegShifted is true |
| 180 | enum ShiftType ShiftType; // used when OffsetRegShifted is true |
| 181 | unsigned OffsetRegShifted : 1; // only used when OffsetIsReg is true |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 182 | unsigned Preindexed : 1; |
| 183 | unsigned Postindexed : 1; |
| 184 | unsigned OffsetIsReg : 1; |
| 185 | unsigned Negative : 1; // only used when OffsetIsReg is true |
| 186 | unsigned Writeback : 1; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 187 | } Mem; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 188 | }; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 189 | |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 190 | ARMOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {} |
| 191 | public: |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 192 | ARMOperand(const ARMOperand &o) : MCParsedAsmOperand() { |
| 193 | Kind = o.Kind; |
| 194 | StartLoc = o.StartLoc; |
| 195 | EndLoc = o.EndLoc; |
| 196 | switch (Kind) { |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 197 | case CondCode: |
| 198 | CC = o.CC; |
| 199 | break; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 200 | case Token: |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 201 | Tok = o.Tok; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 202 | break; |
Jim Grosbach | d67641b | 2010-12-06 18:21:12 +0000 | [diff] [blame] | 203 | case CCOut: |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 204 | case Register: |
| 205 | Reg = o.Reg; |
| 206 | break; |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 207 | case RegisterList: |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 208 | case DPRRegisterList: |
| 209 | case SPRRegisterList: |
Bill Wendling | 24d22d2 | 2010-11-18 21:50:54 +0000 | [diff] [blame] | 210 | Registers = o.Registers; |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 211 | break; |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 212 | case CoprocNum: |
| 213 | case CoprocReg: |
| 214 | Cop = o.Cop; |
| 215 | break; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 216 | case Immediate: |
| 217 | Imm = o.Imm; |
| 218 | break; |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 219 | case MemBarrierOpt: |
| 220 | MBOpt = o.MBOpt; |
| 221 | break; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 222 | case Memory: |
| 223 | Mem = o.Mem; |
| 224 | break; |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 225 | case ProcIFlags: |
| 226 | IFlags = o.IFlags; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 229 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 230 | /// getStartLoc - Get the location of the first token of this operand. |
| 231 | SMLoc getStartLoc() const { return StartLoc; } |
| 232 | /// getEndLoc - Get the location of the last token of this operand. |
| 233 | SMLoc getEndLoc() const { return EndLoc; } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 234 | |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 235 | ARMCC::CondCodes getCondCode() const { |
| 236 | assert(Kind == CondCode && "Invalid access!"); |
| 237 | return CC.Val; |
| 238 | } |
| 239 | |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 240 | unsigned getCoproc() const { |
| 241 | assert((Kind == CoprocNum || Kind == CoprocReg) && "Invalid access!"); |
| 242 | return Cop.Val; |
| 243 | } |
| 244 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 245 | StringRef getToken() const { |
| 246 | assert(Kind == Token && "Invalid access!"); |
| 247 | return StringRef(Tok.Data, Tok.Length); |
| 248 | } |
| 249 | |
| 250 | unsigned getReg() const { |
Benjamin Kramer | 6aa4943 | 2010-12-07 15:50:35 +0000 | [diff] [blame] | 251 | assert((Kind == Register || Kind == CCOut) && "Invalid access!"); |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 252 | return Reg.RegNum; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 255 | const SmallVectorImpl<unsigned> &getRegList() const { |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 256 | assert((Kind == RegisterList || Kind == DPRRegisterList || |
| 257 | Kind == SPRRegisterList) && "Invalid access!"); |
Bill Wendling | 24d22d2 | 2010-11-18 21:50:54 +0000 | [diff] [blame] | 258 | return Registers; |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 261 | const MCExpr *getImm() const { |
| 262 | assert(Kind == Immediate && "Invalid access!"); |
| 263 | return Imm.Val; |
| 264 | } |
| 265 | |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 266 | ARM_MB::MemBOpt getMemBarrierOpt() const { |
| 267 | assert(Kind == MemBarrierOpt && "Invalid access!"); |
| 268 | return MBOpt.Val; |
| 269 | } |
| 270 | |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 271 | ARM_PROC::IFlags getProcIFlags() const { |
| 272 | assert(Kind == ProcIFlags && "Invalid access!"); |
| 273 | return IFlags.Val; |
| 274 | } |
| 275 | |
Daniel Dunbar | 6ec5620 | 2011-01-18 05:55:21 +0000 | [diff] [blame] | 276 | /// @name Memory Operand Accessors |
| 277 | /// @{ |
| 278 | |
| 279 | unsigned getMemBaseRegNum() const { |
| 280 | return Mem.BaseRegNum; |
| 281 | } |
| 282 | unsigned getMemOffsetRegNum() const { |
| 283 | assert(Mem.OffsetIsReg && "Invalid access!"); |
| 284 | return Mem.Offset.RegNum; |
| 285 | } |
| 286 | const MCExpr *getMemOffset() const { |
| 287 | assert(!Mem.OffsetIsReg && "Invalid access!"); |
| 288 | return Mem.Offset.Value; |
| 289 | } |
| 290 | unsigned getMemOffsetRegShifted() const { |
| 291 | assert(Mem.OffsetIsReg && "Invalid access!"); |
| 292 | return Mem.OffsetRegShifted; |
| 293 | } |
| 294 | const MCExpr *getMemShiftAmount() const { |
| 295 | assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!"); |
| 296 | return Mem.ShiftAmount; |
| 297 | } |
| 298 | enum ShiftType getMemShiftType() const { |
| 299 | assert(Mem.OffsetIsReg && Mem.OffsetRegShifted && "Invalid access!"); |
| 300 | return Mem.ShiftType; |
| 301 | } |
| 302 | bool getMemPreindexed() const { return Mem.Preindexed; } |
| 303 | bool getMemPostindexed() const { return Mem.Postindexed; } |
| 304 | bool getMemOffsetIsReg() const { return Mem.OffsetIsReg; } |
| 305 | bool getMemNegative() const { return Mem.Negative; } |
| 306 | bool getMemWriteback() const { return Mem.Writeback; } |
| 307 | |
| 308 | /// @} |
| 309 | |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 310 | bool isCoprocNum() const { return Kind == CoprocNum; } |
| 311 | bool isCoprocReg() const { return Kind == CoprocReg; } |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 312 | bool isCondCode() const { return Kind == CondCode; } |
Jim Grosbach | d67641b | 2010-12-06 18:21:12 +0000 | [diff] [blame] | 313 | bool isCCOut() const { return Kind == CCOut; } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 314 | bool isImm() const { return Kind == Immediate; } |
Bill Wendling | b32e784 | 2010-11-08 00:32:40 +0000 | [diff] [blame] | 315 | bool isReg() const { return Kind == Register; } |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 316 | bool isRegList() const { return Kind == RegisterList; } |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 317 | bool isDPRRegList() const { return Kind == DPRRegisterList; } |
| 318 | bool isSPRRegList() const { return Kind == SPRRegisterList; } |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 319 | bool isToken() const { return Kind == Token; } |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 320 | bool isMemBarrierOpt() const { return Kind == MemBarrierOpt; } |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 321 | bool isMemory() const { return Kind == Memory; } |
Bill Wendling | 87f4f9a | 2010-11-08 23:49:57 +0000 | [diff] [blame] | 322 | bool isMemMode5() const { |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 323 | if (!isMemory() || getMemOffsetIsReg() || getMemWriteback() || |
| 324 | getMemNegative()) |
Bill Wendling | 87f4f9a | 2010-11-08 23:49:57 +0000 | [diff] [blame] | 325 | return false; |
Bill Wendling | ef4a68b | 2010-11-30 07:44:32 +0000 | [diff] [blame] | 326 | |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 327 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset()); |
Bill Wendling | ef4a68b | 2010-11-30 07:44:32 +0000 | [diff] [blame] | 328 | if (!CE) return false; |
| 329 | |
Bill Wendling | 87f4f9a | 2010-11-08 23:49:57 +0000 | [diff] [blame] | 330 | // The offset must be a multiple of 4 in the range 0-1020. |
| 331 | int64_t Value = CE->getValue(); |
| 332 | return ((Value & 0x3) == 0 && Value <= 1020 && Value >= -1020); |
| 333 | } |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 334 | bool isMemModeRegThumb() const { |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 335 | if (!isMemory() || !getMemOffsetIsReg() || getMemWriteback()) |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 336 | return false; |
Daniel Dunbar | d3df5f3 | 2011-01-18 05:34:11 +0000 | [diff] [blame] | 337 | return true; |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 338 | } |
| 339 | bool isMemModeImmThumb() const { |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 340 | if (!isMemory() || getMemOffsetIsReg() || getMemWriteback()) |
Bill Wendling | ef4a68b | 2010-11-30 07:44:32 +0000 | [diff] [blame] | 341 | return false; |
| 342 | |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 343 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset()); |
Bill Wendling | ef4a68b | 2010-11-30 07:44:32 +0000 | [diff] [blame] | 344 | if (!CE) return false; |
| 345 | |
| 346 | // The offset must be a multiple of 4 in the range 0-124. |
| 347 | uint64_t Value = CE->getValue(); |
| 348 | return ((Value & 0x3) == 0 && Value <= 124); |
| 349 | } |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 350 | bool isProcIFlags() const { return Kind == ProcIFlags; } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 351 | |
| 352 | void addExpr(MCInst &Inst, const MCExpr *Expr) const { |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 353 | // Add as immediates when possible. Null MCExpr = 0. |
| 354 | if (Expr == 0) |
| 355 | Inst.addOperand(MCOperand::CreateImm(0)); |
| 356 | else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 357 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 358 | else |
| 359 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
| 360 | } |
| 361 | |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 362 | void addCondCodeOperands(MCInst &Inst, unsigned N) const { |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 363 | assert(N == 2 && "Invalid number of operands!"); |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 364 | Inst.addOperand(MCOperand::CreateImm(unsigned(getCondCode()))); |
Jim Grosbach | 04f7494 | 2010-12-06 18:30:57 +0000 | [diff] [blame] | 365 | unsigned RegNum = getCondCode() == ARMCC::AL ? 0: ARM::CPSR; |
| 366 | Inst.addOperand(MCOperand::CreateReg(RegNum)); |
Daniel Dunbar | 8462b30 | 2010-08-11 06:36:53 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 369 | void addCoprocNumOperands(MCInst &Inst, unsigned N) const { |
| 370 | assert(N == 1 && "Invalid number of operands!"); |
| 371 | Inst.addOperand(MCOperand::CreateImm(getCoproc())); |
| 372 | } |
| 373 | |
| 374 | void addCoprocRegOperands(MCInst &Inst, unsigned N) const { |
| 375 | assert(N == 1 && "Invalid number of operands!"); |
| 376 | Inst.addOperand(MCOperand::CreateImm(getCoproc())); |
| 377 | } |
| 378 | |
Jim Grosbach | d67641b | 2010-12-06 18:21:12 +0000 | [diff] [blame] | 379 | void addCCOutOperands(MCInst &Inst, unsigned N) const { |
| 380 | assert(N == 1 && "Invalid number of operands!"); |
| 381 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 382 | } |
| 383 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 384 | void addRegOperands(MCInst &Inst, unsigned N) const { |
| 385 | assert(N == 1 && "Invalid number of operands!"); |
| 386 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
| 387 | } |
| 388 | |
Bill Wendling | 87f4f9a | 2010-11-08 23:49:57 +0000 | [diff] [blame] | 389 | void addRegListOperands(MCInst &Inst, unsigned N) const { |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 390 | assert(N == 1 && "Invalid number of operands!"); |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 391 | const SmallVectorImpl<unsigned> &RegList = getRegList(); |
| 392 | for (SmallVectorImpl<unsigned>::const_iterator |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 393 | I = RegList.begin(), E = RegList.end(); I != E; ++I) |
| 394 | Inst.addOperand(MCOperand::CreateReg(*I)); |
Bill Wendling | 87f4f9a | 2010-11-08 23:49:57 +0000 | [diff] [blame] | 395 | } |
| 396 | |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 397 | void addDPRRegListOperands(MCInst &Inst, unsigned N) const { |
| 398 | addRegListOperands(Inst, N); |
| 399 | } |
| 400 | |
| 401 | void addSPRRegListOperands(MCInst &Inst, unsigned N) const { |
| 402 | addRegListOperands(Inst, N); |
| 403 | } |
| 404 | |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 405 | void addImmOperands(MCInst &Inst, unsigned N) const { |
| 406 | assert(N == 1 && "Invalid number of operands!"); |
| 407 | addExpr(Inst, getImm()); |
| 408 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 409 | |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 410 | void addMemBarrierOptOperands(MCInst &Inst, unsigned N) const { |
| 411 | assert(N == 1 && "Invalid number of operands!"); |
| 412 | Inst.addOperand(MCOperand::CreateImm(unsigned(getMemBarrierOpt()))); |
| 413 | } |
| 414 | |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 415 | void addMemMode5Operands(MCInst &Inst, unsigned N) const { |
| 416 | assert(N == 2 && isMemMode5() && "Invalid number of operands!"); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 417 | |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 418 | Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum())); |
| 419 | assert(!getMemOffsetIsReg() && "Invalid mode 5 operand"); |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 420 | |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 421 | // FIXME: #-0 is encoded differently than #0. Does the parser preserve |
| 422 | // the difference? |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 423 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset()); |
Daniel Dunbar | d3df5f3 | 2011-01-18 05:34:11 +0000 | [diff] [blame] | 424 | assert(CE && "Non-constant mode 5 offset operand!"); |
Bill Wendling | 92b5a2e | 2010-11-03 01:49:29 +0000 | [diff] [blame] | 425 | |
Daniel Dunbar | d3df5f3 | 2011-01-18 05:34:11 +0000 | [diff] [blame] | 426 | // The MCInst offset operand doesn't include the low two bits (like |
| 427 | // the instruction encoding). |
| 428 | int64_t Offset = CE->getValue() / 4; |
| 429 | if (Offset >= 0) |
| 430 | Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::add, |
| 431 | Offset))); |
| 432 | else |
| 433 | Inst.addOperand(MCOperand::CreateImm(ARM_AM::getAM5Opc(ARM_AM::sub, |
| 434 | -Offset))); |
Chris Lattner | 14b9385 | 2010-10-29 00:27:31 +0000 | [diff] [blame] | 435 | } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 436 | |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 437 | void addMemModeRegThumbOperands(MCInst &Inst, unsigned N) const { |
| 438 | assert(N == 2 && isMemModeRegThumb() && "Invalid number of operands!"); |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 439 | Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum())); |
| 440 | Inst.addOperand(MCOperand::CreateReg(getMemOffsetRegNum())); |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 441 | } |
Bill Wendling | ef4a68b | 2010-11-30 07:44:32 +0000 | [diff] [blame] | 442 | |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 443 | void addMemModeImmThumbOperands(MCInst &Inst, unsigned N) const { |
| 444 | assert(N == 2 && isMemModeImmThumb() && "Invalid number of operands!"); |
Daniel Dunbar | 4b46267 | 2011-01-18 05:55:27 +0000 | [diff] [blame] | 445 | Inst.addOperand(MCOperand::CreateReg(getMemBaseRegNum())); |
| 446 | const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(getMemOffset()); |
Bill Wendling | f4caf69 | 2010-12-14 03:36:38 +0000 | [diff] [blame] | 447 | assert(CE && "Non-constant mode offset operand!"); |
| 448 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
Bill Wendling | ef4a68b | 2010-11-30 07:44:32 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 451 | void addProcIFlagsOperands(MCInst &Inst, unsigned N) const { |
| 452 | assert(N == 1 && "Invalid number of operands!"); |
| 453 | Inst.addOperand(MCOperand::CreateImm(unsigned(getProcIFlags()))); |
| 454 | } |
| 455 | |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 456 | virtual void dump(raw_ostream &OS) const; |
Daniel Dunbar | b3cb696 | 2010-08-11 06:37:04 +0000 | [diff] [blame] | 457 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 458 | static ARMOperand *CreateCondCode(ARMCC::CondCodes CC, SMLoc S) { |
| 459 | ARMOperand *Op = new ARMOperand(CondCode); |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 460 | Op->CC.Val = CC; |
| 461 | Op->StartLoc = S; |
| 462 | Op->EndLoc = S; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 463 | return Op; |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 466 | static ARMOperand *CreateCoprocNum(unsigned CopVal, SMLoc S) { |
| 467 | ARMOperand *Op = new ARMOperand(CoprocNum); |
| 468 | Op->Cop.Val = CopVal; |
| 469 | Op->StartLoc = S; |
| 470 | Op->EndLoc = S; |
| 471 | return Op; |
| 472 | } |
| 473 | |
| 474 | static ARMOperand *CreateCoprocReg(unsigned CopVal, SMLoc S) { |
| 475 | ARMOperand *Op = new ARMOperand(CoprocReg); |
| 476 | Op->Cop.Val = CopVal; |
| 477 | Op->StartLoc = S; |
| 478 | Op->EndLoc = S; |
| 479 | return Op; |
| 480 | } |
| 481 | |
Jim Grosbach | d67641b | 2010-12-06 18:21:12 +0000 | [diff] [blame] | 482 | static ARMOperand *CreateCCOut(unsigned RegNum, SMLoc S) { |
| 483 | ARMOperand *Op = new ARMOperand(CCOut); |
| 484 | Op->Reg.RegNum = RegNum; |
| 485 | Op->StartLoc = S; |
| 486 | Op->EndLoc = S; |
| 487 | return Op; |
| 488 | } |
| 489 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 490 | static ARMOperand *CreateToken(StringRef Str, SMLoc S) { |
| 491 | ARMOperand *Op = new ARMOperand(Token); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 492 | Op->Tok.Data = Str.data(); |
| 493 | Op->Tok.Length = Str.size(); |
| 494 | Op->StartLoc = S; |
| 495 | Op->EndLoc = S; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 496 | return Op; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 499 | static ARMOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) { |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 500 | ARMOperand *Op = new ARMOperand(Register); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 501 | Op->Reg.RegNum = RegNum; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 502 | Op->StartLoc = S; |
| 503 | Op->EndLoc = E; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 504 | return Op; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 507 | static ARMOperand * |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 508 | CreateRegList(const SmallVectorImpl<std::pair<unsigned, SMLoc> > &Regs, |
Matt Beaumont-Gay | cc8d10e | 2010-11-10 00:08:58 +0000 | [diff] [blame] | 509 | SMLoc StartLoc, SMLoc EndLoc) { |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 510 | KindTy Kind = RegisterList; |
| 511 | |
| 512 | if (ARM::DPRRegClass.contains(Regs.front().first)) |
| 513 | Kind = DPRRegisterList; |
| 514 | else if (ARM::SPRRegClass.contains(Regs.front().first)) |
| 515 | Kind = SPRRegisterList; |
| 516 | |
| 517 | ARMOperand *Op = new ARMOperand(Kind); |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 518 | for (SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 519 | I = Regs.begin(), E = Regs.end(); I != E; ++I) |
Bill Wendling | 24d22d2 | 2010-11-18 21:50:54 +0000 | [diff] [blame] | 520 | Op->Registers.push_back(I->first); |
Bill Wendling | cb21d1c | 2010-11-19 00:38:19 +0000 | [diff] [blame] | 521 | array_pod_sort(Op->Registers.begin(), Op->Registers.end()); |
Matt Beaumont-Gay | cc8d10e | 2010-11-10 00:08:58 +0000 | [diff] [blame] | 522 | Op->StartLoc = StartLoc; |
| 523 | Op->EndLoc = EndLoc; |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 524 | return Op; |
| 525 | } |
| 526 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 527 | static ARMOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) { |
| 528 | ARMOperand *Op = new ARMOperand(Immediate); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 529 | Op->Imm.Val = Val; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 530 | Op->StartLoc = S; |
| 531 | Op->EndLoc = E; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 532 | return Op; |
Kevin Enderby | cfe0724 | 2009-10-13 22:19:02 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 535 | static ARMOperand *CreateMem(unsigned BaseRegNum, bool OffsetIsReg, |
Daniel Dunbar | 023835d | 2011-01-18 05:34:05 +0000 | [diff] [blame] | 536 | const MCExpr *Offset, int OffsetRegNum, |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 537 | bool OffsetRegShifted, enum ShiftType ShiftType, |
| 538 | const MCExpr *ShiftAmount, bool Preindexed, |
| 539 | bool Postindexed, bool Negative, bool Writeback, |
| 540 | SMLoc S, SMLoc E) { |
Daniel Dunbar | 023835d | 2011-01-18 05:34:05 +0000 | [diff] [blame] | 541 | assert((OffsetRegNum == -1 || OffsetIsReg) && |
| 542 | "OffsetRegNum must imply OffsetIsReg!"); |
| 543 | assert((!OffsetRegShifted || OffsetIsReg) && |
| 544 | "OffsetRegShifted must imply OffsetIsReg!"); |
Daniel Dunbar | d3df5f3 | 2011-01-18 05:34:11 +0000 | [diff] [blame] | 545 | assert((Offset || OffsetIsReg) && |
| 546 | "Offset must exists unless register offset is used!"); |
Daniel Dunbar | 023835d | 2011-01-18 05:34:05 +0000 | [diff] [blame] | 547 | assert((!ShiftAmount || (OffsetIsReg && OffsetRegShifted)) && |
| 548 | "Cannot have shift amount without shifted register offset!"); |
| 549 | assert((!Offset || !OffsetIsReg) && |
| 550 | "Cannot have expression offset and register offset!"); |
| 551 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 552 | ARMOperand *Op = new ARMOperand(Memory); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 553 | Op->Mem.BaseRegNum = BaseRegNum; |
| 554 | Op->Mem.OffsetIsReg = OffsetIsReg; |
Daniel Dunbar | 2637dc9 | 2011-01-18 05:55:15 +0000 | [diff] [blame] | 555 | if (OffsetIsReg) |
| 556 | Op->Mem.Offset.RegNum = OffsetRegNum; |
| 557 | else |
| 558 | Op->Mem.Offset.Value = Offset; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 559 | Op->Mem.OffsetRegShifted = OffsetRegShifted; |
| 560 | Op->Mem.ShiftType = ShiftType; |
| 561 | Op->Mem.ShiftAmount = ShiftAmount; |
| 562 | Op->Mem.Preindexed = Preindexed; |
| 563 | Op->Mem.Postindexed = Postindexed; |
| 564 | Op->Mem.Negative = Negative; |
| 565 | Op->Mem.Writeback = Writeback; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 566 | |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 567 | Op->StartLoc = S; |
| 568 | Op->EndLoc = E; |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 569 | return Op; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 570 | } |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 571 | |
| 572 | static ARMOperand *CreateMemBarrierOpt(ARM_MB::MemBOpt Opt, SMLoc S) { |
| 573 | ARMOperand *Op = new ARMOperand(MemBarrierOpt); |
| 574 | Op->MBOpt.Val = Opt; |
| 575 | Op->StartLoc = S; |
| 576 | Op->EndLoc = S; |
| 577 | return Op; |
| 578 | } |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 579 | |
| 580 | static ARMOperand *CreateProcIFlags(ARM_PROC::IFlags IFlags, SMLoc S) { |
| 581 | ARMOperand *Op = new ARMOperand(ProcIFlags); |
| 582 | Op->IFlags.Val = IFlags; |
| 583 | Op->StartLoc = S; |
| 584 | Op->EndLoc = S; |
| 585 | return Op; |
| 586 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 587 | }; |
| 588 | |
| 589 | } // end anonymous namespace. |
| 590 | |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 591 | void ARMOperand::dump(raw_ostream &OS) const { |
| 592 | switch (Kind) { |
| 593 | case CondCode: |
Daniel Dunbar | 6a5c22e | 2011-01-10 15:26:21 +0000 | [diff] [blame] | 594 | OS << "<ARMCC::" << ARMCondCodeToString(getCondCode()) << ">"; |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 595 | break; |
Jim Grosbach | d67641b | 2010-12-06 18:21:12 +0000 | [diff] [blame] | 596 | case CCOut: |
| 597 | OS << "<ccout " << getReg() << ">"; |
| 598 | break; |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 599 | case CoprocNum: |
| 600 | OS << "<coprocessor number: " << getCoproc() << ">"; |
| 601 | break; |
| 602 | case CoprocReg: |
| 603 | OS << "<coprocessor register: " << getCoproc() << ">"; |
| 604 | break; |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 605 | case Immediate: |
| 606 | getImm()->print(OS); |
| 607 | break; |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 608 | case MemBarrierOpt: |
| 609 | OS << "<ARM_MB::" << MemBOptToString(getMemBarrierOpt()) << ">"; |
| 610 | break; |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 611 | case Memory: |
Daniel Dunbar | 6ec5620 | 2011-01-18 05:55:21 +0000 | [diff] [blame] | 612 | OS << "<memory " |
| 613 | << "base:" << getMemBaseRegNum(); |
| 614 | if (getMemOffsetIsReg()) { |
| 615 | OS << " offset:<register " << getMemOffsetRegNum(); |
| 616 | if (getMemOffsetRegShifted()) { |
| 617 | OS << " offset-shift-type:" << getMemShiftType(); |
| 618 | OS << " offset-shift-amount:" << *getMemShiftAmount(); |
| 619 | } |
| 620 | } else { |
| 621 | OS << " offset:" << *getMemOffset(); |
| 622 | } |
| 623 | if (getMemOffsetIsReg()) |
| 624 | OS << " (offset-is-reg)"; |
| 625 | if (getMemPreindexed()) |
| 626 | OS << " (pre-indexed)"; |
| 627 | if (getMemPostindexed()) |
| 628 | OS << " (post-indexed)"; |
| 629 | if (getMemNegative()) |
| 630 | OS << " (negative)"; |
| 631 | if (getMemWriteback()) |
| 632 | OS << " (writeback)"; |
| 633 | OS << ">"; |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 634 | break; |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 635 | case ProcIFlags: { |
| 636 | OS << "<ARM_PROC::"; |
| 637 | unsigned IFlags = getProcIFlags(); |
| 638 | for (int i=2; i >= 0; --i) |
| 639 | if (IFlags & (1 << i)) |
| 640 | OS << ARM_PROC::IFlagsToString(1 << i); |
| 641 | OS << ">"; |
| 642 | break; |
| 643 | } |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 644 | case Register: |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 645 | OS << "<register " << getReg() << ">"; |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 646 | break; |
Bill Wendling | 0f63075 | 2010-11-17 04:32:08 +0000 | [diff] [blame] | 647 | case RegisterList: |
| 648 | case DPRRegisterList: |
| 649 | case SPRRegisterList: { |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 650 | OS << "<register_list "; |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 651 | |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 652 | const SmallVectorImpl<unsigned> &RegList = getRegList(); |
| 653 | for (SmallVectorImpl<unsigned>::const_iterator |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 654 | I = RegList.begin(), E = RegList.end(); I != E; ) { |
| 655 | OS << *I; |
| 656 | if (++I < E) OS << ", "; |
Bill Wendling | 8d5acb7 | 2010-11-06 19:56:04 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | OS << ">"; |
| 660 | break; |
| 661 | } |
Daniel Dunbar | fa315de | 2010-08-11 06:37:12 +0000 | [diff] [blame] | 662 | case Token: |
| 663 | OS << "'" << getToken() << "'"; |
| 664 | break; |
| 665 | } |
| 666 | } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 667 | |
| 668 | /// @name Auto-generated Match Functions |
| 669 | /// { |
| 670 | |
| 671 | static unsigned MatchRegisterName(StringRef Name); |
| 672 | |
| 673 | /// } |
| 674 | |
Bob Wilson | 69df723 | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 675 | bool ARMAsmParser::ParseRegister(unsigned &RegNo, |
| 676 | SMLoc &StartLoc, SMLoc &EndLoc) { |
Roman Divacky | bf75532 | 2011-01-27 17:14:22 +0000 | [diff] [blame] | 677 | RegNo = TryParseRegister(); |
| 678 | |
| 679 | return (RegNo == (unsigned)-1); |
| 680 | } |
| 681 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 682 | /// Try to parse a register name. The token must be an Identifier when called, |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 683 | /// and if it is a register name the token is eaten and the register number is |
| 684 | /// returned. Otherwise return -1. |
| 685 | /// |
| 686 | int ARMAsmParser::TryParseRegister() { |
| 687 | const AsmToken &Tok = Parser.getTok(); |
| 688 | assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier"); |
Jim Grosbach | d4462a5 | 2010-11-01 16:44:21 +0000 | [diff] [blame] | 689 | |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 690 | // FIXME: Validate register for the current architecture; we have to do |
| 691 | // validation later, so maybe there is no need for this here. |
Owen Anderson | 0c9f250 | 2011-01-13 22:50:36 +0000 | [diff] [blame] | 692 | std::string upperCase = Tok.getString().str(); |
| 693 | std::string lowerCase = LowercaseString(upperCase); |
| 694 | unsigned RegNum = MatchRegisterName(lowerCase); |
| 695 | if (!RegNum) { |
| 696 | RegNum = StringSwitch<unsigned>(lowerCase) |
| 697 | .Case("r13", ARM::SP) |
| 698 | .Case("r14", ARM::LR) |
| 699 | .Case("r15", ARM::PC) |
| 700 | .Case("ip", ARM::R12) |
| 701 | .Default(0); |
| 702 | } |
| 703 | if (!RegNum) return -1; |
Bob Wilson | 69df723 | 2011-02-03 21:46:10 +0000 | [diff] [blame] | 704 | |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 705 | Parser.Lex(); // Eat identifier token. |
| 706 | return RegNum; |
| 707 | } |
Jim Grosbach | d4462a5 | 2010-11-01 16:44:21 +0000 | [diff] [blame] | 708 | |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 709 | /// Try to parse a register name. The token must be an Identifier when called. |
| 710 | /// If it's a register, an AsmOperand is created. Another AsmOperand is created |
| 711 | /// if there is a "writeback". 'true' if it's not a register. |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 712 | /// |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 713 | /// TODO this is likely to change to allow different register types and or to |
| 714 | /// parse for a specific register type. |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 715 | bool ARMAsmParser:: |
| 716 | TryParseRegisterWithWriteBack(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 717 | SMLoc S = Parser.getTok().getLoc(); |
| 718 | int RegNo = TryParseRegister(); |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 719 | if (RegNo == -1) |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 720 | return true; |
Jim Grosbach | d4462a5 | 2010-11-01 16:44:21 +0000 | [diff] [blame] | 721 | |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 722 | Operands.push_back(ARMOperand::CreateReg(RegNo, S, Parser.getTok().getLoc())); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 723 | |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 724 | const AsmToken &ExclaimTok = Parser.getTok(); |
| 725 | if (ExclaimTok.is(AsmToken::Exclaim)) { |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 726 | Operands.push_back(ARMOperand::CreateToken(ExclaimTok.getString(), |
| 727 | ExclaimTok.getLoc())); |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 728 | Parser.Lex(); // Eat exclaim token |
Kevin Enderby | 99e6d4e | 2009-10-07 18:01:35 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 731 | return false; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 734 | /// MatchCoprocessorOperandName - Try to parse an coprocessor related |
| 735 | /// instruction with a symbolic operand name. Example: "p1", "p7", "c3", |
| 736 | /// "c5", ... |
| 737 | static int MatchCoprocessorOperandName(StringRef Name, char CoprocOp) { |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 738 | // Use the same layout as the tablegen'erated register name matcher. Ugly, |
| 739 | // but efficient. |
| 740 | switch (Name.size()) { |
| 741 | default: break; |
| 742 | case 2: |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 743 | if (Name[0] != CoprocOp) |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 744 | return -1; |
| 745 | switch (Name[1]) { |
| 746 | default: return -1; |
| 747 | case '0': return 0; |
| 748 | case '1': return 1; |
| 749 | case '2': return 2; |
| 750 | case '3': return 3; |
| 751 | case '4': return 4; |
| 752 | case '5': return 5; |
| 753 | case '6': return 6; |
| 754 | case '7': return 7; |
| 755 | case '8': return 8; |
| 756 | case '9': return 9; |
| 757 | } |
| 758 | break; |
| 759 | case 3: |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 760 | if (Name[0] != CoprocOp || Name[1] != '1') |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 761 | return -1; |
| 762 | switch (Name[2]) { |
| 763 | default: return -1; |
| 764 | case '0': return 10; |
| 765 | case '1': return 11; |
| 766 | case '2': return 12; |
| 767 | case '3': return 13; |
| 768 | case '4': return 14; |
| 769 | case '5': return 15; |
| 770 | } |
| 771 | break; |
| 772 | } |
| 773 | |
| 774 | llvm_unreachable("Unhandled coprocessor operand string!"); |
| 775 | return -1; |
| 776 | } |
| 777 | |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 778 | /// tryParseCoprocNumOperand - Try to parse an coprocessor number operand. The |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 779 | /// token must be an Identifier when called, and if it is a coprocessor |
| 780 | /// number, the token is eaten and the operand is added to the operand list. |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 781 | ARMAsmParser::OperandMatchResultTy ARMAsmParser:: |
| 782 | tryParseCoprocNumOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 783 | SMLoc S = Parser.getTok().getLoc(); |
| 784 | const AsmToken &Tok = Parser.getTok(); |
| 785 | assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier"); |
| 786 | |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 787 | int Num = MatchCoprocessorOperandName(Tok.getString(), 'p'); |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 788 | if (Num == -1) |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 789 | return MatchOperand_NoMatch; |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 790 | |
| 791 | Parser.Lex(); // Eat identifier token. |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 792 | Operands.push_back(ARMOperand::CreateCoprocNum(Num, S)); |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 793 | return MatchOperand_Success; |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 794 | } |
| 795 | |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 796 | /// tryParseCoprocRegOperand - Try to parse an coprocessor register operand. The |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 797 | /// token must be an Identifier when called, and if it is a coprocessor |
| 798 | /// number, the token is eaten and the operand is added to the operand list. |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 799 | ARMAsmParser::OperandMatchResultTy ARMAsmParser:: |
| 800 | tryParseCoprocRegOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 801 | SMLoc S = Parser.getTok().getLoc(); |
| 802 | const AsmToken &Tok = Parser.getTok(); |
| 803 | assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier"); |
| 804 | |
| 805 | int Reg = MatchCoprocessorOperandName(Tok.getString(), 'c'); |
| 806 | if (Reg == -1) |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 807 | return MatchOperand_NoMatch; |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 808 | |
| 809 | Parser.Lex(); // Eat identifier token. |
| 810 | Operands.push_back(ARMOperand::CreateCoprocReg(Reg, S)); |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 811 | return MatchOperand_Success; |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 814 | /// Parse a register list, return it if successful else return null. The first |
| 815 | /// token must be a '{' when called. |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 816 | bool ARMAsmParser:: |
| 817 | ParseRegisterList(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 818 | assert(Parser.getTok().is(AsmToken::LCurly) && |
Bill Wendling | a60f157 | 2010-11-06 10:48:18 +0000 | [diff] [blame] | 819 | "Token is not a Left Curly Brace"); |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 820 | SMLoc S = Parser.getTok().getLoc(); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 821 | |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 822 | // Read the rest of the registers in the list. |
| 823 | unsigned PrevRegNum = 0; |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 824 | SmallVector<std::pair<unsigned, SMLoc>, 32> Registers; |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 825 | |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 826 | do { |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 827 | bool IsRange = Parser.getTok().is(AsmToken::Minus); |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 828 | Parser.Lex(); // Eat non-identifier token. |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 829 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 830 | const AsmToken &RegTok = Parser.getTok(); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 831 | SMLoc RegLoc = RegTok.getLoc(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 832 | if (RegTok.isNot(AsmToken::Identifier)) { |
| 833 | Error(RegLoc, "register expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 834 | return true; |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 835 | } |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 836 | |
Bill Wendling | 1d6a265 | 2010-11-06 10:40:24 +0000 | [diff] [blame] | 837 | int RegNum = TryParseRegister(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 838 | if (RegNum == -1) { |
| 839 | Error(RegLoc, "register expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 840 | return true; |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 841 | } |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 842 | |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 843 | if (IsRange) { |
| 844 | int Reg = PrevRegNum; |
| 845 | do { |
| 846 | ++Reg; |
| 847 | Registers.push_back(std::make_pair(Reg, RegLoc)); |
| 848 | } while (Reg != RegNum); |
| 849 | } else { |
| 850 | Registers.push_back(std::make_pair(RegNum, RegLoc)); |
| 851 | } |
| 852 | |
| 853 | PrevRegNum = RegNum; |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 854 | } while (Parser.getTok().is(AsmToken::Comma) || |
| 855 | Parser.getTok().is(AsmToken::Minus)); |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 856 | |
| 857 | // Process the right curly brace of the list. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 858 | const AsmToken &RCurlyTok = Parser.getTok(); |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 859 | if (RCurlyTok.isNot(AsmToken::RCurly)) { |
| 860 | Error(RCurlyTok.getLoc(), "'}' expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 861 | return true; |
Chris Lattner | c0ddfaa | 2010-10-28 17:23:41 +0000 | [diff] [blame] | 862 | } |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 863 | |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 864 | SMLoc E = RCurlyTok.getLoc(); |
| 865 | Parser.Lex(); // Eat right curly brace token. |
Jim Grosbach | 03f44a0 | 2010-11-29 23:18:01 +0000 | [diff] [blame] | 866 | |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 867 | // Verify the register list. |
Bill Wendling | 5fa22a1 | 2010-11-09 23:28:44 +0000 | [diff] [blame] | 868 | SmallVectorImpl<std::pair<unsigned, SMLoc> >::const_iterator |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 869 | RI = Registers.begin(), RE = Registers.end(); |
| 870 | |
Bill Wendling | 7caebff | 2011-01-12 21:20:59 +0000 | [diff] [blame] | 871 | unsigned HighRegNum = getARMRegisterNumbering(RI->first); |
Bill Wendling | 8e8b18b | 2010-11-09 23:45:59 +0000 | [diff] [blame] | 872 | bool EmittedWarning = false; |
| 873 | |
Bill Wendling | 7caebff | 2011-01-12 21:20:59 +0000 | [diff] [blame] | 874 | DenseMap<unsigned, bool> RegMap; |
| 875 | RegMap[HighRegNum] = true; |
| 876 | |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 877 | for (++RI; RI != RE; ++RI) { |
Bill Wendling | 7729e06 | 2010-11-09 22:44:22 +0000 | [diff] [blame] | 878 | const std::pair<unsigned, SMLoc> &RegInfo = *RI; |
Bill Wendling | 7caebff | 2011-01-12 21:20:59 +0000 | [diff] [blame] | 879 | unsigned Reg = getARMRegisterNumbering(RegInfo.first); |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 880 | |
Bill Wendling | 8e8b18b | 2010-11-09 23:45:59 +0000 | [diff] [blame] | 881 | if (RegMap[Reg]) { |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 882 | Error(RegInfo.second, "register duplicated in register list"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 883 | return true; |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 884 | } |
| 885 | |
Bill Wendling | 8e8b18b | 2010-11-09 23:45:59 +0000 | [diff] [blame] | 886 | if (!EmittedWarning && Reg < HighRegNum) |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 887 | Warning(RegInfo.second, |
| 888 | "register not in ascending order in register list"); |
| 889 | |
Bill Wendling | 8e8b18b | 2010-11-09 23:45:59 +0000 | [diff] [blame] | 890 | RegMap[Reg] = true; |
| 891 | HighRegNum = std::max(Reg, HighRegNum); |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 894 | Operands.push_back(ARMOperand::CreateRegList(Registers, S, E)); |
| 895 | return false; |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 898 | /// tryParseMemBarrierOptOperand - Try to parse DSB/DMB data barrier options. |
| 899 | ARMAsmParser::OperandMatchResultTy ARMAsmParser:: |
| 900 | tryParseMemBarrierOptOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 901 | SMLoc S = Parser.getTok().getLoc(); |
| 902 | const AsmToken &Tok = Parser.getTok(); |
| 903 | assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier"); |
| 904 | StringRef OptStr = Tok.getString(); |
| 905 | |
| 906 | unsigned Opt = StringSwitch<unsigned>(OptStr.slice(0, OptStr.size())) |
| 907 | .Case("sy", ARM_MB::SY) |
| 908 | .Case("st", ARM_MB::ST) |
| 909 | .Case("ish", ARM_MB::ISH) |
| 910 | .Case("ishst", ARM_MB::ISHST) |
| 911 | .Case("nsh", ARM_MB::NSH) |
| 912 | .Case("nshst", ARM_MB::NSHST) |
| 913 | .Case("osh", ARM_MB::OSH) |
| 914 | .Case("oshst", ARM_MB::OSHST) |
| 915 | .Default(~0U); |
| 916 | |
| 917 | if (Opt == ~0U) |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 918 | return MatchOperand_NoMatch; |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 919 | |
| 920 | Parser.Lex(); // Eat identifier token. |
| 921 | Operands.push_back(ARMOperand::CreateMemBarrierOpt((ARM_MB::MemBOpt)Opt, S)); |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 922 | return MatchOperand_Success; |
Bruno Cardoso Lopes | 706d946 | 2011-02-07 22:09:15 +0000 | [diff] [blame] | 923 | } |
| 924 | |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 925 | /// ParseProcIFlagsOperand - Try to parse iflags from CPS instruction. |
| 926 | ARMAsmParser::OperandMatchResultTy ARMAsmParser:: |
| 927 | tryParseProcIFlagsOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
| 928 | SMLoc S = Parser.getTok().getLoc(); |
| 929 | const AsmToken &Tok = Parser.getTok(); |
| 930 | assert(Tok.is(AsmToken::Identifier) && "Token is not an Identifier"); |
| 931 | StringRef IFlagsStr = Tok.getString(); |
| 932 | |
| 933 | unsigned IFlags = 0; |
| 934 | for (int i = 0, e = IFlagsStr.size(); i != e; ++i) { |
| 935 | unsigned Flag = StringSwitch<unsigned>(IFlagsStr.substr(i, 1)) |
| 936 | .Case("a", ARM_PROC::A) |
| 937 | .Case("i", ARM_PROC::I) |
| 938 | .Case("f", ARM_PROC::F) |
| 939 | .Default(~0U); |
| 940 | |
| 941 | // If some specific iflag is already set, it means that some letter is |
| 942 | // present more than once, this is not acceptable. |
| 943 | if (Flag == ~0U || (IFlags & Flag)) |
| 944 | return MatchOperand_NoMatch; |
| 945 | |
| 946 | IFlags |= Flag; |
| 947 | } |
| 948 | |
| 949 | Parser.Lex(); // Eat identifier token. |
| 950 | Operands.push_back(ARMOperand::CreateProcIFlags((ARM_PROC::IFlags)IFlags, S)); |
| 951 | return MatchOperand_Success; |
| 952 | } |
| 953 | |
Bill Wendling | e717610 | 2010-11-06 22:36:58 +0000 | [diff] [blame] | 954 | /// Parse an ARM memory expression, return false if successful else return true |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 955 | /// or an error. The first token must be a '[' when called. |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 956 | /// |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 957 | /// TODO Only preindexing and postindexing addressing are started, unindexed |
| 958 | /// with option, etc are still to do. |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 959 | bool ARMAsmParser:: |
| 960 | ParseMemory(SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 961 | SMLoc S, E; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 962 | assert(Parser.getTok().is(AsmToken::LBrac) && |
Bill Wendling | a60f157 | 2010-11-06 10:48:18 +0000 | [diff] [blame] | 963 | "Token is not a Left Bracket"); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 964 | S = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 965 | Parser.Lex(); // Eat left bracket token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 966 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 967 | const AsmToken &BaseRegTok = Parser.getTok(); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 968 | if (BaseRegTok.isNot(AsmToken::Identifier)) { |
| 969 | Error(BaseRegTok.getLoc(), "register expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 970 | return true; |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 971 | } |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 972 | int BaseRegNum = TryParseRegister(); |
| 973 | if (BaseRegNum == -1) { |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 974 | Error(BaseRegTok.getLoc(), "register expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 975 | return true; |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 976 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 977 | |
Daniel Dunbar | 0571093 | 2011-01-18 05:34:17 +0000 | [diff] [blame] | 978 | // The next token must either be a comma or a closing bracket. |
| 979 | const AsmToken &Tok = Parser.getTok(); |
| 980 | if (!Tok.is(AsmToken::Comma) && !Tok.is(AsmToken::RBrac)) |
| 981 | return true; |
| 982 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 983 | bool Preindexed = false; |
| 984 | bool Postindexed = false; |
| 985 | bool OffsetIsReg = false; |
| 986 | bool Negative = false; |
| 987 | bool Writeback = false; |
Daniel Dunbar | 05d8b71 | 2011-01-18 05:34:24 +0000 | [diff] [blame] | 988 | ARMOperand *WBOp = 0; |
| 989 | int OffsetRegNum = -1; |
| 990 | bool OffsetRegShifted = false; |
| 991 | enum ShiftType ShiftType = Lsl; |
| 992 | const MCExpr *ShiftAmount = 0; |
| 993 | const MCExpr *Offset = 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 994 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 995 | // First look for preindexed address forms, that is after the "[Rn" we now |
| 996 | // have to see if the next token is a comma. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 997 | if (Tok.is(AsmToken::Comma)) { |
| 998 | Preindexed = true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 999 | Parser.Lex(); // Eat comma token. |
Daniel Dunbar | 05d8b71 | 2011-01-18 05:34:24 +0000 | [diff] [blame] | 1000 | |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1001 | if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, ShiftAmount, |
| 1002 | Offset, OffsetIsReg, OffsetRegNum, E)) |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1003 | return true; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1004 | const AsmToken &RBracTok = Parser.getTok(); |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1005 | if (RBracTok.isNot(AsmToken::RBrac)) { |
| 1006 | Error(RBracTok.getLoc(), "']' expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1007 | return true; |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1008 | } |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1009 | E = RBracTok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1010 | Parser.Lex(); // Eat right bracket token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1011 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1012 | const AsmToken &ExclaimTok = Parser.getTok(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1013 | if (ExclaimTok.is(AsmToken::Exclaim)) { |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1014 | WBOp = ARMOperand::CreateToken(ExclaimTok.getString(), |
| 1015 | ExclaimTok.getLoc()); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1016 | Writeback = true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1017 | Parser.Lex(); // Eat exclaim token |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1018 | } |
Daniel Dunbar | 0571093 | 2011-01-18 05:34:17 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | // The "[Rn" we have so far was not followed by a comma. |
| 1021 | |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 1022 | // If there's anything other than the right brace, this is a post indexing |
| 1023 | // addressing form. |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1024 | E = Tok.getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1025 | Parser.Lex(); // Eat right bracket token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1026 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1027 | const AsmToken &NextTok = Parser.getTok(); |
Jim Grosbach | 03f44a0 | 2010-11-29 23:18:01 +0000 | [diff] [blame] | 1028 | |
Kevin Enderby | e2a98dd | 2009-10-15 21:42:45 +0000 | [diff] [blame] | 1029 | if (NextTok.isNot(AsmToken::EndOfStatement)) { |
Jim Grosbach | 80eb233 | 2010-10-29 17:41:25 +0000 | [diff] [blame] | 1030 | Postindexed = true; |
| 1031 | Writeback = true; |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1032 | |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1033 | if (NextTok.isNot(AsmToken::Comma)) { |
| 1034 | Error(NextTok.getLoc(), "',' expected"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1035 | return true; |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1036 | } |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1037 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1038 | Parser.Lex(); // Eat comma token. |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1040 | if (ParseMemoryOffsetReg(Negative, OffsetRegShifted, ShiftType, |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1041 | ShiftAmount, Offset, OffsetIsReg, OffsetRegNum, |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1042 | E)) |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1043 | return true; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1044 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1045 | } |
Daniel Dunbar | 05d8b71 | 2011-01-18 05:34:24 +0000 | [diff] [blame] | 1046 | |
| 1047 | // Force Offset to exist if used. |
| 1048 | if (!OffsetIsReg) { |
| 1049 | if (!Offset) |
| 1050 | Offset = MCConstantExpr::Create(0, getContext()); |
| 1051 | } |
| 1052 | |
| 1053 | Operands.push_back(ARMOperand::CreateMem(BaseRegNum, OffsetIsReg, Offset, |
| 1054 | OffsetRegNum, OffsetRegShifted, |
| 1055 | ShiftType, ShiftAmount, Preindexed, |
| 1056 | Postindexed, Negative, Writeback, |
| 1057 | S, E)); |
| 1058 | if (WBOp) |
| 1059 | Operands.push_back(WBOp); |
| 1060 | |
| 1061 | return false; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1062 | } |
| 1063 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1064 | /// Parse the offset of a memory operand after we have seen "[Rn," or "[Rn]," |
| 1065 | /// we will parse the following (were +/- means that a plus or minus is |
| 1066 | /// optional): |
| 1067 | /// +/-Rm |
| 1068 | /// +/-Rm, shift |
| 1069 | /// #offset |
| 1070 | /// we return false on success or an error otherwise. |
| 1071 | bool ARMAsmParser::ParseMemoryOffsetReg(bool &Negative, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1072 | bool &OffsetRegShifted, |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1073 | enum ShiftType &ShiftType, |
| 1074 | const MCExpr *&ShiftAmount, |
| 1075 | const MCExpr *&Offset, |
| 1076 | bool &OffsetIsReg, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1077 | int &OffsetRegNum, |
| 1078 | SMLoc &E) { |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1079 | Negative = false; |
| 1080 | OffsetRegShifted = false; |
| 1081 | OffsetIsReg = false; |
| 1082 | OffsetRegNum = -1; |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1083 | const AsmToken &NextTok = Parser.getTok(); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1084 | E = NextTok.getLoc(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1085 | if (NextTok.is(AsmToken::Plus)) |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1086 | Parser.Lex(); // Eat plus token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1087 | else if (NextTok.is(AsmToken::Minus)) { |
| 1088 | Negative = true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1089 | Parser.Lex(); // Eat minus token |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1090 | } |
| 1091 | // See if there is a register following the "[Rn," or "[Rn]," we have so far. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1092 | const AsmToken &OffsetRegTok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1093 | if (OffsetRegTok.is(AsmToken::Identifier)) { |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 1094 | SMLoc CurLoc = OffsetRegTok.getLoc(); |
| 1095 | OffsetRegNum = TryParseRegister(); |
| 1096 | if (OffsetRegNum != -1) { |
Chris Lattner | 550276e | 2010-10-28 20:52:15 +0000 | [diff] [blame] | 1097 | OffsetIsReg = true; |
Chris Lattner | e5658fa | 2010-10-30 04:09:10 +0000 | [diff] [blame] | 1098 | E = CurLoc; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1099 | } |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1100 | } |
Jim Grosbach | d4462a5 | 2010-11-01 16:44:21 +0000 | [diff] [blame] | 1101 | |
Bill Wendling | 12f40e9 | 2010-11-06 10:51:53 +0000 | [diff] [blame] | 1102 | // If we parsed a register as the offset then there can be a shift after that. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1103 | if (OffsetRegNum != -1) { |
| 1104 | // Look for a comma then a shift |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1105 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1106 | if (Tok.is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1107 | Parser.Lex(); // Eat comma token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1108 | |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1109 | const AsmToken &Tok = Parser.getTok(); |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1110 | if (ParseShift(ShiftType, ShiftAmount, E)) |
Duncan Sands | 3472766 | 2010-07-12 08:16:59 +0000 | [diff] [blame] | 1111 | return Error(Tok.getLoc(), "shift expected"); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1112 | OffsetRegShifted = true; |
| 1113 | } |
| 1114 | } |
| 1115 | else { // the "[Rn," or "[Rn,]" we have so far was not followed by "Rm" |
| 1116 | // Look for #offset following the "[Rn," or "[Rn]," |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1117 | const AsmToken &HashTok = Parser.getTok(); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1118 | if (HashTok.isNot(AsmToken::Hash)) |
| 1119 | return Error(HashTok.getLoc(), "'#' expected"); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1120 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1121 | Parser.Lex(); // Eat hash token. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1122 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1123 | if (getParser().ParseExpression(Offset)) |
| 1124 | return true; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1125 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1126 | } |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1127 | return false; |
| 1128 | } |
| 1129 | |
| 1130 | /// ParseShift as one of these two: |
| 1131 | /// ( lsl | lsr | asr | ror ) , # shift_amount |
| 1132 | /// rrx |
| 1133 | /// and returns true if it parses a shift otherwise it returns false. |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1134 | bool ARMAsmParser::ParseShift(ShiftType &St, const MCExpr *&ShiftAmount, |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1135 | SMLoc &E) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1136 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1137 | if (Tok.isNot(AsmToken::Identifier)) |
| 1138 | return true; |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 1139 | StringRef ShiftName = Tok.getString(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1140 | if (ShiftName == "lsl" || ShiftName == "LSL") |
| 1141 | St = Lsl; |
| 1142 | else if (ShiftName == "lsr" || ShiftName == "LSR") |
| 1143 | St = Lsr; |
| 1144 | else if (ShiftName == "asr" || ShiftName == "ASR") |
| 1145 | St = Asr; |
| 1146 | else if (ShiftName == "ror" || ShiftName == "ROR") |
| 1147 | St = Ror; |
| 1148 | else if (ShiftName == "rrx" || ShiftName == "RRX") |
| 1149 | St = Rrx; |
| 1150 | else |
| 1151 | return true; |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1152 | Parser.Lex(); // Eat shift type token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1153 | |
| 1154 | // Rrx stands alone. |
| 1155 | if (St == Rrx) |
| 1156 | return false; |
| 1157 | |
| 1158 | // Otherwise, there must be a '#' and a shift amount. |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1159 | const AsmToken &HashTok = Parser.getTok(); |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1160 | if (HashTok.isNot(AsmToken::Hash)) |
| 1161 | return Error(HashTok.getLoc(), "'#' expected"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1162 | Parser.Lex(); // Eat hash token. |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1163 | |
| 1164 | if (getParser().ParseExpression(ShiftAmount)) |
| 1165 | return true; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1166 | |
| 1167 | return false; |
| 1168 | } |
| 1169 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1170 | /// Parse a arm instruction operand. For now this parses the operand regardless |
| 1171 | /// of the mnemonic. |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 1172 | bool ARMAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 1173 | StringRef Mnemonic) { |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1174 | SMLoc S, E; |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 1175 | |
| 1176 | // Check if the current operand has a custom associated parser, if so, try to |
| 1177 | // custom parse the operand, or fallback to the general approach. |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 1178 | OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic); |
| 1179 | if (ResTy == MatchOperand_Success) |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 1180 | return false; |
Jim Grosbach | f922c47 | 2011-02-12 01:34:40 +0000 | [diff] [blame] | 1181 | // If there wasn't a custom match, try the generic matcher below. Otherwise, |
| 1182 | // there was a match, but an error occurred, in which case, just return that |
| 1183 | // the operand parsing failed. |
| 1184 | if (ResTy == MatchOperand_ParseFail) |
| 1185 | return true; |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 1186 | |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1187 | switch (getLexer().getKind()) { |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 1188 | default: |
| 1189 | Error(Parser.getTok().getLoc(), "unexpected token in operand"); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1190 | return true; |
Kevin Enderby | 67b212e | 2011-01-13 20:32:36 +0000 | [diff] [blame] | 1191 | case AsmToken::Identifier: |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1192 | if (!TryParseRegisterWithWriteBack(Operands)) |
| 1193 | return false; |
Owen Anderson | e4e5e2a | 2011-01-13 21:46:02 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Fall though for the Identifier case that is not a register or a |
| 1196 | // special name. |
Kevin Enderby | 67b212e | 2011-01-13 20:32:36 +0000 | [diff] [blame] | 1197 | case AsmToken::Integer: // things like 1f and 2b as a branch targets |
| 1198 | case AsmToken::Dot: { // . as a branch target |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1199 | // This was not a register so parse other operands that start with an |
| 1200 | // identifier (like labels) as expressions and create them as immediates. |
| 1201 | const MCExpr *IdVal; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1202 | S = Parser.getTok().getLoc(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1203 | if (getParser().ParseExpression(IdVal)) |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1204 | return true; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1205 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1206 | Operands.push_back(ARMOperand::CreateImm(IdVal, S, E)); |
| 1207 | return false; |
| 1208 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1209 | case AsmToken::LBrac: |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1210 | return ParseMemory(Operands); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 1211 | case AsmToken::LCurly: |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1212 | return ParseRegisterList(Operands); |
Kevin Enderby | d7894f1 | 2009-10-09 21:12:28 +0000 | [diff] [blame] | 1213 | case AsmToken::Hash: |
Kevin Enderby | 079469f | 2009-10-13 23:33:38 +0000 | [diff] [blame] | 1214 | // #42 -> immediate. |
| 1215 | // TODO: ":lower16:" and ":upper16:" modifiers after # before immediate |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1216 | S = Parser.getTok().getLoc(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1217 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1218 | const MCExpr *ImmVal; |
| 1219 | if (getParser().ParseExpression(ImmVal)) |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1220 | return true; |
Sean Callanan | 7626476 | 2010-04-02 22:27:05 +0000 | [diff] [blame] | 1221 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Bill Wendling | 50d0f58 | 2010-11-18 23:43:05 +0000 | [diff] [blame] | 1222 | Operands.push_back(ARMOperand::CreateImm(ImmVal, S, E)); |
| 1223 | return false; |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1224 | case AsmToken::Colon: { |
| 1225 | // ":lower16:" and ":upper16:" expression prefixes |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1226 | // FIXME: Check it's an expression prefix, |
| 1227 | // e.g. (FOO - :lower16:BAR) isn't legal. |
| 1228 | ARMMCExpr::VariantKind RefKind; |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1229 | if (ParsePrefix(RefKind)) |
| 1230 | return true; |
| 1231 | |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1232 | const MCExpr *SubExprVal; |
| 1233 | if (getParser().ParseExpression(SubExprVal)) |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1234 | return true; |
| 1235 | |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1236 | const MCExpr *ExprVal = ARMMCExpr::Create(RefKind, SubExprVal, |
| 1237 | getContext()); |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1238 | E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1239 | Operands.push_back(ARMOperand::CreateImm(ExprVal, S, E)); |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1240 | return false; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1241 | } |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1242 | } |
| 1243 | } |
| 1244 | |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1245 | // ParsePrefix - Parse ARM 16-bit relocations expression prefix, i.e. |
| 1246 | // :lower16: and :upper16:. |
| 1247 | bool ARMAsmParser::ParsePrefix(ARMMCExpr::VariantKind &RefKind) { |
| 1248 | RefKind = ARMMCExpr::VK_ARM_None; |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1249 | |
| 1250 | // :lower16: and :upper16: modifiers |
Jason W Kim | 8a8696d | 2011-01-13 00:27:00 +0000 | [diff] [blame] | 1251 | assert(getLexer().is(AsmToken::Colon) && "expected a :"); |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1252 | Parser.Lex(); // Eat ':' |
| 1253 | |
| 1254 | if (getLexer().isNot(AsmToken::Identifier)) { |
| 1255 | Error(Parser.getTok().getLoc(), "expected prefix identifier in operand"); |
| 1256 | return true; |
| 1257 | } |
| 1258 | |
| 1259 | StringRef IDVal = Parser.getTok().getIdentifier(); |
| 1260 | if (IDVal == "lower16") { |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1261 | RefKind = ARMMCExpr::VK_ARM_LO16; |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1262 | } else if (IDVal == "upper16") { |
Evan Cheng | 7597212 | 2011-01-13 07:58:56 +0000 | [diff] [blame] | 1263 | RefKind = ARMMCExpr::VK_ARM_HI16; |
Jason W Kim | 9081b4b | 2011-01-11 23:53:41 +0000 | [diff] [blame] | 1264 | } else { |
| 1265 | Error(Parser.getTok().getLoc(), "unexpected prefix in operand"); |
| 1266 | return true; |
| 1267 | } |
| 1268 | Parser.Lex(); |
| 1269 | |
| 1270 | if (getLexer().isNot(AsmToken::Colon)) { |
| 1271 | Error(Parser.getTok().getLoc(), "unexpected token after prefix"); |
| 1272 | return true; |
| 1273 | } |
| 1274 | Parser.Lex(); // Eat the last ':' |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
| 1278 | const MCExpr * |
| 1279 | ARMAsmParser::ApplyPrefixToExpr(const MCExpr *E, |
| 1280 | MCSymbolRefExpr::VariantKind Variant) { |
| 1281 | // Recurse over the given expression, rebuilding it to apply the given variant |
| 1282 | // to the leftmost symbol. |
| 1283 | if (Variant == MCSymbolRefExpr::VK_None) |
| 1284 | return E; |
| 1285 | |
| 1286 | switch (E->getKind()) { |
| 1287 | case MCExpr::Target: |
| 1288 | llvm_unreachable("Can't handle target expr yet"); |
| 1289 | case MCExpr::Constant: |
| 1290 | llvm_unreachable("Can't handle lower16/upper16 of constant yet"); |
| 1291 | |
| 1292 | case MCExpr::SymbolRef: { |
| 1293 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); |
| 1294 | |
| 1295 | if (SRE->getKind() != MCSymbolRefExpr::VK_None) |
| 1296 | return 0; |
| 1297 | |
| 1298 | return MCSymbolRefExpr::Create(&SRE->getSymbol(), Variant, getContext()); |
| 1299 | } |
| 1300 | |
| 1301 | case MCExpr::Unary: |
| 1302 | llvm_unreachable("Can't handle unary expressions yet"); |
| 1303 | |
| 1304 | case MCExpr::Binary: { |
| 1305 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); |
| 1306 | const MCExpr *LHS = ApplyPrefixToExpr(BE->getLHS(), Variant); |
| 1307 | const MCExpr *RHS = BE->getRHS(); |
| 1308 | if (!LHS) |
| 1309 | return 0; |
| 1310 | |
| 1311 | return MCBinaryExpr::Create(BE->getOpcode(), LHS, RHS, getContext()); |
| 1312 | } |
| 1313 | } |
| 1314 | |
| 1315 | assert(0 && "Invalid expression kind!"); |
| 1316 | return 0; |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1319 | /// \brief Given a mnemonic, split out possible predication code and carry |
| 1320 | /// setting letters to form a canonical mnemonic and flags. |
| 1321 | // |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1322 | // FIXME: Would be nice to autogen this. |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1323 | static StringRef SplitMnemonic(StringRef Mnemonic, |
| 1324 | unsigned &PredicationCode, |
| 1325 | bool &CarrySetting, |
| 1326 | unsigned &ProcessorIMod) { |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1327 | PredicationCode = ARMCC::AL; |
| 1328 | CarrySetting = false; |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1329 | ProcessorIMod = 0; |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1330 | |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1331 | // Ignore some mnemonics we know aren't predicated forms. |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1332 | // |
| 1333 | // FIXME: Would be nice to autogen this. |
Daniel Dunbar | 8ab1112 | 2011-01-10 21:01:03 +0000 | [diff] [blame] | 1334 | if (Mnemonic == "teq" || Mnemonic == "vceq" || |
| 1335 | Mnemonic == "movs" || |
| 1336 | Mnemonic == "svc" || |
| 1337 | (Mnemonic == "mls" || Mnemonic == "smmls" || Mnemonic == "vcls" || |
| 1338 | Mnemonic == "vmls" || Mnemonic == "vnmls") || |
| 1339 | Mnemonic == "vacge" || Mnemonic == "vcge" || |
| 1340 | Mnemonic == "vclt" || |
| 1341 | Mnemonic == "vacgt" || Mnemonic == "vcgt" || |
| 1342 | Mnemonic == "vcle" || |
| 1343 | (Mnemonic == "smlal" || Mnemonic == "umaal" || Mnemonic == "umlal" || |
| 1344 | Mnemonic == "vabal" || Mnemonic == "vmlal" || Mnemonic == "vpadal" || |
| 1345 | Mnemonic == "vqdmlal")) |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1346 | return Mnemonic; |
Daniel Dunbar | 5747b13 | 2010-08-11 06:37:16 +0000 | [diff] [blame] | 1347 | |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1348 | // First, split out any predication code. |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1349 | unsigned CC = StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2)) |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 1350 | .Case("eq", ARMCC::EQ) |
| 1351 | .Case("ne", ARMCC::NE) |
| 1352 | .Case("hs", ARMCC::HS) |
| 1353 | .Case("lo", ARMCC::LO) |
| 1354 | .Case("mi", ARMCC::MI) |
| 1355 | .Case("pl", ARMCC::PL) |
| 1356 | .Case("vs", ARMCC::VS) |
| 1357 | .Case("vc", ARMCC::VC) |
| 1358 | .Case("hi", ARMCC::HI) |
| 1359 | .Case("ls", ARMCC::LS) |
| 1360 | .Case("ge", ARMCC::GE) |
| 1361 | .Case("lt", ARMCC::LT) |
| 1362 | .Case("gt", ARMCC::GT) |
| 1363 | .Case("le", ARMCC::LE) |
| 1364 | .Case("al", ARMCC::AL) |
| 1365 | .Default(~0U); |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1366 | if (CC != ~0U) { |
| 1367 | Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 2); |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1368 | PredicationCode = CC; |
Bill Wendling | 52925b6 | 2010-10-29 23:50:21 +0000 | [diff] [blame] | 1369 | } |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 1370 | |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1371 | // Next, determine if we have a carry setting bit. We explicitly ignore all |
| 1372 | // the instructions we know end in 's'. |
| 1373 | if (Mnemonic.endswith("s") && |
| 1374 | !(Mnemonic == "asrs" || Mnemonic == "cps" || Mnemonic == "mls" || |
| 1375 | Mnemonic == "movs" || Mnemonic == "mrs" || Mnemonic == "smmls" || |
| 1376 | Mnemonic == "vabs" || Mnemonic == "vcls" || Mnemonic == "vmls" || |
| 1377 | Mnemonic == "vmrs" || Mnemonic == "vnmls" || Mnemonic == "vqabs" || |
| 1378 | Mnemonic == "vrecps" || Mnemonic == "vrsqrts")) { |
| 1379 | Mnemonic = Mnemonic.slice(0, Mnemonic.size() - 1); |
| 1380 | CarrySetting = true; |
| 1381 | } |
| 1382 | |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1383 | // The "cps" instruction can have a interrupt mode operand which is glued into |
| 1384 | // the mnemonic. Check if this is the case, split it and parse the imod op |
| 1385 | if (Mnemonic.startswith("cps")) { |
| 1386 | // Split out any imod code. |
| 1387 | unsigned IMod = |
| 1388 | StringSwitch<unsigned>(Mnemonic.substr(Mnemonic.size()-2, 2)) |
| 1389 | .Case("ie", ARM_PROC::IE) |
| 1390 | .Case("id", ARM_PROC::ID) |
| 1391 | .Default(~0U); |
| 1392 | if (IMod != ~0U) { |
| 1393 | Mnemonic = Mnemonic.slice(0, Mnemonic.size()-2); |
| 1394 | ProcessorIMod = IMod; |
| 1395 | } |
| 1396 | } |
| 1397 | |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1398 | return Mnemonic; |
| 1399 | } |
Daniel Dunbar | 3771dd0 | 2011-01-11 15:59:53 +0000 | [diff] [blame] | 1400 | |
| 1401 | /// \brief Given a canonical mnemonic, determine if the instruction ever allows |
| 1402 | /// inclusion of carry set or predication code operands. |
| 1403 | // |
| 1404 | // FIXME: It would be nice to autogen this. |
Bruno Cardoso Lopes | fdcee77 | 2011-01-18 20:55:11 +0000 | [diff] [blame] | 1405 | void ARMAsmParser:: |
| 1406 | GetMnemonicAcceptInfo(StringRef Mnemonic, bool &CanAcceptCarrySet, |
| 1407 | bool &CanAcceptPredicationCode) { |
| 1408 | bool isThumb = TM.getSubtarget<ARMSubtarget>().isThumb(); |
| 1409 | |
Daniel Dunbar | eb9f3f9 | 2011-01-11 19:06:29 +0000 | [diff] [blame] | 1410 | if (Mnemonic == "and" || Mnemonic == "lsl" || Mnemonic == "lsr" || |
| 1411 | Mnemonic == "rrx" || Mnemonic == "ror" || Mnemonic == "sub" || |
| 1412 | Mnemonic == "smull" || Mnemonic == "add" || Mnemonic == "adc" || |
| 1413 | Mnemonic == "mul" || Mnemonic == "bic" || Mnemonic == "asr" || |
| 1414 | Mnemonic == "umlal" || Mnemonic == "orr" || Mnemonic == "mov" || |
| 1415 | Mnemonic == "rsb" || Mnemonic == "rsc" || Mnemonic == "orn" || |
| 1416 | Mnemonic == "sbc" || Mnemonic == "mla" || Mnemonic == "umull" || |
| 1417 | Mnemonic == "eor" || Mnemonic == "smlal" || Mnemonic == "mvn") { |
| 1418 | CanAcceptCarrySet = true; |
| 1419 | } else { |
| 1420 | CanAcceptCarrySet = false; |
| 1421 | } |
Daniel Dunbar | 3771dd0 | 2011-01-11 15:59:53 +0000 | [diff] [blame] | 1422 | |
Daniel Dunbar | eb9f3f9 | 2011-01-11 19:06:29 +0000 | [diff] [blame] | 1423 | if (Mnemonic == "cbnz" || Mnemonic == "setend" || Mnemonic == "dmb" || |
| 1424 | Mnemonic == "cps" || Mnemonic == "mcr2" || Mnemonic == "it" || |
| 1425 | Mnemonic == "mcrr2" || Mnemonic == "cbz" || Mnemonic == "cdp2" || |
| 1426 | Mnemonic == "trap" || Mnemonic == "mrc2" || Mnemonic == "mrrc2" || |
Bruno Cardoso Lopes | e47f375 | 2011-01-20 19:18:32 +0000 | [diff] [blame] | 1427 | Mnemonic == "dsb" || Mnemonic == "movs" || Mnemonic == "isb" || |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1428 | Mnemonic == "clrex" || Mnemonic.startswith("cps")) { |
Daniel Dunbar | 3771dd0 | 2011-01-11 15:59:53 +0000 | [diff] [blame] | 1429 | CanAcceptPredicationCode = false; |
| 1430 | } else { |
| 1431 | CanAcceptPredicationCode = true; |
| 1432 | } |
Bruno Cardoso Lopes | fa5bd27 | 2011-01-20 16:35:57 +0000 | [diff] [blame] | 1433 | |
| 1434 | if (isThumb) |
| 1435 | if (Mnemonic == "bkpt" || Mnemonic == "mcr" || Mnemonic == "mcrr" || |
Bruno Cardoso Lopes | 8dd37f7 | 2011-01-20 18:32:09 +0000 | [diff] [blame] | 1436 | Mnemonic == "mrc" || Mnemonic == "mrrc" || Mnemonic == "cdp") |
Bruno Cardoso Lopes | fa5bd27 | 2011-01-20 16:35:57 +0000 | [diff] [blame] | 1437 | CanAcceptPredicationCode = false; |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /// Parse an arm instruction mnemonic followed by its operands. |
| 1441 | bool ARMAsmParser::ParseInstruction(StringRef Name, SMLoc NameLoc, |
| 1442 | SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
| 1443 | // Create the leading tokens for the mnemonic, split by '.' characters. |
| 1444 | size_t Start = 0, Next = Name.find('.'); |
| 1445 | StringRef Head = Name.slice(Start, Next); |
| 1446 | |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1447 | // Split out the predication code and carry setting flag from the mnemonic. |
| 1448 | unsigned PredicationCode; |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1449 | unsigned ProcessorIMod; |
Daniel Dunbar | 352e148 | 2011-01-11 15:59:50 +0000 | [diff] [blame] | 1450 | bool CarrySetting; |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1451 | Head = SplitMnemonic(Head, PredicationCode, CarrySetting, |
| 1452 | ProcessorIMod); |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1453 | |
Chris Lattner | 3a69756 | 2010-10-28 17:20:03 +0000 | [diff] [blame] | 1454 | Operands.push_back(ARMOperand::CreateToken(Head, NameLoc)); |
Bill Wendling | 9717fa9 | 2010-11-21 10:56:05 +0000 | [diff] [blame] | 1455 | |
Daniel Dunbar | 3771dd0 | 2011-01-11 15:59:53 +0000 | [diff] [blame] | 1456 | // Next, add the CCOut and ConditionCode operands, if needed. |
| 1457 | // |
| 1458 | // For mnemonics which can ever incorporate a carry setting bit or predication |
| 1459 | // code, our matching model involves us always generating CCOut and |
| 1460 | // ConditionCode operands to match the mnemonic "as written" and then we let |
| 1461 | // the matcher deal with finding the right instruction or generating an |
| 1462 | // appropriate error. |
| 1463 | bool CanAcceptCarrySet, CanAcceptPredicationCode; |
| 1464 | GetMnemonicAcceptInfo(Head, CanAcceptCarrySet, CanAcceptPredicationCode); |
| 1465 | |
| 1466 | // Add the carry setting operand, if necessary. |
| 1467 | // |
| 1468 | // FIXME: It would be awesome if we could somehow invent a location such that |
| 1469 | // match errors on this operand would print a nice diagnostic about how the |
| 1470 | // 's' character in the mnemonic resulted in a CCOut operand. |
| 1471 | if (CanAcceptCarrySet) { |
| 1472 | Operands.push_back(ARMOperand::CreateCCOut(CarrySetting ? ARM::CPSR : 0, |
| 1473 | NameLoc)); |
| 1474 | } else { |
| 1475 | // This mnemonic can't ever accept a carry set, but the user wrote one (or |
| 1476 | // misspelled another mnemonic). |
| 1477 | |
| 1478 | // FIXME: Issue a nice error. |
| 1479 | } |
| 1480 | |
| 1481 | // Add the predication code operand, if necessary. |
| 1482 | if (CanAcceptPredicationCode) { |
| 1483 | Operands.push_back(ARMOperand::CreateCondCode( |
| 1484 | ARMCC::CondCodes(PredicationCode), NameLoc)); |
| 1485 | } else { |
| 1486 | // This mnemonic can't ever accept a predication code, but the user wrote |
| 1487 | // one (or misspelled another mnemonic). |
| 1488 | |
| 1489 | // FIXME: Issue a nice error. |
Daniel Dunbar | badbd2f | 2011-01-10 12:24:52 +0000 | [diff] [blame] | 1490 | } |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 1491 | |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1492 | // Add the processor imod operand, if necessary. |
| 1493 | if (ProcessorIMod) { |
| 1494 | Operands.push_back(ARMOperand::CreateImm( |
| 1495 | MCConstantExpr::Create(ProcessorIMod, getContext()), |
| 1496 | NameLoc, NameLoc)); |
| 1497 | } else { |
| 1498 | // This mnemonic can't ever accept a imod, but the user wrote |
| 1499 | // one (or misspelled another mnemonic). |
| 1500 | |
| 1501 | // FIXME: Issue a nice error. |
| 1502 | } |
| 1503 | |
Daniel Dunbar | 345a9a6 | 2010-08-11 06:37:20 +0000 | [diff] [blame] | 1504 | // Add the remaining tokens in the mnemonic. |
Daniel Dunbar | 5747b13 | 2010-08-11 06:37:16 +0000 | [diff] [blame] | 1505 | while (Next != StringRef::npos) { |
| 1506 | Start = Next; |
| 1507 | Next = Name.find('.', Start + 1); |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1508 | StringRef ExtraToken = Name.slice(Start, Next); |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1509 | |
Bruno Cardoso Lopes | a2b6e41 | 2011-02-14 13:09:44 +0000 | [diff] [blame^] | 1510 | Operands.push_back(ARMOperand::CreateToken(ExtraToken, NameLoc)); |
Daniel Dunbar | 5747b13 | 2010-08-11 06:37:16 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | // Read the remaining operands. |
| 1514 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1515 | // Read the first operand. |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 1516 | if (ParseOperand(Operands, Head)) { |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1517 | Parser.EatToEndOfStatement(); |
| 1518 | return true; |
| 1519 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1520 | |
| 1521 | while (getLexer().is(AsmToken::Comma)) { |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1522 | Parser.Lex(); // Eat the comma. |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1523 | |
| 1524 | // Parse and remember the operand. |
Bruno Cardoso Lopes | fafde7f | 2011-02-07 21:41:25 +0000 | [diff] [blame] | 1525 | if (ParseOperand(Operands, Head)) { |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1526 | Parser.EatToEndOfStatement(); |
| 1527 | return true; |
| 1528 | } |
Kevin Enderby | a7ba3a8 | 2009-10-06 22:26:42 +0000 | [diff] [blame] | 1529 | } |
| 1530 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1531 | |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1532 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1533 | Parser.EatToEndOfStatement(); |
Chris Lattner | 34e5314 | 2010-09-08 05:10:46 +0000 | [diff] [blame] | 1534 | return TokError("unexpected token in argument list"); |
Chris Lattner | cbf8a98 | 2010-09-11 16:18:25 +0000 | [diff] [blame] | 1535 | } |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 1536 | |
Chris Lattner | 34e5314 | 2010-09-08 05:10:46 +0000 | [diff] [blame] | 1537 | Parser.Lex(); // Consume the EndOfStatement |
Chris Lattner | 9898671 | 2010-01-14 22:21:20 +0000 | [diff] [blame] | 1538 | return false; |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 1541 | bool ARMAsmParser:: |
| 1542 | MatchAndEmitInstruction(SMLoc IDLoc, |
| 1543 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 1544 | MCStreamer &Out) { |
| 1545 | MCInst Inst; |
| 1546 | unsigned ErrorInfo; |
Kevin Enderby | 193c3ac | 2010-12-09 19:19:43 +0000 | [diff] [blame] | 1547 | MatchResultTy MatchResult, MatchResult2; |
| 1548 | MatchResult = MatchInstructionImpl(Operands, Inst, ErrorInfo); |
| 1549 | if (MatchResult != Match_Success) { |
| 1550 | // If we get a Match_InvalidOperand it might be some arithmetic instruction |
| 1551 | // that does not update the condition codes. So try adding a CCOut operand |
| 1552 | // with a value of reg0. |
| 1553 | if (MatchResult == Match_InvalidOperand) { |
| 1554 | Operands.insert(Operands.begin() + 1, |
| 1555 | ARMOperand::CreateCCOut(0, |
| 1556 | ((ARMOperand*)Operands[0])->getStartLoc())); |
| 1557 | MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo); |
| 1558 | if (MatchResult2 == Match_Success) |
| 1559 | MatchResult = Match_Success; |
Kevin Enderby | 44a9e8f | 2010-12-10 01:41:56 +0000 | [diff] [blame] | 1560 | else { |
| 1561 | ARMOperand *CCOut = ((ARMOperand*)Operands[1]); |
Kevin Enderby | 193c3ac | 2010-12-09 19:19:43 +0000 | [diff] [blame] | 1562 | Operands.erase(Operands.begin() + 1); |
Kevin Enderby | 44a9e8f | 2010-12-10 01:41:56 +0000 | [diff] [blame] | 1563 | delete CCOut; |
| 1564 | } |
Kevin Enderby | 193c3ac | 2010-12-09 19:19:43 +0000 | [diff] [blame] | 1565 | } |
| 1566 | // If we get a Match_MnemonicFail it might be some arithmetic instruction |
| 1567 | // that updates the condition codes if it ends in 's'. So see if the |
| 1568 | // mnemonic ends in 's' and if so try removing the 's' and adding a CCOut |
| 1569 | // operand with a value of CPSR. |
| 1570 | else if(MatchResult == Match_MnemonicFail) { |
| 1571 | // Get the instruction mnemonic, which is the first token. |
| 1572 | StringRef Mnemonic = ((ARMOperand*)Operands[0])->getToken(); |
| 1573 | if (Mnemonic.substr(Mnemonic.size()-1) == "s") { |
| 1574 | // removed the 's' from the mnemonic for matching. |
| 1575 | StringRef MnemonicNoS = Mnemonic.slice(0, Mnemonic.size() - 1); |
| 1576 | SMLoc NameLoc = ((ARMOperand*)Operands[0])->getStartLoc(); |
Kevin Enderby | 44a9e8f | 2010-12-10 01:41:56 +0000 | [diff] [blame] | 1577 | ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]); |
| 1578 | Operands.erase(Operands.begin()); |
| 1579 | delete OldMnemonic; |
| 1580 | Operands.insert(Operands.begin(), |
Kevin Enderby | 193c3ac | 2010-12-09 19:19:43 +0000 | [diff] [blame] | 1581 | ARMOperand::CreateToken(MnemonicNoS, NameLoc)); |
| 1582 | Operands.insert(Operands.begin() + 1, |
| 1583 | ARMOperand::CreateCCOut(ARM::CPSR, NameLoc)); |
| 1584 | MatchResult2 = MatchInstructionImpl(Operands, Inst, ErrorInfo); |
| 1585 | if (MatchResult2 == Match_Success) |
| 1586 | MatchResult = Match_Success; |
| 1587 | else { |
Kevin Enderby | 44a9e8f | 2010-12-10 01:41:56 +0000 | [diff] [blame] | 1588 | ARMOperand *OldMnemonic = ((ARMOperand*)Operands[0]); |
| 1589 | Operands.erase(Operands.begin()); |
| 1590 | delete OldMnemonic; |
| 1591 | Operands.insert(Operands.begin(), |
Kevin Enderby | 193c3ac | 2010-12-09 19:19:43 +0000 | [diff] [blame] | 1592 | ARMOperand::CreateToken(Mnemonic, NameLoc)); |
Kevin Enderby | 44a9e8f | 2010-12-10 01:41:56 +0000 | [diff] [blame] | 1593 | ARMOperand *CCOut = ((ARMOperand*)Operands[1]); |
| 1594 | Operands.erase(Operands.begin() + 1); |
| 1595 | delete CCOut; |
Kevin Enderby | 193c3ac | 2010-12-09 19:19:43 +0000 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | } |
| 1600 | switch (MatchResult) { |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 1601 | case Match_Success: |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 1602 | Out.EmitInstruction(Inst); |
| 1603 | return false; |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 1604 | case Match_MissingFeature: |
| 1605 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 1606 | return true; |
| 1607 | case Match_InvalidOperand: { |
| 1608 | SMLoc ErrorLoc = IDLoc; |
| 1609 | if (ErrorInfo != ~0U) { |
| 1610 | if (ErrorInfo >= Operands.size()) |
| 1611 | return Error(IDLoc, "too few operands for instruction"); |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1612 | |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 1613 | ErrorLoc = ((ARMOperand*)Operands[ErrorInfo])->getStartLoc(); |
| 1614 | if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc; |
| 1615 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1616 | |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 1617 | return Error(ErrorLoc, "invalid operand for instruction"); |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 1618 | } |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 1619 | case Match_MnemonicFail: |
| 1620 | return Error(IDLoc, "unrecognized instruction mnemonic"); |
Daniel Dunbar | b412915 | 2011-02-04 17:12:23 +0000 | [diff] [blame] | 1621 | case Match_ConversionFail: |
| 1622 | return Error(IDLoc, "unable to convert operands to instruction"); |
Chris Lattner | e73d4f8 | 2010-10-28 21:41:58 +0000 | [diff] [blame] | 1623 | } |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1624 | |
Eric Christopher | c223e2b | 2010-10-29 09:26:59 +0000 | [diff] [blame] | 1625 | llvm_unreachable("Implement any new match types added!"); |
Bill Wendling | 146018f | 2010-11-06 21:42:12 +0000 | [diff] [blame] | 1626 | return true; |
Chris Lattner | fa42fad | 2010-10-28 21:28:01 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1629 | /// ParseDirective parses the arm specific directives |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1630 | bool ARMAsmParser::ParseDirective(AsmToken DirectiveID) { |
| 1631 | StringRef IDVal = DirectiveID.getIdentifier(); |
| 1632 | if (IDVal == ".word") |
| 1633 | return ParseDirectiveWord(4, DirectiveID.getLoc()); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1634 | else if (IDVal == ".thumb") |
| 1635 | return ParseDirectiveThumb(DirectiveID.getLoc()); |
| 1636 | else if (IDVal == ".thumb_func") |
| 1637 | return ParseDirectiveThumbFunc(DirectiveID.getLoc()); |
| 1638 | else if (IDVal == ".code") |
| 1639 | return ParseDirectiveCode(DirectiveID.getLoc()); |
| 1640 | else if (IDVal == ".syntax") |
| 1641 | return ParseDirectiveSyntax(DirectiveID.getLoc()); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1642 | return true; |
| 1643 | } |
| 1644 | |
| 1645 | /// ParseDirectiveWord |
| 1646 | /// ::= .word [ expression (, expression)* ] |
| 1647 | bool ARMAsmParser::ParseDirectiveWord(unsigned Size, SMLoc L) { |
| 1648 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 1649 | for (;;) { |
| 1650 | const MCExpr *Value; |
| 1651 | if (getParser().ParseExpression(Value)) |
| 1652 | return true; |
| 1653 | |
Chris Lattner | aaec205 | 2010-01-19 19:46:13 +0000 | [diff] [blame] | 1654 | getParser().getStreamer().EmitValue(Value, Size, 0/*addrspace*/); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1655 | |
| 1656 | if (getLexer().is(AsmToken::EndOfStatement)) |
| 1657 | break; |
Jim Grosbach | 16c7425 | 2010-10-29 14:46:02 +0000 | [diff] [blame] | 1658 | |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1659 | // FIXME: Improve diagnostic. |
| 1660 | if (getLexer().isNot(AsmToken::Comma)) |
| 1661 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1662 | Parser.Lex(); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1666 | Parser.Lex(); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1667 | return false; |
| 1668 | } |
| 1669 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1670 | /// ParseDirectiveThumb |
| 1671 | /// ::= .thumb |
| 1672 | bool ARMAsmParser::ParseDirectiveThumb(SMLoc L) { |
| 1673 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 1674 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1675 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1676 | |
| 1677 | // TODO: set thumb mode |
| 1678 | // TODO: tell the MC streamer the mode |
| 1679 | // getParser().getStreamer().Emit???(); |
| 1680 | return false; |
| 1681 | } |
| 1682 | |
| 1683 | /// ParseDirectiveThumbFunc |
| 1684 | /// ::= .thumbfunc symbol_name |
| 1685 | bool ARMAsmParser::ParseDirectiveThumbFunc(SMLoc L) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1686 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1687 | if (Tok.isNot(AsmToken::Identifier) && Tok.isNot(AsmToken::String)) |
Jim Grosbach | 83c4018 | 2010-11-05 22:11:33 +0000 | [diff] [blame] | 1688 | return Error(L, "unexpected token in .thumb_func directive"); |
Jim Grosbach | 642fc9c | 2010-11-05 22:33:53 +0000 | [diff] [blame] | 1689 | StringRef Name = Tok.getString(); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1690 | Parser.Lex(); // Consume the identifier token. |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1691 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
| 1692 | return Error(L, "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1693 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1694 | |
Jim Grosbach | 642fc9c | 2010-11-05 22:33:53 +0000 | [diff] [blame] | 1695 | // Mark symbol as a thumb symbol. |
| 1696 | MCSymbol *Func = getParser().getContext().GetOrCreateSymbol(Name); |
| 1697 | getParser().getStreamer().EmitThumbFunc(Func); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1698 | return false; |
| 1699 | } |
| 1700 | |
| 1701 | /// ParseDirectiveSyntax |
| 1702 | /// ::= .syntax unified | divided |
| 1703 | bool ARMAsmParser::ParseDirectiveSyntax(SMLoc L) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1704 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1705 | if (Tok.isNot(AsmToken::Identifier)) |
| 1706 | return Error(L, "unexpected token in .syntax directive"); |
Benjamin Kramer | 38e5989 | 2010-07-14 22:38:02 +0000 | [diff] [blame] | 1707 | StringRef Mode = Tok.getString(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 1708 | if (Mode == "unified" || Mode == "UNIFIED") |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1709 | Parser.Lex(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 1710 | else if (Mode == "divided" || Mode == "DIVIDED") |
Kevin Enderby | 9e56fb1 | 2011-01-27 23:22:36 +0000 | [diff] [blame] | 1711 | return Error(L, "'.syntax divided' arm asssembly not supported"); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1712 | else |
| 1713 | return Error(L, "unrecognized syntax mode in .syntax directive"); |
| 1714 | |
| 1715 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1716 | return Error(Parser.getTok().getLoc(), "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1717 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1718 | |
| 1719 | // TODO tell the MC streamer the mode |
| 1720 | // getParser().getStreamer().Emit???(); |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
| 1724 | /// ParseDirectiveCode |
| 1725 | /// ::= .code 16 | 32 |
| 1726 | bool ARMAsmParser::ParseDirectiveCode(SMLoc L) { |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1727 | const AsmToken &Tok = Parser.getTok(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1728 | if (Tok.isNot(AsmToken::Integer)) |
| 1729 | return Error(L, "unexpected token in .code directive"); |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1730 | int64_t Val = Parser.getTok().getIntVal(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 1731 | if (Val == 16) |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1732 | Parser.Lex(); |
Duncan Sands | 58c8691 | 2010-06-29 13:04:35 +0000 | [diff] [blame] | 1733 | else if (Val == 32) |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1734 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1735 | else |
| 1736 | return Error(L, "invalid operand to .code directive"); |
| 1737 | |
| 1738 | if (getLexer().isNot(AsmToken::EndOfStatement)) |
Sean Callanan | 18b8323 | 2010-01-19 21:44:56 +0000 | [diff] [blame] | 1739 | return Error(Parser.getTok().getLoc(), "unexpected token in directive"); |
Sean Callanan | b9a25b7 | 2010-01-19 20:27:46 +0000 | [diff] [blame] | 1740 | Parser.Lex(); |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1741 | |
Kevin Enderby | fef9ff4 | 2011-01-13 01:07:01 +0000 | [diff] [blame] | 1742 | // FIXME: We need to be able switch subtargets at this point so that |
| 1743 | // MatchInstructionImpl() will work when it gets the AvailableFeatures which |
| 1744 | // includes Feature_IsThumb or not to match the right instructions. This is |
| 1745 | // blocked on the FIXME in llvm-mc.cpp when creating the TargetMachine. |
| 1746 | if (Val == 16){ |
| 1747 | assert(TM.getSubtarget<ARMSubtarget>().isThumb() && |
| 1748 | "switching between arm/thumb not yet suppported via .code 16)"); |
Jim Grosbach | 2a30170 | 2010-11-05 22:40:53 +0000 | [diff] [blame] | 1749 | getParser().getStreamer().EmitAssemblerFlag(MCAF_Code16); |
Kevin Enderby | fef9ff4 | 2011-01-13 01:07:01 +0000 | [diff] [blame] | 1750 | } |
| 1751 | else{ |
| 1752 | assert(!TM.getSubtarget<ARMSubtarget>().isThumb() && |
| 1753 | "switching between thumb/arm not yet suppported via .code 32)"); |
Jim Grosbach | 2a30170 | 2010-11-05 22:40:53 +0000 | [diff] [blame] | 1754 | getParser().getStreamer().EmitAssemblerFlag(MCAF_Code32); |
Kevin Enderby | fef9ff4 | 2011-01-13 01:07:01 +0000 | [diff] [blame] | 1755 | } |
Jim Grosbach | 2a30170 | 2010-11-05 22:40:53 +0000 | [diff] [blame] | 1756 | |
Kevin Enderby | 515d509 | 2009-10-15 20:48:48 +0000 | [diff] [blame] | 1757 | return false; |
| 1758 | } |
| 1759 | |
Sean Callanan | 90b7097 | 2010-04-07 20:29:34 +0000 | [diff] [blame] | 1760 | extern "C" void LLVMInitializeARMAsmLexer(); |
| 1761 | |
Kevin Enderby | 9c41fa8 | 2009-10-30 22:55:57 +0000 | [diff] [blame] | 1762 | /// Force static initialization. |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1763 | extern "C" void LLVMInitializeARMAsmParser() { |
| 1764 | RegisterAsmParser<ARMAsmParser> X(TheARMTarget); |
| 1765 | RegisterAsmParser<ARMAsmParser> Y(TheThumbTarget); |
Sean Callanan | 90b7097 | 2010-04-07 20:29:34 +0000 | [diff] [blame] | 1766 | LLVMInitializeARMAsmLexer(); |
Kevin Enderby | ca9c42c | 2009-09-15 00:27:25 +0000 | [diff] [blame] | 1767 | } |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 1768 | |
Chris Lattner | 0692ee6 | 2010-09-06 19:11:01 +0000 | [diff] [blame] | 1769 | #define GET_REGISTER_MATCHER |
| 1770 | #define GET_MATCHER_IMPLEMENTATION |
Daniel Dunbar | 3483aca | 2010-08-11 05:24:50 +0000 | [diff] [blame] | 1771 | #include "ARMGenAsmMatcher.inc" |