-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdisassemble.c
1420 lines (1252 loc) · 43.5 KB
/
disassemble.c
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include "class_private.h"
#include "disassembly.h"
#include "debug.h"
#include "pool_alloc.h"
/* Outline of the disassembly process;
*
* - identify each instruction and the number of arguments it has
*
* - emulate the effect each instruction has on the stack so we can link them together in something like SSA form
*
* - identify the start and end of each "statement", based on when the stack is empty
* (or perhaps by knowing which instructions are supposed to end a statement)
*
* - identify control flow that has an unambiguous source code representation (if then, do until, loop (while|until), try, ... )
*
* - attempt to classify ambiguous control flow (for, do while, else, elseif, continue, exit, ...)
*
* - insert scope objects to describe indentation regions and where additional labels should be inserted (end if, do, finally)
* since scope objects must stack without overlapping, the order of scope object creation is used to reject
* interpretations of control flow with multiple possible representations where this would create an inconsistency.
*
* Hopefully, all conditional branches will be correctly identified. With unconditional goto's remaining only where they
* existed in the original source code.
*/
static const char *endif_label = "end if";
static const char *endchoose_label = "end choose";
static const char *finally_label = "finally";
static const char *do_label = "do";
static void dump_pcode_inst(FILE *fd, struct instruction *inst);
static void printf_instruction(FILE *fd, struct disassembly *disassembly, struct instruction *inst, uint8_t precedence);
#define PUSH(X) stack[(*stack_ptr)++]=X
#define POP() (*stack_ptr>0 ? stack[--(*stack_ptr)] : NULL)
#define PEEK(I) (*stack_ptr>(I) ? stack[(*stack_ptr) - (I) -1] : NULL)
#define POKE(I,V) if (*stack_ptr>(I)) stack[(*stack_ptr) - (I) -1] = (V)
// emulate the impact each instruction has on the stack
// to discover how each input value is calculated
static void init_stack(struct pool *pool, struct instruction *inst, struct instruction **stack, unsigned *stack_ptr){
inst->stack_count = 0;
inst->stack = NULL;
unsigned i;
unsigned stack_arg = inst->definition->stack_arg;
struct instruction *push_me = inst;
switch(inst->definition->stack_kind){
case stack_unknown:
case stack_none:
return;
// preserve the current top of the stack, discard the next stack_arg items.
case stack_popn:
push_me = POP();
goto stack_result;
case stack_popn_indirect:
push_me = POP();
// fallthrough
// pop stack_arg arguments & push the result of this instruction onto the stack
case stack_result_indirect:
stack_result_indirect:
stack_arg = inst->args[stack_arg];
// fallthrough
case stack_result:
stack_result:
inst->stack_count = stack_arg;
if (stack_arg){
inst->stack = pool_alloc_array(pool, struct instruction*, stack_arg);
for (i=0;i<stack_arg;i++)
inst->stack[i]=POP();
}
if (push_me)
PUSH(push_me);
break;
// pop stack_arg arguments with no result
case stack_action_indirect:
push_me = NULL;
goto stack_result_indirect;
case stack_action:
push_me = NULL;
goto stack_result;
// convert / free the value at this stack offset
case stack_tweak_indirect:
stack_arg = inst->args[stack_arg];
inst->stack = pool_alloc_type(pool, struct instruction*);
inst->stack_count = 1;
inst->stack[0] = PEEK(stack_arg -1);
POKE(stack_arg -1, inst);
break;
case stack_tweak_indirect1:
stack_arg = inst->args[stack_arg]+1;
inst->stack = pool_alloc_type(pool, struct instruction*);
inst->stack_count = 1;
inst->stack[0] = PEEK(stack_arg -1);
POKE(stack_arg -1, inst);
break;
// duplicate the LHS of an expression, to reuse it on the RHS. eg string +=
case stack_clone_indirect:
stack_arg = inst->args[stack_arg];
inst->stack = pool_alloc_type(pool, struct instruction*);
inst->stack[0] = PEEK(stack_arg -1);
inst->stack_count = 1;
{
// is this close enough?
struct instruction *tmp = POP();
PUSH(inst);
PUSH(tmp);
}
break;
// Argument values are left on the stack so they may be cleaned up after this instruction
// Followed by a popn operation to preserve the result
case stack_peek_result_indirect:
stack_arg = inst->args[stack_arg];
case stack_peek_result:
inst->stack_count = stack_arg;
if (stack_arg){
inst->stack = pool_alloc_array(pool, struct instruction*, stack_arg);
for (i=0;i<stack_arg;i++)
inst->stack[i]=PEEK(i);
}
PUSH(inst);
break;
// an object reference was pushed onto the stack before the argument list
case stack_dotcall:
stack_arg = inst->args[stack_arg];
inst->stack = pool_alloc_array(pool, struct instruction*, stack_arg + 1);
inst->stack_count = stack_arg+1;
inst->stack[0] = PEEK(stack_arg);
for (i=0;i<stack_arg;i++)
inst->stack[i+1]=PEEK(i);
PUSH(inst);
break;
// class & function information was pushed onto the stack last
case stack_classcall:
stack_arg = inst->args[stack_arg];
inst->stack = pool_alloc_array(pool, struct instruction*, stack_arg + 1);
inst->stack_count = stack_arg+1;
inst->stack[0] = POP();
for (i=0;i<stack_arg;i++)
inst->stack[i+1]=PEEK(i);
PUSH(inst);
break;
}
}
#undef PUSH
#undef POP
#undef PEEK
#undef POKE
// insert a lexical scope and update instructions to point to that scope
// a child scope must be completely contained within it's parent scope or the insert will fail
// Make sure to insert unambiguous scopes first, letting jumps that are likely to be actual goto's fall out last.
static struct scope *insert_scope(struct disassembly *disassembly,
struct statement *start, struct statement *indent_start,
struct statement *indent_end, struct statement *end){
assert(start->start_offset < end->end_offset);
assert((indent_start ? 1 : 0) == (indent_end ? 1 : 0));
// Nothing to indent if the range is empty
if (indent_start && indent_start->start_offset > indent_end->end_offset)
indent_start = indent_end = NULL;
struct scope *parent_scope = start->scope;
DEBUGF(DISASSEMBLY, "Attempting to insert scope from %02x to %02x", start->start_offset, end->end_offset);
// if multiple scopes already begin or end here, pop scopes that have a smaller range
DEBUGF(DISASSEMBLY, "Current parent %p (%02x %02x)",
parent_scope,
parent_scope?parent_scope->start->start_offset:0,
parent_scope?parent_scope->end->end_offset:0);
while (parent_scope
&& (
(parent_scope->start == start
&& parent_scope->end->end_offset < end->end_offset)
||
(parent_scope->end == end
&& parent_scope->start->start_offset > start->start_offset)
)){
parent_scope = parent_scope->parent;
DEBUGF(DISASSEMBLY, "Current parent %p (%02x %02x)",
parent_scope,
parent_scope?parent_scope->start->start_offset:0,
parent_scope?parent_scope->end->end_offset:0);
}
// if the parent scope partially overlaps, we can't insert a new scope here
if (parent_scope && parent_scope->end->end_offset < end->end_offset){
DEBUGF(DISASSEMBLY, "Ignoring new scope (%p %02x %02x)", parent_scope, parent_scope->end->end_offset, end->end_offset);
return NULL;
}
{
// check the current scope at the end has the same parent
struct scope *end_scope = end->scope;
while(end_scope
&& end_scope != parent_scope
&& end_scope->end == end){
DEBUGF(DISASSEMBLY, "End scope %p has parent %p", end_scope, end_scope->parent);
end_scope = end_scope->parent;
}
if (end_scope != parent_scope){
DEBUGF(DISASSEMBLY, "Ignoring new scope (%p vs %p %02x %02x)",
parent_scope, end_scope,
end_scope ? end_scope->start->start_offset : 0,
end_scope ? end_scope->end->end_offset : 0);
return NULL;
}
}
struct scope *scope = pool_alloc_type(disassembly->pool, struct scope);
memset(scope, 0, sizeof *scope);
scope->start = start;
scope->indent_start = indent_start;
scope->indent_end = indent_end;
scope->end = end;
scope->parent = parent_scope;
struct statement *ptr = start;
while(ptr){
if (ptr->scope == parent_scope){
ptr->scope = scope;
}else if (ptr->scope && ptr->scope->parent == parent_scope){
DEBUGF(DISASSEMBLY, "Reparent scope %p from %p to %p", ptr->scope, ptr->scope->parent, scope);
ptr->scope->parent = scope;
}
if (ptr == end)
break;
ptr = ptr->next;
}
DEBUGF(DISASSEMBLY, "Scope %p added with parent %p", scope, parent_scope);
return scope;
}
// a forwards jumpfalse might be for_next, do_while or just if_then
static void classify_if_then(struct disassembly *disassembly, unsigned statement_number)
{
struct statement *if_test = disassembly->statements[statement_number];
if (!if_test->branch)
return;
// do ... loop (while|until), the conditional branch jumps back to the start
if (if_test->branch->start_offset < if_test->start_offset){
DEBUGF(DISASSEMBLY, "Inserting scope for do ... loop (while|until)");
struct scope *scope = insert_scope(disassembly, if_test->branch, if_test->branch, if_test->prev, if_test);
if (scope){
if (if_test->type == jump_false)
if_test->type = loop_until;
else
if_test->type = loop_while;
scope->begin_label = do_label;
scope->break_dest = if_test->next;
scope->continue_dest = if_test;
if_test->branch->classified_count++;
return;
}
}
struct statement *prior = if_test->branch->prev;
// do (while|until) ... loop, ends with a jump back to the start
if (if_test->branch->start_offset > if_test->start_offset
&& prior && prior->type == jump_goto){
unsigned dest_offset = prior->end->args[0];
if (dest_offset == if_test->start->offset){
if (if_test->type == jump_false)
if_test->type = do_while;
else
if_test->type = do_until;
// loop body
DEBUGF(DISASSEMBLY, "Inserting scope for do while|until) ... loop ");
struct scope *scope = insert_scope(disassembly, if_test, if_test->next, prior->prev, prior);
assert(scope);
scope->break_dest = if_test->branch;
scope->continue_dest = prior;
prior->type = jump_loop;
prior->branch->classified_count++;
if_test->branch->classified_count++;
return;
}
/* for var = initial_value to end_value [step step_by]
* [body]
* next
*
* is equivalent to;
*
* var = initial_value; goto if_test; step: var++; [OR var += step_by;] if_test: if var <= end_value then
* [body]
* goto step; end if
*/
struct statement *init;
struct statement *step;
struct statement *jmp;
if (if_test->type == jump_false
&& if_test->branch->start_offset > if_test->start_offset
&& statement_number >= 3
&& (init = disassembly->statements[statement_number -3])->start_line_number == if_test->end_line_number
&& dest_offset == (step = disassembly->statements[statement_number -1])->start->offset
&& (jmp = disassembly->statements[statement_number -2])->type == jump_goto
&& jmp->end->args[0] == if_test->start->offset
// with a C style for loop, this would be enough. For PB's basic style we should be more explicit;
// && statement_number -3 == SM_ASSIGN_[TYPE]
// && statement_number -1 == SM_INCR_[TYPE] || SM_ADDASSIGN_[TYPE]
// && same variable in all cases
){
init->type = for_init;
jmp->type = for_jump;
step->type = for_step;
if_test->type = for_test;
if_test->classified_count++;
step->classified_count++;
// loop body
DEBUGF(DISASSEMBLY, "Inserting scope for for next loop");
struct scope *scope = insert_scope(disassembly, init, if_test->next, prior->prev, prior);
assert(scope);
scope->break_dest = if_test->branch;
scope->continue_dest = prior;
prior->type = jump_next;
if_test->branch->classified_count++;
return;
}
}
if (if_test->type == jump_false
&& if_test->branch->start_offset > if_test->start_offset){
// one line if test?
unsigned i=statement_number+1;
// TODO, only skip generated statements? (eg SM_JUMP_1 to SM_RETURN_0)
while(i < disassembly->statement_count && disassembly->statements[i]->end_line_number == if_test->start_line_number)
i++;
if (i>statement_number+1 && if_test->branch == disassembly->statements[i]){
DEBUGF(DISASSEMBLY, "Inserting scope for inline if");
struct scope *scope = insert_scope(disassembly, if_test, NULL, NULL, prior);
assert(scope);
if_test->type = if_then;
if_test->branch->classified_count++;
return;
}
}
if(if_test->type == jump_true
&& if_test->branch->start_offset > if_test->start_offset
&& disassembly->statements[disassembly->statement_count -1]->end_line_number == if_test->start_line_number){
// guessing that this looks like generated code to return message.returnvalue at the end of an event
// TODO more explicit matching of the expected code sequence.
unsigned i;
if_test->branch->classified_count++;
for(i=statement_number; i<disassembly->statement_count; i++){
disassembly->statements[i]->type = generated;
}
return;
}
// plain if test?
if (if_test->type == jump_false
&& if_test->branch->start_offset > if_test->start_offset){
struct statement *indent_end = prior;
const char *end_label = endif_label;
if_test->type = if_then;
if (if_test->end->stack[0]->definition->id == SM_CATCH_EXCEPTION_0){
if_test->type = exception_catch;
end_label = NULL;
if (if_test->branch->end->definition->id == SM_GOSUB_1)
indent_end = if_test->branch->branch->prev;
}
DEBUGF(DISASSEMBLY, "Inserting scope for plain if test");
struct scope *scope = insert_scope(disassembly, if_test, if_test->next, indent_end, indent_end);
assert(scope);
if_test->branch->classified_count++;
scope->end_label = end_label;
return;
}
}
static void link_destinations(struct disassembly *disassembly){
unsigned i;
// find the destination of each jump
fflush(stdout);
for (i=0;i<disassembly->statement_count;i++){
struct statement *ptr = disassembly->statements[i];
switch(ptr->type){
case exception_try:{
DEBUGF(DISASSEMBLY, "Try?");
unsigned catch_offset = ptr->end->args[0];
unsigned end_offset = ptr->end->args[1];
struct statement *catch_block=NULL, *end=NULL;
struct statement *finally=NULL, *end_finally=NULL;
unsigned j;
// TODO binary search?
for (j=i+1;j<disassembly->statement_count;j++){
if (disassembly->statements[j]->start_offset == catch_offset){
catch_block = disassembly->statements[j];
}
if (disassembly->statements[j]->start_offset == end_offset){
end = disassembly->statements[j];
}
if (disassembly->statements[j]->end->definition->id == SM_JUMP_1
&& disassembly->statements[j]->end->args[0] == end_offset)
disassembly->statements[j]->type = generated;
}
assert(catch_block && end);
struct statement *try_end = catch_block->prev;
if (end->end->definition->id == SM_GOSUB_1){
DEBUGF(DISASSEMBLY, "has finally?");
unsigned finally_offset = end->end->args[0];
for (j=i+1;j<disassembly->statement_count;j++){
if (disassembly->statements[j]->start->offset == finally_offset){
finally = disassembly->statements[j];
end_finally = end->prev;
if (finally_offset < catch_offset)
try_end = finally->prev;
break;
}
}
end->type = exception_gosub;
end = end->next;
}
assert(end->end->definition->id == SM_POP_TRY_0);
end->type = exception_end_try;
assert(try_end->end->definition->id == SM_JUMP_1);
assert(try_end->end->args[0] == end_offset);
// indent the guarded code (should be some??) with a scope that covers the entire try block
if (ptr != try_end){
DEBUGF(DISASSEMBLY, "Inserting scope for try body");
struct scope *scope = insert_scope(disassembly, ptr, ptr->next, try_end, end);
assert(scope);
}
// scope for the finally block (if any)
if (finally){
DEBUGF(DISASSEMBLY, "Inserting scope for finally");
struct scope *scope = insert_scope(disassembly, finally, finally, end_finally, end_finally);
assert(scope);
scope->begin_label = finally_label;
}
continue;
}
case exception_catch:
assert(ptr->end->definition->id == SM_JUMPFALSE_1);
goto find_dest;
case jump_true:
case jump_false:
case jump_goto:
case loop_while:
case loop_until:
case do_until:
case exception_gosub:
find_dest:
{
unsigned dest_offset = ptr->end->args[0];
unsigned j;
// TODO binary search?
for (j=0;j<disassembly->statement_count;j++){
if (disassembly->statements[j]->start->offset == dest_offset){
ptr->branch = disassembly->statements[j];
ptr->branch->destination_count++;
break;
}
}
if (!ptr->branch){
DEBUGF(DISASSEMBLY, "Branch dest %04x is not the start of a statement (for %s @%04x)", dest_offset, ptr->end->definition->name, ptr->end->offset);
continue;
}
} break;
default:
continue;
}
if (ptr->type == jump_goto
&& ptr->branch->end->definition->id == SM_RETURN_0
&& ptr->prev
&& ptr->prev->end->definition->id == SM_STORE_RETURN_VAL_1){
ptr->type = generated;
ptr->branch->classified_count++;
}
}
for (i=0; i<disassembly->statement_count; i++){
struct statement *jmp = disassembly->statements[i];
if (jmp->type == jump_true || jmp->type == jump_false)
classify_if_then(disassembly, i);
else if (jmp->type == exception_gosub){
jmp->type = generated;
jmp->branch->classified_count++;
}
}
// work backwards to simplify merging elseif's as we go.
for (i=disassembly->statement_count;i>1;i--){
struct statement *jmp = disassembly->statements[i - 1];
if (jmp->type == choose_case){
// find the end of the if then else chain
struct statement *start_indent = jmp->next;
struct statement *end_statement = start_indent;
assert(end_statement->type == if_then);
// follow the first else to the end label
end_statement = end_statement->branch;
if (end_statement->prev->type == jump_else || end_statement->prev->type == jump_elseif){
end_statement = end_statement->prev->branch;
}// else only a single case
// insert a new scope to add another layer of indent
DEBUGF(DISASSEMBLY, "Inserting scope for choose case");
struct scope *scope = insert_scope(disassembly, jmp, start_indent, end_statement->prev, end_statement->prev);
if(scope){
// now mark all the if tests and elseif's
struct statement *ptr = start_indent;
struct scope *endif_scope = ptr->scope;
ptr->type = case_if;
ptr=ptr->branch;
while(ptr
&& ptr->type == if_then
&& ptr->prev->type == jump_elseif
&& ptr->prev->branch == end_statement){
endif_scope = ptr->scope;
ptr->type = case_if;
ptr->prev->type = generated;
ptr = ptr->branch;
}
if (endif_scope->end_label == endif_label){
// empty else, or no else at all
endif_scope->end_label = NULL;
}else if (ptr->prev->type == jump_else
&& ptr->prev->scope->end_label == endif_label){
ptr->prev->scope->end_label = NULL;
}
if (ptr->prev->type == jump_else){
ptr->prev->type = case_else;
}
scope->end_label = endchoose_label;
}else{
// Huh?
}
}
if (jmp->type == jump_goto && jmp->branch){
// try to identify else's and elseif's...
struct scope *if_scope = jmp->scope;
if (if_scope
&& jmp->start->args[0] > jmp->start_offset
&& if_scope->start->type == if_then
&& if_scope->end_label == endif_label
&& if_scope->indent_end == jmp){
struct statement *nxt = jmp->next;
if (nxt->type == if_then
&& nxt->branch
&& nxt->start_line_number == jmp->start_line_number){
struct statement *possible_else = nxt->branch->prev;
if ((possible_else->type == jump_else || possible_else->type == jump_elseif)
&& possible_else->branch == jmp->branch){
// elseif with another else or elseif
jmp->type = jump_elseif;
}else if(nxt->branch == jmp->branch){
// last elseif with no else
jmp->type = jump_elseif;
}
}
// still unclassified?
if (jmp->type == jump_goto){
// TODO check if a continue could be a better choice based on which blank line the end if will be placed on
// empty else? special case, leave the end if where it is but don't indent the else
if (nxt == jmp->branch){
jmp->type = jump_else;
jmp->branch->classified_count ++;
if_scope->indent_end = jmp->prev;
continue;
}else{
// else?
// (the range doesn't include the else, or the insert would fail)
DEBUGF(DISASSEMBLY, "Inserting scope for else block");
struct scope *scope = insert_scope(disassembly, nxt, nxt, jmp->branch->prev, jmp->branch->prev);
if (scope){
scope->end_label = endif_label;
jmp->type = jump_else;
}
}
}
// any of the above, shrink the if test scope to exclude the else and drop the end if label
if (jmp->type != jump_goto){
jmp->branch->classified_count ++;
if_scope->indent_end = jmp->prev;
if_scope->end_label = NULL;
continue;
}
}
// try to reclassify goto's as continue, exit
{
struct scope *scope = jmp->scope;
struct statement *pop_try = jmp->prev;
while(scope){
if (scope->start->type == exception_try){
if (pop_try->start->definition->id == SM_POP_TRY_0){
// skip exception scopes only if the previous instruction will pop out of it.
pop_try->type = generated;
pop_try = pop_try->prev;
scope = scope->parent;
continue;
}
break;
}else if(scope->continue_dest)
// skip any non-loop scopes (without an exit location)
break;
scope = scope->parent;
}
if (scope && jmp->branch == scope->break_dest){
jmp->type = jump_exit;
jmp->branch->classified_count++;
continue;
}
if (scope && jmp->branch == scope->continue_dest){
jmp->type = jump_continue;
jmp->branch->classified_count++;
continue;
}
}
}
}
}
struct disassembly *disassemble(struct class_group *group, struct class_definition *class_def, struct script_definition *script){
struct script_def_private *script_def = (struct script_def_private *)script;
if (!script_def || !script_def->body || !script_def->body->code)
return NULL;
struct pcode_def **opcodes = NULL;
unsigned max_opcode = 0;
{
struct class_group_private *group_def = (struct class_group_private *)group;
switch (group_def->header.compiler_version){
#define CODE(X) case X: opcodes = X ## _opcodes; max_opcode = X ## _maxcode; break
CODE(PB50);
CODE(PB80);
CODE(PB90);
CODE(PB100);
CODE(PB105);
CODE(PB120);
default:
CODE(PB150);
#undef CODE
}
if (!opcodes)
return NULL;
}
struct pool *pool = pool_create();
struct disassembly *disassembly = pool_alloc_type(pool, struct disassembly);
memset(disassembly, 0, sizeof(struct disassembly));
disassembly->pool = pool;
disassembly->group = group;
disassembly->class_def = class_def;
disassembly->script = script;
unsigned offset=0;
#define MAX_STACK 128
struct instruction *stack[MAX_STACK];
unsigned stack_ptr=0;
struct statement *first_statement = NULL, *prev_statement = NULL, *statement = NULL;
unsigned debug_line=0;
unsigned instruction_count=0;
// temp space on the stack, can't be more than this; will usually be much less.
struct instruction *instructions[script_def->body->code_size/2];
while(offset < script_def->body->code_size){
if (debug_line+1 < script_def->body->debugline_count
&& offset >= script_def->body->debug_lines[debug_line+1].pcode_offset)
debug_line++;
const uint16_t *pc = (const uint16_t*)&script_def->body->code[offset];
uint16_t opcode = pc[0];
assert(opcode < max_opcode);
struct instruction *inst = instructions[instruction_count++] = pool_alloc_type(pool, struct instruction);
memset(inst, 0, sizeof(struct instruction));
inst->offset = offset;
inst->opcode = opcode;
inst->definition = opcodes[opcode];
assert(inst->definition);
inst->args = pc+1;
inst->line_number = script_def->body->debug_lines[debug_line].line_number;
offset += (1+inst->definition->args)*2;
init_stack(pool, inst, stack, &stack_ptr);
if (!statement){
disassembly->statement_count++;
statement = pool_alloc_type(pool, struct statement);
memset(statement, 0, sizeof *statement);
if (!first_statement){
first_statement = statement;
}else{
prev_statement->next = statement;
statement->prev = prev_statement;
}
statement->start = inst;
statement->start_line_number = inst->line_number;
statement->end_line_number = inst->line_number;
statement->start_offset = inst->offset;
statement->type=expression;
inst->begin = 1;
prev_statement = statement;
}else{
inst->begin = 0;
if (inst->line_number < statement->start_line_number)
statement->start_line_number = inst->line_number;
if (inst->line_number > statement->end_line_number)
statement->end_line_number = inst->line_number;
}
statement->end = inst;
statement->end_offset = offset -1;
// first guess at flow control structure
if (statement->type == expression){
switch(inst->definition->id){
//case SM_GOSUB_1:
//case SM_POP_TRY_0: // only the last one will end up as exception_end_try
case SM_RETURN_SUB_0:
statement->type=generated;
break;
case SM_JUMP_1:
statement->type=jump_goto;
break;
case SM_JUMPTRUE_1:
statement->type=jump_true;
break;
case SM_JUMPFALSE_1:
statement->type=jump_false;
break;
case SM_PUSH_TRY_2:
statement->type=exception_try;
break;
case SM_PUSH_LOCAL_VAR_LV_1:{
unsigned var = inst->args[0];
assert(var<disassembly->script->local_variable_count);
// temporary case variables are named like "\x01case" LINE_NO
if (*disassembly->script->local_variables[var]->name == 0x01)
statement->type = choose_case;
break;
}
case SM_ASSIGN_BLOB_1:
case SM_ASSIGN_STRING_1:{
struct instruction *cat;
if (inst->stack_count == 2
&& ((cat = inst->stack[0])->definition->id == SM_CAT_STRING_0
|| cat->definition->id == SM_CAT_BINARY_0)
&& cat->stack_count == 2
&& cat->stack[1]->definition->id == SM_DUP_STACKED_LVALUE_1
)
statement->type=mem_append;
}break;
}
}
if (stack_ptr==0){
// end of statement
inst->end = 1;
statement = NULL;
}else
inst->end = 0;
if (IFDEBUG(DISASSEMBLY)){
fflush(stdout);
dump_pcode_inst(stderr, inst);
fprintf(stderr," [");
printf_instruction(stderr, disassembly, inst, 0);
fprintf(stderr,"]\n");
}
}
if (stack_ptr){
fflush(stdout);
WARNF("Stack pointer (%u) is not zero at the end of %s!", stack_ptr, script->name);
}
disassembly->instruction_count = instruction_count;
disassembly->instructions = pool_alloc_array(pool, struct instruction *, instruction_count+1);
memcpy(disassembly->instructions, instructions, instruction_count * sizeof(struct instruction *));
disassembly->instructions[instruction_count]=NULL;
// do we really want to fix this now? or insert goto destination, "do" & "end if" labels first?
disassembly->statements = pool_alloc_array(pool, struct statement*, disassembly->statement_count+1);
{
struct statement *ptr = first_statement;
unsigned i;
for (i=0;i<disassembly->statement_count;i++){
assert(ptr);
disassembly->statements[i] = ptr;
ptr = ptr->next;
}
assert(!ptr);
disassembly->statements[disassembly->statement_count]=NULL;
if (disassembly->statement_count){
// pre 10.5, there's always an extra return at the end.
// post 10.5, the code always sets the return value and jumps to the end
ptr = disassembly->statements[disassembly->statement_count-1];
if (ptr->end->definition->id == SM_RETURN_0 || ptr->end->definition->id == SM_RETURN_2)
ptr->type=generated;
}
link_destinations(disassembly);
}
return disassembly;
}
static void dump_pcode_inst(FILE *fd, struct instruction *inst){
fprintf(fd, "%04x: %04x ", inst->offset, inst->opcode);
unsigned arg;
for(arg = 0; arg < inst->definition->args; arg++)
fprintf(fd, "%04x ", inst->args[arg]);
for(;arg < 6; arg++)
fprintf(fd, " ");
fprintf(fd, "%s [%u](", inst->definition->name, inst->definition->precedence);
for (arg=0; arg < inst->stack_count; arg++){
if (arg>0)
fprintf(fd, ", ");
if (inst->stack && inst->stack[arg])
fprintf(fd, "%04x %s [%u]", inst->stack[arg]->offset, inst->stack[arg]->definition->name, inst->stack[arg]->definition->precedence);
else
fprintf(fd, "NULL");
}
fprintf(fd, ")");
if (inst->begin)
fprintf(fd, " BEGIN");
if (inst->end)
fprintf(fd, " END");
}
void dump_pcode(FILE *fd, struct disassembly *disassembly){
unsigned i;
for(i=0;i<disassembly->instruction_count;i++){
struct instruction *inst = disassembly->instructions[i];
dump_pcode_inst(fd, inst);
fprintf(fd, "\n");
}
}
/*
static void dump_instruction(FILE *fd, struct instruction *inst){
if (!inst){
fprintf(fd, "***NULL***");
return;
}
unsigned i;
fprintf(fd, "%s(", inst->definition->name);
for (i=0; i < inst->stack_count; i++){
if (i>0)
fprintf(fd, ", ");
dump_instruction(fd, inst->stack[i]);
}
fprintf(fd, ")");
}
*/
static void printf_instruction(FILE *fd, struct disassembly *disassembly, struct instruction *inst, uint8_t precedence){
if (!inst){
fprintf(fd, "***NULL***");
return;
}
unsigned i;
const char **tokens=inst->definition->tokens;
struct class_group_private *group = (struct class_group_private *)disassembly->group;
//struct class_def_private *class_def = (struct class_def_private *)disassembly->class_def;
struct script_def_private *script = (struct script_def_private *)disassembly->script;
uint8_t this_precedence = inst->definition->precedence;
if (!*tokens){
fputs(inst->definition->name, fd);
if (inst->definition->args){
fputc('[', fd);
for (i=0; i < inst->definition->args; i++){
if (i>0)
fputs(", ", fd);
fprintf(fd, "%04x", inst->args[i]);
}
fputc(']', fd);
}
fputc('(', fd);
for (i=0; i < inst->stack_count; i++){
if (i>0)
fputs(", ", fd);
printf_instruction(fd, disassembly, inst->stack[inst->stack_count - i - 1], this_precedence);
}
fputc(')', fd);
if (inst->end)
fputc(';', fd);
return;
}
if (precedence && precedence < this_precedence)
fputs("(", fd);
while(*tokens){
//fprintf(fd, "\n%s %p %u\n", inst->definition->name, *tokens, inst->stack_count);
switch((enum token_types)(*tokens)){
case STACK:{
i=(int)(*(++tokens));
assert(i<inst->stack_count);
uint8_t p = this_precedence;
// decrement precendence to detect left to right rule violation, eg a - (b + c)
if (p && inst->stack_count==2 && i==0)
p--;
printf_instruction(fd, disassembly, inst->stack[i], p);
break;
}
case STACK_CSV:
for (i=0; i < inst->stack_count; i++){
if (i>0)
fputs(", ", fd);
printf_instruction(fd, disassembly, inst->stack[inst->stack_count - i - 1], this_precedence);
}
break;
case STACK_DOT_CSV:
for (i=1; i < inst->stack_count; i++){
if (i>1)
fputs(", ", fd);
printf_instruction(fd, disassembly, inst->stack[inst->stack_count - i], this_precedence);
}
break;
case LOCAL:{
i = (int)(*(++tokens));
assert(i < inst->definition->args);
unsigned var = inst->args[i];
assert(var<disassembly->script->local_variable_count);
fputs(disassembly->script->local_variables[var]->name, fd);
}break;
case SHARED: