Compare commits
12 Commits
1.0.0
...
cf5ed7b601
| Author | SHA1 | Date | |
|---|---|---|---|
| cf5ed7b601 | |||
| ba72dae68f | |||
| dd8c65ef79 | |||
| 0fa4e24421 | |||
| ee162a70ed | |||
| b9fa669fd1 | |||
| ad5c2b856a | |||
| 4de066b6c1 | |||
| 0291279f1a | |||
| c2bd5922ff | |||
| 3e3f01db4e | |||
| a69d68f69c |
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -7,7 +7,7 @@
|
|||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/bin/tcpu",
|
"program": "${workspaceFolder}/bin/tcpu",
|
||||||
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
"windows": { "program": "${workspaceFolder}/bin/tcpu.exe" },
|
||||||
"args": [ "-c", "../examples/s.tasm", "o.bin", "--debug" ],
|
"args": [ "-c", "../examples/loop.tasm", "o.bin", "--debug", "-i", "o.bin" ],
|
||||||
"cwd": "${workspaceFolder}/bin",
|
"cwd": "${workspaceFolder}/bin",
|
||||||
"preLaunchTask": "build_exec_dbg",
|
"preLaunchTask": "build_exec_dbg",
|
||||||
"stopAtEntry": false,
|
"stopAtEntry": false,
|
||||||
|
|||||||
89
README.md
89
README.md
@@ -3,36 +3,75 @@ Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYT
|
|||||||
|
|
||||||
## Building
|
## Building
|
||||||
1. Install [cbuild](https://timerix.ddns.net:3322/Timerix/cbuild.git)
|
1. Install [cbuild](https://timerix.ddns.net:3322/Timerix/cbuild.git)
|
||||||
2. ```sh
|
2. Install [SDL3](https://github.com/libsdl-org/SDL) and [SDL3_image](https://github.com/libsdl-org/SDL_image) from package manager or source.
|
||||||
|
3. ```sh
|
||||||
cbuild build_exec_dbg
|
cbuild build_exec_dbg
|
||||||
```
|
```
|
||||||
|
|
||||||
## Assembly language
|
## Assembly language
|
||||||
### Instructions
|
|
||||||
| code | name | arguments | details |
|
|
||||||
|------|------|-----------|---------|
|
|
||||||
| 00 | NOP | | ignored instruction |
|
|
||||||
| 01 | PUSH | `dst_register`, `value_size(bytes)`, `value` | push constant value into `dst_register` |
|
|
||||||
| 02 | MOV | `dst_register`, `src_register` | copy value from `src_register` to `dst_register`
|
|
||||||
| 03 | ADD | `dst_register`, `src_register` | `dst` += `src` |
|
|
||||||
| 04 | SUB | `dst_register`, `src_register` | `dst` -= `src` |
|
|
||||||
| 05 | MUL | `dst_register`, `src_register` | `dst` *= `src` |
|
|
||||||
| 06 | DIV | `dst_register`, `src_register` | `dst` /= `src` |
|
|
||||||
| 07 | MOD | `dst_register`, `src_register` | `dst` %= `src` |
|
|
||||||
| 08 | SYS | | call system function |
|
|
||||||
| 09 | EXIT | | stop the program with exit code in `ax` |
|
|
||||||
|
|
||||||
### Registers
|
### Registers
|
||||||
| code | name | size (bits) |
|
| name | code | size (bits) |
|
||||||
|------|------|-------------|
|
|-----|------|----|
|
||||||
| 00 | ax | 32 |
|
| rax | 0x01 | 64 |
|
||||||
| 01 | bx | 32 |
|
| eax | 0x02 | 32 |
|
||||||
| 02 | cx | 32 |
|
| ax | 0x04 | 16 |
|
||||||
| 03 | dx | 32 |
|
| al | 0x07 | 8 |
|
||||||
|
| ah | 0x08 | 8 |
|
||||||
|
| |
|
||||||
|
| rbx | 0x11 | 64 |
|
||||||
|
| ebx | 0x12 | 32 |
|
||||||
|
| bx | 0x14 | 16 |
|
||||||
|
| bl | 0x17 | 8 |
|
||||||
|
| bh | 0x18 | 8 |
|
||||||
|
| |
|
||||||
|
| rcx | 0x21 | 64 |
|
||||||
|
| ecx | 0x22 | 32 |
|
||||||
|
| cx | 0x24 | 16 |
|
||||||
|
| cl | 0x27 | 8 |
|
||||||
|
| ch | 0x28 | 8 |
|
||||||
|
| |
|
||||||
|
| rdx | 0x31 | 64 |
|
||||||
|
| edx | 0x32 | 32 |
|
||||||
|
| dx | 0x34 | 16 |
|
||||||
|
| dl | 0x37 | 8 |
|
||||||
|
| dh | 0x38 | 8 |
|
||||||
|
|
||||||
|
### Instructions
|
||||||
|
| name | arguments | details |
|
||||||
|
|------|-----------|---------|
|
||||||
|
| NOP | | ignored instruction |
|
||||||
|
| EXIT | | stop the program with exit code in `eax` |
|
||||||
|
| SYS | | call system function |
|
||||||
|
| |
|
||||||
|
| MOVC | `dst_register`, `const_value` | push constant value into `dst_register` |
|
||||||
|
| MOVR | `dst_register`, `src_register` | copy value from `src_register` to `dst_register` |
|
||||||
|
| |
|
||||||
|
| ADD | `dst_register`, `src_register` | `dst += src` |
|
||||||
|
| SUB | `dst_register`, `src_register` | `dst -= src` |
|
||||||
|
| MUL | `dst_register`, `src_register` | `dst *= src` |
|
||||||
|
| DIV | `dst_register`, `src_register` | `dst /= src` |
|
||||||
|
| MOD | `dst_register`, `src_register` | `dst %= src` |
|
||||||
|
| |
|
||||||
|
| EQ | `dst_register`, `src_register` | `cmp_flag = dst == src` |
|
||||||
|
| NE | `dst_register`, `src_register` | `cmp_flag = dst != src` |
|
||||||
|
| LT | `dst_register`, `src_register` | `cmp_flag = dst < src` |
|
||||||
|
| LE | `dst_register`, `src_register` | `cmp_flag = dst <= src` |
|
||||||
|
| GT | `dst_register`, `src_register` | `cmp_flag = dst > src` |
|
||||||
|
| GE | `dst_register`, `src_register` | `cmp_flag = dst >= src` |
|
||||||
|
| |
|
||||||
|
| NOT | `dst_register` | `dst = !dst` |
|
||||||
|
| INV | `dst_register` | `dst = ~dst` |
|
||||||
|
| OR | `dst_register`, `src_register` | `dst = dst \| src` |
|
||||||
|
| XOR | `dst_register`, `src_register` | `dst = dst ^ src` |
|
||||||
|
| AND | `dst_register`, `src_register` | `dst = dst & src` |
|
||||||
|
| |
|
||||||
|
| JMP | `dst_address_const` | goto `dst` |
|
||||||
|
| JNZ | `dst_address_const` | if (`cmp_flag` != 0) goto `dst` |
|
||||||
|
| JZ | `dst_address_const` | if (`cmp_flag` == 0) goto `dst` |
|
||||||
|
|
||||||
### System functions
|
### System functions
|
||||||
To call a system function you need to push values to registers and write `SYS` opcode. The return value of a function will will be avaliable in `ax` after call.
|
To call a system function you need to push values to registers and write `SYS` opcode. The return value of a function will will be avaliable in `ax` after call.
|
||||||
| `ax` | name | `bx` | `cx` | `dx` | details |
|
| name | `al` | `ah` | `rbx` | `ecx` | details |
|
||||||
|-----------|------|----|----|----|---------|
|
|------|------|------|-------|-------|---------|
|
||||||
| 0 | read | file number | buffer pointer | buffer size | read data from file |
|
| read | 0 | file number | buffer pointer | buffer size | read data from file |
|
||||||
| 1 | write | file number | buffer pointer | buffer size | write data to file |
|
| write | 1 | file number | buffer pointer | buffer size | write data to file |
|
||||||
|
|||||||
11
TODO.md
Normal file
11
TODO.md
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# TODO List
|
||||||
|
- add negative number arguments support
|
||||||
|
- add movc char support
|
||||||
|
- add padding to compilation
|
||||||
|
- VM debug log
|
||||||
|
- add display syscalls
|
||||||
|
- change section binary format:
|
||||||
|
1. code
|
||||||
|
2. exit instruction with code ERR_U_FORGOT_TO_CALL_EXIT
|
||||||
|
3. data
|
||||||
|
- arguments validation for each instruction
|
||||||
32
examples/conditional_jump.tasm
Normal file
32
examples/conditional_jump.tasm
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/*
|
||||||
|
Example of behavior change depending on some condition
|
||||||
|
*/
|
||||||
|
|
||||||
|
.main:
|
||||||
|
movc ax 1
|
||||||
|
movc bx 2
|
||||||
|
gt ax bx
|
||||||
|
jnz @true
|
||||||
|
jz @false
|
||||||
|
|
||||||
|
.true:
|
||||||
|
const8 true.msg "true\n"
|
||||||
|
movc rbx @true.msg
|
||||||
|
movc ecx #true.msg
|
||||||
|
jmp @print
|
||||||
|
|
||||||
|
.false
|
||||||
|
const8 false.msg "false\n"
|
||||||
|
movc rbx @false.msg
|
||||||
|
movc ecx #false.msg
|
||||||
|
jmp @print
|
||||||
|
|
||||||
|
.print:
|
||||||
|
movc al 1
|
||||||
|
movc ah 1
|
||||||
|
sys
|
||||||
|
jmp @end
|
||||||
|
|
||||||
|
.end:
|
||||||
|
movc ax 0
|
||||||
|
exit
|
||||||
22
examples/loop.tasm
Normal file
22
examples/loop.tasm
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
/*
|
||||||
|
Example of self-repeating code section
|
||||||
|
*/
|
||||||
|
|
||||||
|
.main:
|
||||||
|
movc dx 0; // loop counter
|
||||||
|
|
||||||
|
.loop
|
||||||
|
const8 datum "ITERATION!!! "
|
||||||
|
movc al 1
|
||||||
|
movc ah 1
|
||||||
|
movc rbx @datum
|
||||||
|
movc ecx #datum
|
||||||
|
sys
|
||||||
|
|
||||||
|
movc cx 1
|
||||||
|
add dx cx
|
||||||
|
movc cx 8
|
||||||
|
lt dx cx
|
||||||
|
jnz @loop
|
||||||
|
movc rax 0
|
||||||
|
exit
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
/*
|
|
||||||
"hello world" program in my assembly language
|
|
||||||
*/
|
|
||||||
|
|
||||||
.data:
|
|
||||||
// named array of 8-bit values
|
|
||||||
const8 msg "Hello, World :3\0"
|
|
||||||
|
|
||||||
.main:
|
|
||||||
push ax 1; // sys_write
|
|
||||||
push bx 1; // stdout
|
|
||||||
push cx @msg; // address of msg data
|
|
||||||
push dx #msg; // size of msg data
|
|
||||||
sys
|
|
||||||
push ax 0
|
|
||||||
exit
|
|
||||||
16
examples/stdout.tasm
Normal file
16
examples/stdout.tasm
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/*
|
||||||
|
"hello world" program in my assembly language
|
||||||
|
*/
|
||||||
|
|
||||||
|
.data:
|
||||||
|
// named array of 8-bit values
|
||||||
|
const8 msg "Hello, World!\n"
|
||||||
|
|
||||||
|
.main:
|
||||||
|
movc al 1; // sys_write
|
||||||
|
movc ah 1; // stdout
|
||||||
|
movc rbx @msg; // address of msg data
|
||||||
|
movc ecx #msg; // size of msg data
|
||||||
|
sys
|
||||||
|
movc ax 0
|
||||||
|
exit
|
||||||
7
examples/window.tasm
Normal file
7
examples/window.tasm
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
Example of graphical application
|
||||||
|
*/
|
||||||
|
|
||||||
|
.main:
|
||||||
|
//TODO: write code here
|
||||||
|
exit
|
||||||
@@ -32,12 +32,13 @@ case "$OS" in
|
|||||||
WINDOWS)
|
WINDOWS)
|
||||||
EXEC_FILE="$PROJECT.exe"
|
EXEC_FILE="$PROJECT.exe"
|
||||||
SHARED_LIB_FILE="$PROJECT.dll"
|
SHARED_LIB_FILE="$PROJECT.dll"
|
||||||
# example: "-I./"
|
LINKER_LIBS="-lSDL3_image -lSDL3"
|
||||||
INCLUDE=""
|
INCLUDE=""
|
||||||
;;
|
;;
|
||||||
LINUX)
|
LINUX)
|
||||||
EXEC_FILE="$PROJECT"
|
EXEC_FILE="$PROJECT"
|
||||||
SHARED_LIB_FILE="$PROJECT.so"
|
SHARED_LIB_FILE="$PROJECT.so"
|
||||||
|
LINKER_LIBS="-lSDL3_image -lSDL3"
|
||||||
INCLUDE=""
|
INCLUDE=""
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
@@ -57,7 +58,7 @@ case "$TASK" in
|
|||||||
# -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
|
# -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code
|
||||||
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
|
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
|
||||||
PRE_TASK_SCRIPT=
|
PRE_TASK_SCRIPT=
|
||||||
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -66,7 +67,7 @@ case "$TASK" in
|
|||||||
build_exec_dbg)
|
build_exec_dbg)
|
||||||
C_ARGS="-O0 -g3"
|
C_ARGS="-O0 -g3"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
|
||||||
PRE_TASK_SCRIPT=
|
PRE_TASK_SCRIPT=
|
||||||
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -75,7 +76,7 @@ case "$TASK" in
|
|||||||
build_shared_lib)
|
build_shared_lib)
|
||||||
C_ARGS="-O2 -fpic -flto -shared"
|
C_ARGS="-O2 -fpic -flto -shared"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS -Wl,-soname,$SHARED_LIB_FILE"
|
||||||
PRE_TASK_SCRIPT=
|
PRE_TASK_SCRIPT=
|
||||||
TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh
|
TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -84,7 +85,7 @@ case "$TASK" in
|
|||||||
build_shared_lib_dbg)
|
build_shared_lib_dbg)
|
||||||
C_ARGS="-O0 -g3 -fpic -shared"
|
C_ARGS="-O0 -g3 -fpic -shared"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS -Wl,-soname,$SHARED_LIB_FILE"
|
||||||
PRE_TASK_SCRIPT=
|
PRE_TASK_SCRIPT=
|
||||||
TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh
|
TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -125,7 +126,7 @@ case "$TASK" in
|
|||||||
# -fprofile-prefix-path sets path where profiling info about objects will be saved
|
# -fprofile-prefix-path sets path where profiling info about objects will be saved
|
||||||
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects"
|
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
|
||||||
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
||||||
TASK_SCRIPT=cbuild/default_tasks/profile.sh
|
TASK_SCRIPT=cbuild/default_tasks/profile.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -138,7 +139,7 @@ case "$TASK" in
|
|||||||
# -pg adds code to executable, that generates file containing function call info (gmon.out)
|
# -pg adds code to executable, that generates file containing function call info (gmon.out)
|
||||||
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg"
|
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
|
||||||
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
||||||
TASK_SCRIPT=cbuild/default_tasks/gprof.sh
|
TASK_SCRIPT=cbuild/default_tasks/gprof.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -152,7 +153,7 @@ case "$TASK" in
|
|||||||
# -pg adds code to executable, that generates file containing function call info (gmon.out)
|
# -pg adds code to executable, that generates file containing function call info (gmon.out)
|
||||||
C_ARGS="-O2 -flto=auto -fuse-linker-plugin"
|
C_ARGS="-O2 -flto=auto -fuse-linker-plugin"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
|
||||||
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
||||||
TASK_SCRIPT=cbuild/default_tasks/callgrind.sh
|
TASK_SCRIPT=cbuild/default_tasks/callgrind.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
@@ -162,7 +163,7 @@ case "$TASK" in
|
|||||||
OUTDIR="$OUTDIR/sanitize"
|
OUTDIR="$OUTDIR/sanitize"
|
||||||
C_ARGS="-O0 -g3 -fsanitize=undefined,address"
|
C_ARGS="-O0 -g3 -fsanitize=undefined,address"
|
||||||
CPP_ARGS="$C_ARGS"
|
CPP_ARGS="$C_ARGS"
|
||||||
LINKER_ARGS="$CPP_ARGS"
|
LINKER_ARGS="$CPP_ARGS $LINKER_LIBS"
|
||||||
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh
|
||||||
TASK_SCRIPT=cbuild/default_tasks/exec.sh
|
TASK_SCRIPT=cbuild/default_tasks/exec.sh
|
||||||
POST_TASK_SCRIPT=
|
POST_TASK_SCRIPT=
|
||||||
|
|||||||
90
src/VM/Display/Display.c
Normal file
90
src/VM/Display/Display.c
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <SDL3_image/SDL_image.h>
|
||||||
|
#include "Display.h"
|
||||||
|
|
||||||
|
typedef struct Display {
|
||||||
|
str name;
|
||||||
|
i32 width;
|
||||||
|
i32 height;
|
||||||
|
SDL_Window* window;
|
||||||
|
SDL_Renderer* renderer;
|
||||||
|
} Display;
|
||||||
|
|
||||||
|
static SDL_InitState sdl_init_state = {0};
|
||||||
|
|
||||||
|
|
||||||
|
Display* Display_create(str name, i32 w, i32 h, DisplayFlags flags){
|
||||||
|
Display* d = malloc(sizeof(Display));
|
||||||
|
|
||||||
|
d->name = str_copy(name);
|
||||||
|
d->width = w;
|
||||||
|
d->height = h;
|
||||||
|
d->window = NULL;
|
||||||
|
d->renderer = NULL;
|
||||||
|
|
||||||
|
if (SDL_ShouldInit(&sdl_init_state)) {
|
||||||
|
bool sdl_initialized = SDL_Init(SDL_INIT_VIDEO);
|
||||||
|
SDL_SetInitialized(&sdl_init_state, sdl_initialized);
|
||||||
|
if(!sdl_initialized)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_WindowFlags window_flags = SDL_WINDOW_ALWAYS_ON_TOP;
|
||||||
|
if(!SDL_CreateWindowAndRenderer(d->name.data, d->width, d->height, window_flags, &d->window, &d->renderer)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Display_destroy(Display* d){
|
||||||
|
free(d->name.data);
|
||||||
|
SDL_DestroyRenderer(d->renderer);
|
||||||
|
SDL_DestroyWindow(d->window);
|
||||||
|
free(d);
|
||||||
|
|
||||||
|
// if (SDL_ShouldQuit(&sdl_init_state)) {
|
||||||
|
// SDL_Quit();
|
||||||
|
// SDL_SetInitialized(&sdl_init_state, false);
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Display_setName(Display* d, str name){
|
||||||
|
d->name = str_copy(name);
|
||||||
|
return SDL_SetWindowTitle(d->window, name.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Display_setSize(Display* d, u32 w, u32 h){
|
||||||
|
d->width = w;
|
||||||
|
d->height = h;
|
||||||
|
return SDL_SetWindowSize(d->window, w, h);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Display_setDrawingColor(Display* d, ColorRGBA color){
|
||||||
|
return SDL_SetRenderDrawColor(d->renderer, color.r, color.g, color.b, color.a);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Display_clear(Display* d){
|
||||||
|
return SDL_RenderClear(d->renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define Rect_copy(DST, SRC) {\
|
||||||
|
DST.x = SRC.x;\
|
||||||
|
DST.y = SRC.y;\
|
||||||
|
DST.w = SRC.w;\
|
||||||
|
DST.h = SRC.h;\
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Display_fillRect(Display* d, Rect rect) {
|
||||||
|
SDL_FRect sdl_rect;
|
||||||
|
Rect_copy(sdl_rect, rect);
|
||||||
|
return SDL_RenderFillRect(d->renderer, &sdl_rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Display_swapBuffers(Display* d){
|
||||||
|
return SDL_RenderPresent(d->renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
NULLABLE(cstr) Display_getError(){
|
||||||
|
return SDL_GetError();
|
||||||
|
}
|
||||||
32
src/VM/Display/Display.h
Normal file
32
src/VM/Display/Display.h
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "../../std.h"
|
||||||
|
#include "../../string/str.h"
|
||||||
|
|
||||||
|
typedef struct Rect {
|
||||||
|
i32 x, y;
|
||||||
|
i32 w, h;
|
||||||
|
} Rect;
|
||||||
|
#define Rect_create(X, Y, W, H) ((Rect){ .x = X, .y = Y, .w = W, .h = H})
|
||||||
|
|
||||||
|
typedef struct ColorRGBA {
|
||||||
|
u8 r, g, b, a;
|
||||||
|
} ColorRGBA;
|
||||||
|
#define ColorRGBA_create(R, G, B, A) ((ColorRGBA){ .r = R, .g = G, .b = B, .a = A })
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum DisplayFlags {
|
||||||
|
DisplayFlags_Default = 0
|
||||||
|
} DisplayFlags;
|
||||||
|
|
||||||
|
typedef struct Display Display;
|
||||||
|
|
||||||
|
Display* Display_create(str name, i32 w, i32 h, DisplayFlags flags);
|
||||||
|
void Display_destroy(Display* d);
|
||||||
|
|
||||||
|
bool Display_setName(Display* d, str name);
|
||||||
|
bool Display_setSize(Display* d, u32 w, u32 h);
|
||||||
|
bool Display_setDrawingColor(Display* d, ColorRGBA color);
|
||||||
|
bool Display_clear(Display* d);
|
||||||
|
bool Display_fillRect(Display* d, Rect rect);
|
||||||
|
bool Display_swapBuffers(Display* d);
|
||||||
|
NULLABLE(cstr) Display_getError();
|
||||||
27
src/VM/VM.c
27
src/VM/VM.c
@@ -50,7 +50,7 @@ i32 VM_boot(VM* vm){
|
|||||||
u8 opcode = vm->data[vm->current_pos];
|
u8 opcode = vm->data[vm->current_pos];
|
||||||
|
|
||||||
const Instruction* instr = Instruction_getByOpcode(opcode);
|
const Instruction* instr = Instruction_getByOpcode(opcode);
|
||||||
// printfe("[at 0x%x] %02X %s\n", (u32)vm->current_pos, opcode, instr->name);
|
// printfe("[at 0x%x] %02X %s\n", (u32)vm->current_pos, opcode, instr->name.data);
|
||||||
if(instr == NULL){
|
if(instr == NULL){
|
||||||
VM_setError(vm, "unknown opcode %02X", opcode);
|
VM_setError(vm, "unknown opcode %02X", opcode);
|
||||||
return -1;
|
return -1;
|
||||||
@@ -60,7 +60,7 @@ i32 VM_boot(VM* vm){
|
|||||||
i32 bytes_read = instr->implementation(vm);
|
i32 bytes_read = instr->implementation(vm);
|
||||||
// internal error occured
|
// internal error occured
|
||||||
if(bytes_read < 0)
|
if(bytes_read < 0)
|
||||||
return -1;
|
return bytes_read;
|
||||||
|
|
||||||
if(vm->state == VMState_Exited)
|
if(vm->state == VMState_Exited)
|
||||||
break;
|
break;
|
||||||
@@ -72,7 +72,7 @@ i32 VM_boot(VM* vm){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// exit code of the program should be in ax register
|
// exit code of the program should be in ax register
|
||||||
return vm->ax.i32v;
|
return vm->registers.a.ex;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
||||||
@@ -84,6 +84,25 @@ bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(dst, vm->data + pos, size);
|
void* addr = vm->data + pos;
|
||||||
|
memcpy(dst, addr, size);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VM_registerRead(VM* vm, void* dst, RegisterCode code) {
|
||||||
|
u8 index = code / 0x10;
|
||||||
|
u8 part = code & 0xf;
|
||||||
|
u8 offset = part / 8;
|
||||||
|
u8 size = 8 / part;
|
||||||
|
void* addr = (u8*)(&vm->registers.array[index]) + offset;
|
||||||
|
memcpy(dst, addr, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VM_registerWrite(VM* vm, void* src, RegisterCode code){
|
||||||
|
u8 index = code / 0x10;
|
||||||
|
u8 part = code & 0xf;
|
||||||
|
u8 offset = part / 8;
|
||||||
|
u8 size = 8 / part;
|
||||||
|
void* addr = (u8*)(&vm->registers.array[index]) + offset;
|
||||||
|
memcpy(addr, src, size);
|
||||||
|
}
|
||||||
|
|||||||
45
src/VM/VM.h
45
src/VM/VM.h
@@ -1,30 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "../std.h"
|
#include "../std.h"
|
||||||
#include "../string/str.h"
|
#include "../string/str.h"
|
||||||
|
#include "../instructions/registers.h"
|
||||||
|
|
||||||
typedef union Register {
|
typedef union Register {
|
||||||
u32 u32v;
|
u64 rx;
|
||||||
i32 i32v;
|
u32 ex;
|
||||||
f32 f32v;
|
u16 x;
|
||||||
struct {
|
struct {
|
||||||
u16 u16v0;
|
u8 l;
|
||||||
u16 u16v1;
|
u8 h;
|
||||||
};
|
|
||||||
struct {
|
|
||||||
i16 i16v0;
|
|
||||||
i16 i16v1;
|
|
||||||
};
|
|
||||||
struct {
|
|
||||||
u8 u8v0;
|
|
||||||
u8 u8v1;
|
|
||||||
u8 u8v2;
|
|
||||||
u8 u8v3;
|
|
||||||
};
|
|
||||||
struct {
|
|
||||||
i8 i8v0;
|
|
||||||
i8 i8v1;
|
|
||||||
i8 i8v2;
|
|
||||||
i8 i8v3;
|
|
||||||
};
|
};
|
||||||
} Register;
|
} Register;
|
||||||
|
|
||||||
@@ -38,13 +23,17 @@ typedef enum VMState {
|
|||||||
typedef struct VM {
|
typedef struct VM {
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
Register ax;
|
Register a;
|
||||||
Register bx;
|
Register b;
|
||||||
Register cx;
|
Register c;
|
||||||
Register dx;
|
Register d;
|
||||||
};
|
|
||||||
Register registers[4];
|
|
||||||
};
|
};
|
||||||
|
Register array[4];
|
||||||
|
} registers;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
bool cmp; // result of comparison operation
|
||||||
|
} flags;
|
||||||
|
|
||||||
VMState state;
|
VMState state;
|
||||||
char* NULLABLE(error_message); // not null on if state == VMState_InternalError
|
char* NULLABLE(error_message); // not null on if state == VMState_InternalError
|
||||||
@@ -66,6 +55,8 @@ bool VM_setMemory(VM* vm, u8* data, size_t size);
|
|||||||
i32 VM_boot(VM* vm);
|
i32 VM_boot(VM* vm);
|
||||||
|
|
||||||
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
|
bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size);
|
||||||
|
void VM_registerRead(VM* vm, void* dst, RegisterCode code);
|
||||||
|
void VM_registerWrite(VM* vm, void* src, RegisterCode code);
|
||||||
|
|
||||||
#define VM_setError(vm, format, ...) _VM_setError(vm, __func__, format ,##__VA_ARGS__)
|
#define VM_setError(vm, format, ...) _VM_setError(vm, __func__, format ,##__VA_ARGS__)
|
||||||
void _VM_setError(VM* vm, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
void _VM_setError(VM* vm, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
|
||||||
|
|||||||
18
src/VM/time.c
Normal file
18
src/VM/time.c
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
#include <SDL3/SDL_timer.h>
|
||||||
|
#include "time.h"
|
||||||
|
|
||||||
|
void time_sleepMS(u32 ms){
|
||||||
|
SDL_Delay(ms);
|
||||||
|
}
|
||||||
|
|
||||||
|
void time_sleepNS(u64 ns){
|
||||||
|
SDL_DelayNS(ns);
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 time_getMonotonicMS(){
|
||||||
|
return SDL_GetTicks();
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 time_getMonotonicNS(){
|
||||||
|
return SDL_GetTicksNS();
|
||||||
|
}
|
||||||
8
src/VM/time.h
Normal file
8
src/VM/time.h
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../std.h"
|
||||||
|
|
||||||
|
void time_sleepMS(u32 ms);
|
||||||
|
void time_sleepNS(u64 ns);
|
||||||
|
u64 time_getMonotonicMS();
|
||||||
|
u64 time_getMonotonicNS();
|
||||||
@@ -27,6 +27,8 @@ void BinaryObject_construct(BinaryObject* ptr){
|
|||||||
ptr->section_list = List_CompiledSection_alloc(64);
|
ptr->section_list = List_CompiledSection_alloc(64);
|
||||||
HashMap_CompiledSectionPtr_alloc(&ptr->section_map);
|
HashMap_CompiledSectionPtr_alloc(&ptr->section_map);
|
||||||
HashMap_ConstDataProps_alloc(&ptr->const_data_map);
|
HashMap_ConstDataProps_alloc(&ptr->const_data_map);
|
||||||
|
ptr->main_section = NULL;
|
||||||
|
ptr->total_size = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BinaryObject_free(BinaryObject* ptr){
|
void BinaryObject_free(BinaryObject* ptr){
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ HashMap_declare(CompiledSectionPtr);
|
|||||||
typedef struct BinaryObject {
|
typedef struct BinaryObject {
|
||||||
List_CompiledSection section_list;
|
List_CompiledSection section_list;
|
||||||
HashMap_CompiledSectionPtr section_map;
|
HashMap_CompiledSectionPtr section_map;
|
||||||
|
NULLABLE(CompiledSection*) main_section;
|
||||||
HashMap_ConstDataProps const_data_map;
|
HashMap_ConstDataProps const_data_map;
|
||||||
u32 total_size;
|
u32 total_size;
|
||||||
} BinaryObject;
|
} BinaryObject;
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ static bool compileSection(Compiler* cmp, Section* sec){
|
|||||||
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1);
|
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1);
|
||||||
CompiledSection_construct(cs, sec->name);
|
CompiledSection_construct(cs, sec->name);
|
||||||
if(!HashMap_CompiledSectionPtr_tryPush(&cmp->binary.section_map, cs->name, cs)){
|
if(!HashMap_CompiledSectionPtr_tryPush(&cmp->binary.section_map, cs->name, cs)){
|
||||||
returnError("duplicate section '%s'", str_copy(sec->name));
|
returnError("duplicate section '%s'", str_copy(sec->name).data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// compile code
|
// compile code
|
||||||
@@ -90,22 +90,21 @@ static bool compileSection(Compiler* cmp, Section* sec){
|
|||||||
List_u8_push(&cs->bytes, arg->value.register_code);
|
List_u8_push(&cs->bytes, arg->value.register_code);
|
||||||
break;
|
break;
|
||||||
case ArgumentType_ConstValue:
|
case ArgumentType_ConstValue:
|
||||||
//TODO: add const value size parsing
|
List_u8_pushMany(&cs->bytes, (void*)&arg->value.i, 8);
|
||||||
List_u8_pushMany(&cs->bytes, (void*)&arg->value.i, 4);
|
|
||||||
break;
|
break;
|
||||||
case ArgumentType_ConstDataPointer:
|
case ArgumentType_ConstDataPointer:
|
||||||
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
|
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
|
||||||
arg->value.data_name,
|
arg->value.data_name,
|
||||||
NamedRefType_Ptr,
|
NamedRefType_Ptr,
|
||||||
cs->bytes.len));
|
cs->bytes.len));
|
||||||
List_u8_pushMany(&cs->bytes, zeroes, 4);
|
List_u8_pushMany(&cs->bytes, zeroes, 8);
|
||||||
break;
|
break;
|
||||||
case ArgumentType_ConstDataSize:
|
case ArgumentType_ConstDataSize:
|
||||||
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
|
List_NamedRef_push(&cs->named_refs, NamedRef_construct(
|
||||||
arg->value.data_name,
|
arg->value.data_name,
|
||||||
NamedRefType_Size,
|
NamedRefType_Size,
|
||||||
cs->bytes.len));
|
cs->bytes.len));
|
||||||
List_u8_pushMany(&cs->bytes, zeroes, 4);
|
List_u8_pushMany(&cs->bytes, zeroes, 8);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -124,6 +123,9 @@ static bool compileSection(Compiler* cmp, Section* sec){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool compileBinary(Compiler* cmp){
|
static bool compileBinary(Compiler* cmp){
|
||||||
|
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
|
||||||
|
cmp->state = CompilerState_Compiling;
|
||||||
|
|
||||||
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
||||||
SectionPtr sec = &cmp->ast.sections.data[i];
|
SectionPtr sec = &cmp->ast.sections.data[i];
|
||||||
if(!compileSection(cmp, sec)){
|
if(!compileSection(cmp, sec)){
|
||||||
@@ -137,6 +139,7 @@ static bool compileBinary(Compiler* cmp){
|
|||||||
if(main_sec_ptrptr == NULL){
|
if(main_sec_ptrptr == NULL){
|
||||||
returnError("no 'main' section was defined");
|
returnError("no 'main' section was defined");
|
||||||
}
|
}
|
||||||
|
cmp->binary.main_section = *main_sec_ptrptr;
|
||||||
|
|
||||||
// create linked list of CompiledSection where main is the first
|
// create linked list of CompiledSection where main is the first
|
||||||
CompiledSection* prev_sec = *main_sec_ptrptr;
|
CompiledSection* prev_sec = *main_sec_ptrptr;
|
||||||
@@ -144,10 +147,10 @@ static bool compileBinary(Compiler* cmp){
|
|||||||
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
|
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
|
||||||
CompiledSection* sec = &cmp->binary.section_list.data[i];
|
CompiledSection* sec = &cmp->binary.section_list.data[i];
|
||||||
total_size += sec->bytes.len;
|
total_size += sec->bytes.len;
|
||||||
if(str_equals(sec->name, main_sec_name))
|
bool is_main_sec = str_equals(sec->name, main_sec_name);
|
||||||
continue;
|
if(!is_main_sec){
|
||||||
prev_sec->next = sec;
|
|
||||||
sec->offset = prev_sec->offset + prev_sec->bytes.len;
|
sec->offset = prev_sec->offset + prev_sec->bytes.len;
|
||||||
|
}
|
||||||
|
|
||||||
ConstDataProps cd = ConstDataProps_construct(sec->name, sec->bytes.len, sec->offset);
|
ConstDataProps cd = ConstDataProps_construct(sec->name, sec->bytes.len, sec->offset);
|
||||||
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
|
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
|
||||||
@@ -160,6 +163,11 @@ static bool compileBinary(Compiler* cmp){
|
|||||||
returnError("duplicate named data '%s'", str_copy(cd.name).data);
|
returnError("duplicate named data '%s'", str_copy(cd.name).data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(is_main_sec)
|
||||||
|
continue;
|
||||||
|
prev_sec->next = sec;
|
||||||
|
prev_sec = sec;
|
||||||
}
|
}
|
||||||
|
|
||||||
// insert calculated offsets into sections
|
// insert calculated offsets into sections
|
||||||
@@ -174,16 +182,15 @@ static bool compileBinary(Compiler* cmp){
|
|||||||
returnError("can't find named data '%s'", str_copy(ref->name).data);
|
returnError("can't find named data '%s'", str_copy(ref->name).data);
|
||||||
}
|
}
|
||||||
|
|
||||||
void* ref_value_ptr = sec->bytes.data + ref->offset;
|
u64* ref_value_ptr = (void*)(sec->bytes.data + ref->offset);
|
||||||
|
|
||||||
switch(ref->type){
|
switch(ref->type){
|
||||||
default:
|
default:
|
||||||
returnError("invalid NamedRefType %i", ref->type);
|
returnError("invalid NamedRefType %i", ref->type);
|
||||||
case NamedRefType_Size:
|
case NamedRefType_Size:
|
||||||
*((u32*)ref_value_ptr) = target_data->size;
|
*ref_value_ptr = target_data->size;
|
||||||
break;
|
break;
|
||||||
case NamedRefType_Ptr:
|
case NamedRefType_Ptr:
|
||||||
*((u32*)ref_value_ptr) = target_data->offset;
|
*ref_value_ptr = target_data->offset;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -194,26 +201,16 @@ static bool compileBinary(Compiler* cmp){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static bool writeBinaryFile(Compiler* cmp, FILE* f){
|
static bool writeBinaryFile(Compiler* cmp, FILE* f){
|
||||||
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
|
returnErrorIf_auto(cmp->state != CompilerState_Compiling);
|
||||||
cmp->state = CompilerState_Compiling;
|
|
||||||
|
|
||||||
if(!compileBinary(cmp)){
|
CompiledSection* sec = cmp->binary.main_section;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, STR("main"));
|
|
||||||
if(main_sec_ptrptr == NULL){
|
|
||||||
returnError("no 'main' section was defined");
|
|
||||||
}
|
|
||||||
CompiledSection* sec = *main_sec_ptrptr;
|
|
||||||
while(sec){
|
while(sec){
|
||||||
fwrite(sec->bytes.data, 1, sec->bytes.len, f);
|
fwrite(sec->bytes.data, 1, sec->bytes.len, f);
|
||||||
|
fflush(f);
|
||||||
sec = sec->next;
|
sec = sec->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: print warnings for unused sections
|
//TODO: print warnings for unused sections
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -289,6 +286,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
if(debug_log)
|
if(debug_log)
|
||||||
printf("===================================[parsing]===================================\n");
|
printf("===================================[parsing]===================================\n");
|
||||||
success = Compiler_parse(cmp);
|
success = Compiler_parse(cmp);
|
||||||
|
|
||||||
if (debug_log){
|
if (debug_log){
|
||||||
printf("-------------------------------------[AST]-------------------------------------\n");
|
printf("-------------------------------------[AST]-------------------------------------\n");
|
||||||
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
||||||
@@ -308,6 +306,11 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
for(u32 j = 0; j < sec->code.len; j++){
|
for(u32 j = 0; j < sec->code.len; j++){
|
||||||
Operation* op = &sec->code.data[j];
|
Operation* op = &sec->code.data[j];
|
||||||
const Instruction* instr = Instruction_getByOpcode(op->opcode);
|
const Instruction* instr = Instruction_getByOpcode(op->opcode);
|
||||||
|
if(instr == NULL){
|
||||||
|
fclose(f);
|
||||||
|
returnError("unknown opcode: %i", op->opcode)
|
||||||
|
}
|
||||||
|
|
||||||
printf(" %s", instr->name.data);
|
printf(" %s", instr->name.data);
|
||||||
for(u32 k = 0; k < op->args.len; k++){
|
for(u32 k = 0; k < op->args.len; k++){
|
||||||
Argument* arg = &op->args.data[k];
|
Argument* arg = &op->args.data[k];
|
||||||
@@ -318,8 +321,9 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
fclose(f);
|
fclose(f);
|
||||||
returnError("invalid argument type %i", arg->type);
|
returnError("invalid argument type %i", arg->type);
|
||||||
case ArgumentType_Register:
|
case ArgumentType_Register:
|
||||||
const char* register_names[] = {"null", "ax", "bx", "cx", "dx"};
|
str register_name = RegisterCode_toString(arg->value.register_code);
|
||||||
printf("%s", register_names[arg->value.register_code]);
|
printf("%s 0x%x", register_name.data, arg->value.register_code);
|
||||||
|
free(register_name.data);
|
||||||
break;
|
break;
|
||||||
case ArgumentType_ConstValue:
|
case ArgumentType_ConstValue:
|
||||||
printf(IFWIN("%lli", "%li"), arg->value.i);
|
printf(IFWIN("%lli", "%li"), arg->value.i);
|
||||||
@@ -348,6 +352,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!success){
|
if(!success){
|
||||||
fclose(f);
|
fclose(f);
|
||||||
return false;
|
return false;
|
||||||
@@ -355,6 +360,25 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
|||||||
|
|
||||||
if(debug_log)
|
if(debug_log)
|
||||||
printf("==================================[compiling]==================================\n");
|
printf("==================================[compiling]==================================\n");
|
||||||
|
success = compileBinary(cmp);
|
||||||
|
|
||||||
|
if(debug_log){
|
||||||
|
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
|
||||||
|
CompiledSection* sec = &cmp->binary.section_list.data[i];
|
||||||
|
str tmpstr = str_copy(sec->name);
|
||||||
|
printf("compiled section '%s' to %u bytes with offset 0x%x\n", tmpstr.data, sec->bytes.len, sec->offset);
|
||||||
|
free(tmpstr.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!success){
|
||||||
|
fclose(f);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(debug_log)
|
||||||
|
printf("----------------------------[writing output to file]---------------------------\n");
|
||||||
success = writeBinaryFile(cmp, f);
|
success = writeBinaryFile(cmp, f);
|
||||||
fclose(f);
|
fclose(f);
|
||||||
if(success){
|
if(success){
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ static void readArguments(Compiler* cmp){
|
|||||||
|
|
||||||
// string argument reading
|
// string argument reading
|
||||||
if(quot != '\0'){
|
if(quot != '\0'){
|
||||||
if(c == quot && cmp->code.data[cmp->pos - 1] != '\\'){
|
if(c == quot && (cmp->code.data[cmp->pos - 1] != '\\' || cmp->code.data[cmp->pos - 2] == '\\')){
|
||||||
quot = '\0';
|
quot = '\0';
|
||||||
}
|
}
|
||||||
else if(c == '\r' || c == '\n'){
|
else if(c == '\r' || c == '\n'){
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
|
|||||||
c = src.data[i];
|
c = src.data[i];
|
||||||
if(c == '\\'){
|
if(c == '\\'){
|
||||||
escaped = !escaped;
|
escaped = !escaped;
|
||||||
|
if(escaped)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,11 +70,17 @@ static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
|
|||||||
case 'e':
|
case 'e':
|
||||||
StringBuilder_append_char(&sb, '\e');
|
StringBuilder_append_char(&sb, '\e');
|
||||||
break;
|
break;
|
||||||
|
case '"':
|
||||||
|
case '\'':
|
||||||
|
StringBuilder_append_char(&sb, c);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
|
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
|
||||||
StringBuilder_free(&sb);
|
StringBuilder_free(&sb);
|
||||||
return str_null;
|
return str_null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
escaped = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return StringBuilder_getStr(&sb);
|
return StringBuilder_getStr(&sb);
|
||||||
@@ -259,10 +266,12 @@ bool Compiler_parse(Compiler* cmp){
|
|||||||
// data definition starts with const
|
// data definition starts with const
|
||||||
if(str_startsWith(instr_name, STR("const"))){
|
if(str_startsWith(instr_name, STR("const"))){
|
||||||
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
||||||
|
memset(dataDefPtr, 0, sizeof(DataDefinition));
|
||||||
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Operation* operPtr = List_Operation_expand(&sec->code, 1);
|
Operation* operPtr = List_Operation_expand(&sec->code, 1);
|
||||||
|
memset(operPtr, 0, sizeof(Operation));
|
||||||
parseOperation(cmp, instr_name, operPtr);
|
parseOperation(cmp, instr_name, operPtr);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
36
src/instructions/impl/JMP.c
Normal file
36
src/instructions/impl/JMP.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include "impl_macros.h"
|
||||||
|
|
||||||
|
// JUMP [destination address]
|
||||||
|
i32 JMP_impl(VM* vm){
|
||||||
|
u64 dst_addr = 0;
|
||||||
|
readVar(dst_addr);
|
||||||
|
|
||||||
|
vm->current_pos = dst_addr;
|
||||||
|
|
||||||
|
return sizeof(dst_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// JNZ [destination address]
|
||||||
|
i32 JNZ_impl(VM* vm){
|
||||||
|
u64 dst_addr = 0;
|
||||||
|
readVar(dst_addr);
|
||||||
|
|
||||||
|
if(vm->flags.cmp != 0){
|
||||||
|
vm->current_pos = dst_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sizeof(dst_addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// JZ [destination address]
|
||||||
|
i32 JZ_impl(VM* vm){
|
||||||
|
u64 dst_addr = 0;
|
||||||
|
readVar(dst_addr);
|
||||||
|
|
||||||
|
if(vm->flags.cmp == 0){
|
||||||
|
vm->current_pos = dst_addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sizeof(dst_addr);
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
#include "impl_macros.h"
|
|
||||||
|
|
||||||
/// MOV [dst_register] [src_register]
|
|
||||||
i32 MOV_impl(VM* vm){
|
|
||||||
u8 dst_register_i = 0;
|
|
||||||
readRegisterVar(dst_register_i);
|
|
||||||
u8 src_register_i = 0;
|
|
||||||
readRegisterVar(src_register_i);
|
|
||||||
if(dst_register_i == src_register_i){
|
|
||||||
VM_setError(vm, "dst_register_i == src_register_i (%x) ", src_register_i);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
vm->registers[dst_register_i].u32v = vm->registers[src_register_i].u32v;
|
|
||||||
return sizeof(dst_register_i) + sizeof(src_register_i);
|
|
||||||
}
|
|
||||||
12
src/instructions/impl/MOVC.c
Normal file
12
src/instructions/impl/MOVC.c
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#include "impl_macros.h"
|
||||||
|
|
||||||
|
/// MOVC [dst_register] [value_size] [value]
|
||||||
|
i32 MOVC_impl(VM* vm){
|
||||||
|
RegisterCode dst_reg_code = 0;
|
||||||
|
readRegisterCode(dst_reg_code);
|
||||||
|
u64 const_value = 0;
|
||||||
|
readVar(const_value);
|
||||||
|
|
||||||
|
VM_registerWrite(vm, &const_value, dst_reg_code);
|
||||||
|
return sizeof(dst_reg_code) + sizeof(const_value);
|
||||||
|
}
|
||||||
17
src/instructions/impl/MOVR.c
Normal file
17
src/instructions/impl/MOVR.c
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#include "impl_macros.h"
|
||||||
|
|
||||||
|
/// MOVR [dst_register] [src_register]
|
||||||
|
i32 MOVR_impl(VM* vm){
|
||||||
|
RegisterCode dst_reg_code = 0, src_reg_code = 0;
|
||||||
|
readRegisterCode(dst_reg_code);
|
||||||
|
readRegisterCode(src_reg_code);
|
||||||
|
if(dst_reg_code == src_reg_code){
|
||||||
|
VM_setError(vm, "dst_reg_code == src_reg_code (%x) ", src_reg_code);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 src_reg_value = 0;
|
||||||
|
VM_registerRead(vm, &src_reg_value, src_reg_code);
|
||||||
|
VM_registerWrite(vm, &src_reg_value, dst_reg_code);
|
||||||
|
return sizeof(dst_reg_code) + sizeof(src_reg_code);
|
||||||
|
}
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#include "impl_macros.h"
|
|
||||||
|
|
||||||
/// PUSH [dst_register] [value_size] [value]
|
|
||||||
i32 PUSH_impl(VM* vm){
|
|
||||||
u8 dst_register_i = 0;
|
|
||||||
readRegisterVar(dst_register_i);
|
|
||||||
/*u8 value_size = 0;
|
|
||||||
readValueSizeVar(value_size);*/
|
|
||||||
u8 value_size = 4;\
|
|
||||||
|
|
||||||
vm->registers[dst_register_i].u32v = 0;
|
|
||||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))
|
|
||||||
return -1;
|
|
||||||
vm->current_pos += value_size;
|
|
||||||
|
|
||||||
return sizeof(dst_register_i) + sizeof(value_size) + value_size;
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
FILE* NULLABLE(fileFromN)(VM* vm, u32 file_n){
|
FILE* NULLABLE(fileFromN)(VM* vm, u8 file_n){
|
||||||
FILE* f = NULL;
|
FILE* f = NULL;
|
||||||
switch(file_n){
|
switch(file_n){
|
||||||
case 0: f = stdin; break;
|
case 0: f = stdin; break;
|
||||||
@@ -15,13 +15,13 @@ FILE* NULLABLE(fileFromN)(VM* vm, u32 file_n){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sys_read
|
// sys_read
|
||||||
// bx - file n
|
// ah - file n
|
||||||
// cx - buffer ptr
|
// rbx - buffer ptr
|
||||||
// dx - buffer size
|
// ecx - buffer size
|
||||||
i32 SYS_read(VM* vm){
|
i32 SYS_read(VM* vm){
|
||||||
const u32 file_n = vm->bx.u32v;
|
const u8 file_n = vm->registers.a.h;
|
||||||
u8* const buf = vm->data + vm->cx.u32v;
|
u8* const buf = vm->data + vm->registers.b.rx;
|
||||||
const u32 size = vm->dx.u32v;
|
const u32 size = vm->registers.c.ex;
|
||||||
|
|
||||||
if(buf + size > vm->data + vm->data_size)
|
if(buf + size > vm->data + vm->data_size)
|
||||||
return 40;
|
return 40;
|
||||||
@@ -31,13 +31,13 @@ i32 SYS_read(VM* vm){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sys_write
|
// sys_write
|
||||||
// bx - file n
|
// ah - file n
|
||||||
// cx - buffer ptr
|
// rbx - buffer ptr
|
||||||
// dx - buffer size
|
// ecx - buffer size
|
||||||
i32 SYS_write(VM* vm){
|
i32 SYS_write(VM* vm){
|
||||||
const u32 file_n = vm->bx.u32v;
|
const u8 file_n = vm->registers.a.h;
|
||||||
u8* const buf = vm->data + vm->cx.u32v;
|
u8* const buf = vm->data + vm->registers.b.rx;
|
||||||
const u32 size = vm->dx.u32v;
|
const u32 size = vm->registers.c.ex;
|
||||||
|
|
||||||
if(buf + size > vm->data + vm->data_size)
|
if(buf + size > vm->data + vm->data_size)
|
||||||
return 41;
|
return 41;
|
||||||
@@ -47,16 +47,16 @@ i32 SYS_write(VM* vm){
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// SYS
|
/// SYS
|
||||||
/// before call: ax - func code
|
/// before call: al - func code
|
||||||
/// after call: ax - result code
|
/// after call: eax - result code
|
||||||
i32 SYS_impl(VM* vm){
|
i32 SYS_impl(VM* vm){
|
||||||
u8 func_code = vm->ax.u8v0;
|
u8 func_code = vm->registers.a.l;
|
||||||
u32 result_code = 0;
|
i32 result_code = 0;
|
||||||
switch(func_code){
|
switch(func_code){
|
||||||
case 0:
|
case 0:
|
||||||
result_code = SYS_read(vm);
|
result_code = SYS_read(vm);
|
||||||
break;
|
break;
|
||||||
case 1:;
|
case 1:
|
||||||
result_code = SYS_write(vm);
|
result_code = SYS_write(vm);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@@ -64,6 +64,6 @@ i32 SYS_impl(VM* vm){
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
vm->ax.u32v = result_code;
|
vm->registers.a.ex = result_code;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,29 +8,52 @@
|
|||||||
vm->current_pos += sizeof(VAR);\
|
vm->current_pos += sizeof(VAR);\
|
||||||
}
|
}
|
||||||
|
|
||||||
#define validateRegisterIndex(VAR) {\
|
#define validateRegisterCode(VAR) \
|
||||||
if(VAR> sizeof(vm->registers)){\
|
if(VAR == RegisterCode_Unset || VAR > RegisterCode_dh){\
|
||||||
VM_setError(vm, "invalid register index (%x)", VAR);\
|
VM_setError(vm, "invalid register index (%x)", VAR);\
|
||||||
return -1;\
|
return -1;\
|
||||||
}\
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define readRegisterVar(VAR) {\
|
#define readRegisterCode(VAR) {\
|
||||||
readVar(VAR);\
|
readVar(VAR);\
|
||||||
VAR -= 1;\
|
validateRegisterCode(VAR);\
|
||||||
validateRegisterIndex(VAR);\
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
#define OPERATOR_IMPL_1(NAME, OPERATOR)\
|
||||||
#define validateValueSize(VAR) {\
|
i32 NAME##_impl (VM* vm) {\
|
||||||
if(VAR < 1 || VAR > 4){\
|
RegisterCode dst_reg_code = 0;\
|
||||||
VM_setError(vm, "invalid value_size (%x)", VAR);\
|
readRegisterCode(dst_reg_code);\
|
||||||
return -1;\
|
u64 dst_reg_value = 0;\
|
||||||
}\
|
VM_registerRead(vm, &dst_reg_value, dst_reg_code);\
|
||||||
|
\
|
||||||
|
dst_reg_value = OPERATOR dst_reg_value;\
|
||||||
|
VM_registerWrite(vm, &dst_reg_value, dst_reg_code);\
|
||||||
|
return sizeof(dst_reg_code);\
|
||||||
}
|
}
|
||||||
|
|
||||||
#define readValueSizeVar(VAR) {\
|
#define OPERATOR_IMPL_2(NAME, OPERATOR)\
|
||||||
readVar(VAR);\
|
i32 NAME##_impl (VM* vm) {\
|
||||||
validateValueSize(VAR);\
|
RegisterCode dst_reg_code = 0, src_reg_code = 0;\
|
||||||
|
readRegisterCode(dst_reg_code);\
|
||||||
|
readRegisterCode(src_reg_code);\
|
||||||
|
u64 dst_reg_value = 0, src_reg_value = 0;\
|
||||||
|
VM_registerRead(vm, &dst_reg_value, dst_reg_code);\
|
||||||
|
VM_registerRead(vm, &src_reg_value, src_reg_code);\
|
||||||
|
\
|
||||||
|
dst_reg_value = dst_reg_value OPERATOR src_reg_value;\
|
||||||
|
VM_registerWrite(vm, &dst_reg_value, dst_reg_code);\
|
||||||
|
return sizeof(dst_reg_code) + sizeof(src_reg_code);\
|
||||||
|
}
|
||||||
|
|
||||||
|
#define OPERATOR_IMPL_CMP_FLAG(NAME, OPERATOR)\
|
||||||
|
i32 NAME##_impl (VM* vm) {\
|
||||||
|
RegisterCode src0_reg_code = 0, src1_reg_code = 0;\
|
||||||
|
readRegisterCode(src0_reg_code);\
|
||||||
|
readRegisterCode(src1_reg_code);\
|
||||||
|
u64 src0_reg_value = 0, src1_reg_value = 0;\
|
||||||
|
VM_registerRead(vm, &src0_reg_value, src0_reg_code);\
|
||||||
|
VM_registerRead(vm, &src1_reg_value, src1_reg_code);\
|
||||||
|
\
|
||||||
|
vm->flags.cmp = src0_reg_value OPERATOR src1_reg_value;\
|
||||||
|
return sizeof(src0_reg_code) + sizeof(src1_reg_code);\
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|||||||
36
src/instructions/impl/logical_operators.c
Normal file
36
src/instructions/impl/logical_operators.c
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include "impl_macros.h"
|
||||||
|
|
||||||
|
/// NOT [dst_register]
|
||||||
|
OPERATOR_IMPL_1(NOT, !)
|
||||||
|
|
||||||
|
/// INV [dst_register]
|
||||||
|
OPERATOR_IMPL_1(INV, ~)
|
||||||
|
|
||||||
|
|
||||||
|
/// OR [dst_register] [src_register]
|
||||||
|
OPERATOR_IMPL_2(OR, |)
|
||||||
|
|
||||||
|
/// XOR [dst_register] [src_register]
|
||||||
|
OPERATOR_IMPL_2(XOR, ^)
|
||||||
|
|
||||||
|
/// AND [dst_register] [src_register]
|
||||||
|
OPERATOR_IMPL_2(AND, &)
|
||||||
|
|
||||||
|
|
||||||
|
/// EQ [src0_register] [src1_register]
|
||||||
|
OPERATOR_IMPL_CMP_FLAG(EQ, ==)
|
||||||
|
|
||||||
|
/// NE [src0_register] [src1_register]
|
||||||
|
OPERATOR_IMPL_CMP_FLAG(NE, !=)
|
||||||
|
|
||||||
|
/// LT [src0_register] [src1_register]
|
||||||
|
OPERATOR_IMPL_CMP_FLAG(LT, <)
|
||||||
|
|
||||||
|
/// LE [src0_register] [src1_register]
|
||||||
|
OPERATOR_IMPL_CMP_FLAG(LE, <=)
|
||||||
|
|
||||||
|
/// GT [src0_register] [src1_register]
|
||||||
|
OPERATOR_IMPL_CMP_FLAG(GT, >)
|
||||||
|
|
||||||
|
/// GE [src0_register] [src1_register]
|
||||||
|
OPERATOR_IMPL_CMP_FLAG(GE, >=)
|
||||||
@@ -1,48 +1,16 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
#define mathOperatorImpl(OPERATOR){\
|
|
||||||
u8 dst_register_i = 0, src_register_i = 0;\
|
|
||||||
readRegisterVar(dst_register_i);\
|
|
||||||
readRegisterVar(src_register_i);\
|
|
||||||
/*u8 value_size = 0;\
|
|
||||||
readValueSizeVar(value_size);*/\
|
|
||||||
u8 value_size = 4;\
|
|
||||||
\
|
|
||||||
switch(value_size){\
|
|
||||||
case 1: \
|
|
||||||
vm->registers[dst_register_i].u8v0 OPERATOR##= vm->registers[src_register_i].u8v0;\
|
|
||||||
break;\
|
|
||||||
case 2: \
|
|
||||||
vm->registers[dst_register_i].u16v0 OPERATOR##= vm->registers[src_register_i].u16v0;\
|
|
||||||
break;\
|
|
||||||
case 4: \
|
|
||||||
vm->registers[dst_register_i].u32v OPERATOR##= vm->registers[src_register_i].u32v;\
|
|
||||||
break;\
|
|
||||||
}\
|
|
||||||
return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\
|
|
||||||
}
|
|
||||||
|
|
||||||
/// ADD [dst_register] [src_register]
|
/// ADD [dst_register] [src_register]
|
||||||
i32 ADD_impl(VM* vm){
|
OPERATOR_IMPL_2(ADD, +)
|
||||||
mathOperatorImpl(+);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// SUB [dst_register] [src_register]
|
/// SUB [dst_register] [src_register]
|
||||||
i32 SUB_impl(VM* vm){
|
OPERATOR_IMPL_2(SUB, -)
|
||||||
mathOperatorImpl(-);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// MUL [dst_register] [src_register]
|
/// MUL [dst_register] [src_register]
|
||||||
i32 MUL_impl(VM* vm){
|
OPERATOR_IMPL_2(MUL, *)
|
||||||
mathOperatorImpl(*)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// DIV [dst_register] [src_register]
|
/// DIV [dst_register] [src_register]
|
||||||
i32 DIV_impl(VM* vm){
|
OPERATOR_IMPL_2(DIV, /)
|
||||||
mathOperatorImpl(/)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// MOD [dst_register] [src_register]
|
/// MOD [dst_register] [src_register]
|
||||||
i32 MOD_impl(VM* vm){
|
OPERATOR_IMPL_2(MOD, %)
|
||||||
mathOperatorImpl(%)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,32 +2,65 @@
|
|||||||
#include "../collections/HashMap.h"
|
#include "../collections/HashMap.h"
|
||||||
|
|
||||||
i32 NOP_impl(VM* vm);
|
i32 NOP_impl(VM* vm);
|
||||||
i32 PUSH_impl(VM* vm);
|
i32 EXIT_impl(VM* vm);
|
||||||
i32 MOV_impl(VM* vm);
|
i32 SYS_impl(VM* vm);
|
||||||
|
|
||||||
|
i32 MOVC_impl(VM* vm);
|
||||||
|
i32 MOVR_impl(VM* vm);
|
||||||
|
|
||||||
i32 ADD_impl(VM* vm);
|
i32 ADD_impl(VM* vm);
|
||||||
i32 SUB_impl(VM* vm);
|
i32 SUB_impl(VM* vm);
|
||||||
i32 MUL_impl(VM* vm);
|
i32 MUL_impl(VM* vm);
|
||||||
i32 DIV_impl(VM* vm);
|
i32 DIV_impl(VM* vm);
|
||||||
i32 MOD_impl(VM* vm);
|
i32 MOD_impl(VM* vm);
|
||||||
i32 SYS_impl(VM* vm);
|
|
||||||
i32 EXIT_impl(VM* vm);
|
i32 EQ_impl(VM* vm);
|
||||||
|
i32 NE_impl(VM* vm);
|
||||||
|
i32 LT_impl(VM* vm);
|
||||||
|
i32 LE_impl(VM* vm);
|
||||||
|
i32 GT_impl(VM* vm);
|
||||||
|
i32 GE_impl(VM* vm);
|
||||||
|
i32 NOT_impl(VM* vm);
|
||||||
|
i32 INV_impl(VM* vm);
|
||||||
|
i32 OR_impl(VM* vm);
|
||||||
|
i32 XOR_impl(VM* vm);
|
||||||
|
i32 AND_impl(VM* vm);
|
||||||
|
|
||||||
i32 JMP_impl(VM* vm);
|
i32 JMP_impl(VM* vm);
|
||||||
i32 CALL_impl(VM* vm);
|
i32 JNZ_impl(VM* vm);
|
||||||
|
i32 JZ_impl(VM* vm);
|
||||||
|
|
||||||
|
|
||||||
Array_declare(Instruction);
|
Array_declare(Instruction);
|
||||||
static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
||||||
Instruction_construct(NOP),
|
Instruction_construct(NOP),
|
||||||
Instruction_construct(PUSH),
|
Instruction_construct(EXIT),
|
||||||
Instruction_construct(MOV),
|
Instruction_construct(SYS),
|
||||||
|
|
||||||
|
Instruction_construct(MOVC),
|
||||||
|
Instruction_construct(MOVR),
|
||||||
|
|
||||||
Instruction_construct(ADD),
|
Instruction_construct(ADD),
|
||||||
Instruction_construct(SUB),
|
Instruction_construct(SUB),
|
||||||
Instruction_construct(MUL),
|
Instruction_construct(MUL),
|
||||||
Instruction_construct(DIV),
|
Instruction_construct(DIV),
|
||||||
Instruction_construct(MOD),
|
Instruction_construct(MOD),
|
||||||
Instruction_construct(SYS),
|
|
||||||
Instruction_construct(EXIT),
|
Instruction_construct(EQ),
|
||||||
// Instruction_construct(JMP),
|
Instruction_construct(NE),
|
||||||
// Instruction_construct(CALL),
|
Instruction_construct(LT),
|
||||||
|
Instruction_construct(LE),
|
||||||
|
Instruction_construct(GT),
|
||||||
|
Instruction_construct(GE),
|
||||||
|
Instruction_construct(NOT),
|
||||||
|
Instruction_construct(INV),
|
||||||
|
Instruction_construct(OR),
|
||||||
|
Instruction_construct(XOR),
|
||||||
|
Instruction_construct(AND),
|
||||||
|
|
||||||
|
Instruction_construct(JMP),
|
||||||
|
Instruction_construct(JNZ),
|
||||||
|
Instruction_construct(JZ),
|
||||||
});
|
});
|
||||||
|
|
||||||
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
const Instruction* Instruction_getByOpcode(Opcode opcode){
|
||||||
|
|||||||
@@ -7,15 +7,33 @@ typedef i32 (*InstructionImplFunc_t)(VM* vm);
|
|||||||
|
|
||||||
typedef enum __attribute__((__packed__)) Opcode {
|
typedef enum __attribute__((__packed__)) Opcode {
|
||||||
Opcode_NOP,
|
Opcode_NOP,
|
||||||
Opcode_PUSH,
|
Opcode_EXIT,
|
||||||
Opcode_MOV,
|
Opcode_SYS,
|
||||||
|
|
||||||
|
Opcode_MOVC,
|
||||||
|
Opcode_MOVR,
|
||||||
|
|
||||||
Opcode_ADD,
|
Opcode_ADD,
|
||||||
Opcode_SUB,
|
Opcode_SUB,
|
||||||
Opcode_MUL,
|
Opcode_MUL,
|
||||||
Opcode_DIV,
|
Opcode_DIV,
|
||||||
Opcode_MOD,
|
Opcode_MOD,
|
||||||
Opcode_SYS,
|
|
||||||
Opcode_EXIT,
|
Opcode_EQ,
|
||||||
|
Opcode_NE,
|
||||||
|
Opcode_LT,
|
||||||
|
Opcode_LE,
|
||||||
|
Opcode_GT,
|
||||||
|
Opcode_GE,
|
||||||
|
Opcode_NOT,
|
||||||
|
Opcode_INV,
|
||||||
|
Opcode_OR,
|
||||||
|
Opcode_XOR,
|
||||||
|
Opcode_AND,
|
||||||
|
|
||||||
|
Opcode_JMP,
|
||||||
|
Opcode_JNZ,
|
||||||
|
Opcode_JZ,
|
||||||
} Opcode;
|
} Opcode;
|
||||||
|
|
||||||
typedef struct Instruction {
|
typedef struct Instruction {
|
||||||
|
|||||||
@@ -1,13 +1,76 @@
|
|||||||
#include "registers.h"
|
#include "registers.h"
|
||||||
|
|
||||||
|
#define check_code(R) if(str_equals(lower, STR(#R))) code = RegisterCode_##R;
|
||||||
|
|
||||||
RegisterCode RegisterCode_parse(str r){
|
RegisterCode RegisterCode_parse(str r){
|
||||||
if(str_equals(r, STR("ax")))
|
str lower = str_toLower(r);
|
||||||
return RegisterCode_ax;
|
RegisterCode code = RegisterCode_Unset;
|
||||||
if(str_equals(r, STR("bx")))
|
// a
|
||||||
return RegisterCode_bx;
|
check_code(rax)
|
||||||
if(str_equals(r, STR("cx")))
|
else check_code(eax)
|
||||||
return RegisterCode_cx;
|
else check_code(ax)
|
||||||
if(str_equals(r, STR("dx")))
|
else check_code(al)
|
||||||
return RegisterCode_dx;
|
else check_code(ah)
|
||||||
return RegisterCode_Unset;
|
// b
|
||||||
|
else check_code(rbx)
|
||||||
|
else check_code(ebx)
|
||||||
|
else check_code(bx)
|
||||||
|
else check_code(bl)
|
||||||
|
else check_code(bh)
|
||||||
|
// c
|
||||||
|
else check_code(rcx)
|
||||||
|
else check_code(ecx)
|
||||||
|
else check_code(cx)
|
||||||
|
else check_code(cl)
|
||||||
|
else check_code(ch)
|
||||||
|
//d
|
||||||
|
else check_code(rdx)
|
||||||
|
else check_code(edx)
|
||||||
|
else check_code(dx)
|
||||||
|
else check_code(dl)
|
||||||
|
else check_code(dh)
|
||||||
|
|
||||||
|
free(lower.data);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
str RegisterCode_toString(RegisterCode code){
|
||||||
|
char buf[3] = { '?', 'a', 'x' };
|
||||||
|
u8 index = code / 0x10;
|
||||||
|
switch(index){
|
||||||
|
default:
|
||||||
|
return str_copy(STR("!!! ERROR: invalid RegisterCode !!!"));
|
||||||
|
case 0:
|
||||||
|
case 1:
|
||||||
|
case 2:
|
||||||
|
case 3:
|
||||||
|
buf[1] += index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
str buf_str = str_construct(buf, 3, false);
|
||||||
|
switch(code & 0xf){
|
||||||
|
default:
|
||||||
|
return str_copy(STR("!!! ERROR: invalid RegisterCode !!!"));
|
||||||
|
case 1:
|
||||||
|
buf_str.data[0] = 'r';
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
buf_str.data[0] = 'e';
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
buf_str.data += 1;
|
||||||
|
buf_str.len -= 1;
|
||||||
|
break;
|
||||||
|
case 7:
|
||||||
|
buf_str.data[0] = 'l';
|
||||||
|
buf_str.len -= 1;
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
buf_str.data[0] = 'h';
|
||||||
|
buf_str.len -= 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return str_copy(buf_str);
|
||||||
}
|
}
|
||||||
@@ -3,11 +3,33 @@
|
|||||||
#include "../string/str.h"
|
#include "../string/str.h"
|
||||||
|
|
||||||
typedef enum RegisterCode {
|
typedef enum RegisterCode {
|
||||||
RegisterCode_Unset,
|
RegisterCode_Unset = 0,
|
||||||
RegisterCode_ax,
|
|
||||||
RegisterCode_bx,
|
RegisterCode_rax = 0x01,
|
||||||
RegisterCode_cx,
|
RegisterCode_eax = 0x02,
|
||||||
RegisterCode_dx
|
RegisterCode_ax = 0x04,
|
||||||
} RegisterCode;
|
RegisterCode_al = 0x07,
|
||||||
|
RegisterCode_ah = 0x08,
|
||||||
|
|
||||||
|
RegisterCode_rbx = 0x11,
|
||||||
|
RegisterCode_ebx = 0x12,
|
||||||
|
RegisterCode_bx = 0x14,
|
||||||
|
RegisterCode_bl = 0x17,
|
||||||
|
RegisterCode_bh = 0x18,
|
||||||
|
|
||||||
|
RegisterCode_rcx = 0x21,
|
||||||
|
RegisterCode_ecx = 0x22,
|
||||||
|
RegisterCode_cx = 0x24,
|
||||||
|
RegisterCode_cl = 0x27,
|
||||||
|
RegisterCode_ch = 0x28,
|
||||||
|
|
||||||
|
RegisterCode_rdx = 0x31,
|
||||||
|
RegisterCode_edx = 0x32,
|
||||||
|
RegisterCode_dx = 0x34,
|
||||||
|
RegisterCode_dl = 0x37,
|
||||||
|
RegisterCode_dh = 0x38,
|
||||||
|
} __attribute__((__packed__)) RegisterCode;
|
||||||
|
|
||||||
RegisterCode RegisterCode_parse(str register_name);
|
RegisterCode RegisterCode_parse(str register_name);
|
||||||
|
/// @return allocated string
|
||||||
|
str RegisterCode_toString(RegisterCode code);
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
#include "instructions/instructions.h"
|
#include "instructions/instructions.h"
|
||||||
#include "collections/List.h"
|
#include "collections/List.h"
|
||||||
#include "compiler/Compiler.h"
|
#include "compiler/Compiler.h"
|
||||||
|
#include "VM/Display/Display.h"
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
#define arg_is(STR) (strcmp(argv[argi], STR) == 0)
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef int8_t i8;
|
typedef int8_t i8;
|
||||||
typedef uint8_t u8;
|
typedef uint8_t u8;
|
||||||
@@ -20,10 +21,6 @@ typedef uint64_t u64;
|
|||||||
typedef float f32;
|
typedef float f32;
|
||||||
typedef double f64;
|
typedef double f64;
|
||||||
|
|
||||||
typedef u8 bool;
|
|
||||||
#define true 1
|
|
||||||
#define false 0
|
|
||||||
|
|
||||||
typedef const char* cstr;
|
typedef const char* cstr;
|
||||||
|
|
||||||
#if defined(_WIN64) || defined(_WIN32)
|
#if defined(_WIN64) || defined(_WIN32)
|
||||||
|
|||||||
Reference in New Issue
Block a user