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/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetAsmParser.h"
33 enum { DEFAULT_ADDRSPACE = 0 };
35 AsmParser::AsmParser(const Target &T, SourceMgr &_SM, MCContext &_Ctx,
36 MCStreamer &_Out, const MCAsmInfo &_MAI)
37 : Lexer(_MAI), Ctx(_Ctx), Out(_Out), SrcMgr(_SM), TargetParser(0),
39 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
41 // Debugging directives.
42 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
43 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
44 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 // Mark the symbol as used in an expression.
193 Sym->setUsedInExpr(true);
195 // Lookup the symbol variant if used.
196 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
197 if (Split.first.size() != getTok().getIdentifier().size())
198 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
200 EndLoc = Lexer.getLoc();
201 Lex(); // Eat identifier.
203 // If this is an absolute variable reference, substitute it now to preserve
204 // semantics in the face of reassignment.
205 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
207 return Error(EndLoc, "unexpected modified on variable reference");
209 Res = Sym->getVariableValue();
213 // Otherwise create a symbol ref.
214 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
217 case AsmToken::Integer: {
218 SMLoc Loc = getTok().getLoc();
219 int64_t IntVal = getTok().getIntVal();
220 Res = MCConstantExpr::Create(IntVal, getContext());
221 EndLoc = Lexer.getLoc();
223 // Look for 'b' or 'f' following an Integer as a directional label
224 if (Lexer.getKind() == AsmToken::Identifier) {
225 StringRef IDVal = getTok().getString();
226 if (IDVal == "f" || IDVal == "b"){
227 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
228 IDVal == "f" ? 1 : 0);
229 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
231 if(IDVal == "b" && Sym->isUndefined())
232 return Error(Loc, "invalid reference to undefined symbol");
233 EndLoc = Lexer.getLoc();
234 Lex(); // Eat identifier.
239 case AsmToken::Dot: {
240 // This is a '.' reference, which references the current PC. Emit a
241 // temporary label to the streamer and refer to it.
242 MCSymbol *Sym = Ctx.CreateTempSymbol();
244 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
245 EndLoc = Lexer.getLoc();
246 Lex(); // Eat identifier.
250 case AsmToken::LParen:
251 Lex(); // Eat the '('.
252 return ParseParenExpr(Res, EndLoc);
253 case AsmToken::Minus:
254 Lex(); // Eat the operator.
255 if (ParsePrimaryExpr(Res, EndLoc))
257 Res = MCUnaryExpr::CreateMinus(Res, getContext());
260 Lex(); // Eat the operator.
261 if (ParsePrimaryExpr(Res, EndLoc))
263 Res = MCUnaryExpr::CreatePlus(Res, getContext());
265 case AsmToken::Tilde:
266 Lex(); // Eat the operator.
267 if (ParsePrimaryExpr(Res, EndLoc))
269 Res = MCUnaryExpr::CreateNot(Res, getContext());
274 bool AsmParser::ParseExpression(const MCExpr *&Res) {
276 return ParseExpression(Res, EndLoc);
279 /// ParseExpression - Parse an expression and return it.
281 /// expr ::= expr +,- expr -> lowest.
282 /// expr ::= expr |,^,&,! expr -> middle.
283 /// expr ::= expr *,/,%,<<,>> expr -> highest.
284 /// expr ::= primaryexpr
286 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
287 // Parse the expression.
289 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
292 // Try to constant fold it up front, if possible.
294 if (Res->EvaluateAsAbsolute(Value))
295 Res = MCConstantExpr::Create(Value, getContext());
300 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
302 return ParseParenExpr(Res, EndLoc) ||
303 ParseBinOpRHS(1, Res, EndLoc);
306 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
309 SMLoc StartLoc = Lexer.getLoc();
310 if (ParseExpression(Expr))
313 if (!Expr->EvaluateAsAbsolute(Res))
314 return Error(StartLoc, "expected absolute expression");
319 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
320 MCBinaryExpr::Opcode &Kind) {
323 return 0; // not a binop.
325 // Lowest Precedence: &&, ||
326 case AsmToken::AmpAmp:
327 Kind = MCBinaryExpr::LAnd;
329 case AsmToken::PipePipe:
330 Kind = MCBinaryExpr::LOr;
333 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
335 Kind = MCBinaryExpr::Add;
337 case AsmToken::Minus:
338 Kind = MCBinaryExpr::Sub;
340 case AsmToken::EqualEqual:
341 Kind = MCBinaryExpr::EQ;
343 case AsmToken::ExclaimEqual:
344 case AsmToken::LessGreater:
345 Kind = MCBinaryExpr::NE;
348 Kind = MCBinaryExpr::LT;
350 case AsmToken::LessEqual:
351 Kind = MCBinaryExpr::LTE;
353 case AsmToken::Greater:
354 Kind = MCBinaryExpr::GT;
356 case AsmToken::GreaterEqual:
357 Kind = MCBinaryExpr::GTE;
360 // Intermediate Precedence: |, &, ^
362 // FIXME: gas seems to support '!' as an infix operator?
364 Kind = MCBinaryExpr::Or;
366 case AsmToken::Caret:
367 Kind = MCBinaryExpr::Xor;
370 Kind = MCBinaryExpr::And;
373 // Highest Precedence: *, /, %, <<, >>
375 Kind = MCBinaryExpr::Mul;
377 case AsmToken::Slash:
378 Kind = MCBinaryExpr::Div;
380 case AsmToken::Percent:
381 Kind = MCBinaryExpr::Mod;
383 case AsmToken::LessLess:
384 Kind = MCBinaryExpr::Shl;
386 case AsmToken::GreaterGreater:
387 Kind = MCBinaryExpr::Shr;
393 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
394 /// Res contains the LHS of the expression on input.
395 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
398 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
399 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
401 // If the next token is lower precedence than we are allowed to eat, return
402 // successfully with what we ate already.
403 if (TokPrec < Precedence)
408 // Eat the next primary expression.
410 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
412 // If BinOp binds less tightly with RHS than the operator after RHS, let
413 // the pending operator take RHS as its LHS.
414 MCBinaryExpr::Opcode Dummy;
415 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
416 if (TokPrec < NextTokPrec) {
417 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
420 // Merge LHS and RHS according to operator.
421 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
429 /// ::= EndOfStatement
430 /// ::= Label* Directive ...Operands... EndOfStatement
431 /// ::= Label* Identifier OperandList* EndOfStatement
432 bool AsmParser::ParseStatement() {
433 if (Lexer.is(AsmToken::EndOfStatement)) {
439 // Statements always start with an identifier.
440 AsmToken ID = getTok();
441 SMLoc IDLoc = ID.getLoc();
443 int64_t LocalLabelVal = -1;
444 // GUESS allow an integer followed by a ':' as a directional local label
445 if (Lexer.is(AsmToken::Integer)) {
446 LocalLabelVal = getTok().getIntVal();
447 if (LocalLabelVal < 0) {
448 if (!TheCondState.Ignore)
449 return TokError("unexpected token at start of statement");
453 IDVal = getTok().getString();
454 Lex(); // Consume the integer token to be used as an identifier token.
455 if (Lexer.getKind() != AsmToken::Colon) {
456 if (!TheCondState.Ignore)
457 return TokError("unexpected token at start of statement");
461 else if (ParseIdentifier(IDVal)) {
462 if (!TheCondState.Ignore)
463 return TokError("unexpected token at start of statement");
467 // Handle conditional assembly here before checking for skipping. We
468 // have to do this so that .endif isn't skipped in a ".if 0" block for
471 return ParseDirectiveIf(IDLoc);
472 if (IDVal == ".elseif")
473 return ParseDirectiveElseIf(IDLoc);
474 if (IDVal == ".else")
475 return ParseDirectiveElse(IDLoc);
476 if (IDVal == ".endif")
477 return ParseDirectiveEndIf(IDLoc);
479 // If we are in a ".if 0" block, ignore this statement.
480 if (TheCondState.Ignore) {
481 EatToEndOfStatement();
485 // FIXME: Recurse on local labels?
487 // See what kind of statement we have.
488 switch (Lexer.getKind()) {
489 case AsmToken::Colon: {
490 // identifier ':' -> Label.
493 // Diagnose attempt to use a variable as a label.
495 // FIXME: Diagnostics. Note the location of the definition as a label.
496 // FIXME: This doesn't diagnose assignment to a symbol which has been
497 // implicitly marked as external.
499 if (LocalLabelVal == -1)
500 Sym = CreateSymbol(IDVal);
502 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
503 if (!Sym->isUndefined() || Sym->isVariable())
504 return Error(IDLoc, "invalid symbol redefinition");
509 // Consume any end of statement token, if present, to avoid spurious
510 // AddBlankLine calls().
511 if (Lexer.is(AsmToken::EndOfStatement)) {
513 if (Lexer.is(AsmToken::Eof))
517 return ParseStatement();
520 case AsmToken::Equal:
521 // identifier '=' ... -> assignment statement
524 return ParseAssignment(IDVal);
526 default: // Normal instruction or directive.
530 // Otherwise, we have a normal instruction or directive.
531 if (IDVal[0] == '.') {
532 // FIXME: This should be driven based on a hash lookup and callback.
533 if (IDVal == ".section")
534 return ParseDirectiveDarwinSection();
535 if (IDVal == ".text")
536 // FIXME: This changes behavior based on the -static flag to the
538 return ParseDirectiveSectionSwitch("__TEXT", "__text",
539 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
540 if (IDVal == ".const")
541 return ParseDirectiveSectionSwitch("__TEXT", "__const");
542 if (IDVal == ".static_const")
543 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
544 if (IDVal == ".cstring")
545 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
546 MCSectionMachO::S_CSTRING_LITERALS);
547 if (IDVal == ".literal4")
548 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
549 MCSectionMachO::S_4BYTE_LITERALS,
551 if (IDVal == ".literal8")
552 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
553 MCSectionMachO::S_8BYTE_LITERALS,
555 if (IDVal == ".literal16")
556 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
557 MCSectionMachO::S_16BYTE_LITERALS,
559 if (IDVal == ".constructor")
560 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
561 if (IDVal == ".destructor")
562 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
563 if (IDVal == ".fvmlib_init0")
564 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
565 if (IDVal == ".fvmlib_init1")
566 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
568 // FIXME: The assembler manual claims that this has the self modify code
569 // flag, at least on x86-32, but that does not appear to be correct.
570 if (IDVal == ".symbol_stub")
571 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
572 MCSectionMachO::S_SYMBOL_STUBS |
573 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
574 // FIXME: Different on PPC and ARM.
576 // FIXME: PowerPC only?
577 if (IDVal == ".picsymbol_stub")
578 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
579 MCSectionMachO::S_SYMBOL_STUBS |
580 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
582 if (IDVal == ".data")
583 return ParseDirectiveSectionSwitch("__DATA", "__data");
584 if (IDVal == ".static_data")
585 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
587 // FIXME: The section names of these two are misspelled in the assembler
589 if (IDVal == ".non_lazy_symbol_pointer")
590 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
591 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
593 if (IDVal == ".lazy_symbol_pointer")
594 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
595 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
598 if (IDVal == ".dyld")
599 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
600 if (IDVal == ".mod_init_func")
601 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
602 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
604 if (IDVal == ".mod_term_func")
605 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
606 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
608 if (IDVal == ".const_data")
609 return ParseDirectiveSectionSwitch("__DATA", "__const");
612 if (IDVal == ".objc_class")
613 return ParseDirectiveSectionSwitch("__OBJC", "__class",
614 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
615 if (IDVal == ".objc_meta_class")
616 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
617 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
618 if (IDVal == ".objc_cat_cls_meth")
619 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
620 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
621 if (IDVal == ".objc_cat_inst_meth")
622 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
623 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
624 if (IDVal == ".objc_protocol")
625 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
626 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
627 if (IDVal == ".objc_string_object")
628 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
629 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
630 if (IDVal == ".objc_cls_meth")
631 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
632 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
633 if (IDVal == ".objc_inst_meth")
634 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
635 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
636 if (IDVal == ".objc_cls_refs")
637 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
638 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
639 MCSectionMachO::S_LITERAL_POINTERS,
641 if (IDVal == ".objc_message_refs")
642 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
643 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
644 MCSectionMachO::S_LITERAL_POINTERS,
646 if (IDVal == ".objc_symbols")
647 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
648 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
649 if (IDVal == ".objc_category")
650 return ParseDirectiveSectionSwitch("__OBJC", "__category",
651 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
652 if (IDVal == ".objc_class_vars")
653 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
654 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
655 if (IDVal == ".objc_instance_vars")
656 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
657 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
658 if (IDVal == ".objc_module_info")
659 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
660 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
661 if (IDVal == ".objc_class_names")
662 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
663 MCSectionMachO::S_CSTRING_LITERALS);
664 if (IDVal == ".objc_meth_var_types")
665 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
666 MCSectionMachO::S_CSTRING_LITERALS);
667 if (IDVal == ".objc_meth_var_names")
668 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
669 MCSectionMachO::S_CSTRING_LITERALS);
670 if (IDVal == ".objc_selector_strs")
671 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
672 MCSectionMachO::S_CSTRING_LITERALS);
674 if (IDVal == ".tdata")
675 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
676 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
678 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
679 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
680 if (IDVal == ".thread_init_func")
681 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
682 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
684 // Assembler features
686 return ParseDirectiveSet();
690 if (IDVal == ".ascii")
691 return ParseDirectiveAscii(false);
692 if (IDVal == ".asciz")
693 return ParseDirectiveAscii(true);
695 if (IDVal == ".byte")
696 return ParseDirectiveValue(1);
697 if (IDVal == ".short")
698 return ParseDirectiveValue(2);
699 if (IDVal == ".long")
700 return ParseDirectiveValue(4);
701 if (IDVal == ".quad")
702 return ParseDirectiveValue(8);
704 // FIXME: Target hooks for IsPow2.
705 if (IDVal == ".align")
706 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
707 if (IDVal == ".align32")
708 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
709 if (IDVal == ".balign")
710 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
711 if (IDVal == ".balignw")
712 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
713 if (IDVal == ".balignl")
714 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
715 if (IDVal == ".p2align")
716 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
717 if (IDVal == ".p2alignw")
718 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
719 if (IDVal == ".p2alignl")
720 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
723 return ParseDirectiveOrg();
725 if (IDVal == ".fill")
726 return ParseDirectiveFill();
727 if (IDVal == ".space")
728 return ParseDirectiveSpace();
730 // Symbol attribute directives
732 if (IDVal == ".globl" || IDVal == ".global")
733 return ParseDirectiveSymbolAttribute(MCSA_Global);
734 if (IDVal == ".hidden")
735 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
736 if (IDVal == ".indirect_symbol")
737 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
738 if (IDVal == ".internal")
739 return ParseDirectiveSymbolAttribute(MCSA_Internal);
740 if (IDVal == ".lazy_reference")
741 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
742 if (IDVal == ".no_dead_strip")
743 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
744 if (IDVal == ".private_extern")
745 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
746 if (IDVal == ".protected")
747 return ParseDirectiveSymbolAttribute(MCSA_Protected);
748 if (IDVal == ".reference")
749 return ParseDirectiveSymbolAttribute(MCSA_Reference);
750 if (IDVal == ".type")
751 return ParseDirectiveELFType();
752 if (IDVal == ".weak")
753 return ParseDirectiveSymbolAttribute(MCSA_Weak);
754 if (IDVal == ".weak_definition")
755 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
756 if (IDVal == ".weak_reference")
757 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
759 if (IDVal == ".comm")
760 return ParseDirectiveComm(/*IsLocal=*/false);
761 if (IDVal == ".lcomm")
762 return ParseDirectiveComm(/*IsLocal=*/true);
763 if (IDVal == ".zerofill")
764 return ParseDirectiveDarwinZerofill();
765 if (IDVal == ".desc")
766 return ParseDirectiveDarwinSymbolDesc();
767 if (IDVal == ".lsym")
768 return ParseDirectiveDarwinLsym();
769 if (IDVal == ".tbss")
770 return ParseDirectiveDarwinTBSS();
772 if (IDVal == ".subsections_via_symbols")
773 return ParseDirectiveDarwinSubsectionsViaSymbols();
774 if (IDVal == ".abort")
775 return ParseDirectiveAbort();
776 if (IDVal == ".include")
777 return ParseDirectiveInclude();
778 if (IDVal == ".dump")
779 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
780 if (IDVal == ".load")
781 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
782 if (IDVal == ".secure_log_unique")
783 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
784 if (IDVal == ".secure_log_reset")
785 return ParseDirectiveDarwinSecureLogReset(IDLoc);
787 // Look up the handler in the handler table,
788 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
790 return (this->*Handler)(IDVal, IDLoc);
792 // Target hook for parsing target specific directives.
793 if (!getTargetParser().ParseDirective(ID))
796 Warning(IDLoc, "ignoring directive for now");
797 EatToEndOfStatement();
801 // Canonicalize the opcode to lower case.
802 SmallString<128> Opcode;
803 for (unsigned i = 0, e = IDVal.size(); i != e; ++i)
804 Opcode.push_back(tolower(IDVal[i]));
806 SmallVector<MCParsedAsmOperand*, 8> ParsedOperands;
807 bool HadError = getTargetParser().ParseInstruction(Opcode.str(), IDLoc,
809 if (!HadError && Lexer.isNot(AsmToken::EndOfStatement))
810 HadError = TokError("unexpected token in argument list");
812 // If parsing succeeded, match the instruction.
815 if (!getTargetParser().MatchInstruction(ParsedOperands, Inst)) {
816 // Emit the instruction on success.
817 Out.EmitInstruction(Inst);
819 // Otherwise emit a diagnostic about the match failure and set the error
822 // FIXME: We should give nicer diagnostics about the exact failure.
823 Error(IDLoc, "unrecognized instruction");
828 // If there was no error, consume the end-of-statement token. Otherwise this
829 // will be done by our caller.
833 // Free any parsed operands.
834 for (unsigned i = 0, e = ParsedOperands.size(); i != e; ++i)
835 delete ParsedOperands[i];
840 bool AsmParser::ParseAssignment(const StringRef &Name) {
841 // FIXME: Use better location, we should use proper tokens.
842 SMLoc EqualLoc = Lexer.getLoc();
845 if (ParseExpression(Value))
848 if (Lexer.isNot(AsmToken::EndOfStatement))
849 return TokError("unexpected token in assignment");
851 // Eat the end of statement marker.
854 // Validate that the LHS is allowed to be a variable (either it has not been
855 // used as a symbol, or it is an absolute symbol).
856 MCSymbol *Sym = getContext().LookupSymbol(Name);
858 // Diagnose assignment to a label.
860 // FIXME: Diagnostics. Note the location of the definition as a label.
861 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
862 if (Sym->isUndefined() && !Sym->isUsedInExpr())
863 ; // Allow redefinitions of undefined symbols only used in directives.
864 else if (!Sym->isUndefined() && !Sym->isAbsolute())
865 return Error(EqualLoc, "redefinition of '" + Name + "'");
866 else if (!Sym->isVariable())
867 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
868 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
869 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
872 Sym = CreateSymbol(Name);
874 // FIXME: Handle '.'.
876 Sym->setUsedInExpr(true);
878 // Do the assignment.
879 Out.EmitAssignment(Sym, Value);
887 bool AsmParser::ParseIdentifier(StringRef &Res) {
888 if (Lexer.isNot(AsmToken::Identifier) &&
889 Lexer.isNot(AsmToken::String))
892 Res = getTok().getIdentifier();
894 Lex(); // Consume the identifier token.
899 /// ParseDirectiveSet:
900 /// ::= .set identifier ',' expression
901 bool AsmParser::ParseDirectiveSet() {
904 if (ParseIdentifier(Name))
905 return TokError("expected identifier after '.set' directive");
907 if (Lexer.isNot(AsmToken::Comma))
908 return TokError("unexpected token in '.set'");
911 return ParseAssignment(Name);
914 /// ParseDirectiveSection:
915 /// ::= .section identifier (',' identifier)*
916 /// FIXME: This should actually parse out the segment, section, attributes and
917 /// sizeof_stub fields.
918 bool AsmParser::ParseDirectiveDarwinSection() {
919 SMLoc Loc = Lexer.getLoc();
921 StringRef SectionName;
922 if (ParseIdentifier(SectionName))
923 return Error(Loc, "expected identifier after '.section' directive");
925 // Verify there is a following comma.
926 if (!Lexer.is(AsmToken::Comma))
927 return TokError("unexpected token in '.section' directive");
929 std::string SectionSpec = SectionName;
932 // Add all the tokens until the end of the line, ParseSectionSpecifier will
934 StringRef EOL = Lexer.LexUntilEndOfStatement();
935 SectionSpec.append(EOL.begin(), EOL.end());
938 if (Lexer.isNot(AsmToken::EndOfStatement))
939 return TokError("unexpected token in '.section' directive");
943 StringRef Segment, Section;
944 unsigned TAA, StubSize;
945 std::string ErrorStr =
946 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
949 if (!ErrorStr.empty())
950 return Error(Loc, ErrorStr.c_str());
952 // FIXME: Arch specific.
953 bool isText = Segment == "__TEXT"; // FIXME: Hack.
954 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
955 isText ? SectionKind::getText()
956 : SectionKind::getDataRel()));
960 /// ParseDirectiveSectionSwitch -
961 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
963 unsigned TAA, unsigned Align,
965 if (Lexer.isNot(AsmToken::EndOfStatement))
966 return TokError("unexpected token in section switching directive");
969 // FIXME: Arch specific.
970 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
971 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
972 isText ? SectionKind::getText()
973 : SectionKind::getDataRel()));
975 // Set the implicit alignment, if any.
977 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
978 // alignment on the section (e.g., if one manually inserts bytes into the
979 // section, then just issueing the section switch directive will not realign
980 // the section. However, this is arguably more reasonable behavior, and there
981 // is no good reason for someone to intentionally emit incorrectly sized
982 // values into the implicitly aligned sections.
984 Out.EmitValueToAlignment(Align, 0, 1, 0);
989 bool AsmParser::ParseEscapedString(std::string &Data) {
990 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
993 StringRef Str = getTok().getStringContents();
994 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
995 if (Str[i] != '\\') {
1000 // Recognize escaped characters. Note that this escape semantics currently
1001 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1004 return TokError("unexpected backslash at end of string");
1006 // Recognize octal sequences.
1007 if ((unsigned) (Str[i] - '0') <= 7) {
1008 // Consume up to three octal characters.
1009 unsigned Value = Str[i] - '0';
1011 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1013 Value = Value * 8 + (Str[i] - '0');
1015 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1017 Value = Value * 8 + (Str[i] - '0');
1022 return TokError("invalid octal escape sequence (out of range)");
1024 Data += (unsigned char) Value;
1028 // Otherwise recognize individual escapes.
1031 // Just reject invalid escape sequences for now.
1032 return TokError("invalid escape sequence (unrecognized character)");
1034 case 'b': Data += '\b'; break;
1035 case 'f': Data += '\f'; break;
1036 case 'n': Data += '\n'; break;
1037 case 'r': Data += '\r'; break;
1038 case 't': Data += '\t'; break;
1039 case '"': Data += '"'; break;
1040 case '\\': Data += '\\'; break;
1047 /// ParseDirectiveAscii:
1048 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
1049 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
1050 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1052 if (Lexer.isNot(AsmToken::String))
1053 return TokError("expected string in '.ascii' or '.asciz' directive");
1056 if (ParseEscapedString(Data))
1059 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
1061 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1065 if (Lexer.is(AsmToken::EndOfStatement))
1068 if (Lexer.isNot(AsmToken::Comma))
1069 return TokError("unexpected token in '.ascii' or '.asciz' directive");
1078 /// ParseDirectiveValue
1079 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1080 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1081 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1083 const MCExpr *Value;
1084 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
1085 if (ParseExpression(Value))
1088 // Special case constant expressions to match code generator.
1089 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1090 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1092 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1094 if (Lexer.is(AsmToken::EndOfStatement))
1097 // FIXME: Improve diagnostic.
1098 if (Lexer.isNot(AsmToken::Comma))
1099 return TokError("unexpected token in directive");
1108 /// ParseDirectiveSpace
1109 /// ::= .space expression [ , expression ]
1110 bool AsmParser::ParseDirectiveSpace() {
1112 if (ParseAbsoluteExpression(NumBytes))
1115 int64_t FillExpr = 0;
1116 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1117 if (Lexer.isNot(AsmToken::Comma))
1118 return TokError("unexpected token in '.space' directive");
1121 if (ParseAbsoluteExpression(FillExpr))
1124 if (Lexer.isNot(AsmToken::EndOfStatement))
1125 return TokError("unexpected token in '.space' directive");
1131 return TokError("invalid number of bytes in '.space' directive");
1133 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1134 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1139 /// ParseDirectiveFill
1140 /// ::= .fill expression , expression , expression
1141 bool AsmParser::ParseDirectiveFill() {
1143 if (ParseAbsoluteExpression(NumValues))
1146 if (Lexer.isNot(AsmToken::Comma))
1147 return TokError("unexpected token in '.fill' directive");
1151 if (ParseAbsoluteExpression(FillSize))
1154 if (Lexer.isNot(AsmToken::Comma))
1155 return TokError("unexpected token in '.fill' directive");
1159 if (ParseAbsoluteExpression(FillExpr))
1162 if (Lexer.isNot(AsmToken::EndOfStatement))
1163 return TokError("unexpected token in '.fill' directive");
1167 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1168 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1170 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1171 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1176 /// ParseDirectiveOrg
1177 /// ::= .org expression [ , expression ]
1178 bool AsmParser::ParseDirectiveOrg() {
1179 const MCExpr *Offset;
1180 if (ParseExpression(Offset))
1183 // Parse optional fill expression.
1184 int64_t FillExpr = 0;
1185 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1186 if (Lexer.isNot(AsmToken::Comma))
1187 return TokError("unexpected token in '.org' directive");
1190 if (ParseAbsoluteExpression(FillExpr))
1193 if (Lexer.isNot(AsmToken::EndOfStatement))
1194 return TokError("unexpected token in '.org' directive");
1199 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1200 // has to be relative to the current section.
1201 Out.EmitValueToOffset(Offset, FillExpr);
1206 /// ParseDirectiveAlign
1207 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1208 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1209 SMLoc AlignmentLoc = Lexer.getLoc();
1211 if (ParseAbsoluteExpression(Alignment))
1215 bool HasFillExpr = false;
1216 int64_t FillExpr = 0;
1217 int64_t MaxBytesToFill = 0;
1218 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1219 if (Lexer.isNot(AsmToken::Comma))
1220 return TokError("unexpected token in directive");
1223 // The fill expression can be omitted while specifying a maximum number of
1224 // alignment bytes, e.g:
1226 if (Lexer.isNot(AsmToken::Comma)) {
1228 if (ParseAbsoluteExpression(FillExpr))
1232 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1233 if (Lexer.isNot(AsmToken::Comma))
1234 return TokError("unexpected token in directive");
1237 MaxBytesLoc = Lexer.getLoc();
1238 if (ParseAbsoluteExpression(MaxBytesToFill))
1241 if (Lexer.isNot(AsmToken::EndOfStatement))
1242 return TokError("unexpected token in directive");
1251 // Compute alignment in bytes.
1253 // FIXME: Diagnose overflow.
1254 if (Alignment >= 32) {
1255 Error(AlignmentLoc, "invalid alignment value");
1259 Alignment = 1ULL << Alignment;
1262 // Diagnose non-sensical max bytes to align.
1263 if (MaxBytesLoc.isValid()) {
1264 if (MaxBytesToFill < 1) {
1265 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1266 "many bytes, ignoring maximum bytes expression");
1270 if (MaxBytesToFill >= Alignment) {
1271 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1277 // Check whether we should use optimal code alignment for this .align
1280 // FIXME: This should be using a target hook.
1281 bool UseCodeAlign = false;
1282 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1283 Out.getCurrentSection()))
1284 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1285 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1286 ValueSize == 1 && UseCodeAlign) {
1287 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1289 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1290 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1296 /// ParseDirectiveSymbolAttribute
1297 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1298 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1299 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1303 if (ParseIdentifier(Name))
1304 return TokError("expected identifier in directive");
1306 MCSymbol *Sym = CreateSymbol(Name);
1308 Out.EmitSymbolAttribute(Sym, Attr);
1310 if (Lexer.is(AsmToken::EndOfStatement))
1313 if (Lexer.isNot(AsmToken::Comma))
1314 return TokError("unexpected token in directive");
1323 /// ParseDirectiveELFType
1324 /// ::= .type identifier , @attribute
1325 bool AsmParser::ParseDirectiveELFType() {
1327 if (ParseIdentifier(Name))
1328 return TokError("expected identifier in directive");
1330 // Handle the identifier as the key symbol.
1331 MCSymbol *Sym = CreateSymbol(Name);
1333 if (Lexer.isNot(AsmToken::Comma))
1334 return TokError("unexpected token in '.type' directive");
1337 if (Lexer.isNot(AsmToken::At))
1338 return TokError("expected '@' before type");
1344 TypeLoc = Lexer.getLoc();
1345 if (ParseIdentifier(Type))
1346 return TokError("expected symbol type in directive");
1348 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1349 .Case("function", MCSA_ELF_TypeFunction)
1350 .Case("object", MCSA_ELF_TypeObject)
1351 .Case("tls_object", MCSA_ELF_TypeTLS)
1352 .Case("common", MCSA_ELF_TypeCommon)
1353 .Case("notype", MCSA_ELF_TypeNoType)
1354 .Default(MCSA_Invalid);
1356 if (Attr == MCSA_Invalid)
1357 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1359 if (Lexer.isNot(AsmToken::EndOfStatement))
1360 return TokError("unexpected token in '.type' directive");
1364 Out.EmitSymbolAttribute(Sym, Attr);
1369 /// ParseDirectiveDarwinSymbolDesc
1370 /// ::= .desc identifier , expression
1371 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1373 if (ParseIdentifier(Name))
1374 return TokError("expected identifier in directive");
1376 // Handle the identifier as the key symbol.
1377 MCSymbol *Sym = CreateSymbol(Name);
1379 if (Lexer.isNot(AsmToken::Comma))
1380 return TokError("unexpected token in '.desc' directive");
1384 if (ParseAbsoluteExpression(DescValue))
1387 if (Lexer.isNot(AsmToken::EndOfStatement))
1388 return TokError("unexpected token in '.desc' directive");
1392 // Set the n_desc field of this Symbol to this DescValue
1393 Out.EmitSymbolDesc(Sym, DescValue);
1398 /// ParseDirectiveComm
1399 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1400 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1401 SMLoc IDLoc = Lexer.getLoc();
1403 if (ParseIdentifier(Name))
1404 return TokError("expected identifier in directive");
1406 // Handle the identifier as the key symbol.
1407 MCSymbol *Sym = CreateSymbol(Name);
1409 if (Lexer.isNot(AsmToken::Comma))
1410 return TokError("unexpected token in directive");
1414 SMLoc SizeLoc = Lexer.getLoc();
1415 if (ParseAbsoluteExpression(Size))
1418 int64_t Pow2Alignment = 0;
1419 SMLoc Pow2AlignmentLoc;
1420 if (Lexer.is(AsmToken::Comma)) {
1422 Pow2AlignmentLoc = Lexer.getLoc();
1423 if (ParseAbsoluteExpression(Pow2Alignment))
1426 // If this target takes alignments in bytes (not log) validate and convert.
1427 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1428 if (!isPowerOf2_64(Pow2Alignment))
1429 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1430 Pow2Alignment = Log2_64(Pow2Alignment);
1434 if (Lexer.isNot(AsmToken::EndOfStatement))
1435 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1439 // NOTE: a size of zero for a .comm should create a undefined symbol
1440 // but a size of .lcomm creates a bss symbol of size zero.
1442 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1443 "be less than zero");
1445 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1446 // may internally end up wanting an alignment in bytes.
1447 // FIXME: Diagnose overflow.
1448 if (Pow2Alignment < 0)
1449 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1450 "alignment, can't be less than zero");
1452 if (!Sym->isUndefined())
1453 return Error(IDLoc, "invalid symbol redefinition");
1455 // '.lcomm' is equivalent to '.zerofill'.
1456 // Create the Symbol as a common or local common with Size and Pow2Alignment
1458 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1459 MCSectionMachO::S_ZEROFILL, 0,
1460 SectionKind::getBSS()),
1461 Sym, Size, 1 << Pow2Alignment);
1465 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1469 /// ParseDirectiveDarwinZerofill
1470 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1471 /// , align_expression ]]
1472 bool AsmParser::ParseDirectiveDarwinZerofill() {
1474 if (ParseIdentifier(Segment))
1475 return TokError("expected segment name after '.zerofill' directive");
1477 if (Lexer.isNot(AsmToken::Comma))
1478 return TokError("unexpected token in directive");
1482 if (ParseIdentifier(Section))
1483 return TokError("expected section name after comma in '.zerofill' "
1486 // If this is the end of the line all that was wanted was to create the
1487 // the section but with no symbol.
1488 if (Lexer.is(AsmToken::EndOfStatement)) {
1489 // Create the zerofill section but no symbol
1490 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1491 MCSectionMachO::S_ZEROFILL, 0,
1492 SectionKind::getBSS()));
1496 if (Lexer.isNot(AsmToken::Comma))
1497 return TokError("unexpected token in directive");
1500 SMLoc IDLoc = Lexer.getLoc();
1502 if (ParseIdentifier(IDStr))
1503 return TokError("expected identifier in directive");
1505 // handle the identifier as the key symbol.
1506 MCSymbol *Sym = CreateSymbol(IDStr);
1508 if (Lexer.isNot(AsmToken::Comma))
1509 return TokError("unexpected token in directive");
1513 SMLoc SizeLoc = Lexer.getLoc();
1514 if (ParseAbsoluteExpression(Size))
1517 int64_t Pow2Alignment = 0;
1518 SMLoc Pow2AlignmentLoc;
1519 if (Lexer.is(AsmToken::Comma)) {
1521 Pow2AlignmentLoc = Lexer.getLoc();
1522 if (ParseAbsoluteExpression(Pow2Alignment))
1526 if (Lexer.isNot(AsmToken::EndOfStatement))
1527 return TokError("unexpected token in '.zerofill' directive");
1532 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1535 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1536 // may internally end up wanting an alignment in bytes.
1537 // FIXME: Diagnose overflow.
1538 if (Pow2Alignment < 0)
1539 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1540 "can't be less than zero");
1542 if (!Sym->isUndefined())
1543 return Error(IDLoc, "invalid symbol redefinition");
1545 // Create the zerofill Symbol with Size and Pow2Alignment
1547 // FIXME: Arch specific.
1548 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1549 MCSectionMachO::S_ZEROFILL, 0,
1550 SectionKind::getBSS()),
1551 Sym, Size, 1 << Pow2Alignment);
1556 /// ParseDirectiveDarwinTBSS
1557 /// ::= .tbss identifier, size, align
1558 bool AsmParser::ParseDirectiveDarwinTBSS() {
1559 SMLoc IDLoc = Lexer.getLoc();
1561 if (ParseIdentifier(Name))
1562 return TokError("expected identifier in directive");
1564 // Handle the identifier as the key symbol.
1565 MCSymbol *Sym = CreateSymbol(Name);
1567 if (Lexer.isNot(AsmToken::Comma))
1568 return TokError("unexpected token in directive");
1572 SMLoc SizeLoc = Lexer.getLoc();
1573 if (ParseAbsoluteExpression(Size))
1576 int64_t Pow2Alignment = 0;
1577 SMLoc Pow2AlignmentLoc;
1578 if (Lexer.is(AsmToken::Comma)) {
1580 Pow2AlignmentLoc = Lexer.getLoc();
1581 if (ParseAbsoluteExpression(Pow2Alignment))
1585 if (Lexer.isNot(AsmToken::EndOfStatement))
1586 return TokError("unexpected token in '.tbss' directive");
1591 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1594 // FIXME: Diagnose overflow.
1595 if (Pow2Alignment < 0)
1596 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1599 if (!Sym->isUndefined())
1600 return Error(IDLoc, "invalid symbol redefinition");
1602 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1603 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1604 0, SectionKind::getThreadBSS()),
1605 Sym, Size, 1 << Pow2Alignment);
1610 /// ParseDirectiveDarwinSubsectionsViaSymbols
1611 /// ::= .subsections_via_symbols
1612 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1613 if (Lexer.isNot(AsmToken::EndOfStatement))
1614 return TokError("unexpected token in '.subsections_via_symbols' directive");
1618 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1623 /// ParseDirectiveAbort
1624 /// ::= .abort [ "abort_string" ]
1625 bool AsmParser::ParseDirectiveAbort() {
1626 // FIXME: Use loc from directive.
1627 SMLoc Loc = Lexer.getLoc();
1630 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1631 if (Lexer.isNot(AsmToken::String))
1632 return TokError("expected string in '.abort' directive");
1634 Str = getTok().getString();
1639 if (Lexer.isNot(AsmToken::EndOfStatement))
1640 return TokError("unexpected token in '.abort' directive");
1644 // FIXME: Handle here.
1646 Error(Loc, ".abort detected. Assembly stopping.");
1648 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1653 /// ParseDirectiveLsym
1654 /// ::= .lsym identifier , expression
1655 bool AsmParser::ParseDirectiveDarwinLsym() {
1657 if (ParseIdentifier(Name))
1658 return TokError("expected identifier in directive");
1660 // Handle the identifier as the key symbol.
1661 MCSymbol *Sym = CreateSymbol(Name);
1663 if (Lexer.isNot(AsmToken::Comma))
1664 return TokError("unexpected token in '.lsym' directive");
1667 const MCExpr *Value;
1668 if (ParseExpression(Value))
1671 if (Lexer.isNot(AsmToken::EndOfStatement))
1672 return TokError("unexpected token in '.lsym' directive");
1676 // We don't currently support this directive.
1678 // FIXME: Diagnostic location!
1680 return TokError("directive '.lsym' is unsupported");
1683 /// ParseDirectiveInclude
1684 /// ::= .include "filename"
1685 bool AsmParser::ParseDirectiveInclude() {
1686 if (Lexer.isNot(AsmToken::String))
1687 return TokError("expected string in '.include' directive");
1689 std::string Filename = getTok().getString();
1690 SMLoc IncludeLoc = Lexer.getLoc();
1693 if (Lexer.isNot(AsmToken::EndOfStatement))
1694 return TokError("unexpected token in '.include' directive");
1696 // Strip the quotes.
1697 Filename = Filename.substr(1, Filename.size()-2);
1699 // Attempt to switch the lexer to the included file before consuming the end
1700 // of statement to avoid losing it when we switch.
1701 if (EnterIncludeFile(Filename)) {
1702 PrintMessage(IncludeLoc,
1703 "Could not find include file '" + Filename + "'",
1711 /// ParseDirectiveDarwinDumpOrLoad
1712 /// ::= ( .dump | .load ) "filename"
1713 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1714 if (Lexer.isNot(AsmToken::String))
1715 return TokError("expected string in '.dump' or '.load' directive");
1719 if (Lexer.isNot(AsmToken::EndOfStatement))
1720 return TokError("unexpected token in '.dump' or '.load' directive");
1724 // FIXME: If/when .dump and .load are implemented they will be done in the
1725 // the assembly parser and not have any need for an MCStreamer API.
1727 Warning(IDLoc, "ignoring directive .dump for now");
1729 Warning(IDLoc, "ignoring directive .load for now");
1734 /// ParseDirectiveDarwinSecureLogUnique
1735 /// ::= .secure_log_unique "log message"
1736 bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1737 std::string LogMessage;
1739 if (Lexer.isNot(AsmToken::String))
1742 LogMessage = getTok().getString();
1746 if (Lexer.isNot(AsmToken::EndOfStatement))
1747 return TokError("unexpected token in '.secure_log_unique' directive");
1749 if (getContext().getSecureLogUsed() != false)
1750 return Error(IDLoc, ".secure_log_unique specified multiple times");
1752 char *SecureLogFile = getContext().getSecureLogFile();
1753 if (SecureLogFile == NULL)
1754 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1755 "environment variable unset.");
1757 raw_ostream *OS = getContext().getSecureLog();
1760 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1763 return Error(IDLoc, Twine("can't open secure log file: ") +
1764 SecureLogFile + " (" + Err + ")");
1766 getContext().setSecureLog(OS);
1769 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1770 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1771 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1772 << LogMessage + "\n";
1774 getContext().setSecureLogUsed(true);
1779 /// ParseDirectiveDarwinSecureLogReset
1780 /// ::= .secure_log_reset
1781 bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1782 if (Lexer.isNot(AsmToken::EndOfStatement))
1783 return TokError("unexpected token in '.secure_log_reset' directive");
1787 getContext().setSecureLogUsed(false);
1792 /// ParseDirectiveIf
1793 /// ::= .if expression
1794 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1795 TheCondStack.push_back(TheCondState);
1796 TheCondState.TheCond = AsmCond::IfCond;
1797 if(TheCondState.Ignore) {
1798 EatToEndOfStatement();
1802 if (ParseAbsoluteExpression(ExprValue))
1805 if (Lexer.isNot(AsmToken::EndOfStatement))
1806 return TokError("unexpected token in '.if' directive");
1810 TheCondState.CondMet = ExprValue;
1811 TheCondState.Ignore = !TheCondState.CondMet;
1817 /// ParseDirectiveElseIf
1818 /// ::= .elseif expression
1819 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1820 if (TheCondState.TheCond != AsmCond::IfCond &&
1821 TheCondState.TheCond != AsmCond::ElseIfCond)
1822 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1824 TheCondState.TheCond = AsmCond::ElseIfCond;
1826 bool LastIgnoreState = false;
1827 if (!TheCondStack.empty())
1828 LastIgnoreState = TheCondStack.back().Ignore;
1829 if (LastIgnoreState || TheCondState.CondMet) {
1830 TheCondState.Ignore = true;
1831 EatToEndOfStatement();
1835 if (ParseAbsoluteExpression(ExprValue))
1838 if (Lexer.isNot(AsmToken::EndOfStatement))
1839 return TokError("unexpected token in '.elseif' directive");
1842 TheCondState.CondMet = ExprValue;
1843 TheCondState.Ignore = !TheCondState.CondMet;
1849 /// ParseDirectiveElse
1851 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1852 if (Lexer.isNot(AsmToken::EndOfStatement))
1853 return TokError("unexpected token in '.else' directive");
1857 if (TheCondState.TheCond != AsmCond::IfCond &&
1858 TheCondState.TheCond != AsmCond::ElseIfCond)
1859 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1861 TheCondState.TheCond = AsmCond::ElseCond;
1862 bool LastIgnoreState = false;
1863 if (!TheCondStack.empty())
1864 LastIgnoreState = TheCondStack.back().Ignore;
1865 if (LastIgnoreState || TheCondState.CondMet)
1866 TheCondState.Ignore = true;
1868 TheCondState.Ignore = false;
1873 /// ParseDirectiveEndIf
1875 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1876 if (Lexer.isNot(AsmToken::EndOfStatement))
1877 return TokError("unexpected token in '.endif' directive");
1881 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1882 TheCondStack.empty())
1883 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1885 if (!TheCondStack.empty()) {
1886 TheCondState = TheCondStack.back();
1887 TheCondStack.pop_back();
1893 /// ParseDirectiveFile
1894 /// ::= .file [number] string
1895 bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
1896 // FIXME: I'm not sure what this is.
1897 int64_t FileNumber = -1;
1898 if (Lexer.is(AsmToken::Integer)) {
1899 FileNumber = getTok().getIntVal();
1903 return TokError("file number less than one");
1906 if (Lexer.isNot(AsmToken::String))
1907 return TokError("unexpected token in '.file' directive");
1909 StringRef Filename = getTok().getString();
1910 Filename = Filename.substr(1, Filename.size()-2);
1913 if (Lexer.isNot(AsmToken::EndOfStatement))
1914 return TokError("unexpected token in '.file' directive");
1916 if (FileNumber == -1)
1917 Out.EmitFileDirective(Filename);
1919 Out.EmitDwarfFileDirective(FileNumber, Filename);
1924 /// ParseDirectiveLine
1925 /// ::= .line [number]
1926 bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
1927 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1928 if (Lexer.isNot(AsmToken::Integer))
1929 return TokError("unexpected token in '.line' directive");
1931 int64_t LineNumber = getTok().getIntVal();
1935 // FIXME: Do something with the .line.
1938 if (Lexer.isNot(AsmToken::EndOfStatement))
1939 return TokError("unexpected token in '.line' directive");
1945 /// ParseDirectiveLoc
1946 /// ::= .loc number [number [number]]
1947 bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
1948 if (Lexer.isNot(AsmToken::Integer))
1949 return TokError("unexpected token in '.loc' directive");
1951 // FIXME: What are these fields?
1952 int64_t FileNumber = getTok().getIntVal();
1954 // FIXME: Validate file.
1957 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1958 if (Lexer.isNot(AsmToken::Integer))
1959 return TokError("unexpected token in '.loc' directive");
1961 int64_t Param2 = getTok().getIntVal();
1965 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1966 if (Lexer.isNot(AsmToken::Integer))
1967 return TokError("unexpected token in '.loc' directive");
1969 int64_t Param3 = getTok().getIntVal();
1973 // FIXME: Do something with the .loc.
1977 if (Lexer.isNot(AsmToken::EndOfStatement))
1978 return TokError("unexpected token in '.file' directive");