-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecompiler.cpp
2953 lines (2481 loc) · 73.1 KB
/
decompiler.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
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
// Copyright 2004-2005 Ludvig Strigeus <[email protected]>
// GNU GPL LICENSE
#include <ida.hpp>
#include <idp.hpp>
#include <typeinf.hpp>
#include <struct.hpp>
#define WITH_CONSTRUCTORS
#include "default.h"
#include "insn.h"
#include "exp.h"
using namespace std;
#define cmd foo
void DumpRS(const RegState &rs);
bool IsBB(BasicBlock *list, BasicBlock *bb);
BasicBlock *Analyzer::NewBasicBlock()
{
// otherwise allocate
BasicBlock *bb = new BasicBlock;
memset(bb, 0, sizeof(BasicBlock));
bb->next = _list->next;
_list->next = bb;
bb->base = 0;
return bb;
}
BasicBlock *Analyzer::GetBasblockAt(ea_t from)
{
BasicBlock *bb, **bbp = &_list;
// first see if it exists already? the list is sorted in address order
while ( (bb = *bbp) != NULL) {
if (from <= bb->base) {
if (from == bb->base)
return bb;
break;
}
bbp = &bb->next;
}
// otherwise allocate
bb = new BasicBlock;
memset(bb, 0, sizeof(BasicBlock));
bb->next = *bbp;
*bbp = bb;
bb->base = from;
return bb;
}
void Analyzer::DestroyBB(BasicBlock *del)
{
// msg("Destroying %x\n",del->base);
BasicBlock *bb, **bbp = &_list;
while ( (bb = *bbp) != del) { bbp = &bb->next; }
*bbp = bb->next;
// delete del;
}
struct JmpDst {
ea_t ea; // address of jump instruction
JmpDst *next; // next jmp instruction
};
static bool InstructionEndsFlow(ArmIns &ins)
{
// unconditional jumps
if (ins.mnem == O_BX || ins.mnem == O_B)
return true;
// assignment to PC
if (ins.mnem == O_MOV && ins.op1.type == O_REG && ins.op1.reg == 15)
return true;
if (ins.mnem == O_POP && ins.op1.value & (1 << 15))
return true;
return false;
}
static bool InsertJmpDst(JmpDst *&first, addr_t dst, Pool &p)
{
// insert into list of jump destinations if it's not there already. the list is sorted in address order.
JmpDst *jd, **jdp = &first;
// add it to the list, sorted in address order.
for(;;) {
if ((jd = *jdp) == NULL || dst < jd->ea) {
jd = (JmpDst*)p.Alloc(sizeof(JmpDst));
jd->ea = dst;
jd->next = *jdp;
*jdp = jd;
return true;
}
if (dst == jd->ea)
break;
jdp = &jd->next;
}
return false;
}
struct SwitchStatement {
SwitchStatement *next;
uint num_edges;
uint reg;
addr_t ea_start; // address of ldr rx, =jmptbl
addr_t ea; // address of mov pc,?? instr
addr_t edges[1]; // edges
int hasdefault;
addr_t defaultadd;
};
static SwitchStatement *GetSwitchAt(SwitchStatement *s, addr_t ea)
{
for(;s;s=s->next)
if (s->ea == ea)
return s;
return NULL;
}
/*Severly old
int CheckSwitchR0(addr_t ea){
//Using R0
// verify that it matches the switch sequence.
//ROM:080003A4 80 00 LSL R0, R0, #2
//ROM:080003A6 02 49 LDR R1, =off_83C173C
//ROM:080003A8 40 18 ADD R0, R0, R1
//ROM:080003AA 00 68 LDR R0, [R0]
//ROM:080003AC 87 46 MOV PC, R0
if (!(get_word(ea) == 0x4687 &&
get_word(ea-2) == 0x6800 &&
get_word(ea-4) == 0x1840 &&
(get_word(ea-6) & 0xff00) == 0x4900 &&
(get_word(ea-8)&~0x38) == 0x0080))
{
return 0;
}
return 1;
}
int CheckSwitchR1(addr_t ea){
char err[1024]={0};
//Using R0
// verify that it matches the switch sequence.
//ROM:0807D4A2 89 00 LSLS R1, R1, #2
//ROM:0807D4A4 01 4A LDR R2, =jpt_807D4AA
//ROM:0807D4A6 89 18 ADDS R1, R1, R2
//ROM:0807D4A8 09 68 LDR R1, [R1]
//ROM:0807D4AA 8f 46 MOV PC, R1
if (!(get_word(ea) == 0x468F &&
get_word(ea-2) == 0x6809 &&
get_word(ea-4) == 0x1889 &&
(get_word(ea-6) & 0xff00) == 0x4A00 &&
(get_word(ea-8)) == 0x0089))
{
return 0;
}
msg("R1 switch obtained!\n");
return 1;
}
*/
int CheckMySwitch(addr_t ea)
{
if ((get_word(ea-0) & 0xFF00) == 0x4600 && //MOV PC, Rx
(get_word(ea-2) & 0xF800) == 0x6800 && //LDR Rx, [Rx]
(get_word(ea-4) & 0xFE00) == 0x1800 && //ADD Rx, Rx, Ry
(get_word(ea-6) & 0xF800) == 0x4800 && //LDR Ry, =jpt
(get_word(ea-8) & 0xFF80) == 0x0080) //LSLS Rx, Rx, #2
{
//Make sure that the registers are correctly setup
return 1;
}
if ((get_word(ea-0) & 0xFF00) == 0x4600 && //MOV PC, Rx
(get_word(ea-2) & 0xF800) == 0x5800 && //LDR Rx, [Ry, Rx]
(get_word(ea-4) & 0xFF80) == 0x0080 && //LSLS Rx, Rx, #2
(get_word(ea-6) & 0xF800) == 0x4800) //LDR Ry, =jpt
{
//Make sure that the registers are correctly setup
return 2;
}
return 0;
}
static SwitchStatement *TryCreateSwitchStatement(SwitchStatement *&ss, addr_t ea, Pool &pool, addr_t funcstart, addr_t funcend,long* addrs)
{
int reg=0;
int jmpSize=4;
addr_t jmptab_addr=0;
addr_t ea_start=0;
ArmIns ins;
uint switch_max=0;
int limit=0;
// first check if a switch statement has already been created, only allow a single one
for(SwitchStatement *s = ss;s;s=s->next)
if (s->ea == ea)
return 0;
// If switch information is present in the database, use it for defaults
switch_info_ex_t si;
if ( get_switch_info_ex(ea, &si, sizeof(si)) > 0 )
{
//msg("si.jumps = %08X\n",si.jumps);
//msg("si.ncases = %08X\n",si.ncases);
//msg("si.jcases = %08X\n",si.jcases);
//msg("si.startea = %08X\n",si.startea);
//msg("si.elbase = %08X\n",si.elbase);
//msg("si.get_jtable_element_size() = %08X\n",si.get_jtable_element_size());
//msg("si.get_shift() = %08X\n",si.get_shift());
//msg("si.flags = %08X\n",si.flags);
//msg("si.values = %08X\n",si.values);
//msg("si.get_vtable_element_size() = %08X\n",si.get_vtable_element_size());
//msg("si.regnum = %08X\n",si.regnum);
//msg("si.regdtyp = %08X\n",si.regdtyp);
//msg("si.defjump = %08X\n",si.defjump);
//msg("si.cb = %08X\n",si.cb);
//msg("si.flags2 = %08X\n",si.flags2);
//msg("si.is_indirect() = %08X\n",si.is_indirect());
//msg("si.is_subtract() = %08X\n",si.is_subtract());
//msg("get_switch_parent(ea) = %08X\n",get_switch_parent(ea));
switch_max = si.get_jtable_size();
jmptab_addr = si.jumps;
reg = si.regnum;
jmpSize = si.get_jtable_element_size();
//msg("get_next_dref_from(jmptab_addr,ea) = %08X\n",get_next_dref_from(jmptab_addr,ea));
//msg("get_next_dref_to(jmptab_addr,ea) = %08X\n",get_next_dref_to(jmptab_addr,ea));
//msg("get_first_dref_to(jmptab_addr) = %08X\n",get_first_dref_to(jmptab_addr));
//msg("get_next_dref_from(jmptab_addr,funcstart) = %08X\n",get_next_dref_from(jmptab_addr,funcstart));
//msg("get_next_dref_to(jmptab_addr,funcstart) = %08X\n",get_next_dref_to(jmptab_addr,funcstart));
addr_t adr_of_ldr = get_next_dref_to(jmptab_addr,funcstart); //LDR Ry, =jpt
if((get_word(adr_of_ldr-2) & 0xFF80) == 0x0080) //LSLS Rx, Rx, #2
ea_start = adr_of_ldr - 2;
else
ea_start = adr_of_ldr;
//This will only work if only one method uses this jump table (Which should be most of the time)
//ea_start = get_first_dref_to(jmptab_addr);
}
else
{
//If IDA PRO doesn't detect the jump table then try to detect it manually
/*
ROM:0807D49C CMP R1, #0x12
ROM:0807D49E BLS JumpTable
ROM:0807D4A0 B loc_807DAD8 @ jumptable 0807D4AA default case
*/
/*
check for tables with limits
2YXX 16 CMP X, Y
d900 14 BLS blah
e000 12 B blah
//*/
//addr_t defaddr=0;
//int hasdefault=0;
//if (get_word(ea-16) & 0x2000 &&
// get_word(ea-14) & 0xd900 &&
// get_word(ea-12) & 0xe000)
//{
// switch_max=(get_word(ea-16)&0xFF);
// hasdefault=1;
// defaddr=(ea-8)+((signed)(get_word(ea-12)&0xFFF) );//0807D4A0 0xE31A B loc_807DAD8
//}
msg("ea = %08X\n",ea);
int jumpTableType = CheckMySwitch(ea);
msg("jumpTableType = %d\n",jumpTableType);
if(jumpTableType == 0)
{
msg("No switches\n");
return 0;
}
int offset1 = ea - 8; //LSLS Rx, Rx, #2
int offset2 = ea - 6; //LDR Ry, =jpt
int offset3 = ea - 10; //BHI Somewhere
int offset4 = ea - 12; //CMP Rx, #xx
ea_start = ea - 8; //Default start address
if(jumpTableType == 2)
{
offset1 = ea - 4;
ea_start = ea - 6;
}
reg = (get_word(offset1) >> 3) & 7; //LSLS Rx, Rx, #2
// indeed it does..
// determine the address of the switch's jumptable
DecodeThumb(ins, offset2);
assert(ins.mnem == O_MOV && ins.op[1].type == O_IMM);
jmptab_addr = ins.op[1].value;
// max number of items in switchtable
// try to determine the number of items in the jumptable
msg("jmptab=%x\nyAt offset %x\n", jmptab_addr,ea);
if(switch_max==0)
{
bool found = false;
//If we didn't have a BLS table earlier..
for(int i=offset2; i>offset2-0x10; i-=2)
{
msg("searching at %08X\n",i);
if ((get_word(i) & 0xFF00) == 0xD800 && //BHI Somewhere
(get_word(i-2) & 0xF800) == 0x2800) //CMP Rx, #xx
{
msg("found switch_max at %08X\n",i-2);
switch_max = get_byte(i-2) + 1;
found = true;
break;
}
else if ((get_word(i) & 0xFF00) == 0xD900 && //BLS JumpTable
(get_word(i-2) & 0xF800) == 0x2800) //CMP Rx, #xx
{
msg("found switch_max at %08X\n",i-2);
switch_max = get_byte(i-2) - 1;
found = true;
break;
}
}
if(!found)
{
for(uint i=0; ; i++) {
addr_t x = get_long(jmptab_addr + i * 4);
if (0==(x & 0x8FFFFFF)||(x|0x8FFFFFF) > (funcend|0x80000000)) {
switch_max = i-1;
break;
}
msg("Address: %x\n", x);
}
}
}
msg("Max: %d\n", switch_max);
// make sure the switch items are inside the function bounds
/*for(uint i=0; i!=switch_max; i++) {
addr_t x = get_long(jmptab_addr + i * 4);
if (x < ea || x > funcend) return 0;
}*/
}
SwitchStatement *s = (SwitchStatement*)pool.Alloc(sizeof(SwitchStatement) + switch_max * sizeof(addr_t));
s->ea = ea;
s->next = ss;
ss = s;
s->num_edges = switch_max;
s->reg = reg;
s->ea_start = ea_start;
for(uint i=0; i!=switch_max; i++) {
s->edges[i] = get_long(jmptab_addr + i * 4);
}
return s;
}
bool Analyzer::CreateSwitchConds(BasicBlock *bb, SwitchStatement *s)
{
int num_jmptbl_lookup_instr = ((s->ea - s->ea_start)/2) + 1;
msg("s->ea = %08X\n", s->ea);
msg("s->ea_start = %08X\n", s->ea_start);
msg("num_jmptbl_lookup_instr = %d\n", num_jmptbl_lookup_instr);
if (bb->num_instr < num_jmptbl_lookup_instr)
return false;
// delete the last 5 instructions...
bb->instr.SetCount(bb->num_instr - num_jmptbl_lookup_instr);
if (s->num_edges == 1) {
// special case.. only a single edge going out?!
bb->flow = GetBasblockAt(s->edges[0]);
bb->flow->ref++;
return true;
}
// otherwise create compare instructions
for(uint i=0; i != s->num_edges - 1; i++) {
MkInstr(bb->instr.Append(), NewCondExp(NewBinExp(E_COMP_EQ, NewRegExp(s->reg), NewConstExp(i))));
bb->cond = GetBasblockAt(s->edges[i]);
bb->cond->ref++;
bb->flow = (i == s->num_edges - 2) ? GetBasblockAt(s->edges[i+1]) : NewBasicBlock();
bb->flow->ref++;
bb = bb->flow;
}
return true;
}
bool Analyzer::AnalyzeOwn(ea_t start, ea_t end)
{
addr_t ea;
ArmIns ins;
JmpDst *first_dst = NULL;
SwitchStatement *switches = NULL;
bool changes;
Pool pool(1000);
_function_base = start;
do {
ea = start;
changes = false;
for(;;) {
if (ea >= end) {
msg("%x: Flow continues beyond function boundary\n", ea);
return false;
}
int len = DecodeThumb(ins, ea);
if (len == 0) {
msg("Unable to decode at %x\n", ea);
return false;
}
// check if the instruction references code in this function?
if (((ins.mnem == O_B || ins.mnem == O_BL) && ins.op[0].value != start || (ins.mnem >= O_BEQ && ins.mnem <= O_BLE)) && ins.op[0].value >= start && ins.op[0].value < end) {
// insert a jump destination.
if (InsertJmpDst(first_dst, ins.op[0].value, pool) && ins.op[0].value <= ea)
changes = true;
}
ea += len;
if (InstructionEndsFlow(ins) || ins.mnem == O_BL && ins.op[0].value > start && ins.op[0].value < end) {
// need to determine if it's a switch statement.
// if it is a switch statement, then jump destinations for the switch statement need to be determined
if (ins.mnem == O_MOV && ins.op[0].reg == 15) {
long addrs;
SwitchStatement *s = TryCreateSwitchStatement(switches, ea - 2, pool, start, end,&addrs);
if (s) {
// it's a switch statement, insert the switch edges into the jmpdst list
for(uint i=0; i!=s->num_edges; i++)
InsertJmpDst(first_dst, s->edges[i], pool);
}
}
// locate the first entrypoint that's >= than "ea" and continue there.
JmpDst *jd = first_dst;
for(;;) {
if (!jd) goto getout; // finished with everything?
if (jd->ea >= ea) { ea = jd->ea; break; }
jd = jd->next;
}
}
}
getout:;
} while (changes);
_list = NULL;
// now we have determined the start address for each basic block.
// do another pass that actually creates the basic blocks and fills them with instructions.
BasicBlock *bb = GetBasblockAt(ea = start);
bool flow;
for(;;) {
assert(ea < end);
int len = DecodeThumb(ins, ea);
if (len == 0) {
msg("Unable to decode at %x\n", ea);
return false;
}
if (((ins.mnem == O_B || ins.mnem == O_BL) && ins.op[0].value != start) && ins.op[0].value >= start && ins.op[0].value < end) {
bb->flow = GetBasblockAt(ins.op[0].value);
bb->flow->ref++;
flow = false;
} else {
CreateInstruction(ins, ea, bb);
ea += len;
// check if to terminate the basblock?
if (ins.mnem >= O_BEQ && ins.mnem <= O_BLE && ins.op[0].value >= start && ins.op[0].value < end && bb->instr[bb->num_instr-1].e->type == E_COND) {
// add a cond to the basblock
bb->cond = GetBasblockAt(ins.op[0].value);
bb->cond->ref++;
flow = true;
} else if (InstructionEndsFlow(ins)) {
flow = false;
} else {
if (!first_dst || ea < first_dst->ea)
continue;
flow = true;
}
}
if (flow) {
if (first_dst) {
if (ea > first_dst->ea) {
msg("AnalyzeOwn: internal error: %x > %x\n", ea, first_dst->ea);
return false;
}
if (ea == first_dst->ea) first_dst = first_dst->next;
}
bb = bb->flow = GetBasblockAt(ea);
bb->ref++;
} else {
// switch statement handling?
if (ins.mnem == O_MOV && ins.op[0].reg == 15) {
// switch statement.
SwitchStatement *s = GetSwitchAt(switches,ea - 2);
if (s && !CreateSwitchConds(bb, s)) {
msg("CreateSwitchConds failure\n");
return false;
}
}
if (!first_dst)
break;
assert(first_dst->ea >= ea);
ea = first_dst->ea;
first_dst = first_dst->next;
bb = GetBasblockAt(ea);
}
}
// return false;
return true;
}
Exp *Analyzer::CreateOpExp(const ArmOp &o)
{
switch(o.type) {
case O_REG: return NewRegExp(o.reg);
case O_IMM: return NewConstExp(o.value);
default:
msg("CreateOpExp: unknown type\n");
return NULL;
}
}
Exp *Analyzer::CreateEffAddrExp(const ArmOp &o)
{
if (o.type == O_MEM) {
Exp *e = NewRegExp(o.reg);
if (o.reg2 != 0xff) { e = NewBinExp(E_ADD, e, NewRegExp(o.reg2)); }
if (o.value != 0) { e = NewBinExp(E_ADD, e, NewConstExp(o.value)); }
return e;
}
msg("CreateEffAddrExp: unknown type\n");
return NULL;
}
void Analyzer::CalcInstrFlags(Instr &i)
{
_uses = _changes = 0;
VisitUC(i.e);
i.uses = (uint16)_uses;
i.changes = (uint16)_changes;
}
// try to determine the calling convention for the call.
static uint32 GetCallingConventionFor(ea_t addr)
{
// if (addr == 0x0809E458) return 0xF + (1<<7);
if (addr == 0x83C0200) return 1<<4;
if (addr == 0x83C01F8) return 1<<2;
char buf[MAXSTR];
if (print_type(addr, buf, sizeof(buf), true)) {
uint32 ret = 0;
if (!memcmp(buf, "int ", 4)) {
ret |= CC_RET;
}
int args = 0;
// parse the number of arguments
int len = strlen(buf);
if(len > 0)
{
if (buf[len-1] == ')' && buf[len-2] == '(') {
args = 0;
} else {
args++;
for(int i=0; buf[i]; i++)
if (buf[i] == ',')
args++;
}
}
// remember # args
ret |= ((1<<args)-1);
return ret | CC_USER;
}
#if 0
type_t ti[MAXSTR];
p_list pl[MAXSTR];
if (!get_ti(addr, ti,pl)) {
// msg("Type for %x is unknown\n", addr);
return 0; // unknown
}
#endif
// msg("Type for %x is known %d\n", addr, strlen((char*)ti)) ;
return 0;
}
static Exp *SetupCallConv(Exp *e)
{
assert(e->type == E_CALL);
e->call.conv = GetCallingConventionFor(e->call.addr);
if (e->call.conv) {
uint n = 0;
for(uint i=0; i!=16; i++) {
if (HASBIT(e->call.conv, i) && n < 4) {
e->call.arg[n++] = NewRegExp(i);
}
}
}
if (e->call.conv & CC_RET)
e = NewMovExp(0, e);
return e;
}
#define BINOP(ooo,xxx) case ooo: t = xxx; goto binop_handler;
#define UNOP(ooo,xxx) case ooo: t = xxx; goto unop_handler;
#define LOADOP(ooo,xxx) case ooo: t = xxx; goto load_handler;
#define STOREOP(ooo,xxx) case ooo: t = xxx; goto store_handler;
#define CONDOP(ooo,xxx) case ooo: t = xxx; goto cond_handler;
void Analyzer::MkInstr(Instr &i, Exp *e, addr_t ea)
{
i.e = e;
i.addr = ea;
CalcInstrFlags(i);
}
void Analyzer::CreateInstruction(const ArmIns &ai, addr_t ea, BasicBlock *bb)
{
int t;
Exp *e;
switch(ai.mnem) {
case O_MOV:
e = NewMovExp(ai.op1.reg, CreateOpExp(ai.op2));
break;
BINOP(O_AND, E_AND)
BINOP(O_EOR, E_EOR)
BINOP(O_LSL, E_LSL)
BINOP(O_LSR, E_LSR)
BINOP(O_ASR, E_ASR)
// BINOP(O_ADC, E_ADC)
// BINOP(O_SBC, E_SBC)
BINOP(O_ROR, E_ROR)
BINOP(O_ORR, E_ORR)
BINOP(O_MUL, E_MUL)
BINOP(O_BIC, E_BIC)
BINOP(O_ADD, E_ADD)
BINOP(O_SUB, E_SUB)
binop_handler:
if (ai.op3.type) {
e = NewMovExp(ai.op1.reg, NewBinExp(t, CreateOpExp(ai.op2), CreateOpExp(ai.op3)));
} else {
e = NewMovExp(ai.op1.reg, NewBinExp(t, CreateOpExp(ai.op1), CreateOpExp(ai.op2)));
}
break;
UNOP(O_NEG, E_NEG)
UNOP(O_MVN, E_MVN)
unop_handler:
e = NewMovExp(ai.op1.reg, NewUnExp(t, CreateOpExp(ai.op2)));
break;
LOADOP(O_LDR, T_I32)
LOADOP(O_LDRB, T_U8)
LOADOP(O_LDRH, T_U16)
LOADOP(O_LDSB, T_I8)
LOADOP(O_LDSH, T_I16)
load_handler:
e = NewMovExp(ai.op1.reg, NewLoadExp(t, CreateEffAddrExp(ai.op2)));
break;
STOREOP(O_STR, T_I32)
STOREOP(O_STRB, T_U8)
STOREOP(O_STRH, T_U16)
store_handler:
e = NewStoreExp(t, CreateEffAddrExp(ai.op2), CreateOpExp(ai.op1));
break;
case O_BL: {
e = SetupCallConv(NewCallExp(ai.op1.value));
break;
}
// handle the LDMIA R0, {R1} case
case O_LDMIA: {
// check that a single bit is set
uint r;
for(r=0; r!=8; r++) {
if (ai.op2.value == (uint)(1 << r))
break;
}
if (r == 8) goto unhandled_handler;
// translate this into..
// LOAD R1,[R0]
// R0 = R0 + 4
MkInstr(bb->instr.Append(), NewMovExp(r, NewLoadExp(T_I32, NewRegExp(ai.op1.reg))), ea);
e = NewMovExp(ai.op1.reg, NewBinExp(E_ADD, NewRegExp(ai.op1.reg), NewConstExp(4)));
break;
}
// handle the STMIA R5, {R4} case
case O_STMIA:
{
// check that a single bit is set
uint r;
for(r=0; r!=8; r++) {
if (ai.op2.value == (uint)(1 << r))
break;
}
if ( r == 8) goto unhandled_handler;
// translate this into..
// STORE R1,[R0]
// R0 = R0 + 4
MkInstr(bb->instr.Append(), NewStoreExp(T_I32,NewRegExp(r), NewRegExp(ai.op1.reg)), ea);
e = NewMovExp(ai.op1.reg, NewBinExp(E_ADD, NewRegExp(ai.op1.reg), NewConstExp(4)));
break;
}
case O_PUSH:
case O_POP:
case O_SWI:
case O_TST:
case O_CMP:
case O_CMN:
case O_ADC:
case O_SBC:
case O_BX:
case O_B:
unhandled_handler:
e = NewOtherExp(ai);
break;
CONDOP(O_BEQ, E_COMP_EQ)
CONDOP(O_BNE, E_COMP_NE)
CONDOP(O_BCS, E_COMP_CS)
CONDOP(O_BCC, E_COMP_CC)
CONDOP(O_BMI, E_COMP_MI)
CONDOP(O_BPL, E_COMP_PL)
CONDOP(O_BVS, E_COMP_VS)
CONDOP(O_BVC, E_COMP_VC)
CONDOP(O_BHI, E_COMP_HI)
CONDOP(O_BLS, E_COMP_LS)
CONDOP(O_BGE, E_COMP_GE)
CONDOP(O_BLT, E_COMP_LT)
CONDOP(O_BGT, E_COMP_GT)
CONDOP(O_BLE, E_COMP_LE)
cond_handler: {
Exp *b = bb->instr.GetCount() == 0 ? NULL : bb->instr[bb->instr.GetCount()-1].e;
// This is a tricky one.
// Check if the instruction before was a compare instruction.
// if it was, generate an appropriate cond expression.
if (!b || b->type != E_OTHER) goto unhandled_handler;
if (b->other.ins.mnem == O_CMP) {
e = NewCondExp(NewBinExp(t, CreateOpExp(b->other.ins.op1), CreateOpExp(b->other.ins.op2)));
} else if (b->other.ins.mnem == O_CMN) {
e = NewCondExp(NewBinExp(t, CreateOpExp(b->other.ins.op1), NewUnExp(E_NEG, CreateOpExp(b->other.ins.op2))));
} else
goto unhandled_handler;
// delete the old otherexp from the array...
bb->instr.SetCount(bb->instr.GetCount() - 1);
break;
}
default:
{
msg("CreateInstruction: not found %s\n", _mnems[ai.mnem]);
return;
}
}
MkInstr(bb->instr.Append(), e, ea);
}
struct SwiInfo {
uint16 uses;
uint16 changes;
const char *name;
};
static const SwiInfo _swi_info[] = {
{0,0,"SoftReset"}, // 00h
{0,0,"RegisterRamReset"}, // 01h
{0,0,"Halt"}, // 02h
{0,0,"Stop"}, // 03h
{0,0,"IntrWait"}, // 04h
{0,0,"VBlankIntrWait"}, // 05h
{0,0,"Div"}, // 06h
{0,0,"DivArm"}, // 07h
{0,0,"Sqrt"}, // 08h
{0,0,"ArcTan"}, // 09h
{0,0,"ArcTan2"}, // 0Ah
{0,0,"CpuSet"}, // 0Bh
{0,0,"CpuFastSet"}, // 0Ch
{0,0,"GetBiosChecksum"}, // 0Dh
{0,0,"BgAffineSet"}, // 0Eh
{0,0,"ObjAffineSet"}, // 0Fh
{0,0,"BitUnPack"}, // 10h
{0,0,"LZ77UnCompWram"}, // 11h
{0,0,"LZ77UnCompVram"}, // 12h
{0,0,"HuffUnComp"}, // 13h
{0,0,"RLUnCompWram"}, // 14h
{0,0,"RLUnCompVram"}, // 15h
{0,0,"Diff8bitUnFilterWram"}, // 16h
{0,0,"Diff8bitUnFilterVram"}, // 17h
{0,0,"Diff16bitUnFilter"}, // 18h
{1,0,"SoundBias"}, // 19h
{0,0,"SoundDriverInit"}, // 1Ah
{0,0,"SoundDriverMode"}, // 1Bh
{0,0,"SoundDriverMain"}, // 1Ch
{0,0,"SoundDriverVSync"}, // 1Dh
{0,0,"SoundChannelClear"}, // 1Eh
{0,0,"MidiKey2Freq"}, // 1Fh
};
void Analyzer::VisitUC(Exp *e)
{
switch(e->type) {
case E_CONST: return;
case E_REG: _uses |= 1 << e->reg; return;
case E_MOV: _changes |= 1 << e->mov.reg; VisitUC(e->mov.e); return;
case E_BIN:
VisitUC(e->bin.left);
VisitUC(e->bin.right);
return;
case E_UN:
VisitUC(e->un.left);
return;
case E_STOR:
VisitUC(e->store.ea);
VisitUC(e->store.value);
return;
case E_LOAD:
VisitUC(e->load.ea);
return;
case E_CALL:
_uses |= e->call.conv & 0xFFFF;
_changes |= 0xF;
return;
case E_OTHER: {
switch(e->other.ins.mnem) {
case O_PUSH: _uses |= e->other.ins.op1.value; break;
case O_POP: _changes |= e->other.ins.op1.value; break;
case O_LDMIA:
case O_STMIA:
_uses |= 1<<e->other.ins.op1.reg;
_changes |= 1<<e->other.ins.op1.reg;
break;
case O_SWI:
if ((uint)e->other.ins.op1.value < lengthof(_swi_info)) {
_uses |= _swi_info[e->other.ins.op1.value].uses;
_changes |= _swi_info[e->other.ins.op1.value].changes;
}
break;
}
return;
}
case E_COND:
VisitUC(e->cond.e);
return;
case E_CHOOSE:
VisitUC(e->choose.e);
VisitUC(e->choose.left);
VisitUC(e->choose.right);
break;
case E_RETURN:
assert(0);
break;
default:
assert(0);
}
}
Exp *DuplicateExp(Exp *e)
{
switch(e->type) {
case E_CONST: return NewConstExp(e->num);
case E_REG: return NewRegExp(e->reg);
case E_MOV: return NewMovExp(e->mov.reg, DuplicateExp(e->mov.e));
case E_BIN: return NewBinExp(e->bin.subtype, DuplicateExp(e->bin.left), DuplicateExp(e->bin.right));
case E_UN: return NewUnExp(e->un.subtype, DuplicateExp(e->un.left));
case E_OTHER: return NewOtherExp(e->other.ins);
case E_LOAD: return NewLoadExp(e->load.subtype, DuplicateExp(e->load.ea));
case E_STOR: {
Exp *t = NewStoreExp(e->store.subtype, DuplicateExp(e->store.ea), DuplicateExp(e->store.value));
t->store.oper = e->store.oper;
return t;
}
case E_CHOOSE: return NewChooseExp(DuplicateExp(e->choose.e),DuplicateExp(e->choose.left),DuplicateExp(e->choose.right));
case E_RETURN: return NewReturnExp(e->ret.e ? DuplicateExp(e->ret.e) : NULL);
default:
msg("Unk type %d\n", e->type);
assert(0);
}
return NULL;
}
bool _errr;
void Analyzer::ForEachNode(Exp **ep, ForEachCallback *func, uint mask, void *param)
{
Exp *e = *ep;
if (mask & (1<<e->type)) {
Exp *f = func(e, param);
if (f) {
*ep = f;
return;
}
}
switch(e->type) {
case E_MOV:
ForEachNode(&e->mov.e, func, mask, param);
return;
case E_BIN:
ForEachNode(&e->bin.left, func, mask, param);
ForEachNode(&e->bin.right, func, mask, param);
return;
case E_UN:
ForEachNode(&e->un.left, func, mask, param);
return;
case E_STOR:
ForEachNode(&e->store.ea, func, mask, param);
ForEachNode(&e->store.value, func, mask, param);
return;
case E_LOAD:
ForEachNode(&e->load.ea, func, mask, param);
return;
case E_COND:
ForEachNode(&e->cond.e, func, mask, param);
return;
case E_CONST:
case E_OTHER:
case E_REG:
return;
case E_CALL: {
for(int i=0; i!=4; i++)
if (e->call.arg[i])
ForEachNode(&e->call.arg[i], func, mask, param);
return;
case E_CHOOSE:
ForEachNode(&e->choose.e, func, mask, param);