-
Notifications
You must be signed in to change notification settings - Fork 6
/
superopt.c
3301 lines (3128 loc) · 106 KB
/
superopt.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
/* Superoptimizer. Finds the shortest instruction sequences for an arbitrary
function y=f(x) where x is a vector of integer and y is a is a single
integers. The algorithm is based on exhaustive search with backtracking
and iterative deepening.
Copyright (C) 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; see the file COPYING. If not, write to the Free
Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "superopt.h"
word random_word(void);
#include "run_program.def"
#include "version.h"
#include "hashtable.c"
// #define DO_HASHTABLE
int goal_function_arity;
enum goal_func goal_function;
word (*eval_goal_function) (const word *);
int flag_output_assembly = 0;
int flag_use_carry = 1;
int flag_shifts = 0;
int flag_extracts = 0;
int flag_nl = 0;
unsigned long long hash_skipped = 0;
unsigned long long test_count = 0;
unsigned long long test_limit = -1;
/* Counts the number of solutions found. Flags to top loop that it should
not go deeper. */
int success;
#ifdef TIMING
#ifndef USG
#include <sys/time.h>
#include <sys/resource.h>
unsigned long
cputime ()
{
struct rusage rus;
getrusage (0, &rus);
return rus.ru_utime.tv_sec * 1000 + rus.ru_utime.tv_usec / 1000;
}
#else
#include <time.h>
unsigned long
cputime ()
{
return clock () / 1000;
}
#endif
int timings[100];
#endif
#ifdef STATISTICS
unsigned int heuristic_reject_count = 0;
#endif
unsigned int heuristic_accept_count = 0;
char *insn_name[] =
{
#undef DEF_INSN
#define DEF_INSN(SYM,CLASS,NAME) NAME,
#include "insn.def"
};
char insn_class[] =
{
#undef DEF_INSN
#define DEF_INSN(SYM,CLASS,NAME) CLASS,
#include "insn.def"
};
/* Initialize the "immediate" values in the top registers. */
void
init_immediates(word *values)
{
int i;
for (i = -1; i < BITS_PER_WORD; i++)
values[0x20 + i] = i;
values[0x20 - 2] = VALUE_MIN_SIGNED;
values[0x20 - 3] = VALUE_MAX_SIGNED;
values[0x20 - 4] = 0xFFFF;
values[0x20 - 5] = 0xFF;
}
#if defined (__svr4) || defined (__hpux) || defined (_SVR4_SOURCE)
#define random mrand48
#endif
void
init_random_word (void)
{
#if defined (__svr4) || defined (__hpux) || defined (_SVR4_SOURCE)
static unsigned short state1[3] = { 0xeb3d, 0x799f, 0xb11e };
srand48 (state1);
#else
static char state1[128] =
{
0, 0, 0, 3,
0x9a,0x31,0x90,0x39, 0x32,0xd9,0xc0,0x24, 0x9b,0x66,0x31,0x82, 0x5d,0xa1,0xf3,0x42,
0x74,0x49,0xe5,0x6b, 0xbe,0xb1,0xdb,0xb0, 0xab,0x5c,0x59,0x18, 0x94,0x65,0x54,0xfd,
0x8c,0x2e,0x68,0x0f, 0xeb,0x3d,0x79,0x9f, 0xb1,0x1e,0xe0,0xb7, 0x2d,0x43,0x6b,0x86,
0xda,0x67,0x2e,0x2a, 0x15,0x88,0xca,0x88, 0xe3,0x69,0x73,0x5d, 0x90,0x4f,0x35,0xf7,
0xd7,0x15,0x8f,0xd6, 0x6f,0xa6,0xf0,0x51, 0x61,0x6e,0x6b,0x96, 0xac,0x94,0xef,0xdc,
0xde,0x3b,0x81,0xe0, 0xdf,0x0a,0x6f,0xb5, 0xf1,0x03,0xbc,0x02, 0x48,0xf3,0x40,0xfb,
0x36,0x41,0x3f,0x93, 0xc6,0x22,0xc2,0x98, 0xf5,0xa4,0x2a,0xb8, 0x8a,0x88,0xd7,0x7b,
0xf5,0xad,0x9d,0x0e, 0x89,0x99,0x22,0x0b, 0x27,0xfb,0x47,0xb9
};
unsigned seed;
int n;
seed = 1;
n = 128;
initstate (seed, (char *) state1, n);
setstate (state1);
#endif
}
#define RANDOM(res,rbits) \
do { \
if (valid_randbits < rbits) \
{ \
ran = random (); \
valid_randbits = 31; \
} \
res = ran & (((word) 1 << rbits) - 1); \
ran >>= rbits; \
valid_randbits -= rbits; \
} while (0)
word
random_word (void)
{
word x;
unsigned int ran, n_bits;
int valid_randbits = 0;
int tmp, i;
/* Return small values with higher probability. */
RANDOM (tmp, 9);
if (tmp <= 60)
return tmp + 4;
/* Start off with only ones or only zeros, with equal probability. */
RANDOM (tmp, 1);
x = 0;
if (tmp)
x = ~x;
/* Now generate random strings of ones and zeros. */
n_bits = 0;
for (i = 8 * sizeof (word); i >= 0; i -= n_bits)
{
RANDOM (tmp, 3);
n_bits = tmp + 1;
x <<= n_bits;
RANDOM (tmp, 1);
if (tmp)
x |= ((word) 1 << n_bits) - 1;
}
return x;
}
void *malloc (), *realloc ();
char *
xrealloc (ptr, size)
char *ptr;
unsigned size;
{
char *result = (char *) realloc (ptr, size);
if (!result)
abort ();
return result;
}
char *
xmalloc (size)
unsigned size;
{
register char *val = (char *) malloc (size);
if (val == 0)
abort ();
return val;
}
#if HAS_NULLIFICATION
#define SYNTH(sequence, n_insns, values, n_values, goal_value, \
allowed_cost, cy, prune_flags, nullify_flag) \
do { \
if (allowed_cost == 1) \
synth_last(sequence, n_insns, values, n_values, goal_value, \
allowed_cost, cy, prune_flags, nullify_flag); \
else \
synth(sequence, n_insns, values, n_values, goal_value, \
allowed_cost, cy, prune_flags, nullify_flag); \
} while (0)
#else
#define SYNTH(sequence, n_insns, values, n_values, goal_value, \
allowed_cost, cy, prune_flags, nullify_flag) \
do { \
if (allowed_cost == 1) \
synth_last(sequence, n_insns, values, n_values, goal_value, \
allowed_cost, cy, prune_flags); \
else \
synth(sequence, n_insns, values, n_values, goal_value, \
allowed_cost, cy, prune_flags); \
} while (0)
#endif
/* Save the last generated instruction and recursively call `synth', if we
are not at a leaf node. Otherwise test the computed value and
back-track. This function is extremely critical for the performance!
OPCODE is the opcode of the insn that was just generated.
D is the destination register.
S1 is the left source register or immediate reference. It is an
immediate reference if IMMEDIATE_P(S1) is true.
S2 is the right source register or immediate reference. It is an
immediate reference if IMMEDIATE_P(S2) is true.
V is the computed result from "rD = rS1 OPCODE rS2".
COST is the cost of OPCODE with the the actual operands.
SEQUENCE is the insn sequence so far, excluding the just generated insn.
N_INSNS is the number of insns in SEQUENCE.
VALUES contains the values in register 0..N_VALUES.
N_VALUES is the number of registers that have been assigned values by
the insns so far.
GOAL_VALUE is the value we aim at, when the sequence is ready.
ALLOWED_COST is the maximum allowed cost of the remaining sequence.
CY is the carry flag. It is negative if it has an undefined value (this
for pruning the search tree), and otherwise takes the values 0 or 1
according to the conventions of the current target.
PRUNE_HINT contains flags to assist pruning of the search tree. */
static void
recurse(opcode_t opcode,
int d,
int s1,
int s2,
word v,
int cost,
insn_t *sequence,
int n_insns,
word *values,
int n_values,
const word goal_value,
int allowed_cost,
int cy,
int prune_flags
#if HAS_NULLIFICATION
,int nullify_flag
#endif
)
{
insn_t insn;
int old_success = heuristic_accept_count;
/* Update the remaining allowed cost with the cost of the last
instruction. */
allowed_cost -= cost;
if(test_limit > 0 && test_count >= test_limit)
return;
if (allowed_cost > 0)
{
/* ALLOWED_COST is still positive, meaning we can generate more
instructions. */
word old_d;
#if HAS_NULLIFICATION
/* If we we have one more instruction, and it will be nullified for the
used arguments, ensure already now that we have the goal value
somewhere. Kludge for speed. */
if (allowed_cost == 1 && nullify_flag)
{
int i;
if (v == goal_value)
goto found_goal_value;
for (i = 0; i < n_values; i++)
if (values[i] == goal_value)
goto found_goal_value;
return;
found_goal_value:;
}
#endif
/* Remember old value of dest. reg. Move to CRECURSE_2OP??? */
old_d = values[d];
values[d] = v;
#if __GNUC__
sequence[n_insns] = (insn_t) {opcode, s1, s2, d};
#else
insn.opcode = opcode;
insn.s1 = s1;
insn.s2 = s2;
insn.d = d;
sequence[n_insns] = insn;
#endif
#ifdef DO_HASHTABLE
/* If we have seen the same state at a lower or equal cost and rejected it
then the sequence cannot be optimal.
TODO check whether the has table needs to include prune_flags */
if (allowed_cost > 1 && allowed_cost <= hashtable_find(values, n_values, cy))
{
hash_skipped ++;
}
else
{
SYNTH(sequence, n_insns + 1, values, n_values, goal_value,
allowed_cost, cy, prune_flags, nullify_flag);
if (heuristic_accept_count == old_success && allowed_cost > 1)
{
hashtable_insert(values, n_values, cy, allowed_cost);
}
}
#else
SYNTH(sequence, n_insns + 1, values, n_values, goal_value,
allowed_cost, cy, prune_flags, nullify_flag);
#endif
/* Restore value of dest. reg. Move to CRECURSE_2OP??? */
values[d] = old_d;
}
else if (goal_value == v)
{
/* We are at a leaf node and got the right answer for the
random value operands. However, we probably have an
incorrect sequence. Call test_sequence to find out. */
#if __GNUC__
sequence[n_insns] = (insn_t) {opcode, s1, s2, d};
#else
insn.opcode = opcode;
insn.s1 = s1;
insn.s2 = s2;
insn.d = d;
sequence[n_insns] = insn;
#endif
test_sequence(sequence, n_insns + 1);
heuristic_accept_count++;
test_count++;
}
else
{
#ifdef STATISTICS
heuristic_reject_count++;
#endif
test_count++;
}
}
static inline void
recurse_last(opcode_t opcode,
int d,
int s1,
int s2,
word v,
insn_t *sequence,
int n_insns,
const word goal_value)
{
insn_t insn;
if(test_limit > 0 && test_count >= test_limit)
return;
if (goal_value == v)
{
/* We are at a leaf node and got the right answer for the
random value operands. However, we probably have an
incorrect sequence. Call test_sequence to find out. */
#if __GNUC__
sequence[n_insns] = (insn_t) {opcode, s1, s2, d};
#else
insn.opcode = opcode;
insn.s1 = s1;
insn.s2 = s2;
insn.d = d;
sequence[n_insns] = insn;
#endif
test_sequence(sequence, n_insns + 1);
heuristic_accept_count++;
test_count++;
}
else
{
#ifdef STATISTICS
heuristic_reject_count++;
#endif
test_count++;
}
}
#define NAME(op) operand_names[op]
static char *operand_names[256]=
{
#if SPARC
"%i0", "%i1", "%i2", "%i3", "%i4", "%i5", "%i6", "%i7",
#elif POWER
"r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
#elif M88000
"r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9",
#elif AM29K
"lr2", "lr3", "lr4", "lr5", "lr6", "lr7", "lr8", "lr9",
#elif M68000
"d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
#elif I386
"%eax", "%edx", "%ecx", "%ebx", "%esi", "%edi", "%noooo!", "%crash!!!",
#elif PYR
"pr0", "pr1", "pr2", "pr3", "pr4", "pr5", "pr6", "pr7",
#elif ALPHA
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
#elif HPPA
"%r26", "%r25", "%r24", "%r23", "%r22", "%r21", "%r20", "%r19",
#elif SH
"r4", "r5", "r6", "r7", "r8", "r9", "r10", "r11",
#elif I960
"g0", "g1", "g2", "g3", "g4", "g5", "g6", "g7",
#elif XCORE
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
#elif AVR
"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
#else
#error no register names for this CPU
#endif
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
#if SPARC
"???%hi(0x7fffffff)","%hi(0x80000000)","-1","%g0","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",
#elif POWER
"0x7fff","0x8000","-1","0","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",
#elif M88000
"hi16(0x7fffffff)","hi16(0x80000000)","-1","r0","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",
#elif AM29K
"0x7fff","0x8000","-1","0","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",
#elif M68000 || SH
"#0x7fffffff","#0x80000000","#-1","#0","#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",
#elif I386 || PYR
"$0x7fffffff","$0x80000000","$-1","$0","$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",
#elif ALPHA
"0x7fff","0x8000","-1","$31","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",
#elif HPPA
"0x7fff","0x8000","-1","%r0","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",
#elif I960
"0x7fffffff","0x80000000","-1","0","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",
#elif XCORE
"0x7fffffff","0x80000000","-1","0","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",
#elif AVR
"0x7f","0x80","-1","0","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",
#else
#error no constant syntax for this CPU
#endif
};
/* Output INSN in assembly mnemonic format on stdout. */
void
output_assembly(insn_t insn)
{
int d, s1, s2;
d = insn.d;
s1 = insn.s1;
s2 = insn.s2;
printf("\t");
switch (insn.opcode)
{
#if SPARC
case COPY:
if (IMMEDIATE_P(s1) && (IMMEDIATE_VAL(s1) & 0x1fff) == 0)
printf("sethi %s,%s",NAME(s1),NAME(d));
else
printf("mov %s,%s",NAME(s1),NAME(d));
break;
case ADD: printf("add %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case ADD_CI:printf("addx %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case ADD_CO:printf("addcc %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case ADD_CIO:printf("addxcc %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case SUB:
if (IMMEDIATE_P(s1) && IMMEDIATE_VAL(s1) == -1)
printf("xnor %%g0,%s,%s",NAME(s2),NAME(d));
else
printf("sub %s,%s,%s",NAME(s1),NAME(s2),NAME(d));
break;
case SUB_CI:printf("subx %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case SUB_CO:printf("subcc %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case SUB_CIO:printf("subxcc %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case AND: printf("and %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case IOR: printf("or %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case XOR: printf("xor %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case ANDC: printf("andn %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case IORC: printf("orn %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case EQV: printf("xnor %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case LSHIFTR:printf("srl %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case ASHIFTR:printf("sra %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
case SHIFTL:printf("sll %s,%s,%s",NAME(s1),NAME(s2),NAME(d));break;
#elif POWER
#ifdef POWERPC /* redefine the PowerPC names */
#define INS_LIL "li"
#define INS_LIU "li"
#define INS_ORIL "ori"
#define INS_CAL "addi"
#define INS_CAU "addis"
#define INS_CAX "add"
#define INS_AI "addic"
#define INS_A "addc"
#define INS_AME "addme"
#define INS_AZE "aze"
#define INS_AE "adde"
#define INS_NEG "neg"
#define INS_SF "subfc"
#define INS_SUBF "subf"
#define INS_SFI "subfic"
#define INS_SFME "subfme"
#define INS_SFZE "subfze"
#define INS_SFE "subfe"
#define INS_AND "and"
#define INS_ORIU "oris"
#define INS_ORIL "ori"
#define INS_OR "or"
#define INS_XORIL "xori"
#define INS_XORIU "xoris"
#define INS_XOR "xor"
#define INS_ANDC "andc"
#define INS_ORC "orc"
#define INS_EQV "eqv"
#define INS_NAND "nand"
#define INS_NOR "nor"
#define INS_SRI "srwi"
#define INS_SRE "srw"
#define INS_SRAI "srawi"
#define INS_SREA "sraw"
#define INS_SLI "slwi"
#define INS_SLE "slw"
#define INS_RLINM "rlwimi"
#define INS_RLNM "rlwnm"
#define INS_MULI "mulli"
#define INS_MULS "mullw"
#define INS_CNTLZ "cntlzw"
#define INS_ABS "abs (illegal)"
#define INS_NABS "nabs (illegal)"
#define INS_DOZI "dozi (illegal)"
#define INS_DOZ "doz (illegal)"
#else /* Power names, not PowerPC */
#define INS_LIL "lil"
#define INS_LIU "liu"
#define INS_ORIL "oril"
#define INS_CAL "cal"
#define INS_CAU "cau"
#define INS_CAX "cax"
#define INS_AI "ai"
#define INS_A "a"
#define INS_AME "ame"
#define INS_AZE "aze"
#define INS_AE "ae"
#define INS_NEG "neg"
#define INS_SF "sf"
#define INS_SUBF "subf (illegal)"
#define INS_SFI "sfi"
#define INS_SFME "sfme"
#define INS_SFZE "sfze"
#define INS_SFE "sfe"
#define INS_AND "and"
#define INS_ORIU "oriu"
#define INS_ORIL "oril"
#define INS_OR "or"
#define INS_XORIL "xoril"
#define INS_XORIU "xoriu"
#define INS_XOR "xor"
#define INS_ANDC "andc"
#define INS_ORC "orc"
#define INS_EQV "eqv"
#define INS_NAND "nand"
#define INS_NOR "nor"
#define INS_SRI "sri"
#define INS_SRE "sre"
#define INS_SRAI "srai"
#define INS_SREA "srea"
#define INS_SLI "sli"
#define INS_SLE "sle"
#define INS_RLINM "rlinm"
#define INS_RLNM "rlnm"
#define INS_MULI "muli"
#define INS_MULS "muls"
#define INS_CNTLZ "cntlz"
#define INS_ABS "abs"
#define INS_NABS "nabs"
#define INS_DOZI "dozi"
#define INS_DOZ "doz"
#endif /* POWER */
case COPY:
if (IMMEDIATE_P(s1))
{
if (IMMEDIATE_VAL(s1) >= 0 && IMMEDIATE_VAL(s1) < 0x8000)
printf("%s\t%s,0x%x",INS_LIL,NAME(d),IMMEDIATE_VAL(s1));
else
printf("%s\t%s,%s",INS_LIU,NAME(d),NAME(s1));
}
else
printf("%s\t%s,%s,0",INS_ORIL,NAME(d),NAME(s1));
break;
case ADD:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) + 0x4000 < 0x8000)
printf("%s\t%s,%d(%s)",INS_CAL,NAME(d),IMMEDIATE_VAL(s2),NAME(s1));
else if ((IMMEDIATE_VAL(s2) & 0xffff) == 0)
printf("%s\t%s,%s,0x%x",INS_CAU,NAME(d),NAME(s1),IMMEDIATE_VAL(s2) >> 16);
else
abort ();
}
else
printf("%s\t%s,%s,%s",INS_CAX,NAME(d),NAME(s1),NAME(s2));
break;
case ADD_CO:
if (IMMEDIATE_P(s2))
printf("%s\t%s,%s,%s",INS_AI,NAME(d),NAME(s1),NAME(s2));
else
printf("%s\t%s,%s,%s",INS_A,NAME(d),NAME(s1),NAME(s2));
break;
case ADD_CIO:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) == -1)
printf("%s\t%s,%s",INS_AME,NAME(d),NAME(s1));
else if (IMMEDIATE_VAL(s2) == 0)
printf("%s\t%s,%s",INS_AZE,NAME(d),NAME(s1));
}
else
printf("%s\t%s,%s,%s",INS_AE,NAME(d),NAME(s1),NAME(s2));
break;
case SUB:
if (IMMEDIATE_P(s1))
{
if (IMMEDIATE_VAL(s1) == 0)
printf("%s\t%s,%s",INS_NEG,NAME(d),NAME(s2));
else if (IMMEDIATE_VAL(s1) == -1)
printf("%s\t%s,%s,%s",INS_NAND,NAME(d),NAME(s2),NAME(s2));
}
else
printf("%s\t%s,%s,%s",INS_SUBF,NAME(d),NAME(s2),NAME(s1));
break;
case ADC_CO:
if (IMMEDIATE_P(s1))
printf("%s\t%s,%s,%s",INS_SFI,NAME(d),NAME(s2),NAME(s1));
else
printf("%s\t%s,%s,%s",INS_SF,NAME(d),NAME(s2),NAME(s1));
break;
case ADC_CIO:
if (IMMEDIATE_P(s1))
{
if (IMMEDIATE_VAL(s1) == -1)
printf("%s\t%s,%s",INS_SFME,NAME(d),NAME(s2));
else if (IMMEDIATE_VAL(s1) == 0)
printf("%s\t%s,%s",INS_SFZE,NAME(d),NAME(s2));
else abort();
}
else
printf("%s\t%s,%s,%s",INS_SFE,NAME(d),NAME(s2),NAME(s1));
break;
case AND:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) == 0x80000000)
printf("%s\t%s,%s,0,0,0",INS_RLINM,NAME(d),NAME(s1));
else if (IMMEDIATE_VAL(s2) == 0x7fffffff)
printf("%s\t%s,%s,0,1,31",INS_RLINM,NAME(d),NAME(s1));
else if (IMMEDIATE_VAL(s2) == 1)
printf("%s\t%s,%s,0,31,31",INS_RLINM,NAME(d),NAME(s1));
else abort();
}
else
printf("%s\t%s,%s,%s",INS_AND,NAME(d),NAME(s1),NAME(s2));
break;
case IOR:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) == 0x80000000)
printf("%s\t%s,%s,0x8000",INS_ORIU,NAME(d),NAME(s1));
else abort();
}
else
printf("%s\t%s,%s,%s",INS_OR,NAME(d),NAME(s1),NAME(s2));
break;
case XOR:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) == 1)
printf("%s\t%s,%s,1",INS_XORIL,NAME(d),NAME(s1));
else if (IMMEDIATE_VAL(s2) == 0x80000000)
printf("%s\t%s,%s,0x8000",INS_XORIU,NAME(d),NAME(s1));
else abort();
}
else
printf("%s\t%s,%s,%s",INS_XOR,NAME(d),NAME(s1),NAME(s2));
break;
case ANDC: printf("%s\t%s,%s,%s",INS_ANDC,NAME(d),NAME(s1),NAME(s2));break;
case IORC: printf("%s\t%s,%s,%s",INS_ORC,NAME(d),NAME(s1),NAME(s2));break;
case EQV: printf("%s\t%s,%s,%s",INS_EQV,NAME(d),NAME(s1),NAME(s2));break;
case NAND: printf("%s\t%s,%s,%s",INS_NAND,NAME(d),NAME(s1),NAME(s2));break;
case NOR: printf("%s\t%s,%s,%s",INS_NOR,NAME(d),NAME(s1),NAME(s2));break;
case LSHIFTR:
if (IMMEDIATE_P(s2))
printf("%s\t%s,%s,%s",INS_SRI,NAME(d),NAME(s1),NAME(s2));
else
printf("%s\t%s,%s,%s",INS_SRE,NAME(d),NAME(s1),NAME(s2));
break;
case ASHIFTR_CON:
if (IMMEDIATE_P(s2))
printf("%s\t%s,%s,%s",INS_SRAI,NAME(d),NAME(s1),NAME(s2));
else
printf("%s\t%s,%s,%s",INS_SREA,NAME(d),NAME(s1),NAME(s2));
break;
case SHIFTL:
if (IMMEDIATE_P(s2))
printf("%s\t%s,%s,%s",INS_SLI,NAME(d),NAME(s1),NAME(s2));
else
printf("%s\t%s,%s,%s",INS_SLE,NAME(d),NAME(s1),NAME(s2));
break;
case ROTATEL:
if (IMMEDIATE_P(s2))
printf("%s\t%s,%s,%s,0,31",INS_RLINM,NAME(d),NAME(s1),NAME(s2));
else
printf("%s\t%s,%s,%s,0,31",INS_RLNM,NAME(d),NAME(s1),NAME(s2));
break;
#ifndef POWERPC
case ABSVAL:printf("%s\t%s,%s",INS_ABS,NAME(d),NAME(s1));break;
case NABSVAL:printf("%s\t%s,%s",INS_NABS,NAME(d),NAME(s1));break;
case DOZ:
if (IMMEDIATE_P(s1))
printf("%s\t%s,%s,%s",INS_DOZI,NAME(d),NAME(s2),NAME(s1));
else
printf("%s\t%s,%s,%s",INS_DOZ,NAME(d),NAME(s2),NAME(s1));
break;
#endif
case MUL:
if (IMMEDIATE_P(s1))
printf("%s\t%s,%s,%s",INS_MULI,NAME(d),NAME(s2),NAME(s1));
else
printf("%s\t%s,%s,%s",INS_MULS,NAME(d),NAME(s2),NAME(s1));
break;
case CLZ:
printf("%s\t%s,%s",INS_CNTLZ,NAME(d),NAME(s1));break;
#elif M88000
case COPY:
if (IMMEDIATE_P(s1))
{
if ((IMMEDIATE_VAL(s1) & 0xffff) == 0)
printf("or.u %s,r0,0x%x",NAME(d),IMMEDIATE_VAL(s1));
else if ((IMMEDIATE_VAL(s1) & 0xffff0000) == 0)
printf("or %s,r0,0x%x",NAME(d),IMMEDIATE_VAL(s1));
else if ((IMMEDIATE_VAL(s1) & 0xffff0000) == 0xffff0000)
printf("subu %s,r0,0x%x",NAME(d),-IMMEDIATE_VAL(s1));
else
{
word x = IMMEDIATE_VAL(s1);
int i, j;
for (i = 31; i > 0; i--)
if ((x & (1 << i)) != 0)
break;
for (j = i; j >= 0; j--)
if ((x & (1 << j)) == 0)
break;
printf("set %s,r0,%d<%d>",NAME(d), i - j, j + 1);
}
}
else
printf("or %s,%s",NAME(d),NAME(s1));
break;
case ADD: printf("addu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ADD_CI:printf("addu.ci %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ADD_CO:printf("addu.co %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ADD_CIO:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) == -1)
{
printf("subu.cio %s,%s,r0",NAME(d),NAME(s1));
break;
}
if (IMMEDIATE_VAL(s2) != 0)
abort();
}
printf("addu.cio %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case SUB:
if (IMMEDIATE_P(s1) && IMMEDIATE_VAL(s1) == -1)
printf("xor.c %s,%s,r0",NAME(d),NAME(s2));
else
printf("subu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case ADC_CI:printf("subu.ci %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ADC_CO:printf("subu.co %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ADC_CIO:printf("subu.cio %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case AND:
if (IMMEDIATE_P(s2))
{
if (IMMEDIATE_VAL(s2) == 0x80000000)
printf("mask.u %s,%s,0x8000",NAME(d),NAME(s1));
else if (IMMEDIATE_VAL(s2) == 0x7fffffff)
printf("and.u %s,%s,0x7fff",NAME(d),NAME(s1));
else if (IMMEDIATE_VAL(s2) == 1)
printf("mask %s,%s,1",NAME(d),NAME(s1));
else abort();
}
else
printf("and %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case IOR:
if (IMMEDIATE_P(s2))
{
if ((IMMEDIATE_VAL(s2) & 0xffff) == 0)
printf("or.u %s,%s,0x%x",NAME(d),NAME(s1),
IMMEDIATE_VAL(s2)>>16);
else if (IMMEDIATE_VAL(s2) < 0x10000)
printf("or %s,%s,0x%x",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
else abort();
}
else
printf("or %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case XOR:
if (IMMEDIATE_P(s2))
{
if ((IMMEDIATE_VAL(s2) & 0xffff) == 0)
printf("xor.u %s,%s,0x%x",NAME(d),NAME(s1),
IMMEDIATE_VAL(s2)>>16);
else if (IMMEDIATE_VAL(s2) < 0x10000)
printf("xor %s,%s,0x%x",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
else abort();
}
else
printf("xor %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case ANDC: printf("and.c %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case IORC: printf("or.c %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case EQV: printf("xor.c %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case LSHIFTR:printf("extu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ASHIFTR:printf("ext %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case SHIFTL:printf("mak %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ROTATEL:
printf("rot %s,%s,%d",NAME(d),NAME(s1),32-IMMEDIATE_VAL(s2));
break;
case FF1:
printf("ff1 %s,%s",NAME(d),NAME(s1));break;
case FF0:
printf("ff0 %s,%s",NAME(d),NAME(s1));break;
case CMPPAR:
if (IMMEDIATE_P(s2))
printf("cmp %s,%s,0x%x",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
else if (IMMEDIATE_P(s1))
printf("cmp %s,r0,%s",NAME(d),NAME(s2));
else
printf("cmp %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case EXTS1:
printf("ext %s,%s,1<%d>",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
break;
case EXTS2:
printf("ext %s,%s,2<%d>",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
break;
case EXTU1:
printf("extu %s,%s,1<%d>",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
break;
case EXTU2:
printf("extu %s,%s,2<%d>",NAME(d),NAME(s1),IMMEDIATE_VAL(s2));
break;
case MUL:
printf("mul %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
#elif AM29K
case COPY:
if (IMMEDIATE_P(s1))
{
if (IMMEDIATE_VAL(s1) < 0x10000)
printf("const %s,0x%x",NAME(d),IMMEDIATE_VAL(s1));
else if (-IMMEDIATE_VAL(s1) < 0x10000)
printf("constn %s,-0x%x",NAME(d),-IMMEDIATE_VAL(s1));
else if (IMMEDIATE_VAL(s1) == 0x80000000)
printf("cpeq %s,gr1,gr1",NAME(d));
else abort();
}
else
printf("or %s,%s,0",NAME(d),NAME(s1));
break;
case ADD_CO:
if (IMMEDIATE_P(s2) && (signed_word) IMMEDIATE_VAL(s2) < 0)
printf("sub %s,%s,0x%x",NAME(d),NAME(s1),-IMMEDIATE_VAL(s2));
else
printf("add %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case ADD_CIO:
if (IMMEDIATE_P(s2) && (signed_word) IMMEDIATE_VAL(s2) < 0)
printf("subc %s,%s,0x%x",NAME(d),NAME(s1),-IMMEDIATE_VAL(s2));
else
printf("addc %s,%s,%s",NAME(d),NAME(s1),NAME(s2));
break;
case SUB:
if (IMMEDIATE_P(s1) && IMMEDIATE_VAL(s1) == -1)
printf("nor %s,%s,0",NAME(d),NAME(s2));
else abort();
break;
case ADC_CO:printf("sub %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ADC_CIO:printf("subc %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case AND: printf("and %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case IOR: printf("or %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case XOR: printf("xor %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ANDC: printf("andn %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case EQV: printf("xnor %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case NAND: printf("nand %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case NOR: printf("nor %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case LSHIFTR:printf("srl %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case ASHIFTR:printf("sra %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case SHIFTL:printf("sll %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPEQ: printf("cpeq %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPGE: printf("cpge %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPGEU: printf("cpgeu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPGT: printf("cpgt %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPGTU: printf("cpgtu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPLE: printf("cple %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPLEU: printf("cpleu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPLT: printf("cplt %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPLTU: printf("cpltu %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;
case CPNEQ: printf("cpneq %s,%s,%s",NAME(d),NAME(s1),NAME(s2));break;