d0fb1b1de0ef38f1e339bcdc4fc9db9feb5f9835
[oota-llvm.git] / examples / BrainF / BrainF.h
1 //===-- BrainF.h - BrainF compiler class ----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
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
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Module.h"
20 #include "llvm/Support/IRBuilder.h"
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.
42     Module *parse(std::istream *in1, int mem, CompileFlags cf, LLVMContext* C);
43
44   protected:
45     /// The different symbols in the BrainF language
46     enum Symbol {
47       SYM_NONE,
48       SYM_READ,
49       SYM_WRITE,
50       SYM_MOVE,
51       SYM_CHANGE,
52       SYM_LOOP,
53       SYM_ENDLOOP,
54       SYM_EOF
55     };
56
57     /// Names of the different parts of the language.
58     /// Tape is used for reading and writing the tape.
59     /// headreg is used for the position of the head.
60     /// label is used for the labels for the BasicBlocks.
61     /// testreg is used for testing the loop exit condition.
62     static const char *tapereg;
63     static const char *headreg;
64     static const char *label;
65     static const char *testreg;
66
67     /// Put the brainf function preamble and other fixed pieces of code
68     void header(LLVMContext* C);
69
70     /// The main loop for parsing.  It calls itself recursively
71     /// to handle the depth of nesting of "[]".
72     void readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb);
73
74     /// Constants during parsing
75     int memtotal;
76     CompileFlags comflag;
77     std::istream *in;
78     Module *module;
79     Function *brainf_func;
80     Function *getchar_func;
81     Function *putchar_func;
82     Value *ptr_arr;
83     Value *ptr_arrmax;
84     BasicBlock *endbb;
85     BasicBlock *aberrorbb;
86
87     /// Variables
88     IRBuilder<> *builder;
89     Value *curhead;
90 };
91
92 #endif