Moved prog_t.count -> prog_t.prog_header_t.count

This commit is contained in:
2024-04-16 18:21:39 +06:30
parent 38d7c13287
commit 5d6cf212e7
2 changed files with 10 additions and 14 deletions

View File

@@ -440,22 +440,21 @@ void prog_header_write_bytecode(prog_header_t header, darr_t *buffer)
{ {
word start = word_htobc(header.start_address); word start = word_htobc(header.start_address);
darr_append_bytes(buffer, (byte *)&start, sizeof(start)); darr_append_bytes(buffer, (byte *)&start, sizeof(start));
word count = word_htobc(header.count);
darr_append_bytes(buffer, (byte *)&count, sizeof(count));
} }
void prog_write_bytecode(prog_t *program, darr_t *buffer) void prog_write_bytecode(prog_t *program, darr_t *buffer)
{ {
// Write program header // Write program header
prog_header_write_bytecode(program->header, buffer); prog_header_write_bytecode(program->header, buffer);
// Write instruction count
word pcount = word_htobc(program->count);
darr_append_bytes(buffer, (byte *)&pcount, sizeof(pcount));
// Write instructions // Write instructions
insts_write_bytecode(program->instructions, program->count, buffer); insts_write_bytecode(program->instructions, program->header.count, buffer);
} }
void prog_append_bytecode(prog_t *program, darr_t *buffer) void prog_append_bytecode(prog_t *program, darr_t *buffer)
{ {
insts_write_bytecode(program->instructions, program->count, buffer); insts_write_bytecode(program->instructions, program->header.count, buffer);
} }
prog_header_t prog_header_read_bytecode(darr_t *buffer) prog_header_t prog_header_read_bytecode(darr_t *buffer)
@@ -463,6 +462,8 @@ prog_header_t prog_header_read_bytecode(darr_t *buffer)
prog_header_t header = {0}; prog_header_t header = {0};
header.start_address = convert_bytes_to_word(buffer->data + buffer->used); header.start_address = convert_bytes_to_word(buffer->data + buffer->used);
buffer->used += sizeof(header.start_address); buffer->used += sizeof(header.start_address);
header.count = convert_bytes_to_word(buffer->data + buffer->used);
buffer->used += sizeof(header.count);
return header; return header;
} }
@@ -477,24 +478,19 @@ prog_t *prog_read_bytecode(darr_t *buffer)
if ((buffer->available - buffer->used) < WORD_SIZE) if ((buffer->available - buffer->used) < WORD_SIZE)
return NULL; return NULL;
// Read instruction count prog_t *program = malloc(sizeof(*program) + (sizeof(inst_t) * header.count));
word count = convert_bytes_to_word(buffer->data + buffer->used);
buffer->used += sizeof(count);
prog_t *program = malloc(sizeof(*program) + (sizeof(inst_t) * count));
size_t i; size_t i;
for (i = 0; i < count && (buffer->used < buffer->available); ++i) for (i = 0; i < header.count && (buffer->used < buffer->available); ++i)
program->instructions[i] = inst_read_bytecode(buffer); program->instructions[i] = inst_read_bytecode(buffer);
// TODO: Error (Expected more instructions) // TODO: Error (Expected more instructions)
if (i < count - 1) if (i < header.count - 1)
{ {
free(program); free(program);
return NULL; return NULL;
} }
program->header = header; program->header = header;
program->count = count;
return program; return program;
} }

View File

@@ -164,12 +164,12 @@ typedef struct
typedef struct typedef struct
{ {
word start_address; word start_address;
word count;
} prog_header_t; } prog_header_t;
typedef struct typedef struct
{ {
prog_header_t header; prog_header_t header;
word count;
inst_t instructions[]; inst_t instructions[];
} prog_t; } prog_t;