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"
16 #include "llvm/ADT/SmallString.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/MCValue.h"
25 #include "llvm/Support/SourceMgr.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Target/TargetAsmParser.h"
30 // Mach-O section uniquing.
32 // FIXME: Figure out where this should live, it should be shared by
33 // TargetLoweringObjectFile.
34 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
36 AsmParser::AsmParser(SourceMgr &_SM, MCContext &_Ctx, MCStreamer &_Out,
37 const MCAsmInfo &_MAI)
38 : Lexer(_SM, _MAI), Ctx(_Ctx), Out(_Out), TargetParser(0),
39 SectionUniquingMap(0) {
40 // Debugging directives.
41 AddDirectiveHandler(".file", &AsmParser::ParseDirectiveFile);
42 AddDirectiveHandler(".line", &AsmParser::ParseDirectiveLine);
43 AddDirectiveHandler(".loc", &AsmParser::ParseDirectiveLoc);
48 AsmParser::~AsmParser() {
49 // If we have the MachO uniquing map, free it.
50 delete (MachOUniqueMapTy*)SectionUniquingMap;
53 const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
54 const StringRef &Section,
55 unsigned TypeAndAttributes,
57 SectionKind Kind) const {
58 // We unique sections by their segment/section pair. The returned section
59 // may not have the same flags as the requested section, if so this should be
60 // diagnosed by the client as an error.
62 // Create the map if it doesn't already exist.
63 if (SectionUniquingMap == 0)
64 SectionUniquingMap = new MachOUniqueMapTy();
65 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
67 // Form the name to look up.
73 // Do the lookup, if we have a hit, return it.
74 const MCSectionMachO *&Entry = Map[Name.str()];
76 // FIXME: This should validate the type and attributes.
77 if (Entry) return Entry;
79 // Otherwise, return a new section.
80 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
81 Reserved2, Kind, Ctx);
84 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
85 Lexer.PrintMessage(L, Msg.str(), "warning");
88 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
89 Lexer.PrintMessage(L, Msg.str(), "error");
93 bool AsmParser::TokError(const char *Msg) {
94 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
98 bool AsmParser::Run() {
99 // Create the initial section.
101 // FIXME: Support -n.
102 // FIXME: Target hook & command line option for initial section.
103 Out.SwitchSection(getMachOSection("__TEXT", "__text",
104 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
111 bool HadError = false;
113 AsmCond StartingCondState = TheCondState;
115 // While we have input, parse each statement.
116 while (Lexer.isNot(AsmToken::Eof)) {
117 // Handle conditional assembly here before calling ParseStatement()
118 if (Lexer.getKind() == AsmToken::Identifier) {
119 // If we have an identifier, handle it as the key symbol.
120 AsmToken ID = Lexer.getTok();
121 SMLoc IDLoc = ID.getLoc();
122 StringRef IDVal = ID.getString();
124 if (IDVal == ".if" ||
125 IDVal == ".elseif" ||
128 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
131 EatToEndOfStatement();
135 if (TheCondState.Ignore) {
136 EatToEndOfStatement();
140 if (!ParseStatement()) continue;
142 // We had an error, remember it and recover by skipping to the next line.
144 EatToEndOfStatement();
147 if (TheCondState.TheCond != StartingCondState.TheCond ||
148 TheCondState.Ignore != StartingCondState.Ignore)
149 return TokError("unmatched .ifs or .elses");
157 /// ParseConditionalAssemblyDirectives - parse the conditional assembly
159 bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
160 SMLoc DirectiveLoc) {
161 if (Directive == ".if")
162 return ParseDirectiveIf(DirectiveLoc);
163 if (Directive == ".elseif")
164 return ParseDirectiveElseIf(DirectiveLoc);
165 if (Directive == ".else")
166 return ParseDirectiveElse(DirectiveLoc);
167 if (Directive == ".endif")
168 return ParseDirectiveEndIf(DirectiveLoc);
172 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
173 void AsmParser::EatToEndOfStatement() {
174 while (Lexer.isNot(AsmToken::EndOfStatement) &&
175 Lexer.isNot(AsmToken::Eof))
179 if (Lexer.is(AsmToken::EndOfStatement))
184 /// ParseParenExpr - Parse a paren expression and return it.
185 /// NOTE: This assumes the leading '(' has already been consumed.
187 /// parenexpr ::= expr)
189 bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
190 if (ParseExpression(Res)) return true;
191 if (Lexer.isNot(AsmToken::RParen))
192 return TokError("expected ')' in parentheses expression");
197 MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
198 if (MCSymbol *S = Ctx.LookupSymbol(Name))
201 // If the label starts with L it is an assembler temporary label.
202 if (Name.startswith("L"))
203 return Ctx.CreateTemporarySymbol(Name);
205 return Ctx.CreateSymbol(Name);
208 /// ParsePrimaryExpr - Parse a primary expression and return it.
209 /// primaryexpr ::= (parenexpr
210 /// primaryexpr ::= symbol
211 /// primaryexpr ::= number
212 /// primaryexpr ::= ~,+,- primaryexpr
213 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
214 switch (Lexer.getKind()) {
216 return TokError("unknown token in expression");
217 case AsmToken::Exclaim:
218 Lexer.Lex(); // Eat the operator.
219 if (ParsePrimaryExpr(Res))
221 Res = MCUnaryExpr::CreateLNot(Res, getContext());
223 case AsmToken::String:
224 case AsmToken::Identifier: {
225 // This is a symbol reference.
226 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
227 Lexer.Lex(); // Eat identifier.
229 // If this is an absolute variable reference, substitute it now to preserve
230 // semantics in the face of reassignment.
231 if (Sym->getValue() && isa<MCConstantExpr>(Sym->getValue())) {
232 Res = Sym->getValue();
236 // Otherwise create a symbol ref.
237 Res = MCSymbolRefExpr::Create(Sym, getContext());
240 case AsmToken::Integer:
241 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
242 Lexer.Lex(); // Eat token.
244 case AsmToken::LParen:
245 Lexer.Lex(); // Eat the '('.
246 return ParseParenExpr(Res);
247 case AsmToken::Minus:
248 Lexer.Lex(); // Eat the operator.
249 if (ParsePrimaryExpr(Res))
251 Res = MCUnaryExpr::CreateMinus(Res, getContext());
254 Lexer.Lex(); // Eat the operator.
255 if (ParsePrimaryExpr(Res))
257 Res = MCUnaryExpr::CreatePlus(Res, getContext());
259 case AsmToken::Tilde:
260 Lexer.Lex(); // Eat the operator.
261 if (ParsePrimaryExpr(Res))
263 Res = MCUnaryExpr::CreateNot(Res, getContext());
268 /// ParseExpression - Parse an expression and return it.
270 /// expr ::= expr +,- expr -> lowest.
271 /// expr ::= expr |,^,&,! expr -> middle.
272 /// expr ::= expr *,/,%,<<,>> expr -> highest.
273 /// expr ::= primaryexpr
275 bool AsmParser::ParseExpression(const MCExpr *&Res) {
277 return ParsePrimaryExpr(Res) ||
278 ParseBinOpRHS(1, Res);
281 bool AsmParser::ParseParenExpression(const MCExpr *&Res) {
282 if (ParseParenExpr(Res))
288 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
291 SMLoc StartLoc = Lexer.getLoc();
292 if (ParseExpression(Expr))
295 if (!Expr->EvaluateAsAbsolute(Res))
296 return Error(StartLoc, "expected absolute expression");
301 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
302 MCBinaryExpr::Opcode &Kind) {
305 return 0; // not a binop.
307 // Lowest Precedence: &&, ||
308 case AsmToken::AmpAmp:
309 Kind = MCBinaryExpr::LAnd;
311 case AsmToken::PipePipe:
312 Kind = MCBinaryExpr::LOr;
315 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
317 Kind = MCBinaryExpr::Add;
319 case AsmToken::Minus:
320 Kind = MCBinaryExpr::Sub;
322 case AsmToken::EqualEqual:
323 Kind = MCBinaryExpr::EQ;
325 case AsmToken::ExclaimEqual:
326 case AsmToken::LessGreater:
327 Kind = MCBinaryExpr::NE;
330 Kind = MCBinaryExpr::LT;
332 case AsmToken::LessEqual:
333 Kind = MCBinaryExpr::LTE;
335 case AsmToken::Greater:
336 Kind = MCBinaryExpr::GT;
338 case AsmToken::GreaterEqual:
339 Kind = MCBinaryExpr::GTE;
342 // Intermediate Precedence: |, &, ^
344 // FIXME: gas seems to support '!' as an infix operator?
346 Kind = MCBinaryExpr::Or;
348 case AsmToken::Caret:
349 Kind = MCBinaryExpr::Xor;
352 Kind = MCBinaryExpr::And;
355 // Highest Precedence: *, /, %, <<, >>
357 Kind = MCBinaryExpr::Mul;
359 case AsmToken::Slash:
360 Kind = MCBinaryExpr::Div;
362 case AsmToken::Percent:
363 Kind = MCBinaryExpr::Mod;
365 case AsmToken::LessLess:
366 Kind = MCBinaryExpr::Shl;
368 case AsmToken::GreaterGreater:
369 Kind = MCBinaryExpr::Shr;
375 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
376 /// Res contains the LHS of the expression on input.
377 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
379 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
380 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
382 // If the next token is lower precedence than we are allowed to eat, return
383 // successfully with what we ate already.
384 if (TokPrec < Precedence)
389 // Eat the next primary expression.
391 if (ParsePrimaryExpr(RHS)) return true;
393 // If BinOp binds less tightly with RHS than the operator after RHS, let
394 // the pending operator take RHS as its LHS.
395 MCBinaryExpr::Opcode Dummy;
396 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
397 if (TokPrec < NextTokPrec) {
398 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
401 // Merge LHS and RHS according to operator.
402 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
410 /// ::= EndOfStatement
411 /// ::= Label* Directive ...Operands... EndOfStatement
412 /// ::= Label* Identifier OperandList* EndOfStatement
413 bool AsmParser::ParseStatement() {
414 if (Lexer.is(AsmToken::EndOfStatement)) {
419 // Statements always start with an identifier.
420 AsmToken ID = Lexer.getTok();
421 SMLoc IDLoc = ID.getLoc();
423 if (ParseIdentifier(IDVal))
424 return TokError("unexpected token at start of statement");
426 // FIXME: Recurse on local labels?
428 // See what kind of statement we have.
429 switch (Lexer.getKind()) {
430 case AsmToken::Colon: {
431 // identifier ':' -> Label.
434 // Diagnose attempt to use a variable as a label.
436 // FIXME: Diagnostics. Note the location of the definition as a label.
437 // FIXME: This doesn't diagnose assignment to a symbol which has been
438 // implicitly marked as external.
439 MCSymbol *Sym = CreateSymbol(IDVal);
440 if (!Sym->isUndefined())
441 return Error(IDLoc, "invalid symbol redefinition");
446 return ParseStatement();
449 case AsmToken::Equal:
450 // identifier '=' ... -> assignment statement
453 return ParseAssignment(IDVal);
455 default: // Normal instruction or directive.
459 // Otherwise, we have a normal instruction or directive.
460 if (IDVal[0] == '.') {
461 // FIXME: This should be driven based on a hash lookup and callback.
462 if (IDVal == ".section")
463 return ParseDirectiveDarwinSection();
464 if (IDVal == ".text")
465 // FIXME: This changes behavior based on the -static flag to the
467 return ParseDirectiveSectionSwitch("__TEXT", "__text",
468 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
469 if (IDVal == ".const")
470 return ParseDirectiveSectionSwitch("__TEXT", "__const");
471 if (IDVal == ".static_const")
472 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
473 if (IDVal == ".cstring")
474 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
475 MCSectionMachO::S_CSTRING_LITERALS);
476 if (IDVal == ".literal4")
477 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
478 MCSectionMachO::S_4BYTE_LITERALS,
480 if (IDVal == ".literal8")
481 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
482 MCSectionMachO::S_8BYTE_LITERALS,
484 if (IDVal == ".literal16")
485 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
486 MCSectionMachO::S_16BYTE_LITERALS,
488 if (IDVal == ".constructor")
489 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
490 if (IDVal == ".destructor")
491 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
492 if (IDVal == ".fvmlib_init0")
493 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
494 if (IDVal == ".fvmlib_init1")
495 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
497 // FIXME: The assembler manual claims that this has the self modify code
498 // flag, at least on x86-32, but that does not appear to be correct.
499 if (IDVal == ".symbol_stub")
500 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
501 MCSectionMachO::S_SYMBOL_STUBS |
502 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
503 // FIXME: Different on PPC and ARM.
505 // FIXME: PowerPC only?
506 if (IDVal == ".picsymbol_stub")
507 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
508 MCSectionMachO::S_SYMBOL_STUBS |
509 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
511 if (IDVal == ".data")
512 return ParseDirectiveSectionSwitch("__DATA", "__data");
513 if (IDVal == ".static_data")
514 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
516 // FIXME: The section names of these two are misspelled in the assembler
518 if (IDVal == ".non_lazy_symbol_pointer")
519 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
520 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
522 if (IDVal == ".lazy_symbol_pointer")
523 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
524 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
527 if (IDVal == ".dyld")
528 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
529 if (IDVal == ".mod_init_func")
530 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
531 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
533 if (IDVal == ".mod_term_func")
534 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
535 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
537 if (IDVal == ".const_data")
538 return ParseDirectiveSectionSwitch("__DATA", "__const");
541 if (IDVal == ".objc_class")
542 return ParseDirectiveSectionSwitch("__OBJC", "__class",
543 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
544 if (IDVal == ".objc_meta_class")
545 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
546 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
547 if (IDVal == ".objc_cat_cls_meth")
548 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
549 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
550 if (IDVal == ".objc_cat_inst_meth")
551 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
552 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
553 if (IDVal == ".objc_protocol")
554 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
555 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
556 if (IDVal == ".objc_string_object")
557 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
558 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
559 if (IDVal == ".objc_cls_meth")
560 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
561 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
562 if (IDVal == ".objc_inst_meth")
563 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
564 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
565 if (IDVal == ".objc_cls_refs")
566 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
567 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
568 MCSectionMachO::S_LITERAL_POINTERS,
570 if (IDVal == ".objc_message_refs")
571 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
572 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
573 MCSectionMachO::S_LITERAL_POINTERS,
575 if (IDVal == ".objc_symbols")
576 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
577 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
578 if (IDVal == ".objc_category")
579 return ParseDirectiveSectionSwitch("__OBJC", "__category",
580 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
581 if (IDVal == ".objc_class_vars")
582 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
583 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
584 if (IDVal == ".objc_instance_vars")
585 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
586 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
587 if (IDVal == ".objc_module_info")
588 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
589 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
590 if (IDVal == ".objc_class_names")
591 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
592 MCSectionMachO::S_CSTRING_LITERALS);
593 if (IDVal == ".objc_meth_var_types")
594 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
595 MCSectionMachO::S_CSTRING_LITERALS);
596 if (IDVal == ".objc_meth_var_names")
597 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
598 MCSectionMachO::S_CSTRING_LITERALS);
599 if (IDVal == ".objc_selector_strs")
600 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
601 MCSectionMachO::S_CSTRING_LITERALS);
603 // Assembler features
605 return ParseDirectiveSet();
609 if (IDVal == ".ascii")
610 return ParseDirectiveAscii(false);
611 if (IDVal == ".asciz")
612 return ParseDirectiveAscii(true);
614 if (IDVal == ".byte")
615 return ParseDirectiveValue(1);
616 if (IDVal == ".short")
617 return ParseDirectiveValue(2);
618 if (IDVal == ".long")
619 return ParseDirectiveValue(4);
620 if (IDVal == ".quad")
621 return ParseDirectiveValue(8);
623 // FIXME: Target hooks for IsPow2.
624 if (IDVal == ".align")
625 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
626 if (IDVal == ".align32")
627 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
628 if (IDVal == ".balign")
629 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
630 if (IDVal == ".balignw")
631 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
632 if (IDVal == ".balignl")
633 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
634 if (IDVal == ".p2align")
635 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
636 if (IDVal == ".p2alignw")
637 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
638 if (IDVal == ".p2alignl")
639 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
642 return ParseDirectiveOrg();
644 if (IDVal == ".fill")
645 return ParseDirectiveFill();
646 if (IDVal == ".space")
647 return ParseDirectiveSpace();
649 // Symbol attribute directives
651 if (IDVal == ".globl" || IDVal == ".global")
652 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
653 if (IDVal == ".hidden")
654 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
655 if (IDVal == ".indirect_symbol")
656 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
657 if (IDVal == ".internal")
658 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
659 if (IDVal == ".lazy_reference")
660 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
661 if (IDVal == ".no_dead_strip")
662 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
663 if (IDVal == ".private_extern")
664 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
665 if (IDVal == ".protected")
666 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
667 if (IDVal == ".reference")
668 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
669 if (IDVal == ".weak")
670 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
671 if (IDVal == ".weak_definition")
672 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
673 if (IDVal == ".weak_reference")
674 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
676 if (IDVal == ".comm")
677 return ParseDirectiveComm(/*IsLocal=*/false);
678 if (IDVal == ".lcomm")
679 return ParseDirectiveComm(/*IsLocal=*/true);
680 if (IDVal == ".zerofill")
681 return ParseDirectiveDarwinZerofill();
682 if (IDVal == ".desc")
683 return ParseDirectiveDarwinSymbolDesc();
684 if (IDVal == ".lsym")
685 return ParseDirectiveDarwinLsym();
687 if (IDVal == ".subsections_via_symbols")
688 return ParseDirectiveDarwinSubsectionsViaSymbols();
689 if (IDVal == ".abort")
690 return ParseDirectiveAbort();
691 if (IDVal == ".include")
692 return ParseDirectiveInclude();
693 if (IDVal == ".dump")
694 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
695 if (IDVal == ".load")
696 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
698 // Look up the handler in the handler table,
699 bool(AsmParser::*Handler)(StringRef, SMLoc) = DirectiveMap[IDVal];
701 return (this->*Handler)(IDVal, IDLoc);
703 // Target hook for parsing target specific directives.
704 if (!getTargetParser().ParseDirective(ID))
707 Warning(IDLoc, "ignoring directive for now");
708 EatToEndOfStatement();
713 if (getTargetParser().ParseInstruction(IDVal, Inst))
716 if (Lexer.isNot(AsmToken::EndOfStatement))
717 return TokError("unexpected token in argument list");
719 // Eat the end of statement marker.
722 // Instruction is good, process it.
723 Out.EmitInstruction(Inst);
725 // Skip to end of line for now.
729 bool AsmParser::ParseAssignment(const StringRef &Name) {
730 // FIXME: Use better location, we should use proper tokens.
731 SMLoc EqualLoc = Lexer.getLoc();
734 SMLoc StartLoc = Lexer.getLoc();
735 if (ParseExpression(Value))
738 if (Lexer.isNot(AsmToken::EndOfStatement))
739 return TokError("unexpected token in assignment");
741 // Eat the end of statement marker.
744 // Validate that the LHS is allowed to be a variable (either it has not been
745 // used as a symbol, or it is an absolute symbol).
746 MCSymbol *Sym = getContext().LookupSymbol(Name);
748 // Diagnose assignment to a label.
750 // FIXME: Diagnostics. Note the location of the definition as a label.
751 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
752 if (!Sym->isUndefined() && !Sym->isAbsolute())
753 return Error(EqualLoc, "redefinition of '" + Name + "'");
754 else if (!Sym->isVariable())
755 return Error(EqualLoc, "invalid assignment to '" + Name + "'");
756 else if (!isa<MCConstantExpr>(Sym->getValue()))
757 return Error(EqualLoc, "invalid reassignment of non-absolute variable '" +
760 Sym = CreateSymbol(Name);
762 // FIXME: Handle '.'.
764 // Do the assignment.
765 Out.EmitAssignment(Sym, Value);
773 bool AsmParser::ParseIdentifier(StringRef &Res) {
774 if (Lexer.isNot(AsmToken::Identifier) &&
775 Lexer.isNot(AsmToken::String))
778 Res = Lexer.getTok().getIdentifier();
780 Lexer.Lex(); // Consume the identifier token.
785 /// ParseDirectiveSet:
786 /// ::= .set identifier ',' expression
787 bool AsmParser::ParseDirectiveSet() {
790 if (ParseIdentifier(Name))
791 return TokError("expected identifier after '.set' directive");
793 if (Lexer.isNot(AsmToken::Comma))
794 return TokError("unexpected token in '.set'");
797 return ParseAssignment(Name);
800 /// ParseDirectiveSection:
801 /// ::= .section identifier (',' identifier)*
802 /// FIXME: This should actually parse out the segment, section, attributes and
803 /// sizeof_stub fields.
804 bool AsmParser::ParseDirectiveDarwinSection() {
805 SMLoc Loc = Lexer.getLoc();
807 StringRef SectionName;
808 if (ParseIdentifier(SectionName))
809 return Error(Loc, "expected identifier after '.section' directive");
811 // Verify there is a following comma.
812 if (!Lexer.is(AsmToken::Comma))
813 return TokError("unexpected token in '.section' directive");
815 std::string SectionSpec = SectionName;
818 // Add all the tokens until the end of the line, ParseSectionSpecifier will
820 StringRef EOL = Lexer.LexUntilEndOfStatement();
821 SectionSpec.append(EOL.begin(), EOL.end());
824 if (Lexer.isNot(AsmToken::EndOfStatement))
825 return TokError("unexpected token in '.section' directive");
829 StringRef Segment, Section;
830 unsigned TAA, StubSize;
831 std::string ErrorStr =
832 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
835 if (!ErrorStr.empty())
836 return Error(Loc, ErrorStr.c_str());
838 // FIXME: Arch specific.
839 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
844 /// ParseDirectiveSectionSwitch -
845 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
847 unsigned TAA, unsigned Align,
849 if (Lexer.isNot(AsmToken::EndOfStatement))
850 return TokError("unexpected token in section switching directive");
853 // FIXME: Arch specific.
854 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
857 // Set the implicit alignment, if any.
859 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
860 // alignment on the section (e.g., if one manually inserts bytes into the
861 // section, then just issueing the section switch directive will not realign
862 // the section. However, this is arguably more reasonable behavior, and there
863 // is no good reason for someone to intentionally emit incorrectly sized
864 // values into the implicitly aligned sections.
866 Out.EmitValueToAlignment(Align, 0, 1, 0);
871 bool AsmParser::ParseEscapedString(std::string &Data) {
872 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
875 StringRef Str = Lexer.getTok().getStringContents();
876 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
877 if (Str[i] != '\\') {
882 // Recognize escaped characters. Note that this escape semantics currently
883 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
886 return TokError("unexpected backslash at end of string");
888 // Recognize octal sequences.
889 if ((unsigned) (Str[i] - '0') <= 7) {
890 // Consume up to three octal characters.
891 unsigned Value = Str[i] - '0';
893 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
895 Value = Value * 8 + (Str[i] - '0');
897 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
899 Value = Value * 8 + (Str[i] - '0');
904 return TokError("invalid octal escape sequence (out of range)");
906 Data += (unsigned char) Value;
910 // Otherwise recognize individual escapes.
913 // Just reject invalid escape sequences for now.
914 return TokError("invalid escape sequence (unrecognized character)");
916 case 'b': Data += '\b'; break;
917 case 'f': Data += '\f'; break;
918 case 'n': Data += '\n'; break;
919 case 'r': Data += '\r'; break;
920 case 't': Data += '\t'; break;
921 case '"': Data += '"'; break;
922 case '\\': Data += '\\'; break;
929 /// ParseDirectiveAscii:
930 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
931 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
932 if (Lexer.isNot(AsmToken::EndOfStatement)) {
934 if (Lexer.isNot(AsmToken::String))
935 return TokError("expected string in '.ascii' or '.asciz' directive");
938 if (ParseEscapedString(Data))
943 Out.EmitBytes(StringRef("\0", 1));
947 if (Lexer.is(AsmToken::EndOfStatement))
950 if (Lexer.isNot(AsmToken::Comma))
951 return TokError("unexpected token in '.ascii' or '.asciz' directive");
960 /// ParseDirectiveValue
961 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
962 bool AsmParser::ParseDirectiveValue(unsigned Size) {
963 if (Lexer.isNot(AsmToken::EndOfStatement)) {
966 SMLoc StartLoc = Lexer.getLoc();
967 if (ParseExpression(Value))
970 Out.EmitValue(Value, Size);
972 if (Lexer.is(AsmToken::EndOfStatement))
975 // FIXME: Improve diagnostic.
976 if (Lexer.isNot(AsmToken::Comma))
977 return TokError("unexpected token in directive");
986 /// ParseDirectiveSpace
987 /// ::= .space expression [ , expression ]
988 bool AsmParser::ParseDirectiveSpace() {
990 if (ParseAbsoluteExpression(NumBytes))
993 int64_t FillExpr = 0;
994 bool HasFillExpr = false;
995 if (Lexer.isNot(AsmToken::EndOfStatement)) {
996 if (Lexer.isNot(AsmToken::Comma))
997 return TokError("unexpected token in '.space' directive");
1000 if (ParseAbsoluteExpression(FillExpr))
1005 if (Lexer.isNot(AsmToken::EndOfStatement))
1006 return TokError("unexpected token in '.space' directive");
1012 return TokError("invalid number of bytes in '.space' directive");
1014 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1015 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
1016 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
1021 /// ParseDirectiveFill
1022 /// ::= .fill expression , expression , expression
1023 bool AsmParser::ParseDirectiveFill() {
1025 if (ParseAbsoluteExpression(NumValues))
1028 if (Lexer.isNot(AsmToken::Comma))
1029 return TokError("unexpected token in '.fill' directive");
1033 if (ParseAbsoluteExpression(FillSize))
1036 if (Lexer.isNot(AsmToken::Comma))
1037 return TokError("unexpected token in '.fill' directive");
1041 if (ParseAbsoluteExpression(FillExpr))
1044 if (Lexer.isNot(AsmToken::EndOfStatement))
1045 return TokError("unexpected token in '.fill' directive");
1049 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1050 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1052 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1053 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
1058 /// ParseDirectiveOrg
1059 /// ::= .org expression [ , expression ]
1060 bool AsmParser::ParseDirectiveOrg() {
1061 const MCExpr *Offset;
1062 SMLoc StartLoc = Lexer.getLoc();
1063 if (ParseExpression(Offset))
1066 // Parse optional fill expression.
1067 int64_t FillExpr = 0;
1068 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1069 if (Lexer.isNot(AsmToken::Comma))
1070 return TokError("unexpected token in '.org' directive");
1073 if (ParseAbsoluteExpression(FillExpr))
1076 if (Lexer.isNot(AsmToken::EndOfStatement))
1077 return TokError("unexpected token in '.org' directive");
1082 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1083 // has to be relative to the current section.
1084 Out.EmitValueToOffset(Offset, FillExpr);
1089 /// ParseDirectiveAlign
1090 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1091 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1092 SMLoc AlignmentLoc = Lexer.getLoc();
1094 if (ParseAbsoluteExpression(Alignment))
1098 bool HasFillExpr = false;
1099 int64_t FillExpr = 0;
1100 int64_t MaxBytesToFill = 0;
1101 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1102 if (Lexer.isNot(AsmToken::Comma))
1103 return TokError("unexpected token in directive");
1106 // The fill expression can be omitted while specifying a maximum number of
1107 // alignment bytes, e.g:
1109 if (Lexer.isNot(AsmToken::Comma)) {
1111 if (ParseAbsoluteExpression(FillExpr))
1115 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1116 if (Lexer.isNot(AsmToken::Comma))
1117 return TokError("unexpected token in directive");
1120 MaxBytesLoc = Lexer.getLoc();
1121 if (ParseAbsoluteExpression(MaxBytesToFill))
1124 if (Lexer.isNot(AsmToken::EndOfStatement))
1125 return TokError("unexpected token in directive");
1132 // FIXME: Sometimes fill with nop.
1136 // Compute alignment in bytes.
1138 // FIXME: Diagnose overflow.
1139 if (Alignment >= 32) {
1140 Error(AlignmentLoc, "invalid alignment value");
1144 Alignment = 1ULL << Alignment;
1147 // Diagnose non-sensical max bytes to align.
1148 if (MaxBytesLoc.isValid()) {
1149 if (MaxBytesToFill < 1) {
1150 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1151 "many bytes, ignoring maximum bytes expression");
1155 if (MaxBytesToFill >= Alignment) {
1156 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1162 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1163 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1168 /// ParseDirectiveSymbolAttribute
1169 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1170 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
1171 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1175 if (ParseIdentifier(Name))
1176 return TokError("expected identifier in directive");
1178 MCSymbol *Sym = CreateSymbol(Name);
1180 Out.EmitSymbolAttribute(Sym, Attr);
1182 if (Lexer.is(AsmToken::EndOfStatement))
1185 if (Lexer.isNot(AsmToken::Comma))
1186 return TokError("unexpected token in directive");
1195 /// ParseDirectiveDarwinSymbolDesc
1196 /// ::= .desc identifier , expression
1197 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1199 if (ParseIdentifier(Name))
1200 return TokError("expected identifier in directive");
1202 // Handle the identifier as the key symbol.
1203 MCSymbol *Sym = CreateSymbol(Name);
1205 if (Lexer.isNot(AsmToken::Comma))
1206 return TokError("unexpected token in '.desc' directive");
1209 SMLoc DescLoc = Lexer.getLoc();
1211 if (ParseAbsoluteExpression(DescValue))
1214 if (Lexer.isNot(AsmToken::EndOfStatement))
1215 return TokError("unexpected token in '.desc' directive");
1219 // Set the n_desc field of this Symbol to this DescValue
1220 Out.EmitSymbolDesc(Sym, DescValue);
1225 /// ParseDirectiveComm
1226 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1227 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1228 SMLoc IDLoc = Lexer.getLoc();
1230 if (ParseIdentifier(Name))
1231 return TokError("expected identifier in directive");
1233 // Handle the identifier as the key symbol.
1234 MCSymbol *Sym = CreateSymbol(Name);
1236 if (Lexer.isNot(AsmToken::Comma))
1237 return TokError("unexpected token in directive");
1241 SMLoc SizeLoc = Lexer.getLoc();
1242 if (ParseAbsoluteExpression(Size))
1245 int64_t Pow2Alignment = 0;
1246 SMLoc Pow2AlignmentLoc;
1247 if (Lexer.is(AsmToken::Comma)) {
1249 Pow2AlignmentLoc = Lexer.getLoc();
1250 if (ParseAbsoluteExpression(Pow2Alignment))
1254 if (Lexer.isNot(AsmToken::EndOfStatement))
1255 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1259 // NOTE: a size of zero for a .comm should create a undefined symbol
1260 // but a size of .lcomm creates a bss symbol of size zero.
1262 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1263 "be less than zero");
1265 // NOTE: The alignment in the directive is a power of 2 value, the assember
1266 // may internally end up wanting an alignment in bytes.
1267 // FIXME: Diagnose overflow.
1268 if (Pow2Alignment < 0)
1269 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1270 "alignment, can't be less than zero");
1272 if (!Sym->isUndefined())
1273 return Error(IDLoc, "invalid symbol redefinition");
1275 // '.lcomm' is equivalent to '.zerofill'.
1276 // Create the Symbol as a common or local common with Size and Pow2Alignment
1278 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1279 MCSectionMachO::S_ZEROFILL, 0,
1281 Sym, Size, 1 << Pow2Alignment);
1285 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1289 /// ParseDirectiveDarwinZerofill
1290 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1291 /// , align_expression ]]
1292 bool AsmParser::ParseDirectiveDarwinZerofill() {
1293 // FIXME: Handle quoted names here.
1295 if (Lexer.isNot(AsmToken::Identifier))
1296 return TokError("expected segment name after '.zerofill' directive");
1297 StringRef Segment = Lexer.getTok().getString();
1300 if (Lexer.isNot(AsmToken::Comma))
1301 return TokError("unexpected token in directive");
1304 if (Lexer.isNot(AsmToken::Identifier))
1305 return TokError("expected section name after comma in '.zerofill' "
1307 StringRef Section = Lexer.getTok().getString();
1310 // If this is the end of the line all that was wanted was to create the
1311 // the section but with no symbol.
1312 if (Lexer.is(AsmToken::EndOfStatement)) {
1313 // Create the zerofill section but no symbol
1314 Out.EmitZerofill(getMachOSection(Segment, Section,
1315 MCSectionMachO::S_ZEROFILL, 0,
1320 if (Lexer.isNot(AsmToken::Comma))
1321 return TokError("unexpected token in directive");
1324 if (Lexer.isNot(AsmToken::Identifier))
1325 return TokError("expected identifier in directive");
1327 // handle the identifier as the key symbol.
1328 SMLoc IDLoc = Lexer.getLoc();
1329 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
1332 if (Lexer.isNot(AsmToken::Comma))
1333 return TokError("unexpected token in directive");
1337 SMLoc SizeLoc = Lexer.getLoc();
1338 if (ParseAbsoluteExpression(Size))
1341 int64_t Pow2Alignment = 0;
1342 SMLoc Pow2AlignmentLoc;
1343 if (Lexer.is(AsmToken::Comma)) {
1345 Pow2AlignmentLoc = Lexer.getLoc();
1346 if (ParseAbsoluteExpression(Pow2Alignment))
1350 if (Lexer.isNot(AsmToken::EndOfStatement))
1351 return TokError("unexpected token in '.zerofill' directive");
1356 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1359 // NOTE: The alignment in the directive is a power of 2 value, the assember
1360 // may internally end up wanting an alignment in bytes.
1361 // FIXME: Diagnose overflow.
1362 if (Pow2Alignment < 0)
1363 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1364 "can't be less than zero");
1366 if (!Sym->isUndefined())
1367 return Error(IDLoc, "invalid symbol redefinition");
1369 // Create the zerofill Symbol with Size and Pow2Alignment
1371 // FIXME: Arch specific.
1372 Out.EmitZerofill(getMachOSection(Segment, Section,
1373 MCSectionMachO::S_ZEROFILL, 0,
1375 Sym, Size, 1 << Pow2Alignment);
1380 /// ParseDirectiveDarwinSubsectionsViaSymbols
1381 /// ::= .subsections_via_symbols
1382 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1383 if (Lexer.isNot(AsmToken::EndOfStatement))
1384 return TokError("unexpected token in '.subsections_via_symbols' directive");
1388 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1393 /// ParseDirectiveAbort
1394 /// ::= .abort [ "abort_string" ]
1395 bool AsmParser::ParseDirectiveAbort() {
1396 // FIXME: Use loc from directive.
1397 SMLoc Loc = Lexer.getLoc();
1400 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1401 if (Lexer.isNot(AsmToken::String))
1402 return TokError("expected string in '.abort' directive");
1404 Str = Lexer.getTok().getString();
1409 if (Lexer.isNot(AsmToken::EndOfStatement))
1410 return TokError("unexpected token in '.abort' directive");
1414 // FIXME: Handle here.
1416 Error(Loc, ".abort detected. Assembly stopping.");
1418 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1423 /// ParseDirectiveLsym
1424 /// ::= .lsym identifier , expression
1425 bool AsmParser::ParseDirectiveDarwinLsym() {
1427 if (ParseIdentifier(Name))
1428 return TokError("expected identifier in directive");
1430 // Handle the identifier as the key symbol.
1431 MCSymbol *Sym = CreateSymbol(Name);
1433 if (Lexer.isNot(AsmToken::Comma))
1434 return TokError("unexpected token in '.lsym' directive");
1437 const MCExpr *Value;
1438 SMLoc StartLoc = Lexer.getLoc();
1439 if (ParseExpression(Value))
1442 if (Lexer.isNot(AsmToken::EndOfStatement))
1443 return TokError("unexpected token in '.lsym' directive");
1447 // We don't currently support this directive.
1449 // FIXME: Diagnostic location!
1451 return TokError("directive '.lsym' is unsupported");
1454 /// ParseDirectiveInclude
1455 /// ::= .include "filename"
1456 bool AsmParser::ParseDirectiveInclude() {
1457 if (Lexer.isNot(AsmToken::String))
1458 return TokError("expected string in '.include' directive");
1460 std::string Filename = Lexer.getTok().getString();
1461 SMLoc IncludeLoc = Lexer.getLoc();
1464 if (Lexer.isNot(AsmToken::EndOfStatement))
1465 return TokError("unexpected token in '.include' directive");
1467 // Strip the quotes.
1468 Filename = Filename.substr(1, Filename.size()-2);
1470 // Attempt to switch the lexer to the included file before consuming the end
1471 // of statement to avoid losing it when we switch.
1472 if (Lexer.EnterIncludeFile(Filename)) {
1473 Lexer.PrintMessage(IncludeLoc,
1474 "Could not find include file '" + Filename + "'",
1482 /// ParseDirectiveDarwinDumpOrLoad
1483 /// ::= ( .dump | .load ) "filename"
1484 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1485 if (Lexer.isNot(AsmToken::String))
1486 return TokError("expected string in '.dump' or '.load' directive");
1490 if (Lexer.isNot(AsmToken::EndOfStatement))
1491 return TokError("unexpected token in '.dump' or '.load' directive");
1495 // FIXME: If/when .dump and .load are implemented they will be done in the
1496 // the assembly parser and not have any need for an MCStreamer API.
1498 Warning(IDLoc, "ignoring directive .dump for now");
1500 Warning(IDLoc, "ignoring directive .load for now");
1505 /// ParseDirectiveIf
1506 /// ::= .if expression
1507 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1508 // Consume the identifier that was the .if directive
1511 TheCondStack.push_back(TheCondState);
1512 TheCondState.TheCond = AsmCond::IfCond;
1513 if(TheCondState.Ignore) {
1514 EatToEndOfStatement();
1518 if (ParseAbsoluteExpression(ExprValue))
1521 if (Lexer.isNot(AsmToken::EndOfStatement))
1522 return TokError("unexpected token in '.if' directive");
1526 TheCondState.CondMet = ExprValue;
1527 TheCondState.Ignore = !TheCondState.CondMet;
1533 /// ParseDirectiveElseIf
1534 /// ::= .elseif expression
1535 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1536 if (TheCondState.TheCond != AsmCond::IfCond &&
1537 TheCondState.TheCond != AsmCond::ElseIfCond)
1538 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1540 TheCondState.TheCond = AsmCond::ElseIfCond;
1542 // Consume the identifier that was the .elseif directive
1545 bool LastIgnoreState = false;
1546 if (!TheCondStack.empty())
1547 LastIgnoreState = TheCondStack.back().Ignore;
1548 if (LastIgnoreState || TheCondState.CondMet) {
1549 TheCondState.Ignore = true;
1550 EatToEndOfStatement();
1554 if (ParseAbsoluteExpression(ExprValue))
1557 if (Lexer.isNot(AsmToken::EndOfStatement))
1558 return TokError("unexpected token in '.elseif' directive");
1561 TheCondState.CondMet = ExprValue;
1562 TheCondState.Ignore = !TheCondState.CondMet;
1568 /// ParseDirectiveElse
1570 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1571 // Consume the identifier that was the .else directive
1574 if (Lexer.isNot(AsmToken::EndOfStatement))
1575 return TokError("unexpected token in '.else' directive");
1579 if (TheCondState.TheCond != AsmCond::IfCond &&
1580 TheCondState.TheCond != AsmCond::ElseIfCond)
1581 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1583 TheCondState.TheCond = AsmCond::ElseCond;
1584 bool LastIgnoreState = false;
1585 if (!TheCondStack.empty())
1586 LastIgnoreState = TheCondStack.back().Ignore;
1587 if (LastIgnoreState || TheCondState.CondMet)
1588 TheCondState.Ignore = true;
1590 TheCondState.Ignore = false;
1595 /// ParseDirectiveEndIf
1597 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1598 // Consume the identifier that was the .endif directive
1601 if (Lexer.isNot(AsmToken::EndOfStatement))
1602 return TokError("unexpected token in '.endif' directive");
1606 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1607 TheCondStack.empty())
1608 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1610 if (!TheCondStack.empty()) {
1611 TheCondState = TheCondStack.back();
1612 TheCondStack.pop_back();
1618 /// ParseDirectiveFile
1619 /// ::= .file [number] string
1620 bool AsmParser::ParseDirectiveFile(StringRef, SMLoc DirectiveLoc) {
1621 // FIXME: I'm not sure what this is.
1622 int64_t FileNumber = -1;
1623 if (Lexer.is(AsmToken::Integer)) {
1624 FileNumber = Lexer.getTok().getIntVal();
1628 return TokError("file number less than one");
1631 if (Lexer.isNot(AsmToken::String))
1632 return TokError("unexpected token in '.file' directive");
1634 StringRef FileName = Lexer.getTok().getString();
1637 if (Lexer.isNot(AsmToken::EndOfStatement))
1638 return TokError("unexpected token in '.file' directive");
1640 // FIXME: Do something with the .file.
1645 /// ParseDirectiveLine
1646 /// ::= .line [number]
1647 bool AsmParser::ParseDirectiveLine(StringRef, SMLoc DirectiveLoc) {
1648 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1649 if (Lexer.isNot(AsmToken::Integer))
1650 return TokError("unexpected token in '.line' directive");
1652 int64_t LineNumber = Lexer.getTok().getIntVal();
1656 // FIXME: Do something with the .line.
1659 if (Lexer.isNot(AsmToken::EndOfStatement))
1660 return TokError("unexpected token in '.file' directive");
1666 /// ParseDirectiveLoc
1667 /// ::= .loc number [number [number]]
1668 bool AsmParser::ParseDirectiveLoc(StringRef, SMLoc DirectiveLoc) {
1669 if (Lexer.isNot(AsmToken::Integer))
1670 return TokError("unexpected token in '.loc' directive");
1672 // FIXME: What are these fields?
1673 int64_t FileNumber = Lexer.getTok().getIntVal();
1675 // FIXME: Validate file.
1678 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1679 if (Lexer.isNot(AsmToken::Integer))
1680 return TokError("unexpected token in '.loc' directive");
1682 int64_t Param2 = Lexer.getTok().getIntVal();
1686 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1687 if (Lexer.isNot(AsmToken::Integer))
1688 return TokError("unexpected token in '.loc' directive");
1690 int64_t Param3 = Lexer.getTok().getIntVal();
1694 // FIXME: Do something with the .loc.
1698 if (Lexer.isNot(AsmToken::EndOfStatement))
1699 return TokError("unexpected token in '.file' directive");