forked from freewilll/wcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrrules.c
More file actions
1752 lines (1469 loc) · 103 KB
/
instrrules.c
File metadata and controls
1752 lines (1469 loc) · 103 KB
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include "wcc.h"
#define MAX_X86_OPERATION_PER_RULE 32
char **signed_moves_templates, **unsigned_moves_templates;
int *signed_moves_operations, *unsigned_moves_operations;
static int transform_rule_value(int extend_size, int extend_sign, int v, int size, int is_unsigned) {
int result = v;
if (extend_size) {
switch(v) {
case XCI: result = CI1 + size - 1; break;
case XCU: result = CU1 + size - 1; break;
case XRI: result = RI1 + size - 1; break;
case XRU: result = RU1 + size - 1; break;
case XMI: result = MI1 + size - 1; break;
case XMU: result = MU1 + size - 1; break;
case XRP: result = RP1 + size - 1; break;
case XC: result = (is_unsigned ? CU1 : CI1) + size - 1; break;
case XR: result = (is_unsigned ? RU1 : RI1) + size - 1; break;
case XM: result = (is_unsigned ? MU1 : MI1) + size - 1; break;
}
}
else if (extend_sign) {
switch (v) {
case XC1: result = (is_unsigned ? CU1 : CI1); break;
case XC2: result = (is_unsigned ? CU2 : CI2); break;
case XC3: result = (is_unsigned ? CU3 : CI3); break;
case XC4: result = (is_unsigned ? CU4 : CI4); break;
case XR1: result = (is_unsigned ? RU1 : RI1); break;
case XR2: result = (is_unsigned ? RU2 : RI2); break;
case XR3: result = (is_unsigned ? RU3 : RI3); break;
case XR4: result = (is_unsigned ? RU4 : RI4); break;
case XM1: result = (is_unsigned ? MU1 : MI1); break;
case XM2: result = (is_unsigned ? MU2 : MI2); break;
case XM3: result = (is_unsigned ? MU3 : MI3); break;
case XM4: result = (is_unsigned ? MU4 : MI4); break;
}
}
return result;
}
static void dup_x86_operations(X86Operation *x86_operations, int x86_operation_count, Rule *dst) {
dst->x86_operation_count = x86_operation_count;
if (!x86_operation_count) return;
dst->x86_operations = wmalloc(MAX_X86_OPERATION_PER_RULE * sizeof(X86Operation));
for (int i = 0; i < x86_operation_count; i++)
dst->x86_operations[i] = *dup_x86_operation(&x86_operations[i]);
return;
}
static Rule *add_rule(int dst, int operation, int src1, int src2, int cost) {
if (instr_rule_count == MAX_RULE_COUNT) panic("Exceeded maximum number of rules %d", MAX_RULE_COUNT);
Rule *r = &(instr_rules[instr_rule_count]);
r->index = instr_rule_count;
r->operation = operation;
r->dst = dst;
r->src1 = src1;
r->src2 = src2;
r->cost = cost;
r->x86_operations = 0;
if (cost == 0 && operation != 0) {
print_rule(r, 0, 0);
printf("A zero cost rule cannot have an operation");
}
instr_rule_count++;
return r;
}
// Add an X86Operation template to a rule's linked list
static void add_x86_op_to_rule(Rule *r, X86Operation *x86op) {
if (!r->x86_operation_count)
r->x86_operations = wmalloc(MAX_X86_OPERATION_PER_RULE * sizeof(X86Operation));
if (r->x86_operation_count == MAX_X86_OPERATION_PER_RULE) panic("Exceeded MAX_X86_OPERATION_PER_RULE");
r->x86_operations[r->x86_operation_count++] = *x86op;
}
// Add an x86 operation template to a rule
static X86Operation *add_op(Rule *r, int operation, int dst, int v1, int v2, char *template) {
X86Operation *x86op = wmalloc(sizeof(X86Operation));
x86op->operation = operation;
x86op->dst = dst;
x86op->v1 = v1;
x86op->v2 = v2;
x86op->template = template;
x86op->save_value_in_slot = 0;
x86op->allocate_stack_index_in_slot = 0;
x86op->allocate_register_in_slot = 0;
x86op->allocate_label_in_slot = 0;
x86op->allocated_type = 0;
x86op->arg = 0;
add_x86_op_to_rule(r, x86op);
return x86op;
}
static char *add_size_to_template(char *template, int size) {
if (!template) return 0; // Some magic operations have no templates but are implemented in codegen.
char x86_size = size_to_x86_size(size);
char *result = wcalloc(1, 128);
char *dst = result;
char *c = template;
while (*c) {
if (c[0] == '%' && c[1] == 's') {
*dst++ = x86_size;
c++;
}
else if (c[0] == '%' && c[1] == 'v' && (c[3] != 'b' && c[3] != 'w' && c[3] != 'l' && c[3] != 'q')) {
*dst++ = '%';
*dst++ = 'v';
*dst++ = c[2];
*dst++ = x86_size;
c += 2;
}
else
*dst++ = *c;
c++;
}
return result;
}
// Create new rules by expanding type and/or sign in non terminals
// e.g.
// (RP, RI) => (RP1, RI1), (RP2, RI2), (RP3, RI3), (RP4, RI4)
//
// XC => CI1, CI2, CI3, CI4, CU1, CU2, CU3, CU4
// XR => RI1, RI2, RI3, RI4, RU1, RU2, RU3, RU4
// XM => MI1, MI2, MI3, MI4, MU1, MU2, MU3, MU4
// XCI => CI1, CI2, CI3, CI4
// XRI => RI1, RI2, RI3, RI4
// XRU => RU1, RU2, RU3, RU4
// XRP => RP1, RP2, RP3, RP4
// XC1 => CI1, CU1, also XC2 => ... etc
// XR1 => RI1, RU1, also XR2 => ... etc
// XM1 => MI1, MU1, also XM2 => ... etc
static void fin_rule(Rule *r) {
int operation = r->operation;
int dst = r->dst;
int src1 = r->src1;
int src2 = r->src2;
int cost = r->cost;
int x86_operation_count = r->x86_operation_count;
X86Operation *x86_operations = r->x86_operations;
int expand_size = dst & EXP_SIZE || src1 & EXP_SIZE || src2 & EXP_SIZE;
int expand_sign = dst & EXP_SIGN || src1 & EXP_SIGN || src2 & EXP_SIGN;
if (!expand_size && !expand_sign) return;
instr_rule_count--; // Rewind next pointer so that the last rule is overwritten
for (int size = 1; size <= (expand_size ? 4 : 1); size++) {
for (int is_unsigned = 0; is_unsigned < (expand_sign ? 2 : 1); is_unsigned++) {
Rule *new_rule = add_rule(
transform_rule_value(expand_size, expand_sign, dst, size, is_unsigned),
operation,
transform_rule_value(expand_size, expand_sign, src1, size, is_unsigned),
transform_rule_value(expand_size, expand_sign, src2, size, is_unsigned),
cost
);
dup_x86_operations(x86_operations, x86_operation_count, new_rule);
for (int i = 0; i < new_rule->x86_operation_count; i++) {
X86Operation *x86_operation = &new_rule->x86_operations[i];
x86_operation->template = add_size_to_template(x86_operation->template, size);
}
}
}
}
// Add a save value operation to a rule
static void add_save_value(Rule *r, int arg, int slot) {
X86Operation *x86op = wcalloc(1, sizeof(X86Operation));
x86op->save_value_in_slot = slot;
x86op->arg = arg;
add_x86_op_to_rule(r, x86op);
}
static void add_allocate_stack_index_in_slot(Rule *r, int slot, int type) {
X86Operation *x86op = wcalloc(1, sizeof(X86Operation));
x86op->allocate_stack_index_in_slot = slot;
x86op->allocated_type = type;
add_x86_op_to_rule(r, x86op);
}
static void add_allocate_register_in_slot(Rule *r, int slot, int type) {
X86Operation *x86op = wcalloc(1, sizeof(X86Operation));
x86op->allocate_register_in_slot = slot;
x86op->allocated_type = type;
add_x86_op_to_rule(r, x86op);
}
static void add_allocate_label_in_slot(Rule *r, int slot) {
X86Operation *x86op = wcalloc(1, sizeof(X86Operation));
x86op->allocate_label_in_slot = slot;
add_x86_op_to_rule(r, x86op);
}
static void add_mov_rule(int dst, int src, int operation, char *template) {
int src_size = make_x86_size_from_non_terminal(src) - 1;
int dst_size = make_x86_size_from_non_terminal(dst) - 1;
int is_signed = src == RI1 || src == RI2 || src == RI3 || src == RI4;
char **moves_templates = (is_signed) ? signed_moves_templates : unsigned_moves_templates;
int *moves_operations = (is_signed) ? signed_moves_operations : unsigned_moves_operations;
if (!template) template = moves_templates[src_size * 4 + dst_size];
if (!operation) operation = moves_operations[src_size * 4 + dst_size];
Rule *r = add_rule(dst, IR_MOVE, src, 0, 1);
add_op(r, operation, DST, SRC1, 0, template);
}
static void make_rule_hash(int i) {
Rule *r = &(instr_rules[i]);
r->hash =
((long) r->dst << 0) +
((long) r->src1 << 9) +
((long) r->src2 << 18) +
((long) r->operation << 27);
}
void check_for_duplicate_rules(void) {
LongMap *map = new_longmap();
int duplicates = 0;
for (int i = 0; i < instr_rule_count; i++) {
make_rule_hash(i);
Rule *other_rule = longmap_get(map, instr_rules[i].hash);
if (other_rule) {
printf("Duplicate rules: %d\n", i);
print_rule(&(instr_rules[i]), 1, 0);
print_rule(other_rule, 1, 0);
printf("\n");
duplicates++;
}
longmap_put(map, instr_rules[i].hash, &(instr_rules[i]));
}
if (duplicates) {
printf("There are %d duplicated rules\n", duplicates);
exit(1);
}
}
static void init_moves_templates(void) {
signed_moves_templates = wmalloc(sizeof(char *) * 16);
unsigned_moves_templates = wmalloc(sizeof(char *) * 16);
signed_moves_operations = wmalloc(sizeof(int) * 16);
unsigned_moves_operations = wmalloc(sizeof(int) * 16);
// A move from src->dst. Index is src * 4 + dst
signed_moves_templates[0 ] = "movb %v1b, %vdb"; // 1 -> 1
signed_moves_templates[1 ] = "movsbw %v1b, %vdw"; // 1 -> 2
signed_moves_templates[2 ] = "movsbl %v1b, %vdl"; // 1 -> 3
signed_moves_templates[3 ] = "movsbq %v1b, %vdq"; // 1 -> 4
signed_moves_templates[4 ] = "movb %v1b, %vdb"; // 2 -> 1
signed_moves_templates[5 ] = "movw %v1w, %vdw"; // 2 -> 2
signed_moves_templates[6 ] = "movswl %v1w, %vdl"; // 2 -> 3
signed_moves_templates[7 ] = "movswq %v1w, %vdq"; // 2 -> 4
signed_moves_templates[8 ] = "movb %v1b, %vdb"; // 3 -> 1
signed_moves_templates[9 ] = "movw %v1w, %vdw"; // 3 -> 2
signed_moves_templates[10] = "movl %v1l, %vdl"; // 3 -> 3
signed_moves_templates[11] = "movslq %v1l, %vdq"; // 3 -> 4
signed_moves_templates[12] = "movb %v1b, %vdb"; // 4 -> 1
signed_moves_templates[13] = "movw %v1w, %vdw"; // 4 -> 2
signed_moves_templates[14] = "movl %v1l, %vdl"; // 4 -> 3
signed_moves_templates[15] = "movq %v1q, %vdq"; // 4 -> 4
unsigned_moves_templates[0 ] = "movb %v1b, %vdb"; // 1 -> 1
unsigned_moves_templates[1 ] = "movzbw %v1b, %vdw"; // 1 -> 2
unsigned_moves_templates[2 ] = "movzbl %v1b, %vdl"; // 1 -> 3
unsigned_moves_templates[3 ] = "movzbq %v1b, %vdq"; // 1 -> 4
unsigned_moves_templates[4 ] = "movb %v1b, %vdb"; // 2 -> 1
unsigned_moves_templates[5 ] = "movw %v1w, %vdw"; // 2 -> 2
unsigned_moves_templates[6 ] = "movzwl %v1w, %vdl"; // 2 -> 3
unsigned_moves_templates[7 ] = "movzwq %v1w, %vdq"; // 2 -> 4
unsigned_moves_templates[8 ] = "movb %v1b, %vdb"; // 3 -> 1
unsigned_moves_templates[9 ] = "movw %v1w, %vdw"; // 3 -> 2
unsigned_moves_templates[10] = "movl %v1l, %vdl"; // 3 -> 3
unsigned_moves_templates[11] = "movl %v1l, %vdl"; // 3 -> 4 Note: special case, see below
unsigned_moves_templates[12] = "movb %v1b, %vdb"; // 4 -> 1
unsigned_moves_templates[13] = "movw %v1w, %vdw"; // 4 -> 2
unsigned_moves_templates[14] = "movl %v1l, %vdl"; // 4 -> 3
unsigned_moves_templates[15] = "movq %v1q, %vdq"; // 4 -> 4
for (int i = 0; i < 16; i++) {
signed_moves_operations[i] = signed_moves_templates[i][3] == 's' ? X_MOVS : X_MOV;
unsigned_moves_operations[i] = unsigned_moves_templates[i][3] == 'z' ? X_MOVZ : X_MOV;
}
// A mov to write a 32-bit value into a register also zeroes the upper 32 bits of
// the register by default. The movl may not be removed by a live range coalesce, so
// it must be treated as a MOVZ, i.e. a zero extension.
unsigned_moves_operations[11] = X_MOVZ;
}
static void add_move_rules_ri_to_mi(void) {
Rule *r;
r = add_rule(MI1, IR_MOVE, RI1, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MI2, IR_MOVE, RI1, 0, 2); add_op(r, X_MOVS, DST, SRC1, 0 , "movsbw %v1b, %v1w"); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MI3, IR_MOVE, RI1, 0, 2); add_op(r, X_MOVS, DST, SRC1, 0 , "movsbl %v1b, %v1l"); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MI4, IR_MOVE, RI1, 0, 2); add_op(r, X_MOVS, DST, SRC1, 0 , "movsbq %v1b, %v1q"); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
r = add_rule(MI1, IR_MOVE, RI2, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MI2, IR_MOVE, RI2, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MI3, IR_MOVE, RI2, 0, 2); add_op(r, X_MOVS, DST, SRC1, 0 , "movswl %v1w, %v1l"); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MI4, IR_MOVE, RI2, 0, 2); add_op(r, X_MOVS, DST, SRC1, 0 , "movswq %v1w, %v1q"); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
r = add_rule(MI1, IR_MOVE, RI3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MI2, IR_MOVE, RI3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MI3, IR_MOVE, RI3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MI4, IR_MOVE, RI3, 0, 2); add_op(r, X_MOVS, DST, SRC1, 0 , "movslq %v1l, %v1q"); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
r = add_rule(MI1, IR_MOVE, RI4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MI2, IR_MOVE, RI4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MI3, IR_MOVE, RI4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MI4, IR_MOVE, RI4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
}
static void add_move_rules_ru_to_mu(void) {
Rule *r;
r = add_rule(MU1, IR_MOVE, RU1, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MU2, IR_MOVE, RU1, 0, 2); add_op(r, X_MOVZ, DST, SRC1, 0 , "movzbw %v1b, %v1w"); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MU3, IR_MOVE, RU1, 0, 2); add_op(r, X_MOVZ, DST, SRC1, 0 , "movzbl %v1b, %v1l"); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MU4, IR_MOVE, RU1, 0, 2); add_op(r, X_MOVZ, DST, SRC1, 0 , "movzbq %v1b, %v1q"); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
r = add_rule(MU1, IR_MOVE, RU2, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MU2, IR_MOVE, RU2, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MU3, IR_MOVE, RU2, 0, 2); add_op(r, X_MOVZ, DST, SRC1, 0 , "movzwl %v1w, %v1l"); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MU4, IR_MOVE, RU2, 0, 2); add_op(r, X_MOVZ, DST, SRC1, 0 , "movzwq %v1w, %v1q"); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
r = add_rule(MU1, IR_MOVE, RU3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MU2, IR_MOVE, RU3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MU3, IR_MOVE, RU3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MU4, IR_MOVE, RU3, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %v1l"); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq"); // Note: movzlq doesn't exist
r = add_rule(MU1, IR_MOVE, RU4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movb %v1b, %vdb");
r = add_rule(MU2, IR_MOVE, RU4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movw %v1w, %vdw");
r = add_rule(MU3, IR_MOVE, RU4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movl %v1l, %vdl");
r = add_rule(MU4, IR_MOVE, RU4, 0, 2); add_op(r, X_MOV, DST, SRC1, 0 , "movq %v1q, %vdq");
}
static void add_integer_long_double_move_rule(int src, int type, char *extend_template, char *mov_template, char *load_template) {
Rule *r = add_rule(MLD5, IR_MOVE, src, 0, 6);
add_allocate_stack_index_in_slot(r, 1, type); // Allocate stack entry and put it in SV1
if (extend_template)
add_op(r, X_MOVC, 0, SRC1, SV1, extend_template); // Sign extend register if needed
add_op(r, X_MOVC, 0, SRC1, SV1, mov_template); // Move register to stack
add_op(r, X_MOVC, 0, 0, SV1, load_template); // Load floating point from stack
add_op(r, X_MOVC, DST, 0, SV1, "fstpt %vdL"); // Store in dst
}
static void add_long_double_integer_move_rule(int dst, int type, char *conv_template, char *mov_template, char *ext_template) {
Rule *r = add_rule(dst, IR_MOVE, MLD5, 0, 6);
// Slot 1: stack index of backup control word
// Slot 2: register for manipulating control word
// Slot 3: stack index of temporary control word
// Slot 4: result of conversion
int x87_dst_type = type;
int ru4 = dst == RU4;
add_allocate_stack_index_in_slot(r, 1, TYPE_SHORT); // Slot 1: stack index of backup control word
add_allocate_register_in_slot (r, 2, TYPE_SHORT); // Slot 2: register for manipulating control word
add_allocate_stack_index_in_slot(r, 3, TYPE_SHORT); // Slot 3: stack index of temporary control word
add_allocate_stack_index_in_slot(r, 4, x87_dst_type); // Slot 4: result of conversion
add_allocate_register_in_slot (r, 5, TYPE_LONG); // Slot 5: Sign of unsigned long
add_op(r, X_MOVC, 0, SRC1, 0, "fldt %v1L"); // Load input long double
if (ru4) {
// Special case for unsigned longs. If the value is > 2^63, subtract it
// then xor the high bit of the integer at the end
add_op(r, X_MOVC, 0, 0, 0, "flds .LDTORU4(%%rip)"); // Load 2^63 stack = (2^63, v)
add_op(r, X_MOVC, 0, 0, 0, "fucomi %%st(1), %%st"); // Compare with 2^63 stack = (2^63, v)
add_op(r, X_SETA, SV5, 0, 0, "setbe %vdb"); // Store 1 if the value > 2^63
add_op(r, X_MOVC, 0, 0, 0, "fldz"); // Load zero stack = (0, 2^63, v)
add_op(r, X_MOVC, 0, 0, 0, "fxch %%st(1)"); // Exchange top two stack entries stack = (2^63, 0, v)
add_op(r, X_MOVC, 0, 0, 0, "fcmovnbe %%st(1), %%st"); // Move st1 to st0 if the value > 2^63 stack = (2^63, 0, v) or stack = (0, 0, v)
add_op(r, X_MOVC, 0, 0, 0, "fstp %%st(1)"); // Delete 2nc stack entry stack = (2^63, v) or stack = (0, v)
add_op(r, X_MOVC, 0, 0, 0, "fsubrp %%st, %%st(1)"); // Reverse subtract stack = (v - 2^63) or stack = (v)
}
add_op(r, X_MOVC, 0, 0, SV1, "fnstcw %v2"); // Backup control word into allocated stack entry
add_op(r, X_MOVZ, 0, SV2, SV1, "movzwl %v2w, %v1l"); // Move control word into register
add_op(r, X_BOR, 0, SV2, 0, "orl $3072, %v1l"); // Set rounding and precision control bits
add_op(r, X_MOVC, SV3, SV2, SV3, "movw %v1w, %v2w"); // Move control word into allocated stack
add_op(r, X_MOVC, 0, 0, SV3, "fldcw %v2w"); // Load control word from allocated stack
add_op(r, X_MOVC, 0, 0, SV4, conv_template); // Do conversion and store it on the stack
add_op(r, X_MOVC, 0, 0, SV1, "fldcw %v2w"); // Restore control register from backup
add_op(r, X_MOVC, DST, 0, SV4, mov_template); // Move converted value into the dst register
if (ext_template)
add_op(r, X_MOVC, DST, 0, SV4, ext_template); // Move converted value into the dst register
if (ru4) {
// xor the high bit if the value was > 2^53
add_op(r, X_MOVC, 0, 0, SV5, "movzbq %v2b, %v2q"); // Zero all bits except the first
add_op(r, X_SHR, 0, 0, SV5, "shlq $63, %v2q"); // Move the first bit to the last bit
add_op(r, X_XOR, DST, 0, SV5, "xorq %v2q, %vdq"); // set the sign bit if the original value was >= 2^63 - 1
}
}
static void add_sse_constant_to_ld_move_rule(int src, int src_type, char *t1, char *t2, char *t3) {
Rule *r = add_rule(MLD5, IR_MOVE, src, 0, 1);
add_allocate_stack_index_in_slot(r, 1, src_type);
add_op(r, X_MOV, 0, SRC1, 0, t1);
add_op(r, X_MOV, SV1, 0, 0, t2);
add_op(r, X_MOV, 0, SV1, 0, t3);
add_op(r, X_MOV, DST, 0, 0, "fstpt %vdL");
}
static void add_sse_on_stack_to_int_in_register_move_rule(int dst, int src, int type, char *t1, char *t2) {
Rule *r = add_rule(dst, IR_MOVE, src, 0, 1);
add_allocate_register_in_slot(r, 1, type);
add_op(r, X_MOVC, SV1, SRC1, 0, t1);
add_op(r, X_MOVC, DST, SV1, 0, t2);
fin_rule(r);
}
static void add_sse_in_register_to_long_double_move_rule(int src, int type, char *t1, char *t2) {
Rule *r = add_rule(MLD5, IR_MOVE, src, 0, 1);
add_allocate_stack_index_in_slot(r, 1, type);
add_op(r, X_MOVC, SV1, SRC1, 0, t1);
add_op(r, X_MOVC, 0, SV1, 0, t2);
add_op(r, X_MOVC, DST, 0, 0, "fstpt %vdL");
}
static void add_sse_in_stack_to_long_double_move_rule(int src, char *t1) {
Rule *r = add_rule(MLD5, IR_MOVE, src, 0, 1);
add_op(r, X_MOVC, 0, SRC1, 0, t1);
add_op(r, X_MOVC, DST, 0, 0, "fstpt %vdL");
}
static Rule *add_int_to_sse_move_rule(int dst, int src) {
int cost = (src == RU3 || src == RU4) ? 26 :2;
return add_rule(dst, IR_MOVE, src, 0, cost);
}
static void add_ulong_to_sse_operations(Rule *r, int dst, int src) {
// The conversion happens by dividing by two, then converting to SSE, then multiplying by two.
// The or and and stuff is to ensure the intermediate number is rounded up to the nearest odd.
// This prevents an double rounding error.
char *t1, *t2; // Convert & add templates
if (dst == RS3 && src == RU4) { t1 = "cvtsi2ssq %v1q, %vdF"; t2 = "addss %vdF, %vdF"; }
if (dst == RS4 && src == RU4) { t1 = "cvtsi2sdq %v1q, %vdD"; t2 = "addsd %vdD, %vdD"; }
add_allocate_label_in_slot(r, 1); // Completion label
add_allocate_label_in_slot(r, 2); // Signed case
add_allocate_register_in_slot(r, 3, TYPE_LONG); // Temporary for unsigned case
add_op(r, X_TEST, SRC1, SRC1, 0, "testq %v1q, %v1q");
add_op(r, X_JZ, 0, SV2, 0, "js %v1"); // Jump if unsigned
add_op(r, X_MOVC, DST, SRC1, 0, t1); // Signed case
add_op(r, X_JMP, 0, SV1, 0, "jmp %v1");
add_op(r, X_MOVC, SV3, SRC1, 0, ".L2:movq %v1q, %vdq"); // Unsigned case
add_op(r, X_SHR, SV3, SV3, 0, "shrq %v1q");
add_op(r, X_BAND, SRC1, SRC1, 0, "andl $1, %v1l");
add_op(r, X_BOR, SV3, SV3, SRC1, "orq %v1q, %v2q");
add_op(r, X_MOVC, DST, SRC1, 0, t1);
add_op(r, X_ADD, DST, DST, DST, t2);
add_op(r, X_MOVC, DST, DST, 0, ".L1:"); // Done
}
static void add_int_to_sse_operations(Rule *r, int dst, int src) {
// Add operations that convert an in a register to a SSE in a register
char *t1 = 0;
char *t2 = 0;
if (dst == RS3 && src == RI1) { t1 = "movsbl %v1b, %vdl"; t2 = "cvtsi2ssl %v1l, %vdF"; }
else if (dst == RS3 && src == RI2) { t1 = "movswl %v1w, %vdl"; t2 = "cvtsi2ssl %v1l, %vdF"; }
else if (dst == RS3 && src == RI3) { t2 = "cvtsi2ssl %v1l, %vdF"; }
else if (dst == RS3 && src == RI4) { t2 = "cvtsi2ssq %v1q, %vdF"; }
else if (dst == RS4 && src == RI1) { t1 = "movsbl %v1b, %vdl"; t2 = "cvtsi2sdl %v1l, %vdD"; }
else if (dst == RS4 && src == RI2) { t1 = "movswl %v1w, %vdl"; t2 = "cvtsi2sdl %v1l, %vdD"; }
else if (dst == RS4 && src == RI3) { t2 = "cvtsi2sdl %v1l, %vdD"; }
else if (dst == RS4 && src == RI4) { t2 = "cvtsi2sdq %v1q, %vdD"; }
else if (dst == RS3 && src == RU1) { t1 = "movzbl %v1b, %vdl"; t2 = "cvtsi2ssl %v1l, %vdF"; }
else if (dst == RS3 && src == RU2) { t1 = "movzwl %v1w, %vdl"; t2 = "cvtsi2ssl %v1l, %vdF"; }
else if (dst == RS3 && src == RU3) { t1 = "movl %v1l, %vdl"; t2 = "cvtsi2ssq %v1q, %vdF"; }
else if (dst == RS3 && src == RU4) { add_ulong_to_sse_operations(r, dst, src); return; }
else if (dst == RS4 && src == RU1) { t1 = "movzbl %v1b, %vdl"; t2 = "cvtsi2sdl %v1l, %vdD"; }
else if (dst == RS4 && src == RU2) { t1 = "movzwl %v1w, %vdl"; t2 = "cvtsi2sdl %v1l, %vdD"; }
else if (dst == RS4 && src == RU3) { t1 = "movl %v1l, %vdl"; t2 = "cvtsi2sdq %v1q, %vdD"; }
else if (dst == RS4 && src == RU4) { add_ulong_to_sse_operations(r, dst, src); return; }
else panic ("Unknown dst/src combination");
if (t1) add_op(r, X_MOVC, SRC1, SRC1, 0, t1);
add_op(r, X_MOVC, DST, SRC1, 0, t2);
}
static void add_long_double_to_sse_move_rule(int dst, int type, char *t1, char *t2) {
Rule *r = add_rule(dst, IR_MOVE, MLD5, 0, 10);
add_allocate_stack_index_in_slot(r, 1, type);
add_op(r, X_MOV, 0, SRC1, 0, "fldt %v1L");
add_op(r, X_MOV, SV1, 0, 0, t1);
add_op(r, X_MOV, DST, SV1, 0, t2);
}
static void add_float_and_double_move_rules(void) {
Rule *r ;
// Constant -> register
r = add_rule(RS3, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movss %v1F, %vdq"); // Load float constant into float
r = add_rule(RS3, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movss %v1F, %vdq"); // Load double constant into float
r = add_rule(RS4, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movsd %v1D, %vdq"); // Load float constant into double
r = add_rule(RS4, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movsd %v1D, %vdq"); // Load double constant into double
// Constant -> memory
r = add_rule(MS3, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movss %v1F, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "movss %%xmm14, %vdq");
r = add_rule(MS4, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movsd %v1D, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "movsd %%xmm14, %vdq");
// Register -> register
r = add_rule(RS3, IR_MOVE, RS3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movss %v1q, %vdq");
r = add_rule(RS3, IR_MOVE, RS4, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvtsd2ss %v1q, %vdq");
r = add_rule(RS4, IR_MOVE, RS3, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvtss2sd %v1q, %vdq");
r = add_rule(RS4, IR_MOVE, RS4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movsd %v1q, %vdq");
// Memory -> register
r = add_rule(RS3, IR_MOVE, MS3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movss %v1q, %vdq");
r = add_rule(RS3, IR_MOVE, MS4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movsd %v1q, %vdq"); add_op(r, X_MOVC, DST, DST, 0, "cvtsd2ss %v1q, %vdq");
r = add_rule(RS4, IR_MOVE, MS3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movss %v1q, %vdq"); add_op(r, X_MOVC, DST, DST, 0, "cvtss2sd %v1q, %vdq");
r = add_rule(RS4, IR_MOVE, MS4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movsd %v1q, %vdq");
// Register -> memory
r = add_rule(MS3, IR_MOVE, RS3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movss %v1q, %vdq");
r = add_rule(MS4, IR_MOVE, RS4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movsd %v1q, %vdq");
// SSE Constant -> integer in register
r = add_rule(XR1, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movss %v1F, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttss2sil %%xmm14, %vdl"); fin_rule(r);
r = add_rule(XR2, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movss %v1F, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttss2sil %%xmm14, %vdl"); fin_rule(r);
r = add_rule(XR3, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movss %v1F, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttss2sil %%xmm14, %vdl"); fin_rule(r);
r = add_rule(XR4, IR_MOVE, CS3, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movss %v1F, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttss2siq %%xmm14, %vdq"); fin_rule(r);
r = add_rule(XR1, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movsd %v1D, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttsd2sil %%xmm14, %vdl"); fin_rule(r);
r = add_rule(XR2, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movsd %v1D, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttsd2sil %%xmm14, %vdl"); fin_rule(r);
r = add_rule(XR3, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movsd %v1D, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttsd2sil %%xmm14, %vdl"); fin_rule(r);
r = add_rule(XR4, IR_MOVE, CS4, 0, 1); add_op(r, X_MOV, 0, SRC1, 0, "movsd %v1D, %%xmm14"); add_op(r, X_MOV, DST, 0, 0, "cvttsd2siq %%xmm14, %vdq"); fin_rule(r);
// SSE Constant -> LD
add_sse_constant_to_ld_move_rule(CS3, TYPE_FLOAT, "movss %v1F, %%xmm0", "movss %%xmm0, %vd", "flds %v1");
add_sse_constant_to_ld_move_rule(CS4, TYPE_DOUBLE, "movsd %v1D, %%xmm0", "movsd %%xmm0, %vd", "fldl %v1");
// SSE in register -> integer in register
r = add_rule(XR1, IR_MOVE, RS3, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttss2sil %v1F, %vdl"); fin_rule(r);
r = add_rule(XR2, IR_MOVE, RS3, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttss2sil %v1F, %vdl"); fin_rule(r);
r = add_rule(XR3, IR_MOVE, RS3, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttss2sil %v1F, %vdl"); fin_rule(r);
r = add_rule(XR4, IR_MOVE, RS3, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttss2siq %v1F, %vdq"); fin_rule(r);
r = add_rule(XR1, IR_MOVE, RS4, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttsd2sil %v1F, %vdl"); fin_rule(r);
r = add_rule(XR2, IR_MOVE, RS4, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttsd2sil %v1F, %vdl"); fin_rule(r);
r = add_rule(XR3, IR_MOVE, RS4, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttsd2sil %v1F, %vdl"); fin_rule(r);
r = add_rule(XR4, IR_MOVE, RS4, 0, 1); add_op(r, X_MOVC, DST, SRC1, 0, "cvttsd2siq %v1F, %vdq"); fin_rule(r);
// SSE on stack -> integer in register
add_sse_on_stack_to_int_in_register_move_rule(XR1, MS3, TYPE_FLOAT, "movss %v1F, %vdF", "cvttss2sil %v1F, %vdl");
add_sse_on_stack_to_int_in_register_move_rule(XR2, MS3, TYPE_FLOAT, "movss %v1F, %vdF", "cvttss2sil %v1F, %vdl");
add_sse_on_stack_to_int_in_register_move_rule(XR3, MS3, TYPE_FLOAT, "movss %v1F, %vdF", "cvttss2sil %v1F, %vdl");
add_sse_on_stack_to_int_in_register_move_rule(XR4, MS3, TYPE_FLOAT, "movss %v1F, %vdF", "cvttss2siq %v1F, %vdq");
add_sse_on_stack_to_int_in_register_move_rule(XR1, MS4, TYPE_DOUBLE, "movsd %v1D, %vdD", "cvttsd2sil %v1D, %vdl");
add_sse_on_stack_to_int_in_register_move_rule(XR2, MS4, TYPE_DOUBLE, "movsd %v1D, %vdD", "cvttsd2sil %v1D, %vdl");
add_sse_on_stack_to_int_in_register_move_rule(XR3, MS4, TYPE_DOUBLE, "movsd %v1D, %vdD", "cvttsd2sil %v1D, %vdl");
add_sse_on_stack_to_int_in_register_move_rule(XR4, MS4, TYPE_DOUBLE, "movsd %v1D, %vdD", "cvttsd2siq %v1D, %vdq");
// SSE in register -> long double
add_sse_in_register_to_long_double_move_rule(RS3, TYPE_FLOAT, "movss %v1F, %vdl", "flds %v1F");
add_sse_in_register_to_long_double_move_rule(RS4, TYPE_DOUBLE, "movsd %v1D, %vdq", "fldl %v1D");
// SSE in stack -> long double
add_sse_in_stack_to_long_double_move_rule(MS3, "flds %v1F");
add_sse_in_stack_to_long_double_move_rule(MS4, "fldl %v1D");
// Integer in register -> SSE in register
for (int dst = RS3; dst <= RS4; dst++)
for (int src = RI1; src <= RU4; src++) {
r = add_int_to_sse_move_rule(dst, src);
add_int_to_sse_operations(r, dst, src);
}
// Long double -> SSE
add_long_double_to_sse_move_rule(RS3, TYPE_FLOAT, "fstps %vdL", "movss %v1F, %vdF");
add_long_double_to_sse_move_rule(RS4, TYPE_DOUBLE, "fstpl %vdL", "movsd %v1D, %vdD");
}
static void add_long_double_move_rules(void) {
Rule *r;
// Long double -> integer rules
// ----------------------------------------------------------------------------------
// Long double constant -> memory
r = add_rule(MLD5, IR_MOVE, CLD, 0, 3);
add_op(r, X_MOV, DST, SRC1, 0, "movabsq %v1L, %%r10"); add_op(r, X_MOV, DST, SRC1, 0, "movq %%r10, %vdL");
add_op(r, X_MOV, DST, SRC1, 0, "movabsq %v1H, %%r10"); add_op(r, X_MOV, DST, SRC1, 0, "movq %%r10, %vdH");
// Long double memory -> memory
r = add_rule(MLD5, IR_MOVE, MLD5, 0, 5);
add_op(r, X_MOVC, SRC1, SRC1, 0, "movq %v1L, %%r10");
add_op(r, X_MOVC, DST, DST, 0, "movq %%r10, %vdL");
add_op(r, X_MOVC, SRC1, SRC1, 0, "movq %v1H, %%r10");
add_op(r, X_MOVC, DST, DST, 0, "movq %%r10, %vdH");
// Integer in register -> long double in memory
// Signed integers
add_integer_long_double_move_rule(RI1, TYPE_CHAR , "movsbw %v1b, %v1w", "movw %v1w, %v2", "filds %v2L");
add_integer_long_double_move_rule(RI2, TYPE_SHORT, 0, "movw %v1w, %v2", "filds %v2L");
add_integer_long_double_move_rule(RI3, TYPE_INT , 0, "movl %v1l, %v2", "fildl %v2L");
add_integer_long_double_move_rule(RI4, TYPE_LONG , 0, "movq %v1q, %v2", "fildq %v2L");
// Unsigned integers
add_integer_long_double_move_rule(RU1, TYPE_CHAR , "movzbw %v1b, %v1w", "movw %v1w, %v2", "filds %v2L");
add_integer_long_double_move_rule(RU2, TYPE_SHORT, "movzwl %v1w, %v1l", "movl %v1l, %v2", "fildl %v2L");
add_integer_long_double_move_rule(RU3, TYPE_INT , "movl %v1l, %v1l", "movq %v1q, %v2", "fildq %v2L");
// RU4 is a special case
// Inspired by clang, when run with -fPIE
// Signed constants <0 are converted and then need a huge constant added to them
r = add_rule(MLD5, IR_MOVE, RU4, 0, 10);
add_allocate_stack_index_in_slot(r, 1, TYPE_LONG); // Allocate stack entry and put it in slot 1
add_op(r, X_MOVC, 0, SRC1, SV1, "movq %v1q, %v2"); // Move register to stack
add_op(r, X_MOVC, 0, 0, 0, "movq $0, %%r10"); // Zero r10
add_op(r, X_MOVC, 0, SRC1, 0, "testq %v1q, %v1q"); // Test & set flags
add_op(r, X_MOVC, DST, 0, 0, "sets %%r10b"); // Store sign in r10 (0=unsigned, 1=signed)
add_op(r, X_MOVC, 0, SRC2, SV1, "fildq %v2L"); // Load floating point from stack
add_op(r, X_MOVC, 0, 0, 0, "leaq .RU4TOLD(%%rip), %%r11"); // Add constant, 0 for unsigned, something huge for signed
add_op(r, X_MOVC, 0, 0, 0, "fadds (%%r11,%%r10,4)");
add_op(r, X_MOVC, DST, 0, 0, "fstpt %vdL"); // Store in dst
// Integer -> long double rules
// ----------------------------------------------------------------------------------
add_long_double_integer_move_rule(RI1, TYPE_SHORT, "fistps %v2w", "movzbl %v2b, %vdl", "movsbl %vdb, %vdl");
add_long_double_integer_move_rule(RU1, TYPE_SHORT, "fistps %v2w", "movzbl %v2b, %vdl", "movzbl %vdb, %vdl");
add_long_double_integer_move_rule(RI2, TYPE_SHORT, "fistps %v2w", "movzwl %v2w, %vdl", "movswl %vdw, %vdl");
add_long_double_integer_move_rule(RU2, TYPE_INT, "fistpl %v2w", "movl %v2w, %vdl", "movzwl %vdw, %vdl");
add_long_double_integer_move_rule(RI3, TYPE_INT , "fistpl %v2l", "movl %v2l, %vdl", 0);
add_long_double_integer_move_rule(RU3, TYPE_LONG , "fistpq %v2l", "movq %v2q, %vdq", "movl %vdl, %vdl");
add_long_double_integer_move_rule(RI4, TYPE_LONG , "fistpq %v2l", "movq %v2q, %vdq", 0);
add_long_double_integer_move_rule(RU4, TYPE_LONG , "fistpq %v2l", "movq %v2q, %vdq", 0);
}
static Rule *add_move_to_ptr(int src1, int src2, char *sign_extend_template, char *op_template) {
Rule *r = add_rule(src1, IR_MOVE_TO_PTR, src1, src2, 3);
if (sign_extend_template) add_op(r, X_MOVS, SRC2, SRC2, 0, sign_extend_template);
add_op(r, X_MOV_TO_IND, 0, SRC1, SRC2, op_template);
return r;
}
// Add rules for loads & address of from IR_BSHL + IR_ADD + IR_INDIRECT/IR_ADDRESS_OF
static void add_scaled_rule(int *ntc, int cst, int add_reg, int indirect_reg, int address_of_reg, int op, char *template) {
Rule *r;
int ntc1 = ++*ntc;
int ntc2 = ++*ntc;
r = add_rule(ntc1, IR_BSHL, RI4, cst, 1); add_save_value(r, 1, 2); // Save index register to slot 2
r = add_rule(ntc1, IR_BSHL, RU4, cst, 1); add_save_value(r, 1, 2); // Save index register to slot 2
r = add_rule(ntc2, IR_ADD, add_reg, ntc1, 1); add_save_value(r, 1, 1); // Save address register to slot 1
if (indirect_reg) r = add_rule(indirect_reg, IR_INDIRECT, ntc2, 0, 1);
if (address_of_reg) r = add_rule(address_of_reg, IR_ADDRESS_OF, ntc2, 0, 1);
add_op(r, op, DST, SV1, SV2, template);
}
static void add_offset_rule(int *ntc, int add_reg, int indirect_reg, char *template) {
Rule *r;
int ntc1 = ++*ntc;
r = add_rule(ntc1, IR_ADD, add_reg, CI4, 1);
add_save_value(r, 1, 1); // Save address register to slot 1
add_save_value(r, 2, 2); // Save offset register to slot 2
r = add_rule(indirect_reg, IR_INDIRECT, ntc1, 0, 1);
add_op(r, X_MOV_FROM_SCALED_IND, DST, SV1, SV2, template);
}
static void add_composite_pointer_rules(int *ntc) {
// Loads
add_scaled_rule(ntc, CSTV1, RP2, RI2, 0, X_MOV_FROM_SCALED_IND, "movw (%v1q,%v2q,2), %vdw"); // from *short to short
add_scaled_rule(ntc, CSTV1, RP2, RU2, 0, X_MOV_FROM_SCALED_IND, "movw (%v1q,%v2q,2), %vdw"); // from *ushort to ushort
add_scaled_rule(ntc, CSTV2, RP3, RI3, 0, X_MOV_FROM_SCALED_IND, "movl (%v1q,%v2q,4), %vdl"); // from *int to int
add_scaled_rule(ntc, CSTV2, RP3, RU3, 0, X_MOV_FROM_SCALED_IND, "movl (%v1q,%v2q,4), %vdl"); // from *uint to uint
add_scaled_rule(ntc, CSTV3, RP4, RI4, 0, X_MOV_FROM_SCALED_IND, "movq (%v1q,%v2q,8), %vdq"); // from *long to long
add_scaled_rule(ntc, CSTV3, RP4, RU4, 0, X_MOV_FROM_SCALED_IND, "movq (%v1q,%v2q,8), %vdq"); // from *ulong to ulong
add_scaled_rule(ntc, CSTV3, RP4, RP4, 0, X_MOV_FROM_SCALED_IND, "movq (%v1q,%v2q,8), %vdq"); // from **
add_offset_rule(ntc, RP1, RI1, "movb %v2q(%v1q), %vdb"); // from struct member from *char -> char
add_offset_rule(ntc, RP1, RU1, "movb %v2q(%v1q), %vdb"); // from struct member from *uchar -> uchar
add_offset_rule(ntc, RP2, RU2, "movw %v2q(%v1q), %vdw"); // from struct member from *ushort -> ushort
add_offset_rule(ntc, RP2, RI2, "movw %v2q(%v1q), %vdw"); // from struct member from *short -> short
add_offset_rule(ntc, RP3, RI3, "movl %v2q(%v1q), %vdl"); // from struct member from *int -> int
add_offset_rule(ntc, RP3, RU3, "movl %v2q(%v1q), %vdl"); // from struct member from *uint -> uint
add_offset_rule(ntc, RP4, RI4, "movq %v2q(%v1q), %vdq"); // from struct member from *long -> long
add_offset_rule(ntc, RP4, RU4, "movq %v2q(%v1q), %vdq"); // from struct member from *ulong -> ulong
add_offset_rule(ntc, RP3, RS3, "movss %v2q(%v1q), %vdF"); // from struct member from *float -> float
add_offset_rule(ntc, RP4, RS4, "movsd %v2q(%v1q), %vdF"); // from struct member from *double -> double
add_offset_rule(ntc, RP4, RP1, "movq %v2q(%v1q), %vdq"); // from ** to *char
add_offset_rule(ntc, RP4, RP2, "movq %v2q(%v1q), %vdq"); // from ** to *short
add_offset_rule(ntc, RP4, RP3, "movq %v2q(%v1q), %vdq"); // from ** to *int
add_offset_rule(ntc, RP4, RP4, "movq %v2q(%v1q), %vdq"); // from ** to *long
// Address of
add_scaled_rule(ntc, CSTV1, RP4, 0, RP4, X_MOV_FROM_SCALED_IND, "lea (%v1q,%v2q,2), %vdq");
add_scaled_rule(ntc, CSTV2, RP4, 0, RP4, X_MOV_FROM_SCALED_IND, "lea (%v1q,%v2q,4), %vdq");
add_scaled_rule(ntc, CSTV3, RP4, 0, RP4, X_MOV_FROM_SCALED_IND, "lea (%v1q,%v2q,8), %vdq");
}
static void add_int_indirect_rule(int dst, int src) {
char *template;
switch (src) {
case RP1: template = "movb %v1o(%v1q), %vdb"; break;
case RP2: template = "movw %v1o(%v1q), %vdw"; break;
case RP3: template = "movl %v1o(%v1q), %vdl"; break;
default: template = "movq %v1o(%v1q), %vdq";
}
Rule *r = add_rule(dst, IR_INDIRECT, src, 0, 2);
add_op(r, X_MOV_FROM_IND, DST, SRC1, 0, template);
}
static void add_sse_indirect_rule(int dst, int src) {
char *template;
switch (src) {
case RP3: template = "movss %v1o(%v1q), %vdF"; break;
case RP4: template = "movsd %v1o(%v1q), %vdD"; break;
default: panic("Unknown src in add_sse_indirect_rule %d", src);
}
Rule *r = add_rule(dst, IR_INDIRECT, src, 0, 2);
add_op(r, X_MOV_FROM_IND, DST, SRC1, 0, template);
}
static void add_indirect_rules(void) {
Rule *r ;
// Integers
for (int dst = 0; dst < 4; dst++) add_int_indirect_rule(RI1 + dst, RP1 + dst);
for (int dst = 0; dst < 4; dst++) add_int_indirect_rule(RU1 + dst, RP1 + dst);
// Rules used when loading a struct into a register
r = add_rule(RU4, IR_INDIRECT, RP1, 0, 2); add_op(r, X_MOV_FROM_IND, DST, SRC1, 0, "movzbq %v1o(%v1q), %vdq");
r = add_rule(RU4, IR_INDIRECT, RP2, 0, 2); add_op(r, X_MOV_FROM_IND, DST, SRC1, 0, "movzwq %v1o(%v1q), %vdq");
r = add_rule(RU4, IR_INDIRECT, RP3, 0, 2); add_op(r, X_MOV_FROM_IND, DST, SRC1, 0, "movl %v1o(%v1q), %vdl");
// SSE
for (int dst = 0; dst < 2; dst++) add_sse_indirect_rule(RS3 + dst, RP3 + dst);
// Pointer to pointer
for (int dst = 0; dst < 4; dst++) add_int_indirect_rule(RP1 + dst, RP4);
add_int_indirect_rule(RP5, RP4);
add_int_indirect_rule(RPF, RP4);
// Pointer to long double in register to pointer in memory
r = add_rule(MPV, IR_MOVE, RP5, 0, 2); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq");
// Long double indirect from pointer in register
r = add_rule(MLD5, IR_INDIRECT, RP5, 0, 5);
add_allocate_register_in_slot(r, 1, TYPE_LONG); // SV1: allocate register for value
add_op(r, X_MOV_FROM_IND, SV1, SRC1, 0, "movq %v1o(%v1q), %vdq"); // Move low byte
add_op(r, X_MOV, DST, SRC1, SV1, "movq %v2q, %vdL");
add_op(r, X_MOV_FROM_IND, SV1, SRC1, 0, "movq %v1or+8(%v1q), %vdq"); // Move high byte
add_op(r, X_MOV, DST, SRC1, SV1, "movq %v2q, %vdH");
// Long double indirect from pointer in memory
r = add_rule(MLD5, IR_INDIRECT, MPV, 0, 5);
add_allocate_register_in_slot(r, 1, TYPE_LONG); // SV1: register for pointer
add_allocate_register_in_slot(r, 2, TYPE_LONG); // SV2: register for value
add_op(r, X_MOVC, SV1, SRC1, 0, "movq %v1q, %vdq"); // Load pointer in memory into pointer in register
add_op(r, X_MOV_FROM_IND, SV2, SV1, 0, "movq %v1o(%v1q), %vdq"); // Move low byte
add_op(r, X_MOVC, DST, SV2, 0, "movq %v1q, %vdL");
add_op(r, X_MOV_FROM_IND, SV2, SV1, 0, "movq %v1or+8(%v1q), %vdq"); // Move high byte
add_op(r, X_MOVC, DST, SV2, 0, "movq %v1q, %vdH");
// Move of a constant to a pointer in a register
r = add_rule(RP5, IR_MOVE_TO_PTR, RP5, CLD, 5);
add_allocate_register_in_slot(r, 1, TYPE_LONG); // SV1: register for value
add_op(r, X_MOVC, SV1, SRC2, 0, "movq %v1L, %vdq"); // Move low byte
add_op(r, X_MOV_TO_IND, 0, SRC1, SV1, "movq %v2q, %v1o(%v1q)"); // Move high byte
add_op(r, X_MOVC, SV1, SRC2, 0, "movq %v1H, %vdq");
add_op(r, X_MOV_TO_IND, 0, SRC1, SV1, "movq %v2q, %v1or+8(%v1q)");
// Move of a long double to a pointer in a register
r = add_rule(RP5, IR_MOVE_TO_PTR, RP5, MLD5, 5);
add_allocate_register_in_slot(r, 1, TYPE_LONG); // SV1: register for value
add_op(r, X_MOVC, SV1, SRC2, 0, "movq %v1L, %vdq"); // Move low byte
add_op(r, X_MOV_TO_IND, 0, SRC1, SV1, "movq %v2q, %v1o(%v1q)"); // Move high byte
add_op(r, X_MOVC, SV1, SRC2, 0, "movq %v1H, %vdq");
add_op(r, X_MOV_TO_IND, 0, SRC1, SV1, "movq %v2q, %v1or+8(%v1q)");
}
static void add_address_of_rule(int dst, int src, char *template_args, int finish) {
Rule *r;
char *mov_template, *lea_template;
wasprintf(&lea_template, "leaq %s", template_args);
wasprintf(&mov_template, "movq %s", template_args);
r = add_rule(dst, IR_ADDRESS_OF, src, 0, 2); add_op(r, X_LEA, DST, SRC1, 0, lea_template); if (finish) fin_rule(r);
r = add_rule(dst, IR_ADDRESS_OF_FROM_GOT, src, 0, 2); add_op(r, X_MOV, DST, SRC1, 0, mov_template); if (finish) fin_rule(r);
}
static void add_pointer_rules(int *ntc) {
Rule *r;
add_indirect_rules();
add_composite_pointer_rules(ntc);
// Address loads
// Any ADDRESS_OF a pointer in a register must be lvalues. Therefore, a mov converts them from an lvalue into an rvalue
r = add_rule(XRP, IR_ADDRESS_OF, RP1, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq"); fin_rule(r);
r = add_rule(XRP, IR_ADDRESS_OF, RP2, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq"); fin_rule(r);
r = add_rule(XRP, IR_ADDRESS_OF, RP3, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq"); fin_rule(r);
r = add_rule(XRP, IR_ADDRESS_OF, RP4, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq"); fin_rule(r);
r = add_rule(RP5, IR_ADDRESS_OF, RP5, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq"); fin_rule(r);
r = add_rule(XRP, IR_ADDRESS_OF, RP5, 0, 1); add_op(r, X_MOV, DST, SRC1, 0, "movq %v1q, %vdq"); fin_rule(r);
// Common rules for IR_ADDRESS_OF and IR_ADDRESS_OF_FROM_GOT
add_address_of_rule(XRP, XMI, "%v1q, %vdq", 1);
add_address_of_rule(XRP, XMU, "%v1q, %vdq", 1);
add_address_of_rule(XRP, MS3, "%v1q, %vdq", 1);
add_address_of_rule(XRP, MS4, "%v1q, %vdq", 1);
add_address_of_rule(XRP, MPV, "%v1q, %vdq", 1);
add_address_of_rule(RP5, MLD5, "%v1L, %vdq", 0);
add_address_of_rule(RP1, MSA, "%v1q, %vdq", 0);
add_address_of_rule(RP2, MSA, "%v1q, %vdq", 0);
add_address_of_rule(RP3, MSA, "%v1q, %vdq", 0);
add_address_of_rule(RP4, MSA, "%v1q, %vdq", 0);
add_address_of_rule(RP5, MSA, "%v1q, %vdq", 0);
add_address_of_rule(RPF, FUN, "%v1q, %vdq", 0);
add_address_of_rule(RP4, MPF, "%v1q, %vdq", 0);
// Stores of a pointer to a pointer
for (int dst = RP1; dst <= RP4; dst++)
for (int src = RP1; src <= RP5; src++)
add_move_to_ptr(dst, src, 0, "movq %v2q, %v1o(%v1q)");
// Stores to a pointer
for (int is_unsigned = 0; is_unsigned < 2; is_unsigned++) {
add_move_to_ptr(RP1, is_unsigned ? RU1 : RI1, 0, "movb %v2b, %v1o(%v1q)");
add_move_to_ptr(RP1, is_unsigned ? RU2 : RI2, 0, "movb %v2b, %v1o(%v1q)");
add_move_to_ptr(RP2, is_unsigned ? RU2 : RI2, 0, "movw %v2w, %v1o(%v1q)");
add_move_to_ptr(RP1, is_unsigned ? RU3 : RI3, 0, "movb %v2b, %v1o(%v1q)");
add_move_to_ptr(RP2, is_unsigned ? RU3 : RI3, 0, "movw %v2w, %v1o(%v1q)");
add_move_to_ptr(RP3, is_unsigned ? RU3 : RI3, 0, "movl %v2l, %v1o(%v1q)");
add_move_to_ptr(RP1, is_unsigned ? RU4 : RI4, 0, "movb %v2b, %v1o(%v1q)");
add_move_to_ptr(RP2, is_unsigned ? RU4 : RI4, 0, "movw %v2w, %v1o(%v1q)");
add_move_to_ptr(RP3, is_unsigned ? RU4 : RI4, 0, "movl %v2l, %v1o(%v1q)");
add_move_to_ptr(RP4, is_unsigned ? RU4 : RI4, 0, "movq %v2q, %v1o(%v1q)");
}
// Move a pointer to a function to a pointer to a pointer to a function
add_move_to_ptr(RP4, RPF, 0, "movq %v2q, %v1o(%v1q)");
// Note, CI4 cannot be written to RP4 since there is no instruction for it
add_move_to_ptr(RP1, CI1, 0, "movb $%v2b, %v1o(%v1q)");
add_move_to_ptr(RP2, CI2, 0, "movw $%v2w, %v1o(%v1q)");
add_move_to_ptr(RP3, CI3, 0, "movl $%v2l, %v1o(%v1q)");
add_move_to_ptr(RP4, CI3, 0, "movq $%v2q, %v1o(%v1q)");
add_move_to_ptr(RP3, RS3, 0, "movss %v2F, %v1o(%v1q)");
add_move_to_ptr(RP4, RS4, 0, "movsd %v2D, %v1o(%v1q)");
}
static void add_conditional_zero_jump_rule(int operation, int src1, int src2, int cost, int x86_cmp_operation, char *comparison, char *conditional_jmp, int do_fin_rule) {
int x86_jmp_operation = operation == IR_JZ ? X_JZ : X_JNZ;
Rule *r = add_rule(0, operation, src1, src2, cost);
add_op(r, x86_cmp_operation, 0, SRC1, 0, comparison);
add_op(r, x86_jmp_operation, 0, SRC2, 0, conditional_jmp);
if (do_fin_rule) fin_rule(r);
}
// Add integer comparision conditional jump rule
static void add_int_comp_cond_jmp_rule(int *ntc, int src1, int src2, char *template, int op, int x86op1, char *t1, int x86op2, char *t2) {
Rule *r;
(*ntc)++;
r = add_rule(*ntc, op, src1, src2, 10); add_op(r, X_CMP, 0, SRC1, SRC2, template ); fin_rule(r);
r = add_rule(0, IR_JNZ, *ntc, LAB, 1 ); add_op(r, x86op1, 0, SRC2, 0, t1); fin_rule(r);
r = add_rule(0, IR_JZ, *ntc, LAB, 1 ); add_op(r, x86op2, 0, SRC2, 0, t2); fin_rule(r);
}
// Add integer comparision conditional jump rules
static void add_int_comp_cond_jmp_rules(int *ntc, int is_unsigned, int src1, int src2, char *template) {
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_EQ, X_JE, "je %v1", X_JNE, "jne %v1");
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_NE, X_JNE, "jne %v1", X_JE, "je %v1");
if (is_unsigned) {
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_LT, X_JB, "jb %v1" , X_JAE, "jae %v1" );
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_GT, X_JA, "ja %v1", X_JBE, "jbe %v1");
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_LE, X_JBE, "jbe %v1", X_JA, "ja %v1");
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_GE, X_JAE, "jae %v1", X_JB, "jb %v1");
}
else {
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_LT, X_JLT, "jl %v1" , X_JGE, "jge %v1" );
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_GT, X_JGT, "jg %v1", X_JLE, "jle %v1");
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_LE, X_JLE, "jle %v1", X_JGT, "jg %v1");
add_int_comp_cond_jmp_rule(ntc, src1, src2, template, IR_GE, X_JGE, "jge %v1", X_JLT, "jl %v1");
}
}
static void add_int_comparison_assignment_rule(int src1, int src2, char *cmp_template, int operation, int set_operation, char *set_template) {
// Comparison operators always return an int
Rule *r = add_rule(RI3, operation, src1, src2, 12);
add_op(r, X_CMP, 0, SRC1, SRC2, cmp_template);
add_op(r, set_operation, DST, 0, 0, set_template);
add_op(r, X_MOVZ, DST, DST, 0, "movzbl %v1b, %v1l");
fin_rule(r);
}
// Comparison and assignment rules for integers
static void add_int_comp_assignment_rules(int is_unsigned, int src1, int src2, char *template) {
add_int_comparison_assignment_rule(src1, src2, template, IR_EQ, X_SETE, "sete %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_NE, X_SETNE, "setne %vdb");
if (is_unsigned) {
add_int_comparison_assignment_rule(src1, src2, template, IR_LT, X_SETB, "setb %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_GT, X_SETA, "seta %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_LE, X_SETBE, "setbe %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_GE, X_SETAE, "setae %vdb");
}
else {
add_int_comparison_assignment_rule(src1, src2, template, IR_LT, X_SETLT, "setl %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_GT, X_SETGT, "setg %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_LE, X_SETLE, "setle %vdb");
add_int_comparison_assignment_rule(src1, src2, template, IR_GE, X_SETGE, "setge %vdb");
}
}
// Add conditional jump and assignment rules for an integer comparison
static void add_int_comparison_rules(int *ntc, int is_unsigned, int src1, int src2, char *template) {
add_int_comp_cond_jmp_rules(ntc, is_unsigned, src1, src2, template);
add_int_comp_assignment_rules(is_unsigned, src1, src2, template);
}
static void add_long_double_comparison_instructions(Rule *r, int src1, int src2, char *src1_template, char *src2_template, char *template, int src1_first) {
if (src1_first) {
add_op(r, X_MOVC, 0, SRC1, 0, src1_template);
add_op(r, X_MOVC, 0, SRC2, 0, src2_template);
}
else {
add_op(r, X_MOVC, 0, SRC2, 0, src2_template);
add_op(r, X_MOVC, 0, SRC1, 0, src1_template);
}
add_op(r, X_CMP, 0, 0, 0, template);
add_op(r, X_MOVC, 0, 0, 0, "fstp %%st(0)");
}
static void add_long_double_comp_assignment_rule(int src1, int src2, char *src1_template, char *src2_template, int operation, int set_operation, char *set_template, int src1_first) {
// Comparison operators always return an int
Rule *r = add_rule(RI3, operation, src1, src2, 15);
add_long_double_comparison_instructions(r, src1, src2, src1_template, src2_template, "fcomip %%st(1), %%st", src1_first);
add_op(r, set_operation, DST, 0, 0, set_template);
add_op(r, X_MOVZ, DST, DST, 0, "movzbl %v1b, %v1l");
}
static void add_long_double_comp_cond_jmp_rule(int *ntc, int src1, int src2, char *src1_template, char *src2_template, int operation, int x86op1, char *t1, int x86op2, char *t2, int src1_first) {
Rule *r;
(*ntc)++;
r = add_rule(*ntc, operation, src1, src2, 14);
add_long_double_comparison_instructions(r, src1, src2, src1_template, src2_template, "fcomip %%st(1), %%st", src1_first);
r = add_rule(0, IR_JNZ, *ntc, LAB, 1); add_op(r, x86op1, 0, SRC2, 0, t1); fin_rule(r);
r = add_rule(0, IR_JZ, *ntc, LAB, 1); add_op(r, x86op2, 0, SRC2, 0, t2); fin_rule(r);
}
// Comparison and assignment/jump rules for floating point numbers
static void add_long_double_comp_rules(int *ntc, int src1, int src2, char *src1_template, char *src2_template) {
add_long_double_comp_assignment_rule(src1, src2, src1_template, src2_template, IR_LT, X_SETA, "seta %vdb", 1);