layer: usize -> u8

This commit is contained in:
2026-05-01 15:30:21 +08:00
parent 0ca5e8af92
commit 0df38f374f
5 changed files with 28 additions and 20 deletions
+12 -12
View File
@@ -103,7 +103,7 @@ pub enum Op {
}
struct ScopeInfo {
depth: u16,
depth: u8,
thunk_map: HashMap<ThunkId, u32>,
}
@@ -143,7 +143,7 @@ pub enum AttrKeyType {
pub enum InlineOperand {
Const(Const),
Local { layer: u16, local: u32 },
Local { layer: u8, local: u32 },
Builtins,
BigInt(i64),
}
@@ -168,8 +168,8 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
use MaybeThunk::*;
match val {
Int(x) => {
if x <= i32::MAX as i64 {
InlineOperand::Const(Const::Smi(x as i32))
if let Ok(x) = x.try_into() {
InlineOperand::Const(Const::Smi(x))
} else {
InlineOperand::BigInt(x)
}
@@ -183,7 +183,7 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
InlineOperand::Local { layer, local }
}
Arg { layer } => InlineOperand::Local {
layer: layer.try_into().expect("scope too deep!"),
layer,
local: 0,
},
_ => todo!(),
@@ -200,7 +200,7 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
}
InlineOperand::Local { layer, local } => {
self.emit_u8(OperandType::Local as u8);
self.emit_u8(layer as u8);
self.emit_u8(layer);
self.emit_u32(local);
}
InlineOperand::Builtins => {
@@ -289,11 +289,11 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
.extend_from_slice(&(id.0.to_usize() as u32).to_le_bytes());
}
fn current_depth(&self) -> u16 {
fn current_depth(&self) -> u8 {
self.scope_stack.last().map_or(0, |s| s.depth)
}
fn resolve_thunk(&self, id: ThunkId) -> (u16, u32) {
fn resolve_thunk(&self, id: ThunkId) -> (u8, u32) {
for scope in self.scope_stack.iter().rev() {
if let Some(&local_idx) = scope.thunk_map.get(&id) {
let layer = self.current_depth() - scope.depth;
@@ -303,13 +303,13 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
panic!("ThunkId {:?} not found in any scope", id);
}
fn emit_load(&mut self, layer: u16, local: u32) {
fn emit_load(&mut self, layer: u8, local: u32) {
if layer == 0 {
self.emit_op(Op::LoadLocal);
self.emit_u32(local);
} else {
self.emit_op(Op::LoadOuter);
self.emit_u8(layer as u8);
self.emit_u8(layer);
self.emit_u32(local);
}
}
@@ -407,7 +407,7 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
}
fn push_scope(&mut self, has_arg: bool, thunk_ids: &[ThunkId]) {
let depth = self.scope_stack.len() as u16;
let depth = self.scope_stack.len().try_into().expect("scope too deep!");
let thunk_base = if has_arg { 1u32 } else { 0u32 };
let thunk_map = thunk_ids
.iter()
@@ -554,7 +554,7 @@ impl<'a, Ctx: BytecodeContext> BytecodeEmitter<'a, Ctx> {
self.emit_maybe_thunk(arg);
}
&Ir::Arg { layer } => {
self.emit_load(layer.try_into().expect("scope too deep!"), 0);
self.emit_load(layer, 0);
}
&Ir::TopLevel { body, ref thunks } => {
self.emit_toplevel_inner(body, thunks);