2016-05-25 03:18:36 +02:00
|
|
|
//===-- BrainFDriver.cpp - BrainF compiler driver -------------------------===//
|
2007-09-12 20:24:00 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-09-12 20:24:00 +02:00
|
|
|
//
|
2016-05-25 03:18:36 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-12 20:24:00 +02:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
2016-05-25 03:18:36 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-12 20:24:00 +02:00
|
|
|
|
|
|
|
#include "BrainF.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/ADT/APInt.h"
|
2016-11-11 07:02:04 +01:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2014-09-03 00:28:02 +02:00
|
|
|
#include "llvm/ExecutionEngine/ExecutionEngine.h"
|
2007-09-12 20:24:00 +02:00
|
|
|
#include "llvm/ExecutionEngine/GenericValue.h"
|
2016-12-15 20:29:42 +01:00
|
|
|
#include "llvm/ExecutionEngine/MCJIT.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 12:56:33 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/IR/Value.h"
|
2014-01-13 10:58:03 +01:00
|
|
|
#include "llvm/IR/Verifier.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include "llvm/Support/Casting.h"
|
2007-09-12 20:24:00 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2014-04-30 01:37:02 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2007-09-12 20:24:00 +02:00
|
|
|
#include "llvm/Support/ManagedStatic.h"
|
2011-08-24 20:08:43 +02:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2009-08-23 09:49:08 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-05-25 03:18:36 +02:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdlib>
|
2009-08-23 09:49:08 +02:00
|
|
|
#include <fstream>
|
2012-12-04 11:16:57 +01:00
|
|
|
#include <iostream>
|
2016-05-25 03:18:36 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <vector>
|
|
|
|
|
2007-09-12 20:24:00 +02:00
|
|
|
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)
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 03:28:03 +01:00
|
|
|
FunctionType *main_func_fty = FunctionType::get(
|
|
|
|
Type::getInt32Ty(mod->getContext()),
|
|
|
|
{Type::getInt32Ty(mod->getContext()),
|
2019-02-01 04:23:42 +01:00
|
|
|
Type::getInt8Ty(mod->getContext())->getPointerTo()->getPointerTo()},
|
|
|
|
false);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 03:28:03 +01:00
|
|
|
Function *main_func =
|
2019-02-01 04:23:42 +01:00
|
|
|
Function::Create(main_func_fty, Function::ExternalLinkage, "main", mod);
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 03:28:03 +01:00
|
|
|
|
2007-09-12 20:24:00 +02:00
|
|
|
{
|
|
|
|
Function::arg_iterator args = main_func->arg_begin();
|
2015-11-07 01:55:46 +01:00
|
|
|
Value *arg_0 = &*args++;
|
2007-09-12 20:24:00 +02:00
|
|
|
arg_0->setName("argc");
|
2015-11-07 01:55:46 +01:00
|
|
|
Value *arg_1 = &*args++;
|
2007-09-12 20:24:00 +02:00
|
|
|
arg_1->setName("argv");
|
|
|
|
}
|
|
|
|
|
|
|
|
//main.0:
|
2009-08-13 23:58:54 +02:00
|
|
|
BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
|
2007-09-12 20:24:00 +02:00
|
|
|
|
|
|
|
//call void @brainf()
|
|
|
|
{
|
2008-04-06 22:25:17 +02:00
|
|
|
CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
|
|
|
|
"", bb);
|
2007-09-12 20:24:00 +02:00
|
|
|
brainf_call->setTailCall(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
//ret i32 0
|
2009-08-13 23:58:54 +02:00
|
|
|
ReturnInst::Create(mod->getContext(),
|
|
|
|
ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
|
2007-09-12 20:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
|
|
|
|
|
2016-04-14 23:59:01 +02:00
|
|
|
LLVMContext Context;
|
2009-07-01 18:58:40 +02:00
|
|
|
|
2007-09-12 20:24:00 +02:00
|
|
|
if (InputFilename == "") {
|
2009-08-23 09:49:08 +02:00
|
|
|
errs() << "Error: You must specify the filename of the program to "
|
2008-08-24 00:00:15 +02:00
|
|
|
"be compiled. Use --help to see the options.\n";
|
2007-09-12 20:24:00 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the output stream
|
2009-08-23 09:49:08 +02:00
|
|
|
raw_ostream *out = &outs();
|
2007-09-12 20:24:00 +02:00
|
|
|
if (!JIT) {
|
|
|
|
if (OutputFilename == "") {
|
|
|
|
std::string base = InputFilename;
|
2009-08-23 09:49:08 +02:00
|
|
|
if (InputFilename == "-") { base = "a"; }
|
2007-09-12 20:24:00 +02:00
|
|
|
|
2009-08-23 09:49:08 +02:00
|
|
|
// Use default filename.
|
|
|
|
OutputFilename = base+".bc";
|
2007-09-12 20:24:00 +02:00
|
|
|
}
|
|
|
|
if (OutputFilename != "-") {
|
2014-08-25 20:16:47 +02:00
|
|
|
std::error_code EC;
|
2019-08-05 07:43:48 +02:00
|
|
|
out = new raw_fd_ostream(OutputFilename, EC, sys::fs::OF_None);
|
2007-09-12 20:24:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get the input stream
|
|
|
|
std::istream *in = &std::cin;
|
2009-08-23 09:49:08 +02:00
|
|
|
if (InputFilename != "-")
|
2007-09-12 20:24:00 +02:00
|
|
|
in = new std::ifstream(InputFilename.c_str());
|
|
|
|
|
|
|
|
//Gather the compile flags
|
|
|
|
BrainF::CompileFlags cf = BrainF::flag_off;
|
2009-08-23 09:49:08 +02:00
|
|
|
if (ArrayBoundsChecking)
|
2007-09-12 20:24:00 +02:00
|
|
|
cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
|
|
|
|
|
|
|
|
//Read the BrainF program
|
|
|
|
BrainF bf;
|
2014-08-19 06:04:25 +02:00
|
|
|
std::unique_ptr<Module> Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
|
2009-08-23 09:49:08 +02:00
|
|
|
if (in != &std::cin)
|
|
|
|
delete in;
|
2014-08-19 06:04:25 +02:00
|
|
|
addMainFunction(Mod.get());
|
2007-09-12 20:24:00 +02:00
|
|
|
|
|
|
|
//Verify generated code
|
2014-08-19 06:04:25 +02:00
|
|
|
if (verifyModule(*Mod)) {
|
2009-08-23 09:49:08 +02:00
|
|
|
errs() << "Error: module failed verification. This shouldn't happen.\n";
|
2007-09-12 20:24:00 +02:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Write it out
|
|
|
|
if (JIT) {
|
2009-06-17 18:48:44 +02:00
|
|
|
InitializeNativeTarget();
|
2016-12-15 20:29:42 +01:00
|
|
|
InitializeNativeTargetAsmPrinter();
|
2009-06-17 18:48:44 +02:00
|
|
|
|
2009-08-23 09:49:08 +02:00
|
|
|
outs() << "------- Running JIT -------\n";
|
2014-08-19 06:04:25 +02:00
|
|
|
Module &M = *Mod;
|
|
|
|
ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
|
2016-12-15 20:29:42 +01:00
|
|
|
if (!ee) {
|
|
|
|
errs() << "Error: execution engine creation failed.\n";
|
|
|
|
abort();
|
|
|
|
}
|
2007-09-12 20:24:00 +02:00
|
|
|
std::vector<GenericValue> args;
|
2014-08-19 06:04:25 +02:00
|
|
|
Function *brainf_func = M.getFunction("brainf");
|
2007-09-12 20:24:00 +02:00
|
|
|
GenericValue gv = ee->runFunction(brainf_func, args);
|
2017-01-17 14:27:28 +01:00
|
|
|
// Genereated code calls putchar, and output is not guaranteed without fflush.
|
|
|
|
// The better place for fflush(stdout) call would be the generated code, but it
|
|
|
|
// is unmanageable because stdout linkage name depends on stdlib implementation.
|
|
|
|
fflush(stdout);
|
2007-09-12 20:24:00 +02:00
|
|
|
} else {
|
2018-02-14 20:23:27 +01:00
|
|
|
WriteBitcodeToFile(*Mod, *out);
|
2007-09-12 20:24:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//Clean up
|
2009-08-23 09:49:08 +02:00
|
|
|
if (out != &outs())
|
|
|
|
delete out;
|
2007-09-12 20:24:00 +02:00
|
|
|
|
|
|
|
llvm_shutdown();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|