TheDan64 / inkwell

It's a New Kind of Wrapper for Exposing LLVM (Safely)
https://thedan64.github.io/inkwell/
Apache License 2.0
2.22k stars 217 forks source link

Const ints have no support for anything bigger than 128 bits #501

Closed Toni-Graphics closed 1 month ago

Toni-Graphics commented 1 month ago

Describe the Bug Hi, The const_intfunction has an argument of u64 and not u128 so it won't support 128 bit intengers.

(If this is skill issue sorry, I never worked with llvm)

To Reproduce

contxt.i128_type().const_int(u128::MAX, false);

Expected Behavior I expect that the function would take a 128 bit wide input and not 64bit.

LLVM Version:

Desktop:

Bye

matt-cornell commented 1 month ago

LLVM can take arbitrary sized integers, so really even a u128 wouldn't be sufficient. The use of u64 comes from the C++ API (which decides the C API), since 128 bit integers aren't widely supported and don't exactly have great ABI compatibility.
If you want to construct constant integers with more than 64 bits, you can use const_int_from_string which will parse it for a simple solution, or you can use const_int_arbitrary_precision to pass in a slice of u64s, which is generally a better solution.
If you insist on using u128, then this should do what you need:

fn const_int_u128(ty: IntType, val: u128) -> IntValue {

  let bytes = [(val & 0xffffffffffffffff) as u64, (val >> 64) as u64];
  ty.const_int_arbitrary_precision(&bytes)
}

Generally though, 64 bit integers are more than enough.

Toni-Graphics commented 1 month ago

Thanks