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?

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.

Similar Posts

  • 禁止对话框关闭按钮和Alt+F4

    在某些情况下我们需要防止用户单击窗口的标题栏中的关闭按钮关闭 MFC 应用程序。可以删除窗口的WS_SYSMENU 样式, 但是,这样最大化最小化和还原按钮也被删除,并且无法添加。 这是Windows的设计依据。 可以通过禁用关闭按钮来模拟没有关闭按钮的窗口。 在 WM_CREATE 消息处理程序中禁用关闭按钮。使用下面的代码: CMenu *pSysMenu = GetSystemMenu(FALSE); ASSERT(pSysMenu != NULL); VERIFY(pSysMenu->RemoveMenu(SC_CLOSE, MF_BYCOMMAND)); 这样删除之后关闭按钮变为灰色,用户无法点击。但是使用Alt+F4仍然可以关闭程序。要将此功能也禁用需要重载CDialog的OnSysCommand方法。代码如下: void MyDlg::OnSysCommand( UINT nID, LPARAM lParam ) { if ( ( nID & 0xFFF0 ) == IDM_ABOUTBOX ) { CAboutDlg dlgAbout; //if you have an about dialog dlgAbout.DoModal(); } //add the following code else if…

Leave a Reply

Your email address will not be published. Required fields are marked *