summaryrefslogtreecommitdiff
path: root/asmcomp/amd64/emit.mlp
blob: 6df9f4e7e792f42f2c3ea4710634d883c47984b0 (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
# 2 "asmcomp/amd64/emit.mlp"
(**************************************************************************)
(*                                                                        *)
(*                                 OCaml                                  *)
(*                                                                        *)
(*             Xavier Leroy, projet Cristal, INRIA Rocquencourt           *)
(*                                                                        *)
(*   Copyright 1996 Institut National de Recherche en Informatique et     *)
(*     en Automatique.                                                    *)
(*                                                                        *)
(*   All rights reserved.  This file is distributed under the terms of    *)
(*   the GNU Lesser General Public License version 2.1, with the          *)
(*   special exception on linking described in the file LICENSE.          *)
(*                                                                        *)
(**************************************************************************)

(* Emission of Intel x86_64 assembly code *)

open Cmm
open Arch
open Proc
open Reg
open Mach
open Linear
open Emitaux

open X86_ast
open X86_proc
open X86_dsl
module String = Misc.Stdlib.String
module Int = Numbers.Int

(* [Branch_relaxation] is not used in this file, but is required by
   emit.mlp files for certain other targets; the reference here ensures
   that when releases are being prepared the .depend files are correct
   for all targets. *)
[@@@ocaml.warning "-66"]
open! Branch_relaxation

let _label s = D.label ~typ:QWORD s

(* Override proc.ml *)

let int_reg_name =
  [| RAX; RBX; RDI; RSI; RDX; RCX; R8; R9;
     R12; R13; R10; R11; RBP; |]

let float_reg_name = Array.init 16 (fun i -> XMM i)

let register_name r =
  if r < 100 then Reg64 (int_reg_name.(r))
  else Regf (float_reg_name.(r - 100))

(* CFI directives *)

let cfi_startproc () =
  if Config.asm_cfi_supported then D.cfi_startproc ()

let cfi_endproc () =
  if Config.asm_cfi_supported then D.cfi_endproc ()

let cfi_adjust_cfa_offset n =
  if Config.asm_cfi_supported then D.cfi_adjust_cfa_offset n

let emit_debug_info dbg =
  emit_debug_info_gen dbg D.file D.loc

let fp = Config.with_frame_pointers

(* Tradeoff between code size and code speed *)

let fastcode_flag = ref true

(* Layout of the stack frame *)
let stack_offset = ref 0

let num_stack_slots = Array.make Proc.num_register_classes 0

let prologue_required = ref false

let frame_required = ref false

let frame_size () =                     (* includes return address *)
  if !frame_required then begin
    let sz =
      (!stack_offset + 8 * (num_stack_slots.(0) + num_stack_slots.(1)) + 8
       + (if fp then 8 else 0))
    in Misc.align sz 16
  end else
    !stack_offset + 8

let slot_offset loc cl =
  match loc with
  | Incoming n -> frame_size() + n
  | Local n ->
      if cl = 0
      then !stack_offset + n * 8
      else !stack_offset + (num_stack_slots.(0) + n) * 8
  | Outgoing n -> n

(* Symbols *)

let symbol_prefix = if system = S_macosx then "_" else ""

let emit_symbol s = string_of_symbol symbol_prefix s

(* Record symbols used and defined - at the end generate extern for those
   used but not defined *)

let symbols_defined = ref String.Set.empty
let symbols_used = ref String.Set.empty

let add_def_symbol s = symbols_defined := String.Set.add s !symbols_defined
let add_used_symbol s = symbols_used := String.Set.add s !symbols_used

let imp_table = Hashtbl.create 16

let reset_imp_table () = Hashtbl.clear imp_table

let get_imp_symbol s =
  match Hashtbl.find imp_table s with
  | exception Not_found ->
      let imps = "__caml_imp_" ^ s in
      Hashtbl.add imp_table s imps;
      imps
  | imps -> imps

let emit_imp_table () =
  let f s imps =
    _label (emit_symbol imps);
    D.qword (ConstLabel (emit_symbol s))
  in
  D.data();
  D.comment "relocation table start";
  D.align 8;
  Hashtbl.iter f imp_table;
  D.comment "relocation table end"

let mem__imp s =
  let imp_s = get_imp_symbol s in
  mem64_rip QWORD (emit_symbol imp_s)

let rel_plt s =
  if windows && !Clflags.dlcode then mem__imp s
  else
    sym (if use_plt then emit_symbol s ^ "@PLT" else emit_symbol s)

let emit_call s = I.call (rel_plt s)

let emit_jump s = I.jmp (rel_plt s)

let load_symbol_addr s arg =
  if !Clflags.dlcode then
    if windows then begin
      (* I.mov (mem__imp s) arg (\* mov __caml_imp_foo(%rip), ... *\) *)
      I.mov (sym (emit_symbol s)) arg (* movabsq $foo, ... *)
    end else I.mov (mem64_rip QWORD (emit_symbol s ^ "@GOTPCREL")) arg
  else if !Clflags.pic_code then
    I.lea (mem64_rip NONE (emit_symbol s)) arg
  else
    I.mov (sym (emit_symbol s)) arg

let domain_field f =
  mem64 QWORD (Domainstate.idx_of_field f * 8) R14

(* Output a label *)

let emit_label lbl =
  match system with
  | S_macosx | S_win64 -> "L" ^ Int.to_string lbl
  | _ -> ".L" ^ Int.to_string lbl

let label s = sym (emit_label s)

(* For Spacetime, keep track of code labels that have been emitted.  *)
let used_labels = ref Int.Set.empty

let mark_used lbl =
  if Config.spacetime && not (Int.Set.mem lbl !used_labels) then begin
    used_labels := Int.Set.add lbl !used_labels
  end

let def_label ?typ s =
  mark_used s;
  D.label ?typ (emit_label s)

let emit_Llabel fallthrough lbl =
  if not fallthrough && !fastcode_flag then D.align 4;
  def_label lbl

(* Output a pseudo-register *)

let reg = function
  | { loc = Reg.Reg r } -> register_name r
  | { loc = Stack s; typ = Float } as r ->
      let ofs = slot_offset s (register_class r) in
      mem64 REAL8 ofs RSP
  | { loc = Stack s } as r ->
      let ofs = slot_offset s (register_class r) in
      mem64 QWORD ofs RSP
  | { loc = Unknown } ->
      assert false

let reg64 = function
  | { loc = Reg.Reg r } -> int_reg_name.(r)
  | _ -> assert false


let res i n = reg i.res.(n)

let arg i n = reg i.arg.(n)

(* Output a reference to the lower 8, 16 or 32 bits of a register *)

let reg_low_8_name  = Array.map (fun r -> Reg8L r) int_reg_name
let reg_low_16_name = Array.map (fun r -> Reg16 r) int_reg_name
let reg_low_32_name = Array.map (fun r -> Reg32 r) int_reg_name

let emit_subreg tbl typ r =
  match r.loc with
  | Reg.Reg r when r < 13 -> tbl.(r)
  | Stack s -> mem64 typ (slot_offset s (register_class r)) RSP
  | _ -> assert false

let arg8 i n = emit_subreg reg_low_8_name BYTE i.arg.(n)
let arg16 i n = emit_subreg reg_low_16_name WORD i.arg.(n)
let arg32 i n = emit_subreg reg_low_32_name DWORD i.arg.(n)
let arg64 i n = reg64 i.arg.(n)

let res16 i n = emit_subreg reg_low_16_name WORD i.res.(n)
let res32 i n = emit_subreg reg_low_32_name DWORD i.res.(n)

(* Output an addressing mode *)

let addressing addr typ i n =
  match addr with
  | Ibased(s, ofs) ->
      add_used_symbol s;
      mem64_rip typ (emit_symbol s) ~ofs
  | Iindexed d ->
      mem64 typ d (arg64 i n)
  | Iindexed2 d ->
      mem64 typ ~base:(arg64 i n) d (arg64 i (n+1))
  | Iscaled(2, d) ->
      mem64 typ ~base:(arg64 i n) d (arg64 i n)
  | Iscaled(scale, d) ->
      mem64 typ ~scale d (arg64 i n)
  | Iindexed2scaled(scale, d) ->
      mem64 typ ~scale ~base:(arg64 i n) d (arg64 i (n+1))

(* Record live pointers at call points -- see Emitaux *)

let record_frame_label ?label live dbg =
  let lbl =
    match label with
    | None -> new_label()
    | Some label -> label
  in
  let live_offset = ref [] in
  Reg.Set.iter
    (function
      | {typ = Val; loc = Reg r} ->
          live_offset := ((r lsl 1) + 1) :: !live_offset
      | {typ = Val; loc = Stack s} as reg ->
          live_offset := slot_offset s (register_class reg) :: !live_offset
      | {typ = Addr} as r ->
          Misc.fatal_error ("bad GC root " ^ Reg.name r)
      | _ -> ()
    )
    live;
  record_frame_descr ~label:lbl ~frame_size:(frame_size())
    ~live_offset:!live_offset dbg;
  lbl

let record_frame ?label live dbg =
  let lbl = record_frame_label ?label live dbg in
  def_label lbl

(* Spacetime instrumentation *)

let spacetime_before_uninstrumented_call ~node_ptr ~index =
  (* At the moment, [node_ptr] is pointing at the node for the current
     OCaml function.  Get hold of the node itself and move the pointer
     forwards, saving it into the distinguished register.  This is used
     for instrumentation of function calls (e.g. caml_call_gc and bounds
     check failures) not inserted until this stage of the compiler
     pipeline. *)
  I.mov node_ptr (reg Proc.loc_spacetime_node_hole);
  assert (index >= 2);
  I.add (int (index * 8)) (reg Proc.loc_spacetime_node_hole)

(* Record calls to the GC -- we've moved them out of the way *)

type gc_call =
  { gc_lbl: label;                      (* Entry label *)
    gc_return_lbl: label;               (* Where to branch after GC *)
    gc_frame: label;                    (* Label of frame descriptor *)
    gc_spacetime : (X86_ast.arg * int) option;
    (* Spacetime node hole pointer and index *)
  }

let call_gc_sites = ref ([] : gc_call list)

let emit_call_gc gc =
  def_label gc.gc_lbl;
  begin match gc.gc_spacetime with
  | None -> assert (not Config.spacetime)
  | Some (node_ptr, index) ->
    assert Config.spacetime;
    spacetime_before_uninstrumented_call ~node_ptr ~index
  end;
  emit_call "caml_call_gc";
  def_label gc.gc_frame;
  I.jmp (label gc.gc_return_lbl)

(* Record calls to caml_ml_array_bound_error.
   In -g mode, or when using Spacetime profiling, we maintain one call to
   caml_ml_array_bound_error per bound check site.  Without -g, we can share
   a single call. *)

type bound_error_call =
  { bd_lbl: label;                      (* Entry label *)
    bd_frame: label;                    (* Label of frame descriptor *)
    bd_spacetime : (X86_ast.arg * int) option;
    (* As for [gc_call]. *)
  }

let bound_error_sites = ref ([] : bound_error_call list)
let bound_error_call = ref 0

let bound_error_label ?label dbg ~spacetime =
  if !Clflags.debug || Config.spacetime then begin
    let lbl_bound_error = new_label() in
    let lbl_frame = record_frame_label ?label Reg.Set.empty (Dbg_other dbg) in
    bound_error_sites :=
      { bd_lbl = lbl_bound_error; bd_frame = lbl_frame;
        bd_spacetime = spacetime; } :: !bound_error_sites;
    lbl_bound_error
  end else begin
    if !bound_error_call = 0 then bound_error_call := new_label();
    !bound_error_call
  end

let emit_call_bound_error bd =
  def_label bd.bd_lbl;
  begin match bd.bd_spacetime with
  | None -> ()
  | Some (node_ptr, index) ->
    spacetime_before_uninstrumented_call ~node_ptr ~index
  end;
  emit_call "caml_ml_array_bound_error";
  def_label bd.bd_frame

let emit_call_bound_errors () =
  List.iter emit_call_bound_error !bound_error_sites;
  if !bound_error_call > 0 then begin
    def_label !bound_error_call;
    emit_call "caml_ml_array_bound_error"
  end

(* Names for instructions *)

let instr_for_intop = function
  | Iadd -> I.add
  | Isub -> I.sub
  | Imul -> (fun arg1 arg2 -> I.imul arg1 (Some arg2))
  | Iand -> I.and_
  | Ior -> I.or_
  | Ixor -> I.xor
  | Ilsl -> I.sal
  | Ilsr -> I.shr
  | Iasr -> I.sar
  | _ -> assert false

let instr_for_floatop = function
  | Iaddf -> I.addsd
  | Isubf -> I.subsd
  | Imulf -> I.mulsd
  | Idivf -> I.divsd
  | _ -> assert false

let instr_for_floatarithmem = function
  | Ifloatadd -> I.addsd
  | Ifloatsub -> I.subsd
  | Ifloatmul -> I.mulsd
  | Ifloatdiv -> I.divsd

let cond = function
  | Isigned Ceq   -> E   | Isigned Cne   -> NE
  | Isigned Cle   -> LE  | Isigned Cgt   -> G
  | Isigned Clt   -> L   | Isigned Cge   -> GE
  | Iunsigned Ceq -> E   | Iunsigned Cne -> NE
  | Iunsigned Cle -> BE  | Iunsigned Cgt -> A
  | Iunsigned Clt -> B   | Iunsigned Cge -> AE

(* Output an = 0 or <> 0 test. *)

let output_test_zero arg =
  match arg.loc with
  | Reg.Reg _ -> I.test (reg arg) (reg arg)
  | _  -> I.cmp (int 0) (reg arg)

(* Output a floating-point compare and branch *)

let emit_float_test cmp i lbl =
  (* Effect of comisd on flags and conditional branches:
                     ZF PF CF  cond. branches taken
        unordered     1  1  1  je, jb, jbe, jp
        >             0  0  0  jne, jae, ja
        <             0  0  1  jne, jbe, jb
        =             1  0  0  je, jae, jbe.
     If FP traps are on (they are off by default),
     comisd traps on QNaN and SNaN but ucomisd traps on SNaN only.
  *)
  match cmp with
  | CFeq ->
      let next = new_label() in
      I.ucomisd (arg i 1) (arg i 0);
      I.jp (label next);          (* skip if unordered *)
      I.je lbl;                   (* branch taken if x=y *)
      def_label next
  | CFneq ->
      I.ucomisd (arg i 1) (arg i 0);
      I.jp lbl;                   (* branch taken if unordered *)
      I.jne lbl                   (* branch taken if x<y or x>y *)
  | CFlt ->
      I.comisd (arg i 0) (arg i 1);
      I.ja lbl                    (* branch taken if y>x i.e. x<y *)
  | CFnlt ->
      I.comisd (arg i 0) (arg i 1);
      I.jbe lbl                   (* taken if unordered or y<=x i.e. !(x<y) *)
  | CFle ->
      I.comisd (arg i 0) (arg i 1);(* swap compare *)
      I.jae lbl                    (* branch taken if y>=x i.e. x<=y *)
  | CFnle ->
      I.comisd (arg i 0) (arg i 1);(* swap compare *)
      I.jb lbl                     (* taken if unordered or y<x i.e. !(x<=y) *)
  | CFgt ->
      I.comisd (arg i 1) (arg i 0);
      I.ja lbl                     (* branch taken if x>y *)
  | CFngt ->
      I.comisd (arg i 1) (arg i 0);
      I.jbe lbl                    (* taken if unordered or x<=y i.e. !(x>y) *)
  | CFge ->
      I.comisd (arg i 1) (arg i 0);(* swap compare *)
      I.jae lbl                    (* branch taken if x>=y *)
  | CFnge ->
      I.comisd (arg i 1) (arg i 0);(* swap compare *)
      I.jb lbl                     (* taken if unordered or x<y i.e. !(x>=y) *)

(* Deallocate the stack frame before a return or tail call *)

let output_epilogue f =
  if !frame_required then begin
    let n = frame_size() - 8 - (if fp then 8 else 0) in
    if n <> 0
    then begin
      I.add (int n) rsp;
      cfi_adjust_cfa_offset (-n);
    end;
    if fp then I.pop rbp;
    f ();
    (* reset CFA back cause function body may continue *)
    if n <> 0
    then cfi_adjust_cfa_offset n
  end
  else
    f ()

(* Floating-point constants *)

let float_constants = ref ([] : (int64 * int) list)

let add_float_constant cst =
  try
    List.assoc cst !float_constants
  with Not_found ->
    let lbl = new_label() in
    float_constants := (cst, lbl) :: !float_constants;
    lbl

let emit_float_constant f lbl =
  _label (emit_label lbl);
  D.qword (Const f)

let emit_global_label s =
  let lbl = Compilenv.make_symbol (Some s) in
  add_def_symbol lbl;
  let lbl = emit_symbol lbl in
  D.global lbl;
  _label lbl

(* Output .text section directive, or named .text.caml.<name> if enabled and
   supported on the target system. *)

let emit_named_text_section func_name =
  if !Clflags.function_sections then
    begin match system with
    | S_macosx
    (* Names of section segments in macosx are restricted to 16 characters,
       but function names are often longer, especially anonymous functions. *)
    | S_win64 | S_mingw64 | S_cygwin
    (* Win systems provide named text sections, but configure on these
       systems does not support function sections. *)
      ->  assert false
    | _ -> D.section
             [ ".text.caml."^(emit_symbol func_name) ]
             (Some "ax")
             ["@progbits"]
    end
  else D.text ()

(* Output the assembly code for an instruction *)

(* Name of current function *)
let function_name = ref ""
(* Entry point for tail recursive calls *)
let tailrec_entry_point = ref 0

(* Emit an instruction *)
let emit_instr fallthrough i =
  emit_debug_info i.dbg;
  match i.desc with
  | Lend -> ()
  | Lprologue ->
    assert (!prologue_required);
    if fp then begin
      I.push rbp;
      cfi_adjust_cfa_offset 8;
      I.mov rsp rbp;
    end;
    if !frame_required then begin
      let n = frame_size() - 8 - (if fp then 8 else 0) in
      if n <> 0
      then begin
        I.sub (int n) rsp;
        cfi_adjust_cfa_offset n;
      end;
    end
  | Lop(Imove | Ispill | Ireload) ->
      let src = i.arg.(0) and dst = i.res.(0) in
      if src.loc <> dst.loc then
        begin match src.typ, src.loc, dst.loc with
        | Float, Reg.Reg _, Reg.Reg _ -> I.movapd (reg src) (reg dst)
        | Float, _, _ -> I.movsd (reg src) (reg dst)
        | _ -> I.mov (reg src) (reg dst)
        end
  | Lop(Iconst_int n) ->
      if n = 0n then begin
        match i.res.(0).loc with
        | Reg _ ->
          (* Clearing the bottom half also clears the top half (except for
             64-bit-only registers where the behaviour is as if the operands
             were 64 bit). *)
          I.xor (res32 i 0) (res32 i 0)
        | _ ->
          I.mov (int 0) (res i 0)
      end else if n > 0n && n <= 0xFFFF_FFFFn then begin
        match i.res.(0).loc with
        | Reg _ ->
          (* Similarly, setting only the bottom half clears the top half. *)
          I.mov (nat n) (res32 i 0)
        | _ ->
          I.mov (nat n) (res i 0)
      end else
        I.mov (nat n) (res i 0)
  | Lop(Iconst_float f) ->
      begin match f with
      | 0x0000_0000_0000_0000L ->       (* +0.0 *)
          I.xorpd (res i 0) (res i 0)
      | _ ->
          let lbl = add_float_constant f in
          I.movsd (mem64_rip NONE (emit_label lbl)) (res i 0)
      end
  | Lop(Iconst_symbol s) ->
      add_used_symbol s;
      load_symbol_addr s (res i 0)
  | Lop(Icall_ind { label_after; }) ->
      I.call (arg i 0);
      record_frame i.live (Dbg_other i.dbg) ~label:label_after
  | Lop(Icall_imm { func; label_after; }) ->
      add_used_symbol func;
      emit_call func;
      record_frame i.live (Dbg_other i.dbg) ~label:label_after
  | Lop(Itailcall_ind { label_after; }) ->
      output_epilogue begin fun () ->
        I.jmp (arg i 0);
        if Config.spacetime then begin
          record_frame Reg.Set.empty (Dbg_other i.dbg) ~label:label_after
        end
      end
  | Lop(Itailcall_imm { func; label_after; }) ->
      begin
        if func = !function_name then
          I.jmp (label !tailrec_entry_point)
        else begin
          output_epilogue begin fun () ->
            add_used_symbol func;
            emit_jump func
          end
        end
      end;
      if Config.spacetime then begin
        record_frame Reg.Set.empty (Dbg_other i.dbg) ~label:label_after
      end
  | Lop(Iextcall { func; alloc; label_after; }) ->
      add_used_symbol func;
      if alloc then begin
        load_symbol_addr func rax;
        emit_call "caml_c_call";
        record_frame i.live (Dbg_other i.dbg) ~label:label_after;
        if system <> S_win64 then begin
          (* TODO: investigate why such a diff.
             This comes from:
            http://caml.inria.fr/cgi-bin/viewvc.cgi?view=revision&revision=12664

             If we do the same for Win64, we probably need to change
             amd64nt.asm accordingly.
          *)
          I.mov (domain_field Domainstate.Domain_young_ptr) r15
        end
      end else begin
        emit_call func;
        if Config.spacetime then begin
          record_frame Reg.Set.empty (Dbg_other i.dbg) ~label:label_after
        end
      end
  | Lop(Istackoffset n) ->
      if n < 0
      then I.add (int (-n)) rsp
      else if n > 0
      then I.sub (int n) rsp;
      if n <> 0
      then cfi_adjust_cfa_offset n;
      stack_offset := !stack_offset + n
  | Lop(Iload(chunk, addr)) ->
      let dest = res i 0 in
      begin match chunk with
      | Word_int | Word_val ->
          I.mov (addressing addr QWORD i 0) dest
      | Byte_unsigned ->
          I.movzx (addressing addr BYTE i 0) dest
      | Byte_signed ->
          I.movsx (addressing addr BYTE i 0) dest
      | Sixteen_unsigned ->
          I.movzx (addressing addr WORD i 0) dest
      | Sixteen_signed ->
          I.movsx (addressing addr WORD i 0) dest;
      | Thirtytwo_unsigned ->
          I.mov (addressing addr DWORD i 0) (res32 i 0)
      | Thirtytwo_signed ->
          I.movsxd (addressing addr DWORD i 0) dest
      | Single ->
          I.cvtss2sd (addressing addr REAL4 i 0) dest
      | Double | Double_u ->
          I.movsd (addressing addr REAL8 i 0) dest
      end
  | Lop(Istore(chunk, addr, _)) ->
      begin match chunk with
      | Word_int | Word_val ->
          I.mov (arg i 0) (addressing addr QWORD i 1)
      | Byte_unsigned | Byte_signed ->
          I.mov (arg8 i 0) (addressing addr BYTE i 1)
      | Sixteen_unsigned | Sixteen_signed ->
          I.mov (arg16 i 0) (addressing addr WORD i 1)
      | Thirtytwo_signed | Thirtytwo_unsigned ->
          I.mov (arg32 i 0) (addressing addr DWORD i 1)
      | Single ->
          I.cvtsd2ss (arg i 0) xmm15;
          I.movss xmm15 (addressing addr REAL4 i 1)
      | Double | Double_u ->
          I.movsd (arg i 0) (addressing addr REAL8 i 1)
      end
  | Lop(Ialloc { bytes = n; label_after_call_gc; spacetime_index; dbginfo }) ->
      assert (n <= (Config.max_young_wosize + 1) * Arch.size_addr);
      if !fastcode_flag then begin
        I.sub (int n) r15;
        I.cmp (domain_field Domainstate.Domain_young_limit) r15;
        let lbl_call_gc = new_label() in
        let lbl_frame =
          record_frame_label ?label:label_after_call_gc i.live (Dbg_alloc dbginfo)
        in
        I.jb (label lbl_call_gc);
        let lbl_after_alloc = new_label() in
        def_label lbl_after_alloc;
        I.lea (mem64 NONE 8 R15) (res i 0);
        let gc_spacetime =
          if not Config.spacetime then None
          else Some (arg i 0, spacetime_index)
        in
        call_gc_sites :=
          { gc_lbl = lbl_call_gc;
            gc_return_lbl = lbl_after_alloc;
            gc_frame = lbl_frame;
            gc_spacetime; } :: !call_gc_sites
      end else begin
        if Config.spacetime then begin
          spacetime_before_uninstrumented_call ~node_ptr:(arg i 0)
            ~index:spacetime_index;
        end;
        begin match n with
        | 16 -> emit_call "caml_alloc1"
        | 24 -> emit_call "caml_alloc2"
        | 32 -> emit_call "caml_alloc3"
        | _  ->
            I.mov (int n) rax;
            emit_call "caml_allocN"
        end;
        let label =
          record_frame_label ?label:label_after_call_gc i.live
            (Dbg_alloc dbginfo)
        in
        def_label label;
        I.lea (mem64 NONE 8 R15) (res i 0)
      end
  | Lop(Iintop(Icomp cmp)) ->
      I.cmp (arg i 1) (arg i 0);
      I.set (cond cmp) al;
      I.movzx al (res i 0)
  | Lop(Iintop_imm(Icomp cmp, n)) ->
      I.cmp (int n) (arg i 0);
      I.set (cond cmp) al;
      I.movzx al (res i 0)
  | Lop(Iintop (Icheckbound { label_after_error; spacetime_index; } )) ->
      let spacetime =
        if not Config.spacetime then None
        else Some (arg i 2, spacetime_index)
      in
      let lbl = bound_error_label ?label:label_after_error i.dbg ~spacetime in
      I.cmp (arg i 1) (arg i 0);
      I.jbe (label lbl)
  | Lop(Iintop_imm(Icheckbound { label_after_error; spacetime_index; }, n)) ->
      let spacetime =
        if not Config.spacetime then None
        else Some (arg i 1, spacetime_index)
      in
      let lbl = bound_error_label ?label:label_after_error i.dbg ~spacetime in
      I.cmp (int n) (arg i 0);
      I.jbe (label lbl)
  | Lop(Iintop(Idiv | Imod)) ->
      I.cqo ();
      I.idiv (arg i 1)
  | Lop(Iintop(Ilsl | Ilsr | Iasr as op)) ->
      (* We have i.arg.(0) = i.res.(0) and i.arg.(1) = %rcx *)
      instr_for_intop op cl (res i 0)
  | Lop(Iintop Imulh) ->
      I.imul (arg i 1) None
  | Lop(Iintop op) ->
      (* We have i.arg.(0) = i.res.(0) *)
      instr_for_intop op (arg i 1) (res i 0)
  | Lop(Iintop_imm(Iadd, n)) when i.arg.(0).loc <> i.res.(0).loc ->
      I.lea (mem64 NONE n (arg64 i 0)) (res i 0)
  | Lop(Iintop_imm(Iadd, 1) | Iintop_imm(Isub, -1)) ->
      I.inc (res i 0)
  | Lop(Iintop_imm(Iadd, -1) | Iintop_imm(Isub, 1)) ->
      I.dec (res i 0)
  | Lop(Iintop_imm(op, n)) ->
      (* We have i.arg.(0) = i.res.(0) *)
      instr_for_intop op (int n) (res i 0)
  | Lop(Inegf) ->
      I.xorpd (mem64_rip OWORD (emit_symbol "caml_negf_mask")) (res i 0)
  | Lop(Iabsf) ->
      I.andpd (mem64_rip OWORD (emit_symbol "caml_absf_mask")) (res i 0)
  | Lop(Iaddf | Isubf | Imulf | Idivf as floatop) ->
      instr_for_floatop floatop (arg i 1) (res i 0)
  | Lop(Ifloatofint) ->
      I.cvtsi2sd  (arg i 0)  (res i 0)
  | Lop(Iintoffloat) ->
      I.cvttsd2si (arg i 0) (res i 0)
  | Lop(Ispecific(Ilea addr)) ->
      I.lea (addressing addr NONE i 0) (res i 0)
  | Lop(Ispecific(Istore_int(n, addr, _))) ->
      I.mov (nat n) (addressing addr QWORD i 0)
  | Lop(Ispecific(Ioffset_loc(n, addr))) ->
      I.add (int n) (addressing addr QWORD i 0)
  | Lop(Ispecific(Ifloatarithmem(op, addr))) ->
      instr_for_floatarithmem op (addressing addr REAL8 i 1) (res i 0)
  | Lop(Ispecific(Ibswap 16)) ->
      I.xchg ah al;
      I.movzx (res16 i 0) (res i 0)
  | Lop(Ispecific(Ibswap 32)) ->
      I.bswap (res32 i 0);
      I.movsxd (res32 i 0) (res i 0)
  | Lop(Ispecific(Ibswap 64)) ->
      I.bswap (res i 0)
  | Lop(Ispecific(Ibswap _)) ->
      assert false
  | Lop(Ispecific Isqrtf) ->
      if arg i 0 <> res i 0 then
        I.xorpd (res i 0) (res i 0); (* avoid partial register stall *)
      I.sqrtsd (arg i 0) (res i 0)
  | Lop(Ispecific(Ifloatsqrtf addr)) ->
      I.xorpd (res i 0) (res i 0); (* avoid partial register stall *)
      I.sqrtsd (addressing addr REAL8 i 0) (res i 0)
  | Lop(Ispecific(Isextend32)) ->
      I.movsxd (arg32 i 0) (res i 0)
  | Lop(Ispecific(Izextend32)) ->
      I.mov (arg32 i 0) (res32 i 0)
  | Lop (Iname_for_debugger _) -> ()
  | Lreloadretaddr ->
      ()
  | Lreturn ->
      output_epilogue begin fun () ->
        I.ret ()
      end
  | Llabel lbl ->
      emit_Llabel fallthrough lbl
  | Lbranch lbl ->
      I.jmp (label lbl)
  | Lcondbranch(tst, lbl) ->
      let lbl = label lbl in
      begin match tst with
      | Itruetest ->
          output_test_zero i.arg.(0);
          I.jne lbl
      | Ifalsetest ->
          output_test_zero i.arg.(0);
          I.je lbl
      | Iinttest cmp ->
          I.cmp (arg i 1) (arg i 0);
          I.j (cond cmp) lbl
      | Iinttest_imm((Isigned Ceq | Isigned Cne |
                      Iunsigned Ceq | Iunsigned Cne) as cmp, 0) ->
          output_test_zero i.arg.(0);
          I.j (cond cmp) lbl
      | Iinttest_imm(cmp, n) ->
          I.cmp (int n) (arg i 0);
          I.j (cond cmp) lbl
      | Ifloattest cmp ->
          emit_float_test cmp i lbl
      | Ioddtest ->
          I.test (int 1) (arg8 i 0);
          I.jne lbl
      | Ieventest ->
          I.test (int 1) (arg8 i 0);
          I.je lbl
      end
  | Lcondbranch3(lbl0, lbl1, lbl2) ->
      I.cmp (int 1) (arg i 0);
      begin match lbl0 with
      | None -> ()
      | Some lbl -> I.jb (label lbl)
      end;
      begin match lbl1 with
      | None -> ()
      | Some lbl -> I.je (label lbl)
      end;
      begin match lbl2 with
      | None -> ()
      | Some lbl -> I.ja (label lbl)
      end
  | Lswitch jumptbl ->
      let lbl = emit_label (new_label()) in
      (* rax and rdx are clobbered by the Lswitch,
         meaning that no variable that is live across the Lswitch
         is assigned to rax or rdx.  However, the argument to Lswitch
         can still be assigned to one of these two registers, so
         we must be careful not to clobber it before use. *)
      let (tmp1, tmp2) =
        if i.arg.(0).loc = Reg 0 (* rax *)
        then (phys_reg 4 (*rdx*), phys_reg 0 (*rax*))
        else (phys_reg 0 (*rax*), phys_reg 4 (*rdx*)) in

      I.lea (mem64_rip NONE lbl) (reg tmp1);
      I.movsxd (mem64 DWORD 0 (arg64 i 0) ~scale:4 ~base:(reg64 tmp1))
               (reg tmp2);
      I.add (reg tmp2) (reg tmp1);
      I.jmp (reg tmp1);

      begin match system with
      | S_mingw64 | S_cygwin -> D.section [".rdata"] (Some "dr") []
      | S_macosx | S_win64 -> () (* with LLVM/OS X and MASM, use the text segment *)
      | _ -> D.section [".rodata"] None []
      end;
      D.align 4;
      _label lbl;
      for i = 0 to Array.length jumptbl - 1 do
        D.long (ConstSub (ConstLabel(emit_label jumptbl.(i)),
                         ConstLabel lbl))
      done;
      emit_named_text_section !function_name
  | Lentertrap ->
      ()
  | Ladjust_trap_depth { delta_traps; } ->
      (* each trap occupies 16 bytes on the stack *)
      let delta = 16 * delta_traps in
      cfi_adjust_cfa_offset delta;
      stack_offset := !stack_offset + delta
  | Lpushtrap { lbl_handler; } ->
      let load_label_addr s arg =
        if !Clflags.pic_code then
          I.lea (mem64_rip NONE (emit_label s)) arg
        else
          I.mov (sym (emit_label s)) arg
      in
      load_label_addr lbl_handler r11;
      I.push r11;
      cfi_adjust_cfa_offset 8;
      I.push (domain_field Domainstate.Domain_exception_pointer);
      cfi_adjust_cfa_offset 8;
      I.mov rsp (domain_field Domainstate.Domain_exception_pointer);
      stack_offset := !stack_offset + 16;
  | Lpoptrap ->
      I.pop (domain_field Domainstate.Domain_exception_pointer);
      cfi_adjust_cfa_offset (-8);
      I.add (int 8) rsp;
      cfi_adjust_cfa_offset (-8);
      stack_offset := !stack_offset - 16
  | Lraise k ->
      (* No Spacetime instrumentation is required for [caml_raise_exn] and
         [caml_reraise_exn].  The only function called that might affect the
         trie is [caml_stash_backtrace], and it does not. *)
      begin match k with
      | Lambda.Raise_regular ->
          I.mov (int 0) (domain_field Domainstate.Domain_backtrace_pos);
          emit_call "caml_raise_exn";
          record_frame Reg.Set.empty (Dbg_raise i.dbg)
      | Lambda.Raise_reraise ->
          emit_call "caml_raise_exn";
          record_frame Reg.Set.empty (Dbg_raise i.dbg)
      | Lambda.Raise_notrace ->
          I.mov (domain_field Domainstate.Domain_exception_pointer) rsp;
          I.pop (domain_field Domainstate.Domain_exception_pointer);
          I.pop r11;
          I.jmp r11
      end

let rec emit_all fallthrough i =
  match i.desc with
  | Lend -> ()
  | _ ->
      emit_instr fallthrough i;
      emit_all (Linear.has_fallthrough i.desc) i.next

let all_functions = ref []

(* Emission of a function declaration *)

let fundecl fundecl =
  function_name := fundecl.fun_name;
  fastcode_flag := fundecl.fun_fast;
  tailrec_entry_point := fundecl.fun_tailrec_entry_point_label;
  stack_offset := 0;
  call_gc_sites := [];
  bound_error_sites := [];
  bound_error_call := 0;
  for i = 0 to Proc.num_register_classes - 1 do
    num_stack_slots.(i) <- fundecl.fun_num_stack_slots.(i);
  done;
  prologue_required := fundecl.fun_prologue_required;
  frame_required := fundecl.fun_frame_required;
  all_functions := fundecl :: !all_functions;
  emit_named_text_section !function_name;
  D.align 16;
  add_def_symbol fundecl.fun_name;
  if system = S_macosx
  && not !Clflags.output_c_object
  && is_generic_function fundecl.fun_name
  then (* PR#4690 *)
    D.private_extern (emit_symbol fundecl.fun_name)
  else
    D.global (emit_symbol fundecl.fun_name);
  D.label (emit_symbol fundecl.fun_name);
  emit_debug_info fundecl.fun_dbg;
  cfi_startproc ();
  emit_all true fundecl.fun_body;
  List.iter emit_call_gc !call_gc_sites;
  emit_call_bound_errors ();
  if !frame_required then begin
    let n = frame_size() - 8 - (if fp then 8 else 0) in
    if n <> 0
    then begin
      cfi_adjust_cfa_offset (-n);
    end;
  end;
  cfi_endproc ();
  begin match system with
  | S_gnu | S_linux ->
      D.type_ (emit_symbol fundecl.fun_name) "@function";
      D.size (emit_symbol fundecl.fun_name)
        (ConstSub (
            ConstThis,
            ConstLabel (emit_symbol fundecl.fun_name)))
  | _ -> ()
  end

(* Emission of data *)

let emit_item = function
  | Cglobal_symbol s -> D.global (emit_symbol s)
  | Cdefine_symbol s -> add_def_symbol s; _label (emit_symbol s)
  | Cint8 n -> D.byte (const n)
  | Cint16 n -> D.word (const n)
  | Cint32 n -> D.long (const_nat n)
  | Cint n -> D.qword (const_nat n)
  | Csingle f -> D.long  (Const (Int64.of_int32 (Int32.bits_of_float f)))
  | Cdouble f -> D.qword (Const (Int64.bits_of_float f))
  | Csymbol_address s -> add_used_symbol s; D.qword (ConstLabel (emit_symbol s))
  | Cstring s -> D.bytes s
  | Cskip n -> if n > 0 then D.space n
  | Calign n -> D.align n

let data l =
  D.data ();
  D.align 8;
  List.iter emit_item l

(* Beginning / end of an assembly file *)

let begin_assembly() =
  X86_proc.reset_asm_code ();
  reset_debug_info();                   (* PR#5603 *)
  reset_imp_table();
  float_constants := [];
  all_functions := [];
  used_labels := Int.Set.empty;
  if system = S_win64 then begin
    D.extrn "caml_call_gc" NEAR;
    D.extrn "caml_c_call" NEAR;
    D.extrn "caml_allocN" NEAR;
    D.extrn "caml_alloc1" NEAR;
    D.extrn "caml_alloc2" NEAR;
    D.extrn "caml_alloc3" NEAR;
    D.extrn "caml_ml_array_bound_error" NEAR;
    D.extrn "caml_raise_exn" NEAR;
  end;


  if !Clflags.dlcode || Arch.win64 then begin
    (* from amd64.S; could emit these constants on demand *)
    begin match system with
    | S_macosx -> D.section ["__TEXT";"__literal16"] None ["16byte_literals"]
    | S_mingw64 | S_cygwin -> D.section [".rdata"] (Some "dr") []
    | S_win64 -> D.data ()
    | _ -> D.section [".rodata.cst8"] (Some "a") ["@progbits"]
    end;
    D.align 16;
    _label (emit_symbol "caml_negf_mask");
    D.qword (Const 0x8000000000000000L);
    D.qword (Const 0L);
    D.align 16;
    _label (emit_symbol "caml_absf_mask");
    D.qword (Const 0x7FFFFFFFFFFFFFFFL);
    D.qword (Const 0xFFFFFFFFFFFFFFFFL);
  end;

  D.data ();
  emit_global_label "data_begin";

  emit_named_text_section (Compilenv.make_symbol (Some "code_begin"));
  emit_global_label "code_begin";
  if system = S_macosx then I.nop (); (* PR#4690 *)
  ()

let emit_spacetime_shapes () =
  D.data ();
  D.align 8;
  emit_global_label "spacetime_shapes";
  List.iter (fun fundecl ->
      (* CR-someday mshinwell: some of this should be platform independent *)
      begin match fundecl.fun_spacetime_shape with
      | None -> ()
      | Some shape ->
        (* Instrumentation that refers to dead code may have been eliminated. *)
        match List.filter (fun (_, l) -> Int.Set.mem l !used_labels) shape with
        | [] -> ()
        | shape ->
          let funsym = emit_symbol fundecl.fun_name in
          D.comment ("Shape for " ^ funsym ^ ":");
          D.qword (ConstLabel funsym);
          List.iter (fun (part_of_shape, label) ->
            let tag =
              match part_of_shape with
              | Direct_call_point _ -> 1
              | Indirect_call_point -> 2
              | Allocation_point -> 3
            in
            D.qword (Const (Int64.of_int tag));
            D.qword (ConstLabel (emit_label label));
            begin match part_of_shape with
            | Direct_call_point { callee; } ->
              D.qword (ConstLabel (emit_symbol callee))
            | Indirect_call_point -> ()
            | Allocation_point -> ()
            end)
            shape;
          D.qword (Const 0L)
      end)
    !all_functions;
  D.qword (Const 0L);
  D.comment "End of Spacetime shapes."

let end_assembly() =
  if !float_constants <> [] then begin
    begin match system with
    | S_macosx -> D.section ["__TEXT";"__literal8"] None ["8byte_literals"]
    | S_mingw64 | S_cygwin -> D.section [".rdata"] (Some "dr") []
    | S_win64 -> D.data ()
    | _ -> D.section [".rodata.cst8"] (Some "a") ["@progbits"]
    end;
    List.iter (fun (cst,lbl) -> emit_float_constant cst lbl) !float_constants
  end;

  emit_named_text_section (Compilenv.make_symbol (Some "code_end"));
  if system = S_macosx then I.nop ();
  (* suppress "ld warning: atom sorting error" *)

  emit_global_label "code_end";

  emit_imp_table();

  D.data ();
  D.qword (const 0);  (* PR#6329 *)
  emit_global_label "data_end";
  D.qword (const 0);

  D.align 8;                            (* PR#7591 *)
  emit_global_label "frametable";

  let setcnt = ref 0 in
  emit_frames
    { efa_code_label = (fun l -> D.qword (ConstLabel (emit_label l)));
      efa_data_label = (fun l -> D.qword (ConstLabel (emit_label l)));
      efa_8 = (fun n -> D.byte (const n));
      efa_16 = (fun n -> D.word (const n));
      efa_32 = (fun n -> D.long (const_32 n));
      efa_word = (fun n -> D.qword (const n));
      efa_align = D.align;
      efa_label_rel =
        (fun lbl ofs ->
           let c =
             ConstAdd (
               ConstSub(ConstLabel(emit_label lbl), ConstThis),
               const_32 ofs
             ) in
           if system = S_macosx then begin
             incr setcnt;
             let s = Printf.sprintf "L$set$%d" !setcnt in
             D.setvar (s, c);
             D.long (ConstLabel s)
           end else
             D.long c
        );
      efa_def_label = (fun l -> _label (emit_label l));
      efa_string = (fun s -> D.bytes (s ^ "\000"))
    };

  if system = S_linux then begin
    let frametable = Compilenv.make_symbol (Some "frametable") in
    D.size frametable (ConstSub (ConstThis, ConstLabel frametable))
  end;

  if Config.spacetime then begin
    emit_spacetime_shapes ()
  end;

  if system = S_linux then
    (* Mark stack as non-executable, PR#4564 *)
    D.section [".note.GNU-stack"] (Some "") [ "%progbits" ];

  if system = S_win64 then begin
    D.comment "External functions";
    String.Set.iter
      (fun s ->
         if not (String.Set.mem s !symbols_defined) then
           D.extrn (emit_symbol s) NEAR)
      !symbols_used;
    symbols_used := String.Set.empty;
    symbols_defined := String.Set.empty;
  end;

  let asm =
    if !Emitaux.create_asm_file then
      Some
        (
         (if X86_proc.masm then X86_masm.generate_asm
          else X86_gas.generate_asm) !Emitaux.output_channel
        )
    else
      None
  in
  X86_proc.generate_code asm