//===----------------------------------------------------------------------===//
#include "llvm/MC/MCParser/MCAsmParserExtension.h"
+#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/Twine.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCParser/MCAsmLexer.h"
+#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCExpr.h"
-#include "llvm/Target/TargetAsmInfo.h"
-#include "llvm/Target/TargetAsmParser.h"
+#include "llvm/MC/MCTargetAsmParser.h"
#include "llvm/Support/COFF.h"
using namespace llvm;
AddDirectiveHandler<&COFFAsmParser::ParseDirectiveScl>(".scl");
AddDirectiveHandler<&COFFAsmParser::ParseDirectiveType>(".type");
AddDirectiveHandler<&COFFAsmParser::ParseDirectiveEndef>(".endef");
+ AddDirectiveHandler<&COFFAsmParser::ParseDirectiveSecRel32>(".secrel32");
// Win64 EH directives.
AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveStartProc>(
".seh_pushframe");
AddDirectiveHandler<&COFFAsmParser::ParseSEHDirectiveEndProlog>(
".seh_endprologue");
+ AddDirectiveHandler<&COFFAsmParser::ParseDirectiveSymbolAttribute>(".weak");
}
bool ParseSectionDirectiveText(StringRef, SMLoc) {
bool ParseDirectiveScl(StringRef, SMLoc);
bool ParseDirectiveType(StringRef, SMLoc);
bool ParseDirectiveEndef(StringRef, SMLoc);
+ bool ParseDirectiveSecRel32(StringRef, SMLoc);
// Win64 EH directives.
bool ParseSEHDirectiveStartProc(StringRef, SMLoc);
bool ParseAtUnwindOrAtExcept(bool &unwind, bool &except);
bool ParseSEHRegisterNumber(unsigned &RegNo);
+ bool ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc);
public:
COFFAsmParser() {}
};
} // end annonomous namespace.
+/// ParseDirectiveSymbolAttribute
+/// ::= { ".weak", ... } [ identifier ( , identifier )* ]
+bool COFFAsmParser::ParseDirectiveSymbolAttribute(StringRef Directive, SMLoc) {
+ MCSymbolAttr Attr = StringSwitch<MCSymbolAttr>(Directive)
+ .Case(".weak", MCSA_Weak)
+ .Default(MCSA_Invalid);
+ assert(Attr != MCSA_Invalid && "unexpected symbol attribute directive!");
+ if (getLexer().isNot(AsmToken::EndOfStatement)) {
+ for (;;) {
+ StringRef Name;
+
+ if (getParser().ParseIdentifier(Name))
+ return TokError("expected identifier in directive");
+
+ MCSymbol *Sym = getContext().GetOrCreateSymbol(Name);
+
+ getStreamer().EmitSymbolAttribute(Sym, Attr);
+
+ if (getLexer().is(AsmToken::EndOfStatement))
+ break;
+
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("unexpected token in directive");
+ Lex();
+ }
+ }
+
+ Lex();
+ return false;
+}
+
bool COFFAsmParser::ParseSectionSwitch(StringRef Section,
unsigned Characteristics,
SectionKind Kind) {
return false;
}
-bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc) {
- const MCExpr *e;
- const MCSymbolRefExpr *funcExpr;
- SMLoc startLoc = getLexer().getLoc();
- if (getParser().ParseExpression(e))
+bool COFFAsmParser::ParseDirectiveSecRel32(StringRef, SMLoc) {
+ StringRef SymbolID;
+ if (getParser().ParseIdentifier(SymbolID))
return true;
- if (!(funcExpr = dyn_cast<MCSymbolRefExpr>(e)))
- return Error(startLoc, "expected symbol");
+ if (getLexer().isNot(AsmToken::EndOfStatement))
+ return TokError("unexpected token in directive");
+
+ MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
+
+ Lex();
+ getStreamer().EmitCOFFSecRel32(Symbol);
+ return false;
+}
+
+bool COFFAsmParser::ParseSEHDirectiveStartProc(StringRef, SMLoc) {
+ StringRef SymbolID;
+ if (getParser().ParseIdentifier(SymbolID))
+ return true;
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in directive");
+ MCSymbol *Symbol = getContext().GetOrCreateSymbol(SymbolID);
+
Lex();
- getStreamer().EmitWin64EHStartProc(&funcExpr->getSymbol());
+ getStreamer().EmitWin64EHStartProc(Symbol);
return false;
}
}
bool COFFAsmParser::ParseSEHDirectiveHandler(StringRef, SMLoc) {
- const MCExpr *e;
- const MCSymbolRefExpr *funcExpr;
- SMLoc startLoc = getLexer().getLoc();
- if (getParser().ParseExpression(e))
+ StringRef SymbolID;
+ if (getParser().ParseIdentifier(SymbolID))
return true;
- if (!(funcExpr = dyn_cast<MCSymbolRefExpr>(e)))
- return Error(startLoc, "expected symbol");
-
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("you must specify one or both of @unwind or @except");
+ Lex();
bool unwind = false, except = false;
- startLoc = getLexer().getLoc();
- if (!ParseAtUnwindOrAtExcept(unwind, except))
- return Error(startLoc,"you must specify one or both of @unwind or @except");
+ if (ParseAtUnwindOrAtExcept(unwind, except))
+ return true;
if (getLexer().is(AsmToken::Comma)) {
Lex();
- if (!ParseAtUnwindOrAtExcept(unwind, except))
+ if (ParseAtUnwindOrAtExcept(unwind, except))
return true;
}
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in directive");
+ MCSymbol *handler = getContext().GetOrCreateSymbol(SymbolID);
+
Lex();
- getStreamer().EmitWin64EHHandler(&funcExpr->getSymbol(), unwind, except);
+ getStreamer().EmitWin64EHHandler(handler, unwind, except);
return false;
}
int64_t Off;
if (ParseSEHRegisterNumber(Reg))
return true;
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("you must specify a stack pointer offset");
+
+ Lex();
SMLoc startLoc = getLexer().getLoc();
if (getParser().ParseAbsoluteExpression(Off))
return true;
int64_t Off;
if (ParseSEHRegisterNumber(Reg))
return true;
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("you must specify an offset on the stack");
+
+ Lex();
SMLoc startLoc = getLexer().getLoc();
if (getParser().ParseAbsoluteExpression(Off))
return true;
int64_t Off;
if (ParseSEHRegisterNumber(Reg))
return true;
+ if (getLexer().isNot(AsmToken::Comma))
+ return TokError("you must specify an offset on the stack");
+
+ Lex();
SMLoc startLoc = getLexer().getLoc();
if (getParser().ParseAbsoluteExpression(Off))
return true;
}
bool COFFAsmParser::ParseSEHDirectivePushFrame(StringRef, SMLoc) {
- bool Code;
+ bool Code = false;
StringRef CodeID;
- SMLoc startLoc = getLexer().getLoc();
- if (!getParser().ParseIdentifier(CodeID)) {
- if (CodeID != "@code")
- return Error(startLoc, "expected @code");
- Code = true;
+ if (getLexer().is(AsmToken::At)) {
+ SMLoc startLoc = getLexer().getLoc();
+ Lex();
+ if (!getParser().ParseIdentifier(CodeID)) {
+ if (CodeID != "code")
+ return Error(startLoc, "expected @code");
+ Code = true;
+ }
}
if (getLexer().isNot(AsmToken::EndOfStatement))
bool COFFAsmParser::ParseAtUnwindOrAtExcept(bool &unwind, bool &except) {
StringRef identifier;
+ if (getLexer().isNot(AsmToken::At))
+ return TokError("a handler attribute must begin with '@'");
SMLoc startLoc = getLexer().getLoc();
- if (!getParser().ParseIdentifier(identifier))
+ Lex();
+ if (getParser().ParseIdentifier(identifier))
return Error(startLoc, "expected @unwind or @except");
- if (identifier == "@unwind")
+ if (identifier == "unwind")
unwind = true;
- else if (identifier == "@except")
+ else if (identifier == "except")
except = true;
else
return Error(startLoc, "expected @unwind or @except");
}
bool COFFAsmParser::ParseSEHRegisterNumber(unsigned &RegNo) {
- int64_t n;
SMLoc startLoc = getLexer().getLoc();
- if (getParser().ParseAbsoluteExpression(n)) {
- const TargetAsmInfo &asmInfo = getContext().getTargetAsmInfo();
+ if (getLexer().is(AsmToken::Percent)) {
+ const MCRegisterInfo &MRI = getContext().getRegisterInfo();
SMLoc endLoc;
unsigned LLVMRegNo;
if (getParser().getTargetParser().ParseRegister(LLVMRegNo,startLoc,endLoc))
- return Error(startLoc, "expected register or number");
+ return true;
+
+#if 0
+ // FIXME: TargetAsmInfo::getCalleeSavedRegs() commits a serious layering
+ // violation so this validation code is disabled.
// Check that this is a non-volatile register.
- const unsigned *NVRegs = asmInfo.getCalleeSavedRegs();
+ const unsigned *NVRegs = TAI.getCalleeSavedRegs();
unsigned i;
for (i = 0; NVRegs[i] != 0; ++i)
if (NVRegs[i] == LLVMRegNo)
break;
if (NVRegs[i] == 0)
return Error(startLoc, "expected non-volatile register");
+#endif
- int SEHRegNo = asmInfo.getSEHRegNum(LLVMRegNo);
+ int SEHRegNo = MRI.getSEHRegNum(LLVMRegNo);
if (SEHRegNo < 0)
return Error(startLoc,"register can't be represented in SEH unwind info");
RegNo = SEHRegNo;
}
- else
+ else {
+ int64_t n;
+ if (getParser().ParseAbsoluteExpression(n))
+ return true;
+ if (n > 15)
+ return Error(startLoc, "register number is too high");
RegNo = n;
+ }
- if (RegNo > 15)
- return Error(startLoc, "register number is too high");
return false;
}