forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasmParser.cpp
7736 lines (6798 loc) · 257 KB
/
MasmParser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This class implements the parser for assembly files.
//
//===----------------------------------------------------------------------===//
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/None.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/DebugInfo/CodeView/SymbolRecord.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCCodeView.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDirectives.h"
#include "llvm/MC/MCDwarf.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrDesc.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCParser/AsmCond.h"
#include "llvm/MC/MCParser/AsmLexer.h"
#include "llvm/MC/MCParser/MCAsmLexer.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
#include "llvm/MC/MCParser/MCAsmParserUtils.h"
#include "llvm/MC/MCParser/MCParsedAsmOperand.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SMLoc.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cstddef>
#include <cstdint>
#include <ctime>
#include <deque>
#include <memory>
#include <sstream>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
using namespace llvm;
extern cl::opt<unsigned> AsmMacroMaxNestingDepth;
namespace {
/// Helper types for tracking macro definitions.
typedef std::vector<AsmToken> MCAsmMacroArgument;
typedef std::vector<MCAsmMacroArgument> MCAsmMacroArguments;
/// Helper class for storing information about an active macro instantiation.
struct MacroInstantiation {
/// The location of the instantiation.
SMLoc InstantiationLoc;
/// The buffer where parsing should resume upon instantiation completion.
unsigned ExitBuffer;
/// The location where parsing should resume upon instantiation completion.
SMLoc ExitLoc;
/// The depth of TheCondStack at the start of the instantiation.
size_t CondStackDepth;
};
struct ParseStatementInfo {
/// The parsed operands from the last parsed statement.
SmallVector<std::unique_ptr<MCParsedAsmOperand>, 8> ParsedOperands;
/// The opcode from the last parsed instruction.
unsigned Opcode = ~0U;
/// Was there an error parsing the inline assembly?
bool ParseError = false;
/// The value associated with a macro exit.
Optional<std::string> ExitValue;
SmallVectorImpl<AsmRewrite> *AsmRewrites = nullptr;
ParseStatementInfo() = delete;
ParseStatementInfo(SmallVectorImpl<AsmRewrite> *rewrites)
: AsmRewrites(rewrites) {}
};
enum FieldType {
FT_INTEGRAL, // Initializer: integer expression, stored as an MCExpr.
FT_REAL, // Initializer: real number, stored as an APInt.
FT_STRUCT // Initializer: struct initializer, stored recursively.
};
struct FieldInfo;
struct StructInfo {
StringRef Name;
bool IsUnion = false;
bool Initializable = true;
unsigned Alignment = 0;
unsigned AlignmentSize = 0;
unsigned NextOffset = 0;
unsigned Size = 0;
std::vector<FieldInfo> Fields;
StringMap<size_t> FieldsByName;
FieldInfo &addField(StringRef FieldName, FieldType FT,
unsigned FieldAlignmentSize);
StructInfo() = default;
StructInfo(StringRef StructName, bool Union, unsigned AlignmentValue)
: Name(StructName), IsUnion(Union), Alignment(AlignmentValue) {}
};
// FIXME: This should probably use a class hierarchy, raw pointers between the
// objects, and dynamic type resolution instead of a union. On the other hand,
// ownership then becomes much more complicated; the obvious thing would be to
// use BumpPtrAllocator, but the lack of a destructor makes that messy.
struct StructInitializer;
struct IntFieldInfo {
SmallVector<const MCExpr *, 1> Values;
IntFieldInfo() = default;
IntFieldInfo(const SmallVector<const MCExpr *, 1> &V) { Values = V; }
IntFieldInfo(SmallVector<const MCExpr *, 1> &&V) { Values = V; }
};
struct RealFieldInfo {
SmallVector<APInt, 1> AsIntValues;
RealFieldInfo() = default;
RealFieldInfo(const SmallVector<APInt, 1> &V) { AsIntValues = V; }
RealFieldInfo(SmallVector<APInt, 1> &&V) { AsIntValues = V; }
};
struct StructFieldInfo {
std::vector<StructInitializer> Initializers;
StructInfo Structure;
StructFieldInfo() = default;
StructFieldInfo(const std::vector<StructInitializer> &V, StructInfo S) {
Initializers = V;
Structure = S;
}
StructFieldInfo(std::vector<StructInitializer> &&V, StructInfo S) {
Initializers = V;
Structure = S;
}
};
class FieldInitializer {
public:
FieldType FT;
union {
IntFieldInfo IntInfo;
RealFieldInfo RealInfo;
StructFieldInfo StructInfo;
};
~FieldInitializer() {
switch (FT) {
case FT_INTEGRAL:
IntInfo.~IntFieldInfo();
break;
case FT_REAL:
RealInfo.~RealFieldInfo();
break;
case FT_STRUCT:
StructInfo.~StructFieldInfo();
break;
}
}
FieldInitializer(FieldType FT) : FT(FT) {
switch (FT) {
case FT_INTEGRAL:
new (&IntInfo) IntFieldInfo();
break;
case FT_REAL:
new (&RealInfo) RealFieldInfo();
break;
case FT_STRUCT:
new (&StructInfo) StructFieldInfo();
break;
}
}
FieldInitializer(SmallVector<const MCExpr *, 1> &&Values) : FT(FT_INTEGRAL) {
new (&IntInfo) IntFieldInfo(Values);
}
FieldInitializer(SmallVector<APInt, 1> &&AsIntValues) : FT(FT_REAL) {
new (&RealInfo) RealFieldInfo(AsIntValues);
}
FieldInitializer(std::vector<StructInitializer> &&Initializers,
struct StructInfo Structure)
: FT(FT_STRUCT) {
new (&StructInfo) StructFieldInfo(Initializers, Structure);
}
FieldInitializer(const FieldInitializer &Initializer) : FT(Initializer.FT) {
switch (FT) {
case FT_INTEGRAL:
new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
break;
case FT_REAL:
new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
break;
case FT_STRUCT:
new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
break;
}
}
FieldInitializer(FieldInitializer &&Initializer) : FT(Initializer.FT) {
switch (FT) {
case FT_INTEGRAL:
new (&IntInfo) IntFieldInfo(Initializer.IntInfo);
break;
case FT_REAL:
new (&RealInfo) RealFieldInfo(Initializer.RealInfo);
break;
case FT_STRUCT:
new (&StructInfo) StructFieldInfo(Initializer.StructInfo);
break;
}
}
FieldInitializer &operator=(const FieldInitializer &Initializer) {
if (FT != Initializer.FT) {
switch (FT) {
case FT_INTEGRAL:
IntInfo.~IntFieldInfo();
break;
case FT_REAL:
RealInfo.~RealFieldInfo();
break;
case FT_STRUCT:
StructInfo.~StructFieldInfo();
break;
}
}
FT = Initializer.FT;
switch (FT) {
case FT_INTEGRAL:
IntInfo = Initializer.IntInfo;
break;
case FT_REAL:
RealInfo = Initializer.RealInfo;
break;
case FT_STRUCT:
StructInfo = Initializer.StructInfo;
break;
}
return *this;
}
FieldInitializer &operator=(FieldInitializer &&Initializer) {
if (FT != Initializer.FT) {
switch (FT) {
case FT_INTEGRAL:
IntInfo.~IntFieldInfo();
break;
case FT_REAL:
RealInfo.~RealFieldInfo();
break;
case FT_STRUCT:
StructInfo.~StructFieldInfo();
break;
}
}
FT = Initializer.FT;
switch (FT) {
case FT_INTEGRAL:
IntInfo = Initializer.IntInfo;
break;
case FT_REAL:
RealInfo = Initializer.RealInfo;
break;
case FT_STRUCT:
StructInfo = Initializer.StructInfo;
break;
}
return *this;
}
};
struct StructInitializer {
std::vector<FieldInitializer> FieldInitializers;
};
struct FieldInfo {
// Offset of the field within the containing STRUCT.
unsigned Offset = 0;
// Total size of the field (= LengthOf * Type).
unsigned SizeOf = 0;
// Number of elements in the field (1 if scalar, >1 if an array).
unsigned LengthOf = 0;
// Size of a single entry in this field, in bytes ("type" in MASM standards).
unsigned Type = 0;
FieldInitializer Contents;
FieldInfo(FieldType FT) : Contents(FT) {}
};
FieldInfo &StructInfo::addField(StringRef FieldName, FieldType FT,
unsigned FieldAlignmentSize) {
if (!FieldName.empty())
FieldsByName[FieldName.lower()] = Fields.size();
Fields.emplace_back(FT);
FieldInfo &Field = Fields.back();
Field.Offset =
llvm::alignTo(NextOffset, std::min(Alignment, FieldAlignmentSize));
if (!IsUnion) {
NextOffset = std::max(NextOffset, Field.Offset);
}
AlignmentSize = std::max(AlignmentSize, FieldAlignmentSize);
return Field;
}
/// The concrete assembly parser instance.
// Note that this is a full MCAsmParser, not an MCAsmParserExtension!
// It's a peer of AsmParser, not of COFFAsmParser, WasmAsmParser, etc.
class MasmParser : public MCAsmParser {
private:
AsmLexer Lexer;
MCContext &Ctx;
MCStreamer &Out;
const MCAsmInfo &MAI;
SourceMgr &SrcMgr;
SourceMgr::DiagHandlerTy SavedDiagHandler;
void *SavedDiagContext;
std::unique_ptr<MCAsmParserExtension> PlatformParser;
/// This is the current buffer index we're lexing from as managed by the
/// SourceMgr object.
unsigned CurBuffer;
/// time of assembly
struct tm TM;
BitVector EndStatementAtEOFStack;
AsmCond TheCondState;
std::vector<AsmCond> TheCondStack;
/// maps directive names to handler methods in parser
/// extensions. Extensions register themselves in this map by calling
/// addDirectiveHandler.
StringMap<ExtensionDirectiveHandler> ExtensionDirectiveMap;
/// maps assembly-time variable names to variables.
struct Variable {
enum RedefinableKind { NOT_REDEFINABLE, WARN_ON_REDEFINITION, REDEFINABLE };
StringRef Name;
RedefinableKind Redefinable = REDEFINABLE;
bool IsText = false;
std::string TextValue;
};
StringMap<Variable> Variables;
/// Stack of active struct definitions.
SmallVector<StructInfo, 1> StructInProgress;
/// Maps struct tags to struct definitions.
StringMap<StructInfo> Structs;
/// Maps data location names to types.
StringMap<AsmTypeInfo> KnownType;
/// Stack of active macro instantiations.
std::vector<MacroInstantiation*> ActiveMacros;
/// List of bodies of anonymous macros.
std::deque<MCAsmMacro> MacroLikeBodies;
/// Keeps track of how many .macro's have been instantiated.
unsigned NumOfMacroInstantiations;
/// The values from the last parsed cpp hash file line comment if any.
struct CppHashInfoTy {
StringRef Filename;
int64_t LineNumber;
SMLoc Loc;
unsigned Buf;
CppHashInfoTy() : LineNumber(0), Buf(0) {}
};
CppHashInfoTy CppHashInfo;
/// The filename from the first cpp hash file line comment, if any.
StringRef FirstCppHashFilename;
/// List of forward directional labels for diagnosis at the end.
SmallVector<std::tuple<SMLoc, CppHashInfoTy, MCSymbol *>, 4> DirLabels;
/// AssemblerDialect. ~OU means unset value and use value provided by MAI.
/// Defaults to 1U, meaning Intel.
unsigned AssemblerDialect = 1U;
/// is Darwin compatibility enabled?
bool IsDarwin = false;
/// Are we parsing ms-style inline assembly?
bool ParsingMSInlineAsm = false;
/// Did we already inform the user about inconsistent MD5 usage?
bool ReportedInconsistentMD5 = false;
// Current <...> expression depth.
unsigned AngleBracketDepth = 0U;
// Number of locals defined.
uint16_t LocalCounter = 0;
public:
MasmParser(SourceMgr &SM, MCContext &Ctx, MCStreamer &Out,
const MCAsmInfo &MAI, struct tm TM, unsigned CB = 0);
MasmParser(const MasmParser &) = delete;
MasmParser &operator=(const MasmParser &) = delete;
~MasmParser() override;
bool Run(bool NoInitialTextSection, bool NoFinalize = false) override;
void addDirectiveHandler(StringRef Directive,
ExtensionDirectiveHandler Handler) override {
ExtensionDirectiveMap[Directive] = Handler;
if (DirectiveKindMap.find(Directive) == DirectiveKindMap.end()) {
DirectiveKindMap[Directive] = DK_HANDLER_DIRECTIVE;
}
}
void addAliasForDirective(StringRef Directive, StringRef Alias) override {
DirectiveKindMap[Directive] = DirectiveKindMap[Alias];
}
/// @name MCAsmParser Interface
/// {
SourceMgr &getSourceManager() override { return SrcMgr; }
MCAsmLexer &getLexer() override { return Lexer; }
MCContext &getContext() override { return Ctx; }
MCStreamer &getStreamer() override { return Out; }
CodeViewContext &getCVContext() { return Ctx.getCVContext(); }
unsigned getAssemblerDialect() override {
if (AssemblerDialect == ~0U)
return MAI.getAssemblerDialect();
else
return AssemblerDialect;
}
void setAssemblerDialect(unsigned i) override {
AssemblerDialect = i;
}
void Note(SMLoc L, const Twine &Msg, SMRange Range = None) override;
bool Warning(SMLoc L, const Twine &Msg, SMRange Range = None) override;
bool printError(SMLoc L, const Twine &Msg, SMRange Range = None) override;
enum ExpandKind { ExpandMacros, DoNotExpandMacros };
const AsmToken &Lex(ExpandKind ExpandNextToken);
const AsmToken &Lex() override { return Lex(ExpandMacros); }
void setParsingMSInlineAsm(bool V) override {
ParsingMSInlineAsm = V;
// When parsing MS inline asm, we must lex 0b1101 and 0ABCH as binary and
// hex integer literals.
Lexer.setLexMasmIntegers(V);
}
bool isParsingMSInlineAsm() override { return ParsingMSInlineAsm; }
bool isParsingMasm() const override { return true; }
bool defineMacro(StringRef Name, StringRef Value) override;
bool lookUpField(StringRef Name, AsmFieldInfo &Info) const override;
bool lookUpField(StringRef Base, StringRef Member,
AsmFieldInfo &Info) const override;
bool lookUpType(StringRef Name, AsmTypeInfo &Info) const override;
bool parseMSInlineAsm(std::string &AsmString, unsigned &NumOutputs,
unsigned &NumInputs,
SmallVectorImpl<std::pair<void *, bool>> &OpDecls,
SmallVectorImpl<std::string> &Constraints,
SmallVectorImpl<std::string> &Clobbers,
const MCInstrInfo *MII, const MCInstPrinter *IP,
MCAsmParserSemaCallback &SI) override;
bool parseExpression(const MCExpr *&Res);
bool parseExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc,
AsmTypeInfo *TypeInfo) override;
bool parseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) override;
bool parseParenExprOfDepth(unsigned ParenDepth, const MCExpr *&Res,
SMLoc &EndLoc) override;
bool parseAbsoluteExpression(int64_t &Res) override;
/// Parse a floating point expression using the float \p Semantics
/// and set \p Res to the value.
bool parseRealValue(const fltSemantics &Semantics, APInt &Res);
/// Parse an identifier or string (as a quoted identifier)
/// and set \p Res to the identifier contents.
enum IdentifierPositionKind { StandardPosition, StartOfStatement };
bool parseIdentifier(StringRef &Res, IdentifierPositionKind Position);
bool parseIdentifier(StringRef &Res) override {
return parseIdentifier(Res, StandardPosition);
}
void eatToEndOfStatement() override;
bool checkForValidSection() override;
/// }
private:
bool expandMacros();
const AsmToken peekTok(bool ShouldSkipSpace = true);
bool parseStatement(ParseStatementInfo &Info,
MCAsmParserSemaCallback *SI);
bool parseCurlyBlockScope(SmallVectorImpl<AsmRewrite>& AsmStrRewrites);
bool parseCppHashLineFilenameComment(SMLoc L);
bool expandMacro(raw_svector_ostream &OS, StringRef Body,
ArrayRef<MCAsmMacroParameter> Parameters,
ArrayRef<MCAsmMacroArgument> A,
const std::vector<std::string> &Locals, SMLoc L);
/// Are we inside a macro instantiation?
bool isInsideMacroInstantiation() {return !ActiveMacros.empty();}
/// Handle entry to macro instantiation.
///
/// \param M The macro.
/// \param NameLoc Instantiation location.
bool handleMacroEntry(
const MCAsmMacro *M, SMLoc NameLoc,
AsmToken::TokenKind ArgumentEndTok = AsmToken::EndOfStatement);
/// Handle invocation of macro function.
///
/// \param M The macro.
/// \param NameLoc Invocation location.
bool handleMacroInvocation(const MCAsmMacro *M, SMLoc NameLoc);
/// Handle exit from macro instantiation.
void handleMacroExit();
/// Extract AsmTokens for a macro argument.
bool
parseMacroArgument(const MCAsmMacroParameter *MP, MCAsmMacroArgument &MA,
AsmToken::TokenKind EndTok = AsmToken::EndOfStatement);
/// Parse all macro arguments for a given macro.
bool
parseMacroArguments(const MCAsmMacro *M, MCAsmMacroArguments &A,
AsmToken::TokenKind EndTok = AsmToken::EndOfStatement);
void printMacroInstantiations();
bool expandStatement(SMLoc Loc);
void printMessage(SMLoc Loc, SourceMgr::DiagKind Kind, const Twine &Msg,
SMRange Range = None) const {
ArrayRef<SMRange> Ranges(Range);
SrcMgr.PrintMessage(Loc, Kind, Msg, Ranges);
}
static void DiagHandler(const SMDiagnostic &Diag, void *Context);
bool lookUpField(const StructInfo &Structure, StringRef Member,
AsmFieldInfo &Info) const;
/// Should we emit DWARF describing this assembler source? (Returns false if
/// the source has .file directives, which means we don't want to generate
/// info describing the assembler source itself.)
bool enabledGenDwarfForAssembly();
/// Enter the specified file. This returns true on failure.
bool enterIncludeFile(const std::string &Filename);
/// Reset the current lexer position to that given by \p Loc. The
/// current token is not set; clients should ensure Lex() is called
/// subsequently.
///
/// \param InBuffer If not 0, should be the known buffer id that contains the
/// location.
void jumpToLoc(SMLoc Loc, unsigned InBuffer = 0,
bool EndStatementAtEOF = true);
/// Parse up to a token of kind \p EndTok and return the contents from the
/// current token up to (but not including) this token; the current token on
/// exit will be either this kind or EOF. Reads through instantiated macro
/// functions and text macros.
SmallVector<StringRef, 1> parseStringRefsTo(AsmToken::TokenKind EndTok);
std::string parseStringTo(AsmToken::TokenKind EndTok);
/// Parse up to the end of statement and return the contents from the current
/// token until the end of the statement; the current token on exit will be
/// either the EndOfStatement or EOF.
StringRef parseStringToEndOfStatement() override;
bool parseTextItem(std::string &Data);
unsigned getBinOpPrecedence(AsmToken::TokenKind K,
MCBinaryExpr::Opcode &Kind);
bool parseBinOpRHS(unsigned Precedence, const MCExpr *&Res, SMLoc &EndLoc);
bool parseParenExpr(const MCExpr *&Res, SMLoc &EndLoc);
bool parseBracketExpr(const MCExpr *&Res, SMLoc &EndLoc);
bool parseRegisterOrRegisterNumber(int64_t &Register, SMLoc DirectiveLoc);
bool parseCVFunctionId(int64_t &FunctionId, StringRef DirectiveName);
bool parseCVFileId(int64_t &FileId, StringRef DirectiveName);
// Generic (target and platform independent) directive parsing.
enum DirectiveKind {
DK_NO_DIRECTIVE, // Placeholder
DK_HANDLER_DIRECTIVE,
DK_ASSIGN,
DK_EQU,
DK_TEXTEQU,
DK_ASCII,
DK_ASCIZ,
DK_STRING,
DK_BYTE,
DK_SBYTE,
DK_WORD,
DK_SWORD,
DK_DWORD,
DK_SDWORD,
DK_FWORD,
DK_QWORD,
DK_SQWORD,
DK_DB,
DK_DD,
DK_DF,
DK_DQ,
DK_DW,
DK_REAL4,
DK_REAL8,
DK_REAL10,
DK_ALIGN,
DK_EVEN,
DK_ORG,
DK_ENDR,
DK_EXTERN,
DK_PUBLIC,
DK_COMM,
DK_COMMENT,
DK_INCLUDE,
DK_REPEAT,
DK_WHILE,
DK_FOR,
DK_FORC,
DK_IF,
DK_IFE,
DK_IFB,
DK_IFNB,
DK_IFDEF,
DK_IFNDEF,
DK_IFDIF,
DK_IFDIFI,
DK_IFIDN,
DK_IFIDNI,
DK_ELSEIF,
DK_ELSEIFE,
DK_ELSEIFB,
DK_ELSEIFNB,
DK_ELSEIFDEF,
DK_ELSEIFNDEF,
DK_ELSEIFDIF,
DK_ELSEIFDIFI,
DK_ELSEIFIDN,
DK_ELSEIFIDNI,
DK_ELSE,
DK_ENDIF,
DK_FILE,
DK_LINE,
DK_LOC,
DK_STABS,
DK_CV_FILE,
DK_CV_FUNC_ID,
DK_CV_INLINE_SITE_ID,
DK_CV_LOC,
DK_CV_LINETABLE,
DK_CV_INLINE_LINETABLE,
DK_CV_DEF_RANGE,
DK_CV_STRINGTABLE,
DK_CV_STRING,
DK_CV_FILECHECKSUMS,
DK_CV_FILECHECKSUM_OFFSET,
DK_CV_FPO_DATA,
DK_CFI_SECTIONS,
DK_CFI_STARTPROC,
DK_CFI_ENDPROC,
DK_CFI_DEF_CFA,
DK_CFI_DEF_CFA_OFFSET,
DK_CFI_ADJUST_CFA_OFFSET,
DK_CFI_DEF_CFA_REGISTER,
DK_CFI_OFFSET,
DK_CFI_REL_OFFSET,
DK_CFI_PERSONALITY,
DK_CFI_LSDA,
DK_CFI_REMEMBER_STATE,
DK_CFI_RESTORE_STATE,
DK_CFI_SAME_VALUE,
DK_CFI_RESTORE,
DK_CFI_ESCAPE,
DK_CFI_RETURN_COLUMN,
DK_CFI_SIGNAL_FRAME,
DK_CFI_UNDEFINED,
DK_CFI_REGISTER,
DK_CFI_WINDOW_SAVE,
DK_CFI_B_KEY_FRAME,
DK_MACRO,
DK_EXITM,
DK_ENDM,
DK_PURGE,
DK_ERR,
DK_ERRB,
DK_ERRNB,
DK_ERRDEF,
DK_ERRNDEF,
DK_ERRDIF,
DK_ERRDIFI,
DK_ERRIDN,
DK_ERRIDNI,
DK_ERRE,
DK_ERRNZ,
DK_ECHO,
DK_STRUCT,
DK_UNION,
DK_ENDS,
DK_END,
DK_PUSHFRAME,
DK_PUSHREG,
DK_SAVEREG,
DK_SAVEXMM128,
DK_SETFRAME,
DK_RADIX,
};
/// Maps directive name --> DirectiveKind enum, for directives parsed by this
/// class.
StringMap<DirectiveKind> DirectiveKindMap;
bool isMacroLikeDirective();
// Codeview def_range type parsing.
enum CVDefRangeType {
CVDR_DEFRANGE = 0, // Placeholder
CVDR_DEFRANGE_REGISTER,
CVDR_DEFRANGE_FRAMEPOINTER_REL,
CVDR_DEFRANGE_SUBFIELD_REGISTER,
CVDR_DEFRANGE_REGISTER_REL
};
/// Maps Codeview def_range types --> CVDefRangeType enum, for Codeview
/// def_range types parsed by this class.
StringMap<CVDefRangeType> CVDefRangeTypeMap;
// Generic (target and platform independent) directive parsing.
enum BuiltinSymbol {
BI_NO_SYMBOL, // Placeholder
BI_DATE,
BI_TIME,
BI_VERSION,
BI_FILECUR,
BI_FILENAME,
BI_LINE,
BI_CURSEG,
BI_CPU,
BI_INTERFACE,
BI_CODE,
BI_DATA,
BI_FARDATA,
BI_WORDSIZE,
BI_CODESIZE,
BI_DATASIZE,
BI_MODEL,
BI_STACK,
};
/// Maps builtin name --> BuiltinSymbol enum, for builtins handled by this
/// class.
StringMap<BuiltinSymbol> BuiltinSymbolMap;
const MCExpr *evaluateBuiltinValue(BuiltinSymbol Symbol, SMLoc StartLoc);
llvm::Optional<std::string> evaluateBuiltinTextMacro(BuiltinSymbol Symbol,
SMLoc StartLoc);
// ".ascii", ".asciz", ".string"
bool parseDirectiveAscii(StringRef IDVal, bool ZeroTerminated);
// "byte", "word", ...
bool emitIntValue(const MCExpr *Value, unsigned Size);
bool parseScalarInitializer(unsigned Size,
SmallVectorImpl<const MCExpr *> &Values,
unsigned StringPadLength = 0);
bool parseScalarInstList(
unsigned Size, SmallVectorImpl<const MCExpr *> &Values,
const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement);
bool emitIntegralValues(unsigned Size, unsigned *Count = nullptr);
bool addIntegralField(StringRef Name, unsigned Size);
bool parseDirectiveValue(StringRef IDVal, unsigned Size);
bool parseDirectiveNamedValue(StringRef TypeName, unsigned Size,
StringRef Name, SMLoc NameLoc);
// "real4", "real8", "real10"
bool emitRealValues(const fltSemantics &Semantics, unsigned *Count = nullptr);
bool addRealField(StringRef Name, const fltSemantics &Semantics, size_t Size);
bool parseDirectiveRealValue(StringRef IDVal, const fltSemantics &Semantics,
size_t Size);
bool parseRealInstList(
const fltSemantics &Semantics, SmallVectorImpl<APInt> &Values,
const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement);
bool parseDirectiveNamedRealValue(StringRef TypeName,
const fltSemantics &Semantics,
unsigned Size, StringRef Name,
SMLoc NameLoc);
bool parseOptionalAngleBracketOpen();
bool parseAngleBracketClose(const Twine &Msg = "expected '>'");
bool parseFieldInitializer(const FieldInfo &Field,
FieldInitializer &Initializer);
bool parseFieldInitializer(const FieldInfo &Field,
const IntFieldInfo &Contents,
FieldInitializer &Initializer);
bool parseFieldInitializer(const FieldInfo &Field,
const RealFieldInfo &Contents,
FieldInitializer &Initializer);
bool parseFieldInitializer(const FieldInfo &Field,
const StructFieldInfo &Contents,
FieldInitializer &Initializer);
bool parseStructInitializer(const StructInfo &Structure,
StructInitializer &Initializer);
bool parseStructInstList(
const StructInfo &Structure, std::vector<StructInitializer> &Initializers,
const AsmToken::TokenKind EndToken = AsmToken::EndOfStatement);
bool emitFieldValue(const FieldInfo &Field);
bool emitFieldValue(const FieldInfo &Field, const IntFieldInfo &Contents);
bool emitFieldValue(const FieldInfo &Field, const RealFieldInfo &Contents);
bool emitFieldValue(const FieldInfo &Field, const StructFieldInfo &Contents);
bool emitFieldInitializer(const FieldInfo &Field,
const FieldInitializer &Initializer);
bool emitFieldInitializer(const FieldInfo &Field,
const IntFieldInfo &Contents,
const IntFieldInfo &Initializer);
bool emitFieldInitializer(const FieldInfo &Field,
const RealFieldInfo &Contents,
const RealFieldInfo &Initializer);
bool emitFieldInitializer(const FieldInfo &Field,
const StructFieldInfo &Contents,
const StructFieldInfo &Initializer);
bool emitStructInitializer(const StructInfo &Structure,
const StructInitializer &Initializer);
// User-defined types (structs, unions):
bool emitStructValues(const StructInfo &Structure, unsigned *Count = nullptr);
bool addStructField(StringRef Name, const StructInfo &Structure);
bool parseDirectiveStructValue(const StructInfo &Structure,
StringRef Directive, SMLoc DirLoc);
bool parseDirectiveNamedStructValue(const StructInfo &Structure,
StringRef Directive, SMLoc DirLoc,
StringRef Name);
// "=", "equ", "textequ"
bool parseDirectiveEquate(StringRef IDVal, StringRef Name,
DirectiveKind DirKind, SMLoc NameLoc);
bool parseDirectiveOrg(); // "org"
bool emitAlignTo(int64_t Alignment);
bool parseDirectiveAlign(); // "align"
bool parseDirectiveEven(); // "even"
// ".file", ".line", ".loc", ".stabs"
bool parseDirectiveFile(SMLoc DirectiveLoc);
bool parseDirectiveLine();
bool parseDirectiveLoc();
bool parseDirectiveStabs();
// ".cv_file", ".cv_func_id", ".cv_inline_site_id", ".cv_loc", ".cv_linetable",
// ".cv_inline_linetable", ".cv_def_range", ".cv_string"
bool parseDirectiveCVFile();
bool parseDirectiveCVFuncId();
bool parseDirectiveCVInlineSiteId();
bool parseDirectiveCVLoc();
bool parseDirectiveCVLinetable();
bool parseDirectiveCVInlineLinetable();
bool parseDirectiveCVDefRange();
bool parseDirectiveCVString();
bool parseDirectiveCVStringTable();
bool parseDirectiveCVFileChecksums();
bool parseDirectiveCVFileChecksumOffset();
bool parseDirectiveCVFPOData();
// .cfi directives
bool parseDirectiveCFIRegister(SMLoc DirectiveLoc);
bool parseDirectiveCFIWindowSave();
bool parseDirectiveCFISections();
bool parseDirectiveCFIStartProc();
bool parseDirectiveCFIEndProc();
bool parseDirectiveCFIDefCfaOffset();
bool parseDirectiveCFIDefCfa(SMLoc DirectiveLoc);
bool parseDirectiveCFIAdjustCfaOffset();
bool parseDirectiveCFIDefCfaRegister(SMLoc DirectiveLoc);
bool parseDirectiveCFIOffset(SMLoc DirectiveLoc);
bool parseDirectiveCFIRelOffset(SMLoc DirectiveLoc);
bool parseDirectiveCFIPersonalityOrLsda(bool IsPersonality);
bool parseDirectiveCFIRememberState();
bool parseDirectiveCFIRestoreState();
bool parseDirectiveCFISameValue(SMLoc DirectiveLoc);
bool parseDirectiveCFIRestore(SMLoc DirectiveLoc);
bool parseDirectiveCFIEscape();
bool parseDirectiveCFIReturnColumn(SMLoc DirectiveLoc);
bool parseDirectiveCFISignalFrame();
bool parseDirectiveCFIUndefined(SMLoc DirectiveLoc);
// macro directives
bool parseDirectivePurgeMacro(SMLoc DirectiveLoc);
bool parseDirectiveExitMacro(SMLoc DirectiveLoc, StringRef Directive,
std::string &Value);
bool parseDirectiveEndMacro(StringRef Directive);
bool parseDirectiveMacro(StringRef Name, SMLoc NameLoc);
bool parseDirectiveStruct(StringRef Directive, DirectiveKind DirKind,
StringRef Name, SMLoc NameLoc);
bool parseDirectiveNestedStruct(StringRef Directive, DirectiveKind DirKind);
bool parseDirectiveEnds(StringRef Name, SMLoc NameLoc);
bool parseDirectiveNestedEnds();
/// Parse a directive like ".globl" which accepts a single symbol (which
/// should be a label or an external).
bool parseDirectiveSymbolAttribute(MCSymbolAttr Attr);
bool parseDirectiveComm(bool IsLocal); // ".comm" and ".lcomm"
bool parseDirectiveComment(SMLoc DirectiveLoc); // "comment"
bool parseDirectiveInclude(); // "include"
// "if" or "ife"
bool parseDirectiveIf(SMLoc DirectiveLoc, DirectiveKind DirKind);
// "ifb" or "ifnb", depending on ExpectBlank.
bool parseDirectiveIfb(SMLoc DirectiveLoc, bool ExpectBlank);
// "ifidn", "ifdif", "ifidni", or "ifdifi", depending on ExpectEqual and
// CaseInsensitive.
bool parseDirectiveIfidn(SMLoc DirectiveLoc, bool ExpectEqual,
bool CaseInsensitive);