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