Index: include/llvm/MC/MCContext.h =================================================================== --- include/llvm/MC/MCContext.h +++ include/llvm/MC/MCContext.h @@ -17,6 +17,7 @@ #include "llvm/ADT/StringMap.h" #include "llvm/ADT/Twine.h" #include "llvm/MC/MCDwarf.h" +#include "llvm/MC/MCParser/MCAsmParser.h" #include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/SectionKind.h" #include "llvm/Support/Allocator.h" @@ -302,6 +303,9 @@ /// Get the symbol for \p Name, or null. MCSymbol *lookupSymbol(const Twine &Name) const; + /// Set value for a symbol. + int setSymbolValue(MCAsmParser &Parser, std::string &I); + /// getSymbols - Get a reference for the symbol table for clients that /// want to, for example, iterate over all symbols. 'const' because we /// still want any modifications to the table itself to use the MCContext Index: lib/MC/MCContext.cpp =================================================================== --- lib/MC/MCContext.cpp +++ lib/MC/MCContext.cpp @@ -260,6 +260,23 @@ return Symbols.lookup(NameRef); } +int MCContext::setSymbolValue(MCAsmParser &Parser, std::string &I) { + auto Pair = StringRef(I).split('='); + if (Pair.second.empty()) { + errs() << "error: defsym must be of the form: sym=value: " << I << "\n"; + return 1; + } + int64_t Value; + if (Pair.second.getAsInteger(0, Value)) { + errs() << "error: Value is not an integer: " << Pair.second << "\n"; + return 1; + } + auto Symbol = getOrCreateSymbol(Pair.first); + Parser.getStreamer().EmitAssignment(Symbol, + MCConstantExpr::create(Value, *this)); + return 0; +} + //===----------------------------------------------------------------------===// // Section Management //===----------------------------------------------------------------------===// Index: tools/llvm-mc/llvm-mc.cpp =================================================================== --- tools/llvm-mc/llvm-mc.cpp +++ tools/llvm-mc/llvm-mc.cpp @@ -394,22 +394,9 @@ } 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; + for (auto &I: DefineSymbol) + if (Parser.getContext().setSymbolValue(Parser, 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; }