about summary refs log tree commit diff
path: root/tvix
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-26T17·54+0300
committertazjin <tazjin@tvl.su>2022-09-03T13·22+0000
commitc73e84d95777c304f1b208cbc43b01012df73262 (patch)
tree88efee3ba7d312afd5f9cc3875b960172aadd99d /tvix
parent2cdc6192b47dbba221d158de31800035c738fdc7 (diff)
refactor(tvix/eval): add opcode::StackIdx type for less ambiguity r/4621
Change-Id: I9b9de1f681972c205d4d20bc5731d2ce79858edb
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6287
Tested-by: BuildkiteCI
Reviewed-by: sterni <sternenseemann@systemli.org>
Diffstat (limited to 'tvix')
-rw-r--r--tvix/eval/src/compiler/mod.rs8
-rw-r--r--tvix/eval/src/opcode.rs9
-rw-r--r--tvix/eval/src/vm.rs6
3 files changed, 14 insertions, 9 deletions
diff --git a/tvix/eval/src/compiler/mod.rs b/tvix/eval/src/compiler/mod.rs
index ac22fc1a6794..37ec3915b4be 100644
--- a/tvix/eval/src/compiler/mod.rs
+++ b/tvix/eval/src/compiler/mod.rs
@@ -22,7 +22,7 @@ use std::rc::Rc;
 
 use crate::chunk::Chunk;
 use crate::errors::{Error, ErrorKind, EvalResult};
-use crate::opcode::{CodeIdx, JumpOffset, OpCode};
+use crate::opcode::{CodeIdx, JumpOffset, OpCode, StackIdx};
 use crate::value::{Closure, Lambda, Value};
 use crate::warnings::{EvalWarning, WarningKind};
 
@@ -116,11 +116,11 @@ impl Scope {
     }
 
     /// Resolve the stack index of a statically known local.
-    fn resolve_local(&mut self, name: &str) -> Option<usize> {
+    fn resolve_local(&mut self, name: &str) -> Option<StackIdx> {
         for (idx, local) in self.locals.iter_mut().enumerate().rev() {
             if !local.phantom && local.name == name {
                 local.used = true;
-                return Some(idx);
+                return Some(StackIdx(idx));
             }
         }
 
@@ -799,7 +799,7 @@ impl Compiler {
         self.scope_mut().with_stack.push(With { depth });
 
         let with_idx = self.scope().locals.len() - 1;
-        self.chunk().push_op(OpCode::OpPushWith(with_idx));
+        self.chunk().push_op(OpCode::OpPushWith(StackIdx(with_idx)));
 
         self.compile(node.body().unwrap());
     }
diff --git a/tvix/eval/src/opcode.rs b/tvix/eval/src/opcode.rs
index f265ef06d7f5..82b72952d698 100644
--- a/tvix/eval/src/opcode.rs
+++ b/tvix/eval/src/opcode.rs
@@ -11,6 +11,11 @@ pub struct ConstantIdx(pub usize);
 #[derive(Clone, Copy, Debug)]
 pub struct CodeIdx(pub usize);
 
+/// Index of a value in the runtime stack.
+#[repr(transparent)]
+#[derive(Clone, Copy, Debug)]
+pub struct StackIdx(pub usize);
+
 /// Offset by which an instruction pointer should change in a jump.
 #[repr(transparent)]
 #[derive(Clone, Copy, Debug)]
@@ -63,7 +68,7 @@ pub enum OpCode {
     OpAttrsIsSet,
 
     // `with`-handling
-    OpPushWith(usize),
+    OpPushWith(StackIdx),
     OpPopWith,
     OpResolveWith,
 
@@ -78,7 +83,7 @@ pub enum OpCode {
     OpAssertBool,
 
     // Access local identifiers with statically known positions.
-    OpGetLocal(usize),
+    OpGetLocal(StackIdx),
 
     // Close scopes while leaving their expression value around.
     OpCloseScope(usize), // number of locals to pop
diff --git a/tvix/eval/src/vm.rs b/tvix/eval/src/vm.rs
index a74051a64975..28b1636488af 100644
--- a/tvix/eval/src/vm.rs
+++ b/tvix/eval/src/vm.rs
@@ -6,7 +6,7 @@ use std::rc::Rc;
 use crate::{
     chunk::Chunk,
     errors::{ErrorKind, EvalResult},
-    opcode::{JumpOffset, OpCode},
+    opcode::{JumpOffset, OpCode, StackIdx},
     value::{Closure, Lambda, NixAttrs, NixList, Value},
 };
 
@@ -318,13 +318,13 @@ impl VM {
                     }
                 }
 
-                OpCode::OpGetLocal(local_idx) => {
+                OpCode::OpGetLocal(StackIdx(local_idx)) => {
                     let idx = self.frame().stack_offset + local_idx;
                     let value = self.stack[idx].clone();
                     self.push(value)
                 }
 
-                OpCode::OpPushWith(idx) => self.with_stack.push(idx),
+                OpCode::OpPushWith(StackIdx(idx)) => self.with_stack.push(idx),
                 OpCode::OpPopWith => {
                     self.with_stack.pop();
                 }