0

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?

Shisui
  • 1,051
  • 1
  • 8
  • 23

1 Answers1

1

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.

Jeffrey Scofield
  • 65,646
  • 2
  • 72
  • 108
  • Small correction, it is possible to do unsafe type casting in OCaml but it is considered, well, unsafe and to be avoided unless ones knows exactly what one is doing. – Yawar Dec 27 '21 at 02:34
  • ((The unsafe type casting is not part of the OCaml language per Xavier Leroy, and I agree with this way of looking at it.)) – Jeffrey Scofield Dec 27 '21 at 05:46
  • Yes, I understand it's claimed that it's not part of the language. I just said that it was possible to do it. – Yawar Jan 01 '22 at 18:46