-
Notifications
You must be signed in to change notification settings - Fork 3
/
bf_jit.h
60 lines (50 loc) · 1.72 KB
/
bf_jit.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2014 Brian Quinlan
// See "LICENSE" file for details.
//
// Implements a BrainfuckRunner that interprets the Brainfuck source one command
// at a time but will use BrainfuckCompileAndGo to execute loops that are
// run frequently.
#ifndef BF_JIT_H_
#define BF_JIT_H_
#include <map>
#include <string>
#include <memory>
#include "bf_runner.h"
#include "bf_compile_and_go.h"
using std::map;
using std::string;
using std::shared_ptr;
class BrainfuckJIT : public BrainfuckRunner {
public:
BrainfuckJIT();
virtual bool init(string::const_iterator start,
string::const_iterator end);
virtual void* run(BrainfuckReader reader,
void* reader_arg,
BrainfuckWriter writer,
void* writer_arg,
void* memory);
private:
struct Loop {
Loop() : condition_evaluation_count(0) { }
explicit Loop(string::const_iterator after) :
after_end(after), condition_evaluation_count(0) { }
// The position of the Brainfuck token after the end of the loop.
string::const_iterator after_end;
// The number of types that the loop condition (e.g. "[") has been
// evaluated. Note that the count will not be updated after the loop has
// been JITed.
uint64_t condition_evaluation_count;
// The compiled code that represents the loop. Will be NULL until the loop
// is JITed.
shared_ptr<BrainfuckCompileAndGo> compiled;
};
string::const_iterator start_;
string::const_iterator end_;
// Maps the position of a Brainfuck block start to Loop e.g.
// ,[..,]
// ^ ^
// x y => loop_start_to_loop_[x] = Loop(y);
map<string::const_iterator, Loop> loop_start_to_loop_;
};
#endif // BF_JIT_H_