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 void AsmParser::PrintMessage(SMLoc Loc, const std::string &Msg,
60 const char *Type) const {
61 SrcMgr.PrintMessage(Loc, Msg, Type);
64 bool AsmParser::EnterIncludeFile(const std::string &Filename) {
65 int NewBuf = SrcMgr.AddIncludeFile(Filename, Lexer.getLoc());
71 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer));
76 const AsmToken &AsmParser::Lex() {
77 const AsmToken *tok = &Lexer.Lex();
79 if (tok->is(AsmToken::Eof)) {
80 // If this is the end of an included file, pop the parent file off the
82 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
83 if (ParentIncludeLoc != SMLoc()) {
84 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
85 Lexer.setBuffer(SrcMgr.getMemoryBuffer(CurBuffer),
86 ParentIncludeLoc.getPointer());
91 if (tok->is(AsmToken::Error))
92 PrintMessage(Lexer.getErrLoc(), Lexer.getErr(), "error");
97 bool AsmParser::Run(bool NoInitialTextSection, bool NoFinalize) {
98 // Create the initial section, if requested.
100 // FIXME: Target hook & command line option for initial section.
101 if (!NoInitialTextSection)
102 Out.SwitchSection(Ctx.getMachOSection("__TEXT", "__text",
103 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
104 0, SectionKind::getText()));
109 bool HadError = false;
111 AsmCond StartingCondState = TheCondState;
113 // While we have input, parse each statement.
114 while (Lexer.isNot(AsmToken::Eof)) {
115 if (!ParseStatement()) continue;
117 // We had an error, remember it and recover by skipping to the next line.
119 EatToEndOfStatement();
122 if (TheCondState.TheCond != StartingCondState.TheCond ||
123 TheCondState.Ignore != StartingCondState.Ignore)
124 return TokError("unmatched .ifs or .elses");
126 // Finalize the output stream if there are no errors and if the client wants
128 if (!HadError && !NoFinalize)
134 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
135 void AsmParser::EatToEndOfStatement() {
136 while (Lexer.isNot(AsmToken::EndOfStatement) &&
137 Lexer.isNot(AsmToken::Eof))
141 if (Lexer.is(AsmToken::EndOfStatement))
146 /// ParseParenExpr - Parse a paren expression and return it.
147 /// NOTE: This assumes the leading '(' has already been consumed.
149 /// parenexpr ::= expr)
151 bool AsmParser::ParseParenExpr(const MCExpr *&Res, SMLoc &EndLoc) {
152 if (ParseExpression(Res)) return true;
153 if (Lexer.isNot(AsmToken::RParen))
154 return TokError("expected ')' in parentheses expression");
155 EndLoc = Lexer.getLoc();
160 MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
161 // FIXME: Inline into callers.
162 return Ctx.GetOrCreateSymbol(Name);
165 /// ParsePrimaryExpr - Parse a primary expression and return it.
166 /// primaryexpr ::= (parenexpr
167 /// primaryexpr ::= symbol
168 /// primaryexpr ::= number
169 /// primaryexpr ::= '.'
170 /// primaryexpr ::= ~,+,- primaryexpr
171 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
172 switch (Lexer.getKind()) {
174 return TokError("unknown token in expression");
175 case AsmToken::Exclaim:
176 Lex(); // Eat the operator.
177 if (ParsePrimaryExpr(Res, EndLoc))
179 Res = MCUnaryExpr::CreateLNot(Res, getContext());
181 case AsmToken::String:
182 case AsmToken::Identifier: {
183 // This is a symbol reference.
184 std::pair<StringRef, StringRef> Split = getTok().getIdentifier().split('@');
185 MCSymbol *Sym = CreateSymbol(Split.first);
187 // Mark the symbol as used in an expression.
188 Sym->setUsedInExpr(true);
190 // Lookup the symbol variant if used.
191 MCSymbolRefExpr::VariantKind Variant = MCSymbolRefExpr::VK_None;
192 if (Split.first.size() != getTok().getIdentifier().size())
193 Variant = MCSymbolRefExpr::getVariantKindForName(Split.second);
195 EndLoc = Lexer.getLoc();
196 Lex(); // Eat identifier.
198 // If this is an absolute variable reference, substitute it now to preserve
199 // semantics in the face of reassignment.
200 if (Sym->isVariable() && isa<MCConstantExpr>(Sym->getVariableValue())) {
202 return Error(EndLoc, "unexpected modified on variable reference");
204 Res = Sym->getVariableValue();
208 // Otherwise create a symbol ref.
209 Res = MCSymbolRefExpr::Create(Sym, Variant, getContext());
212 case AsmToken::Integer: {
213 SMLoc Loc = getTok().getLoc();
214 int64_t IntVal = getTok().getIntVal();
215 Res = MCConstantExpr::Create(IntVal, getContext());
216 EndLoc = Lexer.getLoc();
218 // Look for 'b' or 'f' following an Integer as a directional label
219 if (Lexer.getKind() == AsmToken::Identifier) {
220 StringRef IDVal = getTok().getString();
221 if (IDVal == "f" || IDVal == "b"){
222 MCSymbol *Sym = Ctx.GetDirectionalLocalSymbol(IntVal,
223 IDVal == "f" ? 1 : 0);
224 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None,
226 if(IDVal == "b" && Sym->isUndefined())
227 return Error(Loc, "invalid reference to undefined symbol");
228 EndLoc = Lexer.getLoc();
229 Lex(); // Eat identifier.
234 case AsmToken::Dot: {
235 // This is a '.' reference, which references the current PC. Emit a
236 // temporary label to the streamer and refer to it.
237 MCSymbol *Sym = Ctx.CreateTempSymbol();
239 Res = MCSymbolRefExpr::Create(Sym, MCSymbolRefExpr::VK_None, getContext());
240 EndLoc = Lexer.getLoc();
241 Lex(); // Eat identifier.
245 case AsmToken::LParen:
246 Lex(); // Eat the '('.
247 return ParseParenExpr(Res, EndLoc);
248 case AsmToken::Minus:
249 Lex(); // Eat the operator.
250 if (ParsePrimaryExpr(Res, EndLoc))
252 Res = MCUnaryExpr::CreateMinus(Res, getContext());
255 Lex(); // Eat the operator.
256 if (ParsePrimaryExpr(Res, EndLoc))
258 Res = MCUnaryExpr::CreatePlus(Res, getContext());
260 case AsmToken::Tilde:
261 Lex(); // Eat the operator.
262 if (ParsePrimaryExpr(Res, EndLoc))
264 Res = MCUnaryExpr::CreateNot(Res, getContext());
269 bool AsmParser::ParseExpression(const MCExpr *&Res) {
271 return ParseExpression(Res, EndLoc);
274 /// ParseExpression - Parse an expression and return it.
276 /// expr ::= expr +,- expr -> lowest.
277 /// expr ::= expr |,^,&,! expr -> middle.
278 /// expr ::= expr *,/,%,<<,>> expr -> highest.
279 /// expr ::= primaryexpr
281 bool AsmParser::ParseExpression(const MCExpr *&Res, SMLoc &EndLoc) {
282 // Parse the expression.
284 if (ParsePrimaryExpr(Res, EndLoc) || ParseBinOpRHS(1, Res, EndLoc))
287 // Try to constant fold it up front, if possible.
289 if (Res->EvaluateAsAbsolute(Value))
290 Res = MCConstantExpr::Create(Value, getContext());
295 bool AsmParser::ParseParenExpression(const MCExpr *&Res, SMLoc &EndLoc) {
297 return ParseParenExpr(Res, EndLoc) ||
298 ParseBinOpRHS(1, Res, EndLoc);
301 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
304 SMLoc StartLoc = Lexer.getLoc();
305 if (ParseExpression(Expr))
308 if (!Expr->EvaluateAsAbsolute(Res))
309 return Error(StartLoc, "expected absolute expression");
314 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
315 MCBinaryExpr::Opcode &Kind) {
318 return 0; // not a binop.
320 // Lowest Precedence: &&, ||
321 case AsmToken::AmpAmp:
322 Kind = MCBinaryExpr::LAnd;
324 case AsmToken::PipePipe:
325 Kind = MCBinaryExpr::LOr;
328 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
330 Kind = MCBinaryExpr::Add;
332 case AsmToken::Minus:
333 Kind = MCBinaryExpr::Sub;
335 case AsmToken::EqualEqual:
336 Kind = MCBinaryExpr::EQ;
338 case AsmToken::ExclaimEqual:
339 case AsmToken::LessGreater:
340 Kind = MCBinaryExpr::NE;
343 Kind = MCBinaryExpr::LT;
345 case AsmToken::LessEqual:
346 Kind = MCBinaryExpr::LTE;
348 case AsmToken::Greater:
349 Kind = MCBinaryExpr::GT;
351 case AsmToken::GreaterEqual:
352 Kind = MCBinaryExpr::GTE;
355 // Intermediate Precedence: |, &, ^
357 // FIXME: gas seems to support '!' as an infix operator?
359 Kind = MCBinaryExpr::Or;
361 case AsmToken::Caret:
362 Kind = MCBinaryExpr::Xor;
365 Kind = MCBinaryExpr::And;
368 // Highest Precedence: *, /, %, <<, >>
370 Kind = MCBinaryExpr::Mul;
372 case AsmToken::Slash:
373 Kind = MCBinaryExpr::Div;
375 case AsmToken::Percent:
376 Kind = MCBinaryExpr::Mod;
378 case AsmToken::LessLess:
379 Kind = MCBinaryExpr::Shl;
381 case AsmToken::GreaterGreater:
382 Kind = MCBinaryExpr::Shr;
388 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
389 /// Res contains the LHS of the expression on input.
390 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res,
393 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
394 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
396 // If the next token is lower precedence than we are allowed to eat, return
397 // successfully with what we ate already.
398 if (TokPrec < Precedence)
403 // Eat the next primary expression.
405 if (ParsePrimaryExpr(RHS, EndLoc)) return true;
407 // If BinOp binds less tightly with RHS than the operator after RHS, let
408 // the pending operator take RHS as its LHS.
409 MCBinaryExpr::Opcode Dummy;
410 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
411 if (TokPrec < NextTokPrec) {
412 if (ParseBinOpRHS(Precedence+1, RHS, EndLoc)) return true;
415 // Merge LHS and RHS according to operator.
416 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
424 /// ::= EndOfStatement
425 /// ::= Label* Directive ...Operands... EndOfStatement
426 /// ::= Label* Identifier OperandList* EndOfStatement
427 bool AsmParser::ParseStatement() {
428 if (Lexer.is(AsmToken::EndOfStatement)) {
434 // Statements always start with an identifier.
435 AsmToken ID = getTok();
436 SMLoc IDLoc = ID.getLoc();
438 int64_t LocalLabelVal = -1;
439 // GUESS allow an integer followed by a ':' as a directional local label
440 if (Lexer.is(AsmToken::Integer)) {
441 LocalLabelVal = getTok().getIntVal();
442 if (LocalLabelVal < 0) {
443 if (!TheCondState.Ignore)
444 return TokError("unexpected token at start of statement");
448 IDVal = getTok().getString();
449 Lex(); // Consume the integer token to be used as an identifier token.
450 if (Lexer.getKind() != AsmToken::Colon) {
451 if (!TheCondState.Ignore)
452 return TokError("unexpected token at start of statement");
456 else if (ParseIdentifier(IDVal)) {
457 if (!TheCondState.Ignore)
458 return TokError("unexpected token at start of statement");
462 // Handle conditional assembly here before checking for skipping. We
463 // have to do this so that .endif isn't skipped in a ".if 0" block for
466 return ParseDirectiveIf(IDLoc);
467 if (IDVal == ".elseif")
468 return ParseDirectiveElseIf(IDLoc);
469 if (IDVal == ".else")
470 return ParseDirectiveElse(IDLoc);
471 if (IDVal == ".endif")
472 return ParseDirectiveEndIf(IDLoc);
474 // If we are in a ".if 0" block, ignore this statement.
475 if (TheCondState.Ignore) {
476 EatToEndOfStatement();
480 // FIXME: Recurse on local labels?
482 // See what kind of statement we have.
483 switch (Lexer.getKind()) {
484 case AsmToken::Colon: {
485 // identifier ':' -> Label.
488 // Diagnose attempt to use a variable as a label.
490 // FIXME: Diagnostics. Note the location of the definition as a label.
491 // FIXME: This doesn't diagnose assignment to a symbol which has been
492 // implicitly marked as external.
494 if (LocalLabelVal == -1)
495 Sym = CreateSymbol(IDVal);
497 Sym = Ctx.CreateDirectionalLocalSymbol(LocalLabelVal);
498 if (!Sym->isUndefined() || Sym->isVariable())
499 return Error(IDLoc, "invalid symbol redefinition");
504 // Consume any end of statement token, if present, to avoid spurious
505 // AddBlankLine calls().
506 if (Lexer.is(AsmToken::EndOfStatement)) {
508 if (Lexer.is(AsmToken::Eof))
512 return ParseStatement();
515 case AsmToken::Equal:
516 // identifier '=' ... -> assignment statement
519 return ParseAssignment(IDVal);
521 default: // Normal instruction or directive.
525 // Otherwise, we have a normal instruction or directive.
526 if (IDVal[0] == '.') {
527 // FIXME: This should be driven based on a hash lookup and callback.
528 if (IDVal == ".section")
529 return ParseDirectiveDarwinSection();
530 if (IDVal == ".text")
531 // FIXME: This changes behavior based on the -static flag to the
533 return ParseDirectiveSectionSwitch("__TEXT", "__text",
534 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
535 if (IDVal == ".const")
536 return ParseDirectiveSectionSwitch("__TEXT", "__const");
537 if (IDVal == ".static_const")
538 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
539 if (IDVal == ".cstring")
540 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
541 MCSectionMachO::S_CSTRING_LITERALS);
542 if (IDVal == ".literal4")
543 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
544 MCSectionMachO::S_4BYTE_LITERALS,
546 if (IDVal == ".literal8")
547 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
548 MCSectionMachO::S_8BYTE_LITERALS,
550 if (IDVal == ".literal16")
551 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
552 MCSectionMachO::S_16BYTE_LITERALS,
554 if (IDVal == ".constructor")
555 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
556 if (IDVal == ".destructor")
557 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
558 if (IDVal == ".fvmlib_init0")
559 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
560 if (IDVal == ".fvmlib_init1")
561 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
563 // FIXME: The assembler manual claims that this has the self modify code
564 // flag, at least on x86-32, but that does not appear to be correct.
565 if (IDVal == ".symbol_stub")
566 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
567 MCSectionMachO::S_SYMBOL_STUBS |
568 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
569 // FIXME: Different on PPC and ARM.
571 // FIXME: PowerPC only?
572 if (IDVal == ".picsymbol_stub")
573 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
574 MCSectionMachO::S_SYMBOL_STUBS |
575 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
577 if (IDVal == ".data")
578 return ParseDirectiveSectionSwitch("__DATA", "__data");
579 if (IDVal == ".static_data")
580 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
582 // FIXME: The section names of these two are misspelled in the assembler
584 if (IDVal == ".non_lazy_symbol_pointer")
585 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
586 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
588 if (IDVal == ".lazy_symbol_pointer")
589 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
590 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
593 if (IDVal == ".dyld")
594 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
595 if (IDVal == ".mod_init_func")
596 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
597 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
599 if (IDVal == ".mod_term_func")
600 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
601 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
603 if (IDVal == ".const_data")
604 return ParseDirectiveSectionSwitch("__DATA", "__const");
607 if (IDVal == ".objc_class")
608 return ParseDirectiveSectionSwitch("__OBJC", "__class",
609 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
610 if (IDVal == ".objc_meta_class")
611 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
612 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
613 if (IDVal == ".objc_cat_cls_meth")
614 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
615 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
616 if (IDVal == ".objc_cat_inst_meth")
617 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
618 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
619 if (IDVal == ".objc_protocol")
620 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
621 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
622 if (IDVal == ".objc_string_object")
623 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
624 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
625 if (IDVal == ".objc_cls_meth")
626 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
627 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
628 if (IDVal == ".objc_inst_meth")
629 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
630 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
631 if (IDVal == ".objc_cls_refs")
632 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
633 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
634 MCSectionMachO::S_LITERAL_POINTERS,
636 if (IDVal == ".objc_message_refs")
637 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
638 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
639 MCSectionMachO::S_LITERAL_POINTERS,
641 if (IDVal == ".objc_symbols")
642 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
643 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
644 if (IDVal == ".objc_category")
645 return ParseDirectiveSectionSwitch("__OBJC", "__category",
646 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
647 if (IDVal == ".objc_class_vars")
648 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
649 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
650 if (IDVal == ".objc_instance_vars")
651 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
652 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
653 if (IDVal == ".objc_module_info")
654 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
655 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
656 if (IDVal == ".objc_class_names")
657 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
658 MCSectionMachO::S_CSTRING_LITERALS);
659 if (IDVal == ".objc_meth_var_types")
660 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
661 MCSectionMachO::S_CSTRING_LITERALS);
662 if (IDVal == ".objc_meth_var_names")
663 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
664 MCSectionMachO::S_CSTRING_LITERALS);
665 if (IDVal == ".objc_selector_strs")
666 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
667 MCSectionMachO::S_CSTRING_LITERALS);
669 if (IDVal == ".tdata")
670 return ParseDirectiveSectionSwitch("__DATA", "__thread_data",
671 MCSectionMachO::S_THREAD_LOCAL_REGULAR);
673 return ParseDirectiveSectionSwitch("__DATA", "__thread_vars",
674 MCSectionMachO::S_THREAD_LOCAL_VARIABLES);
675 if (IDVal == ".thread_init_func")
676 return ParseDirectiveSectionSwitch("__DATA", "__thread_init",
677 MCSectionMachO::S_THREAD_LOCAL_INIT_FUNCTION_POINTERS);
679 // Assembler features
681 return ParseDirectiveSet();
685 if (IDVal == ".ascii")
686 return ParseDirectiveAscii(false);
687 if (IDVal == ".asciz")
688 return ParseDirectiveAscii(true);
690 if (IDVal == ".byte")
691 return ParseDirectiveValue(1);
692 if (IDVal == ".short")
693 return ParseDirectiveValue(2);
694 if (IDVal == ".long")
695 return ParseDirectiveValue(4);
696 if (IDVal == ".quad")
697 return ParseDirectiveValue(8);
699 // FIXME: Target hooks for IsPow2.
700 if (IDVal == ".align")
701 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
702 if (IDVal == ".align32")
703 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
704 if (IDVal == ".balign")
705 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
706 if (IDVal == ".balignw")
707 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
708 if (IDVal == ".balignl")
709 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
710 if (IDVal == ".p2align")
711 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
712 if (IDVal == ".p2alignw")
713 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
714 if (IDVal == ".p2alignl")
715 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
718 return ParseDirectiveOrg();
720 if (IDVal == ".fill")
721 return ParseDirectiveFill();
722 if (IDVal == ".space")
723 return ParseDirectiveSpace();
725 // Symbol attribute directives
727 if (IDVal == ".globl" || IDVal == ".global")
728 return ParseDirectiveSymbolAttribute(MCSA_Global);
729 if (IDVal == ".hidden")
730 return ParseDirectiveSymbolAttribute(MCSA_Hidden);
731 if (IDVal == ".indirect_symbol")
732 return ParseDirectiveSymbolAttribute(MCSA_IndirectSymbol);
733 if (IDVal == ".internal")
734 return ParseDirectiveSymbolAttribute(MCSA_Internal);
735 if (IDVal == ".lazy_reference")
736 return ParseDirectiveSymbolAttribute(MCSA_LazyReference);
737 if (IDVal == ".no_dead_strip")
738 return ParseDirectiveSymbolAttribute(MCSA_NoDeadStrip);
739 if (IDVal == ".private_extern")
740 return ParseDirectiveSymbolAttribute(MCSA_PrivateExtern);
741 if (IDVal == ".protected")
742 return ParseDirectiveSymbolAttribute(MCSA_Protected);
743 if (IDVal == ".reference")
744 return ParseDirectiveSymbolAttribute(MCSA_Reference);
745 if (IDVal == ".type")
746 return ParseDirectiveELFType();
747 if (IDVal == ".weak")
748 return ParseDirectiveSymbolAttribute(MCSA_Weak);
749 if (IDVal == ".weak_definition")
750 return ParseDirectiveSymbolAttribute(MCSA_WeakDefinition);
751 if (IDVal == ".weak_reference")
752 return ParseDirectiveSymbolAttribute(MCSA_WeakReference);
753 if (IDVal == ".weak_def_can_be_hidden")
754 return ParseDirectiveSymbolAttribute(MCSA_WeakDefAutoPrivate);
756 if (IDVal == ".comm")
757 return ParseDirectiveComm(/*IsLocal=*/false);
758 if (IDVal == ".lcomm")
759 return ParseDirectiveComm(/*IsLocal=*/true);
760 if (IDVal == ".zerofill")
761 return ParseDirectiveDarwinZerofill();
762 if (IDVal == ".desc")
763 return ParseDirectiveDarwinSymbolDesc();
764 if (IDVal == ".lsym")
765 return ParseDirectiveDarwinLsym();
766 if (IDVal == ".tbss")
767 return ParseDirectiveDarwinTBSS();
769 if (IDVal == ".subsections_via_symbols")
770 return ParseDirectiveDarwinSubsectionsViaSymbols();
771 if (IDVal == ".abort")
772 return ParseDirectiveAbort();
773 if (IDVal == ".include")
774 return ParseDirectiveInclude();
775 if (IDVal == ".dump")
776 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
777 if (IDVal == ".load")
778 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
779 if (IDVal == ".secure_log_unique")
780 return ParseDirectiveDarwinSecureLogUnique(IDLoc);
781 if (IDVal == ".secure_log_reset")
782 return ParseDirectiveDarwinSecureLogReset(IDLoc);
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 if (ParseExpression(Value))
845 if (Lexer.isNot(AsmToken::EndOfStatement))
846 return TokError("unexpected token in assignment");
848 // Eat the end of statement marker.
851 // Validate that the LHS is allowed to be a variable (either it has not been
852 // used as a symbol, or it is an absolute symbol).
853 MCSymbol *Sym = getContext().LookupSymbol(Name);
855 // Diagnose assignment to a label.
857 // FIXME: Diagnostics. Note the location of the definition as a label.
858 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
859 if (Sym->isUndefined() && !Sym->isUsedInExpr())
860 ; // Allow redefinitions of undefined symbols only used in directives.
861 else if (!Sym->isUndefined() && !Sym->isAbsolute())
862 return Error(EqualLoc, "redefinition of '" + Name + "'");
863 else if (!Sym->isVariable())
864 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
865 else if (!isa<MCConstantExpr>(Sym->getVariableValue()))
866 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
869 Sym = CreateSymbol(Name);
871 // FIXME: Handle '.'.
873 Sym->setUsedInExpr(true);
875 // Do the assignment.
876 Out.EmitAssignment(Sym, Value);
884 bool AsmParser::ParseIdentifier(StringRef &Res) {
885 if (Lexer.isNot(AsmToken::Identifier) &&
886 Lexer.isNot(AsmToken::String))
889 Res = getTok().getIdentifier();
891 Lex(); // Consume the identifier token.
896 /// ParseDirectiveSet:
897 /// ::= .set identifier ',' expression
898 bool AsmParser::ParseDirectiveSet() {
901 if (ParseIdentifier(Name))
902 return TokError("expected identifier after '.set' directive");
904 if (Lexer.isNot(AsmToken::Comma))
905 return TokError("unexpected token in '.set'");
908 return ParseAssignment(Name);
911 /// ParseDirectiveSection:
912 /// ::= .section identifier (',' identifier)*
913 /// FIXME: This should actually parse out the segment, section, attributes and
914 /// sizeof_stub fields.
915 bool AsmParser::ParseDirectiveDarwinSection() {
916 SMLoc Loc = Lexer.getLoc();
918 StringRef SectionName;
919 if (ParseIdentifier(SectionName))
920 return Error(Loc, "expected identifier after '.section' directive");
922 // Verify there is a following comma.
923 if (!Lexer.is(AsmToken::Comma))
924 return TokError("unexpected token in '.section' directive");
926 std::string SectionSpec = SectionName;
929 // Add all the tokens until the end of the line, ParseSectionSpecifier will
931 StringRef EOL = Lexer.LexUntilEndOfStatement();
932 SectionSpec.append(EOL.begin(), EOL.end());
935 if (Lexer.isNot(AsmToken::EndOfStatement))
936 return TokError("unexpected token in '.section' directive");
940 StringRef Segment, Section;
941 unsigned TAA, StubSize;
942 std::string ErrorStr =
943 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
946 if (!ErrorStr.empty())
947 return Error(Loc, ErrorStr.c_str());
949 // FIXME: Arch specific.
950 bool isText = Segment == "__TEXT"; // FIXME: Hack.
951 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
952 isText ? SectionKind::getText()
953 : SectionKind::getDataRel()));
957 /// ParseDirectiveSectionSwitch -
958 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
960 unsigned TAA, unsigned Align,
962 if (Lexer.isNot(AsmToken::EndOfStatement))
963 return TokError("unexpected token in section switching directive");
966 // FIXME: Arch specific.
967 bool isText = StringRef(Segment) == "__TEXT"; // FIXME: Hack.
968 Out.SwitchSection(Ctx.getMachOSection(Segment, Section, TAA, StubSize,
969 isText ? SectionKind::getText()
970 : SectionKind::getDataRel()));
972 // Set the implicit alignment, if any.
974 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
975 // alignment on the section (e.g., if one manually inserts bytes into the
976 // section, then just issueing the section switch directive will not realign
977 // the section. However, this is arguably more reasonable behavior, and there
978 // is no good reason for someone to intentionally emit incorrectly sized
979 // values into the implicitly aligned sections.
981 Out.EmitValueToAlignment(Align, 0, 1, 0);
986 bool AsmParser::ParseEscapedString(std::string &Data) {
987 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
990 StringRef Str = getTok().getStringContents();
991 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
992 if (Str[i] != '\\') {
997 // Recognize escaped characters. Note that this escape semantics currently
998 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
1001 return TokError("unexpected backslash at end of string");
1003 // Recognize octal sequences.
1004 if ((unsigned) (Str[i] - '0') <= 7) {
1005 // Consume up to three octal characters.
1006 unsigned Value = Str[i] - '0';
1008 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1010 Value = Value * 8 + (Str[i] - '0');
1012 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
1014 Value = Value * 8 + (Str[i] - '0');
1019 return TokError("invalid octal escape sequence (out of range)");
1021 Data += (unsigned char) Value;
1025 // Otherwise recognize individual escapes.
1028 // Just reject invalid escape sequences for now.
1029 return TokError("invalid escape sequence (unrecognized character)");
1031 case 'b': Data += '\b'; break;
1032 case 'f': Data += '\f'; break;
1033 case 'n': Data += '\n'; break;
1034 case 'r': Data += '\r'; break;
1035 case 't': Data += '\t'; break;
1036 case '"': Data += '"'; break;
1037 case '\\': Data += '\\'; break;
1044 /// ParseDirectiveAscii:
1045 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
1046 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
1047 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1049 if (Lexer.isNot(AsmToken::String))
1050 return TokError("expected string in '.ascii' or '.asciz' directive");
1053 if (ParseEscapedString(Data))
1056 Out.EmitBytes(Data, DEFAULT_ADDRSPACE);
1058 Out.EmitBytes(StringRef("\0", 1), DEFAULT_ADDRSPACE);
1062 if (Lexer.is(AsmToken::EndOfStatement))
1065 if (Lexer.isNot(AsmToken::Comma))
1066 return TokError("unexpected token in '.ascii' or '.asciz' directive");
1075 /// ParseDirectiveValue
1076 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
1077 bool AsmParser::ParseDirectiveValue(unsigned Size) {
1078 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1080 const MCExpr *Value;
1081 SMLoc ATTRIBUTE_UNUSED StartLoc = Lexer.getLoc();
1082 if (ParseExpression(Value))
1085 // Special case constant expressions to match code generator.
1086 if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value))
1087 Out.EmitIntValue(MCE->getValue(), Size, DEFAULT_ADDRSPACE);
1089 Out.EmitValue(Value, Size, DEFAULT_ADDRSPACE);
1091 if (Lexer.is(AsmToken::EndOfStatement))
1094 // FIXME: Improve diagnostic.
1095 if (Lexer.isNot(AsmToken::Comma))
1096 return TokError("unexpected token in directive");
1105 /// ParseDirectiveSpace
1106 /// ::= .space expression [ , expression ]
1107 bool AsmParser::ParseDirectiveSpace() {
1109 if (ParseAbsoluteExpression(NumBytes))
1112 int64_t FillExpr = 0;
1113 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1114 if (Lexer.isNot(AsmToken::Comma))
1115 return TokError("unexpected token in '.space' directive");
1118 if (ParseAbsoluteExpression(FillExpr))
1121 if (Lexer.isNot(AsmToken::EndOfStatement))
1122 return TokError("unexpected token in '.space' directive");
1128 return TokError("invalid number of bytes in '.space' directive");
1130 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1131 Out.EmitFill(NumBytes, FillExpr, DEFAULT_ADDRSPACE);
1136 /// ParseDirectiveFill
1137 /// ::= .fill expression , expression , expression
1138 bool AsmParser::ParseDirectiveFill() {
1140 if (ParseAbsoluteExpression(NumValues))
1143 if (Lexer.isNot(AsmToken::Comma))
1144 return TokError("unexpected token in '.fill' directive");
1148 if (ParseAbsoluteExpression(FillSize))
1151 if (Lexer.isNot(AsmToken::Comma))
1152 return TokError("unexpected token in '.fill' directive");
1156 if (ParseAbsoluteExpression(FillExpr))
1159 if (Lexer.isNot(AsmToken::EndOfStatement))
1160 return TokError("unexpected token in '.fill' directive");
1164 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1165 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1167 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1168 Out.EmitIntValue(FillExpr, FillSize, DEFAULT_ADDRSPACE);
1173 /// ParseDirectiveOrg
1174 /// ::= .org expression [ , expression ]
1175 bool AsmParser::ParseDirectiveOrg() {
1176 const MCExpr *Offset;
1177 if (ParseExpression(Offset))
1180 // Parse optional fill expression.
1181 int64_t FillExpr = 0;
1182 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1183 if (Lexer.isNot(AsmToken::Comma))
1184 return TokError("unexpected token in '.org' directive");
1187 if (ParseAbsoluteExpression(FillExpr))
1190 if (Lexer.isNot(AsmToken::EndOfStatement))
1191 return TokError("unexpected token in '.org' directive");
1196 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1197 // has to be relative to the current section.
1198 Out.EmitValueToOffset(Offset, FillExpr);
1203 /// ParseDirectiveAlign
1204 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1205 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1206 SMLoc AlignmentLoc = Lexer.getLoc();
1208 if (ParseAbsoluteExpression(Alignment))
1212 bool HasFillExpr = false;
1213 int64_t FillExpr = 0;
1214 int64_t MaxBytesToFill = 0;
1215 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1216 if (Lexer.isNot(AsmToken::Comma))
1217 return TokError("unexpected token in directive");
1220 // The fill expression can be omitted while specifying a maximum number of
1221 // alignment bytes, e.g:
1223 if (Lexer.isNot(AsmToken::Comma)) {
1225 if (ParseAbsoluteExpression(FillExpr))
1229 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1230 if (Lexer.isNot(AsmToken::Comma))
1231 return TokError("unexpected token in directive");
1234 MaxBytesLoc = Lexer.getLoc();
1235 if (ParseAbsoluteExpression(MaxBytesToFill))
1238 if (Lexer.isNot(AsmToken::EndOfStatement))
1239 return TokError("unexpected token in directive");
1248 // Compute alignment in bytes.
1250 // FIXME: Diagnose overflow.
1251 if (Alignment >= 32) {
1252 Error(AlignmentLoc, "invalid alignment value");
1256 Alignment = 1ULL << Alignment;
1259 // Diagnose non-sensical max bytes to align.
1260 if (MaxBytesLoc.isValid()) {
1261 if (MaxBytesToFill < 1) {
1262 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1263 "many bytes, ignoring maximum bytes expression");
1267 if (MaxBytesToFill >= Alignment) {
1268 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1274 // Check whether we should use optimal code alignment for this .align
1277 // FIXME: This should be using a target hook.
1278 bool UseCodeAlign = false;
1279 if (const MCSectionMachO *S = dyn_cast<MCSectionMachO>(
1280 Out.getCurrentSection()))
1281 UseCodeAlign = S->hasAttribute(MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
1282 if ((!HasFillExpr || Lexer.getMAI().getTextAlignFillValue() == FillExpr) &&
1283 ValueSize == 1 && UseCodeAlign) {
1284 Out.EmitCodeAlignment(Alignment, MaxBytesToFill);
1286 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1287 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1293 /// ParseDirectiveSymbolAttribute
1294 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1295 bool AsmParser::ParseDirectiveSymbolAttribute(MCSymbolAttr Attr) {
1296 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1300 if (ParseIdentifier(Name))
1301 return TokError("expected identifier in directive");
1303 MCSymbol *Sym = CreateSymbol(Name);
1305 Out.EmitSymbolAttribute(Sym, Attr);
1307 if (Lexer.is(AsmToken::EndOfStatement))
1310 if (Lexer.isNot(AsmToken::Comma))
1311 return TokError("unexpected token in directive");
1320 /// ParseDirectiveELFType
1321 /// ::= .type identifier , @attribute
1322 bool AsmParser::ParseDirectiveELFType() {
1324 if (ParseIdentifier(Name))
1325 return TokError("expected identifier in directive");
1327 // Handle the identifier as the key symbol.
1328 MCSymbol *Sym = CreateSymbol(Name);
1330 if (Lexer.isNot(AsmToken::Comma))
1331 return TokError("unexpected token in '.type' directive");
1334 if (Lexer.isNot(AsmToken::At))
1335 return TokError("expected '@' before type");
1341 TypeLoc = Lexer.getLoc();
1342 if (ParseIdentifier(Type))
1343 return TokError("expected symbol type in directive");
1345 MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Type)
1346 .Case("function", MCSA_ELF_TypeFunction)
1347 .Case("object", MCSA_ELF_TypeObject)
1348 .Case("tls_object", MCSA_ELF_TypeTLS)
1349 .Case("common", MCSA_ELF_TypeCommon)
1350 .Case("notype", MCSA_ELF_TypeNoType)
1351 .Default(MCSA_Invalid);
1353 if (Attr == MCSA_Invalid)
1354 return Error(TypeLoc, "unsupported attribute in '.type' directive");
1356 if (Lexer.isNot(AsmToken::EndOfStatement))
1357 return TokError("unexpected token in '.type' directive");
1361 Out.EmitSymbolAttribute(Sym, Attr);
1366 /// ParseDirectiveDarwinSymbolDesc
1367 /// ::= .desc identifier , expression
1368 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1370 if (ParseIdentifier(Name))
1371 return TokError("expected identifier in directive");
1373 // Handle the identifier as the key symbol.
1374 MCSymbol *Sym = CreateSymbol(Name);
1376 if (Lexer.isNot(AsmToken::Comma))
1377 return TokError("unexpected token in '.desc' directive");
1381 if (ParseAbsoluteExpression(DescValue))
1384 if (Lexer.isNot(AsmToken::EndOfStatement))
1385 return TokError("unexpected token in '.desc' directive");
1389 // Set the n_desc field of this Symbol to this DescValue
1390 Out.EmitSymbolDesc(Sym, DescValue);
1395 /// ParseDirectiveComm
1396 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1397 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1398 SMLoc IDLoc = Lexer.getLoc();
1400 if (ParseIdentifier(Name))
1401 return TokError("expected identifier in directive");
1403 // Handle the identifier as the key symbol.
1404 MCSymbol *Sym = CreateSymbol(Name);
1406 if (Lexer.isNot(AsmToken::Comma))
1407 return TokError("unexpected token in directive");
1411 SMLoc SizeLoc = Lexer.getLoc();
1412 if (ParseAbsoluteExpression(Size))
1415 int64_t Pow2Alignment = 0;
1416 SMLoc Pow2AlignmentLoc;
1417 if (Lexer.is(AsmToken::Comma)) {
1419 Pow2AlignmentLoc = Lexer.getLoc();
1420 if (ParseAbsoluteExpression(Pow2Alignment))
1423 // If this target takes alignments in bytes (not log) validate and convert.
1424 if (Lexer.getMAI().getAlignmentIsInBytes()) {
1425 if (!isPowerOf2_64(Pow2Alignment))
1426 return Error(Pow2AlignmentLoc, "alignment must be a power of 2");
1427 Pow2Alignment = Log2_64(Pow2Alignment);
1431 if (Lexer.isNot(AsmToken::EndOfStatement))
1432 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1436 // NOTE: a size of zero for a .comm should create a undefined symbol
1437 // but a size of .lcomm creates a bss symbol of size zero.
1439 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1440 "be less than zero");
1442 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1443 // may internally end up wanting an alignment in bytes.
1444 // FIXME: Diagnose overflow.
1445 if (Pow2Alignment < 0)
1446 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1447 "alignment, can't be less than zero");
1449 if (!Sym->isUndefined())
1450 return Error(IDLoc, "invalid symbol redefinition");
1452 // '.lcomm' is equivalent to '.zerofill'.
1453 // Create the Symbol as a common or local common with Size and Pow2Alignment
1455 Out.EmitZerofill(Ctx.getMachOSection("__DATA", "__bss",
1456 MCSectionMachO::S_ZEROFILL, 0,
1457 SectionKind::getBSS()),
1458 Sym, Size, 1 << Pow2Alignment);
1462 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1466 /// ParseDirectiveDarwinZerofill
1467 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1468 /// , align_expression ]]
1469 bool AsmParser::ParseDirectiveDarwinZerofill() {
1471 if (ParseIdentifier(Segment))
1472 return TokError("expected segment name after '.zerofill' directive");
1474 if (Lexer.isNot(AsmToken::Comma))
1475 return TokError("unexpected token in directive");
1479 if (ParseIdentifier(Section))
1480 return TokError("expected section name after comma in '.zerofill' "
1483 // If this is the end of the line all that was wanted was to create the
1484 // the section but with no symbol.
1485 if (Lexer.is(AsmToken::EndOfStatement)) {
1486 // Create the zerofill section but no symbol
1487 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1488 MCSectionMachO::S_ZEROFILL, 0,
1489 SectionKind::getBSS()));
1493 if (Lexer.isNot(AsmToken::Comma))
1494 return TokError("unexpected token in directive");
1497 SMLoc IDLoc = Lexer.getLoc();
1499 if (ParseIdentifier(IDStr))
1500 return TokError("expected identifier in directive");
1502 // handle the identifier as the key symbol.
1503 MCSymbol *Sym = CreateSymbol(IDStr);
1505 if (Lexer.isNot(AsmToken::Comma))
1506 return TokError("unexpected token in directive");
1510 SMLoc SizeLoc = Lexer.getLoc();
1511 if (ParseAbsoluteExpression(Size))
1514 int64_t Pow2Alignment = 0;
1515 SMLoc Pow2AlignmentLoc;
1516 if (Lexer.is(AsmToken::Comma)) {
1518 Pow2AlignmentLoc = Lexer.getLoc();
1519 if (ParseAbsoluteExpression(Pow2Alignment))
1523 if (Lexer.isNot(AsmToken::EndOfStatement))
1524 return TokError("unexpected token in '.zerofill' directive");
1529 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1532 // NOTE: The alignment in the directive is a power of 2 value, the assembler
1533 // may internally end up wanting an alignment in bytes.
1534 // FIXME: Diagnose overflow.
1535 if (Pow2Alignment < 0)
1536 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1537 "can't be less than zero");
1539 if (!Sym->isUndefined())
1540 return Error(IDLoc, "invalid symbol redefinition");
1542 // Create the zerofill Symbol with Size and Pow2Alignment
1544 // FIXME: Arch specific.
1545 Out.EmitZerofill(Ctx.getMachOSection(Segment, Section,
1546 MCSectionMachO::S_ZEROFILL, 0,
1547 SectionKind::getBSS()),
1548 Sym, Size, 1 << Pow2Alignment);
1553 /// ParseDirectiveDarwinTBSS
1554 /// ::= .tbss identifier, size, align
1555 bool AsmParser::ParseDirectiveDarwinTBSS() {
1556 SMLoc IDLoc = Lexer.getLoc();
1558 if (ParseIdentifier(Name))
1559 return TokError("expected identifier in directive");
1561 // Handle the identifier as the key symbol.
1562 MCSymbol *Sym = CreateSymbol(Name);
1564 if (Lexer.isNot(AsmToken::Comma))
1565 return TokError("unexpected token in directive");
1569 SMLoc SizeLoc = Lexer.getLoc();
1570 if (ParseAbsoluteExpression(Size))
1573 int64_t Pow2Alignment = 0;
1574 SMLoc Pow2AlignmentLoc;
1575 if (Lexer.is(AsmToken::Comma)) {
1577 Pow2AlignmentLoc = Lexer.getLoc();
1578 if (ParseAbsoluteExpression(Pow2Alignment))
1582 if (Lexer.isNot(AsmToken::EndOfStatement))
1583 return TokError("unexpected token in '.tbss' directive");
1588 return Error(SizeLoc, "invalid '.tbss' directive size, can't be less than"
1591 // FIXME: Diagnose overflow.
1592 if (Pow2Alignment < 0)
1593 return Error(Pow2AlignmentLoc, "invalid '.tbss' alignment, can't be less"
1596 if (!Sym->isUndefined())
1597 return Error(IDLoc, "invalid symbol redefinition");
1599 Out.EmitTBSSSymbol(Ctx.getMachOSection("__DATA", "__thread_bss",
1600 MCSectionMachO::S_THREAD_LOCAL_ZEROFILL,
1601 0, SectionKind::getThreadBSS()),
1602 Sym, Size, 1 << Pow2Alignment);
1607 /// ParseDirectiveDarwinSubsectionsViaSymbols
1608 /// ::= .subsections_via_symbols
1609 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1610 if (Lexer.isNot(AsmToken::EndOfStatement))
1611 return TokError("unexpected token in '.subsections_via_symbols' directive");
1615 Out.EmitAssemblerFlag(MCAF_SubsectionsViaSymbols);
1620 /// ParseDirectiveAbort
1621 /// ::= .abort [ "abort_string" ]
1622 bool AsmParser::ParseDirectiveAbort() {
1623 // FIXME: Use loc from directive.
1624 SMLoc Loc = Lexer.getLoc();
1627 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1628 if (Lexer.isNot(AsmToken::String))
1629 return TokError("expected string in '.abort' directive");
1631 Str = getTok().getString();
1636 if (Lexer.isNot(AsmToken::EndOfStatement))
1637 return TokError("unexpected token in '.abort' directive");
1641 // FIXME: Handle here.
1643 Error(Loc, ".abort detected. Assembly stopping.");
1645 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1650 /// ParseDirectiveLsym
1651 /// ::= .lsym identifier , expression
1652 bool AsmParser::ParseDirectiveDarwinLsym() {
1654 if (ParseIdentifier(Name))
1655 return TokError("expected identifier in directive");
1657 // Handle the identifier as the key symbol.
1658 MCSymbol *Sym = CreateSymbol(Name);
1660 if (Lexer.isNot(AsmToken::Comma))
1661 return TokError("unexpected token in '.lsym' directive");
1664 const MCExpr *Value;
1665 if (ParseExpression(Value))
1668 if (Lexer.isNot(AsmToken::EndOfStatement))
1669 return TokError("unexpected token in '.lsym' directive");
1673 // We don't currently support this directive.
1675 // FIXME: Diagnostic location!
1677 return TokError("directive '.lsym' is unsupported");
1680 /// ParseDirectiveInclude
1681 /// ::= .include "filename"
1682 bool AsmParser::ParseDirectiveInclude() {
1683 if (Lexer.isNot(AsmToken::String))
1684 return TokError("expected string in '.include' directive");
1686 std::string Filename = getTok().getString();
1687 SMLoc IncludeLoc = Lexer.getLoc();
1690 if (Lexer.isNot(AsmToken::EndOfStatement))
1691 return TokError("unexpected token in '.include' directive");
1693 // Strip the quotes.
1694 Filename = Filename.substr(1, Filename.size()-2);
1696 // Attempt to switch the lexer to the included file before consuming the end
1697 // of statement to avoid losing it when we switch.
1698 if (EnterIncludeFile(Filename)) {
1699 PrintMessage(IncludeLoc,
1700 "Could not find include file '" + Filename + "'",
1708 /// ParseDirectiveDarwinDumpOrLoad
1709 /// ::= ( .dump | .load ) "filename"
1710 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1711 if (Lexer.isNot(AsmToken::String))
1712 return TokError("expected string in '.dump' or '.load' directive");
1716 if (Lexer.isNot(AsmToken::EndOfStatement))
1717 return TokError("unexpected token in '.dump' or '.load' directive");
1721 // FIXME: If/when .dump and .load are implemented they will be done in the
1722 // the assembly parser and not have any need for an MCStreamer API.
1724 Warning(IDLoc, "ignoring directive .dump for now");
1726 Warning(IDLoc, "ignoring directive .load for now");
1731 /// ParseDirectiveDarwinSecureLogUnique
1732 /// ::= .secure_log_unique "log message"
1733 bool AsmParser::ParseDirectiveDarwinSecureLogUnique(SMLoc IDLoc) {
1734 std::string LogMessage;
1736 if (Lexer.isNot(AsmToken::String))
1739 LogMessage = getTok().getString();
1743 if (Lexer.isNot(AsmToken::EndOfStatement))
1744 return TokError("unexpected token in '.secure_log_unique' directive");
1746 if (getContext().getSecureLogUsed() != false)
1747 return Error(IDLoc, ".secure_log_unique specified multiple times");
1749 char *SecureLogFile = getContext().getSecureLogFile();
1750 if (SecureLogFile == NULL)
1751 return Error(IDLoc, ".secure_log_unique used but AS_SECURE_LOG_FILE "
1752 "environment variable unset.");
1754 raw_ostream *OS = getContext().getSecureLog();
1757 OS = new raw_fd_ostream(SecureLogFile, Err, raw_fd_ostream::F_Append);
1760 return Error(IDLoc, Twine("can't open secure log file: ") +
1761 SecureLogFile + " (" + Err + ")");
1763 getContext().setSecureLog(OS);
1766 int CurBuf = SrcMgr.FindBufferContainingLoc(IDLoc);
1767 *OS << SrcMgr.getBufferInfo(CurBuf).Buffer->getBufferIdentifier() << ":"
1768 << SrcMgr.FindLineNumber(IDLoc, CurBuf) << ":"
1769 << LogMessage + "\n";
1771 getContext().setSecureLogUsed(true);
1776 /// ParseDirectiveDarwinSecureLogReset
1777 /// ::= .secure_log_reset
1778 bool AsmParser::ParseDirectiveDarwinSecureLogReset(SMLoc IDLoc) {
1779 if (Lexer.isNot(AsmToken::EndOfStatement))
1780 return TokError("unexpected token in '.secure_log_reset' directive");
1784 getContext().setSecureLogUsed(false);
1789 /// ParseDirectiveIf
1790 /// ::= .if expression
1791 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1792 TheCondStack.push_back(TheCondState);
1793 TheCondState.TheCond = AsmCond::IfCond;
1794 if(TheCondState.Ignore) {
1795 EatToEndOfStatement();
1799 if (ParseAbsoluteExpression(ExprValue))
1802 if (Lexer.isNot(AsmToken::EndOfStatement))
1803 return TokError("unexpected token in '.if' directive");
1807 TheCondState.CondMet = ExprValue;
1808 TheCondState.Ignore = !TheCondState.CondMet;
1814 /// ParseDirectiveElseIf
1815 /// ::= .elseif expression
1816 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1817 if (TheCondState.TheCond != AsmCond::IfCond &&
1818 TheCondState.TheCond != AsmCond::ElseIfCond)
1819 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1821 TheCondState.TheCond = AsmCond::ElseIfCond;
1823 bool LastIgnoreState = false;
1824 if (!TheCondStack.empty())
1825 LastIgnoreState = TheCondStack.back().Ignore;
1826 if (LastIgnoreState || TheCondState.CondMet) {
1827 TheCondState.Ignore = true;
1828 EatToEndOfStatement();
1832 if (ParseAbsoluteExpression(ExprValue))
1835 if (Lexer.isNot(AsmToken::EndOfStatement))
1836 return TokError("unexpected token in '.elseif' directive");
1839 TheCondState.CondMet = ExprValue;
1840 TheCondState.Ignore = !TheCondState.CondMet;
1846 /// ParseDirectiveElse
1848 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1849 if (Lexer.isNot(AsmToken::EndOfStatement))
1850 return TokError("unexpected token in '.else' directive");
1854 if (TheCondState.TheCond != AsmCond::IfCond &&
1855 TheCondState.TheCond != AsmCond::ElseIfCond)
1856 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1858 TheCondState.TheCond = AsmCond::ElseCond;
1859 bool LastIgnoreState = false;
1860 if (!TheCondStack.empty())
1861 LastIgnoreState = TheCondStack.back().Ignore;
1862 if (LastIgnoreState || TheCondState.CondMet)
1863 TheCondState.Ignore = true;
1865 TheCondState.Ignore = false;
1870 /// ParseDirectiveEndIf
1872 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1873 if (Lexer.isNot(AsmToken::EndOfStatement))
1874 return TokError("unexpected token in '.endif' directive");
1878 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1879 TheCondStack.empty())
1880 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1882 if (!TheCondStack.empty()) {
1883 TheCondState = TheCondStack.back();
1884 TheCondStack.pop_back();
1890 /// ParseDirectiveFile
1891 /// ::= .file [number] string
1892 bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
1893 // FIXME: I'm not sure what this is.
1894 int64_t FileNumber = -1;
1895 if (Lexer.is(AsmToken::Integer)) {
1896 FileNumber = getTok().getIntVal();
1900 return TokError("file number less than one");
1903 if (Lexer.isNot(AsmToken::String))
1904 return TokError("unexpected token in '.file' directive");
1906 StringRef Filename = getTok().getString();
1907 Filename = Filename.substr(1, Filename.size()-2);
1910 if (Lexer.isNot(AsmToken::EndOfStatement))
1911 return TokError("unexpected token in '.file' directive");
1913 if (FileNumber == -1)
1914 Out.EmitFileDirective(Filename);
1916 Out.EmitDwarfFileDirective(FileNumber, Filename);
1921 /// ParseDirectiveLine
1922 /// ::= .line [number]
1923 bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
1924 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1925 if (Lexer.isNot(AsmToken::Integer))
1926 return TokError("unexpected token in '.line' directive");
1928 int64_t LineNumber = getTok().getIntVal();
1932 // FIXME: Do something with the .line.
1935 if (Lexer.isNot(AsmToken::EndOfStatement))
1936 return TokError("unexpected token in '.line' directive");
1942 /// ParseDirectiveLoc
1943 /// ::= .loc number [number [number]]
1944 bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
1945 if (Lexer.isNot(AsmToken::Integer))
1946 return TokError("unexpected token in '.loc' directive");
1948 // FIXME: What are these fields?
1949 int64_t FileNumber = getTok().getIntVal();
1951 // FIXME: Validate file.
1954 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1955 if (Lexer.isNot(AsmToken::Integer))
1956 return TokError("unexpected token in '.loc' directive");
1958 int64_t Param2 = getTok().getIntVal();
1962 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1963 if (Lexer.isNot(AsmToken::Integer))
1964 return TokError("unexpected token in '.loc' directive");
1966 int64_t Param3 = getTok().getIntVal();
1970 // FIXME: Do something with the .loc.
1974 if (Lexer.isNot(AsmToken::EndOfStatement))
1975 return TokError("unexpected token in '.file' directive");