asm: implement instruction assembly and emission

This commit is contained in:
2024-12-15 22:36:50 +00:00
parent 3ad355e58c
commit b67b861ecf
5 changed files with 397 additions and 65 deletions

View File

@@ -1,12 +1,13 @@
#include "parse.h"
#include <blue/core/hash.h>
#include <blue/core/stringstream.h>
#include <ctype.h>
#include <ivy/asm/assembler.h>
#include <ivy/asm/bin.h>
#include <ivy/asm/instr.h>
#include <ivy/ident.h>
#include <ivy/selector.h>
#include <ivy/asm/instr.h>
#include <stdlib.h>
#define HASH_SELF 0x2d19e518d40792b7
@@ -93,6 +94,47 @@ struct block_parser_state {
struct arg *s_current_arg;
};
struct mnemonic {
const char *m_name;
uint64_t m_hash;
enum ivy_instr_id m_id;
};
#define MNEMONIC(id, name, hash) \
{ \
.m_id = (id), .m_name = (name), .m_hash = (hash) \
}
static const struct mnemonic mnemonics[] = {
MNEMONIC(IVY_INSTR_LDR, "ldr", 0x127688191dd0471d),
MNEMONIC(IVY_INSTR_STR, "str", 0x826e83195d0d60f0),
MNEMONIC(IVY_INSTR_PUSH, "push", 0x6c80030e2762459d),
MNEMONIC(IVY_INSTR_POP, "pop", 0x779b5819564f2f50),
MNEMONIC(IVY_INSTR_MSG, "msg", 0x07e05a191745be82),
MNEMONIC(IVY_INSTR_ADD, "add", 0xe70bc3190530e654),
MNEMONIC(IVY_INSTR_SUB, "sub", 0x82719d195d0fc2f5),
MNEMONIC(IVY_INSTR_MUL, "mul", 0x07e66519174a3ce1),
MNEMONIC(IVY_INSTR_DIV, "div", 0xcaa83a18f46e5888),
MNEMONIC(IVY_INSTR_C_EQ, "c.eq", 0x2d6e0e924e665476),
MNEMONIC(IVY_INSTR_C_NE, "c.ne", 0x2d8d12924e810bd1),
MNEMONIC(IVY_INSTR_C_LT, "c.lt", 0x2d861d924e7affd4),
MNEMONIC(IVY_INSTR_C_LE, "c.le", 0x2d860e924e7ae657),
MNEMONIC(IVY_INSTR_C_GT, "c.gt", 0x2d751f924e6c9007),
MNEMONIC(IVY_INSTR_C_GE, "c.ge", 0x2d750e924e6c7324),
MNEMONIC(IVY_INSTR_BR, "br", 0x08a64607b54df055),
MNEMONIC(IVY_INSTR_BR_T, "br.t", 0x3e0bed9c06af59cf),
MNEMONIC(IVY_INSTR_BR_F, "br.f", 0x3e0bdf9c06af4205),
MNEMONIC(IVY_INSTR_OB_C, "ob.c", 0x70bdc0b4a72facf7),
MNEMONIC(IVY_INSTR_OB_E, "ob.e", 0x70bdbeb4a72fa991),
MNEMONIC(IVY_INSTR_LAM_C, "lam.c", 0xeb2c84ec62ed7472),
MNEMONIC(IVY_INSTR_IT_G, "it.g", 0x2939c6c597a9d607),
MNEMONIC(IVY_INSTR_IT_N, "it.n", 0x2939bdc597a9c6bc),
MNEMONIC(IVY_INSTR_IT_V, "it.v", 0x2939b5c597a9b924),
MNEMONIC(IVY_INSTR_RET, "ret", 0x89e9ae1960f4a6ec),
MNEMONIC(IVY_INSTR_RET_N, "ret.n", 0x16b792f2c490f4d8),
};
static const size_t nr_mnemonics = sizeof mnemonics / sizeof mnemonics[0];
static unsigned long long get_register_index(struct ivy_asm_token *tok)
{
if (tok->t_type != IVY_ASM_TOK_IDENT) {
@@ -155,9 +197,159 @@ static enum index_base get_index_base(struct ivy_asm_token *tok)
}
}
static enum ivy_status write_instruction(struct ivy_asm_parser *p, struct block_parser_state *state)
static enum ivy_instr_id get_instruction_id(b_queue *mnemonic_tokens)
{
char mnemonic[64];
mnemonic[0] = 0;
b_stringstream s;
b_stringstream_begin(&s, mnemonic, sizeof mnemonic);
unsigned int i = 0;
b_queue_iterator it = {0};
b_queue_iterator_begin(mnemonic_tokens, &it);
while (b_queue_iterator_is_valid(&it)) {
struct ivy_asm_token *tok
= b_unbox(struct ivy_asm_token, it.entry, t_entry);
b_queue_iterator_erase(&it);
if (i > 0) {
b_stringstream_add(&s, ".");
}
b_stringstream_add(&s, tok->t_str);
i++;
ivy_asm_token_destroy(tok);
}
uint64_t hash = b_hash_string(mnemonic);
for (i = 0; i < nr_mnemonics; i++) {
if (hash == mnemonics[i].m_hash
&& !strcmp(mnemonic, mnemonics[i].m_name)) {
return mnemonics[i].m_id;
}
}
return IVY_INSTR_NONE;
}
static enum ivy_status write_instruction(
struct ivy_asm_parser *p, struct block_parser_state *state)
{
/* one more than the true maximum to catch if the input has specified
* too many operands */
#define MAX_ARGS 4
unsigned int i = 0;
enum ivy_instr_id id = get_instruction_id(&state->s_mnemonic);
if (id == IVY_INSTR_NONE) {
return IVY_ERR_BAD_SYNTAX;
}
enum ivy_instr_operand_type operand_types[MAX_ARGS] = {0};
b_queue_iterator it = {0};
b_queue_foreach (&it, &state->s_args) {
struct arg *arg = b_unbox(struct arg, it.entry, arg_entry);
if (i >= MAX_ARGS) {
return IVY_ERR_BAD_SYNTAX;
}
switch (arg->arg_type) {
case ARG_REG:
operand_types[i++] = IVY_INSTR_OPERAND_REGISTER;
break;
case ARG_CONST:
case ARG_LABEL:
operand_types[i++] = IVY_INSTR_OPERAND_CONST;
break;
case ARG_INDEX_REG:
switch (arg->arg_index_reg.index_base) {
case INDEX_SELF:
operand_types[i++]
= IVY_INSTR_OPERAND_SELF_INDEX_REG;
break;
case INDEX_POOL:
operand_types[i++]
= IVY_INSTR_OPERAND_POOL_INDEX_REG;
case INDEX_SP:
operand_types[i++] = IVY_INSTR_OPERAND_SP_INDEX_REG;
case INDEX_BP:
operand_types[i++] = IVY_INSTR_OPERAND_BP_INDEX_REG;
default:
return IVY_ERR_BAD_SYNTAX;
}
break;
case ARG_INDEX_CONST:
switch (arg->arg_index_reg.index_base) {
case INDEX_SELF:
operand_types[i++]
= IVY_INSTR_OPERAND_SELF_INDEX_CONST;
break;
case INDEX_POOL:
operand_types[i++]
= IVY_INSTR_OPERAND_POOL_INDEX_CONST;
break;
case INDEX_SP:
operand_types[i++]
= IVY_INSTR_OPERAND_SP_INDEX_CONST;
break;
case INDEX_BP:
operand_types[i++]
= IVY_INSTR_OPERAND_BP_INDEX_CONST;
break;
default:
return IVY_ERR_BAD_SYNTAX;
}
break;
default:
return IVY_ERR_BAD_SYNTAX;
}
}
const struct ivy_instr_definition *instr_info
= ivy_instr_find(id, operand_types);
if (!instr_info) {
return IVY_ERR_BAD_SYNTAX;
}
struct ivy_instr instr = {0};
instr.i_op = instr_info;
i = 0;
b_queue_iterator_begin(&state->s_args, &it);
while (b_queue_iterator_is_valid(&it)) {
struct arg *arg = b_unbox(struct arg, it.entry, arg_entry);
b_queue_iterator_erase(&it);
switch (arg->arg_type) {
case ARG_REG:
instr.i_arg[i++] = arg->arg_reg.reg_index;
break;
case ARG_CONST:
instr.i_arg[i++] = arg->arg_const->t_int.v;
break;
case ARG_LABEL:
instr.i_arg[i++] = 0;
break;
case ARG_INDEX_REG:
instr.i_arg[i++] = arg->arg_index_reg.index_offset_reg;
break;
case ARG_INDEX_CONST:
instr.i_arg[i++]
= arg->arg_index_const.index_offset->t_int.v;
break;
default:
return IVY_ERR_BAD_SYNTAX;
}
free(arg);
}
ivy_assembler_put_instr(p->p_assembler, &instr);
return IVY_OK;
}
@@ -237,7 +429,7 @@ static enum ivy_status push_reg_arg(
static enum ivy_status parse_linefeed(
struct ivy_asm_parser *ctx, struct ivy_asm_token *tok)
{
struct block_parser_state *state
struct block_parser_state *state
= (struct block_parser_state *)asm_parser_get_state(ctx);
switch (state->s_prev_component) {