-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompIR3Translator.ts
75 lines (51 loc) · 2.2 KB
/
CompIR3Translator.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { CompIR4, argument_register } from "./CompIR4.ts";
import { CompIR3 } from "./CompIR3.ts";
export function translate(code: CompIR3[]): CompIR4[] {
const ir4 = new Array<CompIR4>();
ir4.push({directive: [".global", "main"]})
ir4.push({lbl: "str_print"});
ir4.push({directive: [".string", "%lf\n"]});
ir4.push({lbl: "dividend"});
ir4.push({directive: [".double", "4294967296"]});
ir4.push({lbl: "mask_int"});
ir4.push({directive: [".long", "0"]});
ir4.push({directive: [".long", "0xFFFFFFFF"]});
ir4.push({lbl: "main"});
for (const instruccion of code) {
if (instruccion == "callBegin" || instruccion == "return" ||
(typeof instruccion == "object" && ("callEnd" in instruccion || "functionIntro" in instruccion))) {
if (instruccion == "callBegin") {
//nada
} else if (instruccion == "return") {
ir4.push({ret: ""});
} else if ("callEnd" in instruccion) {
ir4.push({call: instruccion.callEnd})
} else if ("functionIntro" in instruccion) {
const parametros_funcion = instruccion.functionIntro;
const registers:argument_register[] = ["rdi", "rsi", "rdx", "rcx", "r8", "r9"];
while (parametros_funcion.length > 0 && registers.length > 0) {
const parameter = parametros_funcion.shift()
const register = registers.shift();
if (parameter == undefined || register == undefined) {
throw new Error("PARAMETRO O REGISTRO ES UNDEFINED");
}
ir4.push({movq: [register, parameter]})
}
for (const ubicacion_destino of parametros_funcion) {
ir4.push({popq: ubicacion_destino});
}
}
} else {
ir4.push(instruccion);
}
}
ir4.push({cvtsi2sdq: [0, "xmm0"]});
ir4.push({divsd: [{relative: "dividend"}, "xmm0"]});
ir4.push({leaq: [{relative: "str_print"}, "rdi"]});
ir4.push({movq: [{literal: 1}, "rax"]});
ir4.push({call: "printf"});
ir4.push({ movq: [{literal: 0}, "rax"]});
ir4.push({leave: ""});
ir4.push({ret: ""});
return ir4;
}