some more optimizations

This commit is contained in:
zongor 2026-01-14 00:52:55 -08:00
parent 431d09656b
commit 634e1ed4eb
3 changed files with 12 additions and 8 deletions

View File

@ -34,14 +34,19 @@ u32 syscall(u32 id, u32 size, u32 mem_ptr) {
return 0;
}
case SYSCALL_CONSOLE_READ: {
u8 *ptr = &mem[mp];
mcpy(ptr, &size, sizeof(u32));
ptr += 4;
for (u32 i = 0; i < size; i++) {
u8 ch = getchar();
if (ch == '\0')
break;
if (ch == '\n')
break;
mem[mem_ptr + i] = ch;
*(ptr++) = ch;
}
ptr[size] = '\0';
mp += 4 + size + 1;
}
}
@ -78,7 +83,7 @@ void test_fibonacci() {
code[cp++] = ENCODE_B(OP_LOAD_IMM, 0, 35);
code[cp++] = ENCODE_B(OP_PUSH, 0, 0);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 1, fib);
code[cp++] = ENCODE_A(OP_CALL, 1, (9 * 4), (2 * 4));
code[cp++] = ENCODE_A(OP_CALL, 1, 9, 2);
/* print */
code[cp++] = ENCODE_A(OP_INT_TO_STR, 3, 2, 0);
code[cp++] = ENCODE_A(OP_SYSCALL, SYSCALL_CONSOLE_WRITE, 1, 3);
@ -92,11 +97,11 @@ void test_fibonacci() {
code[cp++] = ENCODE_B(OP_LOAD_IMM, 3, 2);
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
code[cp++] = ENCODE_B(OP_PUSH, 4, 0);
code[cp++] = ENCODE_A(OP_CALL, 8, (9 * 4), (5 * 4));
code[cp++] = ENCODE_A(OP_CALL, 8, 9, 5);
code[cp++] = ENCODE_B(OP_LOAD_IMM, 3, 1);
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
code[cp++] = ENCODE_B(OP_PUSH, 4, 0);
code[cp++] = ENCODE_A(OP_CALL, 8, (9 * 4), (6 * 4));
code[cp++] = ENCODE_A(OP_CALL, 8, 9, 6);
code[cp++] = ENCODE_A(OP_ADD_INT, 7, 6, 5);
code[cp++] = ENCODE_B(OP_RETURN, 7, 0);
code[cp++] = ENCODE_B(OP_RETURN, 0, 0);

View File

@ -1,14 +1,13 @@
#include "libc.h"
void mcpy(void *to, void *from, u32 length) {
u32 i = 0;
u8 *src, *dest;
if (to == nil || from == nil) return;
src = (u8 *)from;
dest = (u8 *)to;
while (length > (i++)) {
while (length-- > 0) {
*(dest++) = *(src++);
}
return;

View File

@ -52,13 +52,13 @@ bool step_vm() {
/* push return address to child frame */
(*header++) = pc;
/* push local address to return the value to */
(*header++) = fp + src2;
(*header++) = fp + (src2 * 4);
/* increase the mp to new size */
mp += FRAME_HEADER_SIZE;
/* now set the frame pointer, where the locals start */
fp = mp;
/* move mp forward by count many locals */
mp += src1;
mp += (src1 * 4);
/* jump to dest_ptr */
pc = fn_ptr;
return true;