简体   繁体   中英

Is this an example of casting in Ocaml, unclear on what this line is doing?

I am looking at a codegen.ml file being used to create LLVM IR. I am unfamilar with Ocaml so just require some clarification

  let i32_t      = L.i32_type    context
  and float_t    = L.double_type context

  let ltype_of_typ = function
      A.Int   -> i32_t
    | A.Float -> float_t
  in

  let global_vars : L.llvalue StringMap.t =
    let global_var m (t, n) = 
      let init = match t with
          A.Float -> L.const_float (ltype_of_typ t) 0.0

So for this line

 A.Float -> L.const_float (ltype_of_typ t) 0.0

What is the stuff after "->" doing? Is it casting 0.0 to a float?

The code is calling a function L.const_float with two parameters. The code reads as if it's setting up a global mapping from names to values. Some of the values appear to be representations of fixed floating numbers. Beyond that I can't help you because I'm not familiar with LLVM.

However there are no "type casts" in OCaml. There are a few functions in the standard library that convert between numeric types ( int_of_float for example). But there's no language feature for casting types. OCaml is a strongly typed language, and type casting (as in C or C++) is pretty much a violation of that.

It might be more helpful to ask your question in a forum devoted to LLVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM