llvm-6502/examples/BrainF/BrainFDriver.cpp
Jeffrey Yasskin f0356fe140 Kill ModuleProvider and ghost linkage by inverting the relationship between
Modules and ModuleProviders. Because the "ModuleProvider" simply materializes
GlobalValues now, and doesn't provide modules, it's renamed to
"GVMaterializer". Code that used to need a ModuleProvider to materialize
Functions can now materialize the Functions directly. Functions no longer use a
magic linkage to record that they're materializable; they simply ask the
GVMaterializer.

Because the C ABI must never change, we can't remove LLVMModuleProviderRef or
the functions that refer to it. Instead, because Module now exposes the same
functionality ModuleProvider used to, we store a Module* in any
LLVMModuleProviderRef and translate in the wrapper methods.  The bindings to
other languages still use the ModuleProvider concept.  It would probably be
worth some time to update them to follow the C++ more closely, but I don't
intend to do it.

Fixes http://llvm.org/PR5737 and http://llvm.org/PR5735.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@94686 91177308-0d34-0410-b5e6-96231b3b80d8
2010-01-27 20:34:15 +00:00

161 lines
4.7 KiB
C++

//===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===--------------------------------------------------------------------===//
//
// This program converts the BrainF language into LLVM assembly,
// which it can then run using the JIT or output as BitCode.
//
// This implementation has a tape of 65536 bytes,
// with the head starting in the middle.
// Range checking is off by default, so be careful.
// It can be enabled with -abc.
//
// Use:
// ./BrainF -jit prog.bf #Run program now
// ./BrainF -jit -abc prog.bf #Run program now safely
// ./BrainF prog.bf #Write as BitCode
//
// lli prog.bf.bc #Run generated BitCode
// llvm-ld -native -o=prog prog.bf.bc #Compile BitCode into native executable
//
//===--------------------------------------------------------------------===//
#include "BrainF.h"
#include "llvm/Constants.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/ExecutionEngine/JIT.h"
#include "llvm/Target/TargetSelect.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/raw_ostream.h"
#include <iostream>
#include <fstream>
using namespace llvm;
//Command line options
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"));
//Add main function so can be fully compiled
void addMainFunction(Module *mod) {
//define i32 @main(i32 %argc, i8 **%argv)
Function *main_func = cast<Function>(mod->
getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
IntegerType::getInt32Ty(mod->getContext()),
PointerType::getUnqual(PointerType::getUnqual(
IntegerType::getInt8Ty(mod->getContext()))), 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");
}
//main.0:
BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
//call void @brainf()
{
CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
"", bb);
brainf_call->setTailCall(false);
}
//ret i32 0
ReturnInst::Create(mod->getContext(),
ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
}
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
LLVMContext &Context = getGlobalContext();
if (InputFilename == "") {
errs() << "Error: You must specify the filename of the program to "
"be compiled. Use --help to see the options.\n";
abort();
}
//Get the output stream
raw_ostream *out = &outs();
if (!JIT) {
if (OutputFilename == "") {
std::string base = InputFilename;
if (InputFilename == "-") { base = "a"; }
// Use default filename.
OutputFilename = base+".bc";
}
if (OutputFilename != "-") {
std::string ErrInfo;
out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
raw_fd_ostream::F_Binary);
}
}
//Get the input stream
std::istream *in = &std::cin;
if (InputFilename != "-")
in = new std::ifstream(InputFilename.c_str());
//Gather the compile flags
BrainF::CompileFlags cf = BrainF::flag_off;
if (ArrayBoundsChecking)
cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
//Read the BrainF program
BrainF bf;
Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
if (in != &std::cin)
delete in;
addMainFunction(mod);
//Verify generated code
if (verifyModule(*mod)) {
errs() << "Error: module failed verification. This shouldn't happen.\n";
abort();
}
//Write it out
if (JIT) {
InitializeNativeTarget();
outs() << "------- Running JIT -------\n";
ExecutionEngine *ee = EngineBuilder(mod).create();
std::vector<GenericValue> args;
Function *brainf_func = mod->getFunction("brainf");
GenericValue gv = ee->runFunction(brainf_func, args);
} else {
WriteBitcodeToFile(mod, *out);
}
//Clean up
if (out != &outs())
delete out;
delete mod;
llvm_shutdown();
return 0;
}