Commit 8eb0ee24 authored by Naman Dixit's avatar Naman Dixit

Simplified the interpreter

parent 55852ebc
...@@ -117,7 +117,14 @@ static llvm::codegen::RegisterCodeGenFlags CGF; ...@@ -117,7 +117,14 @@ static llvm::codegen::RegisterCodeGenFlags CGF;
static llvm::ExitOnError ExitOnErr; static llvm::ExitOnError ExitOnErr;
#pragma clang diagnostic pop #pragma clang diagnostic pop
int demo2 (int x);
int demo2 (int x) {
return x * 2;
}
int main(int argc, char *argv[], char * const *envp) { int main(int argc, char *argv[], char * const *envp) {
(void)envp;
llvm::InitLLVM X(argc, argv); llvm::InitLLVM X(argc, argv);
ExitOnErr.setBanner("Error: "); ExitOnErr.setBanner("Error: ");
...@@ -132,9 +139,10 @@ int main(int argc, char *argv[], char * const *envp) { ...@@ -132,9 +139,10 @@ int main(int argc, char *argv[], char * const *envp) {
std::string code_name = "test.c"; std::string code_name = "test.c";
std::string code = R"END( std::string code = R"END(
#include <stdio.h> #include <stdio.h>
int main (void) int demo (void)
{ {
printf("Hello, World!\n"); printf("Hello, World!\n");
// printf("demo2: %d\n", demo2(20));
return 42; return 42;
} }
)END"; )END";
...@@ -144,15 +152,10 @@ int main (void) ...@@ -144,15 +152,10 @@ int main (void)
args.push_back(code_name.c_str()); args.push_back(code_name.c_str());
// Prepare DiagnosticEngine // Prepare DiagnosticEngine
clang::DiagnosticOptions DiagOpts; clang::DiagnosticOptions *DiagOpts = new clang::DiagnosticOptions;
clang::TextDiagnosticPrinter *textDiagPrinter = clang::DiagnosticsEngine DiagnosticsEngine(new clang::DiagnosticIDs,
new clang::TextDiagnosticPrinter(llvm::errs(), DiagOpts,
&DiagOpts); new clang::TextDiagnosticPrinter(llvm::errs(), DiagOpts));
clang::IntrusiveRefCntPtr<clang::DiagnosticIDs> pDiagIDs;
clang::DiagnosticsEngine *pDiagnosticsEngine =
new clang::DiagnosticsEngine(pDiagIDs,
&DiagOpts,
textDiagPrinter);
// Initialize CompilerInvocation // Initialize CompilerInvocation
clang::CompilerInvocation *CI = new clang::CompilerInvocation(); clang::CompilerInvocation *CI = new clang::CompilerInvocation();
...@@ -170,14 +173,13 @@ int main (void) ...@@ -170,14 +173,13 @@ int main (void)
args.push_back("/usr/include"); args.push_back("/usr/include");
llvm::ArrayRef<const char*> args_array(args); llvm::ArrayRef<const char*> args_array(args);
clang::CompilerInvocation::CreateFromArgs(*CI, args_array, *pDiagnosticsEngine); clang::CompilerInvocation::CreateFromArgs(*CI, args_array, DiagnosticsEngine);
// Map code filename to a memoryBuffer // Map code filename to a memoryBuffer
llvm::StringRef code_data(code); llvm::StringRef code_data(code);
std::unique_ptr<llvm::MemoryBuffer> buffer = llvm::MemoryBuffer::getMemBufferCopy(code_data); std::unique_ptr<llvm::MemoryBuffer> buffer = llvm::MemoryBuffer::getMemBufferCopy(code_data);
CI->getPreprocessorOpts().addRemappedFile(code_name, buffer.get()); CI->getPreprocessorOpts().addRemappedFile(code_name, buffer.get());
// Create and initialize CompilerInstance // Create and initialize CompilerInstance
clang::CompilerInstance Clang; clang::CompilerInstance Clang;
std::shared_ptr<clang::CompilerInvocation> CI_shared_ptr(CI); std::shared_ptr<clang::CompilerInvocation> CI_shared_ptr(CI);
...@@ -187,7 +189,7 @@ int main (void) ...@@ -187,7 +189,7 @@ int main (void)
// Set target (I guess I can initialize only the BPF target, but I don't know how) // Set target (I guess I can initialize only the BPF target, but I don't know how)
const std::shared_ptr<clang::TargetOptions> targetOptions = std::make_shared<clang::TargetOptions>(); const std::shared_ptr<clang::TargetOptions> targetOptions = std::make_shared<clang::TargetOptions>();
targetOptions->Triple = std::string("bpf"); targetOptions->Triple = std::string("bpf");
clang::TargetInfo *pTargetInfo = clang::TargetInfo::CreateTargetInfo(*pDiagnosticsEngine,targetOptions); clang::TargetInfo *pTargetInfo = clang::TargetInfo::CreateTargetInfo(DiagnosticsEngine, targetOptions);
Clang.setTarget(pTargetInfo); Clang.setTarget(pTargetInfo);
// Create and execute action // Create and execute action
...@@ -195,6 +197,7 @@ int main (void) ...@@ -195,6 +197,7 @@ int main (void)
//clang::CodeGenAction *compilerAction = new clang::EmitAssemblyAction(); //clang::CodeGenAction *compilerAction = new clang::EmitAssemblyAction();
Clang.ExecuteAction(*compilerAction); Clang.ExecuteAction(*compilerAction);
buffer.release();
...@@ -238,22 +241,22 @@ int main (void) ...@@ -238,22 +241,22 @@ int main (void)
builder.setMCPU(llvm::codegen::getCPUStr()); builder.setMCPU(llvm::codegen::getCPUStr());
builder.setMAttrs(llvm::codegen::getFeatureList()); builder.setMAttrs(llvm::codegen::getFeatureList());
if (auto RM = llvm::codegen::getExplicitRelocModel()) { // if (auto RM = llvm::codegen::getExplicitRelocModel()) {
builder.setRelocationModel(RM.getValue()); // builder.setRelocationModel(RM.getValue());
} // }
if (auto CM = llvm::codegen::getExplicitCodeModel()) { // if (auto CM = llvm::codegen::getExplicitCodeModel()) {
builder.setCodeModel(CM.getValue()); // builder.setCodeModel(CM.getValue());
} // }
builder.setEngineKind(llvm::EngineKind::Interpreter); builder.setEngineKind(llvm::EngineKind::Interpreter);
builder.setOptLevel(llvm::CodeGenOpt::None); builder.setOptLevel(llvm::CodeGenOpt::None);
llvm::TargetOptions Options = llvm::codegen::InitTargetOptionsFromCodeGenFlags(llvm::Triple()); llvm::TargetOptions Options = llvm::codegen::InitTargetOptionsFromCodeGenFlags(llvm::Triple());
if (llvm::codegen::getFloatABIForCalls() != llvm::FloatABI::Default) { // if (llvm::codegen::getFloatABIForCalls() != llvm::FloatABI::Default) {
Options.FloatABIType = llvm::codegen::getFloatABIForCalls(); // Options.FloatABIType = llvm::codegen::getFloatABIForCalls();
} // }
builder.setTargetOptions(Options); builder.setTargetOptions(Options);
...@@ -270,10 +273,7 @@ int main (void) ...@@ -270,10 +273,7 @@ int main (void)
EE->DisableLazyCompilation(false); EE->DisableLazyCompilation(false);
if (llvm::StringRef(code_name).endswith(".bc")) llvm::Function *EntryFn = Mod->getFunction("demo");
code_name.erase(code_name.length() - 3);
llvm::Function *EntryFn = Mod->getFunction("main");
if (!EntryFn) { if (!EntryFn) {
llvm::WithColor::error(llvm::errs(), code_name.c_str()) llvm::WithColor::error(llvm::errs(), code_name.c_str())
<< '\'' << "main" << "\' function not found in module.\n"; << '\'' << "main" << "\' function not found in module.\n";
...@@ -282,35 +282,37 @@ int main (void) ...@@ -282,35 +282,37 @@ int main (void)
errno = 0; errno = 0;
int Result = -1; // llvm::FunctionCallee Exit = Mod->getOrInsertFunction("exit",
// llvm::Type::getVoidTy(Context),
llvm::FunctionCallee Exit = Mod->getOrInsertFunction("exit", // llvm::Type::getInt32Ty(Context));
llvm::Type::getVoidTy(Context),
llvm::Type::getInt32Ty(Context));
EE->runStaticConstructorsDestructors(false); EE->runStaticConstructorsDestructors(false);
(void)EE->getPointerToFunction(EntryFn); // (void)EE->getPointerToFunction(EntryFn);
// Run main. // Run main.
Result = EE->runFunctionAsMain(EntryFn, std::vector<std::string>(), envp); llvm::GenericValue Result = EE->runFunction(EntryFn, llvm::ArrayRef<llvm::GenericValue>());
uint64_t ResultValue = Result.IntVal.getZExtValue();
// Run static destructors. // Run static destructors.
EE->runStaticConstructorsDestructors(true); EE->runStaticConstructorsDestructors(true);
if (llvm::Function *ExitF = std::cout << "Returns: " << ResultValue << std::endl;
llvm::dyn_cast<llvm::Function>(Exit.getCallee()->stripPointerCasts())) {
if (ExitF->getFunctionType() == Exit.getFunctionType()) {
std::vector<llvm::GenericValue> Args; // if (llvm::Function *ExitF =
llvm::GenericValue ResultGV; // llvm::dyn_cast<llvm::Function>(Exit.getCallee()->stripPointerCasts())) {
ResultGV.IntVal = llvm::APInt(32, static_cast<uint64_t>(Result)); // if (ExitF->getFunctionType() == Exit.getFunctionType()) {
Args.push_back(ResultGV); // std::vector<llvm::GenericValue> Args;
EE->runFunction(ExitF, Args); // llvm::GenericValue ResultGV;
llvm::WithColor::error(llvm::errs(), code_name.c_str()) // ResultGV.IntVal = llvm::APInt(32, static_cast<uint64_t>(Result));
<< "exit(" << Result << ") returned!\n"; // Args.push_back(ResultGV);
abort(); // EE->runFunction(ExitF, Args);
} // llvm::WithColor::error(llvm::errs(), code_name.c_str())
llvm::WithColor::error(llvm::errs(), code_name.c_str()) << "exit defined with wrong prototype!\n"; // << "exit(" << Result << ") returned!\n";
abort(); // abort();
} // }
// llvm::WithColor::error(llvm::errs(), code_name.c_str()) << "exit defined with wrong prototype!\n";
return Result; // abort();
// }
return static_cast<int>(ResultValue);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment