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