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