1 //===-- HexagonAsmParser.cpp - Parse Hexagon asm 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 #define DEBUG_TYPE "mcasmparser" 11 12 #include "Hexagon.h" 13 #include "HexagonTargetStreamer.h" 14 #include "MCTargetDesc/HexagonMCChecker.h" 15 #include "MCTargetDesc/HexagonMCELFStreamer.h" 16 #include "MCTargetDesc/HexagonMCExpr.h" 17 #include "MCTargetDesc/HexagonMCInstrInfo.h" 18 #include "MCTargetDesc/HexagonMCTargetDesc.h" 19 #include "MCTargetDesc/HexagonShuffler.h" 20 #include "llvm/ADT/STLExtras.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include "llvm/ADT/StringExtras.h" 23 #include "llvm/ADT/StringRef.h" 24 #include "llvm/ADT/Twine.h" 25 #include "llvm/BinaryFormat/ELF.h" 26 #include "llvm/MC/MCAssembler.h" 27 #include "llvm/MC/MCContext.h" 28 #include "llvm/MC/MCDirectives.h" 29 #include "llvm/MC/MCELFStreamer.h" 30 #include "llvm/MC/MCExpr.h" 31 #include "llvm/MC/MCInst.h" 32 #include "llvm/MC/MCParser/MCAsmLexer.h" 33 #include "llvm/MC/MCParser/MCAsmParser.h" 34 #include "llvm/MC/MCParser/MCAsmParserExtension.h" 35 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 36 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 37 #include "llvm/MC/MCRegisterInfo.h" 38 #include "llvm/MC/MCSectionELF.h" 39 #include "llvm/MC/MCStreamer.h" 40 #include "llvm/MC/MCSubtargetInfo.h" 41 #include "llvm/MC/MCSymbol.h" 42 #include "llvm/MC/MCValue.h" 43 #include "llvm/Support/Casting.h" 44 #include "llvm/Support/CommandLine.h" 45 #include "llvm/Support/Debug.h" 46 #include "llvm/Support/ErrorHandling.h" 47 #include "llvm/Support/Format.h" 48 #include "llvm/Support/MathExtras.h" 49 #include "llvm/Support/SMLoc.h" 50 #include "llvm/Support/TargetRegistry.h" 51 #include "llvm/Support/raw_ostream.h" 52 #include <algorithm> 53 #include <cassert> 54 #include <cctype> 55 #include <cstddef> 56 #include <cstdint> 57 #include <memory> 58 #include <string> 59 #include <utility> 60 61 using namespace llvm; 62 63 static cl::opt<bool> EnableFutureRegs("mfuture-regs", 64 cl::desc("Enable future registers")); 65 66 static cl::opt<bool> WarnMissingParenthesis( 67 "mwarn-missing-parenthesis", 68 cl::desc("Warn for missing parenthesis around predicate registers"), 69 cl::init(true)); 70 static cl::opt<bool> ErrorMissingParenthesis( 71 "merror-missing-parenthesis", 72 cl::desc("Error for missing parenthesis around predicate registers"), 73 cl::init(false)); 74 static cl::opt<bool> WarnSignedMismatch( 75 "mwarn-sign-mismatch", 76 cl::desc("Warn for mismatching a signed and unsigned value"), 77 cl::init(true)); 78 static cl::opt<bool> WarnNoncontigiousRegister( 79 "mwarn-noncontigious-register", 80 cl::desc("Warn for register names that arent contigious"), cl::init(true)); 81 static cl::opt<bool> ErrorNoncontigiousRegister( 82 "merror-noncontigious-register", 83 cl::desc("Error for register names that aren't contigious"), 84 cl::init(false)); 85 86 namespace { 87 88 struct HexagonOperand; 89 90 class HexagonAsmParser : public MCTargetAsmParser { 91 92 HexagonTargetStreamer &getTargetStreamer() { 93 MCTargetStreamer &TS = *Parser.getStreamer().getTargetStreamer(); 94 return static_cast<HexagonTargetStreamer &>(TS); 95 } 96 97 MCAsmParser &Parser; 98 MCAssembler *Assembler; 99 MCInstrInfo const &MCII; 100 MCInst MCB; 101 bool InBrackets; 102 103 MCAsmParser &getParser() const { return Parser; } 104 MCAssembler *getAssembler() const { return Assembler; } 105 MCAsmLexer &getLexer() const { return Parser.getLexer(); } 106 107 bool equalIsAsmAssignment() override { return false; } 108 bool isLabel(AsmToken &Token) override; 109 110 void Warning(SMLoc L, const Twine &Msg) { Parser.Warning(L, Msg); } 111 bool Error(SMLoc L, const Twine &Msg) { return Parser.Error(L, Msg); } 112 bool ParseDirectiveFalign(unsigned Size, SMLoc L); 113 114 bool ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) override; 115 bool ParseDirectiveSubsection(SMLoc L); 116 bool ParseDirectiveValue(unsigned Size, SMLoc L); 117 bool ParseDirectiveComm(bool IsLocal, SMLoc L); 118 bool RegisterMatchesArch(unsigned MatchNum) const; 119 120 bool matchBundleOptions(); 121 bool handleNoncontigiousRegister(bool Contigious, SMLoc &Loc); 122 bool finishBundle(SMLoc IDLoc, MCStreamer &Out); 123 void canonicalizeImmediates(MCInst &MCI); 124 bool matchOneInstruction(MCInst &MCB, SMLoc IDLoc, 125 OperandVector &InstOperands, uint64_t &ErrorInfo, 126 bool MatchingInlineAsm); 127 128 bool MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 129 OperandVector &Operands, MCStreamer &Out, 130 uint64_t &ErrorInfo, 131 bool MatchingInlineAsm) override; 132 133 unsigned validateTargetOperandClass(MCParsedAsmOperand &Op, 134 unsigned Kind) override; 135 bool OutOfRange(SMLoc IDLoc, long long Val, long long Max); 136 int processInstruction(MCInst &Inst, OperandVector const &Operands, 137 SMLoc IDLoc); 138 139 // Check if we have an assembler and, if so, set the ELF e_header flags. 140 void chksetELFHeaderEFlags(unsigned flags) { 141 if (getAssembler()) 142 getAssembler()->setELFHeaderEFlags(flags); 143 } 144 145 unsigned matchRegister(StringRef Name); 146 147 /// @name Auto-generated Match Functions 148 /// { 149 150 #define GET_ASSEMBLER_HEADER 151 #include "HexagonGenAsmMatcher.inc" 152 153 /// } 154 155 public: 156 HexagonAsmParser(const MCSubtargetInfo &_STI, MCAsmParser &_Parser, 157 const MCInstrInfo &MII, const MCTargetOptions &Options) 158 : MCTargetAsmParser(Options, _STI), Parser(_Parser), 159 MCII (MII), MCB(HexagonMCInstrInfo::createBundle()), InBrackets(false) { 160 setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits())); 161 162 MCAsmParserExtension::Initialize(_Parser); 163 164 Assembler = nullptr; 165 // FIXME: need better way to detect AsmStreamer (upstream removed getKind()) 166 if (!Parser.getStreamer().hasRawTextSupport()) { 167 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer()); 168 Assembler = &MES->getAssembler(); 169 } 170 } 171 172 bool splitIdentifier(OperandVector &Operands); 173 bool parseOperand(OperandVector &Operands); 174 bool parseInstruction(OperandVector &Operands); 175 bool implicitExpressionLocation(OperandVector &Operands); 176 bool parseExpressionOrOperand(OperandVector &Operands); 177 bool parseExpression(MCExpr const *&Expr); 178 179 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, 180 SMLoc NameLoc, OperandVector &Operands) override { 181 llvm_unreachable("Unimplemented"); 182 } 183 184 bool ParseInstruction(ParseInstructionInfo &Info, StringRef Name, AsmToken ID, 185 OperandVector &Operands) override; 186 187 bool ParseDirective(AsmToken DirectiveID) override; 188 }; 189 190 /// HexagonOperand - Instances of this class represent a parsed Hexagon machine 191 /// instruction. 192 struct HexagonOperand : public MCParsedAsmOperand { 193 enum KindTy { Token, Immediate, Register } Kind; 194 195 SMLoc StartLoc, EndLoc; 196 197 struct TokTy { 198 const char *Data; 199 unsigned Length; 200 }; 201 202 struct RegTy { 203 unsigned RegNum; 204 }; 205 206 struct ImmTy { 207 const MCExpr *Val; 208 }; 209 210 struct InstTy { 211 OperandVector *SubInsts; 212 }; 213 214 union { 215 struct TokTy Tok; 216 struct RegTy Reg; 217 struct ImmTy Imm; 218 }; 219 220 HexagonOperand(KindTy K) : MCParsedAsmOperand(), Kind(K) {} 221 222 public: 223 HexagonOperand(const HexagonOperand &o) : MCParsedAsmOperand() { 224 Kind = o.Kind; 225 StartLoc = o.StartLoc; 226 EndLoc = o.EndLoc; 227 switch (Kind) { 228 case Register: 229 Reg = o.Reg; 230 break; 231 case Immediate: 232 Imm = o.Imm; 233 break; 234 case Token: 235 Tok = o.Tok; 236 break; 237 } 238 } 239 240 /// getStartLoc - Get the location of the first token of this operand. 241 SMLoc getStartLoc() const override { return StartLoc; } 242 243 /// getEndLoc - Get the location of the last token of this operand. 244 SMLoc getEndLoc() const override { return EndLoc; } 245 246 unsigned getReg() const override { 247 assert(Kind == Register && "Invalid access!"); 248 return Reg.RegNum; 249 } 250 251 const MCExpr *getImm() const { 252 assert(Kind == Immediate && "Invalid access!"); 253 return Imm.Val; 254 } 255 256 bool isToken() const override { return Kind == Token; } 257 bool isImm() const override { return Kind == Immediate; } 258 bool isMem() const override { llvm_unreachable("No isMem"); } 259 bool isReg() const override { return Kind == Register; } 260 261 bool CheckImmRange(int immBits, int zeroBits, bool isSigned, 262 bool isRelocatable, bool Extendable) const { 263 if (Kind == Immediate) { 264 const MCExpr *myMCExpr = &HexagonMCInstrInfo::getExpr(*getImm()); 265 if (HexagonMCInstrInfo::mustExtend(*Imm.Val) && !Extendable) 266 return false; 267 int64_t Res; 268 if (myMCExpr->evaluateAsAbsolute(Res)) { 269 int bits = immBits + zeroBits; 270 // Field bit range is zerobits + bits 271 // zeroBits must be 0 272 if (Res & ((1 << zeroBits) - 1)) 273 return false; 274 if (isSigned) { 275 if (Res < (1LL << (bits - 1)) && Res >= -(1LL << (bits - 1))) 276 return true; 277 } else { 278 if (bits == 64) 279 return true; 280 if (Res >= 0) 281 return ((uint64_t)Res < (uint64_t)(1ULL << bits)); 282 else { 283 const int64_t high_bit_set = 1ULL << 63; 284 const uint64_t mask = (high_bit_set >> (63 - bits)); 285 return (((uint64_t)Res & mask) == mask); 286 } 287 } 288 } else if (myMCExpr->getKind() == MCExpr::SymbolRef && isRelocatable) 289 return true; 290 else if (myMCExpr->getKind() == MCExpr::Binary || 291 myMCExpr->getKind() == MCExpr::Unary) 292 return true; 293 } 294 return false; 295 } 296 297 bool isa30_2Imm() const { return CheckImmRange(30, 2, true, true, true); } 298 bool isb30_2Imm() const { return CheckImmRange(30, 2, true, true, true); } 299 bool isb15_2Imm() const { return CheckImmRange(15, 2, true, true, false); } 300 bool isb13_2Imm() const { return CheckImmRange(13, 2, true, true, false); } 301 302 bool ism32_0Imm() const { return true; } 303 304 bool isf32Imm() const { return false; } 305 bool isf64Imm() const { return false; } 306 bool iss32_0Imm() const { return true; } 307 bool iss31_1Imm() const { return true; } 308 bool iss30_2Imm() const { return true; } 309 bool iss29_3Imm() const { return true; } 310 bool iss27_2Imm() const { return CheckImmRange(27, 2, true, true, false); } 311 bool iss10_0Imm() const { return CheckImmRange(10, 0, true, false, false); } 312 bool iss10_6Imm() const { return CheckImmRange(10, 6, true, false, false); } 313 bool iss9_0Imm() const { return CheckImmRange(9, 0, true, false, false); } 314 bool iss8_0Imm() const { return CheckImmRange(8, 0, true, false, false); } 315 bool iss8_0Imm64() const { return CheckImmRange(8, 0, true, true, false); } 316 bool iss7_0Imm() const { return CheckImmRange(7, 0, true, false, false); } 317 bool iss6_0Imm() const { return CheckImmRange(6, 0, true, false, false); } 318 bool iss6_3Imm() const { return CheckImmRange(6, 3, true, false, false); } 319 bool iss4_0Imm() const { return CheckImmRange(4, 0, true, false, false); } 320 bool iss4_1Imm() const { return CheckImmRange(4, 1, true, false, false); } 321 bool iss4_2Imm() const { return CheckImmRange(4, 2, true, false, false); } 322 bool iss4_3Imm() const { return CheckImmRange(4, 3, true, false, false); } 323 bool iss3_0Imm() const { return CheckImmRange(3, 0, true, false, false); } 324 325 bool isu64_0Imm() const { return CheckImmRange(64, 0, false, true, true); } 326 bool isu32_0Imm() const { return true; } 327 bool isu31_1Imm() const { return true; } 328 bool isu30_2Imm() const { return true; } 329 bool isu29_3Imm() const { return true; } 330 bool isu26_6Imm() const { return CheckImmRange(26, 6, false, true, false); } 331 bool isu16_0Imm() const { return CheckImmRange(16, 0, false, true, false); } 332 bool isu16_1Imm() const { return CheckImmRange(16, 1, false, true, false); } 333 bool isu16_2Imm() const { return CheckImmRange(16, 2, false, true, false); } 334 bool isu16_3Imm() const { return CheckImmRange(16, 3, false, true, false); } 335 bool isu11_3Imm() const { return CheckImmRange(11, 3, false, false, false); } 336 bool isu10_0Imm() const { return CheckImmRange(10, 0, false, false, false); } 337 bool isu9_0Imm() const { return CheckImmRange(9, 0, false, false, false); } 338 bool isu8_0Imm() const { return CheckImmRange(8, 0, false, false, false); } 339 bool isu7_0Imm() const { return CheckImmRange(7, 0, false, false, false); } 340 bool isu6_0Imm() const { return CheckImmRange(6, 0, false, false, false); } 341 bool isu6_1Imm() const { return CheckImmRange(6, 1, false, false, false); } 342 bool isu6_2Imm() const { return CheckImmRange(6, 2, false, false, false); } 343 bool isu6_3Imm() const { return CheckImmRange(6, 3, false, false, false); } 344 bool isu5_0Imm() const { return CheckImmRange(5, 0, false, false, false); } 345 bool isu5_2Imm() const { return CheckImmRange(5, 2, false, false, false); } 346 bool isu5_3Imm() const { return CheckImmRange(5, 3, false, false, false); } 347 bool isu4_0Imm() const { return CheckImmRange(4, 0, false, false, false); } 348 bool isu4_2Imm() const { return CheckImmRange(4, 2, false, false, false); } 349 bool isu3_0Imm() const { return CheckImmRange(3, 0, false, false, false); } 350 bool isu3_1Imm() const { return CheckImmRange(3, 1, false, false, false); } 351 bool isu2_0Imm() const { return CheckImmRange(2, 0, false, false, false); } 352 bool isu1_0Imm() const { return CheckImmRange(1, 0, false, false, false); } 353 354 bool isn1Const() const { 355 if (!isImm()) 356 return false; 357 int64_t Value; 358 if (!getImm()->evaluateAsAbsolute(Value)) 359 return false; 360 return Value == -1; 361 } 362 bool iss11_0Imm() const { 363 return CheckImmRange(11 + 26, 0, true, true, true); 364 } 365 bool iss11_1Imm() const { 366 return CheckImmRange(11 + 26, 1, true, true, true); 367 } 368 bool iss11_2Imm() const { 369 return CheckImmRange(11 + 26, 2, true, true, true); 370 } 371 bool iss11_3Imm() const { 372 return CheckImmRange(11 + 26, 3, true, true, true); 373 } 374 bool isu32_0MustExt() const { return isImm(); } 375 376 void addRegOperands(MCInst &Inst, unsigned N) const { 377 assert(N == 1 && "Invalid number of operands!"); 378 Inst.addOperand(MCOperand::createReg(getReg())); 379 } 380 381 void addImmOperands(MCInst &Inst, unsigned N) const { 382 assert(N == 1 && "Invalid number of operands!"); 383 Inst.addOperand(MCOperand::createExpr(getImm())); 384 } 385 386 void addSignedImmOperands(MCInst &Inst, unsigned N) const { 387 assert(N == 1 && "Invalid number of operands!"); 388 HexagonMCExpr *Expr = 389 const_cast<HexagonMCExpr *>(cast<HexagonMCExpr>(getImm())); 390 int64_t Value; 391 if (!Expr->evaluateAsAbsolute(Value)) { 392 Inst.addOperand(MCOperand::createExpr(Expr)); 393 return; 394 } 395 int64_t Extended = SignExtend64(Value, 32); 396 if ((Extended < 0) != (Value < 0)) 397 Expr->setSignMismatch(); 398 Inst.addOperand(MCOperand::createExpr(Expr)); 399 } 400 401 void addn1ConstOperands(MCInst &Inst, unsigned N) const { 402 addImmOperands(Inst, N); 403 } 404 405 StringRef getToken() const { 406 assert(Kind == Token && "Invalid access!"); 407 return StringRef(Tok.Data, Tok.Length); 408 } 409 410 void print(raw_ostream &OS) const override; 411 412 static std::unique_ptr<HexagonOperand> CreateToken(StringRef Str, SMLoc S) { 413 HexagonOperand *Op = new HexagonOperand(Token); 414 Op->Tok.Data = Str.data(); 415 Op->Tok.Length = Str.size(); 416 Op->StartLoc = S; 417 Op->EndLoc = S; 418 return std::unique_ptr<HexagonOperand>(Op); 419 } 420 421 static std::unique_ptr<HexagonOperand> CreateReg(unsigned RegNum, SMLoc S, 422 SMLoc E) { 423 HexagonOperand *Op = new HexagonOperand(Register); 424 Op->Reg.RegNum = RegNum; 425 Op->StartLoc = S; 426 Op->EndLoc = E; 427 return std::unique_ptr<HexagonOperand>(Op); 428 } 429 430 static std::unique_ptr<HexagonOperand> CreateImm(const MCExpr *Val, SMLoc S, 431 SMLoc E) { 432 HexagonOperand *Op = new HexagonOperand(Immediate); 433 Op->Imm.Val = Val; 434 Op->StartLoc = S; 435 Op->EndLoc = E; 436 return std::unique_ptr<HexagonOperand>(Op); 437 } 438 }; 439 440 } // end anonymous namespace 441 442 void HexagonOperand::print(raw_ostream &OS) const { 443 switch (Kind) { 444 case Immediate: 445 getImm()->print(OS, nullptr); 446 break; 447 case Register: 448 OS << "<register R"; 449 OS << getReg() << ">"; 450 break; 451 case Token: 452 OS << "'" << getToken() << "'"; 453 break; 454 } 455 } 456 457 bool HexagonAsmParser::finishBundle(SMLoc IDLoc, MCStreamer &Out) { 458 DEBUG(dbgs() << "Bundle:"); 459 DEBUG(MCB.dump_pretty(dbgs())); 460 DEBUG(dbgs() << "--\n"); 461 462 MCB.setLoc(IDLoc); 463 // Check the bundle for errors. 464 const MCRegisterInfo *RI = getContext().getRegisterInfo(); 465 HexagonMCChecker Check(getContext(), MCII, getSTI(), MCB, *RI); 466 467 bool CheckOk = HexagonMCInstrInfo::canonicalizePacket(MCII, getSTI(), 468 getContext(), MCB, 469 &Check); 470 471 if (CheckOk) { 472 if (HexagonMCInstrInfo::bundleSize(MCB) == 0) { 473 assert(!HexagonMCInstrInfo::isInnerLoop(MCB)); 474 assert(!HexagonMCInstrInfo::isOuterLoop(MCB)); 475 // Empty packets are valid yet aren't emitted 476 return false; 477 } 478 Out.EmitInstruction(MCB, getSTI()); 479 } else { 480 // If compounding and duplexing didn't reduce the size below 481 // 4 or less we have a packet that is too big. 482 if (HexagonMCInstrInfo::bundleSize(MCB) > HEXAGON_PACKET_SIZE) { 483 Error(IDLoc, "invalid instruction packet: out of slots"); 484 return true; // Error 485 } 486 } 487 488 return false; // No error 489 } 490 491 bool HexagonAsmParser::matchBundleOptions() { 492 MCAsmParser &Parser = getParser(); 493 while (true) { 494 if (!Parser.getTok().is(AsmToken::Colon)) 495 return false; 496 Lex(); 497 StringRef Option = Parser.getTok().getString(); 498 if (Option.compare_lower("endloop0") == 0) 499 HexagonMCInstrInfo::setInnerLoop(MCB); 500 else if (Option.compare_lower("endloop1") == 0) 501 HexagonMCInstrInfo::setOuterLoop(MCB); 502 else 503 return true; 504 Lex(); 505 } 506 } 507 508 // For instruction aliases, immediates are generated rather than 509 // MCConstantExpr. Convert them for uniform MCExpr. 510 // Also check for signed/unsigned mismatches and warn 511 void HexagonAsmParser::canonicalizeImmediates(MCInst &MCI) { 512 MCInst NewInst; 513 NewInst.setOpcode(MCI.getOpcode()); 514 for (MCOperand &I : MCI) 515 if (I.isImm()) { 516 int64_t Value (I.getImm()); 517 NewInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create( 518 MCConstantExpr::create(Value, getContext()), getContext()))); 519 } else { 520 if (I.isExpr() && cast<HexagonMCExpr>(I.getExpr())->signMismatch() && 521 WarnSignedMismatch) 522 Warning (MCI.getLoc(), "Signed/Unsigned mismatch"); 523 NewInst.addOperand(I); 524 } 525 MCI = NewInst; 526 } 527 528 bool HexagonAsmParser::matchOneInstruction(MCInst &MCI, SMLoc IDLoc, 529 OperandVector &InstOperands, 530 uint64_t &ErrorInfo, 531 bool MatchingInlineAsm) { 532 // Perform matching with tablegen asmmatcher generated function 533 int result = 534 MatchInstructionImpl(InstOperands, MCI, ErrorInfo, MatchingInlineAsm); 535 if (result == Match_Success) { 536 MCI.setLoc(IDLoc); 537 canonicalizeImmediates(MCI); 538 result = processInstruction(MCI, InstOperands, IDLoc); 539 540 DEBUG(dbgs() << "Insn:"); 541 DEBUG(MCI.dump_pretty(dbgs())); 542 DEBUG(dbgs() << "\n\n"); 543 544 MCI.setLoc(IDLoc); 545 } 546 547 // Create instruction operand for bundle instruction 548 // Break this into a separate function Code here is less readable 549 // Think about how to get an instruction error to report correctly. 550 // SMLoc will return the "{" 551 switch (result) { 552 default: 553 break; 554 case Match_Success: 555 return false; 556 case Match_MissingFeature: 557 return Error(IDLoc, "invalid instruction"); 558 case Match_MnemonicFail: 559 return Error(IDLoc, "unrecognized instruction"); 560 case Match_InvalidOperand: 561 SMLoc ErrorLoc = IDLoc; 562 if (ErrorInfo != ~0U) { 563 if (ErrorInfo >= InstOperands.size()) 564 return Error(IDLoc, "too few operands for instruction"); 565 566 ErrorLoc = (static_cast<HexagonOperand *>(InstOperands[ErrorInfo].get())) 567 ->getStartLoc(); 568 if (ErrorLoc == SMLoc()) 569 ErrorLoc = IDLoc; 570 } 571 return Error(ErrorLoc, "invalid operand for instruction"); 572 } 573 llvm_unreachable("Implement any new match types added!"); 574 } 575 576 bool HexagonAsmParser::MatchAndEmitInstruction(SMLoc IDLoc, unsigned &Opcode, 577 OperandVector &Operands, 578 MCStreamer &Out, 579 uint64_t &ErrorInfo, 580 bool MatchingInlineAsm) { 581 if (!InBrackets) { 582 MCB.clear(); 583 MCB.addOperand(MCOperand::createImm(0)); 584 } 585 HexagonOperand &FirstOperand = static_cast<HexagonOperand &>(*Operands[0]); 586 if (FirstOperand.isToken() && FirstOperand.getToken() == "{") { 587 assert(Operands.size() == 1 && "Brackets should be by themselves"); 588 if (InBrackets) { 589 getParser().Error(IDLoc, "Already in a packet"); 590 return true; 591 } 592 InBrackets = true; 593 return false; 594 } 595 if (FirstOperand.isToken() && FirstOperand.getToken() == "}") { 596 assert(Operands.size() == 1 && "Brackets should be by themselves"); 597 if (!InBrackets) { 598 getParser().Error(IDLoc, "Not in a packet"); 599 return true; 600 } 601 InBrackets = false; 602 if (matchBundleOptions()) 603 return true; 604 return finishBundle(IDLoc, Out); 605 } 606 MCInst *SubInst = new (getParser().getContext()) MCInst; 607 if (matchOneInstruction(*SubInst, IDLoc, Operands, ErrorInfo, 608 MatchingInlineAsm)) 609 return true; 610 HexagonMCInstrInfo::extendIfNeeded( 611 getParser().getContext(), MCII, MCB, *SubInst); 612 MCB.addOperand(MCOperand::createInst(SubInst)); 613 if (!InBrackets) 614 return finishBundle(IDLoc, Out); 615 return false; 616 } 617 618 /// ParseDirective parses the Hexagon specific directives 619 bool HexagonAsmParser::ParseDirective(AsmToken DirectiveID) { 620 StringRef IDVal = DirectiveID.getIdentifier(); 621 if ((IDVal.lower() == ".word") || (IDVal.lower() == ".4byte")) 622 return ParseDirectiveValue(4, DirectiveID.getLoc()); 623 if (IDVal.lower() == ".short" || IDVal.lower() == ".hword" || 624 IDVal.lower() == ".half") 625 return ParseDirectiveValue(2, DirectiveID.getLoc()); 626 if (IDVal.lower() == ".falign") 627 return ParseDirectiveFalign(256, DirectiveID.getLoc()); 628 if ((IDVal.lower() == ".lcomm") || (IDVal.lower() == ".lcommon")) 629 return ParseDirectiveComm(true, DirectiveID.getLoc()); 630 if ((IDVal.lower() == ".comm") || (IDVal.lower() == ".common")) 631 return ParseDirectiveComm(false, DirectiveID.getLoc()); 632 if (IDVal.lower() == ".subsection") 633 return ParseDirectiveSubsection(DirectiveID.getLoc()); 634 635 return true; 636 } 637 bool HexagonAsmParser::ParseDirectiveSubsection(SMLoc L) { 638 const MCExpr *Subsection = nullptr; 639 int64_t Res; 640 641 assert((getLexer().isNot(AsmToken::EndOfStatement)) && 642 "Invalid subsection directive"); 643 getParser().parseExpression(Subsection); 644 645 if (!Subsection->evaluateAsAbsolute(Res)) 646 return Error(L, "Cannot evaluate subsection number"); 647 648 if (getLexer().isNot(AsmToken::EndOfStatement)) 649 return TokError("unexpected token in directive"); 650 651 // 0-8192 is the hard-coded range in MCObjectStreamper.cpp, this keeps the 652 // negative subsections together and in the same order but at the opposite 653 // end of the section. Only legacy hexagon-gcc created assembly code 654 // used negative subsections. 655 if ((Res < 0) && (Res > -8193)) 656 Subsection = HexagonMCExpr::create( 657 MCConstantExpr::create(8192 + Res, getContext()), getContext()); 658 659 getStreamer().SubSection(Subsection); 660 return false; 661 } 662 663 /// ::= .falign [expression] 664 bool HexagonAsmParser::ParseDirectiveFalign(unsigned Size, SMLoc L) { 665 666 int64_t MaxBytesToFill = 15; 667 668 // if there is an argument 669 if (getLexer().isNot(AsmToken::EndOfStatement)) { 670 const MCExpr *Value; 671 SMLoc ExprLoc = L; 672 673 // Make sure we have a number (false is returned if expression is a number) 674 if (!getParser().parseExpression(Value)) { 675 // Make sure this is a number that is in range 676 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); 677 uint64_t IntValue = MCE->getValue(); 678 if (!isUIntN(Size, IntValue) && !isIntN(Size, IntValue)) 679 return Error(ExprLoc, "literal value out of range (256) for falign"); 680 MaxBytesToFill = IntValue; 681 Lex(); 682 } else { 683 return Error(ExprLoc, "not a valid expression for falign directive"); 684 } 685 } 686 687 getTargetStreamer().emitFAlign(16, MaxBytesToFill); 688 Lex(); 689 690 return false; 691 } 692 693 /// ::= .word [ expression (, expression)* ] 694 bool HexagonAsmParser::ParseDirectiveValue(unsigned Size, SMLoc L) { 695 if (getLexer().isNot(AsmToken::EndOfStatement)) { 696 while (true) { 697 const MCExpr *Value; 698 SMLoc ExprLoc = L; 699 if (getParser().parseExpression(Value)) 700 return true; 701 702 // Special case constant expressions to match code generator. 703 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 704 assert(Size <= 8 && "Invalid size"); 705 uint64_t IntValue = MCE->getValue(); 706 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) 707 return Error(ExprLoc, "literal value out of range for directive"); 708 getStreamer().EmitIntValue(IntValue, Size); 709 } else 710 getStreamer().EmitValue(Value, Size); 711 712 if (getLexer().is(AsmToken::EndOfStatement)) 713 break; 714 715 // FIXME: Improve diagnostic. 716 if (getLexer().isNot(AsmToken::Comma)) 717 return TokError("unexpected token in directive"); 718 Lex(); 719 } 720 } 721 722 Lex(); 723 return false; 724 } 725 726 // This is largely a copy of AsmParser's ParseDirectiveComm extended to 727 // accept a 3rd argument, AccessAlignment which indicates the smallest 728 // memory access made to the symbol, expressed in bytes. If no 729 // AccessAlignment is specified it defaults to the Alignment Value. 730 // Hexagon's .lcomm: 731 // .lcomm Symbol, Length, Alignment, AccessAlignment 732 bool HexagonAsmParser::ParseDirectiveComm(bool IsLocal, SMLoc Loc) { 733 // FIXME: need better way to detect if AsmStreamer (upstream removed 734 // getKind()) 735 if (getStreamer().hasRawTextSupport()) 736 return true; // Only object file output requires special treatment. 737 738 StringRef Name; 739 if (getParser().parseIdentifier(Name)) 740 return TokError("expected identifier in directive"); 741 // Handle the identifier as the key symbol. 742 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 743 744 if (getLexer().isNot(AsmToken::Comma)) 745 return TokError("unexpected token in directive"); 746 Lex(); 747 748 int64_t Size; 749 SMLoc SizeLoc = getLexer().getLoc(); 750 if (getParser().parseAbsoluteExpression(Size)) 751 return true; 752 753 int64_t ByteAlignment = 1; 754 SMLoc ByteAlignmentLoc; 755 if (getLexer().is(AsmToken::Comma)) { 756 Lex(); 757 ByteAlignmentLoc = getLexer().getLoc(); 758 if (getParser().parseAbsoluteExpression(ByteAlignment)) 759 return true; 760 if (!isPowerOf2_64(ByteAlignment)) 761 return Error(ByteAlignmentLoc, "alignment must be a power of 2"); 762 } 763 764 int64_t AccessAlignment = 0; 765 if (getLexer().is(AsmToken::Comma)) { 766 // The optional access argument specifies the size of the smallest memory 767 // access to be made to the symbol, expressed in bytes. 768 SMLoc AccessAlignmentLoc; 769 Lex(); 770 AccessAlignmentLoc = getLexer().getLoc(); 771 if (getParser().parseAbsoluteExpression(AccessAlignment)) 772 return true; 773 774 if (!isPowerOf2_64(AccessAlignment)) 775 return Error(AccessAlignmentLoc, "access alignment must be a power of 2"); 776 } 777 778 if (getLexer().isNot(AsmToken::EndOfStatement)) 779 return TokError("unexpected token in '.comm' or '.lcomm' directive"); 780 781 Lex(); 782 783 // NOTE: a size of zero for a .comm should create a undefined symbol 784 // but a size of .lcomm creates a bss symbol of size zero. 785 if (Size < 0) 786 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " 787 "be less than zero"); 788 789 // NOTE: The alignment in the directive is a power of 2 value, the assembler 790 // may internally end up wanting an alignment in bytes. 791 // FIXME: Diagnose overflow. 792 if (ByteAlignment < 0) 793 return Error(ByteAlignmentLoc, "invalid '.comm' or '.lcomm' directive " 794 "alignment, can't be less than zero"); 795 796 if (!Sym->isUndefined()) 797 return Error(Loc, "invalid symbol redefinition"); 798 799 HexagonMCELFStreamer &HexagonELFStreamer = 800 static_cast<HexagonMCELFStreamer &>(getStreamer()); 801 if (IsLocal) { 802 HexagonELFStreamer.HexagonMCEmitLocalCommonSymbol(Sym, Size, ByteAlignment, 803 AccessAlignment); 804 return false; 805 } 806 807 HexagonELFStreamer.HexagonMCEmitCommonSymbol(Sym, Size, ByteAlignment, 808 AccessAlignment); 809 return false; 810 } 811 812 // validate register against architecture 813 bool HexagonAsmParser::RegisterMatchesArch(unsigned MatchNum) const { 814 if (HexagonMCRegisterClasses[Hexagon::V62RegsRegClassID].contains(MatchNum)) 815 if (!getSTI().getFeatureBits()[Hexagon::ArchV62]) 816 return false; 817 return true; 818 } 819 820 // extern "C" void LLVMInitializeHexagonAsmLexer(); 821 822 /// Force static initialization. 823 extern "C" void LLVMInitializeHexagonAsmParser() { 824 RegisterMCAsmParser<HexagonAsmParser> X(getTheHexagonTarget()); 825 } 826 827 #define GET_MATCHER_IMPLEMENTATION 828 #define GET_REGISTER_MATCHER 829 #include "HexagonGenAsmMatcher.inc" 830 831 static bool previousEqual(OperandVector &Operands, size_t Index, 832 StringRef String) { 833 if (Index >= Operands.size()) 834 return false; 835 MCParsedAsmOperand &Operand = *Operands[Operands.size() - Index - 1]; 836 if (!Operand.isToken()) 837 return false; 838 return static_cast<HexagonOperand &>(Operand).getToken().equals_lower(String); 839 } 840 841 static bool previousIsLoop(OperandVector &Operands, size_t Index) { 842 return previousEqual(Operands, Index, "loop0") || 843 previousEqual(Operands, Index, "loop1") || 844 previousEqual(Operands, Index, "sp1loop0") || 845 previousEqual(Operands, Index, "sp2loop0") || 846 previousEqual(Operands, Index, "sp3loop0"); 847 } 848 849 bool HexagonAsmParser::splitIdentifier(OperandVector &Operands) { 850 AsmToken const &Token = getParser().getTok(); 851 StringRef String = Token.getString(); 852 SMLoc Loc = Token.getLoc(); 853 Lex(); 854 do { 855 std::pair<StringRef, StringRef> HeadTail = String.split('.'); 856 if (!HeadTail.first.empty()) 857 Operands.push_back(HexagonOperand::CreateToken(HeadTail.first, Loc)); 858 if (!HeadTail.second.empty()) 859 Operands.push_back(HexagonOperand::CreateToken( 860 String.substr(HeadTail.first.size(), 1), Loc)); 861 String = HeadTail.second; 862 } while (!String.empty()); 863 return false; 864 } 865 866 bool HexagonAsmParser::parseOperand(OperandVector &Operands) { 867 unsigned Register; 868 SMLoc Begin; 869 SMLoc End; 870 MCAsmLexer &Lexer = getLexer(); 871 if (!ParseRegister(Register, Begin, End)) { 872 if (!ErrorMissingParenthesis) 873 switch (Register) { 874 default: 875 break; 876 case Hexagon::P0: 877 case Hexagon::P1: 878 case Hexagon::P2: 879 case Hexagon::P3: 880 if (previousEqual(Operands, 0, "if")) { 881 if (WarnMissingParenthesis) 882 Warning (Begin, "Missing parenthesis around predicate register"); 883 static char const *LParen = "("; 884 static char const *RParen = ")"; 885 Operands.push_back(HexagonOperand::CreateToken(LParen, Begin)); 886 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End)); 887 const AsmToken &MaybeDotNew = Lexer.getTok(); 888 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) && 889 MaybeDotNew.getString().equals_lower(".new")) 890 splitIdentifier(Operands); 891 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin)); 892 return false; 893 } 894 if (previousEqual(Operands, 0, "!") && 895 previousEqual(Operands, 1, "if")) { 896 if (WarnMissingParenthesis) 897 Warning (Begin, "Missing parenthesis around predicate register"); 898 static char const *LParen = "("; 899 static char const *RParen = ")"; 900 Operands.insert(Operands.end () - 1, 901 HexagonOperand::CreateToken(LParen, Begin)); 902 Operands.push_back(HexagonOperand::CreateReg(Register, Begin, End)); 903 const AsmToken &MaybeDotNew = Lexer.getTok(); 904 if (MaybeDotNew.is(AsmToken::TokenKind::Identifier) && 905 MaybeDotNew.getString().equals_lower(".new")) 906 splitIdentifier(Operands); 907 Operands.push_back(HexagonOperand::CreateToken(RParen, Begin)); 908 return false; 909 } 910 break; 911 } 912 Operands.push_back(HexagonOperand::CreateReg( 913 Register, Begin, End)); 914 return false; 915 } 916 return splitIdentifier(Operands); 917 } 918 919 bool HexagonAsmParser::isLabel(AsmToken &Token) { 920 MCAsmLexer &Lexer = getLexer(); 921 AsmToken const &Second = Lexer.getTok(); 922 AsmToken Third = Lexer.peekTok(); 923 StringRef String = Token.getString(); 924 if (Token.is(AsmToken::TokenKind::LCurly) || 925 Token.is(AsmToken::TokenKind::RCurly)) 926 return false; 927 // special case for parsing vwhist256:sat 928 if (String.lower() == "vwhist256" && Second.is(AsmToken::Colon) && 929 Third.getString().lower() == "sat") 930 return false; 931 if (!Token.is(AsmToken::TokenKind::Identifier)) 932 return true; 933 if (!matchRegister(String.lower())) 934 return true; 935 (void)Second; 936 assert(Second.is(AsmToken::Colon)); 937 StringRef Raw (String.data(), Third.getString().data() - String.data() + 938 Third.getString().size()); 939 std::string Collapsed = Raw; 940 Collapsed.erase(llvm::remove_if(Collapsed, isspace), Collapsed.end()); 941 StringRef Whole = Collapsed; 942 std::pair<StringRef, StringRef> DotSplit = Whole.split('.'); 943 if (!matchRegister(DotSplit.first.lower())) 944 return true; 945 return false; 946 } 947 948 bool HexagonAsmParser::handleNoncontigiousRegister(bool Contigious, SMLoc &Loc) { 949 if (!Contigious && ErrorNoncontigiousRegister) { 950 Error(Loc, "Register name is not contigious"); 951 return true; 952 } 953 if (!Contigious && WarnNoncontigiousRegister) 954 Warning(Loc, "Register name is not contigious"); 955 return false; 956 } 957 958 bool HexagonAsmParser::ParseRegister(unsigned &RegNo, SMLoc &StartLoc, SMLoc &EndLoc) { 959 MCAsmLexer &Lexer = getLexer(); 960 StartLoc = getLexer().getLoc(); 961 SmallVector<AsmToken, 5> Lookahead; 962 StringRef RawString(Lexer.getTok().getString().data(), 0); 963 bool Again = Lexer.is(AsmToken::Identifier); 964 bool NeededWorkaround = false; 965 while (Again) { 966 AsmToken const &Token = Lexer.getTok(); 967 RawString = StringRef(RawString.data(), 968 Token.getString().data() - RawString.data () + 969 Token.getString().size()); 970 Lookahead.push_back(Token); 971 Lexer.Lex(); 972 bool Contigious = Lexer.getTok().getString().data() == 973 Lookahead.back().getString().data() + 974 Lookahead.back().getString().size(); 975 bool Type = Lexer.is(AsmToken::Identifier) || Lexer.is(AsmToken::Dot) || 976 Lexer.is(AsmToken::Integer) || Lexer.is(AsmToken::Real) || 977 Lexer.is(AsmToken::Colon); 978 bool Workaround = Lexer.is(AsmToken::Colon) || 979 Lookahead.back().is(AsmToken::Colon); 980 Again = (Contigious && Type) || (Workaround && Type); 981 NeededWorkaround = NeededWorkaround || (Again && !(Contigious && Type)); 982 } 983 std::string Collapsed = RawString; 984 Collapsed.erase(llvm::remove_if(Collapsed, isspace), Collapsed.end()); 985 StringRef FullString = Collapsed; 986 std::pair<StringRef, StringRef> DotSplit = FullString.split('.'); 987 unsigned DotReg = matchRegister(DotSplit.first.lower()); 988 if (DotReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) { 989 if (DotSplit.second.empty()) { 990 RegNo = DotReg; 991 EndLoc = Lexer.getLoc(); 992 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc)) 993 return true; 994 return false; 995 } else { 996 RegNo = DotReg; 997 size_t First = RawString.find('.'); 998 StringRef DotString (RawString.data() + First, RawString.size() - First); 999 Lexer.UnLex(AsmToken(AsmToken::Identifier, DotString)); 1000 EndLoc = Lexer.getLoc(); 1001 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc)) 1002 return true; 1003 return false; 1004 } 1005 } 1006 std::pair<StringRef, StringRef> ColonSplit = StringRef(FullString).split(':'); 1007 unsigned ColonReg = matchRegister(ColonSplit.first.lower()); 1008 if (ColonReg != Hexagon::NoRegister && RegisterMatchesArch(DotReg)) { 1009 Lexer.UnLex(Lookahead.back()); 1010 Lookahead.pop_back(); 1011 Lexer.UnLex(Lookahead.back()); 1012 Lookahead.pop_back(); 1013 RegNo = ColonReg; 1014 EndLoc = Lexer.getLoc(); 1015 if (handleNoncontigiousRegister(!NeededWorkaround, StartLoc)) 1016 return true; 1017 return false; 1018 } 1019 while (!Lookahead.empty()) { 1020 Lexer.UnLex(Lookahead.back()); 1021 Lookahead.pop_back(); 1022 } 1023 return true; 1024 } 1025 1026 bool HexagonAsmParser::implicitExpressionLocation(OperandVector &Operands) { 1027 if (previousEqual(Operands, 0, "call")) 1028 return true; 1029 if (previousEqual(Operands, 0, "jump")) 1030 if (!getLexer().getTok().is(AsmToken::Colon)) 1031 return true; 1032 if (previousEqual(Operands, 0, "(") && previousIsLoop(Operands, 1)) 1033 return true; 1034 if (previousEqual(Operands, 1, ":") && previousEqual(Operands, 2, "jump") && 1035 (previousEqual(Operands, 0, "nt") || previousEqual(Operands, 0, "t"))) 1036 return true; 1037 return false; 1038 } 1039 1040 bool HexagonAsmParser::parseExpression(MCExpr const *& Expr) { 1041 SmallVector<AsmToken, 4> Tokens; 1042 MCAsmLexer &Lexer = getLexer(); 1043 bool Done = false; 1044 static char const * Comma = ","; 1045 do { 1046 Tokens.emplace_back (Lexer.getTok()); 1047 Lex(); 1048 switch (Tokens.back().getKind()) 1049 { 1050 case AsmToken::TokenKind::Hash: 1051 if (Tokens.size () > 1) 1052 if ((Tokens.end () - 2)->getKind() == AsmToken::TokenKind::Plus) { 1053 Tokens.insert(Tokens.end() - 2, 1054 AsmToken(AsmToken::TokenKind::Comma, Comma)); 1055 Done = true; 1056 } 1057 break; 1058 case AsmToken::TokenKind::RCurly: 1059 case AsmToken::TokenKind::EndOfStatement: 1060 case AsmToken::TokenKind::Eof: 1061 Done = true; 1062 break; 1063 default: 1064 break; 1065 } 1066 } while (!Done); 1067 while (!Tokens.empty()) { 1068 Lexer.UnLex(Tokens.back()); 1069 Tokens.pop_back(); 1070 } 1071 return getParser().parseExpression(Expr); 1072 } 1073 1074 bool HexagonAsmParser::parseExpressionOrOperand(OperandVector &Operands) { 1075 if (implicitExpressionLocation(Operands)) { 1076 MCAsmParser &Parser = getParser(); 1077 SMLoc Loc = Parser.getLexer().getLoc(); 1078 MCExpr const *Expr = nullptr; 1079 bool Error = parseExpression(Expr); 1080 Expr = HexagonMCExpr::create(Expr, getContext()); 1081 if (!Error) 1082 Operands.push_back(HexagonOperand::CreateImm(Expr, Loc, Loc)); 1083 return Error; 1084 } 1085 return parseOperand(Operands); 1086 } 1087 1088 /// Parse an instruction. 1089 bool HexagonAsmParser::parseInstruction(OperandVector &Operands) { 1090 MCAsmParser &Parser = getParser(); 1091 MCAsmLexer &Lexer = getLexer(); 1092 while (true) { 1093 AsmToken const &Token = Parser.getTok(); 1094 switch (Token.getKind()) { 1095 case AsmToken::EndOfStatement: { 1096 Lex(); 1097 return false; 1098 } 1099 case AsmToken::LCurly: { 1100 if (!Operands.empty()) 1101 return true; 1102 Operands.push_back( 1103 HexagonOperand::CreateToken(Token.getString(), Token.getLoc())); 1104 Lex(); 1105 return false; 1106 } 1107 case AsmToken::RCurly: { 1108 if (Operands.empty()) { 1109 Operands.push_back( 1110 HexagonOperand::CreateToken(Token.getString(), Token.getLoc())); 1111 Lex(); 1112 } 1113 return false; 1114 } 1115 case AsmToken::Comma: { 1116 Lex(); 1117 continue; 1118 } 1119 case AsmToken::EqualEqual: 1120 case AsmToken::ExclaimEqual: 1121 case AsmToken::GreaterEqual: 1122 case AsmToken::GreaterGreater: 1123 case AsmToken::LessEqual: 1124 case AsmToken::LessLess: { 1125 Operands.push_back(HexagonOperand::CreateToken( 1126 Token.getString().substr(0, 1), Token.getLoc())); 1127 Operands.push_back(HexagonOperand::CreateToken( 1128 Token.getString().substr(1, 1), Token.getLoc())); 1129 Lex(); 1130 continue; 1131 } 1132 case AsmToken::Hash: { 1133 bool MustNotExtend = false; 1134 bool ImplicitExpression = implicitExpressionLocation(Operands); 1135 SMLoc ExprLoc = Lexer.getLoc(); 1136 if (!ImplicitExpression) 1137 Operands.push_back( 1138 HexagonOperand::CreateToken(Token.getString(), Token.getLoc())); 1139 Lex(); 1140 bool MustExtend = false; 1141 bool HiOnly = false; 1142 bool LoOnly = false; 1143 if (Lexer.is(AsmToken::Hash)) { 1144 Lex(); 1145 MustExtend = true; 1146 } else if (ImplicitExpression) 1147 MustNotExtend = true; 1148 AsmToken const &Token = Parser.getTok(); 1149 if (Token.is(AsmToken::Identifier)) { 1150 StringRef String = Token.getString(); 1151 if (String.lower() == "hi") { 1152 HiOnly = true; 1153 } else if (String.lower() == "lo") { 1154 LoOnly = true; 1155 } 1156 if (HiOnly || LoOnly) { 1157 AsmToken LParen = Lexer.peekTok(); 1158 if (!LParen.is(AsmToken::LParen)) { 1159 HiOnly = false; 1160 LoOnly = false; 1161 } else { 1162 Lex(); 1163 } 1164 } 1165 } 1166 MCExpr const *Expr = nullptr; 1167 if (parseExpression(Expr)) 1168 return true; 1169 int64_t Value; 1170 MCContext &Context = Parser.getContext(); 1171 assert(Expr != nullptr); 1172 if (Expr->evaluateAsAbsolute(Value)) { 1173 if (HiOnly) 1174 Expr = MCBinaryExpr::createLShr( 1175 Expr, MCConstantExpr::create(16, Context), Context); 1176 if (HiOnly || LoOnly) 1177 Expr = MCBinaryExpr::createAnd(Expr, 1178 MCConstantExpr::create(0xffff, Context), 1179 Context); 1180 } else { 1181 MCValue Value; 1182 if (Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) { 1183 if (!Value.isAbsolute()) { 1184 switch(Value.getAccessVariant()) { 1185 case MCSymbolRefExpr::VariantKind::VK_TPREL: 1186 case MCSymbolRefExpr::VariantKind::VK_DTPREL: 1187 // Don't lazy extend these expression variants 1188 MustNotExtend = !MustExtend; 1189 break; 1190 default: 1191 break; 1192 } 1193 } 1194 } 1195 } 1196 Expr = HexagonMCExpr::create(Expr, Context); 1197 HexagonMCInstrInfo::setMustNotExtend(*Expr, MustNotExtend); 1198 HexagonMCInstrInfo::setMustExtend(*Expr, MustExtend); 1199 std::unique_ptr<HexagonOperand> Operand = 1200 HexagonOperand::CreateImm(Expr, ExprLoc, ExprLoc); 1201 Operands.push_back(std::move(Operand)); 1202 continue; 1203 } 1204 default: 1205 break; 1206 } 1207 if (parseExpressionOrOperand(Operands)) 1208 return true; 1209 } 1210 } 1211 1212 bool HexagonAsmParser::ParseInstruction(ParseInstructionInfo &Info, 1213 StringRef Name, 1214 AsmToken ID, 1215 OperandVector &Operands) { 1216 getLexer().UnLex(ID); 1217 return parseInstruction(Operands); 1218 } 1219 1220 static MCInst makeCombineInst(int opCode, MCOperand &Rdd, 1221 MCOperand &MO1, MCOperand &MO2) { 1222 MCInst TmpInst; 1223 TmpInst.setOpcode(opCode); 1224 TmpInst.addOperand(Rdd); 1225 TmpInst.addOperand(MO1); 1226 TmpInst.addOperand(MO2); 1227 1228 return TmpInst; 1229 } 1230 1231 // Define this matcher function after the auto-generated include so we 1232 // have the match class enum definitions. 1233 unsigned HexagonAsmParser::validateTargetOperandClass(MCParsedAsmOperand &AsmOp, 1234 unsigned Kind) { 1235 HexagonOperand *Op = static_cast<HexagonOperand *>(&AsmOp); 1236 1237 switch (Kind) { 1238 case MCK_0: { 1239 int64_t Value; 1240 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 0 1241 ? Match_Success 1242 : Match_InvalidOperand; 1243 } 1244 case MCK_1: { 1245 int64_t Value; 1246 return Op->isImm() && Op->Imm.Val->evaluateAsAbsolute(Value) && Value == 1 1247 ? Match_Success 1248 : Match_InvalidOperand; 1249 } 1250 } 1251 if (Op->Kind == HexagonOperand::Token && Kind != InvalidMatchClass) { 1252 StringRef myStringRef = StringRef(Op->Tok.Data, Op->Tok.Length); 1253 if (matchTokenString(myStringRef.lower()) == (MatchClassKind)Kind) 1254 return Match_Success; 1255 if (matchTokenString(myStringRef.upper()) == (MatchClassKind)Kind) 1256 return Match_Success; 1257 } 1258 1259 DEBUG(dbgs() << "Unmatched Operand:"); 1260 DEBUG(Op->dump()); 1261 DEBUG(dbgs() << "\n"); 1262 1263 return Match_InvalidOperand; 1264 } 1265 1266 // FIXME: Calls to OutOfRange shoudl propagate failure up to parseStatement. 1267 bool HexagonAsmParser::OutOfRange(SMLoc IDLoc, long long Val, long long Max) { 1268 std::string errStr; 1269 raw_string_ostream ES(errStr); 1270 ES << "value " << Val << "(" << format_hex(Val, 0) << ") out of range: "; 1271 if (Max >= 0) 1272 ES << "0-" << Max; 1273 else 1274 ES << Max << "-" << (-Max - 1); 1275 return Parser.printError(IDLoc, ES.str()); 1276 } 1277 1278 int HexagonAsmParser::processInstruction(MCInst &Inst, 1279 OperandVector const &Operands, 1280 SMLoc IDLoc) { 1281 MCContext &Context = getParser().getContext(); 1282 const MCRegisterInfo *RI = getContext().getRegisterInfo(); 1283 std::string r = "r"; 1284 std::string v = "v"; 1285 std::string Colon = ":"; 1286 1287 bool is32bit = false; // used to distinguish between CONST32 and CONST64 1288 switch (Inst.getOpcode()) { 1289 default: 1290 break; 1291 1292 case Hexagon::A2_iconst: { 1293 Inst.setOpcode(Hexagon::A2_addi); 1294 MCOperand Reg = Inst.getOperand(0); 1295 MCOperand S27 = Inst.getOperand(1); 1296 HexagonMCInstrInfo::setMustNotExtend(*S27.getExpr()); 1297 HexagonMCInstrInfo::setS27_2_reloc(*S27.getExpr()); 1298 Inst.clear(); 1299 Inst.addOperand(Reg); 1300 Inst.addOperand(MCOperand::createReg(Hexagon::R0)); 1301 Inst.addOperand(S27); 1302 break; 1303 } 1304 case Hexagon::M4_mpyrr_addr: 1305 case Hexagon::S4_addi_asl_ri: 1306 case Hexagon::S4_addi_lsr_ri: 1307 case Hexagon::S4_andi_asl_ri: 1308 case Hexagon::S4_andi_lsr_ri: 1309 case Hexagon::S4_ori_asl_ri: 1310 case Hexagon::S4_ori_lsr_ri: 1311 case Hexagon::S4_or_andix: 1312 case Hexagon::S4_subi_asl_ri: 1313 case Hexagon::S4_subi_lsr_ri: { 1314 MCOperand &Ry = Inst.getOperand(0); 1315 MCOperand &src = Inst.getOperand(2); 1316 if (RI->getEncodingValue(Ry.getReg()) != RI->getEncodingValue(src.getReg())) 1317 return Match_InvalidOperand; 1318 break; 1319 } 1320 1321 case Hexagon::C2_cmpgei: { 1322 MCOperand &MO = Inst.getOperand(2); 1323 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub( 1324 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context)); 1325 Inst.setOpcode(Hexagon::C2_cmpgti); 1326 break; 1327 } 1328 1329 case Hexagon::C2_cmpgeui: { 1330 MCOperand &MO = Inst.getOperand(2); 1331 int64_t Value; 1332 bool Success = MO.getExpr()->evaluateAsAbsolute(Value); 1333 (void)Success; 1334 assert(Success && "Assured by matcher"); 1335 if (Value == 0) { 1336 MCInst TmpInst; 1337 MCOperand &Pd = Inst.getOperand(0); 1338 MCOperand &Rt = Inst.getOperand(1); 1339 TmpInst.setOpcode(Hexagon::C2_cmpeq); 1340 TmpInst.addOperand(Pd); 1341 TmpInst.addOperand(Rt); 1342 TmpInst.addOperand(Rt); 1343 Inst = TmpInst; 1344 } else { 1345 MO.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub( 1346 MO.getExpr(), MCConstantExpr::create(1, Context), Context), Context)); 1347 Inst.setOpcode(Hexagon::C2_cmpgtui); 1348 } 1349 break; 1350 } 1351 1352 // Translate a "$Rdd = $Rss" to "$Rdd = combine($Rs, $Rt)" 1353 case Hexagon::A2_tfrp: { 1354 MCOperand &MO = Inst.getOperand(1); 1355 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg()); 1356 std::string R1 = r + utostr(RegPairNum + 1); 1357 StringRef Reg1(R1); 1358 MO.setReg(matchRegister(Reg1)); 1359 // Add a new operand for the second register in the pair. 1360 std::string R2 = r + utostr(RegPairNum); 1361 StringRef Reg2(R2); 1362 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2))); 1363 Inst.setOpcode(Hexagon::A2_combinew); 1364 break; 1365 } 1366 1367 case Hexagon::A2_tfrpt: 1368 case Hexagon::A2_tfrpf: { 1369 MCOperand &MO = Inst.getOperand(2); 1370 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg()); 1371 std::string R1 = r + utostr(RegPairNum + 1); 1372 StringRef Reg1(R1); 1373 MO.setReg(matchRegister(Reg1)); 1374 // Add a new operand for the second register in the pair. 1375 std::string R2 = r + utostr(RegPairNum); 1376 StringRef Reg2(R2); 1377 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2))); 1378 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrpt) 1379 ? Hexagon::C2_ccombinewt 1380 : Hexagon::C2_ccombinewf); 1381 break; 1382 } 1383 case Hexagon::A2_tfrptnew: 1384 case Hexagon::A2_tfrpfnew: { 1385 MCOperand &MO = Inst.getOperand(2); 1386 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg()); 1387 std::string R1 = r + utostr(RegPairNum + 1); 1388 StringRef Reg1(R1); 1389 MO.setReg(matchRegister(Reg1)); 1390 // Add a new operand for the second register in the pair. 1391 std::string R2 = r + utostr(RegPairNum); 1392 StringRef Reg2(R2); 1393 Inst.addOperand(MCOperand::createReg(matchRegister(Reg2))); 1394 Inst.setOpcode((Inst.getOpcode() == Hexagon::A2_tfrptnew) 1395 ? Hexagon::C2_ccombinewnewt 1396 : Hexagon::C2_ccombinewnewf); 1397 break; 1398 } 1399 1400 // Translate a "$Vdd = $Vss" to "$Vdd = vcombine($Vs, $Vt)" 1401 case Hexagon::V6_vassignp: { 1402 MCOperand &MO = Inst.getOperand(1); 1403 unsigned int RegPairNum = RI->getEncodingValue(MO.getReg()); 1404 std::string R1 = v + utostr(RegPairNum + 1); 1405 MO.setReg(MatchRegisterName(R1)); 1406 // Add a new operand for the second register in the pair. 1407 std::string R2 = v + utostr(RegPairNum); 1408 Inst.addOperand(MCOperand::createReg(MatchRegisterName(R2))); 1409 Inst.setOpcode(Hexagon::V6_vcombine); 1410 break; 1411 } 1412 1413 // Translate a "$Rx = CONST32(#imm)" to "$Rx = memw(gp+#LABEL) " 1414 case Hexagon::CONST32: 1415 is32bit = true; 1416 LLVM_FALLTHROUGH; 1417 // Translate a "$Rx:y = CONST64(#imm)" to "$Rx:y = memd(gp+#LABEL) " 1418 case Hexagon::CONST64: 1419 // FIXME: need better way to detect AsmStreamer (upstream removed getKind()) 1420 if (!Parser.getStreamer().hasRawTextSupport()) { 1421 MCELFStreamer *MES = static_cast<MCELFStreamer *>(&Parser.getStreamer()); 1422 MCOperand &MO_1 = Inst.getOperand(1); 1423 MCOperand &MO_0 = Inst.getOperand(0); 1424 1425 // push section onto section stack 1426 MES->PushSection(); 1427 1428 std::string myCharStr; 1429 MCSectionELF *mySection; 1430 1431 // check if this as an immediate or a symbol 1432 int64_t Value; 1433 bool Absolute = MO_1.getExpr()->evaluateAsAbsolute(Value); 1434 if (Absolute) { 1435 // Create a new section - one for each constant 1436 // Some or all of the zeros are replaced with the given immediate. 1437 if (is32bit) { 1438 std::string myImmStr = utohexstr(static_cast<uint32_t>(Value)); 1439 myCharStr = StringRef(".gnu.linkonce.l4.CONST_00000000") 1440 .drop_back(myImmStr.size()) 1441 .str() + 1442 myImmStr; 1443 } else { 1444 std::string myImmStr = utohexstr(Value); 1445 myCharStr = StringRef(".gnu.linkonce.l8.CONST_0000000000000000") 1446 .drop_back(myImmStr.size()) 1447 .str() + 1448 myImmStr; 1449 } 1450 1451 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS, 1452 ELF::SHF_ALLOC | ELF::SHF_WRITE); 1453 } else if (MO_1.isExpr()) { 1454 // .lita - for expressions 1455 myCharStr = ".lita"; 1456 mySection = getContext().getELFSection(myCharStr, ELF::SHT_PROGBITS, 1457 ELF::SHF_ALLOC | ELF::SHF_WRITE); 1458 } else 1459 llvm_unreachable("unexpected type of machine operand!"); 1460 1461 MES->SwitchSection(mySection); 1462 unsigned byteSize = is32bit ? 4 : 8; 1463 getStreamer().EmitCodeAlignment(byteSize, byteSize); 1464 1465 MCSymbol *Sym; 1466 1467 // for symbols, get rid of prepended ".gnu.linkonce.lx." 1468 1469 // emit symbol if needed 1470 if (Absolute) { 1471 Sym = getContext().getOrCreateSymbol(StringRef(myCharStr.c_str() + 16)); 1472 if (Sym->isUndefined()) { 1473 getStreamer().EmitLabel(Sym); 1474 getStreamer().EmitSymbolAttribute(Sym, MCSA_Global); 1475 getStreamer().EmitIntValue(Value, byteSize); 1476 } 1477 } else if (MO_1.isExpr()) { 1478 const char *StringStart = nullptr; 1479 const char *StringEnd = nullptr; 1480 if (*Operands[4]->getStartLoc().getPointer() == '#') { 1481 StringStart = Operands[5]->getStartLoc().getPointer(); 1482 StringEnd = Operands[6]->getStartLoc().getPointer(); 1483 } else { // no pound 1484 StringStart = Operands[4]->getStartLoc().getPointer(); 1485 StringEnd = Operands[5]->getStartLoc().getPointer(); 1486 } 1487 1488 unsigned size = StringEnd - StringStart; 1489 std::string DotConst = ".CONST_"; 1490 Sym = getContext().getOrCreateSymbol(DotConst + 1491 StringRef(StringStart, size)); 1492 1493 if (Sym->isUndefined()) { 1494 // case where symbol is not yet defined: emit symbol 1495 getStreamer().EmitLabel(Sym); 1496 getStreamer().EmitSymbolAttribute(Sym, MCSA_Local); 1497 getStreamer().EmitValue(MO_1.getExpr(), 4); 1498 } 1499 } else 1500 llvm_unreachable("unexpected type of machine operand!"); 1501 1502 MES->PopSection(); 1503 1504 if (Sym) { 1505 MCInst TmpInst; 1506 if (is32bit) // 32 bit 1507 TmpInst.setOpcode(Hexagon::L2_loadrigp); 1508 else // 64 bit 1509 TmpInst.setOpcode(Hexagon::L2_loadrdgp); 1510 1511 TmpInst.addOperand(MO_0); 1512 TmpInst.addOperand(MCOperand::createExpr(HexagonMCExpr::create( 1513 MCSymbolRefExpr::create(Sym, getContext()), getContext()))); 1514 Inst = TmpInst; 1515 } 1516 } 1517 break; 1518 1519 // Translate a "$Rdd = #-imm" to "$Rdd = combine(#[-1,0], #-imm)" 1520 case Hexagon::A2_tfrpi: { 1521 MCOperand &Rdd = Inst.getOperand(0); 1522 MCOperand &MO = Inst.getOperand(1); 1523 int64_t Value; 1524 int sVal = (MO.getExpr()->evaluateAsAbsolute(Value) && Value < 0) ? -1 : 0; 1525 MCOperand imm(MCOperand::createExpr( 1526 HexagonMCExpr::create(MCConstantExpr::create(sVal, Context), Context))); 1527 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, imm, MO); 1528 break; 1529 } 1530 1531 // Translate a "$Rdd = [#]#imm" to "$Rdd = combine(#, [#]#imm)" 1532 case Hexagon::TFRI64_V4: { 1533 MCOperand &Rdd = Inst.getOperand(0); 1534 MCOperand &MO = Inst.getOperand(1); 1535 int64_t Value; 1536 if (MO.getExpr()->evaluateAsAbsolute(Value)) { 1537 int s8 = Hi_32(Value); 1538 if (!isInt<8>(s8)) 1539 OutOfRange(IDLoc, s8, -128); 1540 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create( 1541 MCConstantExpr::create(s8, Context), Context))); // upper 32 1542 auto Expr = HexagonMCExpr::create( 1543 MCConstantExpr::create(Lo_32(Value), Context), Context); 1544 HexagonMCInstrInfo::setMustExtend(*Expr, HexagonMCInstrInfo::mustExtend(*MO.getExpr())); 1545 MCOperand imm2(MCOperand::createExpr(Expr)); // lower 32 1546 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, imm2); 1547 } else { 1548 MCOperand imm(MCOperand::createExpr(HexagonMCExpr::create( 1549 MCConstantExpr::create(0, Context), Context))); // upper 32 1550 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, imm, MO); 1551 } 1552 break; 1553 } 1554 1555 // Handle $Rdd = combine(##imm, #imm)" 1556 case Hexagon::TFRI64_V2_ext: { 1557 MCOperand &Rdd = Inst.getOperand(0); 1558 MCOperand &MO1 = Inst.getOperand(1); 1559 MCOperand &MO2 = Inst.getOperand(2); 1560 int64_t Value; 1561 if (MO2.getExpr()->evaluateAsAbsolute(Value)) { 1562 int s8 = Value; 1563 if (s8 < -128 || s8 > 127) 1564 OutOfRange(IDLoc, s8, -128); 1565 } 1566 Inst = makeCombineInst(Hexagon::A2_combineii, Rdd, MO1, MO2); 1567 break; 1568 } 1569 1570 // Handle $Rdd = combine(#imm, ##imm)" 1571 case Hexagon::A4_combineii: { 1572 MCOperand &Rdd = Inst.getOperand(0); 1573 MCOperand &MO1 = Inst.getOperand(1); 1574 int64_t Value; 1575 if (MO1.getExpr()->evaluateAsAbsolute(Value)) { 1576 int s8 = Value; 1577 if (s8 < -128 || s8 > 127) 1578 OutOfRange(IDLoc, s8, -128); 1579 } 1580 MCOperand &MO2 = Inst.getOperand(2); 1581 Inst = makeCombineInst(Hexagon::A4_combineii, Rdd, MO1, MO2); 1582 break; 1583 } 1584 1585 case Hexagon::S2_tableidxb_goodsyntax: 1586 Inst.setOpcode(Hexagon::S2_tableidxb); 1587 break; 1588 1589 case Hexagon::S2_tableidxh_goodsyntax: { 1590 MCInst TmpInst; 1591 MCOperand &Rx = Inst.getOperand(0); 1592 MCOperand &_dst_ = Inst.getOperand(1); 1593 MCOperand &Rs = Inst.getOperand(2); 1594 MCOperand &Imm4 = Inst.getOperand(3); 1595 MCOperand &Imm6 = Inst.getOperand(4); 1596 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub( 1597 Imm6.getExpr(), MCConstantExpr::create(1, Context), Context), Context)); 1598 TmpInst.setOpcode(Hexagon::S2_tableidxh); 1599 TmpInst.addOperand(Rx); 1600 TmpInst.addOperand(_dst_); 1601 TmpInst.addOperand(Rs); 1602 TmpInst.addOperand(Imm4); 1603 TmpInst.addOperand(Imm6); 1604 Inst = TmpInst; 1605 break; 1606 } 1607 1608 case Hexagon::S2_tableidxw_goodsyntax: { 1609 MCInst TmpInst; 1610 MCOperand &Rx = Inst.getOperand(0); 1611 MCOperand &_dst_ = Inst.getOperand(1); 1612 MCOperand &Rs = Inst.getOperand(2); 1613 MCOperand &Imm4 = Inst.getOperand(3); 1614 MCOperand &Imm6 = Inst.getOperand(4); 1615 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub( 1616 Imm6.getExpr(), MCConstantExpr::create(2, Context), Context), Context)); 1617 TmpInst.setOpcode(Hexagon::S2_tableidxw); 1618 TmpInst.addOperand(Rx); 1619 TmpInst.addOperand(_dst_); 1620 TmpInst.addOperand(Rs); 1621 TmpInst.addOperand(Imm4); 1622 TmpInst.addOperand(Imm6); 1623 Inst = TmpInst; 1624 break; 1625 } 1626 1627 case Hexagon::S2_tableidxd_goodsyntax: { 1628 MCInst TmpInst; 1629 MCOperand &Rx = Inst.getOperand(0); 1630 MCOperand &_dst_ = Inst.getOperand(1); 1631 MCOperand &Rs = Inst.getOperand(2); 1632 MCOperand &Imm4 = Inst.getOperand(3); 1633 MCOperand &Imm6 = Inst.getOperand(4); 1634 Imm6.setExpr(HexagonMCExpr::create(MCBinaryExpr::createSub( 1635 Imm6.getExpr(), MCConstantExpr::create(3, Context), Context), Context)); 1636 TmpInst.setOpcode(Hexagon::S2_tableidxd); 1637 TmpInst.addOperand(Rx); 1638 TmpInst.addOperand(_dst_); 1639 TmpInst.addOperand(Rs); 1640 TmpInst.addOperand(Imm4); 1641 TmpInst.addOperand(Imm6); 1642 Inst = TmpInst; 1643 break; 1644 } 1645 1646 case Hexagon::M2_mpyui: 1647 Inst.setOpcode(Hexagon::M2_mpyi); 1648 break; 1649 case Hexagon::M2_mpysmi: { 1650 MCInst TmpInst; 1651 MCOperand &Rd = Inst.getOperand(0); 1652 MCOperand &Rs = Inst.getOperand(1); 1653 MCOperand &Imm = Inst.getOperand(2); 1654 int64_t Value; 1655 MCExpr const &Expr = *Imm.getExpr(); 1656 bool Absolute = Expr.evaluateAsAbsolute(Value); 1657 assert(Absolute); 1658 (void)Absolute; 1659 if (!HexagonMCInstrInfo::mustExtend(Expr)) { 1660 if (Value < 0 && Value > -256) { 1661 Imm.setExpr(HexagonMCExpr::create( 1662 MCConstantExpr::create(Value * -1, Context), Context)); 1663 TmpInst.setOpcode(Hexagon::M2_mpysin); 1664 } else if (Value < 256 && Value >= 0) 1665 TmpInst.setOpcode(Hexagon::M2_mpysip); 1666 else 1667 return Match_InvalidOperand; 1668 } else { 1669 if (Value >= 0) 1670 TmpInst.setOpcode(Hexagon::M2_mpysip); 1671 else 1672 return Match_InvalidOperand; 1673 } 1674 TmpInst.addOperand(Rd); 1675 TmpInst.addOperand(Rs); 1676 TmpInst.addOperand(Imm); 1677 Inst = TmpInst; 1678 break; 1679 } 1680 1681 case Hexagon::S2_asr_i_r_rnd_goodsyntax: { 1682 MCOperand &Imm = Inst.getOperand(2); 1683 MCInst TmpInst; 1684 int64_t Value; 1685 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value); 1686 assert(Absolute); 1687 (void)Absolute; 1688 if (Value == 0) { // convert to $Rd = $Rs 1689 TmpInst.setOpcode(Hexagon::A2_tfr); 1690 MCOperand &Rd = Inst.getOperand(0); 1691 MCOperand &Rs = Inst.getOperand(1); 1692 TmpInst.addOperand(Rd); 1693 TmpInst.addOperand(Rs); 1694 } else { 1695 Imm.setExpr(HexagonMCExpr::create( 1696 MCBinaryExpr::createSub(Imm.getExpr(), 1697 MCConstantExpr::create(1, Context), Context), 1698 Context)); 1699 TmpInst.setOpcode(Hexagon::S2_asr_i_r_rnd); 1700 MCOperand &Rd = Inst.getOperand(0); 1701 MCOperand &Rs = Inst.getOperand(1); 1702 TmpInst.addOperand(Rd); 1703 TmpInst.addOperand(Rs); 1704 TmpInst.addOperand(Imm); 1705 } 1706 Inst = TmpInst; 1707 break; 1708 } 1709 1710 case Hexagon::S2_asr_i_p_rnd_goodsyntax: { 1711 MCOperand &Rdd = Inst.getOperand(0); 1712 MCOperand &Rss = Inst.getOperand(1); 1713 MCOperand &Imm = Inst.getOperand(2); 1714 int64_t Value; 1715 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value); 1716 assert(Absolute); 1717 (void)Absolute; 1718 if (Value == 0) { // convert to $Rdd = combine ($Rs[0], $Rs[1]) 1719 MCInst TmpInst; 1720 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg()); 1721 std::string R1 = r + utostr(RegPairNum + 1); 1722 StringRef Reg1(R1); 1723 Rss.setReg(matchRegister(Reg1)); 1724 // Add a new operand for the second register in the pair. 1725 std::string R2 = r + utostr(RegPairNum); 1726 StringRef Reg2(R2); 1727 TmpInst.setOpcode(Hexagon::A2_combinew); 1728 TmpInst.addOperand(Rdd); 1729 TmpInst.addOperand(Rss); 1730 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2))); 1731 Inst = TmpInst; 1732 } else { 1733 Imm.setExpr(HexagonMCExpr::create( 1734 MCBinaryExpr::createSub(Imm.getExpr(), 1735 MCConstantExpr::create(1, Context), Context), 1736 Context)); 1737 Inst.setOpcode(Hexagon::S2_asr_i_p_rnd); 1738 } 1739 break; 1740 } 1741 1742 case Hexagon::A4_boundscheck: { 1743 MCOperand &Rs = Inst.getOperand(1); 1744 unsigned int RegNum = RI->getEncodingValue(Rs.getReg()); 1745 if (RegNum & 1) { // Odd mapped to raw:hi, regpair is rodd:odd-1, like r3:2 1746 Inst.setOpcode(Hexagon::A4_boundscheck_hi); 1747 std::string Name = r + utostr(RegNum) + Colon + utostr(RegNum - 1); 1748 StringRef RegPair = Name; 1749 Rs.setReg(matchRegister(RegPair)); 1750 } else { // raw:lo 1751 Inst.setOpcode(Hexagon::A4_boundscheck_lo); 1752 std::string Name = r + utostr(RegNum + 1) + Colon + utostr(RegNum); 1753 StringRef RegPair = Name; 1754 Rs.setReg(matchRegister(RegPair)); 1755 } 1756 break; 1757 } 1758 1759 case Hexagon::A2_addsp: { 1760 MCOperand &Rs = Inst.getOperand(1); 1761 unsigned int RegNum = RI->getEncodingValue(Rs.getReg()); 1762 if (RegNum & 1) { // Odd mapped to raw:hi 1763 Inst.setOpcode(Hexagon::A2_addsph); 1764 std::string Name = r + utostr(RegNum) + Colon + utostr(RegNum - 1); 1765 StringRef RegPair = Name; 1766 Rs.setReg(matchRegister(RegPair)); 1767 } else { // Even mapped raw:lo 1768 Inst.setOpcode(Hexagon::A2_addspl); 1769 std::string Name = r + utostr(RegNum + 1) + Colon + utostr(RegNum); 1770 StringRef RegPair = Name; 1771 Rs.setReg(matchRegister(RegPair)); 1772 } 1773 break; 1774 } 1775 1776 case Hexagon::M2_vrcmpys_s1: { 1777 MCOperand &Rt = Inst.getOperand(2); 1778 unsigned int RegNum = RI->getEncodingValue(Rt.getReg()); 1779 if (RegNum & 1) { // Odd mapped to sat:raw:hi 1780 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_h); 1781 std::string Name = r + utostr(RegNum) + Colon + utostr(RegNum - 1); 1782 StringRef RegPair = Name; 1783 Rt.setReg(matchRegister(RegPair)); 1784 } else { // Even mapped sat:raw:lo 1785 Inst.setOpcode(Hexagon::M2_vrcmpys_s1_l); 1786 std::string Name = r + utostr(RegNum + 1) + Colon + utostr(RegNum); 1787 StringRef RegPair = Name; 1788 Rt.setReg(matchRegister(RegPair)); 1789 } 1790 break; 1791 } 1792 1793 case Hexagon::M2_vrcmpys_acc_s1: { 1794 MCInst TmpInst; 1795 MCOperand &Rxx = Inst.getOperand(0); 1796 MCOperand &Rss = Inst.getOperand(2); 1797 MCOperand &Rt = Inst.getOperand(3); 1798 unsigned int RegNum = RI->getEncodingValue(Rt.getReg()); 1799 if (RegNum & 1) { // Odd mapped to sat:raw:hi 1800 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_h); 1801 std::string Name = r + utostr(RegNum) + Colon + utostr(RegNum - 1); 1802 StringRef RegPair = Name; 1803 Rt.setReg(matchRegister(RegPair)); 1804 } else { // Even mapped sat:raw:lo 1805 TmpInst.setOpcode(Hexagon::M2_vrcmpys_acc_s1_l); 1806 std::string Name = r + utostr(RegNum + 1) + Colon + utostr(RegNum); 1807 StringRef RegPair = Name; 1808 Rt.setReg(matchRegister(RegPair)); 1809 } 1810 // Registers are in different positions 1811 TmpInst.addOperand(Rxx); 1812 TmpInst.addOperand(Rxx); 1813 TmpInst.addOperand(Rss); 1814 TmpInst.addOperand(Rt); 1815 Inst = TmpInst; 1816 break; 1817 } 1818 1819 case Hexagon::M2_vrcmpys_s1rp: { 1820 MCOperand &Rt = Inst.getOperand(2); 1821 unsigned int RegNum = RI->getEncodingValue(Rt.getReg()); 1822 if (RegNum & 1) { // Odd mapped to rnd:sat:raw:hi 1823 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_h); 1824 std::string Name = r + utostr(RegNum) + Colon + utostr(RegNum - 1); 1825 StringRef RegPair = Name; 1826 Rt.setReg(matchRegister(RegPair)); 1827 } else { // Even mapped rnd:sat:raw:lo 1828 Inst.setOpcode(Hexagon::M2_vrcmpys_s1rp_l); 1829 std::string Name = r + utostr(RegNum + 1) + Colon + utostr(RegNum); 1830 StringRef RegPair = Name; 1831 Rt.setReg(matchRegister(RegPair)); 1832 } 1833 break; 1834 } 1835 1836 case Hexagon::S5_asrhub_rnd_sat_goodsyntax: { 1837 MCOperand &Imm = Inst.getOperand(2); 1838 int64_t Value; 1839 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value); 1840 assert(Absolute); 1841 (void)Absolute; 1842 if (Value == 0) 1843 Inst.setOpcode(Hexagon::S2_vsathub); 1844 else { 1845 Imm.setExpr(HexagonMCExpr::create( 1846 MCBinaryExpr::createSub(Imm.getExpr(), 1847 MCConstantExpr::create(1, Context), Context), 1848 Context)); 1849 Inst.setOpcode(Hexagon::S5_asrhub_rnd_sat); 1850 } 1851 break; 1852 } 1853 1854 case Hexagon::S5_vasrhrnd_goodsyntax: { 1855 MCOperand &Rdd = Inst.getOperand(0); 1856 MCOperand &Rss = Inst.getOperand(1); 1857 MCOperand &Imm = Inst.getOperand(2); 1858 int64_t Value; 1859 bool Absolute = Imm.getExpr()->evaluateAsAbsolute(Value); 1860 assert(Absolute); 1861 (void)Absolute; 1862 if (Value == 0) { 1863 MCInst TmpInst; 1864 unsigned int RegPairNum = RI->getEncodingValue(Rss.getReg()); 1865 std::string R1 = r + utostr(RegPairNum + 1); 1866 StringRef Reg1(R1); 1867 Rss.setReg(matchRegister(Reg1)); 1868 // Add a new operand for the second register in the pair. 1869 std::string R2 = r + utostr(RegPairNum); 1870 StringRef Reg2(R2); 1871 TmpInst.setOpcode(Hexagon::A2_combinew); 1872 TmpInst.addOperand(Rdd); 1873 TmpInst.addOperand(Rss); 1874 TmpInst.addOperand(MCOperand::createReg(matchRegister(Reg2))); 1875 Inst = TmpInst; 1876 } else { 1877 Imm.setExpr(HexagonMCExpr::create( 1878 MCBinaryExpr::createSub(Imm.getExpr(), 1879 MCConstantExpr::create(1, Context), Context), 1880 Context)); 1881 Inst.setOpcode(Hexagon::S5_vasrhrnd); 1882 } 1883 break; 1884 } 1885 1886 case Hexagon::A2_not: { 1887 MCInst TmpInst; 1888 MCOperand &Rd = Inst.getOperand(0); 1889 MCOperand &Rs = Inst.getOperand(1); 1890 TmpInst.setOpcode(Hexagon::A2_subri); 1891 TmpInst.addOperand(Rd); 1892 TmpInst.addOperand(MCOperand::createExpr( 1893 HexagonMCExpr::create(MCConstantExpr::create(-1, Context), Context))); 1894 TmpInst.addOperand(Rs); 1895 Inst = TmpInst; 1896 break; 1897 } 1898 case Hexagon::PS_loadrubabs: 1899 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(1).getExpr())) 1900 Inst.setOpcode(Hexagon::L2_loadrubgp); 1901 break; 1902 case Hexagon::PS_loadrbabs: 1903 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(1).getExpr())) 1904 Inst.setOpcode(Hexagon::L2_loadrbgp); 1905 break; 1906 case Hexagon::PS_loadruhabs: 1907 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(1).getExpr())) 1908 Inst.setOpcode(Hexagon::L2_loadruhgp); 1909 break; 1910 case Hexagon::PS_loadrhabs: 1911 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(1).getExpr())) 1912 Inst.setOpcode(Hexagon::L2_loadrhgp); 1913 break; 1914 case Hexagon::PS_loadriabs: 1915 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(1).getExpr())) 1916 Inst.setOpcode(Hexagon::L2_loadrigp); 1917 break; 1918 case Hexagon::PS_loadrdabs: 1919 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(1).getExpr())) 1920 Inst.setOpcode(Hexagon::L2_loadrdgp); 1921 break; 1922 case Hexagon::PS_storerbabs: 1923 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1924 Inst.setOpcode(Hexagon::S2_storerbgp); 1925 break; 1926 case Hexagon::PS_storerhabs: 1927 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1928 Inst.setOpcode(Hexagon::S2_storerhgp); 1929 break; 1930 case Hexagon::PS_storerfabs: 1931 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1932 Inst.setOpcode(Hexagon::S2_storerfgp); 1933 break; 1934 case Hexagon::PS_storeriabs: 1935 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1936 Inst.setOpcode(Hexagon::S2_storerigp); 1937 break; 1938 case Hexagon::PS_storerdabs: 1939 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1940 Inst.setOpcode(Hexagon::S2_storerdgp); 1941 break; 1942 case Hexagon::PS_storerbnewabs: 1943 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1944 Inst.setOpcode(Hexagon::S2_storerbnewgp); 1945 break; 1946 case Hexagon::PS_storerhnewabs: 1947 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1948 Inst.setOpcode(Hexagon::S2_storerhnewgp); 1949 break; 1950 case Hexagon::PS_storerinewabs: 1951 if (!HexagonMCInstrInfo::mustExtend(*Inst.getOperand(0).getExpr())) 1952 Inst.setOpcode(Hexagon::S2_storerinewgp); 1953 break; 1954 case Hexagon::A2_zxtb: { 1955 Inst.setOpcode(Hexagon::A2_andir); 1956 Inst.addOperand(MCOperand::createExpr(MCConstantExpr::create(255, Context))); 1957 break; 1958 } 1959 } // switch 1960 1961 return Match_Success; 1962 } 1963 1964 unsigned HexagonAsmParser::matchRegister(StringRef Name) { 1965 if (unsigned Reg = MatchRegisterName(Name)) 1966 return Reg; 1967 return MatchRegisterAltName(Name); 1968 } 1969