Add more values to the ast to facilitate encoding

- Add a instruction value that contains the encoding, the address and a
  flag to indicate if this instruction contains label references
- Add label value that contains an address
- Add reference value that contains offset, an absolute address and an
  operand size
This commit is contained in:
omicron 2025-04-18 12:30:51 +02:00
parent 9c6b69e187
commit 9a1570e3e5
3 changed files with 18 additions and 5 deletions

View File

@ -75,7 +75,7 @@ typedef struct register_ {
} register_t; } register_t;
typedef struct opcode_encoding { typedef struct opcode_encoding {
uint8_t encoding[32]; uint8_t buffer[32];
size_t len; size_t len;
} opcode_encoding_t; } opcode_encoding_t;
@ -89,7 +89,19 @@ struct ast_node {
union { union {
register_t reg; register_t reg;
number_t number; number_t number;
opcode_encoding_t encoding; struct {
bool has_reference;
opcode_encoding_t encoding;
int64_t address;
} instruction;
struct {
int64_t offset;
int64_t address;
operand_size_t size;
} reference;
struct {
int64_t address;
} label;
} value; } value;
}; };

View File

@ -481,7 +481,7 @@ error_t *encoder_encode_instruction(encoder_t *encoder,
return err; return err;
// produce the actual encoding output in the NODE_INSTRUCTION value // produce the actual encoding output in the NODE_INSTRUCTION value
uint8_t *output = instruction->value.encoding.encoding; uint8_t *output = instruction->value.instruction.encoding.buffer;
size_t output_len = 0; size_t output_len = 0;
// Handle prefixes // Handle prefixes
@ -500,7 +500,7 @@ error_t *encoder_encode_instruction(encoder_t *encoder,
memcpy(output + output_len, encoding->buffer, encoding->len); memcpy(output + output_len, encoding->buffer, encoding->len);
output_len += encoding->len; output_len += encoding->len;
instruction->value.encoding.len = output_len; instruction->value.instruction.encoding.len = output_len;
return nullptr; return nullptr;
} }

View File

@ -88,7 +88,8 @@ error_t *print_encoding(tokenlist_t *list) {
if (node->id != NODE_INSTRUCTION) if (node->id != NODE_INSTRUCTION)
continue; continue;
print_hex(node->value.encoding.len, node->value.encoding.encoding); print_hex(node->value.instruction.encoding.len,
node->value.instruction.encoding.buffer);
} }
encoder_free(encoder); encoder_free(encoder);