blob: cd6eabfdffaa5e07135301ee6e6d8cda48cb195c [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
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000024//
25//===--------------------------------------------------------------------===//
26
27#include "BrainF.h"
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000028#include "llvm/Analysis/Verifier.h"
29#include "llvm/Bitcode/ReaderWriter.h"
30#include "llvm/ExecutionEngine/GenericValue.h"
31#include "llvm/ExecutionEngine/JIT.h"
Chandler Carruth0a084602013-01-02 11:56:33 +000032#include "llvm/IR/Constants.h"
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000033#include "llvm/Support/CommandLine.h"
34#include "llvm/Support/ManagedStatic.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000035#include "llvm/Support/TargetSelect.h"
Chris Lattnerb515d752009-08-23 07:49:08 +000036#include "llvm/Support/raw_ostream.h"
Chris Lattnerb515d752009-08-23 07:49:08 +000037#include <fstream>
Chandler Carruth4ca7e092012-12-04 10:16:57 +000038#include <iostream>
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000039using namespace llvm;
40
41//Command line options
42
43static cl::opt<std::string>
44InputFilename(cl::Positional, cl::desc("<input brainf>"));
45
46static cl::opt<std::string>
47OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
48
49static cl::opt<bool>
50ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
51
52static cl::opt<bool>
53JIT("jit", cl::desc("Run program Just-In-Time"));
54
55
56//Add main function so can be fully compiled
57void addMainFunction(Module *mod) {
58 //define i32 @main(i32 %argc, i8 **%argv)
59 Function *main_func = cast<Function>(mod->
Owen Anderson1d0be152009-08-13 21:58:54 +000060 getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
61 IntegerType::getInt32Ty(mod->getContext()),
Christopher Lamb43ad6b32007-12-17 01:12:55 +000062 PointerType::getUnqual(PointerType::getUnqual(
Owen Anderson1d0be152009-08-13 21:58:54 +000063 IntegerType::getInt8Ty(mod->getContext()))), NULL));
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000064 {
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:
Owen Anderson1d0be152009-08-13 21:58:54 +000073 BasicBlock *bb = BasicBlock::Create(mod->getContext(), "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
Owen Anderson1d0be152009-08-13 21:58:54 +000083 ReturnInst::Create(mod->getContext(),
84 ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000085}
86
87int main(int argc, char **argv) {
88 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
89
Owen Anderson001dbfe2009-07-16 18:04:31 +000090 LLVMContext &Context = getGlobalContext();
Owen Anderson8b477ed2009-07-01 16:58:40 +000091
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000092 if (InputFilename == "") {
Chris Lattnerb515d752009-08-23 07:49:08 +000093 errs() << "Error: You must specify the filename of the program to "
Chris Lattneref5dc362008-08-23 22:00:15 +000094 "be compiled. Use --help to see the options.\n";
Chris Lattnerbef8e0b2007-09-12 18:24:00 +000095 abort();
96 }
97
98 //Get the output stream
Chris Lattnerb515d752009-08-23 07:49:08 +000099 raw_ostream *out = &outs();
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000100 if (!JIT) {
101 if (OutputFilename == "") {
102 std::string base = InputFilename;
Chris Lattnerb515d752009-08-23 07:49:08 +0000103 if (InputFilename == "-") { base = "a"; }
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000104
Chris Lattnerb515d752009-08-23 07:49:08 +0000105 // Use default filename.
106 OutputFilename = base+".bc";
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000107 }
108 if (OutputFilename != "-") {
Chris Lattnerb515d752009-08-23 07:49:08 +0000109 std::string ErrInfo;
110 out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
Chris Lattnerb515d752009-08-23 07:49:08 +0000111 raw_fd_ostream::F_Binary);
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000112 }
113 }
114
115 //Get the input stream
116 std::istream *in = &std::cin;
Chris Lattnerb515d752009-08-23 07:49:08 +0000117 if (InputFilename != "-")
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000118 in = new std::ifstream(InputFilename.c_str());
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000119
120 //Gather the compile flags
121 BrainF::CompileFlags cf = BrainF::flag_off;
Chris Lattnerb515d752009-08-23 07:49:08 +0000122 if (ArrayBoundsChecking)
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000123 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000124
125 //Read the BrainF program
126 BrainF bf;
Owen Anderson31895e72009-07-01 21:22:36 +0000127 Module *mod = bf.parse(in, 65536, cf, Context); //64 KiB
Chris Lattnerb515d752009-08-23 07:49:08 +0000128 if (in != &std::cin)
129 delete in;
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000130 addMainFunction(mod);
131
132 //Verify generated code
133 if (verifyModule(*mod)) {
Chris Lattnerb515d752009-08-23 07:49:08 +0000134 errs() << "Error: module failed verification. This shouldn't happen.\n";
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000135 abort();
136 }
137
138 //Write it out
139 if (JIT) {
Chris Lattnerda062882009-06-17 16:48:44 +0000140 InitializeNativeTarget();
141
Chris Lattnerb515d752009-08-23 07:49:08 +0000142 outs() << "------- Running JIT -------\n";
Reid Kleckner4b1511b2009-07-18 00:42:18 +0000143 ExecutionEngine *ee = EngineBuilder(mod).create();
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000144 std::vector<GenericValue> args;
145 Function *brainf_func = mod->getFunction("brainf");
146 GenericValue gv = ee->runFunction(brainf_func, args);
147 } else {
148 WriteBitcodeToFile(mod, *out);
149 }
150
151 //Clean up
Chris Lattnerb515d752009-08-23 07:49:08 +0000152 if (out != &outs())
153 delete out;
Chris Lattnerbef8e0b2007-09-12 18:24:00 +0000154 delete mod;
155
156 llvm_shutdown();
157
158 return 0;
159}