diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-01-04 00:09:20 +0200 | 
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2025-01-04 00:09:20 +0200 | 
| commit | b9b5e343accd95b476a8a80e0b713521c7eb6974 (patch) | |
| tree | 3a0dbc9af281cdea578ad0f92a5ce4b588adff0e /src | |
| download | ejit-calc-b9b5e343accd95b476a8a80e0b713521c7eb6974.tar.gz ejit-calc-b9b5e343accd95b476a8a80e0b713521c7eb6974.zip  | |
calculator works on constant string
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.c | 62 | ||||
| -rw-r--r-- | src/source.mk | 2 | 
2 files changed, 64 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..27fe68e --- /dev/null +++ b/src/main.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <inttypes.h> + +#include "../deps/ejit/include/ejit/ejit.h" + +static struct ejit_func *compile(const char *program) +{ +	struct ejit_func *func = ejit_create_func(EJIT_INT32, 0, NULL); +	if (!func) { +		fprintf(stderr, "failed allocating func\n"); +		return NULL; +	} + +	/* initialize accumulator */ +	ejit_movi(func, EJIT_GPR(0), 0); + +	/* load constant 2 into register for multiplication/division since ejit +	 * doesn't have muli/divi instructions */ +	ejit_movi(func, EJIT_GPR(1), 2); + +	char c = 0; +	while ((c = *program++)) +	switch (c) { +	case '+': ejit_addi(func, EJIT_GPR(0), EJIT_GPR(0), 1); break; +	case '-': ejit_subi(func, EJIT_GPR(0), EJIT_GPR(0), 1); break; +	case '*': ejit_mulr(func, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1)); break; +	case '/': ejit_divr(func, EJIT_GPR(0), EJIT_GPR(0), EJIT_GPR(1)); break; +	default: break; +	} + +	/* return accumulator */ +	ejit_retr(func, EJIT_GPR(0)); + +	/* we use GPRs 0 and 1, so 2 in total, no FPRs, and we don't use 64 bit +	 * values (explicitly at least) */ +	ejit_compile_func(func, 2, 0, false); +	return func; +} + +static int32_t run(struct ejit_func *func) +{ +	return ejit_run_func(func, 0, NULL); +} + +static void destroy(struct ejit_func *func) +{ +	ejit_destroy_func(func); +} + +int main() +{ +	const char *program = "+ + * - /"; +	struct ejit_func *func = compile(program); +	if (!func) { +		fprintf(stderr, "failed to compile program\n"); +		return -1; +	} + +	int32_t result = run(func); +	printf("%" PRIi32 "\n", result); +	destroy(func); +} diff --git a/src/source.mk b/src/source.mk new file mode 100644 index 0000000..5b2c0e3 --- /dev/null +++ b/src/source.mk @@ -0,0 +1,2 @@ +SRCS		!= echo src/*.c +CALC_SOURCES	+= $(SRCS)  | 
