Jia Liu | c570711 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsAsmParser.cpp - Parse Mips assembly to MCInst instructions ----===// |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 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 "MCTargetDesc/MipsMCTargetDesc.h" |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 11 | #include "MipsRegisterInfo.h" |
| 12 | #include "llvm/ADT/StringSwitch.h" |
| 13 | #include "llvm/MC/MCContext.h" |
| 14 | #include "llvm/MC/MCExpr.h" |
| 15 | #include "llvm/MC/MCInst.h" |
| 16 | #include "llvm/MC/MCStreamer.h" |
| 17 | #include "llvm/MC/MCSubtargetInfo.h" |
| 18 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCParser/MCAsmLexer.h" |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCParser/MCParsedAsmOperand.h" |
| 21 | #include "llvm/MC/MCTargetAsmParser.h" |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace { |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 27 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 28 | class MipsAsmParser : public MCTargetAsmParser { |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 29 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 30 | MCSubtargetInfo &STI; |
| 31 | MCAsmParser &Parser; |
| 32 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 33 | #define GET_ASSEMBLER_HEADER |
| 34 | #include "MipsGenAsmMatcher.inc" |
| 35 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 36 | bool MatchAndEmitInstruction(SMLoc IDLoc, |
| 37 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 38 | MCStreamer &Out); |
| 39 | |
| 40 | bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc); |
| 41 | |
| 42 | bool ParseInstruction(StringRef Name, SMLoc NameLoc, |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 43 | SmallVectorImpl<MCParsedAsmOperand*> &Operands); |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 44 | |
| 45 | bool ParseDirective(AsmToken DirectiveID); |
| 46 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 47 | MipsAsmParser::OperandMatchResultTy |
| 48 | parseMemOperand(SmallVectorImpl<MCParsedAsmOperand*>&); |
Chad Rosier | 038f3e3 | 2012-09-03 18:47:45 +0000 | [diff] [blame] | 49 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 50 | unsigned |
| 51 | getMCInstOperandNum(unsigned Kind, MCInst &Inst, |
| 52 | const SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 53 | unsigned OperandNum, unsigned &NumMCOperands); |
| 54 | |
| 55 | bool ParseOperand(SmallVectorImpl<MCParsedAsmOperand*> &, |
| 56 | StringRef Mnemonic); |
| 57 | |
| 58 | int tryParseRegister(StringRef Mnemonic); |
| 59 | |
| 60 | bool tryParseRegisterOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 61 | StringRef Mnemonic); |
| 62 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 63 | bool parseMemOffset(const MCExpr *&Res); |
| 64 | bool parseRelocOperand(const MCExpr *&Res); |
| 65 | MCSymbolRefExpr::VariantKind getVariantKind(StringRef Symbol); |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 66 | bool isMips64() const { |
| 67 | return (STI.getFeatureBits() & Mips::FeatureMips64) != 0; |
| 68 | } |
| 69 | |
| 70 | int matchRegisterName(StringRef Symbol); |
| 71 | |
| 72 | int matchRegisterByNumber(unsigned RegNum, StringRef Mnemonic); |
| 73 | |
| 74 | unsigned getReg(int RC,int RegNo); |
Chad Rosier | 038f3e3 | 2012-09-03 18:47:45 +0000 | [diff] [blame] | 75 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 76 | public: |
| 77 | MipsAsmParser(MCSubtargetInfo &sti, MCAsmParser &parser) |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 78 | : MCTargetAsmParser(), STI(sti), Parser(parser) { |
| 79 | // Initialize the set of available features. |
| 80 | setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits())); |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 83 | MCAsmParser &getParser() const { return Parser; } |
| 84 | MCAsmLexer &getLexer() const { return Parser.getLexer(); } |
| 85 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 86 | }; |
| 87 | } |
| 88 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 89 | namespace { |
| 90 | |
| 91 | /// MipsOperand - Instances of this class represent a parsed Mips machine |
| 92 | /// instruction. |
| 93 | class MipsOperand : public MCParsedAsmOperand { |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 94 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 95 | enum KindTy { |
| 96 | k_CondCode, |
| 97 | k_CoprocNum, |
| 98 | k_Immediate, |
| 99 | k_Memory, |
| 100 | k_PostIndexRegister, |
| 101 | k_Register, |
| 102 | k_Token |
| 103 | } Kind; |
| 104 | |
| 105 | MipsOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {} |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 106 | |
| 107 | union { |
| 108 | struct { |
| 109 | const char *Data; |
| 110 | unsigned Length; |
| 111 | } Tok; |
| 112 | |
| 113 | struct { |
| 114 | unsigned RegNum; |
| 115 | } Reg; |
| 116 | |
| 117 | struct { |
| 118 | const MCExpr *Val; |
| 119 | } Imm; |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 120 | |
| 121 | struct { |
| 122 | unsigned Base; |
| 123 | const MCExpr *Off; |
| 124 | } Mem; |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | SMLoc StartLoc, EndLoc; |
| 128 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 129 | public: |
| 130 | void addRegOperands(MCInst &Inst, unsigned N) const { |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 131 | assert(N == 1 && "Invalid number of operands!"); |
| 132 | Inst.addOperand(MCOperand::CreateReg(getReg())); |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 133 | } |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 134 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 135 | void addExpr(MCInst &Inst, const MCExpr *Expr) const{ |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 136 | // Add as immediate when possible. Null MCExpr = 0. |
| 137 | if (Expr == 0) |
| 138 | Inst.addOperand(MCOperand::CreateImm(0)); |
| 139 | else if (const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(Expr)) |
| 140 | Inst.addOperand(MCOperand::CreateImm(CE->getValue())); |
| 141 | else |
| 142 | Inst.addOperand(MCOperand::CreateExpr(Expr)); |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 143 | } |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 144 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 145 | void addImmOperands(MCInst &Inst, unsigned N) const { |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 146 | assert(N == 1 && "Invalid number of operands!"); |
| 147 | const MCExpr *Expr = getImm(); |
| 148 | addExpr(Inst,Expr); |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 149 | } |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 150 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 151 | void addMemOperands(MCInst &Inst, unsigned N) const { |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 152 | assert(N == 2 && "Invalid number of operands!"); |
| 153 | |
| 154 | Inst.addOperand(MCOperand::CreateReg(getMemBase())); |
| 155 | |
| 156 | const MCExpr *Expr = getMemOff(); |
| 157 | addExpr(Inst,Expr); |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | bool isReg() const { return Kind == k_Register; } |
| 161 | bool isImm() const { return Kind == k_Immediate; } |
| 162 | bool isToken() const { return Kind == k_Token; } |
| 163 | bool isMem() const { return Kind == k_Memory; } |
| 164 | |
| 165 | StringRef getToken() const { |
| 166 | assert(Kind == k_Token && "Invalid access!"); |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 167 | return StringRef(Tok.Data, Tok.Length); |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | unsigned getReg() const { |
| 171 | assert((Kind == k_Register) && "Invalid access!"); |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 172 | return Reg.RegNum; |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 175 | const MCExpr *getImm() const { |
| 176 | assert((Kind == k_Immediate) && "Invalid access!"); |
| 177 | return Imm.Val; |
| 178 | } |
| 179 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 180 | unsigned getMemBase() const { |
| 181 | assert((Kind == k_Memory) && "Invalid access!"); |
| 182 | return Mem.Base; |
| 183 | } |
| 184 | |
| 185 | const MCExpr *getMemOff() const { |
| 186 | assert((Kind == k_Memory) && "Invalid access!"); |
| 187 | return Mem.Off; |
| 188 | } |
| 189 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 190 | static MipsOperand *CreateToken(StringRef Str, SMLoc S) { |
| 191 | MipsOperand *Op = new MipsOperand(k_Token); |
| 192 | Op->Tok.Data = Str.data(); |
| 193 | Op->Tok.Length = Str.size(); |
| 194 | Op->StartLoc = S; |
| 195 | Op->EndLoc = S; |
| 196 | return Op; |
| 197 | } |
| 198 | |
| 199 | static MipsOperand *CreateReg(unsigned RegNum, SMLoc S, SMLoc E) { |
| 200 | MipsOperand *Op = new MipsOperand(k_Register); |
| 201 | Op->Reg.RegNum = RegNum; |
| 202 | Op->StartLoc = S; |
| 203 | Op->EndLoc = E; |
| 204 | return Op; |
| 205 | } |
| 206 | |
| 207 | static MipsOperand *CreateImm(const MCExpr *Val, SMLoc S, SMLoc E) { |
| 208 | MipsOperand *Op = new MipsOperand(k_Immediate); |
| 209 | Op->Imm.Val = Val; |
| 210 | Op->StartLoc = S; |
| 211 | Op->EndLoc = E; |
| 212 | return Op; |
| 213 | } |
| 214 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 215 | static MipsOperand *CreateMem(unsigned Base, const MCExpr *Off, |
| 216 | SMLoc S, SMLoc E) { |
| 217 | MipsOperand *Op = new MipsOperand(k_Memory); |
| 218 | Op->Mem.Base = Base; |
| 219 | Op->Mem.Off = Off; |
| 220 | Op->StartLoc = S; |
| 221 | Op->EndLoc = E; |
| 222 | return Op; |
| 223 | } |
| 224 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 225 | /// getStartLoc - Get the location of the first token of this operand. |
| 226 | SMLoc getStartLoc() const { return StartLoc; } |
| 227 | /// getEndLoc - Get the location of the last token of this operand. |
| 228 | SMLoc getEndLoc() const { return EndLoc; } |
| 229 | |
Akira Hatanaka | 72e9b6a | 2012-08-17 20:16:42 +0000 | [diff] [blame] | 230 | virtual void print(raw_ostream &OS) const { |
| 231 | llvm_unreachable("unimplemented!"); |
| 232 | } |
| 233 | }; |
| 234 | } |
| 235 | |
Chad Rosier | 038f3e3 | 2012-09-03 18:47:45 +0000 | [diff] [blame] | 236 | unsigned MipsAsmParser:: |
Chad Rosier | 5d637d7 | 2012-09-05 01:15:43 +0000 | [diff] [blame] | 237 | getMCInstOperandNum(unsigned Kind, MCInst &Inst, |
Chad Rosier | 038f3e3 | 2012-09-03 18:47:45 +0000 | [diff] [blame] | 238 | const SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
Chad Rosier | 2cc97de | 2012-09-03 20:31:23 +0000 | [diff] [blame] | 239 | unsigned OperandNum, unsigned &NumMCOperands) { |
Chad Rosier | 5d637d7 | 2012-09-05 01:15:43 +0000 | [diff] [blame] | 240 | assert (0 && "getMCInstOperandNum() not supported by the Mips target."); |
Chad Rosier | efeaae8 | 2012-09-03 19:04:35 +0000 | [diff] [blame] | 241 | // The Mips backend doesn't currently include the matcher implementation, so |
Chad Rosier | 5d637d7 | 2012-09-05 01:15:43 +0000 | [diff] [blame] | 242 | // the getMCInstOperandNumImpl() is undefined. This is a temporary |
Chad Rosier | efeaae8 | 2012-09-03 19:04:35 +0000 | [diff] [blame] | 243 | // work around. |
Chad Rosier | 2cc97de | 2012-09-03 20:31:23 +0000 | [diff] [blame] | 244 | NumMCOperands = 0; |
Chad Rosier | 038f3e3 | 2012-09-03 18:47:45 +0000 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 248 | bool MipsAsmParser:: |
| 249 | MatchAndEmitInstruction(SMLoc IDLoc, |
| 250 | SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 251 | MCStreamer &Out) { |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 252 | MCInst Inst; |
| 253 | unsigned ErrorInfo; |
| 254 | unsigned Kind; |
| 255 | unsigned MatchResult = MatchInstructionImpl(Operands, Kind, Inst, ErrorInfo); |
| 256 | |
| 257 | switch (MatchResult) { |
| 258 | default: break; |
| 259 | case Match_Success: { |
| 260 | Inst.setLoc(IDLoc); |
| 261 | Out.EmitInstruction(Inst); |
| 262 | return false; |
| 263 | } |
| 264 | case Match_MissingFeature: |
| 265 | Error(IDLoc, "instruction requires a CPU feature not currently enabled"); |
| 266 | return true; |
| 267 | case Match_InvalidOperand: { |
| 268 | SMLoc ErrorLoc = IDLoc; |
| 269 | if (ErrorInfo != ~0U) { |
| 270 | if (ErrorInfo >= Operands.size()) |
| 271 | return Error(IDLoc, "too few operands for instruction"); |
| 272 | |
| 273 | ErrorLoc = ((MipsOperand*)Operands[ErrorInfo])->getStartLoc(); |
| 274 | if (ErrorLoc == SMLoc()) ErrorLoc = IDLoc; |
| 275 | } |
| 276 | |
| 277 | return Error(ErrorLoc, "invalid operand for instruction"); |
| 278 | } |
| 279 | case Match_MnemonicFail: |
| 280 | return Error(IDLoc, "invalid instruction"); |
| 281 | } |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 282 | return true; |
| 283 | } |
| 284 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 285 | int MipsAsmParser::matchRegisterName(StringRef Name) { |
| 286 | |
| 287 | int CC = StringSwitch<unsigned>(Name) |
| 288 | .Case("zero", Mips::ZERO) |
| 289 | .Case("a0", Mips::A0) |
| 290 | .Case("a1", Mips::A1) |
| 291 | .Case("a2", Mips::A2) |
| 292 | .Case("a3", Mips::A3) |
| 293 | .Case("v0", Mips::V0) |
| 294 | .Case("v1", Mips::V1) |
| 295 | .Case("s0", Mips::S0) |
| 296 | .Case("s1", Mips::S1) |
| 297 | .Case("s2", Mips::S2) |
| 298 | .Case("s3", Mips::S3) |
| 299 | .Case("s4", Mips::S4) |
| 300 | .Case("s5", Mips::S5) |
| 301 | .Case("s6", Mips::S6) |
| 302 | .Case("s7", Mips::S7) |
| 303 | .Case("k0", Mips::K0) |
| 304 | .Case("k1", Mips::K1) |
| 305 | .Case("sp", Mips::SP) |
| 306 | .Case("fp", Mips::FP) |
| 307 | .Case("gp", Mips::GP) |
| 308 | .Case("ra", Mips::RA) |
| 309 | .Case("t0", Mips::T0) |
| 310 | .Case("t1", Mips::T1) |
| 311 | .Case("t2", Mips::T2) |
| 312 | .Case("t3", Mips::T3) |
| 313 | .Case("t4", Mips::T4) |
| 314 | .Case("t5", Mips::T5) |
| 315 | .Case("t6", Mips::T6) |
| 316 | .Case("t7", Mips::T7) |
| 317 | .Case("t8", Mips::T8) |
| 318 | .Case("t9", Mips::T9) |
| 319 | .Case("at", Mips::AT) |
| 320 | .Case("fcc0", Mips::FCC0) |
| 321 | .Default(-1); |
| 322 | |
| 323 | if (CC != -1) { |
| 324 | //64 bit register in Mips are following 32 bit definitions. |
| 325 | if (isMips64()) |
| 326 | CC++; |
| 327 | return CC; |
| 328 | } |
| 329 | |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | unsigned MipsAsmParser::getReg(int RC,int RegNo){ |
| 334 | return *(getContext().getRegisterInfo().getRegClass(RC).begin() + RegNo); |
| 335 | } |
| 336 | |
| 337 | int MipsAsmParser::matchRegisterByNumber(unsigned RegNum,StringRef Mnemonic) { |
| 338 | |
| 339 | if (Mnemonic.lower() == "rdhwr") { |
| 340 | //at the moment only hwreg29 is supported |
| 341 | if (RegNum != 29) |
| 342 | return -1; |
| 343 | return Mips::HWR29; |
| 344 | } |
| 345 | |
| 346 | if (RegNum > 31) |
| 347 | return -1; |
| 348 | |
| 349 | return getReg(Mips::CPURegsRegClassID,RegNum); |
| 350 | } |
| 351 | |
| 352 | int MipsAsmParser::tryParseRegister(StringRef Mnemonic) { |
| 353 | const AsmToken &Tok = Parser.getTok(); |
| 354 | int RegNum = -1; |
| 355 | |
| 356 | if (Tok.is(AsmToken::Identifier)) { |
| 357 | std::string lowerCase = Tok.getString().lower(); |
| 358 | RegNum = matchRegisterName(lowerCase); |
| 359 | } else if (Tok.is(AsmToken::Integer)) |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 360 | RegNum = matchRegisterByNumber(static_cast<unsigned> (Tok.getIntVal()), |
| 361 | Mnemonic.lower()); |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 362 | return RegNum; |
| 363 | } |
| 364 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 365 | bool MipsAsmParser:: |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 366 | tryParseRegisterOperand(SmallVectorImpl<MCParsedAsmOperand*> &Operands, |
| 367 | StringRef Mnemonic){ |
| 368 | |
| 369 | SMLoc S = Parser.getTok().getLoc(); |
| 370 | int RegNo = -1; |
| 371 | RegNo = tryParseRegister(Mnemonic); |
| 372 | if (RegNo == -1) |
| 373 | return true; |
| 374 | |
| 375 | Operands.push_back(MipsOperand::CreateReg(RegNo, S, |
| 376 | Parser.getTok().getLoc())); |
| 377 | Parser.Lex(); // Eat register token. |
| 378 | return false; |
| 379 | } |
| 380 | |
| 381 | bool MipsAsmParser::ParseOperand(SmallVectorImpl<MCParsedAsmOperand*>&Operands, |
| 382 | StringRef Mnemonic) { |
| 383 | //Check if the current operand has a custom associated parser, if so, try to |
| 384 | //custom parse the operand, or fallback to the general approach. |
| 385 | OperandMatchResultTy ResTy = MatchOperandParserImpl(Operands, Mnemonic); |
| 386 | if (ResTy == MatchOperand_Success) |
| 387 | return false; |
| 388 | // If there wasn't a custom match, try the generic matcher below. Otherwise, |
| 389 | // there was a match, but an error occurred, in which case, just return that |
| 390 | // the operand parsing failed. |
| 391 | if (ResTy == MatchOperand_ParseFail) |
| 392 | return true; |
| 393 | |
| 394 | switch (getLexer().getKind()) { |
| 395 | default: |
| 396 | Error(Parser.getTok().getLoc(), "unexpected token in operand"); |
| 397 | return true; |
| 398 | case AsmToken::Dollar: { |
| 399 | //parse register |
| 400 | SMLoc S = Parser.getTok().getLoc(); |
| 401 | Parser.Lex(); // Eat dollar token. |
| 402 | //parse register operand |
| 403 | if (!tryParseRegisterOperand(Operands,Mnemonic)) { |
| 404 | if (getLexer().is(AsmToken::LParen)) { |
| 405 | //check if it is indexed addressing operand |
| 406 | Operands.push_back(MipsOperand::CreateToken("(", S)); |
| 407 | Parser.Lex(); //eat parenthesis |
| 408 | if (getLexer().isNot(AsmToken::Dollar)) |
| 409 | return true; |
| 410 | |
| 411 | Parser.Lex(); //eat dollar |
| 412 | if (tryParseRegisterOperand(Operands,Mnemonic)) |
| 413 | return true; |
| 414 | |
| 415 | if (!getLexer().is(AsmToken::RParen)) |
| 416 | return true; |
| 417 | |
| 418 | S = Parser.getTok().getLoc(); |
| 419 | Operands.push_back(MipsOperand::CreateToken(")", S)); |
| 420 | Parser.Lex(); |
| 421 | } |
| 422 | return false; |
| 423 | } |
| 424 | //maybe it is a symbol reference |
| 425 | StringRef Identifier; |
| 426 | if (Parser.ParseIdentifier(Identifier)) |
| 427 | return true; |
| 428 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 429 | SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 430 | |
| 431 | StringRef Id = StringRef("$" + Identifier.str()); |
| 432 | MCSymbol *Sym = getContext().GetOrCreateSymbol(Id); |
| 433 | |
| 434 | // Otherwise create a symbol ref. |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 435 | const MCExpr *Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 436 | getContext()); |
| 437 | |
| 438 | Operands.push_back(MipsOperand::CreateImm(Res, S, E)); |
| 439 | return false; |
| 440 | } |
| 441 | case AsmToken::Identifier: |
| 442 | case AsmToken::LParen: |
| 443 | case AsmToken::Minus: |
| 444 | case AsmToken::Plus: |
| 445 | case AsmToken::Integer: |
| 446 | case AsmToken::String: { |
| 447 | // quoted label names |
| 448 | const MCExpr *IdVal; |
| 449 | SMLoc S = Parser.getTok().getLoc(); |
| 450 | if (getParser().ParseExpression(IdVal)) |
| 451 | return true; |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 452 | SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 453 | Operands.push_back(MipsOperand::CreateImm(IdVal, S, E)); |
| 454 | return false; |
| 455 | } |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 456 | case AsmToken::Percent: { |
| 457 | //it is a symbol reference or constant expression |
| 458 | const MCExpr *IdVal; |
| 459 | SMLoc S = Parser.getTok().getLoc(); //start location of the operand |
| 460 | if (parseRelocOperand(IdVal)) |
| 461 | return true; |
| 462 | |
| 463 | SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
| 464 | |
| 465 | Operands.push_back(MipsOperand::CreateImm(IdVal, S, E)); |
| 466 | return false; |
| 467 | }//case AsmToken::Percent |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 468 | }//switch(getLexer().getKind()) |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 469 | return true; |
| 470 | } |
| 471 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 472 | bool MipsAsmParser::parseRelocOperand(const MCExpr *&Res) { |
| 473 | |
| 474 | Parser.Lex(); //eat % token |
| 475 | const AsmToken &Tok = Parser.getTok(); //get next token, operation |
| 476 | if (Tok.isNot(AsmToken::Identifier)) |
| 477 | return true; |
| 478 | |
| 479 | StringRef Str = Tok.getIdentifier(); |
| 480 | |
| 481 | Parser.Lex(); //eat identifier |
| 482 | //now make expression from the rest of the operand |
| 483 | const MCExpr *IdVal; |
| 484 | SMLoc EndLoc; |
| 485 | |
| 486 | if (getLexer().getKind() == AsmToken::LParen) { |
| 487 | while (1) { |
| 488 | Parser.Lex(); //eat '(' token |
| 489 | if (getLexer().getKind() == AsmToken::Percent) { |
| 490 | Parser.Lex(); //eat % token |
| 491 | const AsmToken &nextTok = Parser.getTok(); |
| 492 | if (nextTok.isNot(AsmToken::Identifier)) |
| 493 | return true; |
| 494 | Str = StringRef(Str.str() + "(%" + nextTok.getIdentifier().str()); |
| 495 | Parser.Lex(); //eat identifier |
| 496 | if (getLexer().getKind() != AsmToken::LParen) |
| 497 | return true; |
| 498 | } else |
| 499 | break; |
| 500 | } |
| 501 | if (getParser().ParseParenExpression(IdVal,EndLoc)) |
| 502 | return true; |
| 503 | |
| 504 | while (getLexer().getKind() == AsmToken::RParen) |
| 505 | Parser.Lex(); //eat ')' token |
| 506 | |
| 507 | } else |
| 508 | return true; //parenthesis must follow reloc operand |
| 509 | |
| 510 | SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
| 511 | |
| 512 | //Check the type of the expression |
| 513 | if (MCConstantExpr::classof(IdVal)) { |
| 514 | //it's a constant, evaluate lo or hi value |
| 515 | int Val = ((const MCConstantExpr*)IdVal)->getValue(); |
| 516 | if (Str == "lo") { |
| 517 | Val = Val & 0xffff; |
| 518 | } else if (Str == "hi") { |
| 519 | Val = (Val & 0xffff0000) >> 16; |
| 520 | } |
| 521 | Res = MCConstantExpr::Create(Val, getContext()); |
| 522 | return false; |
| 523 | } |
| 524 | |
| 525 | if (MCSymbolRefExpr::classof(IdVal)) { |
| 526 | //it's a symbol, create symbolic expression from symbol |
| 527 | StringRef Symbol = ((const MCSymbolRefExpr*)IdVal)->getSymbol().getName(); |
| 528 | MCSymbolRefExpr::VariantKind VK = getVariantKind(Str); |
| 529 | Res = MCSymbolRefExpr::Create(Symbol,VK,getContext()); |
| 530 | return false; |
| 531 | } |
| 532 | return true; |
| 533 | } |
| 534 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 535 | bool MipsAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, |
| 536 | SMLoc &EndLoc) { |
| 537 | |
| 538 | StartLoc = Parser.getTok().getLoc(); |
| 539 | RegNo = tryParseRegister(""); |
| 540 | EndLoc = Parser.getTok().getLoc(); |
| 541 | return (RegNo == (unsigned)-1); |
| 542 | } |
| 543 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 544 | bool MipsAsmParser::parseMemOffset(const MCExpr *&Res) { |
| 545 | |
| 546 | SMLoc S; |
| 547 | |
| 548 | switch(getLexer().getKind()) { |
| 549 | default: |
| 550 | return true; |
| 551 | case AsmToken::Integer: |
| 552 | case AsmToken::Minus: |
| 553 | case AsmToken::Plus: |
| 554 | return (getParser().ParseExpression(Res)); |
| 555 | case AsmToken::Percent: { |
| 556 | return parseRelocOperand(Res); |
| 557 | } |
| 558 | case AsmToken::LParen: |
| 559 | return false; //it's probably assuming 0 |
| 560 | } |
| 561 | return true; |
| 562 | } |
| 563 | |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 564 | MipsAsmParser::OperandMatchResultTy MipsAsmParser::parseMemOperand( |
| 565 | SmallVectorImpl<MCParsedAsmOperand*>&Operands) { |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 566 | |
| 567 | const MCExpr *IdVal = 0; |
| 568 | SMLoc S; |
| 569 | //first operand is the offset |
| 570 | S = Parser.getTok().getLoc(); |
| 571 | |
| 572 | if (parseMemOffset(IdVal)) |
| 573 | return MatchOperand_ParseFail; |
| 574 | |
| 575 | const AsmToken &Tok = Parser.getTok(); //get next token |
| 576 | if (Tok.isNot(AsmToken::LParen)) { |
| 577 | Error(Parser.getTok().getLoc(), "'(' expected"); |
| 578 | return MatchOperand_ParseFail; |
| 579 | } |
| 580 | |
| 581 | Parser.Lex(); // Eat '(' token. |
| 582 | |
| 583 | const AsmToken &Tok1 = Parser.getTok(); //get next token |
| 584 | if (Tok1.is(AsmToken::Dollar)) { |
| 585 | Parser.Lex(); // Eat '$' token. |
| 586 | if (tryParseRegisterOperand(Operands,"")) { |
| 587 | Error(Parser.getTok().getLoc(), "unexpected token in operand"); |
| 588 | return MatchOperand_ParseFail; |
| 589 | } |
| 590 | |
| 591 | } else { |
| 592 | Error(Parser.getTok().getLoc(),"unexpected token in operand"); |
| 593 | return MatchOperand_ParseFail; |
| 594 | } |
| 595 | |
| 596 | const AsmToken &Tok2 = Parser.getTok(); //get next token |
| 597 | if (Tok2.isNot(AsmToken::RParen)) { |
| 598 | Error(Parser.getTok().getLoc(), "')' expected"); |
| 599 | return MatchOperand_ParseFail; |
| 600 | } |
| 601 | |
| 602 | SMLoc E = SMLoc::getFromPointer(Parser.getTok().getLoc().getPointer() - 1); |
| 603 | |
| 604 | Parser.Lex(); // Eat ')' token. |
| 605 | |
| 606 | if (IdVal == 0) |
| 607 | IdVal = MCConstantExpr::Create(0, getContext()); |
| 608 | |
| 609 | //now replace register operand with the mem operand |
| 610 | MipsOperand* op = static_cast<MipsOperand*>(Operands.back()); |
| 611 | int RegNo = op->getReg(); |
| 612 | //remove register from operands |
| 613 | Operands.pop_back(); |
| 614 | //and add memory operand |
| 615 | Operands.push_back(MipsOperand::CreateMem(RegNo, IdVal, S, E)); |
| 616 | delete op; |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 617 | return MatchOperand_Success; |
| 618 | } |
| 619 | |
Jack Carter | 6b96c3f | 2012-09-06 20:00:02 +0000 | [diff] [blame^] | 620 | MCSymbolRefExpr::VariantKind MipsAsmParser::getVariantKind(StringRef Symbol) { |
| 621 | |
| 622 | MCSymbolRefExpr::VariantKind VK |
| 623 | = StringSwitch<MCSymbolRefExpr::VariantKind>(Symbol) |
| 624 | .Case("hi", MCSymbolRefExpr::VK_Mips_ABS_HI) |
| 625 | .Case("lo", MCSymbolRefExpr::VK_Mips_ABS_LO) |
| 626 | .Case("gp_rel", MCSymbolRefExpr::VK_Mips_GPREL) |
| 627 | .Case("call16", MCSymbolRefExpr::VK_Mips_GOT_CALL) |
| 628 | .Case("got", MCSymbolRefExpr::VK_Mips_GOT) |
| 629 | .Case("tlsgd", MCSymbolRefExpr::VK_Mips_TLSGD) |
| 630 | .Case("tlsldm", MCSymbolRefExpr::VK_Mips_TLSLDM) |
| 631 | .Case("dtprel_hi", MCSymbolRefExpr::VK_Mips_DTPREL_HI) |
| 632 | .Case("dtprel_lo", MCSymbolRefExpr::VK_Mips_DTPREL_LO) |
| 633 | .Case("gottprel", MCSymbolRefExpr::VK_Mips_GOTTPREL) |
| 634 | .Case("tprel_hi", MCSymbolRefExpr::VK_Mips_TPREL_HI) |
| 635 | .Case("tprel_lo", MCSymbolRefExpr::VK_Mips_TPREL_LO) |
| 636 | .Case("got_disp", MCSymbolRefExpr::VK_Mips_GOT_DISP) |
| 637 | .Case("got_page", MCSymbolRefExpr::VK_Mips_GOT_PAGE) |
| 638 | .Case("got_ofst", MCSymbolRefExpr::VK_Mips_GOT_OFST) |
| 639 | .Case("hi(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_HI) |
| 640 | .Case("lo(%neg(%gp_rel", MCSymbolRefExpr::VK_Mips_GPOFF_LO) |
| 641 | .Default(MCSymbolRefExpr::VK_None); |
| 642 | |
| 643 | return VK; |
| 644 | } |
| 645 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 646 | bool MipsAsmParser:: |
| 647 | ParseInstruction(StringRef Name, SMLoc NameLoc, |
| 648 | SmallVectorImpl<MCParsedAsmOperand*> &Operands) { |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 649 | |
| 650 | //first operand is a instruction mnemonic |
| 651 | Operands.push_back(MipsOperand::CreateToken(Name, NameLoc)); |
| 652 | |
| 653 | // Read the remaining operands. |
| 654 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 655 | // Read the first operand. |
| 656 | if (ParseOperand(Operands, Name)) { |
| 657 | SMLoc Loc = getLexer().getLoc(); |
| 658 | Parser.EatToEndOfStatement(); |
| 659 | return Error(Loc, "unexpected token in argument list"); |
| 660 | } |
| 661 | |
| 662 | while (getLexer().is(AsmToken::Comma) ) { |
| 663 | Parser.Lex(); // Eat the comma. |
| 664 | |
| 665 | // Parse and remember the operand. |
| 666 | if (ParseOperand(Operands, Name)) { |
| 667 | SMLoc Loc = getLexer().getLoc(); |
| 668 | Parser.EatToEndOfStatement(); |
| 669 | return Error(Loc, "unexpected token in argument list"); |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (getLexer().isNot(AsmToken::EndOfStatement)) { |
| 675 | SMLoc Loc = getLexer().getLoc(); |
| 676 | Parser.EatToEndOfStatement(); |
| 677 | return Error(Loc, "unexpected token in argument list"); |
| 678 | } |
| 679 | |
| 680 | Parser.Lex(); // Consume the EndOfStatement |
| 681 | return false; |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | bool MipsAsmParser:: |
| 685 | ParseDirective(AsmToken DirectiveID) { |
| 686 | return true; |
| 687 | } |
| 688 | |
Rafael Espindola | fddf804 | 2012-01-11 03:56:41 +0000 | [diff] [blame] | 689 | extern "C" void LLVMInitializeMipsAsmParser() { |
| 690 | RegisterMCAsmParser<MipsAsmParser> X(TheMipsTarget); |
| 691 | RegisterMCAsmParser<MipsAsmParser> Y(TheMipselTarget); |
| 692 | RegisterMCAsmParser<MipsAsmParser> A(TheMips64Target); |
| 693 | RegisterMCAsmParser<MipsAsmParser> B(TheMips64elTarget); |
| 694 | } |
Jack Carter | ec65be8 | 2012-09-05 23:34:03 +0000 | [diff] [blame] | 695 | |
| 696 | #define GET_REGISTER_MATCHER |
| 697 | #define GET_MATCHER_IMPLEMENTATION |
| 698 | #include "MipsGenAsmMatcher.inc" |