X-Git-Url: http://plrg.eecs.uci.edu/git/?p=oota-llvm.git;a=blobdiff_plain;f=examples%2FKaleidoscope%2FOrc%2Finitial%2Ftoy.cpp;h=836c3a78a4540db54022b5f2f90faeff298c4af9;hp=8716f09915e8c06b7977ffe937a44789386d2ef2;hb=13f2d9943f045f89e616b857ff29b125cfbd34dd;hpb=eddb26303b1810f1b2e3d0dbc02687374525600a diff --git a/examples/Kaleidoscope/Orc/initial/toy.cpp b/examples/Kaleidoscope/Orc/initial/toy.cpp index 8716f09915e..836c3a78a45 100644 --- a/examples/Kaleidoscope/Orc/initial/toy.cpp +++ b/examples/Kaleidoscope/Orc/initial/toy.cpp @@ -1,3 +1,4 @@ + #include "llvm/Analysis/Passes.h" #include "llvm/ExecutionEngine/Orc/CompileUtils.h" #include "llvm/ExecutionEngine/Orc/IRCompileLayer.h" @@ -13,8 +14,10 @@ #include "llvm/Support/TargetSelect.h" #include "llvm/Transforms/Scalar.h" #include -#include +#include +#include #include +#include #include #include using namespace llvm; @@ -270,14 +273,14 @@ static int GetTokPrecedence() { } template -std::unique_ptr ErrorU(const char *Str) { - fprintf(stderr, "Error: %s\n", Str); +std::unique_ptr ErrorU(const std::string &Str) { + std::cerr << "Error: " << Str << "\n"; return nullptr; } template -T* ErrorP(const char *Str) { - fprintf(stderr, "Error: %s\n", Str); +T* ErrorP(const std::string &Str) { + std::cerr << "Error: " << Str << "\n"; return nullptr; } @@ -643,13 +646,11 @@ static std::unique_ptr ParseExtern() { //===----------------------------------------------------------------------===// // FIXME: Obviously we can do better than this -std::string GenerateUniqueName(const char *root) -{ +std::string GenerateUniqueName(const std::string &Root) { static int i = 0; - char s[16]; - sprintf(s, "%s%d", root, i++); - std::string S = s; - return S; + std::ostringstream NameStream; + NameStream << Root << ++i; + return NameStream.str(); } std::string MakeLegalFunctionName(std::string Name) @@ -669,10 +670,9 @@ std::string MakeLegalFunctionName(std::string Name) std::string legal_elements = "_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; size_t pos; while ((pos = NewName.find_first_not_of(legal_elements)) != std::string::npos) { - char old_c = NewName.at(pos); - char new_str[16]; - sprintf(new_str, "%d", (int)old_c); - NewName = NewName.replace(pos, 1, new_str); + std::ostringstream NumStream; + NumStream << (int)NewName.at(pos); + NewName = NewName.replace(pos, 1, NumStream.str()); } return NewName; @@ -699,7 +699,7 @@ PrototypeAST* SessionContext::getPrototypeAST(const std::string &Name) { if (I != Prototypes.end()) return I->second.get(); return nullptr; -} +} class IRGenContext { public: @@ -750,11 +750,8 @@ Value *VariableExprAST::IRGen(IRGenContext &C) { // Look this variable up in the function. Value *V = C.NamedValues[Name]; - if (V == 0) { - char ErrStr[256]; - sprintf(ErrStr, "Unknown variable name %s", Name.c_str()); - return ErrorP(ErrStr); - } + if (V == 0) + return ErrorP("Unknown variable name '" + Name + "'"); // Load the value. return C.getBuilder().CreateLoad(V, Name.c_str()); @@ -1152,7 +1149,7 @@ public: // new module. Create one that resolves symbols by looking back into the JIT. auto MM = createLookasideRTDyldMM( [&](const std::string &S) { - return getUnmangledSymbolAddress(S); + return getMangledSymbolAddress(S); }, [](const std::string &S) { return 0; } ); @@ -1161,7 +1158,7 @@ public: void removeModule(ModuleHandleT H) { CompileLayer.removeModuleSet(H); } - uint64_t getUnmangledSymbolAddress(const std::string &Name) { + uint64_t getMangledSymbolAddress(const std::string &Name) { return CompileLayer.getSymbolAddress(Name, false); } @@ -1171,7 +1168,7 @@ public: raw_string_ostream MangledNameStream(MangledName); Mang.getNameWithPrefix(MangledNameStream, Name); } - return getUnmangledSymbolAddress(MangledName); + return getMangledSymbolAddress(MangledName); } private: @@ -1188,7 +1185,7 @@ static void HandleDefinition(SessionContext &S, KaleidoscopeJIT &J) { IRGenContext C(S); if (auto LF = F->IRGen(C)) { #ifndef MINIMAL_STDERR_OUTPUT - fprintf(stderr, "Read function definition:"); + std::cerr << "Read function definition:\n"; LF->dump(); #endif J.addModule(C.takeM()); @@ -1215,7 +1212,7 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) { IRGenContext C(S); if (auto ExprFunc = F->IRGen(C)) { #ifndef MINIMAL_STDERR_OUTPUT - fprintf(stderr, "Expression function:\n"); + std::cerr << "Expression function:\n"; ExprFunc->dump(); #endif // Add the CodeGen'd module to the JIT. Keep a handle to it: We can remove @@ -1231,7 +1228,7 @@ static void HandleTopLevelExpression(SessionContext &S, KaleidoscopeJIT &J) { #ifdef MINIMAL_STDERR_OUTPUT FP(); #else - fprintf(stderr, "Evaluated to %f\n", FP()); + std::cerr << "Evaluated to " << FP() << "\n"; #endif // Remove the function. @@ -1250,7 +1247,7 @@ static void MainLoop() { while (1) { #ifndef MINIMAL_STDERR_OUTPUT - fprintf(stderr, "ready> "); + std::cerr << "ready> "; #endif switch (CurTok) { case tok_eof: return; @@ -1294,7 +1291,6 @@ int main() { InitializeNativeTarget(); InitializeNativeTargetAsmPrinter(); InitializeNativeTargetAsmParser(); - LLVMContext &Context = getGlobalContext(); // Install standard binary operators. // 1 is lowest precedence. @@ -1307,10 +1303,12 @@ int main() { // Prime the first token. #ifndef MINIMAL_STDERR_OUTPUT - fprintf(stderr, "ready> "); + std::cerr << "ready> "; #endif getNextToken(); + std::cerr << std::fixed; + // Run the main "interpreter loop" now. MainLoop();