Search code examples
compiler-constructionocamlllvmllvm-irllvm-c++-api

Converting string to lltype in order to utilize struct_set_body?


I'm trying to generate LLVM ir, and currently have this

let fill_structs = StringMap.iter (fun k v -> L.struct_set_body k v false ) structs 
(* more let...in statements *)

The error message I'm getting is over my 'k' that I pass into L.struct_set_body

StringMap.key
This expression has type string but an expression was expected of type
  L.lltype

My understanding of ocaml is weak, so I see from here that struct_set_body does this

lltype -> lltype array -> bool -> unit

Which I assume means it takes a lltype array and a boolean, and returns unit? I don't know, I am thinking about this the way I would a function signature in C or Java, but I don't think that's how I should be thinking about this.

The other thing is I don't know how to make my string 'k' into a lltype to accomplish what I want to do.


Solution

  • Which I assume means it takes a lltype array and a boolean, and returns unit?

    Close, but it takes another argument. The first argument it takes is of type Llvm.lltype. Stringmap.iter's signature means k is a string, but passing this to Llvm.struct_set_body results in a type mismatch.

    You need something that will map a string value to an Llvm.lltype value.

    As @glennsl suggests in comments, the best candidate to do this is named_struct_type which has type llcontext -> string -> lltype.

    You have been asking a lot of very basic questions about OCaml which suggests that you need to do some more introductory work. No one here doubts the energy with which you are applying yourself, but your course of learning is almost certainly too "run before you can walk" to let you really succeed. Issues like not being able to read OCaml types properly is just going to lead to frustration for you when you attempt to use complex modules.