1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This class implements the parser for assembly files. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ADT/APFloat.h" 14 #include "llvm/ADT/APInt.h" 15 #include "llvm/ADT/ArrayRef.h" 16 #include "llvm/ADT/None.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/ADT/SmallString.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/StringExtras.h" 21 #include "llvm/ADT/StringMap.h" 22 #include "llvm/ADT/StringRef.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/BinaryFormat/Dwarf.h" 25 #include "llvm/DebugInfo/CodeView/SymbolRecord.h" 26 #include "llvm/MC/MCAsmInfo.h" 27 #include "llvm/MC/MCCodeView.h" 28 #include "llvm/MC/MCContext.h" 29 #include "llvm/MC/MCDirectives.h" 30 #include "llvm/MC/MCDwarf.h" 31 #include "llvm/MC/MCExpr.h" 32 #include "llvm/MC/MCInstPrinter.h" 33 #include "llvm/MC/MCInstrDesc.h" 34 #include "llvm/MC/MCInstrInfo.h" 35 #include "llvm/MC/MCObjectFileInfo.h" 36 #include "llvm/MC/MCParser/AsmCond.h" 37 #include "llvm/MC/MCParser/AsmLexer.h" 38 #include "llvm/MC/MCParser/MCAsmLexer.h" 39 #include "llvm/MC/MCParser/MCAsmParser.h" 40 #include "llvm/MC/MCParser/MCAsmParserExtension.h" 41 #include "llvm/MC/MCParser/MCAsmParserUtils.h" 42 #include "llvm/MC/MCParser/MCParsedAsmOperand.h" 43 #include "llvm/MC/MCParser/MCTargetAsmParser.h" 44 #include "llvm/MC/MCRegisterInfo.h" 45 #include "llvm/MC/MCSection.h" 46 #include "llvm/MC/MCStreamer.h" 47 #include "llvm/MC/MCSymbol.h" 48 #include "llvm/MC/MCTargetOptions.h" 49 #include "llvm/MC/MCValue.h" 50 #include "llvm/Support/Casting.h" 51 #include "llvm/Support/CommandLine.h" 52 #include "llvm/Support/ErrorHandling.h" 53 #include "llvm/Support/MD5.h" 54 #include "llvm/Support/MathExtras.h" 55 #include "llvm/Support/MemoryBuffer.h" 56 #include "llvm/Support/SMLoc.h" 57 #include "llvm/Support/SourceMgr.h" 58 #include "llvm/Support/raw_ostream.h" 59 #include <algorithm> 60 #include <cassert> 61 #include <cctype> 62 #include <climits> 63 #include <cstddef> 64 #include <cstdint> 65 #include <deque> 66 #include <memory> 67 #include <sstream> 68 #include <string> 69 #include <tuple> 70 #include <utility> 71 #include <vector> 72 73 using namespace llvm; 74 75 MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default; 76 77 static cl::opt<unsigned> AsmMacroMaxNestingDepth( 78 "asm-macro-max-nesting-depth", cl::init(20), cl::Hidden, 79 cl::desc("The maximum nesting depth allowed for assembly macros.")); 80 81 namespace { 82 83 /// Helper types for tracking macro definitions. 84 typedef std::vector<AsmToken> MCAsmMacroArgument; 85 typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; 86 87 /// Helper class for storing information about an active macro 88 /// instantiation. 89 struct MacroInstantiation { 90 /// The location of the instantiation. 91 SMLoc InstantiationLoc; 92 93 /// The buffer where parsing should resume upon instantiation completion. 94 int ExitBuffer; 95 96 /// The location where parsing should resume upon instantiation completion. 97 SMLoc ExitLoc; 98 99 /// The depth of TheCondStack at the start of the instantiation. 100 size_t CondStackDepth; 101 102 public: 103 MacroInstantiation(SMLoc IL, int EB, SMLoc EL, size_t CondStackDepth); 104 }; 105 106 struct ParseStatementInfo { 107 /// The parsed operands from the last parsed statement. 108 SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; 109 110 /// The opcode from the last parsed instruction. 111 unsigned Opcode = ~0U; 112 113 /// Was there an error parsing the inline assembly? 114 bool ParseError = false; 115 116 SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr; 117 118 ParseStatementInfo() = delete; 119 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) 120 : AsmRewrites(rewrites) {} 121 }; 122 123 /// The concrete assembly parser instance. 124 class AsmParser : public MCAsmParser { 125 private: 126 AsmLexer Lexer; 127 MCContext &Ctx; 128 MCStreamer &Out; 129 const MCAsmInfo &MAI; 130 SourceMgr &SrcMgr; 131 SourceMgr::DiagHandlerTy SavedDiagHandler; 132 void *SavedDiagContext; 133 std::unique_ptr<MCAsmParserExtension> PlatformParser; 134 135 /// This is the current buffer index we're lexing from as managed by the 136 /// SourceMgr object. 137 unsigned CurBuffer; 138 139 AsmCond TheCondState; 140 std::vector<AsmCond> TheCondStack; 141 142 /// maps directive names to handler methods in parser 143 /// extensions. Extensions register themselves in this map by calling 144 /// addDirectiveHandler. 145 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; 146 147 /// Stack of active macro instantiations. 148 std::vector<MacroInstantiation*> ActiveMacros; 149 150 /// List of bodies of anonymous macros. 151 std::deque<MCAsmMacro> MacroLikeBodies; 152 153 /// Boolean tracking whether macro substitution is enabled. 154 unsigned MacrosEnabledFlag : 1; 155 156 /// Keeps track of how many .macro's have been instantiated. 157 unsigned NumOfMacroInstantiations; 158 159 /// The values from the last parsed cpp hash file line comment if any. 160 struct CppHashInfoTy { 161 StringRef Filename; 162 int64_t LineNumber; 163 SMLoc Loc; 164 unsigned Buf; 165 CppHashInfoTy() : Filename(), LineNumber(0), Loc(), Buf(0) {} 166 }; 167 CppHashInfoTy CppHashInfo; 168 169 /// The filename from the first cpp hash file line comment, if any. 170 StringRef FirstCppHashFilename; 171 172 /// List of forward directional labels for diagnosis at the end. 173 SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; 174 175 /// AssemblerDialect. ~OU means unset value and use value provided by MAI. 176 unsigned AssemblerDialect = ~0U; 177 178 /// is Darwin compatibility enabled? 179 bool IsDarwin = false; 180 181 /// Are we parsing ms-style inline assembly? 182 bool ParsingInlineAsm = false; 183 184 /// Did we already inform the user about inconsistent MD5 usage? 185 bool ReportedInconsistentMD5 = false; 186 187 // Is alt macro mode enabled. 188 bool AltMacroMode = false; 189 190 public: 191 AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, 192 const MCAsmInfo &MAI, unsigned CB); 193 AsmParser(const AsmParser &) = delete; 194 AsmParser &operator=(const AsmParser &) = delete; 195 ~AsmParser() override; 196 197 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; 198 199 void addDirectiveHandler(StringRef Directive, 200 ExtensionDirectiveHandler Handler) override { 201 ExtensionDirectiveMap[Directive] = Handler; 202 } 203 204 void addAliasForDirective(StringRef Directive, StringRef Alias) override { 205 DirectiveKindMap[Directive] = DirectiveKindMap[Alias]; 206 } 207 208 /// @name MCAsmParser Interface 209 /// { 210 211 SourceMgr &getSourceManager() override { return SrcMgr; } 212 MCAsmLexer &getLexer() override { return Lexer; } 213 MCContext &getContext() override { return Ctx; } 214 MCStreamer &getStreamer() override { return Out; } 215 216 CodeViewContext &getCVContext() { return Ctx.getCVContext(); } 217 218 unsigned getAssemblerDialect() override { 219 if (AssemblerDialect == ~0U) 220 return MAI.getAssemblerDialect(); 221 else 222 return AssemblerDialect; 223 } 224 void setAssemblerDialect(unsigned i) override { 225 AssemblerDialect = i; 226 } 227 228 void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override; 229 bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override; 230 bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override; 231 232 const AsmToken &Lex() override; 233 234 void setParsingInlineAsm(bool V) override { 235 ParsingInlineAsm = V; 236 // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and 237 // hex integer literals. 238 Lexer.setLexMasmIntegers(V); 239 } 240 bool isParsingInlineAsm() override { return ParsingInlineAsm; } 241 242 bool parseMSInlineAsm(void *AsmLoc, std::string &AsmString, 243 unsigned &NumOutputs, unsigned &NumInputs, 244 SmallVectorImpl<std::pair<void *,bool>> &OpDecls, 245 SmallVectorImpl<std::string> &Constraints, 246 SmallVectorImpl<std::string> &Clobbers, 247 const MCInstrInfo *MII, const MCInstPrinter *IP, 248 MCAsmParserSemaCallback &SI) override; 249 250 bool parseExpression(const MCExpr *&Res); 251 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override; 252 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) override; 253 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override; 254 bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, 255 SMLoc &EndLoc) override; 256 bool parseAbsoluteExpression(int64_t &Res) override; 257 258 /// Parse a floating point expression using the float \p Semantics 259 /// and set \p Res to the value. 260 bool parseRealValue(const fltSemantics &Semantics, APInt &Res); 261 262 /// Parse an identifier or string (as a quoted identifier) 263 /// and set \p Res to the identifier contents. 264 bool parseIdentifier(StringRef &Res) override; 265 void eatToEndOfStatement() override; 266 267 bool checkForValidSection() override; 268 269 /// } 270 271 private: 272 bool parseStatement(ParseStatementInfo &Info, 273 MCAsmParserSemaCallback *SI); 274 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites); 275 bool parseCppHashLineFilenameComment(SMLoc L); 276 277 void checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, StringRef Body, 278 ArrayRef<MCAsmMacroParameter> Parameters); 279 bool expandMacro(raw_svector_ostream &OS, StringRef Body, 280 ArrayRef<MCAsmMacroParameter> Parameters, 281 ArrayRef<MCAsmMacroArgument> A, bool EnableAtPseudoVariable, 282 SMLoc L); 283 284 /// Are macros enabled in the parser? 285 bool areMacrosEnabled() {return MacrosEnabledFlag;} 286 287 /// Control a flag in the parser that enables or disables macros. 288 void setMacrosEnabled(bool Flag) {MacrosEnabledFlag = Flag;} 289 290 /// Are we inside a macro instantiation? 291 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();} 292 293 /// Handle entry to macro instantiation. 294 /// 295 /// \param M The macro. 296 /// \param NameLoc Instantiation location. 297 bool handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc); 298 299 /// Handle exit from macro instantiation. 300 void handleMacroExit(); 301 302 /// Extract AsmTokens for a macro argument. 303 bool parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg); 304 305 /// Parse all macro arguments for a given macro. 306 bool parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A); 307 308 void printMacroInstantiations(); 309 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg, 310 SMRange Range = None) const { 311 ArrayRef<SMRange> Ranges(Range); 312 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges); 313 } 314 static void DiagHandler(const SMDiagnostic &Diag, void *Context); 315 316 /// Should we emit DWARF describing this assembler source? (Returns false if 317 /// the source has .file directives, which means we don't want to generate 318 /// info describing the assembler source itself.) 319 bool enabledGenDwarfForAssembly(); 320 321 /// Enter the specified file. This returns true on failure. 322 bool enterIncludeFile(const std::string &Filename); 323 324 /// Process the specified file for the .incbin directive. 325 /// This returns true on failure. 326 bool processIncbinFile(const std::string &Filename, int64_t Skip = 0, 327 const MCExpr *Count = nullptr, SMLoc Loc = SMLoc()); 328 329 /// Reset the current lexer position to that given by \p Loc. The 330 /// current token is not set; clients should ensure Lex() is called 331 /// subsequently. 332 /// 333 /// \param InBuffer If not 0, should be the known buffer id that contains the 334 /// location. 335 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0); 336 337 /// Parse up to the end of statement and a return the contents from the 338 /// current token until the end of the statement; the current token on exit 339 /// will be either the EndOfStatement or EOF. 340 StringRef parseStringToEndOfStatement() override; 341 342 /// Parse until the end of a statement or a comma is encountered, 343 /// return the contents from the current token up to the end or comma. 344 StringRef parseStringToComma(); 345 346 bool parseAssignment(StringRef Name, bool allow_redef, 347 bool NoDeadStrip = false); 348 349 unsigned getBinOpPrecedence(AsmToken::TokenKind K, 350 MCBinaryExpr::Opcode &Kind); 351 352 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc); 353 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc); 354 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc); 355 356 bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc); 357 358 bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName); 359 bool parseCVFileId(int64_t &FileId, StringRef DirectiveName); 360 361 // Generic (target and platform independent) directive parsing. 362 enum DirectiveKind { 363 DK_NO_DIRECTIVE, // Placeholder 364 DK_SET, 365 DK_EQU, 366 DK_EQUIV, 367 DK_ASCII, 368 DK_ASCIZ, 369 DK_STRING, 370 DK_BYTE, 371 DK_SHORT, 372 DK_RELOC, 373 DK_VALUE, 374 DK_2BYTE, 375 DK_LONG, 376 DK_INT, 377 DK_4BYTE, 378 DK_QUAD, 379 DK_8BYTE, 380 DK_OCTA, 381 DK_DC, 382 DK_DC_A, 383 DK_DC_B, 384 DK_DC_D, 385 DK_DC_L, 386 DK_DC_S, 387 DK_DC_W, 388 DK_DC_X, 389 DK_DCB, 390 DK_DCB_B, 391 DK_DCB_D, 392 DK_DCB_L, 393 DK_DCB_S, 394 DK_DCB_W, 395 DK_DCB_X, 396 DK_DS, 397 DK_DS_B, 398 DK_DS_D, 399 DK_DS_L, 400 DK_DS_P, 401 DK_DS_S, 402 DK_DS_W, 403 DK_DS_X, 404 DK_SINGLE, 405 DK_FLOAT, 406 DK_DOUBLE, 407 DK_ALIGN, 408 DK_ALIGN32, 409 DK_BALIGN, 410 DK_BALIGNW, 411 DK_BALIGNL, 412 DK_P2ALIGN, 413 DK_P2ALIGNW, 414 DK_P2ALIGNL, 415 DK_ORG, 416 DK_FILL, 417 DK_ENDR, 418 DK_BUNDLE_ALIGN_MODE, 419 DK_BUNDLE_LOCK, 420 DK_BUNDLE_UNLOCK, 421 DK_ZERO, 422 DK_EXTERN, 423 DK_GLOBL, 424 DK_GLOBAL, 425 DK_LAZY_REFERENCE, 426 DK_NO_DEAD_STRIP, 427 DK_SYMBOL_RESOLVER, 428 DK_PRIVATE_EXTERN, 429 DK_REFERENCE, 430 DK_WEAK_DEFINITION, 431 DK_WEAK_REFERENCE, 432 DK_WEAK_DEF_CAN_BE_HIDDEN, 433 DK_COLD, 434 DK_COMM, 435 DK_COMMON, 436 DK_LCOMM, 437 DK_ABORT, 438 DK_INCLUDE, 439 DK_INCBIN, 440 DK_CODE16, 441 DK_CODE16GCC, 442 DK_REPT, 443 DK_IRP, 444 DK_IRPC, 445 DK_IF, 446 DK_IFEQ, 447 DK_IFGE, 448 DK_IFGT, 449 DK_IFLE, 450 DK_IFLT, 451 DK_IFNE, 452 DK_IFB, 453 DK_IFNB, 454 DK_IFC, 455 DK_IFEQS, 456 DK_IFNC, 457 DK_IFNES, 458 DK_IFDEF, 459 DK_IFNDEF, 460 DK_IFNOTDEF, 461 DK_ELSEIF, 462 DK_ELSE, 463 DK_ENDIF, 464 DK_SPACE, 465 DK_SKIP, 466 DK_FILE, 467 DK_LINE, 468 DK_LOC, 469 DK_STABS, 470 DK_CV_FILE, 471 DK_CV_FUNC_ID, 472 DK_CV_INLINE_SITE_ID, 473 DK_CV_LOC, 474 DK_CV_LINETABLE, 475 DK_CV_INLINE_LINETABLE, 476 DK_CV_DEF_RANGE, 477 DK_CV_STRINGTABLE, 478 DK_CV_STRING, 479 DK_CV_FILECHECKSUMS, 480 DK_CV_FILECHECKSUM_OFFSET, 481 DK_CV_FPO_DATA, 482 DK_CFI_SECTIONS, 483 DK_CFI_STARTPROC, 484 DK_CFI_ENDPROC, 485 DK_CFI_DEF_CFA, 486 DK_CFI_DEF_CFA_OFFSET, 487 DK_CFI_ADJUST_CFA_OFFSET, 488 DK_CFI_DEF_CFA_REGISTER, 489 DK_CFI_OFFSET, 490 DK_CFI_REL_OFFSET, 491 DK_CFI_PERSONALITY, 492 DK_CFI_LSDA, 493 DK_CFI_REMEMBER_STATE, 494 DK_CFI_RESTORE_STATE, 495 DK_CFI_SAME_VALUE, 496 DK_CFI_RESTORE, 497 DK_CFI_ESCAPE, 498 DK_CFI_RETURN_COLUMN, 499 DK_CFI_SIGNAL_FRAME, 500 DK_CFI_UNDEFINED, 501 DK_CFI_REGISTER, 502 DK_CFI_WINDOW_SAVE, 503 DK_CFI_B_KEY_FRAME, 504 DK_MACROS_ON, 505 DK_MACROS_OFF, 506 DK_ALTMACRO, 507 DK_NOALTMACRO, 508 DK_MACRO, 509 DK_EXITM, 510 DK_ENDM, 511 DK_ENDMACRO, 512 DK_PURGEM, 513 DK_SLEB128, 514 DK_ULEB128, 515 DK_ERR, 516 DK_ERROR, 517 DK_WARNING, 518 DK_PRINT, 519 DK_ADDRSIG, 520 DK_ADDRSIG_SYM, 521 DK_END 522 }; 523 524 /// Maps directive name --> DirectiveKind enum, for 525 /// directives parsed by this class. 526 StringMap<DirectiveKind> DirectiveKindMap; 527 528 // Codeview def_range type parsing. 529 enum CVDefRangeType { 530 CVDR_DEFRANGE = 0, // Placeholder 531 CVDR_DEFRANGE_REGISTER, 532 CVDR_DEFRANGE_FRAMEPOINTER_REL, 533 CVDR_DEFRANGE_SUBFIELD_REGISTER, 534 CVDR_DEFRANGE_REGISTER_REL 535 }; 536 537 /// Maps Codeview def_range types --> CVDefRangeType enum, for 538 /// Codeview def_range types parsed by this class. 539 StringMap<CVDefRangeType> CVDefRangeTypeMap; 540 541 // ".ascii", ".asciz", ".string" 542 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated); 543 bool parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" 544 bool parseDirectiveValue(StringRef IDVal, 545 unsigned Size); // ".byte", ".long", ... 546 bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... 547 bool parseDirectiveRealValue(StringRef IDVal, 548 const fltSemantics &); // ".single", ... 549 bool parseDirectiveFill(); // ".fill" 550 bool parseDirectiveZero(); // ".zero" 551 // ".set", ".equ", ".equiv" 552 bool parseDirectiveSet(StringRef IDVal, bool allow_redef); 553 bool parseDirectiveOrg(); // ".org" 554 // ".align{,32}", ".p2align{,w,l}" 555 bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); 556 557 // ".file", ".line", ".loc", ".stabs" 558 bool parseDirectiveFile(SMLoc DirectiveLoc); 559 bool parseDirectiveLine(); 560 bool parseDirectiveLoc(); 561 bool parseDirectiveStabs(); 562 563 // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable", 564 // ".cv_inline_linetable", ".cv_def_range", ".cv_string" 565 bool parseDirectiveCVFile(); 566 bool parseDirectiveCVFuncId(); 567 bool parseDirectiveCVInlineSiteId(); 568 bool parseDirectiveCVLoc(); 569 bool parseDirectiveCVLinetable(); 570 bool parseDirectiveCVInlineLinetable(); 571 bool parseDirectiveCVDefRange(); 572 bool parseDirectiveCVString(); 573 bool parseDirectiveCVStringTable(); 574 bool parseDirectiveCVFileChecksums(); 575 bool parseDirectiveCVFileChecksumOffset(); 576 bool parseDirectiveCVFPOData(); 577 578 // .cfi directives 579 bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); 580 bool parseDirectiveCFIWindowSave(); 581 bool parseDirectiveCFISections(); 582 bool parseDirectiveCFIStartProc(); 583 bool parseDirectiveCFIEndProc(); 584 bool parseDirectiveCFIDefCfaOffset(); 585 bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); 586 bool parseDirectiveCFIAdjustCfaOffset(); 587 bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); 588 bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); 589 bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); 590 bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); 591 bool parseDirectiveCFIRememberState(); 592 bool parseDirectiveCFIRestoreState(); 593 bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); 594 bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); 595 bool parseDirectiveCFIEscape(); 596 bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); 597 bool parseDirectiveCFISignalFrame(); 598 bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); 599 600 // macro directives 601 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); 602 bool parseDirectiveExitMacro(StringRef Directive); 603 bool parseDirectiveEndMacro(StringRef Directive); 604 bool parseDirectiveMacro(SMLoc DirectiveLoc); 605 bool parseDirectiveMacrosOnOff(StringRef Directive); 606 // alternate macro mode directives 607 bool parseDirectiveAltmacro(StringRef Directive); 608 // ".bundle_align_mode" 609 bool parseDirectiveBundleAlignMode(); 610 // ".bundle_lock" 611 bool parseDirectiveBundleLock(); 612 // ".bundle_unlock" 613 bool parseDirectiveBundleUnlock(); 614 615 // ".space", ".skip" 616 bool parseDirectiveSpace(StringRef IDVal); 617 618 // ".dcb" 619 bool parseDirectiveDCB(StringRef IDVal, unsigned Size); 620 bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); 621 // ".ds" 622 bool parseDirectiveDS(StringRef IDVal, unsigned Size); 623 624 // .sleb128 (Signed=true) and .uleb128 (Signed=false) 625 bool parseDirectiveLEB128(bool Signed); 626 627 /// Parse a directive like ".globl" which 628 /// accepts a single symbol (which should be a label or an external). 629 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); 630 631 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" 632 633 bool parseDirectiveAbort(); // ".abort" 634 bool parseDirectiveInclude(); // ".include" 635 bool parseDirectiveIncbin(); // ".incbin" 636 637 // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" 638 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); 639 // ".ifb" or ".ifnb", depending on ExpectBlank. 640 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); 641 // ".ifc" or ".ifnc", depending on ExpectEqual. 642 bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); 643 // ".ifeqs" or ".ifnes", depending on ExpectEqual. 644 bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); 645 // ".ifdef" or ".ifndef", depending on expect_defined 646 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); 647 bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" 648 bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" 649 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif 650 bool parseEscapedString(std::string &Data) override; 651 652 const MCExpr *applyModifierToExpr(const MCExpr *E, 653 MCSymbolRefExpr::VariantKind Variant); 654 655 // Macro-like directives 656 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); 657 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, 658 raw_svector_ostream &OS); 659 bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); 660 bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" 661 bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" 662 bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" 663 664 // "_emit" or "__emit" 665 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, 666 size_t Len); 667 668 // "align" 669 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); 670 671 // "end" 672 bool parseDirectiveEnd(SMLoc DirectiveLoc); 673 674 // ".err" or ".error" 675 bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); 676 677 // ".warning" 678 bool parseDirectiveWarning(SMLoc DirectiveLoc); 679 680 // .print <double-quotes-string> 681 bool parseDirectivePrint(SMLoc DirectiveLoc); 682 683 // Directives to support address-significance tables. 684 bool parseDirectiveAddrsig(); 685 bool parseDirectiveAddrsigSym(); 686 687 void initializeDirectiveKindMap(); 688 void initializeCVDefRangeTypeMap(); 689 }; 690 691 } // end anonymous namespace 692 693 namespace llvm { 694 695 extern MCAsmParserExtension *createDarwinAsmParser(); 696 extern MCAsmParserExtension *createELFAsmParser(); 697 extern MCAsmParserExtension *createCOFFAsmParser(); 698 extern MCAsmParserExtension *createWasmAsmParser(); 699 700 } // end namespace llvm 701 702 enum { DEFAULT_ADDRSPACE = 0 }; 703 704 AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, 705 const MCAsmInfo &MAI, unsigned CB = 0) 706 : Lexer(MAI), Ctx(Ctx), Out(Out), MAI(MAI), SrcMgr(SM), 707 CurBuffer(CB ? CB : SM.getMainFileID()), MacrosEnabledFlag(true) { 708 HadError = false; 709 // Save the old handler. 710 SavedDiagHandler = SrcMgr.getDiagHandler(); 711 SavedDiagContext = SrcMgr.getDiagContext(); 712 // Set our own handler which calls the saved handler. 713 SrcMgr.setDiagHandler(DiagHandler, this); 714 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); 715 716 // Initialize the platform / file format parser. 717 switch (Ctx.getObjectFileInfo()->getObjectFileType()) { 718 case MCObjectFileInfo::IsCOFF: 719 PlatformParser.reset(createCOFFAsmParser()); 720 break; 721 case MCObjectFileInfo::IsMachO: 722 PlatformParser.reset(createDarwinAsmParser()); 723 IsDarwin = true; 724 break; 725 case MCObjectFileInfo::IsELF: 726 PlatformParser.reset(createELFAsmParser()); 727 break; 728 case MCObjectFileInfo::IsWasm: 729 PlatformParser.reset(createWasmAsmParser()); 730 break; 731 case MCObjectFileInfo::IsXCOFF: 732 report_fatal_error( 733 "Need to implement createXCOFFAsmParser for XCOFF format."); 734 break; 735 } 736 737 PlatformParser->Initialize(*this); 738 initializeDirectiveKindMap(); 739 initializeCVDefRangeTypeMap(); 740 741 NumOfMacroInstantiations = 0; 742 } 743 744 AsmParser::~AsmParser() { 745 assert((HadError || ActiveMacros.empty()) && 746 "Unexpected active macro instantiation!"); 747 748 // Restore the saved diagnostics handler and context for use during 749 // finalization. 750 SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext); 751 } 752 753 void AsmParser::printMacroInstantiations() { 754 // Print the active macro instantiation stack. 755 for (std::vector<MacroInstantiation *>::const_reverse_iterator 756 it = ActiveMacros.rbegin(), 757 ie = ActiveMacros.rend(); 758 it != ie; ++it) 759 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note, 760 "while in macro instantiation"); 761 } 762 763 void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { 764 printPendingErrors(); 765 printMessage(L, SourceMgr::DK_Note, Msg, Range); 766 printMacroInstantiations(); 767 } 768 769 bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { 770 if(getTargetParser().getTargetOptions().MCNoWarn) 771 return false; 772 if (getTargetParser().getTargetOptions().MCFatalWarnings) 773 return Error(L, Msg, Range); 774 printMessage(L, SourceMgr::DK_Warning, Msg, Range); 775 printMacroInstantiations(); 776 return false; 777 } 778 779 bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { 780 HadError = true; 781 printMessage(L, SourceMgr::DK_Error, Msg, Range); 782 printMacroInstantiations(); 783 return true; 784 } 785 786 bool AsmParser::enterIncludeFile(const std::string &Filename) { 787 std::string IncludedFile; 788 unsigned NewBuf = 789 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); 790 if (!NewBuf) 791 return true; 792 793 CurBuffer = NewBuf; 794 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); 795 return false; 796 } 797 798 /// Process the specified .incbin file by searching for it in the include paths 799 /// then just emitting the byte contents of the file to the streamer. This 800 /// returns true on failure. 801 bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, 802 const MCExpr *Count, SMLoc Loc) { 803 std::string IncludedFile; 804 unsigned NewBuf = 805 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile); 806 if (!NewBuf) 807 return true; 808 809 // Pick up the bytes from the file and emit them. 810 StringRef Bytes = SrcMgr.getMemoryBuffer(NewBuf)->getBuffer(); 811 Bytes = Bytes.drop_front(Skip); 812 if (Count) { 813 int64_t Res; 814 if (!Count->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) 815 return Error(Loc, "expected absolute expression"); 816 if (Res < 0) 817 return Warning(Loc, "negative count has no effect"); 818 Bytes = Bytes.take_front(Res); 819 } 820 getStreamer().EmitBytes(Bytes); 821 return false; 822 } 823 824 void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { 825 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); 826 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), 827 Loc.getPointer()); 828 } 829 830 const AsmToken &AsmParser::Lex() { 831 if (Lexer.getTok().is(AsmToken::Error)) 832 Error(Lexer.getErrLoc(), Lexer.getErr()); 833 834 // if it's a end of statement with a comment in it 835 if (getTok().is(AsmToken::EndOfStatement)) { 836 // if this is a line comment output it. 837 if (!getTok().getString().empty() && getTok().getString().front() != '\n' && 838 getTok().getString().front() != '\r' && MAI.preserveAsmComments()) 839 Out.addExplicitComment(Twine(getTok().getString())); 840 } 841 842 const AsmToken *tok = &Lexer.Lex(); 843 844 // Parse comments here to be deferred until end of next statement. 845 while (tok->is(AsmToken::Comment)) { 846 if (MAI.preserveAsmComments()) 847 Out.addExplicitComment(Twine(tok->getString())); 848 tok = &Lexer.Lex(); 849 } 850 851 if (tok->is(AsmToken::Eof)) { 852 // If this is the end of an included file, pop the parent file off the 853 // include stack. 854 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer); 855 if (ParentIncludeLoc != SMLoc()) { 856 jumpToLoc(ParentIncludeLoc); 857 return Lex(); 858 } 859 } 860 861 return *tok; 862 } 863 864 bool AsmParser::enabledGenDwarfForAssembly() { 865 // Check whether the user specified -g. 866 if (!getContext().getGenDwarfForAssembly()) 867 return false; 868 // If we haven't encountered any .file directives (which would imply that 869 // the assembler source was produced with debug info already) then emit one 870 // describing the assembler source file itself. 871 if (getContext().getGenDwarfFileNumber() == 0) { 872 // Use the first #line directive for this, if any. It's preprocessed, so 873 // there is no checksum, and of course no source directive. 874 if (!FirstCppHashFilename.empty()) 875 getContext().setMCLineTableRootFile(/*CUID=*/0, 876 getContext().getCompilationDir(), 877 FirstCppHashFilename, 878 /*Cksum=*/None, /*Source=*/None); 879 const MCDwarfFile &RootFile = 880 getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile(); 881 getContext().setGenDwarfFileNumber(getStreamer().EmitDwarfFileDirective( 882 /*CUID=*/0, getContext().getCompilationDir(), RootFile.Name, 883 RootFile.Checksum, RootFile.Source)); 884 } 885 return true; 886 } 887 888 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { 889 // Create the initial section, if requested. 890 if (!NoInitialTextSection) 891 Out.InitSections(false); 892 893 // Prime the lexer. 894 Lex(); 895 896 HadError = false; 897 AsmCond StartingCondState = TheCondState; 898 SmallVector<AsmRewrite, 4> AsmStrRewrites; 899 900 // If we are generating dwarf for assembly source files save the initial text 901 // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't 902 // emitting any actual debug info yet and haven't had a chance to parse any 903 // embedded .file directives.) 904 if (getContext().getGenDwarfForAssembly()) { 905 MCSection *Sec = getStreamer().getCurrentSectionOnly(); 906 if (!Sec->getBeginSymbol()) { 907 MCSymbol *SectionStartSym = getContext().createTempSymbol(); 908 getStreamer().EmitLabel(SectionStartSym); 909 Sec->setBeginSymbol(SectionStartSym); 910 } 911 bool InsertResult = getContext().addGenDwarfSection(Sec); 912 assert(InsertResult && ".text section should not have debug info yet"); 913 (void)InsertResult; 914 } 915 916 // While we have input, parse each statement. 917 while (Lexer.isNot(AsmToken::Eof)) { 918 ParseStatementInfo Info(&AsmStrRewrites); 919 if (!parseStatement(Info, nullptr)) 920 continue; 921 922 // If we have a Lexer Error we are on an Error Token. Load in Lexer Error 923 // for printing ErrMsg via Lex() only if no (presumably better) parser error 924 // exists. 925 if (!hasPendingError() && Lexer.getTok().is(AsmToken::Error)) { 926 Lex(); 927 } 928 929 // parseStatement returned true so may need to emit an error. 930 printPendingErrors(); 931 932 // Skipping to the next line if needed. 933 if (!getLexer().isAtStartOfStatement()) 934 eatToEndOfStatement(); 935 } 936 937 getTargetParser().onEndOfFile(); 938 printPendingErrors(); 939 940 // All errors should have been emitted. 941 assert(!hasPendingError() && "unexpected error from parseStatement"); 942 943 getTargetParser().flushPendingInstructions(getStreamer()); 944 945 if (TheCondState.TheCond != StartingCondState.TheCond || 946 TheCondState.Ignore != StartingCondState.Ignore) 947 printError(getTok().getLoc(), "unmatched .ifs or .elses"); 948 // Check to see there are no empty DwarfFile slots. 949 const auto &LineTables = getContext().getMCDwarfLineTables(); 950 if (!LineTables.empty()) { 951 unsigned Index = 0; 952 for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { 953 if (File.Name.empty() && Index != 0) 954 printError(getTok().getLoc(), "unassigned file number: " + 955 Twine(Index) + 956 " for .file directives"); 957 ++Index; 958 } 959 } 960 961 // Check to see that all assembler local symbols were actually defined. 962 // Targets that don't do subsections via symbols may not want this, though, 963 // so conservatively exclude them. Only do this if we're finalizing, though, 964 // as otherwise we won't necessarilly have seen everything yet. 965 if (!NoFinalize) { 966 if (MAI.hasSubsectionsViaSymbols()) { 967 for (const auto &TableEntry : getContext().getSymbols()) { 968 MCSymbol *Sym = TableEntry.getValue(); 969 // Variable symbols may not be marked as defined, so check those 970 // explicitly. If we know it's a variable, we have a definition for 971 // the purposes of this check. 972 if (Sym->isTemporary() && !Sym->isVariable() && !Sym->isDefined()) 973 // FIXME: We would really like to refer back to where the symbol was 974 // first referenced for a source location. We need to add something 975 // to track that. Currently, we just point to the end of the file. 976 printError(getTok().getLoc(), "assembler local symbol '" + 977 Sym->getName() + "' not defined"); 978 } 979 } 980 981 // Temporary symbols like the ones for directional jumps don't go in the 982 // symbol table. They also need to be diagnosed in all (final) cases. 983 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { 984 if (std::get<2>(LocSym)->isUndefined()) { 985 // Reset the state of any "# line file" directives we've seen to the 986 // context as it was at the diagnostic site. 987 CppHashInfo = std::get<1>(LocSym); 988 printError(std::get<0>(LocSym), "directional label undefined"); 989 } 990 } 991 } 992 993 // Finalize the output stream if there are no errors and if the client wants 994 // us to. 995 if (!HadError && !NoFinalize) 996 Out.Finish(); 997 998 return HadError || getContext().hadError(); 999 } 1000 1001 bool AsmParser::checkForValidSection() { 1002 if (!ParsingInlineAsm && !getStreamer().getCurrentSectionOnly()) { 1003 Out.InitSections(false); 1004 return Error(getTok().getLoc(), 1005 "expected section directive before assembly directive"); 1006 } 1007 return false; 1008 } 1009 1010 /// Throw away the rest of the line for testing purposes. 1011 void AsmParser::eatToEndOfStatement() { 1012 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) 1013 Lexer.Lex(); 1014 1015 // Eat EOL. 1016 if (Lexer.is(AsmToken::EndOfStatement)) 1017 Lexer.Lex(); 1018 } 1019 1020 StringRef AsmParser::parseStringToEndOfStatement() { 1021 const char *Start = getTok().getLoc().getPointer(); 1022 1023 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof)) 1024 Lexer.Lex(); 1025 1026 const char *End = getTok().getLoc().getPointer(); 1027 return StringRef(Start, End - Start); 1028 } 1029 1030 StringRef AsmParser::parseStringToComma() { 1031 const char *Start = getTok().getLoc().getPointer(); 1032 1033 while (Lexer.isNot(AsmToken::EndOfStatement) && 1034 Lexer.isNot(AsmToken::Comma) && Lexer.isNot(AsmToken::Eof)) 1035 Lexer.Lex(); 1036 1037 const char *End = getTok().getLoc().getPointer(); 1038 return StringRef(Start, End - Start); 1039 } 1040 1041 /// Parse a paren expression and return it. 1042 /// NOTE: This assumes the leading '(' has already been consumed. 1043 /// 1044 /// parenexpr ::= expr) 1045 /// 1046 bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { 1047 if (parseExpression(Res)) 1048 return true; 1049 if (Lexer.isNot(AsmToken::RParen)) 1050 return TokError("expected ')' in parentheses expression"); 1051 EndLoc = Lexer.getTok().getEndLoc(); 1052 Lex(); 1053 return false; 1054 } 1055 1056 /// Parse a bracket expression and return it. 1057 /// NOTE: This assumes the leading '[' has already been consumed. 1058 /// 1059 /// bracketexpr ::= expr] 1060 /// 1061 bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { 1062 if (parseExpression(Res)) 1063 return true; 1064 EndLoc = getTok().getEndLoc(); 1065 if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression")) 1066 return true; 1067 return false; 1068 } 1069 1070 /// Parse a primary expression and return it. 1071 /// primaryexpr ::= (parenexpr 1072 /// primaryexpr ::= symbol 1073 /// primaryexpr ::= number 1074 /// primaryexpr ::= '.' 1075 /// primaryexpr ::= ~,+,- primaryexpr 1076 bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) { 1077 SMLoc FirstTokenLoc = getLexer().getLoc(); 1078 AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); 1079 switch (FirstTokenKind) { 1080 default: 1081 return TokError("unknown token in expression"); 1082 // If we have an error assume that we've already handled it. 1083 case AsmToken::Error: 1084 return true; 1085 case AsmToken::Exclaim: 1086 Lex(); // Eat the operator. 1087 if (parsePrimaryExpr(Res, EndLoc)) 1088 return true; 1089 Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc); 1090 return false; 1091 case AsmToken::Dollar: 1092 case AsmToken::At: 1093 case AsmToken::String: 1094 case AsmToken::Identifier: { 1095 StringRef Identifier; 1096 if (parseIdentifier(Identifier)) { 1097 // We may have failed but $ may be a valid token. 1098 if (getTok().is(AsmToken::Dollar)) { 1099 if (Lexer.getMAI().getDollarIsPC()) { 1100 Lex(); 1101 // This is a '$' reference, which references the current PC. Emit a 1102 // temporary label to the streamer and refer to it. 1103 MCSymbol *Sym = Ctx.createTempSymbol(); 1104 Out.EmitLabel(Sym); 1105 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, 1106 getContext()); 1107 EndLoc = FirstTokenLoc; 1108 return false; 1109 } 1110 return Error(FirstTokenLoc, "invalid token in expression"); 1111 } 1112 } 1113 // Parse symbol variant 1114 std::pair<StringRef, StringRef> Split; 1115 if (!MAI.useParensForSymbolVariant()) { 1116 if (FirstTokenKind == AsmToken::String) { 1117 if (Lexer.is(AsmToken::At)) { 1118 Lex(); // eat @ 1119 SMLoc AtLoc = getLexer().getLoc(); 1120 StringRef VName; 1121 if (parseIdentifier(VName)) 1122 return Error(AtLoc, "expected symbol variant after '@'"); 1123 1124 Split = std::make_pair(Identifier, VName); 1125 } 1126 } else { 1127 Split = Identifier.split('@'); 1128 } 1129 } else if (Lexer.is(AsmToken::LParen)) { 1130 Lex(); // eat '('. 1131 StringRef VName; 1132 parseIdentifier(VName); 1133 // eat ')'. 1134 if (parseToken(AsmToken::RParen, 1135 "unexpected token in variant, expected ')'")) 1136 return true; 1137 Split = std::make_pair(Identifier, VName); 1138 } 1139 1140 EndLoc = SMLoc::getFromPointer(Identifier.end()); 1141 1142 // This is a symbol reference. 1143 StringRef SymbolName = Identifier; 1144 if (SymbolName.empty()) 1145 return Error(getLexer().getLoc(), "expected a symbol reference"); 1146 1147 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 1148 1149 // Lookup the symbol variant if used. 1150 if (!Split.second.empty()) { 1151 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); 1152 if (Variant != MCSymbolRefExpr::VK_Invalid) { 1153 SymbolName = Split.first; 1154 } else if (MAI.doesAllowAtInName() && !MAI.useParensForSymbolVariant()) { 1155 Variant = MCSymbolRefExpr::VK_None; 1156 } else { 1157 return Error(SMLoc::getFromPointer(Split.second.begin()), 1158 "invalid variant '" + Split.second + "'"); 1159 } 1160 } 1161 1162 MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName); 1163 if (!Sym) 1164 Sym = getContext().getOrCreateSymbol(SymbolName); 1165 1166 // If this is an absolute variable reference, substitute it now to preserve 1167 // semantics in the face of reassignment. 1168 if (Sym->isVariable()) { 1169 auto V = Sym->getVariableValue(/*SetUsed*/ false); 1170 bool DoInline = isa<MCConstantExpr>(V) && !Variant; 1171 if (auto TV = dyn_cast<MCTargetExpr>(V)) 1172 DoInline = TV->inlineAssignedExpr(); 1173 if (DoInline) { 1174 if (Variant) 1175 return Error(EndLoc, "unexpected modifier on variable reference"); 1176 Res = Sym->getVariableValue(/*SetUsed*/ false); 1177 return false; 1178 } 1179 } 1180 1181 // Otherwise create a symbol ref. 1182 Res = MCSymbolRefExpr::create(Sym, Variant, getContext(), FirstTokenLoc); 1183 return false; 1184 } 1185 case AsmToken::BigNum: 1186 return TokError("literal value out of range for directive"); 1187 case AsmToken::Integer: { 1188 SMLoc Loc = getTok().getLoc(); 1189 int64_t IntVal = getTok().getIntVal(); 1190 Res = MCConstantExpr::create(IntVal, getContext()); 1191 EndLoc = Lexer.getTok().getEndLoc(); 1192 Lex(); // Eat token. 1193 // Look for 'b' or 'f' following an Integer as a directional label 1194 if (Lexer.getKind() == AsmToken::Identifier) { 1195 StringRef IDVal = getTok().getString(); 1196 // Lookup the symbol variant if used. 1197 std::pair<StringRef, StringRef> Split = IDVal.split('@'); 1198 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None; 1199 if (Split.first.size() != IDVal.size()) { 1200 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second); 1201 if (Variant == MCSymbolRefExpr::VK_Invalid) 1202 return TokError("invalid variant '" + Split.second + "'"); 1203 IDVal = Split.first; 1204 } 1205 if (IDVal == "f" || IDVal == "b") { 1206 MCSymbol *Sym = 1207 Ctx.getDirectionalLocalSymbol(IntVal, IDVal == "b"); 1208 Res = MCSymbolRefExpr::create(Sym, Variant, getContext()); 1209 if (IDVal == "b" && Sym->isUndefined()) 1210 return Error(Loc, "directional label undefined"); 1211 DirLabels.push_back(std::make_tuple(Loc, CppHashInfo, Sym)); 1212 EndLoc = Lexer.getTok().getEndLoc(); 1213 Lex(); // Eat identifier. 1214 } 1215 } 1216 return false; 1217 } 1218 case AsmToken::Real: { 1219 APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); 1220 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); 1221 Res = MCConstantExpr::create(IntVal, getContext()); 1222 EndLoc = Lexer.getTok().getEndLoc(); 1223 Lex(); // Eat token. 1224 return false; 1225 } 1226 case AsmToken::Dot: { 1227 // This is a '.' reference, which references the current PC. Emit a 1228 // temporary label to the streamer and refer to it. 1229 MCSymbol *Sym = Ctx.createTempSymbol(); 1230 Out.EmitLabel(Sym); 1231 Res = MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, getContext()); 1232 EndLoc = Lexer.getTok().getEndLoc(); 1233 Lex(); // Eat identifier. 1234 return false; 1235 } 1236 case AsmToken::LParen: 1237 Lex(); // Eat the '('. 1238 return parseParenExpr(Res, EndLoc); 1239 case AsmToken::LBrac: 1240 if (!PlatformParser->HasBracketExpressions()) 1241 return TokError("brackets expression not supported on this target"); 1242 Lex(); // Eat the '['. 1243 return parseBracketExpr(Res, EndLoc); 1244 case AsmToken::Minus: 1245 Lex(); // Eat the operator. 1246 if (parsePrimaryExpr(Res, EndLoc)) 1247 return true; 1248 Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc); 1249 return false; 1250 case AsmToken::Plus: 1251 Lex(); // Eat the operator. 1252 if (parsePrimaryExpr(Res, EndLoc)) 1253 return true; 1254 Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc); 1255 return false; 1256 case AsmToken::Tilde: 1257 Lex(); // Eat the operator. 1258 if (parsePrimaryExpr(Res, EndLoc)) 1259 return true; 1260 Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc); 1261 return false; 1262 // MIPS unary expression operators. The lexer won't generate these tokens if 1263 // MCAsmInfo::HasMipsExpressions is false for the target. 1264 case AsmToken::PercentCall16: 1265 case AsmToken::PercentCall_Hi: 1266 case AsmToken::PercentCall_Lo: 1267 case AsmToken::PercentDtprel_Hi: 1268 case AsmToken::PercentDtprel_Lo: 1269 case AsmToken::PercentGot: 1270 case AsmToken::PercentGot_Disp: 1271 case AsmToken::PercentGot_Hi: 1272 case AsmToken::PercentGot_Lo: 1273 case AsmToken::PercentGot_Ofst: 1274 case AsmToken::PercentGot_Page: 1275 case AsmToken::PercentGottprel: 1276 case AsmToken::PercentGp_Rel: 1277 case AsmToken::PercentHi: 1278 case AsmToken::PercentHigher: 1279 case AsmToken::PercentHighest: 1280 case AsmToken::PercentLo: 1281 case AsmToken::PercentNeg: 1282 case AsmToken::PercentPcrel_Hi: 1283 case AsmToken::PercentPcrel_Lo: 1284 case AsmToken::PercentTlsgd: 1285 case AsmToken::PercentTlsldm: 1286 case AsmToken::PercentTprel_Hi: 1287 case AsmToken::PercentTprel_Lo: 1288 Lex(); // Eat the operator. 1289 if (Lexer.isNot(AsmToken::LParen)) 1290 return TokError("expected '(' after operator"); 1291 Lex(); // Eat the operator. 1292 if (parseExpression(Res, EndLoc)) 1293 return true; 1294 if (Lexer.isNot(AsmToken::RParen)) 1295 return TokError("expected ')'"); 1296 Lex(); // Eat the operator. 1297 Res = getTargetParser().createTargetUnaryExpr(Res, FirstTokenKind, Ctx); 1298 return !Res; 1299 } 1300 } 1301 1302 bool AsmParser::parseExpression(const MCExpr *&Res) { 1303 SMLoc EndLoc; 1304 return parseExpression(Res, EndLoc); 1305 } 1306 1307 const MCExpr * 1308 AsmParser::applyModifierToExpr(const MCExpr *E, 1309 MCSymbolRefExpr::VariantKind Variant) { 1310 // Ask the target implementation about this expression first. 1311 const MCExpr *NewE = getTargetParser().applyModifierToExpr(E, Variant, Ctx); 1312 if (NewE) 1313 return NewE; 1314 // Recurse over the given expression, rebuilding it to apply the given variant 1315 // if there is exactly one symbol. 1316 switch (E->getKind()) { 1317 case MCExpr::Target: 1318 case MCExpr::Constant: 1319 return nullptr; 1320 1321 case MCExpr::SymbolRef: { 1322 const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(E); 1323 1324 if (SRE->getKind() != MCSymbolRefExpr::VK_None) { 1325 TokError("invalid variant on expression '" + getTok().getIdentifier() + 1326 "' (already modified)"); 1327 return E; 1328 } 1329 1330 return MCSymbolRefExpr::create(&SRE->getSymbol(), Variant, getContext()); 1331 } 1332 1333 case MCExpr::Unary: { 1334 const MCUnaryExpr *UE = cast<MCUnaryExpr>(E); 1335 const MCExpr *Sub = applyModifierToExpr(UE->getSubExpr(), Variant); 1336 if (!Sub) 1337 return nullptr; 1338 return MCUnaryExpr::create(UE->getOpcode(), Sub, getContext()); 1339 } 1340 1341 case MCExpr::Binary: { 1342 const MCBinaryExpr *BE = cast<MCBinaryExpr>(E); 1343 const MCExpr *LHS = applyModifierToExpr(BE->getLHS(), Variant); 1344 const MCExpr *RHS = applyModifierToExpr(BE->getRHS(), Variant); 1345 1346 if (!LHS && !RHS) 1347 return nullptr; 1348 1349 if (!LHS) 1350 LHS = BE->getLHS(); 1351 if (!RHS) 1352 RHS = BE->getRHS(); 1353 1354 return MCBinaryExpr::create(BE->getOpcode(), LHS, RHS, getContext()); 1355 } 1356 } 1357 1358 llvm_unreachable("Invalid expression kind!"); 1359 } 1360 1361 /// This function checks if the next token is <string> type or arithmetic. 1362 /// string that begin with character '<' must end with character '>'. 1363 /// otherwise it is arithmetics. 1364 /// If the function returns a 'true' value, 1365 /// the End argument will be filled with the last location pointed to the '>' 1366 /// character. 1367 1368 /// There is a gap between the AltMacro's documentation and the single quote 1369 /// implementation. GCC does not fully support this feature and so we will not 1370 /// support it. 1371 /// TODO: Adding single quote as a string. 1372 static bool isAltmacroString(SMLoc &StrLoc, SMLoc &EndLoc) { 1373 assert((StrLoc.getPointer() != nullptr) && 1374 "Argument to the function cannot be a NULL value"); 1375 const char *CharPtr = StrLoc.getPointer(); 1376 while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') && 1377 (*CharPtr != '\0')) { 1378 if (*CharPtr == '!') 1379 CharPtr++; 1380 CharPtr++; 1381 } 1382 if (*CharPtr == '>') { 1383 EndLoc = StrLoc.getFromPointer(CharPtr + 1); 1384 return true; 1385 } 1386 return false; 1387 } 1388 1389 /// creating a string without the escape characters '!'. 1390 static std::string altMacroString(StringRef AltMacroStr) { 1391 std::string Res; 1392 for (size_t Pos = 0; Pos < AltMacroStr.size(); Pos++) { 1393 if (AltMacroStr[Pos] == '!') 1394 Pos++; 1395 Res += AltMacroStr[Pos]; 1396 } 1397 return Res; 1398 } 1399 1400 /// Parse an expression and return it. 1401 /// 1402 /// expr ::= expr &&,|| expr -> lowest. 1403 /// expr ::= expr |,^,&,! expr 1404 /// expr ::= expr ==,!=,<>,<,<=,>,>= expr 1405 /// expr ::= expr <<,>> expr 1406 /// expr ::= expr +,- expr 1407 /// expr ::= expr *,/,% expr -> highest. 1408 /// expr ::= primaryexpr 1409 /// 1410 bool AsmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) { 1411 // Parse the expression. 1412 Res = nullptr; 1413 if (getTargetParser().parsePrimaryExpr(Res, EndLoc) || 1414 parseBinOpRHS(1, Res, EndLoc)) 1415 return true; 1416 1417 // As a special case, we support 'a op b @ modifier' by rewriting the 1418 // expression to include the modifier. This is inefficient, but in general we 1419 // expect users to use 'a@modifier op b'. 1420 if (Lexer.getKind() == AsmToken::At) { 1421 Lex(); 1422 1423 if (Lexer.isNot(AsmToken::Identifier)) 1424 return TokError("unexpected symbol modifier following '@'"); 1425 1426 MCSymbolRefExpr::VariantKind Variant = 1427 MCSymbolRefExpr::getVariantKindForName(getTok().getIdentifier()); 1428 if (Variant == MCSymbolRefExpr::VK_Invalid) 1429 return TokError("invalid variant '" + getTok().getIdentifier() + "'"); 1430 1431 const MCExpr *ModifiedRes = applyModifierToExpr(Res, Variant); 1432 if (!ModifiedRes) { 1433 return TokError("invalid modifier '" + getTok().getIdentifier() + 1434 "' (no symbols present)"); 1435 } 1436 1437 Res = ModifiedRes; 1438 Lex(); 1439 } 1440 1441 // Try to constant fold it up front, if possible. Do not exploit 1442 // assembler here. 1443 int64_t Value; 1444 if (Res->evaluateAsAbsolute(Value)) 1445 Res = MCConstantExpr::create(Value, getContext()); 1446 1447 return false; 1448 } 1449 1450 bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { 1451 Res = nullptr; 1452 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc); 1453 } 1454 1455 bool AsmParser::parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res, 1456 SMLoc &EndLoc) { 1457 if (parseParenExpr(Res, EndLoc)) 1458 return true; 1459 1460 for (; ParenDepth > 0; --ParenDepth) { 1461 if (parseBinOpRHS(1, Res, EndLoc)) 1462 return true; 1463 1464 // We don't Lex() the last RParen. 1465 // This is the same behavior as parseParenExpression(). 1466 if (ParenDepth - 1 > 0) { 1467 EndLoc = getTok().getEndLoc(); 1468 if (parseToken(AsmToken::RParen, 1469 "expected ')' in parentheses expression")) 1470 return true; 1471 } 1472 } 1473 return false; 1474 } 1475 1476 bool AsmParser::parseAbsoluteExpression(int64_t &Res) { 1477 const MCExpr *Expr; 1478 1479 SMLoc StartLoc = Lexer.getLoc(); 1480 if (parseExpression(Expr)) 1481 return true; 1482 1483 if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr())) 1484 return Error(StartLoc, "expected absolute expression"); 1485 1486 return false; 1487 } 1488 1489 static unsigned getDarwinBinOpPrecedence(AsmToken::TokenKind K, 1490 MCBinaryExpr::Opcode &Kind, 1491 bool ShouldUseLogicalShr) { 1492 switch (K) { 1493 default: 1494 return 0; // not a binop. 1495 1496 // Lowest Precedence: &&, || 1497 case AsmToken::AmpAmp: 1498 Kind = MCBinaryExpr::LAnd; 1499 return 1; 1500 case AsmToken::PipePipe: 1501 Kind = MCBinaryExpr::LOr; 1502 return 1; 1503 1504 // Low Precedence: |, &, ^ 1505 // 1506 // FIXME: gas seems to support '!' as an infix operator? 1507 case AsmToken::Pipe: 1508 Kind = MCBinaryExpr::Or; 1509 return 2; 1510 case AsmToken::Caret: 1511 Kind = MCBinaryExpr::Xor; 1512 return 2; 1513 case AsmToken::Amp: 1514 Kind = MCBinaryExpr::And; 1515 return 2; 1516 1517 // Low Intermediate Precedence: ==, !=, <>, <, <=, >, >= 1518 case AsmToken::EqualEqual: 1519 Kind = MCBinaryExpr::EQ; 1520 return 3; 1521 case AsmToken::ExclaimEqual: 1522 case AsmToken::LessGreater: 1523 Kind = MCBinaryExpr::NE; 1524 return 3; 1525 case AsmToken::Less: 1526 Kind = MCBinaryExpr::LT; 1527 return 3; 1528 case AsmToken::LessEqual: 1529 Kind = MCBinaryExpr::LTE; 1530 return 3; 1531 case AsmToken::Greater: 1532 Kind = MCBinaryExpr::GT; 1533 return 3; 1534 case AsmToken::GreaterEqual: 1535 Kind = MCBinaryExpr::GTE; 1536 return 3; 1537 1538 // Intermediate Precedence: <<, >> 1539 case AsmToken::LessLess: 1540 Kind = MCBinaryExpr::Shl; 1541 return 4; 1542 case AsmToken::GreaterGreater: 1543 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; 1544 return 4; 1545 1546 // High Intermediate Precedence: +, - 1547 case AsmToken::Plus: 1548 Kind = MCBinaryExpr::Add; 1549 return 5; 1550 case AsmToken::Minus: 1551 Kind = MCBinaryExpr::Sub; 1552 return 5; 1553 1554 // Highest Precedence: *, /, % 1555 case AsmToken::Star: 1556 Kind = MCBinaryExpr::Mul; 1557 return 6; 1558 case AsmToken::Slash: 1559 Kind = MCBinaryExpr::Div; 1560 return 6; 1561 case AsmToken::Percent: 1562 Kind = MCBinaryExpr::Mod; 1563 return 6; 1564 } 1565 } 1566 1567 static unsigned getGNUBinOpPrecedence(AsmToken::TokenKind K, 1568 MCBinaryExpr::Opcode &Kind, 1569 bool ShouldUseLogicalShr) { 1570 switch (K) { 1571 default: 1572 return 0; // not a binop. 1573 1574 // Lowest Precedence: &&, || 1575 case AsmToken::AmpAmp: 1576 Kind = MCBinaryExpr::LAnd; 1577 return 2; 1578 case AsmToken::PipePipe: 1579 Kind = MCBinaryExpr::LOr; 1580 return 1; 1581 1582 // Low Precedence: ==, !=, <>, <, <=, >, >= 1583 case AsmToken::EqualEqual: 1584 Kind = MCBinaryExpr::EQ; 1585 return 3; 1586 case AsmToken::ExclaimEqual: 1587 case AsmToken::LessGreater: 1588 Kind = MCBinaryExpr::NE; 1589 return 3; 1590 case AsmToken::Less: 1591 Kind = MCBinaryExpr::LT; 1592 return 3; 1593 case AsmToken::LessEqual: 1594 Kind = MCBinaryExpr::LTE; 1595 return 3; 1596 case AsmToken::Greater: 1597 Kind = MCBinaryExpr::GT; 1598 return 3; 1599 case AsmToken::GreaterEqual: 1600 Kind = MCBinaryExpr::GTE; 1601 return 3; 1602 1603 // Low Intermediate Precedence: +, - 1604 case AsmToken::Plus: 1605 Kind = MCBinaryExpr::Add; 1606 return 4; 1607 case AsmToken::Minus: 1608 Kind = MCBinaryExpr::Sub; 1609 return 4; 1610 1611 // High Intermediate Precedence: |, &, ^ 1612 // 1613 // FIXME: gas seems to support '!' as an infix operator? 1614 case AsmToken::Pipe: 1615 Kind = MCBinaryExpr::Or; 1616 return 5; 1617 case AsmToken::Caret: 1618 Kind = MCBinaryExpr::Xor; 1619 return 5; 1620 case AsmToken::Amp: 1621 Kind = MCBinaryExpr::And; 1622 return 5; 1623 1624 // Highest Precedence: *, /, %, <<, >> 1625 case AsmToken::Star: 1626 Kind = MCBinaryExpr::Mul; 1627 return 6; 1628 case AsmToken::Slash: 1629 Kind = MCBinaryExpr::Div; 1630 return 6; 1631 case AsmToken::Percent: 1632 Kind = MCBinaryExpr::Mod; 1633 return 6; 1634 case AsmToken::LessLess: 1635 Kind = MCBinaryExpr::Shl; 1636 return 6; 1637 case AsmToken::GreaterGreater: 1638 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr; 1639 return 6; 1640 } 1641 } 1642 1643 unsigned AsmParser::getBinOpPrecedence(AsmToken::TokenKind K, 1644 MCBinaryExpr::Opcode &Kind) { 1645 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr(); 1646 return IsDarwin ? getDarwinBinOpPrecedence(K, Kind, ShouldUseLogicalShr) 1647 : getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr); 1648 } 1649 1650 /// Parse all binary operators with precedence >= 'Precedence'. 1651 /// Res contains the LHS of the expression on input. 1652 bool AsmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, 1653 SMLoc &EndLoc) { 1654 SMLoc StartLoc = Lexer.getLoc(); 1655 while (true) { 1656 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add; 1657 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind); 1658 1659 // If the next token is lower precedence than we are allowed to eat, return 1660 // successfully with what we ate already. 1661 if (TokPrec < Precedence) 1662 return false; 1663 1664 Lex(); 1665 1666 // Eat the next primary expression. 1667 const MCExpr *RHS; 1668 if (getTargetParser().parsePrimaryExpr(RHS, EndLoc)) 1669 return true; 1670 1671 // If BinOp binds less tightly with RHS than the operator after RHS, let 1672 // the pending operator take RHS as its LHS. 1673 MCBinaryExpr::Opcode Dummy; 1674 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy); 1675 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc)) 1676 return true; 1677 1678 // Merge LHS and RHS according to operator. 1679 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc); 1680 } 1681 } 1682 1683 /// ParseStatement: 1684 /// ::= EndOfStatement 1685 /// ::= Label* Directive ...Operands... EndOfStatement 1686 /// ::= Label* Identifier OperandList* EndOfStatement 1687 bool AsmParser::parseStatement(ParseStatementInfo &Info, 1688 MCAsmParserSemaCallback *SI) { 1689 assert(!hasPendingError() && "parseStatement started with pending error"); 1690 // Eat initial spaces and comments 1691 while (Lexer.is(AsmToken::Space)) 1692 Lex(); 1693 if (Lexer.is(AsmToken::EndOfStatement)) { 1694 // if this is a line comment we can drop it safely 1695 if (getTok().getString().empty() || getTok().getString().front() == '\r' || 1696 getTok().getString().front() == '\n') 1697 Out.AddBlankLine(); 1698 Lex(); 1699 return false; 1700 } 1701 // Statements always start with an identifier. 1702 AsmToken ID = getTok(); 1703 SMLoc IDLoc = ID.getLoc(); 1704 StringRef IDVal; 1705 int64_t LocalLabelVal = -1; 1706 if (Lexer.is(AsmToken::HashDirective)) 1707 return parseCppHashLineFilenameComment(IDLoc); 1708 // Allow an integer followed by a ':' as a directional local label. 1709 if (Lexer.is(AsmToken::Integer)) { 1710 LocalLabelVal = getTok().getIntVal(); 1711 if (LocalLabelVal < 0) { 1712 if (!TheCondState.Ignore) { 1713 Lex(); // always eat a token 1714 return Error(IDLoc, "unexpected token at start of statement"); 1715 } 1716 IDVal = ""; 1717 } else { 1718 IDVal = getTok().getString(); 1719 Lex(); // Consume the integer token to be used as an identifier token. 1720 if (Lexer.getKind() != AsmToken::Colon) { 1721 if (!TheCondState.Ignore) { 1722 Lex(); // always eat a token 1723 return Error(IDLoc, "unexpected token at start of statement"); 1724 } 1725 } 1726 } 1727 } else if (Lexer.is(AsmToken::Dot)) { 1728 // Treat '.' as a valid identifier in this context. 1729 Lex(); 1730 IDVal = "."; 1731 } else if (Lexer.is(AsmToken::LCurly)) { 1732 // Treat '{' as a valid identifier in this context. 1733 Lex(); 1734 IDVal = "{"; 1735 1736 } else if (Lexer.is(AsmToken::RCurly)) { 1737 // Treat '}' as a valid identifier in this context. 1738 Lex(); 1739 IDVal = "}"; 1740 } else if (Lexer.is(AsmToken::Star) && 1741 getTargetParser().starIsStartOfStatement()) { 1742 // Accept '*' as a valid start of statement. 1743 Lex(); 1744 IDVal = "*"; 1745 } else if (parseIdentifier(IDVal)) { 1746 if (!TheCondState.Ignore) { 1747 Lex(); // always eat a token 1748 return Error(IDLoc, "unexpected token at start of statement"); 1749 } 1750 IDVal = ""; 1751 } 1752 1753 // Handle conditional assembly here before checking for skipping. We 1754 // have to do this so that .endif isn't skipped in a ".if 0" block for 1755 // example. 1756 StringMap<DirectiveKind>::const_iterator DirKindIt = 1757 DirectiveKindMap.find(IDVal); 1758 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end()) 1759 1760 ? DK_NO_DIRECTIVE 1761 : DirKindIt->getValue(); 1762 switch (DirKind) { 1763 default: 1764 break; 1765 case DK_IF: 1766 case DK_IFEQ: 1767 case DK_IFGE: 1768 case DK_IFGT: 1769 case DK_IFLE: 1770 case DK_IFLT: 1771 case DK_IFNE: 1772 return parseDirectiveIf(IDLoc, DirKind); 1773 case DK_IFB: 1774 return parseDirectiveIfb(IDLoc, true); 1775 case DK_IFNB: 1776 return parseDirectiveIfb(IDLoc, false); 1777 case DK_IFC: 1778 return parseDirectiveIfc(IDLoc, true); 1779 case DK_IFEQS: 1780 return parseDirectiveIfeqs(IDLoc, true); 1781 case DK_IFNC: 1782 return parseDirectiveIfc(IDLoc, false); 1783 case DK_IFNES: 1784 return parseDirectiveIfeqs(IDLoc, false); 1785 case DK_IFDEF: 1786 return parseDirectiveIfdef(IDLoc, true); 1787 case DK_IFNDEF: 1788 case DK_IFNOTDEF: 1789 return parseDirectiveIfdef(IDLoc, false); 1790 case DK_ELSEIF: 1791 return parseDirectiveElseIf(IDLoc); 1792 case DK_ELSE: 1793 return parseDirectiveElse(IDLoc); 1794 case DK_ENDIF: 1795 return parseDirectiveEndIf(IDLoc); 1796 } 1797 1798 // Ignore the statement if in the middle of inactive conditional 1799 // (e.g. ".if 0"). 1800 if (TheCondState.Ignore) { 1801 eatToEndOfStatement(); 1802 return false; 1803 } 1804 1805 // FIXME: Recurse on local labels? 1806 1807 // See what kind of statement we have. 1808 switch (Lexer.getKind()) { 1809 case AsmToken::Colon: { 1810 if (!getTargetParser().isLabel(ID)) 1811 break; 1812 if (checkForValidSection()) 1813 return true; 1814 1815 // identifier ':' -> Label. 1816 Lex(); 1817 1818 // Diagnose attempt to use '.' as a label. 1819 if (IDVal == ".") 1820 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label"); 1821 1822 // Diagnose attempt to use a variable as a label. 1823 // 1824 // FIXME: Diagnostics. Note the location of the definition as a label. 1825 // FIXME: This doesn't diagnose assignment to a symbol which has been 1826 // implicitly marked as external. 1827 MCSymbol *Sym; 1828 if (LocalLabelVal == -1) { 1829 if (ParsingInlineAsm && SI) { 1830 StringRef RewrittenLabel = 1831 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true); 1832 assert(!RewrittenLabel.empty() && 1833 "We should have an internal name here."); 1834 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(), 1835 RewrittenLabel); 1836 IDVal = RewrittenLabel; 1837 } 1838 Sym = getContext().getOrCreateSymbol(IDVal); 1839 } else 1840 Sym = Ctx.createDirectionalLocalSymbol(LocalLabelVal); 1841 // End of Labels should be treated as end of line for lexing 1842 // purposes but that information is not available to the Lexer who 1843 // does not understand Labels. This may cause us to see a Hash 1844 // here instead of a preprocessor line comment. 1845 if (getTok().is(AsmToken::Hash)) { 1846 StringRef CommentStr = parseStringToEndOfStatement(); 1847 Lexer.Lex(); 1848 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr)); 1849 } 1850 1851 // Consume any end of statement token, if present, to avoid spurious 1852 // AddBlankLine calls(). 1853 if (getTok().is(AsmToken::EndOfStatement)) { 1854 Lex(); 1855 } 1856 1857 getTargetParser().doBeforeLabelEmit(Sym); 1858 1859 // Emit the label. 1860 if (!getTargetParser().isParsingInlineAsm()) 1861 Out.EmitLabel(Sym, IDLoc); 1862 1863 // If we are generating dwarf for assembly source files then gather the 1864 // info to make a dwarf label entry for this label if needed. 1865 if (enabledGenDwarfForAssembly()) 1866 MCGenDwarfLabelEntry::Make(Sym, &getStreamer(), getSourceManager(), 1867 IDLoc); 1868 1869 getTargetParser().onLabelParsed(Sym); 1870 1871 return false; 1872 } 1873 1874 case AsmToken::Equal: 1875 if (!getTargetParser().equalIsAsmAssignment()) 1876 break; 1877 // identifier '=' ... -> assignment statement 1878 Lex(); 1879 1880 return parseAssignment(IDVal, true); 1881 1882 default: // Normal instruction or directive. 1883 break; 1884 } 1885 1886 // If macros are enabled, check to see if this is a macro instantiation. 1887 if (areMacrosEnabled()) 1888 if (const MCAsmMacro *M = getContext().lookupMacro(IDVal)) { 1889 return handleMacroEntry(M, IDLoc); 1890 } 1891 1892 // Otherwise, we have a normal instruction or directive. 1893 1894 // Directives start with "." 1895 if (IDVal.startswith(".") && IDVal != ".") { 1896 // There are several entities interested in parsing directives: 1897 // 1898 // 1. The target-specific assembly parser. Some directives are target 1899 // specific or may potentially behave differently on certain targets. 1900 // 2. Asm parser extensions. For example, platform-specific parsers 1901 // (like the ELF parser) register themselves as extensions. 1902 // 3. The generic directive parser implemented by this class. These are 1903 // all the directives that behave in a target and platform independent 1904 // manner, or at least have a default behavior that's shared between 1905 // all targets and platforms. 1906 1907 getTargetParser().flushPendingInstructions(getStreamer()); 1908 1909 SMLoc StartTokLoc = getTok().getLoc(); 1910 bool TPDirectiveReturn = getTargetParser().ParseDirective(ID); 1911 1912 if (hasPendingError()) 1913 return true; 1914 // Currently the return value should be true if we are 1915 // uninterested but as this is at odds with the standard parsing 1916 // convention (return true = error) we have instances of a parsed 1917 // directive that fails returning true as an error. Catch these 1918 // cases as best as possible errors here. 1919 if (TPDirectiveReturn && StartTokLoc != getTok().getLoc()) 1920 return true; 1921 // Return if we did some parsing or believe we succeeded. 1922 if (!TPDirectiveReturn || StartTokLoc != getTok().getLoc()) 1923 return false; 1924 1925 // Next, check the extension directive map to see if any extension has 1926 // registered itself to parse this directive. 1927 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = 1928 ExtensionDirectiveMap.lookup(IDVal); 1929 if (Handler.first) 1930 return (*Handler.second)(Handler.first, IDVal, IDLoc); 1931 1932 // Finally, if no one else is interested in this directive, it must be 1933 // generic and familiar to this class. 1934 switch (DirKind) { 1935 default: 1936 break; 1937 case DK_SET: 1938 case DK_EQU: 1939 return parseDirectiveSet(IDVal, true); 1940 case DK_EQUIV: 1941 return parseDirectiveSet(IDVal, false); 1942 case DK_ASCII: 1943 return parseDirectiveAscii(IDVal, false); 1944 case DK_ASCIZ: 1945 case DK_STRING: 1946 return parseDirectiveAscii(IDVal, true); 1947 case DK_BYTE: 1948 case DK_DC_B: 1949 return parseDirectiveValue(IDVal, 1); 1950 case DK_DC: 1951 case DK_DC_W: 1952 case DK_SHORT: 1953 case DK_VALUE: 1954 case DK_2BYTE: 1955 return parseDirectiveValue(IDVal, 2); 1956 case DK_LONG: 1957 case DK_INT: 1958 case DK_4BYTE: 1959 case DK_DC_L: 1960 return parseDirectiveValue(IDVal, 4); 1961 case DK_QUAD: 1962 case DK_8BYTE: 1963 return parseDirectiveValue(IDVal, 8); 1964 case DK_DC_A: 1965 return parseDirectiveValue( 1966 IDVal, getContext().getAsmInfo()->getCodePointerSize()); 1967 case DK_OCTA: 1968 return parseDirectiveOctaValue(IDVal); 1969 case DK_SINGLE: 1970 case DK_FLOAT: 1971 case DK_DC_S: 1972 return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); 1973 case DK_DOUBLE: 1974 case DK_DC_D: 1975 return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); 1976 case DK_ALIGN: { 1977 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); 1978 return parseDirectiveAlign(IsPow2, /*ExprSize=*/1); 1979 } 1980 case DK_ALIGN32: { 1981 bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); 1982 return parseDirectiveAlign(IsPow2, /*ExprSize=*/4); 1983 } 1984 case DK_BALIGN: 1985 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1); 1986 case DK_BALIGNW: 1987 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2); 1988 case DK_BALIGNL: 1989 return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4); 1990 case DK_P2ALIGN: 1991 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1); 1992 case DK_P2ALIGNW: 1993 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2); 1994 case DK_P2ALIGNL: 1995 return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4); 1996 case DK_ORG: 1997 return parseDirectiveOrg(); 1998 case DK_FILL: 1999 return parseDirectiveFill(); 2000 case DK_ZERO: 2001 return parseDirectiveZero(); 2002 case DK_EXTERN: 2003 eatToEndOfStatement(); // .extern is the default, ignore it. 2004 return false; 2005 case DK_GLOBL: 2006 case DK_GLOBAL: 2007 return parseDirectiveSymbolAttribute(MCSA_Global); 2008 case DK_LAZY_REFERENCE: 2009 return parseDirectiveSymbolAttribute(MCSA_LazyReference); 2010 case DK_NO_DEAD_STRIP: 2011 return parseDirectiveSymbolAttribute(MCSA_NoDeadStrip); 2012 case DK_SYMBOL_RESOLVER: 2013 return parseDirectiveSymbolAttribute(MCSA_SymbolResolver); 2014 case DK_PRIVATE_EXTERN: 2015 return parseDirectiveSymbolAttribute(MCSA_PrivateExtern); 2016 case DK_REFERENCE: 2017 return parseDirectiveSymbolAttribute(MCSA_Reference); 2018 case DK_WEAK_DEFINITION: 2019 return parseDirectiveSymbolAttribute(MCSA_WeakDefinition); 2020 case DK_WEAK_REFERENCE: 2021 return parseDirectiveSymbolAttribute(MCSA_WeakReference); 2022 case DK_WEAK_DEF_CAN_BE_HIDDEN: 2023 return parseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate); 2024 case DK_COLD: 2025 return parseDirectiveSymbolAttribute(MCSA_Cold); 2026 case DK_COMM: 2027 case DK_COMMON: 2028 return parseDirectiveComm(/*IsLocal=*/false); 2029 case DK_LCOMM: 2030 return parseDirectiveComm(/*IsLocal=*/true); 2031 case DK_ABORT: 2032 return parseDirectiveAbort(); 2033 case DK_INCLUDE: 2034 return parseDirectiveInclude(); 2035 case DK_INCBIN: 2036 return parseDirectiveIncbin(); 2037 case DK_CODE16: 2038 case DK_CODE16GCC: 2039 return TokError(Twine(IDVal) + 2040 " not currently supported for this target"); 2041 case DK_REPT: 2042 return parseDirectiveRept(IDLoc, IDVal); 2043 case DK_IRP: 2044 return parseDirectiveIrp(IDLoc); 2045 case DK_IRPC: 2046 return parseDirectiveIrpc(IDLoc); 2047 case DK_ENDR: 2048 return parseDirectiveEndr(IDLoc); 2049 case DK_BUNDLE_ALIGN_MODE: 2050 return parseDirectiveBundleAlignMode(); 2051 case DK_BUNDLE_LOCK: 2052 return parseDirectiveBundleLock(); 2053 case DK_BUNDLE_UNLOCK: 2054 return parseDirectiveBundleUnlock(); 2055 case DK_SLEB128: 2056 return parseDirectiveLEB128(true); 2057 case DK_ULEB128: 2058 return parseDirectiveLEB128(false); 2059 case DK_SPACE: 2060 case DK_SKIP: 2061 return parseDirectiveSpace(IDVal); 2062 case DK_FILE: 2063 return parseDirectiveFile(IDLoc); 2064 case DK_LINE: 2065 return parseDirectiveLine(); 2066 case DK_LOC: 2067 return parseDirectiveLoc(); 2068 case DK_STABS: 2069 return parseDirectiveStabs(); 2070 case DK_CV_FILE: 2071 return parseDirectiveCVFile(); 2072 case DK_CV_FUNC_ID: 2073 return parseDirectiveCVFuncId(); 2074 case DK_CV_INLINE_SITE_ID: 2075 return parseDirectiveCVInlineSiteId(); 2076 case DK_CV_LOC: 2077 return parseDirectiveCVLoc(); 2078 case DK_CV_LINETABLE: 2079 return parseDirectiveCVLinetable(); 2080 case DK_CV_INLINE_LINETABLE: 2081 return parseDirectiveCVInlineLinetable(); 2082 case DK_CV_DEF_RANGE: 2083 return parseDirectiveCVDefRange(); 2084 case DK_CV_STRING: 2085 return parseDirectiveCVString(); 2086 case DK_CV_STRINGTABLE: 2087 return parseDirectiveCVStringTable(); 2088 case DK_CV_FILECHECKSUMS: 2089 return parseDirectiveCVFileChecksums(); 2090 case DK_CV_FILECHECKSUM_OFFSET: 2091 return parseDirectiveCVFileChecksumOffset(); 2092 case DK_CV_FPO_DATA: 2093 return parseDirectiveCVFPOData(); 2094 case DK_CFI_SECTIONS: 2095 return parseDirectiveCFISections(); 2096 case DK_CFI_STARTPROC: 2097 return parseDirectiveCFIStartProc(); 2098 case DK_CFI_ENDPROC: 2099 return parseDirectiveCFIEndProc(); 2100 case DK_CFI_DEF_CFA: 2101 return parseDirectiveCFIDefCfa(IDLoc); 2102 case DK_CFI_DEF_CFA_OFFSET: 2103 return parseDirectiveCFIDefCfaOffset(); 2104 case DK_CFI_ADJUST_CFA_OFFSET: 2105 return parseDirectiveCFIAdjustCfaOffset(); 2106 case DK_CFI_DEF_CFA_REGISTER: 2107 return parseDirectiveCFIDefCfaRegister(IDLoc); 2108 case DK_CFI_OFFSET: 2109 return parseDirectiveCFIOffset(IDLoc); 2110 case DK_CFI_REL_OFFSET: 2111 return parseDirectiveCFIRelOffset(IDLoc); 2112 case DK_CFI_PERSONALITY: 2113 return parseDirectiveCFIPersonalityOrLsda(true); 2114 case DK_CFI_LSDA: 2115 return parseDirectiveCFIPersonalityOrLsda(false); 2116 case DK_CFI_REMEMBER_STATE: 2117 return parseDirectiveCFIRememberState(); 2118 case DK_CFI_RESTORE_STATE: 2119 return parseDirectiveCFIRestoreState(); 2120 case DK_CFI_SAME_VALUE: 2121 return parseDirectiveCFISameValue(IDLoc); 2122 case DK_CFI_RESTORE: 2123 return parseDirectiveCFIRestore(IDLoc); 2124 case DK_CFI_ESCAPE: 2125 return parseDirectiveCFIEscape(); 2126 case DK_CFI_RETURN_COLUMN: 2127 return parseDirectiveCFIReturnColumn(IDLoc); 2128 case DK_CFI_SIGNAL_FRAME: 2129 return parseDirectiveCFISignalFrame(); 2130 case DK_CFI_UNDEFINED: 2131 return parseDirectiveCFIUndefined(IDLoc); 2132 case DK_CFI_REGISTER: 2133 return parseDirectiveCFIRegister(IDLoc); 2134 case DK_CFI_WINDOW_SAVE: 2135 return parseDirectiveCFIWindowSave(); 2136 case DK_MACROS_ON: 2137 case DK_MACROS_OFF: 2138 return parseDirectiveMacrosOnOff(IDVal); 2139 case DK_MACRO: 2140 return parseDirectiveMacro(IDLoc); 2141 case DK_ALTMACRO: 2142 case DK_NOALTMACRO: 2143 return parseDirectiveAltmacro(IDVal); 2144 case DK_EXITM: 2145 return parseDirectiveExitMacro(IDVal); 2146 case DK_ENDM: 2147 case DK_ENDMACRO: 2148 return parseDirectiveEndMacro(IDVal); 2149 case DK_PURGEM: 2150 return parseDirectivePurgeMacro(IDLoc); 2151 case DK_END: 2152 return parseDirectiveEnd(IDLoc); 2153 case DK_ERR: 2154 return parseDirectiveError(IDLoc, false); 2155 case DK_ERROR: 2156 return parseDirectiveError(IDLoc, true); 2157 case DK_WARNING: 2158 return parseDirectiveWarning(IDLoc); 2159 case DK_RELOC: 2160 return parseDirectiveReloc(IDLoc); 2161 case DK_DCB: 2162 case DK_DCB_W: 2163 return parseDirectiveDCB(IDVal, 2); 2164 case DK_DCB_B: 2165 return parseDirectiveDCB(IDVal, 1); 2166 case DK_DCB_D: 2167 return parseDirectiveRealDCB(IDVal, APFloat::IEEEdouble()); 2168 case DK_DCB_L: 2169 return parseDirectiveDCB(IDVal, 4); 2170 case DK_DCB_S: 2171 return parseDirectiveRealDCB(IDVal, APFloat::IEEEsingle()); 2172 case DK_DC_X: 2173 case DK_DCB_X: 2174 return TokError(Twine(IDVal) + 2175 " not currently supported for this target"); 2176 case DK_DS: 2177 case DK_DS_W: 2178 return parseDirectiveDS(IDVal, 2); 2179 case DK_DS_B: 2180 return parseDirectiveDS(IDVal, 1); 2181 case DK_DS_D: 2182 return parseDirectiveDS(IDVal, 8); 2183 case DK_DS_L: 2184 case DK_DS_S: 2185 return parseDirectiveDS(IDVal, 4); 2186 case DK_DS_P: 2187 case DK_DS_X: 2188 return parseDirectiveDS(IDVal, 12); 2189 case DK_PRINT: 2190 return parseDirectivePrint(IDLoc); 2191 case DK_ADDRSIG: 2192 return parseDirectiveAddrsig(); 2193 case DK_ADDRSIG_SYM: 2194 return parseDirectiveAddrsigSym(); 2195 } 2196 2197 return Error(IDLoc, "unknown directive"); 2198 } 2199 2200 // __asm _emit or __asm __emit 2201 if (ParsingInlineAsm && (IDVal == "_emit" || IDVal == "__emit" || 2202 IDVal == "_EMIT" || IDVal == "__EMIT")) 2203 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size()); 2204 2205 // __asm align 2206 if (ParsingInlineAsm && (IDVal == "align" || IDVal == "ALIGN")) 2207 return parseDirectiveMSAlign(IDLoc, Info); 2208 2209 if (ParsingInlineAsm && (IDVal == "even" || IDVal == "EVEN")) 2210 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4); 2211 if (checkForValidSection()) 2212 return true; 2213 2214 // Canonicalize the opcode to lower case. 2215 std::string OpcodeStr = IDVal.lower(); 2216 ParseInstructionInfo IInfo(Info.AsmRewrites); 2217 bool ParseHadError = getTargetParser().ParseInstruction(IInfo, OpcodeStr, ID, 2218 Info.ParsedOperands); 2219 Info.ParseError = ParseHadError; 2220 2221 // Dump the parsed representation, if requested. 2222 if (getShowParsedOperands()) { 2223 SmallString<256> Str; 2224 raw_svector_ostream OS(Str); 2225 OS << "parsed instruction: ["; 2226 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) { 2227 if (i != 0) 2228 OS << ", "; 2229 Info.ParsedOperands[i]->print(OS); 2230 } 2231 OS << "]"; 2232 2233 printMessage(IDLoc, SourceMgr::DK_Note, OS.str()); 2234 } 2235 2236 // Fail even if ParseInstruction erroneously returns false. 2237 if (hasPendingError() || ParseHadError) 2238 return true; 2239 2240 // If we are generating dwarf for the current section then generate a .loc 2241 // directive for the instruction. 2242 if (!ParseHadError && enabledGenDwarfForAssembly() && 2243 getContext().getGenDwarfSectionSyms().count( 2244 getStreamer().getCurrentSectionOnly())) { 2245 unsigned Line; 2246 if (ActiveMacros.empty()) 2247 Line = SrcMgr.FindLineNumber(IDLoc, CurBuffer); 2248 else 2249 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc, 2250 ActiveMacros.front()->ExitBuffer); 2251 2252 // If we previously parsed a cpp hash file line comment then make sure the 2253 // current Dwarf File is for the CppHashFilename if not then emit the 2254 // Dwarf File table for it and adjust the line number for the .loc. 2255 if (!CppHashInfo.Filename.empty()) { 2256 unsigned FileNumber = getStreamer().EmitDwarfFileDirective( 2257 0, StringRef(), CppHashInfo.Filename); 2258 getContext().setGenDwarfFileNumber(FileNumber); 2259 2260 unsigned CppHashLocLineNo = 2261 SrcMgr.FindLineNumber(CppHashInfo.Loc, CppHashInfo.Buf); 2262 Line = CppHashInfo.LineNumber - 1 + (Line - CppHashLocLineNo); 2263 } 2264 2265 getStreamer().EmitDwarfLocDirective( 2266 getContext().getGenDwarfFileNumber(), Line, 0, 2267 DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0, 0, 0, 2268 StringRef()); 2269 } 2270 2271 // If parsing succeeded, match the instruction. 2272 if (!ParseHadError) { 2273 uint64_t ErrorInfo; 2274 if (getTargetParser().MatchAndEmitInstruction( 2275 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo, 2276 getTargetParser().isParsingInlineAsm())) 2277 return true; 2278 } 2279 return false; 2280 } 2281 2282 // Parse and erase curly braces marking block start/end 2283 bool 2284 AsmParser::parseCurlyBlockScope(SmallVectorImpl<AsmRewrite> &AsmStrRewrites) { 2285 // Identify curly brace marking block start/end 2286 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly)) 2287 return false; 2288 2289 SMLoc StartLoc = Lexer.getLoc(); 2290 Lex(); // Eat the brace 2291 if (Lexer.is(AsmToken::EndOfStatement)) 2292 Lex(); // Eat EndOfStatement following the brace 2293 2294 // Erase the block start/end brace from the output asm string 2295 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() - 2296 StartLoc.getPointer()); 2297 return true; 2298 } 2299 2300 /// parseCppHashLineFilenameComment as this: 2301 /// ::= # number "filename" 2302 bool AsmParser::parseCppHashLineFilenameComment(SMLoc L) { 2303 Lex(); // Eat the hash token. 2304 // Lexer only ever emits HashDirective if it fully formed if it's 2305 // done the checking already so this is an internal error. 2306 assert(getTok().is(AsmToken::Integer) && 2307 "Lexing Cpp line comment: Expected Integer"); 2308 int64_t LineNumber = getTok().getIntVal(); 2309 Lex(); 2310 assert(getTok().is(AsmToken::String) && 2311 "Lexing Cpp line comment: Expected String"); 2312 StringRef Filename = getTok().getString(); 2313 Lex(); 2314 2315 // Get rid of the enclosing quotes. 2316 Filename = Filename.substr(1, Filename.size() - 2); 2317 2318 // Save the SMLoc, Filename and LineNumber for later use by diagnostics 2319 // and possibly DWARF file info. 2320 CppHashInfo.Loc = L; 2321 CppHashInfo.Filename = Filename; 2322 CppHashInfo.LineNumber = LineNumber; 2323 CppHashInfo.Buf = CurBuffer; 2324 if (FirstCppHashFilename.empty()) 2325 FirstCppHashFilename = Filename; 2326 return false; 2327 } 2328 2329 /// will use the last parsed cpp hash line filename comment 2330 /// for the Filename and LineNo if any in the diagnostic. 2331 void AsmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) { 2332 const AsmParser *Parser = static_cast<const AsmParser *>(Context); 2333 raw_ostream &OS = errs(); 2334 2335 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr(); 2336 SMLoc DiagLoc = Diag.getLoc(); 2337 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); 2338 unsigned CppHashBuf = 2339 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc); 2340 2341 // Like SourceMgr::printMessage() we need to print the include stack if any 2342 // before printing the message. 2343 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc); 2344 if (!Parser->SavedDiagHandler && DiagCurBuffer && 2345 DiagCurBuffer != DiagSrcMgr.getMainFileID()) { 2346 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer); 2347 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS); 2348 } 2349 2350 // If we have not parsed a cpp hash line filename comment or the source 2351 // manager changed or buffer changed (like in a nested include) then just 2352 // print the normal diagnostic using its Filename and LineNo. 2353 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr || 2354 DiagBuf != CppHashBuf) { 2355 if (Parser->SavedDiagHandler) 2356 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext); 2357 else 2358 Diag.print(nullptr, OS); 2359 return; 2360 } 2361 2362 // Use the CppHashFilename and calculate a line number based on the 2363 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc 2364 // for the diagnostic. 2365 const std::string &Filename = Parser->CppHashInfo.Filename; 2366 2367 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf); 2368 int CppHashLocLineNo = 2369 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf); 2370 int LineNo = 2371 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo); 2372 2373 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo, 2374 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(), 2375 Diag.getLineContents(), Diag.getRanges()); 2376 2377 if (Parser->SavedDiagHandler) 2378 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext); 2379 else 2380 NewDiag.print(nullptr, OS); 2381 } 2382 2383 // FIXME: This is mostly duplicated from the function in AsmLexer.cpp. The 2384 // difference being that that function accepts '@' as part of identifiers and 2385 // we can't do that. AsmLexer.cpp should probably be changed to handle 2386 // '@' as a special case when needed. 2387 static bool isIdentifierChar(char c) { 2388 return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || 2389 c == '.'; 2390 } 2391 2392 bool AsmParser::expandMacro(raw_svector_ostream &OS, StringRef Body, 2393 ArrayRef<MCAsmMacroParameter> Parameters, 2394 ArrayRef<MCAsmMacroArgument> A, 2395 bool EnableAtPseudoVariable, SMLoc L) { 2396 unsigned NParameters = Parameters.size(); 2397 bool HasVararg = NParameters ? Parameters.back().Vararg : false; 2398 if ((!IsDarwin || NParameters != 0) && NParameters != A.size()) 2399 return Error(L, "Wrong number of arguments"); 2400 2401 // A macro without parameters is handled differently on Darwin: 2402 // gas accepts no arguments and does no substitutions 2403 while (!Body.empty()) { 2404 // Scan for the next substitution. 2405 std::size_t End = Body.size(), Pos = 0; 2406 for (; Pos != End; ++Pos) { 2407 // Check for a substitution or escape. 2408 if (IsDarwin && !NParameters) { 2409 // This macro has no parameters, look for $0, $1, etc. 2410 if (Body[Pos] != '$' || Pos + 1 == End) 2411 continue; 2412 2413 char Next = Body[Pos + 1]; 2414 if (Next == '$' || Next == 'n' || 2415 isdigit(static_cast<unsigned char>(Next))) 2416 break; 2417 } else { 2418 // This macro has parameters, look for \foo, \bar, etc. 2419 if (Body[Pos] == '\\' && Pos + 1 != End) 2420 break; 2421 } 2422 } 2423 2424 // Add the prefix. 2425 OS << Body.slice(0, Pos); 2426 2427 // Check if we reached the end. 2428 if (Pos == End) 2429 break; 2430 2431 if (IsDarwin && !NParameters) { 2432 switch (Body[Pos + 1]) { 2433 // $$ => $ 2434 case '$': 2435 OS << '$'; 2436 break; 2437 2438 // $n => number of arguments 2439 case 'n': 2440 OS << A.size(); 2441 break; 2442 2443 // $[0-9] => argument 2444 default: { 2445 // Missing arguments are ignored. 2446 unsigned Index = Body[Pos + 1] - '0'; 2447 if (Index >= A.size()) 2448 break; 2449 2450 // Otherwise substitute with the token values, with spaces eliminated. 2451 for (const AsmToken &Token : A[Index]) 2452 OS << Token.getString(); 2453 break; 2454 } 2455 } 2456 Pos += 2; 2457 } else { 2458 unsigned I = Pos + 1; 2459 2460 // Check for the \@ pseudo-variable. 2461 if (EnableAtPseudoVariable && Body[I] == '@' && I + 1 != End) 2462 ++I; 2463 else 2464 while (isIdentifierChar(Body[I]) && I + 1 != End) 2465 ++I; 2466 2467 const char *Begin = Body.data() + Pos + 1; 2468 StringRef Argument(Begin, I - (Pos + 1)); 2469 unsigned Index = 0; 2470 2471 if (Argument == "@") { 2472 OS << NumOfMacroInstantiations; 2473 Pos += 2; 2474 } else { 2475 for (; Index < NParameters; ++Index) 2476 if (Parameters[Index].Name == Argument) 2477 break; 2478 2479 if (Index == NParameters) { 2480 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') 2481 Pos += 3; 2482 else { 2483 OS << '\\' << Argument; 2484 Pos = I; 2485 } 2486 } else { 2487 bool VarargParameter = HasVararg && Index == (NParameters - 1); 2488 for (const AsmToken &Token : A[Index]) 2489 // For altmacro mode, you can write '%expr'. 2490 // The prefix '%' evaluates the expression 'expr' 2491 // and uses the result as a string (e.g. replace %(1+2) with the 2492 // string "3"). 2493 // Here, we identify the integer token which is the result of the 2494 // absolute expression evaluation and replace it with its string 2495 // representation. 2496 if (AltMacroMode && Token.getString().front() == '%' && 2497 Token.is(AsmToken::Integer)) 2498 // Emit an integer value to the buffer. 2499 OS << Token.getIntVal(); 2500 // Only Token that was validated as a string and begins with '<' 2501 // is considered altMacroString!!! 2502 else if (AltMacroMode && Token.getString().front() == '<' && 2503 Token.is(AsmToken::String)) { 2504 OS << altMacroString(Token.getStringContents()); 2505 } 2506 // We expect no quotes around the string's contents when 2507 // parsing for varargs. 2508 else if (Token.isNot(AsmToken::String) || VarargParameter) 2509 OS << Token.getString(); 2510 else 2511 OS << Token.getStringContents(); 2512 2513 Pos += 1 + Argument.size(); 2514 } 2515 } 2516 } 2517 // Update the scan point. 2518 Body = Body.substr(Pos); 2519 } 2520 2521 return false; 2522 } 2523 2524 MacroInstantiation::MacroInstantiation(SMLoc IL, int EB, SMLoc EL, 2525 size_t CondStackDepth) 2526 : InstantiationLoc(IL), ExitBuffer(EB), ExitLoc(EL), 2527 CondStackDepth(CondStackDepth) {} 2528 2529 static bool isOperator(AsmToken::TokenKind kind) { 2530 switch (kind) { 2531 default: 2532 return false; 2533 case AsmToken::Plus: 2534 case AsmToken::Minus: 2535 case AsmToken::Tilde: 2536 case AsmToken::Slash: 2537 case AsmToken::Star: 2538 case AsmToken::Dot: 2539 case AsmToken::Equal: 2540 case AsmToken::EqualEqual: 2541 case AsmToken::Pipe: 2542 case AsmToken::PipePipe: 2543 case AsmToken::Caret: 2544 case AsmToken::Amp: 2545 case AsmToken::AmpAmp: 2546 case AsmToken::Exclaim: 2547 case AsmToken::ExclaimEqual: 2548 case AsmToken::Less: 2549 case AsmToken::LessEqual: 2550 case AsmToken::LessLess: 2551 case AsmToken::LessGreater: 2552 case AsmToken::Greater: 2553 case AsmToken::GreaterEqual: 2554 case AsmToken::GreaterGreater: 2555 return true; 2556 } 2557 } 2558 2559 namespace { 2560 2561 class AsmLexerSkipSpaceRAII { 2562 public: 2563 AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { 2564 Lexer.setSkipSpace(SkipSpace); 2565 } 2566 2567 ~AsmLexerSkipSpaceRAII() { 2568 Lexer.setSkipSpace(true); 2569 } 2570 2571 private: 2572 AsmLexer &Lexer; 2573 }; 2574 2575 } // end anonymous namespace 2576 2577 bool AsmParser::parseMacroArgument(MCAsmMacroArgument &MA, bool Vararg) { 2578 2579 if (Vararg) { 2580 if (Lexer.isNot(AsmToken::EndOfStatement)) { 2581 StringRef Str = parseStringToEndOfStatement(); 2582 MA.emplace_back(AsmToken::String, Str); 2583 } 2584 return false; 2585 } 2586 2587 unsigned ParenLevel = 0; 2588 2589 // Darwin doesn't use spaces to delmit arguments. 2590 AsmLexerSkipSpaceRAII ScopedSkipSpace(Lexer, IsDarwin); 2591 2592 bool SpaceEaten; 2593 2594 while (true) { 2595 SpaceEaten = false; 2596 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal)) 2597 return TokError("unexpected token in macro instantiation"); 2598 2599 if (ParenLevel == 0) { 2600 2601 if (Lexer.is(AsmToken::Comma)) 2602 break; 2603 2604 if (Lexer.is(AsmToken::Space)) { 2605 SpaceEaten = true; 2606 Lexer.Lex(); // Eat spaces 2607 } 2608 2609 // Spaces can delimit parameters, but could also be part an expression. 2610 // If the token after a space is an operator, add the token and the next 2611 // one into this argument 2612 if (!IsDarwin) { 2613 if (isOperator(Lexer.getKind())) { 2614 MA.push_back(getTok()); 2615 Lexer.Lex(); 2616 2617 // Whitespace after an operator can be ignored. 2618 if (Lexer.is(AsmToken::Space)) 2619 Lexer.Lex(); 2620 2621 continue; 2622 } 2623 } 2624 if (SpaceEaten) 2625 break; 2626 } 2627 2628 // handleMacroEntry relies on not advancing the lexer here 2629 // to be able to fill in the remaining default parameter values 2630 if (Lexer.is(AsmToken::EndOfStatement)) 2631 break; 2632 2633 // Adjust the current parentheses level. 2634 if (Lexer.is(AsmToken::LParen)) 2635 ++ParenLevel; 2636 else if (Lexer.is(AsmToken::RParen) && ParenLevel) 2637 --ParenLevel; 2638 2639 // Append the token to the current argument list. 2640 MA.push_back(getTok()); 2641 Lexer.Lex(); 2642 } 2643 2644 if (ParenLevel != 0) 2645 return TokError("unbalanced parentheses in macro argument"); 2646 return false; 2647 } 2648 2649 // Parse the macro instantiation arguments. 2650 bool AsmParser::parseMacroArguments(const MCAsmMacro *M, 2651 MCAsmMacroArguments &A) { 2652 const unsigned NParameters = M ? M->Parameters.size() : 0; 2653 bool NamedParametersFound = false; 2654 SmallVector<SMLoc, 4> FALocs; 2655 2656 A.resize(NParameters); 2657 FALocs.resize(NParameters); 2658 2659 // Parse two kinds of macro invocations: 2660 // - macros defined without any parameters accept an arbitrary number of them 2661 // - macros defined with parameters accept at most that many of them 2662 bool HasVararg = NParameters ? M->Parameters.back().Vararg : false; 2663 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters; 2664 ++Parameter) { 2665 SMLoc IDLoc = Lexer.getLoc(); 2666 MCAsmMacroParameter FA; 2667 2668 if (Lexer.is(AsmToken::Identifier) && Lexer.peekTok().is(AsmToken::Equal)) { 2669 if (parseIdentifier(FA.Name)) 2670 return Error(IDLoc, "invalid argument identifier for formal argument"); 2671 2672 if (Lexer.isNot(AsmToken::Equal)) 2673 return TokError("expected '=' after formal parameter identifier"); 2674 2675 Lex(); 2676 2677 NamedParametersFound = true; 2678 } 2679 bool Vararg = HasVararg && Parameter == (NParameters - 1); 2680 2681 if (NamedParametersFound && FA.Name.empty()) 2682 return Error(IDLoc, "cannot mix positional and keyword arguments"); 2683 2684 SMLoc StrLoc = Lexer.getLoc(); 2685 SMLoc EndLoc; 2686 if (AltMacroMode && Lexer.is(AsmToken::Percent)) { 2687 const MCExpr *AbsoluteExp; 2688 int64_t Value; 2689 /// Eat '%' 2690 Lex(); 2691 if (parseExpression(AbsoluteExp, EndLoc)) 2692 return false; 2693 if (!AbsoluteExp->evaluateAsAbsolute(Value, 2694 getStreamer().getAssemblerPtr())) 2695 return Error(StrLoc, "expected absolute expression"); 2696 const char *StrChar = StrLoc.getPointer(); 2697 const char *EndChar = EndLoc.getPointer(); 2698 AsmToken newToken(AsmToken::Integer, 2699 StringRef(StrChar, EndChar - StrChar), Value); 2700 FA.Value.push_back(newToken); 2701 } else if (AltMacroMode && Lexer.is(AsmToken::Less) && 2702 isAltmacroString(StrLoc, EndLoc)) { 2703 const char *StrChar = StrLoc.getPointer(); 2704 const char *EndChar = EndLoc.getPointer(); 2705 jumpToLoc(EndLoc, CurBuffer); 2706 /// Eat from '<' to '>' 2707 Lex(); 2708 AsmToken newToken(AsmToken::String, 2709 StringRef(StrChar, EndChar - StrChar)); 2710 FA.Value.push_back(newToken); 2711 } else if(parseMacroArgument(FA.Value, Vararg)) 2712 return true; 2713 2714 unsigned PI = Parameter; 2715 if (!FA.Name.empty()) { 2716 unsigned FAI = 0; 2717 for (FAI = 0; FAI < NParameters; ++FAI) 2718 if (M->Parameters[FAI].Name == FA.Name) 2719 break; 2720 2721 if (FAI >= NParameters) { 2722 assert(M && "expected macro to be defined"); 2723 return Error(IDLoc, "parameter named '" + FA.Name + 2724 "' does not exist for macro '" + M->Name + "'"); 2725 } 2726 PI = FAI; 2727 } 2728 2729 if (!FA.Value.empty()) { 2730 if (A.size() <= PI) 2731 A.resize(PI + 1); 2732 A[PI] = FA.Value; 2733 2734 if (FALocs.size() <= PI) 2735 FALocs.resize(PI + 1); 2736 2737 FALocs[PI] = Lexer.getLoc(); 2738 } 2739 2740 // At the end of the statement, fill in remaining arguments that have 2741 // default values. If there aren't any, then the next argument is 2742 // required but missing 2743 if (Lexer.is(AsmToken::EndOfStatement)) { 2744 bool Failure = false; 2745 for (unsigned FAI = 0; FAI < NParameters; ++FAI) { 2746 if (A[FAI].empty()) { 2747 if (M->Parameters[FAI].Required) { 2748 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(), 2749 "missing value for required parameter " 2750 "'" + M->Parameters[FAI].Name + "' in macro '" + M->Name + "'"); 2751 Failure = true; 2752 } 2753 2754 if (!M->Parameters[FAI].Value.empty()) 2755 A[FAI] = M->Parameters[FAI].Value; 2756 } 2757 } 2758 return Failure; 2759 } 2760 2761 if (Lexer.is(AsmToken::Comma)) 2762 Lex(); 2763 } 2764 2765 return TokError("too many positional arguments"); 2766 } 2767 2768 bool AsmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc) { 2769 // Arbitrarily limit macro nesting depth (default matches 'as'). We can 2770 // eliminate this, although we should protect against infinite loops. 2771 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth; 2772 if (ActiveMacros.size() == MaxNestingDepth) { 2773 std::ostringstream MaxNestingDepthError; 2774 MaxNestingDepthError << "macros cannot be nested more than " 2775 << MaxNestingDepth << " levels deep." 2776 << " Use -asm-macro-max-nesting-depth to increase " 2777 "this limit."; 2778 return TokError(MaxNestingDepthError.str()); 2779 } 2780 2781 MCAsmMacroArguments A; 2782 if (parseMacroArguments(M, A)) 2783 return true; 2784 2785 // Macro instantiation is lexical, unfortunately. We construct a new buffer 2786 // to hold the macro body with substitutions. 2787 SmallString<256> Buf; 2788 StringRef Body = M->Body; 2789 raw_svector_ostream OS(Buf); 2790 2791 if (expandMacro(OS, Body, M->Parameters, A, true, getTok().getLoc())) 2792 return true; 2793 2794 // We include the .endmacro in the buffer as our cue to exit the macro 2795 // instantiation. 2796 OS << ".endmacro\n"; 2797 2798 std::unique_ptr<MemoryBuffer> Instantiation = 2799 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); 2800 2801 // Create the macro instantiation object and add to the current macro 2802 // instantiation stack. 2803 MacroInstantiation *MI = new MacroInstantiation( 2804 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); 2805 ActiveMacros.push_back(MI); 2806 2807 ++NumOfMacroInstantiations; 2808 2809 // Jump to the macro instantiation and prime the lexer. 2810 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); 2811 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); 2812 Lex(); 2813 2814 return false; 2815 } 2816 2817 void AsmParser::handleMacroExit() { 2818 // Jump to the EndOfStatement we should return to, and consume it. 2819 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer); 2820 Lex(); 2821 2822 // Pop the instantiation entry. 2823 delete ActiveMacros.back(); 2824 ActiveMacros.pop_back(); 2825 } 2826 2827 bool AsmParser::parseAssignment(StringRef Name, bool allow_redef, 2828 bool NoDeadStrip) { 2829 MCSymbol *Sym; 2830 const MCExpr *Value; 2831 if (MCParserUtils::parseAssignmentExpression(Name, allow_redef, *this, Sym, 2832 Value)) 2833 return true; 2834 2835 if (!Sym) { 2836 // In the case where we parse an expression starting with a '.', we will 2837 // not generate an error, nor will we create a symbol. In this case we 2838 // should just return out. 2839 return false; 2840 } 2841 2842 // Do the assignment. 2843 Out.EmitAssignment(Sym, Value); 2844 if (NoDeadStrip) 2845 Out.EmitSymbolAttribute(Sym, MCSA_NoDeadStrip); 2846 2847 return false; 2848 } 2849 2850 /// parseIdentifier: 2851 /// ::= identifier 2852 /// ::= string 2853 bool AsmParser::parseIdentifier(StringRef &Res) { 2854 // The assembler has relaxed rules for accepting identifiers, in particular we 2855 // allow things like '.globl $foo' and '.def @feat.00', which would normally be 2856 // separate tokens. At this level, we have already lexed so we cannot (currently) 2857 // handle this as a context dependent token, instead we detect adjacent tokens 2858 // and return the combined identifier. 2859 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) { 2860 SMLoc PrefixLoc = getLexer().getLoc(); 2861 2862 // Consume the prefix character, and check for a following identifier. 2863 2864 AsmToken Buf[1]; 2865 Lexer.peekTokens(Buf, false); 2866 2867 if (Buf[0].isNot(AsmToken::Identifier)) 2868 return true; 2869 2870 // We have a '$' or '@' followed by an identifier, make sure they are adjacent. 2871 if (PrefixLoc.getPointer() + 1 != Buf[0].getLoc().getPointer()) 2872 return true; 2873 2874 // eat $ or @ 2875 Lexer.Lex(); // Lexer's Lex guarantees consecutive token. 2876 // Construct the joined identifier and consume the token. 2877 Res = 2878 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1); 2879 Lex(); // Parser Lex to maintain invariants. 2880 return false; 2881 } 2882 2883 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String)) 2884 return true; 2885 2886 Res = getTok().getIdentifier(); 2887 2888 Lex(); // Consume the identifier token. 2889 2890 return false; 2891 } 2892 2893 /// parseDirectiveSet: 2894 /// ::= .equ identifier ',' expression 2895 /// ::= .equiv identifier ',' expression 2896 /// ::= .set identifier ',' expression 2897 bool AsmParser::parseDirectiveSet(StringRef IDVal, bool allow_redef) { 2898 StringRef Name; 2899 if (check(parseIdentifier(Name), "expected identifier") || 2900 parseToken(AsmToken::Comma) || parseAssignment(Name, allow_redef, true)) 2901 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); 2902 return false; 2903 } 2904 2905 bool AsmParser::parseEscapedString(std::string &Data) { 2906 if (check(getTok().isNot(AsmToken::String), "expected string")) 2907 return true; 2908 2909 Data = ""; 2910 StringRef Str = getTok().getStringContents(); 2911 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 2912 if (Str[i] != '\\') { 2913 Data += Str[i]; 2914 continue; 2915 } 2916 2917 // Recognize escaped characters. Note that this escape semantics currently 2918 // loosely follows Darwin 'as'. 2919 ++i; 2920 if (i == e) 2921 return TokError("unexpected backslash at end of string"); 2922 2923 // Recognize hex sequences similarly to GNU 'as'. 2924 if (Str[i] == 'x' || Str[i] == 'X') { 2925 size_t length = Str.size(); 2926 if (i + 1 >= length || !isHexDigit(Str[i + 1])) 2927 return TokError("invalid hexadecimal escape sequence"); 2928 2929 // Consume hex characters. GNU 'as' reads all hexadecimal characters and 2930 // then truncates to the lower 16 bits. Seems reasonable. 2931 unsigned Value = 0; 2932 while (i + 1 < length && isHexDigit(Str[i + 1])) 2933 Value = Value * 16 + hexDigitValue(Str[++i]); 2934 2935 Data += (unsigned char)(Value & 0xFF); 2936 continue; 2937 } 2938 2939 // Recognize octal sequences. 2940 if ((unsigned)(Str[i] - '0') <= 7) { 2941 // Consume up to three octal characters. 2942 unsigned Value = Str[i] - '0'; 2943 2944 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { 2945 ++i; 2946 Value = Value * 8 + (Str[i] - '0'); 2947 2948 if (i + 1 != e && ((unsigned)(Str[i + 1] - '0')) <= 7) { 2949 ++i; 2950 Value = Value * 8 + (Str[i] - '0'); 2951 } 2952 } 2953 2954 if (Value > 255) 2955 return TokError("invalid octal escape sequence (out of range)"); 2956 2957 Data += (unsigned char)Value; 2958 continue; 2959 } 2960 2961 // Otherwise recognize individual escapes. 2962 switch (Str[i]) { 2963 default: 2964 // Just reject invalid escape sequences for now. 2965 return TokError("invalid escape sequence (unrecognized character)"); 2966 2967 case 'b': Data += '\b'; break; 2968 case 'f': Data += '\f'; break; 2969 case 'n': Data += '\n'; break; 2970 case 'r': Data += '\r'; break; 2971 case 't': Data += '\t'; break; 2972 case '"': Data += '"'; break; 2973 case '\\': Data += '\\'; break; 2974 } 2975 } 2976 2977 Lex(); 2978 return false; 2979 } 2980 2981 /// parseDirectiveAscii: 2982 /// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ] 2983 bool AsmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) { 2984 auto parseOp = [&]() -> bool { 2985 std::string Data; 2986 if (checkForValidSection() || parseEscapedString(Data)) 2987 return true; 2988 getStreamer().EmitBytes(Data); 2989 if (ZeroTerminated) 2990 getStreamer().EmitBytes(StringRef("\0", 1)); 2991 return false; 2992 }; 2993 2994 if (parseMany(parseOp)) 2995 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); 2996 return false; 2997 } 2998 2999 /// parseDirectiveReloc 3000 /// ::= .reloc expression , identifier [ , expression ] 3001 bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { 3002 const MCExpr *Offset; 3003 const MCExpr *Expr = nullptr; 3004 int64_t OffsetValue; 3005 SMLoc OffsetLoc = Lexer.getTok().getLoc(); 3006 3007 if (parseExpression(Offset)) 3008 return true; 3009 3010 if ((Offset->evaluateAsAbsolute(OffsetValue, 3011 getStreamer().getAssemblerPtr()) && 3012 check(OffsetValue < 0, OffsetLoc, "expression is negative")) || 3013 (check(Offset->getKind() != llvm::MCExpr::Constant && 3014 Offset->getKind() != llvm::MCExpr::SymbolRef, 3015 OffsetLoc, "expected non-negative number or a label")) || 3016 (parseToken(AsmToken::Comma, "expected comma") || 3017 check(getTok().isNot(AsmToken::Identifier), "expected relocation name"))) 3018 return true; 3019 3020 SMLoc NameLoc = Lexer.getTok().getLoc(); 3021 StringRef Name = Lexer.getTok().getIdentifier(); 3022 Lex(); 3023 3024 if (Lexer.is(AsmToken::Comma)) { 3025 Lex(); 3026 SMLoc ExprLoc = Lexer.getLoc(); 3027 if (parseExpression(Expr)) 3028 return true; 3029 3030 MCValue Value; 3031 if (!Expr->evaluateAsRelocatable(Value, nullptr, nullptr)) 3032 return Error(ExprLoc, "expression must be relocatable"); 3033 } 3034 3035 if (parseToken(AsmToken::EndOfStatement, 3036 "unexpected token in .reloc directive")) 3037 return true; 3038 3039 const MCTargetAsmParser &MCT = getTargetParser(); 3040 const MCSubtargetInfo &STI = MCT.getSTI(); 3041 if (getStreamer().EmitRelocDirective(*Offset, Name, Expr, DirectiveLoc, STI)) 3042 return Error(NameLoc, "unknown relocation name"); 3043 3044 return false; 3045 } 3046 3047 /// parseDirectiveValue 3048 /// ::= (.byte | .short | ... ) [ expression (, expression)* ] 3049 bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) { 3050 auto parseOp = [&]() -> bool { 3051 const MCExpr *Value; 3052 SMLoc ExprLoc = getLexer().getLoc(); 3053 if (checkForValidSection() || parseExpression(Value)) 3054 return true; 3055 // Special case constant expressions to match code generator. 3056 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 3057 assert(Size <= 8 && "Invalid size"); 3058 uint64_t IntValue = MCE->getValue(); 3059 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) 3060 return Error(ExprLoc, "out of range literal value"); 3061 getStreamer().EmitIntValue(IntValue, Size); 3062 } else 3063 getStreamer().EmitValue(Value, Size, ExprLoc); 3064 return false; 3065 }; 3066 3067 if (parseMany(parseOp)) 3068 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); 3069 return false; 3070 } 3071 3072 static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) { 3073 if (Asm.getTok().isNot(AsmToken::Integer) && 3074 Asm.getTok().isNot(AsmToken::BigNum)) 3075 return Asm.TokError("unknown token in expression"); 3076 SMLoc ExprLoc = Asm.getTok().getLoc(); 3077 APInt IntValue = Asm.getTok().getAPIntVal(); 3078 Asm.Lex(); 3079 if (!IntValue.isIntN(128)) 3080 return Asm.Error(ExprLoc, "out of range literal value"); 3081 if (!IntValue.isIntN(64)) { 3082 hi = IntValue.getHiBits(IntValue.getBitWidth() - 64).getZExtValue(); 3083 lo = IntValue.getLoBits(64).getZExtValue(); 3084 } else { 3085 hi = 0; 3086 lo = IntValue.getZExtValue(); 3087 } 3088 return false; 3089 } 3090 3091 /// ParseDirectiveOctaValue 3092 /// ::= .octa [ hexconstant (, hexconstant)* ] 3093 3094 bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) { 3095 auto parseOp = [&]() -> bool { 3096 if (checkForValidSection()) 3097 return true; 3098 uint64_t hi, lo; 3099 if (parseHexOcta(*this, hi, lo)) 3100 return true; 3101 if (MAI.isLittleEndian()) { 3102 getStreamer().EmitIntValue(lo, 8); 3103 getStreamer().EmitIntValue(hi, 8); 3104 } else { 3105 getStreamer().EmitIntValue(hi, 8); 3106 getStreamer().EmitIntValue(lo, 8); 3107 } 3108 return false; 3109 }; 3110 3111 if (parseMany(parseOp)) 3112 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); 3113 return false; 3114 } 3115 3116 bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) { 3117 // We don't truly support arithmetic on floating point expressions, so we 3118 // have to manually parse unary prefixes. 3119 bool IsNeg = false; 3120 if (getLexer().is(AsmToken::Minus)) { 3121 Lexer.Lex(); 3122 IsNeg = true; 3123 } else if (getLexer().is(AsmToken::Plus)) 3124 Lexer.Lex(); 3125 3126 if (Lexer.is(AsmToken::Error)) 3127 return TokError(Lexer.getErr()); 3128 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) && 3129 Lexer.isNot(AsmToken::Identifier)) 3130 return TokError("unexpected token in directive"); 3131 3132 // Convert to an APFloat. 3133 APFloat Value(Semantics); 3134 StringRef IDVal = getTok().getString(); 3135 if (getLexer().is(AsmToken::Identifier)) { 3136 if (!IDVal.compare_lower("infinity") || !IDVal.compare_lower("inf")) 3137 Value = APFloat::getInf(Semantics); 3138 else if (!IDVal.compare_lower("nan")) 3139 Value = APFloat::getNaN(Semantics, false, ~0); 3140 else 3141 return TokError("invalid floating point literal"); 3142 } else if (Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) == 3143 APFloat::opInvalidOp) 3144 return TokError("invalid floating point literal"); 3145 if (IsNeg) 3146 Value.changeSign(); 3147 3148 // Consume the numeric token. 3149 Lex(); 3150 3151 Res = Value.bitcastToAPInt(); 3152 3153 return false; 3154 } 3155 3156 /// parseDirectiveRealValue 3157 /// ::= (.single | .double) [ expression (, expression)* ] 3158 bool AsmParser::parseDirectiveRealValue(StringRef IDVal, 3159 const fltSemantics &Semantics) { 3160 auto parseOp = [&]() -> bool { 3161 APInt AsInt; 3162 if (checkForValidSection() || parseRealValue(Semantics, AsInt)) 3163 return true; 3164 getStreamer().EmitIntValue(AsInt.getLimitedValue(), 3165 AsInt.getBitWidth() / 8); 3166 return false; 3167 }; 3168 3169 if (parseMany(parseOp)) 3170 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive"); 3171 return false; 3172 } 3173 3174 /// parseDirectiveZero 3175 /// ::= .zero expression 3176 bool AsmParser::parseDirectiveZero() { 3177 SMLoc NumBytesLoc = Lexer.getLoc(); 3178 const MCExpr *NumBytes; 3179 if (checkForValidSection() || parseExpression(NumBytes)) 3180 return true; 3181 3182 int64_t Val = 0; 3183 if (getLexer().is(AsmToken::Comma)) { 3184 Lex(); 3185 if (parseAbsoluteExpression(Val)) 3186 return true; 3187 } 3188 3189 if (parseToken(AsmToken::EndOfStatement, 3190 "unexpected token in '.zero' directive")) 3191 return true; 3192 getStreamer().emitFill(*NumBytes, Val, NumBytesLoc); 3193 3194 return false; 3195 } 3196 3197 /// parseDirectiveFill 3198 /// ::= .fill expression [ , expression [ , expression ] ] 3199 bool AsmParser::parseDirectiveFill() { 3200 SMLoc NumValuesLoc = Lexer.getLoc(); 3201 const MCExpr *NumValues; 3202 if (checkForValidSection() || parseExpression(NumValues)) 3203 return true; 3204 3205 int64_t FillSize = 1; 3206 int64_t FillExpr = 0; 3207 3208 SMLoc SizeLoc, ExprLoc; 3209 3210 if (parseOptionalToken(AsmToken::Comma)) { 3211 SizeLoc = getTok().getLoc(); 3212 if (parseAbsoluteExpression(FillSize)) 3213 return true; 3214 if (parseOptionalToken(AsmToken::Comma)) { 3215 ExprLoc = getTok().getLoc(); 3216 if (parseAbsoluteExpression(FillExpr)) 3217 return true; 3218 } 3219 } 3220 if (parseToken(AsmToken::EndOfStatement, 3221 "unexpected token in '.fill' directive")) 3222 return true; 3223 3224 if (FillSize < 0) { 3225 Warning(SizeLoc, "'.fill' directive with negative size has no effect"); 3226 return false; 3227 } 3228 if (FillSize > 8) { 3229 Warning(SizeLoc, "'.fill' directive with size greater than 8 has been truncated to 8"); 3230 FillSize = 8; 3231 } 3232 3233 if (!isUInt<32>(FillExpr) && FillSize > 4) 3234 Warning(ExprLoc, "'.fill' directive pattern has been truncated to 32-bits"); 3235 3236 getStreamer().emitFill(*NumValues, FillSize, FillExpr, NumValuesLoc); 3237 3238 return false; 3239 } 3240 3241 /// parseDirectiveOrg 3242 /// ::= .org expression [ , expression ] 3243 bool AsmParser::parseDirectiveOrg() { 3244 const MCExpr *Offset; 3245 SMLoc OffsetLoc = Lexer.getLoc(); 3246 if (checkForValidSection() || parseExpression(Offset)) 3247 return true; 3248 3249 // Parse optional fill expression. 3250 int64_t FillExpr = 0; 3251 if (parseOptionalToken(AsmToken::Comma)) 3252 if (parseAbsoluteExpression(FillExpr)) 3253 return addErrorSuffix(" in '.org' directive"); 3254 if (parseToken(AsmToken::EndOfStatement)) 3255 return addErrorSuffix(" in '.org' directive"); 3256 3257 getStreamer().emitValueToOffset(Offset, FillExpr, OffsetLoc); 3258 return false; 3259 } 3260 3261 /// parseDirectiveAlign 3262 /// ::= {.align, ...} expression [ , expression [ , expression ]] 3263 bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { 3264 SMLoc AlignmentLoc = getLexer().getLoc(); 3265 int64_t Alignment; 3266 SMLoc MaxBytesLoc; 3267 bool HasFillExpr = false; 3268 int64_t FillExpr = 0; 3269 int64_t MaxBytesToFill = 0; 3270 3271 auto parseAlign = [&]() -> bool { 3272 if (parseAbsoluteExpression(Alignment)) 3273 return true; 3274 if (parseOptionalToken(AsmToken::Comma)) { 3275 // The fill expression can be omitted while specifying a maximum number of 3276 // alignment bytes, e.g: 3277 // .align 3,,4 3278 if (getTok().isNot(AsmToken::Comma)) { 3279 HasFillExpr = true; 3280 if (parseAbsoluteExpression(FillExpr)) 3281 return true; 3282 } 3283 if (parseOptionalToken(AsmToken::Comma)) 3284 if (parseTokenLoc(MaxBytesLoc) || 3285 parseAbsoluteExpression(MaxBytesToFill)) 3286 return true; 3287 } 3288 return parseToken(AsmToken::EndOfStatement); 3289 }; 3290 3291 if (checkForValidSection()) 3292 return addErrorSuffix(" in directive"); 3293 // Ignore empty '.p2align' directives for GNU-as compatibility 3294 if (IsPow2 && (ValueSize == 1) && getTok().is(AsmToken::EndOfStatement)) { 3295 Warning(AlignmentLoc, "p2align directive with no operand(s) is ignored"); 3296 return parseToken(AsmToken::EndOfStatement); 3297 } 3298 if (parseAlign()) 3299 return addErrorSuffix(" in directive"); 3300 3301 // Always emit an alignment here even if we thrown an error. 3302 bool ReturnVal = false; 3303 3304 // Compute alignment in bytes. 3305 if (IsPow2) { 3306 // FIXME: Diagnose overflow. 3307 if (Alignment >= 32) { 3308 ReturnVal |= Error(AlignmentLoc, "invalid alignment value"); 3309 Alignment = 31; 3310 } 3311 3312 Alignment = 1ULL << Alignment; 3313 } else { 3314 // Reject alignments that aren't either a power of two or zero, 3315 // for gas compatibility. Alignment of zero is silently rounded 3316 // up to one. 3317 if (Alignment == 0) 3318 Alignment = 1; 3319 if (!isPowerOf2_64(Alignment)) 3320 ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2"); 3321 } 3322 3323 // Diagnose non-sensical max bytes to align. 3324 if (MaxBytesLoc.isValid()) { 3325 if (MaxBytesToFill < 1) { 3326 ReturnVal |= Error(MaxBytesLoc, 3327 "alignment directive can never be satisfied in this " 3328 "many bytes, ignoring maximum bytes expression"); 3329 MaxBytesToFill = 0; 3330 } 3331 3332 if (MaxBytesToFill >= Alignment) { 3333 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and " 3334 "has no effect"); 3335 MaxBytesToFill = 0; 3336 } 3337 } 3338 3339 // Check whether we should use optimal code alignment for this .align 3340 // directive. 3341 const MCSection *Section = getStreamer().getCurrentSectionOnly(); 3342 assert(Section && "must have section to emit alignment"); 3343 bool UseCodeAlign = Section->UseCodeAlign(); 3344 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) && 3345 ValueSize == 1 && UseCodeAlign) { 3346 getStreamer().EmitCodeAlignment(Alignment, MaxBytesToFill); 3347 } else { 3348 // FIXME: Target specific behavior about how the "extra" bytes are filled. 3349 getStreamer().EmitValueToAlignment(Alignment, FillExpr, ValueSize, 3350 MaxBytesToFill); 3351 } 3352 3353 return ReturnVal; 3354 } 3355 3356 /// parseDirectiveFile 3357 /// ::= .file filename 3358 /// ::= .file number [directory] filename [md5 checksum] [source source-text] 3359 bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { 3360 // FIXME: I'm not sure what this is. 3361 int64_t FileNumber = -1; 3362 if (getLexer().is(AsmToken::Integer)) { 3363 FileNumber = getTok().getIntVal(); 3364 Lex(); 3365 3366 if (FileNumber < 0) 3367 return TokError("negative file number"); 3368 } 3369 3370 std::string Path; 3371 3372 // Usually the directory and filename together, otherwise just the directory. 3373 // Allow the strings to have escaped octal character sequence. 3374 if (check(getTok().isNot(AsmToken::String), 3375 "unexpected token in '.file' directive") || 3376 parseEscapedString(Path)) 3377 return true; 3378 3379 StringRef Directory; 3380 StringRef Filename; 3381 std::string FilenameData; 3382 if (getLexer().is(AsmToken::String)) { 3383 if (check(FileNumber == -1, 3384 "explicit path specified, but no file number") || 3385 parseEscapedString(FilenameData)) 3386 return true; 3387 Filename = FilenameData; 3388 Directory = Path; 3389 } else { 3390 Filename = Path; 3391 } 3392 3393 uint64_t MD5Hi, MD5Lo; 3394 bool HasMD5 = false; 3395 3396 Optional<StringRef> Source; 3397 bool HasSource = false; 3398 std::string SourceString; 3399 3400 while (!parseOptionalToken(AsmToken::EndOfStatement)) { 3401 StringRef Keyword; 3402 if (check(getTok().isNot(AsmToken::Identifier), 3403 "unexpected token in '.file' directive") || 3404 parseIdentifier(Keyword)) 3405 return true; 3406 if (Keyword == "md5") { 3407 HasMD5 = true; 3408 if (check(FileNumber == -1, 3409 "MD5 checksum specified, but no file number") || 3410 parseHexOcta(*this, MD5Hi, MD5Lo)) 3411 return true; 3412 } else if (Keyword == "source") { 3413 HasSource = true; 3414 if (check(FileNumber == -1, 3415 "source specified, but no file number") || 3416 check(getTok().isNot(AsmToken::String), 3417 "unexpected token in '.file' directive") || 3418 parseEscapedString(SourceString)) 3419 return true; 3420 } else { 3421 return TokError("unexpected token in '.file' directive"); 3422 } 3423 } 3424 3425 if (FileNumber == -1) { 3426 // Ignore the directive if there is no number and the target doesn't support 3427 // numberless .file directives. This allows some portability of assembler 3428 // between different object file formats. 3429 if (getContext().getAsmInfo()->hasSingleParameterDotFile()) 3430 getStreamer().EmitFileDirective(Filename); 3431 } else { 3432 // In case there is a -g option as well as debug info from directive .file, 3433 // we turn off the -g option, directly use the existing debug info instead. 3434 // Throw away any implicit file table for the assembler source. 3435 if (Ctx.getGenDwarfForAssembly()) { 3436 Ctx.getMCDwarfLineTable(0).resetFileTable(); 3437 Ctx.setGenDwarfForAssembly(false); 3438 } 3439 3440 Optional<MD5::MD5Result> CKMem; 3441 if (HasMD5) { 3442 MD5::MD5Result Sum; 3443 for (unsigned i = 0; i != 8; ++i) { 3444 Sum.Bytes[i] = uint8_t(MD5Hi >> ((7 - i) * 8)); 3445 Sum.Bytes[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8)); 3446 } 3447 CKMem = Sum; 3448 } 3449 if (HasSource) { 3450 char *SourceBuf = static_cast<char *>(Ctx.allocate(SourceString.size())); 3451 memcpy(SourceBuf, SourceString.data(), SourceString.size()); 3452 Source = StringRef(SourceBuf, SourceString.size()); 3453 } 3454 if (FileNumber == 0) { 3455 if (Ctx.getDwarfVersion() < 5) 3456 return Warning(DirectiveLoc, "file 0 not supported prior to DWARF-5"); 3457 getStreamer().emitDwarfFile0Directive(Directory, Filename, CKMem, Source); 3458 } else { 3459 Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective( 3460 FileNumber, Directory, Filename, CKMem, Source); 3461 if (!FileNumOrErr) 3462 return Error(DirectiveLoc, toString(FileNumOrErr.takeError())); 3463 } 3464 // Alert the user if there are some .file directives with MD5 and some not. 3465 // But only do that once. 3466 if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(0)) { 3467 ReportedInconsistentMD5 = true; 3468 return Warning(DirectiveLoc, "inconsistent use of MD5 checksums"); 3469 } 3470 } 3471 3472 return false; 3473 } 3474 3475 /// parseDirectiveLine 3476 /// ::= .line [number] 3477 bool AsmParser::parseDirectiveLine() { 3478 int64_t LineNumber; 3479 if (getLexer().is(AsmToken::Integer)) { 3480 if (parseIntToken(LineNumber, "unexpected token in '.line' directive")) 3481 return true; 3482 (void)LineNumber; 3483 // FIXME: Do something with the .line. 3484 } 3485 if (parseToken(AsmToken::EndOfStatement, 3486 "unexpected token in '.line' directive")) 3487 return true; 3488 3489 return false; 3490 } 3491 3492 /// parseDirectiveLoc 3493 /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] 3494 /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] 3495 /// The first number is a file number, must have been previously assigned with 3496 /// a .file directive, the second number is the line number and optionally the 3497 /// third number is a column position (zero if not specified). The remaining 3498 /// optional items are .loc sub-directives. 3499 bool AsmParser::parseDirectiveLoc() { 3500 int64_t FileNumber = 0, LineNumber = 0; 3501 SMLoc Loc = getTok().getLoc(); 3502 if (parseIntToken(FileNumber, "unexpected token in '.loc' directive") || 3503 check(FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc, 3504 "file number less than one in '.loc' directive") || 3505 check(!getContext().isValidDwarfFileNumber(FileNumber), Loc, 3506 "unassigned file number in '.loc' directive")) 3507 return true; 3508 3509 // optional 3510 if (getLexer().is(AsmToken::Integer)) { 3511 LineNumber = getTok().getIntVal(); 3512 if (LineNumber < 0) 3513 return TokError("line number less than zero in '.loc' directive"); 3514 Lex(); 3515 } 3516 3517 int64_t ColumnPos = 0; 3518 if (getLexer().is(AsmToken::Integer)) { 3519 ColumnPos = getTok().getIntVal(); 3520 if (ColumnPos < 0) 3521 return TokError("column position less than zero in '.loc' directive"); 3522 Lex(); 3523 } 3524 3525 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 3526 unsigned Isa = 0; 3527 int64_t Discriminator = 0; 3528 3529 auto parseLocOp = [&]() -> bool { 3530 StringRef Name; 3531 SMLoc Loc = getTok().getLoc(); 3532 if (parseIdentifier(Name)) 3533 return TokError("unexpected token in '.loc' directive"); 3534 3535 if (Name == "basic_block") 3536 Flags |= DWARF2_FLAG_BASIC_BLOCK; 3537 else if (Name == "prologue_end") 3538 Flags |= DWARF2_FLAG_PROLOGUE_END; 3539 else if (Name == "epilogue_begin") 3540 Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; 3541 else if (Name == "is_stmt") { 3542 Loc = getTok().getLoc(); 3543 const MCExpr *Value; 3544 if (parseExpression(Value)) 3545 return true; 3546 // The expression must be the constant 0 or 1. 3547 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 3548 int Value = MCE->getValue(); 3549 if (Value == 0) 3550 Flags &= ~DWARF2_FLAG_IS_STMT; 3551 else if (Value == 1) 3552 Flags |= DWARF2_FLAG_IS_STMT; 3553 else 3554 return Error(Loc, "is_stmt value not 0 or 1"); 3555 } else { 3556 return Error(Loc, "is_stmt value not the constant value of 0 or 1"); 3557 } 3558 } else if (Name == "isa") { 3559 Loc = getTok().getLoc(); 3560 const MCExpr *Value; 3561 if (parseExpression(Value)) 3562 return true; 3563 // The expression must be a constant greater or equal to 0. 3564 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 3565 int Value = MCE->getValue(); 3566 if (Value < 0) 3567 return Error(Loc, "isa number less than zero"); 3568 Isa = Value; 3569 } else { 3570 return Error(Loc, "isa number not a constant value"); 3571 } 3572 } else if (Name == "discriminator") { 3573 if (parseAbsoluteExpression(Discriminator)) 3574 return true; 3575 } else { 3576 return Error(Loc, "unknown sub-directive in '.loc' directive"); 3577 } 3578 return false; 3579 }; 3580 3581 if (parseMany(parseLocOp, false /*hasComma*/)) 3582 return true; 3583 3584 getStreamer().EmitDwarfLocDirective(FileNumber, LineNumber, ColumnPos, Flags, 3585 Isa, Discriminator, StringRef()); 3586 3587 return false; 3588 } 3589 3590 /// parseDirectiveStabs 3591 /// ::= .stabs string, number, number, number 3592 bool AsmParser::parseDirectiveStabs() { 3593 return TokError("unsupported directive '.stabs'"); 3594 } 3595 3596 /// parseDirectiveCVFile 3597 /// ::= .cv_file number filename [checksum] [checksumkind] 3598 bool AsmParser::parseDirectiveCVFile() { 3599 SMLoc FileNumberLoc = getTok().getLoc(); 3600 int64_t FileNumber; 3601 std::string Filename; 3602 std::string Checksum; 3603 int64_t ChecksumKind = 0; 3604 3605 if (parseIntToken(FileNumber, 3606 "expected file number in '.cv_file' directive") || 3607 check(FileNumber < 1, FileNumberLoc, "file number less than one") || 3608 check(getTok().isNot(AsmToken::String), 3609 "unexpected token in '.cv_file' directive") || 3610 parseEscapedString(Filename)) 3611 return true; 3612 if (!parseOptionalToken(AsmToken::EndOfStatement)) { 3613 if (check(getTok().isNot(AsmToken::String), 3614 "unexpected token in '.cv_file' directive") || 3615 parseEscapedString(Checksum) || 3616 parseIntToken(ChecksumKind, 3617 "expected checksum kind in '.cv_file' directive") || 3618 parseToken(AsmToken::EndOfStatement, 3619 "unexpected token in '.cv_file' directive")) 3620 return true; 3621 } 3622 3623 Checksum = fromHex(Checksum); 3624 void *CKMem = Ctx.allocate(Checksum.size(), 1); 3625 memcpy(CKMem, Checksum.data(), Checksum.size()); 3626 ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem), 3627 Checksum.size()); 3628 3629 if (!getStreamer().EmitCVFileDirective(FileNumber, Filename, ChecksumAsBytes, 3630 static_cast<uint8_t>(ChecksumKind))) 3631 return Error(FileNumberLoc, "file number already allocated"); 3632 3633 return false; 3634 } 3635 3636 bool AsmParser::parseCVFunctionId(int64_t &FunctionId, 3637 StringRef DirectiveName) { 3638 SMLoc Loc; 3639 return parseTokenLoc(Loc) || 3640 parseIntToken(FunctionId, "expected function id in '" + DirectiveName + 3641 "' directive") || 3642 check(FunctionId < 0 || FunctionId >= UINT_MAX, Loc, 3643 "expected function id within range [0, UINT_MAX)"); 3644 } 3645 3646 bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) { 3647 SMLoc Loc; 3648 return parseTokenLoc(Loc) || 3649 parseIntToken(FileNumber, "expected integer in '" + DirectiveName + 3650 "' directive") || 3651 check(FileNumber < 1, Loc, "file number less than one in '" + 3652 DirectiveName + "' directive") || 3653 check(!getCVContext().isValidFileNumber(FileNumber), Loc, 3654 "unassigned file number in '" + DirectiveName + "' directive"); 3655 } 3656 3657 /// parseDirectiveCVFuncId 3658 /// ::= .cv_func_id FunctionId 3659 /// 3660 /// Introduces a function ID that can be used with .cv_loc. 3661 bool AsmParser::parseDirectiveCVFuncId() { 3662 SMLoc FunctionIdLoc = getTok().getLoc(); 3663 int64_t FunctionId; 3664 3665 if (parseCVFunctionId(FunctionId, ".cv_func_id") || 3666 parseToken(AsmToken::EndOfStatement, 3667 "unexpected token in '.cv_func_id' directive")) 3668 return true; 3669 3670 if (!getStreamer().EmitCVFuncIdDirective(FunctionId)) 3671 return Error(FunctionIdLoc, "function id already allocated"); 3672 3673 return false; 3674 } 3675 3676 /// parseDirectiveCVInlineSiteId 3677 /// ::= .cv_inline_site_id FunctionId 3678 /// "within" IAFunc 3679 /// "inlined_at" IAFile IALine [IACol] 3680 /// 3681 /// Introduces a function ID that can be used with .cv_loc. Includes "inlined 3682 /// at" source location information for use in the line table of the caller, 3683 /// whether the caller is a real function or another inlined call site. 3684 bool AsmParser::parseDirectiveCVInlineSiteId() { 3685 SMLoc FunctionIdLoc = getTok().getLoc(); 3686 int64_t FunctionId; 3687 int64_t IAFunc; 3688 int64_t IAFile; 3689 int64_t IALine; 3690 int64_t IACol = 0; 3691 3692 // FunctionId 3693 if (parseCVFunctionId(FunctionId, ".cv_inline_site_id")) 3694 return true; 3695 3696 // "within" 3697 if (check((getLexer().isNot(AsmToken::Identifier) || 3698 getTok().getIdentifier() != "within"), 3699 "expected 'within' identifier in '.cv_inline_site_id' directive")) 3700 return true; 3701 Lex(); 3702 3703 // IAFunc 3704 if (parseCVFunctionId(IAFunc, ".cv_inline_site_id")) 3705 return true; 3706 3707 // "inlined_at" 3708 if (check((getLexer().isNot(AsmToken::Identifier) || 3709 getTok().getIdentifier() != "inlined_at"), 3710 "expected 'inlined_at' identifier in '.cv_inline_site_id' " 3711 "directive") ) 3712 return true; 3713 Lex(); 3714 3715 // IAFile IALine 3716 if (parseCVFileId(IAFile, ".cv_inline_site_id") || 3717 parseIntToken(IALine, "expected line number after 'inlined_at'")) 3718 return true; 3719 3720 // [IACol] 3721 if (getLexer().is(AsmToken::Integer)) { 3722 IACol = getTok().getIntVal(); 3723 Lex(); 3724 } 3725 3726 if (parseToken(AsmToken::EndOfStatement, 3727 "unexpected token in '.cv_inline_site_id' directive")) 3728 return true; 3729 3730 if (!getStreamer().EmitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile, 3731 IALine, IACol, FunctionIdLoc)) 3732 return Error(FunctionIdLoc, "function id already allocated"); 3733 3734 return false; 3735 } 3736 3737 /// parseDirectiveCVLoc 3738 /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end] 3739 /// [is_stmt VALUE] 3740 /// The first number is a file number, must have been previously assigned with 3741 /// a .file directive, the second number is the line number and optionally the 3742 /// third number is a column position (zero if not specified). The remaining 3743 /// optional items are .loc sub-directives. 3744 bool AsmParser::parseDirectiveCVLoc() { 3745 SMLoc DirectiveLoc = getTok().getLoc(); 3746 int64_t FunctionId, FileNumber; 3747 if (parseCVFunctionId(FunctionId, ".cv_loc") || 3748 parseCVFileId(FileNumber, ".cv_loc")) 3749 return true; 3750 3751 int64_t LineNumber = 0; 3752 if (getLexer().is(AsmToken::Integer)) { 3753 LineNumber = getTok().getIntVal(); 3754 if (LineNumber < 0) 3755 return TokError("line number less than zero in '.cv_loc' directive"); 3756 Lex(); 3757 } 3758 3759 int64_t ColumnPos = 0; 3760 if (getLexer().is(AsmToken::Integer)) { 3761 ColumnPos = getTok().getIntVal(); 3762 if (ColumnPos < 0) 3763 return TokError("column position less than zero in '.cv_loc' directive"); 3764 Lex(); 3765 } 3766 3767 bool PrologueEnd = false; 3768 uint64_t IsStmt = 0; 3769 3770 auto parseOp = [&]() -> bool { 3771 StringRef Name; 3772 SMLoc Loc = getTok().getLoc(); 3773 if (parseIdentifier(Name)) 3774 return TokError("unexpected token in '.cv_loc' directive"); 3775 if (Name == "prologue_end") 3776 PrologueEnd = true; 3777 else if (Name == "is_stmt") { 3778 Loc = getTok().getLoc(); 3779 const MCExpr *Value; 3780 if (parseExpression(Value)) 3781 return true; 3782 // The expression must be the constant 0 or 1. 3783 IsStmt = ~0ULL; 3784 if (const auto *MCE = dyn_cast<MCConstantExpr>(Value)) 3785 IsStmt = MCE->getValue(); 3786 3787 if (IsStmt > 1) 3788 return Error(Loc, "is_stmt value not 0 or 1"); 3789 } else { 3790 return Error(Loc, "unknown sub-directive in '.cv_loc' directive"); 3791 } 3792 return false; 3793 }; 3794 3795 if (parseMany(parseOp, false /*hasComma*/)) 3796 return true; 3797 3798 getStreamer().EmitCVLocDirective(FunctionId, FileNumber, LineNumber, 3799 ColumnPos, PrologueEnd, IsStmt, StringRef(), 3800 DirectiveLoc); 3801 return false; 3802 } 3803 3804 /// parseDirectiveCVLinetable 3805 /// ::= .cv_linetable FunctionId, FnStart, FnEnd 3806 bool AsmParser::parseDirectiveCVLinetable() { 3807 int64_t FunctionId; 3808 StringRef FnStartName, FnEndName; 3809 SMLoc Loc = getTok().getLoc(); 3810 if (parseCVFunctionId(FunctionId, ".cv_linetable") || 3811 parseToken(AsmToken::Comma, 3812 "unexpected token in '.cv_linetable' directive") || 3813 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, 3814 "expected identifier in directive") || 3815 parseToken(AsmToken::Comma, 3816 "unexpected token in '.cv_linetable' directive") || 3817 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, 3818 "expected identifier in directive")) 3819 return true; 3820 3821 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); 3822 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); 3823 3824 getStreamer().EmitCVLinetableDirective(FunctionId, FnStartSym, FnEndSym); 3825 return false; 3826 } 3827 3828 /// parseDirectiveCVInlineLinetable 3829 /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd 3830 bool AsmParser::parseDirectiveCVInlineLinetable() { 3831 int64_t PrimaryFunctionId, SourceFileId, SourceLineNum; 3832 StringRef FnStartName, FnEndName; 3833 SMLoc Loc = getTok().getLoc(); 3834 if (parseCVFunctionId(PrimaryFunctionId, ".cv_inline_linetable") || 3835 parseTokenLoc(Loc) || 3836 parseIntToken( 3837 SourceFileId, 3838 "expected SourceField in '.cv_inline_linetable' directive") || 3839 check(SourceFileId <= 0, Loc, 3840 "File id less than zero in '.cv_inline_linetable' directive") || 3841 parseTokenLoc(Loc) || 3842 parseIntToken( 3843 SourceLineNum, 3844 "expected SourceLineNum in '.cv_inline_linetable' directive") || 3845 check(SourceLineNum < 0, Loc, 3846 "Line number less than zero in '.cv_inline_linetable' directive") || 3847 parseTokenLoc(Loc) || check(parseIdentifier(FnStartName), Loc, 3848 "expected identifier in directive") || 3849 parseTokenLoc(Loc) || check(parseIdentifier(FnEndName), Loc, 3850 "expected identifier in directive")) 3851 return true; 3852 3853 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) 3854 return true; 3855 3856 MCSymbol *FnStartSym = getContext().getOrCreateSymbol(FnStartName); 3857 MCSymbol *FnEndSym = getContext().getOrCreateSymbol(FnEndName); 3858 getStreamer().EmitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId, 3859 SourceLineNum, FnStartSym, 3860 FnEndSym); 3861 return false; 3862 } 3863 3864 void AsmParser::initializeCVDefRangeTypeMap() { 3865 CVDefRangeTypeMap["reg"] = CVDR_DEFRANGE_REGISTER; 3866 CVDefRangeTypeMap["frame_ptr_rel"] = CVDR_DEFRANGE_FRAMEPOINTER_REL; 3867 CVDefRangeTypeMap["subfield_reg"] = CVDR_DEFRANGE_SUBFIELD_REGISTER; 3868 CVDefRangeTypeMap["reg_rel"] = CVDR_DEFRANGE_REGISTER_REL; 3869 } 3870 3871 /// parseDirectiveCVDefRange 3872 /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes* 3873 bool AsmParser::parseDirectiveCVDefRange() { 3874 SMLoc Loc; 3875 std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges; 3876 while (getLexer().is(AsmToken::Identifier)) { 3877 Loc = getLexer().getLoc(); 3878 StringRef GapStartName; 3879 if (parseIdentifier(GapStartName)) 3880 return Error(Loc, "expected identifier in directive"); 3881 MCSymbol *GapStartSym = getContext().getOrCreateSymbol(GapStartName); 3882 3883 Loc = getLexer().getLoc(); 3884 StringRef GapEndName; 3885 if (parseIdentifier(GapEndName)) 3886 return Error(Loc, "expected identifier in directive"); 3887 MCSymbol *GapEndSym = getContext().getOrCreateSymbol(GapEndName); 3888 3889 Ranges.push_back({GapStartSym, GapEndSym}); 3890 } 3891 3892 StringRef CVDefRangeTypeStr; 3893 if (parseToken( 3894 AsmToken::Comma, 3895 "expected comma before def_range type in .cv_def_range directive") || 3896 parseIdentifier(CVDefRangeTypeStr)) 3897 return Error(Loc, "expected def_range type in directive"); 3898 3899 StringMap<CVDefRangeType>::const_iterator CVTypeIt = 3900 CVDefRangeTypeMap.find(CVDefRangeTypeStr); 3901 CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end()) 3902 ? CVDR_DEFRANGE 3903 : CVTypeIt->getValue(); 3904 switch (CVDRType) { 3905 case CVDR_DEFRANGE_REGISTER: { 3906 int64_t DRRegister; 3907 if (parseToken(AsmToken::Comma, "expected comma before register number in " 3908 ".cv_def_range directive") || 3909 parseAbsoluteExpression(DRRegister)) 3910 return Error(Loc, "expected register number"); 3911 3912 codeview::DefRangeRegisterHeader DRHdr; 3913 DRHdr.Register = DRRegister; 3914 DRHdr.MayHaveNoName = 0; 3915 getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); 3916 break; 3917 } 3918 case CVDR_DEFRANGE_FRAMEPOINTER_REL: { 3919 int64_t DROffset; 3920 if (parseToken(AsmToken::Comma, 3921 "expected comma before offset in .cv_def_range directive") || 3922 parseAbsoluteExpression(DROffset)) 3923 return Error(Loc, "expected offset value"); 3924 3925 codeview::DefRangeFramePointerRelHeader DRHdr; 3926 DRHdr.Offset = DROffset; 3927 getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); 3928 break; 3929 } 3930 case CVDR_DEFRANGE_SUBFIELD_REGISTER: { 3931 int64_t DRRegister; 3932 int64_t DROffsetInParent; 3933 if (parseToken(AsmToken::Comma, "expected comma before register number in " 3934 ".cv_def_range directive") || 3935 parseAbsoluteExpression(DRRegister)) 3936 return Error(Loc, "expected register number"); 3937 if (parseToken(AsmToken::Comma, 3938 "expected comma before offset in .cv_def_range directive") || 3939 parseAbsoluteExpression(DROffsetInParent)) 3940 return Error(Loc, "expected offset value"); 3941 3942 codeview::DefRangeSubfieldRegisterHeader DRHdr; 3943 DRHdr.Register = DRRegister; 3944 DRHdr.MayHaveNoName = 0; 3945 DRHdr.OffsetInParent = DROffsetInParent; 3946 getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); 3947 break; 3948 } 3949 case CVDR_DEFRANGE_REGISTER_REL: { 3950 int64_t DRRegister; 3951 int64_t DRFlags; 3952 int64_t DRBasePointerOffset; 3953 if (parseToken(AsmToken::Comma, "expected comma before register number in " 3954 ".cv_def_range directive") || 3955 parseAbsoluteExpression(DRRegister)) 3956 return Error(Loc, "expected register value"); 3957 if (parseToken( 3958 AsmToken::Comma, 3959 "expected comma before flag value in .cv_def_range directive") || 3960 parseAbsoluteExpression(DRFlags)) 3961 return Error(Loc, "expected flag value"); 3962 if (parseToken(AsmToken::Comma, "expected comma before base pointer offset " 3963 "in .cv_def_range directive") || 3964 parseAbsoluteExpression(DRBasePointerOffset)) 3965 return Error(Loc, "expected base pointer offset value"); 3966 3967 codeview::DefRangeRegisterRelHeader DRHdr; 3968 DRHdr.Register = DRRegister; 3969 DRHdr.Flags = DRFlags; 3970 DRHdr.BasePointerOffset = DRBasePointerOffset; 3971 getStreamer().EmitCVDefRangeDirective(Ranges, DRHdr); 3972 break; 3973 } 3974 default: 3975 return Error(Loc, "unexpected def_range type in .cv_def_range directive"); 3976 } 3977 return true; 3978 } 3979 3980 /// parseDirectiveCVString 3981 /// ::= .cv_stringtable "string" 3982 bool AsmParser::parseDirectiveCVString() { 3983 std::string Data; 3984 if (checkForValidSection() || parseEscapedString(Data)) 3985 return addErrorSuffix(" in '.cv_string' directive"); 3986 3987 // Put the string in the table and emit the offset. 3988 std::pair<StringRef, unsigned> Insertion = 3989 getCVContext().addToStringTable(Data); 3990 getStreamer().EmitIntValue(Insertion.second, 4); 3991 return false; 3992 } 3993 3994 /// parseDirectiveCVStringTable 3995 /// ::= .cv_stringtable 3996 bool AsmParser::parseDirectiveCVStringTable() { 3997 getStreamer().EmitCVStringTableDirective(); 3998 return false; 3999 } 4000 4001 /// parseDirectiveCVFileChecksums 4002 /// ::= .cv_filechecksums 4003 bool AsmParser::parseDirectiveCVFileChecksums() { 4004 getStreamer().EmitCVFileChecksumsDirective(); 4005 return false; 4006 } 4007 4008 /// parseDirectiveCVFileChecksumOffset 4009 /// ::= .cv_filechecksumoffset fileno 4010 bool AsmParser::parseDirectiveCVFileChecksumOffset() { 4011 int64_t FileNo; 4012 if (parseIntToken(FileNo, "expected identifier in directive")) 4013 return true; 4014 if (parseToken(AsmToken::EndOfStatement, "Expected End of Statement")) 4015 return true; 4016 getStreamer().EmitCVFileChecksumOffsetDirective(FileNo); 4017 return false; 4018 } 4019 4020 /// parseDirectiveCVFPOData 4021 /// ::= .cv_fpo_data procsym 4022 bool AsmParser::parseDirectiveCVFPOData() { 4023 SMLoc DirLoc = getLexer().getLoc(); 4024 StringRef ProcName; 4025 if (parseIdentifier(ProcName)) 4026 return TokError("expected symbol name"); 4027 if (parseEOL("unexpected tokens")) 4028 return addErrorSuffix(" in '.cv_fpo_data' directive"); 4029 MCSymbol *ProcSym = getContext().getOrCreateSymbol(ProcName); 4030 getStreamer().EmitCVFPOData(ProcSym, DirLoc); 4031 return false; 4032 } 4033 4034 /// parseDirectiveCFISections 4035 /// ::= .cfi_sections section [, section] 4036 bool AsmParser::parseDirectiveCFISections() { 4037 StringRef Name; 4038 bool EH = false; 4039 bool Debug = false; 4040 4041 if (parseIdentifier(Name)) 4042 return TokError("Expected an identifier"); 4043 4044 if (Name == ".eh_frame") 4045 EH = true; 4046 else if (Name == ".debug_frame") 4047 Debug = true; 4048 4049 if (getLexer().is(AsmToken::Comma)) { 4050 Lex(); 4051 4052 if (parseIdentifier(Name)) 4053 return TokError("Expected an identifier"); 4054 4055 if (Name == ".eh_frame") 4056 EH = true; 4057 else if (Name == ".debug_frame") 4058 Debug = true; 4059 } 4060 4061 getStreamer().EmitCFISections(EH, Debug); 4062 return false; 4063 } 4064 4065 /// parseDirectiveCFIStartProc 4066 /// ::= .cfi_startproc [simple] 4067 bool AsmParser::parseDirectiveCFIStartProc() { 4068 StringRef Simple; 4069 if (!parseOptionalToken(AsmToken::EndOfStatement)) { 4070 if (check(parseIdentifier(Simple) || Simple != "simple", 4071 "unexpected token") || 4072 parseToken(AsmToken::EndOfStatement)) 4073 return addErrorSuffix(" in '.cfi_startproc' directive"); 4074 } 4075 4076 // TODO(kristina): Deal with a corner case of incorrect diagnostic context 4077 // being produced if this directive is emitted as part of preprocessor macro 4078 // expansion which can *ONLY* happen if Clang's cc1as is the API consumer. 4079 // Tools like llvm-mc on the other hand are not affected by it, and report 4080 // correct context information. 4081 getStreamer().EmitCFIStartProc(!Simple.empty(), Lexer.getLoc()); 4082 return false; 4083 } 4084 4085 /// parseDirectiveCFIEndProc 4086 /// ::= .cfi_endproc 4087 bool AsmParser::parseDirectiveCFIEndProc() { 4088 getStreamer().EmitCFIEndProc(); 4089 return false; 4090 } 4091 4092 /// parse register name or number. 4093 bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, 4094 SMLoc DirectiveLoc) { 4095 unsigned RegNo; 4096 4097 if (getLexer().isNot(AsmToken::Integer)) { 4098 if (getTargetParser().ParseRegister(RegNo, DirectiveLoc, DirectiveLoc)) 4099 return true; 4100 Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNo, true); 4101 } else 4102 return parseAbsoluteExpression(Register); 4103 4104 return false; 4105 } 4106 4107 /// parseDirectiveCFIDefCfa 4108 /// ::= .cfi_def_cfa register, offset 4109 bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { 4110 int64_t Register = 0, Offset = 0; 4111 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || 4112 parseToken(AsmToken::Comma, "unexpected token in directive") || 4113 parseAbsoluteExpression(Offset)) 4114 return true; 4115 4116 getStreamer().EmitCFIDefCfa(Register, Offset); 4117 return false; 4118 } 4119 4120 /// parseDirectiveCFIDefCfaOffset 4121 /// ::= .cfi_def_cfa_offset offset 4122 bool AsmParser::parseDirectiveCFIDefCfaOffset() { 4123 int64_t Offset = 0; 4124 if (parseAbsoluteExpression(Offset)) 4125 return true; 4126 4127 getStreamer().EmitCFIDefCfaOffset(Offset); 4128 return false; 4129 } 4130 4131 /// parseDirectiveCFIRegister 4132 /// ::= .cfi_register register, register 4133 bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { 4134 int64_t Register1 = 0, Register2 = 0; 4135 if (parseRegisterOrRegisterNumber(Register1, DirectiveLoc) || 4136 parseToken(AsmToken::Comma, "unexpected token in directive") || 4137 parseRegisterOrRegisterNumber(Register2, DirectiveLoc)) 4138 return true; 4139 4140 getStreamer().EmitCFIRegister(Register1, Register2); 4141 return false; 4142 } 4143 4144 /// parseDirectiveCFIWindowSave 4145 /// ::= .cfi_window_save 4146 bool AsmParser::parseDirectiveCFIWindowSave() { 4147 getStreamer().EmitCFIWindowSave(); 4148 return false; 4149 } 4150 4151 /// parseDirectiveCFIAdjustCfaOffset 4152 /// ::= .cfi_adjust_cfa_offset adjustment 4153 bool AsmParser::parseDirectiveCFIAdjustCfaOffset() { 4154 int64_t Adjustment = 0; 4155 if (parseAbsoluteExpression(Adjustment)) 4156 return true; 4157 4158 getStreamer().EmitCFIAdjustCfaOffset(Adjustment); 4159 return false; 4160 } 4161 4162 /// parseDirectiveCFIDefCfaRegister 4163 /// ::= .cfi_def_cfa_register register 4164 bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { 4165 int64_t Register = 0; 4166 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) 4167 return true; 4168 4169 getStreamer().EmitCFIDefCfaRegister(Register); 4170 return false; 4171 } 4172 4173 /// parseDirectiveCFIOffset 4174 /// ::= .cfi_offset register, offset 4175 bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { 4176 int64_t Register = 0; 4177 int64_t Offset = 0; 4178 4179 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || 4180 parseToken(AsmToken::Comma, "unexpected token in directive") || 4181 parseAbsoluteExpression(Offset)) 4182 return true; 4183 4184 getStreamer().EmitCFIOffset(Register, Offset); 4185 return false; 4186 } 4187 4188 /// parseDirectiveCFIRelOffset 4189 /// ::= .cfi_rel_offset register, offset 4190 bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { 4191 int64_t Register = 0, Offset = 0; 4192 4193 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || 4194 parseToken(AsmToken::Comma, "unexpected token in directive") || 4195 parseAbsoluteExpression(Offset)) 4196 return true; 4197 4198 getStreamer().EmitCFIRelOffset(Register, Offset); 4199 return false; 4200 } 4201 4202 static bool isValidEncoding(int64_t Encoding) { 4203 if (Encoding & ~0xff) 4204 return false; 4205 4206 if (Encoding == dwarf::DW_EH_PE_omit) 4207 return true; 4208 4209 const unsigned Format = Encoding & 0xf; 4210 if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && 4211 Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && 4212 Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && 4213 Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) 4214 return false; 4215 4216 const unsigned Application = Encoding & 0x70; 4217 if (Application != dwarf::DW_EH_PE_absptr && 4218 Application != dwarf::DW_EH_PE_pcrel) 4219 return false; 4220 4221 return true; 4222 } 4223 4224 /// parseDirectiveCFIPersonalityOrLsda 4225 /// IsPersonality true for cfi_personality, false for cfi_lsda 4226 /// ::= .cfi_personality encoding, [symbol_name] 4227 /// ::= .cfi_lsda encoding, [symbol_name] 4228 bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { 4229 int64_t Encoding = 0; 4230 if (parseAbsoluteExpression(Encoding)) 4231 return true; 4232 if (Encoding == dwarf::DW_EH_PE_omit) 4233 return false; 4234 4235 StringRef Name; 4236 if (check(!isValidEncoding(Encoding), "unsupported encoding.") || 4237 parseToken(AsmToken::Comma, "unexpected token in directive") || 4238 check(parseIdentifier(Name), "expected identifier in directive")) 4239 return true; 4240 4241 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 4242 4243 if (IsPersonality) 4244 getStreamer().EmitCFIPersonality(Sym, Encoding); 4245 else 4246 getStreamer().EmitCFILsda(Sym, Encoding); 4247 return false; 4248 } 4249 4250 /// parseDirectiveCFIRememberState 4251 /// ::= .cfi_remember_state 4252 bool AsmParser::parseDirectiveCFIRememberState() { 4253 getStreamer().EmitCFIRememberState(); 4254 return false; 4255 } 4256 4257 /// parseDirectiveCFIRestoreState 4258 /// ::= .cfi_remember_state 4259 bool AsmParser::parseDirectiveCFIRestoreState() { 4260 getStreamer().EmitCFIRestoreState(); 4261 return false; 4262 } 4263 4264 /// parseDirectiveCFISameValue 4265 /// ::= .cfi_same_value register 4266 bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { 4267 int64_t Register = 0; 4268 4269 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) 4270 return true; 4271 4272 getStreamer().EmitCFISameValue(Register); 4273 return false; 4274 } 4275 4276 /// parseDirectiveCFIRestore 4277 /// ::= .cfi_restore register 4278 bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { 4279 int64_t Register = 0; 4280 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) 4281 return true; 4282 4283 getStreamer().EmitCFIRestore(Register); 4284 return false; 4285 } 4286 4287 /// parseDirectiveCFIEscape 4288 /// ::= .cfi_escape expression[,...] 4289 bool AsmParser::parseDirectiveCFIEscape() { 4290 std::string Values; 4291 int64_t CurrValue; 4292 if (parseAbsoluteExpression(CurrValue)) 4293 return true; 4294 4295 Values.push_back((uint8_t)CurrValue); 4296 4297 while (getLexer().is(AsmToken::Comma)) { 4298 Lex(); 4299 4300 if (parseAbsoluteExpression(CurrValue)) 4301 return true; 4302 4303 Values.push_back((uint8_t)CurrValue); 4304 } 4305 4306 getStreamer().EmitCFIEscape(Values); 4307 return false; 4308 } 4309 4310 /// parseDirectiveCFIReturnColumn 4311 /// ::= .cfi_return_column register 4312 bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) { 4313 int64_t Register = 0; 4314 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) 4315 return true; 4316 getStreamer().EmitCFIReturnColumn(Register); 4317 return false; 4318 } 4319 4320 /// parseDirectiveCFISignalFrame 4321 /// ::= .cfi_signal_frame 4322 bool AsmParser::parseDirectiveCFISignalFrame() { 4323 if (parseToken(AsmToken::EndOfStatement, 4324 "unexpected token in '.cfi_signal_frame'")) 4325 return true; 4326 4327 getStreamer().EmitCFISignalFrame(); 4328 return false; 4329 } 4330 4331 /// parseDirectiveCFIUndefined 4332 /// ::= .cfi_undefined register 4333 bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { 4334 int64_t Register = 0; 4335 4336 if (parseRegisterOrRegisterNumber(Register, DirectiveLoc)) 4337 return true; 4338 4339 getStreamer().EmitCFIUndefined(Register); 4340 return false; 4341 } 4342 4343 /// parseDirectiveAltmacro 4344 /// ::= .altmacro 4345 /// ::= .noaltmacro 4346 bool AsmParser::parseDirectiveAltmacro(StringRef Directive) { 4347 if (getLexer().isNot(AsmToken::EndOfStatement)) 4348 return TokError("unexpected token in '" + Directive + "' directive"); 4349 AltMacroMode = (Directive == ".altmacro"); 4350 return false; 4351 } 4352 4353 /// parseDirectiveMacrosOnOff 4354 /// ::= .macros_on 4355 /// ::= .macros_off 4356 bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { 4357 if (parseToken(AsmToken::EndOfStatement, 4358 "unexpected token in '" + Directive + "' directive")) 4359 return true; 4360 4361 setMacrosEnabled(Directive == ".macros_on"); 4362 return false; 4363 } 4364 4365 /// parseDirectiveMacro 4366 /// ::= .macro name[,] [parameters] 4367 bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { 4368 StringRef Name; 4369 if (parseIdentifier(Name)) 4370 return TokError("expected identifier in '.macro' directive"); 4371 4372 if (getLexer().is(AsmToken::Comma)) 4373 Lex(); 4374 4375 MCAsmMacroParameters Parameters; 4376 while (getLexer().isNot(AsmToken::EndOfStatement)) { 4377 4378 if (!Parameters.empty() && Parameters.back().Vararg) 4379 return Error(Lexer.getLoc(), 4380 "Vararg parameter '" + Parameters.back().Name + 4381 "' should be last one in the list of parameters."); 4382 4383 MCAsmMacroParameter Parameter; 4384 if (parseIdentifier(Parameter.Name)) 4385 return TokError("expected identifier in '.macro' directive"); 4386 4387 // Emit an error if two (or more) named parameters share the same name 4388 for (const MCAsmMacroParameter& CurrParam : Parameters) 4389 if (CurrParam.Name.equals(Parameter.Name)) 4390 return TokError("macro '" + Name + "' has multiple parameters" 4391 " named '" + Parameter.Name + "'"); 4392 4393 if (Lexer.is(AsmToken::Colon)) { 4394 Lex(); // consume ':' 4395 4396 SMLoc QualLoc; 4397 StringRef Qualifier; 4398 4399 QualLoc = Lexer.getLoc(); 4400 if (parseIdentifier(Qualifier)) 4401 return Error(QualLoc, "missing parameter qualifier for " 4402 "'" + Parameter.Name + "' in macro '" + Name + "'"); 4403 4404 if (Qualifier == "req") 4405 Parameter.Required = true; 4406 else if (Qualifier == "vararg") 4407 Parameter.Vararg = true; 4408 else 4409 return Error(QualLoc, Qualifier + " is not a valid parameter qualifier " 4410 "for '" + Parameter.Name + "' in macro '" + Name + "'"); 4411 } 4412 4413 if (getLexer().is(AsmToken::Equal)) { 4414 Lex(); 4415 4416 SMLoc ParamLoc; 4417 4418 ParamLoc = Lexer.getLoc(); 4419 if (parseMacroArgument(Parameter.Value, /*Vararg=*/false )) 4420 return true; 4421 4422 if (Parameter.Required) 4423 Warning(ParamLoc, "pointless default value for required parameter " 4424 "'" + Parameter.Name + "' in macro '" + Name + "'"); 4425 } 4426 4427 Parameters.push_back(std::move(Parameter)); 4428 4429 if (getLexer().is(AsmToken::Comma)) 4430 Lex(); 4431 } 4432 4433 // Eat just the end of statement. 4434 Lexer.Lex(); 4435 4436 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors 4437 AsmToken EndToken, StartToken = getTok(); 4438 unsigned MacroDepth = 0; 4439 // Lex the macro definition. 4440 while (true) { 4441 // Ignore Lexing errors in macros. 4442 while (Lexer.is(AsmToken::Error)) { 4443 Lexer.Lex(); 4444 } 4445 4446 // Check whether we have reached the end of the file. 4447 if (getLexer().is(AsmToken::Eof)) 4448 return Error(DirectiveLoc, "no matching '.endmacro' in definition"); 4449 4450 // Otherwise, check whether we have reach the .endmacro. 4451 if (getLexer().is(AsmToken::Identifier)) { 4452 if (getTok().getIdentifier() == ".endm" || 4453 getTok().getIdentifier() == ".endmacro") { 4454 if (MacroDepth == 0) { // Outermost macro. 4455 EndToken = getTok(); 4456 Lexer.Lex(); 4457 if (getLexer().isNot(AsmToken::EndOfStatement)) 4458 return TokError("unexpected token in '" + EndToken.getIdentifier() + 4459 "' directive"); 4460 break; 4461 } else { 4462 // Otherwise we just found the end of an inner macro. 4463 --MacroDepth; 4464 } 4465 } else if (getTok().getIdentifier() == ".macro") { 4466 // We allow nested macros. Those aren't instantiated until the outermost 4467 // macro is expanded so just ignore them for now. 4468 ++MacroDepth; 4469 } 4470 } 4471 4472 // Otherwise, scan til the end of the statement. 4473 eatToEndOfStatement(); 4474 } 4475 4476 if (getContext().lookupMacro(Name)) { 4477 return Error(DirectiveLoc, "macro '" + Name + "' is already defined"); 4478 } 4479 4480 const char *BodyStart = StartToken.getLoc().getPointer(); 4481 const char *BodyEnd = EndToken.getLoc().getPointer(); 4482 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); 4483 checkForBadMacro(DirectiveLoc, Name, Body, Parameters); 4484 MCAsmMacro Macro(Name, Body, std::move(Parameters)); 4485 DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n"; 4486 Macro.dump()); 4487 getContext().defineMacro(Name, std::move(Macro)); 4488 return false; 4489 } 4490 4491 /// checkForBadMacro 4492 /// 4493 /// With the support added for named parameters there may be code out there that 4494 /// is transitioning from positional parameters. In versions of gas that did 4495 /// not support named parameters they would be ignored on the macro definition. 4496 /// But to support both styles of parameters this is not possible so if a macro 4497 /// definition has named parameters but does not use them and has what appears 4498 /// to be positional parameters, strings like $1, $2, ... and $n, then issue a 4499 /// warning that the positional parameter found in body which have no effect. 4500 /// Hoping the developer will either remove the named parameters from the macro 4501 /// definition so the positional parameters get used if that was what was 4502 /// intended or change the macro to use the named parameters. It is possible 4503 /// this warning will trigger when the none of the named parameters are used 4504 /// and the strings like $1 are infact to simply to be passed trough unchanged. 4505 void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, 4506 StringRef Body, 4507 ArrayRef<MCAsmMacroParameter> Parameters) { 4508 // If this macro is not defined with named parameters the warning we are 4509 // checking for here doesn't apply. 4510 unsigned NParameters = Parameters.size(); 4511 if (NParameters == 0) 4512 return; 4513 4514 bool NamedParametersFound = false; 4515 bool PositionalParametersFound = false; 4516 4517 // Look at the body of the macro for use of both the named parameters and what 4518 // are likely to be positional parameters. This is what expandMacro() is 4519 // doing when it finds the parameters in the body. 4520 while (!Body.empty()) { 4521 // Scan for the next possible parameter. 4522 std::size_t End = Body.size(), Pos = 0; 4523 for (; Pos != End; ++Pos) { 4524 // Check for a substitution or escape. 4525 // This macro is defined with parameters, look for \foo, \bar, etc. 4526 if (Body[Pos] == '\\' && Pos + 1 != End) 4527 break; 4528 4529 // This macro should have parameters, but look for $0, $1, ..., $n too. 4530 if (Body[Pos] != '$' || Pos + 1 == End) 4531 continue; 4532 char Next = Body[Pos + 1]; 4533 if (Next == '$' || Next == 'n' || 4534 isdigit(static_cast<unsigned char>(Next))) 4535 break; 4536 } 4537 4538 // Check if we reached the end. 4539 if (Pos == End) 4540 break; 4541 4542 if (Body[Pos] == '$') { 4543 switch (Body[Pos + 1]) { 4544 // $$ => $ 4545 case '$': 4546 break; 4547 4548 // $n => number of arguments 4549 case 'n': 4550 PositionalParametersFound = true; 4551 break; 4552 4553 // $[0-9] => argument 4554 default: { 4555 PositionalParametersFound = true; 4556 break; 4557 } 4558 } 4559 Pos += 2; 4560 } else { 4561 unsigned I = Pos + 1; 4562 while (isIdentifierChar(Body[I]) && I + 1 != End) 4563 ++I; 4564 4565 const char *Begin = Body.data() + Pos + 1; 4566 StringRef Argument(Begin, I - (Pos + 1)); 4567 unsigned Index = 0; 4568 for (; Index < NParameters; ++Index) 4569 if (Parameters[Index].Name == Argument) 4570 break; 4571 4572 if (Index == NParameters) { 4573 if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') 4574 Pos += 3; 4575 else { 4576 Pos = I; 4577 } 4578 } else { 4579 NamedParametersFound = true; 4580 Pos += 1 + Argument.size(); 4581 } 4582 } 4583 // Update the scan point. 4584 Body = Body.substr(Pos); 4585 } 4586 4587 if (!NamedParametersFound && PositionalParametersFound) 4588 Warning(DirectiveLoc, "macro defined with named parameters which are not " 4589 "used in macro body, possible positional parameter " 4590 "found in body which will have no effect"); 4591 } 4592 4593 /// parseDirectiveExitMacro 4594 /// ::= .exitm 4595 bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { 4596 if (parseToken(AsmToken::EndOfStatement, 4597 "unexpected token in '" + Directive + "' directive")) 4598 return true; 4599 4600 if (!isInsideMacroInstantiation()) 4601 return TokError("unexpected '" + Directive + "' in file, " 4602 "no current macro definition"); 4603 4604 // Exit all conditionals that are active in the current macro. 4605 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { 4606 TheCondState = TheCondStack.back(); 4607 TheCondStack.pop_back(); 4608 } 4609 4610 handleMacroExit(); 4611 return false; 4612 } 4613 4614 /// parseDirectiveEndMacro 4615 /// ::= .endm 4616 /// ::= .endmacro 4617 bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { 4618 if (getLexer().isNot(AsmToken::EndOfStatement)) 4619 return TokError("unexpected token in '" + Directive + "' directive"); 4620 4621 // If we are inside a macro instantiation, terminate the current 4622 // instantiation. 4623 if (isInsideMacroInstantiation()) { 4624 handleMacroExit(); 4625 return false; 4626 } 4627 4628 // Otherwise, this .endmacro is a stray entry in the file; well formed 4629 // .endmacro directives are handled during the macro definition parsing. 4630 return TokError("unexpected '" + Directive + "' in file, " 4631 "no current macro definition"); 4632 } 4633 4634 /// parseDirectivePurgeMacro 4635 /// ::= .purgem 4636 bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { 4637 StringRef Name; 4638 SMLoc Loc; 4639 if (parseTokenLoc(Loc) || 4640 check(parseIdentifier(Name), Loc, 4641 "expected identifier in '.purgem' directive") || 4642 parseToken(AsmToken::EndOfStatement, 4643 "unexpected token in '.purgem' directive")) 4644 return true; 4645 4646 if (!getContext().lookupMacro(Name)) 4647 return Error(DirectiveLoc, "macro '" + Name + "' is not defined"); 4648 4649 getContext().undefineMacro(Name); 4650 DEBUG_WITH_TYPE("asm-macros", dbgs() 4651 << "Un-defining macro: " << Name << "\n"); 4652 return false; 4653 } 4654 4655 /// parseDirectiveBundleAlignMode 4656 /// ::= {.bundle_align_mode} expression 4657 bool AsmParser::parseDirectiveBundleAlignMode() { 4658 // Expect a single argument: an expression that evaluates to a constant 4659 // in the inclusive range 0-30. 4660 SMLoc ExprLoc = getLexer().getLoc(); 4661 int64_t AlignSizePow2; 4662 if (checkForValidSection() || parseAbsoluteExpression(AlignSizePow2) || 4663 parseToken(AsmToken::EndOfStatement, "unexpected token after expression " 4664 "in '.bundle_align_mode' " 4665 "directive") || 4666 check(AlignSizePow2 < 0 || AlignSizePow2 > 30, ExprLoc, 4667 "invalid bundle alignment size (expected between 0 and 30)")) 4668 return true; 4669 4670 // Because of AlignSizePow2's verified range we can safely truncate it to 4671 // unsigned. 4672 getStreamer().EmitBundleAlignMode(static_cast<unsigned>(AlignSizePow2)); 4673 return false; 4674 } 4675 4676 /// parseDirectiveBundleLock 4677 /// ::= {.bundle_lock} [align_to_end] 4678 bool AsmParser::parseDirectiveBundleLock() { 4679 if (checkForValidSection()) 4680 return true; 4681 bool AlignToEnd = false; 4682 4683 StringRef Option; 4684 SMLoc Loc = getTok().getLoc(); 4685 const char *kInvalidOptionError = 4686 "invalid option for '.bundle_lock' directive"; 4687 4688 if (!parseOptionalToken(AsmToken::EndOfStatement)) { 4689 if (check(parseIdentifier(Option), Loc, kInvalidOptionError) || 4690 check(Option != "align_to_end", Loc, kInvalidOptionError) || 4691 parseToken(AsmToken::EndOfStatement, 4692 "unexpected token after '.bundle_lock' directive option")) 4693 return true; 4694 AlignToEnd = true; 4695 } 4696 4697 getStreamer().EmitBundleLock(AlignToEnd); 4698 return false; 4699 } 4700 4701 /// parseDirectiveBundleLock 4702 /// ::= {.bundle_lock} 4703 bool AsmParser::parseDirectiveBundleUnlock() { 4704 if (checkForValidSection() || 4705 parseToken(AsmToken::EndOfStatement, 4706 "unexpected token in '.bundle_unlock' directive")) 4707 return true; 4708 4709 getStreamer().EmitBundleUnlock(); 4710 return false; 4711 } 4712 4713 /// parseDirectiveSpace 4714 /// ::= (.skip | .space) expression [ , expression ] 4715 bool AsmParser::parseDirectiveSpace(StringRef IDVal) { 4716 SMLoc NumBytesLoc = Lexer.getLoc(); 4717 const MCExpr *NumBytes; 4718 if (checkForValidSection() || parseExpression(NumBytes)) 4719 return true; 4720 4721 int64_t FillExpr = 0; 4722 if (parseOptionalToken(AsmToken::Comma)) 4723 if (parseAbsoluteExpression(FillExpr)) 4724 return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); 4725 if (parseToken(AsmToken::EndOfStatement)) 4726 return addErrorSuffix("in '" + Twine(IDVal) + "' directive"); 4727 4728 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. 4729 getStreamer().emitFill(*NumBytes, FillExpr, NumBytesLoc); 4730 4731 return false; 4732 } 4733 4734 /// parseDirectiveDCB 4735 /// ::= .dcb.{b, l, w} expression, expression 4736 bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) { 4737 SMLoc NumValuesLoc = Lexer.getLoc(); 4738 int64_t NumValues; 4739 if (checkForValidSection() || parseAbsoluteExpression(NumValues)) 4740 return true; 4741 4742 if (NumValues < 0) { 4743 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); 4744 return false; 4745 } 4746 4747 if (parseToken(AsmToken::Comma, 4748 "unexpected token in '" + Twine(IDVal) + "' directive")) 4749 return true; 4750 4751 const MCExpr *Value; 4752 SMLoc ExprLoc = getLexer().getLoc(); 4753 if (parseExpression(Value)) 4754 return true; 4755 4756 // Special case constant expressions to match code generator. 4757 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) { 4758 assert(Size <= 8 && "Invalid size"); 4759 uint64_t IntValue = MCE->getValue(); 4760 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue)) 4761 return Error(ExprLoc, "literal value out of range for directive"); 4762 for (uint64_t i = 0, e = NumValues; i != e; ++i) 4763 getStreamer().EmitIntValue(IntValue, Size); 4764 } else { 4765 for (uint64_t i = 0, e = NumValues; i != e; ++i) 4766 getStreamer().EmitValue(Value, Size, ExprLoc); 4767 } 4768 4769 if (parseToken(AsmToken::EndOfStatement, 4770 "unexpected token in '" + Twine(IDVal) + "' directive")) 4771 return true; 4772 4773 return false; 4774 } 4775 4776 /// parseDirectiveRealDCB 4777 /// ::= .dcb.{d, s} expression, expression 4778 bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) { 4779 SMLoc NumValuesLoc = Lexer.getLoc(); 4780 int64_t NumValues; 4781 if (checkForValidSection() || parseAbsoluteExpression(NumValues)) 4782 return true; 4783 4784 if (NumValues < 0) { 4785 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); 4786 return false; 4787 } 4788 4789 if (parseToken(AsmToken::Comma, 4790 "unexpected token in '" + Twine(IDVal) + "' directive")) 4791 return true; 4792 4793 APInt AsInt; 4794 if (parseRealValue(Semantics, AsInt)) 4795 return true; 4796 4797 if (parseToken(AsmToken::EndOfStatement, 4798 "unexpected token in '" + Twine(IDVal) + "' directive")) 4799 return true; 4800 4801 for (uint64_t i = 0, e = NumValues; i != e; ++i) 4802 getStreamer().EmitIntValue(AsInt.getLimitedValue(), 4803 AsInt.getBitWidth() / 8); 4804 4805 return false; 4806 } 4807 4808 /// parseDirectiveDS 4809 /// ::= .ds.{b, d, l, p, s, w, x} expression 4810 bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) { 4811 SMLoc NumValuesLoc = Lexer.getLoc(); 4812 int64_t NumValues; 4813 if (checkForValidSection() || parseAbsoluteExpression(NumValues)) 4814 return true; 4815 4816 if (NumValues < 0) { 4817 Warning(NumValuesLoc, "'" + Twine(IDVal) + "' directive with negative repeat count has no effect"); 4818 return false; 4819 } 4820 4821 if (parseToken(AsmToken::EndOfStatement, 4822 "unexpected token in '" + Twine(IDVal) + "' directive")) 4823 return true; 4824 4825 for (uint64_t i = 0, e = NumValues; i != e; ++i) 4826 getStreamer().emitFill(Size, 0); 4827 4828 return false; 4829 } 4830 4831 /// parseDirectiveLEB128 4832 /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] 4833 bool AsmParser::parseDirectiveLEB128(bool Signed) { 4834 if (checkForValidSection()) 4835 return true; 4836 4837 auto parseOp = [&]() -> bool { 4838 const MCExpr *Value; 4839 if (parseExpression(Value)) 4840 return true; 4841 if (Signed) 4842 getStreamer().EmitSLEB128Value(Value); 4843 else 4844 getStreamer().EmitULEB128Value(Value); 4845 return false; 4846 }; 4847 4848 if (parseMany(parseOp)) 4849 return addErrorSuffix(" in directive"); 4850 4851 return false; 4852 } 4853 4854 /// parseDirectiveSymbolAttribute 4855 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] 4856 bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { 4857 auto parseOp = [&]() -> bool { 4858 StringRef Name; 4859 SMLoc Loc = getTok().getLoc(); 4860 if (parseIdentifier(Name)) 4861 return Error(Loc, "expected identifier"); 4862 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 4863 4864 // Assembler local symbols don't make any sense here. Complain loudly. 4865 if (Sym->isTemporary()) 4866 return Error(Loc, "non-local symbol required"); 4867 4868 if (!getStreamer().EmitSymbolAttribute(Sym, Attr)) 4869 return Error(Loc, "unable to emit symbol attribute"); 4870 return false; 4871 }; 4872 4873 if (parseMany(parseOp)) 4874 return addErrorSuffix(" in directive"); 4875 return false; 4876 } 4877 4878 /// parseDirectiveComm 4879 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] 4880 bool AsmParser::parseDirectiveComm(bool IsLocal) { 4881 if (checkForValidSection()) 4882 return true; 4883 4884 SMLoc IDLoc = getLexer().getLoc(); 4885 StringRef Name; 4886 if (parseIdentifier(Name)) 4887 return TokError("expected identifier in directive"); 4888 4889 // Handle the identifier as the key symbol. 4890 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 4891 4892 if (getLexer().isNot(AsmToken::Comma)) 4893 return TokError("unexpected token in directive"); 4894 Lex(); 4895 4896 int64_t Size; 4897 SMLoc SizeLoc = getLexer().getLoc(); 4898 if (parseAbsoluteExpression(Size)) 4899 return true; 4900 4901 int64_t Pow2Alignment = 0; 4902 SMLoc Pow2AlignmentLoc; 4903 if (getLexer().is(AsmToken::Comma)) { 4904 Lex(); 4905 Pow2AlignmentLoc = getLexer().getLoc(); 4906 if (parseAbsoluteExpression(Pow2Alignment)) 4907 return true; 4908 4909 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); 4910 if (IsLocal && LCOMM == LCOMM::NoAlignment) 4911 return Error(Pow2AlignmentLoc, "alignment not supported on this target"); 4912 4913 // If this target takes alignments in bytes (not log) validate and convert. 4914 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || 4915 (IsLocal && LCOMM == LCOMM::ByteAlignment)) { 4916 if (!isPowerOf2_64(Pow2Alignment)) 4917 return Error(Pow2AlignmentLoc, "alignment must be a power of 2"); 4918 Pow2Alignment = Log2_64(Pow2Alignment); 4919 } 4920 } 4921 4922 if (parseToken(AsmToken::EndOfStatement, 4923 "unexpected token in '.comm' or '.lcomm' directive")) 4924 return true; 4925 4926 // NOTE: a size of zero for a .comm should create a undefined symbol 4927 // but a size of .lcomm creates a bss symbol of size zero. 4928 if (Size < 0) 4929 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't " 4930 "be less than zero"); 4931 4932 // NOTE: The alignment in the directive is a power of 2 value, the assembler 4933 // may internally end up wanting an alignment in bytes. 4934 // FIXME: Diagnose overflow. 4935 if (Pow2Alignment < 0) 4936 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive " 4937 "alignment, can't be less than zero"); 4938 4939 Sym->redefineIfPossible(); 4940 if (!Sym->isUndefined()) 4941 return Error(IDLoc, "invalid symbol redefinition"); 4942 4943 // Create the Symbol as a common or local common with Size and Pow2Alignment 4944 if (IsLocal) { 4945 getStreamer().EmitLocalCommonSymbol(Sym, Size, 1 << Pow2Alignment); 4946 return false; 4947 } 4948 4949 getStreamer().EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment); 4950 return false; 4951 } 4952 4953 /// parseDirectiveAbort 4954 /// ::= .abort [... message ...] 4955 bool AsmParser::parseDirectiveAbort() { 4956 // FIXME: Use loc from directive. 4957 SMLoc Loc = getLexer().getLoc(); 4958 4959 StringRef Str = parseStringToEndOfStatement(); 4960 if (parseToken(AsmToken::EndOfStatement, 4961 "unexpected token in '.abort' directive")) 4962 return true; 4963 4964 if (Str.empty()) 4965 return Error(Loc, ".abort detected. Assembly stopping."); 4966 else 4967 return Error(Loc, ".abort '" + Str + "' detected. Assembly stopping."); 4968 // FIXME: Actually abort assembly here. 4969 4970 return false; 4971 } 4972 4973 /// parseDirectiveInclude 4974 /// ::= .include "filename" 4975 bool AsmParser::parseDirectiveInclude() { 4976 // Allow the strings to have escaped octal character sequence. 4977 std::string Filename; 4978 SMLoc IncludeLoc = getTok().getLoc(); 4979 4980 if (check(getTok().isNot(AsmToken::String), 4981 "expected string in '.include' directive") || 4982 parseEscapedString(Filename) || 4983 check(getTok().isNot(AsmToken::EndOfStatement), 4984 "unexpected token in '.include' directive") || 4985 // Attempt to switch the lexer to the included file before consuming the 4986 // end of statement to avoid losing it when we switch. 4987 check(enterIncludeFile(Filename), IncludeLoc, 4988 "Could not find include file '" + Filename + "'")) 4989 return true; 4990 4991 return false; 4992 } 4993 4994 /// parseDirectiveIncbin 4995 /// ::= .incbin "filename" [ , skip [ , count ] ] 4996 bool AsmParser::parseDirectiveIncbin() { 4997 // Allow the strings to have escaped octal character sequence. 4998 std::string Filename; 4999 SMLoc IncbinLoc = getTok().getLoc(); 5000 if (check(getTok().isNot(AsmToken::String), 5001 "expected string in '.incbin' directive") || 5002 parseEscapedString(Filename)) 5003 return true; 5004 5005 int64_t Skip = 0; 5006 const MCExpr *Count = nullptr; 5007 SMLoc SkipLoc, CountLoc; 5008 if (parseOptionalToken(AsmToken::Comma)) { 5009 // The skip expression can be omitted while specifying the count, e.g: 5010 // .incbin "filename",,4 5011 if (getTok().isNot(AsmToken::Comma)) { 5012 if (parseTokenLoc(SkipLoc) || parseAbsoluteExpression(Skip)) 5013 return true; 5014 } 5015 if (parseOptionalToken(AsmToken::Comma)) { 5016 CountLoc = getTok().getLoc(); 5017 if (parseExpression(Count)) 5018 return true; 5019 } 5020 } 5021 5022 if (parseToken(AsmToken::EndOfStatement, 5023 "unexpected token in '.incbin' directive")) 5024 return true; 5025 5026 if (check(Skip < 0, SkipLoc, "skip is negative")) 5027 return true; 5028 5029 // Attempt to process the included file. 5030 if (processIncbinFile(Filename, Skip, Count, CountLoc)) 5031 return Error(IncbinLoc, "Could not find incbin file '" + Filename + "'"); 5032 return false; 5033 } 5034 5035 /// parseDirectiveIf 5036 /// ::= .if{,eq,ge,gt,le,lt,ne} expression 5037 bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { 5038 TheCondStack.push_back(TheCondState); 5039 TheCondState.TheCond = AsmCond::IfCond; 5040 if (TheCondState.Ignore) { 5041 eatToEndOfStatement(); 5042 } else { 5043 int64_t ExprValue; 5044 if (parseAbsoluteExpression(ExprValue) || 5045 parseToken(AsmToken::EndOfStatement, 5046 "unexpected token in '.if' directive")) 5047 return true; 5048 5049 switch (DirKind) { 5050 default: 5051 llvm_unreachable("unsupported directive"); 5052 case DK_IF: 5053 case DK_IFNE: 5054 break; 5055 case DK_IFEQ: 5056 ExprValue = ExprValue == 0; 5057 break; 5058 case DK_IFGE: 5059 ExprValue = ExprValue >= 0; 5060 break; 5061 case DK_IFGT: 5062 ExprValue = ExprValue > 0; 5063 break; 5064 case DK_IFLE: 5065 ExprValue = ExprValue <= 0; 5066 break; 5067 case DK_IFLT: 5068 ExprValue = ExprValue < 0; 5069 break; 5070 } 5071 5072 TheCondState.CondMet = ExprValue; 5073 TheCondState.Ignore = !TheCondState.CondMet; 5074 } 5075 5076 return false; 5077 } 5078 5079 /// parseDirectiveIfb 5080 /// ::= .ifb string 5081 bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { 5082 TheCondStack.push_back(TheCondState); 5083 TheCondState.TheCond = AsmCond::IfCond; 5084 5085 if (TheCondState.Ignore) { 5086 eatToEndOfStatement(); 5087 } else { 5088 StringRef Str = parseStringToEndOfStatement(); 5089 5090 if (parseToken(AsmToken::EndOfStatement, 5091 "unexpected token in '.ifb' directive")) 5092 return true; 5093 5094 TheCondState.CondMet = ExpectBlank == Str.empty(); 5095 TheCondState.Ignore = !TheCondState.CondMet; 5096 } 5097 5098 return false; 5099 } 5100 5101 /// parseDirectiveIfc 5102 /// ::= .ifc string1, string2 5103 /// ::= .ifnc string1, string2 5104 bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { 5105 TheCondStack.push_back(TheCondState); 5106 TheCondState.TheCond = AsmCond::IfCond; 5107 5108 if (TheCondState.Ignore) { 5109 eatToEndOfStatement(); 5110 } else { 5111 StringRef Str1 = parseStringToComma(); 5112 5113 if (parseToken(AsmToken::Comma, "unexpected token in '.ifc' directive")) 5114 return true; 5115 5116 StringRef Str2 = parseStringToEndOfStatement(); 5117 5118 if (parseToken(AsmToken::EndOfStatement, 5119 "unexpected token in '.ifc' directive")) 5120 return true; 5121 5122 TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); 5123 TheCondState.Ignore = !TheCondState.CondMet; 5124 } 5125 5126 return false; 5127 } 5128 5129 /// parseDirectiveIfeqs 5130 /// ::= .ifeqs string1, string2 5131 bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { 5132 if (Lexer.isNot(AsmToken::String)) { 5133 if (ExpectEqual) 5134 return TokError("expected string parameter for '.ifeqs' directive"); 5135 return TokError("expected string parameter for '.ifnes' directive"); 5136 } 5137 5138 StringRef String1 = getTok().getStringContents(); 5139 Lex(); 5140 5141 if (Lexer.isNot(AsmToken::Comma)) { 5142 if (ExpectEqual) 5143 return TokError( 5144 "expected comma after first string for '.ifeqs' directive"); 5145 return TokError("expected comma after first string for '.ifnes' directive"); 5146 } 5147 5148 Lex(); 5149 5150 if (Lexer.isNot(AsmToken::String)) { 5151 if (ExpectEqual) 5152 return TokError("expected string parameter for '.ifeqs' directive"); 5153 return TokError("expected string parameter for '.ifnes' directive"); 5154 } 5155 5156 StringRef String2 = getTok().getStringContents(); 5157 Lex(); 5158 5159 TheCondStack.push_back(TheCondState); 5160 TheCondState.TheCond = AsmCond::IfCond; 5161 TheCondState.CondMet = ExpectEqual == (String1 == String2); 5162 TheCondState.Ignore = !TheCondState.CondMet; 5163 5164 return false; 5165 } 5166 5167 /// parseDirectiveIfdef 5168 /// ::= .ifdef symbol 5169 bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { 5170 StringRef Name; 5171 TheCondStack.push_back(TheCondState); 5172 TheCondState.TheCond = AsmCond::IfCond; 5173 5174 if (TheCondState.Ignore) { 5175 eatToEndOfStatement(); 5176 } else { 5177 if (check(parseIdentifier(Name), "expected identifier after '.ifdef'") || 5178 parseToken(AsmToken::EndOfStatement, "unexpected token in '.ifdef'")) 5179 return true; 5180 5181 MCSymbol *Sym = getContext().lookupSymbol(Name); 5182 5183 if (expect_defined) 5184 TheCondState.CondMet = (Sym && !Sym->isUndefined(false)); 5185 else 5186 TheCondState.CondMet = (!Sym || Sym->isUndefined(false)); 5187 TheCondState.Ignore = !TheCondState.CondMet; 5188 } 5189 5190 return false; 5191 } 5192 5193 /// parseDirectiveElseIf 5194 /// ::= .elseif expression 5195 bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { 5196 if (TheCondState.TheCond != AsmCond::IfCond && 5197 TheCondState.TheCond != AsmCond::ElseIfCond) 5198 return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an" 5199 " .if or an .elseif"); 5200 TheCondState.TheCond = AsmCond::ElseIfCond; 5201 5202 bool LastIgnoreState = false; 5203 if (!TheCondStack.empty()) 5204 LastIgnoreState = TheCondStack.back().Ignore; 5205 if (LastIgnoreState || TheCondState.CondMet) { 5206 TheCondState.Ignore = true; 5207 eatToEndOfStatement(); 5208 } else { 5209 int64_t ExprValue; 5210 if (parseAbsoluteExpression(ExprValue)) 5211 return true; 5212 5213 if (parseToken(AsmToken::EndOfStatement, 5214 "unexpected token in '.elseif' directive")) 5215 return true; 5216 5217 TheCondState.CondMet = ExprValue; 5218 TheCondState.Ignore = !TheCondState.CondMet; 5219 } 5220 5221 return false; 5222 } 5223 5224 /// parseDirectiveElse 5225 /// ::= .else 5226 bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { 5227 if (parseToken(AsmToken::EndOfStatement, 5228 "unexpected token in '.else' directive")) 5229 return true; 5230 5231 if (TheCondState.TheCond != AsmCond::IfCond && 5232 TheCondState.TheCond != AsmCond::ElseIfCond) 5233 return Error(DirectiveLoc, "Encountered a .else that doesn't follow " 5234 " an .if or an .elseif"); 5235 TheCondState.TheCond = AsmCond::ElseCond; 5236 bool LastIgnoreState = false; 5237 if (!TheCondStack.empty()) 5238 LastIgnoreState = TheCondStack.back().Ignore; 5239 if (LastIgnoreState || TheCondState.CondMet) 5240 TheCondState.Ignore = true; 5241 else 5242 TheCondState.Ignore = false; 5243 5244 return false; 5245 } 5246 5247 /// parseDirectiveEnd 5248 /// ::= .end 5249 bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { 5250 if (parseToken(AsmToken::EndOfStatement, 5251 "unexpected token in '.end' directive")) 5252 return true; 5253 5254 while (Lexer.isNot(AsmToken::Eof)) 5255 Lexer.Lex(); 5256 5257 return false; 5258 } 5259 5260 /// parseDirectiveError 5261 /// ::= .err 5262 /// ::= .error [string] 5263 bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { 5264 if (!TheCondStack.empty()) { 5265 if (TheCondStack.back().Ignore) { 5266 eatToEndOfStatement(); 5267 return false; 5268 } 5269 } 5270 5271 if (!WithMessage) 5272 return Error(L, ".err encountered"); 5273 5274 StringRef Message = ".error directive invoked in source file"; 5275 if (Lexer.isNot(AsmToken::EndOfStatement)) { 5276 if (Lexer.isNot(AsmToken::String)) 5277 return TokError(".error argument must be a string"); 5278 5279 Message = getTok().getStringContents(); 5280 Lex(); 5281 } 5282 5283 return Error(L, Message); 5284 } 5285 5286 /// parseDirectiveWarning 5287 /// ::= .warning [string] 5288 bool AsmParser::parseDirectiveWarning(SMLoc L) { 5289 if (!TheCondStack.empty()) { 5290 if (TheCondStack.back().Ignore) { 5291 eatToEndOfStatement(); 5292 return false; 5293 } 5294 } 5295 5296 StringRef Message = ".warning directive invoked in source file"; 5297 5298 if (!parseOptionalToken(AsmToken::EndOfStatement)) { 5299 if (Lexer.isNot(AsmToken::String)) 5300 return TokError(".warning argument must be a string"); 5301 5302 Message = getTok().getStringContents(); 5303 Lex(); 5304 if (parseToken(AsmToken::EndOfStatement, 5305 "expected end of statement in '.warning' directive")) 5306 return true; 5307 } 5308 5309 return Warning(L, Message); 5310 } 5311 5312 /// parseDirectiveEndIf 5313 /// ::= .endif 5314 bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { 5315 if (parseToken(AsmToken::EndOfStatement, 5316 "unexpected token in '.endif' directive")) 5317 return true; 5318 5319 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) 5320 return Error(DirectiveLoc, "Encountered a .endif that doesn't follow " 5321 "an .if or .else"); 5322 if (!TheCondStack.empty()) { 5323 TheCondState = TheCondStack.back(); 5324 TheCondStack.pop_back(); 5325 } 5326 5327 return false; 5328 } 5329 5330 void AsmParser::initializeDirectiveKindMap() { 5331 DirectiveKindMap[".set"] = DK_SET; 5332 DirectiveKindMap[".equ"] = DK_EQU; 5333 DirectiveKindMap[".equiv"] = DK_EQUIV; 5334 DirectiveKindMap[".ascii"] = DK_ASCII; 5335 DirectiveKindMap[".asciz"] = DK_ASCIZ; 5336 DirectiveKindMap[".string"] = DK_STRING; 5337 DirectiveKindMap[".byte"] = DK_BYTE; 5338 DirectiveKindMap[".short"] = DK_SHORT; 5339 DirectiveKindMap[".value"] = DK_VALUE; 5340 DirectiveKindMap[".2byte"] = DK_2BYTE; 5341 DirectiveKindMap[".long"] = DK_LONG; 5342 DirectiveKindMap[".int"] = DK_INT; 5343 DirectiveKindMap[".4byte"] = DK_4BYTE; 5344 DirectiveKindMap[".quad"] = DK_QUAD; 5345 DirectiveKindMap[".8byte"] = DK_8BYTE; 5346 DirectiveKindMap[".octa"] = DK_OCTA; 5347 DirectiveKindMap[".single"] = DK_SINGLE; 5348 DirectiveKindMap[".float"] = DK_FLOAT; 5349 DirectiveKindMap[".double"] = DK_DOUBLE; 5350 DirectiveKindMap[".align"] = DK_ALIGN; 5351 DirectiveKindMap[".align32"] = DK_ALIGN32; 5352 DirectiveKindMap[".balign"] = DK_BALIGN; 5353 DirectiveKindMap[".balignw"] = DK_BALIGNW; 5354 DirectiveKindMap[".balignl"] = DK_BALIGNL; 5355 DirectiveKindMap[".p2align"] = DK_P2ALIGN; 5356 DirectiveKindMap[".p2alignw"] = DK_P2ALIGNW; 5357 DirectiveKindMap[".p2alignl"] = DK_P2ALIGNL; 5358 DirectiveKindMap[".org"] = DK_ORG; 5359 DirectiveKindMap[".fill"] = DK_FILL; 5360 DirectiveKindMap[".zero"] = DK_ZERO; 5361 DirectiveKindMap[".extern"] = DK_EXTERN; 5362 DirectiveKindMap[".globl"] = DK_GLOBL; 5363 DirectiveKindMap[".global"] = DK_GLOBAL; 5364 DirectiveKindMap[".lazy_reference"] = DK_LAZY_REFERENCE; 5365 DirectiveKindMap[".no_dead_strip"] = DK_NO_DEAD_STRIP; 5366 DirectiveKindMap[".symbol_resolver"] = DK_SYMBOL_RESOLVER; 5367 DirectiveKindMap[".private_extern"] = DK_PRIVATE_EXTERN; 5368 DirectiveKindMap[".reference"] = DK_REFERENCE; 5369 DirectiveKindMap[".weak_definition"] = DK_WEAK_DEFINITION; 5370 DirectiveKindMap[".weak_reference"] = DK_WEAK_REFERENCE; 5371 DirectiveKindMap[".weak_def_can_be_hidden"] = DK_WEAK_DEF_CAN_BE_HIDDEN; 5372 DirectiveKindMap[".cold"] = DK_COLD; 5373 DirectiveKindMap[".comm"] = DK_COMM; 5374 DirectiveKindMap[".common"] = DK_COMMON; 5375 DirectiveKindMap[".lcomm"] = DK_LCOMM; 5376 DirectiveKindMap[".abort"] = DK_ABORT; 5377 DirectiveKindMap[".include"] = DK_INCLUDE; 5378 DirectiveKindMap[".incbin"] = DK_INCBIN; 5379 DirectiveKindMap[".code16"] = DK_CODE16; 5380 DirectiveKindMap[".code16gcc"] = DK_CODE16GCC; 5381 DirectiveKindMap[".rept"] = DK_REPT; 5382 DirectiveKindMap[".rep"] = DK_REPT; 5383 DirectiveKindMap[".irp"] = DK_IRP; 5384 DirectiveKindMap[".irpc"] = DK_IRPC; 5385 DirectiveKindMap[".endr"] = DK_ENDR; 5386 DirectiveKindMap[".bundle_align_mode"] = DK_BUNDLE_ALIGN_MODE; 5387 DirectiveKindMap[".bundle_lock"] = DK_BUNDLE_LOCK; 5388 DirectiveKindMap[".bundle_unlock"] = DK_BUNDLE_UNLOCK; 5389 DirectiveKindMap[".if"] = DK_IF; 5390 DirectiveKindMap[".ifeq"] = DK_IFEQ; 5391 DirectiveKindMap[".ifge"] = DK_IFGE; 5392 DirectiveKindMap[".ifgt"] = DK_IFGT; 5393 DirectiveKindMap[".ifle"] = DK_IFLE; 5394 DirectiveKindMap[".iflt"] = DK_IFLT; 5395 DirectiveKindMap[".ifne"] = DK_IFNE; 5396 DirectiveKindMap[".ifb"] = DK_IFB; 5397 DirectiveKindMap[".ifnb"] = DK_IFNB; 5398 DirectiveKindMap[".ifc"] = DK_IFC; 5399 DirectiveKindMap[".ifeqs"] = DK_IFEQS; 5400 DirectiveKindMap[".ifnc"] = DK_IFNC; 5401 DirectiveKindMap[".ifnes"] = DK_IFNES; 5402 DirectiveKindMap[".ifdef"] = DK_IFDEF; 5403 DirectiveKindMap[".ifndef"] = DK_IFNDEF; 5404 DirectiveKindMap[".ifnotdef"] = DK_IFNOTDEF; 5405 DirectiveKindMap[".elseif"] = DK_ELSEIF; 5406 DirectiveKindMap[".else"] = DK_ELSE; 5407 DirectiveKindMap[".end"] = DK_END; 5408 DirectiveKindMap[".endif"] = DK_ENDIF; 5409 DirectiveKindMap[".skip"] = DK_SKIP; 5410 DirectiveKindMap[".space"] = DK_SPACE; 5411 DirectiveKindMap[".file"] = DK_FILE; 5412 DirectiveKindMap[".line"] = DK_LINE; 5413 DirectiveKindMap[".loc"] = DK_LOC; 5414 DirectiveKindMap[".stabs"] = DK_STABS; 5415 DirectiveKindMap[".cv_file"] = DK_CV_FILE; 5416 DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID; 5417 DirectiveKindMap[".cv_loc"] = DK_CV_LOC; 5418 DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE; 5419 DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE; 5420 DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID; 5421 DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE; 5422 DirectiveKindMap[".cv_string"] = DK_CV_STRING; 5423 DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE; 5424 DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS; 5425 DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET; 5426 DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA; 5427 DirectiveKindMap[".sleb128"] = DK_SLEB128; 5428 DirectiveKindMap[".uleb128"] = DK_ULEB128; 5429 DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS; 5430 DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC; 5431 DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC; 5432 DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA; 5433 DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET; 5434 DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET; 5435 DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER; 5436 DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET; 5437 DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET; 5438 DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY; 5439 DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA; 5440 DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE; 5441 DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE; 5442 DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE; 5443 DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE; 5444 DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE; 5445 DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN; 5446 DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME; 5447 DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED; 5448 DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER; 5449 DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE; 5450 DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME; 5451 DirectiveKindMap[".macros_on"] = DK_MACROS_ON; 5452 DirectiveKindMap[".macros_off"] = DK_MACROS_OFF; 5453 DirectiveKindMap[".macro"] = DK_MACRO; 5454 DirectiveKindMap[".exitm"] = DK_EXITM; 5455 DirectiveKindMap[".endm"] = DK_ENDM; 5456 DirectiveKindMap[".endmacro"] = DK_ENDMACRO; 5457 DirectiveKindMap[".purgem"] = DK_PURGEM; 5458 DirectiveKindMap[".err"] = DK_ERR; 5459 DirectiveKindMap[".error"] = DK_ERROR; 5460 DirectiveKindMap[".warning"] = DK_WARNING; 5461 DirectiveKindMap[".altmacro"] = DK_ALTMACRO; 5462 DirectiveKindMap[".noaltmacro"] = DK_NOALTMACRO; 5463 DirectiveKindMap[".reloc"] = DK_RELOC; 5464 DirectiveKindMap[".dc"] = DK_DC; 5465 DirectiveKindMap[".dc.a"] = DK_DC_A; 5466 DirectiveKindMap[".dc.b"] = DK_DC_B; 5467 DirectiveKindMap[".dc.d"] = DK_DC_D; 5468 DirectiveKindMap[".dc.l"] = DK_DC_L; 5469 DirectiveKindMap[".dc.s"] = DK_DC_S; 5470 DirectiveKindMap[".dc.w"] = DK_DC_W; 5471 DirectiveKindMap[".dc.x"] = DK_DC_X; 5472 DirectiveKindMap[".dcb"] = DK_DCB; 5473 DirectiveKindMap[".dcb.b"] = DK_DCB_B; 5474 DirectiveKindMap[".dcb.d"] = DK_DCB_D; 5475 DirectiveKindMap[".dcb.l"] = DK_DCB_L; 5476 DirectiveKindMap[".dcb.s"] = DK_DCB_S; 5477 DirectiveKindMap[".dcb.w"] = DK_DCB_W; 5478 DirectiveKindMap[".dcb.x"] = DK_DCB_X; 5479 DirectiveKindMap[".ds"] = DK_DS; 5480 DirectiveKindMap[".ds.b"] = DK_DS_B; 5481 DirectiveKindMap[".ds.d"] = DK_DS_D; 5482 DirectiveKindMap[".ds.l"] = DK_DS_L; 5483 DirectiveKindMap[".ds.p"] = DK_DS_P; 5484 DirectiveKindMap[".ds.s"] = DK_DS_S; 5485 DirectiveKindMap[".ds.w"] = DK_DS_W; 5486 DirectiveKindMap[".ds.x"] = DK_DS_X; 5487 DirectiveKindMap[".print"] = DK_PRINT; 5488 DirectiveKindMap[".addrsig"] = DK_ADDRSIG; 5489 DirectiveKindMap[".addrsig_sym"] = DK_ADDRSIG_SYM; 5490 } 5491 5492 MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { 5493 AsmToken EndToken, StartToken = getTok(); 5494 5495 unsigned NestLevel = 0; 5496 while (true) { 5497 // Check whether we have reached the end of the file. 5498 if (getLexer().is(AsmToken::Eof)) { 5499 printError(DirectiveLoc, "no matching '.endr' in definition"); 5500 return nullptr; 5501 } 5502 5503 if (Lexer.is(AsmToken::Identifier) && 5504 (getTok().getIdentifier() == ".rep" || 5505 getTok().getIdentifier() == ".rept" || 5506 getTok().getIdentifier() == ".irp" || 5507 getTok().getIdentifier() == ".irpc")) { 5508 ++NestLevel; 5509 } 5510 5511 // Otherwise, check whether we have reached the .endr. 5512 if (Lexer.is(AsmToken::Identifier) && getTok().getIdentifier() == ".endr") { 5513 if (NestLevel == 0) { 5514 EndToken = getTok(); 5515 Lex(); 5516 if (Lexer.isNot(AsmToken::EndOfStatement)) { 5517 printError(getTok().getLoc(), 5518 "unexpected token in '.endr' directive"); 5519 return nullptr; 5520 } 5521 break; 5522 } 5523 --NestLevel; 5524 } 5525 5526 // Otherwise, scan till the end of the statement. 5527 eatToEndOfStatement(); 5528 } 5529 5530 const char *BodyStart = StartToken.getLoc().getPointer(); 5531 const char *BodyEnd = EndToken.getLoc().getPointer(); 5532 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); 5533 5534 // We Are Anonymous. 5535 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters()); 5536 return &MacroLikeBodies.back(); 5537 } 5538 5539 void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, 5540 raw_svector_ostream &OS) { 5541 OS << ".endr\n"; 5542 5543 std::unique_ptr<MemoryBuffer> Instantiation = 5544 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>"); 5545 5546 // Create the macro instantiation object and add to the current macro 5547 // instantiation stack. 5548 MacroInstantiation *MI = new MacroInstantiation( 5549 DirectiveLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()); 5550 ActiveMacros.push_back(MI); 5551 5552 // Jump to the macro instantiation and prime the lexer. 5553 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc()); 5554 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer()); 5555 Lex(); 5556 } 5557 5558 /// parseDirectiveRept 5559 /// ::= .rep | .rept count 5560 bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { 5561 const MCExpr *CountExpr; 5562 SMLoc CountLoc = getTok().getLoc(); 5563 if (parseExpression(CountExpr)) 5564 return true; 5565 5566 int64_t Count; 5567 if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) { 5568 return Error(CountLoc, "unexpected token in '" + Dir + "' directive"); 5569 } 5570 5571 if (check(Count < 0, CountLoc, "Count is negative") || 5572 parseToken(AsmToken::EndOfStatement, 5573 "unexpected token in '" + Dir + "' directive")) 5574 return true; 5575 5576 // Lex the rept definition. 5577 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); 5578 if (!M) 5579 return true; 5580 5581 // Macro instantiation is lexical, unfortunately. We construct a new buffer 5582 // to hold the macro body with substitutions. 5583 SmallString<256> Buf; 5584 raw_svector_ostream OS(Buf); 5585 while (Count--) { 5586 // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). 5587 if (expandMacro(OS, M->Body, None, None, false, getTok().getLoc())) 5588 return true; 5589 } 5590 instantiateMacroLikeBody(M, DirectiveLoc, OS); 5591 5592 return false; 5593 } 5594 5595 /// parseDirectiveIrp 5596 /// ::= .irp symbol,values 5597 bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { 5598 MCAsmMacroParameter Parameter; 5599 MCAsmMacroArguments A; 5600 if (check(parseIdentifier(Parameter.Name), 5601 "expected identifier in '.irp' directive") || 5602 parseToken(AsmToken::Comma, "expected comma in '.irp' directive") || 5603 parseMacroArguments(nullptr, A) || 5604 parseToken(AsmToken::EndOfStatement, "expected End of Statement")) 5605 return true; 5606 5607 // Lex the irp definition. 5608 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); 5609 if (!M) 5610 return true; 5611 5612 // Macro instantiation is lexical, unfortunately. We construct a new buffer 5613 // to hold the macro body with substitutions. 5614 SmallString<256> Buf; 5615 raw_svector_ostream OS(Buf); 5616 5617 for (const MCAsmMacroArgument &Arg : A) { 5618 // Note that the AtPseudoVariable is enabled for instantiations of .irp. 5619 // This is undocumented, but GAS seems to support it. 5620 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) 5621 return true; 5622 } 5623 5624 instantiateMacroLikeBody(M, DirectiveLoc, OS); 5625 5626 return false; 5627 } 5628 5629 /// parseDirectiveIrpc 5630 /// ::= .irpc symbol,values 5631 bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { 5632 MCAsmMacroParameter Parameter; 5633 MCAsmMacroArguments A; 5634 5635 if (check(parseIdentifier(Parameter.Name), 5636 "expected identifier in '.irpc' directive") || 5637 parseToken(AsmToken::Comma, "expected comma in '.irpc' directive") || 5638 parseMacroArguments(nullptr, A)) 5639 return true; 5640 5641 if (A.size() != 1 || A.front().size() != 1) 5642 return TokError("unexpected token in '.irpc' directive"); 5643 5644 // Eat the end of statement. 5645 if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) 5646 return true; 5647 5648 // Lex the irpc definition. 5649 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); 5650 if (!M) 5651 return true; 5652 5653 // Macro instantiation is lexical, unfortunately. We construct a new buffer 5654 // to hold the macro body with substitutions. 5655 SmallString<256> Buf; 5656 raw_svector_ostream OS(Buf); 5657 5658 StringRef Values = A.front().front().getString(); 5659 for (std::size_t I = 0, End = Values.size(); I != End; ++I) { 5660 MCAsmMacroArgument Arg; 5661 Arg.emplace_back(AsmToken::Identifier, Values.slice(I, I + 1)); 5662 5663 // Note that the AtPseudoVariable is enabled for instantiations of .irpc. 5664 // This is undocumented, but GAS seems to support it. 5665 if (expandMacro(OS, M->Body, Parameter, Arg, true, getTok().getLoc())) 5666 return true; 5667 } 5668 5669 instantiateMacroLikeBody(M, DirectiveLoc, OS); 5670 5671 return false; 5672 } 5673 5674 bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { 5675 if (ActiveMacros.empty()) 5676 return TokError("unmatched '.endr' directive"); 5677 5678 // The only .repl that should get here are the ones created by 5679 // instantiateMacroLikeBody. 5680 assert(getLexer().is(AsmToken::EndOfStatement)); 5681 5682 handleMacroExit(); 5683 return false; 5684 } 5685 5686 bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, 5687 size_t Len) { 5688 const MCExpr *Value; 5689 SMLoc ExprLoc = getLexer().getLoc(); 5690 if (parseExpression(Value)) 5691 return true; 5692 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); 5693 if (!MCE) 5694 return Error(ExprLoc, "unexpected expression in _emit"); 5695 uint64_t IntValue = MCE->getValue(); 5696 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue)) 5697 return Error(ExprLoc, "literal value out of range for directive"); 5698 5699 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len); 5700 return false; 5701 } 5702 5703 bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { 5704 const MCExpr *Value; 5705 SMLoc ExprLoc = getLexer().getLoc(); 5706 if (parseExpression(Value)) 5707 return true; 5708 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value); 5709 if (!MCE) 5710 return Error(ExprLoc, "unexpected expression in align"); 5711 uint64_t IntValue = MCE->getValue(); 5712 if (!isPowerOf2_64(IntValue)) 5713 return Error(ExprLoc, "literal value not a power of two greater then zero"); 5714 5715 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue)); 5716 return false; 5717 } 5718 5719 bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) { 5720 const AsmToken StrTok = getTok(); 5721 Lex(); 5722 if (StrTok.isNot(AsmToken::String) || StrTok.getString().front() != '"') 5723 return Error(DirectiveLoc, "expected double quoted string after .print"); 5724 if (parseToken(AsmToken::EndOfStatement, "expected end of statement")) 5725 return true; 5726 llvm::outs() << StrTok.getStringContents() << '\n'; 5727 return false; 5728 } 5729 5730 bool AsmParser::parseDirectiveAddrsig() { 5731 getStreamer().EmitAddrsig(); 5732 return false; 5733 } 5734 5735 bool AsmParser::parseDirectiveAddrsigSym() { 5736 StringRef Name; 5737 if (check(parseIdentifier(Name), 5738 "expected identifier in '.addrsig_sym' directive")) 5739 return true; 5740 MCSymbol *Sym = getContext().getOrCreateSymbol(Name); 5741 getStreamer().EmitAddrsigSym(Sym); 5742 return false; 5743 } 5744 5745 // We are comparing pointers, but the pointers are relative to a single string. 5746 // Thus, this should always be deterministic. 5747 static int rewritesSort(const AsmRewrite *AsmRewriteA, 5748 const AsmRewrite *AsmRewriteB) { 5749 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) 5750 return -1; 5751 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) 5752 return 1; 5753 5754 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output 5755 // rewrite to the same location. Make sure the SizeDirective rewrite is 5756 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This 5757 // ensures the sort algorithm is stable. 5758 if (AsmRewritePrecedence[AsmRewriteA->Kind] > 5759 AsmRewritePrecedence[AsmRewriteB->Kind]) 5760 return -1; 5761 5762 if (AsmRewritePrecedence[AsmRewriteA->Kind] < 5763 AsmRewritePrecedence[AsmRewriteB->Kind]) 5764 return 1; 5765 llvm_unreachable("Unstable rewrite sort."); 5766 } 5767 5768 bool AsmParser::parseMSInlineAsm( 5769 void *AsmLoc, std::string &AsmString, unsigned &NumOutputs, 5770 unsigned &NumInputs, SmallVectorImpl<std::pair<void *, bool>> &OpDecls, 5771 SmallVectorImpl<std::string> &Constraints, 5772 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, 5773 const MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { 5774 SmallVector<void *, 4> InputDecls; 5775 SmallVector<void *, 4> OutputDecls; 5776 SmallVector<bool, 4> InputDeclsAddressOf; 5777 SmallVector<bool, 4> OutputDeclsAddressOf; 5778 SmallVector<std::string, 4> InputConstraints; 5779 SmallVector<std::string, 4> OutputConstraints; 5780 SmallVector<unsigned, 4> ClobberRegs; 5781 5782 SmallVector<AsmRewrite, 4> AsmStrRewrites; 5783 5784 // Prime the lexer. 5785 Lex(); 5786 5787 // While we have input, parse each statement. 5788 unsigned InputIdx = 0; 5789 unsigned OutputIdx = 0; 5790 while (getLexer().isNot(AsmToken::Eof)) { 5791 // Parse curly braces marking block start/end 5792 if (parseCurlyBlockScope(AsmStrRewrites)) 5793 continue; 5794 5795 ParseStatementInfo Info(&AsmStrRewrites); 5796 bool StatementErr = parseStatement(Info, &SI); 5797 5798 if (StatementErr || Info.ParseError) { 5799 // Emit pending errors if any exist. 5800 printPendingErrors(); 5801 return true; 5802 } 5803 5804 // No pending error should exist here. 5805 assert(!hasPendingError() && "unexpected error from parseStatement"); 5806 5807 if (Info.Opcode == ~0U) 5808 continue; 5809 5810 const MCInstrDesc &Desc = MII->get(Info.Opcode); 5811 5812 // Build the list of clobbers, outputs and inputs. 5813 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { 5814 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; 5815 5816 // Immediate. 5817 if (Operand.isImm()) 5818 continue; 5819 5820 // Register operand. 5821 if (Operand.isReg() && !Operand.needAddressOf() && 5822 !getTargetParser().OmitRegisterFromClobberLists(Operand.getReg())) { 5823 unsigned NumDefs = Desc.getNumDefs(); 5824 // Clobber. 5825 if (NumDefs && Operand.getMCOperandNum() < NumDefs) 5826 ClobberRegs.push_back(Operand.getReg()); 5827 continue; 5828 } 5829 5830 // Expr/Input or Output. 5831 StringRef SymName = Operand.getSymName(); 5832 if (SymName.empty()) 5833 continue; 5834 5835 void *OpDecl = Operand.getOpDecl(); 5836 if (!OpDecl) 5837 continue; 5838 5839 bool isOutput = (i == 1) && Desc.mayStore(); 5840 SMLoc Start = SMLoc::getFromPointer(SymName.data()); 5841 if (isOutput) { 5842 ++InputIdx; 5843 OutputDecls.push_back(OpDecl); 5844 OutputDeclsAddressOf.push_back(Operand.needAddressOf()); 5845 OutputConstraints.push_back(("=" + Operand.getConstraint()).str()); 5846 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size()); 5847 } else { 5848 InputDecls.push_back(OpDecl); 5849 InputDeclsAddressOf.push_back(Operand.needAddressOf()); 5850 InputConstraints.push_back(Operand.getConstraint().str()); 5851 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size()); 5852 } 5853 } 5854 5855 // Consider implicit defs to be clobbers. Think of cpuid and push. 5856 ArrayRef<MCPhysReg> ImpDefs(Desc.getImplicitDefs(), 5857 Desc.getNumImplicitDefs()); 5858 ClobberRegs.insert(ClobberRegs.end(), ImpDefs.begin(), ImpDefs.end()); 5859 } 5860 5861 // Set the number of Outputs and Inputs. 5862 NumOutputs = OutputDecls.size(); 5863 NumInputs = InputDecls.size(); 5864 5865 // Set the unique clobbers. 5866 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end()); 5867 ClobberRegs.erase(std::unique(ClobberRegs.begin(), ClobberRegs.end()), 5868 ClobberRegs.end()); 5869 Clobbers.assign(ClobberRegs.size(), std::string()); 5870 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { 5871 raw_string_ostream OS(Clobbers[I]); 5872 IP->printRegName(OS, ClobberRegs[I]); 5873 } 5874 5875 // Merge the various outputs and inputs. Output are expected first. 5876 if (NumOutputs || NumInputs) { 5877 unsigned NumExprs = NumOutputs + NumInputs; 5878 OpDecls.resize(NumExprs); 5879 Constraints.resize(NumExprs); 5880 for (unsigned i = 0; i < NumOutputs; ++i) { 5881 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]); 5882 Constraints[i] = OutputConstraints[i]; 5883 } 5884 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { 5885 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]); 5886 Constraints[j] = InputConstraints[i]; 5887 } 5888 } 5889 5890 // Build the IR assembly string. 5891 std::string AsmStringIR; 5892 raw_string_ostream OS(AsmStringIR); 5893 StringRef ASMString = 5894 SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID())->getBuffer(); 5895 const char *AsmStart = ASMString.begin(); 5896 const char *AsmEnd = ASMString.end(); 5897 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort); 5898 for (const AsmRewrite &AR : AsmStrRewrites) { 5899 AsmRewriteKind Kind = AR.Kind; 5900 5901 const char *Loc = AR.Loc.getPointer(); 5902 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!"); 5903 5904 // Emit everything up to the immediate/expression. 5905 if (unsigned Len = Loc - AsmStart) 5906 OS << StringRef(AsmStart, Len); 5907 5908 // Skip the original expression. 5909 if (Kind == AOK_Skip) { 5910 AsmStart = Loc + AR.Len; 5911 continue; 5912 } 5913 5914 unsigned AdditionalSkip = 0; 5915 // Rewrite expressions in $N notation. 5916 switch (Kind) { 5917 default: 5918 break; 5919 case AOK_IntelExpr: 5920 assert(AR.IntelExp.isValid() && "cannot write invalid intel expression"); 5921 if (AR.IntelExp.NeedBracs) 5922 OS << "["; 5923 if (AR.IntelExp.hasBaseReg()) 5924 OS << AR.IntelExp.BaseReg; 5925 if (AR.IntelExp.hasIndexReg()) 5926 OS << (AR.IntelExp.hasBaseReg() ? " + " : "") 5927 << AR.IntelExp.IndexReg; 5928 if (AR.IntelExp.Scale > 1) 5929 OS << " * $$" << AR.IntelExp.Scale; 5930 if (AR.IntelExp.Imm || !AR.IntelExp.hasRegs()) 5931 OS << (AR.IntelExp.hasRegs() ? " + $$" : "$$") << AR.IntelExp.Imm; 5932 if (AR.IntelExp.NeedBracs) 5933 OS << "]"; 5934 break; 5935 case AOK_Label: 5936 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; 5937 break; 5938 case AOK_Input: 5939 OS << '$' << InputIdx++; 5940 break; 5941 case AOK_Output: 5942 OS << '$' << OutputIdx++; 5943 break; 5944 case AOK_SizeDirective: 5945 switch (AR.Val) { 5946 default: break; 5947 case 8: OS << "byte ptr "; break; 5948 case 16: OS << "word ptr "; break; 5949 case 32: OS << "dword ptr "; break; 5950 case 64: OS << "qword ptr "; break; 5951 case 80: OS << "xword ptr "; break; 5952 case 128: OS << "xmmword ptr "; break; 5953 case 256: OS << "ymmword ptr "; break; 5954 } 5955 break; 5956 case AOK_Emit: 5957 OS << ".byte"; 5958 break; 5959 case AOK_Align: { 5960 // MS alignment directives are measured in bytes. If the native assembler 5961 // measures alignment in bytes, we can pass it straight through. 5962 OS << ".align"; 5963 if (getContext().getAsmInfo()->getAlignmentIsInBytes()) 5964 break; 5965 5966 // Alignment is in log2 form, so print that instead and skip the original 5967 // immediate. 5968 unsigned Val = AR.Val; 5969 OS << ' ' << Val; 5970 assert(Val < 10 && "Expected alignment less then 2^10."); 5971 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; 5972 break; 5973 } 5974 case AOK_EVEN: 5975 OS << ".even"; 5976 break; 5977 case AOK_EndOfStatement: 5978 OS << "\n\t"; 5979 break; 5980 } 5981 5982 // Skip the original expression. 5983 AsmStart = Loc + AR.Len + AdditionalSkip; 5984 } 5985 5986 // Emit the remainder of the asm string. 5987 if (AsmStart != AsmEnd) 5988 OS << StringRef(AsmStart, AsmEnd - AsmStart); 5989 5990 AsmString = OS.str(); 5991 return false; 5992 } 5993 5994 namespace llvm { 5995 namespace MCParserUtils { 5996 5997 /// Returns whether the given symbol is used anywhere in the given expression, 5998 /// or subexpressions. 5999 static bool isSymbolUsedInExpression(const MCSymbol *Sym, const MCExpr *Value) { 6000 switch (Value->getKind()) { 6001 case MCExpr::Binary: { 6002 const MCBinaryExpr *BE = static_cast<const MCBinaryExpr *>(Value); 6003 return isSymbolUsedInExpression(Sym, BE->getLHS()) || 6004 isSymbolUsedInExpression(Sym, BE->getRHS()); 6005 } 6006 case MCExpr::Target: 6007 case MCExpr::Constant: 6008 return false; 6009 case MCExpr::SymbolRef: { 6010 const MCSymbol &S = 6011 static_cast<const MCSymbolRefExpr *>(Value)->getSymbol(); 6012 if (S.isVariable()) 6013 return isSymbolUsedInExpression(Sym, S.getVariableValue()); 6014 return &S == Sym; 6015 } 6016 case MCExpr::Unary: 6017 return isSymbolUsedInExpression( 6018 Sym, static_cast<const MCUnaryExpr *>(Value)->getSubExpr()); 6019 } 6020 6021 llvm_unreachable("Unknown expr kind!"); 6022 } 6023 6024 bool parseAssignmentExpression(StringRef Name, bool allow_redef, 6025 MCAsmParser &Parser, MCSymbol *&Sym, 6026 const MCExpr *&Value) { 6027 6028 // FIXME: Use better location, we should use proper tokens. 6029 SMLoc EqualLoc = Parser.getTok().getLoc(); 6030 if (Parser.parseExpression(Value)) 6031 return Parser.TokError("missing expression"); 6032 6033 // Note: we don't count b as used in "a = b". This is to allow 6034 // a = b 6035 // b = c 6036 6037 if (Parser.parseToken(AsmToken::EndOfStatement)) 6038 return true; 6039 6040 // Validate that the LHS is allowed to be a variable (either it has not been 6041 // used as a symbol, or it is an absolute symbol). 6042 Sym = Parser.getContext().lookupSymbol(Name); 6043 if (Sym) { 6044 // Diagnose assignment to a label. 6045 // 6046 // FIXME: Diagnostics. Note the location of the definition as a label. 6047 // FIXME: Diagnose assignment to protected identifier (e.g., register name). 6048 if (isSymbolUsedInExpression(Sym, Value)) 6049 return Parser.Error(EqualLoc, "Recursive use of '" + Name + "'"); 6050 else if (Sym->isUndefined(/*SetUsed*/ false) && !Sym->isUsed() && 6051 !Sym->isVariable()) 6052 ; // Allow redefinitions of undefined symbols only used in directives. 6053 else if (Sym->isVariable() && !Sym->isUsed() && allow_redef) 6054 ; // Allow redefinitions of variables that haven't yet been used. 6055 else if (!Sym->isUndefined() && (!Sym->isVariable() || !allow_redef)) 6056 return Parser.Error(EqualLoc, "redefinition of '" + Name + "'"); 6057 else if (!Sym->isVariable()) 6058 return Parser.Error(EqualLoc, "invalid assignment to '" + Name + "'"); 6059 else if (!isa<MCConstantExpr>(Sym->getVariableValue())) 6060 return Parser.Error(EqualLoc, 6061 "invalid reassignment of non-absolute variable '" + 6062 Name + "'"); 6063 } else if (Name == ".") { 6064 Parser.getStreamer().emitValueToOffset(Value, 0, EqualLoc); 6065 return false; 6066 } else 6067 Sym = Parser.getContext().getOrCreateSymbol(Name); 6068 6069 Sym->setRedefinable(allow_redef); 6070 6071 return false; 6072 } 6073 6074 } // end namespace MCParserUtils 6075 } // end namespace llvm 6076 6077 /// Create an MCAsmParser instance. 6078 MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, 6079 MCStreamer &Out, const MCAsmInfo &MAI, 6080 unsigned CB) { 6081 return new AsmParser(SM, C, Out, MAI, CB); 6082 } 6083