From 38d62ba8d730246884c44e7ecf5cf29bfd8851ee Mon Sep 17 00:00:00 2001 From: Timerix Date: Fri, 15 Nov 2024 09:03:18 +0500 Subject: [PATCH] first commit --- .gitignore | 21 +++ .vscode/.gitignore | 1 + .vscode/launch.json | 29 ++++ .vscode/tasks.json | 30 ++++ README.md | 38 +++++ project.config | 187 +++++++++++++++++++++++++ src/VM/VM.c | 65 +++++++++ src/VM/VM.h | 71 ++++++++++ src/instructions/impl/EXIT.c | 8 ++ src/instructions/impl/MOV.c | 16 +++ src/instructions/impl/NOP.c | 6 + src/instructions/impl/PUSH.c | 15 ++ src/instructions/impl/SYS.c | 44 ++++++ src/instructions/impl/impl_macros.h | 32 +++++ src/instructions/impl/math_operators.c | 47 +++++++ src/instructions/instructions.c | 24 ++++ src/instructions/instructions.h | 34 +++++ src/main.c | 77 ++++++++++ src/std.h | 27 ++++ 19 files changed, 772 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/.gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 README.md create mode 100755 project.config create mode 100644 src/VM/VM.c create mode 100644 src/VM/VM.h create mode 100644 src/instructions/impl/EXIT.c create mode 100644 src/instructions/impl/MOV.c create mode 100644 src/instructions/impl/NOP.c create mode 100644 src/instructions/impl/PUSH.c create mode 100644 src/instructions/impl/SYS.c create mode 100644 src/instructions/impl/impl_macros.h create mode 100644 src/instructions/impl/math_operators.c create mode 100644 src/instructions/instructions.c create mode 100644 src/instructions/instructions.h create mode 100644 src/main.c create mode 100644 src/std.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..498924c --- /dev/null +++ b/.gitignore @@ -0,0 +1,21 @@ +# build results +bin/ +obj/ + +# IDE files +.vs/ +.vshistory/ +.editorconfig +*.user +*.vcxproj.filters + +# other files +.old*/ +old/ +tmp/ +temp/ +*.tmp +*.temp +logs/ +log/ +*.log diff --git a/.vscode/.gitignore b/.vscode/.gitignore new file mode 100644 index 0000000..c3d3a20 --- /dev/null +++ b/.vscode/.gitignore @@ -0,0 +1 @@ +settings.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..4c87fb0 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Debug", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/bin/tali", + "windows": { "program": "${workspaceFolder}/bin/tali.exe" }, + "args": [ "--image", "image.bin" ], + "cwd": "${workspaceFolder}/bin", + "preLaunchTask": "build_exec_dbg", + "stopAtEntry": false, + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ] + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..427cc13 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,30 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build_exec_dbg", + "detail": "build project with debug symbols", + "type": "cppbuild", + "command": "bash", + "args": [ + "-c", + "cbuild build_exec_dbg" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "problemMatcher": ["$gcc"], + "group": { + "kind": "build" + }, + "presentation": { + "echo": true, + "reveal": "always", + "focus": true, + "panel": "shared", + "showReuseMessage": false, + "clear": true + } + } + ] +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..ae83057 --- /dev/null +++ b/README.md @@ -0,0 +1,38 @@ +# TALI +Assembly language interpreter written in pure C. Can execute programs up to 1 MEGABYTE (1048576 bytes) in size!!! + +## Building +1. Install [cbuild](https://timerix.ddns.net:3322/Timerix/cbuild.git) +2. ```sh + cbuild build_exec_dbg + ``` + +## 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 +| code | name | size (bits) | +|------|------|-------------| +| 00 | ax | 32 | +| 01 | bx | 32 | +| 02 | cx | 32 | +| 03 | dx | 32 | + +### 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. +| `ax` | name | `bx` | `cx` | `dx` | details | +|-----------|------|----|----|----|---------| +| 0 | read | file number | buffer pointer | buffer size | read data from file | +| 1 | write | file number | buffer pointer | buffer size | write data to file | diff --git a/project.config b/project.config new file mode 100755 index 0000000..23b9d7a --- /dev/null +++ b/project.config @@ -0,0 +1,187 @@ +#!/usr/bin/env bash +CBUILD_VERSION=2.1.4 +CONFIG_VERSION=1 + +PROJECT="tali" +CMP_C="gcc" +CMP_CPP="g++" +STD_C="c11" +STD_CPP="c++11" +WARN_C="-Wall -Wextra -Wno-unused-parameter" +WARN_CPP="-Wall -Wextra -Wno-unused-parameter" +SRC_C="$(find src -name '*.c')" +SRC_CPP="$(find src -name '*.cpp')" + +# Directory with dependency configs. +# See cbuild/example_dependency_configs +DEPENDENCY_CONFIGS_DIR='.' +# List of dependency config files in DEPENDENCY_CONFIGS_DIR separated by space. +ENABLED_DEPENDENCIES='' + +# OBJDIR structure: +# ├── objects/ - Compiled object files. Cleans on each call of build task +# ├── static_libs/ - Symbolic links to static libraries used by linker. Cleans on each call of build task. +# ├── static_libs/ - Symbolic links to dynamic libraries used by linker. Cleans on each call of build task. +# └── profile/ - gcc *.gcda profiling info files +OBJDIR="obj" +OUTDIR="bin" +STATIC_LIB_FILE="lib$PROJECT.a" + +# OS-specific options +case "$OS" in + WINDOWS) + EXEC_FILE="$PROJECT.exe" + SHARED_LIB_FILE="$PROJECT.dll" + # example: "-I./" + INCLUDE="" + ;; + LINUX) + EXEC_FILE="$PROJECT" + SHARED_LIB_FILE="$PROJECT.so" + INCLUDE="" + ;; + *) + error "operating system $OS has no configuration variants" + ;; +esac + +# TASKS +case "$TASK" in + # creates executable using profiling info if it exists + build_exec) + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto + # -fprofile-use enables compiler to use profiling info files to optimize executable + # -fprofile-prefix-path sets path where profiling info about objects are be saved + # -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" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + POST_TASK_SCRIPT= + ;; + # creates executable with debug info and no optimizations + build_exec_dbg) + C_ARGS="-O0 -g3" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + POST_TASK_SCRIPT= + ;; + # creates shared library + build_shared_lib) + C_ARGS="-O2 -fpic -flto -shared" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh + POST_TASK_SCRIPT= + ;; + # creates shared library with debug symbols and no optimizations + build_shared_lib_dbg) + C_ARGS="-O0 -g3 -fpic -shared" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh + POST_TASK_SCRIPT= + ;; + # creates static library + build_static_lib) + C_ARGS="-O2 -fpic -fdata-sections -ffunction-sections" + CPP_ARGS="$C_ARGS" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh + POST_TASK_SCRIPT= + ;; + # creates static library with debug symbols and no optimizations + build_static_lib_dbg) + C_ARGS="-O0 -g3" + CPP_ARGS="$C_ARGS" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh + POST_TASK_SCRIPT= + ;; + # executes $EXEC_FILE + exec) + TASK_SCRIPT=cbuild/default_tasks/exec.sh + ;; + # executes $EXEC_FILE with valgrind memory checker + valgrind) + VALGRIND_ARGS="-s --read-var-info=yes --track-origins=yes --fullpath-after=$(pwd) --leak-check=full --show-leak-kinds=all" + TASK_SCRIPT=cbuild/default_tasks/valgrind.sh + ;; + # generates profiling info + profile) + OUTDIR="$OUTDIR/profile" + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto + # -pg adds code to executable, that generates file containing function call info (gmon.out) + # -fprofile-generate generates executable with profiling code + # -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" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/profile.sh + POST_TASK_SCRIPT= + ;; + # compiles program with -pg and runs it with gprof + # uses gprof2dot python script to generate function call tree (pip install gprof2dot) + # requires graphviz (https://www.graphviz.org/download/source/) + gprof) + OUTDIR="$OUTDIR/gprof" + # -pg adds code to executable, that generates file containing function call info (gmon.out) + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/gprof.sh + POST_TASK_SCRIPT= + ;; + # compiles program and runs it with callgrind (part of valgrind) + # uses gprof2dot python script to generate function call tree (pip install gprof2dot) + # requires graphviz (https://www.graphviz.org/download/source/) + # P.S. detailed results can be viewed in KCacheGrind + callgrind) + OUTDIR="$OUTDIR/callgrind" + # -pg adds code to executable, that generates file containing function call info (gmon.out) + C_ARGS="-O2 -flto=auto -fuse-linker-plugin" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/callgrind.sh + POST_TASK_SCRIPT= + ;; + # compiles executable with sanitizers and executes it to find errors and warnings + sanitize) + OUTDIR="$OUTDIR/sanitize" + C_ARGS="-O0 -g3 -fsanitize=undefined,address" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/exec.sh + POST_TASK_SCRIPT= + ;; + # rebuilds specified dependencies + # EXAMPLE: `cbuild rebuild_dependencies=libexample1,fonts` + # 'all' can be specified to rebuild all dependencies + rebuild_dependencies) + TASK_SCRIPT=cbuild/default_tasks/rebuild_dependencies.sh + ;; + # deletes generated files + clean) + TASK_SCRIPT=cbuild/default_tasks/clean.sh + ;; + # nothing to do + "" | no_task) + ;; + # unknown task + *) + error "task <$PROJECT/$TASK> not found" + ;; +esac diff --git a/src/VM/VM.c b/src/VM/VM.c new file mode 100644 index 0000000..a6de2fa --- /dev/null +++ b/src/VM/VM.c @@ -0,0 +1,65 @@ +#include "VM.h" +#include "../instructions/instructions.h" + +void VM_init(VM* vm){ + memset(vm, 0, sizeof(VM)); + vm->state = VMState_Initialized; +} + +bool VM_loadProgram(VM* vm, u8* data, size_t size){ + if(data == NULL){ + VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because data == NULL"); + return false; + } + if(size == 0){ + VM_setErrorMessage(vm, "[VM_loadProgram] can't load program because size == 0"); + return false; + } + + vm->data = data; + vm->data_size = size; + return true; +} + +i32 VM_executeProgram(VM* vm){ + if(vm->data == NULL){ + VM_setErrorMessage(vm, "[VM_executeProgram] data is null"); + return -1; + } + + size_t pos = 0; + while (pos < vm->data_size){ + u8 opcode = vm->data[pos]; + + const Instruction* instr = Instruction_getFromOpcode(opcode); + if(instr == NULL){ + + return -1; + } + + pos++; + i32 bytes_read = instr->implementation(vm, pos); + if(bytes_read < 0) + return -1; + + pos += bytes_read; + } + + if(vm->state != VMState_Exited){ + VM_setErrorMessage(vm, "[%p] unexpected end of program", (void*)pos); + return -1; + } + + // exit code of the program should be in ax register + return vm->ax.i32v; +} + +bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size){ + if(pos + size >= vm->data_size){ + VM_setErrorMessage(vm, "[%p] unexpected end of data", (void*)vm->data_size); + return false; + } + + memcpy(dst, vm->data + pos, size); + return true; +} diff --git a/src/VM/VM.h b/src/VM/VM.h new file mode 100644 index 0000000..a141f27 --- /dev/null +++ b/src/VM/VM.h @@ -0,0 +1,71 @@ +#pragma once +#include "../std.h" + +typedef union Register { + u32 u32v; + i32 i32v; + f32 f32v; + struct { + u16 u16v0; + u16 u16v1; + }; + struct { + i16 i16v0; + i16 i16v1; + }; + struct { + u8 u8v0; + u8 u8v1; + u8 u8v2; + u8 u8v3; + }; + struct { + i8 i8v0; + i8 i8v1; + i8 i8v2; + i8 i8v3; + }; +} Register; + +typedef enum VMState { + VMState_Initialized, + VMState_Executing, + VMState_Exited, +} VMState; + +typedef struct VM { + union { + struct { + Register ax; + Register bx; + Register cx; + Register dx; + }; + Register registers[4]; + }; + + VMState state; + char* error_message; + + u8* data; + size_t data_size; +} VM; + +void VM_init(VM* vm); + +/// @brief Loads a program from the buffer. +/// @param data buffer with full program code +/// @param size size of the program in bytes +bool VM_loadProgram(VM* vm, u8* data, size_t size); + +/// @brief Executes the program loaded into VM. +/// @return program exit code or -1 on error (check vm.error_message) +i32 VM_executeProgram(VM* vm); + +#define VM_setErrorMessage(V, FORMAT, ...) {\ + char* buf = malloc(256);\ + sprintf(buf, FORMAT, ##__VA_ARGS__);\ + vm->error_message = buf;\ +} + +bool VM_dataRead(VM* vm, void* dst, size_t pos, size_t size); diff --git a/src/instructions/impl/EXIT.c b/src/instructions/impl/EXIT.c new file mode 100644 index 0000000..bf1d641 --- /dev/null +++ b/src/instructions/impl/EXIT.c @@ -0,0 +1,8 @@ +#include "impl_macros.h" + +/// EXIT +/// ax - exit code +i32 EXIT_impl(VM* vm, size_t pos){ + vm->state = VMState_Exited; + return 0; +} diff --git a/src/instructions/impl/MOV.c b/src/instructions/impl/MOV.c new file mode 100644 index 0000000..5149a0e --- /dev/null +++ b/src/instructions/impl/MOV.c @@ -0,0 +1,16 @@ +#include "impl_macros.h" + +/// MOV [dst_register] [src_register] +i32 MOV_impl(VM* vm, size_t pos){ + 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_setErrorMessage(vm, "[%p] dst_register_i == src_register_i (%x) ", (void*)pos, 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); +} diff --git a/src/instructions/impl/NOP.c b/src/instructions/impl/NOP.c new file mode 100644 index 0000000..59cbe67 --- /dev/null +++ b/src/instructions/impl/NOP.c @@ -0,0 +1,6 @@ +#include "impl_macros.h" + +/// NOP +i32 NOP_impl(VM* vm, size_t pos){ + return 0; +} diff --git a/src/instructions/impl/PUSH.c b/src/instructions/impl/PUSH.c new file mode 100644 index 0000000..ecc2c4a --- /dev/null +++ b/src/instructions/impl/PUSH.c @@ -0,0 +1,15 @@ +#include "impl_macros.h" + +/// PUSH [dst_register] [value_size] [value] +i32 PUSH_impl(VM* vm, size_t pos){ + u8 dst_register_i = 0; + readRegisterVar(dst_register_i); + u8 value_size = 0; + readValueSizeVar(value_size); + + vm->registers[dst_register_i].u32v = 0; + if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, pos, value_size)) + return -1; + + return sizeof(dst_register_i) + sizeof(value_size) + value_size; +} diff --git a/src/instructions/impl/SYS.c b/src/instructions/impl/SYS.c new file mode 100644 index 0000000..1a9e651 --- /dev/null +++ b/src/instructions/impl/SYS.c @@ -0,0 +1,44 @@ +#include "impl_macros.h" + +FILE* fileFromN(VM* vm, size_t pos, u32 file_n){ + FILE* f = NULL; + switch(file_n){ + case 0: f = stdin; break; + case 1: f = stdout; break; + case 2: f = stderr; break; + default: + VM_setErrorMessage(vm, "[%p] invalid file_n (%x) ", (void*)pos, file_n); + break; + } + + return f; +} + +/// SYS +/// ax - func code +i32 SYS_impl(VM* vm, size_t pos){ + u8 func_code = vm->ax.u8v0; + size_t result_code = 0; + switch(func_code){ + // sys_read + // bx - file n + // cx - buffer ptr + // dx - buffer size + case 0:; + result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v)); + break; + // sys_write + // bx - file n + // cx - buffer ptr + // dx - buffer size + case 1:; + result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v)); + break; + default: + VM_setErrorMessage(vm, "[%p] invalid system call (%x) ", (void*)pos, func_code); + return -1; + } + + vm->ax.u32v = result_code; + return 0; +} diff --git a/src/instructions/impl/impl_macros.h b/src/instructions/impl/impl_macros.h new file mode 100644 index 0000000..aaef32d --- /dev/null +++ b/src/instructions/impl/impl_macros.h @@ -0,0 +1,32 @@ +#pragma once +#include "../instructions.h" + +#define readVar(VAR) {\ + if(!VM_dataRead(vm, &VAR, pos, sizeof(VAR))) \ + return -1;\ + pos += sizeof(VAR);\ +} + +#define validateRegisterIndex(VAR) {\ + if(VAR > sizeof(vm->registers)){\ + VM_setErrorMessage(vm, "[%p] invalid register index (%x)", (void*)pos, VAR);\ + return -1;\ + }\ +} + +#define readRegisterVar(VAR) {\ + readVar(VAR);\ + validateRegisterIndex(VAR);\ +} + +#define validateValueSize(VAR) {\ + if(VAR < 1 || VAR > 4){\ + VM_setErrorMessage(vm, "[%p] invalid value_size (%x)", (void*)pos, VAR);\ + return -1;\ + }\ +} + +#define readValueSizeVar(VAR) {\ + readVar(VAR);\ + validateValueSize(VAR);\ +} diff --git a/src/instructions/impl/math_operators.c b/src/instructions/impl/math_operators.c new file mode 100644 index 0000000..feeb0f0 --- /dev/null +++ b/src/instructions/impl/math_operators.c @@ -0,0 +1,47 @@ +#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);\ + \ + 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] +i32 ADD_impl(VM* vm, size_t pos){ + mathOperatorImpl(+); +} + +/// SUB [dst_register] [src_register] +i32 SUB_impl(VM* vm, size_t pos){ + mathOperatorImpl(-); +} + +/// MUL [dst_register] [src_register] +i32 MUL_impl(VM* vm, size_t pos){ + mathOperatorImpl(*) +} + +/// DIV [dst_register] [src_register] +i32 DIV_impl(VM* vm, size_t pos){ + mathOperatorImpl(/) +} + +/// MOD [dst_register] [src_register] +i32 MOD_impl(VM* vm, size_t pos){ + mathOperatorImpl(%) +} diff --git a/src/instructions/instructions.c b/src/instructions/instructions.c new file mode 100644 index 0000000..bb4f01b --- /dev/null +++ b/src/instructions/instructions.c @@ -0,0 +1,24 @@ +#include "instructions.h" + +const Instruction instructions[] = { + Instruction_construct(NOP), + Instruction_construct(PUSH), + Instruction_construct(MOV), + Instruction_construct(ADD), + Instruction_construct(SUB), + Instruction_construct(MUL), + Instruction_construct(DIV), + Instruction_construct(MOD), + Instruction_construct(SYS), + Instruction_construct(EXIT), + // Instruction_construct(JMP), + // Instruction_construct(CALL), +}; +const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]); + +const Instruction* Instruction_getFromOpcode(u8 opcode){ + if(opcode >= instructions_count) + return NULL; + + return instructions + opcode; +} diff --git a/src/instructions/instructions.h b/src/instructions/instructions.h new file mode 100644 index 0000000..a720861 --- /dev/null +++ b/src/instructions/instructions.h @@ -0,0 +1,34 @@ +#pragma once +#include "../VM/VM.h" + +///@param program_pos position in vm->program next afrer opcode +///@returns number of bytes read +typedef i32 (*InstructionImplFunc_t)(VM* vm, size_t program_pos); + +typedef struct Instruction { + const char* name; + InstructionImplFunc_t implementation; +} Instruction; + +#define Instruction_construct(NAME) {\ + .name = #NAME, \ + .implementation = NAME##_impl \ +} + +/// @brief get instruction info from table +/// @param opcode any byte +/// @return ptr to struct or NULL +const Instruction* Instruction_getFromOpcode(u8 opcode); + +i32 NOP_impl(VM* vm, size_t pos); +i32 PUSH_impl(VM* vm, size_t pos); +i32 MOV_impl(VM* vm, size_t pos); +i32 ADD_impl(VM* vm, size_t pos); +i32 SUB_impl(VM* vm, size_t pos); +i32 MUL_impl(VM* vm, size_t pos); +i32 DIV_impl(VM* vm, size_t pos); +i32 MOD_impl(VM* vm, size_t pos); +i32 SYS_impl(VM* vm, size_t pos); +i32 EXIT_impl(VM* vm, size_t pos); +i32 JMP_impl(VM* vm, size_t pos); +i32 CALL_impl(VM* vm, size_t pos); diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e657940 --- /dev/null +++ b/src/main.c @@ -0,0 +1,77 @@ +#include "VM/VM.h" +#include "instructions/instructions.h" + +#define arg_is(STR) (strcmp(argv[argi], STR) == 0) + +i32 main(const i32 argc, const char** argv){ + const char* filename = NULL; + + for(i32 argi = 1; argi < argc; argi++){ + if(arg_is("-h") || arg_is("--help")){ + printf( + "-h, --help Show this message\n" + "-op, --opcodes Shows list of all instructions.\n" + "-i, --image [FILE] Boot VM using image file\n" + ); + return 0; + } + else if(arg_is("-op") || arg_is("--opcodes")){ + for(u8 opcode = 0; opcode < 255; opcode++){ + const Instruction* instr = Instruction_getFromOpcode(opcode); + if(instr != NULL){ + printf("%02x %s\n", opcode, instr->name); + } + } + return 0; + } + else if(arg_is("-i") || arg_is("--image")){ + if(++argi >= argc){ + printfe("ERROR: no image file specified"); + return 1; + } + filename = argv[argi]; + } + else { + printfe("ERROR: unknown argument '%s'", argv[argi]); + return 1; + } + } + + if(filename == NULL){ + printfe("ERROR: no arguments provided. Use --help to know more."); + return 1; + } + + FILE* file = fopen(filename, "rb"); + if(file == NULL){ + printfe("ERROR: can't open file '%s'", filename); + return 1; + } + + const size_t buffer_size = 1024*1024; + u8* buffer = malloc(buffer_size); + memset(buffer, 0, buffer_size); + + size_t bytes_read = fread(buffer, 1, buffer_size, file); + fclose(file); + if(bytes_read == (size_t)EOF){ + printfe("ERROR: can't read file '%s'", filename); + free(buffer); + return 1; + } + + VM vm; + VM_init(&vm); + + i32 exit_code = 1; + if(VM_loadProgram(&vm, buffer, bytes_read)){ + exit_code = VM_executeProgram(&vm); + } + if(vm.error_message != NULL){ + printfe("VM ERROR: %s", vm.error_message); + free(vm.error_message); + } + + free(buffer); + return exit_code; +} diff --git a/src/std.h b/src/std.h new file mode 100644 index 0000000..d3321cc --- /dev/null +++ b/src/std.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +typedef int8_t i8; +typedef uint8_t u8; +typedef int16_t i16; +typedef uint16_t u16; +typedef int32_t i32; +typedef uint32_t u32; +typedef int64_t i64; +typedef uint64_t u64; +typedef float f32; +typedef double f64; + +typedef u8 bool; +#define true 1 +#define false 0 + +#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__) \ No newline at end of file