Commit Graph

24 Commits

Author SHA1 Message Date
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
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 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 21628681d5 build: introduce and fix struct padding warningd
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-06-05 03:39:27 -04:00
Ronald Caesar 301f6df564 assembler: emit BR instruction
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-05-31 20:22:45 -04:00
Ronald Caesar 4f75e6e8ed assembler: emit B instruction
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-05-31 20:22:07 -04:00
Ronald Caesar 2471465388 translate: add support for sub immediate instructions
* assembler: emit SUB immediate
* engine: translate SUB immediate to OPCODE_SUB
* tests/translate: cover SUB immediate translation

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-04-23 08:07:44 -04:00
Tarik Caramanico 09168167e0 assembler: emit SUB immediate
Adds bal_emit_sub_immediate paralleling bal_emit_add_immediate.
The only encoding difference is the op bit (bit 30): SUB sets it,
ADD clears it, so the fixed-bits constant becomes 0xA2 instead of
0x22 while every other field keeps the same layout and the same
input validation.

Kept standalone rather than folded into a shared addsub helper so
this commit does not refactor the existing ADD emission path. A
future commit can extract the shared body once both variants are
in place.

Signed-off-by: Tarik Caramanico <tarikcaramanico@gmail.com>
2026-04-17 23:26:09 -03:00
Ronald Caesar e3a0870b86 engine: fix name mangling with c++ compilers
Added `extern "C"` to every header file to support using Ballistic
with C++ projects

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-04-16 18:20:57 -04:00
Ronald Caesar 72fda031ce assembler: remove mnemonic emit_add_immediate() signature
Having an option to specify the mnemonic does not make sense because
it will always be "ADD (Imm)".

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-04-09 15:45:59 -04:00
Ronald Caesar c70a681153 assembler: emit ret instruction
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-03-12 12:12:51 -04:00
Ronald Caesar 5eb0baad05 assembler: add more guard clauses to emission functions
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-03-12 11:30:24 -04:00
Ronald Caesar c4d2515bc9 assembler: append prefix to add immediate function
All public functions must have the prefix "bal_"

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-03-09 17:10:23 +00:00
Ronald Caesar 402a303902 Merge branch 'engine-support-add' 2026-03-04 07:13:31 +00:00
Ronald Caesar c12b8f22b3 engine: move register index enum to bal_types.h
This enum is very useful for the translation loop and including the
entirety of bal_assembler.h just to get it is not a good idea. This enum
has been moved to bal_types.h.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-03-04 07:12:49 +00:00
Ronald Caesar 7881220883 tools: remove -Werror from doctest
I truly dont if markdown code examples peoduce warnings, but those
warnings cannot make the build fail to compile.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-03-04 07:12:46 +00:00
Ronald Caesar f27d77b056 assembler: emit add immediate instructions
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-03-04 07:09:04 +00:00
Ronald Caesar 851c78c3be assembler: remove redundant doc clause
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 19:34:47 -04:00
Ronald Caesar fd592ea245 assembler: fix doc links
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 19:28:44 -04:00
Ronald Caesar 9ce5081561 assembler: add file level docs to bal_assembler.h
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 19:20:53 -04:00
Ronald Caesar c5f52b1db5 assembler: wrap BAL_REGISTER_X* defines in an enum.
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 18:18:25 -04:00
Ronald Caesar 4c25091d7e assembler: add documentation to bal_assembler.h
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 18:06:02 -04:00
Ronald Caesar 6e660c21f0 assembler: add logging
Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 13:17:35 -04:00
Ronald Caesar 32f799695f assembler: add lightweight assembler
This was created to make creating instructions easier to test
Ballistic's translation loop. Documentation will be added in the
future.

Signed-off-by: Ronald Caesar <github43132@proton.me>
2026-02-15 04:05:02 -04:00