blob: d19427add876af4f2e638e576d90236ebf60475e [file] [log] [blame]
Eugene Zelenko2b8e4172016-05-25 01:18:36 +00001//===-- BrainFDriver.cpp - BrainF compiler driver -------------------------===//
Chris Lattner909ef092007-09-12 18:24:00 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerbcf65db2007-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 Lattner909ef092007-09-12 18:24:00 +00007//
Eugene Zelenko2b8e4172016-05-25 01:18:36 +00008//===----------------------------------------------------------------------===//
Chris Lattner909ef092007-09-12 18:24:00 +00009//
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 Lattner909ef092007-09-12 18:24:00 +000024//
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000025//===----------------------------------------------------------------------===//
Chris Lattner909ef092007-09-12 18:24:00 +000026
27#include "BrainF.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000028#include "llvm/ADT/APInt.h"
Chris Lattner909ef092007-09-12 18:24:00 +000029#include "llvm/Bitcode/ReaderWriter.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000030#include "llvm/ExecutionEngine/ExecutionEngine.h"
Chris Lattner909ef092007-09-12 18:24:00 +000031#include "llvm/ExecutionEngine/GenericValue.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000032#include "llvm/IR/BasicBlock.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000033#include "llvm/IR/Constants.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000034#include "llvm/IR/DerivedTypes.h"
35#include "llvm/IR/Function.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/LLVMContext.h"
38#include "llvm/IR/Module.h"
39#include "llvm/IR/Value.h"
Chandler Carruth20d4e6b2014-01-13 09:58:03 +000040#include "llvm/IR/Verifier.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000041#include "llvm/Support/Casting.h"
Chris Lattner909ef092007-09-12 18:24:00 +000042#include "llvm/Support/CommandLine.h"
Benjamin Kramer74996572014-04-29 23:37:02 +000043#include "llvm/Support/FileSystem.h"
Chris Lattner909ef092007-09-12 18:24:00 +000044#include "llvm/Support/ManagedStatic.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000045#include "llvm/Support/TargetSelect.h"
Chris Lattner69733952009-08-23 07:49:08 +000046#include "llvm/Support/raw_ostream.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000047#include <algorithm>
48#include <cstdlib>
Chris Lattner69733952009-08-23 07:49:08 +000049#include <fstream>
Chandler Carruth605e30e2012-12-04 10:16:57 +000050#include <iostream>
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000051#include <memory>
52#include <string>
53#include <system_error>
54#include <vector>
55
Chris Lattner909ef092007-09-12 18:24:00 +000056using namespace llvm;
57
58//Command line options
59
60static cl::opt<std::string>
61InputFilename(cl::Positional, cl::desc("<input brainf>"));
62
63static cl::opt<std::string>
64OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
65
66static cl::opt<bool>
67ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
68
69static cl::opt<bool>
70JIT("jit", cl::desc("Run program Just-In-Time"));
71
Chris Lattner909ef092007-09-12 18:24:00 +000072//Add main function so can be fully compiled
73void addMainFunction(Module *mod) {
74 //define i32 @main(i32 %argc, i8 **%argv)
75 Function *main_func = cast<Function>(mod->
Owen Anderson55f1c092009-08-13 21:58:54 +000076 getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
77 IntegerType::getInt32Ty(mod->getContext()),
Christopher Lambedf07882007-12-17 01:12:55 +000078 PointerType::getUnqual(PointerType::getUnqual(
Owen Anderson55f1c092009-08-13 21:58:54 +000079 IntegerType::getInt8Ty(mod->getContext()))), NULL));
Chris Lattner909ef092007-09-12 18:24:00 +000080 {
81 Function::arg_iterator args = main_func->arg_begin();
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000082 Value *arg_0 = &*args++;
Chris Lattner909ef092007-09-12 18:24:00 +000083 arg_0->setName("argc");
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000084 Value *arg_1 = &*args++;
Chris Lattner909ef092007-09-12 18:24:00 +000085 arg_1->setName("argv");
86 }
87
88 //main.0:
Owen Anderson55f1c092009-08-13 21:58:54 +000089 BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
Chris Lattner909ef092007-09-12 18:24:00 +000090
91 //call void @brainf()
92 {
Gabor Greife9ecc682008-04-06 20:25:17 +000093 CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
94 "", bb);
Chris Lattner909ef092007-09-12 18:24:00 +000095 brainf_call->setTailCall(false);
96 }
97
98 //ret i32 0
Owen Anderson55f1c092009-08-13 21:58:54 +000099 ReturnInst::Create(mod->getContext(),
100 ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
Chris Lattner909ef092007-09-12 18:24:00 +0000101}
102
103int main(int argc, char **argv) {
104 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
105
Mehdi Amini03b42e42016-04-14 21:59:01 +0000106 LLVMContext Context;
Owen Anderson6773d382009-07-01 16:58:40 +0000107
Chris Lattner909ef092007-09-12 18:24:00 +0000108 if (InputFilename == "") {
Chris Lattner69733952009-08-23 07:49:08 +0000109 errs() << "Error: You must specify the filename of the program to "
Chris Lattner8e8eae62008-08-23 22:00:15 +0000110 "be compiled. Use --help to see the options.\n";
Chris Lattner909ef092007-09-12 18:24:00 +0000111 abort();
112 }
113
114 //Get the output stream
Chris Lattner69733952009-08-23 07:49:08 +0000115 raw_ostream *out = &outs();
Chris Lattner909ef092007-09-12 18:24:00 +0000116 if (!JIT) {
117 if (OutputFilename == "") {
118 std::string base = InputFilename;
Chris Lattner69733952009-08-23 07:49:08 +0000119 if (InputFilename == "-") { base = "a"; }
Chris Lattner909ef092007-09-12 18:24:00 +0000120
Chris Lattner69733952009-08-23 07:49:08 +0000121 // Use default filename.
122 OutputFilename = base+".bc";
Chris Lattner909ef092007-09-12 18:24:00 +0000123 }
124 if (OutputFilename != "-") {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000125 std::error_code EC;
126 out = new raw_fd_ostream(OutputFilename, EC, sys::fs::F_None);
Chris Lattner909ef092007-09-12 18:24:00 +0000127 }
128 }
129
130 //Get the input stream
131 std::istream *in = &std::cin;
Chris Lattner69733952009-08-23 07:49:08 +0000132 if (InputFilename != "-")
Chris Lattner909ef092007-09-12 18:24:00 +0000133 in = new std::ifstream(InputFilename.c_str());
Chris Lattner909ef092007-09-12 18:24:00 +0000134
135 //Gather the compile flags
136 BrainF::CompileFlags cf = BrainF::flag_off;
Chris Lattner69733952009-08-23 07:49:08 +0000137 if (ArrayBoundsChecking)
Chris Lattner909ef092007-09-12 18:24:00 +0000138 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
Chris Lattner909ef092007-09-12 18:24:00 +0000139
140 //Read the BrainF program
141 BrainF bf;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000142 std::unique_ptr<Module> Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
Chris Lattner69733952009-08-23 07:49:08 +0000143 if (in != &std::cin)
144 delete in;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000145 addMainFunction(Mod.get());
Chris Lattner909ef092007-09-12 18:24:00 +0000146
147 //Verify generated code
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000148 if (verifyModule(*Mod)) {
Chris Lattner69733952009-08-23 07:49:08 +0000149 errs() << "Error: module failed verification. This shouldn't happen.\n";
Chris Lattner909ef092007-09-12 18:24:00 +0000150 abort();
151 }
152
153 //Write it out
154 if (JIT) {
Chris Lattnerd24df242009-06-17 16:48:44 +0000155 InitializeNativeTarget();
156
Chris Lattner69733952009-08-23 07:49:08 +0000157 outs() << "------- Running JIT -------\n";
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000158 Module &M = *Mod;
159 ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
Chris Lattner909ef092007-09-12 18:24:00 +0000160 std::vector<GenericValue> args;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000161 Function *brainf_func = M.getFunction("brainf");
Chris Lattner909ef092007-09-12 18:24:00 +0000162 GenericValue gv = ee->runFunction(brainf_func, args);
163 } else {
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000164 WriteBitcodeToFile(Mod.get(), *out);
Chris Lattner909ef092007-09-12 18:24:00 +0000165 }
166
167 //Clean up
Chris Lattner69733952009-08-23 07:49:08 +0000168 if (out != &outs())
169 delete out;
Chris Lattner909ef092007-09-12 18:24:00 +0000170
171 llvm_shutdown();
172
173 return 0;
174}