Teaching llvm-mc how to understand the defsym command line option. This allows integer-constant symbols to be defined on the command line and used during assembly.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239240 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Colin LeMahieu 2015-06-07 01:46:24 +00:00
parent 86a28d87b1
commit 4514db2bfb
4 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,20 @@
# RUN: llvm-mc -filetype=obj -triple=i386-unknown-elf -defsym a=7 -defsym b=11 %s | llvm-objdump -t - | FileCheck %s
.ifndef a
.err
.endif
.if a<>7
.err
.endif
.ifndef b
.err
.endif
.if b<>11
.err
.endif
# CHECK: 00000007 *ABS* 00000000 a
# CHECK: 0000000b *ABS* 00000000 b

View File

@ -0,0 +1,2 @@
# RUN: not llvm-mc -filetype=obj -triple=i386-unknown-elf -defsym aaoeuaoeu %s 2>&1 | FileCheck %s
# CHECK: defsym must be of the form: sym=value

View File

@ -0,0 +1,2 @@
# RUN: not llvm-mc -filetype=obj -triple=i386-unknown-elf -defsym a=a %s 2>&1 | FileCheck %s
# CHECK: error: Value is not an integer: a

View File

@ -70,6 +70,9 @@ static cl::opt<bool>
PrintImmHex("print-imm-hex", cl::init(false),
cl::desc("Prefer hex format for immediate values"));
static cl::list<std::string>
DefineSymbol("defsym", cl::desc("Defines a symbol to be an integer constant"));
enum OutputFileType {
OFT_Null,
OFT_AssemblyFile,
@ -316,6 +319,26 @@ static int AsLexInput(SourceMgr &SrcMgr, MCAsmInfo &MAI,
return Error;
}
static int fillCommandLineSymbols(MCAsmParser &Parser){
for(auto &I: DefineSymbol){
auto Pair = StringRef(I).split('=');
if(Pair.second.empty()){
errs() << "error: defsym must be of the form: sym=value: " << I;
return 1;
}
int64_t Value;
if(Pair.second.getAsInteger(0, Value)){
errs() << "error: Value is not an integer: " << Pair.second;
return 1;
}
auto &Context = Parser.getContext();
auto Symbol = Context.getOrCreateSymbol(Pair.first);
Parser.getStreamer().EmitAssignment(Symbol,
MCConstantExpr::create(Value, Context));
}
return 0;
}
static int AssembleInput(const char *ProgName, const Target *TheTarget,
SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str,
MCAsmInfo &MAI, MCSubtargetInfo &STI,
@ -331,6 +354,9 @@ static int AssembleInput(const char *ProgName, const Target *TheTarget,
return 1;
}
int SymbolResult = fillCommandLineSymbols(*Parser);
if(SymbolResult)
return SymbolResult;
Parser->setShowParsedOperands(ShowInstOperands);
Parser->setTargetParser(*TAP);