184 Commits

Author SHA1 Message Date
Ronald Caesar 35b73efa1b assembler: add ownership rules to bal_assembler_t documentation
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-17 03:30:43 -04:00
Ronald Caesar 35ef021fc9 engine: add magic numbers to verify struct integrity
The main focus of this commit is 'bal_safety.h'. This header file
adds magic numbers to prevent Undefined Behavior. Right now this
has only been added to 'bal_assembler_t'.

This introduces 3 magic numbers:

#define BAL_MAGIC_UNINITIALIZED 0x00000000U
#define BAL_ASSEMBLER_MAGIC_ALIVE 0xBA11A550U
#define BAL_ASSEMBLER_MAGIC_DEAD  0xDEADBA11U

'ALIVE' magic numbers will be set only if the struct has been
initialized properly. Every function should check a struct's magic
number to make sure it's not operating on a memory corrupted struct.

'DEAD' magic numbers should be set on a struct's 'destory()' function.
This is done to prevent Double Free and Use-After-Free scenarios.

Convenience macro 'BAL_CHECK_MAGIC' and 'BAL_CHECK_MAGIC_VOID' have
been added to easily validate struct integrity.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-17 03:20:17 -04:00
Ronald Caesar 2e7240e9d4 engine: make capacity error more specific
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-17 03:00:39 -04:00
Ronald Caesar a69df0101a engine: add missing documentation to error enum values
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-17 01:58:53 -04:00
Ronald Caesar 87923661a0 engine: reorganize error enum values
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-17 01:47:03 -04:00
Ronald Caesar 5b931324c6 engine: add new error for very large buffer capacities
This introduces BAL_ERROR_INVALID_CAPACITY which happens when a
buffer capacity exceeds SIZE_MAX or whatever the type's max value is.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-17 01:40:55 -04:00
Ronald Caesar c45acbf6ad cpu: add explicit padding to bal_cpu_t
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-15 03:34:17 -04:00
Ronald Caesar 80706ef77b engine: remove outdated docs
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-14 02:45:50 -04:00
mcrib884 40246aa4f0 assembler: add bal_x86_emit_sub_r64_r64
Emits the x86 sub r64, r64 instruction (opcode 0x2B, REX.W + ModRM,
3 bytes). Follows the 5-check guard pattern matching add_r64_r64.

Signed-off-by: mcrib884 <farukkaya229@outlook.com>
2026-06-14 05:56:23 +00:00
mcrib884 94cca0e48f assembler: fix branch encoding and add reset function
The B (unconditional branch) instruction encoding used integer
division (/) instead of bitwise OR (|) to combine the opcode with
the branch offset. This produced incorrect machine code for every
branch target other than zero.

Also add bal_assembler_reset() to match the reset API provided by
every other module in Ballistic (bal_x86_assembler, sliding window,
tier1 compiler). The function zeroes the instruction buffer and
resets offset and status.

Wrap the hot function in BAL_UNLIKELY.

Signed-off-by: mcrib884 <farukkaya229@outlook.com>
2026-06-12 04:25:47 +00:00
Ronald Caesar d13a4c34bc engine: fix some MSVC warnings
Compiling with MSVC still fails but I fixed some errors.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-10 23:29:54 -04:00
Ronald Caesar 8f157f120a engine: add export function attribute
Adds the BAL_EXPORT definition which allows to export functions to
Lua via FFI.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-08 23:53:36 -04:00
Ronald Caesar 4c7bbe4209 engine: detect x86 architecture on MSVC
Apparently __x86_64__ is a Unix thing only.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-07 18:46:06 -04:00
Ronald Caesar 6c911c608b memory: rename allocator->handle to allocator->context
This is done for clarity.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 22:18:27 -04:00
Ronald Caesar 4f33c6fc57 x86/assembler: emit add r64, imm32
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 10:06:33 -04:00
Ronald Caesar b50ac94b04 x86/assembler: emit add r64, r64
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 09:05:02 -04:00
Ronald Caesar c0a47775ec assembler: emit ADD (Shifted Register) instructions
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 06:41:53 -04:00
Ronald Caesar be64332f20 x86/assembler: add branch offset overflow error
Adds BAL_ERROR_BRANCH_OFFSET_OVERFLOW to bal_error_t. This should be
returned when a branch offset exceeds its normal accepted range.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 03:01:50 -04:00
Ronald Caesar 44dc0ec602 x86/compiler1: organize macro opcodes
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 02:11:38 -04:00
Ronald Caesar ae91aac6f7 x86/compiler1: support setcc and jcc instructions in sliding window
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 02:08:32 -04:00
Ronald Caesar a0a15707d7 x86/assembler: emit jcc offset
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 01:45:23 -04:00
Ronald Caesar 89c53b1a3c x86/assembler: add static assert for bal_x86_condition_t
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 01:31:52 -04:00
Ronald Caesar 1c5dacaab7 x86/assembler: emit setcc byte ptr [rbp + offset]
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-06 01:10:22 -04:00
Ronald Caesar a80ba0c320 x86/compiler1: add static assert for compiler1
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-05 04:03:35 -04:00
Ronald Caesar 42fa89dab3 x86/compiler: add static assert for sliding window struct size
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-05 04:02:50 -04:00