2023-04-26 01:47:47 +02:00
|
|
|
/*
|
|
|
|
* IR - Lightweight JIT Compilation Framework
|
2023-10-03 07:34:02 +02:00
|
|
|
* (Examples package)
|
2023-04-26 01:47:47 +02:00
|
|
|
* Copyright (C) 2023 by IR project.
|
|
|
|
* Authors: Anatol Belski <anbelski@linux.microsoft.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ir.h"
|
|
|
|
#include "ir_builder.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* int32_t myfunc(int32_t x, int32_t y) {
|
|
|
|
* return x - y;
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
typedef int32_t (*myfunc_t)(int32_t, int32_t);
|
|
|
|
|
|
|
|
void gen_myfunc(ir_ctx *ctx)
|
|
|
|
{
|
2023-05-29 21:06:25 +02:00
|
|
|
/* Function entry start */
|
2023-04-26 01:47:47 +02:00
|
|
|
ir_START();
|
2023-05-29 21:06:25 +02:00
|
|
|
/* Declare function parameters */
|
2023-04-26 01:47:47 +02:00
|
|
|
ir_ref x = ir_PARAM(IR_I32, "x", 1);
|
|
|
|
ir_ref y = ir_PARAM(IR_I32, "y", 2);
|
2023-05-29 21:06:25 +02:00
|
|
|
/* Subtract y from x and save it into a new ref. */
|
2023-04-26 01:47:47 +02:00
|
|
|
ir_ref cr = ir_SUB_I32(x, y);
|
2023-05-29 21:06:25 +02:00
|
|
|
/* Function end */
|
2023-04-26 01:47:47 +02:00
|
|
|
ir_RETURN(cr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
ir_ctx ctx = {0};
|
|
|
|
|
|
|
|
ir_consistency_check();
|
|
|
|
|
2023-05-23 09:52:59 +02:00
|
|
|
ir_init(&ctx, IR_FUNCTION | IR_OPT_FOLDING, IR_CONSTS_LIMIT_MIN, IR_INSNS_LIMIT_MIN);
|
2023-04-26 01:47:47 +02:00
|
|
|
|
|
|
|
gen_myfunc(&ctx);
|
|
|
|
|
|
|
|
size_t size;
|
2023-05-23 09:52:59 +02:00
|
|
|
void *entry = ir_jit_compile(&ctx, 2, &size);
|
2023-04-26 01:47:47 +02:00
|
|
|
if (entry) {
|
|
|
|
printf("42 - 24 = %d\n", ((myfunc_t)entry)(42, 24));
|
|
|
|
}
|
|
|
|
|
|
|
|
ir_free(&ctx);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|