Merge branch 'master' into msp430
[llvm/msp430.git] / examples / BrainF / BrainF.h
blob06c00ae4df247bbced806d2efb9ceddd7bc981c9
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.
13 //===--------------------------------------------------------------------===//
15 #ifndef BRAINF_H
16 #define BRAINF_H
18 #include "llvm/Module.h"
19 #include "llvm/Support/IRBuilder.h"
21 using namespace llvm;
23 /// This class provides a parser for the BrainF language.
24 /// The class itself is made to store values during
25 /// parsing so they don't have to be passed around
26 /// as much.
27 class BrainF {
28 public:
29 /// Options for how BrainF should compile
30 enum CompileFlags {
31 flag_off = 0,
32 flag_arraybounds = 1
35 /// This is the main method. It parses BrainF from in1
36 /// and returns the module with a function
37 /// void brainf()
38 /// containing the resulting code.
39 /// On error, it calls abort.
40 /// The caller must delete the returned module.
41 Module *parse(std::istream *in1, int mem, CompileFlags cf);
43 protected:
44 /// The different symbols in the BrainF language
45 enum Symbol {
46 SYM_NONE,
47 SYM_READ,
48 SYM_WRITE,
49 SYM_MOVE,
50 SYM_CHANGE,
51 SYM_LOOP,
52 SYM_ENDLOOP,
53 SYM_EOF
56 /// Names of the different parts of the language.
57 /// Tape is used for reading and writing the tape.
58 /// headreg is used for the position of the head.
59 /// label is used for the labels for the BasicBlocks.
60 /// testreg is used for testing the loop exit condition.
61 static const char *tapereg;
62 static const char *headreg;
63 static const char *label;
64 static const char *testreg;
66 /// Put the brainf function preamble and other fixed pieces of code
67 void header();
69 /// The main loop for parsing. It calls itself recursively
70 /// to handle the depth of nesting of "[]".
71 void readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb);
73 /// Constants during parsing
74 int memtotal;
75 CompileFlags comflag;
76 std::istream *in;
77 Module *module;
78 Function *brainf_func;
79 Function *getchar_func;
80 Function *putchar_func;
81 Value *ptr_arr;
82 Value *ptr_arrmax;
83 BasicBlock *endbb;
84 BasicBlock *aberrorbb;
86 /// Variables
87 IRBuilder<> *builder;
88 Value *curhead;
91 #endif