Source

ocaml / asmcomp / emit_i386.mlp

Full commit
  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
(* Emission of Intel 386 assembly code *)

open Misc
open Cmm
open Arch
open Proc
open Reg
open Mach
open Linearize
open Emitaux

(* Tradeoff between code size and code speed *)

let fastcode_flag = ref true

(* Output a label *)

let emit_label lbl =
  emit_string "L"; emit_int lbl

(* Output a pseudo-register *)

let emit_reg r =
  match r.loc with
    Reg r ->
      emit_string (register_name r)
  | Stack s ->
      let ofs = slot_offset s (register_class r) in
      `{emit_int ofs}(%esp)`
  | Unknown ->
      fatal_error "Emit_i386.emit_reg"

(* Same, but after one push in the floating-point register set *)

let emit_shift r =
  match r.loc with
    Reg r ->
      emit_string (register_name(r + 1))
  | Stack s ->
      let ofs = slot_offset s (register_class r) in
      `{emit_int ofs}(%esp)`
  | Unknown ->
      fatal_error "Emit_i386.emit_shift"

(* Output an addressing mode *)

let emit_addressing addr r n =
  match addr with
    Ibased(s, d) ->
      `_{emit_symbol s}`;
      if d <> 0 then ` + {emit_int d}`
  | Iindexed d ->
      if d <> 0 then emit_int d;
      `({emit_reg r.(n)})`
  | Iindexed2 d ->
      if d <> 0 then emit_int d;
      `({emit_reg r.(n)}, {emit_reg r.(n+1)})`
  | Iindexed2scaled(scale, d) ->
      if d <> 0 then emit_int d;
      `({emit_reg r.(n)}, {emit_reg r.(n+1)}, {emit_int scale})`

(* Record live pointers at call points *)

type frame_descr =
  { fd_lbl: int;                        (* Return address *)
    fd_frame_size: int;                 (* Size of stack frame *)
    fd_live_offset: int list }          (* Offsets/regs of live addresses *)

let frame_descriptors = ref([] : frame_descr list)

let record_frame live =
  let lbl = new_label() in
  let live_offset = ref [] in
  Reg.Set.iter
    (function
        {typ = Addr; loc = Reg r} ->
          live_offset := (-1 - r) :: !live_offset
      | {typ = Addr; loc = Stack s} as reg ->
          live_offset := slot_offset s (register_class reg) :: !live_offset
      | _ -> ())
    live;
  frame_descriptors :=
    { fd_lbl = lbl;
      fd_frame_size = frame_size();
      fd_live_offset = !live_offset } :: !frame_descriptors;
  `{emit_label lbl}:`

let emit_frame fd =
  `	.long	{emit_label fd.fd_lbl} + 4\n`;
  `	.half	{emit_int fd.fd_frame_size}\n`;
  `	.half	{emit_int (List.length fd.fd_live_offset)}\n`;
  List.iter
    (fun n ->
      `	.half	{emit_int n}\n`)
    fd.fd_live_offset

(* Names for instructions *)

let instr_for_intop = function
    Iadd -> "addl"
  | Isub -> "subl"
  | Imul -> "imull"
  | Iand -> "andl"
  | Ior -> "orl"
  | Ixor -> "xorl"
  | Ilsl -> "sal"
  | Ilsr -> "shr"
  | Iasr -> "sar"
  | _ -> fatal_error "Emit_i386: instr_for_intop"

let name_for_cond_branch = 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 the assembly code for an instruction *)

let function_name = ref ""
let tailrec_entry_point = ref 0

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

let emit_instr i =
    match i.desc with
      Lend -> ()
    | Lop(Imove | Ispill | Ireload) ->
        if i.arg.(0).loc <> i.res.(0).loc then begin
          match i.arg.(0).typ with
            Int | Addr ->
              `	movl	{emit_reg i.arg.(0)}, {emit_reg i.res.(0)}\n`
          | Float ->
              if i.arg.(0).loc = Reg 100 then
                `	fstl	{emit_reg i.res.(0)}\n`
              else begin
                `	fldl	{emit_reg i.arg.(0)}\n`;
                `	fstpl	{emit_shift i.res.(0)}\n`
              end
        end
    | Lop(Iconstant cst) ->
        begin match cst with
          Const_int 0 | Const_pointer 0 ->
            begin match i.res.(0).loc with
              Reg n -> `	xorl	{emit_reg i.res.(0)}, {emit_reg i.res.(0)}\n`
            | _     -> `	movl	$0, {emit_reg i.res.(0)}\n`
            end
        | Const_int n ->
            `	movl	${emit_int n}, {emit_reg i.res.(0)}\n`
        | Const_float f ->
            if float_of_string f = 0.0 then
              `	fldz\n`
            else begin
              let lbl = new_label() in
              float_constants := (lbl, f) :: !float_constants;
              `	fldl	{emit_label lbl}\n`
            end;
            `	fstpl	{emit_shift i.res.(0)}\n`
        | Const_symbol s ->
            `	movl	$_{emit_symbol s}, {emit_reg i.res.(0)}\n`
        | Const_pointer n ->
            `	movl	${emit_int n}, {emit_reg i.res.(0)}\n`
        end
    | Lop(Icall_ind) ->
        `{record_frame i.live}	call	*{emit_reg i.arg.(0)}\n`
    | Lop(Icall_imm s) ->
        `{record_frame i.live}	call	_{emit_symbol s}\n`
    | Lop(Itailcall_ind) ->
        let n = frame_size() - 4 in
        if n > 0 then
          `	addl	{emit_int n}, %esp\n`;
        `	jmp	*{emit_reg i.arg.(0)}\n`
    | Lop(Itailcall_imm s) ->
        if s = !function_name then
          `	jmp	{emit_label !tailrec_entry_point}\n`
        else begin
          let n = frame_size() - 4 in
          if n > 0 then
            `	addl	{emit_int n}, %esp\n`;
          `	jmp	_{emit_symbol s}\n`
        end
    | Lop(Iextcall s) ->
        `	movl	$_{emit_symbol s}, %eax\n`;
        `{record_frame i.live}	call	_caml_c_call\n`
    | Lop(Istackoffset n) ->
        if n >= 0
        then `	subl	{emit_int n}, %esp\n`
        else `	addl	{emit_int(-n)}, %esp\n`;
        stack_offset := !stack_offset + n
    | Lop(Iload(chunk, addr)) ->
        begin match i.res.(0).typ with
          Int | Addr ->
            let loadop =
              match chunk with
                Word -> "movl"
              | Byte_unsigned -> "movzbl"
              | Byte_signed -> "movsbl"
              | Sixteen_unsigned -> "movzwl"
              | Sixteen_signed -> "movswl" in
            `	{emit_string loadop}	{emit_addressing addr i.arg 0}, {emit_reg i.res.(0)}\n`
        | Float ->
            `	fldl	{emit_addressing addr i.arg 0}\n`;
            `	fstpl	{emit_shift i.res.(0)}\n`
        end
    | Lop(Istore(Word, addr)) ->
        begin match i.arg.(0).typ with
          Int | Addr ->
            `	movl	{emit_reg i.arg.(0)}, {emit_addressing addr i.arg 1}\n`
        | Float ->
            `	fldl	{emit_reg i.arg.(0)}\n`;
            `	fstpl	{emit_addressing addr i.arg 1}\n`
        end
    | Lop(Istore(chunk, addr)) ->
        (* i.arg.(0) is guaranteed to be in %edx *)
        begin match chunk with
          Word -> fatal_error "Emit_i386: store word"
        | Byte_unsigned | Byte_signed ->
            `	movb	%dl, {emit_addressing addr i.arg 1}\n`
        | Sixteen_unsigned | Sixteen_signed ->
            `	movw	%dx, {emit_addressing addr i.arg 1}\n`
        end
    | Lop(Ialloc n) ->
        if !fastcode_flag then begin
          `	movl	_young_ptr, %eax\n`;
          `	subl	${emit_int n}, $eax\n`;
          `	movl	%eax, _young_ptr\n`;
          `	cmpl	_young_start, %eax`;
          let lbl_cont = new_label() in
          `	jae	{emit_label lbl_cont}\n`;
          `	movl	${emit_int n}, %eax\n`;
          `{record_frame i.live}	call	_caml_call_gc\n`;
          `{emit_label lbl_cont}:	leal	4(%eax), {emit_reg i.res.(0)}\n`
        end else begin
          begin match n with
            8  -> `{record_frame i.live}	call	_caml_alloc1\n`
          | 12 -> `{record_frame i.live}	call	_caml_alloc2\n`
          | 16 -> `{record_frame i.live}	call	_caml_alloc3\n`
          | _  -> `	movl	${emit_int n}, %eax\n`;
                  `{record_frame i.live}	call	_caml_alloc\n`
          end;
          `	leal	4(%eax), {emit_reg i.res.(0)}\n`
        end
    | Lop(Imodify) ->
        (* Argument is in eax *)
        if !fastcode_flag then begin
          `	btsl	10, -4(%eax)\n`;
          let lbl_cont = new_label() in
          `	jc	{emit_label lbl_cont}\n`;
          `	call	_caml_fast_modify\n`;
          `{emit_label lbl_cont}:\n`
        end else
          `	call	_caml_modify\n`
    | Lop(Iintop(Icomp cmp)) ->
        `	cmpl	{emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`;
        let b = name_for_cond_branch cmp in
        `	set{emit_string b}	%al\n`;
        `	movzbl	%al, {emit_reg i.res.(0)}\n`
    | Lop(Iintop_imm(Icomp cmp, n)) ->
        `	cmpl	${emit_int n}, {emit_reg i.arg.(0)}\n`;
        let b = name_for_cond_branch cmp in
        `	set{emit_string b}	%al\n`;
        `	movzbl	%al, {emit_reg i.res.(0)}\n`
    | Lop(Iintop(Idiv | Imod)) ->
        `	cltd\n`;
        `	idivl	{emit_reg i.arg.(1)}\n`
    | Lop(Iintop(Ilsl | Ilsr | Iasr as op)) ->
        (* We have i.arg.(0) = i.res.(0) and i.arg.(1) = %ecx *)
        `	{emit_string(instr_for_intop op)}	%cl, {emit_reg i.res.(0)}\n`
    | Lop(Iintop op) ->
        (* We have i.arg.(0) = i.res.(0) *)
        `	{emit_string(instr_for_intop op)}	{emit_reg i.arg.(1)}, {emit_reg i.res.(0)}\n`
    | Lop(Iintop_imm(Iadd, 1) | Iintop_imm(Isub, -1)) ->
        `	incl	{emit_reg i.res.(0)}\n`
    | Lop(Iintop_imm(Iadd, -1) | Iintop_imm(Isub, 1)) ->
        `	decl	{emit_reg i.res.(0)}\n`
    | Lop(Iintop_imm(op, n)) ->
        (* We have i.arg.(0) = i.res.(0) *)
        `	{emit_string(instr_for_intop op)}	${emit_int n}, {emit_reg i.res.(0)}\n`
    | Lop(Iaddf | Isubf | Imulf | Idivf as floatop) ->
        let instr =
          match floatop with
              Iaddf -> "fadd"
            | Isubf -> "fsub"
            | Imulf -> "fmul"
            | Idivf -> "fdiv"
            | _ -> fatal_error "Emit_i386.emit_instr: floatop" in
        `	fldl	{emit_reg i.arg.(0)}\n`;
        begin match i.arg.(1).loc with
          Stack s ->
            `	{emit_string instr}l	{emit_shift i.arg.(1)}\n`
        | _ ->
            `	{emit_string instr}	{emit_shift i.arg.(1)}\n`
        end;
        `	fstpl	{emit_shift i.res.(0)}\n`
    | Lop(Ifloatofint) ->
        begin match i.arg.(0).loc with
          Stack s ->
            `	fildl	{emit_reg i.arg.(0)}\n`;
            `	fstpl	{emit_shift i.res.(0)}\n`
        | _ ->
            `	pushl	{emit_reg i.arg.(0)}\n`;
            stack_offset := !stack_offset + 4;
            `	fildl	(%esp)\n`;
            `	fstpl	{emit_shift i.res.(0)}\n`;
            `	addl	$4, %esp\n`;
            stack_offset := !stack_offset - 4
        end
    | Lop(Iintoffloat) ->
        stack_offset := !stack_offset - 8;
        `	subl	$8, %esp\n`;
        `	fnstcw	4(%esp)\n`;
        `	movl	4(%esp), %eax\n`;
        `	movb    $12, %ah\n`;
        `	movl	%eax, (%esp)\n`;
        `	fldcw	(%esp)\n`;
        `	fldl	{emit_reg i.arg.(0)}\n`;
        begin match i.res.(0).loc with
          Stack s ->
            `	fistpl	{emit_shift i.res.(0)}\n`
        | _ ->
            `	fistpl	(%esp)\n`;
            `	movl	(%esp), {emit_reg i.res.(0)}\n`
        end;
        `	addl	$8, %esp\n`;
        stack_offset := !stack_offset + 8
    | Lop(Ispecific Ineg) ->
        `	neg	{emit_reg i.res.(0)}\n`
    | Lop(Ispecific(Ilea addr)) ->
        `	lea	{emit_addressing addr i.arg 0}, {emit_reg i.res.(0)}\n`
    | Lreturn ->
        let n = frame_size() - 4 in
        if n > 0 then
          `	addl	${emit_int n}, %esp\n`;
        `	ret\n`
    | Llabel lbl ->
        `{emit_label lbl}:\n`
    | Lbranch lbl ->
        `	jmp	{emit_label lbl}\n`
    | Lcondbranch(tst, lbl) ->
        begin match tst with
          Itruetest ->
            `	cmpl	$0, {emit_reg i.arg.(0)}\n`;
            `	jne	{emit_label lbl}\n`
        | Ifalsetest ->
            `	cmpl	$0, {emit_reg i.arg.(0)}\n`;
            `	je	{emit_label lbl}\n`
        | Iinttest cmp ->
            `	cmpl	{emit_reg i.arg.(1)}, {emit_reg i.arg.(0)}\n`;
            let b = name_for_cond_branch cmp in
            `	j{emit_string b}	{emit_label lbl}\n`
        | Iinttest_imm(cmp, n) ->
            `	cmpl	${emit_int n}, {emit_reg i.arg.(0)}\n`;
            let b = name_for_cond_branch cmp in
            `	j{emit_string b}	{emit_label lbl}\n`
        | Ifloattest cmp ->
            `	fnstsw	%ax\n`;
            match cmp with
              Ceq ->
                `	andb	$69, %al\n`;
                `	cmpb	$64, %al\n`;
                `	je	{emit_label lbl}\n`
            | Cne ->
                `	andb	$68, %al\n`;
                `	xorb	$64, %al\n`;
                `	jne	{emit_label lbl}\n`
            | Cle ->
                `	andb	$69, %al\n`;
                `	decb	%al\n`;
                `	cmpb	$64, %al\n`;
                `	jb	{emit_label lbl}\n`
            | Cge ->
                `	andb	$5, %al\n`;
                `	je	{emit_label lbl}\n`
            | Clt ->
                `	andb	$69, %al\n`;
                `	cmpb	$1, %al\n`;
                `	je	{emit_label lbl}\n`
            | Cgt ->
                `	andb	$69, %al\n`;
                `	je	{emit_label lbl}\n`
        end
    | Lswitch jumptbl ->
        begin match Array.length jumptbl with
          0 -> ()
        | 1 -> ()
        | 2 ->
            `	cmpl	$0, {emit_reg i.arg.(0)}\n`;
            `	jne	{emit_label jumptbl.(1)}\n`
        | 3 ->
            `	cmpl	$1, {emit_reg i.arg.(0)}\n`;
            `	jg	{emit_label jumptbl.(2)}\n`;
            `	je	{emit_label jumptbl.(1)}\n`
        | n ->
          let lbl = new_label() in
          `	jmp	*{emit_label lbl}(, {emit_reg i.arg.(0)}, 4)\n`;
          `	.align	2\n`;
          `{emit_label lbl}:`;
          for i = 0 to n - 1 do
            `	.long	{emit_label jumptbl.(i)}\n`
          done
        end
    | Lpushtrap lbl ->
        `	pushl	_caml_exception_pointer\n`;
        `	pushl	${emit_label lbl}\n`;
        `	movl	%esp, _caml_exception_pointer\n`;
        stack_offset := !stack_offset + 8
    | Lpoptrap ->
        `	addl	$4, %esp\n`;
        `	popl	_caml_exception_pointer\n`;
        stack_offset := !stack_offset - 8
    | Lentertrap ->
        ()
    | Lraise ->
        `	movl	_caml_exception_pointer, %esp\n`;
        `	popl	%edx\n`;
        `	popl    _caml_exception_pointer\n`;
        `	jmp	*%edx\n`

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

(* Emission of the floating-point constants *)

let emit_float_constant (lbl, cst) =
  `	.data\n`;
  `{emit_label lbl}:	.double	{emit_string cst}\n`

(* Emission of a function declaration *)

let fundecl fundecl =
  function_name := fundecl.fun_name;
  tailrec_entry_point := new_label();
  stack_offset := 0;
  float_constants := [];
  `	.text\n`;
  `	.align	4\n`;
  `	.globl	_{emit_symbol fundecl.fun_name}\n`;
  `_{emit_symbol fundecl.fun_name}:\n`;
  let n = frame_size() - 4 in
  if n > 0 then
    `	subl	${emit_int n}, %esp\n`;
  `{emit_label !tailrec_entry_point}:`;
  emit_all fundecl.fun_body;
  List.iter emit_float_constant !float_constants

(* Emission of data *)

let emit_item = function
    Cdefine_symbol s ->
      `	.globl	_{emit_symbol s}\n`;
      `_{emit_symbol s}:\n`
  | Cdefine_label lbl ->
      `{emit_label (10000 + lbl)}:\n`
  | Cint8 n ->
      `	.byte	{emit_int n}\n`
  | Cint16 n ->
      `	.word	{emit_int n}\n`
  | Cint n ->
      `	.long	{emit_int n}\n`
  | Cfloat f ->
      `	.double	{emit_string f}\n`
  | Csymbol_address s ->
      `	.long	_{emit_symbol s}\n`
  | Clabel_address lbl ->
      `	.long	{emit_label (10000 + lbl)}\n`
  | Cstring s ->
      let l = String.length s in
      if l = 0 then ()
      else if l < 80 then
        `	.ascii	{emit_string_literal s}\n`
      else begin
        let i = ref 0 in
        while !i < l do
          let n = min (l - !i) 80 in
          `	.ascii	{emit_string_literal(String.sub s !i n)}\n`;
          i := !i + n
        done
      end
  | Cskip n ->
      if n > 0 then `	.space	{emit_int n}\n`
  | Calign n ->
      `	.align	{emit_int(Misc.log2 n)}\n`

let data l =
  `	.data\n`;
  List.iter emit_item l

(* Beginning / end of an assembly file *)

let begin_assembly() = ()

let end_assembly() =
  `	.data\n`;
  `	.globl	_Frametable\n`;
  `_Frametable:\n`;
  List.iter emit_frame !frame_descriptors;
  frame_descriptors := [];
  `	.long	0\n`