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/StringSwitch.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/MC/MCParser/MCParsedAsmOperand.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/TargetAsmParser.h"
32 enum { DEFAULT_ADDRSPACE = 0 };
34 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
35 const MCAsmInfo &_MAI)
36 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
38 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
40 // Debugging directives.
41 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
42 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
43 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
48 AsmParser::~AsmParser() {
51 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
52 PrintMessage(L, Msg.str(), "warning");
55 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
56 PrintMessage(L, Msg.str(), "error");
60 bool AsmParser::TokError(const char *Msg) {
61 PrintMessage(Lexer.getLoc(), Msg, "error");
65 void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
66 const char *Type) const {
67 SrcMgr.PrintMessage(Loc, Msg, Type);
70 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
71 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
77 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
82 const AsmToken &AsmParser::Lex() {
83 const AsmToken *tok = &Lexer.Lex();
85 if (tok->is(AsmToken::Eof)) {
86 // If this is the end of an included file, pop the parent file off the
88 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
89 if (ParentIncludeLoc != SMLoc()) {
90 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
91 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
92 ParentIncludeLoc.getPointer());
97 if (tok->is(AsmToken::Error))
98 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
103 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
104 // Create the initial section, if requested.
106 // FIXME: Target hook & command line option for initial section.
107 if (!NoInitialTextSection)
108 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
109 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
110 0, SectionKind::getText()));
115 bool HadError = false;
117 AsmCond StartingCondState = TheCondState;
119 // While we have input, parse each statement.
120 while (Lexer.isNot(AsmToken::Eof)) {
121 if (!ParseStatement()) continue;
123 // We had an error, remember it and recover by skipping to the next line.
125 EatToEndOfStatement();
128 if (TheCondState.TheCond != StartingCondState.TheCond ||
129 TheCondState.Ignore != StartingCondState.Ignore)
130 return TokError("unmatched .ifs or .elses");
132 // Finalize the output stream if there are no errors and if the client wants
134 if (!HadError && !NoFinalize)
140 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
141 void AsmParser::EatToEndOfStatement() {
142 while (Lexer.isNot(AsmToken::EndOfStatement) &&
143 Lexer.isNot(AsmToken::Eof))
147 if (Lexer.is(AsmToken::EndOfStatement))
152 /// ParseParenExpr - Parse a paren expression and return it.
153 /// NOTE: This assumes the leading '(' has already been consumed.
155 /// parenexpr ::= expr)
157 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
158 if (ParseExpression(Res)) return true;
159 if (Lexer.isNot(AsmToken::RParen))
160 return TokError("expected ')' in parentheses expression");
161 EndLoc = Lexer.getLoc();
166 MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
167 // FIXME: Inline into callers.
168 return Ctx.GetOrCreateSymbol(Name);
171 /// ParsePrimaryExpr - Parse a primary expression and return it.
172 /// primaryexpr ::= (parenexpr
173 /// primaryexpr ::= symbol
174 /// primaryexpr ::= number
175 /// primaryexpr ::= '.'
176 /// primaryexpr ::= ~,+,- primaryexpr
177 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
178 switch (Lexer.getKind()) {
180 return TokError("unknown token in expression");
181 case AsmToken::Exclaim:
182 Lex(); // Eat the operator.
183 if (ParsePrimaryExpr(Res, EndLoc))
185 Res = MCUnaryExpr::CreateLNot(Res, getContext());
187 case AsmToken::String:
188 case AsmToken::Identifier: {
189 // This is a symbol reference.
190 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
191 MCSymbol *Sym = CreateSymbol(Split.first);
193 // Mark the symbol as used in an expression.
194 Sym->setUsedInExpr(true);
196 // Lookup the symbol variant if used.
197 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
198 if (Split.first.size() != getTok().getIdentifier().size())
199 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
201 EndLoc = Lexer.getLoc();
202 Lex(); // Eat identifier.
204 // If this is an absolute variable reference, substitute it now to preserve
205 // semantics in the face of reassignment.
206 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
208 return Error(EndLoc, "unexpected modified on variable reference");
210 Res = Sym->getVariableValue();
214 // Otherwise create a symbol ref.
215 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
218 case AsmToken::Integer: {
219 SMLoc Loc = getTok().getLoc();
220 int64_t IntVal = getTok().getIntVal();
221 Res = MCConstantExpr::Create(IntVal, getContext());
222 EndLoc = Lexer.getLoc();
224 // Look for 'b' or 'f' following an Integer as a directional label
225 if (Lexer.getKind() == AsmToken::Identifier) {
226 StringRef IDVal = getTok().getString();
227 if (IDVal == "f" || IDVal == "b"){
228 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
229 IDVal == "f" ? 1 : 0);
230 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
232 if(IDVal == "b" && Sym->isUndefined())
233 return Error(Loc, "invalid reference to undefined symbol");
234 EndLoc = Lexer.getLoc();
235 Lex(); // Eat identifier.
240 case AsmToken::Dot: {
241 // This is a '.' reference, which references the current PC. Emit a
242 // temporary label to the streamer and refer to it.
243 MCSymbol *Sym = Ctx.CreateTempSymbol();
245 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
246 EndLoc = Lexer.getLoc();
247 Lex(); // Eat identifier.
251 case AsmToken::LParen:
252 Lex(); // Eat the '('.
253 return ParseParenExpr(Res, EndLoc);
254 case AsmToken::Minus:
255 Lex(); // Eat the operator.
256 if (ParsePrimaryExpr(Res, EndLoc))
258 Res = MCUnaryExpr::CreateMinus(Res, getContext());
261 Lex(); // Eat the operator.
262 if (ParsePrimaryExpr(Res, EndLoc))
264 Res = MCUnaryExpr::CreatePlus(Res, getContext());
266 case AsmToken::Tilde:
267 Lex(); // Eat the operator.
268 if (ParsePrimaryExpr(Res, EndLoc))
270 Res = MCUnaryExpr::CreateNot(Res, getContext());
275 bool AsmParser::ParseExpression(const MCExpr *&Res) {
277 return ParseExpression(Res, EndLoc);
280 /// ParseExpression - Parse an expression and return it.
282 /// expr ::= expr +,- expr -> lowest.
283 /// expr ::= expr |,^,&,! expr -> middle.
284 /// expr ::= expr *,/,%,<<,>> expr -> highest.
285 /// expr ::= primaryexpr
287 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
288 // Parse the expression.
290 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
293 // Try to constant fold it up front, if possible.
295 if (Res->EvaluateAsAbsolute(Value))
296 Res = MCConstantExpr::Create(Value, getContext());
301 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
303 return ParseParenExpr(Res, EndLoc) ||
304 ParseBinOpRHS(1, Res, EndLoc);
307 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
310 SMLoc StartLoc = Lexer.getLoc();
311 if (ParseExpression(Expr))
314 if (!Expr->EvaluateAsAbsolute(Res))
315 return Error(StartLoc, "expected absolute expression");
320 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
321 MCBinaryExpr::Opcode &Kind) {
324 return 0; // not a binop.
326 // Lowest Precedence: &&, ||
327 case AsmToken::AmpAmp:
328 Kind = MCBinaryExpr::LAnd;
330 case AsmToken::PipePipe:
331 Kind = MCBinaryExpr::LOr;
334 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
336 Kind = MCBinaryExpr::Add;
338 case AsmToken::Minus:
339 Kind = MCBinaryExpr::Sub;
341 case AsmToken::EqualEqual:
342 Kind = MCBinaryExpr::EQ;
344 case AsmToken::ExclaimEqual:
345 case AsmToken::LessGreater:
346 Kind = MCBinaryExpr::NE;
349 Kind = MCBinaryExpr::LT;
351 case AsmToken::LessEqual:
352 Kind = MCBinaryExpr::LTE;
354 case AsmToken::Greater:
355 Kind = MCBinaryExpr::GT;
357 case AsmToken::GreaterEqual:
358 Kind = MCBinaryExpr::GTE;
361 // Intermediate Precedence: |, &, ^
363 // FIXME: gas seems to support '!' as an infix operator?
365 Kind = MCBinaryExpr::Or;
367 case AsmToken::Caret:
368 Kind = MCBinaryExpr::Xor;
371 Kind = MCBinaryExpr::And;
374 // Highest Precedence: *, /, %, <<, >>
376 Kind = MCBinaryExpr::Mul;
378 case AsmToken::Slash:
379 Kind = MCBinaryExpr::Div;
381 case AsmToken::Percent:
382 Kind = MCBinaryExpr::Mod;
384 case AsmToken::LessLess:
385 Kind = MCBinaryExpr::Shl;
387 case AsmToken::GreaterGreater:
388 Kind = MCBinaryExpr::Shr;
394 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
395 /// Res contains the LHS of the expression on input.
396 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
399 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
400 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
402 // If the next token is lower precedence than we are allowed to eat, return
403 // successfully with what we ate already.
404 if (TokPrec < Precedence)
409 // Eat the next primary expression.
411 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
413 // If BinOp binds less tightly with RHS than the operator after RHS, let
414 // the pending operator take RHS as its LHS.
415 MCBinaryExpr::Opcode Dummy;
416 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
417 if (TokPrec < NextTokPrec) {
418 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
421 // Merge LHS and RHS according to operator.
422 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
430 /// ::= EndOfStatement
431 /// ::= Label* Directive ...Operands... EndOfStatement
432 /// ::= Label* Identifier OperandList* EndOfStatement
433 bool AsmParser::ParseStatement() {
434 if (Lexer.is(AsmToken::EndOfStatement)) {
440 // Statements always start with an identifier.
441 AsmToken ID = getTok();
442 SMLoc IDLoc = ID.getLoc();
444 int64_t LocalLabelVal = -1;
445 // GUESS allow an integer followed by a ':' as a directional local label
446 if (Lexer.is(AsmToken::Integer)) {
447 LocalLabelVal = getTok().getIntVal();
448 if (LocalLabelVal < 0) {
449 if (!TheCondState.Ignore)
450 return TokError("unexpected token at start of statement");
454 IDVal = getTok().getString();
455 Lex(); // Consume the integer token to be used as an identifier token.
456 if (Lexer.getKind() != AsmToken::Colon) {
457 if (!TheCondState.Ignore)
458 return TokError("unexpected token at start of statement");
462 else if (ParseIdentifier(IDVal)) {
463 if (!TheCondState.Ignore)
464 return TokError("unexpected token at start of statement");
468 // Handle conditional assembly here before checking for skipping. We
469 // have to do this so that .endif isn't skipped in a ".if 0" block for
472 return ParseDirectiveIf(IDLoc);
473 if (IDVal == ".elseif")
474 return ParseDirectiveElseIf(IDLoc);
475 if (IDVal == ".else")
476 return ParseDirectiveElse(IDLoc);
477 if (IDVal == ".endif")
478 return ParseDirectiveEndIf(IDLoc);
480 // If we are in a ".if 0" block, ignore this statement.
481 if (TheCondState.Ignore) {
482 EatToEndOfStatement();
486 // FIXME: Recurse on local labels?
488 // See what kind of statement we have.
489 switch (Lexer.getKind()) {
490 case AsmToken::Colon: {
491 // identifier ':' -> Label.
494 // Diagnose attempt to use a variable as a label.
496 // FIXME: Diagnostics. Note the location of the definition as a label.
497 // FIXME: This doesn't diagnose assignment to a symbol which has been
498 // implicitly marked as external.
500 if (LocalLabelVal == -1)
501 Sym = CreateSymbol(IDVal);
503 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
504 if (!Sym->isUndefined() || Sym->isVariable())
505 return Error(IDLoc, "invalid symbol redefinition");
510 // Consume any end of statement token, if present, to avoid spurious
511 // AddBlankLine calls().
512 if (Lexer.is(AsmToken::EndOfStatement)) {
514 if (Lexer.is(AsmToken::Eof))
518 return ParseStatement();
521 case AsmToken::Equal:
522 // identifier '=' ... -> assignment statement
525 return ParseAssignment(IDVal);
527 default: // Normal instruction or directive.
531 // Otherwise, we have a normal instruction or directive.
532 if (IDVal[0] == '.') {
533 // FIXME: This should be driven based on a hash lookup and callback.
534 if (IDVal == ".section")
535 return ParseDirectiveDarwinSection();
536 if (IDVal == ".text")
537 // FIXME: This changes behavior based on the -static flag to the
539 return ParseDirectiveSectionSwitch("__TEXT", "__text",
540 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
541 if (IDVal == ".const")
542 return ParseDirectiveSectionSwitch("__TEXT", "__const");
543 if (IDVal == ".static_const")
544 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
545 if (IDVal == ".cstring")
546 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
547 MCSectionMachO::S_CSTRING_LITERALS);
548 if (IDVal == ".literal4")
549 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
550 MCSectionMachO::S_4BYTE_LITERALS,
552 if (IDVal == ".literal8")
553 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
554 MCSectionMachO::S_8BYTE_LITERALS,
556 if (IDVal == ".literal16")
557 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
558 MCSectionMachO::S_16BYTE_LITERALS,
560 if (IDVal == ".constructor")
561 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
562 if (IDVal == ".destructor")
563 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
564 if (IDVal == ".fvmlib_init0")
565 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
566 if (IDVal == ".fvmlib_init1")
567 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
569 // FIXME: The assembler manual claims that this has the self modify code
570 // flag, at least on x86-32, but that does not appear to be correct.
571 if (IDVal == ".symbol_stub")
572 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
573 MCSectionMachO::S_SYMBOL_STUBS |
574 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
575 // FIXME: Different on PPC and ARM.
577 // FIXME: PowerPC only?
578 if (IDVal == ".picsymbol_stub")
579 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
580 MCSectionMachO::S_SYMBOL_STUBS |
581 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
583 if (IDVal == ".data")
584 return ParseDirectiveSectionSwitch("__DATA", "__data");
585 if (IDVal == ".static_data")
586 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
588 // FIXME: The section names of these two are misspelled in the assembler
590 if (IDVal == ".non_lazy_symbol_pointer")
591 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
592 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
594 if (IDVal == ".lazy_symbol_pointer")
595 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
596 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
599 if (IDVal == ".dyld")
600 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
601 if (IDVal == ".mod_init_func")
602 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
603 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
605 if (IDVal == ".mod_term_func")
606 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
607 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
609 if (IDVal == ".const_data")
610 return ParseDirectiveSectionSwitch("__DATA", "__const");
613 if (IDVal == ".objc_class")
614 return ParseDirectiveSectionSwitch("__OBJC", "__class",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
616 if (IDVal == ".objc_meta_class")
617 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
619 if (IDVal == ".objc_cat_cls_meth")
620 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
622 if (IDVal == ".objc_cat_inst_meth")
623 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
624 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
625 if (IDVal == ".objc_protocol")
626 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
628 if (IDVal == ".objc_string_object")
629 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
631 if (IDVal == ".objc_cls_meth")
632 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
634 if (IDVal == ".objc_inst_meth")
635 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
636 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
637 if (IDVal == ".objc_cls_refs")
638 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
639 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
640 MCSectionMachO::S_LITERAL_POINTERS,
642 if (IDVal == ".objc_message_refs")
643 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
644 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
645 MCSectionMachO::S_LITERAL_POINTERS,
647 if (IDVal == ".objc_symbols")
648 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
649 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
650 if (IDVal == ".objc_category")
651 return ParseDirectiveSectionSwitch("__OBJC", "__category",
652 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
653 if (IDVal == ".objc_class_vars")
654 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
655 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
656 if (IDVal == ".objc_instance_vars")
657 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
658 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
659 if (IDVal == ".objc_module_info")
660 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
661 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
662 if (IDVal == ".objc_class_names")
663 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
664 MCSectionMachO::S_CSTRING_LITERALS);
665 if (IDVal == ".objc_meth_var_types")
666 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
667 MCSectionMachO::S_CSTRING_LITERALS);
668 if (IDVal == ".objc_meth_var_names")
669 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
670 MCSectionMachO::S_CSTRING_LITERALS);
671 if (IDVal == ".objc_selector_strs")
672 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
673 MCSectionMachO::S_CSTRING_LITERALS);
675 if (IDVal == ".tdata")
676 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
677 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
679 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
680 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
681 if (IDVal == ".thread_init_func")
682 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
683 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
685 // Assembler features
687 return ParseDirectiveSet();
691 if (IDVal == ".ascii")
692 return ParseDirectiveAscii(false);
693 if (IDVal == ".asciz")
694 return ParseDirectiveAscii(true);
696 if (IDVal == ".byte")
697 return ParseDirectiveValue(1);
698 if (IDVal == ".short")
699 return ParseDirectiveValue(2);
700 if (IDVal == ".long")
701 return ParseDirectiveValue(4);
702 if (IDVal == ".quad")
703 return ParseDirectiveValue(8);
705 // FIXME: Target hooks for IsPow2.
706 if (IDVal == ".align")
707 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
708 if (IDVal == ".align32")
709 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
710 if (IDVal == ".balign")
711 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
712 if (IDVal == ".balignw")
713 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
714 if (IDVal == ".balignl")
715 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
716 if (IDVal == ".p2align")
717 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
718 if (IDVal == ".p2alignw")
719 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
720 if (IDVal == ".p2alignl")
721 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
724 return ParseDirectiveOrg();
726 if (IDVal == ".fill")
727 return ParseDirectiveFill();
728 if (IDVal == ".space")
729 return ParseDirectiveSpace();
731 // Symbol attribute directives
733 if (IDVal == ".globl" || IDVal == ".global")
734 return ParseDirectiveSymbolAttribute(MCSA_Global);
735 if (IDVal == ".hidden")
736 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
737 if (IDVal == ".indirect_symbol")
738 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
739 if (IDVal == ".internal")
740 return ParseDirectiveSymbolAttribute(MCSA_Internal);
741 if (IDVal == ".lazy_reference")
742 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
743 if (IDVal == ".no_dead_strip")
744 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
745 if (IDVal == ".private_extern")
746 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
747 if (IDVal == ".protected")
748 return ParseDirectiveSymbolAttribute(MCSA_Protected);
749 if (IDVal == ".reference")
750 return ParseDirectiveSymbolAttribute(MCSA_Reference);
751 if (IDVal == ".type")
752 return ParseDirectiveELFType();
753 if (IDVal == ".weak")
754 return ParseDirectiveSymbolAttribute(MCSA_Weak);
755 if (IDVal == ".weak_definition")
756 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
757 if (IDVal == ".weak_reference")
758 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
760 if (IDVal == ".comm")
761 return ParseDirectiveComm(/*IsLocal=*/false);
762 if (IDVal == ".lcomm")
763 return ParseDirectiveComm(/*IsLocal=*/true);
764 if (IDVal == ".zerofill")
765 return ParseDirectiveDarwinZerofill();
766 if (IDVal == ".desc")
767 return ParseDirectiveDarwinSymbolDesc();
768 if (IDVal == ".lsym")
769 return ParseDirectiveDarwinLsym();
770 if (IDVal == ".tbss")
771 return ParseDirectiveDarwinTBSS();
773 if (IDVal == ".subsections_via_symbols")
774 return ParseDirectiveDarwinSubsectionsViaSymbols();
775 if (IDVal == ".abort")
776 return ParseDirectiveAbort();
777 if (IDVal == ".include")
778 return ParseDirectiveInclude();
779 if (IDVal == ".dump")
780 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
781 if (IDVal == ".load")
782 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
784 // Look up the handler in the handler table,
785 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
787 return (this->*Handler)(IDVal, IDLoc);
789 // Target hook for parsing target specific directives.
790 if (!getTargetParser().ParseDirective(ID))
793 Warning(IDLoc, "ignoring directive for now");
794 EatToEndOfStatement();
798 // Canonicalize the opcode to lower case.
799 SmallString<128> Opcode;
800 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
801 Opcode.push_back(tolower(IDVal[i]));
803 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
804 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
806 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
807 HadError = TokError("unexpected token in argument list");
809 // If parsing succeeded, match the instruction.
812 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
813 // Emit the instruction on success.
814 Out.EmitInstruction(Inst);
816 // Otherwise emit a diagnostic about the match failure and set the error
819 // FIXME: We should give nicer diagnostics about the exact failure.
820 Error(IDLoc, "unrecognized instruction");
825 // If there was no error, consume the end-of-statement token. Otherwise this
826 // will be done by our caller.
830 // Free any parsed operands.
831 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
832 delete ParsedOperands[i];
837 bool AsmParser::ParseAssignment(const StringRef &Name) {
838 // FIXME: Use better location, we should use proper tokens.
839 SMLoc EqualLoc = Lexer.getLoc();
842 SMLoc StartLoc = Lexer.getLoc();
843 if (ParseExpression(Value))
846 if (Lexer.isNot(AsmToken::EndOfStatement))
847 return TokError("unexpected token in assignment");
849 // Eat the end of statement marker.
852 // Validate that the LHS is allowed to be a variable (either it has not been
853 // used as a symbol, or it is an absolute symbol).
854 MCSymbol *Sym = getContext().LookupSymbol(Name);
856 // Diagnose assignment to a label.
858 // FIXME: Diagnostics. Note the location of the definition as a label.
859 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
860 if (Sym->isUndefined() && !Sym->isUsedInExpr())
861 ; // Allow redefinitions of undefined symbols only used in directives.
862 else if (!Sym->isUndefined() && !Sym->isAbsolute())
863 return Error(EqualLoc, "redefinition of '" + Name + "'");
864 else if (!Sym->isVariable())
865 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
866 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
867 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
870 Sym = CreateSymbol(Name);
872 // FIXME: Handle '.'.
874 Sym->setUsedInExpr(true);
876 // Do the assignment.
877 Out.EmitAssignment(Sym, Value);
885 bool AsmParser::ParseIdentifier(StringRef &Res) {
886 if (Lexer.isNot(AsmToken::Identifier) &&
887 Lexer.isNot(AsmToken::String))
890 Res = getTok().getIdentifier();
892 Lex(); // Consume the identifier token.
897 /// ParseDirectiveSet:
898 /// ::= .set identifier ',' expression
899 bool AsmParser::ParseDirectiveSet() {
902 if (ParseIdentifier(Name))
903 return TokError("expected identifier after '.set' directive");
905 if (Lexer.isNot(AsmToken::Comma))
906 return TokError("unexpected token in '.set'");
909 return ParseAssignment(Name);
912 /// ParseDirectiveSection:
913 /// ::= .section identifier (',' identifier)*
914 /// FIXME: This should actually parse out the segment, section, attributes and
915 /// sizeof_stub fields.
916 bool AsmParser::ParseDirectiveDarwinSection() {
917 SMLoc Loc = Lexer.getLoc();
919 StringRef SectionName;
920 if (ParseIdentifier(SectionName))
921 return Error(Loc, "expected identifier after '.section' directive");
923 // Verify there is a following comma.
924 if (!Lexer.is(AsmToken::Comma))
925 return TokError("unexpected token in '.section' directive");
927 std::string SectionSpec = SectionName;
930 // Add all the tokens until the end of the line, ParseSectionSpecifier will
932 StringRef EOL = Lexer.LexUntilEndOfStatement();
933 SectionSpec.append(EOL.begin(), EOL.end());
936 if (Lexer.isNot(AsmToken::EndOfStatement))
937 return TokError("unexpected token in '.section' directive");
941 StringRef Segment, Section;
942 unsigned TAA, StubSize;
943 std::string ErrorStr =
944 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
947 if (!ErrorStr.empty())
948 return Error(Loc, ErrorStr.c_str());
950 // FIXME: Arch specific.
951 bool isText = Segment == "__TEXT"; // FIXME: Hack.
952 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
953 isText ? SectionKind::getText()
954 : SectionKind::getDataRel()));
958 /// ParseDirectiveSectionSwitch -
959 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
961 unsigned TAA, unsigned Align,
963 if (Lexer.isNot(AsmToken::EndOfStatement))
964 return TokError("unexpected token in section switching directive");
967 // FIXME: Arch specific.
968 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
969 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
970 isText ? SectionKind::getText()
971 : SectionKind::getDataRel()));
973 // Set the implicit alignment, if any.
975 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
976 // alignment on the section (e.g., if one manually inserts bytes into the
977 // section, then just issueing the section switch directive will not realign
978 // the section. However, this is arguably more reasonable behavior, and there
979 // is no good reason for someone to intentionally emit incorrectly sized
980 // values into the implicitly aligned sections.
982 Out.EmitValueToAlignment(Align, 0, 1, 0);
987 bool AsmParser::ParseEscapedString(std::string &Data) {
988 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
991 StringRef Str = getTok().getStringContents();
992 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
993 if (Str[i] != '\\') {
998 // Recognize escaped characters. Note that this escape semantics currently
999 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1002 return TokError("unexpected backslash at end of string");
1004 // Recognize octal sequences.
1005 if ((unsigned) (Str[i] - '0') <= 7) {
1006 // Consume up to three octal characters.
1007 unsigned Value = Str[i] - '0';
1009 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1011 Value = Value * 8 + (Str[i] - '0');
1013 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1015 Value = Value * 8 + (Str[i] - '0');
1020 return TokError("invalid octal escape sequence (out of range)");
1022 Data += (unsigned char) Value;
1026 // Otherwise recognize individual escapes.
1029 // Just reject invalid escape sequences for now.
1030 return TokError("invalid escape sequence (unrecognized character)");
1032 case 'b': Data += '\b'; break;
1033 case 'f': Data += '\f'; break;
1034 case 'n': Data += '\n'; break;
1035 case 'r': Data += '\r'; break;
1036 case 't': Data += '\t'; break;
1037 case '"': Data += '"'; break;
1038 case '\\': Data += '\\'; break;
1045 /// ParseDirectiveAscii:
1046 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
1047 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
1048 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1050 if (Lexer.isNot(AsmToken::String))
1051 return TokError("expected string in '.ascii' or '.asciz' directive");
1054 if (ParseEscapedString(Data))
1057 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
1059 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1063 if (Lexer.is(AsmToken::EndOfStatement))
1066 if (Lexer.isNot(AsmToken::Comma))
1067 return TokError("unexpected token in '.ascii' or '.asciz' directive");
1076 /// ParseDirectiveValue
1077 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1078 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1079 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1081 const MCExpr *Value;
1082 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
1083 if (ParseExpression(Value))
1086 // Special case constant expressions to match code generator.
1087 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1088 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1090 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1092 if (Lexer.is(AsmToken::EndOfStatement))
1095 // FIXME: Improve diagnostic.
1096 if (Lexer.isNot(AsmToken::Comma))
1097 return TokError("unexpected token in directive");
1106 /// ParseDirectiveSpace
1107 /// ::= .space expression [ , expression ]
1108 bool AsmParser::ParseDirectiveSpace() {
1110 if (ParseAbsoluteExpression(NumBytes))
1113 int64_t FillExpr = 0;
1114 bool HasFillExpr = false;
1115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1116 if (Lexer.isNot(AsmToken::Comma))
1117 return TokError("unexpected token in '.space' directive");
1120 if (ParseAbsoluteExpression(FillExpr))
1125 if (Lexer.isNot(AsmToken::EndOfStatement))
1126 return TokError("unexpected token in '.space' directive");
1132 return TokError("invalid number of bytes in '.space' directive");
1134 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1135 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1140 /// ParseDirectiveFill
1141 /// ::= .fill expression , expression , expression
1142 bool AsmParser::ParseDirectiveFill() {
1144 if (ParseAbsoluteExpression(NumValues))
1147 if (Lexer.isNot(AsmToken::Comma))
1148 return TokError("unexpected token in '.fill' directive");
1152 if (ParseAbsoluteExpression(FillSize))
1155 if (Lexer.isNot(AsmToken::Comma))
1156 return TokError("unexpected token in '.fill' directive");
1160 if (ParseAbsoluteExpression(FillExpr))
1163 if (Lexer.isNot(AsmToken::EndOfStatement))
1164 return TokError("unexpected token in '.fill' directive");
1168 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1169 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1171 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1172 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1177 /// ParseDirectiveOrg
1178 /// ::= .org expression [ , expression ]
1179 bool AsmParser::ParseDirectiveOrg() {
1180 const MCExpr *Offset;
1181 SMLoc StartLoc = Lexer.getLoc();
1182 if (ParseExpression(Offset))
1185 // Parse optional fill expression.
1186 int64_t FillExpr = 0;
1187 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1188 if (Lexer.isNot(AsmToken::Comma))
1189 return TokError("unexpected token in '.org' directive");
1192 if (ParseAbsoluteExpression(FillExpr))
1195 if (Lexer.isNot(AsmToken::EndOfStatement))
1196 return TokError("unexpected token in '.org' directive");
1201 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1202 // has to be relative to the current section.
1203 Out.EmitValueToOffset(Offset, FillExpr);
1208 /// ParseDirectiveAlign
1209 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1210 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1211 SMLoc AlignmentLoc = Lexer.getLoc();
1213 if (ParseAbsoluteExpression(Alignment))
1217 bool HasFillExpr = false;
1218 int64_t FillExpr = 0;
1219 int64_t MaxBytesToFill = 0;
1220 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1221 if (Lexer.isNot(AsmToken::Comma))
1222 return TokError("unexpected token in directive");
1225 // The fill expression can be omitted while specifying a maximum number of
1226 // alignment bytes, e.g:
1228 if (Lexer.isNot(AsmToken::Comma)) {
1230 if (ParseAbsoluteExpression(FillExpr))
1234 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1235 if (Lexer.isNot(AsmToken::Comma))
1236 return TokError("unexpected token in directive");
1239 MaxBytesLoc = Lexer.getLoc();
1240 if (ParseAbsoluteExpression(MaxBytesToFill))
1243 if (Lexer.isNot(AsmToken::EndOfStatement))
1244 return TokError("unexpected token in directive");
1253 // Compute alignment in bytes.
1255 // FIXME: Diagnose overflow.
1256 if (Alignment >= 32) {
1257 Error(AlignmentLoc, "invalid alignment value");
1261 Alignment = 1ULL << Alignment;
1264 // Diagnose non-sensical max bytes to align.
1265 if (MaxBytesLoc.isValid()) {
1266 if (MaxBytesToFill < 1) {
1267 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1268 "many bytes, ignoring maximum bytes expression");
1272 if (MaxBytesToFill >= Alignment) {
1273 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1279 // Check whether we should use optimal code alignment for this .align
1282 // FIXME: This should be using a target hook.
1283 bool UseCodeAlign = false;
1284 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1285 Out.getCurrentSection()))
1286 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1287 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1288 ValueSize == 1 && UseCodeAlign) {
1289 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1291 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1292 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1298 /// ParseDirectiveSymbolAttribute
1299 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1300 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1301 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1305 if (ParseIdentifier(Name))
1306 return TokError("expected identifier in directive");
1308 MCSymbol *Sym = CreateSymbol(Name);
1310 Out.EmitSymbolAttribute(Sym, Attr);
1312 if (Lexer.is(AsmToken::EndOfStatement))
1315 if (Lexer.isNot(AsmToken::Comma))
1316 return TokError("unexpected token in directive");
1325 /// ParseDirectiveELFType
1326 /// ::= .type identifier , @attribute
1327 bool AsmParser::ParseDirectiveELFType() {
1329 if (ParseIdentifier(Name))
1330 return TokError("expected identifier in directive");
1332 // Handle the identifier as the key symbol.
1333 MCSymbol *Sym = CreateSymbol(Name);
1335 if (Lexer.isNot(AsmToken::Comma))
1336 return TokError("unexpected token in '.type' directive");
1339 if (Lexer.isNot(AsmToken::At))
1340 return TokError("expected '@' before type");
1346 TypeLoc = Lexer.getLoc();
1347 if (ParseIdentifier(Type))
1348 return TokError("expected symbol type in directive");
1350 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1351 .Case("function", MCSA_ELF_TypeFunction)
1352 .Case("object", MCSA_ELF_TypeObject)
1353 .Case("tls_object", MCSA_ELF_TypeTLS)
1354 .Case("common", MCSA_ELF_TypeCommon)
1355 .Case("notype", MCSA_ELF_TypeNoType)
1356 .Default(MCSA_Invalid);
1358 if (Attr == MCSA_Invalid)
1359 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1361 if (Lexer.isNot(AsmToken::EndOfStatement))
1362 return TokError("unexpected token in '.type' directive");
1366 Out.EmitSymbolAttribute(Sym, Attr);
1371 /// ParseDirectiveDarwinSymbolDesc
1372 /// ::= .desc identifier , expression
1373 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1375 if (ParseIdentifier(Name))
1376 return TokError("expected identifier in directive");
1378 // Handle the identifier as the key symbol.
1379 MCSymbol *Sym = CreateSymbol(Name);
1381 if (Lexer.isNot(AsmToken::Comma))
1382 return TokError("unexpected token in '.desc' directive");
1385 SMLoc DescLoc = Lexer.getLoc();
1387 if (ParseAbsoluteExpression(DescValue))
1390 if (Lexer.isNot(AsmToken::EndOfStatement))
1391 return TokError("unexpected token in '.desc' directive");
1395 // Set the n_desc field of this Symbol to this DescValue
1396 Out.EmitSymbolDesc(Sym, DescValue);
1401 /// ParseDirectiveComm
1402 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1403 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1404 SMLoc IDLoc = Lexer.getLoc();
1406 if (ParseIdentifier(Name))
1407 return TokError("expected identifier in directive");
1409 // Handle the identifier as the key symbol.
1410 MCSymbol *Sym = CreateSymbol(Name);
1412 if (Lexer.isNot(AsmToken::Comma))
1413 return TokError("unexpected token in directive");
1417 SMLoc SizeLoc = Lexer.getLoc();
1418 if (ParseAbsoluteExpression(Size))
1421 int64_t Pow2Alignment = 0;
1422 SMLoc Pow2AlignmentLoc;
1423 if (Lexer.is(AsmToken::Comma)) {
1425 Pow2AlignmentLoc = Lexer.getLoc();
1426 if (ParseAbsoluteExpression(Pow2Alignment))
1429 // If this target takes alignments in bytes (not log) validate and convert.
1430 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1431 if (!isPowerOf2_64(Pow2Alignment))
1432 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1433 Pow2Alignment = Log2_64(Pow2Alignment);
1437 if (Lexer.isNot(AsmToken::EndOfStatement))
1438 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1442 // NOTE: a size of zero for a .comm should create a undefined symbol
1443 // but a size of .lcomm creates a bss symbol of size zero.
1445 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1446 "be less than zero");
1448 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1449 // may internally end up wanting an alignment in bytes.
1450 // FIXME: Diagnose overflow.
1451 if (Pow2Alignment < 0)
1452 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1453 "alignment, can't be less than zero");
1455 if (!Sym->isUndefined())
1456 return Error(IDLoc, "invalid symbol redefinition");
1458 // '.lcomm' is equivalent to '.zerofill'.
1459 // Create the Symbol as a common or local common with Size and Pow2Alignment
1461 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1462 MCSectionMachO::S_ZEROFILL, 0,
1463 SectionKind::getBSS()),
1464 Sym, Size, 1 << Pow2Alignment);
1468 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1472 /// ParseDirectiveDarwinZerofill
1473 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1474 /// , align_expression ]]
1475 bool AsmParser::ParseDirectiveDarwinZerofill() {
1477 if (ParseIdentifier(Segment))
1478 return TokError("expected segment name after '.zerofill' directive");
1480 if (Lexer.isNot(AsmToken::Comma))
1481 return TokError("unexpected token in directive");
1485 if (ParseIdentifier(Section))
1486 return TokError("expected section name after comma in '.zerofill' "
1489 // If this is the end of the line all that was wanted was to create the
1490 // the section but with no symbol.
1491 if (Lexer.is(AsmToken::EndOfStatement)) {
1492 // Create the zerofill section but no symbol
1493 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1494 MCSectionMachO::S_ZEROFILL, 0,
1495 SectionKind::getBSS()));
1499 if (Lexer.isNot(AsmToken::Comma))
1500 return TokError("unexpected token in directive");
1503 SMLoc IDLoc = Lexer.getLoc();
1505 if (ParseIdentifier(IDStr))
1506 return TokError("expected identifier in directive");
1508 // handle the identifier as the key symbol.
1509 MCSymbol *Sym = CreateSymbol(IDStr);
1511 if (Lexer.isNot(AsmToken::Comma))
1512 return TokError("unexpected token in directive");
1516 SMLoc SizeLoc = Lexer.getLoc();
1517 if (ParseAbsoluteExpression(Size))
1520 int64_t Pow2Alignment = 0;
1521 SMLoc Pow2AlignmentLoc;
1522 if (Lexer.is(AsmToken::Comma)) {
1524 Pow2AlignmentLoc = Lexer.getLoc();
1525 if (ParseAbsoluteExpression(Pow2Alignment))
1529 if (Lexer.isNot(AsmToken::EndOfStatement))
1530 return TokError("unexpected token in '.zerofill' directive");
1535 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1538 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1539 // may internally end up wanting an alignment in bytes.
1540 // FIXME: Diagnose overflow.
1541 if (Pow2Alignment < 0)
1542 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1543 "can't be less than zero");
1545 if (!Sym->isUndefined())
1546 return Error(IDLoc, "invalid symbol redefinition");
1548 // Create the zerofill Symbol with Size and Pow2Alignment
1550 // FIXME: Arch specific.
1551 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1552 MCSectionMachO::S_ZEROFILL, 0,
1553 SectionKind::getBSS()),
1554 Sym, Size, 1 << Pow2Alignment);
1559 /// ParseDirectiveDarwinTBSS
1560 /// ::= .tbss identifier, size, align
1561 bool AsmParser::ParseDirectiveDarwinTBSS() {
1562 SMLoc IDLoc = Lexer.getLoc();
1564 if (ParseIdentifier(Name))
1565 return TokError("expected identifier in directive");
1567 // Handle the identifier as the key symbol.
1568 MCSymbol *Sym = CreateSymbol(Name);
1570 if (Lexer.isNot(AsmToken::Comma))
1571 return TokError("unexpected token in directive");
1575 SMLoc SizeLoc = Lexer.getLoc();
1576 if (ParseAbsoluteExpression(Size))
1579 int64_t Pow2Alignment = 0;
1580 SMLoc Pow2AlignmentLoc;
1581 if (Lexer.is(AsmToken::Comma)) {
1583 Pow2AlignmentLoc = Lexer.getLoc();
1584 if (ParseAbsoluteExpression(Pow2Alignment))
1588 if (Lexer.isNot(AsmToken::EndOfStatement))
1589 return TokError("unexpected token in '.tbss' directive");
1594 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1597 // FIXME: Diagnose overflow.
1598 if (Pow2Alignment < 0)
1599 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1602 if (!Sym->isUndefined())
1603 return Error(IDLoc, "invalid symbol redefinition");
1605 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1606 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1607 0, SectionKind::getThreadBSS()),
1608 Sym, Size, 1 << Pow2Alignment);
1613 /// ParseDirectiveDarwinSubsectionsViaSymbols
1614 /// ::= .subsections_via_symbols
1615 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1616 if (Lexer.isNot(AsmToken::EndOfStatement))
1617 return TokError("unexpected token in '.subsections_via_symbols' directive");
1621 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1626 /// ParseDirectiveAbort
1627 /// ::= .abort [ "abort_string" ]
1628 bool AsmParser::ParseDirectiveAbort() {
1629 // FIXME: Use loc from directive.
1630 SMLoc Loc = Lexer.getLoc();
1633 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1634 if (Lexer.isNot(AsmToken::String))
1635 return TokError("expected string in '.abort' directive");
1637 Str = getTok().getString();
1642 if (Lexer.isNot(AsmToken::EndOfStatement))
1643 return TokError("unexpected token in '.abort' directive");
1647 // FIXME: Handle here.
1649 Error(Loc, ".abort detected. Assembly stopping.");
1651 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1656 /// ParseDirectiveLsym
1657 /// ::= .lsym identifier , expression
1658 bool AsmParser::ParseDirectiveDarwinLsym() {
1660 if (ParseIdentifier(Name))
1661 return TokError("expected identifier in directive");
1663 // Handle the identifier as the key symbol.
1664 MCSymbol *Sym = CreateSymbol(Name);
1666 if (Lexer.isNot(AsmToken::Comma))
1667 return TokError("unexpected token in '.lsym' directive");
1670 const MCExpr *Value;
1671 SMLoc StartLoc = Lexer.getLoc();
1672 if (ParseExpression(Value))
1675 if (Lexer.isNot(AsmToken::EndOfStatement))
1676 return TokError("unexpected token in '.lsym' directive");
1680 // We don't currently support this directive.
1682 // FIXME: Diagnostic location!
1684 return TokError("directive '.lsym' is unsupported");
1687 /// ParseDirectiveInclude
1688 /// ::= .include "filename"
1689 bool AsmParser::ParseDirectiveInclude() {
1690 if (Lexer.isNot(AsmToken::String))
1691 return TokError("expected string in '.include' directive");
1693 std::string Filename = getTok().getString();
1694 SMLoc IncludeLoc = Lexer.getLoc();
1697 if (Lexer.isNot(AsmToken::EndOfStatement))
1698 return TokError("unexpected token in '.include' directive");
1700 // Strip the quotes.
1701 Filename = Filename.substr(1, Filename.size()-2);
1703 // Attempt to switch the lexer to the included file before consuming the end
1704 // of statement to avoid losing it when we switch.
1705 if (EnterIncludeFile(Filename)) {
1706 PrintMessage(IncludeLoc,
1707 "Could not find include file '" + Filename + "'",
1715 /// ParseDirectiveDarwinDumpOrLoad
1716 /// ::= ( .dump | .load ) "filename"
1717 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1718 if (Lexer.isNot(AsmToken::String))
1719 return TokError("expected string in '.dump' or '.load' directive");
1723 if (Lexer.isNot(AsmToken::EndOfStatement))
1724 return TokError("unexpected token in '.dump' or '.load' directive");
1728 // FIXME: If/when .dump and .load are implemented they will be done in the
1729 // the assembly parser and not have any need for an MCStreamer API.
1731 Warning(IDLoc, "ignoring directive .dump for now");
1733 Warning(IDLoc, "ignoring directive .load for now");
1738 /// ParseDirectiveIf
1739 /// ::= .if expression
1740 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1741 TheCondStack.push_back(TheCondState);
1742 TheCondState.TheCond = AsmCond::IfCond;
1743 if(TheCondState.Ignore) {
1744 EatToEndOfStatement();
1748 if (ParseAbsoluteExpression(ExprValue))
1751 if (Lexer.isNot(AsmToken::EndOfStatement))
1752 return TokError("unexpected token in '.if' directive");
1756 TheCondState.CondMet = ExprValue;
1757 TheCondState.Ignore = !TheCondState.CondMet;
1763 /// ParseDirectiveElseIf
1764 /// ::= .elseif expression
1765 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1766 if (TheCondState.TheCond != AsmCond::IfCond &&
1767 TheCondState.TheCond != AsmCond::ElseIfCond)
1768 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1770 TheCondState.TheCond = AsmCond::ElseIfCond;
1772 bool LastIgnoreState = false;
1773 if (!TheCondStack.empty())
1774 LastIgnoreState = TheCondStack.back().Ignore;
1775 if (LastIgnoreState || TheCondState.CondMet) {
1776 TheCondState.Ignore = true;
1777 EatToEndOfStatement();
1781 if (ParseAbsoluteExpression(ExprValue))
1784 if (Lexer.isNot(AsmToken::EndOfStatement))
1785 return TokError("unexpected token in '.elseif' directive");
1788 TheCondState.CondMet = ExprValue;
1789 TheCondState.Ignore = !TheCondState.CondMet;
1795 /// ParseDirectiveElse
1797 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1798 if (Lexer.isNot(AsmToken::EndOfStatement))
1799 return TokError("unexpected token in '.else' directive");
1803 if (TheCondState.TheCond != AsmCond::IfCond &&
1804 TheCondState.TheCond != AsmCond::ElseIfCond)
1805 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1807 TheCondState.TheCond = AsmCond::ElseCond;
1808 bool LastIgnoreState = false;
1809 if (!TheCondStack.empty())
1810 LastIgnoreState = TheCondStack.back().Ignore;
1811 if (LastIgnoreState || TheCondState.CondMet)
1812 TheCondState.Ignore = true;
1814 TheCondState.Ignore = false;
1819 /// ParseDirectiveEndIf
1821 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1822 if (Lexer.isNot(AsmToken::EndOfStatement))
1823 return TokError("unexpected token in '.endif' directive");
1827 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1828 TheCondStack.empty())
1829 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1831 if (!TheCondStack.empty()) {
1832 TheCondState = TheCondStack.back();
1833 TheCondStack.pop_back();
1839 /// ParseDirectiveFile
1840 /// ::= .file [number] string
1841 bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
1842 // FIXME: I'm not sure what this is.
1843 int64_t FileNumber = -1;
1844 if (Lexer.is(AsmToken::Integer)) {
1845 FileNumber = getTok().getIntVal();
1849 return TokError("file number less than one");
1852 if (Lexer.isNot(AsmToken::String))
1853 return TokError("unexpected token in '.file' directive");
1855 StringRef Filename = getTok().getString();
1856 Filename = Filename.substr(1, Filename.size()-2);
1859 if (Lexer.isNot(AsmToken::EndOfStatement))
1860 return TokError("unexpected token in '.file' directive");
1862 if (FileNumber == -1)
1863 Out.EmitFileDirective(Filename);
1865 Out.EmitDwarfFileDirective(FileNumber, Filename);
1870 /// ParseDirectiveLine
1871 /// ::= .line [number]
1872 bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
1873 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1874 if (Lexer.isNot(AsmToken::Integer))
1875 return TokError("unexpected token in '.line' directive");
1877 int64_t LineNumber = getTok().getIntVal();
1881 // FIXME: Do something with the .line.
1884 if (Lexer.isNot(AsmToken::EndOfStatement))
1885 return TokError("unexpected token in '.file' directive");
1891 /// ParseDirectiveLoc
1892 /// ::= .loc number [number [number]]
1893 bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
1894 if (Lexer.isNot(AsmToken::Integer))
1895 return TokError("unexpected token in '.loc' directive");
1897 // FIXME: What are these fields?
1898 int64_t FileNumber = getTok().getIntVal();
1900 // FIXME: Validate file.
1903 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1904 if (Lexer.isNot(AsmToken::Integer))
1905 return TokError("unexpected token in '.loc' directive");
1907 int64_t Param2 = getTok().getIntVal();
1911 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1912 if (Lexer.isNot(AsmToken::Integer))
1913 return TokError("unexpected token in '.loc' directive");
1915 int64_t Param3 = getTok().getIntVal();
1919 // FIXME: Do something with the .loc.
1923 if (Lexer.isNot(AsmToken::EndOfStatement))
1924 return TokError("unexpected token in '.file' directive");