| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 1 | //===-- BrainF.h - BrainF compiler class ----------------------*- C++ -*-===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | bcf65db | 2007-12-29 20:37:57 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 7 | // | 
|  | 8 | //===--------------------------------------------------------------------===// | 
|  | 9 | // | 
|  | 10 | // This class stores the data for the BrainF compiler so it doesn't have | 
|  | 11 | // to pass all of it around.  The main method is parse. | 
|  | 12 | // | 
|  | 13 | //===--------------------------------------------------------------------===// | 
|  | 14 |  | 
|  | 15 | #ifndef BRAINF_H | 
|  | 16 | #define BRAINF_H | 
|  | 17 |  | 
| Owen Anderson | 6773d38 | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 18 | #include "llvm/LLVMContext.h" | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 19 | #include "llvm/Module.h" | 
| Duncan Sands | a07136e | 2008-04-13 06:22:09 +0000 | [diff] [blame] | 20 | #include "llvm/Support/IRBuilder.h" | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 21 |  | 
|  | 22 | using namespace llvm; | 
|  | 23 |  | 
|  | 24 | /// This class provides a parser for the BrainF language. | 
|  | 25 | /// The class itself is made to store values during | 
|  | 26 | /// parsing so they don't have to be passed around | 
|  | 27 | /// as much. | 
|  | 28 | class BrainF { | 
|  | 29 | public: | 
|  | 30 | /// Options for how BrainF should compile | 
|  | 31 | enum CompileFlags { | 
|  | 32 | flag_off         = 0, | 
|  | 33 | flag_arraybounds = 1 | 
|  | 34 | }; | 
|  | 35 |  | 
|  | 36 | /// This is the main method.  It parses BrainF from in1 | 
|  | 37 | /// and returns the module with a function | 
|  | 38 | /// void brainf() | 
|  | 39 | /// containing the resulting code. | 
|  | 40 | /// On error, it calls abort. | 
|  | 41 | /// The caller must delete the returned module. | 
| Owen Anderson | 1cf085d | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 42 | Module *parse(std::istream *in1, int mem, CompileFlags cf, | 
| Owen Anderson | 2a15443 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 43 | LLVMContext& C); | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 44 |  | 
|  | 45 | protected: | 
|  | 46 | /// The different symbols in the BrainF language | 
|  | 47 | enum Symbol { | 
|  | 48 | SYM_NONE, | 
|  | 49 | SYM_READ, | 
|  | 50 | SYM_WRITE, | 
|  | 51 | SYM_MOVE, | 
|  | 52 | SYM_CHANGE, | 
|  | 53 | SYM_LOOP, | 
|  | 54 | SYM_ENDLOOP, | 
|  | 55 | SYM_EOF | 
|  | 56 | }; | 
|  | 57 |  | 
|  | 58 | /// Names of the different parts of the language. | 
|  | 59 | /// Tape is used for reading and writing the tape. | 
|  | 60 | /// headreg is used for the position of the head. | 
|  | 61 | /// label is used for the labels for the BasicBlocks. | 
|  | 62 | /// testreg is used for testing the loop exit condition. | 
|  | 63 | static const char *tapereg; | 
|  | 64 | static const char *headreg; | 
|  | 65 | static const char *label; | 
|  | 66 | static const char *testreg; | 
|  | 67 |  | 
|  | 68 | /// Put the brainf function preamble and other fixed pieces of code | 
| Owen Anderson | 2a15443 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 69 | void header(LLVMContext& C); | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 70 |  | 
|  | 71 | /// The main loop for parsing.  It calls itself recursively | 
|  | 72 | /// to handle the depth of nesting of "[]". | 
| Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 73 | void readloop(PHINode *phi, BasicBlock *oldbb, | 
|  | 74 | BasicBlock *testbb, LLVMContext &Context); | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 75 |  | 
|  | 76 | /// Constants during parsing | 
|  | 77 | int memtotal; | 
|  | 78 | CompileFlags comflag; | 
|  | 79 | std::istream *in; | 
|  | 80 | Module *module; | 
|  | 81 | Function *brainf_func; | 
|  | 82 | Function *getchar_func; | 
|  | 83 | Function *putchar_func; | 
|  | 84 | Value *ptr_arr; | 
|  | 85 | Value *ptr_arrmax; | 
|  | 86 | BasicBlock *endbb; | 
|  | 87 | BasicBlock *aberrorbb; | 
|  | 88 |  | 
|  | 89 | /// Variables | 
| Eric Christopher | 5927883 | 2008-08-08 19:39:37 +0000 | [diff] [blame] | 90 | IRBuilder<> *builder; | 
| Chris Lattner | 909ef09 | 2007-09-12 18:24:00 +0000 | [diff] [blame] | 91 | Value *curhead; | 
|  | 92 | }; | 
|  | 93 |  | 
|  | 94 | #endif |