modularize
This commit is contained in:
parent
3dff79adc5
commit
ed28936be1
73
hash.c
Normal file
73
hash.c
Normal file
|
@ -0,0 +1,73 @@
|
|||
// Instruction Hash table, for faster lookups
|
||||
typedef struct InstHtNode_s
|
||||
{
|
||||
uint8_t index1;
|
||||
uint8_t index2;
|
||||
} InstHtNode;
|
||||
typedef InstHtNode *InstHt;
|
||||
|
||||
uint32_t inst_hash(const char *s, size_t len)
|
||||
{
|
||||
uint32_t hash = 0;
|
||||
uint32_t mul = 75;
|
||||
for (size_t ii = 0; ii < len; ii += 1)
|
||||
{
|
||||
hash ^= s[ii] * mul;
|
||||
hash *= mul;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
InstHt build_lookup(void)
|
||||
{
|
||||
const size_t size = 256;
|
||||
InstHt table = (InstHt)malloc(size * sizeof(InstHtNode));
|
||||
if (table == NULL)
|
||||
{
|
||||
return table;
|
||||
}
|
||||
for (size_t ii = 0; ii < size; ii += 1)
|
||||
{
|
||||
table[ii] = (InstHtNode){0xff, 0xff};
|
||||
}
|
||||
for (size_t ii = 0; ii < INST_CNT; ii += 1)
|
||||
{
|
||||
const char *mnemonic = INST[ii].mnemonic;
|
||||
uint32_t hash = inst_hash(mnemonic, strlen(mnemonic));
|
||||
InstHtNode *node = &table[hash & 0xff];
|
||||
if (node->index1 == 0xff)
|
||||
{
|
||||
node->index1 = ii;
|
||||
}
|
||||
else if (node->index2 == 0xff)
|
||||
{
|
||||
node->index2 = ii;
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "more than 1 collision in hash table\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return table;
|
||||
}
|
||||
|
||||
size_t inst_lookup(InstHt ht, const char *s, size_t len)
|
||||
{
|
||||
uint32_t hash = inst_hash(s, len);
|
||||
uint8_t *node = (uint8_t *)&ht[(size_t)(hash & 0xff)];
|
||||
for (size_t ii = 0; ii < 2; ii += 1)
|
||||
{
|
||||
size_t idx = (size_t)node[ii];
|
||||
if (idx == 0xff)
|
||||
{
|
||||
break;
|
||||
}
|
||||
const char *mnemonic = INST[idx].mnemonic;
|
||||
if (strncmp(s, mnemonic, len) == 0 && mnemonic[len] == 0)
|
||||
{
|
||||
return idx;
|
||||
}
|
||||
}
|
||||
return INVALID;
|
||||
}
|
142
instructions.c
Normal file
142
instructions.c
Normal file
|
@ -0,0 +1,142 @@
|
|||
typedef struct InstDesc_s
|
||||
{
|
||||
char *mnemonic;
|
||||
unsigned char opcode;
|
||||
OpType type;
|
||||
} InstDesc;
|
||||
|
||||
const InstDesc INST[] = {
|
||||
{"un", 0x00, Empty},
|
||||
{"tx", 0x01, Empty},
|
||||
{"nop", 0x02, Empty},
|
||||
{"add8", 0x03, RRR},
|
||||
{"add16", 0x04, RRR},
|
||||
{"add32", 0x05, RRR},
|
||||
{"add64", 0x06, RRR},
|
||||
{"sub8", 0x07, RRR},
|
||||
{"sub16", 0x08, RRR},
|
||||
{"sub32", 0x09, RRR},
|
||||
{"sub64", 0x0A, RRR},
|
||||
{"mul8", 0x0B, RRR},
|
||||
{"mul16", 0x0C, RRR},
|
||||
{"mul32", 0x0D, RRR},
|
||||
{"mul64", 0x0E, RRR},
|
||||
{"and", 0x0F, RRR},
|
||||
{"or", 0x10, RRR},
|
||||
{"xor", 0x11, RRR},
|
||||
{"slu8", 0x12, RRR},
|
||||
{"slu16", 0x13, RRR},
|
||||
{"slu32", 0x14, RRR},
|
||||
{"slu64", 0x15, RRR},
|
||||
{"sru8", 0x16, RRR},
|
||||
{"sru16", 0x17, RRR},
|
||||
{"sru32", 0x18, RRR},
|
||||
{"sru64", 0x19, RRR},
|
||||
{"srs8", 0x1A, RRR},
|
||||
{"srs16", 0x1B, RRR},
|
||||
{"srs32", 0x1C, RRR},
|
||||
{"srs64", 0x1D, RRR},
|
||||
{"cmpu", 0x1E, RRR},
|
||||
{"cmps", 0x1F, RRR},
|
||||
{"diru8", 0x20, RRRR},
|
||||
{"diru16", 0x21, RRRR},
|
||||
{"diru32", 0x22, RRRR},
|
||||
{"diru64", 0x23, RRRR},
|
||||
{"dirs8", 0x24, RRRR},
|
||||
{"dirs16", 0x25, RRRR},
|
||||
{"dirs32", 0x26, RRRR},
|
||||
{"dirs64", 0x27, RRRR},
|
||||
{"neg", 0x28, RR},
|
||||
{"not", 0x29, RR},
|
||||
{"sxt8", 0x2A, RR},
|
||||
{"sxt16", 0x2B, RR},
|
||||
{"sxt32", 0x2C, RR},
|
||||
{"addi8", 0x2D, RRx8},
|
||||
{"addi16", 0x2E, RRx16},
|
||||
{"addi32", 0x2F, RRx32},
|
||||
{"addi64", 0x30, RRx64},
|
||||
{"muli8", 0x31, RRx8},
|
||||
{"muli16", 0x32, RRx16},
|
||||
{"muli32", 0x33, RRx32},
|
||||
{"muli64", 0x34, RRx64},
|
||||
{"andi", 0x35, RRx64},
|
||||
{"ori", 0x36, RRx64},
|
||||
{"xori", 0x37, RRx64},
|
||||
{"slui8", 0x38, RRu8},
|
||||
{"slui16", 0x39, RRu8},
|
||||
{"slui32", 0x3A, RRu8},
|
||||
{"slui64", 0x3B, RRu8},
|
||||
{"srui8", 0x3C, RRu8},
|
||||
{"srui16", 0x3D, RRu8},
|
||||
{"srui32", 0x3E, RRu8},
|
||||
{"srui64", 0x3F, RRu8},
|
||||
{"srsi8", 0x40, RRu8},
|
||||
{"srsi16", 0x41, RRu8},
|
||||
{"srsi32", 0x42, RRu8},
|
||||
{"srsi64", 0x43, RRu8},
|
||||
{"cmpui", 0x44, RRu64},
|
||||
{"cmpsi", 0x45, RRs64},
|
||||
{"cp", 0x46, RR},
|
||||
{"swa", 0x47, RR},
|
||||
{"li8", 0x48, Rx8},
|
||||
{"li16", 0x49, Rx16},
|
||||
{"li32", 0x4A, Rx32},
|
||||
{"li64", 0x4B, Rx64},
|
||||
{"lra", 0x4C, RRr32},
|
||||
{"ld", 0x4D, RRu64u16},
|
||||
{"st", 0x4E, RRu64u16},
|
||||
{"ldr", 0x4F, RRr32u16},
|
||||
{"str", 0x50, RRr32u16},
|
||||
{"bmc", 0x51, RRu16},
|
||||
{"brc", 0x52, RRu8},
|
||||
{"jmp", 0x53, r32},
|
||||
{"jal", 0x54, RRr32},
|
||||
{"jala", 0x55, RRu64},
|
||||
{"jeq", 0x56, RRr16},
|
||||
{"jne", 0x57, RRr16},
|
||||
{"jltu", 0x58, RRr16},
|
||||
{"jgtu", 0x59, RRr16},
|
||||
{"jlts", 0x5A, RRr16},
|
||||
{"jgts", 0x5B, RRr16},
|
||||
{"eca", 0x5C, Empty},
|
||||
{"ebp", 0x5D, Empty},
|
||||
{"fadd32", 0x5E, RRR},
|
||||
{"fadd64", 0x5F, RRR},
|
||||
{"fsub32", 0x60, RRR},
|
||||
{"fsub64", 0x61, RRR},
|
||||
{"fmul32", 0x62, RRR},
|
||||
{"fmul64", 0x63, RRR},
|
||||
{"fdiv32", 0x64, RRR},
|
||||
{"fdiv64", 0x65, RRR},
|
||||
{"fma32", 0x66, RRRR},
|
||||
{"fma64", 0x67, RRRR},
|
||||
{"fcmplt32", 0x6A, RRR},
|
||||
{"fcmplt64", 0x6B, RRR},
|
||||
{"fcmpgt32", 0x6C, RRR},
|
||||
{"fcmpgt64", 0x6D, RRR},
|
||||
{"itf32", 0x6E, RR},
|
||||
{"itf64", 0x6F, RR},
|
||||
{"fti32", 0x70, RRu8},
|
||||
{"fti64", 0x71, RRu8},
|
||||
{"fc32t64", 0x72, RR},
|
||||
{"fc64t32", 0x73, RR},
|
||||
{"lra16", 0x74, RRr16},
|
||||
{"ldr16", 0x75, RRr16u16},
|
||||
{"str16", 0x76, RRr16u16},
|
||||
{"jmp16", 0x77, r16},
|
||||
};
|
||||
|
||||
const size_t INST_CNT = sizeof(INST) / sizeof(INST[0]);
|
||||
const size_t INVALID = ~(size_t)0;
|
||||
size_t inst_find(const char *mnemonic, size_t len)
|
||||
{
|
||||
for (size_t ii = 0; ii < INST_CNT; ii += 1)
|
||||
{
|
||||
const char *entry = INST[ii].mnemonic;
|
||||
if (strncmp(entry, mnemonic, len) == 0 && entry[len] == '\0')
|
||||
{
|
||||
return ii;
|
||||
}
|
||||
}
|
||||
return INVALID;
|
||||
}
|
62
op.h
Normal file
62
op.h
Normal file
|
@ -0,0 +1,62 @@
|
|||
typedef enum OpType_e
|
||||
{
|
||||
Empty = 0,
|
||||
R,
|
||||
RR,
|
||||
RRR,
|
||||
RRRR,
|
||||
Rx8,
|
||||
Rx16,
|
||||
Rx32,
|
||||
Rx64,
|
||||
RRx8,
|
||||
RRx16,
|
||||
RRx32,
|
||||
RRx64,
|
||||
RRs32,
|
||||
RRs64,
|
||||
RRu8,
|
||||
RRu16,
|
||||
RRu64,
|
||||
r16,
|
||||
r32,
|
||||
RRr16,
|
||||
RRr32,
|
||||
RRr16u16,
|
||||
RRr32u16,
|
||||
RRu64u16,
|
||||
} OpType;
|
||||
// R -> register,
|
||||
// 1 -> Xi8, 2 -> Xi16, 4 -> Xi32, 8 -> Xi64,
|
||||
// b -> Si8, h -> Si16, w -> Si32, d -> Si64,
|
||||
// B -> Ui8, H -> Ui16, W -> Ui32, D -> Ui64,
|
||||
// o -> 16 bit relative offset,
|
||||
// O -> 32 bit relative offset,
|
||||
|
||||
const char *TYPE_STR[] = {
|
||||
"",
|
||||
"R",
|
||||
"RR",
|
||||
"RRR",
|
||||
"RRRR",
|
||||
"R1",
|
||||
"R2",
|
||||
"R4",
|
||||
"R8",
|
||||
"RR1",
|
||||
"RR2",
|
||||
"RR4",
|
||||
"RR8",
|
||||
"RRw",
|
||||
"RRd",
|
||||
"RRB",
|
||||
"RRH",
|
||||
"RRD",
|
||||
"o",
|
||||
"O",
|
||||
"RRo",
|
||||
"RRO",
|
||||
"RRoH",
|
||||
"RROH",
|
||||
"RRDH",
|
||||
};
|
Loading…
Reference in a new issue