1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "AsmParser.h"
16 #include "llvm/ADT/SmallString.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCInst.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCStreamer.h"
23 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetAsmParser.h"
29 // Mach-O section uniquing.
31 // FIXME: Figure out where this should live, it should be shared by
32 // TargetLoweringObjectFile.
33 typedef StringMap
<const MCSectionMachO
*> MachOUniqueMapTy
;
35 AsmParser::~AsmParser() {
36 // If we have the MachO uniquing map, free it.
37 delete (MachOUniqueMapTy
*)SectionUniquingMap
;
40 const MCSection
*AsmParser::getMachOSection(const StringRef
&Segment
,
41 const StringRef
&Section
,
42 unsigned TypeAndAttributes
,
44 SectionKind Kind
) const {
45 // We unique sections by their segment/section pair. The returned section
46 // may not have the same flags as the requested section, if so this should be
47 // diagnosed by the client as an error.
49 // Create the map if it doesn't already exist.
50 if (SectionUniquingMap
== 0)
51 SectionUniquingMap
= new MachOUniqueMapTy();
52 MachOUniqueMapTy
&Map
= *(MachOUniqueMapTy
*)SectionUniquingMap
;
54 // Form the name to look up.
60 // Do the lookup, if we have a hit, return it.
61 const MCSectionMachO
*&Entry
= Map
[Name
.str()];
63 // FIXME: This should validate the type and attributes.
64 if (Entry
) return Entry
;
66 // Otherwise, return a new section.
67 return Entry
= MCSectionMachO::Create(Segment
, Section
, TypeAndAttributes
,
68 Reserved2
, Kind
, Ctx
);
71 void AsmParser::Warning(SMLoc L
, const Twine
&Msg
) {
72 Lexer
.PrintMessage(L
, Msg
.str(), "warning");
75 bool AsmParser::Error(SMLoc L
, const Twine
&Msg
) {
76 Lexer
.PrintMessage(L
, Msg
.str(), "error");
80 bool AsmParser::TokError(const char *Msg
) {
81 Lexer
.PrintMessage(Lexer
.getLoc(), Msg
, "error");
85 bool AsmParser::Run() {
86 // Create the initial section.
89 // FIXME: Target hook & command line option for initial section.
90 Out
.SwitchSection(getMachOSection("__TEXT", "__text",
91 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS
,
98 bool HadError
= false;
100 AsmCond StartingCondState
= TheCondState
;
102 // While we have input, parse each statement.
103 while (Lexer
.isNot(AsmToken::Eof
)) {
104 // Handle conditional assembly here before calling ParseStatement()
105 if (Lexer
.getKind() == AsmToken::Identifier
) {
106 // If we have an identifier, handle it as the key symbol.
107 AsmToken ID
= Lexer
.getTok();
108 SMLoc IDLoc
= ID
.getLoc();
109 StringRef IDVal
= ID
.getString();
111 if (IDVal
== ".if" ||
112 IDVal
== ".elseif" ||
115 if (!ParseConditionalAssemblyDirectives(IDVal
, IDLoc
))
118 EatToEndOfStatement();
122 if (TheCondState
.Ignore
) {
123 EatToEndOfStatement();
127 if (!ParseStatement()) continue;
129 // We had an error, remember it and recover by skipping to the next line.
131 EatToEndOfStatement();
134 if (TheCondState
.TheCond
!= StartingCondState
.TheCond
||
135 TheCondState
.Ignore
!= StartingCondState
.Ignore
)
136 return TokError("unmatched .ifs or .elses");
144 /// ParseConditionalAssemblyDirectives - parse the conditional assembly
146 bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive
,
147 SMLoc DirectiveLoc
) {
148 if (Directive
== ".if")
149 return ParseDirectiveIf(DirectiveLoc
);
150 if (Directive
== ".elseif")
151 return ParseDirectiveElseIf(DirectiveLoc
);
152 if (Directive
== ".else")
153 return ParseDirectiveElse(DirectiveLoc
);
154 if (Directive
== ".endif")
155 return ParseDirectiveEndIf(DirectiveLoc
);
159 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
160 void AsmParser::EatToEndOfStatement() {
161 while (Lexer
.isNot(AsmToken::EndOfStatement
) &&
162 Lexer
.isNot(AsmToken::Eof
))
166 if (Lexer
.is(AsmToken::EndOfStatement
))
171 /// ParseParenExpr - Parse a paren expression and return it.
172 /// NOTE: This assumes the leading '(' has already been consumed.
174 /// parenexpr ::= expr)
176 bool AsmParser::ParseParenExpr(const MCExpr
*&Res
) {
177 if (ParseExpression(Res
)) return true;
178 if (Lexer
.isNot(AsmToken::RParen
))
179 return TokError("expected ')' in parentheses expression");
184 MCSymbol
*AsmParser::CreateSymbol(StringRef Name
) {
185 if (MCSymbol
*S
= Ctx
.LookupSymbol(Name
))
188 // If the label starts with L it is an assembler temporary label.
189 if (Name
.startswith("L"))
190 return Ctx
.CreateTemporarySymbol(Name
);
192 return Ctx
.CreateSymbol(Name
);
195 /// ParsePrimaryExpr - Parse a primary expression and return it.
196 /// primaryexpr ::= (parenexpr
197 /// primaryexpr ::= symbol
198 /// primaryexpr ::= number
199 /// primaryexpr ::= ~,+,- primaryexpr
200 bool AsmParser::ParsePrimaryExpr(const MCExpr
*&Res
) {
201 switch (Lexer
.getKind()) {
203 return TokError("unknown token in expression");
204 case AsmToken::Exclaim
:
205 Lexer
.Lex(); // Eat the operator.
206 if (ParsePrimaryExpr(Res
))
208 Res
= MCUnaryExpr::CreateLNot(Res
, getContext());
210 case AsmToken::String
:
211 case AsmToken::Identifier
:
212 // This is a label, this should be parsed as part of an expression, to
213 // handle things like LFOO+4.
214 Res
= MCSymbolRefExpr::Create(Lexer
.getTok().getIdentifier(), getContext());
215 Lexer
.Lex(); // Eat identifier.
217 case AsmToken::Integer
:
218 Res
= MCConstantExpr::Create(Lexer
.getTok().getIntVal(), getContext());
219 Lexer
.Lex(); // Eat token.
221 case AsmToken::LParen
:
222 Lexer
.Lex(); // Eat the '('.
223 return ParseParenExpr(Res
);
224 case AsmToken::Minus
:
225 Lexer
.Lex(); // Eat the operator.
226 if (ParsePrimaryExpr(Res
))
228 Res
= MCUnaryExpr::CreateMinus(Res
, getContext());
231 Lexer
.Lex(); // Eat the operator.
232 if (ParsePrimaryExpr(Res
))
234 Res
= MCUnaryExpr::CreatePlus(Res
, getContext());
236 case AsmToken::Tilde
:
237 Lexer
.Lex(); // Eat the operator.
238 if (ParsePrimaryExpr(Res
))
240 Res
= MCUnaryExpr::CreateNot(Res
, getContext());
245 /// ParseExpression - Parse an expression and return it.
247 /// expr ::= expr +,- expr -> lowest.
248 /// expr ::= expr |,^,&,! expr -> middle.
249 /// expr ::= expr *,/,%,<<,>> expr -> highest.
250 /// expr ::= primaryexpr
252 bool AsmParser::ParseExpression(const MCExpr
*&Res
) {
254 return ParsePrimaryExpr(Res
) ||
255 ParseBinOpRHS(1, Res
);
258 bool AsmParser::ParseParenExpression(const MCExpr
*&Res
) {
259 if (ParseParenExpr(Res
))
265 bool AsmParser::ParseAbsoluteExpression(int64_t &Res
) {
268 SMLoc StartLoc
= Lexer
.getLoc();
269 if (ParseExpression(Expr
))
272 if (!Expr
->EvaluateAsAbsolute(Ctx
, Res
))
273 return Error(StartLoc
, "expected absolute expression");
278 static unsigned getBinOpPrecedence(AsmToken::TokenKind K
,
279 MCBinaryExpr::Opcode
&Kind
) {
282 return 0; // not a binop.
284 // Lowest Precedence: &&, ||
285 case AsmToken::AmpAmp
:
286 Kind
= MCBinaryExpr::LAnd
;
288 case AsmToken::PipePipe
:
289 Kind
= MCBinaryExpr::LOr
;
292 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
294 Kind
= MCBinaryExpr::Add
;
296 case AsmToken::Minus
:
297 Kind
= MCBinaryExpr::Sub
;
299 case AsmToken::EqualEqual
:
300 Kind
= MCBinaryExpr::EQ
;
302 case AsmToken::ExclaimEqual
:
303 case AsmToken::LessGreater
:
304 Kind
= MCBinaryExpr::NE
;
307 Kind
= MCBinaryExpr::LT
;
309 case AsmToken::LessEqual
:
310 Kind
= MCBinaryExpr::LTE
;
312 case AsmToken::Greater
:
313 Kind
= MCBinaryExpr::GT
;
315 case AsmToken::GreaterEqual
:
316 Kind
= MCBinaryExpr::GTE
;
319 // Intermediate Precedence: |, &, ^
321 // FIXME: gas seems to support '!' as an infix operator?
323 Kind
= MCBinaryExpr::Or
;
325 case AsmToken::Caret
:
326 Kind
= MCBinaryExpr::Xor
;
329 Kind
= MCBinaryExpr::And
;
332 // Highest Precedence: *, /, %, <<, >>
334 Kind
= MCBinaryExpr::Mul
;
336 case AsmToken::Slash
:
337 Kind
= MCBinaryExpr::Div
;
339 case AsmToken::Percent
:
340 Kind
= MCBinaryExpr::Mod
;
342 case AsmToken::LessLess
:
343 Kind
= MCBinaryExpr::Shl
;
345 case AsmToken::GreaterGreater
:
346 Kind
= MCBinaryExpr::Shr
;
352 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
353 /// Res contains the LHS of the expression on input.
354 bool AsmParser::ParseBinOpRHS(unsigned Precedence
, const MCExpr
*&Res
) {
356 MCBinaryExpr::Opcode Kind
= MCBinaryExpr::Add
;
357 unsigned TokPrec
= getBinOpPrecedence(Lexer
.getKind(), Kind
);
359 // If the next token is lower precedence than we are allowed to eat, return
360 // successfully with what we ate already.
361 if (TokPrec
< Precedence
)
366 // Eat the next primary expression.
368 if (ParsePrimaryExpr(RHS
)) return true;
370 // If BinOp binds less tightly with RHS than the operator after RHS, let
371 // the pending operator take RHS as its LHS.
372 MCBinaryExpr::Opcode Dummy
;
373 unsigned NextTokPrec
= getBinOpPrecedence(Lexer
.getKind(), Dummy
);
374 if (TokPrec
< NextTokPrec
) {
375 if (ParseBinOpRHS(Precedence
+1, RHS
)) return true;
378 // Merge LHS and RHS according to operator.
379 Res
= MCBinaryExpr::Create(Kind
, Res
, RHS
, getContext());
387 /// ::= EndOfStatement
388 /// ::= Label* Directive ...Operands... EndOfStatement
389 /// ::= Label* Identifier OperandList* EndOfStatement
390 bool AsmParser::ParseStatement() {
391 if (Lexer
.is(AsmToken::EndOfStatement
)) {
396 // Statements always start with an identifier.
397 AsmToken ID
= Lexer
.getTok();
398 SMLoc IDLoc
= ID
.getLoc();
400 if (ParseIdentifier(IDVal
))
401 return TokError("unexpected token at start of statement");
403 // FIXME: Recurse on local labels?
405 // See what kind of statement we have.
406 switch (Lexer
.getKind()) {
407 case AsmToken::Colon
: {
408 // identifier ':' -> Label.
411 // Diagnose attempt to use a variable as a label.
413 // FIXME: Diagnostics. Note the location of the definition as a label.
414 // FIXME: This doesn't diagnose assignment to a symbol which has been
415 // implicitly marked as external.
416 MCSymbol
*Sym
= CreateSymbol(IDVal
);
417 if (!Sym
->isUndefined())
418 return Error(IDLoc
, "invalid symbol redefinition");
423 return ParseStatement();
426 case AsmToken::Equal
:
427 // identifier '=' ... -> assignment statement
430 return ParseAssignment(IDVal
);
432 default: // Normal instruction or directive.
436 // Otherwise, we have a normal instruction or directive.
437 if (IDVal
[0] == '.') {
438 // FIXME: This should be driven based on a hash lookup and callback.
439 if (IDVal
== ".section")
440 return ParseDirectiveDarwinSection();
441 if (IDVal
== ".text")
442 // FIXME: This changes behavior based on the -static flag to the
444 return ParseDirectiveSectionSwitch("__TEXT", "__text",
445 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS
);
446 if (IDVal
== ".const")
447 return ParseDirectiveSectionSwitch("__TEXT", "__const");
448 if (IDVal
== ".static_const")
449 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
450 if (IDVal
== ".cstring")
451 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
452 MCSectionMachO::S_CSTRING_LITERALS
);
453 if (IDVal
== ".literal4")
454 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
455 MCSectionMachO::S_4BYTE_LITERALS
,
457 if (IDVal
== ".literal8")
458 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
459 MCSectionMachO::S_8BYTE_LITERALS
,
461 if (IDVal
== ".literal16")
462 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
463 MCSectionMachO::S_16BYTE_LITERALS
,
465 if (IDVal
== ".constructor")
466 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
467 if (IDVal
== ".destructor")
468 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
469 if (IDVal
== ".fvmlib_init0")
470 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
471 if (IDVal
== ".fvmlib_init1")
472 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
474 // FIXME: The assembler manual claims that this has the self modify code
475 // flag, at least on x86-32, but that does not appear to be correct.
476 if (IDVal
== ".symbol_stub")
477 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
478 MCSectionMachO::S_SYMBOL_STUBS
|
479 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS
,
480 // FIXME: Different on PPC and ARM.
482 // FIXME: PowerPC only?
483 if (IDVal
== ".picsymbol_stub")
484 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
485 MCSectionMachO::S_SYMBOL_STUBS
|
486 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS
,
488 if (IDVal
== ".data")
489 return ParseDirectiveSectionSwitch("__DATA", "__data");
490 if (IDVal
== ".static_data")
491 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
493 // FIXME: The section names of these two are misspelled in the assembler
495 if (IDVal
== ".non_lazy_symbol_pointer")
496 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
497 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS
,
499 if (IDVal
== ".lazy_symbol_pointer")
500 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
501 MCSectionMachO::S_LAZY_SYMBOL_POINTERS
,
504 if (IDVal
== ".dyld")
505 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
506 if (IDVal
== ".mod_init_func")
507 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
508 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS
,
510 if (IDVal
== ".mod_term_func")
511 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
512 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS
,
514 if (IDVal
== ".const_data")
515 return ParseDirectiveSectionSwitch("__DATA", "__const");
518 if (IDVal
== ".objc_class")
519 return ParseDirectiveSectionSwitch("__OBJC", "__class",
520 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
521 if (IDVal
== ".objc_meta_class")
522 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
523 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
524 if (IDVal
== ".objc_cat_cls_meth")
525 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
526 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
527 if (IDVal
== ".objc_cat_inst_meth")
528 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
529 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
530 if (IDVal
== ".objc_protocol")
531 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
532 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
533 if (IDVal
== ".objc_string_object")
534 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
535 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
536 if (IDVal
== ".objc_cls_meth")
537 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
538 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
539 if (IDVal
== ".objc_inst_meth")
540 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
541 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
542 if (IDVal
== ".objc_cls_refs")
543 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
|
545 MCSectionMachO::S_LITERAL_POINTERS
,
547 if (IDVal
== ".objc_message_refs")
548 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
549 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
|
550 MCSectionMachO::S_LITERAL_POINTERS
,
552 if (IDVal
== ".objc_symbols")
553 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
554 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
555 if (IDVal
== ".objc_category")
556 return ParseDirectiveSectionSwitch("__OBJC", "__category",
557 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
558 if (IDVal
== ".objc_class_vars")
559 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
560 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
561 if (IDVal
== ".objc_instance_vars")
562 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
564 if (IDVal
== ".objc_module_info")
565 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP
);
567 if (IDVal
== ".objc_class_names")
568 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
569 MCSectionMachO::S_CSTRING_LITERALS
);
570 if (IDVal
== ".objc_meth_var_types")
571 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
572 MCSectionMachO::S_CSTRING_LITERALS
);
573 if (IDVal
== ".objc_meth_var_names")
574 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
575 MCSectionMachO::S_CSTRING_LITERALS
);
576 if (IDVal
== ".objc_selector_strs")
577 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
578 MCSectionMachO::S_CSTRING_LITERALS
);
580 // Assembler features
582 return ParseDirectiveSet();
586 if (IDVal
== ".ascii")
587 return ParseDirectiveAscii(false);
588 if (IDVal
== ".asciz")
589 return ParseDirectiveAscii(true);
591 if (IDVal
== ".byte")
592 return ParseDirectiveValue(1);
593 if (IDVal
== ".short")
594 return ParseDirectiveValue(2);
595 if (IDVal
== ".long")
596 return ParseDirectiveValue(4);
597 if (IDVal
== ".quad")
598 return ParseDirectiveValue(8);
600 // FIXME: Target hooks for IsPow2.
601 if (IDVal
== ".align")
602 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
603 if (IDVal
== ".align32")
604 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
605 if (IDVal
== ".balign")
606 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
607 if (IDVal
== ".balignw")
608 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
609 if (IDVal
== ".balignl")
610 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
611 if (IDVal
== ".p2align")
612 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
613 if (IDVal
== ".p2alignw")
614 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
615 if (IDVal
== ".p2alignl")
616 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
619 return ParseDirectiveOrg();
621 if (IDVal
== ".fill")
622 return ParseDirectiveFill();
623 if (IDVal
== ".space")
624 return ParseDirectiveSpace();
626 // Symbol attribute directives
628 if (IDVal
== ".globl" || IDVal
== ".global")
629 return ParseDirectiveSymbolAttribute(MCStreamer::Global
);
630 if (IDVal
== ".hidden")
631 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden
);
632 if (IDVal
== ".indirect_symbol")
633 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol
);
634 if (IDVal
== ".internal")
635 return ParseDirectiveSymbolAttribute(MCStreamer::Internal
);
636 if (IDVal
== ".lazy_reference")
637 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference
);
638 if (IDVal
== ".no_dead_strip")
639 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip
);
640 if (IDVal
== ".private_extern")
641 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern
);
642 if (IDVal
== ".protected")
643 return ParseDirectiveSymbolAttribute(MCStreamer::Protected
);
644 if (IDVal
== ".reference")
645 return ParseDirectiveSymbolAttribute(MCStreamer::Reference
);
646 if (IDVal
== ".weak")
647 return ParseDirectiveSymbolAttribute(MCStreamer::Weak
);
648 if (IDVal
== ".weak_definition")
649 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition
);
650 if (IDVal
== ".weak_reference")
651 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference
);
653 if (IDVal
== ".comm")
654 return ParseDirectiveComm(/*IsLocal=*/false);
655 if (IDVal
== ".lcomm")
656 return ParseDirectiveComm(/*IsLocal=*/true);
657 if (IDVal
== ".zerofill")
658 return ParseDirectiveDarwinZerofill();
659 if (IDVal
== ".desc")
660 return ParseDirectiveDarwinSymbolDesc();
661 if (IDVal
== ".lsym")
662 return ParseDirectiveDarwinLsym();
664 if (IDVal
== ".subsections_via_symbols")
665 return ParseDirectiveDarwinSubsectionsViaSymbols();
666 if (IDVal
== ".abort")
667 return ParseDirectiveAbort();
668 if (IDVal
== ".include")
669 return ParseDirectiveInclude();
670 if (IDVal
== ".dump")
671 return ParseDirectiveDarwinDumpOrLoad(IDLoc
, /*IsDump=*/true);
672 if (IDVal
== ".load")
673 return ParseDirectiveDarwinDumpOrLoad(IDLoc
, /*IsLoad=*/false);
675 // Debugging directives
677 if (IDVal
== ".file")
678 return ParseDirectiveFile(IDLoc
);
679 if (IDVal
== ".line")
680 return ParseDirectiveLine(IDLoc
);
682 return ParseDirectiveLoc(IDLoc
);
684 // Target hook for parsing target specific directives.
685 if (!getTargetParser().ParseDirective(ID
))
688 Warning(IDLoc
, "ignoring directive for now");
689 EatToEndOfStatement();
694 if (getTargetParser().ParseInstruction(IDVal
, Inst
))
697 if (Lexer
.isNot(AsmToken::EndOfStatement
))
698 return TokError("unexpected token in argument list");
700 // Eat the end of statement marker.
703 // Instruction is good, process it.
704 Out
.EmitInstruction(Inst
);
706 // Skip to end of line for now.
710 bool AsmParser::ParseAssignment(const StringRef
&Name
) {
711 // FIXME: Use better location, we should use proper tokens.
712 SMLoc EqualLoc
= Lexer
.getLoc();
715 SMLoc StartLoc
= Lexer
.getLoc();
716 if (ParseExpression(Value
))
719 if (Lexer
.isNot(AsmToken::EndOfStatement
))
720 return TokError("unexpected token in assignment");
722 // Eat the end of statement marker.
725 // Diagnose assignment to a label.
727 // FIXME: Diagnostics. Note the location of the definition as a label.
728 // FIXME: Handle '.'.
729 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
730 MCSymbol
*Sym
= CreateSymbol(Name
);
731 if (!Sym
->isUndefined() && !Sym
->isAbsolute())
732 return Error(EqualLoc
, "symbol has already been defined");
734 // Do the assignment.
735 Out
.EmitAssignment(Sym
, Value
);
743 bool AsmParser::ParseIdentifier(StringRef
&Res
) {
744 if (Lexer
.isNot(AsmToken::Identifier
) &&
745 Lexer
.isNot(AsmToken::String
))
748 Res
= Lexer
.getTok().getIdentifier();
750 Lexer
.Lex(); // Consume the identifier token.
755 /// ParseDirectiveSet:
756 /// ::= .set identifier ',' expression
757 bool AsmParser::ParseDirectiveSet() {
760 if (ParseIdentifier(Name
))
761 return TokError("expected identifier after '.set' directive");
763 if (Lexer
.isNot(AsmToken::Comma
))
764 return TokError("unexpected token in '.set'");
767 return ParseAssignment(Name
);
770 /// ParseDirectiveSection:
771 /// ::= .section identifier (',' identifier)*
772 /// FIXME: This should actually parse out the segment, section, attributes and
773 /// sizeof_stub fields.
774 bool AsmParser::ParseDirectiveDarwinSection() {
775 SMLoc Loc
= Lexer
.getLoc();
777 StringRef SectionName
;
778 if (ParseIdentifier(SectionName
))
779 return Error(Loc
, "expected identifier after '.section' directive");
781 // Verify there is a following comma.
782 if (!Lexer
.is(AsmToken::Comma
))
783 return TokError("unexpected token in '.section' directive");
785 std::string SectionSpec
= SectionName
;
788 // Add all the tokens until the end of the line, ParseSectionSpecifier will
790 StringRef EOL
= Lexer
.LexUntilEndOfStatement();
791 SectionSpec
.append(EOL
.begin(), EOL
.end());
794 if (Lexer
.isNot(AsmToken::EndOfStatement
))
795 return TokError("unexpected token in '.section' directive");
799 StringRef Segment
, Section
;
800 unsigned TAA
, StubSize
;
801 std::string ErrorStr
=
802 MCSectionMachO::ParseSectionSpecifier(SectionSpec
, Segment
, Section
,
805 if (!ErrorStr
.empty())
806 return Error(Loc
, ErrorStr
.c_str());
808 // FIXME: Arch specific.
809 Out
.SwitchSection(getMachOSection(Segment
, Section
, TAA
, StubSize
,
814 /// ParseDirectiveSectionSwitch -
815 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment
,
817 unsigned TAA
, unsigned Align
,
819 if (Lexer
.isNot(AsmToken::EndOfStatement
))
820 return TokError("unexpected token in section switching directive");
823 // FIXME: Arch specific.
824 Out
.SwitchSection(getMachOSection(Segment
, Section
, TAA
, StubSize
,
827 // Set the implicit alignment, if any.
829 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
830 // alignment on the section (e.g., if one manually inserts bytes into the
831 // section, then just issueing the section switch directive will not realign
832 // the section. However, this is arguably more reasonable behavior, and there
833 // is no good reason for someone to intentionally emit incorrectly sized
834 // values into the implicitly aligned sections.
836 Out
.EmitValueToAlignment(Align
, 0, 1, 0);
841 bool AsmParser::ParseEscapedString(std::string
&Data
) {
842 assert(Lexer
.is(AsmToken::String
) && "Unexpected current token!");
845 StringRef Str
= Lexer
.getTok().getStringContents();
846 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
) {
847 if (Str
[i
] != '\\') {
852 // Recognize escaped characters. Note that this escape semantics currently
853 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
856 return TokError("unexpected backslash at end of string");
858 // Recognize octal sequences.
859 if ((unsigned) (Str
[i
] - '0') <= 7) {
860 // Consume up to three octal characters.
861 unsigned Value
= Str
[i
] - '0';
863 if (i
+ 1 != e
&& ((unsigned) (Str
[i
+ 1] - '0')) <= 7) {
865 Value
= Value
* 8 + (Str
[i
] - '0');
867 if (i
+ 1 != e
&& ((unsigned) (Str
[i
+ 1] - '0')) <= 7) {
869 Value
= Value
* 8 + (Str
[i
] - '0');
874 return TokError("invalid octal escape sequence (out of range)");
876 Data
+= (unsigned char) Value
;
880 // Otherwise recognize individual escapes.
883 // Just reject invalid escape sequences for now.
884 return TokError("invalid escape sequence (unrecognized character)");
886 case 'b': Data
+= '\b'; break;
887 case 'f': Data
+= '\f'; break;
888 case 'n': Data
+= '\n'; break;
889 case 'r': Data
+= '\r'; break;
890 case 't': Data
+= '\t'; break;
891 case '"': Data
+= '"'; break;
892 case '\\': Data
+= '\\'; break;
899 /// ParseDirectiveAscii:
900 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
901 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated
) {
902 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
904 if (Lexer
.isNot(AsmToken::String
))
905 return TokError("expected string in '.ascii' or '.asciz' directive");
908 if (ParseEscapedString(Data
))
913 Out
.EmitBytes(StringRef("\0", 1));
917 if (Lexer
.is(AsmToken::EndOfStatement
))
920 if (Lexer
.isNot(AsmToken::Comma
))
921 return TokError("unexpected token in '.ascii' or '.asciz' directive");
930 /// ParseDirectiveValue
931 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
932 bool AsmParser::ParseDirectiveValue(unsigned Size
) {
933 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
936 SMLoc StartLoc
= Lexer
.getLoc();
937 if (ParseExpression(Value
))
940 Out
.EmitValue(Value
, Size
);
942 if (Lexer
.is(AsmToken::EndOfStatement
))
945 // FIXME: Improve diagnostic.
946 if (Lexer
.isNot(AsmToken::Comma
))
947 return TokError("unexpected token in directive");
956 /// ParseDirectiveSpace
957 /// ::= .space expression [ , expression ]
958 bool AsmParser::ParseDirectiveSpace() {
960 if (ParseAbsoluteExpression(NumBytes
))
963 int64_t FillExpr
= 0;
964 bool HasFillExpr
= false;
965 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
966 if (Lexer
.isNot(AsmToken::Comma
))
967 return TokError("unexpected token in '.space' directive");
970 if (ParseAbsoluteExpression(FillExpr
))
975 if (Lexer
.isNot(AsmToken::EndOfStatement
))
976 return TokError("unexpected token in '.space' directive");
982 return TokError("invalid number of bytes in '.space' directive");
984 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
985 for (uint64_t i
= 0, e
= NumBytes
; i
!= e
; ++i
)
986 Out
.EmitValue(MCConstantExpr::Create(FillExpr
, getContext()), 1);
991 /// ParseDirectiveFill
992 /// ::= .fill expression , expression , expression
993 bool AsmParser::ParseDirectiveFill() {
995 if (ParseAbsoluteExpression(NumValues
))
998 if (Lexer
.isNot(AsmToken::Comma
))
999 return TokError("unexpected token in '.fill' directive");
1003 if (ParseAbsoluteExpression(FillSize
))
1006 if (Lexer
.isNot(AsmToken::Comma
))
1007 return TokError("unexpected token in '.fill' directive");
1011 if (ParseAbsoluteExpression(FillExpr
))
1014 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1015 return TokError("unexpected token in '.fill' directive");
1019 if (FillSize
!= 1 && FillSize
!= 2 && FillSize
!= 4 && FillSize
!= 8)
1020 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1022 for (uint64_t i
= 0, e
= NumValues
; i
!= e
; ++i
)
1023 Out
.EmitValue(MCConstantExpr::Create(FillExpr
, getContext()), FillSize
);
1028 /// ParseDirectiveOrg
1029 /// ::= .org expression [ , expression ]
1030 bool AsmParser::ParseDirectiveOrg() {
1031 const MCExpr
*Offset
;
1032 SMLoc StartLoc
= Lexer
.getLoc();
1033 if (ParseExpression(Offset
))
1036 // Parse optional fill expression.
1037 int64_t FillExpr
= 0;
1038 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1039 if (Lexer
.isNot(AsmToken::Comma
))
1040 return TokError("unexpected token in '.org' directive");
1043 if (ParseAbsoluteExpression(FillExpr
))
1046 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1047 return TokError("unexpected token in '.org' directive");
1052 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1053 // has to be relative to the current section.
1054 Out
.EmitValueToOffset(Offset
, FillExpr
);
1059 /// ParseDirectiveAlign
1060 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1061 bool AsmParser::ParseDirectiveAlign(bool IsPow2
, unsigned ValueSize
) {
1062 SMLoc AlignmentLoc
= Lexer
.getLoc();
1064 if (ParseAbsoluteExpression(Alignment
))
1068 bool HasFillExpr
= false;
1069 int64_t FillExpr
= 0;
1070 int64_t MaxBytesToFill
= 0;
1071 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1072 if (Lexer
.isNot(AsmToken::Comma
))
1073 return TokError("unexpected token in directive");
1076 // The fill expression can be omitted while specifying a maximum number of
1077 // alignment bytes, e.g:
1079 if (Lexer
.isNot(AsmToken::Comma
)) {
1081 if (ParseAbsoluteExpression(FillExpr
))
1085 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1086 if (Lexer
.isNot(AsmToken::Comma
))
1087 return TokError("unexpected token in directive");
1090 MaxBytesLoc
= Lexer
.getLoc();
1091 if (ParseAbsoluteExpression(MaxBytesToFill
))
1094 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1095 return TokError("unexpected token in directive");
1102 // FIXME: Sometimes fill with nop.
1106 // Compute alignment in bytes.
1108 // FIXME: Diagnose overflow.
1109 if (Alignment
>= 32) {
1110 Error(AlignmentLoc
, "invalid alignment value");
1114 Alignment
= 1ULL << Alignment
;
1117 // Diagnose non-sensical max bytes to align.
1118 if (MaxBytesLoc
.isValid()) {
1119 if (MaxBytesToFill
< 1) {
1120 Error(MaxBytesLoc
, "alignment directive can never be satisfied in this "
1121 "many bytes, ignoring maximum bytes expression");
1125 if (MaxBytesToFill
>= Alignment
) {
1126 Warning(MaxBytesLoc
, "maximum bytes expression exceeds alignment and "
1132 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1133 Out
.EmitValueToAlignment(Alignment
, FillExpr
, ValueSize
, MaxBytesToFill
);
1138 /// ParseDirectiveSymbolAttribute
1139 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1140 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr
) {
1141 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1145 if (ParseIdentifier(Name
))
1146 return TokError("expected identifier in directive");
1148 MCSymbol
*Sym
= CreateSymbol(Name
);
1150 Out
.EmitSymbolAttribute(Sym
, Attr
);
1152 if (Lexer
.is(AsmToken::EndOfStatement
))
1155 if (Lexer
.isNot(AsmToken::Comma
))
1156 return TokError("unexpected token in directive");
1165 /// ParseDirectiveDarwinSymbolDesc
1166 /// ::= .desc identifier , expression
1167 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1169 if (ParseIdentifier(Name
))
1170 return TokError("expected identifier in directive");
1172 // Handle the identifier as the key symbol.
1173 MCSymbol
*Sym
= CreateSymbol(Name
);
1175 if (Lexer
.isNot(AsmToken::Comma
))
1176 return TokError("unexpected token in '.desc' directive");
1179 SMLoc DescLoc
= Lexer
.getLoc();
1181 if (ParseAbsoluteExpression(DescValue
))
1184 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1185 return TokError("unexpected token in '.desc' directive");
1189 // Set the n_desc field of this Symbol to this DescValue
1190 Out
.EmitSymbolDesc(Sym
, DescValue
);
1195 /// ParseDirectiveComm
1196 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1197 bool AsmParser::ParseDirectiveComm(bool IsLocal
) {
1198 SMLoc IDLoc
= Lexer
.getLoc();
1200 if (ParseIdentifier(Name
))
1201 return TokError("expected identifier in directive");
1203 // Handle the identifier as the key symbol.
1204 MCSymbol
*Sym
= CreateSymbol(Name
);
1206 if (Lexer
.isNot(AsmToken::Comma
))
1207 return TokError("unexpected token in directive");
1211 SMLoc SizeLoc
= Lexer
.getLoc();
1212 if (ParseAbsoluteExpression(Size
))
1215 int64_t Pow2Alignment
= 0;
1216 SMLoc Pow2AlignmentLoc
;
1217 if (Lexer
.is(AsmToken::Comma
)) {
1219 Pow2AlignmentLoc
= Lexer
.getLoc();
1220 if (ParseAbsoluteExpression(Pow2Alignment
))
1224 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1225 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1229 // NOTE: a size of zero for a .comm should create a undefined symbol
1230 // but a size of .lcomm creates a bss symbol of size zero.
1232 return Error(SizeLoc
, "invalid '.comm' or '.lcomm' directive size, can't "
1233 "be less than zero");
1235 // NOTE: The alignment in the directive is a power of 2 value, the assember
1236 // may internally end up wanting an alignment in bytes.
1237 // FIXME: Diagnose overflow.
1238 if (Pow2Alignment
< 0)
1239 return Error(Pow2AlignmentLoc
, "invalid '.comm' or '.lcomm' directive "
1240 "alignment, can't be less than zero");
1242 if (!Sym
->isUndefined())
1243 return Error(IDLoc
, "invalid symbol redefinition");
1245 // '.lcomm' is equivalent to '.zerofill'.
1246 // Create the Symbol as a common or local common with Size and Pow2Alignment
1248 Out
.EmitZerofill(getMachOSection("__DATA", "__bss",
1249 MCSectionMachO::S_ZEROFILL
, 0,
1251 Sym
, Size
, 1 << Pow2Alignment
);
1255 Out
.EmitCommonSymbol(Sym
, Size
, 1 << Pow2Alignment
);
1259 /// ParseDirectiveDarwinZerofill
1260 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1261 /// , align_expression ]]
1262 bool AsmParser::ParseDirectiveDarwinZerofill() {
1263 // FIXME: Handle quoted names here.
1265 if (Lexer
.isNot(AsmToken::Identifier
))
1266 return TokError("expected segment name after '.zerofill' directive");
1267 StringRef Segment
= Lexer
.getTok().getString();
1270 if (Lexer
.isNot(AsmToken::Comma
))
1271 return TokError("unexpected token in directive");
1274 if (Lexer
.isNot(AsmToken::Identifier
))
1275 return TokError("expected section name after comma in '.zerofill' "
1277 StringRef Section
= Lexer
.getTok().getString();
1280 // If this is the end of the line all that was wanted was to create the
1281 // the section but with no symbol.
1282 if (Lexer
.is(AsmToken::EndOfStatement
)) {
1283 // Create the zerofill section but no symbol
1284 Out
.EmitZerofill(getMachOSection(Segment
, Section
,
1285 MCSectionMachO::S_ZEROFILL
, 0,
1290 if (Lexer
.isNot(AsmToken::Comma
))
1291 return TokError("unexpected token in directive");
1294 if (Lexer
.isNot(AsmToken::Identifier
))
1295 return TokError("expected identifier in directive");
1297 // handle the identifier as the key symbol.
1298 SMLoc IDLoc
= Lexer
.getLoc();
1299 MCSymbol
*Sym
= CreateSymbol(Lexer
.getTok().getString());
1302 if (Lexer
.isNot(AsmToken::Comma
))
1303 return TokError("unexpected token in directive");
1307 SMLoc SizeLoc
= Lexer
.getLoc();
1308 if (ParseAbsoluteExpression(Size
))
1311 int64_t Pow2Alignment
= 0;
1312 SMLoc Pow2AlignmentLoc
;
1313 if (Lexer
.is(AsmToken::Comma
)) {
1315 Pow2AlignmentLoc
= Lexer
.getLoc();
1316 if (ParseAbsoluteExpression(Pow2Alignment
))
1320 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1321 return TokError("unexpected token in '.zerofill' directive");
1326 return Error(SizeLoc
, "invalid '.zerofill' directive size, can't be less "
1329 // NOTE: The alignment in the directive is a power of 2 value, the assember
1330 // may internally end up wanting an alignment in bytes.
1331 // FIXME: Diagnose overflow.
1332 if (Pow2Alignment
< 0)
1333 return Error(Pow2AlignmentLoc
, "invalid '.zerofill' directive alignment, "
1334 "can't be less than zero");
1336 if (!Sym
->isUndefined())
1337 return Error(IDLoc
, "invalid symbol redefinition");
1339 // Create the zerofill Symbol with Size and Pow2Alignment
1341 // FIXME: Arch specific.
1342 Out
.EmitZerofill(getMachOSection(Segment
, Section
,
1343 MCSectionMachO::S_ZEROFILL
, 0,
1345 Sym
, Size
, 1 << Pow2Alignment
);
1350 /// ParseDirectiveDarwinSubsectionsViaSymbols
1351 /// ::= .subsections_via_symbols
1352 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1353 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1354 return TokError("unexpected token in '.subsections_via_symbols' directive");
1358 Out
.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols
);
1363 /// ParseDirectiveAbort
1364 /// ::= .abort [ "abort_string" ]
1365 bool AsmParser::ParseDirectiveAbort() {
1366 // FIXME: Use loc from directive.
1367 SMLoc Loc
= Lexer
.getLoc();
1370 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1371 if (Lexer
.isNot(AsmToken::String
))
1372 return TokError("expected string in '.abort' directive");
1374 Str
= Lexer
.getTok().getString();
1379 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1380 return TokError("unexpected token in '.abort' directive");
1384 // FIXME: Handle here.
1386 Error(Loc
, ".abort detected. Assembly stopping.");
1388 Error(Loc
, ".abort '" + Str
+ "' detected. Assembly stopping.");
1393 /// ParseDirectiveLsym
1394 /// ::= .lsym identifier , expression
1395 bool AsmParser::ParseDirectiveDarwinLsym() {
1397 if (ParseIdentifier(Name
))
1398 return TokError("expected identifier in directive");
1400 // Handle the identifier as the key symbol.
1401 MCSymbol
*Sym
= CreateSymbol(Name
);
1403 if (Lexer
.isNot(AsmToken::Comma
))
1404 return TokError("unexpected token in '.lsym' directive");
1407 const MCExpr
*Value
;
1408 SMLoc StartLoc
= Lexer
.getLoc();
1409 if (ParseExpression(Value
))
1412 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1413 return TokError("unexpected token in '.lsym' directive");
1417 // We don't currently support this directive.
1419 // FIXME: Diagnostic location!
1421 return TokError("directive '.lsym' is unsupported");
1424 /// ParseDirectiveInclude
1425 /// ::= .include "filename"
1426 bool AsmParser::ParseDirectiveInclude() {
1427 if (Lexer
.isNot(AsmToken::String
))
1428 return TokError("expected string in '.include' directive");
1430 std::string Filename
= Lexer
.getTok().getString();
1431 SMLoc IncludeLoc
= Lexer
.getLoc();
1434 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1435 return TokError("unexpected token in '.include' directive");
1437 // Strip the quotes.
1438 Filename
= Filename
.substr(1, Filename
.size()-2);
1440 // Attempt to switch the lexer to the included file before consuming the end
1441 // of statement to avoid losing it when we switch.
1442 if (Lexer
.EnterIncludeFile(Filename
)) {
1443 Lexer
.PrintMessage(IncludeLoc
,
1444 "Could not find include file '" + Filename
+ "'",
1452 /// ParseDirectiveDarwinDumpOrLoad
1453 /// ::= ( .dump | .load ) "filename"
1454 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc
, bool IsDump
) {
1455 if (Lexer
.isNot(AsmToken::String
))
1456 return TokError("expected string in '.dump' or '.load' directive");
1460 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1461 return TokError("unexpected token in '.dump' or '.load' directive");
1465 // FIXME: If/when .dump and .load are implemented they will be done in the
1466 // the assembly parser and not have any need for an MCStreamer API.
1468 Warning(IDLoc
, "ignoring directive .dump for now");
1470 Warning(IDLoc
, "ignoring directive .load for now");
1475 /// ParseDirectiveIf
1476 /// ::= .if expression
1477 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc
) {
1478 // Consume the identifier that was the .if directive
1481 TheCondStack
.push_back(TheCondState
);
1482 TheCondState
.TheCond
= AsmCond::IfCond
;
1483 if(TheCondState
.Ignore
) {
1484 EatToEndOfStatement();
1488 if (ParseAbsoluteExpression(ExprValue
))
1491 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1492 return TokError("unexpected token in '.if' directive");
1496 TheCondState
.CondMet
= ExprValue
;
1497 TheCondState
.Ignore
= !TheCondState
.CondMet
;
1503 /// ParseDirectiveElseIf
1504 /// ::= .elseif expression
1505 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc
) {
1506 if (TheCondState
.TheCond
!= AsmCond::IfCond
&&
1507 TheCondState
.TheCond
!= AsmCond::ElseIfCond
)
1508 Error(DirectiveLoc
, "Encountered a .elseif that doesn't follow a .if or "
1510 TheCondState
.TheCond
= AsmCond::ElseIfCond
;
1512 // Consume the identifier that was the .elseif directive
1515 bool LastIgnoreState
= false;
1516 if (!TheCondStack
.empty())
1517 LastIgnoreState
= TheCondStack
.back().Ignore
;
1518 if (LastIgnoreState
|| TheCondState
.CondMet
) {
1519 TheCondState
.Ignore
= true;
1520 EatToEndOfStatement();
1524 if (ParseAbsoluteExpression(ExprValue
))
1527 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1528 return TokError("unexpected token in '.elseif' directive");
1531 TheCondState
.CondMet
= ExprValue
;
1532 TheCondState
.Ignore
= !TheCondState
.CondMet
;
1538 /// ParseDirectiveElse
1540 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc
) {
1541 // Consume the identifier that was the .else directive
1544 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1545 return TokError("unexpected token in '.else' directive");
1549 if (TheCondState
.TheCond
!= AsmCond::IfCond
&&
1550 TheCondState
.TheCond
!= AsmCond::ElseIfCond
)
1551 Error(DirectiveLoc
, "Encountered a .else that doesn't follow a .if or an "
1553 TheCondState
.TheCond
= AsmCond::ElseCond
;
1554 bool LastIgnoreState
= false;
1555 if (!TheCondStack
.empty())
1556 LastIgnoreState
= TheCondStack
.back().Ignore
;
1557 if (LastIgnoreState
|| TheCondState
.CondMet
)
1558 TheCondState
.Ignore
= true;
1560 TheCondState
.Ignore
= false;
1565 /// ParseDirectiveEndIf
1567 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc
) {
1568 // Consume the identifier that was the .endif directive
1571 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1572 return TokError("unexpected token in '.endif' directive");
1576 if ((TheCondState
.TheCond
== AsmCond::NoCond
) ||
1577 TheCondStack
.empty())
1578 Error(DirectiveLoc
, "Encountered a .endif that doesn't follow a .if or "
1580 if (!TheCondStack
.empty()) {
1581 TheCondState
= TheCondStack
.back();
1582 TheCondStack
.pop_back();
1588 /// ParseDirectiveFile
1589 /// ::= .file [number] string
1590 bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc
) {
1591 // FIXME: I'm not sure what this is.
1592 int64_t FileNumber
= -1;
1593 if (Lexer
.is(AsmToken::Integer
)) {
1594 FileNumber
= Lexer
.getTok().getIntVal();
1598 return TokError("file number less than one");
1601 if (Lexer
.isNot(AsmToken::String
))
1602 return TokError("unexpected token in '.file' directive");
1604 StringRef FileName
= Lexer
.getTok().getString();
1607 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1608 return TokError("unexpected token in '.file' directive");
1610 // FIXME: Do something with the .file.
1615 /// ParseDirectiveLine
1616 /// ::= .line [number]
1617 bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc
) {
1618 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1619 if (Lexer
.isNot(AsmToken::Integer
))
1620 return TokError("unexpected token in '.line' directive");
1622 int64_t LineNumber
= Lexer
.getTok().getIntVal();
1626 // FIXME: Do something with the .line.
1629 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1630 return TokError("unexpected token in '.file' directive");
1636 /// ParseDirectiveLoc
1637 /// ::= .loc number [number [number]]
1638 bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc
) {
1639 if (Lexer
.isNot(AsmToken::Integer
))
1640 return TokError("unexpected token in '.loc' directive");
1642 // FIXME: What are these fields?
1643 int64_t FileNumber
= Lexer
.getTok().getIntVal();
1645 // FIXME: Validate file.
1648 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1649 if (Lexer
.isNot(AsmToken::Integer
))
1650 return TokError("unexpected token in '.loc' directive");
1652 int64_t Param2
= Lexer
.getTok().getIntVal();
1656 if (Lexer
.isNot(AsmToken::EndOfStatement
)) {
1657 if (Lexer
.isNot(AsmToken::Integer
))
1658 return TokError("unexpected token in '.loc' directive");
1660 int64_t Param3
= Lexer
.getTok().getIntVal();
1664 // FIXME: Do something with the .loc.
1668 if (Lexer
.isNot(AsmToken::EndOfStatement
))
1669 return TokError("unexpected token in '.file' directive");