Skip to content

Commit

Permalink
Agregado de numeros con punto fijo
Browse files Browse the repository at this point in the history
  • Loading branch information
Maleriandro committed Dec 11, 2022
1 parent fcde608 commit fa58ed5
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 47 deletions.
8 changes: 4 additions & 4 deletions CompIR1Translator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Deno.test("create nonexistent instructions", () => {

assert(typeof ir2[1] == "object" &&
"value" in ir2[1] &&
JSON.stringify(ir2[1].value) == '{"literal":5}')
JSON.stringify(ir2[1].value) == '{"literal":21474836480}')
})


Expand Down Expand Up @@ -223,12 +223,12 @@ Deno.test("extra: prueba funcion", () => {
{jmp: "l0",},
{lbl: "inc",},
{functionIntro: [1]},
{return: {binop: "+", argl: 1, argr: {literal: 1}}},
{return: {binop: "+", argl: 1, argr: {literal: 1*2**32}}},
{lbl: "l0",},
{set: 2, value: {literal: 5}},
{set: 2, value: {literal: 5*2**32}},
{set: 0, value: {
call: "inc",
args: [{literal:5}],
args: [{literal:5*2**32}],
},
},
])
Expand Down
2 changes: 1 addition & 1 deletion CompIR1Translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function translateExpr(
}

if (typeof expr == "number") {
return {literal: expr};
return {literal: Math.round(expr*2**32)};
}

throw new Error("No debería llegar nunca");
Expand Down
68 changes: 53 additions & 15 deletions CompIR2Translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,32 @@ const set_rax_0:CompIR3 = {movq: [{literal: 0}, "rax"]};
const binops_map:Record<Binops, Array<CompIR3>> = {
"+": [{addq: operators_const}],
"-": [{subq: operators_const}],
"*": [{imulq: operators_const}],
"*": [
{imulq: "rbx"},
{shrd: [{literal: 32}, "rdx", "rax"]},
{sarq: [{literal: 32}, "rdx"]}
],
"/": [
{cqto: ""},
{idivq: "rbx"}
{shld: [{literal: 32}, "rax", "rdx"]},
{salq: [{literal: 32}, "rax"]},
{idivq: "rbx"},
],

"^": [
{movq: [{literal: 1}, "rcx"]},
{lbl: "0"},
{cmpq: [{literal: 0}, "rbx"]},
{je: "9f"},
{imulq: ["rax", "rcx"]},
{dec: "rbx"},
{jmp: "0b"},

{lbl: "9"},
{movq: ["rcx", "rax"]}
{pxor: ["xmm0", "xmm0"]},
{pxor: ["xmm1", "xmm1"]},

{cvtsi2sdq: ["rax", "xmm0"]},
{divsd: [{relative: "dividend"}, "xmm0"]},

{cvtsi2sdq: ["rbx", "xmm1"]},
{divsd: [{relative: "dividend"}, "xmm1"]},

{callEnd: "pow@PLT"},

{mulsd: [{relative: "dividend"}, "xmm0"]},
{cvttsd2siq: ["xmm0", "rax"]}
],

"%": [
Expand All @@ -37,41 +46,53 @@ const binops_map:Record<Binops, Array<CompIR3>> = {
"|": [{orq: operators_const}],
">>": [
{movq: ["rbx", "rcx"]},
{sarq: [{literal: 32}, "rcx"]},
{sarq: ["cl", "rax"]},
{movq: [{relative: "mask_int"}, "rbx"]},
{andq: ["rbx", "rax"]},
],
"<<": [
{movq: ["rbx", "rcx"]},
{sarq: [{literal: 32}, "rcx"]},
{movq: [{relative: "mask_int"}, "rbx"]},
{andq: ["rbx", "rax"]},
{salq: ["cl", "rax"]},
],
"<": [
{cmpq: operators_const},
set_rax_0,
{setl: "al"},
{salq: [{literal: 32}, "rax"]}
],
"<=": [
{cmpq: operators_const},
set_rax_0,
{setle: "al"},
{salq: [{literal: 32}, "rax"]}
],
">": [
{cmpq: operators_const},
set_rax_0,
{setg: "al"},
{salq: [{literal: 32}, "rax"]}
],
">=": [
{cmpq: operators_const},
set_rax_0,
{setge: "al"},
{salq: [{literal: 32}, "rax"]}
],
"==": [
{cmpq: operators_const},
set_rax_0,
{sete: "al"},
{salq: [{literal: 32}, "rax"]}
],
"~=": [
{cmpq: operators_const},
set_rax_0,
{setne: "al"},
{salq: [{literal: 32}, "rax"]}
],

"and": [
Expand Down Expand Up @@ -111,9 +132,14 @@ const unops_map:Record<"-"|"!"|"~", Array<CompIR3>> = {
"!": [
{cmpq: [{literal: 0} , "rax"]},
{movq: [{literal: 0}, "rax"]},
{sete: "al"}
{sete: "al"},
{salq: [{literal: 32}, "rax"]}
],
"~": [{notq: "rax"}]
"~": [
{negq: "rax"},
{movq: [{literal: 0x100000000}, "rbx"]},
{subq: ["rbx", "rax"]}
]
}

function translateOne(stmt: StatementIR2<Expression>): StatementIR3[] {
Expand Down Expand Up @@ -194,10 +220,22 @@ export function translate(code: CompIR2[]): CompIR3[] {
// caso base: pushea en stack el valor
// no caso base, popea en B, popea en A, realiza op B,A; luego pushea A
function translateExpr(expr: Expression): StatementIR3[] {
if (typeof expr == "number" || "literal" in expr) {
if (typeof expr == "number") {
return [{pushq: expr}];
}

if ("literal" in expr) {

if (expr.literal > 2147483647 || expr.literal < -2147483647) {
return [
{movq: [{literal: expr.literal}, "rax"]},
{pushq: "rax"}
]
}

return [{pushq: {literal: expr.literal}}]
}

if ("call" in expr) {
return [...translateOne(expr),
{pushq: "rax"}];
Expand Down
21 changes: 18 additions & 3 deletions CompIR3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ export type StatementIR3 =
type Binops =
| {addq: double_operand} // "+"
| {subq: double_operand} // "-"
| {imulq: [Data, Register]} // | "*"
| {imulq: [Data, Register]|Data} // | "*"
| {shrd: ["cl"|Literal, Register, Data]}

| {idivq: Data} // | "/"
| {shld: ["cl"|Literal, Register, Data]}
// | call pow() // | "^"
// | {} // | "%"
| {andq: double_operand} // | "&"
Expand All @@ -41,8 +44,13 @@ type Binops =
// | {} // | "and"
// | {} ;// | "or";
| {xorq: double_operand}
| {pxor: double_operand} //double
| {dec: Data}
| {cqto: ""} //Extiende el signo de RAX a RDX
| {cvtsi2sdq: double_operand} //Convertir numero a double
| {cvttsd2siq: double_operand} //Convertir double a entero
| {divsd: double_operand} //dividir float
| {mulsd: double_operand} //Multiplicar float

type Unops =
| {negq: Data} // | "neg"
Expand All @@ -51,7 +59,8 @@ type Unops =

export type Data =
| Stack_ubication
| Register;
| Register
| Relative_Label

type Data_or_literal =
| Data
Expand Down Expand Up @@ -94,12 +103,18 @@ type Register =
| "r13"
| "r14"
| "r15"
| ip_register;
| ip_register
| "xmm0"
| "xmm1"
| "xmm2"

type register8bit =
| "al"
| "bl"
| "cl"
| "dl"


export type Relative_Label = {relative: Label};

export type CompIR3 = StatementIR3;
21 changes: 16 additions & 5 deletions CompIR3Translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ export function translate(code: CompIR3[]): CompIR4[] {
ir4.push({directive: [".global", "main"]})

ir4.push({lbl: "str_print"});
ir4.push({directive: [".string", "%li\n"]});
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"});

Expand Down Expand Up @@ -48,11 +56,14 @@ export function translate(code: CompIR3[]): CompIR4[] {
ir4.push(instruccion);
}
}

ir4.push({cvtsi2sdq: [0, "xmm0"]});
ir4.push({divsd: [{relative: "dividend"}, "xmm0"]});

ir4.push({ movq: [{literal: 0}, "rax"]});
ir4.push({ movq: [0, "rsi"]});
ir4.push({ leaq: [{relative: "str_print"}, "rdi"]});
ir4.push({ call: "printf"});
ir4.push({leaq: [{relative: "str_print"}, "rdi"]});
ir4.push({movq: [{literal: 1}, "rax"]});

ir4.push({call: "printf"});


ir4.push({ movq: [{literal: 0}, "rax"]});
Expand Down
28 changes: 21 additions & 7 deletions CompIR3TranslatorIT.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ Deno.test("simple function from the header called below", () => {
assertEquals(ir4, [
{ directive: [".global", "main"] },
{ lbl: "str_print"},
{ directive: [".string", "%li\n"]},
{ directive: [".string", "%lf\n"]},
{ lbl: "dividend"},
{ directive: [".double", "4294967296"]},
{ lbl: "mask_int"},
{ directive: [".long", "0"]},
{ directive: [".long", "0xFFFFFFFF"]},
{ lbl: "main"},
{ jmp: "after_fun" },
{ lbl: "the_fun" },
Expand All @@ -32,9 +37,11 @@ Deno.test("simple function from the header called below", () => {
{ call: "the_fun"},
{ pushq: "rax" },
{ popq: 0 },
{ movq: [0, "rsi"]},
{ cvtsi2sdq: [0, "xmm0"] },
{ divsd: [{relative: "dividend"}, "xmm0"]},
{ leaq: [{relative: "str_print"}, "rdi"]},
{ call: "printf"},
{ movq: [{literal: 1}, "rax"]},
{ call: "printf" },
{ movq: [{literal: 0}, "rax"]},
{ leave: "" },
{ ret: "" }
Expand All @@ -51,7 +58,12 @@ Deno.test("simple function from the header called below", () => {
assertEquals(ir4, [
{ directive: [".global", "main"] },
{ lbl: "str_print"},
{ directive: [".string", "%li\n"]},
{ directive: [".string", "%lf\n"]},
{ lbl: "dividend"},
{ directive: [".double", "4294967296"]},
{ lbl: "mask_int"},
{ directive: [".long", "0"]},
{ directive: [".long", "0xFFFFFFFF"]},
{ lbl: "main"},
{ lbl: "the_fun" },
{ movq: ["rdi",1]},
Expand All @@ -67,12 +79,14 @@ Deno.test("simple function from the header called below", () => {
{ pushq: {literal:10}},
{ popq: "rax" },
{ ret: "" },
{ movq: [0, "rsi"]},
{ cvtsi2sdq: [0, "xmm0"] },
{ divsd: [{relative: "dividend"}, "xmm0"]},
{ leaq: [{relative: "str_print"}, "rdi"]},
{ call: "printf"},
{ movq: [{literal: 1}, "rax"]},
{ call: "printf" },
{ movq: [{literal: 0}, "rax"]},
{ leave: "" },
{ ret: "" }
{ ret: "" }
])
});

Expand Down
19 changes: 16 additions & 3 deletions CompIR4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,11 @@ export type StatementIR4 =
type Binops =
| {addq: double_operand} // "+"
| {subq: double_operand} // "-"
| {imulq: [Data, Register]} // | "*"
| {imulq: [Data, Register]|Data} // | "*"
| {shrd: ["cl"|Literal, Register, Data]}

| {idivq: Data} // | "/"
| {shld: ["cl"|Literal, Register, Data]}
// | call pow() // | "^"
// | {} // | "%"
| {andq: double_operand} // | "&"
Expand All @@ -54,8 +57,14 @@ export type StatementIR4 =
// | {} // | "and"
// | {} ;// | "or";
| {xorq: double_operand}
| {pxor: double_operand}
| {dec: Data}
| {cqto: ""} //Extiende el signo de RAX a RDX
| {cvtsi2sdq: double_operand} //Convertir numero a double
| {cvttsd2siq: double_operand} //Convertir double a entero
| {divsd: double_operand} //dividir float
| {mulsd: double_operand} //Multiplicar float


type Unops =
| {negq: Data} // | "neg"
Expand All @@ -64,7 +73,8 @@ type Unops =

type Data =
| Stack_ubication
| Register;
| Register
| Relative_Label;

type Data_or_literal =
| Data
Expand Down Expand Up @@ -108,7 +118,10 @@ export type Register =
| "r13"
| "r14"
| "r15"
| ip_register;
| ip_register
| "xmm0"
| "xmm1"
| "xmm2"

type register8bit =
| "al"
Expand Down
Loading

0 comments on commit fa58ed5

Please sign in to comment.