1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include "cpu.h"
#include "log.h"
#include "opc.h"
#define REGa(x) (((x) & 0x1FFF) << 45)
#define REGb(x) (((x) & 0x1FFF) << 32)
#define REGc(x) (((x) & 0x1FFF) << 19)
#define IMMc(x) ((x) & 0xFFFFFFFF)
inst_t compile(const char *line)
{
char mnem[5];
int64_t a = 0, b = 0, c = 0;
/* register file size */
if (sscanf(line, ".REGS %"SCNi64"", &a) == 1)
return a;
/* ADD, SUB, MUL, DIV, MOD, AND, OR */
if (sscanf(line, "%4s r%"SCNi64", r%"SCNi64", r%"SCNi64"", mnem, &a, &b, &c) == 4)
return mnemonic2opc(mnem) | REGa(a) | REGb(b) | REGc(c);
/* LW, SW */
if (sscanf(line, "%4s r%"SCNi64", r%"SCNi64", %"SCNi64"", mnem, &a, &b, &c) == 4)
return mnemonic2opc(mnem) | REGa(a) | REGb(b) | IMMc(c);
/* CMP */
if (sscanf(line, "%4s r%"SCNi64", r%"SCNi64"", mnem, &a, &b) == 3)
return mnemonic2opc(mnem) | REGa(a) | REGb(b);
/* MOV, BEZ */
if (sscanf(line, "%4s r%"SCNi64", %"SCNi64"", mnem, &a, &c) == 3)
return mnemonic2opc(mnem) | REGa(a) | IMMc(c);
/* EQ, NE, LT, LE, GE, GT, PUSH, POP */
if (sscanf(line, "%4s r%"SCNi64"", mnem, &a) == 2)
return mnemonic2opc(mnem) | REGa(a);
/* JMP, CALL */
if (sscanf(line, "%4s %"SCNi64"", mnem, &c) == 2)
return mnemonic2opc(mnem) | IMMc(c);
/* RET, SYS */
if (sscanf(line, "%4s", mnem) == 1)
return mnemonic2opc(mnem);
return 0xFFFFFFFFFFFFFFFF;
}
|