-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode-generator.js
More file actions
45 lines (44 loc) · 1.14 KB
/
code-generator.js
File metadata and controls
45 lines (44 loc) · 1.14 KB
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
const BASIC_OPERATION = [
"ADD",
"SUB",
"MUL",
"DIV",
"AND",
"OR",
"XOR",
"CMPEQ",
"CMPLT",
"CMPLE",
];
const BASIC_OPERATION_WITH_CONSTANTS = ["ADDC"];
const MEMORY_ACCESS = ["LD", "ST", "BEQ", "BNE"];
export function codeGenerator(node) {
if (BASIC_OPERATION.includes(node.type)) {
return `${node.type}(${node.reg1}, ${node.reg2}, ${node.reg3})`;
}
if (BASIC_OPERATION_WITH_CONSTANTS.includes(node.type)) {
return `${node.type}(${node.reg1}, ${node.constant}, ${node.reg2})`;
}
if (MEMORY_ACCESS.includes(node.type)) {
return `${node.type}(${node.reg1}, ${node.address}, ${node.reg2})`;
}
switch (node.type) {
case "UASMProgram":
return (
".include beta.uasm\n\n" +
node.body.map(codeGenerator).join("\n") +
"\n\n" +
node.labels.map(codeGenerator).join("\n")
);
case "LabelDeclaration":
return `\n\n${node.name}:`;
case "VarLabelDeclaration":
return `${node.name} : LONG(0)`;
case "BR":
return `BR(${node.address})`;
case "HALT":
return "HALT()";
default:
throw new TypeError("Unknown node type: " + node.type);
}
}