Skip to content

[ocaml.tailcall] is wrong for functions with more than 10 arguments (in Mach). #10424

@yurug

Description

@yurug

Consider:

let rec h x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 =
  if x1 = 0 then
    0
  else
    (i [@ocaml.tailcall]) (x1 - 1) x2 x3 x4 x5 x6 x7 x8 x9 x10 x11
and i x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 =
  (h [@ocaml.tailcall]) x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11

This code is accepted by the compiler even when "Warning 51" is enabled.

However, when looking at its mach representation, the first call to i is not a tailcall:

camlBar__h_81(R/0[%rax] R/1[%rbx] R/2[%rdi] R/3[%rsi] R/4[%rdx] R/5[%rcx] R/6[%r8] R/7[%r9] R/8[%r12] R/9[%r13] R/40[si0]) {bar.ml:1,10-147}
  x1/29 := R/0[%rax]
  x2/30 := R/1[%rbx]
  x3/31 := R/2[%rdi]
  x4/32 := R/3[%rsi]
  x5/33 := R/4[%rdx]
  x6/34 := R/5[%rcx]
  x7/35 := R/6[%r8]
  x8/36 := R/7[%r9]
  x9/37 := R/8[%r12]
  x10/38 := R/9[%r13]
  x11/39 := R/40[si0]
  if x1/29 ==s 1 then
    I/45 := 1
    R/0[%rax] := I/45
    return R/0[%rax]
  else
    I/41 := x1/29
    I/41 := I/41 + -2{bar.ml:5,30-38}
    offset stack 16
    R/0[%rax] := I/41
    R/1[%rbx] := x2/30
    R/2[%rdi] := x3/31
    R/3[%rsi] := x4/32
    R/4[%rdx] := x5/33
    R/5[%rcx] := x6/34
    R/6[%r8] := x7/35
    R/7[%r9] := x8/36
    R/8[%r12] := x9/37
    R/9[%r13] := x10/38
    R/43[so0] := x11/39
    R/0[%rax] := call "camlBar__i_82" R/0[%rax] R/1[%rbx] R/2[%rdi] R/3[%rsi] R/4[%rdx] R/5[%rcx] R/6[%r8] R/7[%r9] R/8[%r12] R/9[%r13] R/43[so0]{bar.ml:5,8-70}
    offset stack -16
    V/42 := R/0[%rax]
    I/44 := V/42
    I/44 := I/44 + 2{bar.ml:5,4-70}
    R/0[%rax] := I/44
    return R/0[%rax]
  endif

Tested with trunk, 4.10.2, and 4.12.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions