Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion compiler/llvm_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ techscript_common = { path = "../common" }
techscript_ir = { path = "../ir" }
techscript_ast = { path = "../ast" }
techscript_syntax = { path = "../syntax" }
llvm-sys = { version = "221", optional = true }
llvm-sys = { version = "180", optional = true }
serde = { workspace = true }
anyhow = { workspace = true }
thiserror = { workspace = true }
Expand Down
22 changes: 21 additions & 1 deletion compiler/llvm_backend/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ use llvm_sys::core::*;
use llvm_sys::prelude::*;
use std::collections::HashMap;
use std::ffi::CString;
use techscript_ir::{BlockId, ValueId};
use techscript_ir::{BlockId, GlobalId, LocalId, ValueId};

pub struct CodegenContext {
pub context: LLVMContextRef,
pub module: LLVMModuleRef,
pub builder: LLVMBuilderRef,
pub values: HashMap<ValueId, LLVMValueRef>,
pub globals: HashMap<GlobalId, LLVMValueRef>,
pub locals: HashMap<LocalId, LLVMValueRef>,
pub blocks: HashMap<BlockId, LLVMBasicBlockRef>,
}

Expand All @@ -30,11 +32,29 @@ impl CodegenContext {
module,
builder,
values: HashMap::new(),
globals: HashMap::new(),
locals: HashMap::new(),
blocks: HashMap::new(),
}
}

/// Looks up an LLVM value for the given ValueId.
pub fn get_global(&self, id: GlobalId) -> Option<LLVMValueRef> {
self.globals.get(&id).copied()
}

pub fn register_global(&mut self, id: GlobalId, val: LLVMValueRef) {
self.globals.insert(id, val);
}

pub fn get_local(&self, id: LocalId) -> Option<LLVMValueRef> {
self.locals.get(&id).copied()
}

pub fn register_local(&mut self, id: LocalId, val: LLVMValueRef) {
self.locals.insert(id, val);
}

pub fn get_value(&self, id: ValueId) -> Option<LLVMValueRef> {
self.values.get(&id).copied()
}
Expand Down
8 changes: 7 additions & 1 deletion runtime/interpreter/src/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,13 @@ impl AstVisitor for Interpreter {
}
Expression::Ask(ask) => {
let prompt_val = self.visit_expression(&ask.prompt)?;
let ask_fn = self.ctx.registry.lookup("ask").ok_or_else(|| crate::RuntimeError::new(crate::RuntimeErrorKind::UndefinedVariable("ask".to_string()), None, None))?;
let ask_fn = self.ctx.registry.lookup("ask").ok_or_else(|| {
crate::RuntimeError::new(
crate::RuntimeErrorKind::UndefinedVariable("ask".to_string()),
None,
None,
)
})?;
ask_fn.call(&mut self.ctx, vec![prompt_val])
}
Expression::Lambda(lambda) => {
Expand Down
8 changes: 7 additions & 1 deletion runtime/interpreter/src/statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ impl Interpreter {
}
Statement::Say(say_stmt) => {
let val = self.visit_expression(&say_stmt.value)?;
let say_func = self.ctx.registry.lookup("say").ok_or_else(|| RuntimeError::new(RuntimeErrorKind::UndefinedVariable("say".to_string()), None, None))?;
let say_func = self.ctx.registry.lookup("say").ok_or_else(|| {
RuntimeError::new(
RuntimeErrorKind::UndefinedVariable("say".to_string()),
None,
None,
)
})?;
say_func.call(&mut self.ctx, vec![val])?;
Ok(FlowSignal::Normal)
}
Expand Down
44 changes: 34 additions & 10 deletions runtime/vm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ impl VM {
self.profiler.record_stack_height(self.stack.len());

if self.debugger.is_enabled() {
let current_func =
&self.module.functions[self.frames.last().ok_or(crate::error::VMError::StackUnderflow)?.function_idx as usize];
let current_func = &self.module.functions[self
.frames
.last()
.ok_or(crate::error::VMError::StackUnderflow)?
.function_idx
as usize];
self.debugger.trace_instruction(
current_func,
ip,
Expand All @@ -51,8 +55,12 @@ impl VM {

Opcode::LoadConst => {
if let Some(Operand::ConstantIndex(c_idx)) = inst_operands.first() {
let current_func = &self.module.functions
[self.frames.last().ok_or(crate::error::VMError::StackUnderflow)?.function_idx as usize];
let current_func = &self.module.functions[self
.frames
.last()
.ok_or(crate::error::VMError::StackUnderflow)?
.function_idx
as usize];
let lit = current_func
.chunk
.constants
Expand All @@ -73,7 +81,11 @@ impl VM {

Opcode::LoadLocal => {
if let Some(Operand::LocalIndex(l_idx)) = inst_operands.first() {
let bp = self.frames.last().ok_or(crate::error::VMError::StackUnderflow)?.base_pointer;
let bp = self
.frames
.last()
.ok_or(crate::error::VMError::StackUnderflow)?
.base_pointer;
let val = self.stack.get(bp + *l_idx as usize)?;
self.stack.push(val.clone())?;
} else {
Expand All @@ -83,7 +95,11 @@ impl VM {

Opcode::StoreLocal => {
if let Some(Operand::LocalIndex(l_idx)) = inst_operands.first() {
let bp = self.frames.last().ok_or(crate::error::VMError::StackUnderflow)?.base_pointer;
let bp = self
.frames
.last()
.ok_or(crate::error::VMError::StackUnderflow)?
.base_pointer;
let val = self.stack.pop()?;
self.stack.set(bp + *l_idx as usize, val)?;
} else {
Expand Down Expand Up @@ -768,8 +784,12 @@ impl VM {

Opcode::FieldLoad => {
if let Some(Operand::ConstantIndex(c_idx)) = inst_operands.first() {
let current_func = &self.module.functions
[self.frames.last().ok_or(crate::error::VMError::StackUnderflow)?.function_idx as usize];
let current_func = &self.module.functions[self
.frames
.last()
.ok_or(crate::error::VMError::StackUnderflow)?
.function_idx
as usize];
let lit = current_func
.chunk
.constants
Expand Down Expand Up @@ -826,8 +846,12 @@ impl VM {

Opcode::FieldStore => {
if let Some(Operand::ConstantIndex(c_idx)) = inst_operands.first() {
let current_func = &self.module.functions
[self.frames.last().ok_or(crate::error::VMError::StackUnderflow)?.function_idx as usize];
let current_func = &self.module.functions[self
.frames
.last()
.ok_or(crate::error::VMError::StackUnderflow)?
.function_idx
as usize];
let lit = current_func
.chunk
.constants
Expand Down
Loading