#include "BrainF.h"
#include "llvm/Constants.h"
#include "llvm/ModuleProvider.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include <fstream>
#include <iostream>
using namespace llvm;
static cl::opt<std::string>
InputFilename(cl::Positional, cl::desc("<input brainf>"));
static cl::opt<std::string>
OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
static cl::opt<bool>
ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
static cl::opt<bool>
JIT("jit", cl::desc("Run program Just-In-Time"));
void addMainFunction(Module *mod) {
Function *main_func = cast<Function>(mod->
getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty,
PointerType::getUnqual(PointerType::getUnqual(
IntegerType::Int8Ty)), NULL));
{
Function::arg_iterator args = main_func->arg_begin();
Value *arg_0 = args++;
arg_0->setName("argc");
Value *arg_1 = args++;
arg_1->setName("argv");
}
BasicBlock *bb = BasicBlock::Create("main.0", main_func);
{
CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
"", bb);
brainf_call->setTailCall(false);
}
ReturnInst::Create(ConstantInt::get(APInt(32, 0)), bb);
}
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
if (InputFilename == "") {
std::cerr<<"Error: You must specify the filename of the program to "
"be compiled. Use --help to see the options.\n";
abort();
}
std::ostream *out = &std::cout;
if (!JIT) {
if (OutputFilename == "") {
std::string base = InputFilename;
if (InputFilename == "-") {base = "a";}
const char *suffix = ".bc";
OutputFilename = base+suffix;
}
if (OutputFilename != "-") {
out = new std::
ofstream(OutputFilename.c_str(),
std::ios::out | std::ios::trunc | std::ios::binary);
}
}
std::istream *in = &std::cin;
if (InputFilename != "-") {
in = new std::ifstream(InputFilename.c_str());
}
BrainF::CompileFlags cf = BrainF::flag_off;
if (ArrayBoundsChecking) {
cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
}
BrainF bf;
Module *mod = bf.parse(in, 65536, cf); if (in != &std::cin) {delete in;}
addMainFunction(mod);
if (verifyModule(*mod)) {
std::cerr<<"Error: module failed verification. This shouldn't happen.\n";
abort();
}
if (JIT) {
std::cout << "------- Running JIT -------\n";
ExistingModuleProvider *mp = new ExistingModuleProvider(mod);
ExecutionEngine *ee = ExecutionEngine::create(mp, false);
std::vector<GenericValue> args;
Function *brainf_func = mod->getFunction("brainf");
GenericValue gv = ee->runFunction(brainf_func, args);
} else {
WriteBitcodeToFile(mod, *out);
}
if (out != &std::cout) {delete out;}
delete mod;
llvm_shutdown();
return 0;
}