#include "AsmParser.h"
#include "AsmExpr.h"
+#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetAsmParser.h"
using namespace llvm;
-bool AsmParser::Error(SMLoc L, const char *Msg) {
- Lexer.PrintMessage(L, Msg);
+void AsmParser::Warning(SMLoc L, const Twine &Msg) {
+ Lexer.PrintMessage(L, Msg.str(), "warning");
+}
+
+bool AsmParser::Error(SMLoc L, const Twine &Msg) {
+ Lexer.PrintMessage(L, Msg.str(), "error");
return true;
}
bool AsmParser::TokError(const char *Msg) {
- Lexer.PrintMessage(Lexer.getLoc(), Msg);
+ Lexer.PrintMessage(Lexer.getLoc(), Msg, "error");
return true;
}
// Prime the lexer.
Lexer.Lex();
- while (Lexer.isNot(asmtok::Eof))
- if (ParseStatement())
- return true;
+ bool HadError = false;
- return false;
+ AsmCond StartingCondState = TheCondState;
+
+ // While we have input, parse each statement.
+ while (Lexer.isNot(AsmToken::Eof)) {
+ // Handle conditional assembly here before calling ParseStatement()
+ if (Lexer.getKind() == AsmToken::Identifier) {
+ // If we have an identifier, handle it as the key symbol.
+ AsmToken ID = Lexer.getTok();
+ SMLoc IDLoc = ID.getLoc();
+ StringRef IDVal = ID.getString();
+
+ if (IDVal == ".if" ||
+ IDVal == ".elseif" ||
+ IDVal == ".else" ||
+ IDVal == ".endif") {
+ if (!ParseConditionalAssemblyDirectives(IDVal, IDLoc))
+ continue;
+ HadError = true;
+ EatToEndOfStatement();
+ continue;
+ }
+ }
+ if (TheCondState.Ignore) {
+ EatToEndOfStatement();
+ continue;
+ }
+
+ if (!ParseStatement()) continue;
+
+ // We had an error, remember it and recover by skipping to the next line.
+ HadError = true;
+ EatToEndOfStatement();
+ }
+
+ if (TheCondState.TheCond != StartingCondState.TheCond ||
+ TheCondState.Ignore != StartingCondState.Ignore)
+ return TokError("unmatched .ifs or .elses");
+
+ return HadError;
+}
+
+/// ParseConditionalAssemblyDirectives - parse the conditional assembly
+/// directives
+bool AsmParser::ParseConditionalAssemblyDirectives(StringRef Directive,
+ SMLoc DirectiveLoc) {
+ if (Directive == ".if")
+ return ParseDirectiveIf(DirectiveLoc);
+ if (Directive == ".elseif")
+ return ParseDirectiveElseIf(DirectiveLoc);
+ if (Directive == ".else")
+ return ParseDirectiveElse(DirectiveLoc);
+ if (Directive == ".endif")
+ return ParseDirectiveEndIf(DirectiveLoc);
+ return true;
}
/// EatToEndOfStatement - Throw away the rest of the line for testing purposes.
void AsmParser::EatToEndOfStatement() {
- while (Lexer.isNot(asmtok::EndOfStatement) &&
- Lexer.isNot(asmtok::Eof))
+ while (Lexer.isNot(AsmToken::EndOfStatement) &&
+ Lexer.isNot(AsmToken::Eof))
Lexer.Lex();
// Eat EOL.
- if (Lexer.is(asmtok::EndOfStatement))
+ if (Lexer.is(AsmToken::EndOfStatement))
Lexer.Lex();
}
///
bool AsmParser::ParseParenExpr(AsmExpr *&Res) {
if (ParseExpression(Res)) return true;
- if (Lexer.isNot(asmtok::RParen))
+ if (Lexer.isNot(AsmToken::RParen))
return TokError("expected ')' in parentheses expression");
Lexer.Lex();
return false;
switch (Lexer.getKind()) {
default:
return TokError("unknown token in expression");
- case asmtok::Exclaim:
+ case AsmToken::Exclaim:
Lexer.Lex(); // Eat the operator.
if (ParsePrimaryExpr(Res))
return true;
Res = new AsmUnaryExpr(AsmUnaryExpr::LNot, Res);
return false;
- case asmtok::Identifier: {
+ case AsmToken::String:
+ case AsmToken::Identifier: {
// This is a label, this should be parsed as part of an expression, to
// handle things like LFOO+4.
- MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getCurStrVal());
+ MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getIdentifier());
// If this is use of an undefined symbol then mark it external.
if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
Lexer.Lex(); // Eat identifier.
return false;
}
- case asmtok::IntVal:
- Res = new AsmConstantExpr(Lexer.getCurIntVal());
- Lexer.Lex(); // Eat identifier.
+ case AsmToken::Integer:
+ Res = new AsmConstantExpr(Lexer.getTok().getIntVal());
+ Lexer.Lex(); // Eat token.
return false;
- case asmtok::LParen:
+ case AsmToken::LParen:
Lexer.Lex(); // Eat the '('.
return ParseParenExpr(Res);
- case asmtok::Minus:
+ case AsmToken::Minus:
Lexer.Lex(); // Eat the operator.
if (ParsePrimaryExpr(Res))
return true;
Res = new AsmUnaryExpr(AsmUnaryExpr::Minus, Res);
return false;
- case asmtok::Plus:
+ case AsmToken::Plus:
Lexer.Lex(); // Eat the operator.
if (ParsePrimaryExpr(Res))
return true;
Res = new AsmUnaryExpr(AsmUnaryExpr::Plus, Res);
return false;
- case asmtok::Tilde:
+ case AsmToken::Tilde:
Lexer.Lex(); // Eat the operator.
if (ParsePrimaryExpr(Res))
return true;
bool AsmParser::ParseAbsoluteExpression(int64_t &Res) {
AsmExpr *Expr;
+ SMLoc StartLoc = Lexer.getLoc();
if (ParseExpression(Expr))
return true;
if (!Expr->EvaluateAsAbsolute(Ctx, Res))
- return TokError("expected absolute expression");
+ return Error(StartLoc, "expected absolute expression");
return false;
}
-static unsigned getBinOpPrecedence(asmtok::TokKind K,
+bool AsmParser::ParseRelocatableExpression(MCValue &Res) {
+ AsmExpr *Expr;
+
+ SMLoc StartLoc = Lexer.getLoc();
+ if (ParseExpression(Expr))
+ return true;
+
+ if (!Expr->EvaluateAsRelocatable(Ctx, Res))
+ return Error(StartLoc, "expected relocatable expression");
+
+ return false;
+}
+
+bool AsmParser::ParseParenRelocatableExpression(MCValue &Res) {
+ AsmExpr *Expr;
+
+ SMLoc StartLoc = Lexer.getLoc();
+ if (ParseParenExpr(Expr))
+ return true;
+
+ if (!Expr->EvaluateAsRelocatable(Ctx, Res))
+ return Error(StartLoc, "expected relocatable expression");
+
+ return false;
+}
+
+static unsigned getBinOpPrecedence(AsmToken::TokenKind K,
AsmBinaryExpr::Opcode &Kind) {
switch (K) {
default: return 0; // not a binop.
// Lowest Precedence: &&, ||
- case asmtok::AmpAmp:
+ case AsmToken::AmpAmp:
Kind = AsmBinaryExpr::LAnd;
return 1;
- case asmtok::PipePipe:
+ case AsmToken::PipePipe:
Kind = AsmBinaryExpr::LOr;
return 1;
// Low Precedence: +, -, ==, !=, <>, <, <=, >, >=
- case asmtok::Plus:
+ case AsmToken::Plus:
Kind = AsmBinaryExpr::Add;
return 2;
- case asmtok::Minus:
+ case AsmToken::Minus:
Kind = AsmBinaryExpr::Sub;
return 2;
- case asmtok::EqualEqual:
+ case AsmToken::EqualEqual:
Kind = AsmBinaryExpr::EQ;
return 2;
- case asmtok::ExclaimEqual:
- case asmtok::LessGreater:
+ case AsmToken::ExclaimEqual:
+ case AsmToken::LessGreater:
Kind = AsmBinaryExpr::NE;
return 2;
- case asmtok::Less:
+ case AsmToken::Less:
Kind = AsmBinaryExpr::LT;
return 2;
- case asmtok::LessEqual:
+ case AsmToken::LessEqual:
Kind = AsmBinaryExpr::LTE;
return 2;
- case asmtok::Greater:
+ case AsmToken::Greater:
Kind = AsmBinaryExpr::GT;
return 2;
- case asmtok::GreaterEqual:
+ case AsmToken::GreaterEqual:
Kind = AsmBinaryExpr::GTE;
return 2;
// Intermediate Precedence: |, &, ^
//
// FIXME: gas seems to support '!' as an infix operator?
- case asmtok::Pipe:
+ case AsmToken::Pipe:
Kind = AsmBinaryExpr::Or;
return 3;
- case asmtok::Caret:
+ case AsmToken::Caret:
Kind = AsmBinaryExpr::Xor;
return 3;
- case asmtok::Amp:
+ case AsmToken::Amp:
Kind = AsmBinaryExpr::And;
return 3;
// Highest Precedence: *, /, %, <<, >>
- case asmtok::Star:
+ case AsmToken::Star:
Kind = AsmBinaryExpr::Mul;
return 4;
- case asmtok::Slash:
+ case AsmToken::Slash:
Kind = AsmBinaryExpr::Div;
return 4;
- case asmtok::Percent:
+ case AsmToken::Percent:
Kind = AsmBinaryExpr::Mod;
return 4;
- case asmtok::LessLess:
+ case AsmToken::LessLess:
Kind = AsmBinaryExpr::Shl;
return 4;
- case asmtok::GreaterGreater:
+ case AsmToken::GreaterGreater:
Kind = AsmBinaryExpr::Shr;
return 4;
}
/// ::= Label* Directive ...Operands... EndOfStatement
/// ::= Label* Identifier OperandList* EndOfStatement
bool AsmParser::ParseStatement() {
- switch (Lexer.getKind()) {
- default:
- return TokError("unexpected token at start of statement");
- case asmtok::EndOfStatement:
+ if (Lexer.is(AsmToken::EndOfStatement)) {
Lexer.Lex();
return false;
- case asmtok::Identifier:
- break;
- // TODO: Recurse on local labels etc.
}
-
- // If we have an identifier, handle it as the key symbol.
- SMLoc IDLoc = Lexer.getLoc();
- const char *IDVal = Lexer.getCurStrVal();
-
- // Consume the identifier, see what is after it.
- switch (Lexer.Lex()) {
- case asmtok::Colon: {
+
+ // Statements always start with an identifier.
+ AsmToken ID = Lexer.getTok();
+ SMLoc IDLoc = ID.getLoc();
+ StringRef IDVal;
+ if (ParseIdentifier(IDVal))
+ return TokError("unexpected token at start of statement");
+
+ // FIXME: Recurse on local labels?
+
+ // See what kind of statement we have.
+ switch (Lexer.getKind()) {
+ case AsmToken::Colon: {
// identifier ':' -> Label.
Lexer.Lex();
return ParseStatement();
}
- case asmtok::Equal:
+ case AsmToken::Equal:
// identifier '=' ... -> assignment statement
Lexer.Lex();
// Otherwise, we have a normal instruction or directive.
if (IDVal[0] == '.') {
// FIXME: This should be driven based on a hash lookup and callback.
- if (!strcmp(IDVal, ".section"))
+ if (IDVal == ".section")
return ParseDirectiveDarwinSection();
- if (!strcmp(IDVal, ".text"))
+ if (IDVal == ".text")
// FIXME: This changes behavior based on the -static flag to the
// assembler.
return ParseDirectiveSectionSwitch("__TEXT,__text",
"regular,pure_instructions");
- if (!strcmp(IDVal, ".const"))
+ if (IDVal == ".const")
return ParseDirectiveSectionSwitch("__TEXT,__const");
- if (!strcmp(IDVal, ".static_const"))
+ if (IDVal == ".static_const")
return ParseDirectiveSectionSwitch("__TEXT,__static_const");
- if (!strcmp(IDVal, ".cstring"))
+ if (IDVal == ".cstring")
return ParseDirectiveSectionSwitch("__TEXT,__cstring",
"cstring_literals");
- if (!strcmp(IDVal, ".literal4"))
+ if (IDVal == ".literal4")
return ParseDirectiveSectionSwitch("__TEXT,__literal4", "4byte_literals");
- if (!strcmp(IDVal, ".literal8"))
+ if (IDVal == ".literal8")
return ParseDirectiveSectionSwitch("__TEXT,__literal8", "8byte_literals");
- if (!strcmp(IDVal, ".literal16"))
+ if (IDVal == ".literal16")
return ParseDirectiveSectionSwitch("__TEXT,__literal16",
"16byte_literals");
- if (!strcmp(IDVal, ".constructor"))
+ if (IDVal == ".constructor")
return ParseDirectiveSectionSwitch("__TEXT,__constructor");
- if (!strcmp(IDVal, ".destructor"))
+ if (IDVal == ".destructor")
return ParseDirectiveSectionSwitch("__TEXT,__destructor");
- if (!strcmp(IDVal, ".fvmlib_init0"))
+ if (IDVal == ".fvmlib_init0")
return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init0");
- if (!strcmp(IDVal, ".fvmlib_init1"))
+ if (IDVal == ".fvmlib_init1")
return ParseDirectiveSectionSwitch("__TEXT,__fvmlib_init1");
- if (!strcmp(IDVal, ".symbol_stub")) // FIXME: Different on PPC.
+ if (IDVal == ".symbol_stub") // FIXME: Different on PPC.
return ParseDirectiveSectionSwitch("__IMPORT,__jump_table,symbol_stubs",
"self_modifying_code+pure_instructions,5");
// FIXME: .picsymbol_stub on PPC.
- if (!strcmp(IDVal, ".data"))
+ if (IDVal == ".data")
return ParseDirectiveSectionSwitch("__DATA,__data");
- if (!strcmp(IDVal, ".static_data"))
+ if (IDVal == ".static_data")
return ParseDirectiveSectionSwitch("__DATA,__static_data");
- if (!strcmp(IDVal, ".non_lazy_symbol_pointer"))
+ if (IDVal == ".non_lazy_symbol_pointer")
return ParseDirectiveSectionSwitch("__DATA,__nl_symbol_pointer",
"non_lazy_symbol_pointers");
- if (!strcmp(IDVal, ".lazy_symbol_pointer"))
+ if (IDVal == ".lazy_symbol_pointer")
return ParseDirectiveSectionSwitch("__DATA,__la_symbol_pointer",
"lazy_symbol_pointers");
- if (!strcmp(IDVal, ".dyld"))
+ if (IDVal == ".dyld")
return ParseDirectiveSectionSwitch("__DATA,__dyld");
- if (!strcmp(IDVal, ".mod_init_func"))
+ if (IDVal == ".mod_init_func")
return ParseDirectiveSectionSwitch("__DATA,__mod_init_func",
"mod_init_funcs");
- if (!strcmp(IDVal, ".mod_term_func"))
+ if (IDVal == ".mod_term_func")
return ParseDirectiveSectionSwitch("__DATA,__mod_term_func",
"mod_term_funcs");
- if (!strcmp(IDVal, ".const_data"))
+ if (IDVal == ".const_data")
return ParseDirectiveSectionSwitch("__DATA,__const", "regular");
// FIXME: Verify attributes on sections.
- if (!strcmp(IDVal, ".objc_class"))
+ if (IDVal == ".objc_class")
return ParseDirectiveSectionSwitch("__OBJC,__class");
- if (!strcmp(IDVal, ".objc_meta_class"))
+ if (IDVal == ".objc_meta_class")
return ParseDirectiveSectionSwitch("__OBJC,__meta_class");
- if (!strcmp(IDVal, ".objc_cat_cls_meth"))
+ if (IDVal == ".objc_cat_cls_meth")
return ParseDirectiveSectionSwitch("__OBJC,__cat_cls_meth");
- if (!strcmp(IDVal, ".objc_cat_inst_meth"))
+ if (IDVal == ".objc_cat_inst_meth")
return ParseDirectiveSectionSwitch("__OBJC,__cat_inst_meth");
- if (!strcmp(IDVal, ".objc_protocol"))
+ if (IDVal == ".objc_protocol")
return ParseDirectiveSectionSwitch("__OBJC,__protocol");
- if (!strcmp(IDVal, ".objc_string_object"))
+ if (IDVal == ".objc_string_object")
return ParseDirectiveSectionSwitch("__OBJC,__string_object");
- if (!strcmp(IDVal, ".objc_cls_meth"))
+ if (IDVal == ".objc_cls_meth")
return ParseDirectiveSectionSwitch("__OBJC,__cls_meth");
- if (!strcmp(IDVal, ".objc_inst_meth"))
+ if (IDVal == ".objc_inst_meth")
return ParseDirectiveSectionSwitch("__OBJC,__inst_meth");
- if (!strcmp(IDVal, ".objc_cls_refs"))
+ if (IDVal == ".objc_cls_refs")
return ParseDirectiveSectionSwitch("__OBJC,__cls_refs");
- if (!strcmp(IDVal, ".objc_message_refs"))
+ if (IDVal == ".objc_message_refs")
return ParseDirectiveSectionSwitch("__OBJC,__message_refs");
- if (!strcmp(IDVal, ".objc_symbols"))
+ if (IDVal == ".objc_symbols")
return ParseDirectiveSectionSwitch("__OBJC,__symbols");
- if (!strcmp(IDVal, ".objc_category"))
+ if (IDVal == ".objc_category")
return ParseDirectiveSectionSwitch("__OBJC,__category");
- if (!strcmp(IDVal, ".objc_class_vars"))
+ if (IDVal == ".objc_class_vars")
return ParseDirectiveSectionSwitch("__OBJC,__class_vars");
- if (!strcmp(IDVal, ".objc_instance_vars"))
+ if (IDVal == ".objc_instance_vars")
return ParseDirectiveSectionSwitch("__OBJC,__instance_vars");
- if (!strcmp(IDVal, ".objc_module_info"))
+ if (IDVal == ".objc_module_info")
return ParseDirectiveSectionSwitch("__OBJC,__module_info");
- if (!strcmp(IDVal, ".objc_class_names"))
+ if (IDVal == ".objc_class_names")
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
- if (!strcmp(IDVal, ".objc_meth_var_types"))
+ if (IDVal == ".objc_meth_var_types")
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
- if (!strcmp(IDVal, ".objc_meth_var_names"))
+ if (IDVal == ".objc_meth_var_names")
return ParseDirectiveSectionSwitch("__TEXT,__cstring","cstring_literals");
- if (!strcmp(IDVal, ".objc_selector_strs"))
+ if (IDVal == ".objc_selector_strs")
return ParseDirectiveSectionSwitch("__OBJC,__selector_strs");
// Assembler features
- if (!strcmp(IDVal, ".set"))
+ if (IDVal == ".set")
return ParseDirectiveSet();
// Data directives
- if (!strcmp(IDVal, ".ascii"))
+ if (IDVal == ".ascii")
return ParseDirectiveAscii(false);
- if (!strcmp(IDVal, ".asciz"))
+ if (IDVal == ".asciz")
return ParseDirectiveAscii(true);
// FIXME: Target hooks for size? Also for "word", "hword".
- if (!strcmp(IDVal, ".byte"))
+ if (IDVal == ".byte")
return ParseDirectiveValue(1);
- if (!strcmp(IDVal, ".short"))
+ if (IDVal == ".short")
return ParseDirectiveValue(2);
- if (!strcmp(IDVal, ".long"))
+ if (IDVal == ".long")
return ParseDirectiveValue(4);
- if (!strcmp(IDVal, ".quad"))
+ if (IDVal == ".quad")
return ParseDirectiveValue(8);
// FIXME: Target hooks for IsPow2.
- if (!strcmp(IDVal, ".align"))
+ if (IDVal == ".align")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
- if (!strcmp(IDVal, ".align32"))
+ if (IDVal == ".align32")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
- if (!strcmp(IDVal, ".balign"))
+ if (IDVal == ".balign")
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/1);
- if (!strcmp(IDVal, ".balignw"))
+ if (IDVal == ".balignw")
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/2);
- if (!strcmp(IDVal, ".balignl"))
+ if (IDVal == ".balignl")
return ParseDirectiveAlign(/*IsPow2=*/false, /*ExprSize=*/4);
- if (!strcmp(IDVal, ".p2align"))
+ if (IDVal == ".p2align")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/1);
- if (!strcmp(IDVal, ".p2alignw"))
+ if (IDVal == ".p2alignw")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/2);
- if (!strcmp(IDVal, ".p2alignl"))
+ if (IDVal == ".p2alignl")
return ParseDirectiveAlign(/*IsPow2=*/true, /*ExprSize=*/4);
- if (!strcmp(IDVal, ".org"))
+ if (IDVal == ".org")
return ParseDirectiveOrg();
- if (!strcmp(IDVal, ".fill"))
+ if (IDVal == ".fill")
return ParseDirectiveFill();
- if (!strcmp(IDVal, ".space"))
+ if (IDVal == ".space")
return ParseDirectiveSpace();
- Lexer.PrintMessage(IDLoc, "warning: ignoring directive for now");
+ // Symbol attribute directives
+ if (IDVal == ".globl" || IDVal == ".global")
+ return ParseDirectiveSymbolAttribute(MCStreamer::Global);
+ if (IDVal == ".hidden")
+ return ParseDirectiveSymbolAttribute(MCStreamer::Hidden);
+ if (IDVal == ".indirect_symbol")
+ return ParseDirectiveSymbolAttribute(MCStreamer::IndirectSymbol);
+ if (IDVal == ".internal")
+ return ParseDirectiveSymbolAttribute(MCStreamer::Internal);
+ if (IDVal == ".lazy_reference")
+ return ParseDirectiveSymbolAttribute(MCStreamer::LazyReference);
+ if (IDVal == ".no_dead_strip")
+ return ParseDirectiveSymbolAttribute(MCStreamer::NoDeadStrip);
+ if (IDVal == ".private_extern")
+ return ParseDirectiveSymbolAttribute(MCStreamer::PrivateExtern);
+ if (IDVal == ".protected")
+ return ParseDirectiveSymbolAttribute(MCStreamer::Protected);
+ if (IDVal == ".reference")
+ return ParseDirectiveSymbolAttribute(MCStreamer::Reference);
+ if (IDVal == ".weak")
+ return ParseDirectiveSymbolAttribute(MCStreamer::Weak);
+ if (IDVal == ".weak_definition")
+ return ParseDirectiveSymbolAttribute(MCStreamer::WeakDefinition);
+ if (IDVal == ".weak_reference")
+ return ParseDirectiveSymbolAttribute(MCStreamer::WeakReference);
+
+ if (IDVal == ".comm")
+ return ParseDirectiveComm(/*IsLocal=*/false);
+ if (IDVal == ".lcomm")
+ return ParseDirectiveComm(/*IsLocal=*/true);
+ if (IDVal == ".zerofill")
+ return ParseDirectiveDarwinZerofill();
+ if (IDVal == ".desc")
+ return ParseDirectiveDarwinSymbolDesc();
+ if (IDVal == ".lsym")
+ return ParseDirectiveDarwinLsym();
+
+ if (IDVal == ".subsections_via_symbols")
+ return ParseDirectiveDarwinSubsectionsViaSymbols();
+ if (IDVal == ".abort")
+ return ParseDirectiveAbort();
+ if (IDVal == ".include")
+ return ParseDirectiveInclude();
+ if (IDVal == ".dump")
+ return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsDump=*/true);
+ if (IDVal == ".load")
+ return ParseDirectiveDarwinDumpOrLoad(IDLoc, /*IsLoad=*/false);
+
+ Warning(IDLoc, "ignoring directive for now");
EatToEndOfStatement();
return false;
}
MCInst Inst;
- if (ParseX86InstOperands(Inst))
+ if (getTargetParser().ParseInstruction(IDVal, Inst))
return true;
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in argument list");
// Eat the end of statement marker.
Lexer.Lex();
// Instruction is good, process it.
- outs() << "Found instruction: " << IDVal << " with " << Inst.getNumOperands()
- << " operands.\n";
+ Out.EmitInstruction(Inst);
// Skip to end of line for now.
return false;
}
-bool AsmParser::ParseAssignment(const char *Name, bool IsDotSet) {
+bool AsmParser::ParseAssignment(const StringRef &Name, bool IsDotSet) {
// FIXME: Use better location, we should use proper tokens.
SMLoc EqualLoc = Lexer.getLoc();
- int64_t Value;
- if (ParseAbsoluteExpression(Value))
+ MCValue Value;
+ if (ParseRelocatableExpression(Value))
return true;
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in assignment");
// Eat the end of statement marker.
return Error(EqualLoc, "invalid assignment to external symbol");
// Do the assignment.
- Out.EmitAssignment(Sym, MCValue::get(Value), IsDotSet);
+ Out.EmitAssignment(Sym, Value, IsDotSet);
+
+ return false;
+}
+
+/// ParseIdentifier:
+/// ::= identifier
+/// ::= string
+bool AsmParser::ParseIdentifier(StringRef &Res) {
+ if (Lexer.isNot(AsmToken::Identifier) &&
+ Lexer.isNot(AsmToken::String))
+ return true;
+
+ Res = Lexer.getTok().getIdentifier();
+
+ Lexer.Lex(); // Consume the identifier token.
return false;
}
/// ParseDirectiveSet:
/// ::= .set identifier ',' expression
bool AsmParser::ParseDirectiveSet() {
- if (Lexer.isNot(asmtok::Identifier))
- return TokError("expected identifier after '.set' directive");
+ StringRef Name;
- const char *Name = Lexer.getCurStrVal();
+ if (ParseIdentifier(Name))
+ return TokError("expected identifier after '.set' directive");
- if (Lexer.Lex() != asmtok::Comma)
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in '.set'");
Lexer.Lex();
/// FIXME: This should actually parse out the segment, section, attributes and
/// sizeof_stub fields.
bool AsmParser::ParseDirectiveDarwinSection() {
- if (Lexer.isNot(asmtok::Identifier))
+ StringRef SectionName;
+
+ if (ParseIdentifier(SectionName))
return TokError("expected identifier after '.section' directive");
- std::string Section = Lexer.getCurStrVal();
- Lexer.Lex();
-
+ std::string Section = SectionName;
+
+ // FIXME: This doesn't work, we lose quoting on things
+
// Accept a comma separated list of modifiers.
- while (Lexer.is(asmtok::Comma)) {
- Lexer.Lex();
-
- if (Lexer.isNot(asmtok::Identifier))
+ while (Lexer.is(AsmToken::Comma)) {
+ Lexer.Lex(); // Consume the comma.
+
+ StringRef ModifierName;
+ if (ParseIdentifier(ModifierName))
return TokError("expected identifier in '.section' directive");
Section += ',';
- Section += Lexer.getCurStrVal();
- Lexer.Lex();
+ Section += ModifierName;
}
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.section' directive");
Lexer.Lex();
- Out.SwitchSection(Ctx.GetSection(Section.c_str()));
+ // FIXME: Arch specific.
+ MCSection *S = Ctx.GetSection(Section);
+ if (S == 0)
+ S = MCSection::Create(Section, false, SectionKind(), Ctx);
+
+ Out.SwitchSection(S);
return false;
}
bool AsmParser::ParseDirectiveSectionSwitch(const char *Section,
const char *Directives) {
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in section switching directive");
Lexer.Lex();
SectionStr += Directives;
}
- Out.SwitchSection(Ctx.GetSection(Section));
+ // FIXME: Arch specific.
+ MCSection *S = Ctx.GetSection(Section);
+ if (S == 0)
+ S = MCSection::Create(Section, false, SectionKind(), Ctx);
+
+ Out.SwitchSection(S);
return false;
}
/// ParseDirectiveAscii:
/// ::= ( .ascii | .asciz ) [ "string" ( , "string" )* ]
bool AsmParser::ParseDirectiveAscii(bool ZeroTerminated) {
- if (Lexer.isNot(asmtok::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
for (;;) {
- if (Lexer.isNot(asmtok::String))
+ if (Lexer.isNot(AsmToken::String))
return TokError("expected string in '.ascii' or '.asciz' directive");
// FIXME: This shouldn't use a const char* + strlen, the string could have
// embedded nulls.
// FIXME: Should have accessor for getting string contents.
- const char *Str = Lexer.getCurStrVal();
- Out.EmitBytes(Str + 1, strlen(Str) - 2);
+ StringRef Str = Lexer.getTok().getString();
+ Out.EmitBytes(Str.substr(1, Str.size() - 2));
if (ZeroTerminated)
- Out.EmitBytes("\0", 1);
+ Out.EmitBytes(StringRef("\0", 1));
Lexer.Lex();
- if (Lexer.is(asmtok::EndOfStatement))
+ if (Lexer.is(AsmToken::EndOfStatement))
break;
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in '.ascii' or '.asciz' directive");
Lexer.Lex();
}
/// ParseDirectiveValue
/// ::= (.byte | .short | ... ) [ expression (, expression)* ]
bool AsmParser::ParseDirectiveValue(unsigned Size) {
- if (Lexer.isNot(asmtok::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
for (;;) {
- int64_t Expr;
- if (ParseAbsoluteExpression(Expr))
+ MCValue Expr;
+ if (ParseRelocatableExpression(Expr))
return true;
- Out.EmitValue(MCValue::get(Expr), Size);
+ Out.EmitValue(Expr, Size);
- if (Lexer.is(asmtok::EndOfStatement))
+ if (Lexer.is(AsmToken::EndOfStatement))
break;
// FIXME: Improve diagnostic.
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lexer.Lex();
}
int64_t FillExpr = 0;
bool HasFillExpr = false;
- if (Lexer.isNot(asmtok::EndOfStatement)) {
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in '.space' directive");
Lexer.Lex();
HasFillExpr = true;
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.space' directive");
}
if (ParseAbsoluteExpression(NumValues))
return true;
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in '.fill' directive");
Lexer.Lex();
if (ParseAbsoluteExpression(FillSize))
return true;
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in '.fill' directive");
Lexer.Lex();
if (ParseAbsoluteExpression(FillExpr))
return true;
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.fill' directive");
Lexer.Lex();
/// ParseDirectiveOrg
/// ::= .org expression [ , expression ]
bool AsmParser::ParseDirectiveOrg() {
- int64_t Offset;
- if (ParseAbsoluteExpression(Offset))
+ MCValue Offset;
+ if (ParseRelocatableExpression(Offset))
return true;
// Parse optional fill expression.
int64_t FillExpr = 0;
- if (Lexer.isNot(asmtok::EndOfStatement)) {
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in '.org' directive");
Lexer.Lex();
if (ParseAbsoluteExpression(FillExpr))
return true;
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in '.org' directive");
}
Lexer.Lex();
-
- Out.EmitValueToOffset(MCValue::get(Offset), FillExpr);
+
+ // FIXME: Only limited forms of relocatable expressions are accepted here, it
+ // has to be relative to the current section.
+ Out.EmitValueToOffset(Offset, FillExpr);
return false;
}
bool HasFillExpr = false;
int64_t FillExpr = 0;
int64_t MaxBytesToFill = 0;
- if (Lexer.isNot(asmtok::EndOfStatement)) {
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lexer.Lex();
// The fill expression can be omitted while specifying a maximum number of
// alignment bytes, e.g:
// .align 3,,4
- if (Lexer.isNot(asmtok::Comma)) {
+ if (Lexer.isNot(AsmToken::Comma)) {
HasFillExpr = true;
if (ParseAbsoluteExpression(FillExpr))
return true;
}
- if (Lexer.isNot(asmtok::EndOfStatement)) {
- if (Lexer.isNot(asmtok::Comma))
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
Lexer.Lex();
if (ParseAbsoluteExpression(MaxBytesToFill))
return true;
- if (Lexer.isNot(asmtok::EndOfStatement))
+ if (Lexer.isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in directive");
}
}
// Compute alignment in bytes.
if (IsPow2) {
// FIXME: Diagnose overflow.
- Alignment = 1 << Alignment;
+ Alignment = 1LL << Alignment;
}
// Diagnose non-sensical max bytes to fill.
if (MaxBytesLoc.isValid()) {
if (MaxBytesToFill < 1) {
- Lexer.PrintMessage(MaxBytesLoc, "warning: alignment directive can never "
- "be satisfied in this many bytes, ignoring");
+ Warning(MaxBytesLoc, "alignment directive can never be satisfied in this "
+ "many bytes, ignoring");
return false;
}
if (MaxBytesToFill >= Alignment) {
- Lexer.PrintMessage(MaxBytesLoc, "warning: maximum bytes expression "
- "exceeds alignment and has no effect");
+ Warning(MaxBytesLoc, "maximum bytes expression exceeds alignment and "
+ "has no effect");
MaxBytesToFill = 0;
}
}
return false;
}
+/// ParseDirectiveSymbolAttribute
+/// ::= { ".globl", ".weak", ... } [ identifier ( , identifier )* ]
+bool AsmParser::ParseDirectiveSymbolAttribute(MCStreamer::SymbolAttr Attr) {
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
+ for (;;) {
+ StringRef Name;
+
+ if (ParseIdentifier(Name))
+ return TokError("expected identifier in directive");
+
+ MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
+
+ // If this is use of an undefined symbol then mark it external.
+ if (!Sym->getSection() && !Ctx.GetSymbolValue(Sym))
+ Sym->setExternal(true);
+
+ Out.EmitSymbolAttribute(Sym, Attr);
+
+ if (Lexer.is(AsmToken::EndOfStatement))
+ break;
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in directive");
+ Lexer.Lex();
+ }
+ }
+
+ Lexer.Lex();
+ return false;
+}
+
+/// ParseDirectiveDarwinSymbolDesc
+/// ::= .desc identifier , expression
+bool AsmParser::ParseDirectiveDarwinSymbolDesc() {
+ StringRef Name;
+ if (ParseIdentifier(Name))
+ return TokError("expected identifier in directive");
+
+ // Handle the identifier as the key symbol.
+ MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in '.desc' directive");
+ Lexer.Lex();
+
+ SMLoc DescLoc = Lexer.getLoc();
+ int64_t DescValue;
+ if (ParseAbsoluteExpression(DescValue))
+ return true;
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.desc' directive");
+
+ Lexer.Lex();
+
+ // Set the n_desc field of this Symbol to this DescValue
+ Out.EmitSymbolDesc(Sym, DescValue);
+
+ return false;
+}
+
+/// ParseDirectiveComm
+/// ::= ( .comm | .lcomm ) identifier , size_expression [ , align_expression ]
+bool AsmParser::ParseDirectiveComm(bool IsLocal) {
+ SMLoc IDLoc = Lexer.getLoc();
+ StringRef Name;
+ if (ParseIdentifier(Name))
+ return TokError("expected identifier in directive");
+
+ // Handle the identifier as the key symbol.
+ MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in directive");
+ Lexer.Lex();
+
+ int64_t Size;
+ SMLoc SizeLoc = Lexer.getLoc();
+ if (ParseAbsoluteExpression(Size))
+ return true;
+
+ int64_t Pow2Alignment = 0;
+ SMLoc Pow2AlignmentLoc;
+ if (Lexer.is(AsmToken::Comma)) {
+ Lexer.Lex();
+ Pow2AlignmentLoc = Lexer.getLoc();
+ if (ParseAbsoluteExpression(Pow2Alignment))
+ return true;
+ }
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.comm' or '.lcomm' directive");
+
+ Lexer.Lex();
+
+ // NOTE: a size of zero for a .comm should create a undefined symbol
+ // but a size of .lcomm creates a bss symbol of size zero.
+ if (Size < 0)
+ return Error(SizeLoc, "invalid '.comm' or '.lcomm' directive size, can't "
+ "be less than zero");
+
+ // NOTE: The alignment in the directive is a power of 2 value, the assember
+ // may internally end up wanting an alignment in bytes.
+ // FIXME: Diagnose overflow.
+ if (Pow2Alignment < 0)
+ return Error(Pow2AlignmentLoc, "invalid '.comm' or '.lcomm' directive "
+ "alignment, can't be less than zero");
+
+ // TODO: Symbol must be undefined or it is a error to re-defined the symbol
+ if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
+ return Error(IDLoc, "invalid symbol redefinition");
+
+ // Create the Symbol as a common or local common with Size and Pow2Alignment
+ Out.EmitCommonSymbol(Sym, Size, Pow2Alignment, IsLocal);
+
+ return false;
+}
+
+/// ParseDirectiveDarwinZerofill
+/// ::= .zerofill segname , sectname [, identifier , size_expression [
+/// , align_expression ]]
+bool AsmParser::ParseDirectiveDarwinZerofill() {
+ // FIXME: Handle quoted names here.
+
+ if (Lexer.isNot(AsmToken::Identifier))
+ return TokError("expected segment name after '.zerofill' directive");
+ std::string Section = Lexer.getTok().getString();
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in directive");
+ Section += ',';
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::Identifier))
+ return TokError("expected section name after comma in '.zerofill' "
+ "directive");
+ Section += Lexer.getTok().getString().str();
+ Lexer.Lex();
+
+ // FIXME: we will need to tell GetSection() that this is to be created with or
+ // must have the Mach-O section type of S_ZEROFILL. Something like the code
+ // below could be done but for now it is not as EmitZerofill() does not know
+ // how to deal with a section type in the section name like
+ // ParseDirectiveDarwinSection() allows.
+ // Section += ',';
+ // Section += "zerofill";
+
+ // If this is the end of the line all that was wanted was to create the
+ // the section but with no symbol.
+ if (Lexer.is(AsmToken::EndOfStatement)) {
+ // FIXME: Arch specific.
+ MCSection *S = Ctx.GetSection(Section);
+ if (S == 0)
+ S = MCSection::Create(Section, false, SectionKind(), Ctx);
+
+ // Create the zerofill section but no symbol
+ Out.EmitZerofill(S);
+ return false;
+ }
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in directive");
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::Identifier))
+ return TokError("expected identifier in directive");
+
+ // handle the identifier as the key symbol.
+ SMLoc IDLoc = Lexer.getLoc();
+ MCSymbol *Sym = Ctx.GetOrCreateSymbol(Lexer.getTok().getString());
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in directive");
+ Lexer.Lex();
+
+ int64_t Size;
+ SMLoc SizeLoc = Lexer.getLoc();
+ if (ParseAbsoluteExpression(Size))
+ return true;
+
+ int64_t Pow2Alignment = 0;
+ SMLoc Pow2AlignmentLoc;
+ if (Lexer.is(AsmToken::Comma)) {
+ Lexer.Lex();
+ Pow2AlignmentLoc = Lexer.getLoc();
+ if (ParseAbsoluteExpression(Pow2Alignment))
+ return true;
+ }
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.zerofill' directive");
+
+ Lexer.Lex();
+
+ if (Size < 0)
+ return Error(SizeLoc, "invalid '.zerofill' directive size, can't be less "
+ "than zero");
+
+ // NOTE: The alignment in the directive is a power of 2 value, the assember
+ // may internally end up wanting an alignment in bytes.
+ // FIXME: Diagnose overflow.
+ if (Pow2Alignment < 0)
+ return Error(Pow2AlignmentLoc, "invalid '.zerofill' directive alignment, "
+ "can't be less than zero");
+
+ // TODO: Symbol must be undefined or it is a error to re-defined the symbol
+ if (Sym->getSection() || Ctx.GetSymbolValue(Sym))
+ return Error(IDLoc, "invalid symbol redefinition");
+
+ // FIXME: Arch specific.
+ MCSection *S = Ctx.GetSection(Section);
+ if (S == 0)
+ S = MCSection::Create(Section, false, SectionKind(), Ctx);
+
+ // Create the zerofill Symbol with Size and Pow2Alignment
+ Out.EmitZerofill(S, Sym, Size, Pow2Alignment);
+
+ return false;
+}
+
+/// ParseDirectiveDarwinSubsectionsViaSymbols
+/// ::= .subsections_via_symbols
+bool AsmParser::ParseDirectiveDarwinSubsectionsViaSymbols() {
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.subsections_via_symbols' directive");
+
+ Lexer.Lex();
+
+ Out.EmitAssemblerFlag(MCStreamer::SubsectionsViaSymbols);
+
+ return false;
+}
+
+/// ParseDirectiveAbort
+/// ::= .abort [ "abort_string" ]
+bool AsmParser::ParseDirectiveAbort() {
+ // FIXME: Use loc from directive.
+ SMLoc Loc = Lexer.getLoc();
+
+ StringRef Str = "";
+ if (Lexer.isNot(AsmToken::EndOfStatement)) {
+ if (Lexer.isNot(AsmToken::String))
+ return TokError("expected string in '.abort' directive");
+
+ Str = Lexer.getTok().getString();
+
+ Lexer.Lex();
+ }
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.abort' directive");
+
+ Lexer.Lex();
+
+ // FIXME: Handle here.
+ if (Str.empty())
+ Error(Loc, ".abort detected. Assembly stopping.");
+ else
+ Error(Loc, ".abort '" + Str + "' detected. Assembly stopping.");
+
+ return false;
+}
+
+/// ParseDirectiveLsym
+/// ::= .lsym identifier , expression
+bool AsmParser::ParseDirectiveDarwinLsym() {
+ StringRef Name;
+ if (ParseIdentifier(Name))
+ return TokError("expected identifier in directive");
+
+ // Handle the identifier as the key symbol.
+ MCSymbol *Sym = Ctx.GetOrCreateSymbol(Name);
+
+ if (Lexer.isNot(AsmToken::Comma))
+ return TokError("unexpected token in '.lsym' directive");
+ Lexer.Lex();
+
+ MCValue Expr;
+ if (ParseRelocatableExpression(Expr))
+ return true;
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.lsym' directive");
+
+ Lexer.Lex();
+
+ // Create the Sym with the value of the Expr
+ Out.EmitLocalSymbol(Sym, Expr);
+
+ return false;
+}
+
+/// ParseDirectiveInclude
+/// ::= .include "filename"
+bool AsmParser::ParseDirectiveInclude() {
+ if (Lexer.isNot(AsmToken::String))
+ return TokError("expected string in '.include' directive");
+
+ std::string Filename = Lexer.getTok().getString();
+ SMLoc IncludeLoc = Lexer.getLoc();
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.include' directive");
+
+ // Strip the quotes.
+ Filename = Filename.substr(1, Filename.size()-2);
+
+ // Attempt to switch the lexer to the included file before consuming the end
+ // of statement to avoid losing it when we switch.
+ if (Lexer.EnterIncludeFile(Filename)) {
+ Lexer.PrintMessage(IncludeLoc,
+ "Could not find include file '" + Filename + "'",
+ "error");
+ return true;
+ }
+
+ return false;
+}
+
+/// ParseDirectiveDarwinDumpOrLoad
+/// ::= ( .dump | .load ) "filename"
+bool AsmParser::ParseDirectiveDarwinDumpOrLoad(SMLoc IDLoc, bool IsDump) {
+ if (Lexer.isNot(AsmToken::String))
+ return TokError("expected string in '.dump' or '.load' directive");
+
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.dump' or '.load' directive");
+
+ Lexer.Lex();
+
+ // FIXME: If/when .dump and .load are implemented they will be done in the
+ // the assembly parser and not have any need for an MCStreamer API.
+ if (IsDump)
+ Warning(IDLoc, "ignoring directive .dump for now");
+ else
+ Warning(IDLoc, "ignoring directive .load for now");
+
+ return false;
+}
+
+/// ParseDirectiveIf
+/// ::= .if expression
+bool AsmParser::ParseDirectiveIf(SMLoc DirectiveLoc) {
+ // Consume the identifier that was the .if directive
+ Lexer.Lex();
+
+ TheCondStack.push_back(TheCondState);
+ TheCondState.TheCond = AsmCond::IfCond;
+ if(TheCondState.Ignore) {
+ EatToEndOfStatement();
+ }
+ else {
+ int64_t ExprValue;
+ if (ParseAbsoluteExpression(ExprValue))
+ return true;
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.if' directive");
+
+ Lexer.Lex();
+
+ TheCondState.CondMet = ExprValue;
+ TheCondState.Ignore = !TheCondState.CondMet;
+ }
+
+ return false;
+}
+
+/// ParseDirectiveElseIf
+/// ::= .elseif expression
+bool AsmParser::ParseDirectiveElseIf(SMLoc DirectiveLoc) {
+ if (TheCondState.TheCond != AsmCond::IfCond &&
+ TheCondState.TheCond != AsmCond::ElseIfCond)
+ Error(DirectiveLoc, "Encountered a .elseif that doesn't follow a .if or "
+ " an .elseif");
+ TheCondState.TheCond = AsmCond::ElseIfCond;
+
+ // Consume the identifier that was the .elseif directive
+ Lexer.Lex();
+
+ bool LastIgnoreState = false;
+ if (!TheCondStack.empty())
+ LastIgnoreState = TheCondStack.back().Ignore;
+ if (LastIgnoreState || TheCondState.CondMet) {
+ TheCondState.Ignore = true;
+ EatToEndOfStatement();
+ }
+ else {
+ int64_t ExprValue;
+ if (ParseAbsoluteExpression(ExprValue))
+ return true;
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.elseif' directive");
+
+ Lexer.Lex();
+ TheCondState.CondMet = ExprValue;
+ TheCondState.Ignore = !TheCondState.CondMet;
+ }
+
+ return false;
+}
+
+/// ParseDirectiveElse
+/// ::= .else
+bool AsmParser::ParseDirectiveElse(SMLoc DirectiveLoc) {
+ // Consume the identifier that was the .else directive
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.else' directive");
+
+ Lexer.Lex();
+
+ if (TheCondState.TheCond != AsmCond::IfCond &&
+ TheCondState.TheCond != AsmCond::ElseIfCond)
+ Error(DirectiveLoc, "Encountered a .else that doesn't follow a .if or an "
+ ".elseif");
+ TheCondState.TheCond = AsmCond::ElseCond;
+ bool LastIgnoreState = false;
+ if (!TheCondStack.empty())
+ LastIgnoreState = TheCondStack.back().Ignore;
+ if (LastIgnoreState || TheCondState.CondMet)
+ TheCondState.Ignore = true;
+ else
+ TheCondState.Ignore = false;
+
+ return false;
+}
+
+/// ParseDirectiveEndIf
+/// ::= .endif
+bool AsmParser::ParseDirectiveEndIf(SMLoc DirectiveLoc) {
+ // Consume the identifier that was the .endif directive
+ Lexer.Lex();
+
+ if (Lexer.isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in '.endif' directive");
+
+ Lexer.Lex();
+
+ if ((TheCondState.TheCond == AsmCond::NoCond) ||
+ TheCondStack.empty())
+ Error(DirectiveLoc, "Encountered a .endif that doesn't follow a .if or "
+ ".else");
+ if (!TheCondStack.empty()) {
+ TheCondState = TheCondStack.back();
+ TheCondStack.pop_back();
+ }
+
+ return false;
+}