Skip to content

Instantly share code, notes, and snippets.

@whitequark
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whitequark/5e50246acea21f9d1312 to your computer and use it in GitHub Desktop.
Save whitequark/5e50246acea21f9d1312 to your computer and use it in GitHub Desktop.
OCaml code and corresponding assembly
OCaml code:
let ptr_of_raw_address addr =
CPointer {
reftype = Void; raw_ptr = Raw.PtrType.of_int64 addr;
pmanaged = None; pbyte_offset = 0; }
Assembly:
0x41ec30 sub $0x8,%rsp ; make a frame
0x41ec34 mov %rax,%rdi ; save 1st arg from clobbering
0x41ec37 sub $0x38,%r15 ; allocate 0x38 bytes from young gen
0x41ec3b lea caml_young_limit, %rax
0x41ec42 cmp (%rax),%r15 ; young gen exhausted?
0x41ec45 jb 0x41ec82 ; go GC and return
0x41ec47 lea 0x8(%r15),%rbx ; load addr of cptr ({reftype; ..} thing) into %rbx
0x41ec4b movq $0x1000,-0x8(%rbx) ; set header: 4 words, white
0x41ec53 movq $0x1,(%rbx) ; put reftype=Void (numeric value 0)
0x41ec5a mov %rdi,0x8(%rbx) ; put raw_ptr=addr (Raw.PtrType.of_int64 is identity; inlined)
0x41ec5e movq $0x1,0x10(%rbx) ; put pmanaged=None (numeric value 0)
0x41ec66 movq $0x1,0x18(%rbx) ; put pbyte_offset=0
0x41ec6e lea 0x28(%rbx),%rax ; load addr of next allocated block (CPointer ..) after %rbx into %rax
0x41ec72 movq $0x400,-0x8(%rax) ; set header: 1 word, white
0x41ec7a mov %rbx,(%rax) ; put {reftype;} thing inside CPointer
0x41ec7d add $0x8,%rsp ; destroy frame
0x41ec81 retq
0x41ec82 callq caml_call_gc ; go GC
0x41ec87 jmp 0x41ec37 ; try alloc again
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment