ocaml_如何获取由ocamlopt生成的OCaml代码的汇编代码?

ocaml

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

如何获取本机ocamlopt编译器生成的OCaml代码的本机汇编代码(例如x86-64 asm)?

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

要获取OCaml程序的汇编代码,请将这些参数添加到ocamlopt

-S -inline 20 -nodynlink

An example is as follows.

一个例子如下。

The OCaml program:

OCaml程序:

$ 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.

在这里, camlNot__not_1008是该函数的组件notcaml是前缀。 Not模块/文件名。

Answered by anonymous.
匿名回答。

翻译自: https://www.systutorials.com/how-to-get-the-assembly-code-for-ocaml-code-generated-by-ocamlopt/

ocaml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OCaml 是一门函数式编程语言,它的平衡二叉树的实现方式可能与其他语言略有不同。下面是一个使用 OCaml 的简单平衡二叉树的实现代码: ``` type 'a tree = | Empty | Node of 'a tree * 'a * 'a tree * int let height = function | Empty -> 0 | Node(_, _, _, h) -> h let create l x r = let hl = height l and hr = height r in Node(l, x, r, (if hl >= hr then hl + 1 else hr + 1)) let bal l x r = let hl = height l and hr = height r in if hl > hr + 2 then match l with | Node(ll, lv, lr, _) -> if height ll >= height lr then create ll lv (create lr x r) else (match lr with | Node(lrl, lrv, lrr, _) -> create (create ll lv lrl) lrv (create lrr x r) | Empty -> assert false) | Empty -> assert false else if hr > hl + 2 then match r with | Node(rl, rv, rr, _) -> if height rr >= height rl then create (create l x rl) rv rr else (match rl with | Node(rll, rlv, rlr, _) -> create (create l x rll) rlv (create rlr rv rr) | Empty -> assert false) | Empty -> assert false else Node(l, x, r, (if hl >= hr then hl + 1 else hr + 1)) let rec add_leftmost x = function | Node(l, v, r, h) -> bal (add_leftmost x l) v r | Empty -> Node(Empty, x, Empty, 1) let rec add_rightmost x = function | Node(l, v, r, h) -> bal l v (add_rightmost x r) | Empty -> Node(Empty, x, Empty, 1) let rec join l v r = match (l, r) with | (Empty, _) -> add_leftmost v r | (_, Empty) -> add_rightmost v l | (Node(ll, lv, lr, lh), Node(rl, rv, rr, rh)) -> if lh > rh + 2 then bal ll lv (join lr v r) else if rh > lh + 2 then bal (join l v rl) rv rr else create l v r let rec split x = function | Empty -> (Empty, None, Empty)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值