Compare commits

...

36 Commits

Author SHA1 Message Date
Timerix
cf724a8d13 updated tlibc 2025-11-26 20:56:58 +05:00
5397965319 added video enable option 2025-08-29 14:37:58 +05:00
7876210be6 updated tlibc 2025-08-10 19:06:22 +03:00
17baabbd95 properly named constructors and destructors 2025-07-24 18:26:51 +03:00
72f8e196a7 moved collections and string code to tlibc submodule 2025-07-24 17:49:50 +03:00
cf5ed7b601 implemented flag for comparison result 2025-04-18 03:03:20 +05:00
ba72dae68f refactored registers 2025-04-18 02:46:46 +05:00
dd8c65ef79 loop.tasm 2025-03-12 21:27:40 +05:00
0fa4e24421 main section compilation fix 2025-03-12 21:27:34 +05:00
ee162a70ed jump 2025-03-12 12:57:42 +05:00
b9fa669fd1 fixed bugs 2025-03-12 12:57:16 +05:00
ad5c2b856a logical operators 2025-03-11 14:01:47 +05:00
4de066b6c1 test render loop 2025-03-09 18:35:56 +05:00
0291279f1a sdl3 2025-02-27 22:53:27 +05:00
c2bd5922ff syntax error 2025-02-27 22:47:45 +05:00
3e3f01db4e register struct update 2025-02-13 22:17:55 +05:00
a69d68f69c instructions rename and uninitialized memory error fix 2025-02-12 14:27:50 +05:00
715a2cd82e temporarely disabled variable size reading 2025-02-05 17:26:01 +05:00
3fd45311c5 constant data names linking 2025-02-05 17:25:12 +05:00
b1b20d336d compile binary 2025-02-05 12:08:40 +05:00
53ca7e1b49 writeBinaryFile 2025-02-04 13:31:19 +05:00
fe6d690251 registers.h 2025-02-04 10:45:37 +05:00
51ef24bb53 fixed memory issues 2025-02-03 22:30:56 +05:00
422d967165 toUpper, toLower 2025-02-03 22:30:43 +05:00
5d275c8dd1 added compatibility with linux 2025-02-03 22:30:13 +05:00
2faff91981 Parser_parseOperation 2025-02-03 21:14:02 +05:00
b443367f46 HashMap 2025-02-03 12:30:01 +05:00
fd9e5dda78 List_tryRemoveAt 2025-02-03 12:29:31 +05:00
823223ffa7 cstr -> str where possible 2025-02-02 19:18:01 +05:00
8a38813ba5 StringBuilder_alloc 2025-02-02 19:16:29 +05:00
7c2809aae2 StringBuilder_removeFromEnd 2025-02-02 17:59:50 +05:00
9e9b43afb4 str_seek 2025-02-02 17:59:30 +05:00
2831474f79 str and StringBuilder 2025-02-02 00:48:34 +05:00
2d3e66dd38 rename fix 2025-01-28 15:18:20 +03:00
7f606dfaff List growth factor and padding 2025-01-27 09:42:34 +05:00
46e5eb1887 parseDataDefinition 2025-01-27 09:40:49 +05:00
55 changed files with 1619 additions and 852 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "dependencies/tlibc"]
path = dependencies/tlibc
url = https://timerix.ddns.net/git/Timerix/tlibc.git

15
.vscode/c_cpp_properties.json vendored Normal file
View File

@@ -0,0 +1,15 @@
{
"configurations": [
{
"name": "all",
"defines": [],
"includePath": [
"dependencies/tlibc/include",
"src",
"${default}"
],
"cStandard": "c11"
}
],
"version": 4
}

8
.vscode/launch.json vendored
View File

@@ -2,16 +2,20 @@
"version": "0.2.0", "version": "0.2.0",
"configurations": [ "configurations": [
{ {
"name": "(gdb) Debug", "name": "gdb_debug",
"type": "cppdbg", "type": "cppdbg",
"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/video.tasm", "o.bin",
"-i", "o.bin", "--debug", "--video"
],
"cwd": "${workspaceFolder}/bin", "cwd": "${workspaceFolder}/bin",
"preLaunchTask": "build_exec_dbg", "preLaunchTask": "build_exec_dbg",
"stopAtEntry": false, "stopAtEntry": false,
"externalConsole": false, "externalConsole": false,
"internalConsoleOptions": "neverOpen",
"MIMode": "gdb", "MIMode": "gdb",
"miDebuggerPath": "gdb", "miDebuggerPath": "gdb",
"setupCommands": [ "setupCommands": [

View File

@@ -2,37 +2,80 @@
Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYTE (1048576 bytes) in size!!! Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYTE (1048576 bytes) in size!!!
## Building ## Building
1. Install [cbuild](https://timerix.ddns.net:3322/Timerix/cbuild.git) 1. Clone repo
2. ```sh ```
git clone --recurse-submodules https://timerix.ddns.net/git/Timerix/tcpu.git
```
2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild.git)
3. 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
View 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

1
dependencies/tlibc vendored Submodule

Submodule dependencies/tlibc added at 6a7f0a8715

19
dependencies/tlibc.config vendored Normal file
View File

@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# This is a dependency config.
# You can copy it to another project to add tlibc dependency.
DEP_WORKING_DIR="$DEPENDENCIES_DIR/tlibc"
if [[ "$TASK" = *_dbg ]]; then
dep_build_target="build_static_lib_dbg"
else
dep_build_target="build_static_lib"
fi
DEP_PRE_BUILD_COMMAND=""
DEP_BUILD_COMMAND="cbuild $dep_build_target"
DEP_POST_BUILD_COMMAND=""
DEP_CLEAN_COMMAND="cbuild clean"
DEP_DYNAMIC_OUT_FILES=""
DEP_STATIC_OUT_FILES="bin/tlibc.a"
DEP_OTHER_OUT_FILES=""
PRESERVE_OUT_DIRECTORY_STRUCTURE=false

View 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
View 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

View File

@@ -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
View 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/video.tasm Normal file
View File

@@ -0,0 +1,7 @@
/*
Example of graphical application
*/
.main:
//TODO: add a way to access Event struct's fields
exit

View File

@@ -1,22 +1,30 @@
#!/usr/bin/env bash #!/usr/bin/env bash
CBUILD_VERSION=2.1.4 CBUILD_VERSION=2.3.2
CONFIG_VERSION=1
PROJECT="tcpu" PROJECT="tcpu"
CMP_C="gcc" CMP_C="gcc"
CMP_CPP="g++" CMP_CPP="g++"
STD_C="c11" STD_C="c99"
STD_CPP="c++11" STD_CPP="c++11"
WARN_C="-Wall -Wextra -Wno-unused-parameter" WARN_C="-Wall -Wextra
WARN_CPP="-Wall -Wextra -Wno-unused-parameter" -Wduplicated-branches
-Wduplicated-cond
-Wformat=2
-Wmissing-include-dirs
-Wshadow
-Werror=return-type
-Werror=pointer-arith
-Werror=init-self
-Werror=incompatible-pointer-types"
WARN_CPP="$WARN_C"
SRC_C="$(find src -name '*.c')" SRC_C="$(find src -name '*.c')"
SRC_CPP="$(find src -name '*.cpp')" SRC_CPP="$(find src -name '*.cpp')"
# Directory with dependency configs. # Directory with dependency configs.
# See cbuild/example_dependency_configs # See cbuild/example_dependency_configs
DEPENDENCY_CONFIGS_DIR='.' DEPENDENCY_CONFIGS_DIR='dependencies'
# List of dependency config files in DEPENDENCY_CONFIGS_DIR separated by space. # List of dependency config files in DEPENDENCY_CONFIGS_DIR separated by space.
ENABLED_DEPENDENCIES='' ENABLED_DEPENDENCIES='tlibc'
# OBJDIR structure: # OBJDIR structure:
# ├── objects/ - Compiled object files. Cleans on each call of build task # ├── objects/ - Compiled object files. Cleans on each call of build task
@@ -25,20 +33,21 @@ ENABLED_DEPENDENCIES=''
# └── profile/ - gcc *.gcda profiling info files # └── profile/ - gcc *.gcda profiling info files
OBJDIR="obj" OBJDIR="obj"
OUTDIR="bin" OUTDIR="bin"
STATIC_LIB_FILE="lib$PROJECT.a" STATIC_LIB_FILE="$PROJECT.a"
INCLUDE="-Isrc -I$DEPENDENCIES_DIR/tlibc/include"
# OS-specific options # OS-specific options
case "$OS" in 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=""
;; ;;
LINUX) LINUX)
EXEC_FILE="$PROJECT" EXEC_FILE="$PROJECT"
SHARED_LIB_FILE="$PROJECT.so" SHARED_LIB_FILE="$PROJECT.so"
INCLUDE="" LINKER_LIBS="-lSDL3_image -lSDL3"
;; ;;
*) *)
error "operating system $OS has no configuration variants" error "operating system $OS has no configuration variants"
@@ -57,62 +66,62 @@ 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="@cbuild/default_tasks/strip_exec.sh"
;; ;;
# creates executable with debug info and no optimizations # creates executable with debug info and no optimizations
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=""
;; ;;
# creates shared library # creates shared library
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=""
;; ;;
# creates shared library with debug symbols and no optimizations # creates shared library with debug symbols and no optimizations
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=""
;; ;;
# creates static library # creates static library
build_static_lib) build_static_lib)
C_ARGS="-O2 -fpic -fdata-sections -ffunction-sections" C_ARGS="-O2 -fpic -fdata-sections -ffunction-sections"
CPP_ARGS="$C_ARGS" CPP_ARGS="$C_ARGS"
PRE_TASK_SCRIPT= PRE_TASK_SCRIPT=""
TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh TASK_SCRIPT="@cbuild/default_tasks/build_static_lib.sh"
POST_TASK_SCRIPT= POST_TASK_SCRIPT=""
;; ;;
# creates static library with debug symbols and no optimizations # creates static library with debug symbols and no optimizations
build_static_lib_dbg) build_static_lib_dbg)
C_ARGS="-O0 -g3" C_ARGS="-O0 -g3"
CPP_ARGS="$C_ARGS" CPP_ARGS="$C_ARGS"
PRE_TASK_SCRIPT= PRE_TASK_SCRIPT=""
TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh TASK_SCRIPT="@cbuild/default_tasks/build_static_lib.sh"
POST_TASK_SCRIPT= POST_TASK_SCRIPT=""
;; ;;
# executes $EXEC_FILE # executes $EXEC_FILE
exec) exec)
TASK_SCRIPT=cbuild/default_tasks/exec.sh TASK_SCRIPT="@cbuild/default_tasks/exec.sh"
;; ;;
# executes $EXEC_FILE with valgrind memory checker # executes $EXEC_FILE with valgrind memory checker
valgrind) valgrind)
VALGRIND_ARGS="-s --read-var-info=yes --track-origins=yes --fullpath-after=$(pwd) --leak-check=full --show-leak-kinds=all" 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 TASK_SCRIPT="@cbuild/default_tasks/valgrind.sh"
;; ;;
# generates profiling info # generates profiling info
profile) profile)
@@ -125,23 +134,26 @@ 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=""
;; ;;
# compiles program with -pg and runs it with gprof # compiles program with -pg and runs it with gprof
# uses gprof2dot python script to generate function call tree (pip install gprof2dot) # uses gprof2dot python script to generate function call tree (pip install gprof2dot)
# requires graphviz (https://www.graphviz.org/download/source/) # requires graphviz (https://www.graphviz.org/download/source/)
gprof) gprof)
OUTDIR="$OUTDIR/gprof" OUTDIR="$OUTDIR/gprof"
# -pg adds code to executable, that generates file containing function call info (gmon.out) # arguments that emit some call counter code and disable optimizations to see function names
C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg" # https://github.com/msys2/MINGW-packages/issues/8503#issuecomment-1365475205
C_ARGS="-O0 -g -pg -no-pie -fno-omit-frame-pointer
-fno-inline-functions -fno-inline-functions-called-once
-fno-optimize-sibling-calls -fopenmp"
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=""
;; ;;
# compiles program and runs it with callgrind (part of valgrind) # compiles program and runs it with callgrind (part of valgrind)
# uses gprof2dot python script to generate function call tree (pip install gprof2dot) # uses gprof2dot python script to generate function call tree (pip install gprof2dot)
@@ -152,30 +164,30 @@ 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=""
;; ;;
# compiles executable with sanitizers and executes it to find errors and warnings # compiles executable with sanitizers and executes it to find errors and warnings
sanitize) sanitize)
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=""
;; ;;
# rebuilds specified dependencies # rebuilds specified dependencies
# EXAMPLE: `cbuild rebuild_dependencies=libexample1,fonts` # EXAMPLE: `cbuild rebuild_dependencies=libexample1,fonts`
# 'all' can be specified to rebuild all dependencies # 'all' can be specified to rebuild all dependencies
rebuild_dependencies) rebuild_dependencies)
TASK_SCRIPT=cbuild/default_tasks/rebuild_dependencies.sh TASK_SCRIPT="@cbuild/default_tasks/rebuild_dependencies.sh"
;; ;;
# deletes generated files # deletes generated files
clean) clean)
TASK_SCRIPT=cbuild/default_tasks/clean.sh TASK_SCRIPT="@cbuild/default_tasks/clean.sh"
;; ;;
# nothing to do # nothing to do
"" | no_task) "" | no_task)

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
# Project user config is ignored by git.
# Here you can add variables that users might want to change
# on their local machine, without commiting to the repository.
# Directory where you install dependencies.
# Do not confuse with DEPENDENCY_CONFIGS_DIR
# Example:
# libexample source code is at `../libexample`, and dependency config
# that specifies how to build this lib is at `dependencies/libexample.config`
DEPENDENCIES_DIR="dependencies"

87
src/VM/Display/Display.c Normal file
View File

@@ -0,0 +1,87 @@
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include "Display.h"
#include "tcpu_version.h"
typedef struct Display {
i32 width;
i32 height;
SDL_Window* window;
SDL_Renderer* renderer;
} Display;
static SDL_InitState _sdl_init_state = {0};
static Display _d = {0};
static cstr _title = "TCPU v" TCPU_VERSION_CSTR;
bool Display_init(i32 w, i32 h, DisplayFlags flags){
(void)flags;
_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(_title, _d.width, _d.height, window_flags, &_d.window, &_d.renderer)){
return false;
}
return true;
}
void Display_destroy(){
SDL_DestroyRenderer(_d.renderer);
SDL_DestroyWindow(_d.window);
// if (SDL_ShouldQuit(&_sdl_init_state)) {
// SDL_Quit();
// SDL_SetInitialized(&_sdl_init_state, false);
// }
}
NULLABLE(cstr) Display_getError(){
return SDL_GetError();
}
bool Display_setSize(u32 w, u32 h){
_d.width = w;
_d.height = h;
return SDL_SetWindowSize(_d.window, w, h);
}
bool Display_setFullScreenMode(bool value){
return SDL_SetWindowFullscreen(_d.window, value);
}
bool Display_setDrawingColor(ColorRGBA color){
return SDL_SetRenderDrawColor(_d.renderer, color.r, color.g, color.b, color.a);
}
bool Display_clear(){
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(Rect rect) {
SDL_FRect sdl_rect;
Rect_copy(sdl_rect, rect);
return SDL_RenderFillRect(_d.renderer, &sdl_rect);
}
bool Display_swapBuffers(){
return SDL_RenderPresent(_d.renderer);
}

30
src/VM/Display/Display.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include "tlibc/std.h"
#include "tlibc/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;
bool Display_init(i32 w, i32 h, DisplayFlags flags);
void Display_destroy();
NULLABLE(cstr) Display_getError();
bool Display_setSize(u32 w, u32 h);
bool Display_setFullScreenMode(bool value);
bool Display_setDrawingColor(ColorRGBA color);
bool Display_clear();
bool Display_fillRect(Rect rect);
bool Display_swapBuffers();

View File

@@ -1,7 +1,7 @@
#include "VM.h" #include "VM.h"
#include "../instructions/instructions.h" #include "instructions/instructions.h"
void VM_init(VM* vm){ void VM_construct(VM* vm){
memset(vm, 0, sizeof(VM)); memset(vm, 0, sizeof(VM));
vm->state = VMState_Initialized; vm->state = VMState_Initialized;
} }
@@ -12,7 +12,7 @@ void _VM_setError(VM* vm, cstr context, cstr format, ...){
sprintf(position_str, "[at 0x%x][", (u32)vm->current_pos); sprintf(position_str, "[at 0x%x][", (u32)vm->current_pos);
char* real_format = strcat_malloc(position_str, context, "] ", format); char* real_format = strcat_malloc(position_str, context, "] ", format);
va_start(argv, format); va_start(argv, format);
char* NULLABLE(buf) = vsprintf_malloc(256, real_format, argv); char* NULLABLE(buf) = vsprintf_malloc(real_format, argv);
va_end(argv); va_end(argv);
free(real_format); free(real_format);
if(buf == NULL){ if(buf == NULL){
@@ -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,16 +72,37 @@ 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){
if(pos + size >= vm->data_size){ if(pos + size >= vm->data_size){
VM_setError(vm, "can't read %lli bytes from 0x%x, because only %lli are avaliable", VM_setError(vm,
"can't read " IFWIN("%lli", "%li") " bytes from 0x%x, because only "
IFWIN("%lli", "%li") " are avaliable",
size, (u32)pos, vm->data_size - size); size, (u32)pos, vm->data_size - 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);
}

View File

@@ -1,29 +1,15 @@
#pragma once #pragma once
#include "../std.h" #include "tlibc/std.h"
#include "tlibc/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;
@@ -37,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
@@ -53,7 +43,7 @@ typedef struct VM {
size_t current_pos; size_t current_pos;
} VM; } VM;
void VM_init(VM* vm); void VM_construct(VM* vm);
/// @brief Loads a program from the buffer. /// @brief Loads a program from the buffer.
/// @param data buffer starting with machine code /// @param data buffer starting with machine code
@@ -65,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)));

View File

@@ -1,20 +0,0 @@
#pragma once
#include "../std.h"
#define Array_declare(T)\
typedef struct Array_##T {\
T* data;\
u32 len;\
} Array_##T;\
\
static inline Array_##T Array_##T##_construct(T* data_ptr, u32 len) {\
return (Array_##T){ .data = data_ptr, .len = len };\
}\
\
static inline Array_##T Array_##T##_alloc(u32 len){\
return Array_##T##_construct(malloc(len * sizeof(T)), len);\
}\
static inline void Array_##T##_realloc(Array_##T* ptr, u32 new_len){\
ptr->data = realloc(ptr->data, new_len * sizeof(T));\
ptr->len = new_len;\
}

View File

@@ -1,5 +0,0 @@
#include "List.h"
List_define(cstr);
List_define(u32);
List_define(u8);

View File

@@ -1,50 +0,0 @@
#pragma once
#include "../std.h"
#define List_declare(T)\
typedef struct List_##T {\
T* data;\
u32 len;\
u32 max_len;\
} List_##T;\
\
static inline List_##T List_##T##_construct(T* data_ptr, u32 len, u32 max_len) {\
return (List_##T){ .data = data_ptr, .len = len, .max_len = max_len };\
}\
\
static inline List_##T List_##T##_alloc(u32 len){\
return List_##T##_construct((T*)(len > 0 ? malloc(len * sizeof(T)) : NULL), 0, 0);\
}\
\
T* List_##T##_expand(List_##T* ptr);\
void List_##T##_push(List_##T* ptr, T value);\
#define List_define(T)\
T* List_##T##_expand(List_##T* ptr){\
if(ptr->len == ptr->max_len){\
u32 max_len = ptr->max_len * 1.5;\
max_len += __List_padding_in_sizeof_T(T);\
/* branchless version of max(max_len, __List_min_size) */\
max_len += (max_len < __List_min_size) * (__List_min_size - max_len);\
ptr->data = (T*)realloc(ptr->data, max_len * sizeof(T));\
ptr->max_len = max_len;\
}\
return &ptr->data[ptr->len++];\
}\
\
void List_##T##_push(List_##T* ptr, T value){\
T* empty_cell_ptr = List_##T##_expand(ptr);\
*empty_cell_ptr = value;\
}\
#define __List_min_size 16
// sizeof(T) == 1 - padding is 7 of sizeof(T)
// sizeof(T) == 2 - padding is 3 of sizeof(T)
// sizeof(T) == 4 - padding is 1 of sizeof(T)
#define __List_padding_in_sizeof_T(T) ((8 - sizeof(T) % 8) / sizeof(T) )
List_declare(cstr);
List_declare(u32);
List_declare(u8);

View File

@@ -1,45 +1,51 @@
#include "AST.h" #include "AST.h"
List_define(Argument); Array_declare(str);
List_define(Operation);
List_define(DataDefinition);
static cstr _ArgumentType_str[] = { static Array(str) _ArgumentType_str_array = ARRAY(str, {
"Unset", STR("Unset"),
"Register", STR("Register"),
"ConstValue", STR("ConstValue"),
"DataName", STR("VarDataName"),
"NamedDataPointer", STR("ConstDataPointer"),
"NamedDataSize", STR("ConstDataSize"),
}; });
cstr ArgumentType_toString(ArgumentType t){ str ArgumentType_toString(ArgumentType t){
if(t >= ARRAY_SIZE(_ArgumentType_str)) if(t >= _ArgumentType_str_array.len)
return "!!INDEX_ERROR!!"; return STR("!!ArgumentType INDEX_ERROR!!");
return _ArgumentType_str[t]; return _ArgumentType_str_array.data[t];
} }
void Section_init(Section* sec, char* name){ void Section_construct(Section* sec, str name){
sec->name = name; sec->name = name;
sec->data = List_DataDefinition_alloc(256); sec->data_definitions_list = List_DataDefinition_alloc(256);
sec->code = List_Operation_alloc(1024); sec->operations_list = List_Operation_alloc(1024);
} }
void Section_free(Section* sec){ void Section_destroy(Section* sec){
free(sec->name); for(u32 i = 0; i < sec->data_definitions_list.len; i++){
free(sec->data.data); DataDefinition* dd = sec->data_definitions_list.data + i;
free(sec->code.data); List_u8_destroy(&dd->data_bytes);
}
List_DataDefinition_destroy(&sec->data_definitions_list);
for(u32 i = 0; i < sec->operations_list.len; i++){
Operation* op = sec->operations_list.data + i;
List_Argument_destroy(&op->args);
}
List_Operation_destroy(&sec->operations_list);
} }
void AST_init(AST* ast){ void AST_construct(AST* ast){
ast->sections = List_Section_alloc(32); ast->sections = List_Section_alloc(32);
} }
void AST_free(AST* ast){ void AST_destroy(AST* ast){
for(u32 i = 0; i != ast->sections.len; i++){ for(u32 i = 0; i != ast->sections.len; i++){
Section_free(&ast->sections.data[i]); Section_destroy(ast->sections.data + i);
} }
free(ast->sections.data); List_Section_destroy(&ast->sections);
} }

View File

@@ -1,60 +1,66 @@
#pragma once #pragma once
#include "../std.h" #include "tlibc/std.h"
#include "../instructions/instructions.h" #include "tlibc/string/str.h"
#include "../collections/List.h" #include "instructions/instructions.h"
#include "instructions/registers.h"
#include "tlibc/collections/List.h"
#include "tlibc/collections/List_impl/List_u8.h"
typedef enum ArgumentType { typedef enum ArgumentType {
ArgumentType_Unset, ArgumentType_Unset,
ArgumentType_Register, ArgumentType_Register,
ArgumentType_ConstValue, ArgumentType_ConstValue,
ArgumentType_DataName, ArgumentType_VarDataName,
ArgumentType_NamedDataPointer, ArgumentType_ConstDataPointer,
ArgumentType_NamedDataSize, ArgumentType_ConstDataSize,
} ArgumentType; } ArgumentType;
cstr ArgumentType_toString(ArgumentType t); str ArgumentType_toString(ArgumentType t);
typedef struct Argument { typedef struct Argument {
ArgumentType type; ArgumentType type;
u32 value; union {
i64 i;
f64 f;
str data_name;
RegisterCode register_code;
} value;
} Argument; } Argument;
List_declare(Argument); List_declare(Argument);
typedef struct Operation { typedef struct Operation {
List_Argument args; List(Argument) args;
Opcode op; Opcode opcode;
} Operation; } Operation;
List_declare(Operation); List_declare(Operation);
typedef struct DataDefinition { typedef struct DataDefinition {
str name;
List(u8) data_bytes;
u32 element_size; u32 element_size;
u32 count;
cstr name;
void* data;
} DataDefinition; } DataDefinition;
List_declare(DataDefinition); List_declare(DataDefinition);
typedef struct Section { typedef struct Section {
char* name; str name;
List_DataDefinition data; List(DataDefinition) data_definitions_list;
List_Operation code; List(Operation) operations_list;
} Section; } Section;
List_declare(Section); List_declare(Section);
void Section_init(Section* Section, char* name); void Section_construct(Section* Section, str name);
void Section_free(Section* Section); void Section_destroy(Section* Section);
typedef struct AST { typedef struct AST {
List_Section sections; List(Section) sections;
} AST; } AST;
void AST_init(AST* ast); void AST_construct(AST* ast);
void AST_free(AST* ast); void AST_destroy(AST* ast);

36
src/compiler/Binary.c Normal file
View File

@@ -0,0 +1,36 @@
#include "Binary.h"
void CompiledSection_construct(CompiledSection* ptr, str name){
ptr->name = name;
ptr->next = NULL;
ptr->offset = 0;
ptr->const_data_props_list = List_ConstDataProps_construct(NULL, 0, 0);
ptr->named_refs = List_NamedRef_construct(NULL, 0, 0);
ptr->bytes = List_u8_alloc(128);
}
void CompiledSection_destroy(CompiledSection* ptr){
List_ConstDataProps_destroy(&ptr->const_data_props_list);
List_NamedRef_destroy(&ptr->named_refs);
List_u8_destroy(&ptr->bytes);
}
void BinaryObject_construct(BinaryObject* ptr){
ptr->comp_sec_list = List_CompiledSection_alloc(64);
HashMap_construct(&ptr->comp_sec_i_map, u32, NULL);
HashMap_construct(&ptr->const_data_props_map, ConstDataProps, NULL);
ptr->main_sec = NULL;
ptr->total_size = 0;
}
void BinaryObject_destroy(BinaryObject* ptr){
for(u32 i = 0; i < ptr->comp_sec_list.len; i++){
CompiledSection* sec_ptr = ptr->comp_sec_list.data + i;
CompiledSection_destroy(sec_ptr);
}
List_CompiledSection_destroy(&ptr->comp_sec_list);
HashMap_destroy(&ptr->comp_sec_i_map);
HashMap_destroy(&ptr->const_data_props_map);
}

64
src/compiler/Binary.h Normal file
View File

@@ -0,0 +1,64 @@
#pragma once
#include "tlibc/std.h"
#include "tlibc/string/str.h"
#include "instructions/instructions.h"
#include "instructions/registers.h"
#include "tlibc/collections/List.h"
#include "tlibc/collections/List_impl/List_u8.h"
#include "tlibc/collections/HashMap.h"
#include "AST.h"
typedef struct CompiledSection CompiledSection;
typedef struct ConstDataProps {
str name;
u32 size; // size in bytes
u32 offset; // offset in bytes from section start
} ConstDataProps;
List_declare(ConstDataProps)
#define ConstDataProps_construct(NAME, SIZE, OFFSET) ((ConstDataProps){ .name = NAME, .size = SIZE, .offset = OFFSET})
typedef enum NamedRefType {
NamedRefType_Unset,
NamedRefType_Ptr,
NamedRefType_Size,
} NamedRefType;
typedef struct NamedRef {
str name;
NamedRefType type;
u32 offset; // offset in bytes from section start
} NamedRef;
List_declare(NamedRef);
#define NamedRef_construct(NAME, TYPE, OFFSET) ((NamedRef){ .name = NAME, .type = TYPE, .offset = OFFSET})
typedef struct CompiledSection {
str name;
CompiledSection* next;
u32 offset;
List(ConstDataProps) const_data_props_list;
List(NamedRef) named_refs;
List(u8) bytes;
} CompiledSection;
List_declare(CompiledSection)
void CompiledSection_construct(CompiledSection* ptr, str name);
void CompiledSection_destroy(CompiledSection* ptr);
typedef struct BinaryObject {
List(CompiledSection) comp_sec_list;
HashMap(u32) comp_sec_i_map;
NULLABLE(CompiledSection*) main_sec;
HashMap(ConstDataProps) const_data_props_map;
u32 total_size;
} BinaryObject;
void BinaryObject_construct(BinaryObject* ptr);
void BinaryObject_destroy(BinaryObject* ptr);

View File

@@ -1,24 +1,25 @@
#include "Compiler_internal.h" #include "Compiler_internal.h"
void Compiler_construct(Compiler* cmp){
void Compiler_init(Compiler* cmp){
memset(cmp, 0, sizeof(Compiler)); memset(cmp, 0, sizeof(Compiler));
cmp->state = CompilerState_Initial; cmp->state = CompilerState_Initial;
cmp->tokens = List_Token_alloc(4096); cmp->tokens = List_Token_alloc(4096);
cmp->line_lengths = List_u32_alloc(1024); cmp->line_lengths = List_u32_alloc(1024);
AST_init(&cmp->ast); AST_construct(&cmp->ast);
BinaryObject_construct(&cmp->binary);
} }
void Compiler_free(Compiler* cmp){ void Compiler_destroy(Compiler* cmp){
free(cmp->code); str_destroy(cmp->code);
free(cmp->tokens.data); List_Token_destroy(&cmp->tokens);
free(cmp->line_lengths.data); List_u32_destroy(&cmp->line_lengths);
AST_free(&cmp->ast); AST_destroy(&cmp->ast);
BinaryObject_destroy(&cmp->binary);
} }
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){ CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
u32 prev_lines_len = 0; u32 prev_lines_len = 0;
if(pos >= cmp->code_len) if(pos >= cmp->code.len)
return CodePos_create(0, 0); return CodePos_create(0, 0);
for(u32 i = 0; i < cmp->line_lengths.len; i++){ for(u32 i = 0; i < cmp->line_lengths.len; i++){
@@ -33,15 +34,15 @@ CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){ void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){
// happens at the end of file // happens at the end of file
if(cmp->pos >= cmp->code_len) if(cmp->pos >= cmp->code.len)
cmp->pos = cmp->code_len - 1; cmp->pos = cmp->code.len - 1;
char position_str[32]; char position_str[32];
CodePos code_pos = Compiler_getLineAndColumn(cmp, cmp->pos); CodePos code_pos = Compiler_getLineAndColumn(cmp, cmp->pos);
sprintf(position_str, "[at %u:%u][", code_pos.line, code_pos.column); sprintf(position_str, "[at %u:%u][", code_pos.line, code_pos.column);
char* real_format = strcat_malloc(position_str, context, "] ", format); char* real_format = strcat_malloc(position_str, context, "] ", format);
va_list argv; va_list argv;
va_start(argv, format); va_start(argv, format);
char* NULLABLE(buf) = vsprintf_malloc(512, real_format, argv); char* NULLABLE(buf) = vsprintf_malloc(real_format, argv);
va_end(argv); va_end(argv);
free(real_format); free(real_format);
if(buf == NULL){ if(buf == NULL){
@@ -56,17 +57,170 @@ void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...){
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\ Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
} }
char* Compiler_extractTokenStr(Compiler* cmp, Token t){ str Compiler_constructTokenStr(Compiler* cmp, Token t){
char* s = malloc(t.length + 1); str s = str_construct((char*)(cmp->code.data + t.begin), t.length, false);
memcpy(s, cmp->code, t.length);
s[t.length] = 0;
return s; return s;
} }
static bool compileFile(Compiler* cmp, FILE* f){ #define List_u8_pushStruct(L_PTR, S_PTR) List_u8_pushMany((L_PTR), (void*)(S_PTR), sizeof(*(S_PTR)))
static bool compileSection(Compiler* cmp, Section* sec){
u32 cs_index = cmp->binary.comp_sec_list.len;
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.comp_sec_list, 1);
CompiledSection_construct(cs, sec->name);
if(!HashMap_tryPush(&cmp->binary.comp_sec_i_map, cs->name, &cs_index)){
returnError("duplicate section '%s'", str_copy(sec->name).data);
}
// compile code
u8 zeroes_8[8];
memset(zeroes_8, 0, sizeof(zeroes_8));
for(u32 i = 0; i < sec->operations_list.len; i++){
Operation* op = sec->operations_list.data + i;
List_u8_pushStruct(&cs->bytes, &op->opcode);
for(u32 j = 0; j < op->args.len; j++){
Argument* arg = op->args.data + j;
switch(arg->type){
case ArgumentType_VarDataName:
returnError("argument type 'VarDataName' is not supported yet");
case ArgumentType_Unset:
returnError("ArgumentType is not set");
default:
returnError("invalid ArgumentType %i", arg->type);
case ArgumentType_Register:
List_u8_push(&cs->bytes, arg->value.register_code);
break;
case ArgumentType_ConstValue:
List_u8_pushStruct(&cs->bytes, &arg->value.i);
break;
case ArgumentType_ConstDataPointer:
List_NamedRef_push(&cs->named_refs,
NamedRef_construct(
arg->value.data_name,
NamedRefType_Ptr,
cs->bytes.len
)
);
List_u8_pushStruct(&cs->bytes, zeroes_8);
break;
case ArgumentType_ConstDataSize:
List_NamedRef_push(&cs->named_refs,
NamedRef_construct(
arg->value.data_name,
NamedRefType_Size,
cs->bytes.len
)
);
List_u8_pushStruct(&cs->bytes, zeroes_8);
break;
}
}
}
// compile data
for(u32 i = 0; i < sec->data_definitions_list.len; i++){
DataDefinition* dd = sec->data_definitions_list.data + i;
ConstDataProps cd_props = ConstDataProps_construct(dd->name, dd->data_bytes.len, cs->bytes.len);
List_ConstDataProps_push(&cs->const_data_props_list, cd_props);
List_u8_pushMany(&cs->bytes, dd->data_bytes.data, dd->data_bytes.len);
}
// TODO: push padding
return true;
}
static bool compileBinary(Compiler* cmp){
returnErrorIf_auto(cmp->state != CompilerState_Parsing); returnErrorIf_auto(cmp->state != CompilerState_Parsing);
cmp->state = CompilerState_Compiling; cmp->state = CompilerState_Compiling;
for(u32 i = 0; i < cmp->ast.sections.len; i++){
Section* sec = cmp->ast.sections.data + i;
if(!compileSection(cmp, sec)){
return false;
}
}
// find main section
str main_sec_name = STR("main");
u32* main_sec_i_ptr = HashMap_tryGetPtr(&cmp->binary.comp_sec_i_map, main_sec_name);
if(main_sec_i_ptr == NULL){
returnError("no 'main' section was defined");
}
u32 main_sec_i = *main_sec_i_ptr;
cmp->binary.main_sec = cmp->binary.comp_sec_list.data + main_sec_i;
// create linked list of CompiledSection where main is the first
CompiledSection* prev_sec = cmp->binary.main_sec;
u32 total_size = 0;
for(u32 i = 0; i < cmp->binary.comp_sec_list.len; i++){
CompiledSection* sec = cmp->binary.comp_sec_list.data + i;
total_size += sec->bytes.len;
bool is_main_sec = str_equals(sec->name, main_sec_name);
if(!is_main_sec){
sec->offset = prev_sec->offset + prev_sec->bytes.len;
}
ConstDataProps cd = ConstDataProps_construct(sec->name, sec->bytes.len, sec->offset);
if(!HashMap_tryPush(&cmp->binary.const_data_props_map, cd.name, &cd)){
returnError("duplicate named data '%s'", str_copy(cd.name).data);
}
for(u32 j = 0; j < sec->const_data_props_list.len; j++){
cd = sec->const_data_props_list.data[j];
cd.offset += sec->offset;
if(!HashMap_tryPush(&cmp->binary.const_data_props_map, cd.name, &cd)){
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
for(u32 i = 0; i < cmp->binary.comp_sec_list.len; i++){
CompiledSection* sec = cmp->binary.comp_sec_list.data + i;
for(u32 j = 0; j < sec->named_refs.len; j++){
NamedRef* ref = sec->named_refs.data + j;
ConstDataProps* target_data = HashMap_tryGetPtr(
&cmp->binary.const_data_props_map, ref->name);
if(target_data == NULL){
returnError("can't find named data '%s'", str_copy(ref->name).data);
}
u64* ref_value_ptr = (void*)((u8*)sec->bytes.data + ref->offset);
switch(ref->type){
default:
returnError("invalid NamedRefType %i", ref->type);
case NamedRefType_Size:
*ref_value_ptr = target_data->size;
break;
case NamedRefType_Ptr:
*ref_value_ptr = target_data->offset;
break;
}
}
}
cmp->binary.total_size = total_size;
return true;
}
static bool writeBinaryFile(Compiler* cmp, FILE* f){
returnErrorIf_auto(cmp->state != CompilerState_Compiling);
CompiledSection* sec = cmp->binary.main_sec;
while(sec){
fwrite(sec->bytes.data, 1, sec->bytes.len, f);
fflush(f);
sec = sec->next;
}
//TODO: print warnings for unused sections
return true; return true;
} }
@@ -75,71 +229,159 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
if(f == NULL) if(f == NULL)
returnError("ERROR: can't open file '%s'", source_file_name); returnError("ERROR: can't open file '%s'", source_file_name);
List_u8 buf = List_u8_alloc(64 * 1024); StringBuilder sb = StringBuilder_alloc(64 * 1024);
int ret; int ret;
while((ret = fgetc(f)) != EOF) { while((ret = fgetc(f)) != EOF) {
List_u8_push(&buf, ret); StringBuilder_append_char(&sb, ret);
} }
if(ferror(f)){ if(ferror(f)){
free(buf.data); StringBuilder_destroy(&sb);
fclose(f); fclose(f);
returnError("can't read file '%s'", source_file_name); returnError("can't read file '%s'", source_file_name);
} }
fclose(f); fclose(f);
if(buf.len == 0){ if(sb.buffer.len == 0){
free(buf.data); StringBuilder_destroy(&sb);
fclose(f);
returnError("soucre file is empty"); returnError("soucre file is empty");
} }
cmp->code = (char*)buf.data; cmp->code = str_copy(StringBuilder_getStr(&sb));
cmp->code_len = buf.len; StringBuilder_destroy(&sb);
List_u8_push(&buf, 0);
f = fopen(out_file_name, "wb"); f = fopen(out_file_name, "wb");
if(f == NULL){ if(f == NULL){
free(buf.data);
returnError("ERROR: can't open file '%s'", out_file_name); returnError("ERROR: can't open file '%s'", out_file_name);
} }
if(debug_log){ if(debug_log){
printf("----------------------------------[%s]---------------------------------\n", source_file_name); printf("===========================[%s]===========================\n", source_file_name);
fputs(cmp->code, stdout); fputs(cmp->code.data, stdout);
fputc('\n', stdout); fputc('\n', stdout);
} }
if(debug_log)
printf("===================================[lexing]===================================\n");
bool success = Compiler_lex(cmp); bool success = Compiler_lex(cmp);
if(debug_log){ if(debug_log){
printf("------------------------------------[lines]-----------------------------------\n"); printf("------------------------------------[lines]------------------------------------\n");
for(u32 i = 0; i < cmp->line_lengths.len; i++){ for(u32 i = 0; i < cmp->line_lengths.len; i++){
printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]); printf("[%u] length: %u\n", i+1, cmp->line_lengths.data[i]);
} }
printf("------------------------------------[tokens]-----------------------------------\n"); printf("------------------------------------[tokens]-----------------------------------\n");
for(u32 i = 0; i < cmp->tokens.len; i++){ for(u32 i = 0; i < cmp->tokens.len; i++){
Token t = cmp->tokens.data[i]; Token t = cmp->tokens.data[i];
CodePos pos = Compiler_getLineAndColumn(cmp, t.begin); CodePos pos = Compiler_getLineAndColumn(cmp, t.begin);
char* tokstr = malloc(4096); char* tokstr = malloc(4096);
strncpy(tokstr, cmp->code + t.begin, t.length); strncpy(tokstr, cmp->code.data + t.begin, t.length);
tokstr[t.length] = 0; tokstr[t.length] = 0;
char* tokstr_stripped = tokstr;
while(*tokstr_stripped == '\r' || *tokstr_stripped == '\n'){
tokstr_stripped++;
}
printf("[l:%3u, c:%3u] %s '%s'\n", printf("[l:%3u, c:%3u] %s '%s'\n",
pos.line, pos.column, pos.line, pos.column,
TokenType_toString(t.type), tokstr); TokenType_toString(t.type).data, tokstr_stripped);
free(tokstr); free(tokstr);
} }
} }
if(!success){ if(!success){
fclose(f); fclose(f);
return false; return false;
} }
if(debug_log)
printf("===================================[parsing]===================================\n");
success = Compiler_parse(cmp); success = Compiler_parse(cmp);
if (debug_log){
printf("-------------------------------------[AST]-------------------------------------\n");
for(u32 i = 0; i < cmp->ast.sections.len; i++){
Section* sec = cmp->ast.sections.data + i;
printf("section '"FMT_str"'\n", sec->name.len, sec->name.data);
for(u32 j = 0; j < sec->data_definitions_list.len; j++){
DataDefinition* dd = sec->data_definitions_list.data + j;
printf(" const%u "FMT_str" (len %u)\n",
dd->element_size * 8,
dd->name.len, dd->name.data,
dd->data_bytes.len/dd->element_size);
}
for(u32 j = 0; j < sec->operations_list.len; j++){
Operation* op = sec->operations_list.data + j;
const Instruction* instr = Instruction_getByOpcode(op->opcode);
if(instr == NULL){
fclose(f);
returnError("unknown opcode: %i", op->opcode)
}
printf(" %s", instr->name.data);
for(u32 k = 0; k < op->args.len; k++){
Argument* arg = op->args.data + k;
printf(" %s(", ArgumentType_toString(arg->type).data);
switch(arg->type){
default:
fclose(f);
returnError("invalid argument type %i", arg->type);
case ArgumentType_Register:;
str register_name = RegisterCode_toString(arg->value.register_code);
printf("%s 0x%x", register_name.data, arg->value.register_code);
str_destroy(register_name);
break;
case ArgumentType_ConstValue:
printf(IFWIN("%lli", "%li"), arg->value.i);
break;
case ArgumentType_ConstDataPointer:
printf("@"FMT_str, arg->value.data_name.len, arg->value.data_name.data);
break;
case ArgumentType_ConstDataSize:
printf("#"FMT_str, arg->value.data_name.len, arg->value.data_name.data);
break;
case ArgumentType_VarDataName:
printf(FMT_str, arg->value.data_name.len, arg->value.data_name.data);
break;
}
printf(")");
}
printf("\n");
}
}
}
if(!success){ if(!success){
fclose(f); fclose(f);
return false; return false;
} }
success = compileFile(cmp, f); if(debug_log)
printf("==================================[compiling]==================================\n");
success = compileBinary(cmp);
if(debug_log){
for(u32 i = 0; i < cmp->binary.comp_sec_list.len; i++){
CompiledSection* sec = cmp->binary.comp_sec_list.data + i;
printf("compiled section '"FMT_str"' to %u bytes with offset 0x%x\n",
sec->name.len, sec->name.data, sec->bytes.len, sec->offset);
}
}
if(!success){
fclose(f);
return false;
}
if(debug_log)
printf("----------------------------[writing output to file]---------------------------\n");
success = writeBinaryFile(cmp, f);
fclose(f); fclose(f);
if(success){ if(success){
cmp->state = CompilerState_Success; cmp->state = CompilerState_Success;

View File

@@ -1,8 +1,11 @@
#pragma once #pragma once
#include "../std.h" #include "tlibc/std.h"
#include "../collections/List.h" #include "tlibc/string/str.h"
#include "tlibc/collections/List.h"
#include "tlibc/collections/List_impl/List_u32.h"
#include "tlibc/collections/HashMap.h"
#include "Token.h" #include "Token.h"
#include "AST.h" #include "Binary.h"
typedef enum CompilerState { typedef enum CompilerState {
CompilerState_Initial, CompilerState_Initial,
@@ -13,21 +16,26 @@ typedef enum CompilerState {
CompilerState_Success CompilerState_Success
} CompilerState; } CompilerState;
typedef struct Compiler { typedef struct Compiler {
char* code; /* general fields */
u32 code_len; str code;
u32 column; // > 0 if code parsing started u32 column; // > 0 if code parsing started
u32 pos; u32 pos;
CompilerState state; CompilerState state;
NULLABLE(char* error_message); NULLABLE(char* error_message);
List_Token tokens; /* lexer fields */
List_u32 line_lengths; List(Token) tokens;
List(u32) line_lengths;
/* parser fields */
AST ast; AST ast;
u32 tok_i; u32 tok_i;
/* compiler fields */
BinaryObject binary;
} Compiler; } Compiler;
void Compiler_init(Compiler* cmp); void Compiler_construct(Compiler* cmp);
void Compiler_free(Compiler* cmp); void Compiler_destroy(Compiler* cmp);
/// @brief compile assembly language code to machine code /// @brief compile assembly language code to machine code
/// @return true if no errors, false if any error occured (check cmp->error_message) /// @return true if no errors, false if any error occured (check cmp->error_message)

View File

@@ -1,4 +1,5 @@
#include "Compiler.h" #include "Compiler.h"
#include "tlibc/string/StringBuilder.h"
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4))); void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
@@ -23,7 +24,7 @@ typedef struct CodePos {
/// @param pos index in code buffer /// @param pos index in code buffer
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos); CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos);
char* Compiler_extractTokenStr(Compiler* cmp, Token t); str Compiler_constructTokenStr(Compiler* cmp, Token t);
bool Compiler_lex(Compiler* cmp); bool Compiler_lex(Compiler* cmp);
bool Compiler_parse(Compiler* cmp); bool Compiler_parse(Compiler* cmp);

View File

@@ -19,8 +19,8 @@ static void readCommentSingleLine(Compiler* cmp){
cmp->column++; cmp->column++;
cmp->pos++; cmp->pos++;
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code.len){
c = cmp->code[cmp->pos]; c = cmp->code.data[cmp->pos];
// end of line // end of line
if(c == '\r' || c == '\n'){ if(c == '\r' || c == '\n'){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
@@ -44,10 +44,10 @@ static void readCommentMultiLine(Compiler* cmp){
cmp->column++; cmp->column++;
cmp->pos++; cmp->pos++;
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code.len){
c = cmp->code[cmp->pos]; c = cmp->code.data[cmp->pos];
// closing comment // closing comment
if(cmp->pos > tok.begin + 3 && c == '/' && cmp->code[cmp->pos - 1] == '*') { if(cmp->pos > tok.begin + 3 && c == '/' && cmp->code.data[cmp->pos - 1] == '*') {
tok.length = cmp->pos - tok.begin + 1; tok.length = cmp->pos - tok.begin + 1;
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
return; return;
@@ -65,14 +65,14 @@ static void readCommentMultiLine(Compiler* cmp){
static void readComment(Compiler* cmp){ static void readComment(Compiler* cmp){
char c; // '/' char c; // '/'
if(cmp->pos + 1 == cmp->code_len){ if(cmp->pos + 1 == cmp->code.len){
setError(Error_endOfFile); setError(Error_endOfFile);
return; return;
} }
c = cmp->code[cmp->pos + 1]; c = cmp->code.data[cmp->pos + 1];
if(c == '\r' || c == '\n'){ if(c == '\r' || c == '\n'){
setError(Error_unexpectedCharacter(cmp->code[--cmp->pos])); setError(Error_unexpectedCharacter(cmp->code.data[--cmp->pos]));
return; return;
} }
@@ -91,19 +91,19 @@ static void readLabel(Compiler* cmp){
cmp->column++; cmp->column++;
Token tok = Token_construct(TokenType_Label, cmp->pos, 0); Token tok = Token_construct(TokenType_Label, cmp->pos, 0);
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code.len){
c = cmp->code[cmp->pos]; c = cmp->code.data[cmp->pos];
// end of line // end of line
if(c == ':' || c == '\r' || c == '\n'){ if(c == ':' || c == '\r' || c == '\n'){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
if(tok.length > 0) if(tok.length > 0)
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
else setError(Error_unexpectedCharacter(cmp->code[--cmp->pos])); else setError(Error_unexpectedCharacter(cmp->code.data[--cmp->pos]));
// cmp->line will be increased in lex() // cmp->line will be increased in lex()
return; return;
} }
if(!isAlphabeticalLower(c) && !isAlphabeticalUpper(c) && !isDigit(c) && if(!char_isLatinLower(c) && !char_isLatinUpper(c) && !char_isDigit(c) &&
c != '_' && c != '.'){ c != '_' && c != '.'){
setError(Error_unexpectedCharacter(c)); setError(Error_unexpectedCharacter(c));
return; return;
@@ -125,12 +125,12 @@ static void readArguments(Compiler* cmp){
Token tok = Token_construct(TokenType_Unset, cmp->pos, 0); Token tok = Token_construct(TokenType_Unset, cmp->pos, 0);
char quot = '\0'; // quotation character of a string value char quot = '\0'; // quotation character of a string value
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code.len){
c = cmp->code[cmp->pos]; c = cmp->code.data[cmp->pos];
// string argument reading // string argument reading
if(quot != '\0'){ if(quot != '\0'){
if(c == quot && cmp->code[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'){
@@ -139,7 +139,7 @@ static void readArguments(Compiler* cmp){
} }
} }
// end of line // end of operation
else if(c == '\r' || c == '\n' || c == ';'){ else if(c == '\r' || c == '\n' || c == ';'){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
if(tok.length > 0) if(tok.length > 0)
@@ -169,7 +169,7 @@ static void readArguments(Compiler* cmp){
tok.type = TokenType_NamedDataPointer; tok.type = TokenType_NamedDataPointer;
else if(c == '#') else if(c == '#')
tok.type = TokenType_NamedDataSize; tok.type = TokenType_NamedDataSize;
else if(isDigit(c)) else if(char_isDigit(c))
tok.type = TokenType_Number; tok.type = TokenType_Number;
else tok.type = TokenType_Name; else tok.type = TokenType_Name;
} }
@@ -189,12 +189,14 @@ static void readInstruction(Compiler* cmp){
cmp->pos++; cmp->pos++;
cmp->column++; cmp->column++;
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code.len){
char c = cmp->code[cmp->pos]; char c = cmp->code.data[cmp->pos];
// end of line // end of line
if(c == '\r' || c == '\n' || c == ';'){ if(c == '\r' || c == '\n' || c == ';'){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
tok = Token_construct(TokenType_OperationEnd, cmp->pos, 1);
List_Token_push(&cmp->tokens, tok);
// cmp->line will be increased in lex() // cmp->line will be increased in lex()
return; return;
} }
@@ -204,10 +206,12 @@ static void readInstruction(Compiler* cmp){
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
readArguments(cmp); readArguments(cmp);
tok = Token_construct(TokenType_OperationEnd, cmp->pos, 1);
List_Token_push(&cmp->tokens, tok);
return; return;
} }
if(!isAlphabeticalLower(c) && !isAlphabeticalUpper(c) && !isDigit(c)){ if(!char_isLatinLower(c) && !char_isLatinUpper(c) && !char_isDigit(c)){
setError(Error_unexpectedCharacter(c)); setError(Error_unexpectedCharacter(c));
return; return;
} }
@@ -219,6 +223,8 @@ static void readInstruction(Compiler* cmp){
// end of file // end of file
tok.length = cmp->pos - tok.begin; tok.length = cmp->pos - tok.begin;
List_Token_push(&cmp->tokens, tok); List_Token_push(&cmp->tokens, tok);
tok = Token_construct(TokenType_OperationEnd, cmp->pos, 1);
List_Token_push(&cmp->tokens, tok);
} }
bool Compiler_lex(Compiler* cmp){ bool Compiler_lex(Compiler* cmp){
@@ -226,8 +232,8 @@ bool Compiler_lex(Compiler* cmp){
cmp->state = CompilerState_Lexing; cmp->state = CompilerState_Lexing;
cmp->column = 1; cmp->column = 1;
while(cmp->pos < cmp->code_len){ while(cmp->pos < cmp->code.len){
char c = cmp->code[cmp->pos]; char c = cmp->code.data[cmp->pos];
switch(c){ switch(c){
// skip blank characters // skip blank characters
case ' ': case '\t': case '\r': case '\n': case ' ': case '\t': case '\r': case '\n':
@@ -242,7 +248,7 @@ bool Compiler_lex(Compiler* cmp){
break; break;
default: default:
// try read instruction // try read instruction
if(isAlphabeticalLower(c) || isAlphabeticalUpper(c)) if(char_isLatinLower(c) || char_isLatinUpper(c))
readInstruction(cmp); readInstruction(cmp);
else returnError(Error_unexpectedCharacter(c)); else returnError(Error_unexpectedCharacter(c));
break; break;
@@ -251,7 +257,7 @@ bool Compiler_lex(Compiler* cmp){
if(cmp->state == CompilerState_Error) if(cmp->state == CompilerState_Error)
return false; return false;
c = cmp->code[cmp->pos]; c = cmp->code.data[cmp->pos];
if(c == '\n') if(c == '\n')
completeLine(cmp); completeLine(cmp);
cmp->column++; cmp->column++;

View File

@@ -6,19 +6,232 @@
} }
#define setError_unexpectedToken(T) {\ #define setError_unexpectedToken(T) {\
char* tok_str = Compiler_extractTokenStr(cmp, T);\ str tok_str_tmp = Compiler_constructTokenStr(cmp, T);\
setError("unexpected character '%s'", tok_str);\ cmp->pos = T.begin;\
free(tok_str);\ Compiler_setError(cmp, "unexpected token '"FMT_str"'", tok_str_tmp.len, tok_str_tmp.data);\
}
#define setError_unexpectedTokenChar(T, I) {\
cmp->pos = T.begin + I;\
Compiler_setError(cmp, "unexpected character '%c'", cmp->code.data[cmp->pos]);\
}
#define setError_unexpectedInstruction(T) {\
str tok_str_tmp = Compiler_constructTokenStr(cmp, T);\
cmp->pos = T.begin;\
Compiler_setError(cmp, "unexpected instruction '"FMT_str"'", tok_str_tmp.len, tok_str_tmp.data);\
}
#define Error_TokenUnset "token of undefined type"
#define Error_BitSize "invalid size in bits"
static inline bool isVarSizeBits(u32 B) { return (B == 8 || B == 16 || B == 32 || B == 64); }
static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
StringBuilder sb = StringBuilder_alloc(src.len);
char c;
bool escaped = false;
for(u32 i = 0; i < src.len; i++){
c = src.data[i];
if(c == '\\'){
escaped = !escaped;
if(escaped)
continue;
}
if(!escaped){
StringBuilder_append_char(&sb, c);
continue;
}
// escape codes
switch(c){
case '0':
StringBuilder_append_char(&sb, '\0');
break;
case 'n':
StringBuilder_append_char(&sb, '\n');
break;
case 'r':
StringBuilder_append_char(&sb, '\r');
break;
case 't':
StringBuilder_append_char(&sb, '\t');
break;
case 'e':
StringBuilder_append_char(&sb, '\e');
break;
case '"':
case '\'':
StringBuilder_append_char(&sb, c);
break;
default:
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
StringBuilder_destroy(&sb);
return str_null;
}
escaped = false;
}
return StringBuilder_getStr(&sb);
}
static void parseDataDefinition(Compiler* cmp, str instr_name, DataDefinition* ddf){
i32 _element_size_bits;
str _instr_name_zero_terminated = str_copy(instr_name);
if(sscanf(_instr_name_zero_terminated.data, "const%i", &_element_size_bits) != 1 || !isVarSizeBits(_element_size_bits)){
str_destroy(_instr_name_zero_terminated);
setError(Error_BitSize);
return;
}
str_destroy(_instr_name_zero_terminated);
ddf->element_size = _element_size_bits / 8;
ddf->data_bytes = List_u8_alloc(32);
Token tok = cmp->tokens.data[++cmp->tok_i];
if(tok.type != TokenType_Name){
setError_unexpectedToken(tok);
return;
}
str tok_str = Compiler_constructTokenStr(cmp, tok);
str processed_str = str_null;
ddf->name = tok_str;
while(++cmp->tok_i < cmp->tokens.len){
tok = cmp->tokens.data[cmp->tok_i];
switch(tok.type){
case TokenType_SingleLineComment:
case TokenType_MultiLineComment:
// skip comments
break;
case TokenType_OperationEnd:
return;
case TokenType_Unset:
setError(Error_TokenUnset);
return;
default:
setError_unexpectedToken(tok);
return;
case TokenType_Number:
tok_str = Compiler_constructTokenStr(cmp, tok);
processed_str = str_copy(tok_str);
if(str_seekChar(tok_str, '.', 0) != -1){
f64 f = atof(processed_str.data);
// for numbers smaller than 64 bits
u8* value_part = (u8*)&f + 8 - ddf->element_size;
List_u8_pushMany(&ddf->data_bytes, value_part, ddf->element_size);
}
else {
i64 i = atoll(processed_str.data);
// for numbers smaller than 64 bits
u8* value_part = (u8*)&i + 8 - ddf->element_size;
List_u8_pushMany(&ddf->data_bytes, value_part, ddf->element_size);
}
str_destroy(processed_str);
break;
case TokenType_Char:
tok.begin += 1;
tok.length -= 2;
tok_str = Compiler_constructTokenStr(cmp, tok);
processed_str = resolveEscapeSequences(cmp, tok_str);
if(processed_str.len != ddf->element_size){
setError("can't fit char of size %i in %u bit variable", processed_str.len, _element_size_bits);
return;
}
List_u8_pushMany(&ddf->data_bytes, (u8*)processed_str.data, processed_str.len);
str_destroy(processed_str);
break;
case TokenType_String:
tok.begin += 1;
tok.length -= 2;
tok_str = Compiler_constructTokenStr(cmp, tok);
processed_str = resolveEscapeSequences(cmp, tok_str);
List_u8_pushMany(&ddf->data_bytes, (u8*)processed_str.data, processed_str.len);
str_destroy(processed_str);
break;
}
}
} }
static void parseDataDefinition(Compiler* cmp, char* instr_name, DataDefinition* dataDefPtr){ static void parseOperation(Compiler* cmp, str instr_name, Operation* operPtr){
Token tok = cmp->tokens.data[cmp->tok_i];
const Instruction* instr = Instruction_getByName(instr_name);
if(instr == NULL){
setError_unexpectedInstruction(tok);
return;
} }
operPtr->opcode = instr->opcode;
operPtr->args = List_Argument_alloc(4);
Argument arg = (Argument){ .type = ArgumentType_Unset, .value.i = 0 };
str tok_str = str_null;
str processed_str = str_null;
while(++cmp->tok_i < cmp->tokens.len){
tok = cmp->tokens.data[cmp->tok_i];
switch(tok.type){
case TokenType_SingleLineComment:
case TokenType_MultiLineComment:
// skip comments
break;
static void parseOperation(Compiler* cmp, char* instr_name, Operation* operPtr){ case TokenType_OperationEnd:
return;
case TokenType_Unset:
setError(Error_TokenUnset);
return;
default:
setError_unexpectedToken(tok);
return;
case TokenType_Number:
arg.type = ArgumentType_ConstValue;
tok_str = Compiler_constructTokenStr(cmp, tok);
processed_str = str_copy(tok_str);
if(str_seekChar(tok_str, '.', 0) != -1){
arg.value.f = atof(processed_str.data);
}
else {
arg.value.i = atoll(processed_str.data);
}
str_destroy(processed_str);
List_Argument_push(&operPtr->args, arg);
break;
case TokenType_Name:
tok_str = Compiler_constructTokenStr(cmp, tok);
arg.value.register_code = RegisterCode_parse(tok_str);
if(arg.value.register_code != RegisterCode_Unset){
arg.type = ArgumentType_Register;
}
else {
arg.type = ArgumentType_VarDataName;
arg.value.data_name = tok_str;
}
List_Argument_push(&operPtr->args, arg);
break;
case TokenType_NamedDataPointer:
tok_str = Compiler_constructTokenStr(cmp, tok);
tok_str.data++;
tok_str.len--;
arg.type = ArgumentType_ConstDataPointer;
arg.value.data_name = tok_str;
List_Argument_push(&operPtr->args, arg);
break;
case TokenType_NamedDataSize:
tok_str = Compiler_constructTokenStr(cmp, tok);
tok_str.data++;
tok_str.len--;
arg.type = ArgumentType_ConstDataSize;
arg.value.data_name = tok_str;
List_Argument_push(&operPtr->args, arg);
break;
}
}
} }
bool Compiler_parse(Compiler* cmp){ bool Compiler_parse(Compiler* cmp){
@@ -31,25 +244,29 @@ bool Compiler_parse(Compiler* cmp){
tok = cmp->tokens.data[cmp->tok_i]; tok = cmp->tokens.data[cmp->tok_i];
switch(tok.type){ switch(tok.type){
case TokenType_Unset: case TokenType_Unset:
returnError("token of undefined type"); returnError(Error_TokenUnset);
case TokenType_SingleLineComment: case TokenType_SingleLineComment:
case TokenType_MultiLineComment: case TokenType_MultiLineComment:
// skip comments // skip comments
break; break;
case TokenType_Label: case TokenType_Label:
// create new section // create new section
sec = List_Section_expand(&cmp->ast.sections); sec = List_Section_expand(&cmp->ast.sections, 1);
Section_init(sec, Compiler_extractTokenStr(cmp, tok)); Section_construct(sec, Compiler_constructTokenStr(cmp, tok));
break; break;
case TokenType_Instruction: case TokenType_Instruction:
char* instr_name = Compiler_extractTokenStr(cmp, tok); if(sec == NULL)
returnError("no section");
str instr_name = Compiler_constructTokenStr(cmp, tok);
// data definition starts with const // data definition starts with const
if(cstr_seek(instr_name, "const", 0, 1)){ if(str_startsWith(instr_name, STR("const"))){
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data); DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data_definitions_list, 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); Operation* operPtr = List_Operation_expand(&sec->operations_list, 1);
memset(operPtr, 0, sizeof(Operation));
parseOperation(cmp, instr_name, operPtr); parseOperation(cmp, instr_name, operPtr);
} }
break; break;

View File

@@ -1,23 +1,24 @@
#include "Token.h" #include "Token.h"
List_define(Token); Array_declare(str);
static cstr _TokenType_str[] = { static Array(str) _TokenType_str_array = ARRAY(str, {
"Unset", STR("Unset"),
"SingleLineComment", STR("SingleLineComment"),
"MultiLineComment", STR("MultiLineComment"),
"Instruction", STR("Instruction"),
"Label", STR("Label"),
"Number", STR("Number"),
"Char", STR("Char"),
"String", STR("String"),
"Name", STR("Name"),
"NamedDataPointer", STR("NamedDataPointer"),
"NamedDataSize" STR("NamedDataSize"),
}; STR("OperationEnd"),
});
cstr TokenType_toString(TokenType t){ str TokenType_toString(TokenType t){
if(t >= ARRAY_SIZE(_TokenType_str)) if(t >= _TokenType_str_array.len)
return "!!INDEX_ERROR!!"; return STR("!!TokenType INDEX_ERROR!!");
return _TokenType_str[t]; return _TokenType_str_array.data[t];
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "../std.h" #include "tlibc/std.h"
#include "../collections/List.h" #include "tlibc/string/str.h"
#include "tlibc/collections/List.h"
typedef enum TokenType { typedef enum TokenType {
TokenType_Unset, // initial value TokenType_Unset, // initial value
@@ -13,10 +14,11 @@ typedef enum TokenType {
TokenType_String, // "aaaa" TokenType_String, // "aaaa"
TokenType_Name, // xyz TokenType_Name, // xyz
TokenType_NamedDataPointer, // @xyz TokenType_NamedDataPointer, // @xyz
TokenType_NamedDataSize // #xyz TokenType_NamedDataSize, // #xyz
TokenType_OperationEnd, // EOL or EOF or ;
} TokenType; } TokenType;
cstr TokenType_toString(TokenType t); str TokenType_toString(TokenType t);
typedef struct Token { typedef struct Token {
u32 begin; // some index in Compiler->code u32 begin; // some index in Compiler->code
@@ -24,6 +26,6 @@ typedef struct Token {
TokenType type : 8; // type of token (8 bits) TokenType type : 8; // type of token (8 bits)
} Token; } Token;
List_declare(Token); List_declare(Token)
#define Token_construct(TYPE, BEGIN, END) ((Token){ .type = TYPE, .begin = BEGIN, .length = END }) #define Token_construct(TYPE, BEGIN, LEN) ((Token){ .type = TYPE, .begin = BEGIN, .length = LEN })

View File

@@ -1,34 +0,0 @@
#pragma once
#include "../std.h"
#include "../collections/List.h"
#include "Token.h"
#include "AST.h"
typedef enum CompilerState {
CompilerState_Initial,
CompilerState_Lexing,
CompilerState_Parsing,
CompilerState_Compiling,
CompilerState_Error,
CompilerState_Success
} CompilerState;
typedef struct Compiler {
char* code;
u32 code_len;
u32 column; // > 0 if code parsing started
u32 pos;
CompilerState state;
NULLABLE(char* error_message);
List_Token tokens;
List_u32 line_lengths;
AST ast;
u32 tok_i;
} Compiler;
void Compiler_init(Compiler* cmp);
void Compiler_free(Compiler* cmp);
/// @brief compile assembly language code to machine code
/// @return true if no errors, false if any error occured (check cmp->error_message)
bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, bool debug);

View File

@@ -1,29 +0,0 @@
#include "Compiler.h"
void _Compiler_setError(Compiler* cmp, cstr context, cstr format, ...) __attribute__((__format__(__printf__, 3, 4)));
#define Compiler_setError(cmp, format, ...) _Compiler_setError(cmp, __func__, format ,##__VA_ARGS__)
#define returnError(FORMAT, ...) {\
setError(FORMAT, ##__VA_ARGS__);\
return false;\
}
#define returnErrorIf(STATEMENT, FORMAT, ...) if(STATEMENT) returnError(FORMAT, ##__VA_ARGS__)
#define returnErrorIf_auto(STATEMENT) returnErrorIf(STATEMENT, #STATEMENT)
typedef struct CodePos {
u32 line; // 0 on error
u32 column; // 0 on error
} CodePos;
#define CodePos_create(L, C) ((CodePos){ .line = L, .column = C })
/// @param pos index in code buffer
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos);
char* Compiler_extractTokenStr(Compiler* cmp, Token t);
bool Compiler_lex(Compiler* cmp);
bool Compiler_parse(Compiler* cmp);

View File

@@ -1,68 +0,0 @@
#include "Compiler_internal.h"
#define setError(FORMAT, ...) {\
cmp->pos = cmp->tokens.data[cmp->tok_i].begin;\
Compiler_setError(cmp, FORMAT, ##__VA_ARGS__);\
}
#define setError_unexpectedToken(T) {\
char* tok_str = Compiler_extractTokenStr(cmp, T);\
setError("unexpected character '%s'", tok_str);\
free(tok_str);\
}
static void parseDataDefinition(Compiler* cmp, char* instr_name, DataDefinition* dataDefPtr){
}
static void parseOperation(Compiler* cmp, char* instr_name, Operation* operPtr){
}
bool Compiler_parse(Compiler* cmp){
returnErrorIf_auto(cmp->state != CompilerState_Lexing);
cmp->state = CompilerState_Parsing;
Token tok;
Section* sec = NULL;
while(cmp->tok_i < cmp->tokens.len){
tok = cmp->tokens.data[cmp->tok_i];
switch(tok.type){
case TokenType_Unset:
returnError("token of undefined type");
case TokenType_SingleLineComment:
case TokenType_MultiLineComment:
// skip comments
break;
case TokenType_Label:
// create new section
sec = List_Section_expand(&cmp->ast.sections);
Section_init(sec, Compiler_extractTokenStr(cmp, tok));
break;
case TokenType_Instruction:
char* instr_name = Compiler_extractTokenStr(cmp, tok);
// data definition starts with const
if(cstr_seek(instr_name, "const", 0, 1)){
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data);
parseDataDefinition(cmp, instr_name, dataDefPtr);
}
else {
Operation* operPtr = List_Operation_expand(&sec->code);
parseOperation(cmp, instr_name, operPtr);
}
break;
default:
setError_unexpectedToken(tok);
return false;
}
if(cmp->state == CompilerState_Error)
return false;
cmp->tok_i++;
}
return true;
}

View File

@@ -1,23 +0,0 @@
#include "Token.h"
List_define(Token);
static cstr _TokenType_str[] = {
"Unset",
"SingleLineComment",
"MultiLineComment",
"Instruction",
"Label",
"Number",
"Char",
"String",
"Name",
"NamedDataPointer",
"NamedDataSize"
};
cstr TokenType_toString(TokenType t){
if(t >= ARRAY_SIZE(_TokenType_str))
return "!!INDEX_ERROR!!";
return _TokenType_str[t];
}

View File

@@ -1,29 +0,0 @@
#pragma once
#include "../std.h"
#include "../collections/List.h"
typedef enum TokenType {
TokenType_Unset, // initial value
TokenType_SingleLineComment, // //comment
TokenType_MultiLineComment, // /* comment */
TokenType_Instruction, // abc
TokenType_Label, // .abc:
TokenType_Number, // 0123
TokenType_Char, // 'A'
TokenType_String, // "aaaa"
TokenType_Name, // xyz
TokenType_NamedDataPointer, // @xyz
TokenType_NamedDataSize // #xyz
} TokenType;
cstr TokenType_toString(TokenType t);
typedef struct Token {
u32 begin; // some index in Compiler->code
u32 length : 24; // length in characters (24 bits)
TokenType type : 8; // type of token (8 bits)
} Token;
List_declare(Token);
#define Token_construct(TYPE, BEGIN, END) ((Token){ .type = TYPE, .begin = BEGIN, .length = END })

View File

@@ -1,92 +0,0 @@
#include "std.h"
char* _strcat_malloc(size_t n, cstr str0, ...){
va_list argv;
va_start(argv, str0);
char* heap_ptr = _vstrcat_malloc(n, str0, argv);
va_end(argv);
return heap_ptr;
}
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv){
size_t str0_len = strlen(str0);
size_t total_len = str0_len;
cstr* const parts = malloc(sizeof(cstr) * n);
size_t* const part_lengths = malloc(sizeof(size_t) * n);
for(size_t i = 0; i < n; i++){
cstr part = va_arg(argv, cstr);
size_t length = strlen(part);
parts[i] = part;
part_lengths[i] = length;
total_len += length;
}
char* const buf = malloc(total_len + 1);
memcpy(buf, str0, str0_len);
char* walking_ptr = buf + str0_len;
for(size_t i = 0; i < n; i++){
memcpy(walking_ptr, parts[i], part_lengths[i]);
walking_ptr += part_lengths[i];
}
buf[total_len] = '\0';
free(parts);
free(part_lengths);
return buf;
}
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...){
va_list argv;
va_start(argv, format);
char* NULLABLE(heap_ptr) = vsprintf_malloc(buffer_size, format, argv);
va_end(argv);
return heap_ptr;
}
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv){
char* buf = malloc(buffer_size);
int r = vsprintf_s(buf, buffer_size, format, argv);
if(r < 0){
free(buf);
return NULL;
}
return buf;
}
i32 cstr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength){
char sc = *src, fc = *fragment;
if(sc == 0 || fc == 0)
return -1;
u32 fr_start = startIndex;
for(u32 si = startIndex; si-startIndex < seekLength && sc != 0; si++){
sc = src[si];
fc = fragment[si-fr_start];
if(fc == 0)
return fr_start;
if(sc != fc)
fr_start++;
}
return -1;
}
i32 cstr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength){
char sc = *src, fc = *fragment;
if(sc == 0 || fc == 0)
return -1;
i32 len = strlen(src);
if(startIndex == (u32)-1)
startIndex = len-1;
u32 fr_len = strlen(fragment);
for(u32 si = startIndex; si < (u32)-1 && si != (len - seekLength - 1); si--){
if(si + 1 < fr_len)
return -1;
sc = src[si];
fc = fragment[0];
u32 fr_start = si;
for(u32 fi = 0; fc == sc ; fi++){
if(fi == fr_len)
return fr_start;
fc = fragment[fi];
sc = src[si--];
}
}
return -1;
}

View 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);
}

View File

@@ -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);
}

View 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);
}

View 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);
}

View File

@@ -2,5 +2,6 @@
/// NOP /// NOP
i32 NOP_impl(VM* vm){ i32 NOP_impl(VM* vm){
(void)vm;
return 0; return 0;
} }

View File

@@ -1,16 +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);
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;
}

View File

@@ -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;
} }

View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include "../instructions.h" #include "instructions/instructions.h"
#include "instructions/registers.h"
#define readVar(VAR) {\ #define readVar(VAR) {\
if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \ if(!VM_dataRead(vm, &VAR, vm->current_pos, sizeof(VAR))) \
@@ -7,26 +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);\
validateRegisterIndex(VAR);\ validateRegisterCode(VAR);\
} }
#define validateValueSize(VAR) {\ #define OPERATOR_IMPL_1(NAME, OPERATOR)\
if(VAR < 1 || VAR > 4){\ i32 NAME##_impl (VM* vm) {\
VM_setError(vm, "invalid value_size (%x)", VAR);\ RegisterCode dst_reg_code = 0;\
return -1;\ readRegisterCode(dst_reg_code);\
}\ 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);\
} }

View 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, >=)

View File

@@ -1,47 +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);\
\
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(%)
}

View File

@@ -1,37 +1,102 @@
#include "instructions.h" #include "instructions.h"
#include "tlibc/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);
const Instruction instructions[] = { 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){
if(opcode >= ARRAY_SIZE(instructions)) if((u32)opcode >= instructions_array.len)
return NULL; return NULL;
return instructions + opcode; return (Instruction*)instructions_array.data + opcode;
}
static HashMap(Opcode)* opcode_map = NULL;
static void _opcode_map_construct(){
opcode_map = malloc(sizeof(*opcode_map));
HashMap_construct(opcode_map, Opcode, NULL);
for(u32 i = 0; i < instructions_array.len; i++){
Instruction* instr_ptr = (Instruction*)instructions_array.data + i;
HashMap_tryPush(opcode_map, instr_ptr->name, &instr_ptr->opcode);
}
}
const Instruction* Instruction_getByName(str name){
if(opcode_map == NULL)
_opcode_map_construct();
str name_upper = str_toUpper(name);
Opcode* op_ptr = HashMap_tryGetPtr(opcode_map, name_upper);
str_destroy(name_upper);
if(op_ptr == NULL)
return NULL;
return Instruction_getByOpcode(*op_ptr);
}
void Instruction_destroySearchStructs(){
if(opcode_map != NULL){
HashMap_destroy(opcode_map);
free(opcode_map);
}
} }

View File

@@ -1,5 +1,5 @@
#pragma once #pragma once
#include "../VM/VM.h" #include "VM/VM.h"
///@param program_pos position in vm->program next afrer opcode ///@param program_pos position in vm->program next afrer opcode
///@returns number of bytes read ///@returns number of bytes read
@@ -7,25 +7,43 @@ 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 {
cstr name; str name;
InstructionImplFunc_t implementation; InstructionImplFunc_t implementation;
Opcode opcode; Opcode opcode;
} Instruction; } Instruction;
#define Instruction_construct(NAME) {\ #define Instruction_construct(NAME) {\
.name = #NAME, \ .name = STR(#NAME), \
.implementation = NAME##_impl, \ .implementation = NAME##_impl, \
.opcode = Opcode_##NAME\ .opcode = Opcode_##NAME\
} }
@@ -34,3 +52,5 @@ typedef struct Instruction {
/// @param opcode any byte /// @param opcode any byte
/// @return ptr to struct or NULL /// @return ptr to struct or NULL
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode); const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
const Instruction* NULLABLE(Instruction_getByName)(str name);
void Instruction_destroySearchStructs();

View File

@@ -0,0 +1,76 @@
#include "registers.h"
#define check_code(R) if(str_equals(lower, STR(#R))) code = RegisterCode_##R;
RegisterCode RegisterCode_parse(str r){
str lower = str_toLower(r);
RegisterCode code = RegisterCode_Unset;
// a
check_code(rax)
else check_code(eax)
else check_code(ax)
else check_code(al)
else check_code(ah)
// 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)
str_destroy(lower);
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);
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include "tlibc/std.h"
#include "tlibc/string/str.h"
typedef enum RegisterCode {
RegisterCode_Unset = 0,
RegisterCode_rax = 0x01,
RegisterCode_eax = 0x02,
RegisterCode_ax = 0x04,
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);
/// @return allocated string
str RegisterCode_toString(RegisterCode code);

View File

@@ -1,9 +1,11 @@
#include "VM/VM.h" #include "VM/VM.h"
#include "instructions/instructions.h" #include "instructions/instructions.h"
#include "collections/List.h"
#include "compiler/Compiler.h" #include "compiler/Compiler.h"
#include "VM/Display/Display.h"
#include "tcpu_version.h"
#include "tlibc/time.h"
#define arg_is(STR) (strcmp(argv[argi], STR) == 0) #define arg_is(LITERAL) str_equals(arg_str, STR(LITERAL))
i32 compileSources(cstr source_file, cstr out_file, bool debug_log); i32 compileSources(cstr source_file, cstr out_file, bool debug_log);
i32 bootFromImage(cstr image_file); i32 bootFromImage(cstr image_file);
@@ -22,15 +24,18 @@ i32 main(const i32 argc, cstr* argv){
cstr NULLABLE(source_file) = NULL; cstr NULLABLE(source_file) = NULL;
bool debug_log = false; bool debug_log = false;
bool video_enabled = false;
for(i32 argi = 1; argi < argc; argi++){ for(i32 argi = 1; argi < argc; argi++){
str arg_str = str_from_cstr(argv[argi]);
if(arg_is("-h") || arg_is("--help")){ if(arg_is("-h") || arg_is("--help")){
printf( printf(
"-h, --help Show this message.\n" "-h, --help Show this message.\n"
"-op, --opcodes Show list of all instructions.\n"
"-i, --image [FILE] Boot VM using image file.\n"
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
"-d, --debug Enable debug log.\n" "-d, --debug Enable debug log.\n"
"-op, --opcodes Show list of all instructions.\n"
"-c, --compile [SOURCE_FILE] [OUT_FILE] Compile assembly source files to machine code.\n"
"-i, --image [FILE] Boot VM using image file.\n"
"--video Enable VM display.\n"
); );
return 0; return 0;
} }
@@ -38,7 +43,7 @@ i32 main(const i32 argc, cstr* argv){
for(u8 opcode = 0; opcode < 255; opcode++){ for(u8 opcode = 0; opcode < 255; opcode++){
const Instruction* instr = Instruction_getByOpcode(opcode); const Instruction* instr = Instruction_getByOpcode(opcode);
if(instr != NULL){ if(instr != NULL){
printf("%02X %s\n", opcode, instr->name); printf("%02X %s\n", opcode, instr->name.data);
} }
} }
return 0; return 0;
@@ -78,6 +83,10 @@ i32 main(const i32 argc, cstr* argv){
else if(arg_is("-d") || arg_is("--debug")){ else if(arg_is("-d") || arg_is("--debug")){
debug_log = true; debug_log = true;
} }
else if(arg_is("--video")){
video_enabled = true;
}
else { else {
printfe("ERROR: unknown argument '%s'\n", argv[argi]); printfe("ERROR: unknown argument '%s'\n", argv[argi]);
return 1; return 1;
@@ -87,11 +96,25 @@ i32 main(const i32 argc, cstr* argv){
i32 exit_code = 0; i32 exit_code = 0;
if(compile){ if(compile){
exit_code = compileSources(source_file, out_file, debug_log); exit_code = compileSources(source_file, out_file, debug_log);
if(exit_code != 0)
goto main_exit;
}
if(boot){
printfe("TCPU version: " TCPU_VERSION_CSTR "\n");
if(video_enabled){
printfe("video enabled\n");
if(!Display_init(1600, 900, DisplayFlags_Default)){
printfe("DISPLAY ERROR: %s\n", Display_getError());
return 1;
}
} }
if(exit_code == 0 && boot){
exit_code = bootFromImage(image_file); exit_code = bootFromImage(image_file);
} }
// frees global variables to supress valgrind memory leak errors
main_exit:
Instruction_destroySearchStructs();
return exit_code; return exit_code;
} }
@@ -115,11 +138,14 @@ i32 bootFromImage(cstr image_file){
} }
VM vm; VM vm;
VM_init(&vm); VM_construct(&vm);
i32 exit_code = 1; i32 exit_code = 1;
if(VM_setMemory(&vm, vm_memory, bytes_read)){ if(VM_setMemory(&vm, vm_memory, bytes_read)){
printf("===============================================================================\n");
exit_code = VM_boot(&vm); exit_code = VM_boot(&vm);
printf("===============================================================================\n");
printfe("VM stopped with code %i\n", exit_code);
} }
if(vm.state == VMState_InternalError){ if(vm.state == VMState_InternalError){
if(vm.error_message){ if(vm.error_message){
@@ -129,17 +155,13 @@ i32 bootFromImage(cstr image_file){
else printfe("VM ERROR: unknown (error_message is null)\n"); else printfe("VM ERROR: unknown (error_message is null)\n");
} }
if(exit_code != 0){
printfe("program exited with code %i\n", exit_code);
}
free(vm_memory); free(vm_memory);
return exit_code; return exit_code;
} }
i32 compileSources(cstr source_file, cstr out_file, bool debug_log){ i32 compileSources(cstr source_file, cstr out_file, bool debug_log){
Compiler cmp; Compiler cmp;
Compiler_init(&cmp); Compiler_construct(&cmp);
bool success = Compiler_compile(&cmp, source_file, out_file, debug_log); bool success = Compiler_compile(&cmp, source_file, out_file, debug_log);
if(!success){ if(!success){
if(cmp.error_message){ if(cmp.error_message){
@@ -147,10 +169,10 @@ i32 compileSources(cstr source_file, cstr out_file, bool debug_log){
free(cmp.error_message); free(cmp.error_message);
} }
else printfe("COMPILER ERROR: unknown (error_message is null)\n"); else printfe("COMPILER ERROR: unknown (error_message is null)\n");
Compiler_free(&cmp); Compiler_destroy(&cmp);
return 111; return 111;
} }
Compiler_free(&cmp); Compiler_destroy(&cmp);
return 0; return 0;
} }

View File

@@ -1,69 +0,0 @@
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdarg.h>
#include <stddef.h>
#include <time.h>
#include <math.h>
#include <string.h>
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
typedef const char* cstr;
#define ARRAY_SIZE(A) sizeof(A)/sizeof(A[0])
#define __count_args( \
a0, a1, a2, a3, a4, a5, a6, a7 , a8, a9, a10,a11,a12,a13,a14,a15, \
a16,a17,a18,a19,a20,a21,a22,a23, a24,a25,a26,a27,a28,a29,a30,a31, \
a32,a33,a34,a35,a36,a37,a38,a39, a40,a41,a42,a43,a44,a45,a46,a47, \
a48,a49,a50,a51,a52,a53,a54,a55, a56,a57,a58,a59,a60,a61,a62,a63, \
a64,...) a64
// Macro for counting variadic arguments (max 64)
// (see usage in kprint.h)
#define count_args(ARGS...) __count_args(ARGS, \
64,63,62,61,60,59,58,57, 56,55,54,53,52,51,50,49, \
48,47,46,45,44,43,42,41, 40,39,38,37,36,35,34,33, \
32,31,30,29,28,27,26,25, 24,23,22,21,20,19,18,17, \
16,15,14,13,12,11,10,9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define printfe(FORMAT, ...) fprintf(stderr, FORMAT ,##__VA_ARGS__)
/// @warning pointer can be null
#define NULLABLE(NAME) NAME
#define strcat_malloc(STR0, ...) _strcat_malloc(count_args(__VA_ARGS__), STR0, __VA_ARGS__)
char* _strcat_malloc(size_t n, cstr str0, ...);
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv);
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...) __attribute__((__format__(__printf__, 2, 3)));
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv);
static inline bool isAlphabeticalLower(char c) { return 'a' <= c && c <= 'z'; }
static inline bool isAlphabeticalUpper(char c) { return 'A' <= c && c <= 'Z'; }
static inline bool isDigit(char c) { return '0' <= c && c <= '9'; }
/// @param startIndex 0 ... src length
/// @param seekLength 0 ... -1
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
i32 cstr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength);
/// @param startIndex -1 ... src length
/// @param seekLength 0 ... -1
/// @return pos of first <fragment> inclusion in <src> or -1 if not found
i32 cstr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength);

3
src/tcpu_version.h Normal file
View File

@@ -0,0 +1,3 @@
#pragma once
#define TCPU_VERSION_CSTR "1.0.0"