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/Support/SourceMgr.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetAsmParser.h"
29 // Mach-O section uniquing.
31 // FIXME: Figure out where this should live, it should be shared by
32 // TargetLoweringObjectFile.
33 typedef StringMap<const MCSectionMachO*> MachOUniqueMapTy;
35 AsmParser::~AsmParser() {
36 // If we have the MachO uniquing map, free it.
37 delete (MachOUniqueMapTy*)SectionUniquingMap;
40 const MCSection *AsmParser::getMachOSection(const StringRef &Segment,
41 const StringRef &Section,
42 unsigned TypeAndAttributes,
44 SectionKind Kind) const {
45 // We unique sections by their segment/section pair. The returned section
46 // may not have the same flags as the requested section, if so this should be
47 // diagnosed by the client as an error.
49 // Create the map if it doesn't already exist.
50 if (SectionUniquingMap == 0)
51 SectionUniquingMap = new MachOUniqueMapTy();
52 MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)SectionUniquingMap;
54 // Form the name to look up.
60 // Do the lookup, if we have a hit, return it.
61 const MCSectionMachO *&Entry = Map[Name.str()];
63 // FIXME: This should validate the type and attributes.
64 if (Entry) return Entry;
66 // Otherwise, return a new section.
67 return Entry = MCSectionMachO::Create(Segment, Section, TypeAndAttributes,
68 Reserved2, Kind, Ctx);
71 void AsmParser::Warning(SMLoc L, const Twine &Msg) {
72 Lexer.PrintMessage(L, Msg.str(), "warning");
75 bool AsmParser::Error(SMLoc L, const Twine &Msg) {
76 Lexer.PrintMessage(L, Msg.str(), "error");
80 bool AsmParser::TokError(const char *Msg) {
81 Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
85 bool AsmParser::Run() {
86 // Create the initial section.
89 // FIXME: Target hook & command line option for initial section.
90 Out.SwitchSection(getMachOSection("__TEXT", "__text",
91 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
98 bool HadError = false;
100 AsmCond StartingCondState = TheCondState;
102 // While we have input, parse each statement.
103 while (Lexer.isNot(AsmToken::Eof)) {
104 // Handle conditional assembly here before calling ParseStatement()
105 if (Lexer.getKind() == AsmToken::Identifier) {
106 // If we have an identifier, handle it as the key symbol.
107 AsmToken ID = Lexer.getTok();
108 SMLoc IDLoc = ID.getLoc();
109 StringRef IDVal = ID.getString();
111 if (IDVal == ".if" ||
112 IDVal == ".elseif" ||
115 if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
118 EatToEndOfStatement();
122 if (TheCondState.Ignore) {
123 EatToEndOfStatement();
127 if (!ParseStatement()) continue;
129 // We had an error, remember it and recover by skipping to the next line.
131 EatToEndOfStatement();
134 if (TheCondState.TheCond != StartingCondState.TheCond ||
135 TheCondState.Ignore != StartingCondState.Ignore)
136 return TokError("unmatched .ifs or .elses");
144 /// ParseConditionalAssemblyDirectives - parse the conditional assembly
146 bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
147 SMLoc DirectiveLoc) {
148 if (Directive == ".if")
149 return ParseDirectiveIf(DirectiveLoc);
150 if (Directive == ".elseif")
151 return ParseDirectiveElseIf(DirectiveLoc);
152 if (Directive == ".else")
153 return ParseDirectiveElse(DirectiveLoc);
154 if (Directive == ".endif")
155 return ParseDirectiveEndIf(DirectiveLoc);
159 /// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
160 void AsmParser::EatToEndOfStatement() {
161 while (Lexer.isNot(AsmToken::EndOfStatement) &&
162 Lexer.isNot(AsmToken::Eof))
166 if (Lexer.is(AsmToken::EndOfStatement))
171 /// ParseParenExpr - Parse a paren expression and return it.
172 /// NOTE: This assumes the leading '(' has already been consumed.
174 /// parenexpr ::= expr)
176 bool AsmParser::ParseParenExpr(const MCExpr *&Res) {
177 if (ParseExpression(Res)) return true;
178 if (Lexer.isNot(AsmToken::RParen))
179 return TokError("expected ')' in parentheses expression");
184 MCSymbol *AsmParser::CreateSymbol(StringRef Name) {
185 if (MCSymbol *S = Ctx.LookupSymbol(Name))
188 // If the label starts with L it is an assembler temporary label.
189 if (Name.startswith("L"))
190 return Ctx.CreateTemporarySymbol(Name);
192 return Ctx.CreateSymbol(Name);
195 /// ParsePrimaryExpr - Parse a primary expression and return it.
196 /// primaryexpr ::= (parenexpr
197 /// primaryexpr ::= symbol
198 /// primaryexpr ::= number
199 /// primaryexpr ::= ~,+,- primaryexpr
200 bool AsmParser::ParsePrimaryExpr(const MCExpr *&Res) {
201 switch (Lexer.getKind()) {
203 return TokError("unknown token in expression");
204 case AsmToken::Exclaim:
205 Lexer.Lex(); // Eat the operator.
206 if (ParsePrimaryExpr(Res))
208 Res = MCUnaryExpr::CreateLNot(Res, Ctx);
210 case AsmToken::String:
211 case AsmToken::Identifier: {
212 // This is a label, this should be parsed as part of an expression, to
213 // handle things like LFOO+4.
214 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getIdentifier());
216 Res = MCSymbolRefExpr::Create(Sym, Ctx);
217 Lexer.Lex(); // Eat identifier.
220 case AsmToken::Integer:
221 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), Ctx);
222 Lexer.Lex(); // Eat token.
224 case AsmToken::LParen:
225 Lexer.Lex(); // Eat the '('.
226 return ParseParenExpr(Res);
227 case AsmToken::Minus:
228 Lexer.Lex(); // Eat the operator.
229 if (ParsePrimaryExpr(Res))
231 Res = MCUnaryExpr::CreateMinus(Res, Ctx);
234 Lexer.Lex(); // Eat the operator.
235 if (ParsePrimaryExpr(Res))
237 Res = MCUnaryExpr::CreatePlus(Res, Ctx);
239 case AsmToken::Tilde:
240 Lexer.Lex(); // Eat the operator.
241 if (ParsePrimaryExpr(Res))
243 Res = MCUnaryExpr::CreateNot(Res, Ctx);
248 /// ParseExpression - Parse an expression and return it.
250 /// expr ::= expr +,- expr -> lowest.
251 /// expr ::= expr |,^,&,! expr -> middle.
252 /// expr ::= expr *,/,%,<<,>> expr -> highest.
253 /// expr ::= primaryexpr
255 bool AsmParser::ParseExpression(const MCExpr *&Res) {
257 return ParsePrimaryExpr(Res) ||
258 ParseBinOpRHS(1, Res);
261 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
264 SMLoc StartLoc = Lexer.getLoc();
265 if (ParseExpression(Expr))
268 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
269 return Error(StartLoc, "expected absolute expression");
274 bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
277 SMLoc StartLoc = Lexer.getLoc();
278 if (ParseExpression(Expr))
281 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
282 return Error(StartLoc, "expected relocatable expression");
287 bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
290 SMLoc StartLoc = Lexer.getLoc();
291 if (ParseParenExpr(Expr))
294 if (!Expr->EvaluateAsRelocatable(Ctx, Res))
295 return Error(StartLoc, "expected relocatable expression");
300 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
301 MCBinaryExpr::Opcode &Kind) {
303 default: return 0; // not a binop.
305 // Lowest Precedence: &&, ||
306 case AsmToken::AmpAmp:
307 Kind = MCBinaryExpr::LAnd;
309 case AsmToken::PipePipe:
310 Kind = MCBinaryExpr::LOr;
313 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
315 Kind = MCBinaryExpr::Add;
317 case AsmToken::Minus:
318 Kind = MCBinaryExpr::Sub;
320 case AsmToken::EqualEqual:
321 Kind = MCBinaryExpr::EQ;
323 case AsmToken::ExclaimEqual:
324 case AsmToken::LessGreater:
325 Kind = MCBinaryExpr::NE;
328 Kind = MCBinaryExpr::LT;
330 case AsmToken::LessEqual:
331 Kind = MCBinaryExpr::LTE;
333 case AsmToken::Greater:
334 Kind = MCBinaryExpr::GT;
336 case AsmToken::GreaterEqual:
337 Kind = MCBinaryExpr::GTE;
340 // Intermediate Precedence: |, &, ^
342 // FIXME: gas seems to support '!' as an infix operator?
344 Kind = MCBinaryExpr::Or;
346 case AsmToken::Caret:
347 Kind = MCBinaryExpr::Xor;
350 Kind = MCBinaryExpr::And;
353 // Highest Precedence: *, /, %, <<, >>
355 Kind = MCBinaryExpr::Mul;
357 case AsmToken::Slash:
358 Kind = MCBinaryExpr::Div;
360 case AsmToken::Percent:
361 Kind = MCBinaryExpr::Mod;
363 case AsmToken::LessLess:
364 Kind = MCBinaryExpr::Shl;
366 case AsmToken::GreaterGreater:
367 Kind = MCBinaryExpr::Shr;
373 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
374 /// Res contains the LHS of the expression on input.
375 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
377 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
378 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
380 // If the next token is lower precedence than we are allowed to eat, return
381 // successfully with what we ate already.
382 if (TokPrec < Precedence)
387 // Eat the next primary expression.
389 if (ParsePrimaryExpr(RHS)) return true;
391 // If BinOp binds less tightly with RHS than the operator after RHS, let
392 // the pending operator take RHS as its LHS.
393 MCBinaryExpr::Opcode Dummy;
394 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
395 if (TokPrec < NextTokPrec) {
396 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
399 // Merge LHS and RHS according to operator.
400 Res = MCBinaryExpr::Create(Kind, Res, RHS, Ctx);
408 /// ::= EndOfStatement
409 /// ::= Label* Directive ...Operands... EndOfStatement
410 /// ::= Label* Identifier OperandList* EndOfStatement
411 bool AsmParser::ParseStatement() {
412 if (Lexer.is(AsmToken::EndOfStatement)) {
417 // Statements always start with an identifier.
418 AsmToken ID = Lexer.getTok();
419 SMLoc IDLoc = ID.getLoc();
421 if (ParseIdentifier(IDVal))
422 return TokError("unexpected token at start of statement");
424 // FIXME: Recurse on local labels?
426 // See what kind of statement we have.
427 switch (Lexer.getKind()) {
428 case AsmToken::Colon: {
429 // identifier ':' -> Label.
432 // Diagnose attempt to use a variable as a label.
434 // FIXME: Diagnostics. Note the location of the definition as a label.
435 // FIXME: This doesn't diagnose assignment to a symbol which has been
436 // implicitly marked as external.
437 MCSymbol *Sym = CreateSymbol(IDVal);
438 if (!Sym->isUndefined())
439 return Error(IDLoc, "invalid symbol redefinition");
444 return ParseStatement();
447 case AsmToken::Equal:
448 // identifier '=' ... -> assignment statement
451 return ParseAssignment(IDVal, false);
453 default: // Normal instruction or directive.
457 // Otherwise, we have a normal instruction or directive.
458 if (IDVal[0] == '.') {
459 // FIXME: This should be driven based on a hash lookup and callback.
460 if (IDVal == ".section")
461 return ParseDirectiveDarwinSection();
462 if (IDVal == ".text")
463 // FIXME: This changes behavior based on the -static flag to the
465 return ParseDirectiveSectionSwitch("__TEXT", "__text",
466 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
467 if (IDVal == ".const")
468 return ParseDirectiveSectionSwitch("__TEXT", "__const");
469 if (IDVal == ".static_const")
470 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
471 if (IDVal == ".cstring")
472 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
473 MCSectionMachO::S_CSTRING_LITERALS);
474 if (IDVal == ".literal4")
475 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
476 MCSectionMachO::S_4BYTE_LITERALS,
478 if (IDVal == ".literal8")
479 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
480 MCSectionMachO::S_8BYTE_LITERALS,
482 if (IDVal == ".literal16")
483 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
484 MCSectionMachO::S_16BYTE_LITERALS,
486 if (IDVal == ".constructor")
487 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
488 if (IDVal == ".destructor")
489 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
490 if (IDVal == ".fvmlib_init0")
491 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
492 if (IDVal == ".fvmlib_init1")
493 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
495 // FIXME: The assembler manual claims that this has the self modify code
496 // flag, at least on x86-32, but that does not appear to be correct.
497 if (IDVal == ".symbol_stub")
498 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
499 MCSectionMachO::S_SYMBOL_STUBS |
500 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
501 // FIXME: Different on PPC and ARM.
503 // FIXME: PowerPC only?
504 if (IDVal == ".picsymbol_stub")
505 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
506 MCSectionMachO::S_SYMBOL_STUBS |
507 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
509 if (IDVal == ".data")
510 return ParseDirectiveSectionSwitch("__DATA", "__data");
511 if (IDVal == ".static_data")
512 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
514 // FIXME: The section names of these two are misspelled in the assembler
516 if (IDVal == ".non_lazy_symbol_pointer")
517 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
518 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
520 if (IDVal == ".lazy_symbol_pointer")
521 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
522 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
525 if (IDVal == ".dyld")
526 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
527 if (IDVal == ".mod_init_func")
528 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
529 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
531 if (IDVal == ".mod_term_func")
532 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
533 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
535 if (IDVal == ".const_data")
536 return ParseDirectiveSectionSwitch("__DATA", "__const");
539 if (IDVal == ".objc_class")
540 return ParseDirectiveSectionSwitch("__OBJC", "__class",
541 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
542 if (IDVal == ".objc_meta_class")
543 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
545 if (IDVal == ".objc_cat_cls_meth")
546 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
547 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
548 if (IDVal == ".objc_cat_inst_meth")
549 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
550 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
551 if (IDVal == ".objc_protocol")
552 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
553 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
554 if (IDVal == ".objc_string_object")
555 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
556 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
557 if (IDVal == ".objc_cls_meth")
558 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
559 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
560 if (IDVal == ".objc_inst_meth")
561 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
562 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
563 if (IDVal == ".objc_cls_refs")
564 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
565 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
566 MCSectionMachO::S_LITERAL_POINTERS,
568 if (IDVal == ".objc_message_refs")
569 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
570 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
571 MCSectionMachO::S_LITERAL_POINTERS,
573 if (IDVal == ".objc_symbols")
574 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
575 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
576 if (IDVal == ".objc_category")
577 return ParseDirectiveSectionSwitch("__OBJC", "__category",
578 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
579 if (IDVal == ".objc_class_vars")
580 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
581 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
582 if (IDVal == ".objc_instance_vars")
583 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
584 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
585 if (IDVal == ".objc_module_info")
586 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
587 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
588 if (IDVal == ".objc_class_names")
589 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
590 MCSectionMachO::S_CSTRING_LITERALS);
591 if (IDVal == ".objc_meth_var_types")
592 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
593 MCSectionMachO::S_CSTRING_LITERALS);
594 if (IDVal == ".objc_meth_var_names")
595 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
596 MCSectionMachO::S_CSTRING_LITERALS);
597 if (IDVal == ".objc_selector_strs")
598 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
599 MCSectionMachO::S_CSTRING_LITERALS);
601 // Assembler features
603 return ParseDirectiveSet();
607 if (IDVal == ".ascii")
608 return ParseDirectiveAscii(false);
609 if (IDVal == ".asciz")
610 return ParseDirectiveAscii(true);
612 // FIXME: Target hooks for size? Also for "word", "hword".
613 if (IDVal == ".byte")
614 return ParseDirectiveValue(1);
615 if (IDVal == ".short" || IDVal == ".word")
616 return ParseDirectiveValue(2);
617 if (IDVal == ".long")
618 return ParseDirectiveValue(4);
619 if (IDVal == ".quad")
620 return ParseDirectiveValue(8);
622 // FIXME: Target hooks for IsPow2.
623 if (IDVal == ".align")
624 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
625 if (IDVal == ".align32")
626 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
627 if (IDVal == ".balign")
628 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
629 if (IDVal == ".balignw")
630 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
631 if (IDVal == ".balignl")
632 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
633 if (IDVal == ".p2align")
634 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
635 if (IDVal == ".p2alignw")
636 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
637 if (IDVal == ".p2alignl")
638 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
641 return ParseDirectiveOrg();
643 if (IDVal == ".fill")
644 return ParseDirectiveFill();
645 if (IDVal == ".space")
646 return ParseDirectiveSpace();
648 // Symbol attribute directives
650 if (IDVal == ".globl" || IDVal == ".global")
651 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
652 if (IDVal == ".hidden")
653 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
654 if (IDVal == ".indirect_symbol")
655 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
656 if (IDVal == ".internal")
657 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
658 if (IDVal == ".lazy_reference")
659 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
660 if (IDVal == ".no_dead_strip")
661 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
662 if (IDVal == ".private_extern")
663 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
664 if (IDVal == ".protected")
665 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
666 if (IDVal == ".reference")
667 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
668 if (IDVal == ".weak")
669 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
670 if (IDVal == ".weak_definition")
671 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
672 if (IDVal == ".weak_reference")
673 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
675 if (IDVal == ".comm")
676 return ParseDirectiveComm(/*IsLocal=*/false);
677 if (IDVal == ".lcomm")
678 return ParseDirectiveComm(/*IsLocal=*/true);
679 if (IDVal == ".zerofill")
680 return ParseDirectiveDarwinZerofill();
681 if (IDVal == ".desc")
682 return ParseDirectiveDarwinSymbolDesc();
683 if (IDVal == ".lsym")
684 return ParseDirectiveDarwinLsym();
686 if (IDVal == ".subsections_via_symbols")
687 return ParseDirectiveDarwinSubsectionsViaSymbols();
688 if (IDVal == ".abort")
689 return ParseDirectiveAbort();
690 if (IDVal == ".include")
691 return ParseDirectiveInclude();
692 if (IDVal == ".dump")
693 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
694 if (IDVal == ".load")
695 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
697 // Debugging directives
699 if (IDVal == ".file")
700 return ParseDirectiveFile(IDLoc);
701 if (IDVal == ".line")
702 return ParseDirectiveLine(IDLoc);
704 return ParseDirectiveLoc(IDLoc);
706 Warning(IDLoc, "ignoring directive for now");
707 EatToEndOfStatement();
712 if (getTargetParser().ParseInstruction(IDVal, Inst))
715 if (Lexer.isNot(AsmToken::EndOfStatement))
716 return TokError("unexpected token in argument list");
718 // Eat the end of statement marker.
721 // Instruction is good, process it.
722 Out.EmitInstruction(Inst);
724 // Skip to end of line for now.
728 bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
729 // FIXME: Use better location, we should use proper tokens.
730 SMLoc EqualLoc = Lexer.getLoc();
733 if (ParseRelocatableExpression(Value))
736 if (Lexer.isNot(AsmToken::EndOfStatement))
737 return TokError("unexpected token in assignment");
739 // Eat the end of statement marker.
742 // Diagnose assignment to a label.
744 // FIXME: Diagnostics. Note the location of the definition as a label.
745 // FIXME: Handle '.'.
746 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
747 MCSymbol *Sym = CreateSymbol(Name);
748 if (!Sym->isUndefined() && !Sym->isAbsolute())
749 return Error(EqualLoc, "symbol has already been defined");
751 // Do the assignment.
752 Out.EmitAssignment(Sym, Value, IsDotSet);
760 bool AsmParser::ParseIdentifier(StringRef &Res) {
761 if (Lexer.isNot(AsmToken::Identifier) &&
762 Lexer.isNot(AsmToken::String))
765 Res = Lexer.getTok().getIdentifier();
767 Lexer.Lex(); // Consume the identifier token.
772 /// ParseDirectiveSet:
773 /// ::= .set identifier ',' expression
774 bool AsmParser::ParseDirectiveSet() {
777 if (ParseIdentifier(Name))
778 return TokError("expected identifier after '.set' directive");
780 if (Lexer.isNot(AsmToken::Comma))
781 return TokError("unexpected token in '.set'");
784 return ParseAssignment(Name, true);
787 /// ParseDirectiveSection:
788 /// ::= .section identifier (',' identifier)*
789 /// FIXME: This should actually parse out the segment, section, attributes and
790 /// sizeof_stub fields.
791 bool AsmParser::ParseDirectiveDarwinSection() {
792 SMLoc Loc = Lexer.getLoc();
794 StringRef SectionName;
795 if (ParseIdentifier(SectionName))
796 return Error(Loc, "expected identifier after '.section' directive");
798 // Verify there is a following comma.
799 if (!Lexer.is(AsmToken::Comma))
800 return TokError("unexpected token in '.section' directive");
802 std::string SectionSpec = SectionName;
805 // Add all the tokens until the end of the line, ParseSectionSpecifier will
807 StringRef EOL = Lexer.LexUntilEndOfStatement();
808 SectionSpec.append(EOL.begin(), EOL.end());
811 if (Lexer.isNot(AsmToken::EndOfStatement))
812 return TokError("unexpected token in '.section' directive");
816 StringRef Segment, Section;
817 unsigned TAA, StubSize;
818 std::string ErrorStr =
819 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
822 if (!ErrorStr.empty())
823 return Error(Loc, ErrorStr.c_str());
825 // FIXME: Arch specific.
826 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
831 /// ParseDirectiveSectionSwitch -
832 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
834 unsigned TAA, unsigned Align,
836 if (Lexer.isNot(AsmToken::EndOfStatement))
837 return TokError("unexpected token in section switching directive");
840 // FIXME: Arch specific.
841 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
844 // Set the implicit alignment, if any.
846 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
847 // alignment on the section (e.g., if one manually inserts bytes into the
848 // section, then just issueing the section switch directive will not realign
849 // the section. However, this is arguably more reasonable behavior, and there
850 // is no good reason for someone to intentionally emit incorrectly sized
851 // values into the implicitly aligned sections.
853 Out.EmitValueToAlignment(Align, 0, 1, 0);
858 bool AsmParser::ParseEscapedString(std::string &Data) {
859 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
862 StringRef Str = Lexer.getTok().getStringContents();
863 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
864 if (Str[i] != '\\') {
869 // Recognize escaped characters. Note that this escape semantics currently
870 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
873 return TokError("unexpected backslash at end of string");
875 // Recognize octal sequences.
876 if ((unsigned) (Str[i] - '0') <= 7) {
877 // Consume up to three octal characters.
878 unsigned Value = Str[i] - '0';
880 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
882 Value = Value * 8 + (Str[i] - '0');
884 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
886 Value = Value * 8 + (Str[i] - '0');
891 return TokError("invalid octal escape sequence (out of range)");
893 Data += (unsigned char) Value;
897 // Otherwise recognize individual escapes.
900 // Just reject invalid escape sequences for now.
901 return TokError("invalid escape sequence (unrecognized character)");
903 case 'b': Data += '\b'; break;
904 case 'f': Data += '\f'; break;
905 case 'n': Data += '\n'; break;
906 case 'r': Data += '\r'; break;
907 case 't': Data += '\t'; break;
908 case '"': Data += '"'; break;
909 case '\\': Data += '\\'; break;
916 /// ParseDirectiveAscii:
917 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
918 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
919 if (Lexer.isNot(AsmToken::EndOfStatement)) {
921 if (Lexer.isNot(AsmToken::String))
922 return TokError("expected string in '.ascii' or '.asciz' directive");
925 if (ParseEscapedString(Data))
930 Out.EmitBytes(StringRef("\0", 1));
934 if (Lexer.is(AsmToken::EndOfStatement))
937 if (Lexer.isNot(AsmToken::Comma))
938 return TokError("unexpected token in '.ascii' or '.asciz' directive");
947 /// ParseDirectiveValue
948 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
949 bool AsmParser::ParseDirectiveValue(unsigned Size) {
950 if (Lexer.isNot(AsmToken::EndOfStatement)) {
953 if (ParseRelocatableExpression(Expr))
956 Out.EmitValue(Expr, Size);
958 if (Lexer.is(AsmToken::EndOfStatement))
961 // FIXME: Improve diagnostic.
962 if (Lexer.isNot(AsmToken::Comma))
963 return TokError("unexpected token in directive");
972 /// ParseDirectiveSpace
973 /// ::= .space expression [ , expression ]
974 bool AsmParser::ParseDirectiveSpace() {
976 if (ParseAbsoluteExpression(NumBytes))
979 int64_t FillExpr = 0;
980 bool HasFillExpr = false;
981 if (Lexer.isNot(AsmToken::EndOfStatement)) {
982 if (Lexer.isNot(AsmToken::Comma))
983 return TokError("unexpected token in '.space' directive");
986 if (ParseAbsoluteExpression(FillExpr))
991 if (Lexer.isNot(AsmToken::EndOfStatement))
992 return TokError("unexpected token in '.space' directive");
998 return TokError("invalid number of bytes in '.space' directive");
1000 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
1001 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
1002 Out.EmitValue(MCValue::get(FillExpr), 1);
1007 /// ParseDirectiveFill
1008 /// ::= .fill expression , expression , expression
1009 bool AsmParser::ParseDirectiveFill() {
1011 if (ParseAbsoluteExpression(NumValues))
1014 if (Lexer.isNot(AsmToken::Comma))
1015 return TokError("unexpected token in '.fill' directive");
1019 if (ParseAbsoluteExpression(FillSize))
1022 if (Lexer.isNot(AsmToken::Comma))
1023 return TokError("unexpected token in '.fill' directive");
1027 if (ParseAbsoluteExpression(FillExpr))
1030 if (Lexer.isNot(AsmToken::EndOfStatement))
1031 return TokError("unexpected token in '.fill' directive");
1035 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1036 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1038 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1039 Out.EmitValue(MCValue::get(FillExpr), FillSize);
1044 /// ParseDirectiveOrg
1045 /// ::= .org expression [ , expression ]
1046 bool AsmParser::ParseDirectiveOrg() {
1048 if (ParseRelocatableExpression(Offset))
1051 // Parse optional fill expression.
1052 int64_t FillExpr = 0;
1053 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1054 if (Lexer.isNot(AsmToken::Comma))
1055 return TokError("unexpected token in '.org' directive");
1058 if (ParseAbsoluteExpression(FillExpr))
1061 if (Lexer.isNot(AsmToken::EndOfStatement))
1062 return TokError("unexpected token in '.org' directive");
1067 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1068 // has to be relative to the current section.
1069 Out.EmitValueToOffset(Offset, FillExpr);
1074 /// ParseDirectiveAlign
1075 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1076 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1077 SMLoc AlignmentLoc = Lexer.getLoc();
1079 if (ParseAbsoluteExpression(Alignment))
1083 bool HasFillExpr = false;
1084 int64_t FillExpr = 0;
1085 int64_t MaxBytesToFill = 0;
1086 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1087 if (Lexer.isNot(AsmToken::Comma))
1088 return TokError("unexpected token in directive");
1091 // The fill expression can be omitted while specifying a maximum number of
1092 // alignment bytes, e.g:
1094 if (Lexer.isNot(AsmToken::Comma)) {
1096 if (ParseAbsoluteExpression(FillExpr))
1100 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1101 if (Lexer.isNot(AsmToken::Comma))
1102 return TokError("unexpected token in directive");
1105 MaxBytesLoc = Lexer.getLoc();
1106 if (ParseAbsoluteExpression(MaxBytesToFill))
1109 if (Lexer.isNot(AsmToken::EndOfStatement))
1110 return TokError("unexpected token in directive");
1117 // FIXME: Sometimes fill with nop.
1121 // Compute alignment in bytes.
1123 // FIXME: Diagnose overflow.
1124 if (Alignment >= 32) {
1125 Error(AlignmentLoc, "invalid alignment value");
1129 Alignment = 1 << Alignment;
1132 // Diagnose non-sensical max bytes to align.
1133 if (MaxBytesLoc.isValid()) {
1134 if (MaxBytesToFill < 1) {
1135 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1136 "many bytes, ignoring maximum bytes expression");
1140 if (MaxBytesToFill >= Alignment) {
1141 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1147 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1148 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1153 /// ParseDirectiveSymbolAttribute
1154 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1155 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
1156 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1160 if (ParseIdentifier(Name))
1161 return TokError("expected identifier in directive");
1163 MCSymbol *Sym = CreateSymbol(Name);
1165 Out.EmitSymbolAttribute(Sym, Attr);
1167 if (Lexer.is(AsmToken::EndOfStatement))
1170 if (Lexer.isNot(AsmToken::Comma))
1171 return TokError("unexpected token in directive");
1180 /// ParseDirectiveDarwinSymbolDesc
1181 /// ::= .desc identifier , expression
1182 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1184 if (ParseIdentifier(Name))
1185 return TokError("expected identifier in directive");
1187 // Handle the identifier as the key symbol.
1188 MCSymbol *Sym = CreateSymbol(Name);
1190 if (Lexer.isNot(AsmToken::Comma))
1191 return TokError("unexpected token in '.desc' directive");
1194 SMLoc DescLoc = Lexer.getLoc();
1196 if (ParseAbsoluteExpression(DescValue))
1199 if (Lexer.isNot(AsmToken::EndOfStatement))
1200 return TokError("unexpected token in '.desc' directive");
1204 // Set the n_desc field of this Symbol to this DescValue
1205 Out.EmitSymbolDesc(Sym, DescValue);
1210 /// ParseDirectiveComm
1211 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1212 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1213 SMLoc IDLoc = Lexer.getLoc();
1215 if (ParseIdentifier(Name))
1216 return TokError("expected identifier in directive");
1218 // Handle the identifier as the key symbol.
1219 MCSymbol *Sym = CreateSymbol(Name);
1221 if (Lexer.isNot(AsmToken::Comma))
1222 return TokError("unexpected token in directive");
1226 SMLoc SizeLoc = Lexer.getLoc();
1227 if (ParseAbsoluteExpression(Size))
1230 int64_t Pow2Alignment = 0;
1231 SMLoc Pow2AlignmentLoc;
1232 if (Lexer.is(AsmToken::Comma)) {
1234 Pow2AlignmentLoc = Lexer.getLoc();
1235 if (ParseAbsoluteExpression(Pow2Alignment))
1239 if (Lexer.isNot(AsmToken::EndOfStatement))
1240 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1244 // NOTE: a size of zero for a .comm should create a undefined symbol
1245 // but a size of .lcomm creates a bss symbol of size zero.
1247 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1248 "be less than zero");
1250 // NOTE: The alignment in the directive is a power of 2 value, the assember
1251 // may internally end up wanting an alignment in bytes.
1252 // FIXME: Diagnose overflow.
1253 if (Pow2Alignment < 0)
1254 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1255 "alignment, can't be less than zero");
1257 if (!Sym->isUndefined())
1258 return Error(IDLoc, "invalid symbol redefinition");
1260 // '.lcomm' is equivalent to '.zerofill'.
1261 // Create the Symbol as a common or local common with Size and Pow2Alignment
1263 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1264 MCSectionMachO::S_ZEROFILL, 0,
1266 Sym, Size, 1 << Pow2Alignment);
1270 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1274 /// ParseDirectiveDarwinZerofill
1275 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1276 /// , align_expression ]]
1277 bool AsmParser::ParseDirectiveDarwinZerofill() {
1278 // FIXME: Handle quoted names here.
1280 if (Lexer.isNot(AsmToken::Identifier))
1281 return TokError("expected segment name after '.zerofill' directive");
1282 StringRef Segment = Lexer.getTok().getString();
1285 if (Lexer.isNot(AsmToken::Comma))
1286 return TokError("unexpected token in directive");
1289 if (Lexer.isNot(AsmToken::Identifier))
1290 return TokError("expected section name after comma in '.zerofill' "
1292 StringRef Section = Lexer.getTok().getString();
1295 // If this is the end of the line all that was wanted was to create the
1296 // the section but with no symbol.
1297 if (Lexer.is(AsmToken::EndOfStatement)) {
1298 // Create the zerofill section but no symbol
1299 Out.EmitZerofill(getMachOSection(Segment, Section,
1300 MCSectionMachO::S_ZEROFILL, 0,
1305 if (Lexer.isNot(AsmToken::Comma))
1306 return TokError("unexpected token in directive");
1309 if (Lexer.isNot(AsmToken::Identifier))
1310 return TokError("expected identifier in directive");
1312 // handle the identifier as the key symbol.
1313 SMLoc IDLoc = Lexer.getLoc();
1314 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
1317 if (Lexer.isNot(AsmToken::Comma))
1318 return TokError("unexpected token in directive");
1322 SMLoc SizeLoc = Lexer.getLoc();
1323 if (ParseAbsoluteExpression(Size))
1326 int64_t Pow2Alignment = 0;
1327 SMLoc Pow2AlignmentLoc;
1328 if (Lexer.is(AsmToken::Comma)) {
1330 Pow2AlignmentLoc = Lexer.getLoc();
1331 if (ParseAbsoluteExpression(Pow2Alignment))
1335 if (Lexer.isNot(AsmToken::EndOfStatement))
1336 return TokError("unexpected token in '.zerofill' directive");
1341 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1344 // NOTE: The alignment in the directive is a power of 2 value, the assember
1345 // may internally end up wanting an alignment in bytes.
1346 // FIXME: Diagnose overflow.
1347 if (Pow2Alignment < 0)
1348 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1349 "can't be less than zero");
1351 if (!Sym->isUndefined())
1352 return Error(IDLoc, "invalid symbol redefinition");
1354 // Create the zerofill Symbol with Size and Pow2Alignment
1356 // FIXME: Arch specific.
1357 Out.EmitZerofill(getMachOSection(Segment, Section,
1358 MCSectionMachO::S_ZEROFILL, 0,
1360 Sym, Size, 1 << Pow2Alignment);
1365 /// ParseDirectiveDarwinSubsectionsViaSymbols
1366 /// ::= .subsections_via_symbols
1367 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1368 if (Lexer.isNot(AsmToken::EndOfStatement))
1369 return TokError("unexpected token in '.subsections_via_symbols' directive");
1373 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1378 /// ParseDirectiveAbort
1379 /// ::= .abort [ "abort_string" ]
1380 bool AsmParser::ParseDirectiveAbort() {
1381 // FIXME: Use loc from directive.
1382 SMLoc Loc = Lexer.getLoc();
1385 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1386 if (Lexer.isNot(AsmToken::String))
1387 return TokError("expected string in '.abort' directive");
1389 Str = Lexer.getTok().getString();
1394 if (Lexer.isNot(AsmToken::EndOfStatement))
1395 return TokError("unexpected token in '.abort' directive");
1399 // FIXME: Handle here.
1401 Error(Loc, ".abort detected. Assembly stopping.");
1403 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1408 /// ParseDirectiveLsym
1409 /// ::= .lsym identifier , expression
1410 bool AsmParser::ParseDirectiveDarwinLsym() {
1412 if (ParseIdentifier(Name))
1413 return TokError("expected identifier in directive");
1415 // Handle the identifier as the key symbol.
1416 MCSymbol *Sym = CreateSymbol(Name);
1418 if (Lexer.isNot(AsmToken::Comma))
1419 return TokError("unexpected token in '.lsym' directive");
1423 if (ParseRelocatableExpression(Expr))
1426 if (Lexer.isNot(AsmToken::EndOfStatement))
1427 return TokError("unexpected token in '.lsym' directive");
1431 // We don't currently support this directive.
1433 // FIXME: Diagnostic location!
1435 return TokError("directive '.lsym' is unsupported");
1438 /// ParseDirectiveInclude
1439 /// ::= .include "filename"
1440 bool AsmParser::ParseDirectiveInclude() {
1441 if (Lexer.isNot(AsmToken::String))
1442 return TokError("expected string in '.include' directive");
1444 std::string Filename = Lexer.getTok().getString();
1445 SMLoc IncludeLoc = Lexer.getLoc();
1448 if (Lexer.isNot(AsmToken::EndOfStatement))
1449 return TokError("unexpected token in '.include' directive");
1451 // Strip the quotes.
1452 Filename = Filename.substr(1, Filename.size()-2);
1454 // Attempt to switch the lexer to the included file before consuming the end
1455 // of statement to avoid losing it when we switch.
1456 if (Lexer.EnterIncludeFile(Filename)) {
1457 Lexer.PrintMessage(IncludeLoc,
1458 "Could not find include file '" + Filename + "'",
1466 /// ParseDirectiveDarwinDumpOrLoad
1467 /// ::= ( .dump | .load ) "filename"
1468 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1469 if (Lexer.isNot(AsmToken::String))
1470 return TokError("expected string in '.dump' or '.load' directive");
1474 if (Lexer.isNot(AsmToken::EndOfStatement))
1475 return TokError("unexpected token in '.dump' or '.load' directive");
1479 // FIXME: If/when .dump and .load are implemented they will be done in the
1480 // the assembly parser and not have any need for an MCStreamer API.
1482 Warning(IDLoc, "ignoring directive .dump for now");
1484 Warning(IDLoc, "ignoring directive .load for now");
1489 /// ParseDirectiveIf
1490 /// ::= .if expression
1491 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1492 // Consume the identifier that was the .if directive
1495 TheCondStack.push_back(TheCondState);
1496 TheCondState.TheCond = AsmCond::IfCond;
1497 if(TheCondState.Ignore) {
1498 EatToEndOfStatement();
1502 if (ParseAbsoluteExpression(ExprValue))
1505 if (Lexer.isNot(AsmToken::EndOfStatement))
1506 return TokError("unexpected token in '.if' directive");
1510 TheCondState.CondMet = ExprValue;
1511 TheCondState.Ignore = !TheCondState.CondMet;
1517 /// ParseDirectiveElseIf
1518 /// ::= .elseif expression
1519 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1520 if (TheCondState.TheCond != AsmCond::IfCond &&
1521 TheCondState.TheCond != AsmCond::ElseIfCond)
1522 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1524 TheCondState.TheCond = AsmCond::ElseIfCond;
1526 // Consume the identifier that was the .elseif directive
1529 bool LastIgnoreState = false;
1530 if (!TheCondStack.empty())
1531 LastIgnoreState = TheCondStack.back().Ignore;
1532 if (LastIgnoreState || TheCondState.CondMet) {
1533 TheCondState.Ignore = true;
1534 EatToEndOfStatement();
1538 if (ParseAbsoluteExpression(ExprValue))
1541 if (Lexer.isNot(AsmToken::EndOfStatement))
1542 return TokError("unexpected token in '.elseif' directive");
1545 TheCondState.CondMet = ExprValue;
1546 TheCondState.Ignore = !TheCondState.CondMet;
1552 /// ParseDirectiveElse
1554 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1555 // Consume the identifier that was the .else directive
1558 if (Lexer.isNot(AsmToken::EndOfStatement))
1559 return TokError("unexpected token in '.else' directive");
1563 if (TheCondState.TheCond != AsmCond::IfCond &&
1564 TheCondState.TheCond != AsmCond::ElseIfCond)
1565 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1567 TheCondState.TheCond = AsmCond::ElseCond;
1568 bool LastIgnoreState = false;
1569 if (!TheCondStack.empty())
1570 LastIgnoreState = TheCondStack.back().Ignore;
1571 if (LastIgnoreState || TheCondState.CondMet)
1572 TheCondState.Ignore = true;
1574 TheCondState.Ignore = false;
1579 /// ParseDirectiveEndIf
1581 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1582 // Consume the identifier that was the .endif directive
1585 if (Lexer.isNot(AsmToken::EndOfStatement))
1586 return TokError("unexpected token in '.endif' directive");
1590 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1591 TheCondStack.empty())
1592 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1594 if (!TheCondStack.empty()) {
1595 TheCondState = TheCondStack.back();
1596 TheCondStack.pop_back();
1602 /// ParseDirectiveFile
1603 /// ::= .file [number] string
1604 bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1605 // FIXME: I'm not sure what this is.
1606 int64_t FileNumber = -1;
1607 if (Lexer.is(AsmToken::Integer)) {
1608 FileNumber = Lexer.getTok().getIntVal();
1612 return TokError("file number less than one");
1615 if (Lexer.isNot(AsmToken::String))
1616 return TokError("unexpected token in '.file' directive");
1618 StringRef FileName = Lexer.getTok().getString();
1621 if (Lexer.isNot(AsmToken::EndOfStatement))
1622 return TokError("unexpected token in '.file' directive");
1624 // FIXME: Do something with the .file.
1629 /// ParseDirectiveLine
1630 /// ::= .line [number]
1631 bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1632 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1633 if (Lexer.isNot(AsmToken::Integer))
1634 return TokError("unexpected token in '.line' directive");
1636 int64_t LineNumber = Lexer.getTok().getIntVal();
1640 // FIXME: Do something with the .line.
1643 if (Lexer.isNot(AsmToken::EndOfStatement))
1644 return TokError("unexpected token in '.file' directive");
1650 /// ParseDirectiveLoc
1651 /// ::= .loc number [number [number]]
1652 bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1653 if (Lexer.isNot(AsmToken::Integer))
1654 return TokError("unexpected token in '.loc' directive");
1656 // FIXME: What are these fields?
1657 int64_t FileNumber = Lexer.getTok().getIntVal();
1659 // FIXME: Validate file.
1662 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1663 if (Lexer.isNot(AsmToken::Integer))
1664 return TokError("unexpected token in '.loc' directive");
1666 int64_t Param2 = Lexer.getTok().getIntVal();
1670 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1671 if (Lexer.isNot(AsmToken::Integer))
1672 return TokError("unexpected token in '.loc' directive");
1674 int64_t Param3 = Lexer.getTok().getIntVal();
1678 // FIXME: Do something with the .loc.
1682 if (Lexer.isNot(AsmToken::EndOfStatement))
1683 return TokError("unexpected token in '.file' directive");