-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.cpp
548 lines (439 loc) · 9.77 KB
/
printer.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <string>
#include "printer.h"
#include "Expression.h"
#include "omf.h"
namespace {
std::string to_x(unsigned x, int bytes, char prefix = 0) {
std::string rv;
rv.reserve(8 + prefix ? 1 : 0);
if (prefix) rv.push_back(prefix);
return rv;
}
const std::string prefix(AddressMode mode) {
switch(mode) {
case kUndefinedAddressMode:
case implied:
case relative:
case relative_long:
case block:
case interrupt:
case stack_relative: // < ?
break;
case stack_relative_y:
return "("; // (< ?
break;
case zp:
case zp_x:
case zp_y:
case zp_relative:
return "<";
break;
case absolute:
case absolute_x:
case absolute_y:
return "|";
break;
case absolute_long:
case absolute_long_x:
return ">";
break;
case immediate:
return "#";
break;
case zp_indirect:
case zp_indirect_x:
case zp_indirect_y:
case zp_indirect_z:
return "(<";
break;
case zp_indirect_long:
case zp_indirect_long_y:
return "[<";
break;
case absolute_indirect:
case absolute_indirect_x:
return "(|";
break;
case absolute_indirect_long:
return "[|"; // ??
break;
default:
return "";
}
return "";
}
std::string suffix(AddressMode mode) {
switch(mode)
{
case kUndefinedAddressMode:
case implied:
case relative:
case relative_long:
case block:
case interrupt:
case immediate:
case zp:
case absolute:
case absolute_long:
case zp_relative:
break;
case zp_x:
case absolute_x:
case absolute_long_x:
return ",x";
break;
case zp_y:
case absolute_y:
return ",y";
break;
case zp_indirect:
case absolute_indirect:
return ")";
break;
case zp_indirect_y:
return "),y";
break;
case zp_indirect_z:
return "),z";
break;
case zp_indirect_x:
case absolute_indirect_x:
return ",x)";
break;
case stack_relative:
return ",s";
break;
case stack_relative_y:
return ",s),y";
break;
case absolute_indirect_long:
case zp_indirect_long:
return "]";
break;
case zp_indirect_long_y:
return "],y";
break;
default:
return "";
}
return "";
}
}
printer::~printer() {
}
void printer::print(FILE *f, const Module &m) {
begin(f);
for (const auto &segment : m.segments) {
begin_segment(f, segment);
for (const auto &line : segment->lines) {
if (line->label) print_label(f, line);
if (line->opcode) print_opcode(f, line);
if (line->directive) print_data(f, line);
}
end_segment(f, segment);
}
end(f);
}
void printer::begin(FILE *f) {
}
void printer::end(FILE *f) {
}
void printer::begin_segment(FILE *f, const SegmentPtr &segment) {
}
void printer::end_segment(FILE *f, const SegmentPtr &segment) {
}
void printer::print_opcode(FILE *f, const BasicLinePtr &line) {
if (!line->opcode) return;
std::string op;
AddressMode mode = line->opcode.addressMode();
op = prefix(mode);
if (mode != implied) {
op.append(line->operands[0]->to_string());
}
if (mode == block || mode == zp_relative) {
op.push_back(',');
op.append(line->operands[1]->to_string());
}
op.append(suffix(mode));
fprintf(f, " %s %s\n",
line->opcode.toString(),
op.c_str()
);
}
void printer::print_label(FILE *f, const BasicLinePtr &line) {
identifier name = line->label;
if (name) fprintf(f, "%s\n", name->c_str());
}
#pragma mark - mpw
void mpw_printer::begin(FILE *f) {
fprintf(f, " CASE ON\n\n");
}
void mpw_printer::end(FILE *f) {
fprintf(f, " END\n");
}
void mpw_printer::begin_segment(FILE *f, const SegmentPtr &segment) {
identifier name = segment->name;
const char *type;
int attr = segment->kind & 0xff00;
int kind = segment->kind & 0x00ff;
switch (kind) {
case OMF::KIND_DATA:
type = "RECORD";
break;
case OMF::KIND_CODE:
default:
type = "PROC";
break;
case OMF::KIND_INIT:
type = "INIT";
break;
case OMF::KIND_DP:
type = "STACKDP";
break;
}
// todo -- align, org, skip included w/ header line
int diff = attr ^ seg_attr;
std::string pa;
if (attr & OMF::ATTR_PRIVATE) {
pa = "ENTRY";
} else {
pa = "EXPORT";
}
if (attr & OMF::ATTR_SKIP) {
pa += ",SKIP";
}
// todo -- align, org
attr &= ~(OMF::ATTR_SKIP | OMF::ATTR_PRIVATE);
if (diff) {
std::string sa;
if (diff & OMF::ATTR_RELOAD) {
if (attr & OMF::ATTR_RELOAD) {
sa += "RELOAD";
} else {
sa += "NORELOAD";
}
}
// todo BANK xx / NOBANK
if (diff & OMF::ATTR_SPECMEM) {
if (attr & OMF::ATTR_SPECMEM) {
sa += ",SPECIAL";
} else {
sa += ",NOSPECIAL";
}
}
if (diff & OMF::ATTR_DYNAMIC) {
if (attr & OMF::ATTR_DYNAMIC) {
sa += ",DYNAMIC";
} else {
sa += ",STATIC";
}
}
fprintf(f, " SEGATTR %s\n", sa.c_str());
seg_attr = attr;
}
fprintf(f, "%s %s %s\n", name ? name->c_str() : "", type, pa.c_str());
}
void mpw_printer::end_segment(FILE *f, const SegmentPtr &segment) {
int kind = segment->kind & 0x00ff;
const char *type;
switch (kind) {
case OMF::KIND_DATA:
type = "ENDR";
break;
case OMF::KIND_CODE:
default:
type = "ENDP";
break;
case OMF::KIND_INIT:
type = "ENDI";
break;
case OMF::KIND_DP:
type = "ENDS";
break;
}
fprintf(f, " %s\n\n", type);
}
void mpw_printer::print_data(FILE *f, const BasicLinePtr &line) {
if (!line->directive) return;
if (!line->operands[0]) return;
std::string s = line->operands[0]->to_string();
switch(line->directive) {
case DCB:
fprintf(f, " dc.b %s\n", s.c_str());
break;
case DCW:
fprintf(f, " dc.w %s\n", s.c_str());
break;
case DCL:
fprintf(f, " dc.l %s\n", s.c_str());
break;
case DS:
fprintf(f, " ds.b %s\n", s.c_str());
break;
default:
return;
}
}
#pragma mark - merlin
void merlin_printer::begin(FILE *f) {
fprintf(f, " xc\n");
fprintf(f, " xc\n");
fprintf(f, "\n");
}
void merlin_printer::end(FILE *f) {
}
void merlin_printer::begin_segment(FILE *f, const SegmentPtr &segment) {
identifier name = segment->name;
fprintf(f, "%s\n", name ? name->c_str() : "");
}
void merlin_printer::end_segment(FILE *f, const SegmentPtr &segment) {
}
void merlin_printer::print_data(FILE *f, const BasicLinePtr &line) {
if (!line->directive) return;
if (!line->operands[0]) return;
std::string s = line->operands[0]->to_string();
switch(line->directive) {
case DCB:
fprintf(f, " db %s\n", s.c_str());
break;
case DCW:
fprintf(f, " dw %s\n", s.c_str());
break;
case DCL:
fprintf(f, " adrl %s\n", s.c_str());
break;
case DS:
fprintf(f, " ds %s\n", s.c_str());
break;
default:
return;
}
}
#pragma mark - orca
void orca_printer::begin(FILE *f) {
fprintf(f, " CASE ON\n\n");
}
void orca_printer::print_label(FILE *f, const BasicLinePtr &line) {
identifier name = line->label;
if (name) fprintf(f, "%s anop\n", name->c_str());
}
void orca_printer::begin_segment(FILE *f, const SegmentPtr &segment) {
identifier name = segment->name;
const char *type;
int attr = segment->kind & 0xff00;
int kind = segment->kind & 0x00ff;
switch (kind) {
case OMF::KIND_DATA:
type = attr & OMF::ATTR_PRIVATE ? "PRIVDATA" : "DATA";
break;
case OMF::KIND_CODE:
default:
type = attr & OMF::ATTR_PRIVATE ? "PRIVATE" : "START";
break;
}
fprintf(f, "%s %s\n", name ? name->c_str() : "", type);
switch(segment->kind) {
case OMF::KIND_DATA:
case OMF::KIND_CODE:
case OMF::KIND_DATA | OMF::ATTR_PRIVATE:
case OMF::KIND_CODE | OMF::ATTR_PRIVATE:
break;
default:
fprintf(f, " KIND $%04x\n", segment->kind);
break;
}
}
void orca_printer::end_segment(FILE *f, const SegmentPtr &segment) {
fprintf(f, " END\n\n");
}
void orca_printer::print_data(FILE *f, const BasicLinePtr &line) {
if (!line->directive) return;
if (!line->operands[0]) return;
std::string s = line->operands[0]->to_string();
switch(line->directive) {
case DCB:
fprintf(f, " dc i1'%s'\n", s.c_str());
break;
case DCW:
fprintf(f, " dc i2'%s'\n", s.c_str());
break;
case DCL:
fprintf(f, " dc i4'%s'\n", s.c_str());
break;
case DS:
fprintf(f, " ds %s\n", s.c_str());
break;
default:
return;
}
}
#pragma mark - orca
void harpoon_printer::begin(FILE *f) {
}
void harpoon_printer::begin_segment(FILE *f, const SegmentPtr &segment) {
identifier name = segment->name;
const char *type = "function";
int kind = segment->kind;
int attr = kind & 0xff00;
switch(kind & 0xff) {
case OMF::KIND_DATA:
type = "data";
break;
case OMF::KIND_CODE:
type = "function";
break;
default:
type = "function";
break;
}
fprintf(f, "%s %s {\n", type, name ? name->c_str() : "");
switch(kind & 0xff) {
case OMF::KIND_CODE:
case OMF::KIND_DATA:
switch (attr) {
case 0:
fprintf(f, " pragma public\n");
break;
case OMF::ATTR_PRIVATE:
fprintf(f, " pragma private\n");
break;
case OMF::ATTR_DYNAMIC:
fprintf(f, " pragma dynamic,public\n");
break;
case OMF::ATTR_PRIVATE | OMF::ATTR_DYNAMIC:
fprintf(f, " pragma dynamic,private\n");
break;
default:
fprintf(f, " pragma kind=$%04x\n", kind);
break;
}
break;
default:
fprintf(f, " pragma kind=$%04x\n", kind);
break;
}
}
void harpoon_printer::end_segment(FILE *f, const SegmentPtr &segment) {
fprintf(f, "}\n\n");
}
void harpoon_printer::print_data(FILE *f, const BasicLinePtr &line) {
if (!line->directive) return;
if (!line->operands[0]) return;
std::string s = line->operands[0]->to_string();
switch(line->directive) {
case DCB:
fprintf(f, " dc.b %s\n", s.c_str());
break;
case DCW:
fprintf(f, " dc.w %s\n", s.c_str());
break;
case DCL:
fprintf(f, " dc.l %s\n", s.c_str());
break;
case DS:
fprintf(f, " ds %s\n", s.c_str());
break;
default:
return;
}
}