-
Notifications
You must be signed in to change notification settings - Fork 0
/
hla.inc
502 lines (405 loc) · 11.6 KB
/
hla.inc
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
#importonce
.filenamespace hla
/*
Supported defines:
HLA_ARITH_COMPARE
Enables unsigned comparison opcodes and the matching HLA structures.
*/
/////////////////////
// Structured code //
/////////////////////
// Storage and types for the structured statement jump-points
.var hla = List()
.struct jumpPoint_s { type, subtype, jump, addr }
// Information about opcodes for jumps/branches
.struct opcodeInfo_s { size }
.var opcodeInfo = Hashtable()
.eval opcodeInfo.put( "bcc", opcodeInfo_s( asmCommandSize(BCC_REL) ))
.eval opcodeInfo.put( "bcs", opcodeInfo_s( asmCommandSize(BCS_REL) ))
.eval opcodeInfo.put( "beq", opcodeInfo_s( asmCommandSize(BEQ_REL) ))
.eval opcodeInfo.put( "bne", opcodeInfo_s( asmCommandSize(BNE_REL) ))
.eval opcodeInfo.put( "bmi", opcodeInfo_s( asmCommandSize(BMI_REL) ))
.eval opcodeInfo.put( "bpl", opcodeInfo_s( asmCommandSize(BPL_REL) ))
.eval opcodeInfo.put( "bvc", opcodeInfo_s( asmCommandSize(BVC_REL) ))
.eval opcodeInfo.put( "bvs", opcodeInfo_s( asmCommandSize(BVS_REL) ))
.eval opcodeInfo.put( "jmp", opcodeInfo_s( asmCommandSize(JMP_ABS) ))
.eval opcodeInfo.put( "jsr", opcodeInfo_s( asmCommandSize(JSR_ABS) ))
// Helper to aid in constructing and pushing a new entry onto the hla stack
.macro push_hla( type, subtype, jump, addr ) {
.eval hla.add( jumpPoint_s(type, subtype, jump, addr) )
}
// ====================================
// Add missing stack handling functions
// ====================================
.function pop(stk) {
.var last = stk.size()-1
.errorif last < 0, "Invalid scoping reached"
.var item = stk.get(last)
.eval stk.remove(last)
.return item
}
.function top(stk) {
.var last = stk.size()-1
.errorif last < 0, "Invalid scoping reached"
.var item = stk.get(last)
.return item
}
// Pull from the names stack, until the named type is found.
// Returns a stack of all popped items in reverse order.
.function search_type(inStk, type, subtype) {
.var stack = List();
// Pull from the HLA stack until we get the type req'd - save everything we get
.var top = pop(inStk)
.while (top.type != type || top.subtype != subtype) {
.eval stack.add(top)
.errorif (inStk.size() == 0), "Unable to find " + type + "/" + subtype
.eval top = pop(inStk)
}
.eval stack.add(top)
.return stack
}
// ============================
// Placement of jump statements
// ============================
// Place the named jump statement at the current pc
.macro jump_stmt(jump, target) {
// Save the current pc
.var savePc = *
// Attempt to place the instruction to jump to the target
.if (jump == "bcc") bcc target
.if (jump == "bcs") bcs target
.if (jump == "beq") beq target
.if (jump == "bne") bne target
.if (jump == "bmi") bmi target
.if (jump == "bpl") bpl target
.if (jump == "bvc") bvc target
.if (jump == "bvs") bvs target
.if (jump == "jmp") jmp target
.if (jump == "jsr") jsr target
#if HLA_ARITH_COMPARE
// Support the 4 additional arithmetic unsigned comparison branches
.if (jump == "blt") blt target
.if (jump == "bgt") bgt target
.if (jump == "ble") ble target
.if (jump == "bge") bge target
#endif
// Validate that the jump statement was handled as the pc will have advanced from
// the saved position.
.errorif savePc == *, "Unsupported jump/branch statement: " + jump
}
// Place the named jump statement at the requested placement point.
// Will fail if it overlaps an existing statement (ie if you didn't use reserve_pc or equivalent).
.macro place_jump_stmt(placement, jump, target)
{
.var savePc = *
* = placement
jump_stmt( jump, target )
* = savePc
}
// Reserve space in the instruction stream for a later jump statement
// to be inserted - unfortunately, there doesn't appear to be a way
// for KickAss to allow overwrites, so a gap is left instead.
.macro reserve_pc(gap)
{
* = * + gap
}
.macro @add_patch_point(jump, type, subtype) {
.errorif !opcodeInfo.containsKey(jump), "Unsupported/unknown opcode: " + jump
push_hla( type, subtype, jump, * )
.var info = opcodeInfo.get(jump)
reserve_pc( info.size )
}
.macro @verify_structure() {
.if (hla.size() > 0) {
.error "There's something left over on the HLA stack:"
.var last = hla.size()-1
.for (var i = 0; i < last; i++) {
.print "- " + hla.get(i).type + "/" + hla.get(i).subtype
}
}
}
// ======================
// IF_xx...[ELSE]...ENDIF
// ======================
.pseudocommand @if_eq {
_if_eq:
add_patch_point("bne", "if", "if")
}
.pseudocommand @if_zero {
_if_zero:
add_patch_point("bne", "if", "if")
}
.pseudocommand @if_ne {
_if_ne:
add_patch_point("beq", "if", "if")
}
.pseudocommand @if_not_zero {
_if_not_zero:
add_patch_point("beq", "if", "if")
}
.pseudocommand @if_plus {
_if_plus:
add_patch_point("bmi", "if", "if")
}
.pseudocommand @if_minus {
_if_minus:
add_patch_point("bpl", "if", "if")
}
.pseudocommand @if_c_set {
_if_c_set:
add_patch_point("bcc", "if", "if")
}
.pseudocommand @if_c_clr {
_if_c_clr:
add_patch_point("bcs", "if", "if")
}
.pseudocommand @if_v_set {
_if_v_set:
add_patch_point("bvc", "if", "if")
}
.pseudocommand @if_v_clr {
_if_v_clr:
add_patch_point("bvs", "if", "if")
}
#if HLA_ARITH_COMPARE
// Support the 4 additional arithmetic unsigned comparison branches
.pseudocommand @if_lt {
_if_lt:
add_patch_point("bge", "if", "if")
}
.pseudocommand @if_gt {
_if_gt:
add_patch_point("ble", "if", "if")
}
.pseudocommand @if_le {
_if_le:
add_patch_point("bgt", "if", "if")
}
.pseudocommand @if_ge {
_if_ge:
add_patch_point("blt", "if", "if")
}
#endif
.pseudocommand @else {
.var stack = search_type(hla, "if", "if")
// Need a place for the endif to place a jmp for the end of the 'if'
add_patch_point("jmp", "if", "else")
_else:
.while (stack.size() > 0) {
.var top = pop(stack)
.if (top.type == "if") {
.errorif top.subtype == "else", "Cannot have multiple else statements for a single if"
.if (top.subtype == "if" || top.subtype == "test") {
place_jump_stmt(top.get("addr"), top.get("jump"), *)
} else {
.error "Unknown statement subtype: " + top.subtype
}
} else { // Don't deal with this here
.eval hla.add(top)
}
}
}
.pseudocommand @endif {
.var stack = List()
.var top = pop(hla)
.while (top.type != "if" || (top.subtype != "if" && top.subtype != "else")) {
.eval stack.add(top)
.eval top = pop(hla)
}
.eval stack.add(top)
_endif:
.while (stack.size() > 0) {
.eval top = pop(stack)
.if (top.type == "if") {
.if (top.subtype == "if" || top.subtype == "test" || top.subtype == "else") {
place_jump_stmt(top.get("addr"), top.get("jump"), *)
} else {
.error "Unknown statement subtype: " + top.subtype
}
} else { // Don't deal with this here
.eval hla.add(top)
}
}
}
// =====================================
// DO...[WHILE_xx|EXIT_LOOP]...LOOP[_xx]
// =====================================
.pseudocommand @do {
_do:
push_hla( "loop", "do", "", *)
// DO doesn't have a jump command. The entry is just used to record a target for another
// jump from somewhere else, ie from the tail end of the loop.
}
.pseudocommand @exit_loop {
_exit_loop:
add_patch_point("jmp", "loop", "test")
}
.pseudocommand @while_eq {
_while_eq:
add_patch_point("bne", "loop", "test")
}
.pseudocommand @while_zero {
_while_zero:
add_patch_point("bne", "loop", "test")
}
.pseudocommand @while_ne {
_while_ne:
add_patch_point("beq", "loop", "test")
}
.pseudocommand @while_not_zero {
_while_not_zero:
add_patch_point("beq", "loop", "test")
}
.pseudocommand @while_plus {
_while_plus:
add_patch_point("bmi", "loop", "test")
}
.pseudocommand @while_minus {
_while_minus:
add_patch_point("bpl", "loop", "test")
}
.pseudocommand @while_c_set {
_while_c_set:
add_patch_point("bcc", "loop", "test")
}
.pseudocommand @while_c_clr {
_while_c_clr:
add_patch_point("bcs", "loop", "test")
}
.pseudocommand @while_v_set {
_while_v_set:
add_patch_point("bvc", "loop", "test")
}
.pseudocommand @while_v_clr {
_while_v_clr:
add_patch_point("bvs", "loop", "test")
}
#if HLA_ARITH_COMPARE
// Support the 4 additional arithmetic comparison branches
.pseudocommand @while_lt {
_while_lt:
add_patch_point("bge", "loop", "test")
}
.pseudocommand @while_gt {
_while_gt:
add_patch_point("ble", "loop", "test")
}
.pseudocommand @while_le {
_while_le:
add_patch_point("bgt", "loop", "test")
}
.pseudocommand @while_ge {
_while_ge:
add_patch_point("blt", "loop", "test")
}
#endif
// Helper macro to provide all the machinery to link back to the top
// of the loop, and to patch all of the while/exit loop statements
// to after the loop.
.macro @terminate_loop(jump) {
.var stack = search_type(hla, "loop", "do")
.var top = pop(stack)
// This is the end of the loop, so jump back to the 'DO' location.
jump_stmt( jump, top.get("addr") )
// Now deal with everything else we got from the stack
.while (stack.size() > 0) {
.eval top = pop(stack)
.if (top.type == "loop") {
// Can deal with it, so do so, by placing a jump to the current pc
place_jump_stmt(top.get("addr"), top.get("jump"), *)
} else {
.errorif (top.type == "else"), "else without endif"
.errorif (top.type == "if"), "if without endif"
.errorif (top.type != "loop"), "Internal error - I got " + top.subtype
}
}
}
.pseudocommand @loop {
terminate_loop("jmp")
_loop:
}
.pseudocommand @loop_if_eq {
terminate_loop("beq")
_loop_if_eq:
}
.pseudocommand @loop_if_zero {
terminate_loop("beq")
_loop_if_zero:
}
.pseudocommand @loop_if_ne {
terminate_loop("bne")
_loop_if_ne:
}
.pseudocommand @loop_if_not_zero {
terminate_loop("bne")
_loop_if_not_zero:
}
.pseudocommand @loop_if_plus {
terminate_loop("bpl")
_loop_if_plus:
}
.pseudocommand @loop_if_minus {
terminate_loop("bmi")
_loop_if_minus:
}
.pseudocommand @loop_if_c_set {
terminate_loop("bcs")
_loop_if_c_set:
}
.pseudocommand @loop_if_c_clr {
terminate_loop("bcc")
_loop_if_c_clr:
}
.pseudocommand @loop_if_v_set {
terminate_loop("bvs")
_loop_if_v_set:
}
.pseudocommand @loop_if_v_clr {
terminate_loop("bvc")
_loop_if_v_clr:
}
#if HLA_ARITH_COMPARE
// Support the 4 additional arithmetic unsigned comparison branches
.pseudocommand @loop_if_lt {
terminate_loop("blt")
_loop_if_lt:
}
.pseudocommand @loop_if_gt {
terminate_loop("bgt")
_loop_if_gt:
}
.pseudocommand @loop_if_le {
terminate_loop("ble")
_loop_if_le:
}
.pseudocommand @loop_if_ge {
terminate_loop("bge")
_loop_if_ge:
}
#endif
////////////////////////
// New pseudo opcodes //
////////////////////////
#if HLA_ARITH_COMPARE
// The 4 additional arithmetic unsigned comparison branches
// NOTE: Broken my own rules here - some of these branches are 2 instructions.
.eval opcodeInfo.put( "blt", opcodeInfo_s( asmCommandSize(BCC_REL) ))
.eval opcodeInfo.put( "bgt", opcodeInfo_s( asmCommandSize(BEQ_REL) + asmCommandSize(BCS_REL) ))
.eval opcodeInfo.put( "ble", opcodeInfo_s( asmCommandSize(BEQ_REL) + asmCommandSize(BCC_REL) ))
.eval opcodeInfo.put( "bge", opcodeInfo_s( asmCommandSize(BCS_REL) ))
.pseudocommand @blt target {
bcc target
}
.pseudocommand @bgt target {
beq !+
bcs target
!:
}
.pseudocommand @ble target {
bcc target
beq target
}
.pseudocommand @bge target {
bcs target
}
#endif