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, getContext());
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, getContext());
217 Lexer.Lex(); // Eat identifier.
220 case AsmToken::Integer:
221 Res = MCConstantExpr::Create(Lexer.getTok().getIntVal(), getContext());
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, getContext());
234 Lexer.Lex(); // Eat the operator.
235 if (ParsePrimaryExpr(Res))
237 Res = MCUnaryExpr::CreatePlus(Res, getContext());
239 case AsmToken::Tilde:
240 Lexer.Lex(); // Eat the operator.
241 if (ParsePrimaryExpr(Res))
243 Res = MCUnaryExpr::CreateNot(Res, getContext());
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::ParseParenExpression(const MCExpr *&Res) {
262 if (ParseParenExpr(Res))
268 bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
271 SMLoc StartLoc = Lexer.getLoc();
272 if (ParseExpression(Expr))
275 if (!Expr->EvaluateAsAbsolute(Ctx, Res))
276 return Error(StartLoc, "expected absolute expression");
281 static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
282 MCBinaryExpr::Opcode &Kind) {
285 return 0; // not a binop.
287 // Lowest Precedence: &&, ||
288 case AsmToken::AmpAmp:
289 Kind = MCBinaryExpr::LAnd;
291 case AsmToken::PipePipe:
292 Kind = MCBinaryExpr::LOr;
295 // Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
297 Kind = MCBinaryExpr::Add;
299 case AsmToken::Minus:
300 Kind = MCBinaryExpr::Sub;
302 case AsmToken::EqualEqual:
303 Kind = MCBinaryExpr::EQ;
305 case AsmToken::ExclaimEqual:
306 case AsmToken::LessGreater:
307 Kind = MCBinaryExpr::NE;
310 Kind = MCBinaryExpr::LT;
312 case AsmToken::LessEqual:
313 Kind = MCBinaryExpr::LTE;
315 case AsmToken::Greater:
316 Kind = MCBinaryExpr::GT;
318 case AsmToken::GreaterEqual:
319 Kind = MCBinaryExpr::GTE;
322 // Intermediate Precedence: |, &, ^
324 // FIXME: gas seems to support '!' as an infix operator?
326 Kind = MCBinaryExpr::Or;
328 case AsmToken::Caret:
329 Kind = MCBinaryExpr::Xor;
332 Kind = MCBinaryExpr::And;
335 // Highest Precedence: *, /, %, <<, >>
337 Kind = MCBinaryExpr::Mul;
339 case AsmToken::Slash:
340 Kind = MCBinaryExpr::Div;
342 case AsmToken::Percent:
343 Kind = MCBinaryExpr::Mod;
345 case AsmToken::LessLess:
346 Kind = MCBinaryExpr::Shl;
348 case AsmToken::GreaterGreater:
349 Kind = MCBinaryExpr::Shr;
355 /// ParseBinOpRHS - Parse all binary operators with precedence >= 'Precedence'.
356 /// Res contains the LHS of the expression on input.
357 bool AsmParser::ParseBinOpRHS(unsigned Precedence, const MCExpr *&Res) {
359 MCBinaryExpr::Opcode Kind = MCBinaryExpr::Add;
360 unsigned TokPrec = getBinOpPrecedence(Lexer.getKind(), Kind);
362 // If the next token is lower precedence than we are allowed to eat, return
363 // successfully with what we ate already.
364 if (TokPrec < Precedence)
369 // Eat the next primary expression.
371 if (ParsePrimaryExpr(RHS)) return true;
373 // If BinOp binds less tightly with RHS than the operator after RHS, let
374 // the pending operator take RHS as its LHS.
375 MCBinaryExpr::Opcode Dummy;
376 unsigned NextTokPrec = getBinOpPrecedence(Lexer.getKind(), Dummy);
377 if (TokPrec < NextTokPrec) {
378 if (ParseBinOpRHS(Precedence+1, RHS)) return true;
381 // Merge LHS and RHS according to operator.
382 Res = MCBinaryExpr::Create(Kind, Res, RHS, getContext());
390 /// ::= EndOfStatement
391 /// ::= Label* Directive ...Operands... EndOfStatement
392 /// ::= Label* Identifier OperandList* EndOfStatement
393 bool AsmParser::ParseStatement() {
394 if (Lexer.is(AsmToken::EndOfStatement)) {
399 // Statements always start with an identifier.
400 AsmToken ID = Lexer.getTok();
401 SMLoc IDLoc = ID.getLoc();
403 if (ParseIdentifier(IDVal))
404 return TokError("unexpected token at start of statement");
406 // FIXME: Recurse on local labels?
408 // See what kind of statement we have.
409 switch (Lexer.getKind()) {
410 case AsmToken::Colon: {
411 // identifier ':' -> Label.
414 // Diagnose attempt to use a variable as a label.
416 // FIXME: Diagnostics. Note the location of the definition as a label.
417 // FIXME: This doesn't diagnose assignment to a symbol which has been
418 // implicitly marked as external.
419 MCSymbol *Sym = CreateSymbol(IDVal);
420 if (!Sym->isUndefined())
421 return Error(IDLoc, "invalid symbol redefinition");
426 return ParseStatement();
429 case AsmToken::Equal:
430 // identifier '=' ... -> assignment statement
433 return ParseAssignment(IDVal);
435 default: // Normal instruction or directive.
439 // Otherwise, we have a normal instruction or directive.
440 if (IDVal[0] == '.') {
441 // FIXME: This should be driven based on a hash lookup and callback.
442 if (IDVal == ".section")
443 return ParseDirectiveDarwinSection();
444 if (IDVal == ".text")
445 // FIXME: This changes behavior based on the -static flag to the
447 return ParseDirectiveSectionSwitch("__TEXT", "__text",
448 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS);
449 if (IDVal == ".const")
450 return ParseDirectiveSectionSwitch("__TEXT", "__const");
451 if (IDVal == ".static_const")
452 return ParseDirectiveSectionSwitch("__TEXT", "__static_const");
453 if (IDVal == ".cstring")
454 return ParseDirectiveSectionSwitch("__TEXT","__cstring",
455 MCSectionMachO::S_CSTRING_LITERALS);
456 if (IDVal == ".literal4")
457 return ParseDirectiveSectionSwitch("__TEXT", "__literal4",
458 MCSectionMachO::S_4BYTE_LITERALS,
460 if (IDVal == ".literal8")
461 return ParseDirectiveSectionSwitch("__TEXT", "__literal8",
462 MCSectionMachO::S_8BYTE_LITERALS,
464 if (IDVal == ".literal16")
465 return ParseDirectiveSectionSwitch("__TEXT","__literal16",
466 MCSectionMachO::S_16BYTE_LITERALS,
468 if (IDVal == ".constructor")
469 return ParseDirectiveSectionSwitch("__TEXT","__constructor");
470 if (IDVal == ".destructor")
471 return ParseDirectiveSectionSwitch("__TEXT","__destructor");
472 if (IDVal == ".fvmlib_init0")
473 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init0");
474 if (IDVal == ".fvmlib_init1")
475 return ParseDirectiveSectionSwitch("__TEXT","__fvmlib_init1");
477 // FIXME: The assembler manual claims that this has the self modify code
478 // flag, at least on x86-32, but that does not appear to be correct.
479 if (IDVal == ".symbol_stub")
480 return ParseDirectiveSectionSwitch("__TEXT","__symbol_stub",
481 MCSectionMachO::S_SYMBOL_STUBS |
482 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
483 // FIXME: Different on PPC and ARM.
485 // FIXME: PowerPC only?
486 if (IDVal == ".picsymbol_stub")
487 return ParseDirectiveSectionSwitch("__TEXT","__picsymbol_stub",
488 MCSectionMachO::S_SYMBOL_STUBS |
489 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
491 if (IDVal == ".data")
492 return ParseDirectiveSectionSwitch("__DATA", "__data");
493 if (IDVal == ".static_data")
494 return ParseDirectiveSectionSwitch("__DATA", "__static_data");
496 // FIXME: The section names of these two are misspelled in the assembler
498 if (IDVal == ".non_lazy_symbol_pointer")
499 return ParseDirectiveSectionSwitch("__DATA", "__nl_symbol_ptr",
500 MCSectionMachO::S_NON_LAZY_SYMBOL_POINTERS,
502 if (IDVal == ".lazy_symbol_pointer")
503 return ParseDirectiveSectionSwitch("__DATA", "__la_symbol_ptr",
504 MCSectionMachO::S_LAZY_SYMBOL_POINTERS,
507 if (IDVal == ".dyld")
508 return ParseDirectiveSectionSwitch("__DATA", "__dyld");
509 if (IDVal == ".mod_init_func")
510 return ParseDirectiveSectionSwitch("__DATA", "__mod_init_func",
511 MCSectionMachO::S_MOD_INIT_FUNC_POINTERS,
513 if (IDVal == ".mod_term_func")
514 return ParseDirectiveSectionSwitch("__DATA", "__mod_term_func",
515 MCSectionMachO::S_MOD_TERM_FUNC_POINTERS,
517 if (IDVal == ".const_data")
518 return ParseDirectiveSectionSwitch("__DATA", "__const");
521 if (IDVal == ".objc_class")
522 return ParseDirectiveSectionSwitch("__OBJC", "__class",
523 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
524 if (IDVal == ".objc_meta_class")
525 return ParseDirectiveSectionSwitch("__OBJC", "__meta_class",
526 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
527 if (IDVal == ".objc_cat_cls_meth")
528 return ParseDirectiveSectionSwitch("__OBJC", "__cat_cls_meth",
529 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
530 if (IDVal == ".objc_cat_inst_meth")
531 return ParseDirectiveSectionSwitch("__OBJC", "__cat_inst_meth",
532 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
533 if (IDVal == ".objc_protocol")
534 return ParseDirectiveSectionSwitch("__OBJC", "__protocol",
535 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
536 if (IDVal == ".objc_string_object")
537 return ParseDirectiveSectionSwitch("__OBJC", "__string_object",
538 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
539 if (IDVal == ".objc_cls_meth")
540 return ParseDirectiveSectionSwitch("__OBJC", "__cls_meth",
541 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
542 if (IDVal == ".objc_inst_meth")
543 return ParseDirectiveSectionSwitch("__OBJC", "__inst_meth",
544 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
545 if (IDVal == ".objc_cls_refs")
546 return ParseDirectiveSectionSwitch("__OBJC", "__cls_refs",
547 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
548 MCSectionMachO::S_LITERAL_POINTERS,
550 if (IDVal == ".objc_message_refs")
551 return ParseDirectiveSectionSwitch("__OBJC", "__message_refs",
552 MCSectionMachO::S_ATTR_NO_DEAD_STRIP |
553 MCSectionMachO::S_LITERAL_POINTERS,
555 if (IDVal == ".objc_symbols")
556 return ParseDirectiveSectionSwitch("__OBJC", "__symbols",
557 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
558 if (IDVal == ".objc_category")
559 return ParseDirectiveSectionSwitch("__OBJC", "__category",
560 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
561 if (IDVal == ".objc_class_vars")
562 return ParseDirectiveSectionSwitch("__OBJC", "__class_vars",
563 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
564 if (IDVal == ".objc_instance_vars")
565 return ParseDirectiveSectionSwitch("__OBJC", "__instance_vars",
566 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
567 if (IDVal == ".objc_module_info")
568 return ParseDirectiveSectionSwitch("__OBJC", "__module_info",
569 MCSectionMachO::S_ATTR_NO_DEAD_STRIP);
570 if (IDVal == ".objc_class_names")
571 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
572 MCSectionMachO::S_CSTRING_LITERALS);
573 if (IDVal == ".objc_meth_var_types")
574 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
575 MCSectionMachO::S_CSTRING_LITERALS);
576 if (IDVal == ".objc_meth_var_names")
577 return ParseDirectiveSectionSwitch("__TEXT", "__cstring",
578 MCSectionMachO::S_CSTRING_LITERALS);
579 if (IDVal == ".objc_selector_strs")
580 return ParseDirectiveSectionSwitch("__OBJC", "__selector_strs",
581 MCSectionMachO::S_CSTRING_LITERALS);
583 // Assembler features
585 return ParseDirectiveSet();
589 if (IDVal == ".ascii")
590 return ParseDirectiveAscii(false);
591 if (IDVal == ".asciz")
592 return ParseDirectiveAscii(true);
594 // FIXME: Target hooks for size? Also for "word", "hword".
595 if (IDVal == ".byte")
596 return ParseDirectiveValue(1);
597 if (IDVal == ".short" || IDVal == ".word")
598 return ParseDirectiveValue(2);
599 if (IDVal == ".long")
600 return ParseDirectiveValue(4);
601 if (IDVal == ".quad")
602 return ParseDirectiveValue(8);
604 // FIXME: Target hooks for IsPow2.
605 if (IDVal == ".align")
606 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
607 if (IDVal == ".align32")
608 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
609 if (IDVal == ".balign")
610 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
611 if (IDVal == ".balignw")
612 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
613 if (IDVal == ".balignl")
614 return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
615 if (IDVal == ".p2align")
616 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
617 if (IDVal == ".p2alignw")
618 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
619 if (IDVal == ".p2alignl")
620 return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
623 return ParseDirectiveOrg();
625 if (IDVal == ".fill")
626 return ParseDirectiveFill();
627 if (IDVal == ".space")
628 return ParseDirectiveSpace();
630 // Symbol attribute directives
632 if (IDVal == ".globl" || IDVal == ".global")
633 return ParseDirectiveSymbolAttribute(MCStreamer::Global);
634 if (IDVal == ".hidden")
635 return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
636 if (IDVal == ".indirect_symbol")
637 return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
638 if (IDVal == ".internal")
639 return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
640 if (IDVal == ".lazy_reference")
641 return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
642 if (IDVal == ".no_dead_strip")
643 return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
644 if (IDVal == ".private_extern")
645 return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
646 if (IDVal == ".protected")
647 return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
648 if (IDVal == ".reference")
649 return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
650 if (IDVal == ".weak")
651 return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
652 if (IDVal == ".weak_definition")
653 return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
654 if (IDVal == ".weak_reference")
655 return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
657 if (IDVal == ".comm")
658 return ParseDirectiveComm(/*IsLocal=*/false);
659 if (IDVal == ".lcomm")
660 return ParseDirectiveComm(/*IsLocal=*/true);
661 if (IDVal == ".zerofill")
662 return ParseDirectiveDarwinZerofill();
663 if (IDVal == ".desc")
664 return ParseDirectiveDarwinSymbolDesc();
665 if (IDVal == ".lsym")
666 return ParseDirectiveDarwinLsym();
668 if (IDVal == ".subsections_via_symbols")
669 return ParseDirectiveDarwinSubsectionsViaSymbols();
670 if (IDVal == ".abort")
671 return ParseDirectiveAbort();
672 if (IDVal == ".include")
673 return ParseDirectiveInclude();
674 if (IDVal == ".dump")
675 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
676 if (IDVal == ".load")
677 return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
679 // Debugging directives
681 if (IDVal == ".file")
682 return ParseDirectiveFile(IDLoc);
683 if (IDVal == ".line")
684 return ParseDirectiveLine(IDLoc);
686 return ParseDirectiveLoc(IDLoc);
688 Warning(IDLoc, "ignoring directive for now");
689 EatToEndOfStatement();
694 if (getTargetParser().ParseInstruction(IDVal, Inst))
697 if (Lexer.isNot(AsmToken::EndOfStatement))
698 return TokError("unexpected token in argument list");
700 // Eat the end of statement marker.
703 // Instruction is good, process it.
704 Out.EmitInstruction(Inst);
706 // Skip to end of line for now.
710 bool AsmParser::ParseAssignment(const StringRef &Name) {
711 // FIXME: Use better location, we should use proper tokens.
712 SMLoc EqualLoc = Lexer.getLoc();
715 SMLoc StartLoc = Lexer.getLoc();
716 if (ParseExpression(Value))
719 if (Lexer.isNot(AsmToken::EndOfStatement))
720 return TokError("unexpected token in assignment");
722 // Eat the end of statement marker.
725 // Diagnose assignment to a label.
727 // FIXME: Diagnostics. Note the location of the definition as a label.
728 // FIXME: Handle '.'.
729 // FIXME: Diagnose assignment to protected identifier (e.g., register name).
730 MCSymbol *Sym = CreateSymbol(Name);
731 if (!Sym->isUndefined() && !Sym->isAbsolute())
732 return Error(EqualLoc, "symbol has already been defined");
734 // Do the assignment.
735 Out.EmitAssignment(Sym, Value);
743 bool AsmParser::ParseIdentifier(StringRef &Res) {
744 if (Lexer.isNot(AsmToken::Identifier) &&
745 Lexer.isNot(AsmToken::String))
748 Res = Lexer.getTok().getIdentifier();
750 Lexer.Lex(); // Consume the identifier token.
755 /// ParseDirectiveSet:
756 /// ::= .set identifier ',' expression
757 bool AsmParser::ParseDirectiveSet() {
760 if (ParseIdentifier(Name))
761 return TokError("expected identifier after '.set' directive");
763 if (Lexer.isNot(AsmToken::Comma))
764 return TokError("unexpected token in '.set'");
767 return ParseAssignment(Name);
770 /// ParseDirectiveSection:
771 /// ::= .section identifier (',' identifier)*
772 /// FIXME: This should actually parse out the segment, section, attributes and
773 /// sizeof_stub fields.
774 bool AsmParser::ParseDirectiveDarwinSection() {
775 SMLoc Loc = Lexer.getLoc();
777 StringRef SectionName;
778 if (ParseIdentifier(SectionName))
779 return Error(Loc, "expected identifier after '.section' directive");
781 // Verify there is a following comma.
782 if (!Lexer.is(AsmToken::Comma))
783 return TokError("unexpected token in '.section' directive");
785 std::string SectionSpec = SectionName;
788 // Add all the tokens until the end of the line, ParseSectionSpecifier will
790 StringRef EOL = Lexer.LexUntilEndOfStatement();
791 SectionSpec.append(EOL.begin(), EOL.end());
794 if (Lexer.isNot(AsmToken::EndOfStatement))
795 return TokError("unexpected token in '.section' directive");
799 StringRef Segment, Section;
800 unsigned TAA, StubSize;
801 std::string ErrorStr =
802 MCSectionMachO::ParseSectionSpecifier(SectionSpec, Segment, Section,
805 if (!ErrorStr.empty())
806 return Error(Loc, ErrorStr.c_str());
808 // FIXME: Arch specific.
809 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
814 /// ParseDirectiveSectionSwitch -
815 bool AsmParser::ParseDirectiveSectionSwitch(const char *Segment,
817 unsigned TAA, unsigned Align,
819 if (Lexer.isNot(AsmToken::EndOfStatement))
820 return TokError("unexpected token in section switching directive");
823 // FIXME: Arch specific.
824 Out.SwitchSection(getMachOSection(Segment, Section, TAA, StubSize,
827 // Set the implicit alignment, if any.
829 // FIXME: This isn't really what 'as' does; I think it just uses the implicit
830 // alignment on the section (e.g., if one manually inserts bytes into the
831 // section, then just issueing the section switch directive will not realign
832 // the section. However, this is arguably more reasonable behavior, and there
833 // is no good reason for someone to intentionally emit incorrectly sized
834 // values into the implicitly aligned sections.
836 Out.EmitValueToAlignment(Align, 0, 1, 0);
841 bool AsmParser::ParseEscapedString(std::string &Data) {
842 assert(Lexer.is(AsmToken::String) && "Unexpected current token!");
845 StringRef Str = Lexer.getTok().getStringContents();
846 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
847 if (Str[i] != '\\') {
852 // Recognize escaped characters. Note that this escape semantics currently
853 // loosely follows Darwin 'as'. Notably, it doesn't support hex escapes.
856 return TokError("unexpected backslash at end of string");
858 // Recognize octal sequences.
859 if ((unsigned) (Str[i] - '0') <= 7) {
860 // Consume up to three octal characters.
861 unsigned Value = Str[i] - '0';
863 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
865 Value = Value * 8 + (Str[i] - '0');
867 if (i + 1 != e && ((unsigned) (Str[i + 1] - '0')) <= 7) {
869 Value = Value * 8 + (Str[i] - '0');
874 return TokError("invalid octal escape sequence (out of range)");
876 Data += (unsigned char) Value;
880 // Otherwise recognize individual escapes.
883 // Just reject invalid escape sequences for now.
884 return TokError("invalid escape sequence (unrecognized character)");
886 case 'b': Data += '\b'; break;
887 case 'f': Data += '\f'; break;
888 case 'n': Data += '\n'; break;
889 case 'r': Data += '\r'; break;
890 case 't': Data += '\t'; break;
891 case '"': Data += '"'; break;
892 case '\\': Data += '\\'; break;
899 /// ParseDirectiveAscii:
900 /// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
901 bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
902 if (Lexer.isNot(AsmToken::EndOfStatement)) {
904 if (Lexer.isNot(AsmToken::String))
905 return TokError("expected string in '.ascii' or '.asciz' directive");
908 if (ParseEscapedString(Data))
913 Out.EmitBytes(StringRef("\0", 1));
917 if (Lexer.is(AsmToken::EndOfStatement))
920 if (Lexer.isNot(AsmToken::Comma))
921 return TokError("unexpected token in '.ascii' or '.asciz' directive");
930 /// ParseDirectiveValue
931 /// ::= (.byte | .short | ... ) [ expression (, expression)* ]
932 bool AsmParser::ParseDirectiveValue(unsigned Size) {
933 if (Lexer.isNot(AsmToken::EndOfStatement)) {
936 SMLoc StartLoc = Lexer.getLoc();
937 if (ParseExpression(Value))
940 Out.EmitValue(Value, Size);
942 if (Lexer.is(AsmToken::EndOfStatement))
945 // FIXME: Improve diagnostic.
946 if (Lexer.isNot(AsmToken::Comma))
947 return TokError("unexpected token in directive");
956 /// ParseDirectiveSpace
957 /// ::= .space expression [ , expression ]
958 bool AsmParser::ParseDirectiveSpace() {
960 if (ParseAbsoluteExpression(NumBytes))
963 int64_t FillExpr = 0;
964 bool HasFillExpr = false;
965 if (Lexer.isNot(AsmToken::EndOfStatement)) {
966 if (Lexer.isNot(AsmToken::Comma))
967 return TokError("unexpected token in '.space' directive");
970 if (ParseAbsoluteExpression(FillExpr))
975 if (Lexer.isNot(AsmToken::EndOfStatement))
976 return TokError("unexpected token in '.space' directive");
982 return TokError("invalid number of bytes in '.space' directive");
984 // FIXME: Sometimes the fill expr is 'nop' if it isn't supplied, instead of 0.
985 for (uint64_t i = 0, e = NumBytes; i != e; ++i)
986 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), 1);
991 /// ParseDirectiveFill
992 /// ::= .fill expression , expression , expression
993 bool AsmParser::ParseDirectiveFill() {
995 if (ParseAbsoluteExpression(NumValues))
998 if (Lexer.isNot(AsmToken::Comma))
999 return TokError("unexpected token in '.fill' directive");
1003 if (ParseAbsoluteExpression(FillSize))
1006 if (Lexer.isNot(AsmToken::Comma))
1007 return TokError("unexpected token in '.fill' directive");
1011 if (ParseAbsoluteExpression(FillExpr))
1014 if (Lexer.isNot(AsmToken::EndOfStatement))
1015 return TokError("unexpected token in '.fill' directive");
1019 if (FillSize != 1 && FillSize != 2 && FillSize != 4 && FillSize != 8)
1020 return TokError("invalid '.fill' size, expected 1, 2, 4, or 8");
1022 for (uint64_t i = 0, e = NumValues; i != e; ++i)
1023 Out.EmitValue(MCConstantExpr::Create(FillExpr, getContext()), FillSize);
1028 /// ParseDirectiveOrg
1029 /// ::= .org expression [ , expression ]
1030 bool AsmParser::ParseDirectiveOrg() {
1031 const MCExpr *Offset;
1032 SMLoc StartLoc = Lexer.getLoc();
1033 if (ParseExpression(Offset))
1036 // Parse optional fill expression.
1037 int64_t FillExpr = 0;
1038 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1039 if (Lexer.isNot(AsmToken::Comma))
1040 return TokError("unexpected token in '.org' directive");
1043 if (ParseAbsoluteExpression(FillExpr))
1046 if (Lexer.isNot(AsmToken::EndOfStatement))
1047 return TokError("unexpected token in '.org' directive");
1052 // FIXME: Only limited forms of relocatable expressions are accepted here, it
1053 // has to be relative to the current section.
1054 Out.EmitValueToOffset(Offset, FillExpr);
1059 /// ParseDirectiveAlign
1060 /// ::= {.align, ...} expression [ , expression [ , expression ]]
1061 bool AsmParser::ParseDirectiveAlign(bool IsPow2, unsigned ValueSize) {
1062 SMLoc AlignmentLoc = Lexer.getLoc();
1064 if (ParseAbsoluteExpression(Alignment))
1068 bool HasFillExpr = false;
1069 int64_t FillExpr = 0;
1070 int64_t MaxBytesToFill = 0;
1071 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1072 if (Lexer.isNot(AsmToken::Comma))
1073 return TokError("unexpected token in directive");
1076 // The fill expression can be omitted while specifying a maximum number of
1077 // alignment bytes, e.g:
1079 if (Lexer.isNot(AsmToken::Comma)) {
1081 if (ParseAbsoluteExpression(FillExpr))
1085 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1086 if (Lexer.isNot(AsmToken::Comma))
1087 return TokError("unexpected token in directive");
1090 MaxBytesLoc = Lexer.getLoc();
1091 if (ParseAbsoluteExpression(MaxBytesToFill))
1094 if (Lexer.isNot(AsmToken::EndOfStatement))
1095 return TokError("unexpected token in directive");
1102 // FIXME: Sometimes fill with nop.
1106 // Compute alignment in bytes.
1108 // FIXME: Diagnose overflow.
1109 if (Alignment >= 32) {
1110 Error(AlignmentLoc, "invalid alignment value");
1114 Alignment = 1 << Alignment;
1117 // Diagnose non-sensical max bytes to align.
1118 if (MaxBytesLoc.isValid()) {
1119 if (MaxBytesToFill < 1) {
1120 Error(MaxBytesLoc, "alignment directive can never be satisfied in this "
1121 "many bytes, ignoring maximum bytes expression");
1125 if (MaxBytesToFill >= Alignment) {
1126 Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
1132 // FIXME: Target specific behavior about how the "extra" bytes are filled.
1133 Out.EmitValueToAlignment(Alignment, FillExpr, ValueSize, MaxBytesToFill);
1138 /// ParseDirectiveSymbolAttribute
1139 /// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
1140 bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
1141 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1145 if (ParseIdentifier(Name))
1146 return TokError("expected identifier in directive");
1148 MCSymbol *Sym = CreateSymbol(Name);
1150 Out.EmitSymbolAttribute(Sym, Attr);
1152 if (Lexer.is(AsmToken::EndOfStatement))
1155 if (Lexer.isNot(AsmToken::Comma))
1156 return TokError("unexpected token in directive");
1165 /// ParseDirectiveDarwinSymbolDesc
1166 /// ::= .desc identifier , expression
1167 bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
1169 if (ParseIdentifier(Name))
1170 return TokError("expected identifier in directive");
1172 // Handle the identifier as the key symbol.
1173 MCSymbol *Sym = CreateSymbol(Name);
1175 if (Lexer.isNot(AsmToken::Comma))
1176 return TokError("unexpected token in '.desc' directive");
1179 SMLoc DescLoc = Lexer.getLoc();
1181 if (ParseAbsoluteExpression(DescValue))
1184 if (Lexer.isNot(AsmToken::EndOfStatement))
1185 return TokError("unexpected token in '.desc' directive");
1189 // Set the n_desc field of this Symbol to this DescValue
1190 Out.EmitSymbolDesc(Sym, DescValue);
1195 /// ParseDirectiveComm
1196 /// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
1197 bool AsmParser::ParseDirectiveComm(bool IsLocal) {
1198 SMLoc IDLoc = Lexer.getLoc();
1200 if (ParseIdentifier(Name))
1201 return TokError("expected identifier in directive");
1203 // Handle the identifier as the key symbol.
1204 MCSymbol *Sym = CreateSymbol(Name);
1206 if (Lexer.isNot(AsmToken::Comma))
1207 return TokError("unexpected token in directive");
1211 SMLoc SizeLoc = Lexer.getLoc();
1212 if (ParseAbsoluteExpression(Size))
1215 int64_t Pow2Alignment = 0;
1216 SMLoc Pow2AlignmentLoc;
1217 if (Lexer.is(AsmToken::Comma)) {
1219 Pow2AlignmentLoc = Lexer.getLoc();
1220 if (ParseAbsoluteExpression(Pow2Alignment))
1224 if (Lexer.isNot(AsmToken::EndOfStatement))
1225 return TokError("unexpected token in '.comm' or '.lcomm' directive");
1229 // NOTE: a size of zero for a .comm should create a undefined symbol
1230 // but a size of .lcomm creates a bss symbol of size zero.
1232 return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
1233 "be less than zero");
1235 // NOTE: The alignment in the directive is a power of 2 value, the assember
1236 // may internally end up wanting an alignment in bytes.
1237 // FIXME: Diagnose overflow.
1238 if (Pow2Alignment < 0)
1239 return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
1240 "alignment, can't be less than zero");
1242 if (!Sym->isUndefined())
1243 return Error(IDLoc, "invalid symbol redefinition");
1245 // '.lcomm' is equivalent to '.zerofill'.
1246 // Create the Symbol as a common or local common with Size and Pow2Alignment
1248 Out.EmitZerofill(getMachOSection("__DATA", "__bss",
1249 MCSectionMachO::S_ZEROFILL, 0,
1251 Sym, Size, 1 << Pow2Alignment);
1255 Out.EmitCommonSymbol(Sym, Size, 1 << Pow2Alignment);
1259 /// ParseDirectiveDarwinZerofill
1260 /// ::= .zerofill segname , sectname [, identifier , size_expression [
1261 /// , align_expression ]]
1262 bool AsmParser::ParseDirectiveDarwinZerofill() {
1263 // FIXME: Handle quoted names here.
1265 if (Lexer.isNot(AsmToken::Identifier))
1266 return TokError("expected segment name after '.zerofill' directive");
1267 StringRef Segment = Lexer.getTok().getString();
1270 if (Lexer.isNot(AsmToken::Comma))
1271 return TokError("unexpected token in directive");
1274 if (Lexer.isNot(AsmToken::Identifier))
1275 return TokError("expected section name after comma in '.zerofill' "
1277 StringRef Section = Lexer.getTok().getString();
1280 // If this is the end of the line all that was wanted was to create the
1281 // the section but with no symbol.
1282 if (Lexer.is(AsmToken::EndOfStatement)) {
1283 // Create the zerofill section but no symbol
1284 Out.EmitZerofill(getMachOSection(Segment, Section,
1285 MCSectionMachO::S_ZEROFILL, 0,
1290 if (Lexer.isNot(AsmToken::Comma))
1291 return TokError("unexpected token in directive");
1294 if (Lexer.isNot(AsmToken::Identifier))
1295 return TokError("expected identifier in directive");
1297 // handle the identifier as the key symbol.
1298 SMLoc IDLoc = Lexer.getLoc();
1299 MCSymbol *Sym = CreateSymbol(Lexer.getTok().getString());
1302 if (Lexer.isNot(AsmToken::Comma))
1303 return TokError("unexpected token in directive");
1307 SMLoc SizeLoc = Lexer.getLoc();
1308 if (ParseAbsoluteExpression(Size))
1311 int64_t Pow2Alignment = 0;
1312 SMLoc Pow2AlignmentLoc;
1313 if (Lexer.is(AsmToken::Comma)) {
1315 Pow2AlignmentLoc = Lexer.getLoc();
1316 if (ParseAbsoluteExpression(Pow2Alignment))
1320 if (Lexer.isNot(AsmToken::EndOfStatement))
1321 return TokError("unexpected token in '.zerofill' directive");
1326 return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
1329 // NOTE: The alignment in the directive is a power of 2 value, the assember
1330 // may internally end up wanting an alignment in bytes.
1331 // FIXME: Diagnose overflow.
1332 if (Pow2Alignment < 0)
1333 return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
1334 "can't be less than zero");
1336 if (!Sym->isUndefined())
1337 return Error(IDLoc, "invalid symbol redefinition");
1339 // Create the zerofill Symbol with Size and Pow2Alignment
1341 // FIXME: Arch specific.
1342 Out.EmitZerofill(getMachOSection(Segment, Section,
1343 MCSectionMachO::S_ZEROFILL, 0,
1345 Sym, Size, 1 << Pow2Alignment);
1350 /// ParseDirectiveDarwinSubsectionsViaSymbols
1351 /// ::= .subsections_via_symbols
1352 bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
1353 if (Lexer.isNot(AsmToken::EndOfStatement))
1354 return TokError("unexpected token in '.subsections_via_symbols' directive");
1358 Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
1363 /// ParseDirectiveAbort
1364 /// ::= .abort [ "abort_string" ]
1365 bool AsmParser::ParseDirectiveAbort() {
1366 // FIXME: Use loc from directive.
1367 SMLoc Loc = Lexer.getLoc();
1370 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1371 if (Lexer.isNot(AsmToken::String))
1372 return TokError("expected string in '.abort' directive");
1374 Str = Lexer.getTok().getString();
1379 if (Lexer.isNot(AsmToken::EndOfStatement))
1380 return TokError("unexpected token in '.abort' directive");
1384 // FIXME: Handle here.
1386 Error(Loc, ".abort detected. Assembly stopping.");
1388 Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
1393 /// ParseDirectiveLsym
1394 /// ::= .lsym identifier , expression
1395 bool AsmParser::ParseDirectiveDarwinLsym() {
1397 if (ParseIdentifier(Name))
1398 return TokError("expected identifier in directive");
1400 // Handle the identifier as the key symbol.
1401 MCSymbol *Sym = CreateSymbol(Name);
1403 if (Lexer.isNot(AsmToken::Comma))
1404 return TokError("unexpected token in '.lsym' directive");
1407 const MCExpr *Value;
1408 SMLoc StartLoc = Lexer.getLoc();
1409 if (ParseExpression(Value))
1412 if (Lexer.isNot(AsmToken::EndOfStatement))
1413 return TokError("unexpected token in '.lsym' directive");
1417 // We don't currently support this directive.
1419 // FIXME: Diagnostic location!
1421 return TokError("directive '.lsym' is unsupported");
1424 /// ParseDirectiveInclude
1425 /// ::= .include "filename"
1426 bool AsmParser::ParseDirectiveInclude() {
1427 if (Lexer.isNot(AsmToken::String))
1428 return TokError("expected string in '.include' directive");
1430 std::string Filename = Lexer.getTok().getString();
1431 SMLoc IncludeLoc = Lexer.getLoc();
1434 if (Lexer.isNot(AsmToken::EndOfStatement))
1435 return TokError("unexpected token in '.include' directive");
1437 // Strip the quotes.
1438 Filename = Filename.substr(1, Filename.size()-2);
1440 // Attempt to switch the lexer to the included file before consuming the end
1441 // of statement to avoid losing it when we switch.
1442 if (Lexer.EnterIncludeFile(Filename)) {
1443 Lexer.PrintMessage(IncludeLoc,
1444 "Could not find include file '" + Filename + "'",
1452 /// ParseDirectiveDarwinDumpOrLoad
1453 /// ::= ( .dump | .load ) "filename"
1454 bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
1455 if (Lexer.isNot(AsmToken::String))
1456 return TokError("expected string in '.dump' or '.load' directive");
1460 if (Lexer.isNot(AsmToken::EndOfStatement))
1461 return TokError("unexpected token in '.dump' or '.load' directive");
1465 // FIXME: If/when .dump and .load are implemented they will be done in the
1466 // the assembly parser and not have any need for an MCStreamer API.
1468 Warning(IDLoc, "ignoring directive .dump for now");
1470 Warning(IDLoc, "ignoring directive .load for now");
1475 /// ParseDirectiveIf
1476 /// ::= .if expression
1477 bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
1478 // Consume the identifier that was the .if directive
1481 TheCondStack.push_back(TheCondState);
1482 TheCondState.TheCond = AsmCond::IfCond;
1483 if(TheCondState.Ignore) {
1484 EatToEndOfStatement();
1488 if (ParseAbsoluteExpression(ExprValue))
1491 if (Lexer.isNot(AsmToken::EndOfStatement))
1492 return TokError("unexpected token in '.if' directive");
1496 TheCondState.CondMet = ExprValue;
1497 TheCondState.Ignore = !TheCondState.CondMet;
1503 /// ParseDirectiveElseIf
1504 /// ::= .elseif expression
1505 bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
1506 if (TheCondState.TheCond != AsmCond::IfCond &&
1507 TheCondState.TheCond != AsmCond::ElseIfCond)
1508 Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
1510 TheCondState.TheCond = AsmCond::ElseIfCond;
1512 // Consume the identifier that was the .elseif directive
1515 bool LastIgnoreState = false;
1516 if (!TheCondStack.empty())
1517 LastIgnoreState = TheCondStack.back().Ignore;
1518 if (LastIgnoreState || TheCondState.CondMet) {
1519 TheCondState.Ignore = true;
1520 EatToEndOfStatement();
1524 if (ParseAbsoluteExpression(ExprValue))
1527 if (Lexer.isNot(AsmToken::EndOfStatement))
1528 return TokError("unexpected token in '.elseif' directive");
1531 TheCondState.CondMet = ExprValue;
1532 TheCondState.Ignore = !TheCondState.CondMet;
1538 /// ParseDirectiveElse
1540 bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
1541 // Consume the identifier that was the .else directive
1544 if (Lexer.isNot(AsmToken::EndOfStatement))
1545 return TokError("unexpected token in '.else' directive");
1549 if (TheCondState.TheCond != AsmCond::IfCond &&
1550 TheCondState.TheCond != AsmCond::ElseIfCond)
1551 Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
1553 TheCondState.TheCond = AsmCond::ElseCond;
1554 bool LastIgnoreState = false;
1555 if (!TheCondStack.empty())
1556 LastIgnoreState = TheCondStack.back().Ignore;
1557 if (LastIgnoreState || TheCondState.CondMet)
1558 TheCondState.Ignore = true;
1560 TheCondState.Ignore = false;
1565 /// ParseDirectiveEndIf
1567 bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
1568 // Consume the identifier that was the .endif directive
1571 if (Lexer.isNot(AsmToken::EndOfStatement))
1572 return TokError("unexpected token in '.endif' directive");
1576 if ((TheCondState.TheCond == AsmCond::NoCond) ||
1577 TheCondStack.empty())
1578 Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
1580 if (!TheCondStack.empty()) {
1581 TheCondState = TheCondStack.back();
1582 TheCondStack.pop_back();
1588 /// ParseDirectiveFile
1589 /// ::= .file [number] string
1590 bool AsmParser::ParseDirectiveFile(SMLoc DirectiveLoc) {
1591 // FIXME: I'm not sure what this is.
1592 int64_t FileNumber = -1;
1593 if (Lexer.is(AsmToken::Integer)) {
1594 FileNumber = Lexer.getTok().getIntVal();
1598 return TokError("file number less than one");
1601 if (Lexer.isNot(AsmToken::String))
1602 return TokError("unexpected token in '.file' directive");
1604 StringRef FileName = Lexer.getTok().getString();
1607 if (Lexer.isNot(AsmToken::EndOfStatement))
1608 return TokError("unexpected token in '.file' directive");
1610 // FIXME: Do something with the .file.
1615 /// ParseDirectiveLine
1616 /// ::= .line [number]
1617 bool AsmParser::ParseDirectiveLine(SMLoc DirectiveLoc) {
1618 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1619 if (Lexer.isNot(AsmToken::Integer))
1620 return TokError("unexpected token in '.line' directive");
1622 int64_t LineNumber = Lexer.getTok().getIntVal();
1626 // FIXME: Do something with the .line.
1629 if (Lexer.isNot(AsmToken::EndOfStatement))
1630 return TokError("unexpected token in '.file' directive");
1636 /// ParseDirectiveLoc
1637 /// ::= .loc number [number [number]]
1638 bool AsmParser::ParseDirectiveLoc(SMLoc DirectiveLoc) {
1639 if (Lexer.isNot(AsmToken::Integer))
1640 return TokError("unexpected token in '.loc' directive");
1642 // FIXME: What are these fields?
1643 int64_t FileNumber = Lexer.getTok().getIntVal();
1645 // FIXME: Validate file.
1648 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1649 if (Lexer.isNot(AsmToken::Integer))
1650 return TokError("unexpected token in '.loc' directive");
1652 int64_t Param2 = Lexer.getTok().getIntVal();
1656 if (Lexer.isNot(AsmToken::EndOfStatement)) {
1657 if (Lexer.isNot(AsmToken::Integer))
1658 return TokError("unexpected token in '.loc' directive");
1660 int64_t Param3 = Lexer.getTok().getIntVal();
1664 // FIXME: Do something with the .loc.
1668 if (Lexer.isNot(AsmToken::EndOfStatement))
1669 return TokError("unexpected token in '.file' directive");