aboutsummaryrefslogtreecommitdiffstats
path: root/kvx/Asmblockgen.v
blob: 07b31b11e0513ac68e461aa690cc1cf8dd226adf (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
(* *************************************************************)
(*                                                             *)
(*             The Compcert verified compiler                  *)
(*                                                             *)
(*           Sylvain Boulmé     Grenoble-INP, VERIMAG          *)
(*           Xavier Leroy       INRIA Paris-Rocquencourt       *)
(*           David Monniaux     CNRS, VERIMAG                  *)
(*           Cyril Six          Kalray                         *)
(*                                                             *)
(*  Copyright Kalray. Copyright VERIMAG. All rights reserved.  *)
(*  This file is distributed under the terms of the INRIA      *)
(*  Non-Commercial License Agreement.                          *)
(*                                                             *)
(* *************************************************************)

(** * Translation from Machblock to KVX assembly language (Asmblock) 
    Inspired from the Mach->Asm pass of other backends, but adapted to the block structure *)

Require Archi.
Require Import Coqlib Errors.
Require Import AST Integers Floats Memdata.
Require Import Op Locations Machblock Asmvliw Asmblock.
Require ExtValues.
Require Import Chunks.

Local Open Scope string_scope.
Local Open Scope error_monad_scope.

Import PArithCoercions.

(** The code generation functions take advantage of several
  characteristics of the [Mach] code generated by earlier passes of the
  compiler, mostly that argument and result registers are of the correct
  types.  These properties are true by construction, but it's easier to
  recheck them during code generation and fail if they do not hold. *)

(** Extracting integer or float registers. *)

Inductive immed32 : Type :=
  | Imm32_single (imm: int).

Definition make_immed32 (val: int) := Imm32_single val.

Inductive immed64 : Type :=
  | Imm64_single (imm: int64)
.

Definition make_immed64 (val: int64) := Imm64_single val.

Notation "a ::g b" := (cons (A:=instruction) a b) (at level 49, right associativity).
Notation "a ::i b" := (cons (A:=basic) a b) (at level 49, right associativity).
Notation "a ::b lb" := ((bblock_single_inst a) :: lb) (at level 49, right associativity).
Notation "a ++g b" := (app (A:=instruction) a b) (at level 49, right associativity).
Notation "a @@ b" := (app a b) (at level 49, right associativity).

Definition loadimm32 (r: ireg) (n: int) :=
  match make_immed32 n with
  | Imm32_single imm => Pmake r imm
  end.

Definition opimm32 (op: arith_name_rrr)
                   (opimm: arith_name_rri32)
                   (rd rs: ireg) (n: int) :=
  match make_immed32 n with
  | Imm32_single imm => opimm rd rs imm
  end.

Definition addimm32 := opimm32 Paddw Paddiw.
Definition mulimm32 := opimm32 Pmulw Pmuliw.
Definition andimm32 := opimm32 Pandw Pandiw.
Definition nandimm32 := opimm32 Pnandw Pnandiw.
Definition orimm32  := opimm32 Porw Poriw.
Definition norimm32 := opimm32 Pnorw Pnoriw.
Definition xorimm32 := opimm32 Pxorw Pxoriw.
Definition nxorimm32 := opimm32 Pnxorw Pnxoriw.

Definition loadimm64 (r: ireg) (n: int64) :=
  match make_immed64 n with
  | Imm64_single imm => Pmakel r imm
  end.

Definition opimm64 (op: arith_name_rrr)
                   (opimm: arith_name_rri64)
                   (rd rs: ireg) (n: int64) :=
  match make_immed64 n with
  | Imm64_single imm => opimm rd rs imm
end.

Definition addimm64 := opimm64 Paddl Paddil.
Definition mulimm64 := opimm64 Pmull Pmulil.
Definition orimm64  := opimm64 Porl  Poril.
Definition andimm64 := opimm64 Pandl Pandil.
Definition xorimm64 := opimm64 Pxorl  Pxoril.
Definition norimm64  := opimm64 Pnorl  Pnoril.
Definition nandimm64 := opimm64 Pnandl Pnandil.
Definition nxorimm64 := opimm64 Pnxorl  Pnxoril.

Definition addptrofs (rd rs: ireg) (n: ptrofs) :=
  if Ptrofs.eq_dec n Ptrofs.zero then
    Pmv rd rs
  else
    addimm64 rd rs (Ptrofs.to_int64 n).

(** Translation of conditional branches. *)

Definition transl_comp
    (c: comparison) (s: signedness) (r1 r2: ireg) (lbl: label) (k: code) : list instruction :=
  Pcompw (itest_for_cmp c s) RTMP r1 r2 ::g Pcb BTwnez RTMP lbl ::g k.

Definition transl_compi
    (c: comparison) (s: signedness) (r: ireg) (imm: int) (lbl: label) (k: code) : list instruction :=
  Pcompiw (itest_for_cmp c s) RTMP r imm ::g Pcb BTwnez RTMP lbl ::g k.

Definition transl_compl
    (c: comparison) (s: signedness) (r1 r2: ireg) (lbl: label) (k: code) : list instruction :=
  Pcompl (itest_for_cmp c s) RTMP r1 r2 ::g Pcb BTwnez RTMP lbl ::g k.

Definition transl_compil
    (c: comparison) (s: signedness) (r: ireg) (imm: int64) (lbl: label) (k: code) : list instruction :=
  Pcompil (itest_for_cmp c s) RTMP r imm ::g Pcb BTwnez RTMP lbl ::g k.

Definition select_comp (n: int) (c: comparison) : option comparison :=
  if Int.eq n Int.zero then
    match c with
    | Ceq => Some Ceq
    | Cne => Some Cne
    | _   => None
    end
  else
    None
  .

Definition transl_opt_compuimm
    (n: int) (c: comparison) (r1: ireg) (lbl: label) (k: code) : list instruction :=
  if Int.eq n Int.zero then
    match c with
    | Ceq => Pcbu BTweqz r1 lbl ::g k
    | Cne => Pcbu BTwnez r1 lbl ::g k
    | _ => transl_compi c Unsigned r1 n lbl k
    end
  else
    transl_compi c Unsigned r1 n lbl k
  .

Definition select_compl (n: int64) (c: comparison) : option comparison :=
  if Int64.eq n Int64.zero then
    match c with
    | Ceq => Some Ceq
    | Cne => Some Cne
    | _   => None
    end
  else
    None
  .

Definition transl_opt_compluimm
    (n: int64) (c: comparison) (r1: ireg) (lbl: label) (k: code) : list instruction :=
  if Int64.eq n Int64.zero then
    match c with
    | Ceq => Pcbu BTdeqz r1 lbl ::g k
    | Cne => Pcbu BTdnez r1 lbl ::g k
    | _ => transl_compil c Unsigned r1 n lbl k
    end
  else
    transl_compil c Unsigned r1 n lbl k
  .

Definition transl_comp_float32 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
  match ftest_for_cmp cmp with
  | Normal ft => Pfcompw ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
  | Reversed ft => Pfcompw ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
  end.

Definition transl_comp_notfloat32 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
  match notftest_for_cmp cmp with
  | Normal ft => Pfcompw ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
  | Reversed ft => Pfcompw ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
  end.

Definition transl_comp_float64 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
  match ftest_for_cmp cmp with
  | Normal ft => Pfcompl ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
  | Reversed ft => Pfcompl ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
  end.

Definition transl_comp_notfloat64 (cmp: comparison) (r1 r2: ireg) (lbl: label) (k: code) :=
  match notftest_for_cmp cmp with
  | Normal ft => Pfcompl ft GPR32 r1 r2 ::g Pcb BTwnez GPR32 lbl ::g k
  | Reversed ft => Pfcompl ft GPR32 r2 r1 ::g Pcb BTwnez GPR32 lbl ::g k
  end.

Definition transl_cbranch
    (cond: condition) (args: list mreg) (lbl: label) (k: code) : res (list instruction ) :=
  match cond, args with
  | Ccompuimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (transl_opt_compuimm n c r1 lbl k)
  | Ccomp c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_comp c Signed r1 r2 lbl k)
  | Ccompu c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_comp c Unsigned r1 r2 lbl k)
  | Ccompimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (if Int.eq n Int.zero then
            Pcb (btest_for_cmpswz c) r1 lbl ::g k
          else
            transl_compi c Signed r1 n lbl k
         )
  | Ccompluimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (transl_opt_compluimm n c r1 lbl k)
  | Ccompl c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_compl c Signed r1 r2 lbl k)
  | Ccomplu c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_compl c Unsigned r1 r2 lbl k)
  | Ccomplimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (if Int64.eq n Int64.zero then
            Pcb (btest_for_cmpsdz c) r1 lbl ::g k
          else
            transl_compil c Signed r1 n lbl k
         )
  | Ccompf c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_comp_float64 c r1 r2 lbl k)
  | Cnotcompf c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_comp_notfloat64 c r1 r2 lbl k)
  | Ccompfs c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_comp_float32 c r1 r2 lbl k)
  | Cnotcompfs c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_comp_notfloat32 c r1 r2 lbl k)
  | _, _ =>
      Error(msg "Asmgenblock.transl_cbranch")
  end.

(** Translation of a condition operator.  The generated code sets the
  [rd] target register to 0 or 1 depending on the truth value of the
  condition. *)

Definition transl_cond_int32s (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  Pcompw (itest_for_cmp cmp Signed) rd r1 r2 ::i k.

Definition transl_cond_int32u (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  Pcompw (itest_for_cmp cmp Unsigned) rd r1 r2 ::i k.

Definition transl_cond_int64s (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  Pcompl (itest_for_cmp cmp Signed) rd r1 r2 ::i k.

Definition transl_cond_int64u (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  Pcompl (itest_for_cmp cmp Unsigned) rd r1 r2 ::i k.

Definition transl_condimm_int32s (cmp: comparison) (rd r1: ireg) (n: int) (k: bcode) :=
  Pcompiw (itest_for_cmp cmp Signed) rd r1 n ::i k.

Definition transl_condimm_int32u (cmp: comparison) (rd r1: ireg) (n: int) (k: bcode) :=
  Pcompiw (itest_for_cmp cmp Unsigned) rd r1 n ::i k.

Definition transl_condimm_int64s (cmp: comparison) (rd r1: ireg) (n: int64) (k: bcode) :=
  Pcompil (itest_for_cmp cmp Signed) rd r1 n ::i k.

Definition transl_condimm_int64u (cmp: comparison) (rd r1: ireg) (n: int64) (k: bcode) :=
  Pcompil (itest_for_cmp cmp Unsigned) rd r1 n ::i k.


Definition transl_cond_float32 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  match ftest_for_cmp cmp with
  | Normal ft => Pfcompw ft rd r1 r2 ::i k
  | Reversed ft => Pfcompw ft rd r2 r1 ::i k
  end.

Definition transl_cond_notfloat32 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  match notftest_for_cmp cmp with
  | Normal ft => Pfcompw ft rd r1 r2 ::i k
  | Reversed ft => Pfcompw ft rd r2 r1 ::i k
  end.

Definition transl_cond_float64 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  match ftest_for_cmp cmp with
  | Normal ft => Pfcompl ft rd r1 r2 ::i k
  | Reversed ft => Pfcompl ft rd r2 r1 ::i k
  end.

Definition transl_cond_notfloat64 (cmp: comparison) (rd r1 r2: ireg) (k: bcode) :=
  match notftest_for_cmp cmp with
  | Normal ft => Pfcompl ft rd r1 r2 ::i k
  | Reversed ft => Pfcompl ft rd r2 r1 ::i k
  end.


(* CoMPare Unsigned Words to Zero *)
Definition btest_for_cmpuwz (c: comparison) :=
  match c with
  | Cne => OK BTwnez
  | Ceq => OK BTweqz
  | Clt => Error (msg "btest_for_compuwz: Clt") (* TODO reachable *)
  | Cge => Error (msg "btest_for_compuwz: Cge")
  | Cle => OK BTweqz
  | Cgt => OK BTwnez
  end.

(* CoMPare Unsigned Words to Zero *)
Definition btest_for_cmpudz (c: comparison) :=
  match c with
  | Cne => OK BTdnez
  | Ceq => OK BTdeqz
  | Clt => Error (msg "btest_for_compudz: Clt")
  | Cge => Error (msg "btest_for_compudz: Cge")
  | Cle => OK BTdeqz
  | Cgt => OK BTdnez
  end.

Definition conditional_move (cond0 : condition0) (rc rd rs : ireg) :
  res basic :=
  if ireg_eq rd rs
  then OK Pnop
  else
    (match cond0 with
     | Ccomp0 cmp =>
       OK (PArith (Pcmove (btest_for_cmpswz cmp) rd rc rs))
     | Ccompu0 cmp =>
       do bt <- btest_for_cmpuwz cmp;
         OK (PArith (Pcmoveu bt rd rc rs))
     | Ccompl0 cmp =>
       OK (PArith (Pcmove (btest_for_cmpsdz cmp) rd rc rs))
     | Ccomplu0 cmp =>
       do bt <- btest_for_cmpudz cmp;
         OK (PArith (Pcmoveu bt rd rc rs))
     end).

Definition conditional_move_imm32 (cond0 : condition0) (rc rd : ireg) (imm : int) : res basic :=
  match cond0 with
  | Ccomp0 cmp =>
    OK (PArith (Pcmoveiw (btest_for_cmpswz cmp) rd rc imm))
  | Ccompu0 cmp =>
    do bt <- btest_for_cmpuwz cmp;
      OK (PArith (Pcmoveuiw bt rd rc imm))
  | Ccompl0 cmp =>
    OK (PArith (Pcmoveiw (btest_for_cmpsdz cmp) rd rc imm))
  | Ccomplu0 cmp =>
    do bt <- btest_for_cmpudz cmp;
      OK (PArith (Pcmoveuiw bt rd rc imm))
  end.

Definition conditional_move_imm64 (cond0 : condition0) (rc rd : ireg) (imm : int64) : res basic :=
  match cond0 with
  | Ccomp0 cmp =>
    OK (PArith (Pcmoveil (btest_for_cmpswz cmp) rd rc imm))
  | Ccompu0 cmp =>
    do bt <- btest_for_cmpuwz cmp;
      OK (PArith (Pcmoveuil bt rd rc imm))
  | Ccompl0 cmp =>
    OK (PArith (Pcmoveil (btest_for_cmpsdz cmp) rd rc imm))
  | Ccomplu0 cmp =>
    do bt <- btest_for_cmpudz cmp;
      OK (PArith (Pcmoveuil bt rd rc imm))
  end.

Definition transl_cond_op
           (cond: condition) (rd: ireg) (args: list mreg) (k: bcode) :=
  match cond, args with
  | Ccomp c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_int32s c rd r1 r2 k)
  | Ccompu c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_int32u c rd r1 r2 k)
  | Ccompimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (transl_condimm_int32s c rd r1 n k)
  | Ccompuimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (transl_condimm_int32u c rd r1 n k)
  | Ccompl c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_int64s c rd r1 r2 k)
  | Ccomplu c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_int64u c rd r1 r2 k)
  | Ccomplimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (transl_condimm_int64s c rd r1 n k)
  | Ccompluimm c n, a1 :: nil =>
      do r1 <- ireg_of a1;
      OK (transl_condimm_int64u c rd r1 n k)
  | Ccompfs c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_float32 c rd r1 r2 k)
  | Cnotcompfs c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_notfloat32 c rd r1 r2 k)
  | Ccompf c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_float64 c rd r1 r2 k)
  | Cnotcompf c, a1 :: a2 :: nil =>
      do r1 <- ireg_of a1; do r2 <- ireg_of a2;
      OK (transl_cond_notfloat64 c rd r1 r2 k)
  | _, _ =>
      Error(msg "Asmblockgen.transl_cond_op")
end.

(** Translation of the arithmetic operation [r <- op(args)].
  The corresponding instructions are prepended to [k]. *)

Definition transl_op
              (op: operation) (args: list mreg) (res: mreg) (k: bcode) :=
  match op, args with
  | Omove, a1 :: nil =>
      match preg_of res, preg_of a1 with
      | IR r, IR a => OK (Pmv r a ::i k)
      |  _  ,  _   => Error(msg "Asmgenblock.transl_op: Omove")
      end
  | Ointconst n, nil =>
      do rd <- ireg_of res;
      OK (loadimm32 rd n ::i k)
  | Olongconst n, nil =>
      do rd <- ireg_of res;
      OK (loadimm64 rd n ::i k)
  | Ofloatconst f, nil =>
      do rd <- freg_of res;
      OK (Pmakef rd f ::i k)
  | Osingleconst f, nil =>
      do rd <- freg_of res;
      OK (Pmakefs rd f ::i k)
  | Oaddrsymbol s ofs, nil =>
      do rd <- ireg_of res;
      OK (if Archi.pic_code tt && negb (Ptrofs.eq ofs Ptrofs.zero)
          then Ploadsymbol s Ptrofs.zero rd ::i addptrofs rd rd ofs ::i k
          else Ploadsymbol s ofs rd ::i k)
  | Oaddrstack n, nil =>
      do rd <- ireg_of res;
      OK (addptrofs rd SP n ::i k)

  | Ocast8signed, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Pslliw rd rs (Int.repr 24) ::i Psraiw rd rd (Int.repr 24) ::i k)
  | Ocast16signed, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Pslliw rd rs (Int.repr 16) ::i Psraiw rd rd (Int.repr 16) ::i k)
  | Oadd, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Paddw rd rs1 rs2 ::i k)
  | Oaddimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (addimm32 rd rs n ::i k)
  | Oaddx shift, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Paddxw shift rd rs1 rs2 ::i k)
  | Oaddximm shift n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Paddxiw shift rd rs n ::i k)
  | Oaddxl shift, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Paddxl shift rd rs1 rs2 ::i k)
  | Oaddxlimm shift n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Paddxil shift rd rs n ::i k)
  | Oneg, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pnegw rd rs ::i k)
  | Osub, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psubw rd rs1 rs2 ::i k)
  | Orevsubimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Prevsubiw rd rs n ::i k)
  | Orevsubx shift, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Prevsubxw shift rd rs1 rs2 ::i k)
  | Orevsubximm shift n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Prevsubxiw shift rd rs n ::i k)
  | Omul, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pmulw rd rs1 rs2 ::i k)
  | Omulimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1;
      OK (mulimm32 rd rs1 n ::i k)
  | Omulhs, _ => Error(msg "Asmblockgen.transl_op: Omulhs") (* Normalement pas émis *)
  | Omulhu, _ => Error(msg "Asmblockgen.transl_op: Omulhu") (* Normalement pas émis *)
  | Oand, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pandw rd rs1 rs2 ::i k)
  | Oandimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (andimm32 rd rs n ::i k)
  | Onand, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pnandw rd rs1 rs2 ::i k)
  | Onandimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (nandimm32 rd rs n ::i k)
  | Oor, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Porw rd rs1 rs2 ::i k)
  | Onor, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pnorw rd rs1 rs2 ::i k)
  | Oorimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (orimm32 rd rs n ::i k)
  | Onorimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (norimm32 rd rs n ::i k)
  | Oxor, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pxorw rd rs1 rs2 ::i k)
  | Oxorimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (xorimm32 rd rs n ::i k)
  | Onxor, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pnxorw rd rs1 rs2 ::i k)
  | Onxorimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
        OK (nxorimm32 rd rs n ::i k)
  | Onot, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
        OK (xorimm32 rd rs Int.mone ::i k)
  | Oandn, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pandnw rd rs1 rs2 ::i k)
  | Oandnimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pandniw rd rs n ::i k)
  | Oorn, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pornw rd rs1 rs2 ::i k)
  | Oornimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Porniw rd rs n ::i k)
  | Oabsdiff, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pabdw rd rs1 rs2 ::i k)
  | Oabsdiffimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pabdiw rd rs n ::i k)
  | Oshl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psllw rd rs1 rs2 ::i k)
  | Oshlimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Pslliw rd rs n ::i k)
  | Oshr, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psraw rd rs1 rs2 ::i k)
  | Oshrimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psraiw rd rs n ::i k)
  | Oshru, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psrlw rd rs1 rs2 ::i k)
  | Oshruimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psrliw rd rs n ::i k)
  | Oshrximm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psrxiw rd rs n ::i k)
  | Ororimm n, a1 :: nil =>
    do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Proriw rd rs n ::i k)
  | Omadd, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
      do r1 <- ireg_of a1;
      do r2 <- ireg_of a2;
      do r3 <- ireg_of a3;
        OK (Pmaddw r1 r2 r3 ::i k)
  | Omaddimm n, a1 :: a2 :: nil =>
    assertion (mreg_eq a1 res);
      do r1 <- ireg_of a1;
      do r2 <- ireg_of a2;
        OK (Pmaddiw r1 r2 n ::i k)
  | Omsub, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
      do r1 <- ireg_of a1;
      do r2 <- ireg_of a2;
      do r3 <- ireg_of a3;
        OK (Pmsubw r1 r2 r3 ::i k)
  (* [Omakelong], [Ohighlong]  should not occur *)
  | Olowlong, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Pcvtl2w rd rs ::i k)  
  | Ocast32signed, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psxwd rd rs ::i k)
  | Ocast32unsigned, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Pzxwd rd rs ::i k)
(*       assertion (ireg_eq rd rs);
      OK (Pcvtw2l rd ::i Psllil rd rd (Int.repr 32) ::i Psrlil rd rd (Int.repr 32) ::i k) *)
  | Oaddl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Paddl rd rs1 rs2 ::i k)
  | Oaddlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (addimm64 rd rs n ::i k)
  | Onegl, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pnegl rd rs ::i k)
  | Osubl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psubl rd rs1 rs2 ::i k)
  | Orevsubxl shift, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Prevsubxl shift rd rs1 rs2 ::i k)
  | Orevsublimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Prevsubil rd rs n ::i k)
  | Orevsubxlimm shift n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Prevsubxil shift rd rs n ::i k)
  | Omull, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pmull rd rs1 rs2 ::i k)
  | Omullimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1;
      OK (mulimm64 rd rs1 n ::i k)
  | Omullhs, _ => Error (msg "Asmblockgen.transl_op: Omullhs") (* Normalement pas émis *)
  | Omullhu, _ => Error (msg "Asmblockgen.transl_op: Omullhu") (* Normalement pas émis *)
  | Odivl, _ => Error (msg "Asmblockgen.transl_op: Odivl") (* Géré par fonction externe *)
  | Odivlu, _ => Error (msg "Asmblockgen.transl_op: Odivlu") (* Géré par fonction externe *)
  | Omodl, _ => Error (msg "Asmblockgen.transl_op: Omodl") (* Géré par fonction externe *)
  | Omodlu, _ => Error (msg "Asmblockgen.transl_op: Omodlu") (* Géré par fonction externe *)
  | Onotl, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
        OK (xorimm64 rd rs Int64.mone ::i k)
  | Oandl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pandl rd rs1 rs2 ::i k)
  | Oandlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (andimm64 rd rs n ::i k)
  | Onandl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pnandl rd rs1 rs2 ::i k)
  | Onandlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (nandimm64 rd rs n ::i k)
  | Oorl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Porl rd rs1 rs2 ::i k)
  | Oorlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (orimm64 rd rs n ::i k)
  | Onorl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pnorl rd rs1 rs2 ::i k)
  | Onorlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (norimm64 rd rs n ::i k)
  | Oxorl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pxorl rd rs1 rs2 ::i k)
  | Oxorlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (xorimm64 rd rs n ::i k)
  | Onxorl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pnxorl rd rs1 rs2 ::i k)
  | Onxorlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (nxorimm64 rd rs n ::i k)
  | Oandnl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pandnl rd rs1 rs2 ::i k)
  | Oandnlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pandnil rd rs n ::i k)
  | Oornl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pornl rd rs1 rs2 ::i k)
  | Oornlimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pornil rd rs n ::i k)
  | Oabsdiffl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pabdl rd rs1 rs2 ::i k)
  | Oabsdifflimm n, a1 :: nil =>
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pabdil rd rs n ::i k)
  | Oshll, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Pslll rd rs1 rs2 ::i k)
  | Oshllimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psllil rd rs n ::i k)
  | Oshrl, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psral rd rs1 rs2 ::i k)
  | Oshrlimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psrail rd rs n ::i k)
  | Oshrlu, a1 :: a2 :: nil =>
      do rd <- ireg_of res; do rs1 <- ireg_of a1; do rs2 <- ireg_of a2;
      OK (Psrll rd rs1 rs2 ::i k)
  | Oshrluimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psrlil rd rs n ::i k)
  | Oshrxlimm n, a1 :: nil =>
      do rd <- ireg_of res; do rs <- ireg_of a1;
      OK (Psrxil rd rs n ::i k)
  | Omaddl, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
      do r1 <- ireg_of a1;
      do r2 <- ireg_of a2;
      do r3 <- ireg_of a3;
        OK (Pmaddl r1 r2 r3 ::i k)
  | Omaddlimm n, a1 :: a2 :: nil =>
    assertion (mreg_eq a1 res);
      do r1 <- ireg_of a1;
      do r2 <- ireg_of a2;
        OK (Pmaddil r1 r2 n ::i k)
  | Omsubl, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
      do r1 <- ireg_of a1;
      do r2 <- ireg_of a2;
      do r3 <- ireg_of a3;
        OK (Pmsubl r1 r2 r3 ::i k)
  | Oabsf, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfabsd rd rs ::i k)
  | Oabsfs, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfabsw rd rs ::i k)
  | Oaddf, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfaddd rd rs1 rs2 ::i k)
  | Oaddfs, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfaddw rd rs1 rs2 ::i k)
  | Osubf, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfsbfd rd rs1 rs2 ::i k)
  | Osubfs, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfsbfw rd rs1 rs2 ::i k)
  | Omulf, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfmuld rd rs1 rs2 ::i k)
  | Omulfs, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfmulw rd rs1 rs2 ::i k)
  | Ominf, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfmind rd rs1 rs2 ::i k)
  | Ominfs, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfminw rd rs1 rs2 ::i k)
  | Omaxf, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfmaxd rd rs1 rs2 ::i k)
  | Omaxfs, a1 :: a2 :: nil =>
      do rd <- freg_of res; do rs1 <- freg_of a1; do rs2 <- freg_of a2;
      OK (Pfmaxw rd rs1 rs2 ::i k)
  | Onegf, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfnegd rd rs ::i k)
  | Onegfs, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfnegw rd rs ::i k)
  | Oinvfs, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfinvw rd rs ::i k)

  | Ofmaddf, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
        do rs1 <- freg_of a1;
        do rs2 <- freg_of a2;
        do rs3 <- freg_of a3;
      OK (Pfmaddfl rs1 rs2 rs3 ::i k)
  | Ofmaddfs, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
        do rs1 <- freg_of a1;
        do rs2 <- freg_of a2;
        do rs3 <- freg_of a3;
      OK (Pfmaddfw rs1 rs2 rs3 ::i k)
  | Ofmsubf, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
        do rs1 <- freg_of a1;
        do rs2 <- freg_of a2;
        do rs3 <- freg_of a3;
      OK (Pfmsubfl rs1 rs2 rs3 ::i k)
  | Ofmsubfs, a1 :: a2 :: a3 :: nil =>
    assertion (mreg_eq a1 res);
        do rs1 <- freg_of a1;
        do rs2 <- freg_of a2;
        do rs3 <- freg_of a3;
      OK (Pfmsubfw rs1 rs2 rs3 ::i k)

  | Osingleofint, a1 :: nil =>
      do rd <- freg_of res; do rs <- ireg_of a1;
      OK (Pfloatwrnsz rd rs ::i k)
  | Osingleofintu, a1 :: nil =>
      do rd <- freg_of res; do rs <- ireg_of a1;
      OK (Pfloatuwrnsz rd rs ::i k)
  | Ofloatoflong, a1 :: nil =>
      do rd <- freg_of res; do rs <- ireg_of a1;
      OK (Pfloatdrnsz rd rs ::i k)
  | Ofloatoflongu, a1 :: nil =>
      do rd <- freg_of res; do rs <- ireg_of a1;
      OK (Pfloatudrnsz rd rs ::i k)
  | Ointofsingle, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixedwrzz rd rs ::i k)
  | Ointuofsingle, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixeduwrzz rd rs ::i k)
  | Ointofsingle_ne, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixedwrne rd rs ::i k)
  | Ointuofsingle_ne, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixeduwrne rd rs ::i k)
  | Olongoffloat, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixeddrzz rd rs ::i k)
  | Ointoffloat, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixeddrzz_i32 rd rs ::i k)
  | Ointuoffloat, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixedudrzz_i32 rd rs ::i k)
  | Olonguoffloat, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixedudrzz rd rs ::i k)
  | Olongoffloat_ne, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixeddrne rd rs ::i k)
  | Olonguoffloat_ne, a1 :: nil =>
      do rd <- ireg_of res; do rs <- freg_of a1;
      OK (Pfixedudrne rd rs ::i k)

  | Ofloatofsingle, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfwidenlwd rd rs ::i k)
  | Osingleoffloat, a1 :: nil =>
      do rd <- freg_of res; do rs <- freg_of a1;
      OK (Pfnarrowdw rd rs ::i k)


  | Odivf , _ => Error (msg "Asmblockgen.transl_op: Odivf")
  | Odivfs, _ => Error (msg "Asmblockgen.transl_op: Odivfs")

  (* We use the Splitlong instead for these four conversions *)
  | Osingleoflong , _ => Error (msg "Asmblockgen.transl_op: Osingleoflong") 
  | Osingleoflongu , _ => Error (msg "Asmblockgen.transl_op: Osingleoflongu")
  | Olongofsingle , _ => Error (msg "Asmblockgen.transl_op: Olongofsingle")
  | Olonguofsingle , _ => Error (msg "Asmblockgen.transl_op: Olonguofsingle")


  | Ocmp cmp, _ =>
      do rd <- ireg_of res;
      transl_cond_op cmp rd args k


  | Oextfz stop start, a1 :: nil =>
    assertion (ExtValues.is_bitfield stop start);
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pextfz stop start rd rs ::i k)

  | Oextfs stop start, a1 :: nil =>
    assertion (ExtValues.is_bitfield stop start);
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pextfs stop start rd rs ::i k)

  | Oextfzl stop start, a1 :: nil =>
    assertion (ExtValues.is_bitfieldl stop start);
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pextfzl stop start rd rs ::i k)

  | Oextfsl stop start, a1 :: nil =>
    assertion (ExtValues.is_bitfieldl stop start);
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pextfsl stop start rd rs ::i k)
    
  | Oinsf stop start, a0 :: a1 :: nil =>
    assertion (ExtValues.is_bitfield stop start);
    assertion (mreg_eq a0 res);
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pinsf stop start rd rs ::i k)
    
  | Oinsfl stop start, a0 :: a1 :: nil =>
    assertion (ExtValues.is_bitfieldl stop start);
    assertion (mreg_eq a0 res);
      do rd  <- ireg_of res; do rs <- ireg_of a1;
      OK (Pinsfl stop start rd rs ::i k)

  | Osel cond0 ty, aT :: aF :: aC :: nil =>
    assertion (mreg_eq aT res);
      do rT <- ireg_of aT;
      do rF <- ireg_of aF;
      do rC <- ireg_of aC;
      do op <- conditional_move (negate_condition0 cond0) rC rT rF;
      OK (op ::i k)

  | Oselimm cond0 imm, aT :: aC :: nil =>
    assertion (mreg_eq aT res);
      do rT <- ireg_of aT;
      do rC <- ireg_of aC;
      do op <- conditional_move_imm32 (negate_condition0 cond0) rC rT imm;
      OK (op ::i k)
      

  | Osellimm cond0 imm, aT :: aC :: nil =>
    assertion (mreg_eq aT res);
      do rT <- ireg_of aT;
      do rC <- ireg_of aC;
      do op <- conditional_move_imm64 (negate_condition0 cond0) rC rT imm;
      OK (op ::i k)
         
  | _, _ =>
      Error(msg "Asmgenblock.transl_op")
  end.

(** Accessing data in the stack frame. *)

Definition indexed_memory_access
        (mk_instr: ireg -> offset -> basic)
        (base: ireg) (ofs: ptrofs) :=
  match make_immed64 (Ptrofs.to_int64 ofs) with
  | Imm64_single imm =>
      mk_instr base (Ptrofs.of_int64 imm)
end.

Definition loadind (base: ireg) (ofs: ptrofs) (ty: typ) (dst: mreg) (k: bcode) :=
  match ty, preg_of dst with
  | Tint,    IR rd => OK (indexed_memory_access (PLoadRRO TRAP Plw rd) base ofs ::i k)
  | Tlong,   IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pld rd) base ofs ::i k)
  | Tsingle, IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pfls rd) base ofs ::i k)
  | Tfloat,  IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pfld rd) base ofs ::i k)
  | Tany32,  IR rd => OK (indexed_memory_access (PLoadRRO TRAP Plw_a rd) base ofs ::i k)
  | Tany64,  IR rd => OK (indexed_memory_access (PLoadRRO TRAP Pld_a rd) base ofs ::i k)
  | _, _           => Error (msg "Asmblockgen.loadind")
  end.

Definition storeind (src: mreg) (base: ireg) (ofs: ptrofs) (ty: typ) (k: bcode) :=
  match ty, preg_of src with
  | Tint,    IR rd => OK (indexed_memory_access (PStoreRRO Psw rd) base ofs ::i k)
  | Tlong,   IR rd => OK (indexed_memory_access (PStoreRRO Psd rd) base ofs ::i k)
  | Tsingle, IR rd => OK (indexed_memory_access (PStoreRRO Pfss rd) base ofs ::i k)
  | Tfloat,  IR rd => OK (indexed_memory_access (PStoreRRO Pfsd rd) base ofs ::i k)
  | Tany32,  IR rd => OK (indexed_memory_access (PStoreRRO Psw_a rd) base ofs ::i k)
  | Tany64,  IR rd => OK (indexed_memory_access (PStoreRRO Psd_a rd) base ofs ::i k)
  | _, _           => Error (msg "Asmblockgen.storeind")
  end.

Definition loadind_ptr (base: ireg) (ofs: ptrofs) (dst: ireg) :=
  indexed_memory_access (PLoadRRO TRAP Pld dst) base ofs.

Definition storeind_ptr (src: ireg) (base: ireg) (ofs: ptrofs) :=
  indexed_memory_access (PStoreRRO Psd src) base ofs.

(** Translation of memory accesses: loads, and stores. *)

Definition transl_memory_access2
     (mk_instr: ireg -> ireg -> basic)
     (addr: addressing) (args: list mreg) (k: bcode) : res bcode :=
  match addr, args with
  | Aindexed2, a1 :: a2 :: nil =>
      do rs1 <- ireg_of a1;
      do rs2 <- ireg_of a2;
      OK (mk_instr rs1 rs2 ::i k)
  | _, _ => Error (msg "Asmblockgen.transl_memory_access2")
  end.

Definition transl_memory_access2XS
     (chunk: memory_chunk)
     (mk_instr: ireg -> ireg -> basic)
     scale (args: list mreg) (k: bcode) : res bcode :=
  match args with
  | (a1 :: a2 :: nil) =>
      assertion (Z.eqb (zscale_of_chunk chunk) scale);
      do rs1 <- ireg_of a1;
      do rs2 <- ireg_of a2;
      OK (mk_instr rs1 rs2 ::i k)
  | _ => Error (msg "Asmblockgen.transl_memory_access2XS")
  end.

Definition transl_memory_access
     (mk_instr: ireg -> offset -> basic)
     (addr: addressing) (args: list mreg) (k: bcode) : res bcode :=
  match addr, args with
  | Aindexed ofs, a1 :: nil =>
      do rs <- ireg_of a1;
      OK (indexed_memory_access mk_instr rs ofs ::i k)
  | Aglobal id ofs, nil =>
      OK (Ploadsymbol id ofs RTMP ::i (mk_instr RTMP Ptrofs.zero ::i k))
  | Ainstack ofs, nil =>
      OK (indexed_memory_access mk_instr SP ofs ::i k)
  | _, _ =>
      Error(msg "Asmblockgen.transl_memory_access")
  end.

Definition chunk2load (chunk: memory_chunk) :=
  match chunk with
  | Mint8signed => Plb
  | Mint8unsigned => Plbu
  | Mint16signed => Plh
  | Mint16unsigned => Plhu
  | Mint32 => Plw
  | Mint64 => Pld
  | Mfloat32 => Pfls
  | Mfloat64 => Pfld
  | Many32 => Plw_a
  | Many64 => Pld_a
  end.

Definition transl_load_rro (trap: trapping_mode) (chunk: memory_chunk) (addr: addressing)
           (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
  do r <- ireg_of dst;
  transl_memory_access (PLoadRRO trap (chunk2load chunk) r) addr args k.

Definition transl_load_rrr (trap: trapping_mode)  (chunk: memory_chunk) (addr: addressing)
           (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
  do r <- ireg_of dst;
  transl_memory_access2 (PLoadRRR trap (chunk2load chunk) r) addr args k.

Definition transl_load_rrrXS (trap: trapping_mode) (chunk: memory_chunk) (scale : Z)
           (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
  do r <- ireg_of dst;
  transl_memory_access2XS chunk (PLoadRRRXS trap (chunk2load chunk) r) scale args k.

Definition transl_load (trap : trapping_mode)
           (chunk: memory_chunk) (addr: addressing)
           (args: list mreg) (dst: mreg) (k: bcode) : res bcode :=
  match addr with
    | Aindexed2XS scale => transl_load_rrrXS trap chunk scale args dst k
    | Aindexed2 => transl_load_rrr trap chunk addr args dst k
    | _ => transl_load_rro trap chunk addr args dst k
  end.

Definition chunk2store (chunk: memory_chunk) :=
  match chunk with
  | Mint8signed | Mint8unsigned => Psb
  | Mint16signed | Mint16unsigned => Psh
  | Mint32 => Psw
  | Mint64 => Psd
  | Mfloat32 => Pfss
  | Mfloat64 => Pfsd
  | Many32 => Psw_a
  | Many64 => Psd_a
  end.

Definition transl_store_rro (chunk: memory_chunk) (addr: addressing)
           (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
  do r <- ireg_of src;
  transl_memory_access (PStoreRRO (chunk2store chunk) r) addr args k.

Definition transl_store_rrr (chunk: memory_chunk) (addr: addressing)
           (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
  do r <- ireg_of src;
  transl_memory_access2 (PStoreRRR (chunk2store chunk) r) addr args k.

Definition transl_store_rrrxs (chunk: memory_chunk) (scale: Z)
           (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
  do r <- ireg_of src;
  transl_memory_access2XS chunk (PStoreRRRXS (chunk2store chunk) r) scale args k.

Definition transl_store (chunk: memory_chunk) (addr: addressing)
           (args: list mreg) (src: mreg) (k: bcode) : res bcode :=
  match addr with
  | Aindexed2 => transl_store_rrr chunk addr args src k
  | Aindexed2XS scale => transl_store_rrrxs chunk scale args src k
  | _ => transl_store_rro chunk addr args src k
  end.

(** Function epilogue *)

Definition make_epilogue (f: Machblock.function) (k: code) :=
  (loadind_ptr SP f.(fn_retaddr_ofs) GPRA) 
  ::g Pset RA GPRA ::g Pfreeframe f.(fn_stacksize) f.(fn_link_ofs) ::g k.

(** Translation of a Machblock instruction. *)

Definition transl_instr_basic (f: Machblock.function) (i: Machblock.basic_inst)
                              (ep: bool) (k: bcode) :=
  match i with
  | MBgetstack ofs ty dst =>
      loadind SP ofs ty dst k
  | MBsetstack src ofs ty =>
      storeind src SP ofs ty k
  | MBgetparam ofs ty dst =>
      (* load via the frame pointer if it is valid *)
      do c <- loadind FP ofs ty dst k;
      OK (if ep then c
                else (loadind_ptr SP f.(fn_link_ofs) FP) ::i c)
  | MBop op args res =>
      transl_op op args res k
  | MBload trap chunk addr args dst =>
      transl_load trap chunk addr args dst k
  | MBstore chunk addr args src =>
      transl_store chunk addr args src k
  end.

Definition transl_instr_control (f: Machblock.function) (oi: option Machblock.control_flow_inst)
                                : res code :=
  match oi with
  | None => OK nil
  | Some i =>
    match i with
    | MBcall sig (inl r) =>
        do r1 <- ireg_of r; OK ((Picall r1) ::g nil)
    | MBcall sig (inr symb) =>
        OK ((Pcall symb) ::g nil)
    | MBtailcall sig (inr symb) =>
        OK (make_epilogue f ((Pgoto symb) ::g nil))
    | MBtailcall sig (inl r) =>
        do r1 <- ireg_of r; OK (make_epilogue f ((Pigoto r1) ::g nil))
    | MBbuiltin ef args res =>
        OK (Pbuiltin ef (List.map (map_builtin_arg preg_of) args) (map_builtin_res preg_of res) ::g nil)
    | MBgoto lbl =>
        OK (Pj_l lbl ::g nil)
    | MBcond cond args lbl =>
        transl_cbranch cond args lbl nil
    | MBreturn =>
        OK (make_epilogue f (Pret ::g nil))
    | MBjumptable arg tbl =>
      do r <- ireg_of arg;
      OK (Pjumptable r tbl ::g nil)
    end
  end.

(** Translation of a code sequence *)

Definition fp_is_parent (before: bool) (i: Machblock.basic_inst) : bool :=
  match i with
  | MBgetstack ofs ty dst => before && negb (mreg_eq dst MFP)
  | MBsetstack src ofs ty => before
  | MBgetparam ofs ty dst => negb (mreg_eq dst MFP)
  | MBop op args res => before && negb (mreg_eq res MFP)
  | MBload trapping_mode chunk addr args dst => before && negb (mreg_eq dst MFP)
  | MBstore chunk addr args res => before
  end.

(** This is the naive definition, which is not tail-recursive unlike the other backends *)

Fixpoint transl_basic_code (f: Machblock.function) (il: list Machblock.basic_inst) (it1p: bool) :=
  match il with
  | nil => OK nil
  | i1 :: il' =>
      do k <- transl_basic_code f il' (fp_is_parent it1p i1);
      transl_instr_basic f i1 it1p k
  end.

(* (** This is an equivalent definition in continuation-passing style
  that runs in constant stack space. *)

Fixpoint transl_basic_rec (f: Machblock.function) (il: list Machblock.basic_inst)
                          (it1p: bool) (k: bcode -> res bcode) :=
  match il with
  | nil => k nil
  | i1 :: il' =>
      transl_basic_rec f il' (fp_is_parent it1p i1)
        (fun c1 => do c2 <- transl_instr_basic f i1 it1p c1; k c2)
  end.

Definition transl_basic_code' (f: Machblock.function) (il: list Machblock.basic_inst) (it1p: bool) :=
  transl_basic_rec f il it1p (fun c => OK c). *)

(** Translation of a whole function.  Note that we must check
  that the generated code contains less than [2^64] instructions,
  otherwise the offset part of the [PC] code pointer could wrap
  around, leading to incorrect executions. *)

(* gen_bblocks can generate two bblocks if the ctl is a PExpand (since the PExpand must be alone in its block) *)
Program Definition gen_bblocks (hd: list label) (c: list basic) (ctl: list instruction) :=
  match (extract_ctl ctl) with
  | None => 
      match c with
      | nil => {| header := hd; body := Pnop::nil; exit := None |} :: nil
      | i::c => {| header := hd; body := ((i::c) ++ extract_basic ctl); exit := None |} :: nil
      end
  | Some (PExpand (Pbuiltin ef args res)) =>
      match c with
      | nil => {| header := hd; body := nil; exit := Some (PExpand (Pbuiltin ef args res)) |} :: nil
      | _ => {| header := hd; body := c; exit := None |} 
              :: {| header := nil; body := nil; exit := Some (PExpand (Pbuiltin ef args res)) |} :: nil
      end
  | Some ex => {| header := hd; body := (c ++ extract_basic ctl); exit := Some ex |} :: nil
  end
.
Next Obligation.
  apply wf_bblock_refl. constructor.
    left. auto.
    discriminate.
Qed. Next Obligation.
  apply wf_bblock_refl. constructor.
    right. discriminate.
    unfold builtin_alone. intros. pose (H ef args res). rewrite H0 in n. contradiction.
Qed.

Definition transl_block (f: Machblock.function) (fb: Machblock.bblock) (ep: bool) : res (list bblock) :=
  do c <- transl_basic_code f fb.(Machblock.body) ep;
  do ctl <- transl_instr_control f fb.(Machblock.exit);
  OK (gen_bblocks fb.(Machblock.header) c ctl)
.

Fixpoint transl_blocks (f: Machblock.function) (lmb: list Machblock.bblock) (ep: bool) :=
  match lmb with
  | nil => OK nil
  | mb :: lmb => 
      do lb <- transl_block f mb (if Machblock.header mb then ep else false);
      do lb' <- transl_blocks f lmb false;
      OK (lb @@ lb')
  end
.

Program Definition make_prologue (f:  Machblock.function) lb :=
  ({| header := nil; body := Pallocframe f.(fn_stacksize) f.(fn_link_ofs) ::i
         Pget GPRA RA ::i
         storeind_ptr GPRA SP f.(fn_retaddr_ofs) ::i nil;
      exit := None |} :: lb).

Definition transl_function (f: Machblock.function) :=
  do lb <- transl_blocks f f.(Machblock.fn_code) true;
  OK (mkfunction f.(Machblock.fn_sig)
        (make_prologue f lb)).

Definition transf_function (f: Machblock.function) : res Asmvliw.function :=
  do tf <- transl_function f;
  if zlt Ptrofs.max_unsigned (size_blocks tf.(fn_blocks))
  then Error (msg "code size exceeded")
  else OK tf.

Definition transf_fundef (f: Machblock.fundef) : res Asmvliw.fundef :=
  transf_partial_fundef transf_function f.

Definition transf_program (p: Machblock.program) : res Asmvliw.program :=
  transform_partial_program transf_fundef p.