blob: 65f8033a7e27ecd44b0f04331fd074edd41c69f6 [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"
Teresa Johnson59238642016-11-11 06:02:04 +000029#include "llvm/Bitcode/BitcodeWriter.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"
Boris Ulasevichb76f6c22016-12-15 19:29:42 +000032#include "llvm/ExecutionEngine/MCJIT.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000033#include "llvm/IR/BasicBlock.h"
Chandler Carruth005f27a2013-01-02 11:56:33 +000034#include "llvm/IR/Constants.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000035#include "llvm/IR/DerivedTypes.h"
36#include "llvm/IR/Function.h"
37#include "llvm/IR/Instructions.h"
38#include "llvm/IR/LLVMContext.h"
39#include "llvm/IR/Module.h"
40#include "llvm/IR/Value.h"
Chandler Carruth20d4e6b2014-01-13 09:58:03 +000041#include "llvm/IR/Verifier.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000042#include "llvm/Support/Casting.h"
Chris Lattner909ef092007-09-12 18:24:00 +000043#include "llvm/Support/CommandLine.h"
Benjamin Kramer74996572014-04-29 23:37:02 +000044#include "llvm/Support/FileSystem.h"
Chris Lattner909ef092007-09-12 18:24:00 +000045#include "llvm/Support/ManagedStatic.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000046#include "llvm/Support/TargetSelect.h"
Chris Lattner69733952009-08-23 07:49:08 +000047#include "llvm/Support/raw_ostream.h"
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000048#include <algorithm>
49#include <cstdlib>
Chris Lattner69733952009-08-23 07:49:08 +000050#include <fstream>
Chandler Carruth605e30e2012-12-04 10:16:57 +000051#include <iostream>
Eugene Zelenko2b8e4172016-05-25 01:18:36 +000052#include <memory>
53#include <string>
54#include <system_error>
55#include <vector>
56
Chris Lattner909ef092007-09-12 18:24:00 +000057using namespace llvm;
58
59//Command line options
60
61static cl::opt<std::string>
62InputFilename(cl::Positional, cl::desc("<input brainf>"));
63
64static cl::opt<std::string>
65OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
66
67static cl::opt<bool>
68ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
69
70static cl::opt<bool>
71JIT("jit", cl::desc("Run program Just-In-Time"));
72
Chris Lattner909ef092007-09-12 18:24:00 +000073//Add main function so can be fully compiled
74void addMainFunction(Module *mod) {
75 //define i32 @main(i32 %argc, i8 **%argv)
76 Function *main_func = cast<Function>(mod->
Owen Anderson55f1c092009-08-13 21:58:54 +000077 getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
78 IntegerType::getInt32Ty(mod->getContext()),
Christopher Lambedf07882007-12-17 01:12:55 +000079 PointerType::getUnqual(PointerType::getUnqual(
Serge Guelton59a2d7b2017-04-11 15:01:18 +000080 IntegerType::getInt8Ty(mod->getContext())))));
Chris Lattner909ef092007-09-12 18:24:00 +000081 {
82 Function::arg_iterator args = main_func->arg_begin();
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000083 Value *arg_0 = &*args++;
Chris Lattner909ef092007-09-12 18:24:00 +000084 arg_0->setName("argc");
Duncan P. N. Exon Smith5717ecb2015-11-07 00:55:46 +000085 Value *arg_1 = &*args++;
Chris Lattner909ef092007-09-12 18:24:00 +000086 arg_1->setName("argv");
87 }
88
89 //main.0:
Owen Anderson55f1c092009-08-13 21:58:54 +000090 BasicBlock *bb = BasicBlock::Create(mod->getContext(), "main.0", main_func);
Chris Lattner909ef092007-09-12 18:24:00 +000091
92 //call void @brainf()
93 {
Gabor Greife9ecc682008-04-06 20:25:17 +000094 CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
95 "", bb);
Chris Lattner909ef092007-09-12 18:24:00 +000096 brainf_call->setTailCall(false);
97 }
98
99 //ret i32 0
Owen Anderson55f1c092009-08-13 21:58:54 +0000100 ReturnInst::Create(mod->getContext(),
101 ConstantInt::get(mod->getContext(), APInt(32, 0)), bb);
Chris Lattner909ef092007-09-12 18:24:00 +0000102}
103
104int main(int argc, char **argv) {
105 cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
106
Mehdi Amini03b42e42016-04-14 21:59:01 +0000107 LLVMContext Context;
Owen Anderson6773d382009-07-01 16:58:40 +0000108
Chris Lattner909ef092007-09-12 18:24:00 +0000109 if (InputFilename == "") {
Chris Lattner69733952009-08-23 07:49:08 +0000110 errs() << "Error: You must specify the filename of the program to "
Chris Lattner8e8eae62008-08-23 22:00:15 +0000111 "be compiled. Use --help to see the options.\n";
Chris Lattner909ef092007-09-12 18:24:00 +0000112 abort();
113 }
114
115 //Get the output stream
Chris Lattner69733952009-08-23 07:49:08 +0000116 raw_ostream *out = &outs();
Chris Lattner909ef092007-09-12 18:24:00 +0000117 if (!JIT) {
118 if (OutputFilename == "") {
119 std::string base = InputFilename;
Chris Lattner69733952009-08-23 07:49:08 +0000120 if (InputFilename == "-") { base = "a"; }
Chris Lattner909ef092007-09-12 18:24:00 +0000121
Chris Lattner69733952009-08-23 07:49:08 +0000122 // Use default filename.
123 OutputFilename = base+".bc";
Chris Lattner909ef092007-09-12 18:24:00 +0000124 }
125 if (OutputFilename != "-") {
Rafael Espindola3fd1e992014-08-25 18:16:47 +0000126 std::error_code EC;
127 out = new raw_fd_ostream(OutputFilename, EC, sys::fs::F_None);
Chris Lattner909ef092007-09-12 18:24:00 +0000128 }
129 }
130
131 //Get the input stream
132 std::istream *in = &std::cin;
Chris Lattner69733952009-08-23 07:49:08 +0000133 if (InputFilename != "-")
Chris Lattner909ef092007-09-12 18:24:00 +0000134 in = new std::ifstream(InputFilename.c_str());
Chris Lattner909ef092007-09-12 18:24:00 +0000135
136 //Gather the compile flags
137 BrainF::CompileFlags cf = BrainF::flag_off;
Chris Lattner69733952009-08-23 07:49:08 +0000138 if (ArrayBoundsChecking)
Chris Lattner909ef092007-09-12 18:24:00 +0000139 cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
Chris Lattner909ef092007-09-12 18:24:00 +0000140
141 //Read the BrainF program
142 BrainF bf;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000143 std::unique_ptr<Module> Mod(bf.parse(in, 65536, cf, Context)); // 64 KiB
Chris Lattner69733952009-08-23 07:49:08 +0000144 if (in != &std::cin)
145 delete in;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000146 addMainFunction(Mod.get());
Chris Lattner909ef092007-09-12 18:24:00 +0000147
148 //Verify generated code
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000149 if (verifyModule(*Mod)) {
Chris Lattner69733952009-08-23 07:49:08 +0000150 errs() << "Error: module failed verification. This shouldn't happen.\n";
Chris Lattner909ef092007-09-12 18:24:00 +0000151 abort();
152 }
153
154 //Write it out
155 if (JIT) {
Chris Lattnerd24df242009-06-17 16:48:44 +0000156 InitializeNativeTarget();
Boris Ulasevichb76f6c22016-12-15 19:29:42 +0000157 InitializeNativeTargetAsmPrinter();
Chris Lattnerd24df242009-06-17 16:48:44 +0000158
Chris Lattner69733952009-08-23 07:49:08 +0000159 outs() << "------- Running JIT -------\n";
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000160 Module &M = *Mod;
161 ExecutionEngine *ee = EngineBuilder(std::move(Mod)).create();
Boris Ulasevichb76f6c22016-12-15 19:29:42 +0000162 if (!ee) {
163 errs() << "Error: execution engine creation failed.\n";
164 abort();
165 }
Chris Lattner909ef092007-09-12 18:24:00 +0000166 std::vector<GenericValue> args;
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000167 Function *brainf_func = M.getFunction("brainf");
Chris Lattner909ef092007-09-12 18:24:00 +0000168 GenericValue gv = ee->runFunction(brainf_func, args);
Boris Ulasevicha2a02132017-01-17 13:27:28 +0000169 // Genereated code calls putchar, and output is not guaranteed without fflush.
170 // The better place for fflush(stdout) call would be the generated code, but it
171 // is unmanageable because stdout linkage name depends on stdlib implementation.
172 fflush(stdout);
Chris Lattner909ef092007-09-12 18:24:00 +0000173 } else {
Rafael Espindola2a8a2792014-08-19 04:04:25 +0000174 WriteBitcodeToFile(Mod.get(), *out);
Chris Lattner909ef092007-09-12 18:24:00 +0000175 }
176
177 //Clean up
Chris Lattner69733952009-08-23 07:49:08 +0000178 if (out != &outs())
179 delete out;
Chris Lattner909ef092007-09-12 18:24:00 +0000180
181 llvm_shutdown();
182
183 return 0;
184}