Skip to main content

luaur_compiler/methods/
compiler_push_local.rs

1use crate::records::compile_error::CompileError;
2use crate::records::compiler::Compiler;
3use luaur_ast::records::ast_local::AstLocal;
4use luaur_common::macros::luau_assert::LUAU_ASSERT;
5
6const K_DEFAULT_ALLOC_PC: u32 = !0u32;
7const K_MAX_LOCAL_COUNT: usize = 200;
8
9impl Compiler {
10    pub fn push_local(&mut self, local: *mut AstLocal, reg: u8, allocpc: u32) {
11        if self.local_stack.len() >= K_MAX_LOCAL_COUNT {
12            let local_ref = unsafe { &*local };
13            let name = unsafe { core::ffi::CStr::from_ptr(local_ref.name.value) }.to_string_lossy();
14            CompileError::raise(
15                &local_ref.location,
16                format_args!(
17                    "Out of local registers when trying to allocate {}: exceeded limit {}",
18                    name, K_MAX_LOCAL_COUNT
19                ),
20            );
21        }
22
23        self.local_stack.push(local);
24
25        let debugpc = unsafe { (*self.bytecode).get_debug_pc() };
26        let l = self.locals.get_or_insert(local);
27        LUAU_ASSERT!(!l.allocated);
28
29        l.reg = reg;
30        l.allocated = true;
31        l.debugpc = debugpc;
32        l.allocpc = if allocpc == K_DEFAULT_ALLOC_PC {
33            l.debugpc
34        } else {
35            allocpc
36        };
37    }
38}