#include <stdio.h>
#include <stdint.h>
#include "cpu.h"
#include "log.h"
#include "opc.h"
#define REGa(x) (((x) & 0x1F) << 21)
#define REGb(x) (((x) & 0x1F) << 16)
#define REGc(x) (((x) & 0x1F) << 11)
#define IMMc(x) ((x) & 0xFFFF)
uint32_t compile(const char *line)
{
char mnem[5];
int32_t a = 0, b = 0, c = 0;
static size_t regs = 0;
/* register file size */
if (sscanf(line, ".REGS %d", &a) == 1) {
regs = a;
return a;
}
/* ADD, SUB, MUL, DIV, MOD, AND, OR */
if (sscanf(line, "%s r%d, r%d, r%d", mnem, &a, &b, &c) == 4)
return mnemonic2opc(mnem) | REGa(a) | REGb(b) | REGc(c);
/* LW, SW */
if (sscanf(line, "%s r%d, r%d, %d", mnem, &a, &b, &c) == 4)
return mnemonic2opc(mnem) | REGa(a) | REGb(b) | IMMc(c);
/* CMP */
if (sscanf(line, "%s r%d, r%d", mnem, &a, &b) == 3)
return mnemonic2opc(mnem) | REGa(a) | REGb(b);
/* MOV, BEZ */
if (sscanf(line, "%s r%2d, %d", mnem, &a, &c) == 3)
return mnemonic2opc(mnem) | REGa(a) | IMMc(c);
/* EQ, NE, LT, LE, GE, GT, PUSH, POP */
if (sscanf(line, "%s r%d", mnem, &a) == 2)
return mnemonic2opc(mnem) | REGa(a);
/* JMP, CALL */
if (sscanf(line, "%s %d", mnem, &c) == 2)
return mnemonic2opc(mnem) | IMMc(c);
/* RET, SYS */
if (sscanf(line, "%s", mnem) == 1)
return mnemonic2opc(mnem);
return 0xFFFFFFFF;
}