[llvm] r239240 - 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.
Colin LeMahieu
colinl at codeaurora.org
Sat Jun 6 18:46:25 PDT 2015
Author: colinl
Date: Sat Jun 6 20:46:24 2015
New Revision: 239240
URL: http://llvm.org/viewvc/llvm-project?rev=239240&view=rev
Log:
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.
Added:
llvm/trunk/test/MC/AsmParser/defsym.s
llvm/trunk/test/MC/AsmParser/defsym_error1.s
llvm/trunk/test/MC/AsmParser/defsym_error2.s
Modified:
llvm/trunk/tools/llvm-mc/llvm-mc.cpp
Added: llvm/trunk/test/MC/AsmParser/defsym.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/defsym.s?rev=239240&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/defsym.s (added)
+++ llvm/trunk/test/MC/AsmParser/defsym.s Sat Jun 6 20:46:24 2015
@@ -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
\ No newline at end of file
Added: llvm/trunk/test/MC/AsmParser/defsym_error1.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/defsym_error1.s?rev=239240&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/defsym_error1.s (added)
+++ llvm/trunk/test/MC/AsmParser/defsym_error1.s Sat Jun 6 20:46:24 2015
@@ -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
Added: llvm/trunk/test/MC/AsmParser/defsym_error2.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/AsmParser/defsym_error2.s?rev=239240&view=auto
==============================================================================
--- llvm/trunk/test/MC/AsmParser/defsym_error2.s (added)
+++ llvm/trunk/test/MC/AsmParser/defsym_error2.s Sat Jun 6 20:46:24 2015
@@ -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
Modified: llvm/trunk/tools/llvm-mc/llvm-mc.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc/llvm-mc.cpp?rev=239240&r1=239239&r2=239240&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mc/llvm-mc.cpp (original)
+++ llvm/trunk/tools/llvm-mc/llvm-mc.cpp Sat Jun 6 20:46:24 2015
@@ -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,
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 *Pro
return 1;
}
+ int SymbolResult = fillCommandLineSymbols(*Parser);
+ if(SymbolResult)
+ return SymbolResult;
Parser->setShowParsedOperands(ShowInstOperands);
Parser->setTargetParser(*TAP);
More information about the llvm-commits
mailing list