import type { AbsInsn2 } from "./insn"
|
|
import type { Loc } from "./loc"
|
|
|
|
export class BasicBlock {
|
|
insns: Array<AbsInsn2>
|
|
labels: { [label: string]: number }
|
|
|
|
constructor(insns: Array<AbsInsn2>) {
|
|
this.insns = insns
|
|
this.labels = this.insns
|
|
.reduce((labels, insn, i) => {
|
|
if (insn.type !== "label") {
|
|
return labels
|
|
}
|
|
|
|
return {
|
|
...labels,
|
|
[insn.dest]: i,
|
|
}
|
|
}, {})
|
|
}
|
|
|
|
locs(): Set<Loc> {
|
|
return new Set(
|
|
this.insns.flatMap(insn => {
|
|
const insnLocs = []
|
|
|
|
if (insn.type === "copy" || insn.type === "unary") {
|
|
insnLocs.push(insn.dest)
|
|
|
|
if (typeof insn.source !== "number") {
|
|
insnLocs.push(insn.source)
|
|
}
|
|
}
|
|
|
|
return insnLocs
|
|
})
|
|
)
|
|
}
|
|
}
|