1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the parser for assembly files.
12 //===----------------------------------------------------------------------===//
14 #include "AsmParser.h"
17 #include "llvm/ADT/Twine.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCSection.h"
21 #include "llvm/MC/MCStreamer.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Target/TargetAsmParser.h"
28 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
29 Lexer.PrintMessage(L, Msg.str(), "warning");
32 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
33 Lexer.PrintMessage(L, Msg.str(), "error");
37 bool AsmParser::TokError(const char *Msg) {
38 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
42 bool AsmParser::Run() {
46 bool HadError = false;
48 AsmCond StartingCondState = TheCondState;
50 // While we have input, parse each statement.
51 while (Lexer.isNot(AsmToken::Eof)) {
52 // Handle conditional assembly here before calling ParseStatement()
53 if (Lexer.getKind() == AsmToken::Identifier) {
54 // If we have an identifier, handle it as the key symbol.
55 AsmToken ID = Lexer.getTok();
56 SMLoc IDLoc = ID.getLoc();
57 StringRef IDVal = ID.getString();
63 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
66 EatToEndOfStatement();
70 if (TheCondState.Ignore) {
71 EatToEndOfStatement();
75 if (!ParseStatement()) continue;
77 // We had an error, remember it and recover by skipping to the next line.
79 EatToEndOfStatement();
82 if (TheCondState.TheCond != StartingCondState.TheCond ||
83 TheCondState.Ignore != StartingCondState.Ignore)
84 return TokError("unmatched .ifs or .elses");
89 /// ParseConditionalAssemblyDirectives - parse the conditional assembly
91 bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
93 if (Directive == ".if")
94 return ParseDirectiveIf(DirectiveLoc);
95 if (Directive == ".elseif")
96 return ParseDirectiveElseIf(DirectiveLoc);
97 if (Directive == ".else")
98 return ParseDirectiveElse(DirectiveLoc);
99 if (Directive == ".endif")
100 return ParseDirectiveEndIf(DirectiveLoc);
104 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
105 void AsmParser::EatToEndOfStatement() {
106 while (Lexer.isNot(AsmToken::EndOfStatement) &&
107 Lexer.isNot(AsmToken::Eof))
111 if (Lexer.is(AsmToken::EndOfStatement))
116 /// ParseParenExpr - Parse a paren expression and return it.
117 /// NOTE: This assumes the leading '(' has already been consumed.
119 /// parenexpr ::= expr)
121 bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
122 if (ParseExpression(Res)) return true;
123 if (Lexer.isNot(AsmToken::RParen))
124 return TokError("expected ')' in parentheses expression");
129 /// ParsePrimaryExpr - Parse a primary expression and return it.
130 /// primaryexpr ::= (parenexpr
131 /// primaryexpr ::= symbol
132 /// primaryexpr ::= number
133 /// primaryexpr ::= ~,+,- primaryexpr
134 bool AsmParser::ParsePrimaryExpr(AsmExpr *&Res) {
135 switch (Lexer.getKind()) {
137 return TokError("unknown token in expression");
138 case AsmToken::Exclaim:
139 Lexer.Lex(); // Eat the operator.
140 if (ParsePrimaryExpr(Res))
142 Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
144 case AsmToken::String:
145 case AsmToken::Identifier: {
146 // This is a label, this should be parsed as part of an expression, to
147 // handle things like LFOO+4.
148 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
150 // If this is use of an undefined symbol then mark it external.
151 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
152 Sym->setExternal(true);
154 Res = new AsmSymbolRefExpr(Sym);
155 Lexer.Lex(); // Eat identifier.
158 case AsmToken::Integer:
159 Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
160 Lexer.Lex(); // Eat token.
162 case AsmToken::LParen:
163 Lexer.Lex(); // Eat the '('.
164 return ParseParenExpr(Res);
165 case AsmToken::Minus:
166 Lexer.Lex(); // Eat the operator.
167 if (ParsePrimaryExpr(Res))
169 Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
172 Lexer.Lex(); // Eat the operator.
173 if (ParsePrimaryExpr(Res))
175 Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
177 case AsmToken::Tilde:
178 Lexer.Lex(); // Eat the operator.
179 if (ParsePrimaryExpr(Res))
181 Res = new AsmUnaryExpr(AsmUnaryExpr::Not, Res);
186 /// ParseExpression - Parse an expression and return it.
188 /// expr ::= expr +,- expr -> lowest.
189 /// expr ::= expr |,^,&,! expr -> middle.
190 /// expr ::= expr *,/,%,<<,>> expr -> highest.
191 /// expr ::= primaryexpr
193 bool AsmParser::ParseExpression(AsmExpr *&Res) {
195 return ParsePrimaryExpr(Res) ||
196 ParseBinOpRHS(1, Res);
199 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
202 SMLoc StartLoc = Lexer.getLoc();
203 if (ParseExpression(Expr))
206 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
207 return Error(StartLoc, "expected absolute expression");
212 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
215 SMLoc StartLoc = Lexer.getLoc();
216 if (ParseExpression(Expr))
219 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
220 return Error(StartLoc, "expected relocatable expression");
225 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
228 SMLoc StartLoc = Lexer.getLoc();
229 if (ParseParenExpr(Expr))
232 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
233 return Error(StartLoc, "expected relocatable expression");
238 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
239 AsmBinaryExpr::Opcode &Kind) {
241 default: return 0; // not a binop.
243 // Lowest Precedence: &&, ||
244 case AsmToken::AmpAmp:
245 Kind = AsmBinaryExpr::LAnd;
247 case AsmToken::PipePipe:
248 Kind = AsmBinaryExpr::LOr;
251 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
253 Kind = AsmBinaryExpr::Add;
255 case AsmToken::Minus:
256 Kind = AsmBinaryExpr::Sub;
258 case AsmToken::EqualEqual:
259 Kind = AsmBinaryExpr::EQ;
261 case AsmToken::ExclaimEqual:
262 case AsmToken::LessGreater:
263 Kind = AsmBinaryExpr::NE;
266 Kind = AsmBinaryExpr::LT;
268 case AsmToken::LessEqual:
269 Kind = AsmBinaryExpr::LTE;
271 case AsmToken::Greater:
272 Kind = AsmBinaryExpr::GT;
274 case AsmToken::GreaterEqual:
275 Kind = AsmBinaryExpr::GTE;
278 // Intermediate Precedence: |, &, ^
280 // FIXME: gas seems to support '!' as an infix operator?
282 Kind = AsmBinaryExpr::Or;
284 case AsmToken::Caret:
285 Kind = AsmBinaryExpr::Xor;
288 Kind = AsmBinaryExpr::And;
291 // Highest Precedence: *, /, %, <<, >>
293 Kind = AsmBinaryExpr::Mul;
295 case AsmToken::Slash:
296 Kind = AsmBinaryExpr::Div;
298 case AsmToken::Percent:
299 Kind = AsmBinaryExpr::Mod;
301 case AsmToken::LessLess:
302 Kind = AsmBinaryExpr::Shl;
304 case AsmToken::GreaterGreater:
305 Kind = AsmBinaryExpr::Shr;
311 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
312 /// Res contains the LHS of the expression on input.
313 bool AsmParser::ParseBinOpRHS(unsigned Precedence, AsmExpr *&Res) {
315 AsmBinaryExpr::Opcode Kind = AsmBinaryExpr::Add;
316 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
318 // If the next token is lower precedence than we are allowed to eat, return
319 // successfully with what we ate already.
320 if (TokPrec < Precedence)
325 // Eat the next primary expression.
327 if (ParsePrimaryExpr(RHS)) return true;
329 // If BinOp binds less tightly with RHS than the operator after RHS, let
330 // the pending operator take RHS as its LHS.
331 AsmBinaryExpr::Opcode Dummy;
332 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
333 if (TokPrec < NextTokPrec) {
334 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
337 // Merge LHS and RHS according to operator.
338 Res = new AsmBinaryExpr(Kind, Res, RHS);
346 /// ::= EndOfStatement
347 /// ::= Label* Directive ...Operands... EndOfStatement
348 /// ::= Label* Identifier OperandList* EndOfStatement
349 bool AsmParser::ParseStatement() {
350 if (Lexer.is(AsmToken::EndOfStatement)) {
355 // Statements always start with an identifier.
356 AsmToken ID = Lexer.getTok();
357 SMLoc IDLoc = ID.getLoc();
359 if (ParseIdentifier(IDVal))
360 return TokError("unexpected token at start of statement");
362 // FIXME: Recurse on local labels?
364 // See what kind of statement we have.
365 switch (Lexer.getKind()) {
366 case AsmToken::Colon: {
367 // identifier ':' -> Label.
370 // Diagnose attempt to use a variable as a label.
372 // FIXME: Diagnostics. Note the location of the definition as a label.
373 // FIXME: This doesn't diagnose assignment to a symbol which has been
374 // implicitly marked as external.
375 MCSymbol *Sym = Ctx.GetOrCreateSymbol(IDVal);
376 if (Sym->getSection())
377 return Error(IDLoc, "invalid symbol redefinition");
378 if (Ctx.GetSymbolValue(Sym))
379 return Error(IDLoc, "symbol already used as assembler variable");
381 // Since we saw a label, create a symbol and emit it.
382 // FIXME: If the label starts with L it is an assembler temporary label.
383 // Why does the client of this api need to know this?
386 return ParseStatement();
389 case AsmToken::Equal:
390 // identifier '=' ... -> assignment statement
393 return ParseAssignment(IDVal, false);
395 default: // Normal instruction or directive.
399 // Otherwise, we have a normal instruction or directive.
400 if (IDVal[0] == '.') {
401 // FIXME: This should be driven based on a hash lookup and callback.
402 if (IDVal == ".section")
403 return ParseDirectiveDarwinSection();
404 if (IDVal == ".text")
405 // FIXME: This changes behavior based on the -static flag to the
407 return ParseDirectiveSectionSwitch("__TEXT,__text",
408 "regular,pure_instructions");
409 if (IDVal == ".const")
410 return ParseDirectiveSectionSwitch("__TEXT,__const");
411 if (IDVal == ".static_const")
412 return ParseDirectiveSectionSwitch("__TEXT,__static_const");
413 if (IDVal == ".cstring")
414 return ParseDirectiveSectionSwitch("__TEXT,__cstring",
416 if (IDVal == ".literal4")
417 return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
418 if (IDVal == ".literal8")
419 return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
420 if (IDVal == ".literal16")
421 return ParseDirectiveSectionSwitch("__TEXT,__literal16",
423 if (IDVal == ".constructor")
424 return ParseDirectiveSectionSwitch("__TEXT,__constructor");
425 if (IDVal == ".destructor")
426 return ParseDirectiveSectionSwitch("__TEXT,__destructor");
427 if (IDVal == ".fvmlib_init0")
428 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
429 if (IDVal == ".fvmlib_init1")
430 return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
431 if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
432 return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
433 "self_modifying_code+pure_instructions,5");
434 // FIXME: .picsymbol_stub on PPC.
435 if (IDVal == ".data")
436 return ParseDirectiveSectionSwitch("__DATA,__data");
437 if (IDVal == ".static_data")
438 return ParseDirectiveSectionSwitch("__DATA,__static_data");
439 if (IDVal == ".non_lazy_symbol_pointer")
440 return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
441 "non_lazy_symbol_pointers");
442 if (IDVal == ".lazy_symbol_pointer")
443 return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
444 "lazy_symbol_pointers");
445 if (IDVal == ".dyld")
446 return ParseDirectiveSectionSwitch("__DATA,__dyld");
447 if (IDVal == ".mod_init_func")
448 return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
450 if (IDVal == ".mod_term_func")
451 return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
453 if (IDVal == ".const_data")
454 return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
457 // FIXME: Verify attributes on sections.
458 if (IDVal == ".objc_class")
459 return ParseDirectiveSectionSwitch("__OBJC,__class");
460 if (IDVal == ".objc_meta_class")
461 return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
462 if (IDVal == ".objc_cat_cls_meth")
463 return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
464 if (IDVal == ".objc_cat_inst_meth")
465 return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
466 if (IDVal == ".objc_protocol")
467 return ParseDirectiveSectionSwitch("__OBJC,__protocol");
468 if (IDVal == ".objc_string_object")
469 return ParseDirectiveSectionSwitch("__OBJC,__string_object");
470 if (IDVal == ".objc_cls_meth")
471 return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
472 if (IDVal == ".objc_inst_meth")
473 return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
474 if (IDVal == ".objc_cls_refs")
475 return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
476 if (IDVal == ".objc_message_refs")
477 return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
478 if (IDVal == ".objc_symbols")
479 return ParseDirectiveSectionSwitch("__OBJC,__symbols");
480 if (IDVal == ".objc_category")
481 return ParseDirectiveSectionSwitch("__OBJC,__category");
482 if (IDVal == ".objc_class_vars")
483 return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
484 if (IDVal == ".objc_instance_vars")
485 return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
486 if (IDVal == ".objc_module_info")
487 return ParseDirectiveSectionSwitch("__OBJC,__module_info");
488 if (IDVal == ".objc_class_names")
489 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
490 if (IDVal == ".objc_meth_var_types")
491 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
492 if (IDVal == ".objc_meth_var_names")
493 return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
494 if (IDVal == ".objc_selector_strs")
495 return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
497 // Assembler features
499 return ParseDirectiveSet();
503 if (IDVal == ".ascii")
504 return ParseDirectiveAscii(false);
505 if (IDVal == ".asciz")
506 return ParseDirectiveAscii(true);
508 // FIXME: Target hooks for size? Also for "word", "hword".
509 if (IDVal == ".byte")
510 return ParseDirectiveValue(1);
511 if (IDVal == ".short")
512 return ParseDirectiveValue(2);
513 if (IDVal == ".long")
514 return ParseDirectiveValue(4);
515 if (IDVal == ".quad")
516 return ParseDirectiveValue(8);
518 // FIXME: Target hooks for IsPow2.
519 if (IDVal == ".align")
520 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
521 if (IDVal == ".align32")
522 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
523 if (IDVal == ".balign")
524 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
525 if (IDVal == ".balignw")
526 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
527 if (IDVal == ".balignl")
528 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
529 if (IDVal == ".p2align")
530 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
531 if (IDVal == ".p2alignw")
532 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
533 if (IDVal == ".p2alignl")
534 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
537 return ParseDirectiveOrg();
539 if (IDVal == ".fill")
540 return ParseDirectiveFill();
541 if (IDVal == ".space")
542 return ParseDirectiveSpace();
544 // Symbol attribute directives
545 if (IDVal == ".globl" || IDVal == ".global")
546 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
547 if (IDVal == ".hidden")
548 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
549 if (IDVal == ".indirect_symbol")
550 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
551 if (IDVal == ".internal")
552 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
553 if (IDVal == ".lazy_reference")
554 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
555 if (IDVal == ".no_dead_strip")
556 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
557 if (IDVal == ".private_extern")
558 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
559 if (IDVal == ".protected")
560 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
561 if (IDVal == ".reference")
562 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
563 if (IDVal == ".weak")
564 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
565 if (IDVal == ".weak_definition")
566 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
567 if (IDVal == ".weak_reference")
568 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
570 if (IDVal == ".comm")
571 return ParseDirectiveComm(/*IsLocal=*/false);
572 if (IDVal == ".lcomm")
573 return ParseDirectiveComm(/*IsLocal=*/true);
574 if (IDVal == ".zerofill")
575 return ParseDirectiveDarwinZerofill();
576 if (IDVal == ".desc")
577 return ParseDirectiveDarwinSymbolDesc();
578 if (IDVal == ".lsym")
579 return ParseDirectiveDarwinLsym();
581 if (IDVal == ".subsections_via_symbols")
582 return ParseDirectiveDarwinSubsectionsViaSymbols();
583 if (IDVal == ".abort")
584 return ParseDirectiveAbort();
585 if (IDVal == ".include")
586 return ParseDirectiveInclude();
587 if (IDVal == ".dump")
588 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
589 if (IDVal == ".load")
590 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
592 Warning(IDLoc, "ignoring directive for now");
593 EatToEndOfStatement();
598 if (getTargetParser().ParseInstruction(IDVal, Inst))
601 if (Lexer.isNot(AsmToken::EndOfStatement))
602 return TokError("unexpected token in argument list");
604 // Eat the end of statement marker.
607 // Instruction is good, process it.
608 Out.EmitInstruction(Inst);
610 // Skip to end of line for now.
614 bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
615 // FIXME: Use better location, we should use proper tokens.
616 SMLoc EqualLoc = Lexer.getLoc();
619 if (ParseRelocatableExpression(Value))
622 if (Lexer.isNot(AsmToken::EndOfStatement))
623 return TokError("unexpected token in assignment");
625 // Eat the end of statement marker.
628 // Diagnose assignment to a label.
630 // FIXME: Diagnostics. Note the location of the definition as a label.
631 // FIXME: This doesn't diagnose assignment to a symbol which has been
632 // implicitly marked as external.
633 // FIXME: Handle '.'.
634 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
635 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
636 if (Sym->getSection())
637 return Error(EqualLoc, "invalid assignment to symbol emitted as a label");
638 if (Sym->isExternal())
639 return Error(EqualLoc, "invalid assignment to external symbol");
641 // Do the assignment.
642 Out.EmitAssignment(Sym, Value, IsDotSet);
650 bool AsmParser::ParseIdentifier(StringRef &Res) {
651 if (Lexer.isNot(AsmToken::Identifier) &&
652 Lexer.isNot(AsmToken::String))
655 Res = Lexer.getTok().getIdentifier();
657 Lexer.Lex(); // Consume the identifier token.
662 /// ParseDirectiveSet:
663 /// ::= .set identifier ',' expression
664 bool AsmParser::ParseDirectiveSet() {
667 if (ParseIdentifier(Name))
668 return TokError("expected identifier after '.set' directive");
670 if (Lexer.isNot(AsmToken::Comma))
671 return TokError("unexpected token in '.set'");
674 return ParseAssignment(Name, true);
677 /// ParseDirectiveSection:
678 /// ::= .section identifier (',' identifier)*
679 /// FIXME: This should actually parse out the segment, section, attributes and
680 /// sizeof_stub fields.
681 bool AsmParser::ParseDirectiveDarwinSection() {
682 StringRef SectionName;
684 if (ParseIdentifier(SectionName))
685 return TokError("expected identifier after '.section' directive");
687 std::string Section = SectionName;
689 // FIXME: This doesn't work, we lose quoting on things
691 // Accept a comma separated list of modifiers.
692 while (Lexer.is(AsmToken::Comma)) {
693 Lexer.Lex(); // Consume the comma.
695 StringRef ModifierName;
696 if (ParseIdentifier(ModifierName))
697 return TokError("expected identifier in '.section' directive");
699 Section += ModifierName;
702 if (Lexer.isNot(AsmToken::EndOfStatement))
703 return TokError("unexpected token in '.section' directive");
706 // FIXME: Arch specific.
707 MCSection *S = Ctx.GetSection(Section);
709 S = MCSection::Create(Section, false, SectionKind(), Ctx);
711 Out.SwitchSection(S);
715 bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
716 const char *Directives) {
717 if (Lexer.isNot(AsmToken::EndOfStatement))
718 return TokError("unexpected token in section switching directive");
721 std::string SectionStr = Section;
722 if (Directives && Directives[0]) {
724 SectionStr += Directives;
727 // FIXME: Arch specific.
728 MCSection *S = Ctx.GetSection(Section);
730 S = MCSection::Create(Section, false, SectionKind(), Ctx);
732 Out.SwitchSection(S);
736 /// ParseDirectiveAscii:
737 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
738 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
739 if (Lexer.isNot(AsmToken::EndOfStatement)) {
741 if (Lexer.isNot(AsmToken::String))
742 return TokError("expected string in '.ascii' or '.asciz' directive");
744 // FIXME: This shouldn't use a const char* + strlen, the string could have
746 // FIXME: Should have accessor for getting string contents.
747 StringRef Str = Lexer.getTok().getString();
748 Out.EmitBytes(Str.substr(1, Str.size() - 2));
750 Out.EmitBytes(StringRef("\0", 1));
754 if (Lexer.is(AsmToken::EndOfStatement))
757 if (Lexer.isNot(AsmToken::Comma))
758 return TokError("unexpected token in '.ascii' or '.asciz' directive");
767 /// ParseDirectiveValue
768 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
769 bool AsmParser::ParseDirectiveValue(unsigned Size) {
770 if (Lexer.isNot(AsmToken::EndOfStatement)) {
773 if (ParseRelocatableExpression(Expr))
776 Out.EmitValue(Expr, Size);
778 if (Lexer.is(AsmToken::EndOfStatement))
781 // FIXME: Improve diagnostic.
782 if (Lexer.isNot(AsmToken::Comma))
783 return TokError("unexpected token in directive");
792 /// ParseDirectiveSpace
793 /// ::= .space expression [ , expression ]
794 bool AsmParser::ParseDirectiveSpace() {
796 if (ParseAbsoluteExpression(NumBytes))
799 int64_t FillExpr = 0;
800 bool HasFillExpr = false;
801 if (Lexer.isNot(AsmToken::EndOfStatement)) {
802 if (Lexer.isNot(AsmToken::Comma))
803 return TokError("unexpected token in '.space' directive");
806 if (ParseAbsoluteExpression(FillExpr))
811 if (Lexer.isNot(AsmToken::EndOfStatement))
812 return TokError("unexpected token in '.space' directive");
818 return TokError("invalid number of bytes in '.space' directive");
820 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
821 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
822 Out.EmitValue(MCValue::get(FillExpr), 1);
827 /// ParseDirectiveFill
828 /// ::= .fill expression , expression , expression
829 bool AsmParser::ParseDirectiveFill() {
831 if (ParseAbsoluteExpression(NumValues))
834 if (Lexer.isNot(AsmToken::Comma))
835 return TokError("unexpected token in '.fill' directive");
839 if (ParseAbsoluteExpression(FillSize))
842 if (Lexer.isNot(AsmToken::Comma))
843 return TokError("unexpected token in '.fill' directive");
847 if (ParseAbsoluteExpression(FillExpr))
850 if (Lexer.isNot(AsmToken::EndOfStatement))
851 return TokError("unexpected token in '.fill' directive");
855 if (FillSize != 1 && FillSize != 2 && FillSize != 4)
856 return TokError("invalid '.fill' size, expected 1, 2, or 4");
858 for (uint64_t i = 0, e = NumValues; i != e; ++i)
859 Out.EmitValue(MCValue::get(FillExpr), FillSize);
864 /// ParseDirectiveOrg
865 /// ::= .org expression [ , expression ]
866 bool AsmParser::ParseDirectiveOrg() {
868 if (ParseRelocatableExpression(Offset))
871 // Parse optional fill expression.
872 int64_t FillExpr = 0;
873 if (Lexer.isNot(AsmToken::EndOfStatement)) {
874 if (Lexer.isNot(AsmToken::Comma))
875 return TokError("unexpected token in '.org' directive");
878 if (ParseAbsoluteExpression(FillExpr))
881 if (Lexer.isNot(AsmToken::EndOfStatement))
882 return TokError("unexpected token in '.org' directive");
887 // FIXME: Only limited forms of relocatable expressions are accepted here, it
888 // has to be relative to the current section.
889 Out.EmitValueToOffset(Offset, FillExpr);
894 /// ParseDirectiveAlign
895 /// ::= {.align, ...} expression [ , expression [ , expression ]]
896 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
898 if (ParseAbsoluteExpression(Alignment))
902 bool HasFillExpr = false;
903 int64_t FillExpr = 0;
904 int64_t MaxBytesToFill = 0;
905 if (Lexer.isNot(AsmToken::EndOfStatement)) {
906 if (Lexer.isNot(AsmToken::Comma))
907 return TokError("unexpected token in directive");
910 // The fill expression can be omitted while specifying a maximum number of
911 // alignment bytes, e.g:
913 if (Lexer.isNot(AsmToken::Comma)) {
915 if (ParseAbsoluteExpression(FillExpr))
919 if (Lexer.isNot(AsmToken::EndOfStatement)) {
920 if (Lexer.isNot(AsmToken::Comma))
921 return TokError("unexpected token in directive");
924 MaxBytesLoc = Lexer.getLoc();
925 if (ParseAbsoluteExpression(MaxBytesToFill))
928 if (Lexer.isNot(AsmToken::EndOfStatement))
929 return TokError("unexpected token in directive");
936 // FIXME: Sometimes fill with nop.
940 // Compute alignment in bytes.
942 // FIXME: Diagnose overflow.
943 Alignment = 1LL << Alignment;
946 // Diagnose non-sensical max bytes to fill.
947 if (MaxBytesLoc.isValid()) {
948 if (MaxBytesToFill < 1) {
949 Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
950 "many bytes, ignoring");
954 if (MaxBytesToFill >= Alignment) {
955 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
961 // FIXME: Target specific behavior about how the "extra" bytes are filled.
962 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
967 /// ParseDirectiveSymbolAttribute
968 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
969 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
970 if (Lexer.isNot(AsmToken::EndOfStatement)) {
974 if (ParseIdentifier(Name))
975 return TokError("expected identifier in directive");
977 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
979 // If this is use of an undefined symbol then mark it external.
980 if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
981 Sym->setExternal(true);
983 Out.EmitSymbolAttribute(Sym, Attr);
985 if (Lexer.is(AsmToken::EndOfStatement))
988 if (Lexer.isNot(AsmToken::Comma))
989 return TokError("unexpected token in directive");
998 /// ParseDirectiveDarwinSymbolDesc
999 /// ::= .desc identifier , expression
1000 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1002 if (ParseIdentifier(Name))
1003 return TokError("expected identifier in directive");
1005 // Handle the identifier as the key symbol.
1006 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1008 if (Lexer.isNot(AsmToken::Comma))
1009 return TokError("unexpected token in '.desc' directive");
1012 SMLoc DescLoc = Lexer.getLoc();
1014 if (ParseAbsoluteExpression(DescValue))
1017 if (Lexer.isNot(AsmToken::EndOfStatement))
1018 return TokError("unexpected token in '.desc' directive");
1022 // Set the n_desc field of this Symbol to this DescValue
1023 Out.EmitSymbolDesc(Sym, DescValue);
1028 /// ParseDirectiveComm
1029 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1030 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1031 SMLoc IDLoc = Lexer.getLoc();
1033 if (ParseIdentifier(Name))
1034 return TokError("expected identifier in directive");
1036 // Handle the identifier as the key symbol.
1037 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1039 if (Lexer.isNot(AsmToken::Comma))
1040 return TokError("unexpected token in directive");
1044 SMLoc SizeLoc = Lexer.getLoc();
1045 if (ParseAbsoluteExpression(Size))
1048 int64_t Pow2Alignment = 0;
1049 SMLoc Pow2AlignmentLoc;
1050 if (Lexer.is(AsmToken::Comma)) {
1052 Pow2AlignmentLoc = Lexer.getLoc();
1053 if (ParseAbsoluteExpression(Pow2Alignment))
1057 if (Lexer.isNot(AsmToken::EndOfStatement))
1058 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1062 // NOTE: a size of zero for a .comm should create a undefined symbol
1063 // but a size of .lcomm creates a bss symbol of size zero.
1065 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1066 "be less than zero");
1068 // NOTE: The alignment in the directive is a power of 2 value, the assember
1069 // may internally end up wanting an alignment in bytes.
1070 // FIXME: Diagnose overflow.
1071 if (Pow2Alignment < 0)
1072 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1073 "alignment, can't be less than zero");
1075 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1076 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1077 return Error(IDLoc, "invalid symbol redefinition");
1079 // Create the Symbol as a common or local common with Size and Pow2Alignment
1080 Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
1085 /// ParseDirectiveDarwinZerofill
1086 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1087 /// , align_expression ]]
1088 bool AsmParser::ParseDirectiveDarwinZerofill() {
1089 // FIXME: Handle quoted names here.
1091 if (Lexer.isNot(AsmToken::Identifier))
1092 return TokError("expected segment name after '.zerofill' directive");
1093 std::string Section = Lexer.getTok().getString();
1096 if (Lexer.isNot(AsmToken::Comma))
1097 return TokError("unexpected token in directive");
1101 if (Lexer.isNot(AsmToken::Identifier))
1102 return TokError("expected section name after comma in '.zerofill' "
1104 Section += Lexer.getTok().getString().str();
1107 // FIXME: we will need to tell GetSection() that this is to be created with or
1108 // must have the Mach-O section type of S_ZEROFILL. Something like the code
1109 // below could be done but for now it is not as EmitZerofill() does not know
1110 // how to deal with a section type in the section name like
1111 // ParseDirectiveDarwinSection() allows.
1113 // Section += "zerofill";
1115 // If this is the end of the line all that was wanted was to create the
1116 // the section but with no symbol.
1117 if (Lexer.is(AsmToken::EndOfStatement)) {
1118 // FIXME: Arch specific.
1119 MCSection *S = Ctx.GetSection(Section);
1121 S = MCSection::Create(Section, false, SectionKind(), Ctx);
1123 // Create the zerofill section but no symbol
1124 Out.EmitZerofill(S);
1128 if (Lexer.isNot(AsmToken::Comma))
1129 return TokError("unexpected token in directive");
1132 if (Lexer.isNot(AsmToken::Identifier))
1133 return TokError("expected identifier in directive");
1135 // handle the identifier as the key symbol.
1136 SMLoc IDLoc = Lexer.getLoc();
1137 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
1140 if (Lexer.isNot(AsmToken::Comma))
1141 return TokError("unexpected token in directive");
1145 SMLoc SizeLoc = Lexer.getLoc();
1146 if (ParseAbsoluteExpression(Size))
1149 int64_t Pow2Alignment = 0;
1150 SMLoc Pow2AlignmentLoc;
1151 if (Lexer.is(AsmToken::Comma)) {
1153 Pow2AlignmentLoc = Lexer.getLoc();
1154 if (ParseAbsoluteExpression(Pow2Alignment))
1158 if (Lexer.isNot(AsmToken::EndOfStatement))
1159 return TokError("unexpected token in '.zerofill' directive");
1164 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1167 // NOTE: The alignment in the directive is a power of 2 value, the assember
1168 // may internally end up wanting an alignment in bytes.
1169 // FIXME: Diagnose overflow.
1170 if (Pow2Alignment < 0)
1171 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1172 "can't be less than zero");
1174 // TODO: Symbol must be undefined or it is a error to re-defined the symbol
1175 if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
1176 return Error(IDLoc, "invalid symbol redefinition");
1178 // FIXME: Arch specific.
1179 MCSection *S = Ctx.GetSection(Section);
1181 S = MCSection::Create(Section, false, SectionKind(), Ctx);
1183 // Create the zerofill Symbol with Size and Pow2Alignment
1184 Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
1189 /// ParseDirectiveDarwinSubsectionsViaSymbols
1190 /// ::= .subsections_via_symbols
1191 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1192 if (Lexer.isNot(AsmToken::EndOfStatement))
1193 return TokError("unexpected token in '.subsections_via_symbols' directive");
1197 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1202 /// ParseDirectiveAbort
1203 /// ::= .abort [ "abort_string" ]
1204 bool AsmParser::ParseDirectiveAbort() {
1205 // FIXME: Use loc from directive.
1206 SMLoc Loc = Lexer.getLoc();
1209 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1210 if (Lexer.isNot(AsmToken::String))
1211 return TokError("expected string in '.abort' directive");
1213 Str = Lexer.getTok().getString();
1218 if (Lexer.isNot(AsmToken::EndOfStatement))
1219 return TokError("unexpected token in '.abort' directive");
1223 // FIXME: Handle here.
1225 Error(Loc, ".abort detected. Assembly stopping.");
1227 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1232 /// ParseDirectiveLsym
1233 /// ::= .lsym identifier , expression
1234 bool AsmParser::ParseDirectiveDarwinLsym() {
1236 if (ParseIdentifier(Name))
1237 return TokError("expected identifier in directive");
1239 // Handle the identifier as the key symbol.
1240 MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
1242 if (Lexer.isNot(AsmToken::Comma))
1243 return TokError("unexpected token in '.lsym' directive");
1247 if (ParseRelocatableExpression(Expr))
1250 if (Lexer.isNot(AsmToken::EndOfStatement))
1251 return TokError("unexpected token in '.lsym' directive");
1255 // Create the Sym with the value of the Expr
1256 Out.EmitLocalSymbol(Sym, Expr);
1261 /// ParseDirectiveInclude
1262 /// ::= .include "filename"
1263 bool AsmParser::ParseDirectiveInclude() {
1264 if (Lexer.isNot(AsmToken::String))
1265 return TokError("expected string in '.include' directive");
1267 std::string Filename = Lexer.getTok().getString();
1268 SMLoc IncludeLoc = Lexer.getLoc();
1271 if (Lexer.isNot(AsmToken::EndOfStatement))
1272 return TokError("unexpected token in '.include' directive");
1274 // Strip the quotes.
1275 Filename = Filename.substr(1, Filename.size()-2);
1277 // Attempt to switch the lexer to the included file before consuming the end
1278 // of statement to avoid losing it when we switch.
1279 if (Lexer.EnterIncludeFile(Filename)) {
1280 Lexer.PrintMessage(IncludeLoc,
1281 "Could not find include file '" + Filename + "'",
1289 /// ParseDirectiveDarwinDumpOrLoad
1290 /// ::= ( .dump | .load ) "filename"
1291 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1292 if (Lexer.isNot(AsmToken::String))
1293 return TokError("expected string in '.dump' or '.load' directive");
1297 if (Lexer.isNot(AsmToken::EndOfStatement))
1298 return TokError("unexpected token in '.dump' or '.load' directive");
1302 // FIXME: If/when .dump and .load are implemented they will be done in the
1303 // the assembly parser and not have any need for an MCStreamer API.
1305 Warning(IDLoc, "ignoring directive .dump for now");
1307 Warning(IDLoc, "ignoring directive .load for now");
1312 /// ParseDirectiveIf
1313 /// ::= .if expression
1314 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1315 // Consume the identifier that was the .if directive
1318 TheCondStack.push_back(TheCondState);
1319 TheCondState.TheCond = AsmCond::IfCond;
1320 if(TheCondState.Ignore) {
1321 EatToEndOfStatement();
1325 if (ParseAbsoluteExpression(ExprValue))
1328 if (Lexer.isNot(AsmToken::EndOfStatement))
1329 return TokError("unexpected token in '.if' directive");
1333 TheCondState.CondMet = ExprValue;
1334 TheCondState.Ignore = !TheCondState.CondMet;
1340 /// ParseDirectiveElseIf
1341 /// ::= .elseif expression
1342 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1343 if (TheCondState.TheCond != AsmCond::IfCond &&
1344 TheCondState.TheCond != AsmCond::ElseIfCond)
1345 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1347 TheCondState.TheCond = AsmCond::ElseIfCond;
1349 // Consume the identifier that was the .elseif directive
1352 bool LastIgnoreState = false;
1353 if (!TheCondStack.empty())
1354 LastIgnoreState = TheCondStack.back().Ignore;
1355 if (LastIgnoreState || TheCondState.CondMet) {
1356 TheCondState.Ignore = true;
1357 EatToEndOfStatement();
1361 if (ParseAbsoluteExpression(ExprValue))
1364 if (Lexer.isNot(AsmToken::EndOfStatement))
1365 return TokError("unexpected token in '.elseif' directive");
1368 TheCondState.CondMet = ExprValue;
1369 TheCondState.Ignore = !TheCondState.CondMet;
1375 /// ParseDirectiveElse
1377 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1378 // Consume the identifier that was the .else directive
1381 if (Lexer.isNot(AsmToken::EndOfStatement))
1382 return TokError("unexpected token in '.else' directive");
1386 if (TheCondState.TheCond != AsmCond::IfCond &&
1387 TheCondState.TheCond != AsmCond::ElseIfCond)
1388 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1390 TheCondState.TheCond = AsmCond::ElseCond;
1391 bool LastIgnoreState = false;
1392 if (!TheCondStack.empty())
1393 LastIgnoreState = TheCondStack.back().Ignore;
1394 if (LastIgnoreState || TheCondState.CondMet)
1395 TheCondState.Ignore = true;
1397 TheCondState.Ignore = false;
1402 /// ParseDirectiveEndIf
1404 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1405 // Consume the identifier that was the .endif directive
1408 if (Lexer.isNot(AsmToken::EndOfStatement))
1409 return TokError("unexpected token in '.endif' directive");
1413 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1414 TheCondStack.empty())
1415 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1417 if (!TheCondStack.empty()) {
1418 TheCondState = TheCondStack.back();
1419 TheCondStack.pop_back();