some more optimizations
This commit is contained in:
parent
431d09656b
commit
634e1ed4eb
|
|
@ -34,14 +34,19 @@ u32 syscall(u32 id, u32 size, u32 mem_ptr) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
case SYSCALL_CONSOLE_READ: {
|
case SYSCALL_CONSOLE_READ: {
|
||||||
|
u8 *ptr = &mem[mp];
|
||||||
|
mcpy(ptr, &size, sizeof(u32));
|
||||||
|
ptr += 4;
|
||||||
for (u32 i = 0; i < size; i++) {
|
for (u32 i = 0; i < size; i++) {
|
||||||
u8 ch = getchar();
|
u8 ch = getchar();
|
||||||
if (ch == '\0')
|
if (ch == '\0')
|
||||||
break;
|
break;
|
||||||
if (ch == '\n')
|
if (ch == '\n')
|
||||||
break;
|
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_LOAD_IMM, 0, 35);
|
||||||
code[cp++] = ENCODE_B(OP_PUSH, 0, 0);
|
code[cp++] = ENCODE_B(OP_PUSH, 0, 0);
|
||||||
code[cp++] = ENCODE_B(OP_LOAD_IMM, 1, fib);
|
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 */
|
/* print */
|
||||||
code[cp++] = ENCODE_A(OP_INT_TO_STR, 3, 2, 0);
|
code[cp++] = ENCODE_A(OP_INT_TO_STR, 3, 2, 0);
|
||||||
code[cp++] = ENCODE_A(OP_SYSCALL, SYSCALL_CONSOLE_WRITE, 1, 3);
|
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_B(OP_LOAD_IMM, 3, 2);
|
||||||
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
|
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
|
||||||
code[cp++] = ENCODE_B(OP_PUSH, 4, 0);
|
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_B(OP_LOAD_IMM, 3, 1);
|
||||||
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
|
code[cp++] = ENCODE_A(OP_SUB_INT, 4, 0, 3);
|
||||||
code[cp++] = ENCODE_B(OP_PUSH, 4, 0);
|
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_A(OP_ADD_INT, 7, 6, 5);
|
||||||
code[cp++] = ENCODE_B(OP_RETURN, 7, 0);
|
code[cp++] = ENCODE_B(OP_RETURN, 7, 0);
|
||||||
code[cp++] = ENCODE_B(OP_RETURN, 0, 0);
|
code[cp++] = ENCODE_B(OP_RETURN, 0, 0);
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
#include "libc.h"
|
#include "libc.h"
|
||||||
|
|
||||||
void mcpy(void *to, void *from, u32 length) {
|
void mcpy(void *to, void *from, u32 length) {
|
||||||
u32 i = 0;
|
|
||||||
u8 *src, *dest;
|
u8 *src, *dest;
|
||||||
if (to == nil || from == nil) return;
|
if (to == nil || from == nil) return;
|
||||||
|
|
||||||
src = (u8 *)from;
|
src = (u8 *)from;
|
||||||
dest = (u8 *)to;
|
dest = (u8 *)to;
|
||||||
|
|
||||||
while (length > (i++)) {
|
while (length-- > 0) {
|
||||||
*(dest++) = *(src++);
|
*(dest++) = *(src++);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
4
vm/vm.c
4
vm/vm.c
|
|
@ -52,13 +52,13 @@ bool step_vm() {
|
||||||
/* push return address to child frame */
|
/* push return address to child frame */
|
||||||
(*header++) = pc;
|
(*header++) = pc;
|
||||||
/* push local address to return the value to */
|
/* push local address to return the value to */
|
||||||
(*header++) = fp + src2;
|
(*header++) = fp + (src2 * 4);
|
||||||
/* increase the mp to new size */
|
/* increase the mp to new size */
|
||||||
mp += FRAME_HEADER_SIZE;
|
mp += FRAME_HEADER_SIZE;
|
||||||
/* now set the frame pointer, where the locals start */
|
/* now set the frame pointer, where the locals start */
|
||||||
fp = mp;
|
fp = mp;
|
||||||
/* move mp forward by count many locals */
|
/* move mp forward by count many locals */
|
||||||
mp += src1;
|
mp += (src1 * 4);
|
||||||
/* jump to dest_ptr */
|
/* jump to dest_ptr */
|
||||||
pc = fn_ptr;
|
pc = fn_ptr;
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue