Skip to content

Commit 7a61401

Browse files
committed
Fix stack effect calculation to exclude the unpack opcode which was removed.
1 parent e2b6961 commit 7a61401

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

compiler/src/stack_effect.rs

-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ pub fn stack_effect(instruction: &Instruction) -> isize {
7070
ListAppend { .. } => -1,
7171
SetAdd { .. } => -1,
7272
MapAdd { .. } => -2,
73-
Unpack => {
74-
unimplemented!("we cannot know the effect of this instruction on the stack :(");
75-
}
7673
UnpackEx { before, after } => -1 + (*before as isize) + (*after as isize) + 1,
7774
UnpackSequence { size } => -1 + (*size as isize),
7875
SetupLoop { .. } => 0,

vm/src/frame.rs

+9-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::RefCell;
1+
use std::cell::{Cell, RefCell};
22
use std::fmt;
33

44
use crate::builtins;
@@ -85,7 +85,7 @@ pub struct Frame {
8585
stack: RefCell<Vec<PyObjectRef>>, // The main data frame of the stack machine
8686
blocks: RefCell<Vec<Block>>, // Block frames, for controlling loops and exceptions
8787
pub scope: Scope, // Variables
88-
lasti: RefCell<usize>, // index of last instruction ran
88+
lasti: Cell<usize>, // index of last instruction ran
8989
}
9090

9191
impl PyValue for Frame {
@@ -105,27 +105,15 @@ pub type FrameResult = PyResult<Option<ExecutionResult>>;
105105

106106
impl Frame {
107107
pub fn new(code: PyCodeRef, scope: Scope) -> Frame {
108-
//populate the globals and locals
109-
//TODO: This is wrong, check https://github.com/nedbat/byterun/blob/31e6c4a8212c35b5157919abff43a7daa0f377c6/byterun/pyvm2.py#L95
110-
/*
111-
let globals = match globals {
112-
Some(g) => g,
113-
None => HashMap::new(),
114-
};
115-
*/
116-
// let locals = globals;
117-
// locals.extend(callargs);
118108
let code = code.code.clone();
119-
let max_stack_size = 32; // code.max_stack_size;
109+
let max_stack_size = code.max_stack_size;
120110

121111
Frame {
122112
code,
123113
stack: RefCell::new(Vec::with_capacity(max_stack_size)),
124114
blocks: RefCell::new(vec![]),
125-
// save the callargs as locals
126-
// globals: locals.clone(),
127115
scope,
128-
lasti: RefCell::new(0),
116+
lasti: Cell::new(0),
129117
}
130118
}
131119

@@ -191,8 +179,8 @@ impl Frame {
191179
}
192180

193181
pub fn fetch_instruction(&self) -> &bytecode::Instruction {
194-
let ins2 = &self.code.instructions[*self.lasti.borrow()];
195-
*self.lasti.borrow_mut() += 1;
182+
let ins2 = &self.code.instructions[self.get_lasti()];
183+
self.lasti.set(self.lasti.get() + 1);
196184
ins2
197185
}
198186

@@ -951,7 +939,7 @@ impl Frame {
951939
match next_obj {
952940
Some(value) => {
953941
// Set back program counter:
954-
*self.lasti.borrow_mut() -= 1;
942+
self.lasti.set(self.get_lasti() - 1);
955943
Ok(Some(ExecutionResult::Yield(value)))
956944
}
957945
None => Ok(None),
@@ -1024,7 +1012,7 @@ impl Frame {
10241012
}
10251013

10261014
pub fn get_lasti(&self) -> usize {
1027-
*self.lasti.borrow()
1015+
self.lasti.get()
10281016
}
10291017

10301018
fn execute_make_function(
@@ -1225,7 +1213,7 @@ impl Frame {
12251213
}
12261214

12271215
pub fn get_lineno(&self) -> bytecode::Location {
1228-
self.code.locations[*self.lasti.borrow()].clone()
1216+
self.code.locations[self.get_lasti()].clone()
12291217
}
12301218

12311219
fn push_block(&self, typ: BlockType) {
@@ -1251,7 +1239,6 @@ impl Frame {
12511239

12521240
pub fn push_value(&self, obj: PyObjectRef) {
12531241
self.stack.borrow_mut().push(obj);
1254-
// println!("Stack size: {}, capa: {}", self.stack.borrow().len(), self.stack.borrow().capacity());
12551242
}
12561243

12571244
fn pop_value(&self) -> PyObjectRef {

0 commit comments

Comments
 (0)