refactor(tvix/eval): index into Chunk with ConstantIdx/CodeIdx
This is a step towards hiding the internal fields of thunk, and making the interface of the type more predictable. Part of the preparation for implementing observers. Change-Id: I1a88a96419c72eb9e2332b56a2dd94afa47e6f88 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6447 Reviewed-by: sterni <sternenseemann@systemli.org> Tested-by: BuildkiteCI
This commit is contained in:
		
							parent
							
								
									8c64ebe074
								
							
						
					
					
						commit
						1fe6cfe5a2
					
				
					 2 changed files with 22 additions and 8 deletions
				
			
		| 
						 | 
					@ -1,3 +1,5 @@
 | 
				
			||||||
 | 
					use std::ops::Index;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
 | 
					use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
 | 
				
			||||||
use crate::value::Value;
 | 
					use crate::value::Value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -31,6 +33,22 @@ pub struct Chunk {
 | 
				
			||||||
    pub codemap: std::rc::Rc<codemap::CodeMap>,
 | 
					    pub codemap: std::rc::Rc<codemap::CodeMap>,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Index<ConstantIdx> for Chunk {
 | 
				
			||||||
 | 
					    type Output = Value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn index(&self, index: ConstantIdx) -> &Self::Output {
 | 
				
			||||||
 | 
					        &self.constants[index.0]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl Index<CodeIdx> for Chunk {
 | 
				
			||||||
 | 
					    type Output = OpCode;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn index(&self, index: CodeIdx) -> &Self::Output {
 | 
				
			||||||
 | 
					        &self.code[index.0]
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Chunk {
 | 
					impl Chunk {
 | 
				
			||||||
    pub fn push_op(&mut self, data: OpCode, span: codemap::Span) -> CodeIdx {
 | 
					    pub fn push_op(&mut self, data: OpCode, span: codemap::Span) -> CodeIdx {
 | 
				
			||||||
        let idx = self.code.len();
 | 
					        let idx = self.code.len();
 | 
				
			||||||
| 
						 | 
					@ -45,10 +63,6 @@ impl Chunk {
 | 
				
			||||||
        ConstantIdx(idx)
 | 
					        ConstantIdx(idx)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn constant(&self, idx: ConstantIdx) -> &Value {
 | 
					 | 
				
			||||||
        &self.constants[idx.0]
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Span tracking implementation
 | 
					    // Span tracking implementation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn push_span(&mut self, span: codemap::Span) {
 | 
					    fn push_span(&mut self, span: codemap::Span) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -218,7 +218,7 @@ impl VM {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            match op {
 | 
					            match op {
 | 
				
			||||||
                OpCode::OpConstant(idx) => {
 | 
					                OpCode::OpConstant(idx) => {
 | 
				
			||||||
                    let c = self.chunk().constant(idx).clone();
 | 
					                    let c = self.chunk()[idx].clone();
 | 
				
			||||||
                    self.push(c);
 | 
					                    self.push(c);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -477,7 +477,7 @@ impl VM {
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                OpCode::OpClosure(idx) => {
 | 
					                OpCode::OpClosure(idx) => {
 | 
				
			||||||
                    let blueprint = match self.chunk().constant(idx) {
 | 
					                    let blueprint = match &self.chunk()[idx] {
 | 
				
			||||||
                        Value::Blueprint(lambda) => lambda.clone(),
 | 
					                        Value::Blueprint(lambda) => lambda.clone(),
 | 
				
			||||||
                        _ => panic!("compiler bug: non-blueprint in blueprint slot"),
 | 
					                        _ => panic!("compiler bug: non-blueprint in blueprint slot"),
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
| 
						 | 
					@ -501,7 +501,7 @@ impl VM {
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                OpCode::OpThunk(idx) => {
 | 
					                OpCode::OpThunk(idx) => {
 | 
				
			||||||
                    let blueprint = match self.chunk().constant(idx) {
 | 
					                    let blueprint = match &self.chunk()[idx] {
 | 
				
			||||||
                        Value::Blueprint(lambda) => lambda.clone(),
 | 
					                        Value::Blueprint(lambda) => lambda.clone(),
 | 
				
			||||||
                        _ => panic!("compiler bug: non-blueprint in blueprint slot"),
 | 
					                        _ => panic!("compiler bug: non-blueprint in blueprint slot"),
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
| 
						 | 
					@ -594,7 +594,7 @@ impl VM {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn resolve_dynamic_upvalue(&mut self, ident_idx: ConstantIdx) -> EvalResult<Value> {
 | 
					    fn resolve_dynamic_upvalue(&mut self, ident_idx: ConstantIdx) -> EvalResult<Value> {
 | 
				
			||||||
        let chunk = self.chunk();
 | 
					        let chunk = self.chunk();
 | 
				
			||||||
        let ident = fallible!(self, chunk.constant(ident_idx).to_str());
 | 
					        let ident = fallible!(self, chunk[ident_idx].to_str());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Peek at the current instruction (note: IP has already
 | 
					        // Peek at the current instruction (note: IP has already
 | 
				
			||||||
        // advanced!) to see if it is actually data indicating a
 | 
					        // advanced!) to see if it is actually data indicating a
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue