-
Notifications
You must be signed in to change notification settings - Fork 6
/
jitasm.Frontend.h
100 lines (87 loc) · 2.17 KB
/
jitasm.Frontend.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#pragma once
#ifndef jitasm_Frontend_h__
#define jitasm_Frontend_h__
#include "jitasm.h"
#include "jitasm.CodeBuffer.h"
namespace jitasm
{
class Frontend
{
protected:
virtual ~Frontend()
{
}
virtual void Assemble() = 0;
};
template < typename Derived > struct Frontend$CRTP : Frontend /* using Curiously Recurring Template Pattern */
{
Derived & derived()
{
return *static_cast<Derived *>(this);
}
Derived const & derived() const
{
return *static_cast<Derived const *>(this);
}
struct Label
{
sint32 key;
size_t instr;
explicit Label(sint32 key) : key(key), instr(0) {}
};
typedef std::deque< Label > LabelList;
bool assembled_;
LabelList labels_;
Frontend$CRTP() : assembled_(false)
{
}
void * GetCodePointer()
{
if (!assembled_)
{
Assemble();
}
return derived().GetBufferPointer();
}
size_t GetCodeSize()
{
return derived().GetBufferSize();
}
size_t NewLabelID(sint32 label_key)
{
labels_.push_back(Label(label_key));
return labels_.size() - 1;
}
size_t CheckLabelID(sint32 label_key)
{
for (size_t i = 0; i < labels_.size(); i++)
{
if (labels_[i].key == label_key)
{
return i;
}
}
return (size_t)-1;
}
size_t GetLabelID(sint32 label_key)
{
for (auto const & label : labels)
{
if (label.key == label_key)
{
return i;
}
}
return NewLabelID(label_key);
}
void SetLabelID(size_t label_id)
{
labels_[label_id].instr = instrs_.size();
}
void L(sint32 label_key)
{
SetLabelID(GetLabelID(label_key));
}
};
}
#endif // jitasm_Frontend_h__