|
|
@ -0,0 +1,259 @@ |
|
|
|
import { expect } from "chai" |
|
|
|
import { convertAssignStmt, prettyPrintIR, SSA } from "../lib/ir" |
|
|
|
|
|
|
|
describe("ir", () => { |
|
|
|
describe("convertExpr", () => { |
|
|
|
it("load with immediate", () => { |
|
|
|
const state = { nextID: 0, ssa_stmts: [] } |
|
|
|
const ir = convertAssignStmt(state, { |
|
|
|
type: "stmt", |
|
|
|
stmt_type: "assign", |
|
|
|
args: { |
|
|
|
name: "x", |
|
|
|
expr: 42, |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
expect(ir).to.deep.equal([ |
|
|
|
{ |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: 42, |
|
|
|
}, |
|
|
|
]) |
|
|
|
}) |
|
|
|
|
|
|
|
it("load with variable", () => { |
|
|
|
const state = { nextID: 0, ssa_stmts: [] } |
|
|
|
const ir = convertAssignStmt(state, { |
|
|
|
type: "stmt", |
|
|
|
stmt_type: "assign", |
|
|
|
args: { |
|
|
|
name: "x", |
|
|
|
expr: "y", |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
expect(ir).to.deep.equal([ |
|
|
|
{ |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y", |
|
|
|
}, |
|
|
|
}, |
|
|
|
]) |
|
|
|
}) |
|
|
|
|
|
|
|
it("unary op", () => { |
|
|
|
const state = { nextID: 0, ssa_stmts: [] } |
|
|
|
const ir = convertAssignStmt(state, { |
|
|
|
type: "stmt", |
|
|
|
stmt_type: "assign", |
|
|
|
args: { |
|
|
|
name: "x", |
|
|
|
expr: { |
|
|
|
type: "unary", |
|
|
|
op: "arith_negate", |
|
|
|
arg: "y" |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
expect(ir).to.deep.equal([ |
|
|
|
{ |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y", |
|
|
|
}, |
|
|
|
op: "arith_negate", |
|
|
|
}, |
|
|
|
]) |
|
|
|
}) |
|
|
|
|
|
|
|
it("binary op", () => { |
|
|
|
const state = { nextID: 0, ssa_stmts: [] } |
|
|
|
const ir = convertAssignStmt(state, { |
|
|
|
type: "stmt", |
|
|
|
stmt_type: "assign", |
|
|
|
args: { |
|
|
|
name: "x", |
|
|
|
expr: { |
|
|
|
type: "binary", |
|
|
|
op: "add", |
|
|
|
left: "y", |
|
|
|
right: 42, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
expect(ir).to.deep.equal([ |
|
|
|
{ |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y", |
|
|
|
}, |
|
|
|
source1: 42, |
|
|
|
op: "add", |
|
|
|
}, |
|
|
|
]) |
|
|
|
}) |
|
|
|
|
|
|
|
it("nested binary op", () => { |
|
|
|
const state = { nextID: 0, ssa_stmts: [] } |
|
|
|
const ir = convertAssignStmt(state, { |
|
|
|
type: "stmt", |
|
|
|
stmt_type: "assign", |
|
|
|
args: { |
|
|
|
name: "x", |
|
|
|
expr: { |
|
|
|
type: "binary", |
|
|
|
op: "add", |
|
|
|
left: "y", |
|
|
|
right: { |
|
|
|
type: "binary", |
|
|
|
op: "subtract", |
|
|
|
left: "z", |
|
|
|
right: 42, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}, |
|
|
|
}) |
|
|
|
|
|
|
|
expect(ir).to.deep.equal([ |
|
|
|
{ |
|
|
|
dest: { |
|
|
|
type: "temporary", |
|
|
|
name: "t0", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "z", |
|
|
|
}, |
|
|
|
op: "subtract", |
|
|
|
source1: 42, |
|
|
|
}, |
|
|
|
{ |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y", |
|
|
|
}, |
|
|
|
op: "add", |
|
|
|
source1: { |
|
|
|
type: "temporary", |
|
|
|
name: "t0", |
|
|
|
}, |
|
|
|
}, |
|
|
|
]) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
describe("prettyPrintIR", () => { |
|
|
|
it("load with immediate", () => { |
|
|
|
const ssa: SSA = { |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: 42, |
|
|
|
} |
|
|
|
|
|
|
|
expect(prettyPrintIR(ssa)).to.equal("x = 42") |
|
|
|
}) |
|
|
|
|
|
|
|
it("load with register", () => { |
|
|
|
const ssa: SSA = { |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "register", |
|
|
|
name: "A" |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
expect(prettyPrintIR(ssa)).to.equal("x = %A") |
|
|
|
}) |
|
|
|
|
|
|
|
it("load with temporary", () => { |
|
|
|
const ssa: SSA = { |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "temporary", |
|
|
|
name: "t0" |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
expect(prettyPrintIR(ssa)).to.equal("x = #t0") |
|
|
|
}) |
|
|
|
|
|
|
|
it("load with variable", () => { |
|
|
|
const ssa: SSA = { |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y" |
|
|
|
}, |
|
|
|
} |
|
|
|
|
|
|
|
expect(prettyPrintIR(ssa)).to.equal("x = y") |
|
|
|
}) |
|
|
|
|
|
|
|
it("unary op", () => { |
|
|
|
const ssa: SSA = { |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y" |
|
|
|
}, |
|
|
|
op: "arith_negate", |
|
|
|
} |
|
|
|
|
|
|
|
expect(prettyPrintIR(ssa)).to.equal("x = arith_negate(y)") |
|
|
|
}) |
|
|
|
|
|
|
|
it("binary op", () => { |
|
|
|
const ssa: SSA = { |
|
|
|
dest: { |
|
|
|
type: "variable", |
|
|
|
name: "x", |
|
|
|
}, |
|
|
|
source: { |
|
|
|
type: "variable", |
|
|
|
name: "y" |
|
|
|
}, |
|
|
|
source1: 42, |
|
|
|
op: "add", |
|
|
|
} |
|
|
|
|
|
|
|
expect(prettyPrintIR(ssa)).to.equal("x = add(y, 42)") |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |