import { expect } from "chai"
|
|
|
|
import { convertAssignStmt } from "../../lib/ir/convert"
|
|
import { Loc } from "../../lib/ir/loc"
|
|
|
|
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: Loc.vari("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: Loc.vari("x"),
|
|
source: Loc.vari("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: Loc.vari("x"),
|
|
source: Loc.vari("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: Loc.vari("x"),
|
|
source: Loc.vari("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: Loc.temp("t0"),
|
|
source: Loc.vari("z"),
|
|
op: "subtract",
|
|
source1: 42,
|
|
},
|
|
{
|
|
dest: Loc.vari("x"),
|
|
source: Loc.vari("y"),
|
|
op: "add",
|
|
source1: Loc.temp("t0"),
|
|
},
|
|
])
|
|
})
|
|
})
|
|
})
|