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 "llvm/MC/MCParser/AsmParser.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/MC/MCParser/MCParsedAsmOperand.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetAsmParser.h"
31 enum { DEFAULT_ADDRSPACE = 0 };
33 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
34 const MCAsmInfo &_MAI)
35 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
37 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
39 // Debugging directives.
40 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
41 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
42 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
47 AsmParser::~AsmParser() {
50 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
51 PrintMessage(L, Msg.str(), "warning");
54 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
55 PrintMessage(L, Msg.str(), "error");
59 bool AsmParser::TokError(const char *Msg) {
60 PrintMessage(Lexer.getLoc(), Msg, "error");
64 void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
65 const char *Type) const {
66 SrcMgr.PrintMessage(Loc, Msg, Type);
69 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
70 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
76 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
81 const AsmToken &AsmParser::Lex() {
82 const AsmToken *tok = &Lexer.Lex();
84 if (tok->is(AsmToken::Eof)) {
85 // If this is the end of an included file, pop the parent file off the
87 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
88 if (ParentIncludeLoc != SMLoc()) {
89 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
90 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
91 ParentIncludeLoc.getPointer());
96 if (tok->is(AsmToken::Error))
97 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
102 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
103 // Create the initial section, if requested.
105 // FIXME: Target hook & command line option for initial section.
106 if (!NoInitialTextSection)
107 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
108 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
109 0, SectionKind::getText()));
114 bool HadError = false;
116 AsmCond StartingCondState = TheCondState;
118 // While we have input, parse each statement.
119 while (Lexer.isNot(AsmToken::Eof)) {
120 if (!ParseStatement()) continue;
122 // We had an error, remember it and recover by skipping to the next line.
124 EatToEndOfStatement();
127 if (TheCondState.TheCond != StartingCondState.TheCond ||
128 TheCondState.Ignore != StartingCondState.Ignore)
129 return TokError("unmatched .ifs or .elses");
131 // Finalize the output stream if there are no errors and if the client wants
133 if (!HadError && !NoFinalize)
139 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
140 void AsmParser::EatToEndOfStatement() {
141 while (Lexer.isNot(AsmToken::EndOfStatement) &&
142 Lexer.isNot(AsmToken::Eof))
146 if (Lexer.is(AsmToken::EndOfStatement))
151 /// ParseParenExpr - Parse a paren expression and return it.
152 /// NOTE: This assumes the leading '(' has already been consumed.
154 /// parenexpr ::= expr)
156 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
157 if (ParseExpression(Res)) return true;
158 if (Lexer.isNot(AsmToken::RParen))
159 return TokError("expected ')' in parentheses expression");
160 EndLoc = Lexer.getLoc();
165 MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
166 // FIXME: Inline into callers.
167 return Ctx.GetOrCreateSymbol(Name);
170 /// ParsePrimaryExpr - Parse a primary expression and return it.
171 /// primaryexpr ::= (parenexpr
172 /// primaryexpr ::= symbol
173 /// primaryexpr ::= number
174 /// primaryexpr ::= '.'
175 /// primaryexpr ::= ~,+,- primaryexpr
176 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
177 switch (Lexer.getKind()) {
179 return TokError("unknown token in expression");
180 case AsmToken::Exclaim:
181 Lex(); // Eat the operator.
182 if (ParsePrimaryExpr(Res, EndLoc))
184 Res = MCUnaryExpr::CreateLNot(Res, getContext());
186 case AsmToken::String:
187 case AsmToken::Identifier: {
188 // This is a symbol reference.
189 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
190 MCSymbol *Sym = CreateSymbol(Split.first);
192 // Lookup the symbol variant if used.
193 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
194 if (Split.first.size() != getTok().getIdentifier().size())
195 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
197 EndLoc = Lexer.getLoc();
198 Lex(); // Eat identifier.
200 // If this is an absolute variable reference, substitute it now to preserve
201 // semantics in the face of reassignment.
202 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
204 return Error(EndLoc, "unexpected modified on variable reference");
206 Res = Sym->getVariableValue();
210 // Otherwise create a symbol ref.
211 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
214 case AsmToken::Integer:
215 Res = MCConstantExpr::Create(getTok().getIntVal(), getContext());
216 EndLoc = Lexer.getLoc();
219 case AsmToken::Dot: {
220 // This is a '.' reference, which references the current PC. Emit a
221 // temporary label to the streamer and refer to it.
222 MCSymbol *Sym = Ctx.CreateTempSymbol();
224 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
225 EndLoc = Lexer.getLoc();
226 Lex(); // Eat identifier.
230 case AsmToken::LParen:
231 Lex(); // Eat the '('.
232 return ParseParenExpr(Res, EndLoc);
233 case AsmToken::Minus:
234 Lex(); // Eat the operator.
235 if (ParsePrimaryExpr(Res, EndLoc))
237 Res = MCUnaryExpr::CreateMinus(Res, getContext());
240 Lex(); // Eat the operator.
241 if (ParsePrimaryExpr(Res, EndLoc))
243 Res = MCUnaryExpr::CreatePlus(Res, getContext());
245 case AsmToken::Tilde:
246 Lex(); // Eat the operator.
247 if (ParsePrimaryExpr(Res, EndLoc))
249 Res = MCUnaryExpr::CreateNot(Res, getContext());
254 bool AsmParser::ParseExpression(const MCExpr *&Res) {
256 return ParseExpression(Res, EndLoc);
259 /// ParseExpression - Parse an expression and return it.
261 /// expr ::= expr +,- expr -> lowest.
262 /// expr ::= expr |,^,&,! expr -> middle.
263 /// expr ::= expr *,/,%,<<,>> expr -> highest.
264 /// expr ::= primaryexpr
266 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
267 // Parse the expression.
269 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
272 // Try to constant fold it up front, if possible.
274 if (Res->EvaluateAsAbsolute(Value))
275 Res = MCConstantExpr::Create(Value, getContext());
280 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
282 return ParseParenExpr(Res, EndLoc) ||
283 ParseBinOpRHS(1, Res, EndLoc);
286 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
289 SMLoc StartLoc = Lexer.getLoc();
290 if (ParseExpression(Expr))
293 if (!Expr->EvaluateAsAbsolute(Res))
294 return Error(StartLoc, "expected absolute expression");
299 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
300 MCBinaryExpr::Opcode &Kind) {
303 return 0; // not a binop.
305 // Lowest Precedence: &&, ||
306 case AsmToken::AmpAmp:
307 Kind = MCBinaryExpr::LAnd;
309 case AsmToken::PipePipe:
310 Kind = MCBinaryExpr::LOr;
313 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
315 Kind = MCBinaryExpr::Add;
317 case AsmToken::Minus:
318 Kind = MCBinaryExpr::Sub;
320 case AsmToken::EqualEqual:
321 Kind = MCBinaryExpr::EQ;
323 case AsmToken::ExclaimEqual:
324 case AsmToken::LessGreater:
325 Kind = MCBinaryExpr::NE;
328 Kind = MCBinaryExpr::LT;
330 case AsmToken::LessEqual:
331 Kind = MCBinaryExpr::LTE;
333 case AsmToken::Greater:
334 Kind = MCBinaryExpr::GT;
336 case AsmToken::GreaterEqual:
337 Kind = MCBinaryExpr::GTE;
340 // Intermediate Precedence: |, &, ^
342 // FIXME: gas seems to support '!' as an infix operator?
344 Kind = MCBinaryExpr::Or;
346 case AsmToken::Caret:
347 Kind = MCBinaryExpr::Xor;
350 Kind = MCBinaryExpr::And;
353 // Highest Precedence: *, /, %, <<, >>
355 Kind = MCBinaryExpr::Mul;
357 case AsmToken::Slash:
358 Kind = MCBinaryExpr::Div;
360 case AsmToken::Percent:
361 Kind = MCBinaryExpr::Mod;
363 case AsmToken::LessLess:
364 Kind = MCBinaryExpr::Shl;
366 case AsmToken::GreaterGreater:
367 Kind = MCBinaryExpr::Shr;
373 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
374 /// Res contains the LHS of the expression on input.
375 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
378 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
379 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
381 // If the next token is lower precedence than we are allowed to eat, return
382 // successfully with what we ate already.
383 if (TokPrec < Precedence)
388 // Eat the next primary expression.
390 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
392 // If BinOp binds less tightly with RHS than the operator after RHS, let
393 // the pending operator take RHS as its LHS.
394 MCBinaryExpr::Opcode Dummy;
395 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
396 if (TokPrec < NextTokPrec) {
397 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
400 // Merge LHS and RHS according to operator.
401 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
409 /// ::= EndOfStatement
410 /// ::= Label* Directive ...Operands... EndOfStatement
411 /// ::= Label* Identifier OperandList* EndOfStatement
412 bool AsmParser::ParseStatement() {
413 if (Lexer.is(AsmToken::EndOfStatement)) {
418 // Statements always start with an identifier.
419 AsmToken ID = getTok();
420 SMLoc IDLoc = ID.getLoc();
422 if (ParseIdentifier(IDVal)) {
423 if (!TheCondState.Ignore)
424 return TokError("unexpected token at start of statement");
428 // Handle conditional assembly here before checking for skipping. We
429 // have to do this so that .endif isn't skipped in a ".if 0" block for
432 return ParseDirectiveIf(IDLoc);
433 if (IDVal == ".elseif")
434 return ParseDirectiveElseIf(IDLoc);
435 if (IDVal == ".else")
436 return ParseDirectiveElse(IDLoc);
437 if (IDVal == ".endif")
438 return ParseDirectiveEndIf(IDLoc);
440 // If we are in a ".if 0" block, ignore this statement.
441 if (TheCondState.Ignore) {
442 EatToEndOfStatement();
446 // FIXME: Recurse on local labels?
448 // See what kind of statement we have.
449 switch (Lexer.getKind()) {
450 case AsmToken::Colon: {
451 // identifier ':' -> Label.
454 // Diagnose attempt to use a variable as a label.
456 // FIXME: Diagnostics. Note the location of the definition as a label.
457 // FIXME: This doesn't diagnose assignment to a symbol which has been
458 // implicitly marked as external.
459 MCSymbol *Sym = CreateSymbol(IDVal);
460 if (!Sym->isUndefined())
461 return Error(IDLoc, "invalid symbol redefinition");
466 return ParseStatement();
469 case AsmToken::Equal:
470 // identifier '=' ... -> assignment statement
473 return ParseAssignment(IDVal);
475 default: // Normal instruction or directive.
479 // Otherwise, we have a normal instruction or directive.
480 if (IDVal[0] == '.') {
481 // FIXME: This should be driven based on a hash lookup and callback.
482 if (IDVal == ".section")
483 return ParseDirectiveDarwinSection();
484 if (IDVal == ".text")
485 // FIXME: This changes behavior based on the -static flag to the
487 return ParseDirectiveSectionSwitch("__TEXT", "__text",
488 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
489 if (IDVal == ".const")
490 return ParseDirectiveSectionSwitch("__TEXT", "__const");
491 if (IDVal == ".static_const")
492 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
493 if (IDVal == ".cstring")
494 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
495 MCSectionMachO::S_CSTRING_LITERALS);
496 if (IDVal == ".literal4")
497 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
498 MCSectionMachO::S_4BYTE_LITERALS,
500 if (IDVal == ".literal8")
501 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
502 MCSectionMachO::S_8BYTE_LITERALS,
504 if (IDVal == ".literal16")
505 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
506 MCSectionMachO::S_16BYTE_LITERALS,
508 if (IDVal == ".constructor")
509 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
510 if (IDVal == ".destructor")
511 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
512 if (IDVal == ".fvmlib_init0")
513 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
514 if (IDVal == ".fvmlib_init1")
515 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
517 // FIXME: The assembler manual claims that this has the self modify code
518 // flag, at least on x86-32, but that does not appear to be correct.
519 if (IDVal == ".symbol_stub")
520 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
521 MCSectionMachO::S_SYMBOL_STUBS |
522 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
523 // FIXME: Different on PPC and ARM.
525 // FIXME: PowerPC only?
526 if (IDVal == ".picsymbol_stub")
527 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
528 MCSectionMachO::S_SYMBOL_STUBS |
529 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
531 if (IDVal == ".data")
532 return ParseDirectiveSectionSwitch("__DATA", "__data");
533 if (IDVal == ".static_data")
534 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
536 // FIXME: The section names of these two are misspelled in the assembler
538 if (IDVal == ".non_lazy_symbol_pointer")
539 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
540 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
542 if (IDVal == ".lazy_symbol_pointer")
543 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
544 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
547 if (IDVal == ".dyld")
548 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
549 if (IDVal == ".mod_init_func")
550 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
551 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
553 if (IDVal == ".mod_term_func")
554 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
555 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
557 if (IDVal == ".const_data")
558 return ParseDirectiveSectionSwitch("__DATA", "__const");
561 if (IDVal == ".objc_class")
562 return ParseDirectiveSectionSwitch("__OBJC", "__class",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
564 if (IDVal == ".objc_meta_class")
565 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
567 if (IDVal == ".objc_cat_cls_meth")
568 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
570 if (IDVal == ".objc_cat_inst_meth")
571 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
573 if (IDVal == ".objc_protocol")
574 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
576 if (IDVal == ".objc_string_object")
577 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
579 if (IDVal == ".objc_cls_meth")
580 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
582 if (IDVal == ".objc_inst_meth")
583 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
585 if (IDVal == ".objc_cls_refs")
586 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
588 MCSectionMachO::S_LITERAL_POINTERS,
590 if (IDVal == ".objc_message_refs")
591 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
592 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
593 MCSectionMachO::S_LITERAL_POINTERS,
595 if (IDVal == ".objc_symbols")
596 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
597 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
598 if (IDVal == ".objc_category")
599 return ParseDirectiveSectionSwitch("__OBJC", "__category",
600 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
601 if (IDVal == ".objc_class_vars")
602 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
603 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
604 if (IDVal == ".objc_instance_vars")
605 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
606 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
607 if (IDVal == ".objc_module_info")
608 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
610 if (IDVal == ".objc_class_names")
611 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
612 MCSectionMachO::S_CSTRING_LITERALS);
613 if (IDVal == ".objc_meth_var_types")
614 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
615 MCSectionMachO::S_CSTRING_LITERALS);
616 if (IDVal == ".objc_meth_var_names")
617 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
618 MCSectionMachO::S_CSTRING_LITERALS);
619 if (IDVal == ".objc_selector_strs")
620 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
621 MCSectionMachO::S_CSTRING_LITERALS);
623 // Assembler features
625 return ParseDirectiveSet();
629 if (IDVal == ".ascii")
630 return ParseDirectiveAscii(false);
631 if (IDVal == ".asciz")
632 return ParseDirectiveAscii(true);
634 if (IDVal == ".byte")
635 return ParseDirectiveValue(1);
636 if (IDVal == ".short")
637 return ParseDirectiveValue(2);
638 if (IDVal == ".long")
639 return ParseDirectiveValue(4);
640 if (IDVal == ".quad")
641 return ParseDirectiveValue(8);
643 // FIXME: Target hooks for IsPow2.
644 if (IDVal == ".align")
645 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
646 if (IDVal == ".align32")
647 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
648 if (IDVal == ".balign")
649 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
650 if (IDVal == ".balignw")
651 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
652 if (IDVal == ".balignl")
653 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
654 if (IDVal == ".p2align")
655 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
656 if (IDVal == ".p2alignw")
657 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
658 if (IDVal == ".p2alignl")
659 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
662 return ParseDirectiveOrg();
664 if (IDVal == ".fill")
665 return ParseDirectiveFill();
666 if (IDVal == ".space")
667 return ParseDirectiveSpace();
669 // Symbol attribute directives
671 if (IDVal == ".globl" || IDVal == ".global")
672 return ParseDirectiveSymbolAttribute(MCSA_Global);
673 if (IDVal == ".hidden")
674 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
675 if (IDVal == ".indirect_symbol")
676 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
677 if (IDVal == ".internal")
678 return ParseDirectiveSymbolAttribute(MCSA_Internal);
679 if (IDVal == ".lazy_reference")
680 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
681 if (IDVal == ".no_dead_strip")
682 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
683 if (IDVal == ".private_extern")
684 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
685 if (IDVal == ".protected")
686 return ParseDirectiveSymbolAttribute(MCSA_Protected);
687 if (IDVal == ".reference")
688 return ParseDirectiveSymbolAttribute(MCSA_Reference);
689 if (IDVal == ".weak")
690 return ParseDirectiveSymbolAttribute(MCSA_Weak);
691 if (IDVal == ".weak_definition")
692 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
693 if (IDVal == ".weak_reference")
694 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
696 if (IDVal == ".comm")
697 return ParseDirectiveComm(/*IsLocal=*/false);
698 if (IDVal == ".lcomm")
699 return ParseDirectiveComm(/*IsLocal=*/true);
700 if (IDVal == ".zerofill")
701 return ParseDirectiveDarwinZerofill();
702 if (IDVal == ".desc")
703 return ParseDirectiveDarwinSymbolDesc();
704 if (IDVal == ".lsym")
705 return ParseDirectiveDarwinLsym();
707 if (IDVal == ".subsections_via_symbols")
708 return ParseDirectiveDarwinSubsectionsViaSymbols();
709 if (IDVal == ".abort")
710 return ParseDirectiveAbort();
711 if (IDVal == ".include")
712 return ParseDirectiveInclude();
713 if (IDVal == ".dump")
714 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
715 if (IDVal == ".load")
716 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
718 // Look up the handler in the handler table,
719 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
721 return (this->*Handler)(IDVal, IDLoc);
723 // Target hook for parsing target specific directives.
724 if (!getTargetParser().ParseDirective(ID))
727 Warning(IDLoc, "ignoring directive for now");
728 EatToEndOfStatement();
732 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
733 bool HadError = getTargetParser().ParseInstruction(IDVal, IDLoc,
735 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
736 HadError = TokError("unexpected token in argument list");
738 // If parsing succeeded, match the instruction.
741 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
742 // Emit the instruction on success.
743 Out.EmitInstruction(Inst);
745 // Otherwise emit a diagnostic about the match failure and set the error
748 // FIXME: We should give nicer diagnostics about the exact failure.
749 Error(IDLoc, "unrecognized instruction");
754 // If there was no error, consume the end-of-statement token. Otherwise this
755 // will be done by our caller.
759 // Free any parsed operands.
760 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
761 delete ParsedOperands[i];
766 bool AsmParser::ParseAssignment(const StringRef &Name) {
767 // FIXME: Use better location, we should use proper tokens.
768 SMLoc EqualLoc = Lexer.getLoc();
771 SMLoc StartLoc = Lexer.getLoc();
772 if (ParseExpression(Value))
775 if (Lexer.isNot(AsmToken::EndOfStatement))
776 return TokError("unexpected token in assignment");
778 // Eat the end of statement marker.
781 // Validate that the LHS is allowed to be a variable (either it has not been
782 // used as a symbol, or it is an absolute symbol).
783 MCSymbol *Sym = getContext().LookupSymbol(Name);
785 // Diagnose assignment to a label.
787 // FIXME: Diagnostics. Note the location of the definition as a label.
788 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
789 if (!Sym->isUndefined() && !Sym->isAbsolute())
790 return Error(EqualLoc, "redefinition of '" + Name + "'");
791 else if (!Sym->isVariable())
792 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
793 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
794 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
797 Sym = CreateSymbol(Name);
799 // FIXME: Handle '.'.
801 // Do the assignment.
802 Out.EmitAssignment(Sym, Value);
810 bool AsmParser::ParseIdentifier(StringRef &Res) {
811 if (Lexer.isNot(AsmToken::Identifier) &&
812 Lexer.isNot(AsmToken::String))
815 Res = getTok().getIdentifier();
817 Lex(); // Consume the identifier token.
822 /// ParseDirectiveSet:
823 /// ::= .set identifier ',' expression
824 bool AsmParser::ParseDirectiveSet() {
827 if (ParseIdentifier(Name))
828 return TokError("expected identifier after '.set' directive");
830 if (Lexer.isNot(AsmToken::Comma))
831 return TokError("unexpected token in '.set'");
834 return ParseAssignment(Name);
837 /// ParseDirectiveSection:
838 /// ::= .section identifier (',' identifier)*
839 /// FIXME: This should actually parse out the segment, section, attributes and
840 /// sizeof_stub fields.
841 bool AsmParser::ParseDirectiveDarwinSection() {
842 SMLoc Loc = Lexer.getLoc();
844 StringRef SectionName;
845 if (ParseIdentifier(SectionName))
846 return Error(Loc, "expected identifier after '.section' directive");
848 // Verify there is a following comma.
849 if (!Lexer.is(AsmToken::Comma))
850 return TokError("unexpected token in '.section' directive");
852 std::string SectionSpec = SectionName;
855 // Add all the tokens until the end of the line, ParseSectionSpecifier will
857 StringRef EOL = Lexer.LexUntilEndOfStatement();
858 SectionSpec.append(EOL.begin(), EOL.end());
861 if (Lexer.isNot(AsmToken::EndOfStatement))
862 return TokError("unexpected token in '.section' directive");
866 StringRef Segment, Section;
867 unsigned TAA, StubSize;
868 std::string ErrorStr =
869 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
872 if (!ErrorStr.empty())
873 return Error(Loc, ErrorStr.c_str());
875 // FIXME: Arch specific.
876 bool isText = Segment == "__TEXT"; // FIXME: Hack.
877 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
878 isText ? SectionKind::getText()
879 : SectionKind::getDataRel()));
883 /// ParseDirectiveSectionSwitch -
884 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
886 unsigned TAA, unsigned Align,
888 if (Lexer.isNot(AsmToken::EndOfStatement))
889 return TokError("unexpected token in section switching directive");
892 // FIXME: Arch specific.
893 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
894 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
895 isText ? SectionKind::getText()
896 : SectionKind::getDataRel()));
898 // Set the implicit alignment, if any.
900 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
901 // alignment on the section (e.g., if one manually inserts bytes into the
902 // section, then just issueing the section switch directive will not realign
903 // the section. However, this is arguably more reasonable behavior, and there
904 // is no good reason for someone to intentionally emit incorrectly sized
905 // values into the implicitly aligned sections.
907 Out.EmitValueToAlignment(Align, 0, 1, 0);
912 bool AsmParser::ParseEscapedString(std::string &Data) {
913 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
916 StringRef Str = getTok().getStringContents();
917 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
918 if (Str[i] != '\\') {
923 // Recognize escaped characters. Note that this escape semantics currently
924 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
927 return TokError("unexpected backslash at end of string");
929 // Recognize octal sequences.
930 if ((unsigned) (Str[i] - '0') <= 7) {
931 // Consume up to three octal characters.
932 unsigned Value = Str[i] - '0';
934 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
936 Value = Value * 8 + (Str[i] - '0');
938 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
940 Value = Value * 8 + (Str[i] - '0');
945 return TokError("invalid octal escape sequence (out of range)");
947 Data += (unsigned char) Value;
951 // Otherwise recognize individual escapes.
954 // Just reject invalid escape sequences for now.
955 return TokError("invalid escape sequence (unrecognized character)");
957 case 'b': Data += '\b'; break;
958 case 'f': Data += '\f'; break;
959 case 'n': Data += '\n'; break;
960 case 'r': Data += '\r'; break;
961 case 't': Data += '\t'; break;
962 case '"': Data += '"'; break;
963 case '\\': Data += '\\'; break;
970 /// ParseDirectiveAscii:
971 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
972 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
973 if (Lexer.isNot(AsmToken::EndOfStatement)) {
975 if (Lexer.isNot(AsmToken::String))
976 return TokError("expected string in '.ascii' or '.asciz' directive");
979 if (ParseEscapedString(Data))
982 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
984 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
988 if (Lexer.is(AsmToken::EndOfStatement))
991 if (Lexer.isNot(AsmToken::Comma))
992 return TokError("unexpected token in '.ascii' or '.asciz' directive");
1001 /// ParseDirectiveValue
1002 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1003 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1004 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1006 const MCExpr *Value;
1007 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
1008 if (ParseExpression(Value))
1011 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1013 if (Lexer.is(AsmToken::EndOfStatement))
1016 // FIXME: Improve diagnostic.
1017 if (Lexer.isNot(AsmToken::Comma))
1018 return TokError("unexpected token in directive");
1027 /// ParseDirectiveSpace
1028 /// ::= .space expression [ , expression ]
1029 bool AsmParser::ParseDirectiveSpace() {
1031 if (ParseAbsoluteExpression(NumBytes))
1034 int64_t FillExpr = 0;
1035 bool HasFillExpr = false;
1036 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1037 if (Lexer.isNot(AsmToken::Comma))
1038 return TokError("unexpected token in '.space' directive");
1041 if (ParseAbsoluteExpression(FillExpr))
1046 if (Lexer.isNot(AsmToken::EndOfStatement))
1047 return TokError("unexpected token in '.space' directive");
1053 return TokError("invalid number of bytes in '.space' directive");
1055 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1056 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1061 /// ParseDirectiveFill
1062 /// ::= .fill expression , expression , expression
1063 bool AsmParser::ParseDirectiveFill() {
1065 if (ParseAbsoluteExpression(NumValues))
1068 if (Lexer.isNot(AsmToken::Comma))
1069 return TokError("unexpected token in '.fill' directive");
1073 if (ParseAbsoluteExpression(FillSize))
1076 if (Lexer.isNot(AsmToken::Comma))
1077 return TokError("unexpected token in '.fill' directive");
1081 if (ParseAbsoluteExpression(FillExpr))
1084 if (Lexer.isNot(AsmToken::EndOfStatement))
1085 return TokError("unexpected token in '.fill' directive");
1089 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1090 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1092 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1093 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize,
1099 /// ParseDirectiveOrg
1100 /// ::= .org expression [ , expression ]
1101 bool AsmParser::ParseDirectiveOrg() {
1102 const MCExpr *Offset;
1103 SMLoc StartLoc = Lexer.getLoc();
1104 if (ParseExpression(Offset))
1107 // Parse optional fill expression.
1108 int64_t FillExpr = 0;
1109 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1110 if (Lexer.isNot(AsmToken::Comma))
1111 return TokError("unexpected token in '.org' directive");
1114 if (ParseAbsoluteExpression(FillExpr))
1117 if (Lexer.isNot(AsmToken::EndOfStatement))
1118 return TokError("unexpected token in '.org' directive");
1123 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1124 // has to be relative to the current section.
1125 Out.EmitValueToOffset(Offset, FillExpr);
1130 /// ParseDirectiveAlign
1131 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1132 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1133 SMLoc AlignmentLoc = Lexer.getLoc();
1135 if (ParseAbsoluteExpression(Alignment))
1139 bool HasFillExpr = false;
1140 int64_t FillExpr = 0;
1141 int64_t MaxBytesToFill = 0;
1142 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1143 if (Lexer.isNot(AsmToken::Comma))
1144 return TokError("unexpected token in directive");
1147 // The fill expression can be omitted while specifying a maximum number of
1148 // alignment bytes, e.g:
1150 if (Lexer.isNot(AsmToken::Comma)) {
1152 if (ParseAbsoluteExpression(FillExpr))
1156 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1157 if (Lexer.isNot(AsmToken::Comma))
1158 return TokError("unexpected token in directive");
1161 MaxBytesLoc = Lexer.getLoc();
1162 if (ParseAbsoluteExpression(MaxBytesToFill))
1165 if (Lexer.isNot(AsmToken::EndOfStatement))
1166 return TokError("unexpected token in directive");
1173 // FIXME: Sometimes fill with nop.
1177 // Compute alignment in bytes.
1179 // FIXME: Diagnose overflow.
1180 if (Alignment >= 32) {
1181 Error(AlignmentLoc, "invalid alignment value");
1185 Alignment = 1ULL << Alignment;
1188 // Diagnose non-sensical max bytes to align.
1189 if (MaxBytesLoc.isValid()) {
1190 if (MaxBytesToFill < 1) {
1191 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1192 "many bytes, ignoring maximum bytes expression");
1196 if (MaxBytesToFill >= Alignment) {
1197 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1203 // FIXME: hard code the parser to use EmitCodeAlignment for text when using
1204 // the TextAlignFillValue.
1205 if(Out.getCurrentSection()->getKind().isText() &&
1206 Lexer.getMAI().getTextAlignFillValue() == FillExpr)
1207 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1209 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1210 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1215 /// ParseDirectiveSymbolAttribute
1216 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1217 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1218 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1222 if (ParseIdentifier(Name))
1223 return TokError("expected identifier in directive");
1225 MCSymbol *Sym = CreateSymbol(Name);
1227 Out.EmitSymbolAttribute(Sym, Attr);
1229 if (Lexer.is(AsmToken::EndOfStatement))
1232 if (Lexer.isNot(AsmToken::Comma))
1233 return TokError("unexpected token in directive");
1242 /// ParseDirectiveDarwinSymbolDesc
1243 /// ::= .desc identifier , expression
1244 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1246 if (ParseIdentifier(Name))
1247 return TokError("expected identifier in directive");
1249 // Handle the identifier as the key symbol.
1250 MCSymbol *Sym = CreateSymbol(Name);
1252 if (Lexer.isNot(AsmToken::Comma))
1253 return TokError("unexpected token in '.desc' directive");
1256 SMLoc DescLoc = Lexer.getLoc();
1258 if (ParseAbsoluteExpression(DescValue))
1261 if (Lexer.isNot(AsmToken::EndOfStatement))
1262 return TokError("unexpected token in '.desc' directive");
1266 // Set the n_desc field of this Symbol to this DescValue
1267 Out.EmitSymbolDesc(Sym, DescValue);
1272 /// ParseDirectiveComm
1273 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1274 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1275 SMLoc IDLoc = Lexer.getLoc();
1277 if (ParseIdentifier(Name))
1278 return TokError("expected identifier in directive");
1280 // Handle the identifier as the key symbol.
1281 MCSymbol *Sym = CreateSymbol(Name);
1283 if (Lexer.isNot(AsmToken::Comma))
1284 return TokError("unexpected token in directive");
1288 SMLoc SizeLoc = Lexer.getLoc();
1289 if (ParseAbsoluteExpression(Size))
1292 int64_t Pow2Alignment = 0;
1293 SMLoc Pow2AlignmentLoc;
1294 if (Lexer.is(AsmToken::Comma)) {
1296 Pow2AlignmentLoc = Lexer.getLoc();
1297 if (ParseAbsoluteExpression(Pow2Alignment))
1300 // If this target takes alignments in bytes (not log) validate and convert.
1301 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1302 if (!isPowerOf2_64(Pow2Alignment))
1303 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1304 Pow2Alignment = Log2_64(Pow2Alignment);
1308 if (Lexer.isNot(AsmToken::EndOfStatement))
1309 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1313 // NOTE: a size of zero for a .comm should create a undefined symbol
1314 // but a size of .lcomm creates a bss symbol of size zero.
1316 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1317 "be less than zero");
1319 // NOTE: The alignment in the directive is a power of 2 value, the assember
1320 // may internally end up wanting an alignment in bytes.
1321 // FIXME: Diagnose overflow.
1322 if (Pow2Alignment < 0)
1323 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1324 "alignment, can't be less than zero");
1326 if (!Sym->isUndefined())
1327 return Error(IDLoc, "invalid symbol redefinition");
1329 // '.lcomm' is equivalent to '.zerofill'.
1330 // Create the Symbol as a common or local common with Size and Pow2Alignment
1332 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1333 MCSectionMachO::S_ZEROFILL, 0,
1334 SectionKind::getBSS()),
1335 Sym, Size, 1 << Pow2Alignment);
1339 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1343 /// ParseDirectiveDarwinZerofill
1344 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1345 /// , align_expression ]]
1346 bool AsmParser::ParseDirectiveDarwinZerofill() {
1347 // FIXME: Handle quoted names here.
1349 if (Lexer.isNot(AsmToken::Identifier))
1350 return TokError("expected segment name after '.zerofill' directive");
1351 StringRef Segment = getTok().getString();
1354 if (Lexer.isNot(AsmToken::Comma))
1355 return TokError("unexpected token in directive");
1358 if (Lexer.isNot(AsmToken::Identifier))
1359 return TokError("expected section name after comma in '.zerofill' "
1361 StringRef Section = getTok().getString();
1364 // If this is the end of the line all that was wanted was to create the
1365 // the section but with no symbol.
1366 if (Lexer.is(AsmToken::EndOfStatement)) {
1367 // Create the zerofill section but no symbol
1368 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1369 MCSectionMachO::S_ZEROFILL, 0,
1370 SectionKind::getBSS()));
1374 if (Lexer.isNot(AsmToken::Comma))
1375 return TokError("unexpected token in directive");
1378 if (Lexer.isNot(AsmToken::Identifier))
1379 return TokError("expected identifier in directive");
1381 // handle the identifier as the key symbol.
1382 SMLoc IDLoc = Lexer.getLoc();
1383 MCSymbol *Sym = CreateSymbol(getTok().getString());
1386 if (Lexer.isNot(AsmToken::Comma))
1387 return TokError("unexpected token in directive");
1391 SMLoc SizeLoc = Lexer.getLoc();
1392 if (ParseAbsoluteExpression(Size))
1395 int64_t Pow2Alignment = 0;
1396 SMLoc Pow2AlignmentLoc;
1397 if (Lexer.is(AsmToken::Comma)) {
1399 Pow2AlignmentLoc = Lexer.getLoc();
1400 if (ParseAbsoluteExpression(Pow2Alignment))
1404 if (Lexer.isNot(AsmToken::EndOfStatement))
1405 return TokError("unexpected token in '.zerofill' directive");
1410 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1413 // NOTE: The alignment in the directive is a power of 2 value, the assember
1414 // may internally end up wanting an alignment in bytes.
1415 // FIXME: Diagnose overflow.
1416 if (Pow2Alignment < 0)
1417 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1418 "can't be less than zero");
1420 if (!Sym->isUndefined())
1421 return Error(IDLoc, "invalid symbol redefinition");
1423 // Create the zerofill Symbol with Size and Pow2Alignment
1425 // FIXME: Arch specific.
1426 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1427 MCSectionMachO::S_ZEROFILL, 0,
1428 SectionKind::getBSS()),
1429 Sym, Size, 1 << Pow2Alignment);
1434 /// ParseDirectiveDarwinSubsectionsViaSymbols
1435 /// ::= .subsections_via_symbols
1436 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1437 if (Lexer.isNot(AsmToken::EndOfStatement))
1438 return TokError("unexpected token in '.subsections_via_symbols' directive");
1442 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1447 /// ParseDirectiveAbort
1448 /// ::= .abort [ "abort_string" ]
1449 bool AsmParser::ParseDirectiveAbort() {
1450 // FIXME: Use loc from directive.
1451 SMLoc Loc = Lexer.getLoc();
1454 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1455 if (Lexer.isNot(AsmToken::String))
1456 return TokError("expected string in '.abort' directive");
1458 Str = getTok().getString();
1463 if (Lexer.isNot(AsmToken::EndOfStatement))
1464 return TokError("unexpected token in '.abort' directive");
1468 // FIXME: Handle here.
1470 Error(Loc, ".abort detected. Assembly stopping.");
1472 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1477 /// ParseDirectiveLsym
1478 /// ::= .lsym identifier , expression
1479 bool AsmParser::ParseDirectiveDarwinLsym() {
1481 if (ParseIdentifier(Name))
1482 return TokError("expected identifier in directive");
1484 // Handle the identifier as the key symbol.
1485 MCSymbol *Sym = CreateSymbol(Name);
1487 if (Lexer.isNot(AsmToken::Comma))
1488 return TokError("unexpected token in '.lsym' directive");
1491 const MCExpr *Value;
1492 SMLoc StartLoc = Lexer.getLoc();
1493 if (ParseExpression(Value))
1496 if (Lexer.isNot(AsmToken::EndOfStatement))
1497 return TokError("unexpected token in '.lsym' directive");
1501 // We don't currently support this directive.
1503 // FIXME: Diagnostic location!
1505 return TokError("directive '.lsym' is unsupported");
1508 /// ParseDirectiveInclude
1509 /// ::= .include "filename"
1510 bool AsmParser::ParseDirectiveInclude() {
1511 if (Lexer.isNot(AsmToken::String))
1512 return TokError("expected string in '.include' directive");
1514 std::string Filename = getTok().getString();
1515 SMLoc IncludeLoc = Lexer.getLoc();
1518 if (Lexer.isNot(AsmToken::EndOfStatement))
1519 return TokError("unexpected token in '.include' directive");
1521 // Strip the quotes.
1522 Filename = Filename.substr(1, Filename.size()-2);
1524 // Attempt to switch the lexer to the included file before consuming the end
1525 // of statement to avoid losing it when we switch.
1526 if (EnterIncludeFile(Filename)) {
1527 PrintMessage(IncludeLoc,
1528 "Could not find include file '" + Filename + "'",
1536 /// ParseDirectiveDarwinDumpOrLoad
1537 /// ::= ( .dump | .load ) "filename"
1538 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1539 if (Lexer.isNot(AsmToken::String))
1540 return TokError("expected string in '.dump' or '.load' directive");
1544 if (Lexer.isNot(AsmToken::EndOfStatement))
1545 return TokError("unexpected token in '.dump' or '.load' directive");
1549 // FIXME: If/when .dump and .load are implemented they will be done in the
1550 // the assembly parser and not have any need for an MCStreamer API.
1552 Warning(IDLoc, "ignoring directive .dump for now");
1554 Warning(IDLoc, "ignoring directive .load for now");
1559 /// ParseDirectiveIf
1560 /// ::= .if expression
1561 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1562 TheCondStack.push_back(TheCondState);
1563 TheCondState.TheCond = AsmCond::IfCond;
1564 if(TheCondState.Ignore) {
1565 EatToEndOfStatement();
1569 if (ParseAbsoluteExpression(ExprValue))
1572 if (Lexer.isNot(AsmToken::EndOfStatement))
1573 return TokError("unexpected token in '.if' directive");
1577 TheCondState.CondMet = ExprValue;
1578 TheCondState.Ignore = !TheCondState.CondMet;
1584 /// ParseDirectiveElseIf
1585 /// ::= .elseif expression
1586 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1587 if (TheCondState.TheCond != AsmCond::IfCond &&
1588 TheCondState.TheCond != AsmCond::ElseIfCond)
1589 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1591 TheCondState.TheCond = AsmCond::ElseIfCond;
1593 bool LastIgnoreState = false;
1594 if (!TheCondStack.empty())
1595 LastIgnoreState = TheCondStack.back().Ignore;
1596 if (LastIgnoreState || TheCondState.CondMet) {
1597 TheCondState.Ignore = true;
1598 EatToEndOfStatement();
1602 if (ParseAbsoluteExpression(ExprValue))
1605 if (Lexer.isNot(AsmToken::EndOfStatement))
1606 return TokError("unexpected token in '.elseif' directive");
1609 TheCondState.CondMet = ExprValue;
1610 TheCondState.Ignore = !TheCondState.CondMet;
1616 /// ParseDirectiveElse
1618 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1619 if (Lexer.isNot(AsmToken::EndOfStatement))
1620 return TokError("unexpected token in '.else' directive");
1624 if (TheCondState.TheCond != AsmCond::IfCond &&
1625 TheCondState.TheCond != AsmCond::ElseIfCond)
1626 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1628 TheCondState.TheCond = AsmCond::ElseCond;
1629 bool LastIgnoreState = false;
1630 if (!TheCondStack.empty())
1631 LastIgnoreState = TheCondStack.back().Ignore;
1632 if (LastIgnoreState || TheCondState.CondMet)
1633 TheCondState.Ignore = true;
1635 TheCondState.Ignore = false;
1640 /// ParseDirectiveEndIf
1642 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1643 if (Lexer.isNot(AsmToken::EndOfStatement))
1644 return TokError("unexpected token in '.endif' directive");
1648 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1649 TheCondStack.empty())
1650 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1652 if (!TheCondStack.empty()) {
1653 TheCondState = TheCondStack.back();
1654 TheCondStack.pop_back();
1660 /// ParseDirectiveFile
1661 /// ::= .file [number] string
1662 bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
1663 // FIXME: I'm not sure what this is.
1664 int64_t FileNumber = -1;
1665 if (Lexer.is(AsmToken::Integer)) {
1666 FileNumber = getTok().getIntVal();
1670 return TokError("file number less than one");
1673 if (Lexer.isNot(AsmToken::String))
1674 return TokError("unexpected token in '.file' directive");
1676 StringRef Filename = getTok().getString();
1677 Filename = Filename.substr(1, Filename.size()-2);
1680 if (Lexer.isNot(AsmToken::EndOfStatement))
1681 return TokError("unexpected token in '.file' directive");
1683 if (FileNumber == -1)
1684 Out.EmitFileDirective(Filename);
1686 Out.EmitDwarfFileDirective(FileNumber, Filename);
1691 /// ParseDirectiveLine
1692 /// ::= .line [number]
1693 bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
1694 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1695 if (Lexer.isNot(AsmToken::Integer))
1696 return TokError("unexpected token in '.line' directive");
1698 int64_t LineNumber = getTok().getIntVal();
1702 // FIXME: Do something with the .line.
1705 if (Lexer.isNot(AsmToken::EndOfStatement))
1706 return TokError("unexpected token in '.file' directive");
1712 /// ParseDirectiveLoc
1713 /// ::= .loc number [number [number]]
1714 bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
1715 if (Lexer.isNot(AsmToken::Integer))
1716 return TokError("unexpected token in '.loc' directive");
1718 // FIXME: What are these fields?
1719 int64_t FileNumber = getTok().getIntVal();
1721 // FIXME: Validate file.
1724 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1725 if (Lexer.isNot(AsmToken::Integer))
1726 return TokError("unexpected token in '.loc' directive");
1728 int64_t Param2 = getTok().getIntVal();
1732 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1733 if (Lexer.isNot(AsmToken::Integer))
1734 return TokError("unexpected token in '.loc' directive");
1736 int64_t Param3 = getTok().getIntVal();
1740 // FIXME: Do something with the .loc.
1744 if (Lexer.isNot(AsmToken::EndOfStatement))
1745 return TokError("unexpected token in '.file' directive");