4 ms·
Because I have not yet figured out how to start executing at somewhere other than the beginning of the generated code, and because I don’t store generated code
by jsmith45 6y ago
Because I have not yet figured out how to start executing at somewhere other than the beginning of the generated code, and because I don’t store generated code in any intermediate buffers, and because we don’t know the sizes of any code in advance, I do this funky thing where I emit a jmp to the body code.
If you, dear reader, have a better solution, please let me know.
Assuming you know the byte offset into the code buffer, of your "main" equivalent label, you should be able to use the following:
byte* start_address = buf->address + offset;
JitFunction function = *(JitFunction*)(&start_address);
return function(heap);
I'm guessing you were running into the issue that you can't do this in one line while also suppressing the warning about the data pointer to function pointer.
The "normal" way to do this would be the following, but it has the downside of the compiler warning:
JitFunction function = (JitFunction)(buf->address + offset);
return function(heap);
- tekknolagi 6y agoSo I was doing this (storing the main entry point) but it was crashing. I'll have to try again, I suppose.
- oso2k 6y agoIs `kLabelPlaceholder` an absolute or relative address? If it's absolute, you start executing at `kLabelPlaceholder`. If it's relative, you start executing at `buf + kLabelPlaceholder`. You could see the final value if you compile with debug info and run the binary under `gdb` or some other debugger. It might help to read skeeto's blogs [0][1][2]. [0] https://nullprogram.com/blog/2015/03/19/ https://nullprogram.com/blog/2015/03/19/ [1] https://nullprogram.com/blog/2016/12/11/ https://nullprogram.com/blog/2016/12/11/ [2] https://nullprogram.com/blog/2018/11/15/ https://nullprogram.com/blog/2018/11/15/
- tekknolagi 6y agoIt's a placeholder. It's meant to fill in before actual addresses are filled in. But all the call targets are relative addresses.
- tekknolagi 6y agoAlright, I got it to work! I think I might have done something stupid like forget to move the prologue, too, in the past revision. Now it works. Thanks! I've updated the post.