-1

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.

Shisui
  • 1,051
  • 1
  • 8
  • 23
  • 6
    1. Learn the language you're trying to use, starting at the shallow end instead of jumping right into the deep end. 2. Learn the concepts of the framework you're trying to use, starting at the shallow end instead of jumping in at the deep end (do you see a pattern here?). 3. Find a function that takes a `string` and returns an `lltype`, then read the documentation for that function thoroughly to make sure it does what you want. – glennsl Dec 31 '21 at 13:49
  • 1
    Your reading of the type is almost correct, but for some reason you've ignored the first argument. – glennsl Dec 31 '21 at 13:50
  • 3
    You say yourself that your knowledge of OCaml is weak, and your questions demonstrate quite clearly that you have not understood the basics of neither the language nor LLVM. This is the underlying problem of all your questions these past few days. You're trying to grasp beyond your reach, which isn't going to get you to your goal any faster because you'll be spending most of your time repeatedly falling and trying to get up from the floor. Take this question, for example. It should have taken you 1 minute to find the function you need, but has probably taken you at least an hour at this point. – glennsl Dec 31 '21 at 14:12
  • @glennsl Thank you, I appreciate your help and am trying my best – Shisui Dec 31 '21 at 14:20
  • The function is probably `named_struct_type` btw. I don't really know LLVM, but just from the type signature that seems to be how you'd create an `lltype` from a `string`. – glennsl Dec 31 '21 at 15:06

1 Answers1

3

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.

Chris
  • 26,361
  • 5
  • 21
  • 42