blob: 6f4ba69927ce1c90b90199ad0a764c7499664b43 [file] [log] [blame]
Chris Lattnere26f6a82007-09-12 18:24:00 +00001//===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner45ca7c12007-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 Lattnere26f6a82007-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"
Chris Lattner371c1ef2009-08-23 07:49:08 +000035#include "llvm/Target/TargetSelect.h"
Chris Lattnere26f6a82007-09-12 18:24:00 +000036#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/ManagedStatic.h"
Chris Lattner371c1ef2009-08-23 07:49:08 +000038#include "llvm/Support/raw_ostream.h"
Chris Lattnere26f6a82007-09-12 18:24:00 +000039#include <iostream>
Chris Lattner371c1ef2009-08-23 07:49:08 +000040#include <fstream>
Chris Lattnere26f6a82007-09-12 18:24:00 +000041using namespace llvm;
42
43//Command line options
44
45static cl::opt<std::string>
46InputFilename(cl::Positional, cl::desc("<input brainf>"));
47
48static cl::opt<std::string>
49OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
50
51static cl::opt<bool>
52ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
53
54static cl::opt<bool>
55JIT("jit", cl::desc("Run program Just-In-Time"));
56
57
58//Add main function so can be fully compiled
59void addMainFunction(Module *mod) {
60 //define i32 @main(i32 %argc, i8 **%argv)
61 Function *main_func = cast<Function>(mod->
Owen Anderson35b47072009-08-13 21:58:54 +000062 getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
63 IntegerType::getInt32Ty(mod->getContext()),
Christopher Lambbb2f2222007-12-17 01:12:55 +000064 PointerType::getUnqual(PointerType::getUnqual(
Owen Anderson35b47072009-08-13 21:58:54 +000065 IntegerType::getInt8Ty(mod->getContext()))), NULL));
Chris Lattnere26f6a82007-09-12 18:24:00 +000066 {
67 Function::arg_iterator args = main_func->arg_begin();
68 Value *arg_0 = args++;
69 arg_0->setName("argc");
70 Value *arg_1 = args++;
71 arg_1->setName("argv");
72 }
73
74 //main.0:
Owen Anderson35b47072009-08-13 21:58:54 +000075 BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
Chris Lattnere26f6a82007-09-12 18:24:00 +000076
77 //call void @brainf()
78 {
Gabor Greifd6da1d02008-04-06 20:25:17 +000079 CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
80 "", bb);
Chris Lattnere26f6a82007-09-12 18:24:00 +000081 brainf_call->setTailCall(false);
82 }
83
84 //ret i32 0
Owen Anderson35b47072009-08-13 21:58:54 +000085 ReturnInst::Create(mod->getContext(),
86 ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
Chris Lattnere26f6a82007-09-12 18:24:00 +000087}
88
89int main(int argc, char **argv) {
90 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
91
Owen Andersone1f1f822009-07-16 18:04:31 +000092 LLVMContext &Context = getGlobalContext();
Owen Anderson25209b42009-07-01 16:58:40 +000093
Chris Lattnere26f6a82007-09-12 18:24:00 +000094 if (InputFilename == "") {
Chris Lattner371c1ef2009-08-23 07:49:08 +000095 errs() << "Error: You must specify the filename of the program to "
Chris Lattnerdf270d32008-08-23 22:00:15 +000096 "be compiled. Use --help to see the options.\n";
Chris Lattnere26f6a82007-09-12 18:24:00 +000097 abort();
98 }
99
100 //Get the output stream
Chris Lattner371c1ef2009-08-23 07:49:08 +0000101 raw_ostream *out = &outs();
Chris Lattnere26f6a82007-09-12 18:24:00 +0000102 if (!JIT) {
103 if (OutputFilename == "") {
104 std::string base = InputFilename;
Chris Lattner371c1ef2009-08-23 07:49:08 +0000105 if (InputFilename == "-") { base = "a"; }
Chris Lattnere26f6a82007-09-12 18:24:00 +0000106
Chris Lattner371c1ef2009-08-23 07:49:08 +0000107 // Use default filename.
108 OutputFilename = base+".bc";
Chris Lattnere26f6a82007-09-12 18:24:00 +0000109 }
110 if (OutputFilename != "-") {
Chris Lattner371c1ef2009-08-23 07:49:08 +0000111 std::string ErrInfo;
112 out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
Chris Lattner371c1ef2009-08-23 07:49:08 +0000113 raw_fd_ostream::F_Binary);
Chris Lattnere26f6a82007-09-12 18:24:00 +0000114 }
115 }
116
117 //Get the input stream
118 std::istream *in = &std::cin;
Chris Lattner371c1ef2009-08-23 07:49:08 +0000119 if (InputFilename != "-")
Chris Lattnere26f6a82007-09-12 18:24:00 +0000120 in = new std::ifstream(InputFilename.c_str());
Chris Lattnere26f6a82007-09-12 18:24:00 +0000121
122 //Gather the compile flags
123 BrainF::CompileFlags cf = BrainF::flag_off;
Chris Lattner371c1ef2009-08-23 07:49:08 +0000124 if (ArrayBoundsChecking)
Chris Lattnere26f6a82007-09-12 18:24:00 +0000125 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
Chris Lattnere26f6a82007-09-12 18:24:00 +0000126
127 //Read the BrainF program
128 BrainF bf;
Owen Andersona148fdd2009-07-01 21:22:36 +0000129 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
Chris Lattner371c1ef2009-08-23 07:49:08 +0000130 if (in != &std::cin)
131 delete in;
Chris Lattnere26f6a82007-09-12 18:24:00 +0000132 addMainFunction(mod);
133
134 //Verify generated code
135 if (verifyModule(*mod)) {
Chris Lattner371c1ef2009-08-23 07:49:08 +0000136 errs() << "Error: module failed verification. This shouldn't happen.\n";
Chris Lattnere26f6a82007-09-12 18:24:00 +0000137 abort();
138 }
139
140 //Write it out
141 if (JIT) {
Chris Lattner8ed24c52009-06-17 16:48:44 +0000142 InitializeNativeTarget();
143
Chris Lattner371c1ef2009-08-23 07:49:08 +0000144 outs() << "------- Running JIT -------\n";
Reid Klecknerfa3585b2009-07-18 00:42:18 +0000145 ExecutionEngine *ee = EngineBuilder(mod).create();
Chris Lattnere26f6a82007-09-12 18:24:00 +0000146 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
Chris Lattner371c1ef2009-08-23 07:49:08 +0000154 if (out != &outs())
155 delete out;
Chris Lattnere26f6a82007-09-12 18:24:00 +0000156 delete mod;
157
158 llvm_shutdown();
159
160 return 0;
161}