phantasmal-world/src/scripting/data_flow_analysis/register_values.ts

192 lines
6.6 KiB
TypeScript
Raw Normal View History

import { Instruction } from "../instructions";
import { Opcode, ParamAccess, RegTupRefType } from "../opcodes";
import { BasicBlock, ControlFlowGraph } from "./ControlFlowGraph";
import { ValueSet } from "./ValueSet";
export const MIN_REGISTER_VALUE = -Math.pow(2, 31);
export const MAX_REGISTER_VALUE = Math.pow(2, 31) - 1;
export const REGISTER_VALUES = Math.pow(2, 32);
/**
* Computes the possible values of a register at a specific instruction.
*/
export function register_values(
cfg: ControlFlowGraph,
instruction: Instruction,
register: number
): ValueSet {
const block = cfg.get_block_for_instuction(instruction);
if (block) {
let inst_idx = block.start;
while (inst_idx < block.end) {
if (block.segment.instructions[inst_idx] === instruction) {
break;
}
inst_idx++;
}
return find_values(new Set(), block, inst_idx, register);
} else {
return new ValueSet();
}
}
function find_values(
path: Set<BasicBlock>,
block: BasicBlock,
end: number,
register: number
): ValueSet {
let values = new ValueSet();
for (let i = block.start; i < end; i++) {
const instruction = block.segment.instructions[i];
const args = instruction.args;
switch (instruction.opcode) {
case Opcode.LET:
if (args[0].value === register) {
values = find_values(new Set(path), block, i, args[1].value);
}
break;
case Opcode.LETI:
case Opcode.LETB:
case Opcode.LETW:
case Opcode.SYNC_LETI:
if (args[0].value === register) {
values.set_value(args[1].value);
}
break;
case Opcode.SET:
if (args[0].value === register) {
values.set_value(1);
}
break;
case Opcode.CLEAR:
if (args[0].value === register) {
values.set_value(0);
}
break;
case Opcode.REV:
if (args[0].value === register) {
const prev_vals = find_values(new Set(path), block, i, register);
const prev_size = prev_vals.size();
if (prev_size === 0 || (prev_size === 1 && prev_vals.get(0) === 0)) {
values.set_value(1);
} else if (values.has(0)) {
values.set_interval(0, 1);
} else {
values.set_value(0);
}
}
break;
case Opcode.ADDI:
if (args[0].value === register) {
values = find_values(new Set(path), block, i, register);
values.scalar_add(args[1].value);
}
break;
case Opcode.SUBI:
if (args[0].value === register) {
values = find_values(new Set(path), block, i, register);
values.scalar_sub(args[1].value);
}
break;
case Opcode.MULI:
if (args[0].value === register) {
values = find_values(new Set(path), block, i, register);
values.scalar_mul(args[1].value);
}
break;
case Opcode.DIVI:
if (args[0].value === register) {
values = find_values(new Set(path), block, i, register);
values.scalar_div(args[1].value);
}
break;
case Opcode.IF_ZONE_CLEAR:
if (args[0].value === register) {
values.set_interval(0, 1);
}
break;
case Opcode.GET_DIFFLVL:
case Opcode.GET_SLOTNUMBER:
if (args[0].value === register) {
values.set_interval(0, 3);
}
break;
case Opcode.GET_RANDOM:
if (args[1].value === register) {
// TODO: undefined values.
const min = find_values(new Set(path), block, i, args[0].value).min() || 0;
const max = Math.max(
find_values(new Set(path), block, i, args[0].value + 1).max() || 0,
min + 1
);
values.set_interval(min, max - 1);
}
break;
case Opcode.STACK_PUSHM:
case Opcode.STACK_POPM:
{
const min_reg = args[0].value;
const max_reg = args[0].value + args[1].value;
if (min_reg <= register && register < max_reg) {
values.set_interval(MIN_REGISTER_VALUE, MAX_REGISTER_VALUE);
}
}
break;
default:
// Assume any other opcodes that write to the register can produce any value.
{
const params = instruction.opcode.params;
const arg_len = Math.min(args.length, params.length);
outer: for (let j = 0; j < arg_len; j++) {
const param = params[j];
if (param.type instanceof RegTupRefType) {
const reg_ref = args[j].value;
let k = 0;
for (const reg_param of param.type.registers) {
if (
(reg_param.access === ParamAccess.Write ||
reg_param.access === ParamAccess.ReadWrite) &&
reg_ref + k === register
) {
values.set_interval(MIN_REGISTER_VALUE, MAX_REGISTER_VALUE);
break outer;
}
k++;
}
}
}
}
break;
}
}
if (values.size() === 0) {
path.add(block);
for (const from of block.from) {
// Bail out from loops.
if (path.has(from)) {
values.set_interval(MIN_REGISTER_VALUE, MAX_REGISTER_VALUE);
break;
}
values.union(find_values(new Set(path), from, from.end, register));
}
}
return values;
}