Created custom functions to convert (h)words to and from bytecode format

Instead of using endian.h that is not portable AND doesn't work with
C++, I'll just write my own using a forced union based type punning
trick.

I've decided to use little endian for the format as well: it seems to
be used by most desktop computers so it should make these functions
faster to run for most CPUs.
This commit is contained in:
2024-04-14 03:54:54 +06:30
parent e12f364669
commit 3b912495de
3 changed files with 87 additions and 8 deletions

View File

@@ -438,7 +438,7 @@ void insts_write_bytecode_file(inst_t *instructions, size_t size, FILE *fp)
void prog_header_write_bytecode(prog_header_t header, darr_t *buffer)
{
word start = htobe64(header.start_address);
word start = word_htobc(header.start_address);
darr_append_bytes(buffer, (byte *)&start, sizeof(start));
}
@@ -447,7 +447,7 @@ void prog_write_bytecode(prog_t *program, darr_t *buffer)
// Write program header
prog_header_write_bytecode(program->header, buffer);
// Write instruction count
word pcount = htobe64(program->count);
word pcount = word_htobc(program->count);
darr_append_bytes(buffer, (byte *)&pcount, sizeof(pcount));
// Write instructions
insts_write_bytecode(program->instructions, program->count, buffer);