How to get the assembly code for OCaml code generated by ocamlopt?

How to get the native assembly code (e.g. x86-64 asm) for OCaml code generated by the native ocamlopt compiler?

asked May 10, 2013 by anonymous
retagged May 14, 2013

1 Answer

 
Best answer

To get the assembly code for an OCaml program, add these parameters to ocamlopt:

-S -inline 20 -nodynlink

An example is as follows.

The OCaml program:

$ cat not.ml 
let not x = 
    ((x - 1) lxor (x land (x - 1))) lsr 62
;;

not 0;;

not 1;;

not (-1);;

not (1 lsl 62);;

Compile the code:

$ ocamlopt -S -inline 20 -nodynlink not.ml -o not.opt

The assembly code generated:

$ cat not.s 
	.data
	.globl	camlNot__data_begin
camlNot__data_begin:
	.text
	.globl	camlNot__code_begin
camlNot__code_begin:
	.data
	.quad	1024
	.globl	camlNot
camlNot:
	.space	8
	.data
	.quad	2295
camlNot__1:
	.quad	camlNot__not_1008
	.quad	3
	.text
	.align	16
	.globl	camlNot__not_1008
camlNot__not_1008:
	.cfi_startproc
.L100:
	movq	%rax, %rdi
	addq	$-2, %rdi
	movq	%rax, %rbx
	andq	%rdi, %rbx
	addq	$-2, %rax
	xorq	%rbx, %rax
	orq	$1, %rax
	shrq	$62, %rax
	orq	$1, %rax
	ret
	.cfi_endproc
	.type	camlNot__not_1008,@function
	.size	camlNot__not_1008,.-camlNot__not_1008
	.text
	.align	16
	.globl	camlNot__entry
camlNot__entry:
	.cfi_startproc
.L101:
	leaq	camlNot__1(%rip), %rax
	movq	%rax, camlNot(%rip)
	movq	$3, %rax
	movq	$1, %rax
	movq	$1, %rax
	movq	$1, %rax
	movq	$1, %rax
	ret
	.cfi_endproc
	.type	camlNot__entry,@function
	.size	camlNot__entry,.-camlNot__entry
	.data
	.text
	.globl	camlNot__code_end
camlNot__code_end:
	.data
	.globl	camlNot__data_end
camlNot__data_end:
	.long	0
	.globl	camlNot__frametable
camlNot__frametable:
	.quad	0
	.section .note.GNU-stack,"",%progbits

Here, camlNot__not_1008 is the assembly for the function not. caml is the prefix. Not is the module/file name.

answered May 10, 2013 by anonymous

Please log in or register to answer this question.

Related questions

1 answer
1 answer
1 answer
1 answer
asked Dec 14, 2013 by anonymous
1 answer
1 answer
asked Aug 11, 2013 by anonymous
Copyright © SysTutorials. User contributions licensed under cc-wiki with attribution required.
Hosted on Dreamhost

...