-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign_pc.cpp
More file actions
228 lines (165 loc) · 4.38 KB
/
Copy pathassign_pc.cpp
File metadata and controls
228 lines (165 loc) · 4.38 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
#include <cassert>
#include <unordered_map>
#include "common.h"
#include "Expression.h"
#include "branch.h"
/*
* assign a pc to each line. This also converts local labels to relative form.
* fix_branches -- extend out-of range branches. (bra -> brl)
*
*/
namespace {
inline OpCode i(Mnemonic m, AddressMode mode) {
return OpCode(Instruction(m65816, m), mode);
}
bool in_range(ExpressionPtr e, uint32_t pc, const identifier_map &map) {
try {
uint32_t target = e->evaluate(pc, map);
// pc is pc before the instruction. actual pc is + 2.
pc += 2;
if (target > pc + 0x7f) return false;
if (target < pc - 0x80) return false;
return true;
} catch (std::exception &ex) {
return false;
}
}
// returns true if in-range.
bool in_range(ExpressionPtr e, uint32_t pc, const identifier_map &map, const branch &b) {
if (b.far) return true;
try {
uint32_t target = e->evaluate(pc, map);
if (b.in_range(pc, target)) return true;
return false;
} catch (std::exception &ex) {
std::string s = e->to_string();
fprintf(stderr, "Unable to evaluate smart branch target: %s. Assuming far branch.\n", s.c_str());
return false;
}
}
}
void assign_pc(BasicLinePtr line, uint32_t &pc, identifier_map *map = nullptr) {
if (!line) return;
line->pc = pc;
if (line->label) {
if (map)
map->emplace(std::make_pair(line->label, pc));
return;
}
if (line->directive != kUndefinedDirective) {
ExpressionPtr operand = line->operands[0];
switch (line->directive) {
case kUndefinedDirective:
case ALIGN:
// should never happen.
assert("unexpected directive!");
return;
case DCB:
assert(operand->is_vector());
pc += std::static_pointer_cast<VectorExpression>(operand)->size() * 1;
return;
case DCW:
assert(operand->is_vector());
pc += std::static_pointer_cast<VectorExpression>(operand)->size() * 2;
return;
case DCL:
assert(operand->is_vector());
pc += std::static_pointer_cast<VectorExpression>(operand)->size() * 4;
return;
case SMART_BRANCH:
pc += line->branch.size();
return;
case DS:
// todo -- simplify stuff like
// ds 512-*
//
uint32_t x;
if (operand->is_integer(x)) {
pc += x;
return;
}
throw std::runtime_error("Expression is not absolute");
}
}
if (line->opcode) {
OpCode op = line->opcode;
pc += op.bytes(line->longM, line->longX);
}
}
void assign_pc(LineQueue &lines) {
uint32_t pc = 0;
identifier_map map;
for (auto line : lines) {
assign_pc(line, pc, &map);
}
// convert to relative labels.
for (auto line : lines) {
for (auto &e : line->operands) {
if (e) e = e->make_relative(line->pc, map);
}
}
}
void assign_pc(BlockQueue &blocks) {
uint32_t pc = 0;
for (auto block : blocks) {
block->pc = pc;
for (auto line : block->lines) {
assign_pc(line, pc);
}
assign_pc(block->exit_branch, pc);
}
}
void fix_branches(Segment *segment, BlockQueue &blocks) {
// rewrite short branches, if necessary.
// todo -- optimization
// multi-branches could be optimized
// eg
// if (a <= b) goto foo;
// bcc foo
// beq foo
//
// expands to
// bcs *+5
// bra foo
// bne *+5
// bra foo
bool delta;
do {
delta = false;
// 1. assign a pc to all lines
uint32_t pc = 0;
identifier_map map;
// insert function name
// this is not actually correct... need to insert as a label so
// optimizer can work correctly.
if (segment && segment->name) {
map.emplace(std::make_pair(segment->name, pc));
}
for (auto block : blocks) {
block->pc = pc;
if (block->label) map.emplace(std::make_pair(block->label, pc));
for (auto line : block->lines) {
assign_pc(line, pc, &map);
}
assign_pc(block->exit_branch, pc, &map);
}
// 2. if a relative branch is out of range, make it long.
uint32_t pc_fudge = 0;
for (auto block : blocks) {
auto line = block->exit_branch;
if (line && line->directive == SMART_BRANCH) {
uint32_t fudge = 0;
uint32_t pc = line->pc + pc_fudge;
if (in_range(line->operands[0], pc, map, line->branch)) continue;
// if it's already a far branch... oops!
delta = true;
line->branch.far = true;
fudge = line->branch.make_far();
// adjust all future labels.
for (auto &kv : map) { if (kv.second >= pc) kv.second += fudge; }
pc_fudge += fudge;
}
}
} while (delta);
// check if regular branches are in-range, too?
}