-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
215 lines (154 loc) · 3.74 KB
/
common.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#ifndef __common_h__
#define __common_h__
#include <string>
#include <deque>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include "Machine.h"
#include "Instruction.h"
#include "OpCode.h"
#include "dp_register_set.h"
#include "register_set.h"
#include "branch.h"
#include "types.h"
#include "cxx/filesystem.h"
enum Directive {
kUndefinedDirective = 0,
DCB,
DCW,
DCL,
DS,
ALIGN,
SMART_BRANCH,
};
enum {
reg_none = 0,
reg_read = 1,
reg_write = 2,
reg_rw = 3
};
struct BasicLine {
// Line data while analyzing.
BasicLine() = default;
BasicLine(const BasicLine &) = default;
BasicLine(BasicLine &&) = default;
BasicLine(identifier l) : label(l)
{}
BasicLine(Mnemonic m, AddressMode mode) {
opcode = OpCode(m65816, m, mode);
}
template<class E>
BasicLine(Mnemonic m, AddressMode mode, E && a) {
opcode = OpCode(m65816, m, mode);
operands[0] = std::forward<E>(a);
}
template<class E>
BasicLine(Mnemonic m, AddressMode mode, E && a, E && b) {
opcode = OpCode(m65816, m, mode);
operands[0] = std::forward<E>(a);
operands[1] = std::forward<E>(b);
}
template<class E>
BasicLine(const OpCode &op, E &&a) {
opcode = op;
operands[0] = std::forward<E>(a);
}
template<class E>
BasicLine(const OpCode &op, E && a, E && b) {
opcode = op;
operands[0] = std::forward<E>(a);
operands[1] = std::forward<E>(b);
}
BasicLine(Directive d) {
directive = d;
}
template<class E>
BasicLine(Directive d, E &&e) {
directive = d;
operands[0] = std::forward<E>(e);
}
template<class...Args>
static BasicLinePtr Make(Args&&... args) {
return std::make_shared<BasicLine>(std::forward<Args>(args)...);
}
identifier label = nullptr;
bool global = false;
Directive directive = kUndefinedDirective;
OpCode opcode;
ExpressionPtr operands[2];
bool longM = true;
bool longX = true;
branch branch;
uint32_t pc = 0;
// live registers, etc.
//dp_register_set reg_live;
dp_register reg; // from operands.
unsigned reg_count = 0;
unsigned reg_status = reg_none;
// update reg, reg_count, reg_status.
// depends on opcode, operands, longM, longX
void calc_registers();
register_set read_registers() const;
register_set write_registers() const;
};
typedef std::deque<BasicLinePtr> LineQueue;
typedef std::deque<BasicBlockPtr> BlockQueue;
#include "basic_block.h"
class Segment {
public:
identifier name = nullptr;
identifier segment = nullptr;
//BlockQueue blocks;
LineQueue lines;
// attributes
unsigned parm_size = 0;
unsigned local_size = 0;
unsigned temp_size = 0;
unsigned return_size = 0;
unsigned kind = 0;
bool debug = false;
bool global = false;
bool databank = false;
bool noreturn = false;
bool has_volatile = false;
enum {
rtl = 0,
rts,
rti
} return_type = rtl;
enum {
naked = 0,
data,
bss,
arrays,
cdecl,
pascal,
stdcall
} convention = naked;
std::vector<BasicLinePtr> prologue_code;
std::vector<BasicLinePtr> epilogue_code;
std::vector<identifier> strong_vector;
};
// n. b. -- other stuff outside of segment, like export / import set.
typedef std::deque< SegmentPtr> SegmentQueue;
// Translation unit aka file
struct Module {
std::string filename;
SegmentQueue segments;
std::vector<identifier> imports;
std::vector<identifier> exports;
};
typedef std::unique_ptr<Module> ModulePtr;
std::unique_ptr<Module> parse_file(int fd);
std::unique_ptr<Module> parse_file(const filesystem::path &p);
bool peephole(BasicBlockPtr);
bool final_peephole(BasicBlockPtr);
void print(const LineQueue &lines);
void simplify(LineQueue &lines);
bool parse_file(const filesystem::path &path, SegmentQueue &segments);
bool parse_file(FILE *file, SegmentQueue &segments);
unsigned classify(Mnemonic);
unsigned classify(OpCode);
void basic_block(Segment *segment);
#endif