carlokok commented on Issue #1164:
If it's of any interest to anyone, I wrote a C api that allows this.
api: https://gist.github.com/carlokok/d0d079dba76ee253a1571d58f2e791fb
#include <cranelift.h> #include <stdio.h> void cb1(uintptr_t userdata, const char* err, const char* fn) { printf("Error %s: %s\n", fn, err); } void cb2(uintptr_t userdata, const char* err, const char* fn) { printf("Message %s: %s\n", fn, err); } void cb(uintptr_t userdata, FunctionData* fd) { BlockCode entry = cranelift_create_block(fd); BlockCode larger = cranelift_create_block(fd); BlockCode exit = cranelift_create_block(fd); cranelift_append_block_params_for_function_params(fd, entry); cranelift_switch_to_block(fd, entry); ValueCode res[2]; cranelift_block_params(fd, entry, res); VariableCode a = cranelift_declare_var(fd, TypeI32); cranelift_def_var(fd, a, res[0]); VariableCode b = cranelift_declare_var(fd, TypeI32); cranelift_def_var(fd, b, res[1]); VariableCode retvar = cranelift_declare_var(fd, TypeI32); cranelift_def_var(fd, retvar, cranelift_iconst(fd, TypeI32, 0)); cranelift_ins_br_icmp(fd, CraneliftIntCCSignedLessThan, a, b, larger, 0, NULL); cranelift_def_var(fd, retvar, cranelift_use_var(fd, a)); cranelift_ins_jump(fd, exit, 0, NULL); cranelift_switch_to_block(fd, larger); cranelift_def_var(fd, retvar, cranelift_use_var(fd, b)); cranelift_ins_jump(fd, exit, 0, NULL); cranelift_switch_to_block(fd, exit); ValueCode retval = cranelift_use_var(fd, retvar); cranelift_return(fd, 1, &retval); cranelift_seal_all_blocks(fd); } void cbp(uintptr_t ud, char* data) { printf("\n%s\n", data); } int main() { ModuleData* mod = cranelift_module_new("x86_64-pc-windows", "is_pic,enable_simd,enable_atomics,enable_verifier", "mymodule", 0, cb1, cb2); cranelift_signature_builder_add_param(mod, TypeI32); cranelift_signature_builder_add_param(mod, TypeI32); cranelift_signature_builder_add_result(mod, TypeI32); cranelift_build_function(mod, 0, cb); cranelift_function_to_string(mod, 0, cbp); uint32_t id; cranelift_declare_function(mod, "max", Export, &id); cranelift_define_function(mod, id); uint32_t id2; uint8_t c[4] = { 1,2,3,4 }; cranelift_set_data_value(mod, c, 4); cranelift_define_data(mod, "testdata_w", Export, Writable, 0, &id2); cranelift_assign_data_to_global(mod, id2); cranelift_clear_data(mod); cranelift_set_data_value(mod, c, 4); cranelift_define_data(mod, "testdata_r", Export, None, 0, &id2); cranelift_assign_data_to_global(mod, id2); cranelift_clear_data(mod); cranelift_set_data_value(mod, c, 4); cranelift_define_data(mod, "testdata_w2", Export, Writable, 0, &id2); cranelift_set_data_section(mod, "SEG", "SEC"); cranelift_assign_data_to_global(mod, id2); // cranelift_clear_data(mod); cranelift_module_emit_object(mod, "c:\\projects\\test.o"); cranelift_module_delete(mod); }
bjorn3 commented on Issue #1164:
I think
cranelift_block_params
should take the expected param count as argument to prevent accidental out-of-bounds memory accesses.
bjorn3 commented on Issue #1164:
It would also be nice if
cranelift-codegen-meta
automatically generated allcranelift_ins_*
functions.
carlokok commented on Issue #1164:
agreed :) but if there's no interest or if the interfaces don't want to be tied down then I won't bother. Also this was my very first foray into Rust.
Last updated: Nov 22 2024 at 16:03 UTC