Search code examples
ocaml

Compiling OCaml .cmm file


I wrote simple OCaml code like:

let rec fib(n: int) = 
  match n with
  | 0 -> 0
  | 1 -> 1
  | _ -> fib(n-1) + fib(n-2)

With -dcmm option to ocamlopt, I could generate .cmm files following:


cmm:
(data)
(data int 3063 "camlTest__1": addr "camlTest__fib_267" int 72057594037927941)
(data int 1792 global "camlTest" "camlTest": int 1)
(data
 global "camlTest__gc_roots"
 "camlTest__gc_roots":
 addr "camlTest"
 int 0)
(function{test.ml:1,11-88} camlTest__fib_267 (n/268: val)
 (if (!= n/268 1)
   (if (!= n/268 3)
     (+
       (+ (app{test.ml:5,9-17} "camlTest__fib_267" (+ n/268 -2) val)
         (app{test.ml:5,20-28} "camlTest__fib_267" (+ n/268 -4) val))
       -1)
     3)
   1))

(function camlTest__entry ()
 (let clos/271 "camlTest__1" (store val(root-init) "camlTest" clos/271)) 1)

(data)

ocamlopt can compile OCaml code (.ml) into machine code, then can I generate machine code directly from .cmm files?

I didn't find any option that compile .cmm files to machine code in ocamlopt, so I tried to use Asmgen in ocaml-base-compiler modules.

However, Asmgen function requires Cmm.phrase type but did not found how to convert .cmm files to Cmm.phrase type.


Solution

  • OCaml compiler flags starting with -d... are debugging flags mostly aimed at compiler developers. The -dcmm flag is no exception, and its output cannot be read back by the compiler (and is probably incomplete).