-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.cpp
317 lines (240 loc) · 6.62 KB
/
data.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* data segment to omf.
*/
#include <stdlib.h>
#include "common.h"
#include "omf.h"
#include "Expression.h"
/*
* this assumes little endian NUMSEX, 32-bit NUMLEN, variable length LABLEN.
*
*/
namespace {
void dc(OMF::SegmentBuilder &builder, ExpressionPtr e, unsigned bytes);
void dc(OMF::SegmentBuilder &builder, std::vector<ExpressionPtr> &v, unsigned bytes);
void dc(OMF::SegmentBuilder &builder, std::vector<ExpressionPtr> &v, unsigned bytes) {
for (auto e : v)
dc(builder, e, bytes);
}
void dc(OMF::SegmentBuilder &builder, ExpressionPtr e, unsigned bytes) {
uint32_t i;
std::vector<ExpressionPtr> v;
if (e->is_vector(v)) {
dc(builder, v, bytes);
return;
}
// todo -- special handling for relative data (*).
if (e->is_integer(i)) {
builder.data(i, bytes);
return;
}
std::vector<uint8_t> tmp = e->to_omf(OMF::EXPR, bytes);
builder.raw_append(tmp.begin(), tmp.end(), bytes);
}
void ds(OMF::SegmentBuilder &builder, ExpressionPtr e) {
// DS #
// DS $1000-* ?
uint32_t size;
e = e->simplify();
if (e->is_integer(size)) {
builder.ds(size);
return;
}
throw std::runtime_error("Expression is not absolute");
}
void align(OMF::SegmentBuilder &builder, ExpressionPtr e) {
// orca/m align is a ds, but requires the
// segment be aligned, first.
// too complicated and not useful.
// segments only support page and bank alignment.
// bank alignment is not supported by the memory manager,
// so it can cause purging as the loader emulates it.
fprintf(stderr, "align - not supported\n");
exit(1);
}
void dc(OMF::SegmentBuilder &builder, ExpressionPtr e, unsigned bytes, uint32_t pc, OpCode op) {
unsigned type = OMF::EXPR; // expression.
AddressMode mode = op.addressMode();
uint32_t i;
//special handling for relative instructions.
if (op.isRelative()) {
int32_t branch;
if (e->evaluate_relative(pc + 1 + bytes, branch))
{
// check if in range...
if (bytes == 1) {
if (branch >= -128 && branch <= 127) {
builder.data((uint32_t)branch, 1);
return;
}
// or throw...
throw std::runtime_error("branch out of range");
}
if (bytes == 2) {
if (branch >= -32768 && branch <= 32767) {
// mmmm will wrap at $ffff so all numbers reachable.
builder.data((uint32_t)branch, 2);
return;
}
throw std::runtime_error("branch out of range");
}
return;
}
std::vector<uint8_t> tmp = e->to_omf(OMF::RELEXPR, bytes, bytes); // 0 displacement?
builder.raw_append(tmp.begin(), tmp.end(), bytes);
return;
}
if (e->is_integer(i)) {
builder.data(i, bytes);
return;
}
const std::string *s;
if (e->is_string(s)) {
builder.data(s->begin(), s->end());
// zero-pad to boundary.
int mod = s->size() % bytes;
builder.data(0, mod);
return;
}
switch(op.mnemonic()) {
case JSL:
type = OMF::LEXPR; // lexpr
break;
case JMP:
// jmp (abs) actually uses bank 0.
if (mode == absolute_indirect)
break;
case JSR:
type = OMF::BKEXPR; // bkexpr
break;
default:
break;
}
std::vector<uint8_t> tmp = e->to_omf(type, bytes);
builder.raw_append(tmp.begin(), tmp.end(), bytes);
//
}
}
extern void assign_pc(LineQueue &lines);
OMF::Segment data_to_omf(Segment *segment) {
OMF::SegmentBuilder builder;
assign_pc(segment->lines); //
for (auto &line : segment->lines) {
if (line->label) {
auto label = line->label;
// create a global label
builder.global(*label, 0, 0x4e, line->global);
continue;
}
switch (line->directive) {
case DCB:
dc(builder, line->operands[0], 1);
break;
case DCW:
dc(builder, line->operands[0], 2);
break;
case DCL:
dc(builder, line->operands[0], 4);
break;
case DS:
ds(builder, line->operands[0]);
break;
case ALIGN:
align(builder, line->operands[0]);
break;
default:
fprintf(stderr, "Invalid data directive\n");
exit(1);
}
}
// strong references
for (auto id : segment->strong_vector) {
if (id) builder.strong(*id);
}
builder.end();
//
OMF::Segment seg;
seg.length = builder.length;
seg.body = std::move(builder.body);
seg.kind = segment->kind;
if (seg.kind == 0) seg.kind = OMF::KIND_DATA | OMF::ATTR_PRIVATE; // data, static, private
if (segment->global) seg.kind &= ~OMF::ATTR_PRIVATE; // public.
// orca has a banksize of $10000 for ~GLOBALS (and code segments), 0 for ~ARRAYS
// orca/c uses code, static, private. ($4000)
// apw c uses data, static, private, reload. ($4401)
// could have separate segments for bss / data?
// bss is 0-initialized.
seg.banksize = 0x010000;
if (segment->name) seg.segname = *segment->name;
if (segment->segment) seg.loadname = *segment->segment;
if (seg.segname.empty()) seg.segname = "~globals";
return seg;
}
OMF::Segment code_to_omf(Segment *segment) {
// this operates on basic blocks.
OMF::SegmentBuilder builder;
assign_pc(segment->lines); // todo -- move this elsewhere. optimizer will set pc as part of fixing branches.
for (auto line : segment->lines) {
if (line->label) {
// code -- only include global labels.
if (line->global)
builder.global(*line->label, 0, 'N', line->global);
continue;
}
if (line->directive != kUndefinedDirective) {
switch (line->directive) {
case DCB:
dc(builder, line->operands[0], 1);
break;
case DCW:
dc(builder, line->operands[0], 2);
break;
case DCL:
dc(builder, line->operands[0], 4);
break;
case DS:
ds(builder, line->operands[0]);
break;
default:
fprintf(stderr, "Invalid code directive\n");
exit(1);
}
continue;
}
// show time! opcode and operands.
{
OpCode op = line->opcode;
builder.data(op.opcode());
unsigned bytes;
// MVP / MVN are special cases.
if (op.mnemonic() == MVN || op.mnemonic() == MVP) {
dc(builder,line->operands[1], 1);
dc(builder,line->operands[0], 1);
continue;
}
// also,
// todo -- relative / relative long addressing needs REL expr.
// dp operands s/b ZPEXPR
// abs operands s/b BKEXPR [?]
bytes = op.bytes(line->longM, line->longX);
if (bytes > 1) {
dc(builder, line->operands[0], bytes - 1, line->pc, op);
}
}
}
// strong references
for (auto id : segment->strong_vector) {
if (id) builder.strong(*id);
}
builder.end();
//
OMF::Segment seg;
seg.length = builder.length;
seg.body = std::move(builder.body);
seg.kind = segment->kind;
if (segment->global) seg.kind &= OMF::ATTR_PRIVATE; // public.
seg.banksize = 0x010000;
if (segment->name) seg.segname = *segment->name;
if (segment->segment) seg.loadname = *segment->segment;
return seg;
}