blob: 4eaa4940e70abf91aaab211b59a6541261fc0c09 [file] [log] [blame]
Chris Lattnerbef8e0b2007-09-12 18:24:00 +00001//===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerfc001bb2007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerbef8e0b2007-09-12 18:24:00 +00007//
8//===--------------------------------------------------------------------===//
9//
10// This program converts the BrainF language into LLVM assembly,
11// which it can then run using the JIT or output as BitCode.
12//
13// This implementation has a tape of 65536 bytes,
14// with the head starting in the middle.
15// Range checking is off by default, so be careful.
16// It can be enabled with -abc.
17//
18// Use:
19// ./BrainF -jit prog.bf #Run program now
20// ./BrainF -jit -abc prog.bf #Run program now safely
21// ./BrainF prog.bf #Write as BitCode
22//
23// lli prog.bf.bc #Run generated BitCode
24// llvm-ld -native -o=prog prog.bf.bc #Compile BitCode into native executable
25//
26//===--------------------------------------------------------------------===//
27
28#include "BrainF.h"
29#include "llvm/Constants.h"
30#include "llvm/ModuleProvider.h"
31#include "llvm/Analysis/Verifier.h"
32#include "llvm/Bitcode/ReaderWriter.h"
33#include "llvm/ExecutionEngine/GenericValue.h"
34#include "llvm/ExecutionEngine/JIT.h"
35#include "llvm/Support/CommandLine.h"
36#include "llvm/Support/ManagedStatic.h"
Chris Lattnerda062882009-06-17 16:48:44 +000037#include "llvm/Target/TargetSelect.h"
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000038#include <fstream>
39#include <iostream>
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000040using namespace llvm;
41
42//Command line options
43
44static cl::opt<std::string>
45InputFilename(cl::Positional, cl::desc("<input brainf>"));
46
47static cl::opt<std::string>
48OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
49
50static cl::opt<bool>
51ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
52
53static cl::opt<bool>
54JIT("jit", cl::desc("Run program Just-In-Time"));
55
56
57//Add main function so can be fully compiled
58void addMainFunction(Module *mod) {
59 //define i32 @main(i32 %argc, i8 **%argv)
60 Function *main_func = cast<Function>(mod->
61 getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty,
Christopher Lamb43ad6b32007-12-17 01:12:55 +000062 PointerType::getUnqual(PointerType::getUnqual(
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000063 IntegerType::Int8Ty)), NULL));
64 {
65 Function::arg_iterator args = main_func->arg_begin();
66 Value *arg_0 = args++;
67 arg_0->setName("argc");
68 Value *arg_1 = args++;
69 arg_1->setName("argv");
70 }
71
72 //main.0:
Gabor Greif051a9502008-04-06 20:25:17 +000073 BasicBlock *bb = BasicBlock::Create("main.0", main_func);
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000074
75 //call void @brainf()
76 {
Gabor Greif051a9502008-04-06 20:25:17 +000077 CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
78 "", bb);
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000079 brainf_call->setTailCall(false);
80 }
81
82 //ret i32 0
Gabor Greif051a9502008-04-06 20:25:17 +000083 ReturnInst::Create(ConstantInt::get(APInt(32, 0)), bb);
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000084}
85
86int main(int argc, char **argv) {
87 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
88
Owen Anderson8b477ed2009-07-01 16:58:40 +000089 LLVMContext Context;
90
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000091 if (InputFilename == "") {
Chris Lattneref5dc362008-08-23 22:00:15 +000092 std::cerr<<"Error: You must specify the filename of the program to "
93 "be compiled. Use --help to see the options.\n";
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000094 abort();
95 }
96
97 //Get the output stream
98 std::ostream *out = &std::cout;
99 if (!JIT) {
100 if (OutputFilename == "") {
101 std::string base = InputFilename;
102 if (InputFilename == "-") {base = "a";}
103
104 //Use default filename
105 const char *suffix = ".bc";
106 OutputFilename = base+suffix;
107 }
108 if (OutputFilename != "-") {
109 out = new std::
110 ofstream(OutputFilename.c_str(),
111 std::ios::out | std::ios::trunc | std::ios::binary);
112 }
113 }
114
115 //Get the input stream
116 std::istream *in = &std::cin;
117 if (InputFilename != "-") {
118 in = new std::ifstream(InputFilename.c_str());
119 }
120
121 //Gather the compile flags
122 BrainF::CompileFlags cf = BrainF::flag_off;
123 if (ArrayBoundsChecking) {
124 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
125 }
126
127 //Read the BrainF program
128 BrainF bf;
Owen Anderson31895e72009-07-01 21:22:36 +0000129 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000130 if (in != &std::cin) {delete in;}
131 addMainFunction(mod);
132
133 //Verify generated code
134 if (verifyModule(*mod)) {
Chris Lattneref5dc362008-08-23 22:00:15 +0000135 std::cerr<<"Error: module failed verification. This shouldn't happen.\n";
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000136 abort();
137 }
138
139 //Write it out
140 if (JIT) {
Chris Lattnerda062882009-06-17 16:48:44 +0000141 InitializeNativeTarget();
142
Chris Lattneref5dc362008-08-23 22:00:15 +0000143 std::cout << "------- Running JIT -------\n";
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000144 ExistingModuleProvider *mp = new ExistingModuleProvider(mod);
145 ExecutionEngine *ee = ExecutionEngine::create(mp, false);
146 std::vector<GenericValue> args;
147 Function *brainf_func = mod->getFunction("brainf");
148 GenericValue gv = ee->runFunction(brainf_func, args);
149 } else {
150 WriteBitcodeToFile(mod, *out);
151 }
152
153 //Clean up
154 if (out != &std::cout) {delete out;}
155 delete mod;
156
157 llvm_shutdown();
158
159 return 0;
160}