o/
I'm very new here & to Cranelift, so sorry if I misunderstand some things or struggle alot. I'm just gonna cut to the chase with my questions.
load
and store
methods on InstBuilder
, but I always get an access violation.error: process didn't exit successfully: `target\debug\jitmemtest.exe` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)
@Endistic
For the address I passed zero, because I wasn't really sure what else to pass. I couldn't find anything address-related besides the reference types in the docs. It was something like:
{
let param = builder.block_params(block)[0];
let addr = builder.ins().iconst(types::I32, 0);
builder
.ins()
.store(MemFlags::new(), param, addr, Offset32::new(0));
let got = builder
.ins()
.load(types::I32, MemFlags::new(), addr, Offset32::new(0));
builder.ins().return_(&[got]);
}
I think the zero is probably what caused the access violation, but I don't know how I coul0d get a valid address the function could write to into the function.
Also, for the Cranelift Book, I might give making that a try once I learn how to do those things - I've wrote my fair share of documentation before and have a fair bit in C and other low-level things.
Right, you have to pass the address you want to load/store. 0 is never a valid address.
Do you want to access a stack variable?
Then you can create a stackslot using builder.func.create_sized_stack_slot()
and get the address using builder.ins().stack_addr()
.
That did the trick, thank you!!
{
let slot = builder.func.create_sized_stack_slot(StackSlotData::new(StackSlotKind::ExplicitSlot, 4));
let param = builder.block_params(block)[0];
let addr = builder.ins().stack_addr(types::I32, slot, Offset32::new(0));
builder
.ins()
.store(MemFlags::new(), param, addr, Offset32::new(0));
let got = builder
.ins()
.load(types::I32, MemFlags::new(), addr, Offset32::new(0));
builder.ins().return_(&[got]);
}
And for future reference while you're here, what about memory on the heap? How would I allocate & do stuff with that?
You could use malloc like you would do in C. load and store will work with the returned address too. Just make sure to free() it again once you are done to avoid a memory leak.
Ah ok, thank you so much for your help!
4 messages were moved from this topic to #cranelift > new documentation book by Jamey Sharp.
Last updated: Dec 23 2024 at 12:05 UTC