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