Skip to content

Instantly share code, notes, and snippets.

@kayceesrk
Created September 3, 2025 06:40
Show Gist options
  • Save kayceesrk/b26dd73eafb0cd26f608844256bdf346 to your computer and use it in GitHub Desktop.
Save kayceesrk/b26dd73eafb0cd26f608844256bdf346 to your computer and use it in GitHub Desktop.
d.file ""
// Assembly output for sum.ml
.data
.globl _camlSum__data_begin
_camlSum__data_begin:
.text
.globl _camlSum__code_begin
_camlSum__code_begin:
.data
.align 3
.data
.align 3
.quad 4087
.globl _camlSum__2
_camlSum__2:
.quad _caml_curry2
.quad 144115188075855879
.quad _camlSum__sum_of_elements_267
.data
.align 3
.quad 3063
.globl _camlSum__1
_camlSum__1:
.quad _camlSum__sum_272
.quad 72057594037927941
.data
.align 3
.quad 2816
.globl _camlSum
.globl _camlSum
_camlSum:
.quad 1
.quad 1
.data
.align 3
.globl _camlSum__gc_roots
.globl _camlSum__gc_roots
_camlSum__gc_roots:
.quad _camlSum
.quad 0
.text
.align 3
.globl _camlSum__sum_of_elements_267
_camlSum__sum_of_elements_267:
.cfi_startproc
sub sp, sp, #16 // Allocate stack space for return address
.cfi_adjust_cfa_offset 16
.cfi_offset 30, -8
str x30, [sp, #8] // Save return address
L101:
ldr x16, [x28, #0] // GC check (boilerplate)
cmp x27, x16 // GC check (boilerplate)
b.ls L103 // GC call if needed
L104:
tbz x0, #0, L100 // Test if l (x0) is [] (empty list)
// If l is empty, return acc (x1)
mov x0, x1 // Return acc
ldr x30, [sp, #8] // Restore return address
add sp, sp, #16 // Restore stack pointer
.cfi_adjust_cfa_offset -16
ret // Return
.cfi_adjust_cfa_offset 16
L100:
// l is not empty: pattern match h :: t
ldr x2, [x0, #0] // Load h (head of list) into x2
add x3, x1, x2 // acc + h, store in x3
// OCaml integers are tagged, so subtract 1 to keep correct representation
sub x1, x3, #1 // Prepare acc for next call (OCaml int representation)
ldr x0, [x0, #8] // Load t (tail of list) into x0
b L101 // Recursive call: sum_of_elements t (acc + h)
L103: bl _caml_call_gc // Call GC if needed
L102: b L104 // Continue after GC
.cfi_endproc
.text
.align 3
.globl _camlSum__sum_272
_camlSum__sum_272: // let sum l = sum_of_elements l 0
.cfi_startproc
L106:
tbz x0, #0, L105 // Test if l (x0) is [] (empty list)
orr x0, xzr, #1 // Return 0 for empty list (OCaml int representation)
ret // Return
L105:
// l is not empty, call sum_of_elements
ldr x1, [x0, #0] // Load first element (not used - we want acc=0)
ldr x0, [x0, #8] // Load list l
b _camlSum__sum_of_elements_267 // Jump to sum_of_elements with acc=0
.cfi_endproc
.text
.align 3
.globl _camlSum__entry
_camlSum__entry:
.cfi_startproc
L107:
adrp x0, _camlSum__2@GOTPAGE
ldr x0, [x0, _camlSum__2@GOTPAGEOFF]
adrp x1, _camlSum@GOTPAGE
ldr x1, [x1, _camlSum@GOTPAGEOFF]
str x0, [x1, #0]
adrp x2, _camlSum__1@GOTPAGE
ldr x2, [x2, _camlSum__1@GOTPAGEOFF]
str x2, [x1, #8]
orr x0, xzr, #1
ret
.cfi_endproc
.data
.align 3
.text
.globl _camlSum__code_end
_camlSum__code_end:
.data
.quad 0
.globl _camlSum__data_end
_camlSum__data_end:
.quad 0
.align 3
.globl _camlSum__frametable
_camlSum__frametable:
.quad 1
.quad L102
.short 18
.short 2
.short 1
.short 3
.byte 0
.align 3
.align 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment