Thanks to visit codestin.com
Credit goes to llvm.org

LLVM 22.0.0git
MasmParser.cpp
Go to the documentation of this file.
1//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This class implements the parser for assembly files.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/ADT/APFloat.h"
14#include "llvm/ADT/APInt.h"
15#include "llvm/ADT/ArrayRef.h"
16#include "llvm/ADT/BitVector.h"
17#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/StringMap.h"
22#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/Twine.h"
25#include "llvm/MC/MCAsmInfo.h"
26#include "llvm/MC/MCCodeView.h"
27#include "llvm/MC/MCContext.h"
29#include "llvm/MC/MCExpr.h"
31#include "llvm/MC/MCInstrDesc.h"
32#include "llvm/MC/MCInstrInfo.h"
39#include "llvm/MC/MCSection.h"
40#include "llvm/MC/MCStreamer.h"
47#include "llvm/Support/Format.h"
48#include "llvm/Support/MD5.h"
51#include "llvm/Support/Path.h"
52#include "llvm/Support/SMLoc.h"
55#include <algorithm>
56#include <cassert>
57#include <climits>
58#include <cstddef>
59#include <cstdint>
60#include <ctime>
61#include <deque>
62#include <memory>
63#include <optional>
64#include <sstream>
65#include <string>
66#include <tuple>
67#include <utility>
68#include <vector>
69
70using namespace llvm;
71
72namespace {
73
74/// Helper types for tracking macro definitions.
75typedef std::vector<AsmToken> MCAsmMacroArgument;
76typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
77
78/// Helper class for storing information about an active macro instantiation.
79struct MacroInstantiation {
80 /// The location of the instantiation.
81 SMLoc InstantiationLoc;
82
83 /// The buffer where parsing should resume upon instantiation completion.
84 unsigned ExitBuffer;
85
86 /// The location where parsing should resume upon instantiation completion.
87 SMLoc ExitLoc;
88
89 /// The depth of TheCondStack at the start of the instantiation.
90 size_t CondStackDepth;
91};
92
93struct ParseStatementInfo {
94 /// The parsed operands from the last parsed statement.
96
97 /// The opcode from the last parsed instruction.
98 unsigned Opcode = ~0U;
99
100 /// Was there an error parsing the inline assembly?
101 bool ParseError = false;
102
103 /// The value associated with a macro exit.
104 std::optional<std::string> ExitValue;
105
106 SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
107
108 ParseStatementInfo() = delete;
109 ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
110 : AsmRewrites(rewrites) {}
111};
112
113enum FieldType {
114 FT_INTEGRAL, // Initializer: integer expression, stored as an MCExpr.
115 FT_REAL, // Initializer: real number, stored as an APInt.
116 FT_STRUCT // Initializer: struct initializer, stored recursively.
117};
118
119struct FieldInfo;
120struct StructInfo {
121 StringRef Name;
122 bool IsUnion = false;
123 bool Initializable = true;
124 unsigned Alignment = 0;
125 unsigned AlignmentSize = 0;
126 unsigned NextOffset = 0;
127 unsigned Size = 0;
128 std::vector<FieldInfo> Fields;
129 StringMap<size_t> FieldsByName;
130
131 FieldInfo &addField(StringRef FieldName, FieldType FT,
132 unsigned FieldAlignmentSize);
133
134 StructInfo() = default;
135 StructInfo(StringRef StructName, bool Union, unsigned AlignmentValue);
136};
137
138// FIXME: This should probably use a class hierarchy, raw pointers between the
139// objects, and dynamic type resolution instead of a union. On the other hand,
140// ownership then becomes much more complicated; the obvious thing would be to
141// use BumpPtrAllocator, but the lack of a destructor makes that messy.
142
143struct StructInitializer;
144struct IntFieldInfo {
146
147 IntFieldInfo() = default;
148 IntFieldInfo(const SmallVector<const MCExpr *, 1> &V) { Values = V; }
149 IntFieldInfo(SmallVector<const MCExpr *, 1> &&V) { Values = std::move(V); }
150};
151struct RealFieldInfo {
152 SmallVector<APInt, 1> AsIntValues;
153
154 RealFieldInfo() = default;
155 RealFieldInfo(const SmallVector<APInt, 1> &V) { AsIntValues = V; }
156 RealFieldInfo(SmallVector<APInt, 1> &&V) { AsIntValues = std::move(V); }
157};
158struct StructFieldInfo {
159 std::vector<StructInitializer> Initializers;
160 StructInfo Structure;
161
162 StructFieldInfo() = default;
163 StructFieldInfo(std::vector<StructInitializer> V, StructInfo S);
164};
165
166class FieldInitializer {
167public:
168 FieldType FT;
169 union {
170 IntFieldInfo IntInfo;
171 RealFieldInfo RealInfo;
172 StructFieldInfo StructInfo;
173 };
174
175 ~FieldInitializer();
176 FieldInitializer(FieldType FT);
177
178 FieldInitializer(SmallVector<const MCExpr *, 1> &&Values);
179 FieldInitializer(SmallVector<APInt, 1> &&AsIntValues);
180 FieldInitializer(std::vector<StructInitializer> &&Initializers,
181 struct StructInfo Structure);
182
183 FieldInitializer(const FieldInitializer &Initializer);
184 FieldInitializer(FieldInitializer &&Initializer);
185
186 FieldInitializer &operator=(const FieldInitializer &Initializer);
187 FieldInitializer &operator=(FieldInitializer &&Initializer);
188};
189
190struct StructInitializer {
191 std::vector<FieldInitializer> FieldInitializers;
192};
193
194struct FieldInfo {
195 // Offset of the field within the containing STRUCT.
196 unsigned Offset = 0;
197
198 // Total size of the field (= LengthOf * Type).
199 unsigned SizeOf = 0;
200
201 // Number of elements in the field (1 if scalar, >1 if an array).
202 unsigned LengthOf = 0;
203
204 // Size of a single entry in this field, in bytes ("type" in MASM standards).
205 unsigned Type = 0;
206
207 FieldInitializer Contents;
208
209 FieldInfo(FieldType FT) : Contents(FT) {}
210};
211
212StructFieldInfo::StructFieldInfo(std::vector<StructInitializer> V,
213 StructInfo S) {
214 Initializers = std::move(V);
215 Structure = S;
216}
217
218StructInfo::StructInfo(StringRef StructName, bool Union,
219 unsigned AlignmentValue)
220 : Name(StructName), IsUnion(Union), Alignment(AlignmentValue) {}
221
222FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT,
223 unsigned FieldAlignmentSize) {
224 if (!FieldName.empty())
225 FieldsByName[FieldName.lower()] = Fields.size();
226 Fields.emplace_back(FT);
227 FieldInfo &Field = Fields.back();
228 Field.Offset =
229 llvm::alignTo(NextOffset, std::min(Alignment, FieldAlignmentSize));
230 if (!IsUnion) {
231 NextOffset = std::max(NextOffset, Field.Offset);
232 }
233 AlignmentSize = std::max(AlignmentSize, FieldAlignmentSize);
234 return Field;
235}
236
237FieldInitializer::~FieldInitializer() {
238 switch (FT) {
239 case FT_INTEGRAL:
240 IntInfo.~IntFieldInfo();
241 break;
242 case FT_REAL:
243 RealInfo.~RealFieldInfo();
244 break;
245 case FT_STRUCT:
246 StructInfo.~StructFieldInfo();
247 break;
248 }
249}
250
251FieldInitializer::FieldInitializer(FieldType FT) : FT(FT) {
252 switch (FT) {
253 case FT_INTEGRAL:
254 new (&IntInfo) IntFieldInfo();
255 break;
256 case FT_REAL:
257 new (&RealInfo) RealFieldInfo();
258 break;
259 case FT_STRUCT:
260 new (&StructInfo) StructFieldInfo();
261 break;
262 }
263}
264
265FieldInitializer::FieldInitializer(SmallVector<const MCExpr *, 1> &&Values)
266 : FT(FT_INTEGRAL) {
267 new (&IntInfo) IntFieldInfo(std::move(Values));
268}
269
270FieldInitializer::FieldInitializer(SmallVector<APInt, 1> &&AsIntValues)
271 : FT(FT_REAL) {
272 new (&RealInfo) RealFieldInfo(std::move(AsIntValues));
273}
274
275FieldInitializer::FieldInitializer(
276 std::vector<StructInitializer> &&Initializers, struct StructInfo Structure)
277 : FT(FT_STRUCT) {
278 new (&StructInfo) StructFieldInfo(std::move(Initializers), Structure);
279}
280
281FieldInitializer::FieldInitializer(const FieldInitializer &Initializer)
282 : FT(Initializer.FT) {
283 switch (FT) {
284 case FT_INTEGRAL:
285 new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
286 break;
287 case FT_REAL:
288 new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
289 break;
290 case FT_STRUCT:
291 new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
292 break;
293 }
294}
295
296FieldInitializer::FieldInitializer(FieldInitializer &&Initializer)
297 : FT(Initializer.FT) {
298 switch (FT) {
299 case FT_INTEGRAL:
300 new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
301 break;
302 case FT_REAL:
303 new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
304 break;
305 case FT_STRUCT:
306 new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
307 break;
308 }
309}
310
311FieldInitializer &
312FieldInitializer::operator=(const FieldInitializer &Initializer) {
313 if (FT != Initializer.FT) {
314 switch (FT) {
315 case FT_INTEGRAL:
316 IntInfo.~IntFieldInfo();
317 break;
318 case FT_REAL:
319 RealInfo.~RealFieldInfo();
320 break;
321 case FT_STRUCT:
322 StructInfo.~StructFieldInfo();
323 break;
324 }
325 }
326 FT = Initializer.FT;
327 switch (FT) {
328 case FT_INTEGRAL:
329 IntInfo = Initializer.IntInfo;
330 break;
331 case FT_REAL:
332 RealInfo = Initializer.RealInfo;
333 break;
334 case FT_STRUCT:
335 StructInfo = Initializer.StructInfo;
336 break;
337 }
338 return *this;
339}
340
341FieldInitializer &FieldInitializer::operator=(FieldInitializer &&Initializer) {
342 if (FT != Initializer.FT) {
343 switch (FT) {
344 case FT_INTEGRAL:
345 IntInfo.~IntFieldInfo();
346 break;
347 case FT_REAL:
348 RealInfo.~RealFieldInfo();
349 break;
350 case FT_STRUCT:
351 StructInfo.~StructFieldInfo();
352 break;
353 }
354 }
355 FT = Initializer.FT;
356 switch (FT) {
357 case FT_INTEGRAL:
358 IntInfo = Initializer.IntInfo;
359 break;
360 case FT_REAL:
361 RealInfo = Initializer.RealInfo;
362 break;
363 case FT_STRUCT:
364 StructInfo = Initializer.StructInfo;
365 break;
366 }
367 return *this;
368}
369
370/// The concrete assembly parser instance.
371// Note that this is a full MCAsmParser, not an MCAsmParserExtension!
372// It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
373class MasmParser : public MCAsmParser {
374private:
375 SourceMgr::DiagHandlerTy SavedDiagHandler;
376 void *SavedDiagContext;
377 std::unique_ptr<MCAsmParserExtension> PlatformParser;
378
379 /// This is the current buffer index we're lexing from as managed by the
380 /// SourceMgr object.
381 unsigned CurBuffer;
382
383 /// time of assembly
384 struct tm TM;
385
386 BitVector EndStatementAtEOFStack;
387
388 AsmCond TheCondState;
389 std::vector<AsmCond> TheCondStack;
390
391 /// maps directive names to handler methods in parser
392 /// extensions. Extensions register themselves in this map by calling
393 /// addDirectiveHandler.
394 StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
395
396 /// maps assembly-time variable names to variables.
397 struct Variable {
398 enum RedefinableKind { NOT_REDEFINABLE, WARN_ON_REDEFINITION, REDEFINABLE };
399
400 StringRef Name;
401 RedefinableKind Redefinable = REDEFINABLE;
402 bool IsText = false;
403 std::string TextValue;
404 };
405 StringMap<Variable> Variables;
406
407 /// Stack of active struct definitions.
408 SmallVector<StructInfo, 1> StructInProgress;
409
410 /// Maps struct tags to struct definitions.
411 StringMap<StructInfo> Structs;
412
413 /// Maps data location names to types.
414 StringMap<AsmTypeInfo> KnownType;
415
416 /// Stack of active macro instantiations.
417 std::vector<MacroInstantiation*> ActiveMacros;
418
419 /// List of bodies of anonymous macros.
420 std::deque<MCAsmMacro> MacroLikeBodies;
421
422 /// Keeps track of how many .macro's have been instantiated.
423 unsigned NumOfMacroInstantiations;
424
425 /// The values from the last parsed cpp hash file line comment if any.
426 struct CppHashInfoTy {
427 StringRef Filename;
428 int64_t LineNumber;
429 SMLoc Loc;
430 unsigned Buf;
431 CppHashInfoTy() : LineNumber(0), Buf(0) {}
432 };
433 CppHashInfoTy CppHashInfo;
434
435 /// The filename from the first cpp hash file line comment, if any.
436 StringRef FirstCppHashFilename;
437
438 /// List of forward directional labels for diagnosis at the end.
440
441 /// AssemblerDialect. ~OU means unset value and use value provided by MAI.
442 /// Defaults to 1U, meaning Intel.
443 unsigned AssemblerDialect = 1U;
444
445 /// Are we parsing ms-style inline assembly?
446 bool ParsingMSInlineAsm = false;
447
448 // Current <...> expression depth.
449 unsigned AngleBracketDepth = 0U;
450
451 // Number of locals defined.
452 uint16_t LocalCounter = 0;
453
454public:
455 MasmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
456 const MCAsmInfo &MAI, struct tm TM, unsigned CB = 0);
457 MasmParser(const MasmParser &) = delete;
458 MasmParser &operator=(const MasmParser &) = delete;
459 ~MasmParser() override;
460
461 bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
462
463 void addDirectiveHandler(StringRef Directive,
464 ExtensionDirectiveHandler Handler) override {
465 ExtensionDirectiveMap[Directive] = Handler;
466 DirectiveKindMap.try_emplace(Directive, DK_HANDLER_DIRECTIVE);
467 }
468
469 void addAliasForDirective(StringRef Directive, StringRef Alias) override {
470 DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
471 }
472
473 /// @name MCAsmParser Interface
474 /// {
475
476 unsigned getAssemblerDialect() override {
477 if (AssemblerDialect == ~0U)
478 return MAI.getAssemblerDialect();
479 else
480 return AssemblerDialect;
481 }
482 void setAssemblerDialect(unsigned i) override {
483 AssemblerDialect = i;
484 }
485
486 void Note(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt) override;
487 bool Warning(SMLoc L, const Twine &Msg,
488 SMRange Range = std::nullopt) override;
489 bool printError(SMLoc L, const Twine &Msg,
490 SMRange Range = std::nullopt) override;
491
492 enum ExpandKind { ExpandMacros, DoNotExpandMacros };
493 const AsmToken &Lex(ExpandKind ExpandNextToken);
494 const AsmToken &Lex() override { return Lex(ExpandMacros); }
495
496 void setParsingMSInlineAsm(bool V) override {
497 ParsingMSInlineAsm = V;
498 // When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
499 // hex integer literals.
500 Lexer.setLexMasmIntegers(V);
501 }
502 bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
503
504 bool isParsingMasm() const override { return true; }
505
506 bool defineMacro(StringRef Name, StringRef Value) override;
507
508 bool lookUpField(StringRef Name, AsmFieldInfo &Info) const override;
509 bool lookUpField(StringRef Base, StringRef Member,
510 AsmFieldInfo &Info) const override;
511
512 bool lookUpType(StringRef Name, AsmTypeInfo &Info) const override;
513
514 bool parseMSInlineAsm(std::string &AsmString, unsigned &NumOutputs,
515 unsigned &NumInputs,
516 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
517 SmallVectorImpl<std::string> &Constraints,
518 SmallVectorImpl<std::string> &Clobbers,
519 const MCInstrInfo *MII, MCInstPrinter *IP,
520 MCAsmParserSemaCallback &SI) override;
521
522 bool parseExpression(const MCExpr *&Res);
523 bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
524 bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
525 AsmTypeInfo *TypeInfo) override;
526 bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
527 bool parseAbsoluteExpression(int64_t &Res) override;
528
529 /// Parse a floating point expression using the float \p Semantics
530 /// and set \p Res to the value.
531 bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
532
533 /// Parse an identifier or string (as a quoted identifier)
534 /// and set \p Res to the identifier contents.
535 enum IdentifierPositionKind { StandardPosition, StartOfStatement };
536 bool parseIdentifier(StringRef &Res, IdentifierPositionKind Position);
537 bool parseIdentifier(StringRef &Res) override {
538 return parseIdentifier(Res, StandardPosition);
539 }
540 void eatToEndOfStatement() override;
541
542 bool checkForValidSection() override;
543
544 /// }
545
546private:
547 bool expandMacros();
548 const AsmToken peekTok(bool ShouldSkipSpace = true);
549
550 bool parseStatement(ParseStatementInfo &Info,
551 MCAsmParserSemaCallback *SI);
552 bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
553 bool parseCppHashLineFilenameComment(SMLoc L);
554
555 bool expandMacro(raw_svector_ostream &OS, StringRef Body,
558 const std::vector<std::string> &Locals, SMLoc L);
559
560 /// Are we inside a macro instantiation?
561 bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
562
563 /// Handle entry to macro instantiation.
564 ///
565 /// \param M The macro.
566 /// \param NameLoc Instantiation location.
567 bool handleMacroEntry(
568 const MCAsmMacro *M, SMLoc NameLoc,
570
571 /// Handle invocation of macro function.
572 ///
573 /// \param M The macro.
574 /// \param NameLoc Invocation location.
575 bool handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc);
576
577 /// Handle exit from macro instantiation.
578 void handleMacroExit();
579
580 /// Extract AsmTokens for a macro argument.
581 bool
582 parseMacroArgument(const MCAsmMacroParameter *MP, MCAsmMacroArgument &MA,
584
585 /// Parse all macro arguments for a given macro.
586 bool
587 parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A,
589
590 void printMacroInstantiations();
591
592 bool expandStatement(SMLoc Loc);
593
594 void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
595 SMRange Range = std::nullopt) const {
597 SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
598 }
599 static void DiagHandler(const SMDiagnostic &Diag, void *Context);
600
601 bool lookUpField(const StructInfo &Structure, StringRef Member,
602 AsmFieldInfo &Info) const;
603
604 /// Enter the specified file. This returns true on failure.
605 bool enterIncludeFile(const std::string &Filename);
606
607 /// Reset the current lexer position to that given by \p Loc. The
608 /// current token is not set; clients should ensure Lex() is called
609 /// subsequently.
610 ///
611 /// \param InBuffer If not 0, should be the known buffer id that contains the
612 /// location.
613 void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0,
614 bool EndStatementAtEOF = true);
615
616 /// Parse up to a token of kind \p EndTok and return the contents from the
617 /// current token up to (but not including) this token; the current token on
618 /// exit will be either this kind or EOF. Reads through instantiated macro
619 /// functions and text macros.
620 SmallVector<StringRef, 1> parseStringRefsTo(AsmToken::TokenKind EndTok);
621 std::string parseStringTo(AsmToken::TokenKind EndTok);
622
623 /// Parse up to the end of statement and return the contents from the current
624 /// token until the end of the statement; the current token on exit will be
625 /// either the EndOfStatement or EOF.
626 StringRef parseStringToEndOfStatement() override;
627
628 bool parseTextItem(std::string &Data);
629
630 unsigned getBinOpPrecedence(AsmToken::TokenKind K,
632
633 bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
634 bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
635 bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
636
637 // Generic (target and platform independent) directive parsing.
638 enum DirectiveKind {
639 DK_NO_DIRECTIVE, // Placeholder
640 DK_HANDLER_DIRECTIVE,
641 DK_ASSIGN,
642 DK_EQU,
643 DK_TEXTEQU,
644 DK_ASCII,
645 DK_ASCIZ,
646 DK_STRING,
647 DK_BYTE,
648 DK_SBYTE,
649 DK_WORD,
650 DK_SWORD,
651 DK_DWORD,
652 DK_SDWORD,
653 DK_FWORD,
654 DK_QWORD,
655 DK_SQWORD,
656 DK_DB,
657 DK_DD,
658 DK_DF,
659 DK_DQ,
660 DK_DW,
661 DK_REAL4,
662 DK_REAL8,
663 DK_REAL10,
664 DK_ALIGN,
665 DK_EVEN,
666 DK_ORG,
667 DK_ENDR,
668 DK_EXTERN,
669 DK_PUBLIC,
670 DK_COMM,
671 DK_COMMENT,
672 DK_INCLUDE,
673 DK_REPEAT,
674 DK_WHILE,
675 DK_FOR,
676 DK_FORC,
677 DK_IF,
678 DK_IFE,
679 DK_IFB,
680 DK_IFNB,
681 DK_IFDEF,
682 DK_IFNDEF,
683 DK_IFDIF,
684 DK_IFDIFI,
685 DK_IFIDN,
686 DK_IFIDNI,
687 DK_ELSEIF,
688 DK_ELSEIFE,
689 DK_ELSEIFB,
690 DK_ELSEIFNB,
691 DK_ELSEIFDEF,
692 DK_ELSEIFNDEF,
693 DK_ELSEIFDIF,
694 DK_ELSEIFDIFI,
695 DK_ELSEIFIDN,
696 DK_ELSEIFIDNI,
697 DK_ELSE,
698 DK_ENDIF,
699
700 DK_MACRO,
701 DK_EXITM,
702 DK_ENDM,
703 DK_PURGE,
704 DK_ERR,
705 DK_ERRB,
706 DK_ERRNB,
707 DK_ERRDEF,
708 DK_ERRNDEF,
709 DK_ERRDIF,
710 DK_ERRDIFI,
711 DK_ERRIDN,
712 DK_ERRIDNI,
713 DK_ERRE,
714 DK_ERRNZ,
715 DK_ECHO,
716 DK_STRUCT,
717 DK_UNION,
718 DK_ENDS,
719 DK_END,
720 DK_PUSHFRAME,
721 DK_PUSHREG,
722 DK_SAVEREG,
723 DK_SAVEXMM128,
724 DK_SETFRAME,
725 DK_RADIX,
726 };
727
728 /// Maps directive name --> DirectiveKind enum, for directives parsed by this
729 /// class.
730 StringMap<DirectiveKind> DirectiveKindMap;
731
732 bool isMacroLikeDirective();
733
734 // Generic (target and platform independent) directive parsing.
735 enum BuiltinSymbol {
736 BI_NO_SYMBOL, // Placeholder
737 BI_DATE,
738 BI_TIME,
739 BI_VERSION,
740 BI_FILECUR,
741 BI_FILENAME,
742 BI_LINE,
743 BI_CURSEG,
744 BI_CPU,
745 BI_INTERFACE,
746 BI_CODE,
747 BI_DATA,
748 BI_FARDATA,
749 BI_WORDSIZE,
750 BI_CODESIZE,
751 BI_DATASIZE,
752 BI_MODEL,
753 BI_STACK,
754 };
755
756 /// Maps builtin name --> BuiltinSymbol enum, for builtins handled by this
757 /// class.
758 StringMap<BuiltinSymbol> BuiltinSymbolMap;
759
760 const MCExpr *evaluateBuiltinValue(BuiltinSymbol Symbol, SMLoc StartLoc);
761
762 std::optional<std::string> evaluateBuiltinTextMacro(BuiltinSymbol Symbol,
763 SMLoc StartLoc);
764
765 // Generic (target and platform independent) directive parsing.
766 enum BuiltinFunction {
767 BI_NO_FUNCTION, // Placeholder
768 BI_CATSTR,
769 };
770
771 /// Maps builtin name --> BuiltinFunction enum, for builtins handled by this
772 /// class.
773 StringMap<BuiltinFunction> BuiltinFunctionMap;
774
775 bool evaluateBuiltinMacroFunction(BuiltinFunction Function, StringRef Name,
776 std::string &Res);
777
778 // ".ascii", ".asciz", ".string"
779 bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
780
781 // "byte", "word", ...
782 bool emitIntValue(const MCExpr *Value, unsigned Size);
783 bool parseScalarInitializer(unsigned Size,
784 SmallVectorImpl<const MCExpr *> &Values,
785 unsigned StringPadLength = 0);
786 bool parseScalarInstList(
787 unsigned Size, SmallVectorImpl<const MCExpr *> &Values,
789 bool emitIntegralValues(unsigned Size, unsigned *Count = nullptr);
790 bool addIntegralField(StringRef Name, unsigned Size);
791 bool parseDirectiveValue(StringRef IDVal, unsigned Size);
792 bool parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
793 StringRef Name, SMLoc NameLoc);
794
795 // "real4", "real8", "real10"
796 bool emitRealValues(const fltSemantics &Semantics, unsigned *Count = nullptr);
797 bool addRealField(StringRef Name, const fltSemantics &Semantics, size_t Size);
798 bool parseDirectiveRealValue(StringRef IDVal, const fltSemantics &Semantics,
799 size_t Size);
800 bool parseRealInstList(
801 const fltSemantics &Semantics, SmallVectorImpl<APInt> &Values,
803 bool parseDirectiveNamedRealValue(StringRef TypeName,
804 const fltSemantics &Semantics,
805 unsigned Size, StringRef Name,
806 SMLoc NameLoc);
807
808 bool parseOptionalAngleBracketOpen();
809 bool parseAngleBracketClose(const Twine &Msg = "expected '>'");
810
811 bool parseFieldInitializer(const FieldInfo &Field,
812 FieldInitializer &Initializer);
813 bool parseFieldInitializer(const FieldInfo &Field,
814 const IntFieldInfo &Contents,
815 FieldInitializer &Initializer);
816 bool parseFieldInitializer(const FieldInfo &Field,
817 const RealFieldInfo &Contents,
818 FieldInitializer &Initializer);
819 bool parseFieldInitializer(const FieldInfo &Field,
820 const StructFieldInfo &Contents,
821 FieldInitializer &Initializer);
822
823 bool parseStructInitializer(const StructInfo &Structure,
824 StructInitializer &Initializer);
825 bool parseStructInstList(
826 const StructInfo &Structure, std::vector<StructInitializer> &Initializers,
828
829 bool emitFieldValue(const FieldInfo &Field);
830 bool emitFieldValue(const FieldInfo &Field, const IntFieldInfo &Contents);
831 bool emitFieldValue(const FieldInfo &Field, const RealFieldInfo &Contents);
832 bool emitFieldValue(const FieldInfo &Field, const StructFieldInfo &Contents);
833
834 bool emitFieldInitializer(const FieldInfo &Field,
835 const FieldInitializer &Initializer);
836 bool emitFieldInitializer(const FieldInfo &Field,
837 const IntFieldInfo &Contents,
838 const IntFieldInfo &Initializer);
839 bool emitFieldInitializer(const FieldInfo &Field,
840 const RealFieldInfo &Contents,
841 const RealFieldInfo &Initializer);
842 bool emitFieldInitializer(const FieldInfo &Field,
843 const StructFieldInfo &Contents,
844 const StructFieldInfo &Initializer);
845
846 bool emitStructInitializer(const StructInfo &Structure,
847 const StructInitializer &Initializer);
848
849 // User-defined types (structs, unions):
850 bool emitStructValues(const StructInfo &Structure, unsigned *Count = nullptr);
851 bool addStructField(StringRef Name, const StructInfo &Structure);
852 bool parseDirectiveStructValue(const StructInfo &Structure,
853 StringRef Directive, SMLoc DirLoc);
854 bool parseDirectiveNamedStructValue(const StructInfo &Structure,
855 StringRef Directive, SMLoc DirLoc,
856 StringRef Name);
857
858 // "=", "equ", "textequ"
859 bool parseDirectiveEquate(StringRef IDVal, StringRef Name,
860 DirectiveKind DirKind, SMLoc NameLoc);
861
862 bool parseDirectiveOrg(); // "org"
863
864 bool emitAlignTo(int64_t Alignment);
865 bool parseDirectiveAlign(); // "align"
866 bool parseDirectiveEven(); // "even"
867
868 // macro directives
869 bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
870 bool parseDirectiveExitMacro(SMLoc DirectiveLoc, StringRef Directive,
871 std::string &Value);
872 bool parseDirectiveEndMacro(StringRef Directive);
873 bool parseDirectiveMacro(StringRef Name, SMLoc NameLoc);
874
875 bool parseDirectiveStruct(StringRef Directive, DirectiveKind DirKind,
876 StringRef Name, SMLoc NameLoc);
877 bool parseDirectiveNestedStruct(StringRef Directive, DirectiveKind DirKind);
878 bool parseDirectiveEnds(StringRef Name, SMLoc NameLoc);
879 bool parseDirectiveNestedEnds();
880
881 bool parseDirectiveExtern();
882
883 /// Parse a directive like ".globl" which accepts a single symbol (which
884 /// should be a label or an external).
885 bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
886
887 bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
888
889 bool parseDirectiveComment(SMLoc DirectiveLoc); // "comment"
890
891 bool parseDirectiveInclude(); // "include"
892
893 // "if" or "ife"
894 bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
895 // "ifb" or "ifnb", depending on ExpectBlank.
896 bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
897 // "ifidn", "ifdif", "ifidni", or "ifdifi", depending on ExpectEqual and
898 // CaseInsensitive.
899 bool parseDirectiveIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
900 bool CaseInsensitive);
901 // "ifdef" or "ifndef", depending on expect_defined
902 bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined);
903 // "elseif" or "elseife"
904 bool parseDirectiveElseIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
905 // "elseifb" or "elseifnb", depending on ExpectBlank.
906 bool parseDirectiveElseIfb(SMLoc DirectiveLoc, bool ExpectBlank);
907 // ".elseifdef" or ".elseifndef", depending on expect_defined
908 bool parseDirectiveElseIfdef(SMLoc DirectiveLoc, bool expect_defined);
909 // "elseifidn", "elseifdif", "elseifidni", or "elseifdifi", depending on
910 // ExpectEqual and CaseInsensitive.
911 bool parseDirectiveElseIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
912 bool CaseInsensitive);
913 bool parseDirectiveElse(SMLoc DirectiveLoc); // "else"
914 bool parseDirectiveEndIf(SMLoc DirectiveLoc); // "endif"
915 bool parseEscapedString(std::string &Data) override;
916 bool parseAngleBracketString(std::string &Data) override;
917
918 // Macro-like directives
919 MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc);
920 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
921 raw_svector_ostream &OS);
922 void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
923 SMLoc ExitLoc, raw_svector_ostream &OS);
924 bool parseDirectiveRepeat(SMLoc DirectiveLoc, StringRef Directive);
925 bool parseDirectiveFor(SMLoc DirectiveLoc, StringRef Directive);
926 bool parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive);
927 bool parseDirectiveWhile(SMLoc DirectiveLoc);
928
929 // "_emit" or "__emit"
930 bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info,
931 size_t Len);
932
933 // "align"
934 bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info);
935
936 // "end"
937 bool parseDirectiveEnd(SMLoc DirectiveLoc);
938
939 // ".err"
940 bool parseDirectiveError(SMLoc DirectiveLoc);
941 // ".errb" or ".errnb", depending on ExpectBlank.
942 bool parseDirectiveErrorIfb(SMLoc DirectiveLoc, bool ExpectBlank);
943 // ".errdef" or ".errndef", depending on ExpectBlank.
944 bool parseDirectiveErrorIfdef(SMLoc DirectiveLoc, bool ExpectDefined);
945 // ".erridn", ".errdif", ".erridni", or ".errdifi", depending on ExpectEqual
946 // and CaseInsensitive.
947 bool parseDirectiveErrorIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
948 bool CaseInsensitive);
949 // ".erre" or ".errnz", depending on ExpectZero.
950 bool parseDirectiveErrorIfe(SMLoc DirectiveLoc, bool ExpectZero);
951
952 // ".radix"
953 bool parseDirectiveRadix(SMLoc DirectiveLoc);
954
955 // "echo"
956 bool parseDirectiveEcho(SMLoc DirectiveLoc);
957
958 void initializeDirectiveKindMap();
959 void initializeBuiltinSymbolMaps();
960};
961
962} // end anonymous namespace
963
964namespace llvm {
965
967
969
970} // end namespace llvm
971
972enum { DEFAULT_ADDRSPACE = 0 };
973
974MasmParser::MasmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
975 const MCAsmInfo &MAI, struct tm TM, unsigned CB)
976 : MCAsmParser(Ctx, Out, SM, MAI), CurBuffer(CB ? CB : SM.getMainFileID()),
977 TM(TM) {
978 HadError = false;
979 // Save the old handler.
980 SavedDiagHandler = SrcMgr.getDiagHandler();
981 SavedDiagContext = SrcMgr.getDiagContext();
982 // Set our own handler which calls the saved handler.
983 SrcMgr.setDiagHandler(DiagHandler, this);
984 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
985 EndStatementAtEOFStack.push_back(true);
986
987 // Initialize the platform / file format parser.
988 switch (Ctx.getObjectFileType()) {
989 case MCContext::IsCOFF:
990 PlatformParser.reset(createCOFFMasmParser());
991 break;
992 default:
993 report_fatal_error("llvm-ml currently supports only COFF output.");
994 break;
995 }
996
997 initializeDirectiveKindMap();
998 PlatformParser->Initialize(*this);
999 initializeBuiltinSymbolMaps();
1000
1001 NumOfMacroInstantiations = 0;
1002}
1003
1004MasmParser::~MasmParser() {
1005 assert((HadError || ActiveMacros.empty()) &&
1006 "Unexpected active macro instantiation!");
1007
1008 // Restore the saved diagnostics handler and context for use during
1009 // finalization.
1010 SrcMgr.setDiagHandler(SavedDiagHandler, SavedDiagContext);
1011}
1012
1013void MasmParser::printMacroInstantiations() {
1014 // Print the active macro instantiation stack.
1015 for (std::vector<MacroInstantiation *>::const_reverse_iterator
1016 it = ActiveMacros.rbegin(),
1017 ie = ActiveMacros.rend();
1018 it != ie; ++it)
1019 printMessage((*it)->InstantiationLoc, SourceMgr::DK_Note,
1020 "while in macro instantiation");
1021}
1022
1023void MasmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) {
1024 printPendingErrors();
1025 printMessage(L, SourceMgr::DK_Note, Msg, Range);
1026 printMacroInstantiations();
1027}
1028
1029bool MasmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) {
1030 if (getTargetParser().getTargetOptions().MCNoWarn)
1031 return false;
1032 if (getTargetParser().getTargetOptions().MCFatalWarnings)
1033 return Error(L, Msg, Range);
1034 printMessage(L, SourceMgr::DK_Warning, Msg, Range);
1035 printMacroInstantiations();
1036 return false;
1037}
1038
1039bool MasmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) {
1040 HadError = true;
1041 printMessage(L, SourceMgr::DK_Error, Msg, Range);
1042 printMacroInstantiations();
1043 return true;
1044}
1045
1046bool MasmParser::enterIncludeFile(const std::string &Filename) {
1047 std::string IncludedFile;
1048 unsigned NewBuf =
1049 SrcMgr.AddIncludeFile(Filename, Lexer.getLoc(), IncludedFile);
1050 if (!NewBuf)
1051 return true;
1052
1053 CurBuffer = NewBuf;
1054 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
1055 EndStatementAtEOFStack.push_back(true);
1056 return false;
1057}
1058
1059void MasmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer,
1060 bool EndStatementAtEOF) {
1061 CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc);
1062 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(),
1063 Loc.getPointer(), EndStatementAtEOF);
1064}
1065
1066bool MasmParser::expandMacros() {
1067 const AsmToken &Tok = getTok();
1068 const std::string IDLower = Tok.getIdentifier().lower();
1069
1070 const llvm::MCAsmMacro *M = getContext().lookupMacro(IDLower);
1071 if (M && M->IsFunction && peekTok().is(AsmToken::LParen)) {
1072 // This is a macro function invocation; expand it in place.
1073 const SMLoc MacroLoc = Tok.getLoc();
1074 const StringRef MacroId = Tok.getIdentifier();
1075 Lexer.Lex();
1076 if (handleMacroInvocation(M, MacroLoc)) {
1077 Lexer.UnLex(AsmToken(AsmToken::Error, MacroId));
1078 Lexer.Lex();
1079 }
1080 return false;
1081 }
1082
1083 std::optional<std::string> ExpandedValue;
1084
1085 if (auto BuiltinIt = BuiltinSymbolMap.find(IDLower);
1086 BuiltinIt != BuiltinSymbolMap.end()) {
1087 ExpandedValue =
1088 evaluateBuiltinTextMacro(BuiltinIt->getValue(), Tok.getLoc());
1089 } else if (auto BuiltinFuncIt = BuiltinFunctionMap.find(IDLower);
1090 BuiltinFuncIt != BuiltinFunctionMap.end()) {
1091 StringRef Name;
1092 if (parseIdentifier(Name)) {
1093 return true;
1094 }
1095 std::string Res;
1096 if (evaluateBuiltinMacroFunction(BuiltinFuncIt->getValue(), Name, Res)) {
1097 return true;
1098 }
1099 ExpandedValue = Res;
1100 } else if (auto VarIt = Variables.find(IDLower);
1101 VarIt != Variables.end() && VarIt->getValue().IsText) {
1102 ExpandedValue = VarIt->getValue().TextValue;
1103 }
1104
1105 if (!ExpandedValue)
1106 return true;
1107 std::unique_ptr<MemoryBuffer> Instantiation =
1108 MemoryBuffer::getMemBufferCopy(*ExpandedValue, "<instantiation>");
1109
1110 // Jump to the macro instantiation and prime the lexer.
1111 CurBuffer =
1112 SrcMgr.AddNewSourceBuffer(std::move(Instantiation), Tok.getEndLoc());
1113 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), nullptr,
1114 /*EndStatementAtEOF=*/false);
1115 EndStatementAtEOFStack.push_back(false);
1116 Lexer.Lex();
1117 return false;
1118}
1119
1120const AsmToken &MasmParser::Lex(ExpandKind ExpandNextToken) {
1121 if (Lexer.getTok().is(AsmToken::Error))
1122 Error(Lexer.getErrLoc(), Lexer.getErr());
1123 bool StartOfStatement = false;
1124
1125 // if it's a end of statement with a comment in it
1126 if (getTok().is(AsmToken::EndOfStatement)) {
1127 // if this is a line comment output it.
1128 if (!getTok().getString().empty() && getTok().getString().front() != '\n' &&
1129 getTok().getString().front() != '\r' && MAI.preserveAsmComments())
1130 Out.addExplicitComment(Twine(getTok().getString()));
1131 StartOfStatement = true;
1132 }
1133
1134 const AsmToken *tok = &Lexer.Lex();
1135
1136 while (ExpandNextToken == ExpandMacros && tok->is(AsmToken::Identifier)) {
1137 if (StartOfStatement) {
1138 AsmToken NextTok;
1139 MutableArrayRef<AsmToken> Buf(NextTok);
1140 size_t ReadCount = Lexer.peekTokens(Buf);
1141 if (ReadCount && NextTok.is(AsmToken::Identifier) &&
1142 (NextTok.getString().equals_insensitive("equ") ||
1143 NextTok.getString().equals_insensitive("textequ"))) {
1144 // This looks like an EQU or TEXTEQU directive; don't expand the
1145 // identifier, allowing for redefinitions.
1146 break;
1147 }
1148 }
1149 if (expandMacros())
1150 break;
1151 }
1152
1153 // Parse comments here to be deferred until end of next statement.
1154 while (tok->is(AsmToken::Comment)) {
1155 if (MAI.preserveAsmComments())
1156 Out.addExplicitComment(Twine(tok->getString()));
1157 tok = &Lexer.Lex();
1158 }
1159
1160 // Recognize and bypass line continuations.
1161 while (tok->is(AsmToken::BackSlash) &&
1162 peekTok().is(AsmToken::EndOfStatement)) {
1163 // Eat both the backslash and the end of statement.
1164 Lexer.Lex();
1165 tok = &Lexer.Lex();
1166 }
1167
1168 if (tok->is(AsmToken::Eof)) {
1169 // If this is the end of an included file, pop the parent file off the
1170 // include stack.
1171 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
1172 if (ParentIncludeLoc != SMLoc()) {
1173 EndStatementAtEOFStack.pop_back();
1174 jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
1175 return Lex();
1176 }
1177 EndStatementAtEOFStack.pop_back();
1178 assert(EndStatementAtEOFStack.empty());
1179 }
1180
1181 return *tok;
1182}
1183
1184const AsmToken MasmParser::peekTok(bool ShouldSkipSpace) {
1185 AsmToken Tok;
1186
1188 size_t ReadCount = Lexer.peekTokens(Buf, ShouldSkipSpace);
1189
1190 if (ReadCount == 0) {
1191 // If this is the end of an included file, pop the parent file off the
1192 // include stack.
1193 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
1194 if (ParentIncludeLoc != SMLoc()) {
1195 EndStatementAtEOFStack.pop_back();
1196 jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
1197 return peekTok(ShouldSkipSpace);
1198 }
1199 EndStatementAtEOFStack.pop_back();
1200 assert(EndStatementAtEOFStack.empty());
1201 }
1202
1203 assert(ReadCount == 1);
1204 return Tok;
1205}
1206
1207bool MasmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
1208 // Create the initial section, if requested.
1209 if (!NoInitialTextSection)
1210 Out.initSections(false, getTargetParser().getSTI());
1211
1212 // Prime the lexer.
1213 Lex();
1214
1215 HadError = false;
1216 AsmCond StartingCondState = TheCondState;
1217 SmallVector<AsmRewrite, 4> AsmStrRewrites;
1218
1219 // While we have input, parse each statement.
1220 while (Lexer.isNot(AsmToken::Eof) ||
1221 SrcMgr.getParentIncludeLoc(CurBuffer) != SMLoc()) {
1222 // Skip through the EOF at the end of an inclusion.
1223 if (Lexer.is(AsmToken::Eof))
1224 Lex();
1225
1226 ParseStatementInfo Info(&AsmStrRewrites);
1227 bool HasError = parseStatement(Info, nullptr);
1228
1229 // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
1230 // for printing ErrMsg via Lex() only if no (presumably better) parser error
1231 // exists.
1232 if (HasError && !hasPendingError() && Lexer.getTok().is(AsmToken::Error))
1233 Lex();
1234
1235 // parseStatement returned true so may need to emit an error.
1236 printPendingErrors();
1237
1238 // Skipping to the next line if needed.
1239 if (HasError && !getLexer().justConsumedEOL())
1240 eatToEndOfStatement();
1241 }
1242
1243 printPendingErrors();
1244
1245 // All errors should have been emitted.
1246 assert(!hasPendingError() && "unexpected error from parseStatement");
1247
1248 if (TheCondState.TheCond != StartingCondState.TheCond ||
1249 TheCondState.Ignore != StartingCondState.Ignore)
1250 printError(getTok().getLoc(), "unmatched .ifs or .elses");
1251
1252 // Check to see that all assembler local symbols were actually defined.
1253 // Targets that don't do subsections via symbols may not want this, though,
1254 // so conservatively exclude them. Only do this if we're finalizing, though,
1255 // as otherwise we won't necessarily have seen everything yet.
1256 if (!NoFinalize) {
1257 // Temporary symbols like the ones for directional jumps don't go in the
1258 // symbol table. They also need to be diagnosed in all (final) cases.
1259 for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) {
1260 if (std::get<2>(LocSym)->isUndefined()) {
1261 // Reset the state of any "# line file" directives we've seen to the
1262 // context as it was at the diagnostic site.
1263 CppHashInfo = std::get<1>(LocSym);
1264 printError(std::get<0>(LocSym), "directional label undefined");
1265 }
1266 }
1267 }
1268
1269 // Finalize the output stream if there are no errors and if the client wants
1270 // us to.
1271 if (!HadError && !NoFinalize)
1272 Out.finish(Lexer.getLoc());
1273
1274 return HadError || getContext().hadError();
1275}
1276
1277bool MasmParser::checkForValidSection() {
1278 if (!ParsingMSInlineAsm && !(getStreamer().getCurrentFragment() &&
1279 getStreamer().getCurrentSectionOnly())) {
1280 Out.initSections(false, getTargetParser().getSTI());
1281 return Error(getTok().getLoc(),
1282 "expected section directive before assembly directive");
1283 }
1284 return false;
1285}
1286
1287/// Throw away the rest of the line for testing purposes.
1288void MasmParser::eatToEndOfStatement() {
1289 while (Lexer.isNot(AsmToken::EndOfStatement)) {
1290 if (Lexer.is(AsmToken::Eof)) {
1291 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
1292 if (ParentIncludeLoc == SMLoc()) {
1293 break;
1294 }
1295
1296 EndStatementAtEOFStack.pop_back();
1297 jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
1298 }
1299
1300 Lexer.Lex();
1301 }
1302
1303 // Eat EOL.
1304 if (Lexer.is(AsmToken::EndOfStatement))
1305 Lexer.Lex();
1306}
1307
1308SmallVector<StringRef, 1>
1309MasmParser::parseStringRefsTo(AsmToken::TokenKind EndTok) {
1310 SmallVector<StringRef, 1> Refs;
1311 const char *Start = getTok().getLoc().getPointer();
1312 while (Lexer.isNot(EndTok)) {
1313 if (Lexer.is(AsmToken::Eof)) {
1314 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
1315 if (ParentIncludeLoc == SMLoc()) {
1316 break;
1317 }
1318 Refs.emplace_back(Start, getTok().getLoc().getPointer() - Start);
1319
1320 EndStatementAtEOFStack.pop_back();
1321 jumpToLoc(ParentIncludeLoc, 0, EndStatementAtEOFStack.back());
1322 Lexer.Lex();
1323 Start = getTok().getLoc().getPointer();
1324 } else {
1325 Lexer.Lex();
1326 }
1327 }
1328 Refs.emplace_back(Start, getTok().getLoc().getPointer() - Start);
1329 return Refs;
1330}
1331
1332std::string MasmParser::parseStringTo(AsmToken::TokenKind EndTok) {
1333 SmallVector<StringRef, 1> Refs = parseStringRefsTo(EndTok);
1334 std::string Str;
1335 for (StringRef S : Refs) {
1336 Str.append(S.str());
1337 }
1338 return Str;
1339}
1340
1341StringRef MasmParser::parseStringToEndOfStatement() {
1342 const char *Start = getTok().getLoc().getPointer();
1343
1344 while (Lexer.isNot(AsmToken::EndOfStatement) && Lexer.isNot(AsmToken::Eof))
1345 Lexer.Lex();
1346
1347 const char *End = getTok().getLoc().getPointer();
1348 return StringRef(Start, End - Start);
1349}
1350
1351/// Parse a paren expression and return it.
1352/// NOTE: This assumes the leading '(' has already been consumed.
1353///
1354/// parenexpr ::= expr)
1355///
1356bool MasmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1357 if (parseExpression(Res))
1358 return true;
1359 EndLoc = Lexer.getTok().getEndLoc();
1360 return parseRParen();
1361}
1362
1363/// Parse a bracket expression and return it.
1364/// NOTE: This assumes the leading '[' has already been consumed.
1365///
1366/// bracketexpr ::= expr]
1367///
1368bool MasmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) {
1369 if (parseExpression(Res))
1370 return true;
1371 EndLoc = getTok().getEndLoc();
1372 if (parseToken(AsmToken::RBrac, "expected ']' in brackets expression"))
1373 return true;
1374 return false;
1375}
1376
1377/// Parse a primary expression and return it.
1378/// primaryexpr ::= (parenexpr
1379/// primaryexpr ::= symbol
1380/// primaryexpr ::= number
1381/// primaryexpr ::= '.'
1382/// primaryexpr ::= ~,+,-,'not' primaryexpr
1383/// primaryexpr ::= string
1384/// (a string is interpreted as a 64-bit number in big-endian base-256)
1385bool MasmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
1386 AsmTypeInfo *TypeInfo) {
1387 SMLoc FirstTokenLoc = getLexer().getLoc();
1388 AsmToken::TokenKind FirstTokenKind = Lexer.getKind();
1389 switch (FirstTokenKind) {
1390 default:
1391 return TokError("unknown token in expression");
1392 // If we have an error assume that we've already handled it.
1393 case AsmToken::Error:
1394 return true;
1395 case AsmToken::Exclaim:
1396 Lex(); // Eat the operator.
1397 if (parsePrimaryExpr(Res, EndLoc, nullptr))
1398 return true;
1399 Res = MCUnaryExpr::createLNot(Res, getContext(), FirstTokenLoc);
1400 return false;
1401 case AsmToken::Dollar:
1402 case AsmToken::At:
1403 case AsmToken::Identifier: {
1404 StringRef Identifier;
1405 if (parseIdentifier(Identifier)) {
1406 // We may have failed but $ may be a valid token.
1407 if (getTok().is(AsmToken::Dollar)) {
1408 if (Lexer.getMAI().getDollarIsPC()) {
1409 Lex();
1410 // This is a '$' reference, which references the current PC. Emit a
1411 // temporary label to the streamer and refer to it.
1412 MCSymbol *Sym = Ctx.createTempSymbol();
1413 Out.emitLabel(Sym);
1414 Res = MCSymbolRefExpr::create(Sym, getContext());
1415 EndLoc = FirstTokenLoc;
1416 return false;
1417 }
1418 return Error(FirstTokenLoc, "invalid token in expression");
1419 }
1420 }
1421 // Parse named bitwise negation.
1422 if (Identifier.equals_insensitive("not")) {
1423 if (parsePrimaryExpr(Res, EndLoc, nullptr))
1424 return true;
1425 Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
1426 return false;
1427 }
1428 // Parse directional local label references.
1429 if (Identifier.equals_insensitive("@b") ||
1430 Identifier.equals_insensitive("@f")) {
1431 bool Before = Identifier.equals_insensitive("@b");
1432 MCSymbol *Sym = getContext().getDirectionalLocalSymbol(0, Before);
1433 if (Before && Sym->isUndefined())
1434 return Error(FirstTokenLoc, "Expected @@ label before @B reference");
1435 Res = MCSymbolRefExpr::create(Sym, getContext());
1436 return false;
1437 }
1438
1439 EndLoc = SMLoc::getFromPointer(Identifier.end());
1440
1441 // This is a symbol reference.
1442 StringRef SymbolName = Identifier;
1443 if (SymbolName.empty())
1444 return Error(getLexer().getLoc(), "expected a symbol reference");
1445
1446 // Find the field offset if used.
1447 AsmFieldInfo Info;
1448 auto Split = SymbolName.split('.');
1449 if (Split.second.empty()) {
1450 } else {
1451 SymbolName = Split.first;
1452 if (lookUpField(SymbolName, Split.second, Info)) {
1453 std::pair<StringRef, StringRef> BaseMember = Split.second.split('.');
1454 StringRef Base = BaseMember.first, Member = BaseMember.second;
1455 lookUpField(Base, Member, Info);
1456 } else if (Structs.count(SymbolName.lower())) {
1457 // This is actually a reference to a field offset.
1458 Res = MCConstantExpr::create(Info.Offset, getContext());
1459 return false;
1460 }
1461 }
1462
1463 MCSymbol *Sym = getContext().getInlineAsmLabel(SymbolName);
1464 if (!Sym) {
1465 // If this is a built-in numeric value, treat it as a constant.
1466 auto BuiltinIt = BuiltinSymbolMap.find(SymbolName.lower());
1467 const BuiltinSymbol Symbol = (BuiltinIt == BuiltinSymbolMap.end())
1468 ? BI_NO_SYMBOL
1469 : BuiltinIt->getValue();
1470 if (Symbol != BI_NO_SYMBOL) {
1471 const MCExpr *Value = evaluateBuiltinValue(Symbol, FirstTokenLoc);
1472 if (Value) {
1473 Res = Value;
1474 return false;
1475 }
1476 }
1477
1478 // Variables use case-insensitive symbol names; if this is a variable, we
1479 // find the symbol using its canonical name.
1480 auto VarIt = Variables.find(SymbolName.lower());
1481 if (VarIt != Variables.end())
1482 SymbolName = VarIt->second.Name;
1483 Sym = getContext().parseSymbol(SymbolName);
1484 }
1485
1486 // If this is an absolute variable reference, substitute it now to preserve
1487 // semantics in the face of reassignment.
1488 if (Sym->isVariable()) {
1489 auto V = Sym->getVariableValue();
1490 bool DoInline = isa<MCConstantExpr>(V);
1491 if (auto TV = dyn_cast<MCTargetExpr>(V))
1492 DoInline = TV->inlineAssignedExpr();
1493 if (DoInline) {
1494 Res = Sym->getVariableValue();
1495 return false;
1496 }
1497 }
1498
1499 // Otherwise create a symbol ref.
1500 const MCExpr *SymRef =
1501 MCSymbolRefExpr::create(Sym, getContext(), FirstTokenLoc);
1502 if (Info.Offset) {
1504 MCBinaryExpr::Add, SymRef,
1506 } else {
1507 Res = SymRef;
1508 }
1509 if (TypeInfo) {
1510 if (Info.Type.Name.empty()) {
1511 auto TypeIt = KnownType.find(Identifier.lower());
1512 if (TypeIt != KnownType.end()) {
1513 Info.Type = TypeIt->second;
1514 }
1515 }
1516
1517 *TypeInfo = Info.Type;
1518 }
1519 return false;
1520 }
1521 case AsmToken::BigNum:
1522 return TokError("literal value out of range for directive");
1523 case AsmToken::Integer: {
1524 int64_t IntVal = getTok().getIntVal();
1525 Res = MCConstantExpr::create(IntVal, getContext());
1526 EndLoc = Lexer.getTok().getEndLoc();
1527 Lex(); // Eat token.
1528 return false;
1529 }
1530 case AsmToken::String: {
1531 // MASM strings (used as constants) are interpreted as big-endian base-256.
1532 SMLoc ValueLoc = getTok().getLoc();
1533 std::string Value;
1534 if (parseEscapedString(Value))
1535 return true;
1536 if (Value.size() > 8)
1537 return Error(ValueLoc, "literal value out of range");
1538 uint64_t IntValue = 0;
1539 for (const unsigned char CharVal : Value)
1540 IntValue = (IntValue << 8) | CharVal;
1541 Res = MCConstantExpr::create(IntValue, getContext());
1542 return false;
1543 }
1544 case AsmToken::Real: {
1545 APFloat RealVal(APFloat::IEEEdouble(), getTok().getString());
1546 uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue();
1547 Res = MCConstantExpr::create(IntVal, getContext());
1548 EndLoc = Lexer.getTok().getEndLoc();
1549 Lex(); // Eat token.
1550 return false;
1551 }
1552 case AsmToken::Dot: {
1553 // This is a '.' reference, which references the current PC. Emit a
1554 // temporary label to the streamer and refer to it.
1555 MCSymbol *Sym = Ctx.createTempSymbol();
1556 Out.emitLabel(Sym);
1557 Res = MCSymbolRefExpr::create(Sym, getContext());
1558 EndLoc = Lexer.getTok().getEndLoc();
1559 Lex(); // Eat identifier.
1560 return false;
1561 }
1562 case AsmToken::LParen:
1563 Lex(); // Eat the '('.
1564 return parseParenExpr(Res, EndLoc);
1565 case AsmToken::LBrac:
1566 if (!PlatformParser->HasBracketExpressions())
1567 return TokError("brackets expression not supported on this target");
1568 Lex(); // Eat the '['.
1569 return parseBracketExpr(Res, EndLoc);
1570 case AsmToken::Minus:
1571 Lex(); // Eat the operator.
1572 if (parsePrimaryExpr(Res, EndLoc, nullptr))
1573 return true;
1574 Res = MCUnaryExpr::createMinus(Res, getContext(), FirstTokenLoc);
1575 return false;
1576 case AsmToken::Plus:
1577 Lex(); // Eat the operator.
1578 if (parsePrimaryExpr(Res, EndLoc, nullptr))
1579 return true;
1580 Res = MCUnaryExpr::createPlus(Res, getContext(), FirstTokenLoc);
1581 return false;
1582 case AsmToken::Tilde:
1583 Lex(); // Eat the operator.
1584 if (parsePrimaryExpr(Res, EndLoc, nullptr))
1585 return true;
1586 Res = MCUnaryExpr::createNot(Res, getContext(), FirstTokenLoc);
1587 return false;
1588 }
1589}
1590
1591bool MasmParser::parseExpression(const MCExpr *&Res) {
1592 SMLoc EndLoc;
1593 return parseExpression(Res, EndLoc);
1594}
1595
1596/// This function checks if the next token is <string> type or arithmetic.
1597/// string that begin with character '<' must end with character '>'.
1598/// otherwise it is arithmetics.
1599/// If the function returns a 'true' value,
1600/// the End argument will be filled with the last location pointed to the '>'
1601/// character.
1602static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc) {
1603 assert((StrLoc.getPointer() != nullptr) &&
1604 "Argument to the function cannot be a NULL value");
1605 const char *CharPtr = StrLoc.getPointer();
1606 while ((*CharPtr != '>') && (*CharPtr != '\n') && (*CharPtr != '\r') &&
1607 (*CharPtr != '\0')) {
1608 if (*CharPtr == '!')
1609 CharPtr++;
1610 CharPtr++;
1611 }
1612 if (*CharPtr == '>') {
1613 EndLoc = StrLoc.getFromPointer(CharPtr + 1);
1614 return true;
1615 }
1616 return false;
1617}
1618
1619/// creating a string without the escape characters '!'.
1620static std::string angleBracketString(StringRef BracketContents) {
1621 std::string Res;
1622 for (size_t Pos = 0; Pos < BracketContents.size(); Pos++) {
1623 if (BracketContents[Pos] == '!')
1624 Pos++;
1625 Res += BracketContents[Pos];
1626 }
1627 return Res;
1628}
1629
1630/// Parse an expression and return it.
1631///
1632/// expr ::= expr &&,|| expr -> lowest.
1633/// expr ::= expr |,^,&,! expr
1634/// expr ::= expr ==,!=,<>,<,<=,>,>= expr
1635/// expr ::= expr <<,>> expr
1636/// expr ::= expr +,- expr
1637/// expr ::= expr *,/,% expr -> highest.
1638/// expr ::= primaryexpr
1639///
1640bool MasmParser::parseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1641 // Parse the expression.
1642 Res = nullptr;
1643 if (getTargetParser().parsePrimaryExpr(Res, EndLoc) ||
1644 parseBinOpRHS(1, Res, EndLoc))
1645 return true;
1646
1647 // Try to constant fold it up front, if possible. Do not exploit
1648 // assembler here.
1649 int64_t Value;
1650 if (Res->evaluateAsAbsolute(Value))
1652
1653 return false;
1654}
1655
1656bool MasmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
1657 Res = nullptr;
1658 return parseParenExpr(Res, EndLoc) || parseBinOpRHS(1, Res, EndLoc);
1659}
1660
1661bool MasmParser::parseAbsoluteExpression(int64_t &Res) {
1662 const MCExpr *Expr;
1663
1664 SMLoc StartLoc = Lexer.getLoc();
1665 if (parseExpression(Expr))
1666 return true;
1667
1668 if (!Expr->evaluateAsAbsolute(Res, getStreamer().getAssemblerPtr()))
1669 return Error(StartLoc, "expected absolute expression");
1670
1671 return false;
1672}
1673
1676 bool ShouldUseLogicalShr,
1677 bool EndExpressionAtGreater) {
1678 switch (K) {
1679 default:
1680 return 0; // not a binop.
1681
1682 // Lowest Precedence: &&, ||
1683 case AsmToken::AmpAmp:
1684 Kind = MCBinaryExpr::LAnd;
1685 return 2;
1686 case AsmToken::PipePipe:
1687 Kind = MCBinaryExpr::LOr;
1688 return 1;
1689
1690 // Low Precedence: ==, !=, <>, <, <=, >, >=
1692 Kind = MCBinaryExpr::EQ;
1693 return 3;
1696 Kind = MCBinaryExpr::NE;
1697 return 3;
1698 case AsmToken::Less:
1699 Kind = MCBinaryExpr::LT;
1700 return 3;
1702 Kind = MCBinaryExpr::LTE;
1703 return 3;
1704 case AsmToken::Greater:
1705 if (EndExpressionAtGreater)
1706 return 0;
1707 Kind = MCBinaryExpr::GT;
1708 return 3;
1710 Kind = MCBinaryExpr::GTE;
1711 return 3;
1712
1713 // Low Intermediate Precedence: +, -
1714 case AsmToken::Plus:
1715 Kind = MCBinaryExpr::Add;
1716 return 4;
1717 case AsmToken::Minus:
1718 Kind = MCBinaryExpr::Sub;
1719 return 4;
1720
1721 // High Intermediate Precedence: |, &, ^
1722 case AsmToken::Pipe:
1723 Kind = MCBinaryExpr::Or;
1724 return 5;
1725 case AsmToken::Caret:
1726 Kind = MCBinaryExpr::Xor;
1727 return 5;
1728 case AsmToken::Amp:
1729 Kind = MCBinaryExpr::And;
1730 return 5;
1731
1732 // Highest Precedence: *, /, %, <<, >>
1733 case AsmToken::Star:
1734 Kind = MCBinaryExpr::Mul;
1735 return 6;
1736 case AsmToken::Slash:
1737 Kind = MCBinaryExpr::Div;
1738 return 6;
1739 case AsmToken::Percent:
1740 Kind = MCBinaryExpr::Mod;
1741 return 6;
1742 case AsmToken::LessLess:
1743 Kind = MCBinaryExpr::Shl;
1744 return 6;
1746 if (EndExpressionAtGreater)
1747 return 0;
1748 Kind = ShouldUseLogicalShr ? MCBinaryExpr::LShr : MCBinaryExpr::AShr;
1749 return 6;
1750 }
1751}
1752
1753unsigned MasmParser::getBinOpPrecedence(AsmToken::TokenKind K,
1754 MCBinaryExpr::Opcode &Kind) {
1755 bool ShouldUseLogicalShr = MAI.shouldUseLogicalShr();
1756 return getGNUBinOpPrecedence(K, Kind, ShouldUseLogicalShr,
1757 AngleBracketDepth > 0);
1758}
1759
1760/// Parse all binary operators with precedence >= 'Precedence'.
1761/// Res contains the LHS of the expression on input.
1762bool MasmParser::parseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
1763 SMLoc &EndLoc) {
1764 SMLoc StartLoc = Lexer.getLoc();
1765 while (true) {
1766 AsmToken::TokenKind TokKind = Lexer.getKind();
1767 if (Lexer.getKind() == AsmToken::Identifier) {
1768 TokKind = StringSwitch<AsmToken::TokenKind>(Lexer.getTok().getString())
1769 .CaseLower("and", AsmToken::Amp)
1770 .CaseLower("not", AsmToken::Exclaim)
1771 .CaseLower("or", AsmToken::Pipe)
1772 .CaseLower("xor", AsmToken::Caret)
1773 .CaseLower("shl", AsmToken::LessLess)
1774 .CaseLower("shr", AsmToken::GreaterGreater)
1775 .CaseLower("eq", AsmToken::EqualEqual)
1776 .CaseLower("ne", AsmToken::ExclaimEqual)
1777 .CaseLower("lt", AsmToken::Less)
1778 .CaseLower("le", AsmToken::LessEqual)
1779 .CaseLower("gt", AsmToken::Greater)
1780 .CaseLower("ge", AsmToken::GreaterEqual)
1781 .Default(TokKind);
1782 }
1784 unsigned TokPrec = getBinOpPrecedence(TokKind, Kind);
1785
1786 // If the next token is lower precedence than we are allowed to eat, return
1787 // successfully with what we ate already.
1788 if (TokPrec < Precedence)
1789 return false;
1790
1791 Lex();
1792
1793 // Eat the next primary expression.
1794 const MCExpr *RHS;
1795 if (getTargetParser().parsePrimaryExpr(RHS, EndLoc))
1796 return true;
1797
1798 // If BinOp binds less tightly with RHS than the operator after RHS, let
1799 // the pending operator take RHS as its LHS.
1801 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
1802 if (TokPrec < NextTokPrec && parseBinOpRHS(TokPrec + 1, RHS, EndLoc))
1803 return true;
1804
1805 // Merge LHS and RHS according to operator.
1806 Res = MCBinaryExpr::create(Kind, Res, RHS, getContext(), StartLoc);
1807 }
1808}
1809
1810/// ParseStatement:
1811/// ::= % statement
1812/// ::= EndOfStatement
1813/// ::= Label* Directive ...Operands... EndOfStatement
1814/// ::= Label* Identifier OperandList* EndOfStatement
1815bool MasmParser::parseStatement(ParseStatementInfo &Info,
1816 MCAsmParserSemaCallback *SI) {
1817 assert(!hasPendingError() && "parseStatement started with pending error");
1818 // Eat initial spaces and comments.
1819 while (Lexer.is(AsmToken::Space))
1820 Lex();
1821 if (Lexer.is(AsmToken::EndOfStatement)) {
1822 // If this is a line comment we can drop it safely.
1823 if (getTok().getString().empty() || getTok().getString().front() == '\r' ||
1824 getTok().getString().front() == '\n')
1825 Out.addBlankLine();
1826 Lex();
1827 return false;
1828 }
1829
1830 // If preceded by an expansion operator, first expand all text macros and
1831 // macro functions.
1832 if (getTok().is(AsmToken::Percent)) {
1833 SMLoc ExpansionLoc = getTok().getLoc();
1834 if (parseToken(AsmToken::Percent) || expandStatement(ExpansionLoc))
1835 return true;
1836 }
1837
1838 // Statements always start with an identifier, unless we're dealing with a
1839 // processor directive (.386, .686, etc.) that lexes as a real.
1840 AsmToken ID = getTok();
1841 SMLoc IDLoc = ID.getLoc();
1842 StringRef IDVal;
1843 if (Lexer.is(AsmToken::HashDirective))
1844 return parseCppHashLineFilenameComment(IDLoc);
1845 if (Lexer.is(AsmToken::Dot)) {
1846 // Treat '.' as a valid identifier in this context.
1847 Lex();
1848 IDVal = ".";
1849 } else if (Lexer.is(AsmToken::Real)) {
1850 // Treat ".<number>" as a valid identifier in this context.
1851 IDVal = getTok().getString();
1852 Lex(); // always eat a token
1853 if (!IDVal.starts_with("."))
1854 return Error(IDLoc, "unexpected token at start of statement");
1855 } else if (parseIdentifier(IDVal, StartOfStatement)) {
1856 if (!TheCondState.Ignore) {
1857 Lex(); // always eat a token
1858 return Error(IDLoc, "unexpected token at start of statement");
1859 }
1860 IDVal = "";
1861 }
1862
1863 // Handle conditional assembly here before checking for skipping. We
1864 // have to do this so that .endif isn't skipped in a ".if 0" block for
1865 // example.
1867 DirectiveKindMap.find(IDVal.lower());
1868 DirectiveKind DirKind = (DirKindIt == DirectiveKindMap.end())
1869 ? DK_NO_DIRECTIVE
1870 : DirKindIt->getValue();
1871 switch (DirKind) {
1872 default:
1873 break;
1874 case DK_IF:
1875 case DK_IFE:
1876 return parseDirectiveIf(IDLoc, DirKind);
1877 case DK_IFB:
1878 return parseDirectiveIfb(IDLoc, true);
1879 case DK_IFNB:
1880 return parseDirectiveIfb(IDLoc, false);
1881 case DK_IFDEF:
1882 return parseDirectiveIfdef(IDLoc, true);
1883 case DK_IFNDEF:
1884 return parseDirectiveIfdef(IDLoc, false);
1885 case DK_IFDIF:
1886 return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/false,
1887 /*CaseInsensitive=*/false);
1888 case DK_IFDIFI:
1889 return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/false,
1890 /*CaseInsensitive=*/true);
1891 case DK_IFIDN:
1892 return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/true,
1893 /*CaseInsensitive=*/false);
1894 case DK_IFIDNI:
1895 return parseDirectiveIfidn(IDLoc, /*ExpectEqual=*/true,
1896 /*CaseInsensitive=*/true);
1897 case DK_ELSEIF:
1898 case DK_ELSEIFE:
1899 return parseDirectiveElseIf(IDLoc, DirKind);
1900 case DK_ELSEIFB:
1901 return parseDirectiveElseIfb(IDLoc, true);
1902 case DK_ELSEIFNB:
1903 return parseDirectiveElseIfb(IDLoc, false);
1904 case DK_ELSEIFDEF:
1905 return parseDirectiveElseIfdef(IDLoc, true);
1906 case DK_ELSEIFNDEF:
1907 return parseDirectiveElseIfdef(IDLoc, false);
1908 case DK_ELSEIFDIF:
1909 return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/false,
1910 /*CaseInsensitive=*/false);
1911 case DK_ELSEIFDIFI:
1912 return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/false,
1913 /*CaseInsensitive=*/true);
1914 case DK_ELSEIFIDN:
1915 return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/true,
1916 /*CaseInsensitive=*/false);
1917 case DK_ELSEIFIDNI:
1918 return parseDirectiveElseIfidn(IDLoc, /*ExpectEqual=*/true,
1919 /*CaseInsensitive=*/true);
1920 case DK_ELSE:
1921 return parseDirectiveElse(IDLoc);
1922 case DK_ENDIF:
1923 return parseDirectiveEndIf(IDLoc);
1924 }
1925
1926 // Ignore the statement if in the middle of inactive conditional
1927 // (e.g. ".if 0").
1928 if (TheCondState.Ignore) {
1929 eatToEndOfStatement();
1930 return false;
1931 }
1932
1933 // FIXME: Recurse on local labels?
1934
1935 // Check for a label.
1936 // ::= identifier ':'
1937 // ::= number ':'
1938 if (Lexer.is(AsmToken::Colon) && getTargetParser().isLabel(ID)) {
1939 if (checkForValidSection())
1940 return true;
1941
1942 // identifier ':' -> Label.
1943 Lex();
1944
1945 // Diagnose attempt to use '.' as a label.
1946 if (IDVal == ".")
1947 return Error(IDLoc, "invalid use of pseudo-symbol '.' as a label");
1948
1949 // Diagnose attempt to use a variable as a label.
1950 //
1951 // FIXME: Diagnostics. Note the location of the definition as a label.
1952 // FIXME: This doesn't diagnose assignment to a symbol which has been
1953 // implicitly marked as external.
1954 MCSymbol *Sym;
1955 if (ParsingMSInlineAsm && SI) {
1956 StringRef RewrittenLabel =
1957 SI->LookupInlineAsmLabel(IDVal, getSourceManager(), IDLoc, true);
1958 assert(!RewrittenLabel.empty() &&
1959 "We should have an internal name here.");
1960 Info.AsmRewrites->emplace_back(AOK_Label, IDLoc, IDVal.size(),
1961 RewrittenLabel);
1962 IDVal = RewrittenLabel;
1963 }
1964 // Handle directional local labels
1965 if (IDVal == "@@") {
1966 Sym = Ctx.createDirectionalLocalSymbol(0);
1967 } else {
1968 Sym = getContext().parseSymbol(IDVal);
1969 }
1970
1971 // End of Labels should be treated as end of line for lexing
1972 // purposes but that information is not available to the Lexer who
1973 // does not understand Labels. This may cause us to see a Hash
1974 // here instead of a preprocessor line comment.
1975 if (getTok().is(AsmToken::Hash)) {
1976 std::string CommentStr = parseStringTo(AsmToken::EndOfStatement);
1977 Lexer.Lex();
1978 Lexer.UnLex(AsmToken(AsmToken::EndOfStatement, CommentStr));
1979 }
1980
1981 // Consume any end of statement token, if present, to avoid spurious
1982 // addBlankLine calls().
1983 if (getTok().is(AsmToken::EndOfStatement)) {
1984 Lex();
1985 }
1986
1987 // Emit the label.
1988 if (!getTargetParser().isParsingMSInlineAsm())
1989 Out.emitLabel(Sym, IDLoc);
1990 return false;
1991 }
1992
1993 // If macros are enabled, check to see if this is a macro instantiation.
1994 if (const MCAsmMacro *M = getContext().lookupMacro(IDVal.lower())) {
1995 AsmToken::TokenKind ArgumentEndTok = parseOptionalToken(AsmToken::LParen)
1998 return handleMacroEntry(M, IDLoc, ArgumentEndTok);
1999 }
2000
2001 // Otherwise, we have a normal instruction or directive.
2002
2003 if (DirKind != DK_NO_DIRECTIVE) {
2004 // There are several entities interested in parsing directives:
2005 //
2006 // 1. Asm parser extensions. For example, platform-specific parsers
2007 // (like the ELF parser) register themselves as extensions.
2008 // 2. The target-specific assembly parser. Some directives are target
2009 // specific or may potentially behave differently on certain targets.
2010 // 3. The generic directive parser implemented by this class. These are
2011 // all the directives that behave in a target and platform independent
2012 // manner, or at least have a default behavior that's shared between
2013 // all targets and platforms.
2014
2015 // Special-case handling of structure-end directives at higher priority,
2016 // since ENDS is overloaded as a segment-end directive.
2017 if (IDVal.equals_insensitive("ends") && StructInProgress.size() > 1 &&
2018 getTok().is(AsmToken::EndOfStatement)) {
2019 return parseDirectiveNestedEnds();
2020 }
2021
2022 // First, check the extension directive map to see if any extension has
2023 // registered itself to parse this directive.
2024 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
2025 ExtensionDirectiveMap.lookup(IDVal.lower());
2026 if (Handler.first)
2027 return (*Handler.second)(Handler.first, IDVal, IDLoc);
2028
2029 // Next, let the target-specific assembly parser try.
2030 if (ID.isNot(AsmToken::Identifier))
2031 return false;
2032
2033 ParseStatus TPDirectiveReturn = getTargetParser().parseDirective(ID);
2034 assert(TPDirectiveReturn.isFailure() == hasPendingError() &&
2035 "Should only return Failure iff there was an error");
2036 if (TPDirectiveReturn.isFailure())
2037 return true;
2038 if (TPDirectiveReturn.isSuccess())
2039 return false;
2040
2041 // Finally, if no one else is interested in this directive, it must be
2042 // generic and familiar to this class.
2043 switch (DirKind) {
2044 default:
2045 break;
2046 case DK_ASCII:
2047 return parseDirectiveAscii(IDVal, false);
2048 case DK_ASCIZ:
2049 case DK_STRING:
2050 return parseDirectiveAscii(IDVal, true);
2051 case DK_BYTE:
2052 case DK_SBYTE:
2053 case DK_DB:
2054 return parseDirectiveValue(IDVal, 1);
2055 case DK_WORD:
2056 case DK_SWORD:
2057 case DK_DW:
2058 return parseDirectiveValue(IDVal, 2);
2059 case DK_DWORD:
2060 case DK_SDWORD:
2061 case DK_DD:
2062 return parseDirectiveValue(IDVal, 4);
2063 case DK_FWORD:
2064 case DK_DF:
2065 return parseDirectiveValue(IDVal, 6);
2066 case DK_QWORD:
2067 case DK_SQWORD:
2068 case DK_DQ:
2069 return parseDirectiveValue(IDVal, 8);
2070 case DK_REAL4:
2071 return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle(), 4);
2072 case DK_REAL8:
2073 return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble(), 8);
2074 case DK_REAL10:
2075 return parseDirectiveRealValue(IDVal, APFloat::x87DoubleExtended(), 10);
2076 case DK_STRUCT:
2077 case DK_UNION:
2078 return parseDirectiveNestedStruct(IDVal, DirKind);
2079 case DK_ENDS:
2080 return parseDirectiveNestedEnds();
2081 case DK_ALIGN:
2082 return parseDirectiveAlign();
2083 case DK_EVEN:
2084 return parseDirectiveEven();
2085 case DK_ORG:
2086 return parseDirectiveOrg();
2087 case DK_EXTERN:
2088 return parseDirectiveExtern();
2089 case DK_PUBLIC:
2090 return parseDirectiveSymbolAttribute(MCSA_Global);
2091 case DK_COMM:
2092 return parseDirectiveComm(/*IsLocal=*/false);
2093 case DK_COMMENT:
2094 return parseDirectiveComment(IDLoc);
2095 case DK_INCLUDE:
2096 return parseDirectiveInclude();
2097 case DK_REPEAT:
2098 return parseDirectiveRepeat(IDLoc, IDVal);
2099 case DK_WHILE:
2100 return parseDirectiveWhile(IDLoc);
2101 case DK_FOR:
2102 return parseDirectiveFor(IDLoc, IDVal);
2103 case DK_FORC:
2104 return parseDirectiveForc(IDLoc, IDVal);
2105 case DK_EXITM:
2106 Info.ExitValue = "";
2107 return parseDirectiveExitMacro(IDLoc, IDVal, *Info.ExitValue);
2108 case DK_ENDM:
2109 Info.ExitValue = "";
2110 return parseDirectiveEndMacro(IDVal);
2111 case DK_PURGE:
2112 return parseDirectivePurgeMacro(IDLoc);
2113 case DK_END:
2114 return parseDirectiveEnd(IDLoc);
2115 case DK_ERR:
2116 return parseDirectiveError(IDLoc);
2117 case DK_ERRB:
2118 return parseDirectiveErrorIfb(IDLoc, true);
2119 case DK_ERRNB:
2120 return parseDirectiveErrorIfb(IDLoc, false);
2121 case DK_ERRDEF:
2122 return parseDirectiveErrorIfdef(IDLoc, true);
2123 case DK_ERRNDEF:
2124 return parseDirectiveErrorIfdef(IDLoc, false);
2125 case DK_ERRDIF:
2126 return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/false,
2127 /*CaseInsensitive=*/false);
2128 case DK_ERRDIFI:
2129 return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/false,
2130 /*CaseInsensitive=*/true);
2131 case DK_ERRIDN:
2132 return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/true,
2133 /*CaseInsensitive=*/false);
2134 case DK_ERRIDNI:
2135 return parseDirectiveErrorIfidn(IDLoc, /*ExpectEqual=*/true,
2136 /*CaseInsensitive=*/true);
2137 case DK_ERRE:
2138 return parseDirectiveErrorIfe(IDLoc, true);
2139 case DK_ERRNZ:
2140 return parseDirectiveErrorIfe(IDLoc, false);
2141 case DK_RADIX:
2142 return parseDirectiveRadix(IDLoc);
2143 case DK_ECHO:
2144 return parseDirectiveEcho(IDLoc);
2145 }
2146
2147 return Error(IDLoc, "unknown directive");
2148 }
2149
2150 // We also check if this is allocating memory with user-defined type.
2151 auto IDIt = Structs.find(IDVal.lower());
2152 if (IDIt != Structs.end())
2153 return parseDirectiveStructValue(/*Structure=*/IDIt->getValue(), IDVal,
2154 IDLoc);
2155
2156 // Non-conditional Microsoft directives sometimes follow their first argument.
2157 const AsmToken nextTok = getTok();
2158 const StringRef nextVal = nextTok.getString();
2159 const SMLoc nextLoc = nextTok.getLoc();
2160
2161 const AsmToken afterNextTok = peekTok();
2162
2163 // There are several entities interested in parsing infix directives:
2164 //
2165 // 1. Asm parser extensions. For example, platform-specific parsers
2166 // (like the ELF parser) register themselves as extensions.
2167 // 2. The generic directive parser implemented by this class. These are
2168 // all the directives that behave in a target and platform independent
2169 // manner, or at least have a default behavior that's shared between
2170 // all targets and platforms.
2171
2172 getTargetParser().flushPendingInstructions(getStreamer());
2173
2174 // Special-case handling of structure-end directives at higher priority, since
2175 // ENDS is overloaded as a segment-end directive.
2176 if (nextVal.equals_insensitive("ends") && StructInProgress.size() == 1) {
2177 Lex();
2178 return parseDirectiveEnds(IDVal, IDLoc);
2179 }
2180
2181 // First, check the extension directive map to see if any extension has
2182 // registered itself to parse this directive.
2183 std::pair<MCAsmParserExtension *, DirectiveHandler> Handler =
2184 ExtensionDirectiveMap.lookup(nextVal.lower());
2185 if (Handler.first) {
2186 Lex();
2187 Lexer.UnLex(ID);
2188 return (*Handler.second)(Handler.first, nextVal, nextLoc);
2189 }
2190
2191 // If no one else is interested in this directive, it must be
2192 // generic and familiar to this class.
2193 DirKindIt = DirectiveKindMap.find(nextVal.lower());
2194 DirKind = (DirKindIt == DirectiveKindMap.end())
2195 ? DK_NO_DIRECTIVE
2196 : DirKindIt->getValue();
2197 switch (DirKind) {
2198 default:
2199 break;
2200 case DK_ASSIGN:
2201 case DK_EQU:
2202 case DK_TEXTEQU:
2203 Lex();
2204 return parseDirectiveEquate(nextVal, IDVal, DirKind, IDLoc);
2205 case DK_BYTE:
2206 if (afterNextTok.is(AsmToken::Identifier) &&
2207 afterNextTok.getString().equals_insensitive("ptr")) {
2208 // Size directive; part of an instruction.
2209 break;
2210 }
2211 [[fallthrough]];
2212 case DK_SBYTE:
2213 case DK_DB:
2214 Lex();
2215 return parseDirectiveNamedValue(nextVal, 1, IDVal, IDLoc);
2216 case DK_WORD:
2217 if (afterNextTok.is(AsmToken::Identifier) &&
2218 afterNextTok.getString().equals_insensitive("ptr")) {
2219 // Size directive; part of an instruction.
2220 break;
2221 }
2222 [[fallthrough]];
2223 case DK_SWORD:
2224 case DK_DW:
2225 Lex();
2226 return parseDirectiveNamedValue(nextVal, 2, IDVal, IDLoc);
2227 case DK_DWORD:
2228 if (afterNextTok.is(AsmToken::Identifier) &&
2229 afterNextTok.getString().equals_insensitive("ptr")) {
2230 // Size directive; part of an instruction.
2231 break;
2232 }
2233 [[fallthrough]];
2234 case DK_SDWORD:
2235 case DK_DD:
2236 Lex();
2237 return parseDirectiveNamedValue(nextVal, 4, IDVal, IDLoc);
2238 case DK_FWORD:
2239 if (afterNextTok.is(AsmToken::Identifier) &&
2240 afterNextTok.getString().equals_insensitive("ptr")) {
2241 // Size directive; part of an instruction.
2242 break;
2243 }
2244 [[fallthrough]];
2245 case DK_DF:
2246 Lex();
2247 return parseDirectiveNamedValue(nextVal, 6, IDVal, IDLoc);
2248 case DK_QWORD:
2249 if (afterNextTok.is(AsmToken::Identifier) &&
2250 afterNextTok.getString().equals_insensitive("ptr")) {
2251 // Size directive; part of an instruction.
2252 break;
2253 }
2254 [[fallthrough]];
2255 case DK_SQWORD:
2256 case DK_DQ:
2257 Lex();
2258 return parseDirectiveNamedValue(nextVal, 8, IDVal, IDLoc);
2259 case DK_REAL4:
2260 Lex();
2261 return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEsingle(), 4,
2262 IDVal, IDLoc);
2263 case DK_REAL8:
2264 Lex();
2265 return parseDirectiveNamedRealValue(nextVal, APFloat::IEEEdouble(), 8,
2266 IDVal, IDLoc);
2267 case DK_REAL10:
2268 Lex();
2269 return parseDirectiveNamedRealValue(nextVal, APFloat::x87DoubleExtended(),
2270 10, IDVal, IDLoc);
2271 case DK_STRUCT:
2272 case DK_UNION:
2273 Lex();
2274 return parseDirectiveStruct(nextVal, DirKind, IDVal, IDLoc);
2275 case DK_ENDS:
2276 Lex();
2277 return parseDirectiveEnds(IDVal, IDLoc);
2278 case DK_MACRO:
2279 Lex();
2280 return parseDirectiveMacro(IDVal, IDLoc);
2281 }
2282
2283 // Finally, we check if this is allocating a variable with user-defined type.
2284 auto NextIt = Structs.find(nextVal.lower());
2285 if (NextIt != Structs.end()) {
2286 Lex();
2287 return parseDirectiveNamedStructValue(/*Structure=*/NextIt->getValue(),
2288 nextVal, nextLoc, IDVal);
2289 }
2290
2291 // __asm _emit or __asm __emit
2292 if (ParsingMSInlineAsm && (IDVal == "_emit" || IDVal == "__emit" ||
2293 IDVal == "_EMIT" || IDVal == "__EMIT"))
2294 return parseDirectiveMSEmit(IDLoc, Info, IDVal.size());
2295
2296 // __asm align
2297 if (ParsingMSInlineAsm && (IDVal == "align" || IDVal == "ALIGN"))
2298 return parseDirectiveMSAlign(IDLoc, Info);
2299
2300 if (ParsingMSInlineAsm && (IDVal == "even" || IDVal == "EVEN"))
2301 Info.AsmRewrites->emplace_back(AOK_EVEN, IDLoc, 4);
2302 if (checkForValidSection())
2303 return true;
2304
2305 // Canonicalize the opcode to lower case.
2306 std::string OpcodeStr = IDVal.lower();
2307 ParseInstructionInfo IInfo(Info.AsmRewrites);
2308 bool ParseHadError = getTargetParser().parseInstruction(IInfo, OpcodeStr, ID,
2309 Info.ParsedOperands);
2310 Info.ParseError = ParseHadError;
2311
2312 // Dump the parsed representation, if requested.
2313 if (getShowParsedOperands()) {
2314 SmallString<256> Str;
2315 raw_svector_ostream OS(Str);
2316 OS << "parsed instruction: [";
2317 for (unsigned i = 0; i != Info.ParsedOperands.size(); ++i) {
2318 if (i != 0)
2319 OS << ", ";
2320 Info.ParsedOperands[i]->print(OS, MAI);
2321 }
2322 OS << "]";
2323
2324 printMessage(IDLoc, SourceMgr::DK_Note, OS.str());
2325 }
2326
2327 // Fail even if ParseInstruction erroneously returns false.
2328 if (hasPendingError() || ParseHadError)
2329 return true;
2330
2331 // If parsing succeeded, match the instruction.
2332 if (!ParseHadError) {
2333 uint64_t ErrorInfo;
2334 if (getTargetParser().matchAndEmitInstruction(
2335 IDLoc, Info.Opcode, Info.ParsedOperands, Out, ErrorInfo,
2336 getTargetParser().isParsingMSInlineAsm()))
2337 return true;
2338 }
2339 return false;
2340}
2341
2342// Parse and erase curly braces marking block start/end.
2343bool MasmParser::parseCurlyBlockScope(
2344 SmallVectorImpl<AsmRewrite> &AsmStrRewrites) {
2345 // Identify curly brace marking block start/end.
2346 if (Lexer.isNot(AsmToken::LCurly) && Lexer.isNot(AsmToken::RCurly))
2347 return false;
2348
2349 SMLoc StartLoc = Lexer.getLoc();
2350 Lex(); // Eat the brace.
2351 if (Lexer.is(AsmToken::EndOfStatement))
2352 Lex(); // Eat EndOfStatement following the brace.
2353
2354 // Erase the block start/end brace from the output asm string.
2355 AsmStrRewrites.emplace_back(AOK_Skip, StartLoc, Lexer.getLoc().getPointer() -
2356 StartLoc.getPointer());
2357 return true;
2358}
2359
2360/// parseCppHashLineFilenameComment as this:
2361/// ::= # number "filename"
2362bool MasmParser::parseCppHashLineFilenameComment(SMLoc L) {
2363 Lex(); // Eat the hash token.
2364 // Lexer only ever emits HashDirective if it fully formed if it's
2365 // done the checking already so this is an internal error.
2366 assert(getTok().is(AsmToken::Integer) &&
2367 "Lexing Cpp line comment: Expected Integer");
2368 int64_t LineNumber = getTok().getIntVal();
2369 Lex();
2370 assert(getTok().is(AsmToken::String) &&
2371 "Lexing Cpp line comment: Expected String");
2372 StringRef Filename = getTok().getString();
2373 Lex();
2374
2375 // Get rid of the enclosing quotes.
2376 Filename = Filename.substr(1, Filename.size() - 2);
2377
2378 // Save the SMLoc, Filename and LineNumber for later use by diagnostics
2379 // and possibly DWARF file info.
2380 CppHashInfo.Loc = L;
2381 CppHashInfo.Filename = Filename;
2382 CppHashInfo.LineNumber = LineNumber;
2383 CppHashInfo.Buf = CurBuffer;
2384 if (FirstCppHashFilename.empty())
2385 FirstCppHashFilename = Filename;
2386 return false;
2387}
2388
2389/// will use the last parsed cpp hash line filename comment
2390/// for the Filename and LineNo if any in the diagnostic.
2391void MasmParser::DiagHandler(const SMDiagnostic &Diag, void *Context) {
2392 const MasmParser *Parser = static_cast<const MasmParser *>(Context);
2393 raw_ostream &OS = errs();
2394
2395 const SourceMgr &DiagSrcMgr = *Diag.getSourceMgr();
2396 SMLoc DiagLoc = Diag.getLoc();
2397 unsigned DiagBuf = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2398 unsigned CppHashBuf =
2399 Parser->SrcMgr.FindBufferContainingLoc(Parser->CppHashInfo.Loc);
2400
2401 // Like SourceMgr::printMessage() we need to print the include stack if any
2402 // before printing the message.
2403 unsigned DiagCurBuffer = DiagSrcMgr.FindBufferContainingLoc(DiagLoc);
2404 if (!Parser->SavedDiagHandler && DiagCurBuffer &&
2405 DiagCurBuffer != DiagSrcMgr.getMainFileID()) {
2406 SMLoc ParentIncludeLoc = DiagSrcMgr.getParentIncludeLoc(DiagCurBuffer);
2407 DiagSrcMgr.PrintIncludeStack(ParentIncludeLoc, OS);
2408 }
2409
2410 // If we have not parsed a cpp hash line filename comment or the source
2411 // manager changed or buffer changed (like in a nested include) then just
2412 // print the normal diagnostic using its Filename and LineNo.
2413 if (!Parser->CppHashInfo.LineNumber || &DiagSrcMgr != &Parser->SrcMgr ||
2414 DiagBuf != CppHashBuf) {
2415 if (Parser->SavedDiagHandler)
2416 Parser->SavedDiagHandler(Diag, Parser->SavedDiagContext);
2417 else
2418 Diag.print(nullptr, OS);
2419 return;
2420 }
2421
2422 // Use the CppHashFilename and calculate a line number based on the
2423 // CppHashInfo.Loc and CppHashInfo.LineNumber relative to this Diag's SMLoc
2424 // for the diagnostic.
2425 const std::string &Filename = std::string(Parser->CppHashInfo.Filename);
2426
2427 int DiagLocLineNo = DiagSrcMgr.FindLineNumber(DiagLoc, DiagBuf);
2428 int CppHashLocLineNo =
2429 Parser->SrcMgr.FindLineNumber(Parser->CppHashInfo.Loc, CppHashBuf);
2430 int LineNo =
2431 Parser->CppHashInfo.LineNumber - 1 + (DiagLocLineNo - CppHashLocLineNo);
2432
2433 SMDiagnostic NewDiag(*Diag.getSourceMgr(), Diag.getLoc(), Filename, LineNo,
2434 Diag.getColumnNo(), Diag.getKind(), Diag.getMessage(),
2435 Diag.getLineContents(), Diag.getRanges());
2436
2437 if (Parser->SavedDiagHandler)
2438 Parser->SavedDiagHandler(NewDiag, Parser->SavedDiagContext);
2439 else
2440 NewDiag.print(nullptr, OS);
2441}
2442
2443// This is similar to the IsIdentifierChar function in AsmLexer.cpp, but does
2444// not accept '.'.
2445static bool isMacroParameterChar(char C) {
2446 return isAlnum(C) || C == '_' || C == '$' || C == '@' || C == '?';
2447}
2448
2449bool MasmParser::expandMacro(raw_svector_ostream &OS, StringRef Body,
2452 const std::vector<std::string> &Locals, SMLoc L) {
2453 unsigned NParameters = Parameters.size();
2454 if (NParameters != A.size())
2455 return Error(L, "Wrong number of arguments");
2456 StringMap<std::string> LocalSymbols;
2457 std::string Name;
2458 Name.reserve(6);
2459 for (StringRef Local : Locals) {
2460 raw_string_ostream LocalName(Name);
2461 LocalName << "??"
2462 << format_hex_no_prefix(LocalCounter++, 4, /*Upper=*/true);
2463 LocalSymbols.insert({Local, Name});
2464 Name.clear();
2465 }
2466
2467 std::optional<char> CurrentQuote;
2468 while (!Body.empty()) {
2469 // Scan for the next substitution.
2470 std::size_t End = Body.size(), Pos = 0;
2471 std::size_t IdentifierPos = End;
2472 for (; Pos != End; ++Pos) {
2473 // Find the next possible macro parameter, including preceding a '&'
2474 // inside quotes.
2475 if (Body[Pos] == '&')
2476 break;
2477 if (isMacroParameterChar(Body[Pos])) {
2478 if (!CurrentQuote)
2479 break;
2480 if (IdentifierPos == End)
2481 IdentifierPos = Pos;
2482 } else {
2483 IdentifierPos = End;
2484 }
2485
2486 // Track quotation status
2487 if (!CurrentQuote) {
2488 if (Body[Pos] == '\'' || Body[Pos] == '"')
2489 CurrentQuote = Body[Pos];
2490 } else if (Body[Pos] == CurrentQuote) {
2491 if (Pos + 1 != End && Body[Pos + 1] == CurrentQuote) {
2492 // Escaped quote, and quotes aren't identifier chars; skip
2493 ++Pos;
2494 continue;
2495 } else {
2496 CurrentQuote.reset();
2497 }
2498 }
2499 }
2500 if (IdentifierPos != End) {
2501 // We've recognized an identifier before an apostrophe inside quotes;
2502 // check once to see if we can expand it.
2503 Pos = IdentifierPos;
2504 IdentifierPos = End;
2505 }
2506
2507 // Add the prefix.
2508 OS << Body.slice(0, Pos);
2509
2510 // Check if we reached the end.
2511 if (Pos == End)
2512 break;
2513
2514 unsigned I = Pos;
2515 bool InitialAmpersand = (Body[I] == '&');
2516 if (InitialAmpersand) {
2517 ++I;
2518 ++Pos;
2519 }
2520 while (I < End && isMacroParameterChar(Body[I]))
2521 ++I;
2522
2523 const char *Begin = Body.data() + Pos;
2524 StringRef Argument(Begin, I - Pos);
2525 const std::string ArgumentLower = Argument.lower();
2526 unsigned Index = 0;
2527
2528 for (; Index < NParameters; ++Index)
2529 if (Parameters[Index].Name.equals_insensitive(ArgumentLower))
2530 break;
2531
2532 if (Index == NParameters) {
2533 if (InitialAmpersand)
2534 OS << '&';
2535 auto it = LocalSymbols.find(ArgumentLower);
2536 if (it != LocalSymbols.end())
2537 OS << it->second;
2538 else
2539 OS << Argument;
2540 Pos = I;
2541 } else {
2542 for (const AsmToken &Token : A[Index]) {
2543 // In MASM, you can write '%expr'.
2544 // The prefix '%' evaluates the expression 'expr'
2545 // and uses the result as a string (e.g. replace %(1+2) with the
2546 // string "3").
2547 // Here, we identify the integer token which is the result of the
2548 // absolute expression evaluation and replace it with its string
2549 // representation.
2550 if (Token.getString().front() == '%' && Token.is(AsmToken::Integer))
2551 // Emit an integer value to the buffer.
2552 OS << Token.getIntVal();
2553 else
2554 OS << Token.getString();
2555 }
2556
2557 Pos += Argument.size();
2558 if (Pos < End && Body[Pos] == '&') {
2559 ++Pos;
2560 }
2561 }
2562 // Update the scan point.
2563 Body = Body.substr(Pos);
2564 }
2565
2566 return false;
2567}
2568
2569bool MasmParser::parseMacroArgument(const MCAsmMacroParameter *MP,
2570 MCAsmMacroArgument &MA,
2571 AsmToken::TokenKind EndTok) {
2572 if (MP && MP->Vararg) {
2573 if (Lexer.isNot(EndTok)) {
2574 SmallVector<StringRef, 1> Str = parseStringRefsTo(EndTok);
2575 for (StringRef S : Str) {
2576 MA.emplace_back(AsmToken::String, S);
2577 }
2578 }
2579 return false;
2580 }
2581
2582 SMLoc StrLoc = Lexer.getLoc(), EndLoc;
2583 if (Lexer.is(AsmToken::Less) && isAngleBracketString(StrLoc, EndLoc)) {
2584 const char *StrChar = StrLoc.getPointer() + 1;
2585 const char *EndChar = EndLoc.getPointer() - 1;
2586 jumpToLoc(EndLoc, CurBuffer, EndStatementAtEOFStack.back());
2587 /// Eat from '<' to '>'.
2588 Lex();
2589 MA.emplace_back(AsmToken::String, StringRef(StrChar, EndChar - StrChar));
2590 return false;
2591 }
2592
2593 unsigned ParenLevel = 0;
2594
2595 while (true) {
2596 if (Lexer.is(AsmToken::Eof) || Lexer.is(AsmToken::Equal))
2597 return TokError("unexpected token");
2598
2599 if (ParenLevel == 0 && Lexer.is(AsmToken::Comma))
2600 break;
2601
2602 // handleMacroEntry relies on not advancing the lexer here
2603 // to be able to fill in the remaining default parameter values
2604 if (Lexer.is(EndTok) && (EndTok != AsmToken::RParen || ParenLevel == 0))
2605 break;
2606
2607 // Adjust the current parentheses level.
2608 if (Lexer.is(AsmToken::LParen))
2609 ++ParenLevel;
2610 else if (Lexer.is(AsmToken::RParen) && ParenLevel)
2611 --ParenLevel;
2612
2613 // Append the token to the current argument list.
2614 MA.push_back(getTok());
2615 Lex();
2616 }
2617
2618 if (ParenLevel != 0)
2619 return TokError("unbalanced parentheses in argument");
2620
2621 if (MA.empty() && MP) {
2622 if (MP->Required) {
2623 return TokError("missing value for required parameter '" + MP->Name +
2624 "'");
2625 } else {
2626 MA = MP->Value;
2627 }
2628 }
2629 return false;
2630}
2631
2632// Parse the macro instantiation arguments.
2633bool MasmParser::parseMacroArguments(const MCAsmMacro *M,
2634 MCAsmMacroArguments &A,
2635 AsmToken::TokenKind EndTok) {
2636 const unsigned NParameters = M ? M->Parameters.size() : 0;
2637 bool NamedParametersFound = false;
2638 SmallVector<SMLoc, 4> FALocs;
2639
2640 A.resize(NParameters);
2641 FALocs.resize(NParameters);
2642
2643 // Parse two kinds of macro invocations:
2644 // - macros defined without any parameters accept an arbitrary number of them
2645 // - macros defined with parameters accept at most that many of them
2646 for (unsigned Parameter = 0; !NParameters || Parameter < NParameters;
2647 ++Parameter) {
2648 SMLoc IDLoc = Lexer.getLoc();
2649 MCAsmMacroParameter FA;
2650
2651 if (Lexer.is(AsmToken::Identifier) && peekTok().is(AsmToken::Equal)) {
2652 if (parseIdentifier(FA.Name))
2653 return Error(IDLoc, "invalid argument identifier for formal argument");
2654
2655 if (Lexer.isNot(AsmToken::Equal))
2656 return TokError("expected '=' after formal parameter identifier");
2657
2658 Lex();
2659
2660 NamedParametersFound = true;
2661 }
2662
2663 if (NamedParametersFound && FA.Name.empty())
2664 return Error(IDLoc, "cannot mix positional and keyword arguments");
2665
2666 unsigned PI = Parameter;
2667 if (!FA.Name.empty()) {
2668 assert(M && "expected macro to be defined");
2669 unsigned FAI = 0;
2670 for (FAI = 0; FAI < NParameters; ++FAI)
2671 if (M->Parameters[FAI].Name == FA.Name)
2672 break;
2673
2674 if (FAI >= NParameters) {
2675 return Error(IDLoc, "parameter named '" + FA.Name +
2676 "' does not exist for macro '" + M->Name + "'");
2677 }
2678 PI = FAI;
2679 }
2680 const MCAsmMacroParameter *MP = nullptr;
2681 if (M && PI < NParameters)
2682 MP = &M->Parameters[PI];
2683
2684 SMLoc StrLoc = Lexer.getLoc();
2685 SMLoc EndLoc;
2686 if (Lexer.is(AsmToken::Percent)) {
2687 const MCExpr *AbsoluteExp;
2688 int64_t Value;
2689 /// Eat '%'.
2690 Lex();
2691 if (parseExpression(AbsoluteExp, EndLoc))
2692 return false;
2693 if (!AbsoluteExp->evaluateAsAbsolute(Value,
2694 getStreamer().getAssemblerPtr()))
2695 return Error(StrLoc, "expected absolute expression");
2696 const char *StrChar = StrLoc.getPointer();
2697 const char *EndChar = EndLoc.getPointer();
2698 AsmToken newToken(AsmToken::Integer,
2699 StringRef(StrChar, EndChar - StrChar), Value);
2700 FA.Value.push_back(newToken);
2701 } else if (parseMacroArgument(MP, FA.Value, EndTok)) {
2702 if (M)
2703 return addErrorSuffix(" in '" + M->Name + "' macro");
2704 else
2705 return true;
2706 }
2707
2708 if (!FA.Value.empty()) {
2709 if (A.size() <= PI)
2710 A.resize(PI + 1);
2711 A[PI] = FA.Value;
2712
2713 if (FALocs.size() <= PI)
2714 FALocs.resize(PI + 1);
2715
2716 FALocs[PI] = Lexer.getLoc();
2717 }
2718
2719 // At the end of the statement, fill in remaining arguments that have
2720 // default values. If there aren't any, then the next argument is
2721 // required but missing
2722 if (Lexer.is(EndTok)) {
2723 bool Failure = false;
2724 for (unsigned FAI = 0; FAI < NParameters; ++FAI) {
2725 if (A[FAI].empty()) {
2726 if (M->Parameters[FAI].Required) {
2727 Error(FALocs[FAI].isValid() ? FALocs[FAI] : Lexer.getLoc(),
2728 "missing value for required parameter "
2729 "'" +
2730 M->Parameters[FAI].Name + "' in macro '" + M->Name + "'");
2731 Failure = true;
2732 }
2733
2734 if (!M->Parameters[FAI].Value.empty())
2735 A[FAI] = M->Parameters[FAI].Value;
2736 }
2737 }
2738 return Failure;
2739 }
2740
2741 if (Lexer.is(AsmToken::Comma))
2742 Lex();
2743 }
2744
2745 return TokError("too many positional arguments");
2746}
2747
2748bool MasmParser::handleMacroEntry(const MCAsmMacro *M, SMLoc NameLoc,
2749 AsmToken::TokenKind ArgumentEndTok) {
2750 // Arbitrarily limit macro nesting depth (default matches 'as'). We can
2751 // eliminate this, although we should protect against infinite loops.
2752 unsigned MaxNestingDepth = AsmMacroMaxNestingDepth;
2753 if (ActiveMacros.size() == MaxNestingDepth) {
2754 std::ostringstream MaxNestingDepthError;
2755 MaxNestingDepthError << "macros cannot be nested more than "
2756 << MaxNestingDepth << " levels deep."
2757 << " Use -asm-macro-max-nesting-depth to increase "
2758 "this limit.";
2759 return TokError(MaxNestingDepthError.str());
2760 }
2761
2762 MCAsmMacroArguments A;
2763 if (parseMacroArguments(M, A, ArgumentEndTok) || parseToken(ArgumentEndTok))
2764 return true;
2765
2766 // Macro instantiation is lexical, unfortunately. We construct a new buffer
2767 // to hold the macro body with substitutions.
2768 SmallString<256> Buf;
2769 StringRef Body = M->Body;
2770 raw_svector_ostream OS(Buf);
2771
2772 if (expandMacro(OS, Body, M->Parameters, A, M->Locals, getTok().getLoc()))
2773 return true;
2774
2775 // We include the endm in the buffer as our cue to exit the macro
2776 // instantiation.
2777 OS << "endm\n";
2778
2779 std::unique_ptr<MemoryBuffer> Instantiation =
2780 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
2781
2782 // Create the macro instantiation object and add to the current macro
2783 // instantiation stack.
2784 MacroInstantiation *MI = new MacroInstantiation{
2785 NameLoc, CurBuffer, getTok().getLoc(), TheCondStack.size()};
2786 ActiveMacros.push_back(MI);
2787
2788 ++NumOfMacroInstantiations;
2789
2790 // Jump to the macro instantiation and prime the lexer.
2791 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
2792 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
2793 EndStatementAtEOFStack.push_back(true);
2794 Lex();
2795
2796 return false;
2797}
2798
2799void MasmParser::handleMacroExit() {
2800 // Jump to the token we should return to, and consume it.
2801 EndStatementAtEOFStack.pop_back();
2802 jumpToLoc(ActiveMacros.back()->ExitLoc, ActiveMacros.back()->ExitBuffer,
2803 EndStatementAtEOFStack.back());
2804 Lex();
2805
2806 // Pop the instantiation entry.
2807 delete ActiveMacros.back();
2808 ActiveMacros.pop_back();
2809}
2810
2811bool MasmParser::handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc) {
2812 if (!M->IsFunction)
2813 return Error(NameLoc, "cannot invoke macro procedure as function");
2814
2815 if (parseToken(AsmToken::LParen, "invoking macro function '" + M->Name +
2816 "' requires arguments in parentheses") ||
2817 handleMacroEntry(M, NameLoc, AsmToken::RParen))
2818 return true;
2819
2820 // Parse all statements in the macro, retrieving the exit value when it ends.
2821 std::string ExitValue;
2822 SmallVector<AsmRewrite, 4> AsmStrRewrites;
2823 while (Lexer.isNot(AsmToken::Eof)) {
2824 ParseStatementInfo Info(&AsmStrRewrites);
2825 bool HasError = parseStatement(Info, nullptr);
2826
2827 if (!HasError && Info.ExitValue) {
2828 ExitValue = std::move(*Info.ExitValue);
2829 break;
2830 }
2831
2832 // If we have a Lexer Error we are on an Error Token. Load in Lexer Error
2833 // for printing ErrMsg via Lex() only if no (presumably better) parser error
2834 // exists.
2835 if (HasError && !hasPendingError() && Lexer.getTok().is(AsmToken::Error))
2836 Lex();
2837
2838 // parseStatement returned true so may need to emit an error.
2839 printPendingErrors();
2840
2841 // Skipping to the next line if needed.
2842 if (HasError && !getLexer().justConsumedEOL())
2843 eatToEndOfStatement();
2844 }
2845
2846 // Exit values may require lexing, unfortunately. We construct a new buffer to
2847 // hold the exit value.
2848 std::unique_ptr<MemoryBuffer> MacroValue =
2849 MemoryBuffer::getMemBufferCopy(ExitValue, "<macro-value>");
2850
2851 // Jump from this location to the instantiated exit value, and prime the
2852 // lexer.
2853 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(MacroValue), Lexer.getLoc());
2854 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer(), nullptr,
2855 /*EndStatementAtEOF=*/false);
2856 EndStatementAtEOFStack.push_back(false);
2857 Lex();
2858
2859 return false;
2860}
2861
2862/// parseIdentifier:
2863/// ::= identifier
2864/// ::= string
2865bool MasmParser::parseIdentifier(StringRef &Res,
2866 IdentifierPositionKind Position) {
2867 // The assembler has relaxed rules for accepting identifiers, in particular we
2868 // allow things like '.globl $foo' and '.def @feat.00', which would normally
2869 // be separate tokens. At this level, we have already lexed so we cannot
2870 // (currently) handle this as a context dependent token, instead we detect
2871 // adjacent tokens and return the combined identifier.
2872 if (Lexer.is(AsmToken::Dollar) || Lexer.is(AsmToken::At)) {
2873 SMLoc PrefixLoc = getLexer().getLoc();
2874
2875 // Consume the prefix character, and check for a following identifier.
2876
2877 AsmToken nextTok = peekTok(false);
2878
2879 if (nextTok.isNot(AsmToken::Identifier))
2880 return true;
2881
2882 // We have a '$' or '@' followed by an identifier, make sure they are adjacent.
2883 if (PrefixLoc.getPointer() + 1 != nextTok.getLoc().getPointer())
2884 return true;
2885
2886 // eat $ or @
2887 Lexer.Lex(); // Lexer's Lex guarantees consecutive token.
2888 // Construct the joined identifier and consume the token.
2889 Res =
2890 StringRef(PrefixLoc.getPointer(), getTok().getIdentifier().size() + 1);
2891 Lex(); // Parser Lex to maintain invariants.
2892 return false;
2893 }
2894
2895 if (Lexer.isNot(AsmToken::Identifier) && Lexer.isNot(AsmToken::String))
2896 return true;
2897
2898 Res = getTok().getIdentifier();
2899
2900 // Consume the identifier token - but if parsing certain directives, avoid
2901 // lexical expansion of the next token.
2902 ExpandKind ExpandNextToken = ExpandMacros;
2903 if (Position == StartOfStatement &&
2904 StringSwitch<bool>(Res)
2905 .CaseLower("echo", true)
2906 .CasesLower("ifdef", "ifndef", "elseifdef", "elseifndef", true)
2907 .Default(false)) {
2908 ExpandNextToken = DoNotExpandMacros;
2909 }
2910 Lex(ExpandNextToken);
2911
2912 return false;
2913}
2914
2915/// parseDirectiveEquate:
2916/// ::= name "=" expression
2917/// | name "equ" expression (not redefinable)
2918/// | name "equ" text-list
2919/// | name "textequ" text-list (redefinability unspecified)
2920bool MasmParser::parseDirectiveEquate(StringRef IDVal, StringRef Name,
2921 DirectiveKind DirKind, SMLoc NameLoc) {
2922 auto BuiltinIt = BuiltinSymbolMap.find(Name.lower());
2923 if (BuiltinIt != BuiltinSymbolMap.end())
2924 return Error(NameLoc, "cannot redefine a built-in symbol");
2925
2926 Variable &Var = Variables[Name.lower()];
2927 if (Var.Name.empty()) {
2928 Var.Name = Name;
2929 }
2930
2931 SMLoc StartLoc = Lexer.getLoc();
2932 if (DirKind == DK_EQU || DirKind == DK_TEXTEQU) {
2933 // "equ" and "textequ" both allow text expressions.
2934 std::string Value;
2935 std::string TextItem;
2936 if (!parseTextItem(TextItem)) {
2937 Value += TextItem;
2938
2939 // Accept a text-list, not just one text-item.
2940 auto parseItem = [&]() -> bool {
2941 if (parseTextItem(TextItem))
2942 return TokError("expected text item");
2943 Value += TextItem;
2944 return false;
2945 };
2946 if (parseOptionalToken(AsmToken::Comma) && parseMany(parseItem))
2947 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
2948
2949 if (!Var.IsText || Var.TextValue != Value) {
2950 switch (Var.Redefinable) {
2951 case Variable::NOT_REDEFINABLE:
2952 return Error(getTok().getLoc(), "invalid variable redefinition");
2953 case Variable::WARN_ON_REDEFINITION:
2954 if (Warning(NameLoc, "redefining '" + Name +
2955 "', already defined on the command line")) {
2956 return true;
2957 }
2958 break;
2959 default:
2960 break;
2961 }
2962 }
2963 Var.IsText = true;
2964 Var.TextValue = Value;
2965 Var.Redefinable = Variable::REDEFINABLE;
2966
2967 return false;
2968 }
2969 }
2970 if (DirKind == DK_TEXTEQU)
2971 return TokError("expected <text> in '" + Twine(IDVal) + "' directive");
2972
2973 // Parse as expression assignment.
2974 const MCExpr *Expr;
2975 SMLoc EndLoc;
2976 if (parseExpression(Expr, EndLoc))
2977 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
2978 StringRef ExprAsString = StringRef(
2979 StartLoc.getPointer(), EndLoc.getPointer() - StartLoc.getPointer());
2980
2981 int64_t Value;
2982 if (!Expr->evaluateAsAbsolute(Value, getStreamer().getAssemblerPtr())) {
2983 if (DirKind == DK_ASSIGN)
2984 return Error(
2985 StartLoc,
2986 "expected absolute expression; not all symbols have known values",
2987 {StartLoc, EndLoc});
2988
2989 // Not an absolute expression; define as a text replacement.
2990 if (!Var.IsText || Var.TextValue != ExprAsString) {
2991 switch (Var.Redefinable) {
2992 case Variable::NOT_REDEFINABLE:
2993 return Error(getTok().getLoc(), "invalid variable redefinition");
2994 case Variable::WARN_ON_REDEFINITION:
2995 if (Warning(NameLoc, "redefining '" + Name +
2996 "', already defined on the command line")) {
2997 return true;
2998 }
2999 break;
3000 default:
3001 break;
3002 }
3003 }
3004
3005 Var.IsText = true;
3006 Var.TextValue = ExprAsString.str();
3007 Var.Redefinable = Variable::REDEFINABLE;
3008
3009 return false;
3010 }
3011
3012 auto *Sym = static_cast<MCSymbolCOFF *>(getContext().parseSymbol(Var.Name));
3013 const MCConstantExpr *PrevValue =
3014 Sym->isVariable()
3016 : nullptr;
3017 if (Var.IsText || !PrevValue || PrevValue->getValue() != Value) {
3018 switch (Var.Redefinable) {
3019 case Variable::NOT_REDEFINABLE:
3020 return Error(getTok().getLoc(), "invalid variable redefinition");
3021 case Variable::WARN_ON_REDEFINITION:
3022 if (Warning(NameLoc, "redefining '" + Name +
3023 "', already defined on the command line")) {
3024 return true;
3025 }
3026 break;
3027 default:
3028 break;
3029 }
3030 }
3031
3032 Var.IsText = false;
3033 Var.TextValue.clear();
3034 Var.Redefinable = (DirKind == DK_ASSIGN) ? Variable::REDEFINABLE
3035 : Variable::NOT_REDEFINABLE;
3036
3037 Sym->setRedefinable(Var.Redefinable != Variable::NOT_REDEFINABLE);
3038 Sym->setVariableValue(Expr);
3039 Sym->setExternal(false);
3040
3041 return false;
3042}
3043
3044bool MasmParser::parseEscapedString(std::string &Data) {
3045 if (check(getTok().isNot(AsmToken::String), "expected string"))
3046 return true;
3047
3048 Data = "";
3049 char Quote = getTok().getString().front();
3050 StringRef Str = getTok().getStringContents();
3051 Data.reserve(Str.size());
3052 for (size_t i = 0, e = Str.size(); i != e; ++i) {
3053 Data.push_back(Str[i]);
3054 if (Str[i] == Quote) {
3055 // MASM treats doubled delimiting quotes as an escaped delimiting quote.
3056 // If we're escaping the string's trailing delimiter, we're definitely
3057 // missing a quotation mark.
3058 if (i + 1 == Str.size())
3059 return Error(getTok().getLoc(), "missing quotation mark in string");
3060 if (Str[i + 1] == Quote)
3061 ++i;
3062 }
3063 }
3064
3065 Lex();
3066 return false;
3067}
3068
3069bool MasmParser::parseAngleBracketString(std::string &Data) {
3070 SMLoc EndLoc, StartLoc = getTok().getLoc();
3071 if (isAngleBracketString(StartLoc, EndLoc)) {
3072 const char *StartChar = StartLoc.getPointer() + 1;
3073 const char *EndChar = EndLoc.getPointer() - 1;
3074 jumpToLoc(EndLoc, CurBuffer, EndStatementAtEOFStack.back());
3075 // Eat from '<' to '>'.
3076 Lex();
3077
3078 Data = angleBracketString(StringRef(StartChar, EndChar - StartChar));
3079 return false;
3080 }
3081 return true;
3082}
3083
3084/// textItem ::= textLiteral | textMacroID | % constExpr
3085bool MasmParser::parseTextItem(std::string &Data) {
3086 switch (getTok().getKind()) {
3087 default:
3088 return true;
3089 case AsmToken::Percent: {
3090 int64_t Res;
3091 if (parseToken(AsmToken::Percent) || parseAbsoluteExpression(Res))
3092 return true;
3093 Data = std::to_string(Res);
3094 return false;
3095 }
3096 case AsmToken::Less:
3098 case AsmToken::LessLess:
3100 return parseAngleBracketString(Data);
3101 case AsmToken::Identifier: {
3102 // This must be a text macro; we need to expand it accordingly.
3103 StringRef ID;
3104 SMLoc StartLoc = getTok().getLoc();
3105 if (parseIdentifier(ID))
3106 return true;
3107 Data = ID.str();
3108
3109 bool Expanded = false;
3110 while (true) {
3111 // Try to resolve as a built-in text macro
3112 auto BuiltinIt = BuiltinSymbolMap.find(ID.lower());
3113 if (BuiltinIt != BuiltinSymbolMap.end()) {
3114 std::optional<std::string> BuiltinText =
3115 evaluateBuiltinTextMacro(BuiltinIt->getValue(), StartLoc);
3116 if (!BuiltinText) {
3117 // Not a text macro; break without substituting
3118 break;
3119 }
3120 Data = std::move(*BuiltinText);
3121 ID = StringRef(Data);
3122 Expanded = true;
3123 continue;
3124 }
3125
3126 // Try to resolve as a built-in macro function
3127 auto BuiltinFuncIt = BuiltinFunctionMap.find(ID.lower());
3128 if (BuiltinFuncIt != BuiltinFunctionMap.end()) {
3129 Data.clear();
3130 if (evaluateBuiltinMacroFunction(BuiltinFuncIt->getValue(), ID, Data)) {
3131 return true;
3132 }
3133 ID = StringRef(Data);
3134 Expanded = true;
3135 continue;
3136 }
3137
3138 // Try to resolve as a variable text macro
3139 auto VarIt = Variables.find(ID.lower());
3140 if (VarIt != Variables.end()) {
3141 const Variable &Var = VarIt->getValue();
3142 if (!Var.IsText) {
3143 // Not a text macro; break without substituting
3144 break;
3145 }
3146 Data = Var.TextValue;
3147 ID = StringRef(Data);
3148 Expanded = true;
3149 continue;
3150 }
3151
3152 break;
3153 }
3154
3155 if (!Expanded) {
3156 // Not a text macro; not usable in TextItem context. Since we haven't used
3157 // the token, put it back for better error recovery.
3158 getLexer().UnLex(AsmToken(AsmToken::Identifier, ID));
3159 return true;
3160 }
3161 return false;
3162 }
3163 }
3164 llvm_unreachable("unhandled token kind");
3165}
3166
3167/// parseDirectiveAscii:
3168/// ::= ( .ascii | .asciz | .string ) [ "string" ( , "string" )* ]
3169bool MasmParser::parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated) {
3170 auto parseOp = [&]() -> bool {
3171 std::string Data;
3172 if (checkForValidSection() || parseEscapedString(Data))
3173 return true;
3174 getStreamer().emitBytes(Data);
3175 if (ZeroTerminated)
3176 getStreamer().emitBytes(StringRef("\0", 1));
3177 return false;
3178 };
3179
3180 if (parseMany(parseOp))
3181 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3182 return false;
3183}
3184
3185bool MasmParser::emitIntValue(const MCExpr *Value, unsigned Size) {
3186 // Special case constant expressions to match code generator.
3187 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {
3188 assert(Size <= 8 && "Invalid size");
3189 int64_t IntValue = MCE->getValue();
3190 if (!isUIntN(8 * Size, IntValue) && !isIntN(8 * Size, IntValue))
3191 return Error(MCE->getLoc(), "out of range literal value");
3192 getStreamer().emitIntValue(IntValue, Size);
3193 } else {
3194 const MCSymbolRefExpr *MSE = dyn_cast<MCSymbolRefExpr>(Value);
3195 if (MSE && MSE->getSymbol().getName() == "?") {
3196 // ? initializer; treat as 0.
3197 getStreamer().emitIntValue(0, Size);
3198 } else {
3199 getStreamer().emitValue(Value, Size, Value->getLoc());
3200 }
3201 }
3202 return false;
3203}
3204
3205bool MasmParser::parseScalarInitializer(unsigned Size,
3206 SmallVectorImpl<const MCExpr *> &Values,
3207 unsigned StringPadLength) {
3208 if (Size == 1 && getTok().is(AsmToken::String)) {
3209 std::string Value;
3210 if (parseEscapedString(Value))
3211 return true;
3212 // Treat each character as an initializer.
3213 for (const unsigned char CharVal : Value)
3214 Values.push_back(MCConstantExpr::create(CharVal, getContext()));
3215
3216 // Pad the string with spaces to the specified length.
3217 for (size_t i = Value.size(); i < StringPadLength; ++i)
3219 } else {
3220 const MCExpr *Value;
3221 if (parseExpression(Value))
3222 return true;
3223 if (getTok().is(AsmToken::Identifier) &&
3224 getTok().getString().equals_insensitive("dup")) {
3225 Lex(); // Eat 'dup'.
3226 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3227 if (!MCE)
3228 return Error(Value->getLoc(),
3229 "cannot repeat value a non-constant number of times");
3230 const int64_t Repetitions = MCE->getValue();
3231 if (Repetitions < 0)
3232 return Error(Value->getLoc(),
3233 "cannot repeat value a negative number of times");
3234
3235 SmallVector<const MCExpr *, 1> DuplicatedValues;
3236 if (parseToken(AsmToken::LParen,
3237 "parentheses required for 'dup' contents") ||
3238 parseScalarInstList(Size, DuplicatedValues) || parseRParen())
3239 return true;
3240
3241 for (int i = 0; i < Repetitions; ++i)
3242 Values.append(DuplicatedValues.begin(), DuplicatedValues.end());
3243 } else {
3244 Values.push_back(Value);
3245 }
3246 }
3247 return false;
3248}
3249
3250bool MasmParser::parseScalarInstList(unsigned Size,
3251 SmallVectorImpl<const MCExpr *> &Values,
3252 const AsmToken::TokenKind EndToken) {
3253 while (getTok().isNot(EndToken) &&
3254 (EndToken != AsmToken::Greater ||
3255 getTok().isNot(AsmToken::GreaterGreater))) {
3256 parseScalarInitializer(Size, Values);
3257
3258 // If we see a comma, continue, and allow line continuation.
3259 if (!parseOptionalToken(AsmToken::Comma))
3260 break;
3261 parseOptionalToken(AsmToken::EndOfStatement);
3262 }
3263 return false;
3264}
3265
3266bool MasmParser::emitIntegralValues(unsigned Size, unsigned *Count) {
3268 if (checkForValidSection() || parseScalarInstList(Size, Values))
3269 return true;
3270
3271 for (const auto *Value : Values) {
3272 emitIntValue(Value, Size);
3273 }
3274 if (Count)
3275 *Count = Values.size();
3276 return false;
3277}
3278
3279// Add a field to the current structure.
3280bool MasmParser::addIntegralField(StringRef Name, unsigned Size) {
3281 StructInfo &Struct = StructInProgress.back();
3282 FieldInfo &Field = Struct.addField(Name, FT_INTEGRAL, Size);
3283 IntFieldInfo &IntInfo = Field.Contents.IntInfo;
3284
3285 Field.Type = Size;
3286
3287 if (parseScalarInstList(Size, IntInfo.Values))
3288 return true;
3289
3290 Field.SizeOf = Field.Type * IntInfo.Values.size();
3291 Field.LengthOf = IntInfo.Values.size();
3292 const unsigned FieldEnd = Field.Offset + Field.SizeOf;
3293 if (!Struct.IsUnion) {
3294 Struct.NextOffset = FieldEnd;
3295 }
3296 Struct.Size = std::max(Struct.Size, FieldEnd);
3297 return false;
3298}
3299
3300/// parseDirectiveValue
3301/// ::= (byte | word | ... ) [ expression (, expression)* ]
3302bool MasmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
3303 if (StructInProgress.empty()) {
3304 // Initialize data value.
3305 if (emitIntegralValues(Size))
3306 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3307 } else if (addIntegralField("", Size)) {
3308 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3309 }
3310
3311 return false;
3312}
3313
3314/// parseDirectiveNamedValue
3315/// ::= name (byte | word | ... ) [ expression (, expression)* ]
3316bool MasmParser::parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
3317 StringRef Name, SMLoc NameLoc) {
3318 if (StructInProgress.empty()) {
3319 // Initialize named data value.
3320 MCSymbol *Sym = getContext().parseSymbol(Name);
3321 getStreamer().emitLabel(Sym);
3322 unsigned Count;
3323 if (emitIntegralValues(Size, &Count))
3324 return addErrorSuffix(" in '" + Twine(TypeName) + "' directive");
3325
3326 AsmTypeInfo Type;
3327 Type.Name = TypeName;
3328 Type.Size = Size * Count;
3329 Type.ElementSize = Size;
3330 Type.Length = Count;
3331 KnownType[Name.lower()] = Type;
3332 } else if (addIntegralField(Name, Size)) {
3333 return addErrorSuffix(" in '" + Twine(TypeName) + "' directive");
3334 }
3335
3336 return false;
3337}
3338
3339bool MasmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) {
3340 // We don't truly support arithmetic on floating point expressions, so we
3341 // have to manually parse unary prefixes.
3342 bool IsNeg = false;
3343 SMLoc SignLoc;
3344 if (getLexer().is(AsmToken::Minus)) {
3345 SignLoc = getLexer().getLoc();
3346 Lexer.Lex();
3347 IsNeg = true;
3348 } else if (getLexer().is(AsmToken::Plus)) {
3349 SignLoc = getLexer().getLoc();
3350 Lexer.Lex();
3351 }
3352
3353 if (Lexer.is(AsmToken::Error))
3354 return TokError(Lexer.getErr());
3355 if (Lexer.isNot(AsmToken::Integer) && Lexer.isNot(AsmToken::Real) &&
3356 Lexer.isNot(AsmToken::Identifier))
3357 return TokError("unexpected token in directive");
3358
3359 // Convert to an APFloat.
3360 APFloat Value(Semantics);
3361 StringRef IDVal = getTok().getString();
3362 if (getLexer().is(AsmToken::Identifier)) {
3363 if (IDVal.equals_insensitive("infinity") || IDVal.equals_insensitive("inf"))
3364 Value = APFloat::getInf(Semantics);
3365 else if (IDVal.equals_insensitive("nan"))
3366 Value = APFloat::getNaN(Semantics, false, ~0);
3367 else if (IDVal.equals_insensitive("?"))
3368 Value = APFloat::getZero(Semantics);
3369 else
3370 return TokError("invalid floating point literal");
3371 } else if (IDVal.consume_back("r") || IDVal.consume_back("R")) {
3372 // MASM hexadecimal floating-point literal; no APFloat conversion needed.
3373 // To match ML64.exe, ignore the initial sign.
3374 unsigned SizeInBits = Value.getSizeInBits(Semantics);
3375 if (SizeInBits != (IDVal.size() << 2))
3376 return TokError("invalid floating point literal");
3377
3378 // Consume the numeric token.
3379 Lex();
3380
3381 Res = APInt(SizeInBits, IDVal, 16);
3382 if (SignLoc.isValid())
3383 return Warning(SignLoc, "MASM-style hex floats ignore explicit sign");
3384 return false;
3385 } else if (errorToBool(
3386 Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven)
3387 .takeError())) {
3388 return TokError("invalid floating point literal");
3389 }
3390 if (IsNeg)
3391 Value.changeSign();
3392
3393 // Consume the numeric token.
3394 Lex();
3395
3396 Res = Value.bitcastToAPInt();
3397
3398 return false;
3399}
3400
3401bool MasmParser::parseRealInstList(const fltSemantics &Semantics,
3402 SmallVectorImpl<APInt> &ValuesAsInt,
3403 const AsmToken::TokenKind EndToken) {
3404 while (getTok().isNot(EndToken) ||
3405 (EndToken == AsmToken::Greater &&
3406 getTok().isNot(AsmToken::GreaterGreater))) {
3407 const AsmToken NextTok = peekTok();
3408 if (NextTok.is(AsmToken::Identifier) &&
3409 NextTok.getString().equals_insensitive("dup")) {
3410 const MCExpr *Value;
3411 if (parseExpression(Value) || parseToken(AsmToken::Identifier))
3412 return true;
3413 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3414 if (!MCE)
3415 return Error(Value->getLoc(),
3416 "cannot repeat value a non-constant number of times");
3417 const int64_t Repetitions = MCE->getValue();
3418 if (Repetitions < 0)
3419 return Error(Value->getLoc(),
3420 "cannot repeat value a negative number of times");
3421
3422 SmallVector<APInt, 1> DuplicatedValues;
3423 if (parseToken(AsmToken::LParen,
3424 "parentheses required for 'dup' contents") ||
3425 parseRealInstList(Semantics, DuplicatedValues) || parseRParen())
3426 return true;
3427
3428 for (int i = 0; i < Repetitions; ++i)
3429 ValuesAsInt.append(DuplicatedValues.begin(), DuplicatedValues.end());
3430 } else {
3431 APInt AsInt;
3432 if (parseRealValue(Semantics, AsInt))
3433 return true;
3434 ValuesAsInt.push_back(AsInt);
3435 }
3436
3437 // Continue if we see a comma. (Also, allow line continuation.)
3438 if (!parseOptionalToken(AsmToken::Comma))
3439 break;
3440 parseOptionalToken(AsmToken::EndOfStatement);
3441 }
3442
3443 return false;
3444}
3445
3446// Initialize real data values.
3447bool MasmParser::emitRealValues(const fltSemantics &Semantics,
3448 unsigned *Count) {
3449 if (checkForValidSection())
3450 return true;
3451
3452 SmallVector<APInt, 1> ValuesAsInt;
3453 if (parseRealInstList(Semantics, ValuesAsInt))
3454 return true;
3455
3456 for (const APInt &AsInt : ValuesAsInt) {
3457 getStreamer().emitIntValue(AsInt);
3458 }
3459 if (Count)
3460 *Count = ValuesAsInt.size();
3461 return false;
3462}
3463
3464// Add a real field to the current struct.
3465bool MasmParser::addRealField(StringRef Name, const fltSemantics &Semantics,
3466 size_t Size) {
3467 StructInfo &Struct = StructInProgress.back();
3468 FieldInfo &Field = Struct.addField(Name, FT_REAL, Size);
3469 RealFieldInfo &RealInfo = Field.Contents.RealInfo;
3470
3471 Field.SizeOf = 0;
3472
3473 if (parseRealInstList(Semantics, RealInfo.AsIntValues))
3474 return true;
3475
3476 Field.Type = RealInfo.AsIntValues.back().getBitWidth() / 8;
3477 Field.LengthOf = RealInfo.AsIntValues.size();
3478 Field.SizeOf = Field.Type * Field.LengthOf;
3479
3480 const unsigned FieldEnd = Field.Offset + Field.SizeOf;
3481 if (!Struct.IsUnion) {
3482 Struct.NextOffset = FieldEnd;
3483 }
3484 Struct.Size = std::max(Struct.Size, FieldEnd);
3485 return false;
3486}
3487
3488/// parseDirectiveRealValue
3489/// ::= (real4 | real8 | real10) [ expression (, expression)* ]
3490bool MasmParser::parseDirectiveRealValue(StringRef IDVal,
3491 const fltSemantics &Semantics,
3492 size_t Size) {
3493 if (StructInProgress.empty()) {
3494 // Initialize data value.
3495 if (emitRealValues(Semantics))
3496 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3497 } else if (addRealField("", Semantics, Size)) {
3498 return addErrorSuffix(" in '" + Twine(IDVal) + "' directive");
3499 }
3500 return false;
3501}
3502
3503/// parseDirectiveNamedRealValue
3504/// ::= name (real4 | real8 | real10) [ expression (, expression)* ]
3505bool MasmParser::parseDirectiveNamedRealValue(StringRef TypeName,
3506 const fltSemantics &Semantics,
3507 unsigned Size, StringRef Name,
3508 SMLoc NameLoc) {
3509 if (StructInProgress.empty()) {
3510 // Initialize named data value.
3511 MCSymbol *Sym = getContext().parseSymbol(Name);
3512 getStreamer().emitLabel(Sym);
3513 unsigned Count;
3514 if (emitRealValues(Semantics, &Count))
3515 return addErrorSuffix(" in '" + TypeName + "' directive");
3516
3517 AsmTypeInfo Type;
3518 Type.Name = TypeName;
3519 Type.Size = Size * Count;
3520 Type.ElementSize = Size;
3521 Type.Length = Count;
3522 KnownType[Name.lower()] = Type;
3523 } else if (addRealField(Name, Semantics, Size)) {
3524 return addErrorSuffix(" in '" + TypeName + "' directive");
3525 }
3526 return false;
3527}
3528
3529bool MasmParser::parseOptionalAngleBracketOpen() {
3530 const AsmToken Tok = getTok();
3531 if (parseOptionalToken(AsmToken::LessLess)) {
3532 AngleBracketDepth++;
3533 Lexer.UnLex(AsmToken(AsmToken::Less, Tok.getString().substr(1)));
3534 return true;
3535 } else if (parseOptionalToken(AsmToken::LessGreater)) {
3536 AngleBracketDepth++;
3537 Lexer.UnLex(AsmToken(AsmToken::Greater, Tok.getString().substr(1)));
3538 return true;
3539 } else if (parseOptionalToken(AsmToken::Less)) {
3540 AngleBracketDepth++;
3541 return true;
3542 }
3543
3544 return false;
3545}
3546
3547bool MasmParser::parseAngleBracketClose(const Twine &Msg) {
3548 const AsmToken Tok = getTok();
3549 if (parseOptionalToken(AsmToken::GreaterGreater)) {
3550 Lexer.UnLex(AsmToken(AsmToken::Greater, Tok.getString().substr(1)));
3551 } else if (parseToken(AsmToken::Greater, Msg)) {
3552 return true;
3553 }
3554 AngleBracketDepth--;
3555 return false;
3556}
3557
3558bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
3559 const IntFieldInfo &Contents,
3560 FieldInitializer &Initializer) {
3561 SMLoc Loc = getTok().getLoc();
3562
3564 if (parseOptionalToken(AsmToken::LCurly)) {
3565 if (Field.LengthOf == 1 && Field.Type > 1)
3566 return Error(Loc, "Cannot initialize scalar field with array value");
3567 if (parseScalarInstList(Field.Type, Values, AsmToken::RCurly) ||
3568 parseToken(AsmToken::RCurly))
3569 return true;
3570 } else if (parseOptionalAngleBracketOpen()) {
3571 if (Field.LengthOf == 1 && Field.Type > 1)
3572 return Error(Loc, "Cannot initialize scalar field with array value");
3573 if (parseScalarInstList(Field.Type, Values, AsmToken::Greater) ||
3574 parseAngleBracketClose())
3575 return true;
3576 } else if (Field.LengthOf > 1 && Field.Type > 1) {
3577 return Error(Loc, "Cannot initialize array field with scalar value");
3578 } else if (parseScalarInitializer(Field.Type, Values,
3579 /*StringPadLength=*/Field.LengthOf)) {
3580 return true;
3581 }
3582
3583 if (Values.size() > Field.LengthOf) {
3584 return Error(Loc, "Initializer too long for field; expected at most " +
3585 std::to_string(Field.LengthOf) + " elements, got " +
3586 std::to_string(Values.size()));
3587 }
3588 // Default-initialize all remaining values.
3589 Values.append(Contents.Values.begin() + Values.size(), Contents.Values.end());
3590
3591 Initializer = FieldInitializer(std::move(Values));
3592 return false;
3593}
3594
3595bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
3596 const RealFieldInfo &Contents,
3597 FieldInitializer &Initializer) {
3598 const fltSemantics *Semantics;
3599 switch (Field.Type) {
3600 case 4:
3601 Semantics = &APFloat::IEEEsingle();
3602 break;
3603 case 8:
3604 Semantics = &APFloat::IEEEdouble();
3605 break;
3606 case 10:
3607 Semantics = &APFloat::x87DoubleExtended();
3608 break;
3609 default:
3610 llvm_unreachable("unknown real field type");
3611 }
3612
3613 SMLoc Loc = getTok().getLoc();
3614
3615 SmallVector<APInt, 1> AsIntValues;
3616 if (parseOptionalToken(AsmToken::LCurly)) {
3617 if (Field.LengthOf == 1)
3618 return Error(Loc, "Cannot initialize scalar field with array value");
3619 if (parseRealInstList(*Semantics, AsIntValues, AsmToken::RCurly) ||
3620 parseToken(AsmToken::RCurly))
3621 return true;
3622 } else if (parseOptionalAngleBracketOpen()) {
3623 if (Field.LengthOf == 1)
3624 return Error(Loc, "Cannot initialize scalar field with array value");
3625 if (parseRealInstList(*Semantics, AsIntValues, AsmToken::Greater) ||
3626 parseAngleBracketClose())
3627 return true;
3628 } else if (Field.LengthOf > 1) {
3629 return Error(Loc, "Cannot initialize array field with scalar value");
3630 } else {
3631 AsIntValues.emplace_back();
3632 if (parseRealValue(*Semantics, AsIntValues.back()))
3633 return true;
3634 }
3635
3636 if (AsIntValues.size() > Field.LengthOf) {
3637 return Error(Loc, "Initializer too long for field; expected at most " +
3638 std::to_string(Field.LengthOf) + " elements, got " +
3639 std::to_string(AsIntValues.size()));
3640 }
3641 // Default-initialize all remaining values.
3642 AsIntValues.append(Contents.AsIntValues.begin() + AsIntValues.size(),
3643 Contents.AsIntValues.end());
3644
3645 Initializer = FieldInitializer(std::move(AsIntValues));
3646 return false;
3647}
3648
3649bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
3650 const StructFieldInfo &Contents,
3651 FieldInitializer &Initializer) {
3652 SMLoc Loc = getTok().getLoc();
3653
3654 std::vector<StructInitializer> Initializers;
3655 if (Field.LengthOf > 1) {
3656 if (parseOptionalToken(AsmToken::LCurly)) {
3657 if (parseStructInstList(Contents.Structure, Initializers,
3659 parseToken(AsmToken::RCurly))
3660 return true;
3661 } else if (parseOptionalAngleBracketOpen()) {
3662 if (parseStructInstList(Contents.Structure, Initializers,
3664 parseAngleBracketClose())
3665 return true;
3666 } else {
3667 return Error(Loc, "Cannot initialize array field with scalar value");
3668 }
3669 } else {
3670 Initializers.emplace_back();
3671 if (parseStructInitializer(Contents.Structure, Initializers.back()))
3672 return true;
3673 }
3674
3675 if (Initializers.size() > Field.LengthOf) {
3676 return Error(Loc, "Initializer too long for field; expected at most " +
3677 std::to_string(Field.LengthOf) + " elements, got " +
3678 std::to_string(Initializers.size()));
3679 }
3680 // Default-initialize all remaining values.
3681 llvm::append_range(Initializers, llvm::drop_begin(Contents.Initializers,
3682 Initializers.size()));
3683
3684 Initializer = FieldInitializer(std::move(Initializers), Contents.Structure);
3685 return false;
3686}
3687
3688bool MasmParser::parseFieldInitializer(const FieldInfo &Field,
3689 FieldInitializer &Initializer) {
3690 switch (Field.Contents.FT) {
3691 case FT_INTEGRAL:
3692 return parseFieldInitializer(Field, Field.Contents.IntInfo, Initializer);
3693 case FT_REAL:
3694 return parseFieldInitializer(Field, Field.Contents.RealInfo, Initializer);
3695 case FT_STRUCT:
3696 return parseFieldInitializer(Field, Field.Contents.StructInfo, Initializer);
3697 }
3698 llvm_unreachable("Unhandled FieldType enum");
3699}
3700
3701bool MasmParser::parseStructInitializer(const StructInfo &Structure,
3702 StructInitializer &Initializer) {
3703 const AsmToken FirstToken = getTok();
3704
3705 std::optional<AsmToken::TokenKind> EndToken;
3706 if (parseOptionalToken(AsmToken::LCurly)) {
3707 EndToken = AsmToken::RCurly;
3708 } else if (parseOptionalAngleBracketOpen()) {
3709 EndToken = AsmToken::Greater;
3710 AngleBracketDepth++;
3711 } else if (FirstToken.is(AsmToken::Identifier) &&
3712 FirstToken.getString() == "?") {
3713 // ? initializer; leave EndToken uninitialized to treat as empty.
3714 if (parseToken(AsmToken::Identifier))
3715 return true;
3716 } else {
3717 return Error(FirstToken.getLoc(), "Expected struct initializer");
3718 }
3719
3720 auto &FieldInitializers = Initializer.FieldInitializers;
3721 size_t FieldIndex = 0;
3722 if (EndToken) {
3723 // Initialize all fields with given initializers.
3724 while (getTok().isNot(*EndToken) && FieldIndex < Structure.Fields.size()) {
3725 const FieldInfo &Field = Structure.Fields[FieldIndex++];
3726 if (parseOptionalToken(AsmToken::Comma)) {
3727 // Empty initializer; use the default and continue. (Also, allow line
3728 // continuation.)
3729 FieldInitializers.push_back(Field.Contents);
3730 parseOptionalToken(AsmToken::EndOfStatement);
3731 continue;
3732 }
3733 FieldInitializers.emplace_back(Field.Contents.FT);
3734 if (parseFieldInitializer(Field, FieldInitializers.back()))
3735 return true;
3736
3737 // Continue if we see a comma. (Also, allow line continuation.)
3738 SMLoc CommaLoc = getTok().getLoc();
3739 if (!parseOptionalToken(AsmToken::Comma))
3740 break;
3741 if (FieldIndex == Structure.Fields.size())
3742 return Error(CommaLoc, "'" + Structure.Name +
3743 "' initializer initializes too many fields");
3744 parseOptionalToken(AsmToken::EndOfStatement);
3745 }
3746 }
3747 // Default-initialize all remaining fields.
3748 for (const FieldInfo &Field : llvm::drop_begin(Structure.Fields, FieldIndex))
3749 FieldInitializers.push_back(Field.Contents);
3750
3751 if (EndToken) {
3752 if (*EndToken == AsmToken::Greater)
3753 return parseAngleBracketClose();
3754
3755 return parseToken(*EndToken);
3756 }
3757
3758 return false;
3759}
3760
3761bool MasmParser::parseStructInstList(
3762 const StructInfo &Structure, std::vector<StructInitializer> &Initializers,
3763 const AsmToken::TokenKind EndToken) {
3764 while (getTok().isNot(EndToken) ||
3765 (EndToken == AsmToken::Greater &&
3766 getTok().isNot(AsmToken::GreaterGreater))) {
3767 const AsmToken NextTok = peekTok();
3768 if (NextTok.is(AsmToken::Identifier) &&
3769 NextTok.getString().equals_insensitive("dup")) {
3770 const MCExpr *Value;
3771 if (parseExpression(Value) || parseToken(AsmToken::Identifier))
3772 return true;
3773 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
3774 if (!MCE)
3775 return Error(Value->getLoc(),
3776 "cannot repeat value a non-constant number of times");
3777 const int64_t Repetitions = MCE->getValue();
3778 if (Repetitions < 0)
3779 return Error(Value->getLoc(),
3780 "cannot repeat value a negative number of times");
3781
3782 std::vector<StructInitializer> DuplicatedValues;
3783 if (parseToken(AsmToken::LParen,
3784 "parentheses required for 'dup' contents") ||
3785 parseStructInstList(Structure, DuplicatedValues) || parseRParen())
3786 return true;
3787
3788 for (int i = 0; i < Repetitions; ++i)
3789 llvm::append_range(Initializers, DuplicatedValues);
3790 } else {
3791 Initializers.emplace_back();
3792 if (parseStructInitializer(Structure, Initializers.back()))
3793 return true;
3794 }
3795
3796 // Continue if we see a comma. (Also, allow line continuation.)
3797 if (!parseOptionalToken(AsmToken::Comma))
3798 break;
3799 parseOptionalToken(AsmToken::EndOfStatement);
3800 }
3801
3802 return false;
3803}
3804
3805bool MasmParser::emitFieldValue(const FieldInfo &Field,
3806 const IntFieldInfo &Contents) {
3807 // Default-initialize all values.
3808 for (const MCExpr *Value : Contents.Values) {
3809 if (emitIntValue(Value, Field.Type))
3810 return true;
3811 }
3812 return false;
3813}
3814
3815bool MasmParser::emitFieldValue(const FieldInfo &Field,
3816 const RealFieldInfo &Contents) {
3817 for (const APInt &AsInt : Contents.AsIntValues) {
3818 getStreamer().emitIntValue(AsInt.getLimitedValue(),
3819 AsInt.getBitWidth() / 8);
3820 }
3821 return false;
3822}
3823
3824bool MasmParser::emitFieldValue(const FieldInfo &Field,
3825 const StructFieldInfo &Contents) {
3826 for (const auto &Initializer : Contents.Initializers) {
3827 size_t Index = 0, Offset = 0;
3828 for (const auto &SubField : Contents.Structure.Fields) {
3829 getStreamer().emitZeros(SubField.Offset - Offset);
3830 Offset = SubField.Offset + SubField.SizeOf;
3831 emitFieldInitializer(SubField, Initializer.FieldInitializers[Index++]);
3832 }
3833 }
3834 return false;
3835}
3836
3837bool MasmParser::emitFieldValue(const FieldInfo &Field) {
3838 switch (Field.Contents.FT) {
3839 case FT_INTEGRAL:
3840 return emitFieldValue(Field, Field.Contents.IntInfo);
3841 case FT_REAL:
3842 return emitFieldValue(Field, Field.Contents.RealInfo);
3843 case FT_STRUCT:
3844 return emitFieldValue(Field, Field.Contents.StructInfo);
3845 }
3846 llvm_unreachable("Unhandled FieldType enum");
3847}
3848
3849bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
3850 const IntFieldInfo &Contents,
3851 const IntFieldInfo &Initializer) {
3852 for (const auto &Value : Initializer.Values) {
3853 if (emitIntValue(Value, Field.Type))
3854 return true;
3855 }
3856 // Default-initialize all remaining values.
3857 for (const auto &Value :
3858 llvm::drop_begin(Contents.Values, Initializer.Values.size())) {
3859 if (emitIntValue(Value, Field.Type))
3860 return true;
3861 }
3862 return false;
3863}
3864
3865bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
3866 const RealFieldInfo &Contents,
3867 const RealFieldInfo &Initializer) {
3868 for (const auto &AsInt : Initializer.AsIntValues) {
3869 getStreamer().emitIntValue(AsInt.getLimitedValue(),
3870 AsInt.getBitWidth() / 8);
3871 }
3872 // Default-initialize all remaining values.
3873 for (const auto &AsInt :
3874 llvm::drop_begin(Contents.AsIntValues, Initializer.AsIntValues.size())) {
3875 getStreamer().emitIntValue(AsInt.getLimitedValue(),
3876 AsInt.getBitWidth() / 8);
3877 }
3878 return false;
3879}
3880
3881bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
3882 const StructFieldInfo &Contents,
3883 const StructFieldInfo &Initializer) {
3884 for (const auto &Init : Initializer.Initializers) {
3885 if (emitStructInitializer(Contents.Structure, Init))
3886 return true;
3887 }
3888 // Default-initialize all remaining values.
3889 for (const auto &Init : llvm::drop_begin(Contents.Initializers,
3890 Initializer.Initializers.size())) {
3891 if (emitStructInitializer(Contents.Structure, Init))
3892 return true;
3893 }
3894 return false;
3895}
3896
3897bool MasmParser::emitFieldInitializer(const FieldInfo &Field,
3898 const FieldInitializer &Initializer) {
3899 switch (Field.Contents.FT) {
3900 case FT_INTEGRAL:
3901 return emitFieldInitializer(Field, Field.Contents.IntInfo,
3902 Initializer.IntInfo);
3903 case FT_REAL:
3904 return emitFieldInitializer(Field, Field.Contents.RealInfo,
3905 Initializer.RealInfo);
3906 case FT_STRUCT:
3907 return emitFieldInitializer(Field, Field.Contents.StructInfo,
3908 Initializer.StructInfo);
3909 }
3910 llvm_unreachable("Unhandled FieldType enum");
3911}
3912
3913bool MasmParser::emitStructInitializer(const StructInfo &Structure,
3914 const StructInitializer &Initializer) {
3915 if (!Structure.Initializable)
3916 return Error(getLexer().getLoc(),
3917 "cannot initialize a value of type '" + Structure.Name +
3918 "'; 'org' was used in the type's declaration");
3919 size_t Index = 0, Offset = 0;
3920 for (const auto &Init : Initializer.FieldInitializers) {
3921 const auto &Field = Structure.Fields[Index++];
3922 getStreamer().emitZeros(Field.Offset - Offset);
3923 Offset = Field.Offset + Field.SizeOf;
3924 if (emitFieldInitializer(Field, Init))
3925 return true;
3926 }
3927 // Default-initialize all remaining fields.
3928 for (const auto &Field : llvm::drop_begin(
3929 Structure.Fields, Initializer.FieldInitializers.size())) {
3930 getStreamer().emitZeros(Field.Offset - Offset);
3931 Offset = Field.Offset + Field.SizeOf;
3932 if (emitFieldValue(Field))
3933 return true;
3934 }
3935 // Add final padding.
3936 if (Offset != Structure.Size)
3937 getStreamer().emitZeros(Structure.Size - Offset);
3938 return false;
3939}
3940
3941// Set data values from initializers.
3942bool MasmParser::emitStructValues(const StructInfo &Structure,
3943 unsigned *Count) {
3944 std::vector<StructInitializer> Initializers;
3945 if (parseStructInstList(Structure, Initializers))
3946 return true;
3947
3948 for (const auto &Initializer : Initializers) {
3949 if (emitStructInitializer(Structure, Initializer))
3950 return true;
3951 }
3952
3953 if (Count)
3954 *Count = Initializers.size();
3955 return false;
3956}
3957
3958// Declare a field in the current struct.
3959bool MasmParser::addStructField(StringRef Name, const StructInfo &Structure) {
3960 StructInfo &OwningStruct = StructInProgress.back();
3961 FieldInfo &Field =
3962 OwningStruct.addField(Name, FT_STRUCT, Structure.AlignmentSize);
3963 StructFieldInfo &StructInfo = Field.Contents.StructInfo;
3964
3965 StructInfo.Structure = Structure;
3966 Field.Type = Structure.Size;
3967
3968 if (parseStructInstList(Structure, StructInfo.Initializers))
3969 return true;
3970
3971 Field.LengthOf = StructInfo.Initializers.size();
3972 Field.SizeOf = Field.Type * Field.LengthOf;
3973
3974 const unsigned FieldEnd = Field.Offset + Field.SizeOf;
3975 if (!OwningStruct.IsUnion) {
3976 OwningStruct.NextOffset = FieldEnd;
3977 }
3978 OwningStruct.Size = std::max(OwningStruct.Size, FieldEnd);
3979
3980 return false;
3981}
3982
3983/// parseDirectiveStructValue
3984/// ::= struct-id (<struct-initializer> | {struct-initializer})
3985/// [, (<struct-initializer> | {struct-initializer})]*
3986bool MasmParser::parseDirectiveStructValue(const StructInfo &Structure,
3987 StringRef Directive, SMLoc DirLoc) {
3988 if (StructInProgress.empty()) {
3989 if (emitStructValues(Structure))
3990 return true;
3991 } else if (addStructField("", Structure)) {
3992 return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
3993 }
3994
3995 return false;
3996}
3997
3998/// parseDirectiveNamedValue
3999/// ::= name (byte | word | ... ) [ expression (, expression)* ]
4000bool MasmParser::parseDirectiveNamedStructValue(const StructInfo &Structure,
4001 StringRef Directive,
4002 SMLoc DirLoc, StringRef Name) {
4003 if (StructInProgress.empty()) {
4004 // Initialize named data value.
4005 MCSymbol *Sym = getContext().parseSymbol(Name);
4006 getStreamer().emitLabel(Sym);
4007 unsigned Count;
4008 if (emitStructValues(Structure, &Count))
4009 return true;
4010 AsmTypeInfo Type;
4011 Type.Name = Structure.Name;
4012 Type.Size = Structure.Size * Count;
4013 Type.ElementSize = Structure.Size;
4014 Type.Length = Count;
4015 KnownType[Name.lower()] = Type;
4016 } else if (addStructField(Name, Structure)) {
4017 return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
4018 }
4019
4020 return false;
4021}
4022
4023/// parseDirectiveStruct
4024/// ::= <name> (STRUC | STRUCT | UNION) [fieldAlign] [, NONUNIQUE]
4025/// (dataDir | generalDir | offsetDir | nestedStruct)+
4026/// <name> ENDS
4027////// dataDir = data declaration
4028////// offsetDir = EVEN, ORG, ALIGN
4029bool MasmParser::parseDirectiveStruct(StringRef Directive,
4030 DirectiveKind DirKind, StringRef Name,
4031 SMLoc NameLoc) {
4032 // We ignore NONUNIQUE; we do not support OPTION M510 or OPTION OLDSTRUCTS
4033 // anyway, so all field accesses must be qualified.
4034 AsmToken NextTok = getTok();
4035 int64_t AlignmentValue = 1;
4036 if (NextTok.isNot(AsmToken::Comma) &&
4038 parseAbsoluteExpression(AlignmentValue)) {
4039 return addErrorSuffix(" in alignment value for '" + Twine(Directive) +
4040 "' directive");
4041 }
4042 if (!isPowerOf2_64(AlignmentValue)) {
4043 return Error(NextTok.getLoc(), "alignment must be a power of two; was " +
4044 std::to_string(AlignmentValue));
4045 }
4046
4047 StringRef Qualifier;
4048 SMLoc QualifierLoc;
4049 if (parseOptionalToken(AsmToken::Comma)) {
4050 QualifierLoc = getTok().getLoc();
4051 if (parseIdentifier(Qualifier))
4052 return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
4053 if (!Qualifier.equals_insensitive("nonunique"))
4054 return Error(QualifierLoc, "Unrecognized qualifier for '" +
4055 Twine(Directive) +
4056 "' directive; expected none or NONUNIQUE");
4057 }
4058
4059 if (parseEOL())
4060 return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
4061
4062 StructInProgress.emplace_back(Name, DirKind == DK_UNION, AlignmentValue);
4063 return false;
4064}
4065
4066/// parseDirectiveNestedStruct
4067/// ::= (STRUC | STRUCT | UNION) [name]
4068/// (dataDir | generalDir | offsetDir | nestedStruct)+
4069/// ENDS
4070bool MasmParser::parseDirectiveNestedStruct(StringRef Directive,
4071 DirectiveKind DirKind) {
4072 if (StructInProgress.empty())
4073 return TokError("missing name in top-level '" + Twine(Directive) +
4074 "' directive");
4075
4076 StringRef Name;
4077 if (getTok().is(AsmToken::Identifier)) {
4078 Name = getTok().getIdentifier();
4079 parseToken(AsmToken::Identifier);
4080 }
4081 if (parseEOL())
4082 return addErrorSuffix(" in '" + Twine(Directive) + "' directive");
4083
4084 // Reserve space to ensure Alignment doesn't get invalidated when
4085 // StructInProgress grows.
4086 StructInProgress.reserve(StructInProgress.size() + 1);
4087 StructInProgress.emplace_back(Name, DirKind == DK_UNION,
4088 StructInProgress.back().Alignment);
4089 return false;
4090}
4091
4092bool MasmParser::parseDirectiveEnds(StringRef Name, SMLoc NameLoc) {
4093 if (StructInProgress.empty())
4094 return Error(NameLoc, "ENDS directive without matching STRUC/STRUCT/UNION");
4095 if (StructInProgress.size() > 1)
4096 return Error(NameLoc, "unexpected name in nested ENDS directive");
4097 if (StructInProgress.back().Name.compare_insensitive(Name))
4098 return Error(NameLoc, "mismatched name in ENDS directive; expected '" +
4099 StructInProgress.back().Name + "'");
4100 StructInfo Structure = StructInProgress.pop_back_val();
4101 // Pad to make the structure's size divisible by the smaller of its alignment
4102 // and the size of its largest field.
4103 Structure.Size = llvm::alignTo(
4104 Structure.Size, std::min(Structure.Alignment, Structure.AlignmentSize));
4105 Structs[Name.lower()] = Structure;
4106
4107 if (parseEOL())
4108 return addErrorSuffix(" in ENDS directive");
4109
4110 return false;
4111}
4112
4113bool MasmParser::parseDirectiveNestedEnds() {
4114 if (StructInProgress.empty())
4115 return TokError("ENDS directive without matching STRUC/STRUCT/UNION");
4116 if (StructInProgress.size() == 1)
4117 return TokError("missing name in top-level ENDS directive");
4118
4119 if (parseEOL())
4120 return addErrorSuffix(" in nested ENDS directive");
4121
4122 StructInfo Structure = StructInProgress.pop_back_val();
4123 // Pad to make the structure's size divisible by its alignment.
4124 Structure.Size = llvm::alignTo(Structure.Size, Structure.Alignment);
4125
4126 StructInfo &ParentStruct = StructInProgress.back();
4127 if (Structure.Name.empty()) {
4128 // Anonymous substructures' fields are addressed as if they belong to the
4129 // parent structure - so we transfer them to the parent here.
4130 const size_t OldFields = ParentStruct.Fields.size();
4131 ParentStruct.Fields.insert(
4132 ParentStruct.Fields.end(),
4133 std::make_move_iterator(Structure.Fields.begin()),
4134 std::make_move_iterator(Structure.Fields.end()));
4135 for (const auto &FieldByName : Structure.FieldsByName) {
4136 ParentStruct.FieldsByName[FieldByName.getKey()] =
4137 FieldByName.getValue() + OldFields;
4138 }
4139
4140 unsigned FirstFieldOffset = 0;
4141 if (!Structure.Fields.empty() && !ParentStruct.IsUnion) {
4142 FirstFieldOffset = llvm::alignTo(
4143 ParentStruct.NextOffset,
4144 std::min(ParentStruct.Alignment, Structure.AlignmentSize));
4145 }
4146
4147 if (ParentStruct.IsUnion) {
4148 ParentStruct.Size = std::max(ParentStruct.Size, Structure.Size);
4149 } else {
4150 for (auto &Field : llvm::drop_begin(ParentStruct.Fields, OldFields))
4151 Field.Offset += FirstFieldOffset;
4152
4153 const unsigned StructureEnd = FirstFieldOffset + Structure.Size;
4154 if (!ParentStruct.IsUnion) {
4155 ParentStruct.NextOffset = StructureEnd;
4156 }
4157 ParentStruct.Size = std::max(ParentStruct.Size, StructureEnd);
4158 }
4159 } else {
4160 FieldInfo &Field = ParentStruct.addField(Structure.Name, FT_STRUCT,
4161 Structure.AlignmentSize);
4162 StructFieldInfo &StructInfo = Field.Contents.StructInfo;
4163 Field.Type = Structure.Size;
4164 Field.LengthOf = 1;
4165 Field.SizeOf = Structure.Size;
4166
4167 const unsigned StructureEnd = Field.Offset + Field.SizeOf;
4168 if (!ParentStruct.IsUnion) {
4169 ParentStruct.NextOffset = StructureEnd;
4170 }
4171 ParentStruct.Size = std::max(ParentStruct.Size, StructureEnd);
4172
4173 StructInfo.Structure = Structure;
4174 StructInfo.Initializers.emplace_back();
4175 auto &FieldInitializers = StructInfo.Initializers.back().FieldInitializers;
4176 for (const auto &SubField : Structure.Fields) {
4177 FieldInitializers.push_back(SubField.Contents);
4178 }
4179 }
4180
4181 return false;
4182}
4183
4184/// parseDirectiveOrg
4185/// ::= org expression
4186bool MasmParser::parseDirectiveOrg() {
4187 const MCExpr *Offset;
4188 SMLoc OffsetLoc = Lexer.getLoc();
4189 if (checkForValidSection() || parseExpression(Offset))
4190 return true;
4191 if (parseEOL())
4192 return addErrorSuffix(" in 'org' directive");
4193
4194 if (StructInProgress.empty()) {
4195 // Not in a struct; change the offset for the next instruction or data
4196 if (checkForValidSection())
4197 return addErrorSuffix(" in 'org' directive");
4198
4199 getStreamer().emitValueToOffset(Offset, 0, OffsetLoc);
4200 } else {
4201 // Offset the next field of this struct
4202 StructInfo &Structure = StructInProgress.back();
4203 int64_t OffsetRes;
4204 if (!Offset->evaluateAsAbsolute(OffsetRes, getStreamer().getAssemblerPtr()))
4205 return Error(OffsetLoc,
4206 "expected absolute expression in 'org' directive");
4207 if (OffsetRes < 0)
4208 return Error(
4209 OffsetLoc,
4210 "expected non-negative value in struct's 'org' directive; was " +
4211 std::to_string(OffsetRes));
4212 Structure.NextOffset = static_cast<unsigned>(OffsetRes);
4213
4214 // ORG-affected structures cannot be initialized
4215 Structure.Initializable = false;
4216 }
4217
4218 return false;
4219}
4220
4221bool MasmParser::emitAlignTo(int64_t Alignment) {
4222 if (StructInProgress.empty()) {
4223 // Not in a struct; align the next instruction or data
4224 if (checkForValidSection())
4225 return true;
4226
4227 // Check whether we should use optimal code alignment for this align
4228 // directive.
4229 const MCSection *Section = getStreamer().getCurrentSectionOnly();
4230 if (MAI.useCodeAlign(*Section)) {
4231 getStreamer().emitCodeAlignment(Align(Alignment),
4232 &getTargetParser().getSTI(),
4233 /*MaxBytesToEmit=*/0);
4234 } else {
4235 // FIXME: Target specific behavior about how the "extra" bytes are filled.
4236 getStreamer().emitValueToAlignment(Align(Alignment), /*Value=*/0,
4237 /*ValueSize=*/1,
4238 /*MaxBytesToEmit=*/0);
4239 }
4240 } else {
4241 // Align the next field of this struct
4242 StructInfo &Structure = StructInProgress.back();
4243 Structure.NextOffset = llvm::alignTo(Structure.NextOffset, Alignment);
4244 }
4245
4246 return false;
4247}
4248
4249/// parseDirectiveAlign
4250/// ::= align expression
4251bool MasmParser::parseDirectiveAlign() {
4252 SMLoc AlignmentLoc = getLexer().getLoc();
4253 int64_t Alignment;
4254
4255 // Ignore empty 'align' directives.
4256 if (getTok().is(AsmToken::EndOfStatement)) {
4257 return Warning(AlignmentLoc,
4258 "align directive with no operand is ignored") &&
4259 parseEOL();
4260 }
4261 if (parseAbsoluteExpression(Alignment) || parseEOL())
4262 return addErrorSuffix(" in align directive");
4263
4264 // Always emit an alignment here even if we throw an error.
4265 bool ReturnVal = false;
4266
4267 // Reject alignments that aren't either a power of two or zero, for ML.exe
4268 // compatibility. Alignment of zero is silently rounded up to one.
4269 if (Alignment == 0)
4270 Alignment = 1;
4271 if (!isPowerOf2_64(Alignment))
4272 ReturnVal |= Error(AlignmentLoc, "alignment must be a power of 2; was " +
4273 std::to_string(Alignment));
4274
4275 if (emitAlignTo(Alignment))
4276 ReturnVal |= addErrorSuffix(" in align directive");
4277
4278 return ReturnVal;
4279}
4280
4281/// parseDirectiveEven
4282/// ::= even
4283bool MasmParser::parseDirectiveEven() {
4284 if (parseEOL() || emitAlignTo(2))
4285 return addErrorSuffix(" in even directive");
4286
4287 return false;
4288}
4289
4290/// parseDirectiveMacro
4291/// ::= name macro [parameters]
4292/// ["LOCAL" identifiers]
4293/// parameters ::= parameter [, parameter]*
4294/// parameter ::= name ":" qualifier
4295/// qualifier ::= "req" | "vararg" | "=" macro_argument
4296bool MasmParser::parseDirectiveMacro(StringRef Name, SMLoc NameLoc) {
4298 while (getLexer().isNot(AsmToken::EndOfStatement)) {
4299 if (!Parameters.empty() && Parameters.back().Vararg)
4300 return Error(Lexer.getLoc(),
4301 "Vararg parameter '" + Parameters.back().Name +
4302 "' should be last in the list of parameters");
4303
4304 MCAsmMacroParameter Parameter;
4305 if (parseIdentifier(Parameter.Name))
4306 return TokError("expected identifier in 'macro' directive");
4307
4308 // Emit an error if two (or more) named parameters share the same name.
4309 for (const MCAsmMacroParameter& CurrParam : Parameters)
4310 if (CurrParam.Name.equals_insensitive(Parameter.Name))
4311 return TokError("macro '" + Name + "' has multiple parameters"
4312 " named '" + Parameter.Name + "'");
4313
4314 if (Lexer.is(AsmToken::Colon)) {
4315 Lex(); // consume ':'
4316
4317 if (parseOptionalToken(AsmToken::Equal)) {
4318 // Default value
4319 SMLoc ParamLoc;
4320
4321 ParamLoc = Lexer.getLoc();
4322 if (parseMacroArgument(nullptr, Parameter.Value))
4323 return true;
4324 } else {
4325 SMLoc QualLoc;
4326 StringRef Qualifier;
4327
4328 QualLoc = Lexer.getLoc();
4329 if (parseIdentifier(Qualifier))
4330 return Error(QualLoc, "missing parameter qualifier for "
4331 "'" +
4332 Parameter.Name + "' in macro '" + Name +
4333 "'");
4334
4335 if (Qualifier.equals_insensitive("req"))
4336 Parameter.Required = true;
4337 else if (Qualifier.equals_insensitive("vararg"))
4338 Parameter.Vararg = true;
4339 else
4340 return Error(QualLoc,
4341 Qualifier + " is not a valid parameter qualifier for '" +
4342 Parameter.Name + "' in macro '" + Name + "'");
4343 }
4344 }
4345
4346 Parameters.push_back(std::move(Parameter));
4347
4348 if (getLexer().is(AsmToken::Comma))
4349 Lex();
4350 }
4351
4352 // Eat just the end of statement.
4353 Lexer.Lex();
4354
4355 std::vector<std::string> Locals;
4356 if (getTok().is(AsmToken::Identifier) &&
4357 getTok().getIdentifier().equals_insensitive("local")) {
4358 Lex(); // Eat the LOCAL directive.
4359
4360 StringRef ID;
4361 while (true) {
4362 if (parseIdentifier(ID))
4363 return true;
4364 Locals.push_back(ID.lower());
4365
4366 // If we see a comma, continue (and allow line continuation).
4367 if (!parseOptionalToken(AsmToken::Comma))
4368 break;
4369 parseOptionalToken(AsmToken::EndOfStatement);
4370 }
4371 }
4372
4373 // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors.
4374 AsmToken EndToken, StartToken = getTok();
4375 unsigned MacroDepth = 0;
4376 bool IsMacroFunction = false;
4377 // Lex the macro definition.
4378 while (true) {
4379 // Ignore Lexing errors in macros.
4380 while (Lexer.is(AsmToken::Error)) {
4381 Lexer.Lex();
4382 }
4383
4384 // Check whether we have reached the end of the file.
4385 if (getLexer().is(AsmToken::Eof))
4386 return Error(NameLoc, "no matching 'endm' in definition");
4387
4388 // Otherwise, check whether we have reached the 'endm'... and determine if
4389 // this is a macro function.
4390 if (getLexer().is(AsmToken::Identifier)) {
4391 if (getTok().getIdentifier().equals_insensitive("endm")) {
4392 if (MacroDepth == 0) { // Outermost macro.
4393 EndToken = getTok();
4394 Lexer.Lex();
4395 if (getLexer().isNot(AsmToken::EndOfStatement))
4396 return TokError("unexpected token in '" + EndToken.getIdentifier() +
4397 "' directive");
4398 break;
4399 } else {
4400 // Otherwise we just found the end of an inner macro.
4401 --MacroDepth;
4402 }
4403 } else if (getTok().getIdentifier().equals_insensitive("exitm")) {
4404 if (MacroDepth == 0 && peekTok().isNot(AsmToken::EndOfStatement)) {
4405 IsMacroFunction = true;
4406 }
4407 } else if (isMacroLikeDirective()) {
4408 // We allow nested macros. Those aren't instantiated until the
4409 // outermost macro is expanded so just ignore them for now.
4410 ++MacroDepth;
4411 }
4412 }
4413
4414 // Otherwise, scan til the end of the statement.
4415 eatToEndOfStatement();
4416 }
4417
4418 if (getContext().lookupMacro(Name.lower())) {
4419 return Error(NameLoc, "macro '" + Name + "' is already defined");
4420 }
4421
4422 const char *BodyStart = StartToken.getLoc().getPointer();
4423 const char *BodyEnd = EndToken.getLoc().getPointer();
4424 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
4425 MCAsmMacro Macro(Name, Body, std::move(Parameters), std::move(Locals),
4426 IsMacroFunction);
4427 DEBUG_WITH_TYPE("asm-macros", dbgs() << "Defining new macro:\n";
4428 Macro.dump());
4429 getContext().defineMacro(Name.lower(), std::move(Macro));
4430 return false;
4431}
4432
4433/// parseDirectiveExitMacro
4434/// ::= "exitm" [textitem]
4435bool MasmParser::parseDirectiveExitMacro(SMLoc DirectiveLoc,
4436 StringRef Directive,
4437 std::string &Value) {
4438 SMLoc EndLoc = getTok().getLoc();
4439 if (getTok().isNot(AsmToken::EndOfStatement) && parseTextItem(Value))
4440 return Error(EndLoc,
4441 "unable to parse text item in '" + Directive + "' directive");
4442 eatToEndOfStatement();
4443
4444 if (!isInsideMacroInstantiation())
4445 return TokError("unexpected '" + Directive + "' in file, "
4446 "no current macro definition");
4447
4448 // Exit all conditionals that are active in the current macro.
4449 while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) {
4450 TheCondState = TheCondStack.back();
4451 TheCondStack.pop_back();
4452 }
4453
4454 handleMacroExit();
4455 return false;
4456}
4457
4458/// parseDirectiveEndMacro
4459/// ::= endm
4460bool MasmParser::parseDirectiveEndMacro(StringRef Directive) {
4461 if (getLexer().isNot(AsmToken::EndOfStatement))
4462 return TokError("unexpected token in '" + Directive + "' directive");
4463
4464 // If we are inside a macro instantiation, terminate the current
4465 // instantiation.
4466 if (isInsideMacroInstantiation()) {
4467 handleMacroExit();
4468 return false;
4469 }
4470
4471 // Otherwise, this .endmacro is a stray entry in the file; well formed
4472 // .endmacro directives are handled during the macro definition parsing.
4473 return TokError("unexpected '" + Directive + "' in file, "
4474 "no current macro definition");
4475}
4476
4477/// parseDirectivePurgeMacro
4478/// ::= purge identifier ( , identifier )*
4479bool MasmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) {
4480 StringRef Name;
4481 while (true) {
4482 SMLoc NameLoc;
4483 if (parseTokenLoc(NameLoc) ||
4484 check(parseIdentifier(Name), NameLoc,
4485 "expected identifier in 'purge' directive"))
4486 return true;
4487
4488 DEBUG_WITH_TYPE("asm-macros", dbgs()
4489 << "Un-defining macro: " << Name << "\n");
4490 if (!getContext().lookupMacro(Name.lower()))
4491 return Error(NameLoc, "macro '" + Name + "' is not defined");
4492 getContext().undefineMacro(Name.lower());
4493
4494 if (!parseOptionalToken(AsmToken::Comma))
4495 break;
4496 parseOptionalToken(AsmToken::EndOfStatement);
4497 }
4498
4499 return false;
4500}
4501
4502bool MasmParser::parseDirectiveExtern() {
4503 // .extern is the default - but we still need to take any provided type info.
4504 auto parseOp = [&]() -> bool {
4505 MCSymbol *Sym;
4506 SMLoc NameLoc = getTok().getLoc();
4507 if (parseSymbol(Sym))
4508 return Error(NameLoc, "expected name");
4509 if (parseToken(AsmToken::Colon))
4510 return true;
4511
4512 StringRef TypeName;
4513 SMLoc TypeLoc = getTok().getLoc();
4514 if (parseIdentifier(TypeName))
4515 return Error(TypeLoc, "expected type");
4516 if (!TypeName.equals_insensitive("proc")) {
4517 AsmTypeInfo Type;
4518 if (lookUpType(TypeName, Type))
4519 return Error(TypeLoc, "unrecognized type");
4520 KnownType[Sym->getName().lower()] = Type;
4521 }
4522
4523 static_cast<MCSymbolCOFF *>(Sym)->setExternal(true);
4524 getStreamer().emitSymbolAttribute(Sym, MCSA_Extern);
4525
4526 return false;
4527 };
4528
4529 if (parseMany(parseOp))
4530 return addErrorSuffix(" in directive 'extern'");
4531 return false;
4532}
4533
4534/// parseDirectiveSymbolAttribute
4535/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
4536bool MasmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
4537 auto parseOp = [&]() -> bool {
4538 SMLoc Loc = getTok().getLoc();
4539 MCSymbol *Sym;
4540 if (parseSymbol(Sym))
4541 return Error(Loc, "expected identifier");
4542
4543 // Assembler local symbols don't make any sense here. Complain loudly.
4544 if (Sym->isTemporary())
4545 return Error(Loc, "non-local symbol required");
4546
4547 if (!getStreamer().emitSymbolAttribute(Sym, Attr))
4548 return Error(Loc, "unable to emit symbol attribute");
4549 return false;
4550 };
4551
4552 if (parseMany(parseOp))
4553 return addErrorSuffix(" in directive");
4554 return false;
4555}
4556
4557/// parseDirectiveComm
4558/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
4559bool MasmParser::parseDirectiveComm(bool IsLocal) {
4560 if (checkForValidSection())
4561 return true;
4562
4563 SMLoc IDLoc = getLexer().getLoc();
4564 MCSymbol *Sym;
4565 if (parseSymbol(Sym))
4566 return TokError("expected identifier in directive");
4567
4568 if (getLexer().isNot(AsmToken::Comma))
4569 return TokError("unexpected token in directive");
4570 Lex();
4571
4572 int64_t Size;
4573 SMLoc SizeLoc = getLexer().getLoc();
4574 if (parseAbsoluteExpression(Size))
4575 return true;
4576
4577 int64_t Pow2Alignment = 0;
4578 SMLoc Pow2AlignmentLoc;
4579 if (getLexer().is(AsmToken::Comma)) {
4580 Lex();
4581 Pow2AlignmentLoc = getLexer().getLoc();
4582 if (parseAbsoluteExpression(Pow2Alignment))
4583 return true;
4584
4585 LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType();
4586 if (IsLocal && LCOMM == LCOMM::NoAlignment)
4587 return Error(Pow2AlignmentLoc, "alignment not supported on this target");
4588
4589 // If this target takes alignments in bytes (not log) validate and convert.
4590 if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) ||
4591 (IsLocal && LCOMM == LCOMM::ByteAlignment)) {
4592 if (!isPowerOf2_64(Pow2Alignment))
4593 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
4594 Pow2Alignment = Log2_64(Pow2Alignment);
4595 }
4596 }
4597
4598 if (parseEOL())
4599 return true;
4600
4601 // NOTE: a size of zero for a .comm should create a undefined symbol
4602 // but a size of .lcomm creates a bss symbol of size zero.
4603 if (Size < 0)
4604 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
4605 "be less than zero");
4606
4607 // NOTE: The alignment in the directive is a power of 2 value, the assembler
4608 // may internally end up wanting an alignment in bytes.
4609 // FIXME: Diagnose overflow.
4610 if (Pow2Alignment < 0)
4611 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
4612 "alignment, can't be less than zero");
4613
4614 Sym->redefineIfPossible();
4615 if (!Sym->isUndefined())
4616 return Error(IDLoc, "invalid symbol redefinition");
4617
4618 // Create the Symbol as a common or local common with Size and Pow2Alignment.
4619 if (IsLocal) {
4620 getStreamer().emitLocalCommonSymbol(Sym, Size,
4621 Align(1ULL << Pow2Alignment));
4622 return false;
4623 }
4624
4625 getStreamer().emitCommonSymbol(Sym, Size, Align(1ULL << Pow2Alignment));
4626 return false;
4627}
4628
4629/// parseDirectiveComment
4630/// ::= comment delimiter [[text]]
4631/// [[text]]
4632/// [[text]] delimiter [[text]]
4633bool MasmParser::parseDirectiveComment(SMLoc DirectiveLoc) {
4634 std::string FirstLine = parseStringTo(AsmToken::EndOfStatement);
4635 size_t DelimiterEnd = FirstLine.find_first_of("\b\t\v\f\r\x1A ");
4636 assert(DelimiterEnd != std::string::npos);
4637 StringRef Delimiter = StringRef(FirstLine).take_front(DelimiterEnd);
4638 if (Delimiter.empty())
4639 return Error(DirectiveLoc, "no delimiter in 'comment' directive");
4640 do {
4641 if (getTok().is(AsmToken::Eof))
4642 return Error(DirectiveLoc, "unmatched delimiter in 'comment' directive");
4643 Lex(); // eat end of statement
4644 } while (
4645 !StringRef(parseStringTo(AsmToken::EndOfStatement)).contains(Delimiter));
4646 return parseEOL();
4647}
4648
4649/// parseDirectiveInclude
4650/// ::= include <filename>
4651/// | include filename
4652bool MasmParser::parseDirectiveInclude() {
4653 // Allow the strings to have escaped octal character sequence.
4654 std::string Filename;
4655 SMLoc IncludeLoc = getTok().getLoc();
4656
4657 if (parseAngleBracketString(Filename))
4658 Filename = parseStringTo(AsmToken::EndOfStatement);
4659 if (check(Filename.empty(), "missing filename in 'include' directive") ||
4660 check(getTok().isNot(AsmToken::EndOfStatement),
4661 "unexpected token in 'include' directive") ||
4662 // Attempt to switch the lexer to the included file before consuming the
4663 // end of statement to avoid losing it when we switch.
4664 check(enterIncludeFile(Filename), IncludeLoc,
4665 "Could not find include file '" + Filename + "'"))
4666 return true;
4667
4668 return false;
4669}
4670
4671/// parseDirectiveIf
4672/// ::= .if{,eq,ge,gt,le,lt,ne} expression
4673bool MasmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) {
4674 TheCondStack.push_back(TheCondState);
4675 TheCondState.TheCond = AsmCond::IfCond;
4676 if (TheCondState.Ignore) {
4677 eatToEndOfStatement();
4678 } else {
4679 int64_t ExprValue;
4680 if (parseAbsoluteExpression(ExprValue) || parseEOL())
4681 return true;
4682
4683 switch (DirKind) {
4684 default:
4685 llvm_unreachable("unsupported directive");
4686 case DK_IF:
4687 break;
4688 case DK_IFE:
4689 ExprValue = ExprValue == 0;
4690 break;
4691 }
4692
4693 TheCondState.CondMet = ExprValue;
4694 TheCondState.Ignore = !TheCondState.CondMet;
4695 }
4696
4697 return false;
4698}
4699
4700/// parseDirectiveIfb
4701/// ::= .ifb textitem
4702bool MasmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
4703 TheCondStack.push_back(TheCondState);
4704 TheCondState.TheCond = AsmCond::IfCond;
4705
4706 if (TheCondState.Ignore) {
4707 eatToEndOfStatement();
4708 } else {
4709 std::string Str;
4710 if (parseTextItem(Str))
4711 return TokError("expected text item parameter for 'ifb' directive");
4712
4713 if (parseEOL())
4714 return true;
4715
4716 TheCondState.CondMet = ExpectBlank == Str.empty();
4717 TheCondState.Ignore = !TheCondState.CondMet;
4718 }
4719
4720 return false;
4721}
4722
4723/// parseDirectiveIfidn
4724/// ::= ifidn textitem, textitem
4725bool MasmParser::parseDirectiveIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
4726 bool CaseInsensitive) {
4727 std::string String1, String2;
4728
4729 if (parseTextItem(String1)) {
4730 if (ExpectEqual)
4731 return TokError("expected text item parameter for 'ifidn' directive");
4732 return TokError("expected text item parameter for 'ifdif' directive");
4733 }
4734
4735 if (Lexer.isNot(AsmToken::Comma)) {
4736 if (ExpectEqual)
4737 return TokError(
4738 "expected comma after first string for 'ifidn' directive");
4739 return TokError("expected comma after first string for 'ifdif' directive");
4740 }
4741 Lex();
4742
4743 if (parseTextItem(String2)) {
4744 if (ExpectEqual)
4745 return TokError("expected text item parameter for 'ifidn' directive");
4746 return TokError("expected text item parameter for 'ifdif' directive");
4747 }
4748
4749 TheCondStack.push_back(TheCondState);
4750 TheCondState.TheCond = AsmCond::IfCond;
4751 if (CaseInsensitive)
4752 TheCondState.CondMet =
4753 ExpectEqual == (StringRef(String1).equals_insensitive(String2));
4754 else
4755 TheCondState.CondMet = ExpectEqual == (String1 == String2);
4756 TheCondState.Ignore = !TheCondState.CondMet;
4757
4758 return false;
4759}
4760
4761/// parseDirectiveIfdef
4762/// ::= ifdef symbol
4763/// | ifdef variable
4764bool MasmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) {
4765 TheCondStack.push_back(TheCondState);
4766 TheCondState.TheCond = AsmCond::IfCond;
4767
4768 if (TheCondState.Ignore) {
4769 eatToEndOfStatement();
4770 } else {
4771 bool is_defined = false;
4772 MCRegister Reg;
4773 SMLoc StartLoc, EndLoc;
4774 is_defined =
4775 getTargetParser().tryParseRegister(Reg, StartLoc, EndLoc).isSuccess();
4776 if (!is_defined) {
4777 StringRef Name;
4778 if (check(parseIdentifier(Name), "expected identifier after 'ifdef'") ||
4779 parseEOL())
4780 return true;
4781
4782 if (BuiltinSymbolMap.contains(Name.lower())) {
4783 is_defined = true;
4784 } else if (Variables.contains(Name.lower())) {
4785 is_defined = true;
4786 } else {
4787 MCSymbol *Sym = getContext().lookupSymbol(Name.lower());
4788 is_defined = (Sym && !Sym->isUndefined());
4789 }
4790 }
4791
4792 TheCondState.CondMet = (is_defined == expect_defined);
4793 TheCondState.Ignore = !TheCondState.CondMet;
4794 }
4795
4796 return false;
4797}
4798
4799/// parseDirectiveElseIf
4800/// ::= elseif expression
4801bool MasmParser::parseDirectiveElseIf(SMLoc DirectiveLoc,
4802 DirectiveKind DirKind) {
4803 if (TheCondState.TheCond != AsmCond::IfCond &&
4804 TheCondState.TheCond != AsmCond::ElseIfCond)
4805 return Error(DirectiveLoc, "Encountered a .elseif that doesn't follow an"
4806 " .if or an .elseif");
4807 TheCondState.TheCond = AsmCond::ElseIfCond;
4808
4809 bool LastIgnoreState = false;
4810 if (!TheCondStack.empty())
4811 LastIgnoreState = TheCondStack.back().Ignore;
4812 if (LastIgnoreState || TheCondState.CondMet) {
4813 TheCondState.Ignore = true;
4814 eatToEndOfStatement();
4815 } else {
4816 int64_t ExprValue;
4817 if (parseAbsoluteExpression(ExprValue))
4818 return true;
4819
4820 if (parseEOL())
4821 return true;
4822
4823 switch (DirKind) {
4824 default:
4825 llvm_unreachable("unsupported directive");
4826 case DK_ELSEIF:
4827 break;
4828 case DK_ELSEIFE:
4829 ExprValue = ExprValue == 0;
4830 break;
4831 }
4832
4833 TheCondState.CondMet = ExprValue;
4834 TheCondState.Ignore = !TheCondState.CondMet;
4835 }
4836
4837 return false;
4838}
4839
4840/// parseDirectiveElseIfb
4841/// ::= elseifb textitem
4842bool MasmParser::parseDirectiveElseIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
4843 if (TheCondState.TheCond != AsmCond::IfCond &&
4844 TheCondState.TheCond != AsmCond::ElseIfCond)
4845 return Error(DirectiveLoc, "Encountered an elseif that doesn't follow an"
4846 " if or an elseif");
4847 TheCondState.TheCond = AsmCond::ElseIfCond;
4848
4849 bool LastIgnoreState = false;
4850 if (!TheCondStack.empty())
4851 LastIgnoreState = TheCondStack.back().Ignore;
4852 if (LastIgnoreState || TheCondState.CondMet) {
4853 TheCondState.Ignore = true;
4854 eatToEndOfStatement();
4855 } else {
4856 std::string Str;
4857 if (parseTextItem(Str)) {
4858 if (ExpectBlank)
4859 return TokError("expected text item parameter for 'elseifb' directive");
4860 return TokError("expected text item parameter for 'elseifnb' directive");
4861 }
4862
4863 if (parseEOL())
4864 return true;
4865
4866 TheCondState.CondMet = ExpectBlank == Str.empty();
4867 TheCondState.Ignore = !TheCondState.CondMet;
4868 }
4869
4870 return false;
4871}
4872
4873/// parseDirectiveElseIfdef
4874/// ::= elseifdef symbol
4875/// | elseifdef variable
4876bool MasmParser::parseDirectiveElseIfdef(SMLoc DirectiveLoc,
4877 bool expect_defined) {
4878 if (TheCondState.TheCond != AsmCond::IfCond &&
4879 TheCondState.TheCond != AsmCond::ElseIfCond)
4880 return Error(DirectiveLoc, "Encountered an elseif that doesn't follow an"
4881 " if or an elseif");
4882 TheCondState.TheCond = AsmCond::ElseIfCond;
4883
4884 bool LastIgnoreState = false;
4885 if (!TheCondStack.empty())
4886 LastIgnoreState = TheCondStack.back().Ignore;
4887 if (LastIgnoreState || TheCondState.CondMet) {
4888 TheCondState.Ignore = true;
4889 eatToEndOfStatement();
4890 } else {
4891 bool is_defined = false;
4892 MCRegister Reg;
4893 SMLoc StartLoc, EndLoc;
4894 is_defined =
4895 getTargetParser().tryParseRegister(Reg, StartLoc, EndLoc).isSuccess();
4896 if (!is_defined) {
4897 StringRef Name;
4898 if (check(parseIdentifier(Name),
4899 "expected identifier after 'elseifdef'") ||
4900 parseEOL())
4901 return true;
4902
4903 if (BuiltinSymbolMap.contains(Name.lower())) {
4904 is_defined = true;
4905 } else if (Variables.contains(Name.lower())) {
4906 is_defined = true;
4907 } else {
4908 MCSymbol *Sym = getContext().lookupSymbol(Name);
4909 is_defined = (Sym && !Sym->isUndefined());
4910 }
4911 }
4912
4913 TheCondState.CondMet = (is_defined == expect_defined);
4914 TheCondState.Ignore = !TheCondState.CondMet;
4915 }
4916
4917 return false;
4918}
4919
4920/// parseDirectiveElseIfidn
4921/// ::= elseifidn textitem, textitem
4922bool MasmParser::parseDirectiveElseIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
4923 bool CaseInsensitive) {
4924 if (TheCondState.TheCond != AsmCond::IfCond &&
4925 TheCondState.TheCond != AsmCond::ElseIfCond)
4926 return Error(DirectiveLoc, "Encountered an elseif that doesn't follow an"
4927 " if or an elseif");
4928 TheCondState.TheCond = AsmCond::ElseIfCond;
4929
4930 bool LastIgnoreState = false;
4931 if (!TheCondStack.empty())
4932 LastIgnoreState = TheCondStack.back().Ignore;
4933 if (LastIgnoreState || TheCondState.CondMet) {
4934 TheCondState.Ignore = true;
4935 eatToEndOfStatement();
4936 } else {
4937 std::string String1, String2;
4938
4939 if (parseTextItem(String1)) {
4940 if (ExpectEqual)
4941 return TokError(
4942 "expected text item parameter for 'elseifidn' directive");
4943 return TokError("expected text item parameter for 'elseifdif' directive");
4944 }
4945
4946 if (Lexer.isNot(AsmToken::Comma)) {
4947 if (ExpectEqual)
4948 return TokError(
4949 "expected comma after first string for 'elseifidn' directive");
4950 return TokError(
4951 "expected comma after first string for 'elseifdif' directive");
4952 }
4953 Lex();
4954
4955 if (parseTextItem(String2)) {
4956 if (ExpectEqual)
4957 return TokError(
4958 "expected text item parameter for 'elseifidn' directive");
4959 return TokError("expected text item parameter for 'elseifdif' directive");
4960 }
4961
4962 if (CaseInsensitive)
4963 TheCondState.CondMet =
4964 ExpectEqual == (StringRef(String1).equals_insensitive(String2));
4965 else
4966 TheCondState.CondMet = ExpectEqual == (String1 == String2);
4967 TheCondState.Ignore = !TheCondState.CondMet;
4968 }
4969
4970 return false;
4971}
4972
4973/// parseDirectiveElse
4974/// ::= else
4975bool MasmParser::parseDirectiveElse(SMLoc DirectiveLoc) {
4976 if (parseEOL())
4977 return true;
4978
4979 if (TheCondState.TheCond != AsmCond::IfCond &&
4980 TheCondState.TheCond != AsmCond::ElseIfCond)
4981 return Error(DirectiveLoc, "Encountered an else that doesn't follow an if"
4982 " or an elseif");
4983 TheCondState.TheCond = AsmCond::ElseCond;
4984 bool LastIgnoreState = false;
4985 if (!TheCondStack.empty())
4986 LastIgnoreState = TheCondStack.back().Ignore;
4987 if (LastIgnoreState || TheCondState.CondMet)
4988 TheCondState.Ignore = true;
4989 else
4990 TheCondState.Ignore = false;
4991
4992 return false;
4993}
4994
4995/// parseDirectiveEnd
4996/// ::= end
4997bool MasmParser::parseDirectiveEnd(SMLoc DirectiveLoc) {
4998 if (parseEOL())
4999 return true;
5000
5001 while (Lexer.isNot(AsmToken::Eof))
5002 Lexer.Lex();
5003
5004 return false;
5005}
5006
5007/// parseDirectiveError
5008/// ::= .err [message]
5009bool MasmParser::parseDirectiveError(SMLoc DirectiveLoc) {
5010 if (!TheCondStack.empty()) {
5011 if (TheCondStack.back().Ignore) {
5012 eatToEndOfStatement();
5013 return false;
5014 }
5015 }
5016
5017 std::string Message = ".err directive invoked in source file";
5018 if (Lexer.isNot(AsmToken::EndOfStatement))
5019 Message = parseStringTo(AsmToken::EndOfStatement);
5020 Lex();
5021
5022 return Error(DirectiveLoc, Message);
5023}
5024
5025/// parseDirectiveErrorIfb
5026/// ::= .errb textitem[, message]
5027bool MasmParser::parseDirectiveErrorIfb(SMLoc DirectiveLoc, bool ExpectBlank) {
5028 if (!TheCondStack.empty()) {
5029 if (TheCondStack.back().Ignore) {
5030 eatToEndOfStatement();
5031 return false;
5032 }
5033 }
5034
5035 std::string Text;
5036 if (parseTextItem(Text))
5037 return Error(getTok().getLoc(), "missing text item in '.errb' directive");
5038
5039 std::string Message = ".errb directive invoked in source file";
5040 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5041 if (parseToken(AsmToken::Comma))
5042 return addErrorSuffix(" in '.errb' directive");
5043 Message = parseStringTo(AsmToken::EndOfStatement);
5044 }
5045 Lex();
5046
5047 if (Text.empty() == ExpectBlank)
5048 return Error(DirectiveLoc, Message);
5049 return false;
5050}
5051
5052/// parseDirectiveErrorIfdef
5053/// ::= .errdef name[, message]
5054bool MasmParser::parseDirectiveErrorIfdef(SMLoc DirectiveLoc,
5055 bool ExpectDefined) {
5056 if (!TheCondStack.empty()) {
5057 if (TheCondStack.back().Ignore) {
5058 eatToEndOfStatement();
5059 return false;
5060 }
5061 }
5062
5063 bool IsDefined = false;
5064 MCRegister Reg;
5065 SMLoc StartLoc, EndLoc;
5066 IsDefined =
5067 getTargetParser().tryParseRegister(Reg, StartLoc, EndLoc).isSuccess();
5068 if (!IsDefined) {
5069 StringRef Name;
5070 if (check(parseIdentifier(Name), "expected identifier after '.errdef'"))
5071 return true;
5072
5073 if (BuiltinSymbolMap.contains(Name.lower())) {
5074 IsDefined = true;
5075 } else if (Variables.contains(Name.lower())) {
5076 IsDefined = true;
5077 } else {
5078 MCSymbol *Sym = getContext().lookupSymbol(Name);
5079 IsDefined = (Sym && !Sym->isUndefined());
5080 }
5081 }
5082
5083 std::string Message = ".errdef directive invoked in source file";
5084 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5085 if (parseToken(AsmToken::Comma))
5086 return addErrorSuffix(" in '.errdef' directive");
5087 Message = parseStringTo(AsmToken::EndOfStatement);
5088 }
5089 Lex();
5090
5091 if (IsDefined == ExpectDefined)
5092 return Error(DirectiveLoc, Message);
5093 return false;
5094}
5095
5096/// parseDirectiveErrorIfidn
5097/// ::= .erridn textitem, textitem[, message]
5098bool MasmParser::parseDirectiveErrorIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
5099 bool CaseInsensitive) {
5100 if (!TheCondStack.empty()) {
5101 if (TheCondStack.back().Ignore) {
5102 eatToEndOfStatement();
5103 return false;
5104 }
5105 }
5106
5107 std::string String1, String2;
5108
5109 if (parseTextItem(String1)) {
5110 if (ExpectEqual)
5111 return TokError("expected string parameter for '.erridn' directive");
5112 return TokError("expected string parameter for '.errdif' directive");
5113 }
5114
5115 if (Lexer.isNot(AsmToken::Comma)) {
5116 if (ExpectEqual)
5117 return TokError(
5118 "expected comma after first string for '.erridn' directive");
5119 return TokError(
5120 "expected comma after first string for '.errdif' directive");
5121 }
5122 Lex();
5123
5124 if (parseTextItem(String2)) {
5125 if (ExpectEqual)
5126 return TokError("expected string parameter for '.erridn' directive");
5127 return TokError("expected string parameter for '.errdif' directive");
5128 }
5129
5130 std::string Message;
5131 if (ExpectEqual)
5132 Message = ".erridn directive invoked in source file";
5133 else
5134 Message = ".errdif directive invoked in source file";
5135 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5136 if (parseToken(AsmToken::Comma))
5137 return addErrorSuffix(" in '.erridn' directive");
5138 Message = parseStringTo(AsmToken::EndOfStatement);
5139 }
5140 Lex();
5141
5142 if (CaseInsensitive)
5143 TheCondState.CondMet =
5144 ExpectEqual == (StringRef(String1).equals_insensitive(String2));
5145 else
5146 TheCondState.CondMet = ExpectEqual == (String1 == String2);
5147 TheCondState.Ignore = !TheCondState.CondMet;
5148
5149 if ((CaseInsensitive &&
5150 ExpectEqual == StringRef(String1).equals_insensitive(String2)) ||
5151 (ExpectEqual == (String1 == String2)))
5152 return Error(DirectiveLoc, Message);
5153 return false;
5154}
5155
5156/// parseDirectiveErrorIfe
5157/// ::= .erre expression[, message]
5158bool MasmParser::parseDirectiveErrorIfe(SMLoc DirectiveLoc, bool ExpectZero) {
5159 if (!TheCondStack.empty()) {
5160 if (TheCondStack.back().Ignore) {
5161 eatToEndOfStatement();
5162 return false;
5163 }
5164 }
5165
5166 int64_t ExprValue;
5167 if (parseAbsoluteExpression(ExprValue))
5168 return addErrorSuffix(" in '.erre' directive");
5169
5170 std::string Message = ".erre directive invoked in source file";
5171 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5172 if (parseToken(AsmToken::Comma))
5173 return addErrorSuffix(" in '.erre' directive");
5174 Message = parseStringTo(AsmToken::EndOfStatement);
5175 }
5176 Lex();
5177
5178 if ((ExprValue == 0) == ExpectZero)
5179 return Error(DirectiveLoc, Message);
5180 return false;
5181}
5182
5183/// parseDirectiveEndIf
5184/// ::= .endif
5185bool MasmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) {
5186 if (parseEOL())
5187 return true;
5188
5189 if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty())
5190 return Error(DirectiveLoc, "Encountered a .endif that doesn't follow "
5191 "an .if or .else");
5192 if (!TheCondStack.empty()) {
5193 TheCondState = TheCondStack.back();
5194 TheCondStack.pop_back();
5195 }
5196
5197 return false;
5198}
5199
5200void MasmParser::initializeDirectiveKindMap() {
5201 DirectiveKindMap["="] = DK_ASSIGN;
5202 DirectiveKindMap["equ"] = DK_EQU;
5203 DirectiveKindMap["textequ"] = DK_TEXTEQU;
5204 // DirectiveKindMap[".ascii"] = DK_ASCII;
5205 // DirectiveKindMap[".asciz"] = DK_ASCIZ;
5206 // DirectiveKindMap[".string"] = DK_STRING;
5207 DirectiveKindMap["byte"] = DK_BYTE;
5208 DirectiveKindMap["sbyte"] = DK_SBYTE;
5209 DirectiveKindMap["word"] = DK_WORD;
5210 DirectiveKindMap["sword"] = DK_SWORD;
5211 DirectiveKindMap["dword"] = DK_DWORD;
5212 DirectiveKindMap["sdword"] = DK_SDWORD;
5213 DirectiveKindMap["fword"] = DK_FWORD;
5214 DirectiveKindMap["qword"] = DK_QWORD;
5215 DirectiveKindMap["sqword"] = DK_SQWORD;
5216 DirectiveKindMap["real4"] = DK_REAL4;
5217 DirectiveKindMap["real8"] = DK_REAL8;
5218 DirectiveKindMap["real10"] = DK_REAL10;
5219 DirectiveKindMap["align"] = DK_ALIGN;
5220 DirectiveKindMap["even"] = DK_EVEN;
5221 DirectiveKindMap["org"] = DK_ORG;
5222 DirectiveKindMap["extern"] = DK_EXTERN;
5223 DirectiveKindMap["extrn"] = DK_EXTERN;
5224 DirectiveKindMap["public"] = DK_PUBLIC;
5225 // DirectiveKindMap[".comm"] = DK_COMM;
5226 DirectiveKindMap["comment"] = DK_COMMENT;
5227 DirectiveKindMap["include"] = DK_INCLUDE;
5228 DirectiveKindMap["repeat"] = DK_REPEAT;
5229 DirectiveKindMap["rept"] = DK_REPEAT;
5230 DirectiveKindMap["while"] = DK_WHILE;
5231 DirectiveKindMap["for"] = DK_FOR;
5232 DirectiveKindMap["irp"] = DK_FOR;
5233 DirectiveKindMap["forc"] = DK_FORC;
5234 DirectiveKindMap["irpc"] = DK_FORC;
5235 DirectiveKindMap["if"] = DK_IF;
5236 DirectiveKindMap["ife"] = DK_IFE;
5237 DirectiveKindMap["ifb"] = DK_IFB;
5238 DirectiveKindMap["ifnb"] = DK_IFNB;
5239 DirectiveKindMap["ifdef"] = DK_IFDEF;
5240 DirectiveKindMap["ifndef"] = DK_IFNDEF;
5241 DirectiveKindMap["ifdif"] = DK_IFDIF;
5242 DirectiveKindMap["ifdifi"] = DK_IFDIFI;
5243 DirectiveKindMap["ifidn"] = DK_IFIDN;
5244 DirectiveKindMap["ifidni"] = DK_IFIDNI;
5245 DirectiveKindMap["elseif"] = DK_ELSEIF;
5246 DirectiveKindMap["elseifdef"] = DK_ELSEIFDEF;
5247 DirectiveKindMap["elseifndef"] = DK_ELSEIFNDEF;
5248 DirectiveKindMap["elseifdif"] = DK_ELSEIFDIF;
5249 DirectiveKindMap["elseifidn"] = DK_ELSEIFIDN;
5250 DirectiveKindMap["else"] = DK_ELSE;
5251 DirectiveKindMap["end"] = DK_END;
5252 DirectiveKindMap["endif"] = DK_ENDIF;
5253 // DirectiveKindMap[".file"] = DK_FILE;
5254 // DirectiveKindMap[".line"] = DK_LINE;
5255 // DirectiveKindMap[".loc"] = DK_LOC;
5256 // DirectiveKindMap[".stabs"] = DK_STABS;
5257 // DirectiveKindMap[".cv_file"] = DK_CV_FILE;
5258 // DirectiveKindMap[".cv_func_id"] = DK_CV_FUNC_ID;
5259 // DirectiveKindMap[".cv_loc"] = DK_CV_LOC;
5260 // DirectiveKindMap[".cv_linetable"] = DK_CV_LINETABLE;
5261 // DirectiveKindMap[".cv_inline_linetable"] = DK_CV_INLINE_LINETABLE;
5262 // DirectiveKindMap[".cv_inline_site_id"] = DK_CV_INLINE_SITE_ID;
5263 // DirectiveKindMap[".cv_def_range"] = DK_CV_DEF_RANGE;
5264 // DirectiveKindMap[".cv_string"] = DK_CV_STRING;
5265 // DirectiveKindMap[".cv_stringtable"] = DK_CV_STRINGTABLE;
5266 // DirectiveKindMap[".cv_filechecksums"] = DK_CV_FILECHECKSUMS;
5267 // DirectiveKindMap[".cv_filechecksumoffset"] = DK_CV_FILECHECKSUM_OFFSET;
5268 // DirectiveKindMap[".cv_fpo_data"] = DK_CV_FPO_DATA;
5269 // DirectiveKindMap[".cfi_sections"] = DK_CFI_SECTIONS;
5270 // DirectiveKindMap[".cfi_startproc"] = DK_CFI_STARTPROC;
5271 // DirectiveKindMap[".cfi_endproc"] = DK_CFI_ENDPROC;
5272 // DirectiveKindMap[".cfi_def_cfa"] = DK_CFI_DEF_CFA;
5273 // DirectiveKindMap[".cfi_def_cfa_offset"] = DK_CFI_DEF_CFA_OFFSET;
5274 // DirectiveKindMap[".cfi_adjust_cfa_offset"] = DK_CFI_ADJUST_CFA_OFFSET;
5275 // DirectiveKindMap[".cfi_def_cfa_register"] = DK_CFI_DEF_CFA_REGISTER;
5276 // DirectiveKindMap[".cfi_offset"] = DK_CFI_OFFSET;
5277 // DirectiveKindMap[".cfi_rel_offset"] = DK_CFI_REL_OFFSET;
5278 // DirectiveKindMap[".cfi_personality"] = DK_CFI_PERSONALITY;
5279 // DirectiveKindMap[".cfi_lsda"] = DK_CFI_LSDA;
5280 // DirectiveKindMap[".cfi_remember_state"] = DK_CFI_REMEMBER_STATE;
5281 // DirectiveKindMap[".cfi_restore_state"] = DK_CFI_RESTORE_STATE;
5282 // DirectiveKindMap[".cfi_same_value"] = DK_CFI_SAME_VALUE;
5283 // DirectiveKindMap[".cfi_restore"] = DK_CFI_RESTORE;
5284 // DirectiveKindMap[".cfi_escape"] = DK_CFI_ESCAPE;
5285 // DirectiveKindMap[".cfi_return_column"] = DK_CFI_RETURN_COLUMN;
5286 // DirectiveKindMap[".cfi_signal_frame"] = DK_CFI_SIGNAL_FRAME;
5287 // DirectiveKindMap[".cfi_undefined"] = DK_CFI_UNDEFINED;
5288 // DirectiveKindMap[".cfi_register"] = DK_CFI_REGISTER;
5289 // DirectiveKindMap[".cfi_window_save"] = DK_CFI_WINDOW_SAVE;
5290 // DirectiveKindMap[".cfi_b_key_frame"] = DK_CFI_B_KEY_FRAME;
5291 // DirectiveKindMap[".cfi_val_offset"] = DK_CFI_VAL_OFFSET;
5292 DirectiveKindMap["macro"] = DK_MACRO;
5293 DirectiveKindMap["exitm"] = DK_EXITM;
5294 DirectiveKindMap["endm"] = DK_ENDM;
5295 DirectiveKindMap["purge"] = DK_PURGE;
5296 DirectiveKindMap[".err"] = DK_ERR;
5297 DirectiveKindMap[".errb"] = DK_ERRB;
5298 DirectiveKindMap[".errnb"] = DK_ERRNB;
5299 DirectiveKindMap[".errdef"] = DK_ERRDEF;
5300 DirectiveKindMap[".errndef"] = DK_ERRNDEF;
5301 DirectiveKindMap[".errdif"] = DK_ERRDIF;
5302 DirectiveKindMap[".errdifi"] = DK_ERRDIFI;
5303 DirectiveKindMap[".erridn"] = DK_ERRIDN;
5304 DirectiveKindMap[".erridni"] = DK_ERRIDNI;
5305 DirectiveKindMap[".erre"] = DK_ERRE;
5306 DirectiveKindMap[".errnz"] = DK_ERRNZ;
5307 DirectiveKindMap[".pushframe"] = DK_PUSHFRAME;
5308 DirectiveKindMap[".pushreg"] = DK_PUSHREG;
5309 DirectiveKindMap[".savereg"] = DK_SAVEREG;
5310 DirectiveKindMap[".savexmm128"] = DK_SAVEXMM128;
5311 DirectiveKindMap[".setframe"] = DK_SETFRAME;
5312 DirectiveKindMap[".radix"] = DK_RADIX;
5313 DirectiveKindMap["db"] = DK_DB;
5314 DirectiveKindMap["dd"] = DK_DD;
5315 DirectiveKindMap["df"] = DK_DF;
5316 DirectiveKindMap["dq"] = DK_DQ;
5317 DirectiveKindMap["dw"] = DK_DW;
5318 DirectiveKindMap["echo"] = DK_ECHO;
5319 DirectiveKindMap["struc"] = DK_STRUCT;
5320 DirectiveKindMap["struct"] = DK_STRUCT;
5321 DirectiveKindMap["union"] = DK_UNION;
5322 DirectiveKindMap["ends"] = DK_ENDS;
5323}
5324
5325bool MasmParser::isMacroLikeDirective() {
5326 if (getLexer().is(AsmToken::Identifier)) {
5327 bool IsMacroLike = StringSwitch<bool>(getTok().getIdentifier())
5328 .CasesLower("repeat", "rept", true)
5329 .CaseLower("while", true)
5330 .CasesLower("for", "irp", true)
5331 .CasesLower("forc", "irpc", true)
5332 .Default(false);
5333 if (IsMacroLike)
5334 return true;
5335 }
5336 if (peekTok().is(AsmToken::Identifier) &&
5337 peekTok().getIdentifier().equals_insensitive("macro"))
5338 return true;
5339
5340 return false;
5341}
5342
5343MCAsmMacro *MasmParser::parseMacroLikeBody(SMLoc DirectiveLoc) {
5344 AsmToken EndToken, StartToken = getTok();
5345
5346 unsigned NestLevel = 0;
5347 while (true) {
5348 // Check whether we have reached the end of the file.
5349 if (getLexer().is(AsmToken::Eof)) {
5350 printError(DirectiveLoc, "no matching 'endm' in definition");
5351 return nullptr;
5352 }
5353
5354 if (isMacroLikeDirective())
5355 ++NestLevel;
5356
5357 // Otherwise, check whether we have reached the endm.
5358 if (Lexer.is(AsmToken::Identifier) &&
5359 getTok().getIdentifier().equals_insensitive("endm")) {
5360 if (NestLevel == 0) {
5361 EndToken = getTok();
5362 Lex();
5363 if (Lexer.isNot(AsmToken::EndOfStatement)) {
5364 printError(getTok().getLoc(), "unexpected token in 'endm' directive");
5365 return nullptr;
5366 }
5367 break;
5368 }
5369 --NestLevel;
5370 }
5371
5372 // Otherwise, scan till the end of the statement.
5373 eatToEndOfStatement();
5374 }
5375
5376 const char *BodyStart = StartToken.getLoc().getPointer();
5377 const char *BodyEnd = EndToken.getLoc().getPointer();
5378 StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart);
5379
5380 // We Are Anonymous.
5381 MacroLikeBodies.emplace_back(StringRef(), Body, MCAsmMacroParameters());
5382 return &MacroLikeBodies.back();
5383}
5384
5385bool MasmParser::expandStatement(SMLoc Loc) {
5386 std::string Body = parseStringTo(AsmToken::EndOfStatement);
5387 SMLoc EndLoc = getTok().getLoc();
5388
5390 MCAsmMacroArguments Arguments;
5391
5392 StringMap<std::string> BuiltinValues;
5393 for (const auto &S : BuiltinSymbolMap) {
5394 const BuiltinSymbol &Sym = S.getValue();
5395 if (std::optional<std::string> Text = evaluateBuiltinTextMacro(Sym, Loc)) {
5396 BuiltinValues[S.getKey().lower()] = std::move(*Text);
5397 }
5398 }
5399 for (const auto &B : BuiltinValues) {
5400 MCAsmMacroParameter P;
5401 MCAsmMacroArgument A;
5402 P.Name = B.getKey();
5403 P.Required = true;
5404 A.push_back(AsmToken(AsmToken::String, B.getValue()));
5405
5406 Parameters.push_back(std::move(P));
5407 Arguments.push_back(std::move(A));
5408 }
5409
5410 for (const auto &V : Variables) {
5411 const Variable &Var = V.getValue();
5412 if (Var.IsText) {
5413 MCAsmMacroParameter P;
5414 MCAsmMacroArgument A;
5415 P.Name = Var.Name;
5416 P.Required = true;
5417 A.push_back(AsmToken(AsmToken::String, Var.TextValue));
5418
5419 Parameters.push_back(std::move(P));
5420 Arguments.push_back(std::move(A));
5421 }
5422 }
5423 MacroLikeBodies.emplace_back(StringRef(), Body, Parameters);
5424 MCAsmMacro M = MacroLikeBodies.back();
5425
5426 // Expand the statement in a new buffer.
5427 SmallString<80> Buf;
5428 raw_svector_ostream OS(Buf);
5429 if (expandMacro(OS, M.Body, M.Parameters, Arguments, M.Locals, EndLoc))
5430 return true;
5431 std::unique_ptr<MemoryBuffer> Expansion =
5432 MemoryBuffer::getMemBufferCopy(OS.str(), "<expansion>");
5433
5434 // Jump to the expanded statement and prime the lexer.
5435 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Expansion), EndLoc);
5436 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
5437 EndStatementAtEOFStack.push_back(false);
5438 Lex();
5439 return false;
5440}
5441
5442void MasmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
5443 raw_svector_ostream &OS) {
5444 instantiateMacroLikeBody(M, DirectiveLoc, /*ExitLoc=*/getTok().getLoc(), OS);
5445}
5446void MasmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc,
5447 SMLoc ExitLoc,
5448 raw_svector_ostream &OS) {
5449 OS << "endm\n";
5450
5451 std::unique_ptr<MemoryBuffer> Instantiation =
5452 MemoryBuffer::getMemBufferCopy(OS.str(), "<instantiation>");
5453
5454 // Create the macro instantiation object and add to the current macro
5455 // instantiation stack.
5456 MacroInstantiation *MI = new MacroInstantiation{DirectiveLoc, CurBuffer,
5457 ExitLoc, TheCondStack.size()};
5458 ActiveMacros.push_back(MI);
5459
5460 // Jump to the macro instantiation and prime the lexer.
5461 CurBuffer = SrcMgr.AddNewSourceBuffer(std::move(Instantiation), SMLoc());
5462 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer)->getBuffer());
5463 EndStatementAtEOFStack.push_back(true);
5464 Lex();
5465}
5466
5467/// parseDirectiveRepeat
5468/// ::= ("repeat" | "rept") count
5469/// body
5470/// endm
5471bool MasmParser::parseDirectiveRepeat(SMLoc DirectiveLoc, StringRef Dir) {
5472 const MCExpr *CountExpr;
5473 SMLoc CountLoc = getTok().getLoc();
5474 if (parseExpression(CountExpr))
5475 return true;
5476
5477 int64_t Count;
5478 if (!CountExpr->evaluateAsAbsolute(Count, getStreamer().getAssemblerPtr())) {
5479 return Error(CountLoc, "unexpected token in '" + Dir + "' directive");
5480 }
5481
5482 if (check(Count < 0, CountLoc, "Count is negative") || parseEOL())
5483 return true;
5484
5485 // Lex the repeat definition.
5486 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5487 if (!M)
5488 return true;
5489
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);
5494 while (Count--) {
5495 if (expandMacro(OS, M->Body, {}, {}, M->Locals, getTok().getLoc()))
5496 return true;
5497 }
5498 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5499
5500 return false;
5501}
5502
5503/// parseDirectiveWhile
5504/// ::= "while" expression
5505/// body
5506/// endm
5507bool MasmParser::parseDirectiveWhile(SMLoc DirectiveLoc) {
5508 const MCExpr *CondExpr;
5509 SMLoc CondLoc = getTok().getLoc();
5510 if (parseExpression(CondExpr))
5511 return true;
5512
5513 // Lex the repeat definition.
5514 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5515 if (!M)
5516 return true;
5517
5518 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5519 // to hold the macro body with substitutions.
5520 SmallString<256> Buf;
5521 raw_svector_ostream OS(Buf);
5522 int64_t Condition;
5523 if (!CondExpr->evaluateAsAbsolute(Condition, getStreamer().getAssemblerPtr()))
5524 return Error(CondLoc, "expected absolute expression in 'while' directive");
5525 if (Condition) {
5526 // Instantiate the macro, then resume at this directive to recheck the
5527 // condition.
5528 if (expandMacro(OS, M->Body, {}, {}, M->Locals, getTok().getLoc()))
5529 return true;
5530 instantiateMacroLikeBody(M, DirectiveLoc, /*ExitLoc=*/DirectiveLoc, OS);
5531 }
5532
5533 return false;
5534}
5535
5536/// parseDirectiveFor
5537/// ::= ("for" | "irp") symbol [":" qualifier], <values>
5538/// body
5539/// endm
5540bool MasmParser::parseDirectiveFor(SMLoc DirectiveLoc, StringRef Dir) {
5541 MCAsmMacroParameter Parameter;
5542 MCAsmMacroArguments A;
5543 if (check(parseIdentifier(Parameter.Name),
5544 "expected identifier in '" + Dir + "' directive"))
5545 return true;
5546
5547 // Parse optional qualifier (default value, or "req")
5548 if (parseOptionalToken(AsmToken::Colon)) {
5549 if (parseOptionalToken(AsmToken::Equal)) {
5550 // Default value
5551 SMLoc ParamLoc;
5552
5553 ParamLoc = Lexer.getLoc();
5554 if (parseMacroArgument(nullptr, Parameter.Value))
5555 return true;
5556 } else {
5557 SMLoc QualLoc;
5558 StringRef Qualifier;
5559
5560 QualLoc = Lexer.getLoc();
5561 if (parseIdentifier(Qualifier))
5562 return Error(QualLoc, "missing parameter qualifier for "
5563 "'" +
5564 Parameter.Name + "' in '" + Dir +
5565 "' directive");
5566
5567 if (Qualifier.equals_insensitive("req"))
5568 Parameter.Required = true;
5569 else
5570 return Error(QualLoc,
5571 Qualifier + " is not a valid parameter qualifier for '" +
5572 Parameter.Name + "' in '" + Dir + "' directive");
5573 }
5574 }
5575
5576 if (parseToken(AsmToken::Comma,
5577 "expected comma in '" + Dir + "' directive") ||
5578 parseToken(AsmToken::Less,
5579 "values in '" + Dir +
5580 "' directive must be enclosed in angle brackets"))
5581 return true;
5582
5583 while (true) {
5584 A.emplace_back();
5585 if (parseMacroArgument(&Parameter, A.back(), /*EndTok=*/AsmToken::Greater))
5586 return addErrorSuffix(" in arguments for '" + Dir + "' directive");
5587
5588 // If we see a comma, continue, and allow line continuation.
5589 if (!parseOptionalToken(AsmToken::Comma))
5590 break;
5591 parseOptionalToken(AsmToken::EndOfStatement);
5592 }
5593
5594 if (parseToken(AsmToken::Greater,
5595 "values in '" + Dir +
5596 "' directive must be enclosed in angle brackets") ||
5597 parseEOL())
5598 return true;
5599
5600 // Lex the for definition.
5601 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5602 if (!M)
5603 return true;
5604
5605 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5606 // to hold the macro body with substitutions.
5607 SmallString<256> Buf;
5608 raw_svector_ostream OS(Buf);
5609
5610 for (const MCAsmMacroArgument &Arg : A) {
5611 if (expandMacro(OS, M->Body, Parameter, Arg, M->Locals, getTok().getLoc()))
5612 return true;
5613 }
5614
5615 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5616
5617 return false;
5618}
5619
5620/// parseDirectiveForc
5621/// ::= ("forc" | "irpc") symbol, <string>
5622/// body
5623/// endm
5624bool MasmParser::parseDirectiveForc(SMLoc DirectiveLoc, StringRef Directive) {
5625 MCAsmMacroParameter Parameter;
5626
5627 std::string Argument;
5628 if (check(parseIdentifier(Parameter.Name),
5629 "expected identifier in '" + Directive + "' directive") ||
5630 parseToken(AsmToken::Comma,
5631 "expected comma in '" + Directive + "' directive"))
5632 return true;
5633 if (parseAngleBracketString(Argument)) {
5634 // Match ml64.exe; treat all characters to end of statement as a string,
5635 // ignoring comment markers, then discard anything following a space (using
5636 // the C locale).
5637 Argument = parseStringTo(AsmToken::EndOfStatement);
5638 if (getTok().is(AsmToken::EndOfStatement))
5639 Argument += getTok().getString();
5640 size_t End = 0;
5641 for (; End < Argument.size(); ++End) {
5642 if (isSpace(Argument[End]))
5643 break;
5644 }
5645 Argument.resize(End);
5646 }
5647 if (parseEOL())
5648 return true;
5649
5650 // Lex the irpc definition.
5651 MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc);
5652 if (!M)
5653 return true;
5654
5655 // Macro instantiation is lexical, unfortunately. We construct a new buffer
5656 // to hold the macro body with substitutions.
5657 SmallString<256> Buf;
5658 raw_svector_ostream OS(Buf);
5659
5660 StringRef Values(Argument);
5661 for (std::size_t I = 0, End = Values.size(); I != End; ++I) {
5662 MCAsmMacroArgument Arg;
5663 Arg.emplace_back(AsmToken::Identifier, Values.substr(I, 1));
5664
5665 if (expandMacro(OS, M->Body, Parameter, Arg, M->Locals, getTok().getLoc()))
5666 return true;
5667 }
5668
5669 instantiateMacroLikeBody(M, DirectiveLoc, OS);
5670
5671 return false;
5672}
5673
5674bool MasmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info,
5675 size_t Len) {
5676 const MCExpr *Value;
5677 SMLoc ExprLoc = getLexer().getLoc();
5678 if (parseExpression(Value))
5679 return true;
5680 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5681 if (!MCE)
5682 return Error(ExprLoc, "unexpected expression in _emit");
5683 uint64_t IntValue = MCE->getValue();
5684 if (!isUInt<8>(IntValue) && !isInt<8>(IntValue))
5685 return Error(ExprLoc, "literal value out of range for directive");
5686
5687 Info.AsmRewrites->emplace_back(AOK_Emit, IDLoc, Len);
5688 return false;
5689}
5690
5691bool MasmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) {
5692 const MCExpr *Value;
5693 SMLoc ExprLoc = getLexer().getLoc();
5694 if (parseExpression(Value))
5695 return true;
5696 const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value);
5697 if (!MCE)
5698 return Error(ExprLoc, "unexpected expression in align");
5699 uint64_t IntValue = MCE->getValue();
5700 if (!isPowerOf2_64(IntValue))
5701 return Error(ExprLoc, "literal value not a power of two greater then zero");
5702
5703 Info.AsmRewrites->emplace_back(AOK_Align, IDLoc, 5, Log2_64(IntValue));
5704 return false;
5705}
5706
5707bool MasmParser::parseDirectiveRadix(SMLoc DirectiveLoc) {
5708 const SMLoc Loc = getLexer().getLoc();
5709 std::string RadixStringRaw = parseStringTo(AsmToken::EndOfStatement);
5710 StringRef RadixString = StringRef(RadixStringRaw).trim();
5711 unsigned Radix;
5712 if (RadixString.getAsInteger(10, Radix)) {
5713 return Error(Loc,
5714 "radix must be a decimal number in the range 2 to 16; was " +
5715 RadixString);
5716 }
5717 if (Radix < 2 || Radix > 16)
5718 return Error(Loc, "radix must be in the range 2 to 16; was " +
5719 std::to_string(Radix));
5720 getLexer().setMasmDefaultRadix(Radix);
5721 return false;
5722}
5723
5724/// parseDirectiveEcho
5725/// ::= "echo" message
5726bool MasmParser::parseDirectiveEcho(SMLoc DirectiveLoc) {
5727 std::string Message = parseStringTo(AsmToken::EndOfStatement);
5728 llvm::outs() << Message;
5729 if (!StringRef(Message).ends_with("\n"))
5730 llvm::outs() << '\n';
5731 return false;
5732}
5733
5734// We are comparing pointers, but the pointers are relative to a single string.
5735// Thus, this should always be deterministic.
5736static int rewritesSort(const AsmRewrite *AsmRewriteA,
5737 const AsmRewrite *AsmRewriteB) {
5738 if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer())
5739 return -1;
5740 if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer())
5741 return 1;
5742
5743 // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output
5744 // rewrite to the same location. Make sure the SizeDirective rewrite is
5745 // performed first, then the Imm/ImmPrefix and finally the Input/Output. This
5746 // ensures the sort algorithm is stable.
5747 if (AsmRewritePrecedence[AsmRewriteA->Kind] >
5748 AsmRewritePrecedence[AsmRewriteB->Kind])
5749 return -1;
5750
5751 if (AsmRewritePrecedence[AsmRewriteA->Kind] <
5752 AsmRewritePrecedence[AsmRewriteB->Kind])
5753 return 1;
5754 llvm_unreachable("Unstable rewrite sort.");
5755}
5756
5757bool MasmParser::defineMacro(StringRef Name, StringRef Value) {
5758 Variable &Var = Variables[Name.lower()];
5759 if (Var.Name.empty()) {
5760 Var.Name = Name;
5761 } else if (Var.Redefinable == Variable::NOT_REDEFINABLE) {
5762 return Error(SMLoc(), "invalid variable redefinition");
5763 } else if (Var.Redefinable == Variable::WARN_ON_REDEFINITION &&
5764 Warning(SMLoc(), "redefining '" + Name +
5765 "', already defined on the command line")) {
5766 return true;
5767 }
5768 Var.Redefinable = Variable::WARN_ON_REDEFINITION;
5769 Var.IsText = true;
5770 Var.TextValue = Value.str();
5771 return false;
5772}
5773
5774bool MasmParser::lookUpField(StringRef Name, AsmFieldInfo &Info) const {
5775 const std::pair<StringRef, StringRef> BaseMember = Name.split('.');
5776 const StringRef Base = BaseMember.first, Member = BaseMember.second;
5777 return lookUpField(Base, Member, Info);
5778}
5779
5780bool MasmParser::lookUpField(StringRef Base, StringRef Member,
5781 AsmFieldInfo &Info) const {
5782 if (Base.empty())
5783 return true;
5784
5785 AsmFieldInfo BaseInfo;
5786 if (Base.contains('.') && !lookUpField(Base, BaseInfo))
5787 Base = BaseInfo.Type.Name;
5788
5789 auto StructIt = Structs.find(Base.lower());
5790 auto TypeIt = KnownType.find(Base.lower());
5791 if (TypeIt != KnownType.end()) {
5792 StructIt = Structs.find(TypeIt->second.Name.lower());
5793 }
5794 if (StructIt != Structs.end())
5795 return lookUpField(StructIt->second, Member, Info);
5796
5797 return true;
5798}
5799
5800bool MasmParser::lookUpField(const StructInfo &Structure, StringRef Member,
5801 AsmFieldInfo &Info) const {
5802 if (Member.empty()) {
5803 Info.Type.Name = Structure.Name;
5804 Info.Type.Size = Structure.Size;
5805 Info.Type.ElementSize = Structure.Size;
5806 Info.Type.Length = 1;
5807 return false;
5808 }
5809
5810 std::pair<StringRef, StringRef> Split = Member.split('.');
5811 const StringRef FieldName = Split.first, FieldMember = Split.second;
5812
5813 auto StructIt = Structs.find(FieldName.lower());
5814 if (StructIt != Structs.end())
5815 return lookUpField(StructIt->second, FieldMember, Info);
5816
5817 auto FieldIt = Structure.FieldsByName.find(FieldName.lower());
5818 if (FieldIt == Structure.FieldsByName.end())
5819 return true;
5820
5821 const FieldInfo &Field = Structure.Fields[FieldIt->second];
5822 if (FieldMember.empty()) {
5823 Info.Offset += Field.Offset;
5824 Info.Type.Size = Field.SizeOf;
5825 Info.Type.ElementSize = Field.Type;
5826 Info.Type.Length = Field.LengthOf;
5827 if (Field.Contents.FT == FT_STRUCT)
5828 Info.Type.Name = Field.Contents.StructInfo.Structure.Name;
5829 else
5830 Info.Type.Name = "";
5831 return false;
5832 }
5833
5834 if (Field.Contents.FT != FT_STRUCT)
5835 return true;
5836 const StructFieldInfo &StructInfo = Field.Contents.StructInfo;
5837
5838 if (lookUpField(StructInfo.Structure, FieldMember, Info))
5839 return true;
5840
5842 return false;
5843}
5844
5845bool MasmParser::lookUpType(StringRef Name, AsmTypeInfo &Info) const {
5846 unsigned Size = StringSwitch<unsigned>(Name)
5847 .CasesLower("byte", "db", "sbyte", 1)
5848 .CasesLower("word", "dw", "sword", 2)
5849 .CasesLower("dword", "dd", "sdword", 4)
5850 .CasesLower("fword", "df", 6)
5851 .CasesLower("qword", "dq", "sqword", 8)
5852 .CaseLower("real4", 4)
5853 .CaseLower("real8", 8)
5854 .CaseLower("real10", 10)
5855 .Default(0);
5856 if (Size) {
5857 Info.Name = Name;
5858 Info.ElementSize = Size;
5859 Info.Length = 1;
5860 Info.Size = Size;
5861 return false;
5862 }
5863
5864 auto StructIt = Structs.find(Name.lower());
5865 if (StructIt != Structs.end()) {
5866 const StructInfo &Structure = StructIt->second;
5867 Info.Name = Name;
5868 Info.ElementSize = Structure.Size;
5869 Info.Length = 1;
5870 Info.Size = Structure.Size;
5871 return false;
5872 }
5873
5874 return true;
5875}
5876
5877bool MasmParser::parseMSInlineAsm(
5878 std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs,
5879 SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
5880 SmallVectorImpl<std::string> &Constraints,
5881 SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII,
5882 MCInstPrinter *IP, MCAsmParserSemaCallback &SI) {
5883 SmallVector<void *, 4> InputDecls;
5884 SmallVector<void *, 4> OutputDecls;
5885 SmallVector<bool, 4> InputDeclsAddressOf;
5886 SmallVector<bool, 4> OutputDeclsAddressOf;
5887 SmallVector<std::string, 4> InputConstraints;
5888 SmallVector<std::string, 4> OutputConstraints;
5889 SmallVector<MCRegister, 4> ClobberRegs;
5890
5891 SmallVector<AsmRewrite, 4> AsmStrRewrites;
5892
5893 // Prime the lexer.
5894 Lex();
5895
5896 // While we have input, parse each statement.
5897 unsigned InputIdx = 0;
5898 unsigned OutputIdx = 0;
5899 while (getLexer().isNot(AsmToken::Eof)) {
5900 // Parse curly braces marking block start/end.
5901 if (parseCurlyBlockScope(AsmStrRewrites))
5902 continue;
5903
5904 ParseStatementInfo Info(&AsmStrRewrites);
5905 bool StatementErr = parseStatement(Info, &SI);
5906
5907 if (StatementErr || Info.ParseError) {
5908 // Emit pending errors if any exist.
5909 printPendingErrors();
5910 return true;
5911 }
5912
5913 // No pending error should exist here.
5914 assert(!hasPendingError() && "unexpected error from parseStatement");
5915
5916 if (Info.Opcode == ~0U)
5917 continue;
5918
5919 const MCInstrDesc &Desc = MII->get(Info.Opcode);
5920
5921 // Build the list of clobbers, outputs and inputs.
5922 for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) {
5923 MCParsedAsmOperand &Operand = *Info.ParsedOperands[i];
5924
5925 // Register operand.
5926 if (Operand.isReg() && !Operand.needAddressOf() &&
5927 !getTargetParser().omitRegisterFromClobberLists(Operand.getReg())) {
5928 unsigned NumDefs = Desc.getNumDefs();
5929 // Clobber.
5930 if (NumDefs && Operand.getMCOperandNum() < NumDefs)
5931 ClobberRegs.push_back(Operand.getReg());
5932 continue;
5933 }
5934
5935 // Expr/Input or Output.
5936 StringRef SymName = Operand.getSymName();
5937 if (SymName.empty())
5938 continue;
5939
5940 void *OpDecl = Operand.getOpDecl();
5941 if (!OpDecl)
5942 continue;
5943
5944 StringRef Constraint = Operand.getConstraint();
5945 if (Operand.isImm()) {
5946 // Offset as immediate.
5947 if (Operand.isOffsetOfLocal())
5948 Constraint = "r";
5949 else
5950 Constraint = "i";
5951 }
5952
5953 bool isOutput = (i == 1) && Desc.mayStore();
5954 SMLoc Start = SMLoc::getFromPointer(SymName.data());
5955 if (isOutput) {
5956 ++InputIdx;
5957 OutputDecls.push_back(OpDecl);
5958 OutputDeclsAddressOf.push_back(Operand.needAddressOf());
5959 OutputConstraints.push_back(("=" + Constraint).str());
5960 AsmStrRewrites.emplace_back(AOK_Output, Start, SymName.size());
5961 } else {
5962 InputDecls.push_back(OpDecl);
5963 InputDeclsAddressOf.push_back(Operand.needAddressOf());
5964 InputConstraints.push_back(Constraint.str());
5965 if (Desc.operands()[i - 1].isBranchTarget())
5966 AsmStrRewrites.emplace_back(AOK_CallInput, Start, SymName.size());
5967 else
5968 AsmStrRewrites.emplace_back(AOK_Input, Start, SymName.size());
5969 }
5970 }
5971
5972 // Consider implicit defs to be clobbers. Think of cpuid and push.
5973 llvm::append_range(ClobberRegs, Desc.implicit_defs());
5974 }
5975
5976 // Set the number of Outputs and Inputs.
5977 NumOutputs = OutputDecls.size();
5978 NumInputs = InputDecls.size();
5979
5980 // Set the unique clobbers.
5981 array_pod_sort(ClobberRegs.begin(), ClobberRegs.end());
5982 ClobberRegs.erase(llvm::unique(ClobberRegs), ClobberRegs.end());
5983 Clobbers.assign(ClobberRegs.size(), std::string());
5984 for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) {
5985 raw_string_ostream OS(Clobbers[I]);
5986 IP->printRegName(OS, ClobberRegs[I]);
5987 }
5988
5989 // Merge the various outputs and inputs. Output are expected first.
5990 if (NumOutputs || NumInputs) {
5991 unsigned NumExprs = NumOutputs + NumInputs;
5992 OpDecls.resize(NumExprs);
5993 Constraints.resize(NumExprs);
5994 for (unsigned i = 0; i < NumOutputs; ++i) {
5995 OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
5996 Constraints[i] = OutputConstraints[i];
5997 }
5998 for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) {
5999 OpDecls[j] = std::make_pair(InputDecls[i], InputDeclsAddressOf[i]);
6000 Constraints[j] = InputConstraints[i];
6001 }
6002 }
6003
6004 // Build the IR assembly string.
6005 std::string AsmStringIR;
6006 raw_string_ostream OS(AsmStringIR);
6007 StringRef ASMString =
6009 const char *AsmStart = ASMString.begin();
6010 const char *AsmEnd = ASMString.end();
6011 array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
6012 for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
6013 const AsmRewrite &AR = *I;
6014 // Check if this has already been covered by another rewrite...
6015 if (AR.Done)
6016 continue;
6018
6019 const char *Loc = AR.Loc.getPointer();
6020 assert(Loc >= AsmStart && "Expected Loc to be at or after Start!");
6021
6022 // Emit everything up to the immediate/expression.
6023 if (unsigned Len = Loc - AsmStart)
6024 OS << StringRef(AsmStart, Len);
6025
6026 // Skip the original expression.
6027 if (Kind == AOK_Skip) {
6028 AsmStart = Loc + AR.Len;
6029 continue;
6030 }
6031
6032 unsigned AdditionalSkip = 0;
6033 // Rewrite expressions in $N notation.
6034 switch (Kind) {
6035 default:
6036 break;
6037 case AOK_IntelExpr:
6038 assert(AR.IntelExp.isValid() && "cannot write invalid intel expression");
6039 if (AR.IntelExp.NeedBracs)
6040 OS << "[";
6041 if (AR.IntelExp.hasBaseReg())
6042 OS << AR.IntelExp.BaseReg;
6043 if (AR.IntelExp.hasIndexReg())
6044 OS << (AR.IntelExp.hasBaseReg() ? " + " : "")
6045 << AR.IntelExp.IndexReg;
6046 if (AR.IntelExp.Scale > 1)
6047 OS << " * $$" << AR.IntelExp.Scale;
6048 if (AR.IntelExp.hasOffset()) {
6049 if (AR.IntelExp.hasRegs())
6050 OS << " + ";
6051 // Fuse this rewrite with a rewrite of the offset name, if present.
6052 StringRef OffsetName = AR.IntelExp.OffsetName;
6053 SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
6054 size_t OffsetLen = OffsetName.size();
6055 auto rewrite_it = std::find_if(
6056 I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
6057 return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
6058 (FusingAR.Kind == AOK_Input ||
6059 FusingAR.Kind == AOK_CallInput);
6060 });
6061 if (rewrite_it == AsmStrRewrites.end()) {
6062 OS << "offset " << OffsetName;
6063 } else if (rewrite_it->Kind == AOK_CallInput) {
6064 OS << "${" << InputIdx++ << ":P}";
6065 rewrite_it->Done = true;
6066 } else {
6067 OS << '$' << InputIdx++;
6068 rewrite_it->Done = true;
6069 }
6070 }
6071 if (AR.IntelExp.Imm || AR.IntelExp.emitImm())
6072 OS << (AR.IntelExp.emitImm() ? "$$" : " + $$") << AR.IntelExp.Imm;
6073 if (AR.IntelExp.NeedBracs)
6074 OS << "]";
6075 break;
6076 case AOK_Label:
6077 OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label;
6078 break;
6079 case AOK_Input:
6080 OS << '$' << InputIdx++;
6081 break;
6082 case AOK_CallInput:
6083 OS << "${" << InputIdx++ << ":P}";
6084 break;
6085 case AOK_Output:
6086 OS << '$' << OutputIdx++;
6087 break;
6088 case AOK_SizeDirective:
6089 switch (AR.Val) {
6090 default: break;
6091 case 8: OS << "byte ptr "; break;
6092 case 16: OS << "word ptr "; break;
6093 case 32: OS << "dword ptr "; break;
6094 case 64: OS << "qword ptr "; break;
6095 case 80: OS << "xword ptr "; break;
6096 case 128: OS << "xmmword ptr "; break;
6097 case 256: OS << "ymmword ptr "; break;
6098 }
6099 break;
6100 case AOK_Emit:
6101 OS << ".byte";
6102 break;
6103 case AOK_Align: {
6104 // MS alignment directives are measured in bytes. If the native assembler
6105 // measures alignment in bytes, we can pass it straight through.
6106 OS << ".align";
6107 if (getContext().getAsmInfo()->getAlignmentIsInBytes())
6108 break;
6109
6110 // Alignment is in log2 form, so print that instead and skip the original
6111 // immediate.
6112 unsigned Val = AR.Val;
6113 OS << ' ' << Val;
6114 assert(Val < 10 && "Expected alignment less then 2^10.");
6115 AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4;
6116 break;
6117 }
6118 case AOK_EVEN:
6119 OS << ".even";
6120 break;
6121 case AOK_EndOfStatement:
6122 OS << "\n\t";
6123 break;
6124 }
6125
6126 // Skip the original expression.
6127 AsmStart = Loc + AR.Len + AdditionalSkip;
6128 }
6129
6130 // Emit the remainder of the asm string.
6131 if (AsmStart != AsmEnd)
6132 OS << StringRef(AsmStart, AsmEnd - AsmStart);
6133
6134 AsmString = OS.str();
6135 return false;
6136}
6137
6138void MasmParser::initializeBuiltinSymbolMaps() {
6139 // Numeric built-ins (supported in all versions)
6140 BuiltinSymbolMap["@version"] = BI_VERSION;
6141 BuiltinSymbolMap["@line"] = BI_LINE;
6142
6143 // Text built-ins (supported in all versions)
6144 BuiltinSymbolMap["@date"] = BI_DATE;
6145 BuiltinSymbolMap["@time"] = BI_TIME;
6146 BuiltinSymbolMap["@filecur"] = BI_FILECUR;
6147 BuiltinSymbolMap["@filename"] = BI_FILENAME;
6148 BuiltinSymbolMap["@curseg"] = BI_CURSEG;
6149
6150 // Function built-ins (supported in all versions)
6151 BuiltinFunctionMap["@catstr"] = BI_CATSTR;
6152
6153 // Some built-ins exist only for MASM32 (32-bit x86)
6154 if (getContext().getSubtargetInfo()->getTargetTriple().getArch() ==
6155 Triple::x86) {
6156 // Numeric built-ins
6157 // BuiltinSymbolMap["@cpu"] = BI_CPU;
6158 // BuiltinSymbolMap["@interface"] = BI_INTERFACE;
6159 // BuiltinSymbolMap["@wordsize"] = BI_WORDSIZE;
6160 // BuiltinSymbolMap["@codesize"] = BI_CODESIZE;
6161 // BuiltinSymbolMap["@datasize"] = BI_DATASIZE;
6162 // BuiltinSymbolMap["@model"] = BI_MODEL;
6163
6164 // Text built-ins
6165 // BuiltinSymbolMap["@code"] = BI_CODE;
6166 // BuiltinSymbolMap["@data"] = BI_DATA;
6167 // BuiltinSymbolMap["@fardata?"] = BI_FARDATA;
6168 // BuiltinSymbolMap["@stack"] = BI_STACK;
6169 }
6170}
6171
6172const MCExpr *MasmParser::evaluateBuiltinValue(BuiltinSymbol Symbol,
6173 SMLoc StartLoc) {
6174 switch (Symbol) {
6175 default:
6176 return nullptr;
6177 case BI_VERSION:
6178 // Match a recent version of ML.EXE.
6179 return MCConstantExpr::create(1427, getContext());
6180 case BI_LINE: {
6181 int64_t Line;
6182 if (ActiveMacros.empty())
6183 Line = SrcMgr.FindLineNumber(StartLoc, CurBuffer);
6184 else
6185 Line = SrcMgr.FindLineNumber(ActiveMacros.front()->InstantiationLoc,
6186 ActiveMacros.front()->ExitBuffer);
6187 return MCConstantExpr::create(Line, getContext());
6188 }
6189 }
6190 llvm_unreachable("unhandled built-in symbol");
6191}
6192
6193std::optional<std::string>
6194MasmParser::evaluateBuiltinTextMacro(BuiltinSymbol Symbol, SMLoc StartLoc) {
6195 switch (Symbol) {
6196 default:
6197 return {};
6198 case BI_DATE: {
6199 // Current local date, formatted MM/DD/YY
6200 char TmpBuffer[sizeof("mm/dd/yy")];
6201 const size_t Len = strftime(TmpBuffer, sizeof(TmpBuffer), "%D", &TM);
6202 return std::string(TmpBuffer, Len);
6203 }
6204 case BI_TIME: {
6205 // Current local time, formatted HH:MM:SS (24-hour clock)
6206 char TmpBuffer[sizeof("hh:mm:ss")];
6207 const size_t Len = strftime(TmpBuffer, sizeof(TmpBuffer), "%T", &TM);
6208 return std::string(TmpBuffer, Len);
6209 }
6210 case BI_FILECUR:
6211 return SrcMgr
6213 ActiveMacros.empty() ? CurBuffer : ActiveMacros.front()->ExitBuffer)
6215 .str();
6216 case BI_FILENAME:
6219 .upper();
6220 case BI_CURSEG:
6221 return getStreamer().getCurrentSectionOnly()->getName().str();
6222 }
6223 llvm_unreachable("unhandled built-in symbol");
6224}
6225
6226bool MasmParser::evaluateBuiltinMacroFunction(BuiltinFunction Function,
6227 StringRef Name,
6228 std::string &Res) {
6229 if (parseToken(AsmToken::LParen, "invoking macro function '" + Name +
6230 "' requires arguments in parentheses")) {
6231 return true;
6232 }
6233
6235 switch (Function) {
6236 default:
6237 return true;
6238 case BI_CATSTR:
6239 break;
6240 }
6241 MCAsmMacro M(Name, "", P, {}, true);
6242
6243 MCAsmMacroArguments A;
6244 if (parseMacroArguments(&M, A, AsmToken::RParen) || parseRParen()) {
6245 return true;
6246 }
6247
6248 switch (Function) {
6249 default:
6250 llvm_unreachable("unhandled built-in function");
6251 case BI_CATSTR: {
6252 for (const MCAsmMacroArgument &Arg : A) {
6253 for (const AsmToken &Tok : Arg) {
6254 if (Tok.is(AsmToken::String)) {
6255 Res.append(Tok.getStringContents());
6256 } else {
6257 Res.append(Tok.getString());
6258 }
6259 }
6260 }
6261 return false;
6262 }
6263 }
6264 llvm_unreachable("unhandled built-in function");
6265 return true;
6266}
6267
6268/// Create an MCAsmParser instance.
6270 MCStreamer &Out, const MCAsmInfo &MAI,
6271 struct tm TM, unsigned CB) {
6272 return new MasmParser(SM, C, Out, MAI, TM, CB);
6273}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
static bool isNot(const MachineRegisterInfo &MRI, const MachineInstr &MI)
AMDGPU Lower Kernel Arguments
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
static bool isAngleBracketString(SMLoc &StrLoc, SMLoc &EndLoc)
This function checks if the next token is <string> type or arithmetic.
static unsigned getGNUBinOpPrecedence(const MCAsmInfo &MAI, AsmToken::TokenKind K, MCBinaryExpr::Opcode &Kind, bool ShouldUseLogicalShr)
static std::string angleBracketString(StringRef AltMacroStr)
creating a string without the escape characters '!'.
static int rewritesSort(const AsmRewrite *AsmRewriteA, const AsmRewrite *AsmRewriteB)
This file implements the BitVector class.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Analysis containing CSE Info
Definition CSEInfo.cpp:27
DXIL Intrinsic Expansion
@ Default
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:58
const std::string FatArchTraits< MachO::fat_arch >::StructName
Register Reg
static bool isMacroParameterChar(char C)
@ DEFAULT_ADDRSPACE
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
OptimizedStructLayoutField Field
#define P(N)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:480
This file defines the SmallString class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
#define DEBUG_WITH_TYPE(TYPE,...)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
Definition Debug.h:72
static void DiagHandler(const SMDiagnostic &Diag, void *Context)
Value * RHS
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
static APFloat getNaN(const fltSemantics &Sem, bool Negative=false, uint64_t payload=0)
Factory for NaN values.
Definition APFloat.h:1109
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1079
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:475
ConditionalAssemblyType TheCond
Definition AsmCond.h:30
bool Ignore
Definition AsmCond.h:32
bool CondMet
Definition AsmCond.h:31
LLVM_ABI SMLoc getLoc() const
Definition AsmLexer.cpp:32
bool isNot(TokenKind K) const
Definition MCAsmMacro.h:76
StringRef getString() const
Get the string for the current token, this includes all characters (for example, the quotes on string...
Definition MCAsmMacro.h:103
StringRef getStringContents() const
Get the contents of a string token (without quotes).
Definition MCAsmMacro.h:83
bool is(TokenKind K) const
Definition MCAsmMacro.h:75
LLVM_ABI SMLoc getEndLoc() const
Definition AsmLexer.cpp:34
StringRef getIdentifier() const
Get the identifier string for the current token, which should be an identifier or a string.
Definition MCAsmMacro.h:92
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition MCAsmInfo.h:64
bool preserveAsmComments() const
Return true if assembly (inline or otherwise) should be parsed.
Definition MCAsmInfo.h:709
StringRef getPrivateLabelPrefix() const
Definition MCAsmInfo.h:546
bool shouldUseLogicalShr() const
Definition MCAsmInfo.h:717
virtual bool useCodeAlign(const MCSection &Sec) const
Definition MCAsmInfo.h:503
Generic interface for extending the MCAsmParser, which is implemented by target and object file assem...
Generic assembler parser interface, for use by target specific assembly parsers.
static LLVM_ABI const MCBinaryExpr * create(Opcode Op, const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.cpp:201
@ Div
Signed division.
Definition MCExpr.h:304
@ Shl
Shift left.
Definition MCExpr.h:321
@ AShr
Arithmetic shift right.
Definition MCExpr.h:322
@ LShr
Logical shift right.
Definition MCExpr.h:323
@ GTE
Signed greater than or equal comparison (result is either 0 or some target-specific non-zero value).
Definition MCExpr.h:308
@ EQ
Equality comparison.
Definition MCExpr.h:305
@ Sub
Subtraction.
Definition MCExpr.h:324
@ Mul
Multiplication.
Definition MCExpr.h:317
@ GT
Signed greater than comparison (result is either 0 or some target-specific non-zero value)
Definition MCExpr.h:306
@ Mod
Signed remainder.
Definition MCExpr.h:316
@ And
Bitwise and.
Definition MCExpr.h:303
@ Or
Bitwise or.
Definition MCExpr.h:319
@ Xor
Bitwise exclusive or.
Definition MCExpr.h:325
@ LAnd
Logical and.
Definition MCExpr.h:310
@ LOr
Logical or.
Definition MCExpr.h:311
@ LT
Signed less than comparison (result is either 0 or some target-specific non-zero value).
Definition MCExpr.h:312
@ Add
Addition.
Definition MCExpr.h:302
@ LTE
Signed less than or equal comparison (result is either 0 or some target-specific non-zero value).
Definition MCExpr.h:314
@ NE
Inequality comparison.
Definition MCExpr.h:318
int64_t getValue() const
Definition MCExpr.h:171
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Definition MCExpr.cpp:212
Context object for machine code objects.
Definition MCContext.h:83
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI MCSymbol * createDirectionalLocalSymbol(unsigned LocalLabelVal)
Create the definition of a directional local symbol for numbered label (used for "1:" definitions).
const MCAsmInfo * getAsmInfo() const
Definition MCContext.h:412
virtual void printRegName(raw_ostream &OS, MCRegister Reg)
Print the assembler register name.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode.
Definition MCInstrInfo.h:90
virtual bool isReg() const =0
isReg - Is this a register operand?
virtual bool needAddressOf() const
needAddressOf - Do we need to emit code to get the address of the variable/label?
virtual MCRegister getReg() const =0
virtual bool isOffsetOfLocal() const
isOffsetOfLocal - Do we need to emit code to get the offset of the local variable,...
virtual StringRef getSymName()
virtual bool isImm() const =0
isImm - Is this an immediate operand?
Streaming machine code generation interface.
Definition MCStreamer.h:220
virtual void addBlankLine()
Emit a blank line to a .s file to pretty it up.
Definition MCStreamer.h:408
virtual void initSections(bool NoExecStack, const MCSubtargetInfo &STI)
Create the default sections and set the initial one.
virtual void addExplicitComment(const Twine &T)
Add explicit comment T.
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
void finish(SMLoc EndLoc=SMLoc())
Finish emission of machine code.
const MCSymbol & getSymbol() const
Definition MCExpr.h:227
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:214
bool isUndefined() const
isUndefined - Check if this symbol undefined (i.e., implicitly defined).
Definition MCSymbol.h:243
StringRef getName() const
getName - Get the symbol name.
Definition MCSymbol.h:188
bool isVariable() const
isVariable - Check if this is a variable symbol.
Definition MCSymbol.h:267
LLVM_ABI void setVariableValue(const MCExpr *Value)
Definition MCSymbol.cpp:50
void setRedefinable(bool Value)
Mark this symbol as redefinable.
Definition MCSymbol.h:210
void redefineIfPossible()
Prepare this symbol to be redefined.
Definition MCSymbol.h:212
const MCExpr * getVariableValue() const
Get the expression of the variable symbol.
Definition MCSymbol.h:270
bool isTemporary() const
isTemporary - Check if this is an assembler temporary symbol.
Definition MCSymbol.h:205
static const MCUnaryExpr * createLNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:265
static const MCUnaryExpr * createPlus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:277
static const MCUnaryExpr * createNot(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:273
static const MCUnaryExpr * createMinus(const MCExpr *Expr, MCContext &Ctx, SMLoc Loc=SMLoc())
Definition MCExpr.h:269
virtual StringRef getBufferIdentifier() const
Return an identifier for this buffer, typically the filename it was read from.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it.
StringRef getBuffer() const
constexpr bool isFailure() const
constexpr bool isSuccess() const
LLVM_ABI void print(const char *ProgName, raw_ostream &S, bool ShowColors=true, bool ShowKindLabel=true, bool ShowLocation=true) const
SourceMgr::DiagKind getKind() const
Definition SourceMgr.h:312
StringRef getLineContents() const
Definition SourceMgr.h:314
SMLoc getLoc() const
Definition SourceMgr.h:308
StringRef getMessage() const
Definition SourceMgr.h:313
ArrayRef< std::pair< unsigned, unsigned > > getRanges() const
Definition SourceMgr.h:315
const SourceMgr * getSourceMgr() const
Definition SourceMgr.h:307
int getColumnNo() const
Definition SourceMgr.h:311
Represents a location in source code.
Definition SMLoc.h:23
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:36
constexpr const char * getPointer() const
Definition SMLoc.h:34
constexpr bool isValid() const
Definition SMLoc.h:29
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void resize(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:32
unsigned getMainFileID() const
Definition SourceMgr.h:133
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition SourceMgr.h:126
LLVM_ABI void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
SMLoc getParentIncludeLoc(unsigned i) const
Definition SourceMgr.h:138
LLVM_ABI void PrintIncludeStack(SMLoc IncludeLoc, raw_ostream &OS) const
Prints the names of included files and the line of the file they were included from.
LLVM_ABI unsigned FindBufferContainingLoc(SMLoc Loc) const
Return the ID of the buffer containing the specified location.
Definition SourceMgr.cpp:73
void(*)(const SMDiagnostic &, void *Context) DiagHandlerTy
Clients that want to handle their own diagnostics in a custom way can register a function pointer+con...
Definition SourceMgr.h:44
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition SourceMgr.h:113
LLVM_ABI unsigned AddIncludeFile(const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile)
Search for a file with the specified name in the current directory or in one of the IncludeDirs.
Definition SourceMgr.cpp:41
unsigned FindLineNumber(SMLoc Loc, unsigned BufferID=0) const
Find the line number for the specified location in the specified file.
Definition SourceMgr.h:197
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition SourceMgr.h:145
iterator end()
Definition StringMap.h:224
iterator find(StringRef Key)
Definition StringMap.h:235
bool contains(StringRef Key) const
contains - Return true if the element is in the map, false otherwise.
Definition StringMap.h:275
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Definition StringMap.h:278
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition StringMap.h:255
StringMapIterBase< ValueTy, true > const_iterator
Definition StringMap.h:220
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:310
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
Definition StringRef.h:665
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:480
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:233
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:581
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:269
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:151
iterator begin() const
Definition StringRef.h:120
LLVM_ABI std::string upper() const
Convert the given ASCII string to uppercase.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition StringRef.h:694
constexpr size_t size() const
size - Get the string size.
Definition StringRef.h:154
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:148
iterator end() const
Definition StringRef.h:122
LLVM_ABI std::string lower() const
bool equals_insensitive(StringRef RHS) const
Check for string equality, ignoring case.
Definition StringRef.h:180
StringRef str() const
Return a StringRef for the vector contents.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char TypeName[]
Key for Kernel::Arg::Metadata::mTypeName.
constexpr char SymbolName[]
Key for Kernel::Metadata::mSymbolName.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
LLVM_ABI SimpleSymbol parseSymbol(StringRef SymName)
Get symbol classification by parsing the name of a symbol.
Definition Symbol.cpp:75
std::variant< std::monostate, DecisionParameters, BranchParameters > Parameters
The type of MC/DC-specific parameters.
Definition MCDCTypes.h:56
@ Parameter
An inlay hint that is for a parameter.
Definition Protocol.h:1105
bool empty() const
Definition BasicBlock.h:101
Context & getContext() const
Definition BasicBlock.h:99
LLVM_ABI Instruction & front() const
LLVM_ABI StringRef stem(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get stem.
Definition Path.cpp:579
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:318
bool errorToBool(Error Err)
Helper for converting an Error to a bool.
Definition Error.h:1113
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1657
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
Definition MathExtras.h:174
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
@ AOK_EndOfStatement
@ AOK_SizeDirective
LLVM_ABI raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
LLVM_ABI MCAsmParser * createMCMasmParser(SourceMgr &, MCContext &, MCStreamer &, const MCAsmInfo &, struct tm, unsigned CB=0)
Create an MCAsmParser instance for parsing Microsoft MASM-style assembly.
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:252
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
std::vector< MCAsmMacroParameter > MCAsmMacroParameters
Definition MCAsmMacro.h:134
auto unique(Range &&R, Predicate P)
Definition STLExtras.h:2056
Op::Description Desc
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:348
SourceMgr SrcMgr
Definition Error.cpp:24
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
cl::opt< unsigned > AsmMacroMaxNestingDepth
MCAsmParserExtension * createCOFFMasmParser()
const char AsmRewritePrecedence[]
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
constexpr bool isUInt(uint64_t x)
Checks if an unsigned integer fits into the given bit width.
Definition MathExtras.h:198
bool isAlnum(char C)
Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
Definition Format.h:201
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:155
ArrayRef(const T &OneElt) -> ArrayRef< T >
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:257
bool isSpace(char C)
Checks whether character C is whitespace in the "C" locale.
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition STLExtras.h:1584
@ MCSA_Global
.type _foo, @gnu_unique_object
@ MCSA_Extern
.extern (XCOFF)
AsmRewriteKind Kind
bool hasIndexReg() const
bool hasRegs() const
bool hasOffset() const
bool hasBaseReg() const
bool emitImm() const
bool isValid() const
std::vector< AsmToken > Value
Definition MCAsmMacro.h:124
uint64_t Offset
The offset of this field in the final layout.
uint64_t Size
The required size of this field in bytes.