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

Simplified the interpreter

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