Temp commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::fmt::Debug;
|
||||
|
||||
use crate::{
|
||||
flatten::{Capture, FlatFn},
|
||||
parse::{ImmOperand, Instr, JsValue, opcodes::OpCode},
|
||||
};
|
||||
|
||||
pub struct RegInstr {
|
||||
pub dsts: Vec<usize>,
|
||||
pub op: OpCode,
|
||||
pub imm: ImmOperand,
|
||||
pub args: Vec<usize>,
|
||||
}
|
||||
|
||||
impl Debug for RegInstr {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(
|
||||
f,
|
||||
"{:?} = {:?} {:?} {:?}",
|
||||
self.dsts, self.op, self.imm, self.args
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A `FlatFn` with its stack bytecode lowered to register code.
|
||||
#[derive(Debug)]
|
||||
pub struct RegFn {
|
||||
pub name: String,
|
||||
pub arg_count: usize,
|
||||
pub var_count: usize,
|
||||
pub captures: Vec<Capture>,
|
||||
pub cpool: Vec<JsValue>,
|
||||
pub code: Vec<RegInstr>,
|
||||
/// Set by `declass` when this function is a class constructor.
|
||||
pub is_ctor: bool,
|
||||
}
|
||||
|
||||
fn regify_walk(code: &[Instr], var_count: usize) -> Vec<RegInstr> {
|
||||
let mut stack = vec![];
|
||||
let mut output = vec![];
|
||||
let mut next_val = var_count;
|
||||
|
||||
let mut fresh_val = || {
|
||||
let v = next_val;
|
||||
next_val += 1;
|
||||
v
|
||||
};
|
||||
|
||||
for instr in code {
|
||||
let mut reg_instr = RegInstr {
|
||||
dsts: vec![],
|
||||
op: instr.op,
|
||||
imm: instr.imm_operands,
|
||||
args: vec![],
|
||||
};
|
||||
|
||||
let extra_pop = match instr.op {
|
||||
OpCode::Call0 => 0,
|
||||
OpCode::Call1 => 1,
|
||||
OpCode::Call2 => 2,
|
||||
OpCode::Call3 => 3,
|
||||
_ => match instr.imm_operands {
|
||||
ImmOperand::Npop(npop) => npop as usize,
|
||||
ImmOperand::NpopU16 { npop, .. } => npop as usize,
|
||||
_ => 0,
|
||||
},
|
||||
};
|
||||
|
||||
let info = instr.op.info();
|
||||
let n_pop = info.n_pop as usize + extra_pop;
|
||||
let n_push = info.n_push as usize;
|
||||
|
||||
for _ in 0..n_pop {
|
||||
let v = stack.pop().unwrap();
|
||||
reg_instr.args.push(v);
|
||||
}
|
||||
|
||||
for _ in 0..n_push {
|
||||
let v = fresh_val();
|
||||
stack.push(v);
|
||||
reg_instr.dsts.push(v);
|
||||
}
|
||||
|
||||
output.push(reg_instr);
|
||||
}
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
/// Lower every function's stack code to register code, keeping the rest.
|
||||
pub fn regify(flat_fns: Vec<FlatFn>) -> Vec<RegFn> {
|
||||
flat_fns
|
||||
.into_iter()
|
||||
.map(|f| RegFn {
|
||||
code: regify_walk(&f.code, f.var_count),
|
||||
name: f.name,
|
||||
arg_count: f.arg_count,
|
||||
var_count: f.var_count,
|
||||
captures: f.captures,
|
||||
cpool: f.cpool,
|
||||
is_ctor: false,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
Reference in New Issue
Block a user