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/Casting.h" |
50 | #include "llvm/Support/CommandLine.h" |
51 | #include "llvm/Support/ErrorHandling.h" |
52 | #include "llvm/Support/MD5.h" |
53 | #include "llvm/Support/MathExtras.h" |
54 | #include "llvm/Support/MemoryBuffer.h" |
55 | #include "llvm/Support/SMLoc.h" |
56 | #include "llvm/Support/SourceMgr.h" |
57 | #include "llvm/Support/raw_ostream.h" |
58 | #include <algorithm> |
59 | #include <cassert> |
60 | #include <cctype> |
61 | #include <climits> |
62 | #include <cstddef> |
63 | #include <cstdint> |
64 | #include <deque> |
65 | #include <memory> |
66 | #include <optional> |
67 | #include <sstream> |
68 | #include <string> |
69 | #include <tuple> |
70 | #include <utility> |
71 | #include <vector> |
72 | |
73 | using namespace llvm; |
74 | |
75 | MCAsmParserSemaCallback::~MCAsmParserSemaCallback() = default; |
76 | |
77 | namespace { |
78 | |
79 | /// Helper types for tracking macro definitions. |
80 | typedef std::vector<AsmToken> MCAsmMacroArgument; |
81 | typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments; |
82 | |
83 | /// Helper class for storing information about an active macro |
84 | /// instantiation. |
85 | struct MacroInstantiation { |
86 | /// The location of the instantiation. |
87 | SMLoc InstantiationLoc; |
88 | |
89 | /// The buffer where parsing should resume upon instantiation completion. |
90 | unsigned ExitBuffer; |
91 | |
92 | /// The location where parsing should resume upon instantiation completion. |
93 | SMLoc ExitLoc; |
94 | |
95 | /// The depth of TheCondStack at the start of the instantiation. |
96 | size_t CondStackDepth; |
97 | }; |
98 | |
99 | struct ParseStatementInfo { |
100 | /// The parsed operands from the last parsed statement. |
101 | SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands; |
102 | |
103 | /// The opcode from the last parsed instruction. |
104 | unsigned Opcode = ~0U; |
105 | |
106 | /// Was there an error parsing the inline assembly? |
107 | bool ParseError = false; |
108 | |
109 | SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr; |
110 | |
111 | ParseStatementInfo() = delete; |
112 | ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites) |
113 | : AsmRewrites(rewrites) {} |
114 | }; |
115 | |
116 | /// The concrete assembly parser instance. |
117 | class AsmParser : public MCAsmParser { |
118 | private: |
119 | SourceMgr::DiagHandlerTy SavedDiagHandler; |
120 | void *SavedDiagContext; |
121 | std::unique_ptr<MCAsmParserExtension> PlatformParser; |
122 | SMLoc StartTokLoc; |
123 | std::optional<SMLoc> CFIStartProcLoc; |
124 | |
125 | /// This is the current buffer index we're lexing from as managed by the |
126 | /// SourceMgr object. |
127 | unsigned CurBuffer; |
128 | |
129 | AsmCond TheCondState; |
130 | std::vector<AsmCond> TheCondStack; |
131 | |
132 | /// maps directive names to handler methods in parser |
133 | /// extensions. Extensions register themselves in this map by calling |
134 | /// addDirectiveHandler. |
135 | StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap; |
136 | |
137 | /// Stack of active macro instantiations. |
138 | std::vector<MacroInstantiation*> ActiveMacros; |
139 | |
140 | /// List of bodies of anonymous macros. |
141 | std::deque<MCAsmMacro> MacroLikeBodies; |
142 | |
143 | /// Boolean tracking whether macro substitution is enabled. |
144 | unsigned MacrosEnabledFlag : 1; |
145 | |
146 | /// Keeps track of how many .macro's have been instantiated. |
147 | unsigned NumOfMacroInstantiations = 0; |
148 | |
149 | /// The values from the last parsed cpp hash file line comment if any. |
150 | struct CppHashInfoTy { |
151 | StringRef Filename; |
152 | int64_t LineNumber; |
153 | SMLoc Loc; |
154 | unsigned Buf; |
155 | CppHashInfoTy() : LineNumber(0), Buf(0) {} |
156 | }; |
157 | CppHashInfoTy CppHashInfo; |
158 | |
159 | /// Have we seen any file line comment. |
160 | bool HadCppHashFilename = false; |
161 | |
162 | /// List of forward directional labels for diagnosis at the end. |
163 | SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels; |
164 | |
165 | SmallSet<StringRef, 2> LTODiscardSymbols; |
166 | |
167 | /// AssemblerDialect. ~OU means unset value and use value provided by MAI. |
168 | unsigned AssemblerDialect = ~0U; |
169 | |
170 | /// is Darwin compatibility enabled? |
171 | bool IsDarwin = false; |
172 | |
173 | /// Are we parsing ms-style inline assembly? |
174 | bool ParsingMSInlineAsm = false; |
175 | |
176 | /// Did we already inform the user about inconsistent MD5 usage? |
177 | bool ReportedInconsistentMD5 = false; |
178 | |
179 | // Is alt macro mode enabled. |
180 | bool AltMacroMode = false; |
181 | |
182 | protected: |
183 | virtual bool parseStatement(ParseStatementInfo &Info, |
184 | MCAsmParserSemaCallback *SI); |
185 | |
186 | /// This routine uses the target specific ParseInstruction function to |
187 | /// parse an instruction into Operands, and then call the target specific |
188 | /// MatchAndEmit function to match and emit the instruction. |
189 | bool parseAndMatchAndEmitTargetInstruction(ParseStatementInfo &Info, |
190 | StringRef IDVal, AsmToken ID, |
191 | SMLoc IDLoc); |
192 | |
193 | /// Should we emit DWARF describing this assembler source? (Returns false if |
194 | /// the source has .file directives, which means we don't want to generate |
195 | /// info describing the assembler source itself.) |
196 | bool enabledGenDwarfForAssembly(); |
197 | |
198 | public: |
199 | AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
200 | const MCAsmInfo &MAI, unsigned CB); |
201 | AsmParser(const AsmParser &) = delete; |
202 | AsmParser &operator=(const AsmParser &) = delete; |
203 | ~AsmParser() override; |
204 | |
205 | bool Run(bool NoInitialTextSection, bool NoFinalize = false) override; |
206 | |
207 | void addDirectiveHandler(StringRef Directive, |
208 | ExtensionDirectiveHandler Handler) override { |
209 | ExtensionDirectiveMap[Directive] = Handler; |
210 | } |
211 | |
212 | void addAliasForDirective(StringRef Directive, StringRef Alias) override { |
213 | DirectiveKindMap[Directive.lower()] = DirectiveKindMap[Alias.lower()]; |
214 | } |
215 | |
216 | /// @name MCAsmParser Interface |
217 | /// { |
218 | |
219 | CodeViewContext &getCVContext() { return Ctx.getCVContext(); } |
220 | |
221 | unsigned getAssemblerDialect() override { |
222 | if (AssemblerDialect == ~0U) |
223 | return MAI.getAssemblerDialect(); |
224 | else |
225 | return AssemblerDialect; |
226 | } |
227 | void setAssemblerDialect(unsigned i) override { |
228 | AssemblerDialect = i; |
229 | } |
230 | |
231 | void Note(SMLoc L, const Twine &Msg, SMRange Range = std::nullopt) override; |
232 | bool Warning(SMLoc L, const Twine &Msg, |
233 | SMRange Range = std::nullopt) override; |
234 | bool printError(SMLoc L, const Twine &Msg, |
235 | SMRange Range = std::nullopt) 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 | |
279 | private: |
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 = std::nullopt) 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_ORG, |
422 | DK_FILL, |
423 | DK_ENDR, |
424 | DK_BUNDLE_ALIGN_MODE, |
425 | DK_BUNDLE_LOCK, |
426 | DK_BUNDLE_UNLOCK, |
427 | DK_ZERO, |
428 | DK_EXTERN, |
429 | DK_GLOBL, |
430 | DK_GLOBAL, |
431 | DK_LAZY_REFERENCE, |
432 | DK_NO_DEAD_STRIP, |
433 | DK_SYMBOL_RESOLVER, |
434 | DK_PRIVATE_EXTERN, |
435 | DK_REFERENCE, |
436 | DK_WEAK_DEFINITION, |
437 | DK_WEAK_REFERENCE, |
438 | DK_WEAK_DEF_CAN_BE_HIDDEN, |
439 | DK_COLD, |
440 | DK_COMM, |
441 | DK_COMMON, |
442 | DK_LCOMM, |
443 | DK_ABORT, |
444 | DK_INCLUDE, |
445 | DK_INCBIN, |
446 | DK_CODE16, |
447 | DK_CODE16GCC, |
448 | DK_REPT, |
449 | DK_IRP, |
450 | DK_IRPC, |
451 | DK_IF, |
452 | DK_IFEQ, |
453 | DK_IFGE, |
454 | DK_IFGT, |
455 | DK_IFLE, |
456 | DK_IFLT, |
457 | DK_IFNE, |
458 | DK_IFB, |
459 | DK_IFNB, |
460 | DK_IFC, |
461 | DK_IFEQS, |
462 | DK_IFNC, |
463 | DK_IFNES, |
464 | DK_IFDEF, |
465 | DK_IFNDEF, |
466 | DK_IFNOTDEF, |
467 | DK_ELSEIF, |
468 | DK_ELSE, |
469 | DK_ENDIF, |
470 | DK_SPACE, |
471 | DK_SKIP, |
472 | DK_FILE, |
473 | DK_LINE, |
474 | DK_LOC, |
475 | DK_LOC_LABEL, |
476 | DK_STABS, |
477 | DK_CV_FILE, |
478 | DK_CV_FUNC_ID, |
479 | DK_CV_INLINE_SITE_ID, |
480 | DK_CV_LOC, |
481 | DK_CV_LINETABLE, |
482 | DK_CV_INLINE_LINETABLE, |
483 | DK_CV_DEF_RANGE, |
484 | DK_CV_STRINGTABLE, |
485 | DK_CV_STRING, |
486 | DK_CV_FILECHECKSUMS, |
487 | DK_CV_FILECHECKSUM_OFFSET, |
488 | DK_CV_FPO_DATA, |
489 | DK_CFI_SECTIONS, |
490 | DK_CFI_STARTPROC, |
491 | DK_CFI_ENDPROC, |
492 | DK_CFI_DEF_CFA, |
493 | DK_CFI_DEF_CFA_OFFSET, |
494 | DK_CFI_ADJUST_CFA_OFFSET, |
495 | DK_CFI_DEF_CFA_REGISTER, |
496 | DK_CFI_LLVM_DEF_ASPACE_CFA, |
497 | DK_CFI_OFFSET, |
498 | DK_CFI_REL_OFFSET, |
499 | DK_CFI_PERSONALITY, |
500 | DK_CFI_LSDA, |
501 | DK_CFI_REMEMBER_STATE, |
502 | DK_CFI_RESTORE_STATE, |
503 | DK_CFI_SAME_VALUE, |
504 | DK_CFI_RESTORE, |
505 | DK_CFI_ESCAPE, |
506 | DK_CFI_RETURN_COLUMN, |
507 | DK_CFI_SIGNAL_FRAME, |
508 | DK_CFI_UNDEFINED, |
509 | DK_CFI_REGISTER, |
510 | DK_CFI_WINDOW_SAVE, |
511 | DK_CFI_LABEL, |
512 | DK_CFI_B_KEY_FRAME, |
513 | DK_CFI_VAL_OFFSET, |
514 | DK_MACROS_ON, |
515 | DK_MACROS_OFF, |
516 | DK_ALTMACRO, |
517 | DK_NOALTMACRO, |
518 | DK_MACRO, |
519 | DK_EXITM, |
520 | DK_ENDM, |
521 | DK_ENDMACRO, |
522 | DK_PURGEM, |
523 | DK_SLEB128, |
524 | DK_ULEB128, |
525 | DK_ERR, |
526 | DK_ERROR, |
527 | DK_WARNING, |
528 | DK_PRINT, |
529 | DK_ADDRSIG, |
530 | DK_ADDRSIG_SYM, |
531 | DK_PSEUDO_PROBE, |
532 | DK_LTO_DISCARD, |
533 | DK_LTO_SET_CONDITIONAL, |
534 | DK_CFI_MTE_TAGGED_FRAME, |
535 | DK_MEMTAG, |
536 | DK_END |
537 | }; |
538 | |
539 | /// Maps directive name --> DirectiveKind enum, for |
540 | /// directives parsed by this class. |
541 | StringMap<DirectiveKind> DirectiveKindMap; |
542 | |
543 | // Codeview def_range type parsing. |
544 | enum CVDefRangeType { |
545 | CVDR_DEFRANGE = 0, // Placeholder |
546 | CVDR_DEFRANGE_REGISTER, |
547 | CVDR_DEFRANGE_FRAMEPOINTER_REL, |
548 | CVDR_DEFRANGE_SUBFIELD_REGISTER, |
549 | CVDR_DEFRANGE_REGISTER_REL |
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 parseDirectiveReloc(SMLoc DirectiveLoc); // ".reloc" |
559 | bool parseDirectiveValue(StringRef IDVal, |
560 | unsigned Size); // ".byte", ".long", ... |
561 | bool parseDirectiveOctaValue(StringRef IDVal); // ".octa", ... |
562 | bool parseDirectiveRealValue(StringRef IDVal, |
563 | const fltSemantics &); // ".single", ... |
564 | bool parseDirectiveFill(); // ".fill" |
565 | bool parseDirectiveZero(); // ".zero" |
566 | // ".set", ".equ", ".equiv", ".lto_set_conditional" |
567 | bool parseDirectiveSet(StringRef IDVal, AssignmentKind Kind); |
568 | bool parseDirectiveOrg(); // ".org" |
569 | // ".align{,32}", ".p2align{,w,l}" |
570 | bool parseDirectiveAlign(bool IsPow2, unsigned ValueSize); |
571 | |
572 | // ".file", ".line", ".loc", ".loc_label", ".stabs" |
573 | bool parseDirectiveFile(SMLoc DirectiveLoc); |
574 | bool parseDirectiveLine(); |
575 | bool parseDirectiveLoc(); |
576 | bool parseDirectiveLocLabel(SMLoc DirectiveLoc); |
577 | bool parseDirectiveStabs(); |
578 | |
579 | // ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable", |
580 | // ".cv_inline_linetable", ".cv_def_range", ".cv_string" |
581 | bool parseDirectiveCVFile(); |
582 | bool parseDirectiveCVFuncId(); |
583 | bool parseDirectiveCVInlineSiteId(); |
584 | bool parseDirectiveCVLoc(); |
585 | bool parseDirectiveCVLinetable(); |
586 | bool parseDirectiveCVInlineLinetable(); |
587 | bool parseDirectiveCVDefRange(); |
588 | bool parseDirectiveCVString(); |
589 | bool parseDirectiveCVStringTable(); |
590 | bool parseDirectiveCVFileChecksums(); |
591 | bool parseDirectiveCVFileChecksumOffset(); |
592 | bool parseDirectiveCVFPOData(); |
593 | |
594 | // .cfi directives |
595 | bool parseDirectiveCFIRegister(SMLoc DirectiveLoc); |
596 | bool parseDirectiveCFIWindowSave(SMLoc DirectiveLoc); |
597 | bool parseDirectiveCFISections(); |
598 | bool parseDirectiveCFIStartProc(); |
599 | bool parseDirectiveCFIEndProc(); |
600 | bool parseDirectiveCFIDefCfaOffset(SMLoc DirectiveLoc); |
601 | bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc); |
602 | bool parseDirectiveCFIAdjustCfaOffset(SMLoc DirectiveLoc); |
603 | bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc); |
604 | bool parseDirectiveCFILLVMDefAspaceCfa(SMLoc DirectiveLoc); |
605 | bool parseDirectiveCFIOffset(SMLoc DirectiveLoc); |
606 | bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc); |
607 | bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality); |
608 | bool parseDirectiveCFIRememberState(SMLoc DirectiveLoc); |
609 | bool parseDirectiveCFIRestoreState(SMLoc DirectiveLoc); |
610 | bool parseDirectiveCFISameValue(SMLoc DirectiveLoc); |
611 | bool parseDirectiveCFIRestore(SMLoc DirectiveLoc); |
612 | bool parseDirectiveCFIEscape(SMLoc DirectiveLoc); |
613 | bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc); |
614 | bool parseDirectiveCFISignalFrame(SMLoc DirectiveLoc); |
615 | bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc); |
616 | bool parseDirectiveCFILabel(SMLoc DirectiveLoc); |
617 | bool parseDirectiveCFIValOffset(SMLoc DirectiveLoc); |
618 | |
619 | // macro directives |
620 | bool parseDirectivePurgeMacro(SMLoc DirectiveLoc); |
621 | bool parseDirectiveExitMacro(StringRef Directive); |
622 | bool parseDirectiveEndMacro(StringRef Directive); |
623 | bool parseDirectiveMacro(SMLoc DirectiveLoc); |
624 | bool parseDirectiveMacrosOnOff(StringRef Directive); |
625 | // alternate macro mode directives |
626 | bool parseDirectiveAltmacro(StringRef Directive); |
627 | // ".bundle_align_mode" |
628 | bool parseDirectiveBundleAlignMode(); |
629 | // ".bundle_lock" |
630 | bool parseDirectiveBundleLock(); |
631 | // ".bundle_unlock" |
632 | bool parseDirectiveBundleUnlock(); |
633 | |
634 | // ".space", ".skip" |
635 | bool parseDirectiveSpace(StringRef IDVal); |
636 | |
637 | // ".dcb" |
638 | bool parseDirectiveDCB(StringRef IDVal, unsigned Size); |
639 | bool parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &); |
640 | // ".ds" |
641 | bool parseDirectiveDS(StringRef IDVal, unsigned Size); |
642 | |
643 | // .sleb128 (Signed=true) and .uleb128 (Signed=false) |
644 | bool parseDirectiveLEB128(bool Signed); |
645 | |
646 | /// Parse a directive like ".globl" which |
647 | /// accepts a single symbol (which should be a label or an external). |
648 | bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr); |
649 | |
650 | bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm" |
651 | |
652 | bool parseDirectiveAbort(SMLoc DirectiveLoc); // ".abort" |
653 | bool parseDirectiveInclude(); // ".include" |
654 | bool parseDirectiveIncbin(); // ".incbin" |
655 | |
656 | // ".if", ".ifeq", ".ifge", ".ifgt" , ".ifle", ".iflt" or ".ifne" |
657 | bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind); |
658 | // ".ifb" or ".ifnb", depending on ExpectBlank. |
659 | bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank); |
660 | // ".ifc" or ".ifnc", depending on ExpectEqual. |
661 | bool parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual); |
662 | // ".ifeqs" or ".ifnes", depending on ExpectEqual. |
663 | bool parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual); |
664 | // ".ifdef" or ".ifndef", depending on expect_defined |
665 | bool parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined); |
666 | bool parseDirectiveElseIf(SMLoc DirectiveLoc); // ".elseif" |
667 | bool parseDirectiveElse(SMLoc DirectiveLoc); // ".else" |
668 | bool parseDirectiveEndIf(SMLoc DirectiveLoc); // .endif |
669 | bool parseEscapedString(std::string &Data) override; |
670 | bool parseAngleBracketString(std::string &Data) override; |
671 | |
672 | // Macro-like directives |
673 | MCAsmMacro *parseMacroLikeBody(SMLoc DirectiveLoc); |
674 | void instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
675 | raw_svector_ostream &OS); |
676 | bool parseDirectiveRept(SMLoc DirectiveLoc, StringRef Directive); |
677 | bool parseDirectiveIrp(SMLoc DirectiveLoc); // ".irp" |
678 | bool parseDirectiveIrpc(SMLoc DirectiveLoc); // ".irpc" |
679 | bool parseDirectiveEndr(SMLoc DirectiveLoc); // ".endr" |
680 | |
681 | // "_emit" or "__emit" |
682 | bool parseDirectiveMSEmit(SMLoc DirectiveLoc, ParseStatementInfo &Info, |
683 | size_t Len); |
684 | |
685 | // "align" |
686 | bool parseDirectiveMSAlign(SMLoc DirectiveLoc, ParseStatementInfo &Info); |
687 | |
688 | // "end" |
689 | bool parseDirectiveEnd(SMLoc DirectiveLoc); |
690 | |
691 | // ".err" or ".error" |
692 | bool parseDirectiveError(SMLoc DirectiveLoc, bool WithMessage); |
693 | |
694 | // ".warning" |
695 | bool parseDirectiveWarning(SMLoc DirectiveLoc); |
696 | |
697 | // .print <double-quotes-string> |
698 | bool parseDirectivePrint(SMLoc DirectiveLoc); |
699 | |
700 | // .pseudoprobe |
701 | bool parseDirectivePseudoProbe(); |
702 | |
703 | // ".lto_discard" |
704 | bool parseDirectiveLTODiscard(); |
705 | |
706 | // Directives to support address-significance tables. |
707 | bool parseDirectiveAddrsig(); |
708 | bool parseDirectiveAddrsigSym(); |
709 | |
710 | void initializeDirectiveKindMap(); |
711 | void initializeCVDefRangeTypeMap(); |
712 | }; |
713 | |
714 | class HLASMAsmParser final : public AsmParser { |
715 | private: |
716 | AsmLexer &Lexer; |
717 | MCStreamer &Out; |
718 | |
719 | void lexLeadingSpaces() { |
720 | while (Lexer.is(K: AsmToken::Space)) |
721 | Lexer.Lex(); |
722 | } |
723 | |
724 | bool parseAsHLASMLabel(ParseStatementInfo &Info, MCAsmParserSemaCallback *SI); |
725 | bool parseAsMachineInstruction(ParseStatementInfo &Info, |
726 | MCAsmParserSemaCallback *SI); |
727 | |
728 | public: |
729 | HLASMAsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
730 | const MCAsmInfo &MAI, unsigned CB = 0) |
731 | : AsmParser(SM, Ctx, Out, MAI, CB), Lexer(getLexer()), Out(Out) { |
732 | Lexer.setSkipSpace(false); |
733 | Lexer.setAllowHashInIdentifier(true); |
734 | Lexer.setLexHLASMIntegers(true); |
735 | Lexer.setLexHLASMStrings(true); |
736 | } |
737 | |
738 | ~HLASMAsmParser() { Lexer.setSkipSpace(true); } |
739 | |
740 | bool parseStatement(ParseStatementInfo &Info, |
741 | MCAsmParserSemaCallback *SI) override; |
742 | }; |
743 | |
744 | } // end anonymous namespace |
745 | |
746 | namespace llvm { |
747 | |
748 | extern cl::opt<unsigned> AsmMacroMaxNestingDepth; |
749 | |
750 | extern MCAsmParserExtension *createDarwinAsmParser(); |
751 | extern MCAsmParserExtension *createELFAsmParser(); |
752 | extern MCAsmParserExtension *createCOFFAsmParser(); |
753 | extern MCAsmParserExtension *createGOFFAsmParser(); |
754 | extern MCAsmParserExtension *createXCOFFAsmParser(); |
755 | extern MCAsmParserExtension *createWasmAsmParser(); |
756 | |
757 | } // end namespace llvm |
758 | |
759 | AsmParser::AsmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out, |
760 | const MCAsmInfo &MAI, unsigned CB = 0) |
761 | : MCAsmParser(Ctx, Out, SM, MAI), CurBuffer(CB ? CB : SM.getMainFileID()), |
762 | MacrosEnabledFlag(true) { |
763 | HadError = false; |
764 | // Save the old handler. |
765 | SavedDiagHandler = SrcMgr.getDiagHandler(); |
766 | SavedDiagContext = SrcMgr.getDiagContext(); |
767 | // Set our own handler which calls the saved handler. |
768 | SrcMgr.setDiagHandler(DH: DiagHandler, Ctx: this); |
769 | Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer()); |
770 | // Make MCStreamer aware of the StartTokLoc for locations in diagnostics. |
771 | Out.setStartTokLocPtr(&StartTokLoc); |
772 | |
773 | // Initialize the platform / file format parser. |
774 | switch (Ctx.getObjectFileType()) { |
775 | case MCContext::IsCOFF: |
776 | PlatformParser.reset(p: createCOFFAsmParser()); |
777 | break; |
778 | case MCContext::IsMachO: |
779 | PlatformParser.reset(p: createDarwinAsmParser()); |
780 | IsDarwin = true; |
781 | break; |
782 | case MCContext::IsELF: |
783 | PlatformParser.reset(p: createELFAsmParser()); |
784 | break; |
785 | case MCContext::IsGOFF: |
786 | PlatformParser.reset(p: createGOFFAsmParser()); |
787 | break; |
788 | case MCContext::IsSPIRV: |
789 | report_fatal_error( |
790 | reason: "Need to implement createSPIRVAsmParser for SPIRV format." ); |
791 | break; |
792 | case MCContext::IsWasm: |
793 | PlatformParser.reset(p: createWasmAsmParser()); |
794 | break; |
795 | case MCContext::IsXCOFF: |
796 | PlatformParser.reset(p: createXCOFFAsmParser()); |
797 | break; |
798 | case MCContext::IsDXContainer: |
799 | report_fatal_error(reason: "DXContainer is not supported yet" ); |
800 | break; |
801 | } |
802 | |
803 | PlatformParser->Initialize(Parser&: *this); |
804 | initializeDirectiveKindMap(); |
805 | initializeCVDefRangeTypeMap(); |
806 | } |
807 | |
808 | AsmParser::~AsmParser() { |
809 | assert((HadError || ActiveMacros.empty()) && |
810 | "Unexpected active macro instantiation!" ); |
811 | |
812 | // Remove MCStreamer's reference to the parser SMLoc. |
813 | Out.setStartTokLocPtr(nullptr); |
814 | // Restore the saved diagnostics handler and context for use during |
815 | // finalization. |
816 | SrcMgr.setDiagHandler(DH: SavedDiagHandler, Ctx: SavedDiagContext); |
817 | } |
818 | |
819 | void AsmParser::printMacroInstantiations() { |
820 | // Print the active macro instantiation stack. |
821 | for (MacroInstantiation *M : reverse(C&: ActiveMacros)) |
822 | printMessage(Loc: M->InstantiationLoc, Kind: SourceMgr::DK_Note, |
823 | Msg: "while in macro instantiation" ); |
824 | } |
825 | |
826 | void AsmParser::Note(SMLoc L, const Twine &Msg, SMRange Range) { |
827 | printPendingErrors(); |
828 | printMessage(Loc: L, Kind: SourceMgr::DK_Note, Msg, Range); |
829 | printMacroInstantiations(); |
830 | } |
831 | |
832 | bool AsmParser::Warning(SMLoc L, const Twine &Msg, SMRange Range) { |
833 | if(getTargetParser().getTargetOptions().MCNoWarn) |
834 | return false; |
835 | if (getTargetParser().getTargetOptions().MCFatalWarnings) |
836 | return Error(L, Msg, Range); |
837 | printMessage(Loc: L, Kind: SourceMgr::DK_Warning, Msg, Range); |
838 | printMacroInstantiations(); |
839 | return false; |
840 | } |
841 | |
842 | bool AsmParser::printError(SMLoc L, const Twine &Msg, SMRange Range) { |
843 | HadError = true; |
844 | printMessage(Loc: L, Kind: SourceMgr::DK_Error, Msg, Range); |
845 | printMacroInstantiations(); |
846 | return true; |
847 | } |
848 | |
849 | bool AsmParser::enterIncludeFile(const std::string &Filename) { |
850 | std::string IncludedFile; |
851 | unsigned NewBuf = |
852 | SrcMgr.AddIncludeFile(Filename, IncludeLoc: Lexer.getLoc(), IncludedFile); |
853 | if (!NewBuf) |
854 | return true; |
855 | |
856 | CurBuffer = NewBuf; |
857 | Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer()); |
858 | return false; |
859 | } |
860 | |
861 | /// Process the specified .incbin file by searching for it in the include paths |
862 | /// then just emitting the byte contents of the file to the streamer. This |
863 | /// returns true on failure. |
864 | bool AsmParser::processIncbinFile(const std::string &Filename, int64_t Skip, |
865 | const MCExpr *Count, SMLoc Loc) { |
866 | std::string IncludedFile; |
867 | unsigned NewBuf = |
868 | SrcMgr.AddIncludeFile(Filename, IncludeLoc: Lexer.getLoc(), IncludedFile); |
869 | if (!NewBuf) |
870 | return true; |
871 | |
872 | // Pick up the bytes from the file and emit them. |
873 | StringRef Bytes = SrcMgr.getMemoryBuffer(i: NewBuf)->getBuffer(); |
874 | Bytes = Bytes.drop_front(N: Skip); |
875 | if (Count) { |
876 | int64_t Res; |
877 | if (!Count->evaluateAsAbsolute(Res, Asm: getStreamer().getAssemblerPtr())) |
878 | return Error(L: Loc, Msg: "expected absolute expression" ); |
879 | if (Res < 0) |
880 | return Warning(L: Loc, Msg: "negative count has no effect" ); |
881 | Bytes = Bytes.take_front(N: Res); |
882 | } |
883 | getStreamer().emitBytes(Data: Bytes); |
884 | return false; |
885 | } |
886 | |
887 | void AsmParser::jumpToLoc(SMLoc Loc, unsigned InBuffer) { |
888 | CurBuffer = InBuffer ? InBuffer : SrcMgr.FindBufferContainingLoc(Loc); |
889 | Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer(), |
890 | ptr: Loc.getPointer()); |
891 | } |
892 | |
893 | const AsmToken &AsmParser::Lex() { |
894 | if (Lexer.getTok().is(K: AsmToken::Error)) |
895 | Error(L: Lexer.getErrLoc(), Msg: Lexer.getErr()); |
896 | |
897 | // if it's a end of statement with a comment in it |
898 | if (getTok().is(K: AsmToken::EndOfStatement)) { |
899 | // if this is a line comment output it. |
900 | if (!getTok().getString().empty() && getTok().getString().front() != '\n' && |
901 | getTok().getString().front() != '\r' && MAI.preserveAsmComments()) |
902 | Out.addExplicitComment(T: Twine(getTok().getString())); |
903 | } |
904 | |
905 | const AsmToken *tok = &Lexer.Lex(); |
906 | |
907 | // Parse comments here to be deferred until end of next statement. |
908 | while (tok->is(K: AsmToken::Comment)) { |
909 | if (MAI.preserveAsmComments()) |
910 | Out.addExplicitComment(T: Twine(tok->getString())); |
911 | tok = &Lexer.Lex(); |
912 | } |
913 | |
914 | if (tok->is(K: AsmToken::Eof)) { |
915 | // If this is the end of an included file, pop the parent file off the |
916 | // include stack. |
917 | SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(i: CurBuffer); |
918 | if (ParentIncludeLoc != SMLoc()) { |
919 | jumpToLoc(Loc: ParentIncludeLoc); |
920 | return Lex(); |
921 | } |
922 | } |
923 | |
924 | return *tok; |
925 | } |
926 | |
927 | bool AsmParser::enabledGenDwarfForAssembly() { |
928 | // Check whether the user specified -g. |
929 | if (!getContext().getGenDwarfForAssembly()) |
930 | return false; |
931 | // If we haven't encountered any .file directives (which would imply that |
932 | // the assembler source was produced with debug info already) then emit one |
933 | // describing the assembler source file itself. |
934 | if (getContext().getGenDwarfFileNumber() == 0) { |
935 | const MCDwarfFile &RootFile = |
936 | getContext().getMCDwarfLineTable(/*CUID=*/0).getRootFile(); |
937 | getContext().setGenDwarfFileNumber(getStreamer().emitDwarfFileDirective( |
938 | /*CUID=*/FileNo: 0, Directory: getContext().getCompilationDir(), Filename: RootFile.Name, |
939 | Checksum: RootFile.Checksum, Source: RootFile.Source)); |
940 | } |
941 | return true; |
942 | } |
943 | |
944 | bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) { |
945 | LTODiscardSymbols.clear(); |
946 | |
947 | // Create the initial section, if requested. |
948 | if (!NoInitialTextSection) |
949 | Out.initSections(NoExecStack: false, STI: getTargetParser().getSTI()); |
950 | |
951 | // Prime the lexer. |
952 | Lex(); |
953 | |
954 | HadError = false; |
955 | AsmCond StartingCondState = TheCondState; |
956 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
957 | |
958 | // If we are generating dwarf for assembly source files save the initial text |
959 | // section. (Don't use enabledGenDwarfForAssembly() here, as we aren't |
960 | // emitting any actual debug info yet and haven't had a chance to parse any |
961 | // embedded .file directives.) |
962 | if (getContext().getGenDwarfForAssembly()) { |
963 | MCSection *Sec = getStreamer().getCurrentSectionOnly(); |
964 | if (!Sec->getBeginSymbol()) { |
965 | MCSymbol *SectionStartSym = getContext().createTempSymbol(); |
966 | getStreamer().emitLabel(Symbol: SectionStartSym); |
967 | Sec->setBeginSymbol(SectionStartSym); |
968 | } |
969 | bool InsertResult = getContext().addGenDwarfSection(Sec); |
970 | assert(InsertResult && ".text section should not have debug info yet" ); |
971 | (void)InsertResult; |
972 | } |
973 | |
974 | getTargetParser().onBeginOfFile(); |
975 | |
976 | // While we have input, parse each statement. |
977 | while (Lexer.isNot(K: AsmToken::Eof)) { |
978 | ParseStatementInfo Info(&AsmStrRewrites); |
979 | bool HasError = parseStatement(Info, SI: nullptr); |
980 | |
981 | // If we have a Lexer Error we are on an Error Token. Load in Lexer Error |
982 | // for printing ErrMsg via Lex() only if no (presumably better) parser error |
983 | // exists. |
984 | if (HasError && !hasPendingError() && Lexer.getTok().is(K: AsmToken::Error)) |
985 | Lex(); |
986 | |
987 | // parseStatement returned true so may need to emit an error. |
988 | printPendingErrors(); |
989 | |
990 | // Skipping to the next line if needed. |
991 | if (HasError && !getLexer().justConsumedEOL()) |
992 | eatToEndOfStatement(); |
993 | } |
994 | |
995 | getTargetParser().onEndOfFile(); |
996 | printPendingErrors(); |
997 | |
998 | // All errors should have been emitted. |
999 | assert(!hasPendingError() && "unexpected error from parseStatement" ); |
1000 | |
1001 | if (TheCondState.TheCond != StartingCondState.TheCond || |
1002 | TheCondState.Ignore != StartingCondState.Ignore) |
1003 | printError(L: getTok().getLoc(), Msg: "unmatched .ifs or .elses" ); |
1004 | // Check to see there are no empty DwarfFile slots. |
1005 | const auto &LineTables = getContext().getMCDwarfLineTables(); |
1006 | if (!LineTables.empty()) { |
1007 | unsigned Index = 0; |
1008 | for (const auto &File : LineTables.begin()->second.getMCDwarfFiles()) { |
1009 | if (File.Name.empty() && Index != 0) |
1010 | printError(L: getTok().getLoc(), Msg: "unassigned file number: " + |
1011 | Twine(Index) + |
1012 | " for .file directives" ); |
1013 | ++Index; |
1014 | } |
1015 | } |
1016 | |
1017 | // Check to see that all assembler local symbols were actually defined. |
1018 | // Targets that don't do subsections via symbols may not want this, though, |
1019 | // so conservatively exclude them. Only do this if we're finalizing, though, |
1020 | // as otherwise we won't necessarily have seen everything yet. |
1021 | if (!NoFinalize) { |
1022 | if (MAI.hasSubsectionsViaSymbols()) { |
1023 | for (const auto &TableEntry : getContext().getSymbols()) { |
1024 | MCSymbol *Sym = TableEntry.getValue().Symbol; |
1025 | // Variable symbols may not be marked as defined, so check those |
1026 | // explicitly. If we know it's a variable, we have a definition for |
1027 | // the purposes of this check. |
1028 | if (Sym && Sym->isTemporary() && !Sym->isVariable() && |
1029 | !Sym->isDefined()) |
1030 | // FIXME: We would really like to refer back to where the symbol was |
1031 | // first referenced for a source location. We need to add something |
1032 | // to track that. Currently, we just point to the end of the file. |
1033 | printError(L: getTok().getLoc(), Msg: "assembler local symbol '" + |
1034 | Sym->getName() + "' not defined" ); |
1035 | } |
1036 | } |
1037 | |
1038 | // Temporary symbols like the ones for directional jumps don't go in the |
1039 | // symbol table. They also need to be diagnosed in all (final) cases. |
1040 | for (std::tuple<SMLoc, CppHashInfoTy, MCSymbol *> &LocSym : DirLabels) { |
1041 | if (std::get<2>(t&: LocSym)->isUndefined()) { |
1042 | // Reset the state of any "# line file" directives we've seen to the |
1043 | // context as it was at the diagnostic site. |
1044 | CppHashInfo = std::get<1>(t&: LocSym); |
1045 | printError(L: std::get<0>(t&: LocSym), Msg: "directional label undefined" ); |
1046 | } |
1047 | } |
1048 | } |
1049 | // Finalize the output stream if there are no errors and if the client wants |
1050 | // us to. |
1051 | if (!HadError && !NoFinalize) { |
1052 | if (auto *TS = Out.getTargetStreamer()) |
1053 | TS->emitConstantPools(); |
1054 | |
1055 | Out.finish(EndLoc: Lexer.getLoc()); |
1056 | } |
1057 | |
1058 | return HadError || getContext().hadError(); |
1059 | } |
1060 | |
1061 | bool AsmParser::checkForValidSection() { |
1062 | if (!ParsingMSInlineAsm && !getStreamer().getCurrentFragment()) { |
1063 | Out.initSections(NoExecStack: false, STI: getTargetParser().getSTI()); |
1064 | return Error(L: getTok().getLoc(), |
1065 | Msg: "expected section directive before assembly directive" ); |
1066 | } |
1067 | return false; |
1068 | } |
1069 | |
1070 | /// Throw away the rest of the line for testing purposes. |
1071 | void AsmParser::eatToEndOfStatement() { |
1072 | while (Lexer.isNot(K: AsmToken::EndOfStatement) && Lexer.isNot(K: AsmToken::Eof)) |
1073 | Lexer.Lex(); |
1074 | |
1075 | // Eat EOL. |
1076 | if (Lexer.is(K: AsmToken::EndOfStatement)) |
1077 | Lexer.Lex(); |
1078 | } |
1079 | |
1080 | StringRef AsmParser::parseStringToEndOfStatement() { |
1081 | const char *Start = getTok().getLoc().getPointer(); |
1082 | |
1083 | while (Lexer.isNot(K: AsmToken::EndOfStatement) && Lexer.isNot(K: AsmToken::Eof)) |
1084 | Lexer.Lex(); |
1085 | |
1086 | const char *End = getTok().getLoc().getPointer(); |
1087 | return StringRef(Start, End - Start); |
1088 | } |
1089 | |
1090 | StringRef AsmParser::parseStringToComma() { |
1091 | const char *Start = getTok().getLoc().getPointer(); |
1092 | |
1093 | while (Lexer.isNot(K: AsmToken::EndOfStatement) && |
1094 | Lexer.isNot(K: AsmToken::Comma) && Lexer.isNot(K: AsmToken::Eof)) |
1095 | Lexer.Lex(); |
1096 | |
1097 | const char *End = getTok().getLoc().getPointer(); |
1098 | return StringRef(Start, End - Start); |
1099 | } |
1100 | |
1101 | /// Parse a paren expression and return it. |
1102 | /// NOTE: This assumes the leading '(' has already been consumed. |
1103 | /// |
1104 | /// parenexpr ::= expr) |
1105 | /// |
1106 | bool AsmParser::parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
1107 | if (parseExpression(Res)) |
1108 | return true; |
1109 | EndLoc = Lexer.getTok().getEndLoc(); |
1110 | return parseRParen(); |
1111 | } |
1112 | |
1113 | /// Parse a bracket expression and return it. |
1114 | /// NOTE: This assumes the leading '[' has already been consumed. |
1115 | /// |
1116 | /// bracketexpr ::= expr] |
1117 | /// |
1118 | bool AsmParser::parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc) { |
1119 | if (parseExpression(Res)) |
1120 | return true; |
1121 | EndLoc = getTok().getEndLoc(); |
1122 | if (parseToken(T: AsmToken::RBrac, Msg: "expected ']' in brackets expression" )) |
1123 | return true; |
1124 | return false; |
1125 | } |
1126 | |
1127 | /// Parse a primary expression and return it. |
1128 | /// primaryexpr ::= (parenexpr |
1129 | /// primaryexpr ::= symbol |
1130 | /// primaryexpr ::= number |
1131 | /// primaryexpr ::= '.' |
1132 | /// primaryexpr ::= ~,+,- primaryexpr |
1133 | bool AsmParser::parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc, |
1134 | AsmTypeInfo *TypeInfo) { |
1135 | SMLoc FirstTokenLoc = getLexer().getLoc(); |
1136 | AsmToken::TokenKind FirstTokenKind = Lexer.getKind(); |
1137 | switch (FirstTokenKind) { |
1138 | default: |
1139 | return TokError(Msg: "unknown token in expression" ); |
1140 | // If we have an error assume that we've already handled it. |
1141 | case AsmToken::Error: |
1142 | return true; |
1143 | case AsmToken::Exclaim: |
1144 | Lex(); // Eat the operator. |
1145 | if (parsePrimaryExpr(Res, EndLoc, TypeInfo)) |
1146 | return true; |
1147 | Res = MCUnaryExpr::createLNot(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc); |
1148 | return false; |
1149 | case AsmToken::Dollar: |
1150 | case AsmToken::Star: |
1151 | case AsmToken::At: |
1152 | case AsmToken::String: |
1153 | case AsmToken::Identifier: { |
1154 | StringRef Identifier; |
1155 | if (parseIdentifier(Res&: Identifier)) { |
1156 | // We may have failed but '$'|'*' may be a valid token in context of |
1157 | // the current PC. |
1158 | if (getTok().is(K: AsmToken::Dollar) || getTok().is(K: AsmToken::Star)) { |
1159 | bool ShouldGenerateTempSymbol = false; |
1160 | if ((getTok().is(K: AsmToken::Dollar) && MAI.getDollarIsPC()) || |
1161 | (getTok().is(K: AsmToken::Star) && MAI.isHLASM())) |
1162 | ShouldGenerateTempSymbol = true; |
1163 | |
1164 | if (!ShouldGenerateTempSymbol) |
1165 | return Error(L: FirstTokenLoc, Msg: "invalid token in expression" ); |
1166 | |
1167 | // Eat the '$'|'*' token. |
1168 | Lex(); |
1169 | // This is either a '$'|'*' reference, which references the current PC. |
1170 | // Emit a temporary label to the streamer and refer to it. |
1171 | MCSymbol *Sym = Ctx.createTempSymbol(); |
1172 | Out.emitLabel(Symbol: Sym); |
1173 | Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext()); |
1174 | EndLoc = FirstTokenLoc; |
1175 | return false; |
1176 | } |
1177 | } |
1178 | // Parse an optional relocation specifier. |
1179 | std::pair<StringRef, StringRef> Split; |
1180 | if (MAI.useAtForSpecifier()) { |
1181 | if (FirstTokenKind == AsmToken::String) { |
1182 | if (Lexer.is(K: AsmToken::At)) { |
1183 | Lex(); // eat @ |
1184 | SMLoc AtLoc = getLexer().getLoc(); |
1185 | StringRef VName; |
1186 | if (parseIdentifier(Res&: VName)) |
1187 | return Error(L: AtLoc, Msg: "expected symbol variant after '@'" ); |
1188 | |
1189 | Split = std::make_pair(x&: Identifier, y&: VName); |
1190 | } |
1191 | } else if (Lexer.getAllowAtInIdentifier()) { |
1192 | Split = Identifier.split(Separator: '@'); |
1193 | } |
1194 | } else if (MAI.useParensForSpecifier() && |
1195 | parseOptionalToken(T: AsmToken::LParen)) { |
1196 | StringRef VName; |
1197 | parseIdentifier(Res&: VName); |
1198 | if (parseRParen()) |
1199 | return true; |
1200 | Split = std::make_pair(x&: Identifier, y&: VName); |
1201 | } |
1202 | |
1203 | EndLoc = SMLoc::getFromPointer(Ptr: Identifier.end()); |
1204 | |
1205 | // This is a symbol reference. |
1206 | StringRef SymbolName = Identifier; |
1207 | if (SymbolName.empty()) |
1208 | return Error(L: getLexer().getLoc(), Msg: "expected a symbol reference" ); |
1209 | |
1210 | // Lookup the @specifier if used. |
1211 | uint16_t Spec = 0; |
1212 | if (!Split.second.empty()) { |
1213 | auto MaybeSpecifier = MAI.getSpecifierForName(Name: Split.second); |
1214 | if (MaybeSpecifier) { |
1215 | SymbolName = Split.first; |
1216 | Spec = *MaybeSpecifier; |
1217 | } else if (!MAI.doesAllowAtInName()) { |
1218 | return Error(L: SMLoc::getFromPointer(Ptr: Split.second.begin()), |
1219 | Msg: "invalid variant '" + Split.second + "'" ); |
1220 | } |
1221 | } |
1222 | |
1223 | MCSymbol *Sym = getContext().getInlineAsmLabel(Name: SymbolName); |
1224 | if (!Sym) |
1225 | Sym = getContext().getOrCreateSymbol(Name: MAI.isHLASM() ? SymbolName.upper() |
1226 | : SymbolName); |
1227 | |
1228 | // If this is an absolute variable reference, substitute it now to preserve |
1229 | // semantics in the face of reassignment. |
1230 | if (Sym->isVariable()) { |
1231 | auto V = Sym->getVariableValue(); |
1232 | bool DoInline = isa<MCConstantExpr>(Val: V) && !Spec; |
1233 | if (auto TV = dyn_cast<MCTargetExpr>(Val: V)) |
1234 | DoInline = TV->inlineAssignedExpr(); |
1235 | if (DoInline) { |
1236 | if (Spec) |
1237 | return Error(L: EndLoc, Msg: "unexpected modifier on variable reference" ); |
1238 | Res = Sym->getVariableValue(); |
1239 | return false; |
1240 | } |
1241 | } |
1242 | |
1243 | // Otherwise create a symbol ref. |
1244 | Res = MCSymbolRefExpr::create(Symbol: Sym, specifier: Spec, Ctx&: getContext(), Loc: FirstTokenLoc); |
1245 | return false; |
1246 | } |
1247 | case AsmToken::BigNum: |
1248 | return TokError(Msg: "literal value out of range for directive" ); |
1249 | case AsmToken::Integer: { |
1250 | SMLoc Loc = getTok().getLoc(); |
1251 | int64_t IntVal = getTok().getIntVal(); |
1252 | Res = MCConstantExpr::create(Value: IntVal, Ctx&: getContext()); |
1253 | EndLoc = Lexer.getTok().getEndLoc(); |
1254 | Lex(); // Eat token. |
1255 | // Look for 'b' or 'f' following an Integer as a directional label |
1256 | if (Lexer.getKind() == AsmToken::Identifier) { |
1257 | StringRef IDVal = getTok().getString(); |
1258 | // Lookup the symbol variant if used. |
1259 | std::pair<StringRef, StringRef> Split = IDVal.split(Separator: '@'); |
1260 | uint16_t Spec = 0; |
1261 | if (Split.first.size() != IDVal.size()) { |
1262 | auto MaybeSpec = MAI.getSpecifierForName(Name: Split.second); |
1263 | if (!MaybeSpec) |
1264 | return TokError(Msg: "invalid variant '" + Split.second + "'" ); |
1265 | IDVal = Split.first; |
1266 | Spec = *MaybeSpec; |
1267 | } |
1268 | if (IDVal == "f" || IDVal == "b" ) { |
1269 | MCSymbol *Sym = |
1270 | Ctx.getDirectionalLocalSymbol(LocalLabelVal: IntVal, Before: IDVal == "b" ); |
1271 | Res = MCSymbolRefExpr::create(Symbol: Sym, specifier: Spec, Ctx&: getContext(), Loc); |
1272 | if (IDVal == "b" && Sym->isUndefined()) |
1273 | return Error(L: Loc, Msg: "directional label undefined" ); |
1274 | DirLabels.push_back(Elt: std::make_tuple(args&: Loc, args&: CppHashInfo, args&: Sym)); |
1275 | EndLoc = Lexer.getTok().getEndLoc(); |
1276 | Lex(); // Eat identifier. |
1277 | } |
1278 | } |
1279 | return false; |
1280 | } |
1281 | case AsmToken::Real: { |
1282 | APFloat RealVal(APFloat::IEEEdouble(), getTok().getString()); |
1283 | uint64_t IntVal = RealVal.bitcastToAPInt().getZExtValue(); |
1284 | Res = MCConstantExpr::create(Value: IntVal, Ctx&: getContext()); |
1285 | EndLoc = Lexer.getTok().getEndLoc(); |
1286 | Lex(); // Eat token. |
1287 | return false; |
1288 | } |
1289 | case AsmToken::Dot: { |
1290 | if (MAI.isHLASM()) |
1291 | return TokError(Msg: "cannot use . as current PC" ); |
1292 | |
1293 | // This is a '.' reference, which references the current PC. Emit a |
1294 | // temporary label to the streamer and refer to it. |
1295 | MCSymbol *Sym = Ctx.createTempSymbol(); |
1296 | Out.emitLabel(Symbol: Sym); |
1297 | Res = MCSymbolRefExpr::create(Symbol: Sym, Ctx&: getContext()); |
1298 | EndLoc = Lexer.getTok().getEndLoc(); |
1299 | Lex(); // Eat identifier. |
1300 | return false; |
1301 | } |
1302 | case AsmToken::LParen: |
1303 | Lex(); // Eat the '('. |
1304 | return parseParenExpr(Res, EndLoc); |
1305 | case AsmToken::LBrac: |
1306 | if (!PlatformParser->HasBracketExpressions()) |
1307 | return TokError(Msg: "brackets expression not supported on this target" ); |
1308 | Lex(); // Eat the '['. |
1309 | return parseBracketExpr(Res, EndLoc); |
1310 | case AsmToken::Minus: |
1311 | Lex(); // Eat the operator. |
1312 | if (parsePrimaryExpr(Res, EndLoc, TypeInfo)) |
1313 | return true; |
1314 | Res = MCUnaryExpr::createMinus(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc); |
1315 | return false; |
1316 | case AsmToken::Plus: |
1317 | Lex(); // Eat the operator. |
1318 | if (parsePrimaryExpr(Res, EndLoc, TypeInfo)) |
1319 | return true; |
1320 | Res = MCUnaryExpr::createPlus(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc); |
1321 | return false; |
1322 | case AsmToken::Tilde: |
1323 | Lex(); // Eat the operator. |
1324 | if (parsePrimaryExpr(Res, EndLoc, TypeInfo)) |
1325 | return true; |
1326 | Res = MCUnaryExpr::createNot(Expr: Res, Ctx&: getContext(), Loc: FirstTokenLoc); |
1327 | return false; |
1328 | } |
1329 | } |
1330 | |
1331 | bool AsmParser::parseExpression(const MCExpr *&Res) { |
1332 | SMLoc EndLoc; |
1333 | return parseExpression(Res, EndLoc); |
1334 | } |
1335 | |
1336 | const MCExpr *MCAsmParser::applySpecifier(const MCExpr *E, uint32_t Spec) { |
1337 | // Ask the target implementation about this expression first. |
1338 | const MCExpr *NewE = getTargetParser().applySpecifier(E, Spec, Ctx); |
1339 | if (NewE) |
1340 | return NewE; |
1341 | // Recurse over the given expression, rebuilding it to apply the given variant |
1342 | // if there is exactly one symbol. |
1343 | switch (E->getKind()) { |
1344 | case MCExpr::Specifier: |
1345 | llvm_unreachable("cannot apply another specifier to MCSpecifierExpr" ); |
1346 | case MCExpr::Target: |
1347 | case MCExpr::Constant: |
1348 | return nullptr; |
1349 | |
1350 | case MCExpr::SymbolRef: { |
1351 | const MCSymbolRefExpr *SRE = cast<MCSymbolRefExpr>(Val: E); |
1352 | |
1353 | if (SRE->getSpecifier()) { |
1354 | TokError(Msg: "invalid variant on expression '" + getTok().getIdentifier() + |
1355 | "' (already modified)" ); |
1356 | return E; |
1357 | } |
1358 | |
1359 | return MCSymbolRefExpr::create(Symbol: &SRE->getSymbol(), specifier: Spec, Ctx&: getContext()); |
1360 | } |
1361 | |
1362 | case MCExpr::Unary: { |
1363 | const MCUnaryExpr *UE = cast<MCUnaryExpr>(Val: E); |
1364 | const MCExpr *Sub = applySpecifier(E: UE->getSubExpr(), Spec); |
1365 | if (!Sub) |
1366 | return nullptr; |
1367 | return MCUnaryExpr::create(Op: UE->getOpcode(), Expr: Sub, Ctx&: getContext()); |
1368 | } |
1369 | |
1370 | case MCExpr::Binary: { |
1371 | const MCBinaryExpr *BE = cast<MCBinaryExpr>(Val: E); |
1372 | const MCExpr *LHS = applySpecifier(E: BE->getLHS(), Spec); |
1373 | const MCExpr *RHS = applySpecifier(E: BE->getRHS(), Spec); |
1374 | |
1375 | if (!LHS && !RHS) |
1376 | return nullptr; |
1377 | |
1378 | if (!LHS) |
1379 | LHS = BE->getLHS(); |
1380 | if (!RHS) |
1381 | RHS = BE->getRHS(); |
1382 | |
1383 | return MCBinaryExpr::create(Op: BE->getOpcode(), LHS, RHS, Ctx&: getContext()); |
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. |
1401 | static 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 '!'. |
1419 | static 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 | |
1429 | bool 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 | /// |
1456 | bool 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 | |
1493 | bool AsmParser::parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) { |
1494 | Res = nullptr; |
1495 | return parseParenExpr(Res, EndLoc) || parseBinOpRHS(Precedence: 1, Res, EndLoc); |
1496 | } |
1497 | |
1498 | bool 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 | |
1511 | static 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 | |
1587 | static 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 | |
1670 | unsigned 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. |
1679 | bool 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 |
1714 | bool 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().getOrCreateSymbol(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 = 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.hasSubsectionsViaSymbols() && CFIStartProcLoc && |
1874 | Sym->isExternal() && !cast<MCSymbolMachO>(Val: Sym)->isAltEntry()) |
1875 | return Error(L: StartTokLoc, Msg: "non-private labels cannot appear between " |
1876 | ".cfi_startproc / .cfi_endproc pairs" ) && |
1877 | Error(L: *CFIStartProcLoc, Msg: "previous .cfi_startproc was here" ); |
1878 | |
1879 | if (discardLTOSymbol(Name: IDVal)) |
1880 | return false; |
1881 | |
1882 | getTargetParser().doBeforeLabelEmit(Symbol: Sym, IDLoc); |
1883 | |
1884 | // Emit the label. |
1885 | if (!getTargetParser().isParsingMSInlineAsm()) |
1886 | Out.emitLabel(Symbol: Sym, Loc: IDLoc); |
1887 | |
1888 | // If we are generating dwarf for assembly source files then gather the |
1889 | // info to make a dwarf label entry for this label if needed. |
1890 | if (enabledGenDwarfForAssembly()) |
1891 | MCGenDwarfLabelEntry::Make(Symbol: Sym, MCOS: &getStreamer(), SrcMgr&: getSourceManager(), |
1892 | Loc&: IDLoc); |
1893 | |
1894 | getTargetParser().onLabelParsed(Symbol: Sym); |
1895 | |
1896 | return false; |
1897 | } |
1898 | |
1899 | // Check for an assignment statement. |
1900 | // ::= identifier '=' |
1901 | if (Lexer.is(K: AsmToken::Equal) && getTargetParser().equalIsAsmAssignment()) { |
1902 | Lex(); |
1903 | return parseAssignment(Name: IDVal, Kind: AssignmentKind::Equal); |
1904 | } |
1905 | |
1906 | // If macros are enabled, check to see if this is a macro instantiation. |
1907 | if (areMacrosEnabled()) |
1908 | if (MCAsmMacro *M = getContext().lookupMacro(Name: IDVal)) |
1909 | return handleMacroEntry(M, NameLoc: IDLoc); |
1910 | |
1911 | // Otherwise, we have a normal instruction or directive. |
1912 | |
1913 | // Directives start with "." |
1914 | if (IDVal.starts_with(Prefix: "." ) && IDVal != "." ) { |
1915 | // There are several entities interested in parsing directives: |
1916 | // |
1917 | // 1. The target-specific assembly parser. Some directives are target |
1918 | // specific or may potentially behave differently on certain targets. |
1919 | // 2. Asm parser extensions. For example, platform-specific parsers |
1920 | // (like the ELF parser) register themselves as extensions. |
1921 | // 3. The generic directive parser implemented by this class. These are |
1922 | // all the directives that behave in a target and platform independent |
1923 | // manner, or at least have a default behavior that's shared between |
1924 | // all targets and platforms. |
1925 | |
1926 | getTargetParser().flushPendingInstructions(Out&: getStreamer()); |
1927 | |
1928 | ParseStatus TPDirectiveReturn = getTargetParser().parseDirective(DirectiveID: ID); |
1929 | assert(TPDirectiveReturn.isFailure() == hasPendingError() && |
1930 | "Should only return Failure iff there was an error" ); |
1931 | if (TPDirectiveReturn.isFailure()) |
1932 | return true; |
1933 | if (TPDirectiveReturn.isSuccess()) |
1934 | return false; |
1935 | |
1936 | // Next, check the extension directive map to see if any extension has |
1937 | // registered itself to parse this directive. |
1938 | std::pair<MCAsmParserExtension *, DirectiveHandler> Handler = |
1939 | ExtensionDirectiveMap.lookup(Key: IDVal); |
1940 | if (Handler.first) |
1941 | return (*Handler.second)(Handler.first, IDVal, IDLoc); |
1942 | |
1943 | // Finally, if no one else is interested in this directive, it must be |
1944 | // generic and familiar to this class. |
1945 | switch (DirKind) { |
1946 | default: |
1947 | break; |
1948 | case DK_SET: |
1949 | case DK_EQU: |
1950 | return parseDirectiveSet(IDVal, Kind: AssignmentKind::Set); |
1951 | case DK_EQUIV: |
1952 | return parseDirectiveSet(IDVal, Kind: AssignmentKind::Equiv); |
1953 | case DK_LTO_SET_CONDITIONAL: |
1954 | return parseDirectiveSet(IDVal, Kind: AssignmentKind::LTOSetConditional); |
1955 | case DK_ASCII: |
1956 | return parseDirectiveAscii(IDVal, ZeroTerminated: false); |
1957 | case DK_ASCIZ: |
1958 | case DK_STRING: |
1959 | return parseDirectiveAscii(IDVal, ZeroTerminated: true); |
1960 | case DK_BYTE: |
1961 | case DK_DC_B: |
1962 | return parseDirectiveValue(IDVal, Size: 1); |
1963 | case DK_DC: |
1964 | case DK_DC_W: |
1965 | case DK_SHORT: |
1966 | case DK_VALUE: |
1967 | case DK_2BYTE: |
1968 | return parseDirectiveValue(IDVal, Size: 2); |
1969 | case DK_LONG: |
1970 | case DK_INT: |
1971 | case DK_4BYTE: |
1972 | case DK_DC_L: |
1973 | return parseDirectiveValue(IDVal, Size: 4); |
1974 | case DK_QUAD: |
1975 | case DK_8BYTE: |
1976 | return parseDirectiveValue(IDVal, Size: 8); |
1977 | case DK_DC_A: |
1978 | return parseDirectiveValue( |
1979 | IDVal, Size: getContext().getAsmInfo()->getCodePointerSize()); |
1980 | case DK_OCTA: |
1981 | return parseDirectiveOctaValue(IDVal); |
1982 | case DK_SINGLE: |
1983 | case DK_FLOAT: |
1984 | case DK_DC_S: |
1985 | return parseDirectiveRealValue(IDVal, APFloat::IEEEsingle()); |
1986 | case DK_DOUBLE: |
1987 | case DK_DC_D: |
1988 | return parseDirectiveRealValue(IDVal, APFloat::IEEEdouble()); |
1989 | case DK_ALIGN: { |
1990 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
1991 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/ValueSize: 1); |
1992 | } |
1993 | case DK_ALIGN32: { |
1994 | bool IsPow2 = !getContext().getAsmInfo()->getAlignmentIsInBytes(); |
1995 | return parseDirectiveAlign(IsPow2, /*ExprSize=*/ValueSize: 4); |
1996 | } |
1997 | case DK_BALIGN: |
1998 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/ValueSize: 1); |
1999 | case DK_BALIGNW: |
2000 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/ValueSize: 2); |
2001 | case DK_BALIGNL: |
2002 | return parseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/ValueSize: 4); |
2003 | case DK_P2ALIGN: |
2004 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/ValueSize: 1); |
2005 | case DK_P2ALIGNW: |
2006 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/ValueSize: 2); |
2007 | case DK_P2ALIGNL: |
2008 | return parseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/ValueSize: 4); |
2009 | case DK_ORG: |
2010 | return parseDirectiveOrg(); |
2011 | case DK_FILL: |
2012 | return parseDirectiveFill(); |
2013 | case DK_ZERO: |
2014 | return parseDirectiveZero(); |
2015 | case DK_EXTERN: |
2016 | eatToEndOfStatement(); // .extern is the default, ignore it. |
2017 | return false; |
2018 | case DK_GLOBL: |
2019 | case DK_GLOBAL: |
2020 | return parseDirectiveSymbolAttribute(Attr: MCSA_Global); |
2021 | case DK_LAZY_REFERENCE: |
2022 | return parseDirectiveSymbolAttribute(Attr: MCSA_LazyReference); |
2023 | case DK_NO_DEAD_STRIP: |
2024 | return parseDirectiveSymbolAttribute(Attr: MCSA_NoDeadStrip); |
2025 | case DK_SYMBOL_RESOLVER: |
2026 | return parseDirectiveSymbolAttribute(Attr: MCSA_SymbolResolver); |
2027 | case DK_PRIVATE_EXTERN: |
2028 | return parseDirectiveSymbolAttribute(Attr: MCSA_PrivateExtern); |
2029 | case DK_REFERENCE: |
2030 | return parseDirectiveSymbolAttribute(Attr: MCSA_Reference); |
2031 | case DK_WEAK_DEFINITION: |
2032 | return parseDirectiveSymbolAttribute(Attr: MCSA_WeakDefinition); |
2033 | case DK_WEAK_REFERENCE: |
2034 | return parseDirectiveSymbolAttribute(Attr: MCSA_WeakReference); |
2035 | case DK_WEAK_DEF_CAN_BE_HIDDEN: |
2036 | return parseDirectiveSymbolAttribute(Attr: MCSA_WeakDefAutoPrivate); |
2037 | case DK_COLD: |
2038 | return parseDirectiveSymbolAttribute(Attr: MCSA_Cold); |
2039 | case DK_COMM: |
2040 | case DK_COMMON: |
2041 | return parseDirectiveComm(/*IsLocal=*/false); |
2042 | case DK_LCOMM: |
2043 | return parseDirectiveComm(/*IsLocal=*/true); |
2044 | case DK_ABORT: |
2045 | return parseDirectiveAbort(DirectiveLoc: IDLoc); |
2046 | case DK_INCLUDE: |
2047 | return parseDirectiveInclude(); |
2048 | case DK_INCBIN: |
2049 | return parseDirectiveIncbin(); |
2050 | case DK_CODE16: |
2051 | case DK_CODE16GCC: |
2052 | return TokError(Msg: Twine(IDVal) + |
2053 | " not currently supported for this target" ); |
2054 | case DK_REPT: |
2055 | return parseDirectiveRept(DirectiveLoc: IDLoc, Directive: IDVal); |
2056 | case DK_IRP: |
2057 | return parseDirectiveIrp(DirectiveLoc: IDLoc); |
2058 | case DK_IRPC: |
2059 | return parseDirectiveIrpc(DirectiveLoc: IDLoc); |
2060 | case DK_ENDR: |
2061 | return parseDirectiveEndr(DirectiveLoc: IDLoc); |
2062 | case DK_BUNDLE_ALIGN_MODE: |
2063 | return parseDirectiveBundleAlignMode(); |
2064 | case DK_BUNDLE_LOCK: |
2065 | return parseDirectiveBundleLock(); |
2066 | case DK_BUNDLE_UNLOCK: |
2067 | return parseDirectiveBundleUnlock(); |
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 | |
2244 | bool 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 |
2317 | bool |
2318 | AsmParser::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" |
2336 | bool AsmParser::(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. |
2380 | void 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. |
2435 | static bool isIdentifierChar(char c) { |
2436 | return isalnum(static_cast<unsigned char>(c)) || c == '_' || c == '$' || |
2437 | c == '.'; |
2438 | } |
2439 | |
2440 | bool 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 | |
2571 | static 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 | |
2601 | namespace { |
2602 | |
2603 | class AsmLexerSkipSpaceRAII { |
2604 | public: |
2605 | AsmLexerSkipSpaceRAII(AsmLexer &Lexer, bool SkipSpace) : Lexer(Lexer) { |
2606 | Lexer.setSkipSpace(SkipSpace); |
2607 | } |
2608 | |
2609 | ~AsmLexerSkipSpaceRAII() { |
2610 | Lexer.setSkipSpace(true); |
2611 | } |
2612 | |
2613 | private: |
2614 | AsmLexer &Lexer; |
2615 | }; |
2616 | |
2617 | } // end anonymous namespace |
2618 | |
2619 | bool 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. |
2688 | bool 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 | |
2805 | bool 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 | |
2855 | void 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 | |
2869 | bool 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 |
2913 | bool 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 |
2958 | bool 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 | |
2966 | bool 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 | |
3051 | bool 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" )* ] |
3069 | bool 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 | /// parseDirectiveReloc |
3090 | /// ::= .reloc expression , identifier [ , expression ] |
3091 | bool AsmParser::parseDirectiveReloc(SMLoc DirectiveLoc) { |
3092 | const MCExpr *Offset; |
3093 | const MCExpr *Expr = nullptr; |
3094 | SMLoc OffsetLoc = Lexer.getTok().getLoc(); |
3095 | |
3096 | if (parseExpression(Res&: Offset)) |
3097 | return true; |
3098 | if (parseComma() || |
3099 | check(P: getTok().isNot(K: AsmToken::Identifier), Msg: "expected relocation name" )) |
3100 | return true; |
3101 | |
3102 | SMLoc NameLoc = Lexer.getTok().getLoc(); |
3103 | StringRef Name = Lexer.getTok().getIdentifier(); |
3104 | Lex(); |
3105 | |
3106 | if (Lexer.is(K: AsmToken::Comma)) { |
3107 | Lex(); |
3108 | SMLoc ExprLoc = Lexer.getLoc(); |
3109 | if (parseExpression(Res&: Expr)) |
3110 | return true; |
3111 | |
3112 | MCValue Value; |
3113 | if (!Expr->evaluateAsRelocatable(Res&: Value, Asm: nullptr)) |
3114 | return Error(L: ExprLoc, Msg: "expression must be relocatable" ); |
3115 | } |
3116 | |
3117 | if (parseEOL()) |
3118 | return true; |
3119 | |
3120 | const MCTargetAsmParser &MCT = getTargetParser(); |
3121 | const MCSubtargetInfo &STI = MCT.getSTI(); |
3122 | if (std::optional<std::pair<bool, std::string>> Err = |
3123 | getStreamer().emitRelocDirective(Offset: *Offset, Name, Expr, Loc: DirectiveLoc, |
3124 | STI)) |
3125 | return Error(L: Err->first ? NameLoc : OffsetLoc, Msg: Err->second); |
3126 | |
3127 | return false; |
3128 | } |
3129 | |
3130 | /// parseDirectiveValue |
3131 | /// ::= (.byte | .short | ... ) [ expression (, expression)* ] |
3132 | bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) { |
3133 | auto parseOp = [&]() -> bool { |
3134 | const MCExpr *Value; |
3135 | SMLoc ExprLoc = getLexer().getLoc(); |
3136 | if (checkForValidSection() || getTargetParser().parseDataExpr(Res&: Value)) |
3137 | return true; |
3138 | // Special case constant expressions to match code generator. |
3139 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) { |
3140 | assert(Size <= 8 && "Invalid size" ); |
3141 | uint64_t IntValue = MCE->getValue(); |
3142 | if (!isUIntN(N: 8 * Size, x: IntValue) && !isIntN(N: 8 * Size, x: IntValue)) |
3143 | return Error(L: ExprLoc, Msg: "out of range literal value" ); |
3144 | getStreamer().emitIntValue(Value: IntValue, Size); |
3145 | } else |
3146 | getStreamer().emitValue(Value, Size, Loc: ExprLoc); |
3147 | return false; |
3148 | }; |
3149 | |
3150 | return parseMany(parseOne: parseOp); |
3151 | } |
3152 | |
3153 | static bool parseHexOcta(AsmParser &Asm, uint64_t &hi, uint64_t &lo) { |
3154 | if (Asm.getTok().isNot(K: AsmToken::Integer) && |
3155 | Asm.getTok().isNot(K: AsmToken::BigNum)) |
3156 | return Asm.TokError(Msg: "unknown token in expression" ); |
3157 | SMLoc ExprLoc = Asm.getTok().getLoc(); |
3158 | APInt IntValue = Asm.getTok().getAPIntVal(); |
3159 | Asm.Lex(); |
3160 | if (!IntValue.isIntN(N: 128)) |
3161 | return Asm.Error(L: ExprLoc, Msg: "out of range literal value" ); |
3162 | if (!IntValue.isIntN(N: 64)) { |
3163 | hi = IntValue.getHiBits(numBits: IntValue.getBitWidth() - 64).getZExtValue(); |
3164 | lo = IntValue.getLoBits(numBits: 64).getZExtValue(); |
3165 | } else { |
3166 | hi = 0; |
3167 | lo = IntValue.getZExtValue(); |
3168 | } |
3169 | return false; |
3170 | } |
3171 | |
3172 | /// ParseDirectiveOctaValue |
3173 | /// ::= .octa [ hexconstant (, hexconstant)* ] |
3174 | |
3175 | bool AsmParser::parseDirectiveOctaValue(StringRef IDVal) { |
3176 | auto parseOp = [&]() -> bool { |
3177 | if (checkForValidSection()) |
3178 | return true; |
3179 | uint64_t hi, lo; |
3180 | if (parseHexOcta(Asm&: *this, hi, lo)) |
3181 | return true; |
3182 | if (MAI.isLittleEndian()) { |
3183 | getStreamer().emitInt64(Value: lo); |
3184 | getStreamer().emitInt64(Value: hi); |
3185 | } else { |
3186 | getStreamer().emitInt64(Value: hi); |
3187 | getStreamer().emitInt64(Value: lo); |
3188 | } |
3189 | return false; |
3190 | }; |
3191 | |
3192 | return parseMany(parseOne: parseOp); |
3193 | } |
3194 | |
3195 | bool AsmParser::parseRealValue(const fltSemantics &Semantics, APInt &Res) { |
3196 | // We don't truly support arithmetic on floating point expressions, so we |
3197 | // have to manually parse unary prefixes. |
3198 | bool IsNeg = false; |
3199 | if (getLexer().is(K: AsmToken::Minus)) { |
3200 | Lexer.Lex(); |
3201 | IsNeg = true; |
3202 | } else if (getLexer().is(K: AsmToken::Plus)) |
3203 | Lexer.Lex(); |
3204 | |
3205 | if (Lexer.is(K: AsmToken::Error)) |
3206 | return TokError(Msg: Lexer.getErr()); |
3207 | if (Lexer.isNot(K: AsmToken::Integer) && Lexer.isNot(K: AsmToken::Real) && |
3208 | Lexer.isNot(K: AsmToken::Identifier)) |
3209 | return TokError(Msg: "unexpected token in directive" ); |
3210 | |
3211 | // Convert to an APFloat. |
3212 | APFloat Value(Semantics); |
3213 | StringRef IDVal = getTok().getString(); |
3214 | if (getLexer().is(K: AsmToken::Identifier)) { |
3215 | if (!IDVal.compare_insensitive(RHS: "infinity" ) || |
3216 | !IDVal.compare_insensitive(RHS: "inf" )) |
3217 | Value = APFloat::getInf(Sem: Semantics); |
3218 | else if (!IDVal.compare_insensitive(RHS: "nan" )) |
3219 | Value = APFloat::getNaN(Sem: Semantics, Negative: false, payload: ~0); |
3220 | else |
3221 | return TokError(Msg: "invalid floating point literal" ); |
3222 | } else if (errorToBool( |
3223 | Err: Value.convertFromString(IDVal, APFloat::rmNearestTiesToEven) |
3224 | .takeError())) |
3225 | return TokError(Msg: "invalid floating point literal" ); |
3226 | if (IsNeg) |
3227 | Value.changeSign(); |
3228 | |
3229 | // Consume the numeric token. |
3230 | Lex(); |
3231 | |
3232 | Res = Value.bitcastToAPInt(); |
3233 | |
3234 | return false; |
3235 | } |
3236 | |
3237 | /// parseDirectiveRealValue |
3238 | /// ::= (.single | .double) [ expression (, expression)* ] |
3239 | bool AsmParser::parseDirectiveRealValue(StringRef IDVal, |
3240 | const fltSemantics &Semantics) { |
3241 | auto parseOp = [&]() -> bool { |
3242 | APInt AsInt; |
3243 | if (checkForValidSection() || parseRealValue(Semantics, Res&: AsInt)) |
3244 | return true; |
3245 | getStreamer().emitIntValue(Value: AsInt.getLimitedValue(), |
3246 | Size: AsInt.getBitWidth() / 8); |
3247 | return false; |
3248 | }; |
3249 | |
3250 | return parseMany(parseOne: parseOp); |
3251 | } |
3252 | |
3253 | /// parseDirectiveZero |
3254 | /// ::= .zero expression |
3255 | bool AsmParser::parseDirectiveZero() { |
3256 | SMLoc NumBytesLoc = Lexer.getLoc(); |
3257 | const MCExpr *NumBytes; |
3258 | if (checkForValidSection() || parseExpression(Res&: NumBytes)) |
3259 | return true; |
3260 | |
3261 | int64_t Val = 0; |
3262 | if (getLexer().is(K: AsmToken::Comma)) { |
3263 | Lex(); |
3264 | if (parseAbsoluteExpression(Res&: Val)) |
3265 | return true; |
3266 | } |
3267 | |
3268 | if (parseEOL()) |
3269 | return true; |
3270 | getStreamer().emitFill(NumBytes: *NumBytes, FillValue: Val, Loc: NumBytesLoc); |
3271 | |
3272 | return false; |
3273 | } |
3274 | |
3275 | /// parseDirectiveFill |
3276 | /// ::= .fill expression [ , expression [ , expression ] ] |
3277 | bool AsmParser::parseDirectiveFill() { |
3278 | SMLoc NumValuesLoc = Lexer.getLoc(); |
3279 | const MCExpr *NumValues; |
3280 | if (checkForValidSection() || parseExpression(Res&: NumValues)) |
3281 | return true; |
3282 | |
3283 | int64_t FillSize = 1; |
3284 | int64_t FillExpr = 0; |
3285 | |
3286 | SMLoc SizeLoc, ExprLoc; |
3287 | |
3288 | if (parseOptionalToken(T: AsmToken::Comma)) { |
3289 | SizeLoc = getTok().getLoc(); |
3290 | if (parseAbsoluteExpression(Res&: FillSize)) |
3291 | return true; |
3292 | if (parseOptionalToken(T: AsmToken::Comma)) { |
3293 | ExprLoc = getTok().getLoc(); |
3294 | if (parseAbsoluteExpression(Res&: FillExpr)) |
3295 | return true; |
3296 | } |
3297 | } |
3298 | if (parseEOL()) |
3299 | return true; |
3300 | |
3301 | if (FillSize < 0) { |
3302 | Warning(L: SizeLoc, Msg: "'.fill' directive with negative size has no effect" ); |
3303 | return false; |
3304 | } |
3305 | if (FillSize > 8) { |
3306 | Warning(L: SizeLoc, Msg: "'.fill' directive with size greater than 8 has been truncated to 8" ); |
3307 | FillSize = 8; |
3308 | } |
3309 | |
3310 | if (!isUInt<32>(x: FillExpr) && FillSize > 4) |
3311 | Warning(L: ExprLoc, Msg: "'.fill' directive pattern has been truncated to 32-bits" ); |
3312 | |
3313 | getStreamer().emitFill(NumValues: *NumValues, Size: FillSize, Expr: FillExpr, Loc: NumValuesLoc); |
3314 | |
3315 | return false; |
3316 | } |
3317 | |
3318 | /// parseDirectiveOrg |
3319 | /// ::= .org expression [ , expression ] |
3320 | bool AsmParser::parseDirectiveOrg() { |
3321 | const MCExpr *Offset; |
3322 | SMLoc OffsetLoc = Lexer.getLoc(); |
3323 | if (checkForValidSection() || parseExpression(Res&: Offset)) |
3324 | return true; |
3325 | |
3326 | // Parse optional fill expression. |
3327 | int64_t FillExpr = 0; |
3328 | if (parseOptionalToken(T: AsmToken::Comma)) |
3329 | if (parseAbsoluteExpression(Res&: FillExpr)) |
3330 | return true; |
3331 | if (parseEOL()) |
3332 | return true; |
3333 | |
3334 | getStreamer().emitValueToOffset(Offset, Value: FillExpr, Loc: OffsetLoc); |
3335 | return false; |
3336 | } |
3337 | |
3338 | /// parseDirectiveAlign |
3339 | /// ::= {.align, ...} expression [ , expression [ , expression ]] |
3340 | bool AsmParser::parseDirectiveAlign(bool IsPow2, unsigned ValueSize) { |
3341 | SMLoc AlignmentLoc = getLexer().getLoc(); |
3342 | int64_t Alignment; |
3343 | SMLoc MaxBytesLoc; |
3344 | bool HasFillExpr = false; |
3345 | int64_t FillExpr = 0; |
3346 | int64_t MaxBytesToFill = 0; |
3347 | SMLoc FillExprLoc; |
3348 | |
3349 | auto parseAlign = [&]() -> bool { |
3350 | if (parseAbsoluteExpression(Res&: Alignment)) |
3351 | return true; |
3352 | if (parseOptionalToken(T: AsmToken::Comma)) { |
3353 | // The fill expression can be omitted while specifying a maximum number of |
3354 | // alignment bytes, e.g: |
3355 | // .align 3,,4 |
3356 | if (getTok().isNot(K: AsmToken::Comma)) { |
3357 | HasFillExpr = true; |
3358 | if (parseTokenLoc(Loc&: FillExprLoc) || parseAbsoluteExpression(Res&: FillExpr)) |
3359 | return true; |
3360 | } |
3361 | if (parseOptionalToken(T: AsmToken::Comma)) |
3362 | if (parseTokenLoc(Loc&: MaxBytesLoc) || |
3363 | parseAbsoluteExpression(Res&: MaxBytesToFill)) |
3364 | return true; |
3365 | } |
3366 | return parseEOL(); |
3367 | }; |
3368 | |
3369 | if (checkForValidSection()) |
3370 | return true; |
3371 | // Ignore empty '.p2align' directives for GNU-as compatibility |
3372 | if (IsPow2 && (ValueSize == 1) && getTok().is(K: AsmToken::EndOfStatement)) { |
3373 | Warning(L: AlignmentLoc, Msg: "p2align directive with no operand(s) is ignored" ); |
3374 | return parseEOL(); |
3375 | } |
3376 | if (parseAlign()) |
3377 | return true; |
3378 | |
3379 | // Always emit an alignment here even if we thrown an error. |
3380 | bool ReturnVal = false; |
3381 | |
3382 | // Compute alignment in bytes. |
3383 | if (IsPow2) { |
3384 | // FIXME: Diagnose overflow. |
3385 | if (Alignment >= 32) { |
3386 | ReturnVal |= Error(L: AlignmentLoc, Msg: "invalid alignment value" ); |
3387 | Alignment = 31; |
3388 | } |
3389 | |
3390 | Alignment = 1ULL << Alignment; |
3391 | } else { |
3392 | // Reject alignments that aren't either a power of two or zero, |
3393 | // for gas compatibility. Alignment of zero is silently rounded |
3394 | // up to one. |
3395 | if (Alignment == 0) |
3396 | Alignment = 1; |
3397 | else if (!isPowerOf2_64(Value: Alignment)) { |
3398 | ReturnVal |= Error(L: AlignmentLoc, Msg: "alignment must be a power of 2" ); |
3399 | Alignment = llvm::bit_floor<uint64_t>(Value: Alignment); |
3400 | } |
3401 | if (!isUInt<32>(x: Alignment)) { |
3402 | ReturnVal |= Error(L: AlignmentLoc, Msg: "alignment must be smaller than 2**32" ); |
3403 | Alignment = 1u << 31; |
3404 | } |
3405 | } |
3406 | |
3407 | // Diagnose non-sensical max bytes to align. |
3408 | if (MaxBytesLoc.isValid()) { |
3409 | if (MaxBytesToFill < 1) { |
3410 | ReturnVal |= Error(L: MaxBytesLoc, |
3411 | Msg: "alignment directive can never be satisfied in this " |
3412 | "many bytes, ignoring maximum bytes expression" ); |
3413 | MaxBytesToFill = 0; |
3414 | } |
3415 | |
3416 | if (MaxBytesToFill >= Alignment) { |
3417 | Warning(L: MaxBytesLoc, Msg: "maximum bytes expression exceeds alignment and " |
3418 | "has no effect" ); |
3419 | MaxBytesToFill = 0; |
3420 | } |
3421 | } |
3422 | |
3423 | const MCSection *Section = getStreamer().getCurrentSectionOnly(); |
3424 | assert(Section && "must have section to emit alignment" ); |
3425 | |
3426 | if (HasFillExpr && FillExpr != 0 && Section->isVirtualSection()) { |
3427 | ReturnVal |= |
3428 | Warning(L: FillExprLoc, Msg: "ignoring non-zero fill value in " + |
3429 | Section->getVirtualSectionKind() + |
3430 | " section '" + Section->getName() + "'" ); |
3431 | FillExpr = 0; |
3432 | } |
3433 | |
3434 | // Check whether we should use optimal code alignment for this .align |
3435 | // directive. |
3436 | if (Section->useCodeAlign() && !HasFillExpr) { |
3437 | getStreamer().emitCodeAlignment( |
3438 | Alignment: Align(Alignment), STI: &getTargetParser().getSTI(), MaxBytesToEmit: MaxBytesToFill); |
3439 | } else { |
3440 | // FIXME: Target specific behavior about how the "extra" bytes are filled. |
3441 | getStreamer().emitValueToAlignment(Alignment: Align(Alignment), Value: FillExpr, ValueSize, |
3442 | MaxBytesToEmit: MaxBytesToFill); |
3443 | } |
3444 | |
3445 | return ReturnVal; |
3446 | } |
3447 | |
3448 | /// parseDirectiveFile |
3449 | /// ::= .file filename |
3450 | /// ::= .file number [directory] filename [md5 checksum] [source source-text] |
3451 | bool AsmParser::parseDirectiveFile(SMLoc DirectiveLoc) { |
3452 | // FIXME: I'm not sure what this is. |
3453 | int64_t FileNumber = -1; |
3454 | if (getLexer().is(K: AsmToken::Integer)) { |
3455 | FileNumber = getTok().getIntVal(); |
3456 | Lex(); |
3457 | |
3458 | if (FileNumber < 0) |
3459 | return TokError(Msg: "negative file number" ); |
3460 | } |
3461 | |
3462 | std::string Path; |
3463 | |
3464 | // Usually the directory and filename together, otherwise just the directory. |
3465 | // Allow the strings to have escaped octal character sequence. |
3466 | if (parseEscapedString(Data&: Path)) |
3467 | return true; |
3468 | |
3469 | StringRef Directory; |
3470 | StringRef Filename; |
3471 | std::string FilenameData; |
3472 | if (getLexer().is(K: AsmToken::String)) { |
3473 | if (check(P: FileNumber == -1, |
3474 | Msg: "explicit path specified, but no file number" ) || |
3475 | parseEscapedString(Data&: FilenameData)) |
3476 | return true; |
3477 | Filename = FilenameData; |
3478 | Directory = Path; |
3479 | } else { |
3480 | Filename = Path; |
3481 | } |
3482 | |
3483 | uint64_t MD5Hi, MD5Lo; |
3484 | bool HasMD5 = false; |
3485 | |
3486 | std::optional<StringRef> Source; |
3487 | bool HasSource = false; |
3488 | std::string SourceString; |
3489 | |
3490 | while (!parseOptionalToken(T: AsmToken::EndOfStatement)) { |
3491 | StringRef Keyword; |
3492 | if (check(P: getTok().isNot(K: AsmToken::Identifier), |
3493 | Msg: "unexpected token in '.file' directive" ) || |
3494 | parseIdentifier(Res&: Keyword)) |
3495 | return true; |
3496 | if (Keyword == "md5" ) { |
3497 | HasMD5 = true; |
3498 | if (check(P: FileNumber == -1, |
3499 | Msg: "MD5 checksum specified, but no file number" ) || |
3500 | parseHexOcta(Asm&: *this, hi&: MD5Hi, lo&: MD5Lo)) |
3501 | return true; |
3502 | } else if (Keyword == "source" ) { |
3503 | HasSource = true; |
3504 | if (check(P: FileNumber == -1, |
3505 | Msg: "source specified, but no file number" ) || |
3506 | check(P: getTok().isNot(K: AsmToken::String), |
3507 | Msg: "unexpected token in '.file' directive" ) || |
3508 | parseEscapedString(Data&: SourceString)) |
3509 | return true; |
3510 | } else { |
3511 | return TokError(Msg: "unexpected token in '.file' directive" ); |
3512 | } |
3513 | } |
3514 | |
3515 | if (FileNumber == -1) { |
3516 | // Ignore the directive if there is no number and the target doesn't support |
3517 | // numberless .file directives. This allows some portability of assembler |
3518 | // between different object file formats. |
3519 | if (getContext().getAsmInfo()->hasSingleParameterDotFile()) |
3520 | getStreamer().emitFileDirective(Filename); |
3521 | } else { |
3522 | // In case there is a -g option as well as debug info from directive .file, |
3523 | // we turn off the -g option, directly use the existing debug info instead. |
3524 | // Throw away any implicit file table for the assembler source. |
3525 | if (Ctx.getGenDwarfForAssembly()) { |
3526 | Ctx.getMCDwarfLineTable(CUID: 0).resetFileTable(); |
3527 | Ctx.setGenDwarfForAssembly(false); |
3528 | } |
3529 | |
3530 | std::optional<MD5::MD5Result> CKMem; |
3531 | if (HasMD5) { |
3532 | MD5::MD5Result Sum; |
3533 | for (unsigned i = 0; i != 8; ++i) { |
3534 | Sum[i] = uint8_t(MD5Hi >> ((7 - i) * 8)); |
3535 | Sum[i + 8] = uint8_t(MD5Lo >> ((7 - i) * 8)); |
3536 | } |
3537 | CKMem = Sum; |
3538 | } |
3539 | if (HasSource) { |
3540 | char *SourceBuf = static_cast<char *>(Ctx.allocate(Size: SourceString.size())); |
3541 | memcpy(dest: SourceBuf, src: SourceString.data(), n: SourceString.size()); |
3542 | Source = StringRef(SourceBuf, SourceString.size()); |
3543 | } |
3544 | if (FileNumber == 0) { |
3545 | // Upgrade to Version 5 for assembly actions like clang -c a.s. |
3546 | if (Ctx.getDwarfVersion() < 5) |
3547 | Ctx.setDwarfVersion(5); |
3548 | getStreamer().emitDwarfFile0Directive(Directory, Filename, Checksum: CKMem, Source); |
3549 | } else { |
3550 | Expected<unsigned> FileNumOrErr = getStreamer().tryEmitDwarfFileDirective( |
3551 | FileNo: FileNumber, Directory, Filename, Checksum: CKMem, Source); |
3552 | if (!FileNumOrErr) |
3553 | return Error(L: DirectiveLoc, Msg: toString(E: FileNumOrErr.takeError())); |
3554 | } |
3555 | // Alert the user if there are some .file directives with MD5 and some not. |
3556 | // But only do that once. |
3557 | if (!ReportedInconsistentMD5 && !Ctx.isDwarfMD5UsageConsistent(CUID: 0)) { |
3558 | ReportedInconsistentMD5 = true; |
3559 | return Warning(L: DirectiveLoc, Msg: "inconsistent use of MD5 checksums" ); |
3560 | } |
3561 | } |
3562 | |
3563 | return false; |
3564 | } |
3565 | |
3566 | /// parseDirectiveLine |
3567 | /// ::= .line [number] |
3568 | bool AsmParser::parseDirectiveLine() { |
3569 | parseOptionalToken(T: AsmToken::Integer); |
3570 | return parseEOL(); |
3571 | } |
3572 | |
3573 | /// parseDirectiveLoc |
3574 | /// ::= .loc FileNumber [LineNumber] [ColumnPos] [basic_block] [prologue_end] |
3575 | /// [epilogue_begin] [is_stmt VALUE] [isa VALUE] |
3576 | /// The first number is a file number, must have been previously assigned with |
3577 | /// a .file directive, the second number is the line number and optionally the |
3578 | /// third number is a column position (zero if not specified). The remaining |
3579 | /// optional items are .loc sub-directives. |
3580 | bool AsmParser::parseDirectiveLoc() { |
3581 | int64_t FileNumber = 0, LineNumber = 0; |
3582 | SMLoc Loc = getTok().getLoc(); |
3583 | if (parseIntToken(V&: FileNumber) || |
3584 | check(P: FileNumber < 1 && Ctx.getDwarfVersion() < 5, Loc, |
3585 | Msg: "file number less than one in '.loc' directive" ) || |
3586 | check(P: !getContext().isValidDwarfFileNumber(FileNumber), Loc, |
3587 | Msg: "unassigned file number in '.loc' directive" )) |
3588 | return true; |
3589 | |
3590 | // optional |
3591 | if (getLexer().is(K: AsmToken::Integer)) { |
3592 | LineNumber = getTok().getIntVal(); |
3593 | if (LineNumber < 0) |
3594 | return TokError(Msg: "line number less than zero in '.loc' directive" ); |
3595 | Lex(); |
3596 | } |
3597 | |
3598 | int64_t ColumnPos = 0; |
3599 | if (getLexer().is(K: AsmToken::Integer)) { |
3600 | ColumnPos = getTok().getIntVal(); |
3601 | if (ColumnPos < 0) |
3602 | return TokError(Msg: "column position less than zero in '.loc' directive" ); |
3603 | Lex(); |
3604 | } |
3605 | |
3606 | auto PrevFlags = getContext().getCurrentDwarfLoc().getFlags(); |
3607 | unsigned Flags = PrevFlags & DWARF2_FLAG_IS_STMT; |
3608 | unsigned Isa = 0; |
3609 | int64_t Discriminator = 0; |
3610 | |
3611 | auto parseLocOp = [&]() -> bool { |
3612 | StringRef Name; |
3613 | SMLoc Loc = getTok().getLoc(); |
3614 | if (parseIdentifier(Res&: Name)) |
3615 | return TokError(Msg: "unexpected token in '.loc' directive" ); |
3616 | |
3617 | if (Name == "basic_block" ) |
3618 | Flags |= DWARF2_FLAG_BASIC_BLOCK; |
3619 | else if (Name == "prologue_end" ) |
3620 | Flags |= DWARF2_FLAG_PROLOGUE_END; |
3621 | else if (Name == "epilogue_begin" ) |
3622 | Flags |= DWARF2_FLAG_EPILOGUE_BEGIN; |
3623 | else if (Name == "is_stmt" ) { |
3624 | Loc = getTok().getLoc(); |
3625 | const MCExpr *Value; |
3626 | if (parseExpression(Res&: Value)) |
3627 | return true; |
3628 | // The expression must be the constant 0 or 1. |
3629 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) { |
3630 | int Value = MCE->getValue(); |
3631 | if (Value == 0) |
3632 | Flags &= ~DWARF2_FLAG_IS_STMT; |
3633 | else if (Value == 1) |
3634 | Flags |= DWARF2_FLAG_IS_STMT; |
3635 | else |
3636 | return Error(L: Loc, Msg: "is_stmt value not 0 or 1" ); |
3637 | } else { |
3638 | return Error(L: Loc, Msg: "is_stmt value not the constant value of 0 or 1" ); |
3639 | } |
3640 | } else if (Name == "isa" ) { |
3641 | Loc = getTok().getLoc(); |
3642 | const MCExpr *Value; |
3643 | if (parseExpression(Res&: Value)) |
3644 | return true; |
3645 | // The expression must be a constant greater or equal to 0. |
3646 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) { |
3647 | int Value = MCE->getValue(); |
3648 | if (Value < 0) |
3649 | return Error(L: Loc, Msg: "isa number less than zero" ); |
3650 | Isa = Value; |
3651 | } else { |
3652 | return Error(L: Loc, Msg: "isa number not a constant value" ); |
3653 | } |
3654 | } else if (Name == "discriminator" ) { |
3655 | if (parseAbsoluteExpression(Res&: Discriminator)) |
3656 | return true; |
3657 | } else { |
3658 | return Error(L: Loc, Msg: "unknown sub-directive in '.loc' directive" ); |
3659 | } |
3660 | return false; |
3661 | }; |
3662 | |
3663 | if (parseMany(parseOne: parseLocOp, hasComma: false /*hasComma*/)) |
3664 | return true; |
3665 | |
3666 | getStreamer().emitDwarfLocDirective(FileNo: FileNumber, Line: LineNumber, Column: ColumnPos, Flags, |
3667 | Isa, Discriminator, FileName: StringRef()); |
3668 | |
3669 | return false; |
3670 | } |
3671 | |
3672 | /// parseDirectiveLoc |
3673 | /// ::= .loc_label label |
3674 | bool AsmParser::parseDirectiveLocLabel(SMLoc DirectiveLoc) { |
3675 | StringRef Name; |
3676 | DirectiveLoc = Lexer.getLoc(); |
3677 | if (parseIdentifier(Res&: Name)) |
3678 | return TokError(Msg: "expected identifier" ); |
3679 | if (parseEOL()) |
3680 | return true; |
3681 | getStreamer().emitDwarfLocLabelDirective(Loc: DirectiveLoc, Name); |
3682 | return false; |
3683 | } |
3684 | |
3685 | /// parseDirectiveStabs |
3686 | /// ::= .stabs string, number, number, number |
3687 | bool AsmParser::parseDirectiveStabs() { |
3688 | return TokError(Msg: "unsupported directive '.stabs'" ); |
3689 | } |
3690 | |
3691 | /// parseDirectiveCVFile |
3692 | /// ::= .cv_file number filename [checksum] [checksumkind] |
3693 | bool AsmParser::parseDirectiveCVFile() { |
3694 | SMLoc FileNumberLoc = getTok().getLoc(); |
3695 | int64_t FileNumber; |
3696 | std::string Filename; |
3697 | std::string Checksum; |
3698 | int64_t ChecksumKind = 0; |
3699 | |
3700 | if (parseIntToken(V&: FileNumber, ErrMsg: "expected file number" ) || |
3701 | check(P: FileNumber < 1, Loc: FileNumberLoc, Msg: "file number less than one" ) || |
3702 | check(P: getTok().isNot(K: AsmToken::String), |
3703 | Msg: "unexpected token in '.cv_file' directive" ) || |
3704 | parseEscapedString(Data&: Filename)) |
3705 | return true; |
3706 | if (!parseOptionalToken(T: AsmToken::EndOfStatement)) { |
3707 | if (check(P: getTok().isNot(K: AsmToken::String), |
3708 | Msg: "unexpected token in '.cv_file' directive" ) || |
3709 | parseEscapedString(Data&: Checksum) || |
3710 | parseIntToken(V&: ChecksumKind, |
3711 | ErrMsg: "expected checksum kind in '.cv_file' directive" ) || |
3712 | parseEOL()) |
3713 | return true; |
3714 | } |
3715 | |
3716 | Checksum = fromHex(Input: Checksum); |
3717 | void *CKMem = Ctx.allocate(Size: Checksum.size(), Align: 1); |
3718 | memcpy(dest: CKMem, src: Checksum.data(), n: Checksum.size()); |
3719 | ArrayRef<uint8_t> ChecksumAsBytes(reinterpret_cast<const uint8_t *>(CKMem), |
3720 | Checksum.size()); |
3721 | |
3722 | if (!getStreamer().emitCVFileDirective(FileNo: FileNumber, Filename, Checksum: ChecksumAsBytes, |
3723 | ChecksumKind: static_cast<uint8_t>(ChecksumKind))) |
3724 | return Error(L: FileNumberLoc, Msg: "file number already allocated" ); |
3725 | |
3726 | return false; |
3727 | } |
3728 | |
3729 | bool AsmParser::parseCVFunctionId(int64_t &FunctionId, |
3730 | StringRef DirectiveName) { |
3731 | SMLoc Loc; |
3732 | return parseTokenLoc(Loc) || |
3733 | parseIntToken(V&: FunctionId, ErrMsg: "expected function id" ) || |
3734 | check(P: FunctionId < 0 || FunctionId >= UINT_MAX, Loc, |
3735 | Msg: "expected function id within range [0, UINT_MAX)" ); |
3736 | } |
3737 | |
3738 | bool AsmParser::parseCVFileId(int64_t &FileNumber, StringRef DirectiveName) { |
3739 | SMLoc Loc; |
3740 | return parseTokenLoc(Loc) || |
3741 | parseIntToken(V&: FileNumber, ErrMsg: "expected file number" ) || |
3742 | check(P: FileNumber < 1, Loc, |
3743 | Msg: "file number less than one in '" + DirectiveName + |
3744 | "' directive" ) || |
3745 | check(P: !getCVContext().isValidFileNumber(FileNumber), Loc, |
3746 | Msg: "unassigned file number in '" + DirectiveName + "' directive" ); |
3747 | } |
3748 | |
3749 | /// parseDirectiveCVFuncId |
3750 | /// ::= .cv_func_id FunctionId |
3751 | /// |
3752 | /// Introduces a function ID that can be used with .cv_loc. |
3753 | bool AsmParser::parseDirectiveCVFuncId() { |
3754 | SMLoc FunctionIdLoc = getTok().getLoc(); |
3755 | int64_t FunctionId; |
3756 | |
3757 | if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_func_id" ) || parseEOL()) |
3758 | return true; |
3759 | |
3760 | if (!getStreamer().emitCVFuncIdDirective(FunctionId)) |
3761 | return Error(L: FunctionIdLoc, Msg: "function id already allocated" ); |
3762 | |
3763 | return false; |
3764 | } |
3765 | |
3766 | /// parseDirectiveCVInlineSiteId |
3767 | /// ::= .cv_inline_site_id FunctionId |
3768 | /// "within" IAFunc |
3769 | /// "inlined_at" IAFile IALine [IACol] |
3770 | /// |
3771 | /// Introduces a function ID that can be used with .cv_loc. Includes "inlined |
3772 | /// at" source location information for use in the line table of the caller, |
3773 | /// whether the caller is a real function or another inlined call site. |
3774 | bool AsmParser::parseDirectiveCVInlineSiteId() { |
3775 | SMLoc FunctionIdLoc = getTok().getLoc(); |
3776 | int64_t FunctionId; |
3777 | int64_t IAFunc; |
3778 | int64_t IAFile; |
3779 | int64_t IALine; |
3780 | int64_t IACol = 0; |
3781 | |
3782 | // FunctionId |
3783 | if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_inline_site_id" )) |
3784 | return true; |
3785 | |
3786 | // "within" |
3787 | if (check(P: (getLexer().isNot(K: AsmToken::Identifier) || |
3788 | getTok().getIdentifier() != "within" ), |
3789 | Msg: "expected 'within' identifier in '.cv_inline_site_id' directive" )) |
3790 | return true; |
3791 | Lex(); |
3792 | |
3793 | // IAFunc |
3794 | if (parseCVFunctionId(FunctionId&: IAFunc, DirectiveName: ".cv_inline_site_id" )) |
3795 | return true; |
3796 | |
3797 | // "inlined_at" |
3798 | if (check(P: (getLexer().isNot(K: AsmToken::Identifier) || |
3799 | getTok().getIdentifier() != "inlined_at" ), |
3800 | Msg: "expected 'inlined_at' identifier in '.cv_inline_site_id' " |
3801 | "directive" ) ) |
3802 | return true; |
3803 | Lex(); |
3804 | |
3805 | // IAFile IALine |
3806 | if (parseCVFileId(FileNumber&: IAFile, DirectiveName: ".cv_inline_site_id" ) || |
3807 | parseIntToken(V&: IALine, ErrMsg: "expected line number after 'inlined_at'" )) |
3808 | return true; |
3809 | |
3810 | // [IACol] |
3811 | if (getLexer().is(K: AsmToken::Integer)) { |
3812 | IACol = getTok().getIntVal(); |
3813 | Lex(); |
3814 | } |
3815 | |
3816 | if (parseEOL()) |
3817 | return true; |
3818 | |
3819 | if (!getStreamer().emitCVInlineSiteIdDirective(FunctionId, IAFunc, IAFile, |
3820 | IALine, IACol, Loc: FunctionIdLoc)) |
3821 | return Error(L: FunctionIdLoc, Msg: "function id already allocated" ); |
3822 | |
3823 | return false; |
3824 | } |
3825 | |
3826 | /// parseDirectiveCVLoc |
3827 | /// ::= .cv_loc FunctionId FileNumber [LineNumber] [ColumnPos] [prologue_end] |
3828 | /// [is_stmt VALUE] |
3829 | /// The first number is a file number, must have been previously assigned with |
3830 | /// a .file directive, the second number is the line number and optionally the |
3831 | /// third number is a column position (zero if not specified). The remaining |
3832 | /// optional items are .loc sub-directives. |
3833 | bool AsmParser::parseDirectiveCVLoc() { |
3834 | SMLoc DirectiveLoc = getTok().getLoc(); |
3835 | int64_t FunctionId, FileNumber; |
3836 | if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_loc" ) || |
3837 | parseCVFileId(FileNumber, DirectiveName: ".cv_loc" )) |
3838 | return true; |
3839 | |
3840 | int64_t LineNumber = 0; |
3841 | if (getLexer().is(K: AsmToken::Integer)) { |
3842 | LineNumber = getTok().getIntVal(); |
3843 | if (LineNumber < 0) |
3844 | return TokError(Msg: "line number less than zero in '.cv_loc' directive" ); |
3845 | Lex(); |
3846 | } |
3847 | |
3848 | int64_t ColumnPos = 0; |
3849 | if (getLexer().is(K: AsmToken::Integer)) { |
3850 | ColumnPos = getTok().getIntVal(); |
3851 | if (ColumnPos < 0) |
3852 | return TokError(Msg: "column position less than zero in '.cv_loc' directive" ); |
3853 | Lex(); |
3854 | } |
3855 | |
3856 | bool PrologueEnd = false; |
3857 | uint64_t IsStmt = 0; |
3858 | |
3859 | auto parseOp = [&]() -> bool { |
3860 | StringRef Name; |
3861 | SMLoc Loc = getTok().getLoc(); |
3862 | if (parseIdentifier(Res&: Name)) |
3863 | return TokError(Msg: "unexpected token in '.cv_loc' directive" ); |
3864 | if (Name == "prologue_end" ) |
3865 | PrologueEnd = true; |
3866 | else if (Name == "is_stmt" ) { |
3867 | Loc = getTok().getLoc(); |
3868 | const MCExpr *Value; |
3869 | if (parseExpression(Res&: Value)) |
3870 | return true; |
3871 | // The expression must be the constant 0 or 1. |
3872 | IsStmt = ~0ULL; |
3873 | if (const auto *MCE = dyn_cast<MCConstantExpr>(Val: Value)) |
3874 | IsStmt = MCE->getValue(); |
3875 | |
3876 | if (IsStmt > 1) |
3877 | return Error(L: Loc, Msg: "is_stmt value not 0 or 1" ); |
3878 | } else { |
3879 | return Error(L: Loc, Msg: "unknown sub-directive in '.cv_loc' directive" ); |
3880 | } |
3881 | return false; |
3882 | }; |
3883 | |
3884 | if (parseMany(parseOne: parseOp, hasComma: false /*hasComma*/)) |
3885 | return true; |
3886 | |
3887 | getStreamer().emitCVLocDirective(FunctionId, FileNo: FileNumber, Line: LineNumber, |
3888 | Column: ColumnPos, PrologueEnd, IsStmt, FileName: StringRef(), |
3889 | Loc: DirectiveLoc); |
3890 | return false; |
3891 | } |
3892 | |
3893 | /// parseDirectiveCVLinetable |
3894 | /// ::= .cv_linetable FunctionId, FnStart, FnEnd |
3895 | bool AsmParser::parseDirectiveCVLinetable() { |
3896 | int64_t FunctionId; |
3897 | StringRef FnStartName, FnEndName; |
3898 | SMLoc Loc = getTok().getLoc(); |
3899 | if (parseCVFunctionId(FunctionId, DirectiveName: ".cv_linetable" ) || parseComma() || |
3900 | parseTokenLoc(Loc) || |
3901 | check(P: parseIdentifier(Res&: FnStartName), Loc, |
3902 | Msg: "expected identifier in directive" ) || |
3903 | parseComma() || parseTokenLoc(Loc) || |
3904 | check(P: parseIdentifier(Res&: FnEndName), Loc, |
3905 | Msg: "expected identifier in directive" )) |
3906 | return true; |
3907 | |
3908 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(Name: FnStartName); |
3909 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(Name: FnEndName); |
3910 | |
3911 | getStreamer().emitCVLinetableDirective(FunctionId, FnStart: FnStartSym, FnEnd: FnEndSym); |
3912 | return false; |
3913 | } |
3914 | |
3915 | /// parseDirectiveCVInlineLinetable |
3916 | /// ::= .cv_inline_linetable PrimaryFunctionId FileId LineNum FnStart FnEnd |
3917 | bool AsmParser::parseDirectiveCVInlineLinetable() { |
3918 | int64_t PrimaryFunctionId, SourceFileId, SourceLineNum; |
3919 | StringRef FnStartName, FnEndName; |
3920 | SMLoc Loc = getTok().getLoc(); |
3921 | if (parseCVFunctionId(FunctionId&: PrimaryFunctionId, DirectiveName: ".cv_inline_linetable" ) || |
3922 | parseTokenLoc(Loc) || |
3923 | parseIntToken(V&: SourceFileId, ErrMsg: "expected SourceField" ) || |
3924 | check(P: SourceFileId <= 0, Loc, Msg: "File id less than zero" ) || |
3925 | parseTokenLoc(Loc) || |
3926 | parseIntToken(V&: SourceLineNum, ErrMsg: "expected SourceLineNum" ) || |
3927 | check(P: SourceLineNum < 0, Loc, Msg: "Line number less than zero" ) || |
3928 | parseTokenLoc(Loc) || |
3929 | check(P: parseIdentifier(Res&: FnStartName), Loc, Msg: "expected identifier" ) || |
3930 | parseTokenLoc(Loc) || |
3931 | check(P: parseIdentifier(Res&: FnEndName), Loc, Msg: "expected identifier" )) |
3932 | return true; |
3933 | |
3934 | if (parseEOL()) |
3935 | return true; |
3936 | |
3937 | MCSymbol *FnStartSym = getContext().getOrCreateSymbol(Name: FnStartName); |
3938 | MCSymbol *FnEndSym = getContext().getOrCreateSymbol(Name: FnEndName); |
3939 | getStreamer().emitCVInlineLinetableDirective(PrimaryFunctionId, SourceFileId, |
3940 | SourceLineNum, FnStartSym, |
3941 | FnEndSym); |
3942 | return false; |
3943 | } |
3944 | |
3945 | void AsmParser::initializeCVDefRangeTypeMap() { |
3946 | CVDefRangeTypeMap["reg" ] = CVDR_DEFRANGE_REGISTER; |
3947 | CVDefRangeTypeMap["frame_ptr_rel" ] = CVDR_DEFRANGE_FRAMEPOINTER_REL; |
3948 | CVDefRangeTypeMap["subfield_reg" ] = CVDR_DEFRANGE_SUBFIELD_REGISTER; |
3949 | CVDefRangeTypeMap["reg_rel" ] = CVDR_DEFRANGE_REGISTER_REL; |
3950 | } |
3951 | |
3952 | /// parseDirectiveCVDefRange |
3953 | /// ::= .cv_def_range RangeStart RangeEnd (GapStart GapEnd)*, bytes* |
3954 | bool AsmParser::parseDirectiveCVDefRange() { |
3955 | SMLoc Loc; |
3956 | std::vector<std::pair<const MCSymbol *, const MCSymbol *>> Ranges; |
3957 | while (getLexer().is(K: AsmToken::Identifier)) { |
3958 | Loc = getLexer().getLoc(); |
3959 | StringRef GapStartName; |
3960 | if (parseIdentifier(Res&: GapStartName)) |
3961 | return Error(L: Loc, Msg: "expected identifier in directive" ); |
3962 | MCSymbol *GapStartSym = getContext().getOrCreateSymbol(Name: GapStartName); |
3963 | |
3964 | Loc = getLexer().getLoc(); |
3965 | StringRef GapEndName; |
3966 | if (parseIdentifier(Res&: GapEndName)) |
3967 | return Error(L: Loc, Msg: "expected identifier in directive" ); |
3968 | MCSymbol *GapEndSym = getContext().getOrCreateSymbol(Name: GapEndName); |
3969 | |
3970 | Ranges.push_back(x: {GapStartSym, GapEndSym}); |
3971 | } |
3972 | |
3973 | StringRef CVDefRangeTypeStr; |
3974 | if (parseToken( |
3975 | T: AsmToken::Comma, |
3976 | Msg: "expected comma before def_range type in .cv_def_range directive" ) || |
3977 | parseIdentifier(Res&: CVDefRangeTypeStr)) |
3978 | return Error(L: Loc, Msg: "expected def_range type in directive" ); |
3979 | |
3980 | StringMap<CVDefRangeType>::const_iterator CVTypeIt = |
3981 | CVDefRangeTypeMap.find(Key: CVDefRangeTypeStr); |
3982 | CVDefRangeType CVDRType = (CVTypeIt == CVDefRangeTypeMap.end()) |
3983 | ? CVDR_DEFRANGE |
3984 | : CVTypeIt->getValue(); |
3985 | switch (CVDRType) { |
3986 | case CVDR_DEFRANGE_REGISTER: { |
3987 | int64_t DRRegister; |
3988 | if (parseToken(T: AsmToken::Comma, Msg: "expected comma before register number in " |
3989 | ".cv_def_range directive" ) || |
3990 | parseAbsoluteExpression(Res&: DRRegister)) |
3991 | return Error(L: Loc, Msg: "expected register number" ); |
3992 | |
3993 | codeview::DefRangeRegisterHeader DRHdr; |
3994 | DRHdr.Register = DRRegister; |
3995 | DRHdr.MayHaveNoName = 0; |
3996 | getStreamer().emitCVDefRangeDirective(Ranges, DRHdr); |
3997 | break; |
3998 | } |
3999 | case CVDR_DEFRANGE_FRAMEPOINTER_REL: { |
4000 | int64_t DROffset; |
4001 | if (parseToken(T: AsmToken::Comma, |
4002 | Msg: "expected comma before offset in .cv_def_range directive" ) || |
4003 | parseAbsoluteExpression(Res&: DROffset)) |
4004 | return Error(L: Loc, Msg: "expected offset value" ); |
4005 | |
4006 | codeview::DefRangeFramePointerRelHeader DRHdr; |
4007 | DRHdr.Offset = DROffset; |
4008 | getStreamer().emitCVDefRangeDirective(Ranges, DRHdr); |
4009 | break; |
4010 | } |
4011 | case CVDR_DEFRANGE_SUBFIELD_REGISTER: { |
4012 | int64_t DRRegister; |
4013 | int64_t DROffsetInParent; |
4014 | if (parseToken(T: AsmToken::Comma, Msg: "expected comma before register number in " |
4015 | ".cv_def_range directive" ) || |
4016 | parseAbsoluteExpression(Res&: DRRegister)) |
4017 | return Error(L: Loc, Msg: "expected register number" ); |
4018 | if (parseToken(T: AsmToken::Comma, |
4019 | Msg: "expected comma before offset in .cv_def_range directive" ) || |
4020 | parseAbsoluteExpression(Res&: DROffsetInParent)) |
4021 | return Error(L: Loc, Msg: "expected offset value" ); |
4022 | |
4023 | codeview::DefRangeSubfieldRegisterHeader DRHdr; |
4024 | DRHdr.Register = DRRegister; |
4025 | DRHdr.MayHaveNoName = 0; |
4026 | DRHdr.OffsetInParent = DROffsetInParent; |
4027 | getStreamer().emitCVDefRangeDirective(Ranges, DRHdr); |
4028 | break; |
4029 | } |
4030 | case CVDR_DEFRANGE_REGISTER_REL: { |
4031 | int64_t DRRegister; |
4032 | int64_t DRFlags; |
4033 | int64_t DRBasePointerOffset; |
4034 | if (parseToken(T: AsmToken::Comma, Msg: "expected comma before register number in " |
4035 | ".cv_def_range directive" ) || |
4036 | parseAbsoluteExpression(Res&: DRRegister)) |
4037 | return Error(L: Loc, Msg: "expected register value" ); |
4038 | if (parseToken( |
4039 | T: AsmToken::Comma, |
4040 | Msg: "expected comma before flag value in .cv_def_range directive" ) || |
4041 | parseAbsoluteExpression(Res&: DRFlags)) |
4042 | return Error(L: Loc, Msg: "expected flag value" ); |
4043 | if (parseToken(T: AsmToken::Comma, Msg: "expected comma before base pointer offset " |
4044 | "in .cv_def_range directive" ) || |
4045 | parseAbsoluteExpression(Res&: DRBasePointerOffset)) |
4046 | return Error(L: Loc, Msg: "expected base pointer offset value" ); |
4047 | |
4048 | codeview::DefRangeRegisterRelHeader DRHdr; |
4049 | DRHdr.Register = DRRegister; |
4050 | DRHdr.Flags = DRFlags; |
4051 | DRHdr.BasePointerOffset = DRBasePointerOffset; |
4052 | getStreamer().emitCVDefRangeDirective(Ranges, DRHdr); |
4053 | break; |
4054 | } |
4055 | default: |
4056 | return Error(L: Loc, Msg: "unexpected def_range type in .cv_def_range directive" ); |
4057 | } |
4058 | return true; |
4059 | } |
4060 | |
4061 | /// parseDirectiveCVString |
4062 | /// ::= .cv_stringtable "string" |
4063 | bool AsmParser::parseDirectiveCVString() { |
4064 | std::string Data; |
4065 | if (checkForValidSection() || parseEscapedString(Data)) |
4066 | return true; |
4067 | |
4068 | // Put the string in the table and emit the offset. |
4069 | std::pair<StringRef, unsigned> Insertion = |
4070 | getCVContext().addToStringTable(S: Data); |
4071 | getStreamer().emitInt32(Value: Insertion.second); |
4072 | return false; |
4073 | } |
4074 | |
4075 | /// parseDirectiveCVStringTable |
4076 | /// ::= .cv_stringtable |
4077 | bool AsmParser::parseDirectiveCVStringTable() { |
4078 | getStreamer().emitCVStringTableDirective(); |
4079 | return false; |
4080 | } |
4081 | |
4082 | /// parseDirectiveCVFileChecksums |
4083 | /// ::= .cv_filechecksums |
4084 | bool AsmParser::parseDirectiveCVFileChecksums() { |
4085 | getStreamer().emitCVFileChecksumsDirective(); |
4086 | return false; |
4087 | } |
4088 | |
4089 | /// parseDirectiveCVFileChecksumOffset |
4090 | /// ::= .cv_filechecksumoffset fileno |
4091 | bool AsmParser::parseDirectiveCVFileChecksumOffset() { |
4092 | int64_t FileNo; |
4093 | if (parseIntToken(V&: FileNo)) |
4094 | return true; |
4095 | if (parseEOL()) |
4096 | return true; |
4097 | getStreamer().emitCVFileChecksumOffsetDirective(FileNo); |
4098 | return false; |
4099 | } |
4100 | |
4101 | /// parseDirectiveCVFPOData |
4102 | /// ::= .cv_fpo_data procsym |
4103 | bool AsmParser::parseDirectiveCVFPOData() { |
4104 | SMLoc DirLoc = getLexer().getLoc(); |
4105 | StringRef ProcName; |
4106 | if (parseIdentifier(Res&: ProcName)) |
4107 | return TokError(Msg: "expected symbol name" ); |
4108 | if (parseEOL()) |
4109 | return true; |
4110 | MCSymbol *ProcSym = getContext().getOrCreateSymbol(Name: ProcName); |
4111 | getStreamer().emitCVFPOData(ProcSym, Loc: DirLoc); |
4112 | return false; |
4113 | } |
4114 | |
4115 | /// parseDirectiveCFISections |
4116 | /// ::= .cfi_sections section [, section] |
4117 | bool AsmParser::parseDirectiveCFISections() { |
4118 | StringRef Name; |
4119 | bool EH = false; |
4120 | bool Debug = false; |
4121 | |
4122 | if (!parseOptionalToken(T: AsmToken::EndOfStatement)) { |
4123 | for (;;) { |
4124 | if (parseIdentifier(Res&: Name)) |
4125 | return TokError(Msg: "expected .eh_frame or .debug_frame" ); |
4126 | if (Name == ".eh_frame" ) |
4127 | EH = true; |
4128 | else if (Name == ".debug_frame" ) |
4129 | Debug = true; |
4130 | if (parseOptionalToken(T: AsmToken::EndOfStatement)) |
4131 | break; |
4132 | if (parseComma()) |
4133 | return true; |
4134 | } |
4135 | } |
4136 | getStreamer().emitCFISections(EH, Debug); |
4137 | return false; |
4138 | } |
4139 | |
4140 | /// parseDirectiveCFIStartProc |
4141 | /// ::= .cfi_startproc [simple] |
4142 | bool AsmParser::parseDirectiveCFIStartProc() { |
4143 | CFIStartProcLoc = StartTokLoc; |
4144 | |
4145 | StringRef Simple; |
4146 | if (!parseOptionalToken(T: AsmToken::EndOfStatement)) { |
4147 | if (check(P: parseIdentifier(Res&: Simple) || Simple != "simple" , |
4148 | Msg: "unexpected token" ) || |
4149 | parseEOL()) |
4150 | return true; |
4151 | } |
4152 | |
4153 | // TODO(kristina): Deal with a corner case of incorrect diagnostic context |
4154 | // being produced if this directive is emitted as part of preprocessor macro |
4155 | // expansion which can *ONLY* happen if Clang's cc1as is the API consumer. |
4156 | // Tools like llvm-mc on the other hand are not affected by it, and report |
4157 | // correct context information. |
4158 | getStreamer().emitCFIStartProc(IsSimple: !Simple.empty(), Loc: Lexer.getLoc()); |
4159 | return false; |
4160 | } |
4161 | |
4162 | /// parseDirectiveCFIEndProc |
4163 | /// ::= .cfi_endproc |
4164 | bool AsmParser::parseDirectiveCFIEndProc() { |
4165 | CFIStartProcLoc = std::nullopt; |
4166 | |
4167 | if (parseEOL()) |
4168 | return true; |
4169 | |
4170 | getStreamer().emitCFIEndProc(); |
4171 | return false; |
4172 | } |
4173 | |
4174 | /// parse register name or number. |
4175 | bool AsmParser::parseRegisterOrRegisterNumber(int64_t &Register, |
4176 | SMLoc DirectiveLoc) { |
4177 | MCRegister RegNo; |
4178 | |
4179 | if (getLexer().isNot(K: AsmToken::Integer)) { |
4180 | if (getTargetParser().parseRegister(Reg&: RegNo, StartLoc&: DirectiveLoc, EndLoc&: DirectiveLoc)) |
4181 | return true; |
4182 | Register = getContext().getRegisterInfo()->getDwarfRegNum(RegNum: RegNo, isEH: true); |
4183 | } else |
4184 | return parseAbsoluteExpression(Res&: Register); |
4185 | |
4186 | return false; |
4187 | } |
4188 | |
4189 | /// parseDirectiveCFIDefCfa |
4190 | /// ::= .cfi_def_cfa register, offset |
4191 | bool AsmParser::parseDirectiveCFIDefCfa(SMLoc DirectiveLoc) { |
4192 | int64_t Register = 0, Offset = 0; |
4193 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() || |
4194 | parseAbsoluteExpression(Res&: Offset) || parseEOL()) |
4195 | return true; |
4196 | |
4197 | getStreamer().emitCFIDefCfa(Register, Offset, Loc: DirectiveLoc); |
4198 | return false; |
4199 | } |
4200 | |
4201 | /// parseDirectiveCFIDefCfaOffset |
4202 | /// ::= .cfi_def_cfa_offset offset |
4203 | bool AsmParser::parseDirectiveCFIDefCfaOffset(SMLoc DirectiveLoc) { |
4204 | int64_t Offset = 0; |
4205 | if (parseAbsoluteExpression(Res&: Offset) || parseEOL()) |
4206 | return true; |
4207 | |
4208 | getStreamer().emitCFIDefCfaOffset(Offset, Loc: DirectiveLoc); |
4209 | return false; |
4210 | } |
4211 | |
4212 | /// parseDirectiveCFIRegister |
4213 | /// ::= .cfi_register register, register |
4214 | bool AsmParser::parseDirectiveCFIRegister(SMLoc DirectiveLoc) { |
4215 | int64_t Register1 = 0, Register2 = 0; |
4216 | if (parseRegisterOrRegisterNumber(Register&: Register1, DirectiveLoc) || parseComma() || |
4217 | parseRegisterOrRegisterNumber(Register&: Register2, DirectiveLoc) || parseEOL()) |
4218 | return true; |
4219 | |
4220 | getStreamer().emitCFIRegister(Register1, Register2, Loc: DirectiveLoc); |
4221 | return false; |
4222 | } |
4223 | |
4224 | /// parseDirectiveCFIWindowSave |
4225 | /// ::= .cfi_window_save |
4226 | bool AsmParser::parseDirectiveCFIWindowSave(SMLoc DirectiveLoc) { |
4227 | if (parseEOL()) |
4228 | return true; |
4229 | getStreamer().emitCFIWindowSave(Loc: DirectiveLoc); |
4230 | return false; |
4231 | } |
4232 | |
4233 | /// parseDirectiveCFIAdjustCfaOffset |
4234 | /// ::= .cfi_adjust_cfa_offset adjustment |
4235 | bool AsmParser::parseDirectiveCFIAdjustCfaOffset(SMLoc DirectiveLoc) { |
4236 | int64_t Adjustment = 0; |
4237 | if (parseAbsoluteExpression(Res&: Adjustment) || parseEOL()) |
4238 | return true; |
4239 | |
4240 | getStreamer().emitCFIAdjustCfaOffset(Adjustment, Loc: DirectiveLoc); |
4241 | return false; |
4242 | } |
4243 | |
4244 | /// parseDirectiveCFIDefCfaRegister |
4245 | /// ::= .cfi_def_cfa_register register |
4246 | bool AsmParser::parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc) { |
4247 | int64_t Register = 0; |
4248 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL()) |
4249 | return true; |
4250 | |
4251 | getStreamer().emitCFIDefCfaRegister(Register, Loc: DirectiveLoc); |
4252 | return false; |
4253 | } |
4254 | |
4255 | /// parseDirectiveCFILLVMDefAspaceCfa |
4256 | /// ::= .cfi_llvm_def_aspace_cfa register, offset, address_space |
4257 | bool AsmParser::parseDirectiveCFILLVMDefAspaceCfa(SMLoc DirectiveLoc) { |
4258 | int64_t Register = 0, Offset = 0, AddressSpace = 0; |
4259 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() || |
4260 | parseAbsoluteExpression(Res&: Offset) || parseComma() || |
4261 | parseAbsoluteExpression(Res&: AddressSpace) || parseEOL()) |
4262 | return true; |
4263 | |
4264 | getStreamer().emitCFILLVMDefAspaceCfa(Register, Offset, AddressSpace, |
4265 | Loc: DirectiveLoc); |
4266 | return false; |
4267 | } |
4268 | |
4269 | /// parseDirectiveCFIOffset |
4270 | /// ::= .cfi_offset register, offset |
4271 | bool AsmParser::parseDirectiveCFIOffset(SMLoc DirectiveLoc) { |
4272 | int64_t Register = 0; |
4273 | int64_t Offset = 0; |
4274 | |
4275 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() || |
4276 | parseAbsoluteExpression(Res&: Offset) || parseEOL()) |
4277 | return true; |
4278 | |
4279 | getStreamer().emitCFIOffset(Register, Offset, Loc: DirectiveLoc); |
4280 | return false; |
4281 | } |
4282 | |
4283 | /// parseDirectiveCFIRelOffset |
4284 | /// ::= .cfi_rel_offset register, offset |
4285 | bool AsmParser::parseDirectiveCFIRelOffset(SMLoc DirectiveLoc) { |
4286 | int64_t Register = 0, Offset = 0; |
4287 | |
4288 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() || |
4289 | parseAbsoluteExpression(Res&: Offset) || parseEOL()) |
4290 | return true; |
4291 | |
4292 | getStreamer().emitCFIRelOffset(Register, Offset, Loc: DirectiveLoc); |
4293 | return false; |
4294 | } |
4295 | |
4296 | static bool isValidEncoding(int64_t Encoding) { |
4297 | if (Encoding & ~0xff) |
4298 | return false; |
4299 | |
4300 | if (Encoding == dwarf::DW_EH_PE_omit) |
4301 | return true; |
4302 | |
4303 | const unsigned Format = Encoding & 0xf; |
4304 | if (Format != dwarf::DW_EH_PE_absptr && Format != dwarf::DW_EH_PE_udata2 && |
4305 | Format != dwarf::DW_EH_PE_udata4 && Format != dwarf::DW_EH_PE_udata8 && |
4306 | Format != dwarf::DW_EH_PE_sdata2 && Format != dwarf::DW_EH_PE_sdata4 && |
4307 | Format != dwarf::DW_EH_PE_sdata8 && Format != dwarf::DW_EH_PE_signed) |
4308 | return false; |
4309 | |
4310 | const unsigned Application = Encoding & 0x70; |
4311 | if (Application != dwarf::DW_EH_PE_absptr && |
4312 | Application != dwarf::DW_EH_PE_pcrel) |
4313 | return false; |
4314 | |
4315 | return true; |
4316 | } |
4317 | |
4318 | /// parseDirectiveCFIPersonalityOrLsda |
4319 | /// IsPersonality true for cfi_personality, false for cfi_lsda |
4320 | /// ::= .cfi_personality encoding, [symbol_name] |
4321 | /// ::= .cfi_lsda encoding, [symbol_name] |
4322 | bool AsmParser::parseDirectiveCFIPersonalityOrLsda(bool IsPersonality) { |
4323 | int64_t Encoding = 0; |
4324 | if (parseAbsoluteExpression(Res&: Encoding)) |
4325 | return true; |
4326 | if (Encoding == dwarf::DW_EH_PE_omit) |
4327 | return false; |
4328 | |
4329 | StringRef Name; |
4330 | if (check(P: !isValidEncoding(Encoding), Msg: "unsupported encoding." ) || |
4331 | parseComma() || |
4332 | check(P: parseIdentifier(Res&: Name), Msg: "expected identifier in directive" ) || |
4333 | parseEOL()) |
4334 | return true; |
4335 | |
4336 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
4337 | |
4338 | if (IsPersonality) |
4339 | getStreamer().emitCFIPersonality(Sym, Encoding); |
4340 | else |
4341 | getStreamer().emitCFILsda(Sym, Encoding); |
4342 | return false; |
4343 | } |
4344 | |
4345 | /// parseDirectiveCFIRememberState |
4346 | /// ::= .cfi_remember_state |
4347 | bool AsmParser::parseDirectiveCFIRememberState(SMLoc DirectiveLoc) { |
4348 | if (parseEOL()) |
4349 | return true; |
4350 | getStreamer().emitCFIRememberState(Loc: DirectiveLoc); |
4351 | return false; |
4352 | } |
4353 | |
4354 | /// parseDirectiveCFIRestoreState |
4355 | /// ::= .cfi_remember_state |
4356 | bool AsmParser::parseDirectiveCFIRestoreState(SMLoc DirectiveLoc) { |
4357 | if (parseEOL()) |
4358 | return true; |
4359 | getStreamer().emitCFIRestoreState(Loc: DirectiveLoc); |
4360 | return false; |
4361 | } |
4362 | |
4363 | /// parseDirectiveCFISameValue |
4364 | /// ::= .cfi_same_value register |
4365 | bool AsmParser::parseDirectiveCFISameValue(SMLoc DirectiveLoc) { |
4366 | int64_t Register = 0; |
4367 | |
4368 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL()) |
4369 | return true; |
4370 | |
4371 | getStreamer().emitCFISameValue(Register, Loc: DirectiveLoc); |
4372 | return false; |
4373 | } |
4374 | |
4375 | /// parseDirectiveCFIRestore |
4376 | /// ::= .cfi_restore register |
4377 | bool AsmParser::parseDirectiveCFIRestore(SMLoc DirectiveLoc) { |
4378 | int64_t Register = 0; |
4379 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL()) |
4380 | return true; |
4381 | |
4382 | getStreamer().emitCFIRestore(Register, Loc: DirectiveLoc); |
4383 | return false; |
4384 | } |
4385 | |
4386 | /// parseDirectiveCFIEscape |
4387 | /// ::= .cfi_escape expression[,...] |
4388 | bool AsmParser::parseDirectiveCFIEscape(SMLoc DirectiveLoc) { |
4389 | std::string Values; |
4390 | int64_t CurrValue; |
4391 | if (parseAbsoluteExpression(Res&: CurrValue)) |
4392 | return true; |
4393 | |
4394 | Values.push_back(c: (uint8_t)CurrValue); |
4395 | |
4396 | while (getLexer().is(K: AsmToken::Comma)) { |
4397 | Lex(); |
4398 | |
4399 | if (parseAbsoluteExpression(Res&: CurrValue)) |
4400 | return true; |
4401 | |
4402 | Values.push_back(c: (uint8_t)CurrValue); |
4403 | } |
4404 | |
4405 | getStreamer().emitCFIEscape(Values, Loc: DirectiveLoc); |
4406 | return false; |
4407 | } |
4408 | |
4409 | /// parseDirectiveCFIReturnColumn |
4410 | /// ::= .cfi_return_column register |
4411 | bool AsmParser::parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc) { |
4412 | int64_t Register = 0; |
4413 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL()) |
4414 | return true; |
4415 | getStreamer().emitCFIReturnColumn(Register); |
4416 | return false; |
4417 | } |
4418 | |
4419 | /// parseDirectiveCFISignalFrame |
4420 | /// ::= .cfi_signal_frame |
4421 | bool AsmParser::parseDirectiveCFISignalFrame(SMLoc DirectiveLoc) { |
4422 | if (parseEOL()) |
4423 | return true; |
4424 | |
4425 | getStreamer().emitCFISignalFrame(); |
4426 | return false; |
4427 | } |
4428 | |
4429 | /// parseDirectiveCFIUndefined |
4430 | /// ::= .cfi_undefined register |
4431 | bool AsmParser::parseDirectiveCFIUndefined(SMLoc DirectiveLoc) { |
4432 | int64_t Register = 0; |
4433 | |
4434 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseEOL()) |
4435 | return true; |
4436 | |
4437 | getStreamer().emitCFIUndefined(Register, Loc: DirectiveLoc); |
4438 | return false; |
4439 | } |
4440 | |
4441 | /// parseDirectiveCFILabel |
4442 | /// ::= .cfi_label label |
4443 | bool AsmParser::parseDirectiveCFILabel(SMLoc Loc) { |
4444 | StringRef Name; |
4445 | Loc = Lexer.getLoc(); |
4446 | if (parseIdentifier(Res&: Name)) |
4447 | return TokError(Msg: "expected identifier" ); |
4448 | if (parseEOL()) |
4449 | return true; |
4450 | getStreamer().emitCFILabelDirective(Loc, Name); |
4451 | return false; |
4452 | } |
4453 | |
4454 | /// parseDirectiveCFIValOffset |
4455 | /// ::= .cfi_val_offset register, offset |
4456 | bool AsmParser::parseDirectiveCFIValOffset(SMLoc DirectiveLoc) { |
4457 | int64_t Register = 0; |
4458 | int64_t Offset = 0; |
4459 | |
4460 | if (parseRegisterOrRegisterNumber(Register, DirectiveLoc) || parseComma() || |
4461 | parseAbsoluteExpression(Res&: Offset) || parseEOL()) |
4462 | return true; |
4463 | |
4464 | getStreamer().emitCFIValOffset(Register, Offset, Loc: DirectiveLoc); |
4465 | return false; |
4466 | } |
4467 | |
4468 | /// parseDirectiveAltmacro |
4469 | /// ::= .altmacro |
4470 | /// ::= .noaltmacro |
4471 | bool AsmParser::parseDirectiveAltmacro(StringRef Directive) { |
4472 | if (parseEOL()) |
4473 | return true; |
4474 | AltMacroMode = (Directive == ".altmacro" ); |
4475 | return false; |
4476 | } |
4477 | |
4478 | /// parseDirectiveMacrosOnOff |
4479 | /// ::= .macros_on |
4480 | /// ::= .macros_off |
4481 | bool AsmParser::parseDirectiveMacrosOnOff(StringRef Directive) { |
4482 | if (parseEOL()) |
4483 | return true; |
4484 | setMacrosEnabled(Directive == ".macros_on" ); |
4485 | return false; |
4486 | } |
4487 | |
4488 | /// parseDirectiveMacro |
4489 | /// ::= .macro name[,] [parameters] |
4490 | bool AsmParser::parseDirectiveMacro(SMLoc DirectiveLoc) { |
4491 | StringRef Name; |
4492 | if (parseIdentifier(Res&: Name)) |
4493 | return TokError(Msg: "expected identifier in '.macro' directive" ); |
4494 | |
4495 | if (getLexer().is(K: AsmToken::Comma)) |
4496 | Lex(); |
4497 | |
4498 | MCAsmMacroParameters Parameters; |
4499 | while (getLexer().isNot(K: AsmToken::EndOfStatement)) { |
4500 | |
4501 | if (!Parameters.empty() && Parameters.back().Vararg) |
4502 | return Error(L: Lexer.getLoc(), Msg: "vararg parameter '" + |
4503 | Parameters.back().Name + |
4504 | "' should be the last parameter" ); |
4505 | |
4506 | MCAsmMacroParameter Parameter; |
4507 | if (parseIdentifier(Res&: Parameter.Name)) |
4508 | return TokError(Msg: "expected identifier in '.macro' directive" ); |
4509 | |
4510 | // Emit an error if two (or more) named parameters share the same name |
4511 | for (const MCAsmMacroParameter& CurrParam : Parameters) |
4512 | if (CurrParam.Name == Parameter.Name) |
4513 | return TokError(Msg: "macro '" + Name + "' has multiple parameters" |
4514 | " named '" + Parameter.Name + "'" ); |
4515 | |
4516 | if (Lexer.is(K: AsmToken::Colon)) { |
4517 | Lex(); // consume ':' |
4518 | |
4519 | SMLoc QualLoc; |
4520 | StringRef Qualifier; |
4521 | |
4522 | QualLoc = Lexer.getLoc(); |
4523 | if (parseIdentifier(Res&: Qualifier)) |
4524 | return Error(L: QualLoc, Msg: "missing parameter qualifier for " |
4525 | "'" + Parameter.Name + "' in macro '" + Name + "'" ); |
4526 | |
4527 | if (Qualifier == "req" ) |
4528 | Parameter.Required = true; |
4529 | else if (Qualifier == "vararg" ) |
4530 | Parameter.Vararg = true; |
4531 | else |
4532 | return Error(L: QualLoc, Msg: Qualifier + " is not a valid parameter qualifier " |
4533 | "for '" + Parameter.Name + "' in macro '" + Name + "'" ); |
4534 | } |
4535 | |
4536 | if (getLexer().is(K: AsmToken::Equal)) { |
4537 | Lex(); |
4538 | |
4539 | SMLoc ParamLoc; |
4540 | |
4541 | ParamLoc = Lexer.getLoc(); |
4542 | if (parseMacroArgument(MA&: Parameter.Value, /*Vararg=*/false )) |
4543 | return true; |
4544 | |
4545 | if (Parameter.Required) |
4546 | Warning(L: ParamLoc, Msg: "pointless default value for required parameter " |
4547 | "'" + Parameter.Name + "' in macro '" + Name + "'" ); |
4548 | } |
4549 | |
4550 | Parameters.push_back(x: std::move(Parameter)); |
4551 | |
4552 | if (getLexer().is(K: AsmToken::Comma)) |
4553 | Lex(); |
4554 | } |
4555 | |
4556 | // Eat just the end of statement. |
4557 | Lexer.Lex(); |
4558 | |
4559 | // Consuming deferred text, so use Lexer.Lex to ignore Lexing Errors |
4560 | AsmToken EndToken, StartToken = getTok(); |
4561 | unsigned MacroDepth = 0; |
4562 | // Lex the macro definition. |
4563 | while (true) { |
4564 | // Ignore Lexing errors in macros. |
4565 | while (Lexer.is(K: AsmToken::Error)) { |
4566 | Lexer.Lex(); |
4567 | } |
4568 | |
4569 | // Check whether we have reached the end of the file. |
4570 | if (getLexer().is(K: AsmToken::Eof)) |
4571 | return Error(L: DirectiveLoc, Msg: "no matching '.endmacro' in definition" ); |
4572 | |
4573 | // Otherwise, check whether we have reach the .endmacro or the start of a |
4574 | // preprocessor line marker. |
4575 | if (getLexer().is(K: AsmToken::Identifier)) { |
4576 | if (getTok().getIdentifier() == ".endm" || |
4577 | getTok().getIdentifier() == ".endmacro" ) { |
4578 | if (MacroDepth == 0) { // Outermost macro. |
4579 | EndToken = getTok(); |
4580 | Lexer.Lex(); |
4581 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
4582 | return TokError(Msg: "unexpected token in '" + EndToken.getIdentifier() + |
4583 | "' directive" ); |
4584 | break; |
4585 | } else { |
4586 | // Otherwise we just found the end of an inner macro. |
4587 | --MacroDepth; |
4588 | } |
4589 | } else if (getTok().getIdentifier() == ".macro" ) { |
4590 | // We allow nested macros. Those aren't instantiated until the outermost |
4591 | // macro is expanded so just ignore them for now. |
4592 | ++MacroDepth; |
4593 | } |
4594 | } else if (Lexer.is(K: AsmToken::HashDirective)) { |
4595 | (void)parseCppHashLineFilenameComment(L: getLexer().getLoc()); |
4596 | } |
4597 | |
4598 | // Otherwise, scan til the end of the statement. |
4599 | eatToEndOfStatement(); |
4600 | } |
4601 | |
4602 | if (getContext().lookupMacro(Name)) { |
4603 | return Error(L: DirectiveLoc, Msg: "macro '" + Name + "' is already defined" ); |
4604 | } |
4605 | |
4606 | const char *BodyStart = StartToken.getLoc().getPointer(); |
4607 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
4608 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
4609 | checkForBadMacro(DirectiveLoc, Name, Body, Parameters); |
4610 | MCAsmMacro Macro(Name, Body, std::move(Parameters)); |
4611 | DEBUG_WITH_TYPE("asm-macros" , dbgs() << "Defining new macro:\n" ; |
4612 | Macro.dump()); |
4613 | getContext().defineMacro(Name, Macro: std::move(Macro)); |
4614 | return false; |
4615 | } |
4616 | |
4617 | /// checkForBadMacro |
4618 | /// |
4619 | /// With the support added for named parameters there may be code out there that |
4620 | /// is transitioning from positional parameters. In versions of gas that did |
4621 | /// not support named parameters they would be ignored on the macro definition. |
4622 | /// But to support both styles of parameters this is not possible so if a macro |
4623 | /// definition has named parameters but does not use them and has what appears |
4624 | /// to be positional parameters, strings like $1, $2, ... and $n, then issue a |
4625 | /// warning that the positional parameter found in body which have no effect. |
4626 | /// Hoping the developer will either remove the named parameters from the macro |
4627 | /// definition so the positional parameters get used if that was what was |
4628 | /// intended or change the macro to use the named parameters. It is possible |
4629 | /// this warning will trigger when the none of the named parameters are used |
4630 | /// and the strings like $1 are infact to simply to be passed trough unchanged. |
4631 | void AsmParser::checkForBadMacro(SMLoc DirectiveLoc, StringRef Name, |
4632 | StringRef Body, |
4633 | ArrayRef<MCAsmMacroParameter> Parameters) { |
4634 | // If this macro is not defined with named parameters the warning we are |
4635 | // checking for here doesn't apply. |
4636 | unsigned NParameters = Parameters.size(); |
4637 | if (NParameters == 0) |
4638 | return; |
4639 | |
4640 | bool NamedParametersFound = false; |
4641 | bool PositionalParametersFound = false; |
4642 | |
4643 | // Look at the body of the macro for use of both the named parameters and what |
4644 | // are likely to be positional parameters. This is what expandMacro() is |
4645 | // doing when it finds the parameters in the body. |
4646 | while (!Body.empty()) { |
4647 | // Scan for the next possible parameter. |
4648 | std::size_t End = Body.size(), Pos = 0; |
4649 | for (; Pos != End; ++Pos) { |
4650 | // Check for a substitution or escape. |
4651 | // This macro is defined with parameters, look for \foo, \bar, etc. |
4652 | if (Body[Pos] == '\\' && Pos + 1 != End) |
4653 | break; |
4654 | |
4655 | // This macro should have parameters, but look for $0, $1, ..., $n too. |
4656 | if (Body[Pos] != '$' || Pos + 1 == End) |
4657 | continue; |
4658 | char Next = Body[Pos + 1]; |
4659 | if (Next == '$' || Next == 'n' || |
4660 | isdigit(static_cast<unsigned char>(Next))) |
4661 | break; |
4662 | } |
4663 | |
4664 | // Check if we reached the end. |
4665 | if (Pos == End) |
4666 | break; |
4667 | |
4668 | if (Body[Pos] == '$') { |
4669 | switch (Body[Pos + 1]) { |
4670 | // $$ => $ |
4671 | case '$': |
4672 | break; |
4673 | |
4674 | // $n => number of arguments |
4675 | case 'n': |
4676 | PositionalParametersFound = true; |
4677 | break; |
4678 | |
4679 | // $[0-9] => argument |
4680 | default: { |
4681 | PositionalParametersFound = true; |
4682 | break; |
4683 | } |
4684 | } |
4685 | Pos += 2; |
4686 | } else { |
4687 | unsigned I = Pos + 1; |
4688 | while (isIdentifierChar(c: Body[I]) && I + 1 != End) |
4689 | ++I; |
4690 | |
4691 | const char *Begin = Body.data() + Pos + 1; |
4692 | StringRef Argument(Begin, I - (Pos + 1)); |
4693 | unsigned Index = 0; |
4694 | for (; Index < NParameters; ++Index) |
4695 | if (Parameters[Index].Name == Argument) |
4696 | break; |
4697 | |
4698 | if (Index == NParameters) { |
4699 | if (Body[Pos + 1] == '(' && Body[Pos + 2] == ')') |
4700 | Pos += 3; |
4701 | else { |
4702 | Pos = I; |
4703 | } |
4704 | } else { |
4705 | NamedParametersFound = true; |
4706 | Pos += 1 + Argument.size(); |
4707 | } |
4708 | } |
4709 | // Update the scan point. |
4710 | Body = Body.substr(Start: Pos); |
4711 | } |
4712 | |
4713 | if (!NamedParametersFound && PositionalParametersFound) |
4714 | Warning(L: DirectiveLoc, Msg: "macro defined with named parameters which are not " |
4715 | "used in macro body, possible positional parameter " |
4716 | "found in body which will have no effect" ); |
4717 | } |
4718 | |
4719 | /// parseDirectiveExitMacro |
4720 | /// ::= .exitm |
4721 | bool AsmParser::parseDirectiveExitMacro(StringRef Directive) { |
4722 | if (parseEOL()) |
4723 | return true; |
4724 | |
4725 | if (!isInsideMacroInstantiation()) |
4726 | return TokError(Msg: "unexpected '" + Directive + "' in file, " |
4727 | "no current macro definition" ); |
4728 | |
4729 | // Exit all conditionals that are active in the current macro. |
4730 | while (TheCondStack.size() != ActiveMacros.back()->CondStackDepth) { |
4731 | TheCondState = TheCondStack.back(); |
4732 | TheCondStack.pop_back(); |
4733 | } |
4734 | |
4735 | handleMacroExit(); |
4736 | return false; |
4737 | } |
4738 | |
4739 | /// parseDirectiveEndMacro |
4740 | /// ::= .endm |
4741 | /// ::= .endmacro |
4742 | bool AsmParser::parseDirectiveEndMacro(StringRef Directive) { |
4743 | if (getLexer().isNot(K: AsmToken::EndOfStatement)) |
4744 | return TokError(Msg: "unexpected token in '" + Directive + "' directive" ); |
4745 | |
4746 | // If we are inside a macro instantiation, terminate the current |
4747 | // instantiation. |
4748 | if (isInsideMacroInstantiation()) { |
4749 | handleMacroExit(); |
4750 | return false; |
4751 | } |
4752 | |
4753 | // Otherwise, this .endmacro is a stray entry in the file; well formed |
4754 | // .endmacro directives are handled during the macro definition parsing. |
4755 | return TokError(Msg: "unexpected '" + Directive + "' in file, " |
4756 | "no current macro definition" ); |
4757 | } |
4758 | |
4759 | /// parseDirectivePurgeMacro |
4760 | /// ::= .purgem name |
4761 | bool AsmParser::parseDirectivePurgeMacro(SMLoc DirectiveLoc) { |
4762 | StringRef Name; |
4763 | SMLoc Loc; |
4764 | if (parseTokenLoc(Loc) || |
4765 | check(P: parseIdentifier(Res&: Name), Loc, |
4766 | Msg: "expected identifier in '.purgem' directive" ) || |
4767 | parseEOL()) |
4768 | return true; |
4769 | |
4770 | if (!getContext().lookupMacro(Name)) |
4771 | return Error(L: DirectiveLoc, Msg: "macro '" + Name + "' is not defined" ); |
4772 | |
4773 | getContext().undefineMacro(Name); |
4774 | DEBUG_WITH_TYPE("asm-macros" , dbgs() |
4775 | << "Un-defining macro: " << Name << "\n" ); |
4776 | return false; |
4777 | } |
4778 | |
4779 | /// parseDirectiveBundleAlignMode |
4780 | /// ::= {.bundle_align_mode} expression |
4781 | bool AsmParser::parseDirectiveBundleAlignMode() { |
4782 | // Expect a single argument: an expression that evaluates to a constant |
4783 | // in the inclusive range 0-30. |
4784 | SMLoc ExprLoc = getLexer().getLoc(); |
4785 | int64_t AlignSizePow2; |
4786 | if (checkForValidSection() || parseAbsoluteExpression(Res&: AlignSizePow2) || |
4787 | parseEOL() || |
4788 | check(P: AlignSizePow2 < 0 || AlignSizePow2 > 30, Loc: ExprLoc, |
4789 | Msg: "invalid bundle alignment size (expected between 0 and 30)" )) |
4790 | return true; |
4791 | |
4792 | getStreamer().emitBundleAlignMode(Alignment: Align(1ULL << AlignSizePow2)); |
4793 | return false; |
4794 | } |
4795 | |
4796 | /// parseDirectiveBundleLock |
4797 | /// ::= {.bundle_lock} [align_to_end] |
4798 | bool AsmParser::parseDirectiveBundleLock() { |
4799 | if (checkForValidSection()) |
4800 | return true; |
4801 | bool AlignToEnd = false; |
4802 | |
4803 | StringRef Option; |
4804 | SMLoc Loc = getTok().getLoc(); |
4805 | const char *kInvalidOptionError = |
4806 | "invalid option for '.bundle_lock' directive" ; |
4807 | |
4808 | if (!parseOptionalToken(T: AsmToken::EndOfStatement)) { |
4809 | if (check(P: parseIdentifier(Res&: Option), Loc, Msg: kInvalidOptionError) || |
4810 | check(P: Option != "align_to_end" , Loc, Msg: kInvalidOptionError) || parseEOL()) |
4811 | return true; |
4812 | AlignToEnd = true; |
4813 | } |
4814 | |
4815 | getStreamer().emitBundleLock(AlignToEnd); |
4816 | return false; |
4817 | } |
4818 | |
4819 | /// parseDirectiveBundleLock |
4820 | /// ::= {.bundle_lock} |
4821 | bool AsmParser::parseDirectiveBundleUnlock() { |
4822 | if (checkForValidSection() || parseEOL()) |
4823 | return true; |
4824 | |
4825 | getStreamer().emitBundleUnlock(); |
4826 | return false; |
4827 | } |
4828 | |
4829 | /// parseDirectiveSpace |
4830 | /// ::= (.skip | .space) expression [ , expression ] |
4831 | bool AsmParser::parseDirectiveSpace(StringRef IDVal) { |
4832 | SMLoc NumBytesLoc = Lexer.getLoc(); |
4833 | const MCExpr *NumBytes; |
4834 | if (checkForValidSection() || parseExpression(Res&: NumBytes)) |
4835 | return true; |
4836 | |
4837 | int64_t FillExpr = 0; |
4838 | if (parseOptionalToken(T: AsmToken::Comma)) |
4839 | if (parseAbsoluteExpression(Res&: FillExpr)) |
4840 | return true; |
4841 | if (parseEOL()) |
4842 | return true; |
4843 | |
4844 | // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0. |
4845 | getStreamer().emitFill(NumBytes: *NumBytes, FillValue: FillExpr, Loc: NumBytesLoc); |
4846 | |
4847 | return false; |
4848 | } |
4849 | |
4850 | /// parseDirectiveDCB |
4851 | /// ::= .dcb.{b, l, w} expression, expression |
4852 | bool AsmParser::parseDirectiveDCB(StringRef IDVal, unsigned Size) { |
4853 | SMLoc NumValuesLoc = Lexer.getLoc(); |
4854 | int64_t NumValues; |
4855 | if (checkForValidSection() || parseAbsoluteExpression(Res&: NumValues)) |
4856 | return true; |
4857 | |
4858 | if (NumValues < 0) { |
4859 | Warning(L: NumValuesLoc, Msg: "'" + Twine(IDVal) + "' directive with negative repeat count has no effect" ); |
4860 | return false; |
4861 | } |
4862 | |
4863 | if (parseComma()) |
4864 | return true; |
4865 | |
4866 | const MCExpr *Value; |
4867 | SMLoc ExprLoc = getLexer().getLoc(); |
4868 | if (parseExpression(Res&: Value)) |
4869 | return true; |
4870 | |
4871 | // Special case constant expressions to match code generator. |
4872 | if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value)) { |
4873 | assert(Size <= 8 && "Invalid size" ); |
4874 | uint64_t IntValue = MCE->getValue(); |
4875 | if (!isUIntN(N: 8 * Size, x: IntValue) && !isIntN(N: 8 * Size, x: IntValue)) |
4876 | return Error(L: ExprLoc, Msg: "literal value out of range for directive" ); |
4877 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
4878 | getStreamer().emitIntValue(Value: IntValue, Size); |
4879 | } else { |
4880 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
4881 | getStreamer().emitValue(Value, Size, Loc: ExprLoc); |
4882 | } |
4883 | |
4884 | return parseEOL(); |
4885 | } |
4886 | |
4887 | /// parseDirectiveRealDCB |
4888 | /// ::= .dcb.{d, s} expression, expression |
4889 | bool AsmParser::parseDirectiveRealDCB(StringRef IDVal, const fltSemantics &Semantics) { |
4890 | SMLoc NumValuesLoc = Lexer.getLoc(); |
4891 | int64_t NumValues; |
4892 | if (checkForValidSection() || parseAbsoluteExpression(Res&: NumValues)) |
4893 | return true; |
4894 | |
4895 | if (NumValues < 0) { |
4896 | Warning(L: NumValuesLoc, Msg: "'" + Twine(IDVal) + "' directive with negative repeat count has no effect" ); |
4897 | return false; |
4898 | } |
4899 | |
4900 | if (parseComma()) |
4901 | return true; |
4902 | |
4903 | APInt AsInt; |
4904 | if (parseRealValue(Semantics, Res&: AsInt) || parseEOL()) |
4905 | return true; |
4906 | |
4907 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
4908 | getStreamer().emitIntValue(Value: AsInt.getLimitedValue(), |
4909 | Size: AsInt.getBitWidth() / 8); |
4910 | |
4911 | return false; |
4912 | } |
4913 | |
4914 | /// parseDirectiveDS |
4915 | /// ::= .ds.{b, d, l, p, s, w, x} expression |
4916 | bool AsmParser::parseDirectiveDS(StringRef IDVal, unsigned Size) { |
4917 | SMLoc NumValuesLoc = Lexer.getLoc(); |
4918 | int64_t NumValues; |
4919 | if (checkForValidSection() || parseAbsoluteExpression(Res&: NumValues) || |
4920 | parseEOL()) |
4921 | return true; |
4922 | |
4923 | if (NumValues < 0) { |
4924 | Warning(L: NumValuesLoc, Msg: "'" + Twine(IDVal) + "' directive with negative repeat count has no effect" ); |
4925 | return false; |
4926 | } |
4927 | |
4928 | for (uint64_t i = 0, e = NumValues; i != e; ++i) |
4929 | getStreamer().emitFill(NumBytes: Size, FillValue: 0); |
4930 | |
4931 | return false; |
4932 | } |
4933 | |
4934 | /// parseDirectiveLEB128 |
4935 | /// ::= (.sleb128 | .uleb128) [ expression (, expression)* ] |
4936 | bool AsmParser::parseDirectiveLEB128(bool Signed) { |
4937 | if (checkForValidSection()) |
4938 | return true; |
4939 | |
4940 | auto parseOp = [&]() -> bool { |
4941 | const MCExpr *Value; |
4942 | if (parseExpression(Res&: Value)) |
4943 | return true; |
4944 | if (Signed) |
4945 | getStreamer().emitSLEB128Value(Value); |
4946 | else |
4947 | getStreamer().emitULEB128Value(Value); |
4948 | return false; |
4949 | }; |
4950 | |
4951 | return parseMany(parseOne: parseOp); |
4952 | } |
4953 | |
4954 | /// parseDirectiveSymbolAttribute |
4955 | /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ] |
4956 | bool AsmParser::parseDirectiveSymbolAttribute(MCSymbolAttr Attr) { |
4957 | auto parseOp = [&]() -> bool { |
4958 | StringRef Name; |
4959 | SMLoc Loc = getTok().getLoc(); |
4960 | if (parseIdentifier(Res&: Name)) |
4961 | return Error(L: Loc, Msg: "expected identifier" ); |
4962 | |
4963 | if (discardLTOSymbol(Name)) |
4964 | return false; |
4965 | |
4966 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
4967 | |
4968 | // Assembler local symbols don't make any sense here, except for directives |
4969 | // that the symbol should be tagged. |
4970 | if (Sym->isTemporary() && Attr != MCSA_Memtag) |
4971 | return Error(L: Loc, Msg: "non-local symbol required" ); |
4972 | |
4973 | if (!getStreamer().emitSymbolAttribute(Symbol: Sym, Attribute: Attr)) |
4974 | return Error(L: Loc, Msg: "unable to emit symbol attribute" ); |
4975 | return false; |
4976 | }; |
4977 | |
4978 | return parseMany(parseOne: parseOp); |
4979 | } |
4980 | |
4981 | /// parseDirectiveComm |
4982 | /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ] |
4983 | bool AsmParser::parseDirectiveComm(bool IsLocal) { |
4984 | if (checkForValidSection()) |
4985 | return true; |
4986 | |
4987 | SMLoc IDLoc = getLexer().getLoc(); |
4988 | StringRef Name; |
4989 | if (parseIdentifier(Res&: Name)) |
4990 | return TokError(Msg: "expected identifier in directive" ); |
4991 | |
4992 | // Handle the identifier as the key symbol. |
4993 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
4994 | |
4995 | if (parseComma()) |
4996 | return true; |
4997 | |
4998 | int64_t Size; |
4999 | SMLoc SizeLoc = getLexer().getLoc(); |
5000 | if (parseAbsoluteExpression(Res&: Size)) |
5001 | return true; |
5002 | |
5003 | int64_t Pow2Alignment = 0; |
5004 | SMLoc Pow2AlignmentLoc; |
5005 | if (getLexer().is(K: AsmToken::Comma)) { |
5006 | Lex(); |
5007 | Pow2AlignmentLoc = getLexer().getLoc(); |
5008 | if (parseAbsoluteExpression(Res&: Pow2Alignment)) |
5009 | return true; |
5010 | |
5011 | LCOMM::LCOMMType LCOMM = Lexer.getMAI().getLCOMMDirectiveAlignmentType(); |
5012 | if (IsLocal && LCOMM == LCOMM::NoAlignment) |
5013 | return Error(L: Pow2AlignmentLoc, Msg: "alignment not supported on this target" ); |
5014 | |
5015 | // If this target takes alignments in bytes (not log) validate and convert. |
5016 | if ((!IsLocal && Lexer.getMAI().getCOMMDirectiveAlignmentIsInBytes()) || |
5017 | (IsLocal && LCOMM == LCOMM::ByteAlignment)) { |
5018 | if (!isPowerOf2_64(Value: Pow2Alignment)) |
5019 | return Error(L: Pow2AlignmentLoc, Msg: "alignment must be a power of 2" ); |
5020 | Pow2Alignment = Log2_64(Value: Pow2Alignment); |
5021 | } |
5022 | } |
5023 | |
5024 | if (parseEOL()) |
5025 | return true; |
5026 | |
5027 | // NOTE: a size of zero for a .comm should create a undefined symbol |
5028 | // but a size of .lcomm creates a bss symbol of size zero. |
5029 | if (Size < 0) |
5030 | return Error(L: SizeLoc, Msg: "size must be non-negative" ); |
5031 | |
5032 | Sym->redefineIfPossible(); |
5033 | if (!Sym->isUndefined()) |
5034 | return Error(L: IDLoc, Msg: "invalid symbol redefinition" ); |
5035 | |
5036 | // Create the Symbol as a common or local common with Size and Pow2Alignment |
5037 | if (IsLocal) { |
5038 | getStreamer().emitLocalCommonSymbol(Symbol: Sym, Size, |
5039 | ByteAlignment: Align(1ULL << Pow2Alignment)); |
5040 | return false; |
5041 | } |
5042 | |
5043 | getStreamer().emitCommonSymbol(Symbol: Sym, Size, ByteAlignment: Align(1ULL << Pow2Alignment)); |
5044 | return false; |
5045 | } |
5046 | |
5047 | /// parseDirectiveAbort |
5048 | /// ::= .abort [... message ...] |
5049 | bool AsmParser::parseDirectiveAbort(SMLoc DirectiveLoc) { |
5050 | StringRef Str = parseStringToEndOfStatement(); |
5051 | if (parseEOL()) |
5052 | return true; |
5053 | |
5054 | if (Str.empty()) |
5055 | return Error(L: DirectiveLoc, Msg: ".abort detected. Assembly stopping" ); |
5056 | |
5057 | // FIXME: Actually abort assembly here. |
5058 | return Error(L: DirectiveLoc, |
5059 | Msg: ".abort '" + Str + "' detected. Assembly stopping" ); |
5060 | } |
5061 | |
5062 | /// parseDirectiveInclude |
5063 | /// ::= .include "filename" |
5064 | bool AsmParser::parseDirectiveInclude() { |
5065 | // Allow the strings to have escaped octal character sequence. |
5066 | std::string Filename; |
5067 | SMLoc IncludeLoc = getTok().getLoc(); |
5068 | |
5069 | if (check(P: getTok().isNot(K: AsmToken::String), |
5070 | Msg: "expected string in '.include' directive" ) || |
5071 | parseEscapedString(Data&: Filename) || |
5072 | check(P: getTok().isNot(K: AsmToken::EndOfStatement), |
5073 | Msg: "unexpected token in '.include' directive" ) || |
5074 | // Attempt to switch the lexer to the included file before consuming the |
5075 | // end of statement to avoid losing it when we switch. |
5076 | check(P: enterIncludeFile(Filename), Loc: IncludeLoc, |
5077 | Msg: "Could not find include file '" + Filename + "'" )) |
5078 | return true; |
5079 | |
5080 | return false; |
5081 | } |
5082 | |
5083 | /// parseDirectiveIncbin |
5084 | /// ::= .incbin "filename" [ , skip [ , count ] ] |
5085 | bool AsmParser::parseDirectiveIncbin() { |
5086 | // Allow the strings to have escaped octal character sequence. |
5087 | std::string Filename; |
5088 | SMLoc IncbinLoc = getTok().getLoc(); |
5089 | if (check(P: getTok().isNot(K: AsmToken::String), |
5090 | Msg: "expected string in '.incbin' directive" ) || |
5091 | parseEscapedString(Data&: Filename)) |
5092 | return true; |
5093 | |
5094 | int64_t Skip = 0; |
5095 | const MCExpr *Count = nullptr; |
5096 | SMLoc SkipLoc, CountLoc; |
5097 | if (parseOptionalToken(T: AsmToken::Comma)) { |
5098 | // The skip expression can be omitted while specifying the count, e.g: |
5099 | // .incbin "filename",,4 |
5100 | if (getTok().isNot(K: AsmToken::Comma)) { |
5101 | if (parseTokenLoc(Loc&: SkipLoc) || parseAbsoluteExpression(Res&: Skip)) |
5102 | return true; |
5103 | } |
5104 | if (parseOptionalToken(T: AsmToken::Comma)) { |
5105 | CountLoc = getTok().getLoc(); |
5106 | if (parseExpression(Res&: Count)) |
5107 | return true; |
5108 | } |
5109 | } |
5110 | |
5111 | if (parseEOL()) |
5112 | return true; |
5113 | |
5114 | if (check(P: Skip < 0, Loc: SkipLoc, Msg: "skip is negative" )) |
5115 | return true; |
5116 | |
5117 | // Attempt to process the included file. |
5118 | if (processIncbinFile(Filename, Skip, Count, Loc: CountLoc)) |
5119 | return Error(L: IncbinLoc, Msg: "Could not find incbin file '" + Filename + "'" ); |
5120 | return false; |
5121 | } |
5122 | |
5123 | /// parseDirectiveIf |
5124 | /// ::= .if{,eq,ge,gt,le,lt,ne} expression |
5125 | bool AsmParser::parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind) { |
5126 | TheCondStack.push_back(x: TheCondState); |
5127 | TheCondState.TheCond = AsmCond::IfCond; |
5128 | if (TheCondState.Ignore) { |
5129 | eatToEndOfStatement(); |
5130 | } else { |
5131 | int64_t ExprValue; |
5132 | if (parseAbsoluteExpression(Res&: ExprValue) || parseEOL()) |
5133 | return true; |
5134 | |
5135 | switch (DirKind) { |
5136 | default: |
5137 | llvm_unreachable("unsupported directive" ); |
5138 | case DK_IF: |
5139 | case DK_IFNE: |
5140 | break; |
5141 | case DK_IFEQ: |
5142 | ExprValue = ExprValue == 0; |
5143 | break; |
5144 | case DK_IFGE: |
5145 | ExprValue = ExprValue >= 0; |
5146 | break; |
5147 | case DK_IFGT: |
5148 | ExprValue = ExprValue > 0; |
5149 | break; |
5150 | case DK_IFLE: |
5151 | ExprValue = ExprValue <= 0; |
5152 | break; |
5153 | case DK_IFLT: |
5154 | ExprValue = ExprValue < 0; |
5155 | break; |
5156 | } |
5157 | |
5158 | TheCondState.CondMet = ExprValue; |
5159 | TheCondState.Ignore = !TheCondState.CondMet; |
5160 | } |
5161 | |
5162 | return false; |
5163 | } |
5164 | |
5165 | /// parseDirectiveIfb |
5166 | /// ::= .ifb string |
5167 | bool AsmParser::parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank) { |
5168 | TheCondStack.push_back(x: TheCondState); |
5169 | TheCondState.TheCond = AsmCond::IfCond; |
5170 | |
5171 | if (TheCondState.Ignore) { |
5172 | eatToEndOfStatement(); |
5173 | } else { |
5174 | StringRef Str = parseStringToEndOfStatement(); |
5175 | |
5176 | if (parseEOL()) |
5177 | return true; |
5178 | |
5179 | TheCondState.CondMet = ExpectBlank == Str.empty(); |
5180 | TheCondState.Ignore = !TheCondState.CondMet; |
5181 | } |
5182 | |
5183 | return false; |
5184 | } |
5185 | |
5186 | /// parseDirectiveIfc |
5187 | /// ::= .ifc string1, string2 |
5188 | /// ::= .ifnc string1, string2 |
5189 | bool AsmParser::parseDirectiveIfc(SMLoc DirectiveLoc, bool ExpectEqual) { |
5190 | TheCondStack.push_back(x: TheCondState); |
5191 | TheCondState.TheCond = AsmCond::IfCond; |
5192 | |
5193 | if (TheCondState.Ignore) { |
5194 | eatToEndOfStatement(); |
5195 | } else { |
5196 | StringRef Str1 = parseStringToComma(); |
5197 | |
5198 | if (parseComma()) |
5199 | return true; |
5200 | |
5201 | StringRef Str2 = parseStringToEndOfStatement(); |
5202 | |
5203 | if (parseEOL()) |
5204 | return true; |
5205 | |
5206 | TheCondState.CondMet = ExpectEqual == (Str1.trim() == Str2.trim()); |
5207 | TheCondState.Ignore = !TheCondState.CondMet; |
5208 | } |
5209 | |
5210 | return false; |
5211 | } |
5212 | |
5213 | /// parseDirectiveIfeqs |
5214 | /// ::= .ifeqs string1, string2 |
5215 | bool AsmParser::parseDirectiveIfeqs(SMLoc DirectiveLoc, bool ExpectEqual) { |
5216 | TheCondStack.push_back(x: TheCondState); |
5217 | TheCondState.TheCond = AsmCond::IfCond; |
5218 | |
5219 | if (TheCondState.Ignore) { |
5220 | eatToEndOfStatement(); |
5221 | } else { |
5222 | if (Lexer.isNot(K: AsmToken::String)) { |
5223 | if (ExpectEqual) |
5224 | return TokError(Msg: "expected string parameter for '.ifeqs' directive" ); |
5225 | return TokError(Msg: "expected string parameter for '.ifnes' directive" ); |
5226 | } |
5227 | |
5228 | StringRef String1 = getTok().getStringContents(); |
5229 | Lex(); |
5230 | |
5231 | if (Lexer.isNot(K: AsmToken::Comma)) { |
5232 | if (ExpectEqual) |
5233 | return TokError( |
5234 | Msg: "expected comma after first string for '.ifeqs' directive" ); |
5235 | return TokError( |
5236 | Msg: "expected comma after first string for '.ifnes' directive" ); |
5237 | } |
5238 | |
5239 | Lex(); |
5240 | |
5241 | if (Lexer.isNot(K: AsmToken::String)) { |
5242 | if (ExpectEqual) |
5243 | return TokError(Msg: "expected string parameter for '.ifeqs' directive" ); |
5244 | return TokError(Msg: "expected string parameter for '.ifnes' directive" ); |
5245 | } |
5246 | |
5247 | StringRef String2 = getTok().getStringContents(); |
5248 | Lex(); |
5249 | |
5250 | TheCondState.CondMet = ExpectEqual == (String1 == String2); |
5251 | TheCondState.Ignore = !TheCondState.CondMet; |
5252 | } |
5253 | |
5254 | return false; |
5255 | } |
5256 | |
5257 | /// parseDirectiveIfdef |
5258 | /// ::= .ifdef symbol |
5259 | bool AsmParser::parseDirectiveIfdef(SMLoc DirectiveLoc, bool expect_defined) { |
5260 | StringRef Name; |
5261 | TheCondStack.push_back(x: TheCondState); |
5262 | TheCondState.TheCond = AsmCond::IfCond; |
5263 | |
5264 | if (TheCondState.Ignore) { |
5265 | eatToEndOfStatement(); |
5266 | } else { |
5267 | if (check(P: parseIdentifier(Res&: Name), Msg: "expected identifier after '.ifdef'" ) || |
5268 | parseEOL()) |
5269 | return true; |
5270 | |
5271 | MCSymbol *Sym = getContext().lookupSymbol(Name); |
5272 | |
5273 | if (expect_defined) |
5274 | TheCondState.CondMet = (Sym && !Sym->isUndefined()); |
5275 | else |
5276 | TheCondState.CondMet = (!Sym || Sym->isUndefined()); |
5277 | TheCondState.Ignore = !TheCondState.CondMet; |
5278 | } |
5279 | |
5280 | return false; |
5281 | } |
5282 | |
5283 | /// parseDirectiveElseIf |
5284 | /// ::= .elseif expression |
5285 | bool AsmParser::parseDirectiveElseIf(SMLoc DirectiveLoc) { |
5286 | if (TheCondState.TheCond != AsmCond::IfCond && |
5287 | TheCondState.TheCond != AsmCond::ElseIfCond) |
5288 | return Error(L: DirectiveLoc, Msg: "Encountered a .elseif that doesn't follow an" |
5289 | " .if or an .elseif" ); |
5290 | TheCondState.TheCond = AsmCond::ElseIfCond; |
5291 | |
5292 | bool LastIgnoreState = false; |
5293 | if (!TheCondStack.empty()) |
5294 | LastIgnoreState = TheCondStack.back().Ignore; |
5295 | if (LastIgnoreState || TheCondState.CondMet) { |
5296 | TheCondState.Ignore = true; |
5297 | eatToEndOfStatement(); |
5298 | } else { |
5299 | int64_t ExprValue; |
5300 | if (parseAbsoluteExpression(Res&: ExprValue)) |
5301 | return true; |
5302 | |
5303 | if (parseEOL()) |
5304 | return true; |
5305 | |
5306 | TheCondState.CondMet = ExprValue; |
5307 | TheCondState.Ignore = !TheCondState.CondMet; |
5308 | } |
5309 | |
5310 | return false; |
5311 | } |
5312 | |
5313 | /// parseDirectiveElse |
5314 | /// ::= .else |
5315 | bool AsmParser::parseDirectiveElse(SMLoc DirectiveLoc) { |
5316 | if (parseEOL()) |
5317 | return true; |
5318 | |
5319 | if (TheCondState.TheCond != AsmCond::IfCond && |
5320 | TheCondState.TheCond != AsmCond::ElseIfCond) |
5321 | return Error(L: DirectiveLoc, Msg: "Encountered a .else that doesn't follow " |
5322 | " an .if or an .elseif" ); |
5323 | TheCondState.TheCond = AsmCond::ElseCond; |
5324 | bool LastIgnoreState = false; |
5325 | if (!TheCondStack.empty()) |
5326 | LastIgnoreState = TheCondStack.back().Ignore; |
5327 | if (LastIgnoreState || TheCondState.CondMet) |
5328 | TheCondState.Ignore = true; |
5329 | else |
5330 | TheCondState.Ignore = false; |
5331 | |
5332 | return false; |
5333 | } |
5334 | |
5335 | /// parseDirectiveEnd |
5336 | /// ::= .end |
5337 | bool AsmParser::parseDirectiveEnd(SMLoc DirectiveLoc) { |
5338 | if (parseEOL()) |
5339 | return true; |
5340 | |
5341 | while (Lexer.isNot(K: AsmToken::Eof)) |
5342 | Lexer.Lex(); |
5343 | |
5344 | return false; |
5345 | } |
5346 | |
5347 | /// parseDirectiveError |
5348 | /// ::= .err |
5349 | /// ::= .error [string] |
5350 | bool AsmParser::parseDirectiveError(SMLoc L, bool WithMessage) { |
5351 | if (!TheCondStack.empty()) { |
5352 | if (TheCondStack.back().Ignore) { |
5353 | eatToEndOfStatement(); |
5354 | return false; |
5355 | } |
5356 | } |
5357 | |
5358 | if (!WithMessage) |
5359 | return Error(L, Msg: ".err encountered" ); |
5360 | |
5361 | StringRef Message = ".error directive invoked in source file" ; |
5362 | if (Lexer.isNot(K: AsmToken::EndOfStatement)) { |
5363 | if (Lexer.isNot(K: AsmToken::String)) |
5364 | return TokError(Msg: ".error argument must be a string" ); |
5365 | |
5366 | Message = getTok().getStringContents(); |
5367 | Lex(); |
5368 | } |
5369 | |
5370 | return Error(L, Msg: Message); |
5371 | } |
5372 | |
5373 | /// parseDirectiveWarning |
5374 | /// ::= .warning [string] |
5375 | bool AsmParser::parseDirectiveWarning(SMLoc L) { |
5376 | if (!TheCondStack.empty()) { |
5377 | if (TheCondStack.back().Ignore) { |
5378 | eatToEndOfStatement(); |
5379 | return false; |
5380 | } |
5381 | } |
5382 | |
5383 | StringRef Message = ".warning directive invoked in source file" ; |
5384 | |
5385 | if (!parseOptionalToken(T: AsmToken::EndOfStatement)) { |
5386 | if (Lexer.isNot(K: AsmToken::String)) |
5387 | return TokError(Msg: ".warning argument must be a string" ); |
5388 | |
5389 | Message = getTok().getStringContents(); |
5390 | Lex(); |
5391 | if (parseEOL()) |
5392 | return true; |
5393 | } |
5394 | |
5395 | return Warning(L, Msg: Message); |
5396 | } |
5397 | |
5398 | /// parseDirectiveEndIf |
5399 | /// ::= .endif |
5400 | bool AsmParser::parseDirectiveEndIf(SMLoc DirectiveLoc) { |
5401 | if (parseEOL()) |
5402 | return true; |
5403 | |
5404 | if ((TheCondState.TheCond == AsmCond::NoCond) || TheCondStack.empty()) |
5405 | return Error(L: DirectiveLoc, Msg: "Encountered a .endif that doesn't follow " |
5406 | "an .if or .else" ); |
5407 | if (!TheCondStack.empty()) { |
5408 | TheCondState = TheCondStack.back(); |
5409 | TheCondStack.pop_back(); |
5410 | } |
5411 | |
5412 | return false; |
5413 | } |
5414 | |
5415 | void AsmParser::initializeDirectiveKindMap() { |
5416 | /* Lookup will be done with the directive |
5417 | * converted to lower case, so all these |
5418 | * keys should be lower case. |
5419 | * (target specific directives are handled |
5420 | * elsewhere) |
5421 | */ |
5422 | DirectiveKindMap[".set" ] = DK_SET; |
5423 | DirectiveKindMap[".equ" ] = DK_EQU; |
5424 | DirectiveKindMap[".equiv" ] = DK_EQUIV; |
5425 | DirectiveKindMap[".ascii" ] = DK_ASCII; |
5426 | DirectiveKindMap[".asciz" ] = DK_ASCIZ; |
5427 | DirectiveKindMap[".string" ] = DK_STRING; |
5428 | DirectiveKindMap[".byte" ] = DK_BYTE; |
5429 | DirectiveKindMap[".short" ] = DK_SHORT; |
5430 | DirectiveKindMap[".value" ] = DK_VALUE; |
5431 | DirectiveKindMap[".2byte" ] = DK_2BYTE; |
5432 | DirectiveKindMap[".long" ] = DK_LONG; |
5433 | DirectiveKindMap[".int" ] = DK_INT; |
5434 | DirectiveKindMap[".4byte" ] = DK_4BYTE; |
5435 | DirectiveKindMap[".quad" ] = DK_QUAD; |
5436 | DirectiveKindMap[".8byte" ] = DK_8BYTE; |
5437 | DirectiveKindMap[".octa" ] = DK_OCTA; |
5438 | DirectiveKindMap[".single" ] = DK_SINGLE; |
5439 | DirectiveKindMap[".float" ] = DK_FLOAT; |
5440 | DirectiveKindMap[".double" ] = DK_DOUBLE; |
5441 | DirectiveKindMap[".align" ] = DK_ALIGN; |
5442 | DirectiveKindMap[".align32" ] = DK_ALIGN32; |
5443 | DirectiveKindMap[".balign" ] = DK_BALIGN; |
5444 | DirectiveKindMap[".balignw" ] = DK_BALIGNW; |
5445 | DirectiveKindMap[".balignl" ] = DK_BALIGNL; |
5446 | DirectiveKindMap[".p2align" ] = DK_P2ALIGN; |
5447 | DirectiveKindMap[".p2alignw" ] = DK_P2ALIGNW; |
5448 | DirectiveKindMap[".p2alignl" ] = DK_P2ALIGNL; |
5449 | DirectiveKindMap[".org" ] = DK_ORG; |
5450 | DirectiveKindMap[".fill" ] = DK_FILL; |
5451 | DirectiveKindMap[".zero" ] = DK_ZERO; |
5452 | DirectiveKindMap[".extern" ] = DK_EXTERN; |
5453 | DirectiveKindMap[".globl" ] = DK_GLOBL; |
5454 | DirectiveKindMap[".global" ] = DK_GLOBAL; |
5455 | DirectiveKindMap[".lazy_reference" ] = DK_LAZY_REFERENCE; |
5456 | DirectiveKindMap[".no_dead_strip" ] = DK_NO_DEAD_STRIP; |
5457 | DirectiveKindMap[".symbol_resolver" ] = DK_SYMBOL_RESOLVER; |
5458 | DirectiveKindMap[".private_extern" ] = DK_PRIVATE_EXTERN; |
5459 | DirectiveKindMap[".reference" ] = DK_REFERENCE; |
5460 | DirectiveKindMap[".weak_definition" ] = DK_WEAK_DEFINITION; |
5461 | DirectiveKindMap[".weak_reference" ] = DK_WEAK_REFERENCE; |
5462 | DirectiveKindMap[".weak_def_can_be_hidden" ] = DK_WEAK_DEF_CAN_BE_HIDDEN; |
5463 | DirectiveKindMap[".cold" ] = DK_COLD; |
5464 | DirectiveKindMap[".comm" ] = DK_COMM; |
5465 | DirectiveKindMap[".common" ] = DK_COMMON; |
5466 | DirectiveKindMap[".lcomm" ] = DK_LCOMM; |
5467 | DirectiveKindMap[".abort" ] = DK_ABORT; |
5468 | DirectiveKindMap[".include" ] = DK_INCLUDE; |
5469 | DirectiveKindMap[".incbin" ] = DK_INCBIN; |
5470 | DirectiveKindMap[".code16" ] = DK_CODE16; |
5471 | DirectiveKindMap[".code16gcc" ] = DK_CODE16GCC; |
5472 | DirectiveKindMap[".rept" ] = DK_REPT; |
5473 | DirectiveKindMap[".rep" ] = DK_REPT; |
5474 | DirectiveKindMap[".irp" ] = DK_IRP; |
5475 | DirectiveKindMap[".irpc" ] = DK_IRPC; |
5476 | DirectiveKindMap[".endr" ] = DK_ENDR; |
5477 | DirectiveKindMap[".bundle_align_mode" ] = DK_BUNDLE_ALIGN_MODE; |
5478 | DirectiveKindMap[".bundle_lock" ] = DK_BUNDLE_LOCK; |
5479 | DirectiveKindMap[".bundle_unlock" ] = DK_BUNDLE_UNLOCK; |
5480 | DirectiveKindMap[".if" ] = DK_IF; |
5481 | DirectiveKindMap[".ifeq" ] = DK_IFEQ; |
5482 | DirectiveKindMap[".ifge" ] = DK_IFGE; |
5483 | DirectiveKindMap[".ifgt" ] = DK_IFGT; |
5484 | DirectiveKindMap[".ifle" ] = DK_IFLE; |
5485 | DirectiveKindMap[".iflt" ] = DK_IFLT; |
5486 | DirectiveKindMap[".ifne" ] = DK_IFNE; |
5487 | DirectiveKindMap[".ifb" ] = DK_IFB; |
5488 | DirectiveKindMap[".ifnb" ] = DK_IFNB; |
5489 | DirectiveKindMap[".ifc" ] = DK_IFC; |
5490 | DirectiveKindMap[".ifeqs" ] = DK_IFEQS; |
5491 | DirectiveKindMap[".ifnc" ] = DK_IFNC; |
5492 | DirectiveKindMap[".ifnes" ] = DK_IFNES; |
5493 | DirectiveKindMap[".ifdef" ] = DK_IFDEF; |
5494 | DirectiveKindMap[".ifndef" ] = DK_IFNDEF; |
5495 | DirectiveKindMap[".ifnotdef" ] = DK_IFNOTDEF; |
5496 | DirectiveKindMap[".elseif" ] = DK_ELSEIF; |
5497 | DirectiveKindMap[".else" ] = DK_ELSE; |
5498 | DirectiveKindMap[".end" ] = DK_END; |
5499 | DirectiveKindMap[".endif" ] = DK_ENDIF; |
5500 | DirectiveKindMap[".skip" ] = DK_SKIP; |
5501 | DirectiveKindMap[".space" ] = DK_SPACE; |
5502 | DirectiveKindMap[".file" ] = DK_FILE; |
5503 | DirectiveKindMap[".line" ] = DK_LINE; |
5504 | DirectiveKindMap[".loc" ] = DK_LOC; |
5505 | DirectiveKindMap[".loc_label" ] = DK_LOC_LABEL; |
5506 | DirectiveKindMap[".stabs" ] = DK_STABS; |
5507 | DirectiveKindMap[".cv_file" ] = DK_CV_FILE; |
5508 | DirectiveKindMap[".cv_func_id" ] = DK_CV_FUNC_ID; |
5509 | DirectiveKindMap[".cv_loc" ] = DK_CV_LOC; |
5510 | DirectiveKindMap[".cv_linetable" ] = DK_CV_LINETABLE; |
5511 | DirectiveKindMap[".cv_inline_linetable" ] = DK_CV_INLINE_LINETABLE; |
5512 | DirectiveKindMap[".cv_inline_site_id" ] = DK_CV_INLINE_SITE_ID; |
5513 | DirectiveKindMap[".cv_def_range" ] = DK_CV_DEF_RANGE; |
5514 | DirectiveKindMap[".cv_string" ] = DK_CV_STRING; |
5515 | DirectiveKindMap[".cv_stringtable" ] = DK_CV_STRINGTABLE; |
5516 | DirectiveKindMap[".cv_filechecksums" ] = DK_CV_FILECHECKSUMS; |
5517 | DirectiveKindMap[".cv_filechecksumoffset" ] = DK_CV_FILECHECKSUM_OFFSET; |
5518 | DirectiveKindMap[".cv_fpo_data" ] = DK_CV_FPO_DATA; |
5519 | DirectiveKindMap[".sleb128" ] = DK_SLEB128; |
5520 | DirectiveKindMap[".uleb128" ] = DK_ULEB128; |
5521 | DirectiveKindMap[".cfi_sections" ] = DK_CFI_SECTIONS; |
5522 | DirectiveKindMap[".cfi_startproc" ] = DK_CFI_STARTPROC; |
5523 | DirectiveKindMap[".cfi_endproc" ] = DK_CFI_ENDPROC; |
5524 | DirectiveKindMap[".cfi_def_cfa" ] = DK_CFI_DEF_CFA; |
5525 | DirectiveKindMap[".cfi_def_cfa_offset" ] = DK_CFI_DEF_CFA_OFFSET; |
5526 | DirectiveKindMap[".cfi_adjust_cfa_offset" ] = DK_CFI_ADJUST_CFA_OFFSET; |
5527 | DirectiveKindMap[".cfi_def_cfa_register" ] = DK_CFI_DEF_CFA_REGISTER; |
5528 | DirectiveKindMap[".cfi_llvm_def_aspace_cfa" ] = DK_CFI_LLVM_DEF_ASPACE_CFA; |
5529 | DirectiveKindMap[".cfi_offset" ] = DK_CFI_OFFSET; |
5530 | DirectiveKindMap[".cfi_rel_offset" ] = DK_CFI_REL_OFFSET; |
5531 | DirectiveKindMap[".cfi_personality" ] = DK_CFI_PERSONALITY; |
5532 | DirectiveKindMap[".cfi_lsda" ] = DK_CFI_LSDA; |
5533 | DirectiveKindMap[".cfi_remember_state" ] = DK_CFI_REMEMBER_STATE; |
5534 | DirectiveKindMap[".cfi_restore_state" ] = DK_CFI_RESTORE_STATE; |
5535 | DirectiveKindMap[".cfi_same_value" ] = DK_CFI_SAME_VALUE; |
5536 | DirectiveKindMap[".cfi_restore" ] = DK_CFI_RESTORE; |
5537 | DirectiveKindMap[".cfi_escape" ] = DK_CFI_ESCAPE; |
5538 | DirectiveKindMap[".cfi_return_column" ] = DK_CFI_RETURN_COLUMN; |
5539 | DirectiveKindMap[".cfi_signal_frame" ] = DK_CFI_SIGNAL_FRAME; |
5540 | DirectiveKindMap[".cfi_undefined" ] = DK_CFI_UNDEFINED; |
5541 | DirectiveKindMap[".cfi_register" ] = DK_CFI_REGISTER; |
5542 | DirectiveKindMap[".cfi_window_save" ] = DK_CFI_WINDOW_SAVE; |
5543 | DirectiveKindMap[".cfi_label" ] = DK_CFI_LABEL; |
5544 | DirectiveKindMap[".cfi_b_key_frame" ] = DK_CFI_B_KEY_FRAME; |
5545 | DirectiveKindMap[".cfi_mte_tagged_frame" ] = DK_CFI_MTE_TAGGED_FRAME; |
5546 | DirectiveKindMap[".cfi_val_offset" ] = DK_CFI_VAL_OFFSET; |
5547 | DirectiveKindMap[".macros_on" ] = DK_MACROS_ON; |
5548 | DirectiveKindMap[".macros_off" ] = DK_MACROS_OFF; |
5549 | DirectiveKindMap[".macro" ] = DK_MACRO; |
5550 | DirectiveKindMap[".exitm" ] = DK_EXITM; |
5551 | DirectiveKindMap[".endm" ] = DK_ENDM; |
5552 | DirectiveKindMap[".endmacro" ] = DK_ENDMACRO; |
5553 | DirectiveKindMap[".purgem" ] = DK_PURGEM; |
5554 | DirectiveKindMap[".err" ] = DK_ERR; |
5555 | DirectiveKindMap[".error" ] = DK_ERROR; |
5556 | DirectiveKindMap[".warning" ] = DK_WARNING; |
5557 | DirectiveKindMap[".altmacro" ] = DK_ALTMACRO; |
5558 | DirectiveKindMap[".noaltmacro" ] = DK_NOALTMACRO; |
5559 | DirectiveKindMap[".reloc" ] = DK_RELOC; |
5560 | DirectiveKindMap[".dc" ] = DK_DC; |
5561 | DirectiveKindMap[".dc.a" ] = DK_DC_A; |
5562 | DirectiveKindMap[".dc.b" ] = DK_DC_B; |
5563 | DirectiveKindMap[".dc.d" ] = DK_DC_D; |
5564 | DirectiveKindMap[".dc.l" ] = DK_DC_L; |
5565 | DirectiveKindMap[".dc.s" ] = DK_DC_S; |
5566 | DirectiveKindMap[".dc.w" ] = DK_DC_W; |
5567 | DirectiveKindMap[".dc.x" ] = DK_DC_X; |
5568 | DirectiveKindMap[".dcb" ] = DK_DCB; |
5569 | DirectiveKindMap[".dcb.b" ] = DK_DCB_B; |
5570 | DirectiveKindMap[".dcb.d" ] = DK_DCB_D; |
5571 | DirectiveKindMap[".dcb.l" ] = DK_DCB_L; |
5572 | DirectiveKindMap[".dcb.s" ] = DK_DCB_S; |
5573 | DirectiveKindMap[".dcb.w" ] = DK_DCB_W; |
5574 | DirectiveKindMap[".dcb.x" ] = DK_DCB_X; |
5575 | DirectiveKindMap[".ds" ] = DK_DS; |
5576 | DirectiveKindMap[".ds.b" ] = DK_DS_B; |
5577 | DirectiveKindMap[".ds.d" ] = DK_DS_D; |
5578 | DirectiveKindMap[".ds.l" ] = DK_DS_L; |
5579 | DirectiveKindMap[".ds.p" ] = DK_DS_P; |
5580 | DirectiveKindMap[".ds.s" ] = DK_DS_S; |
5581 | DirectiveKindMap[".ds.w" ] = DK_DS_W; |
5582 | DirectiveKindMap[".ds.x" ] = DK_DS_X; |
5583 | DirectiveKindMap[".print" ] = DK_PRINT; |
5584 | DirectiveKindMap[".addrsig" ] = DK_ADDRSIG; |
5585 | DirectiveKindMap[".addrsig_sym" ] = DK_ADDRSIG_SYM; |
5586 | DirectiveKindMap[".pseudoprobe" ] = DK_PSEUDO_PROBE; |
5587 | DirectiveKindMap[".lto_discard" ] = DK_LTO_DISCARD; |
5588 | DirectiveKindMap[".lto_set_conditional" ] = DK_LTO_SET_CONDITIONAL; |
5589 | DirectiveKindMap[".memtag" ] = DK_MEMTAG; |
5590 | } |
5591 | |
5592 | MCAsmMacro *AsmParser::parseMacroLikeBody(SMLoc DirectiveLoc) { |
5593 | AsmToken EndToken, StartToken = getTok(); |
5594 | |
5595 | unsigned NestLevel = 0; |
5596 | while (true) { |
5597 | // Check whether we have reached the end of the file. |
5598 | if (getLexer().is(K: AsmToken::Eof)) { |
5599 | printError(L: DirectiveLoc, Msg: "no matching '.endr' in definition" ); |
5600 | return nullptr; |
5601 | } |
5602 | |
5603 | if (Lexer.is(K: AsmToken::Identifier)) { |
5604 | StringRef Ident = getTok().getIdentifier(); |
5605 | if (Ident == ".rep" || Ident == ".rept" || Ident == ".irp" || |
5606 | Ident == ".irpc" ) { |
5607 | ++NestLevel; |
5608 | } else if (Ident == ".endr" ) { |
5609 | if (NestLevel == 0) { |
5610 | EndToken = getTok(); |
5611 | Lex(); |
5612 | if (Lexer.is(K: AsmToken::EndOfStatement)) |
5613 | break; |
5614 | printError(L: getTok().getLoc(), Msg: "expected newline" ); |
5615 | return nullptr; |
5616 | } |
5617 | --NestLevel; |
5618 | } |
5619 | } |
5620 | |
5621 | // Otherwise, scan till the end of the statement. |
5622 | eatToEndOfStatement(); |
5623 | } |
5624 | |
5625 | const char *BodyStart = StartToken.getLoc().getPointer(); |
5626 | const char *BodyEnd = EndToken.getLoc().getPointer(); |
5627 | StringRef Body = StringRef(BodyStart, BodyEnd - BodyStart); |
5628 | |
5629 | // We Are Anonymous. |
5630 | MacroLikeBodies.emplace_back(args: StringRef(), args&: Body, args: MCAsmMacroParameters()); |
5631 | return &MacroLikeBodies.back(); |
5632 | } |
5633 | |
5634 | void AsmParser::instantiateMacroLikeBody(MCAsmMacro *M, SMLoc DirectiveLoc, |
5635 | raw_svector_ostream &OS) { |
5636 | OS << ".endr\n" ; |
5637 | |
5638 | std::unique_ptr<MemoryBuffer> Instantiation = |
5639 | MemoryBuffer::getMemBufferCopy(InputData: OS.str(), BufferName: "<instantiation>" ); |
5640 | |
5641 | // Create the macro instantiation object and add to the current macro |
5642 | // instantiation stack. |
5643 | MacroInstantiation *MI = new MacroInstantiation{ |
5644 | .InstantiationLoc: DirectiveLoc, .ExitBuffer: CurBuffer, .ExitLoc: getTok().getLoc(), .CondStackDepth: TheCondStack.size()}; |
5645 | ActiveMacros.push_back(x: MI); |
5646 | |
5647 | // Jump to the macro instantiation and prime the lexer. |
5648 | CurBuffer = SrcMgr.AddNewSourceBuffer(F: std::move(Instantiation), IncludeLoc: SMLoc()); |
5649 | Lexer.setBuffer(Buf: SrcMgr.getMemoryBuffer(i: CurBuffer)->getBuffer()); |
5650 | Lex(); |
5651 | } |
5652 | |
5653 | /// parseDirectiveRept |
5654 | /// ::= .rep | .rept count |
5655 | bool AsmParser::parseDirectiveRept(SMLoc DirectiveLoc, StringRef Dir) { |
5656 | const MCExpr *CountExpr; |
5657 | SMLoc CountLoc = getTok().getLoc(); |
5658 | if (parseExpression(Res&: CountExpr)) |
5659 | return true; |
5660 | |
5661 | int64_t Count; |
5662 | if (!CountExpr->evaluateAsAbsolute(Res&: Count, Asm: getStreamer().getAssemblerPtr())) { |
5663 | return Error(L: CountLoc, Msg: "unexpected token in '" + Dir + "' directive" ); |
5664 | } |
5665 | |
5666 | if (check(P: Count < 0, Loc: CountLoc, Msg: "Count is negative" ) || parseEOL()) |
5667 | return true; |
5668 | |
5669 | // Lex the rept definition. |
5670 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
5671 | if (!M) |
5672 | return true; |
5673 | |
5674 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
5675 | // to hold the macro body with substitutions. |
5676 | SmallString<256> Buf; |
5677 | raw_svector_ostream OS(Buf); |
5678 | while (Count--) { |
5679 | // Note that the AtPseudoVariable is disabled for instantiations of .rep(t). |
5680 | if (expandMacro(OS, Macro&: *M, Parameters: {}, A: {}, EnableAtPseudoVariable: false)) |
5681 | return true; |
5682 | } |
5683 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
5684 | |
5685 | return false; |
5686 | } |
5687 | |
5688 | /// parseDirectiveIrp |
5689 | /// ::= .irp symbol,values |
5690 | bool AsmParser::parseDirectiveIrp(SMLoc DirectiveLoc) { |
5691 | MCAsmMacroParameter Parameter; |
5692 | MCAsmMacroArguments A; |
5693 | if (check(P: parseIdentifier(Res&: Parameter.Name), |
5694 | Msg: "expected identifier in '.irp' directive" ) || |
5695 | parseComma() || parseMacroArguments(M: nullptr, A) || parseEOL()) |
5696 | return true; |
5697 | |
5698 | // Lex the irp definition. |
5699 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
5700 | if (!M) |
5701 | return true; |
5702 | |
5703 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
5704 | // to hold the macro body with substitutions. |
5705 | SmallString<256> Buf; |
5706 | raw_svector_ostream OS(Buf); |
5707 | |
5708 | for (const MCAsmMacroArgument &Arg : A) { |
5709 | // Note that the AtPseudoVariable is enabled for instantiations of .irp. |
5710 | // This is undocumented, but GAS seems to support it. |
5711 | if (expandMacro(OS, Macro&: *M, Parameters: Parameter, A: Arg, EnableAtPseudoVariable: true)) |
5712 | return true; |
5713 | } |
5714 | |
5715 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
5716 | |
5717 | return false; |
5718 | } |
5719 | |
5720 | /// parseDirectiveIrpc |
5721 | /// ::= .irpc symbol,values |
5722 | bool AsmParser::parseDirectiveIrpc(SMLoc DirectiveLoc) { |
5723 | MCAsmMacroParameter Parameter; |
5724 | MCAsmMacroArguments A; |
5725 | |
5726 | if (check(P: parseIdentifier(Res&: Parameter.Name), |
5727 | Msg: "expected identifier in '.irpc' directive" ) || |
5728 | parseComma() || parseMacroArguments(M: nullptr, A)) |
5729 | return true; |
5730 | |
5731 | if (A.size() != 1 || A.front().size() != 1) |
5732 | return TokError(Msg: "unexpected token in '.irpc' directive" ); |
5733 | if (parseEOL()) |
5734 | return true; |
5735 | |
5736 | // Lex the irpc definition. |
5737 | MCAsmMacro *M = parseMacroLikeBody(DirectiveLoc); |
5738 | if (!M) |
5739 | return true; |
5740 | |
5741 | // Macro instantiation is lexical, unfortunately. We construct a new buffer |
5742 | // to hold the macro body with substitutions. |
5743 | SmallString<256> Buf; |
5744 | raw_svector_ostream OS(Buf); |
5745 | |
5746 | StringRef Values = A[0][0].is(K: AsmToken::String) ? A[0][0].getStringContents() |
5747 | : A[0][0].getString(); |
5748 | for (std::size_t I = 0, End = Values.size(); I != End; ++I) { |
5749 | MCAsmMacroArgument Arg; |
5750 | Arg.emplace_back(args: AsmToken::Identifier, args: Values.substr(Start: I, N: 1)); |
5751 | |
5752 | // Note that the AtPseudoVariable is enabled for instantiations of .irpc. |
5753 | // This is undocumented, but GAS seems to support it. |
5754 | if (expandMacro(OS, Macro&: *M, Parameters: Parameter, A: Arg, EnableAtPseudoVariable: true)) |
5755 | return true; |
5756 | } |
5757 | |
5758 | instantiateMacroLikeBody(M, DirectiveLoc, OS); |
5759 | |
5760 | return false; |
5761 | } |
5762 | |
5763 | bool AsmParser::parseDirectiveEndr(SMLoc DirectiveLoc) { |
5764 | if (ActiveMacros.empty()) |
5765 | return TokError(Msg: "unmatched '.endr' directive" ); |
5766 | |
5767 | // The only .repl that should get here are the ones created by |
5768 | // instantiateMacroLikeBody. |
5769 | assert(getLexer().is(AsmToken::EndOfStatement)); |
5770 | |
5771 | handleMacroExit(); |
5772 | return false; |
5773 | } |
5774 | |
5775 | bool AsmParser::parseDirectiveMSEmit(SMLoc IDLoc, ParseStatementInfo &Info, |
5776 | size_t Len) { |
5777 | const MCExpr *Value; |
5778 | SMLoc ExprLoc = getLexer().getLoc(); |
5779 | if (parseExpression(Res&: Value)) |
5780 | return true; |
5781 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value); |
5782 | if (!MCE) |
5783 | return Error(L: ExprLoc, Msg: "unexpected expression in _emit" ); |
5784 | uint64_t IntValue = MCE->getValue(); |
5785 | if (!isUInt<8>(x: IntValue) && !isInt<8>(x: IntValue)) |
5786 | return Error(L: ExprLoc, Msg: "literal value out of range for directive" ); |
5787 | |
5788 | Info.AsmRewrites->emplace_back(Args: AOK_Emit, Args&: IDLoc, Args&: Len); |
5789 | return false; |
5790 | } |
5791 | |
5792 | bool AsmParser::parseDirectiveMSAlign(SMLoc IDLoc, ParseStatementInfo &Info) { |
5793 | const MCExpr *Value; |
5794 | SMLoc ExprLoc = getLexer().getLoc(); |
5795 | if (parseExpression(Res&: Value)) |
5796 | return true; |
5797 | const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Val: Value); |
5798 | if (!MCE) |
5799 | return Error(L: ExprLoc, Msg: "unexpected expression in align" ); |
5800 | uint64_t IntValue = MCE->getValue(); |
5801 | if (!isPowerOf2_64(Value: IntValue)) |
5802 | return Error(L: ExprLoc, Msg: "literal value not a power of two greater then zero" ); |
5803 | |
5804 | Info.AsmRewrites->emplace_back(Args: AOK_Align, Args&: IDLoc, Args: 5, Args: Log2_64(Value: IntValue)); |
5805 | return false; |
5806 | } |
5807 | |
5808 | bool AsmParser::parseDirectivePrint(SMLoc DirectiveLoc) { |
5809 | const AsmToken StrTok = getTok(); |
5810 | Lex(); |
5811 | if (StrTok.isNot(K: AsmToken::String) || StrTok.getString().front() != '"') |
5812 | return Error(L: DirectiveLoc, Msg: "expected double quoted string after .print" ); |
5813 | if (parseEOL()) |
5814 | return true; |
5815 | llvm::outs() << StrTok.getStringContents() << '\n'; |
5816 | return false; |
5817 | } |
5818 | |
5819 | bool AsmParser::parseDirectiveAddrsig() { |
5820 | if (parseEOL()) |
5821 | return true; |
5822 | getStreamer().emitAddrsig(); |
5823 | return false; |
5824 | } |
5825 | |
5826 | bool AsmParser::parseDirectiveAddrsigSym() { |
5827 | StringRef Name; |
5828 | if (check(P: parseIdentifier(Res&: Name), Msg: "expected identifier" ) || parseEOL()) |
5829 | return true; |
5830 | MCSymbol *Sym = getContext().getOrCreateSymbol(Name); |
5831 | getStreamer().emitAddrsigSym(Sym); |
5832 | return false; |
5833 | } |
5834 | |
5835 | bool AsmParser::parseDirectivePseudoProbe() { |
5836 | int64_t Guid; |
5837 | int64_t Index; |
5838 | int64_t Type; |
5839 | int64_t Attr; |
5840 | int64_t Discriminator = 0; |
5841 | if (parseIntToken(V&: Guid)) |
5842 | return true; |
5843 | if (parseIntToken(V&: Index)) |
5844 | return true; |
5845 | if (parseIntToken(V&: Type)) |
5846 | return true; |
5847 | if (parseIntToken(V&: Attr)) |
5848 | return true; |
5849 | if (hasDiscriminator(Flags: Attr) && parseIntToken(V&: Discriminator)) |
5850 | return true; |
5851 | |
5852 | // Parse inline stack like @ GUID:11:12 @ GUID:1:11 @ GUID:3:21 |
5853 | MCPseudoProbeInlineStack InlineStack; |
5854 | |
5855 | while (getLexer().is(K: AsmToken::At)) { |
5856 | // eat @ |
5857 | Lex(); |
5858 | |
5859 | int64_t CallerGuid = 0; |
5860 | if (getLexer().is(K: AsmToken::Integer)) { |
5861 | CallerGuid = getTok().getIntVal(); |
5862 | Lex(); |
5863 | } |
5864 | |
5865 | // eat colon |
5866 | if (getLexer().is(K: AsmToken::Colon)) |
5867 | Lex(); |
5868 | |
5869 | int64_t CallerProbeId = 0; |
5870 | if (getLexer().is(K: AsmToken::Integer)) { |
5871 | CallerProbeId = getTok().getIntVal(); |
5872 | Lex(); |
5873 | } |
5874 | |
5875 | InlineSite Site(CallerGuid, CallerProbeId); |
5876 | InlineStack.push_back(Elt: Site); |
5877 | } |
5878 | |
5879 | // Parse function entry name |
5880 | StringRef FnName; |
5881 | if (parseIdentifier(Res&: FnName)) |
5882 | return Error(L: getLexer().getLoc(), Msg: "expected identifier" ); |
5883 | MCSymbol *FnSym = getContext().lookupSymbol(Name: FnName); |
5884 | |
5885 | if (parseEOL()) |
5886 | return true; |
5887 | |
5888 | getStreamer().emitPseudoProbe(Guid, Index, Type, Attr, Discriminator, |
5889 | InlineStack, FnSym); |
5890 | return false; |
5891 | } |
5892 | |
5893 | /// parseDirectiveLTODiscard |
5894 | /// ::= ".lto_discard" [ identifier ( , identifier )* ] |
5895 | /// The LTO library emits this directive to discard non-prevailing symbols. |
5896 | /// We ignore symbol assignments and attribute changes for the specified |
5897 | /// symbols. |
5898 | bool AsmParser::parseDirectiveLTODiscard() { |
5899 | auto ParseOp = [&]() -> bool { |
5900 | StringRef Name; |
5901 | SMLoc Loc = getTok().getLoc(); |
5902 | if (parseIdentifier(Res&: Name)) |
5903 | return Error(L: Loc, Msg: "expected identifier" ); |
5904 | LTODiscardSymbols.insert(V: Name); |
5905 | return false; |
5906 | }; |
5907 | |
5908 | LTODiscardSymbols.clear(); |
5909 | return parseMany(parseOne: ParseOp); |
5910 | } |
5911 | |
5912 | // We are comparing pointers, but the pointers are relative to a single string. |
5913 | // Thus, this should always be deterministic. |
5914 | static int rewritesSort(const AsmRewrite *AsmRewriteA, |
5915 | const AsmRewrite *AsmRewriteB) { |
5916 | if (AsmRewriteA->Loc.getPointer() < AsmRewriteB->Loc.getPointer()) |
5917 | return -1; |
5918 | if (AsmRewriteB->Loc.getPointer() < AsmRewriteA->Loc.getPointer()) |
5919 | return 1; |
5920 | |
5921 | // It's possible to have a SizeDirective, Imm/ImmPrefix and an Input/Output |
5922 | // rewrite to the same location. Make sure the SizeDirective rewrite is |
5923 | // performed first, then the Imm/ImmPrefix and finally the Input/Output. This |
5924 | // ensures the sort algorithm is stable. |
5925 | if (AsmRewritePrecedence[AsmRewriteA->Kind] > |
5926 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
5927 | return -1; |
5928 | |
5929 | if (AsmRewritePrecedence[AsmRewriteA->Kind] < |
5930 | AsmRewritePrecedence[AsmRewriteB->Kind]) |
5931 | return 1; |
5932 | llvm_unreachable("Unstable rewrite sort." ); |
5933 | } |
5934 | |
5935 | bool AsmParser::parseMSInlineAsm( |
5936 | std::string &AsmString, unsigned &NumOutputs, unsigned &NumInputs, |
5937 | SmallVectorImpl<std::pair<void *, bool>> &OpDecls, |
5938 | SmallVectorImpl<std::string> &Constraints, |
5939 | SmallVectorImpl<std::string> &Clobbers, const MCInstrInfo *MII, |
5940 | MCInstPrinter *IP, MCAsmParserSemaCallback &SI) { |
5941 | SmallVector<void *, 4> InputDecls; |
5942 | SmallVector<void *, 4> OutputDecls; |
5943 | SmallVector<bool, 4> InputDeclsAddressOf; |
5944 | SmallVector<bool, 4> OutputDeclsAddressOf; |
5945 | SmallVector<std::string, 4> InputConstraints; |
5946 | SmallVector<std::string, 4> OutputConstraints; |
5947 | SmallVector<MCRegister, 4> ClobberRegs; |
5948 | |
5949 | SmallVector<AsmRewrite, 4> AsmStrRewrites; |
5950 | |
5951 | // Prime the lexer. |
5952 | Lex(); |
5953 | |
5954 | // While we have input, parse each statement. |
5955 | unsigned InputIdx = 0; |
5956 | unsigned OutputIdx = 0; |
5957 | while (getLexer().isNot(K: AsmToken::Eof)) { |
5958 | // Parse curly braces marking block start/end |
5959 | if (parseCurlyBlockScope(AsmStrRewrites)) |
5960 | continue; |
5961 | |
5962 | ParseStatementInfo Info(&AsmStrRewrites); |
5963 | bool StatementErr = parseStatement(Info, SI: &SI); |
5964 | |
5965 | if (StatementErr || Info.ParseError) { |
5966 | // Emit pending errors if any exist. |
5967 | printPendingErrors(); |
5968 | return true; |
5969 | } |
5970 | |
5971 | // No pending error should exist here. |
5972 | assert(!hasPendingError() && "unexpected error from parseStatement" ); |
5973 | |
5974 | if (Info.Opcode == ~0U) |
5975 | continue; |
5976 | |
5977 | const MCInstrDesc &Desc = MII->get(Opcode: Info.Opcode); |
5978 | |
5979 | // Build the list of clobbers, outputs and inputs. |
5980 | for (unsigned i = 1, e = Info.ParsedOperands.size(); i != e; ++i) { |
5981 | MCParsedAsmOperand &Operand = *Info.ParsedOperands[i]; |
5982 | |
5983 | // Register operand. |
5984 | if (Operand.isReg() && !Operand.needAddressOf() && |
5985 | !getTargetParser().omitRegisterFromClobberLists(Reg: Operand.getReg())) { |
5986 | unsigned NumDefs = Desc.getNumDefs(); |
5987 | // Clobber. |
5988 | if (NumDefs && Operand.getMCOperandNum() < NumDefs) |
5989 | ClobberRegs.push_back(Elt: Operand.getReg()); |
5990 | continue; |
5991 | } |
5992 | |
5993 | // Expr/Input or Output. |
5994 | StringRef SymName = Operand.getSymName(); |
5995 | if (SymName.empty()) |
5996 | continue; |
5997 | |
5998 | void *OpDecl = Operand.getOpDecl(); |
5999 | if (!OpDecl) |
6000 | continue; |
6001 | |
6002 | StringRef Constraint = Operand.getConstraint(); |
6003 | if (Operand.isImm()) { |
6004 | // Offset as immediate |
6005 | if (Operand.isOffsetOfLocal()) |
6006 | Constraint = "r" ; |
6007 | else |
6008 | Constraint = "i" ; |
6009 | } |
6010 | |
6011 | bool isOutput = (i == 1) && Desc.mayStore(); |
6012 | bool Restricted = Operand.isMemUseUpRegs(); |
6013 | SMLoc Start = SMLoc::getFromPointer(Ptr: SymName.data()); |
6014 | if (isOutput) { |
6015 | ++InputIdx; |
6016 | OutputDecls.push_back(Elt: OpDecl); |
6017 | OutputDeclsAddressOf.push_back(Elt: Operand.needAddressOf()); |
6018 | OutputConstraints.push_back(Elt: ("=" + Constraint).str()); |
6019 | AsmStrRewrites.emplace_back(Args: AOK_Output, Args&: Start, Args: SymName.size(), Args: 0, |
6020 | Args&: Restricted); |
6021 | } else { |
6022 | InputDecls.push_back(Elt: OpDecl); |
6023 | InputDeclsAddressOf.push_back(Elt: Operand.needAddressOf()); |
6024 | InputConstraints.push_back(Elt: Constraint.str()); |
6025 | if (Desc.operands()[i - 1].isBranchTarget()) |
6026 | AsmStrRewrites.emplace_back(Args: AOK_CallInput, Args&: Start, Args: SymName.size(), Args: 0, |
6027 | Args&: Restricted); |
6028 | else |
6029 | AsmStrRewrites.emplace_back(Args: AOK_Input, Args&: Start, Args: SymName.size(), Args: 0, |
6030 | Args&: Restricted); |
6031 | } |
6032 | } |
6033 | |
6034 | // Consider implicit defs to be clobbers. Think of cpuid and push. |
6035 | llvm::append_range(C&: ClobberRegs, R: Desc.implicit_defs()); |
6036 | } |
6037 | |
6038 | // Set the number of Outputs and Inputs. |
6039 | NumOutputs = OutputDecls.size(); |
6040 | NumInputs = InputDecls.size(); |
6041 | |
6042 | // Set the unique clobbers. |
6043 | array_pod_sort(Start: ClobberRegs.begin(), End: ClobberRegs.end()); |
6044 | ClobberRegs.erase(CS: llvm::unique(R&: ClobberRegs), CE: ClobberRegs.end()); |
6045 | Clobbers.assign(NumElts: ClobberRegs.size(), Elt: std::string()); |
6046 | for (unsigned I = 0, E = ClobberRegs.size(); I != E; ++I) { |
6047 | raw_string_ostream OS(Clobbers[I]); |
6048 | IP->printRegName(OS, Reg: ClobberRegs[I]); |
6049 | } |
6050 | |
6051 | // Merge the various outputs and inputs. Output are expected first. |
6052 | if (NumOutputs || NumInputs) { |
6053 | unsigned NumExprs = NumOutputs + NumInputs; |
6054 | OpDecls.resize(N: NumExprs); |
6055 | Constraints.resize(N: NumExprs); |
6056 | for (unsigned i = 0; i < NumOutputs; ++i) { |
6057 | OpDecls[i] = std::make_pair(x&: OutputDecls[i], y&: OutputDeclsAddressOf[i]); |
6058 | Constraints[i] = OutputConstraints[i]; |
6059 | } |
6060 | for (unsigned i = 0, j = NumOutputs; i < NumInputs; ++i, ++j) { |
6061 | OpDecls[j] = std::make_pair(x&: InputDecls[i], y&: InputDeclsAddressOf[i]); |
6062 | Constraints[j] = InputConstraints[i]; |
6063 | } |
6064 | } |
6065 | |
6066 | // Build the IR assembly string. |
6067 | std::string AsmStringIR; |
6068 | raw_string_ostream OS(AsmStringIR); |
6069 | StringRef ASMString = |
6070 | SrcMgr.getMemoryBuffer(i: SrcMgr.getMainFileID())->getBuffer(); |
6071 | const char *AsmStart = ASMString.begin(); |
6072 | const char *AsmEnd = ASMString.end(); |
6073 | array_pod_sort(Start: AsmStrRewrites.begin(), End: AsmStrRewrites.end(), Compare: rewritesSort); |
6074 | for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) { |
6075 | const AsmRewrite &AR = *I; |
6076 | // Check if this has already been covered by another rewrite... |
6077 | if (AR.Done) |
6078 | continue; |
6079 | AsmRewriteKind Kind = AR.Kind; |
6080 | |
6081 | const char *Loc = AR.Loc.getPointer(); |
6082 | assert(Loc >= AsmStart && "Expected Loc to be at or after Start!" ); |
6083 | |
6084 | // Emit everything up to the immediate/expression. |
6085 | if (unsigned Len = Loc - AsmStart) |
6086 | OS << StringRef(AsmStart, Len); |
6087 | |
6088 | // Skip the original expression. |
6089 | if (Kind == AOK_Skip) { |
6090 | AsmStart = Loc + AR.Len; |
6091 | continue; |
6092 | } |
6093 | |
6094 | unsigned AdditionalSkip = 0; |
6095 | // Rewrite expressions in $N notation. |
6096 | switch (Kind) { |
6097 | default: |
6098 | break; |
6099 | case AOK_IntelExpr: |
6100 | assert(AR.IntelExp.isValid() && "cannot write invalid intel expression" ); |
6101 | if (AR.IntelExp.NeedBracs) |
6102 | OS << "[" ; |
6103 | if (AR.IntelExp.hasBaseReg()) |
6104 | OS << AR.IntelExp.BaseReg; |
6105 | if (AR.IntelExp.hasIndexReg()) |
6106 | OS << (AR.IntelExp.hasBaseReg() ? " + " : "" ) |
6107 | << AR.IntelExp.IndexReg; |
6108 | if (AR.IntelExp.Scale > 1) |
6109 | OS << " * $$" << AR.IntelExp.Scale; |
6110 | if (AR.IntelExp.hasOffset()) { |
6111 | if (AR.IntelExp.hasRegs()) |
6112 | OS << " + " ; |
6113 | // Fuse this rewrite with a rewrite of the offset name, if present. |
6114 | StringRef OffsetName = AR.IntelExp.OffsetName; |
6115 | SMLoc OffsetLoc = SMLoc::getFromPointer(Ptr: AR.IntelExp.OffsetName.data()); |
6116 | size_t OffsetLen = OffsetName.size(); |
6117 | auto rewrite_it = std::find_if( |
6118 | first: I, last: AsmStrRewrites.end(), pred: [&](const AsmRewrite &FusingAR) { |
6119 | return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen && |
6120 | (FusingAR.Kind == AOK_Input || |
6121 | FusingAR.Kind == AOK_CallInput); |
6122 | }); |
6123 | if (rewrite_it == AsmStrRewrites.end()) { |
6124 | OS << "offset " << OffsetName; |
6125 | } else if (rewrite_it->Kind == AOK_CallInput) { |
6126 | OS << "${" << InputIdx++ << ":P}" ; |
6127 | rewrite_it->Done = true; |
6128 | } else { |
6129 | OS << '$' << InputIdx++; |
6130 | rewrite_it->Done = true; |
6131 | } |
6132 | } |
6133 | if (AR.IntelExp.Imm || AR.IntelExp.emitImm()) |
6134 | OS << (AR.IntelExp.emitImm() ? "$$" : " + $$" ) << AR.IntelExp.Imm; |
6135 | if (AR.IntelExp.NeedBracs) |
6136 | OS << "]" ; |
6137 | break; |
6138 | case AOK_Label: |
6139 | OS << Ctx.getAsmInfo()->getPrivateLabelPrefix() << AR.Label; |
6140 | break; |
6141 | case AOK_Input: |
6142 | if (AR.IntelExpRestricted) |
6143 | OS << "${" << InputIdx++ << ":P}" ; |
6144 | else |
6145 | OS << '$' << InputIdx++; |
6146 | break; |
6147 | case AOK_CallInput: |
6148 | OS << "${" << InputIdx++ << ":P}" ; |
6149 | break; |
6150 | case AOK_Output: |
6151 | if (AR.IntelExpRestricted) |
6152 | OS << "${" << OutputIdx++ << ":P}" ; |
6153 | else |
6154 | OS << '$' << OutputIdx++; |
6155 | break; |
6156 | case AOK_SizeDirective: |
6157 | switch (AR.Val) { |
6158 | default: break; |
6159 | case 8: OS << "byte ptr " ; break; |
6160 | case 16: OS << "word ptr " ; break; |
6161 | case 32: OS << "dword ptr " ; break; |
6162 | case 64: OS << "qword ptr " ; break; |
6163 | case 80: OS << "xword ptr " ; break; |
6164 | case 128: OS << "xmmword ptr " ; break; |
6165 | case 256: OS << "ymmword ptr " ; break; |
6166 | } |
6167 | break; |
6168 | case AOK_Emit: |
6169 | OS << ".byte" ; |
6170 | break; |
6171 | case AOK_Align: { |
6172 | // MS alignment directives are measured in bytes. If the native assembler |
6173 | // measures alignment in bytes, we can pass it straight through. |
6174 | OS << ".align" ; |
6175 | if (getContext().getAsmInfo()->getAlignmentIsInBytes()) |
6176 | break; |
6177 | |
6178 | // Alignment is in log2 form, so print that instead and skip the original |
6179 | // immediate. |
6180 | unsigned Val = AR.Val; |
6181 | OS << ' ' << Val; |
6182 | assert(Val < 10 && "Expected alignment less then 2^10." ); |
6183 | AdditionalSkip = (Val < 4) ? 2 : Val < 7 ? 3 : 4; |
6184 | break; |
6185 | } |
6186 | case AOK_EVEN: |
6187 | OS << ".even" ; |
6188 | break; |
6189 | case AOK_EndOfStatement: |
6190 | OS << "\n\t" ; |
6191 | break; |
6192 | } |
6193 | |
6194 | // Skip the original expression. |
6195 | AsmStart = Loc + AR.Len + AdditionalSkip; |
6196 | } |
6197 | |
6198 | // Emit the remainder of the asm string. |
6199 | if (AsmStart != AsmEnd) |
6200 | OS << StringRef(AsmStart, AsmEnd - AsmStart); |
6201 | |
6202 | AsmString = std::move(AsmStringIR); |
6203 | return false; |
6204 | } |
6205 | |
6206 | bool HLASMAsmParser::parseAsHLASMLabel(ParseStatementInfo &Info, |
6207 | MCAsmParserSemaCallback *SI) { |
6208 | AsmToken LabelTok = getTok(); |
6209 | SMLoc LabelLoc = LabelTok.getLoc(); |
6210 | StringRef LabelVal; |
6211 | |
6212 | if (parseIdentifier(Res&: LabelVal)) |
6213 | return Error(L: LabelLoc, Msg: "The HLASM Label has to be an Identifier" ); |
6214 | |
6215 | // We have validated whether the token is an Identifier. |
6216 | // Now we have to validate whether the token is a |
6217 | // valid HLASM Label. |
6218 | if (!getTargetParser().isLabel(Token&: LabelTok) || checkForValidSection()) |
6219 | return true; |
6220 | |
6221 | // Lex leading spaces to get to the next operand. |
6222 | lexLeadingSpaces(); |
6223 | |
6224 | // We shouldn't emit the label if there is nothing else after the label. |
6225 | // i.e asm("<token>\n") |
6226 | if (getTok().is(K: AsmToken::EndOfStatement)) |
6227 | return Error(L: LabelLoc, |
6228 | Msg: "Cannot have just a label for an HLASM inline asm statement" ); |
6229 | |
6230 | MCSymbol *Sym = getContext().getOrCreateSymbol( |
6231 | Name: getContext().getAsmInfo()->isHLASM() ? LabelVal.upper() : LabelVal); |
6232 | |
6233 | // Emit the label. |
6234 | Out.emitLabel(Symbol: Sym, Loc: LabelLoc); |
6235 | |
6236 | // If we are generating dwarf for assembly source files then gather the |
6237 | // info to make a dwarf label entry for this label if needed. |
6238 | if (enabledGenDwarfForAssembly()) |
6239 | MCGenDwarfLabelEntry::Make(Symbol: Sym, MCOS: &getStreamer(), SrcMgr&: getSourceManager(), |
6240 | Loc&: LabelLoc); |
6241 | |
6242 | return false; |
6243 | } |
6244 | |
6245 | bool HLASMAsmParser::parseAsMachineInstruction(ParseStatementInfo &Info, |
6246 | MCAsmParserSemaCallback *SI) { |
6247 | AsmToken OperationEntryTok = Lexer.getTok(); |
6248 | SMLoc OperationEntryLoc = OperationEntryTok.getLoc(); |
6249 | StringRef OperationEntryVal; |
6250 | |
6251 | // Attempt to parse the first token as an Identifier |
6252 | if (parseIdentifier(Res&: OperationEntryVal)) |
6253 | return Error(L: OperationEntryLoc, Msg: "unexpected token at start of statement" ); |
6254 | |
6255 | // Once we've parsed the operation entry successfully, lex |
6256 | // any spaces to get to the OperandEntries. |
6257 | lexLeadingSpaces(); |
6258 | |
6259 | return parseAndMatchAndEmitTargetInstruction( |
6260 | Info, IDVal: OperationEntryVal, ID: OperationEntryTok, IDLoc: OperationEntryLoc); |
6261 | } |
6262 | |
6263 | bool HLASMAsmParser::parseStatement(ParseStatementInfo &Info, |
6264 | MCAsmParserSemaCallback *SI) { |
6265 | assert(!hasPendingError() && "parseStatement started with pending error" ); |
6266 | |
6267 | // Should the first token be interpreted as a HLASM Label. |
6268 | bool ShouldParseAsHLASMLabel = false; |
6269 | |
6270 | // If a Name Entry exists, it should occur at the very |
6271 | // start of the string. In this case, we should parse the |
6272 | // first non-space token as a Label. |
6273 | // If the Name entry is missing (i.e. there's some other |
6274 | // token), then we attempt to parse the first non-space |
6275 | // token as a Machine Instruction. |
6276 | if (getTok().isNot(K: AsmToken::Space)) |
6277 | ShouldParseAsHLASMLabel = true; |
6278 | |
6279 | // If we have an EndOfStatement (which includes the target's comment |
6280 | // string) we can appropriately lex it early on) |
6281 | if (Lexer.is(K: AsmToken::EndOfStatement)) { |
6282 | // if this is a line comment we can drop it safely |
6283 | if (getTok().getString().empty() || getTok().getString().front() == '\r' || |
6284 | getTok().getString().front() == '\n') |
6285 | Out.addBlankLine(); |
6286 | Lex(); |
6287 | return false; |
6288 | } |
6289 | |
6290 | // We have established how to parse the inline asm statement. |
6291 | // Now we can safely lex any leading spaces to get to the |
6292 | // first token. |
6293 | lexLeadingSpaces(); |
6294 | |
6295 | // If we see a new line or carriage return as the first operand, |
6296 | // after lexing leading spaces, emit the new line and lex the |
6297 | // EndOfStatement token. |
6298 | if (Lexer.is(K: AsmToken::EndOfStatement)) { |
6299 | if (getTok().getString().front() == '\n' || |
6300 | getTok().getString().front() == '\r') { |
6301 | Out.addBlankLine(); |
6302 | Lex(); |
6303 | return false; |
6304 | } |
6305 | } |
6306 | |
6307 | // Handle the label first if we have to before processing the rest |
6308 | // of the tokens as a machine instruction. |
6309 | if (ShouldParseAsHLASMLabel) { |
6310 | // If there were any errors while handling and emitting the label, |
6311 | // early return. |
6312 | if (parseAsHLASMLabel(Info, SI)) { |
6313 | // If we know we've failed in parsing, simply eat until end of the |
6314 | // statement. This ensures that we don't process any other statements. |
6315 | eatToEndOfStatement(); |
6316 | return true; |
6317 | } |
6318 | } |
6319 | |
6320 | return parseAsMachineInstruction(Info, SI); |
6321 | } |
6322 | |
6323 | namespace llvm { |
6324 | namespace MCParserUtils { |
6325 | |
6326 | bool parseAssignmentExpression(StringRef Name, bool allow_redef, |
6327 | MCAsmParser &Parser, MCSymbol *&Sym, |
6328 | const MCExpr *&Value) { |
6329 | |
6330 | // FIXME: Use better location, we should use proper tokens. |
6331 | SMLoc EqualLoc = Parser.getTok().getLoc(); |
6332 | if (Parser.parseExpression(Res&: Value)) |
6333 | return Parser.TokError(Msg: "missing expression" ); |
6334 | if (Parser.parseEOL()) |
6335 | return true; |
6336 | // Relocation specifiers are not permitted. For now, handle just |
6337 | // MCSymbolRefExpr. |
6338 | if (auto *S = dyn_cast<MCSymbolRefExpr>(Val: Value); S && S->getSpecifier()) |
6339 | return Parser.Error( |
6340 | L: EqualLoc, Msg: "relocation specifier not permitted in symbol equating" ); |
6341 | |
6342 | // Validate that the LHS is allowed to be a variable (either it has not been |
6343 | // used as a symbol, or it is an absolute symbol). |
6344 | Sym = Parser.getContext().lookupSymbol(Name); |
6345 | if (Sym) { |
6346 | if (!Sym->isUnset() && (!allow_redef || !Sym->isRedefinable())) |
6347 | return Parser.Error(L: EqualLoc, Msg: "redefinition of '" + Name + "'" ); |
6348 | // If the symbol is redefinable, clone it and update the symbol table |
6349 | // to the new symbol. Existing references to the original symbol remain |
6350 | // unchanged. |
6351 | if (Sym->isRedefinable()) |
6352 | Sym = Parser.getContext().cloneSymbol(Sym&: *Sym); |
6353 | } else if (Name == "." ) { |
6354 | Parser.getStreamer().emitValueToOffset(Offset: Value, Value: 0, Loc: EqualLoc); |
6355 | return false; |
6356 | } else |
6357 | Sym = Parser.getContext().getOrCreateSymbol(Name); |
6358 | |
6359 | Sym->setRedefinable(allow_redef); |
6360 | |
6361 | return false; |
6362 | } |
6363 | |
6364 | } // end namespace MCParserUtils |
6365 | } // end namespace llvm |
6366 | |
6367 | /// Create an MCAsmParser instance. |
6368 | MCAsmParser *llvm::createMCAsmParser(SourceMgr &SM, MCContext &C, |
6369 | MCStreamer &Out, const MCAsmInfo &MAI, |
6370 | unsigned CB) { |
6371 | if (C.getTargetTriple().isSystemZ() && C.getTargetTriple().isOSzOS()) |
6372 | return new HLASMAsmParser(SM, C, Out, MAI, CB); |
6373 | |
6374 | return new AsmParser(SM, C, Out, MAI, CB); |
6375 | } |
6376 | |