-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoutput.cpp
1621 lines (1362 loc) · 34.5 KB
/
output.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
#include <ida.hpp>
#include <idp.hpp>
#include <name.hpp>
#include <bytes.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include <netnode.hpp>
#include <struct.hpp>
#include <xref.hpp>
#include <auto.hpp>
#include <typeinf.hpp>
#include <offset.hpp>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windows.h>
#include <ida.hpp>
#include <idp.hpp>
#include <loader.hpp>
#include <kernwin.hpp>
#include "default.h"
#include "insn.h"
#include "exp.h"
LList<byte *> lines;
static char linebuf[512];
static int linepos;
static uint pendlabel;
// stuff in linebuf is escaped.
// Escape codes:
// 1 <BYTE> - Register Number
// 2 <UINT16> <UINT32> - Integer formatted in some special way. also used for addresses and refs.
// 3 <UINT16> <BYTE> - label name. the index is the index of the label. the byte is reserved.
// 4 <REG:4> <TYPE:4> <UINT16> - Register displacement memory access
// 5 <TYPE:4> <TID:32> <OFFS:32> - Struct offset memory load. On the form dword<R1 + StructName.member + 1>. starts a nesting group
// 6 - End of nesting.
// format of integer formatting word.
// &0x78 = 0x00 - normal number which is not an address
// &0x78 = 0x08 - dereferenced address, store
// &0x78 = 0x10 - dereferenced address, load
// &0x78 = 0x18 - address, but don't add a reference
// &0x78 = 0x20 - subroutine call
// &7 - access data type
enum {
TF_MASK = 0x78,
TF_CALL = 0x20,
TF_NUM = 0x00,
TF_DEREF_STORE = 0x08,
TF_DEREF_LOAD = 0x10,
TF_ADDRESS_NOREF = 0x18,
};
struct OutHdr {
byte version;
byte flags;
uint16 len;
ea_t ea; // real address of this instruction, when flags&2 this is instead num remaining bytes
uint16 label;
int get_indent() { return flags >> 4; }
bool is_empty_line_after() { return !!(flags & 4); }
};
// output a single bute
static void outbyte(byte b)
{
if (linepos != sizeof(linebuf))
linebuf[linepos++] = b;
}
static void outundo()
{
linepos--;
}
// output a formatted string
static void out(const char *s, ...)
{
va_list va;
va_start(va,s);
int r = _vsnprintf(linebuf + linepos, sizeof(linebuf) - linepos, s, va);
if (r != -1)
linepos += r;
else
linepos = sizeof(linebuf);
va_end(va);
}
static void outbytes(const void *b, size_t len)
{
if (linepos + len > sizeof(linebuf)) return;
memcpy(linebuf + linepos, b, len);
linepos += len;
}
static void outstr(const char *s)
{
outbytes(s, strlen(s));
}
// flush the line
static void outnewline(int indent = 0)
{
if (linepos) {
((OutHdr*)linebuf)->len = linepos;
lines.Append((byte*)memdup(linebuf, linepos));
}
((OutHdr*)linebuf)->version = 0x11;
((OutHdr*)linebuf)->flags = (indent > 15 ? 15 : indent)<<4; // max indent is 15
((OutHdr*)linebuf)->label = pendlabel; pendlabel = 0;
((OutHdr*)linebuf)->ea = 0;
linepos = sizeof(OutHdr);
}
static void outspacedline()
{
((OutHdr*)linebuf)->flags |= 4;
}
static void undospacedline()
{
((OutHdr*)linebuf)->flags &= ~4;
}
static void outorigea(ea_t ea)
{
((OutHdr*)linebuf)->ea = ea;
}
static void outlabeldef(uint label)
{
pendlabel = label;
}
static void outnum(uint32 num, uint16 format)
{
outbyte(2);
outbyte(format & 0xff);
outbyte(format>>8);
outbyte(byte(num));
outbyte(byte(num>>8));
outbyte(byte(num>>16));
outbyte(byte(num>>24));
}
static void outreg(uint r)
{
outbyte(1);
outbyte(r);
}
static void outdispl(uint r, uint t, uint32 num)
{
outbyte(4);
outbyte((r << 4) | (t & 0xF));
outbyte(byte(num));
outbyte(byte(num>>8));
}
static void outstruct(tid_t tid, uint32 offs, uint t)
{
outbyte(5);
outbyte(t);
outbyte(byte(tid));
outbyte(byte(tid>>8));
outbyte(byte(tid>>16));
outbyte(byte(tid>>24));
outbyte(byte(offs));
outbyte(byte(offs>>8));
outbyte(byte(offs>>16));
outbyte(byte(offs>>24));
}
static void outnestend()
{
outbyte(6);
}
static const char * const _opnames[] = {
"",
"&", "^", "<<", ">>", ">>s", ">><", "|", "*", "&~", "+", "-",
"||","&&",
"==", // EQ
"!=", // NE
">=u", // CS
"<u", // CC
"?mi?", //
"?pl?",
"?vs?",
"?vc?",
">u", // HI
"<=u", // LS
">=", // GE
"<", // LT
">", // GT
"<=", // LE
// unary operators
"~", "-", "!",
"EXTB(","EXTW(",
"EXTSB(","EXTSW(",
};
static const char * const _typenames[] = {
"char<",
"byte<",
"short<",
"word<",
"dword<",
};
static const byte _typesize[] = {
1,1,2,2,4,
};
static bool LooksLikeArrayBase(uint32 addr)
{
return addr >= 0x1000000 && isEnabled(addr);
}
struct TidBk {
TypeId r[16];
};
static TidBk cur_tid;
static void outexp(Exp *e, uint flags);
#define CPUFASTSET_ADDR 0x0809DFBC
static bool HandleSpecialCall(Exp *e)
{
if ((e->call.addr == CPUFASTSET_ADDR || e->call.addr == 0x0809DFC0) &&
e->call.arg[0] && e->call.arg[1] && e->call.arg[2] && !e->call.arg[3] && e->call.arg[2]->type == E_CONST) {
// CPUFastSet or CPUSet
uint32 num = e->call.arg[2]->num;
uint size = num & 0x3ffff;
if (e->call.addr == CPUFASTSET_ADDR)
size *= 4;
else if (num & (1 << 26))
size *= 4;
else
size *= 2;
if ((num >> 24) & 1) {
outstr("bios_set(");
} else {
outstr("bios_copy(");
}
outexp(e->call.arg[1],0);
outstr(", ");
outexp(e->call.arg[0],0);
outstr(", ");
outnum(size, 0);
outstr(")");
return true;
}
return false;
}
static Exp *RemoveLsl4(Exp *e, Exp *&mul)
{
if (e->type == E_BIN && e->bin.subtype == E_ADD) {
AddMulStruct ams;
Exp *l = IsAddMul(e->bin.left, ams);
if (ams.add == 0 && ams.mul == 4) { mul = l; return e->bin.right; }
l = IsAddMul(e->bin.right, ams);
if (ams.add == 0 && ams.mul == 4) { mul = l; return e->bin.left; }
}
return e;
}
enum {
PF_TYPEMASK = 7, // optional type when using PF_EA
PF_EA = 8,
PF_NEGCONST = 16,
PF_NEGATE = 32,
PF_EA_STORE = 64,
};
#define PF_TYPE(x) ((x)+1)
static void outea(Exp *eax, int type, bool store)
{
if (type == T_I32) {
Exp *eamul = NULL;
Exp *eal = RemoveLsl4(eax, eamul);
if (eal->type == E_LOAD) {
TypeId id = GetTypeOf(eal, cur_tid.r);
if (id & TI_INDIRECT_MASK) {
outexp(eal, 0);
outbyte('[');
if (eamul == NULL)
outnum(0,0);
else
outexp(eamul, 0);
outbyte(']');
return;
}
}
}
AddMulStruct ams;
Exp *ea = IsAddMul(eax, ams);
if (ams.mul == 1) {
TypeId id = GetTypeOf(ea, cur_tid.r);
// out("{tid=%x}", id);
if (id >= TI_STRUCT && !(id & TI_INDIRECT_MASK)) {
outstruct(GetTidForType(id), ams.add, (type + 1) | (store ? 0x8 : 0));
outexp(ea, 0);
outnestend();
return;
}
}
if (ams.add!=0 && ams.mul % _typesize[type] == 0 && LooksLikeArrayBase(ams.add) ) {
ams.mul /= _typesize[type];
outnum(ams.add, (store ? TF_DEREF_STORE : TF_DEREF_LOAD) | PF_TYPE(type));
outbyte('[');
if (ams.mul != 1) out("(");
outexp(ea, 0);
if (ams.mul != 1) { out(") * "); outnum(ams.mul,0); }
outbyte(']');
} else if (ea->type == E_REG && ams.mul == 1 && ((long)ams.add >= -32768 && (long)ams.add <= 32767)) {
outdispl(ea->reg, (type + 1) | (store ? 0x8 : 0), ams.add);
} else {
outexp(eax, PF_EA | (store ? PF_EA_STORE : 0) | PF_TYPE(type));
}
}
static void outexp(Exp *e, uint flags)
{
bool need_type = false;
static char charbuf[80];
if (flags & PF_NEGATE) { outbyte('!'); }
if (flags & PF_EA && flags & PF_TYPEMASK && e->type != E_CONST) {
outstr(_typenames[(flags & PF_TYPEMASK) - 1]);
flags &= ~PF_TYPEMASK;
need_type = true;
}
switch(e->type) {
case E_CONST: {
uint f = flags & PF_TYPEMASK;
if (flags & PF_EA) {
if (flags & PF_EA_STORE) {
f |= TF_DEREF_STORE;
} else {
f |= TF_DEREF_LOAD;
}
}
outnum(e->num, f); // TODO: XXX
break;
}
case E_REG:
outreg(e->reg);
break;
case E_MOV:
outreg(e->reg);
// out("{tid=%x}", e->mov.tid);
outstr(" = ");
outexp(e->mov.e, flags &~ PF_NEGATE);
break;
case E_BIN: {
int st = e->bin.subtype;
if (st == E_ADD && e->bin.right->type == E_CONST) {
uint32 num = e->bin.right->num;
// add with constant. check if the item to the left is a structure..
TypeId x = GetTypeOf(e->bin.left, cur_tid.r);
if (x >= TI_STRUCT && !(x & TI_INDIRECT_MASK) && num < GetSizeOfStruct(x)) {
// taking address of struct item
outstruct(GetTidForType(x), num, 0);
outexp(e->bin.left, 0);
outnestend();
break;
}
}
// handle negation...
if ((flags & PF_NEGATE) && st >= E_COMP_EQ && st <= E_COMP_LE) {
outundo();
st = ((st - E_COMP_EQ) ^ 1) + E_COMP_EQ;
flags ^= PF_NEGATE;
} else if ((flags & PF_NEGATE) && (st == E_LAND || st == E_LOR)) {
outundo();
st ^= E_LAND ^ E_LOR;
} else if (flags & PF_NEGATE) {
flags |= 0x7f000000;
flags ^= PF_NEGATE;
}
// need () ?
uint prec = _expression_prec[st];
bool paren = false;
if ((prec&0x3F) < (flags >> 24)) { paren = true; outbyte('('); }
outexp(e->bin.left, (flags & PF_NEGATE) | (((prec & 0x3F) + (prec>>7))<<24));
outbyte(' ');
outstr(_opnames[st]);
outbyte(' ');
outexp(e->bin.right, (flags & PF_NEGATE) | (((prec & 0x3F) + (prec>>6&1))<<24) );
if (paren) { outbyte(')'); }
break;
}
case E_UN:
if (e->un.subtype == E_LNOT) {
if (flags & PF_NEGATE) outundo();
flags ^= PF_NEGATE;
outexp(e->un.left, (flags & PF_NEGATE));
} else {
outstr(_opnames[e->un.subtype]);
outexp(e->un.left, _expression_prec[e->un.subtype]<<24);
if (e->un.subtype >= E_FROM_U8 && e->un.subtype <= E_FROM_I16)
outbyte(')');
}
break;
case E_STOR: {
outea(e->store.ea, e->store.subtype, true);
outbyte(' ');
if (e->store.oper) {
outstr(_opnames[e->store.oper]);
}
outstr("= ");
outexp(e->store.value, (e->store.oper == E_AND ? PF_NEGCONST | PF_TYPE(e->store.subtype) : 0));
outbyte(';');
break;
}
case E_LOAD: {
outea(e->load.ea, e->load.subtype, false);
break;
}
case E_CALL: {
if (!HandleSpecialCall(e)) {
outnum(e->call.addr, TF_CALL);
outbyte('(');
int num = 4;
while (num && !e->call.arg[num-1]) num--;
for(int i=0; i!=num; i++) {
if (i != 0) { outstr(", "); }
if (e->call.arg[i])
outexp(e->call.arg[i], 0);
else {
outbyte('?');
outbyte('?');
}
}
outbyte(')');
}
break;
}
case E_OTHER: {
outstr("__asm ");
PrintThumb(e->other.ins, charbuf);
outstr(charbuf);
break;
}
case E_CHOOSE: {
outbyte('(');
outexp(e->choose.e, 0);
outstr(" ? ");
outexp(e->choose.left, 0);
outstr(" : ");
outexp(e->choose.right, 0);
outbyte(')');
break;
case E_RETURN:
outstr("return ");
if (e->ret.e)
outexp(e->ret.e, 0);
else
outstr("void");
break;
}
default:
assert(0);
}
if (need_type) {
outbyte('>');
}
}
static void outbb(BasicBlock *bb, int indent)
{
memcpy(cur_tid.r, bb->tid, sizeof(cur_tid.r));
for(size_t j=0; j!=bb->num_instr; j++) {
Instr &i = bb->instr[j];
if (i.e->type != E_COND) {
outnewline(indent);
outorigea(i.addr);
outexp(i.e, 0);
}
if (i.e->type == E_MOV) {
cur_tid.r[i.e->mov.reg] = i.e->mov.tid;
}
}
}
static EmittedEnt *SkipEmptyEE(EmittedEnt *ee)
{
while (ee->type == EE_BAS && !ee->bas.bas->need_label && (ee->bas.bas->num_instr == 0 || (ee->bas.bas->num_instr == 1 && ee->bas.bas->cond)) && ee->next)
ee = ee->next;
return ee;
}
void outrs(const RegState &rs)
{
int i;
uint regs = rs.changed;
for(i=0; regs; regs>>=1, i++) {
if (regs & 1) {
if (rs.known & (1 << i)) {
out(": %s=0x%X ", _regs[i], rs.values[i]);
} else {
// out(": %s=? ", _regs[i]);
}
}
}
}
void outtid(const TypeId tid[16])
{
for(int i=0; i!=16; i++) {
if (tid[i] >= 2)
out(": %s=0x%X ", _regs[i], tid[i]);
}
}
void outlbl(BasicBlock *bas)
{
outbyte(3);
outbyte(bas->order & 0xff);
outbyte(bas->order >> 8);
outbyte(0);
}
static bool QualifiesForSingleLineIf(EmittedEnt *e)
{
if (e->type == EE_GOTO || e->type == EE_RETURN)
return true;
if (e->type == EE_BAS && IsReturn(e->bas.bas) && e->next == NULL)
return true;
if (e->type == EE_BAS && e->bas.bas->num_instr == 1 && e->next == NULL)
return true;
return false;
}
static void PrintEmittedEnt(EmittedEnt *e, int indent, BasicBlock *enclosing_for, int flags = 0)
{
for(;e;e=e->next) {
switch(e->type) {
case EE_BAS: {
BasicBlock *bb = e->bas.bas;
#if 0
outnewline(indent);
out("/* %d: 0x%X: %d %d : if=%d dom=%d lh=%d", bb->order, bb->base, bb->flow->ord(), bb->cond->ord(), bb->if_follow->ord(), bb->immed_dom->ord(), bb->loop_head->ord());
if (bb->loop_type) {
static const char * const _loopnames[] = {
"for loop","while loop", "repeat loop",
};
out(": %s latch=%d follow=%d", _loopnames[bb->loop_type-1], bb->loop_latch->ord(), bb->loop_follow->ord());
}
//outrs(bb->rs);
outtid(bb->tid);
out("*/");
#endif
if (bb->need_label) {
outlabeldef(bb->order);
}
outbb(bb , indent);
break;
}
case EE_GOTO:
outnewline(indent);
if (enclosing_for) {
if (enclosing_for->loop_type == LT_PRE_TESTED) {
if (enclosing_for->num_instr == 1 && e->bas.bas == enclosing_for) {
out("continue // ");
outlbl(e->bas.bas);
break;
}
} else if (enclosing_for->loop_type == LT_ENDLESS) {
if (e->bas.bas == enclosing_for) {
out("continue //");
outlbl(e->bas.bas);
break;
}
}
if (enclosing_for->loop_follow == e->bas.bas) {
out("break //");
outlbl(e->bas.bas);
break;
}
}
// if (enclosing_for && enclosing_for->loop_latch == e->bas.bas)
// out("continue");
out("goto ");
outlbl(e->bas.bas);
break;
case EE_IF: {
if (!(flags&1))
outnewline(indent);
flags &= ~1;
bool skip_bracket = (e->cond.left && !e->cond.right && QualifiesForSingleLineIf(e->cond.left));
out("if (");
outexp(e->cond.exp, e->cond.negate ? PF_NEGATE : 0);
out(skip_bracket ? ")" : ") {");
if (e->cond.left) PrintEmittedEnt(e->cond.left, indent + 1, enclosing_for);
if (!skip_bracket) {
undospacedline();
outnewline(indent);
out("}");
}
if (e->cond.right) {
EmittedEnt *er = SkipEmptyEE(e->cond.right);
if (er->type == EE_IF && er->cond.bas->if_follow == e->cond.bas->if_follow && er->next == NULL) {
out(" else ");
PrintEmittedEnt(er, indent, enclosing_for, 1);
} else {
out(" else {");
PrintEmittedEnt(er, indent + 1, enclosing_for);
undospacedline();
outnewline(indent);
out("}");
outspacedline();
}
} else {
outspacedline();
}
break;
}
case EE_ENDLESS:
outnewline(indent);
out("for(;;) {");
PrintEmittedEnt(e->loop.body, indent + 1, e->loop.bas);
undospacedline();
outnewline(indent);
out("} /* for */");
outspacedline();
break;
case EE_REPEAT:
outnewline(indent);
out("do {");
PrintEmittedEnt(e->loop.body, indent + 1, e->loop.bas);
undospacedline();
outnewline(indent);
out("} while (");
outexp(e->loop.exp, e->loop.negate ? PF_NEGATE : 0);
out(")");
outspacedline();
break;
case EE_WHILE:
outnewline(indent);
out("while (");
outexp(e->loop.exp, e->loop.negate ? PF_NEGATE : 0);
out(") {");
PrintEmittedEnt(e->loop.body, indent + 1, e->loop.bas);
undospacedline();
outnewline(indent);
out("} /* while */");
outspacedline();
break;
case EE_RETURN:
outnewline(indent);
out("return ");
if (e->ret.e)
outexp(e->ret.e, 0);
else
out("void");
break;
default:
assert(0);
}
}
}
byte Analyzer::CheckRegFuncPar(int r)
{
BasicBlock *bb = _list;
// someone jumps to the bb?
if (bb->ref != 0) return r;
int found = -1;
uint found_reg = 0;
// search the first instructions to see if we find a move from this register
for(uint i=0; ; i++) {
if (i == bb->num_instr) {
if (HASBIT(bb->liveout, r))
return r;
break;
}
Instr &j = bb->instr[i];
if (HASBIT(j.uses, r)) {
// check if it's one of
// MOV r2,r
// MOV r2,r & 0xff
// mov r2,r & 0xffff
if (found != -1 || j.e->type != E_MOV) return r;
Exp *e = j.e->mov.e;
if (e->type == E_REG) {
// direct reg
found_reg = 0;
} else if (e->type == E_BIN &&
e->bin.subtype == E_AND &&
e->bin.left->type == E_REG &&
e->bin.right->type == E_CONST && ((found_reg = T_U8<<4, e->bin.right->num == 0xff) || (found_reg = T_U16<<4, e->bin.right->num == 0xffff))) {
// and..
} else {
return r;
}
found = i;
found_reg += j.e->mov.reg;
}
if (HASBIT(j.changes, r))
break;
}
if (found == -1) return r;
// if we get here, then there's exactly one write to the reg.. so delete the instr,
// and return the mod reg..
KillInstr(bb, found);
return found_reg;
}
int Analyzer::DetermineFunctionArgs(byte *p)
{
int n = 0;
for(int i=0; i!=16; i++) {
if (HASBIT(_callconv, i)) {
p[n++] = CheckRegFuncPar(i);
}
}
return n;
}
void Analyzer::Dump2()
{
// PrintConstant(buf, _list->base, PF_EA);
for(BasicBlock *bb = _list; bb; bb = bb->next) {
if (bb->next == NULL && bb->num_instr == 0)
bb->written = true;
}
outnewline();
if (_returns)
outstr("int ");
else
outstr("void ");
// outstr("function ");
outnum(_function_base, TF_ADDRESS_NOREF);
outstr("(");
byte args[16];
int n = DetermineFunctionArgs(args);
for(int i=0; i!=n; i++) {
if (i != 0) out(", ");
switch(args[i] >> 4) {
case T_U8: outstr("byte "); break;
case T_U16: outstr("word "); break;
default: outstr("int "); break;
}
outreg(args[i] & 0xF);
}
outstr(") {");
if (_framesize) {
outstr(" // framesize ");
outnum(_framesize, 0);
}
EmittedEnt *r = GenerateCode();
PrintEmittedEnt(r, 1, NULL);
outnewline(0);
outstr("}");
outnewline(0);
}
void Analyzer::Dump()
{
BasicBlock *bb;
BasicBlock *flow = NULL;
msg("\n\n");
// first see if it exists already?
for(bb = _list; bb; bb = bb->next) {
int ref = bb->ref;
if (flow != NULL) {
if (bb != flow) {
if (flow->flow == NULL)
msg("\n return");
else
msg("\n goto L_%d", flow->order);
} else {
ref--;
}
}
// msg("\n// %2d:%2d,%2d, refs:%d", bb->order, bb->interval->order,
// bb->loop_head ? bb->loop_head->order : 0, bb->ref - bb->back_ref);
// msg("\n// immediate dominiator %d", bb->immed_dom ? bb->immed_dom->order : 0);
if (bb->if_follow) {
msg("\n// if follow %d", bb->if_follow->order);
}
if (bb->loop_type) {
static const char * const _ln[] = { "endless loop", "while loop", "repeat loop" };
msg("\n// %s starts here. follow = %d, latch = %d", _ln[bb->loop_type - 1], bb->loop_follow ? bb->loop_follow->order : 0, bb->loop_latch->order);
}
#if 0
msg("\n%2d,%2d: BAS %X-%X: ", bb->order, bb->interval->order, bb->base, bb->end);
if (bb->flow) msg("%X ", bb->flow->base);
if (bb->cond) msg("%X ", bb->cond->base);
msg(": ");
DumpRegs(bb->uses);
msg(": ");
DumpRegs(bb->writes);
msg(": ");
DumpRegs(bb->liveout);
msg("\nREG ");
DumpRS(bb->rs);
#endif
#if 0
if (ref != 0)
msg("L_%d: /* %d */\n", bb->order, bb->interval->order);
else
msg("/* %2d,%2d */\n", bb->order, bb->interval->order);
#else
if (ref != 0)
msg("\nL_%d:", bb->order);
#endif
outbb(bb, 1);
if (bb->cond) {
msg("\n if(");
outexp(GetIfExp(bb), 0);
msg(") goto L_%d", bb->cond->order);
}
flow = bb->flow;
}
}
void printnum(uint32 num, uint16 fmt)
{
msg("0x%X", num);
}
const byte *scantoken(const byte *s, const byte *e)
{
while (s<e && *s > 10) s++;
return s;
}
void printlines()
{
for(size_t i=0; i!=lines.GetCount(); i++) {
const byte *s = lines[i];
const byte *e = s + ((OutHdr*)s)->len;
if (((OutHdr*)s)->get_indent()) {
msg("%*c", ((OutHdr*)s)->get_indent() * 4, ' ');
}
s += sizeof(OutHdr);
assert(s <= e);
for(;;) {
const byte *t = s;
s = scantoken(s,e);
if (t != s) {
msg("%.*s", s - t, t);
}
assert(s<=e);
if (s == e)
break;
switch(*s++) {
case 1: // register
msg("%s", _regs[*s++]);
break;
case 2: // integer
printnum(*(uint32*)(s+2), *(uint16*)s);
s += 6;
break;
case 3: // label
s += 3;
break;
default:
assert(0);
}
}
msg("\n");
}
}
void printexp(Exp *e)
{
bool need_type = false;
static char charbuf[80];
switch(e->type) {
case E_CONST: {
msg("0x%x", e->num);
break;
}
case E_REG:
msg(_regs[e->reg]);
break;
case E_MOV:
msg(_regs[e->reg]);
msg(" = ");
printexp(e->mov.e);
break;
case E_BIN: {
int st = e->bin.subtype;
printexp(e->bin.left);
msg(" %s ", _opnames[st]);
printexp(e->bin.right);
break;
}
case E_UN:
msg("%s",_opnames[e->un.subtype]);