add zlc benchmarks

This commit is contained in:
zongor 2025-07-20 19:12:41 -04:00
parent 5184b60136
commit c43761a8da
12 changed files with 66 additions and 9 deletions

View File

@ -2,7 +2,7 @@
# -----------------------
# Native build (gcc)
CC_NATIVE = gcc
CFLAGS_NATIVE = -g -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I.
CFLAGS_NATIVE = -g -O2 -std=c89 -Wall -Wextra -Werror -Wno-unused-parameter -I.
LDFLAGS_NATIVE =
LDLIBS_NATIVE = -lSDL2

View File

@ -13,8 +13,12 @@ int main(int argc, char **argv) {
/* test_add_compile(&vm); */
/* test_add_function_compile(&vm); */
/* test_loop_compile(&vm); */
test_hello_world_compile(&vm);
/* test_recursive_function_compile(&vm); */
/* test_hello_world_compile(&vm); */
test_recursive_function_compile(&vm);
while(step_vm(&vm));
return 0;
uint32_t buffer_size = 640 * 480 * sizeof(uint32_t);
@ -58,6 +62,7 @@ int main(int argc, char **argv) {
bool running = true;
while (running) {
step_vm(&vm);
SDL_PumpEvents();
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {

View File

@ -34,6 +34,23 @@ bool test_add_compile(VM *vm) {
}
bool test_loop_compile(VM *vm) {
vm->memory[vm->mp++].u = 17;
vm->memory[vm->mp].c[0] = 'E';
vm->memory[vm->mp].c[1] = 'n';
vm->memory[vm->mp].c[2] = 't';
vm->memory[vm->mp++].c[3] = 'e';
vm->memory[vm->mp].c[0] = 'r';
vm->memory[vm->mp].c[1] = ' ';
vm->memory[vm->mp].c[2] = 'a';
vm->memory[vm->mp++].c[3] = ' ';
vm->memory[vm->mp].c[0] = 's';
vm->memory[vm->mp].c[1] = 't';
vm->memory[vm->mp].c[2] = 'r';
vm->memory[vm->mp++].c[3] = 'i';
vm->memory[vm->mp].c[0] = 'n';
vm->memory[vm->mp].c[1] = 'g';
vm->memory[vm->mp].c[2] = ':';
vm->memory[vm->mp++].c[3] = '\0';
vm->code[vm->cp++].u = OP(OP_LOADF, 0, 0, 0); /* let a = 5.0 */
vm->code[vm->cp++].f = 5.0f;
vm->code[vm->cp++].u = OP(OP_LOADI, 1, 0, 0); /* do (i = 50000, 0, -1) { */
@ -51,6 +68,9 @@ bool test_loop_compile(VM *vm) {
vm->code[vm->cp++].u = OP(OP_ADD_INT, 1, 1, 3); /* (implied by loop) i = i + (-1) */
vm->code[vm->cp++].u = OP(OP_JGE_INT, 4, 1, 2); /* } */
vm->code[vm->cp++].u = OP(OP_REAL_TO_UINT, 1, 0, 0); /* let b = a as nat; */
vm->code[vm->cp++].u = OP(OP_LOADU, 5, 0, 0);
vm->code[vm->cp++].u = 0;
vm->code[vm->cp++].u = OP(OP_PRINT_STRING, 0, 5, 0); /* print("Enter a string: "); */
vm->code[vm->cp++].u = OP(OP_READ_STRING, 2, 0, 0); /* let user_string = gets(); */
vm->code[vm->cp++].u = OP(OP_UINT_TO_STRING, 3, 1, 0);
vm->code[vm->cp++].u = OP(OP_PRINT_STRING, 0, 3, 0); /* print(a.toS()); */

5
test/add.zl Normal file
View File

@ -0,0 +1,5 @@
fn add(a, b) {
return a + b;
}
let sum = add(1, 1);
print sum;

View File

@ -1,6 +1,6 @@
int add(int a, int b) {
fn add(int a, int b) {
return a + b;
}
int sum = add(1, 1);
let sum = add(1, 1);
print(sum.toS());

7
test/fib.zl Normal file
View File

@ -0,0 +1,7 @@
fn fib(n) {
if (n < 2) return n;
return fib(n - 2) + fib(n - 1);
}
let result = fib(35);
print result;

View File

@ -1,6 +1,10 @@
.memory
.ascii Enter a string:
.code
main:
loadf $0 5.0
loadi $1 5
loadi $1 50000
loadi $2 0
loadi $3 -1
loop:
@ -10,6 +14,8 @@ loop:
loadu $4 &loop
jgei $4 $1 $2
rtou $1 $0
loadu $5 0
puts $5
gets $2
utos $3 $1
puts $3

View File

@ -3,7 +3,7 @@ for i = 50000, 0, -1 do
a = a + 5.0
end
local b = math.floor(a)
io.write("Enter a string: ")
io.write("Enter a string:")
local user_string = io.read("*l")
print(tostring(a))
print(tostring(b))

View File

@ -3,7 +3,7 @@ for (my $i = 50000; $i >= 0; $i--) {
$a += 5.0;
}
my $b = int($a);
print "Enter a string: ";
print "Enter a string:";
my $user_string = <STDIN>;
chomp($user_string);
print($a . "\n");

View File

@ -2,7 +2,7 @@ a = 5.0
for i in range(50000, -1, -1):
a += 5.0
b = int(a)
user_string = input("Enter a string: ")
user_string = input("Enter a string:")
print(str(a))
print(str(b))
print(user_string)

10
test/loop.zl Normal file
View File

@ -0,0 +1,10 @@
let a = 5.0;
for (let i = 50000; i > 0; i = i - 1) {
a = a + 5.0;
}
let b = a;
print "Enter a string:";
let user_string = read();
print(a);
print(b);
print(user_string);

View File

@ -32,6 +32,10 @@ print_section "Perl ($FILENAME.pl)"
print_section "Python ($FILENAME.py)"
echo "test input" | time python3 "$FILENAME.py"
# ZLC Implementation
print_section "zlc ($FILENAME.zl)"
echo "test input" | time zlc "$FILENAME.zl"
# ZRE Implementation
print_section "zre ($FILENAME.zre)"
echo "test input" | time ../src/zre "$FILENAME.zre"