Skip to content

Commit ac766fa

Browse files
authored
Merge pull request #55 from Tcode-Motion/fix-compilation-and-clippy-4585464244175969887
🧹 [Code Health] Fix workspace compilation errors and clippy warnings
2 parents 909fd26 + d1716c4 commit ac766fa

20 files changed

Lines changed: 350 additions & 173 deletions

File tree

‎Cargo.lock‎

Lines changed: 243 additions & 133 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎cli/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ anyhow = { workspace = true }
3737
rustyline = { workspace = true }
3838
toml = { workspace = true }
3939
indexmap = { workspace = true }
40-
bincode = "3.0"
40+
bincode = "1.3.3"
4141
dirs = "6.0"
4242
num_cpus = "1.15"
43-
ureq = { version = "3.4.0" }
43+
ureq = { version = "2.9" }
4444

4545
[build-dependencies]
4646
winres = "0.1.12"

‎compiler/bytecode/Cargo.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ techscript_syntax = { path = "../syntax" }
1010
techscript_ast = { path = "../ast" }
1111
techscript_ir = { path = "../ir" }
1212
serde = { workspace = true }
13-
bincode = "3.0"
13+
bincode = "1.3.3"
1414

1515
[dev-dependencies]
1616
techscript_errors = { path = "../errors" }
@@ -19,4 +19,4 @@ techscript_parser = { path = "../parser" }
1919
techscript_semantic = { path = "../semantic" }
2020
techscript_optimizer = { path = "../optimizer" }
2121
serde = { workspace = true }
22-
bincode = "3.0"
22+
bincode = "1.3.3"

‎compiler/llvm_backend/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ techscript_common = { path = "../common" }
99
techscript_ir = { path = "../ir" }
1010
techscript_ast = { path = "../ast" }
1111
techscript_syntax = { path = "../syntax" }
12-
llvm-sys = { version = "221", optional = true }
12+
llvm-sys = { version = "180", optional = true }
1313
serde = { workspace = true }
1414
anyhow = { workspace = true }
1515
thiserror = { workspace = true }

‎compiler/llvm_backend/src/codegen.rs‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ impl<'a> CodegenEngine<'a> {
3333
}
3434
}
3535

36+
/// # Safety
37+
///
38+
/// Caller must ensure LLVM context is valid.
3639
pub unsafe fn compile_module(&mut self, ir_module: &Module) -> Result<(), String> {
3740
self.global_names.clear();
3841

3942
// 1. Declare globals
40-
for &(ref global_id, ref name, ref ty) in &ir_module.globals {
43+
for (global_id, name, ty) in &ir_module.globals {
4144
let llvm_ty = to_llvm_type(self.ctx.context, ty);
4245
let global_var = LLVMAddGlobal(
4346
self.ctx.module,
@@ -124,8 +127,6 @@ impl<'a> CodegenEngine<'a> {
124127
let dest_block = self.ctx.get_block(*dest).unwrap();
125128
LLVMBuildBr(self.ctx.builder, dest_block);
126129
}
127-
<
128-
}
129130
TerminatorKind::ConditionalJump {
130131
cond,
131132
then_block,
@@ -199,6 +200,25 @@ impl<'a> CodegenEngine<'a> {
199200
TerminatorKind::Unreachable => {
200201
LLVMBuildUnreachable(self.ctx.builder);
201202
}
203+
TerminatorKind::Throw(val) => {
204+
let thrown_val = self.codegen_val(val)?;
205+
let boxed_val = self.box_val(thrown_val)?;
206+
let i8_ptr_ty = LLVMPointerType(LLVMInt8TypeInContext(self.ctx.context), 0);
207+
let fn_throw = self.get_or_declare_runtime_fn(
208+
"ts_throw",
209+
LLVMVoidTypeInContext(self.ctx.context),
210+
&[i8_ptr_ty],
211+
);
212+
LLVMBuildCall2(
213+
self.ctx.builder,
214+
LLVMTypeOf(fn_throw),
215+
fn_throw,
216+
[boxed_val].as_mut_ptr(),
217+
1,
218+
c"".as_ptr(),
219+
);
220+
LLVMBuildUnreachable(self.ctx.builder);
221+
}
202222
}
203223
}
204224
}
@@ -1056,7 +1076,10 @@ impl<'a> CodegenEngine<'a> {
10561076
CString::new("cast").unwrap().as_ptr(),
10571077
)
10581078
}
1059-
Op::Try { catch_block, catch_var } => {
1079+
Op::Try {
1080+
catch_block,
1081+
catch_var,
1082+
} => {
10601083
let i8_ptr_ty = LLVMPointerType(LLVMInt8TypeInContext(context), 0);
10611084
let fn_push = self.get_or_declare_runtime_fn("ts_try_push", i8_ptr_ty, &[]);
10621085
let buf_ptr = LLVMBuildCall2(
@@ -1120,7 +1143,11 @@ impl<'a> CodegenEngine<'a> {
11201143
LLVMPositionBuilderAtEnd(self.ctx.builder, dispatch_block);
11211144

11221145
// Clean up the jmp_buf since we arrived here via longjmp and ts_try_pop wasn't called
1123-
let fn_free_buf = self.get_or_declare_runtime_fn("ts_try_free", LLVMVoidTypeInContext(context), &[i8_ptr_ty]);
1146+
let fn_free_buf = self.get_or_declare_runtime_fn(
1147+
"ts_try_free",
1148+
LLVMVoidTypeInContext(context),
1149+
&[i8_ptr_ty],
1150+
);
11241151
LLVMBuildCall2(
11251152
self.ctx.builder,
11261153
LLVMTypeOf(fn_free_buf),
@@ -1151,7 +1178,11 @@ impl<'a> CodegenEngine<'a> {
11511178
return Ok(());
11521179
}
11531180
Op::EndTry => {
1154-
let fn_pop = self.get_or_declare_runtime_fn("ts_try_pop", LLVMVoidTypeInContext(context), &[]);
1181+
let fn_pop = self.get_or_declare_runtime_fn(
1182+
"ts_try_pop",
1183+
LLVMVoidTypeInContext(context),
1184+
&[],
1185+
);
11551186
LLVMBuildCall2(
11561187
self.ctx.builder,
11571188
LLVMTypeOf(fn_pop),

‎compiler/llvm_backend/src/context.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ pub struct CodegenContext {
2121
}
2222

2323
impl CodegenContext {
24+
/// # Safety
25+
///
26+
/// Caller must ensure LLVM context is valid.
2427
pub unsafe fn new(name: &str) -> Self {
2528
let context = LLVMContextCreate();
2629
let module =

‎compiler/llvm_backend/src/jit.rs‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
55
#![cfg(feature = "llvm")]
66

7-
use llvm_sys::core::*;
87
use llvm_sys::orc2::lljit::*;
98
use llvm_sys::orc2::*;
109
use std::collections::HashMap;
@@ -23,6 +22,9 @@ pub struct LLVMJitEngine {
2322

2423
impl LLVMJitEngine {
2524
/// Creates a new LLVMJitEngine instance.
25+
/// # Safety
26+
///
27+
/// Caller must ensure LLVM context is valid.
2628
pub unsafe fn new() -> Result<Self, String> {
2729
let mut jit = ptr::null_mut();
2830
let builder = LLVMOrcCreateLLJITBuilder();
@@ -41,10 +43,13 @@ impl LLVMJitEngine {
4143
}
4244

4345
/// Compiles a TechScript IR Module to JIT memory.
46+
/// # Safety
47+
///
48+
/// Caller must ensure LLVM context is valid.
4449
pub unsafe fn compile(
4550
&mut self,
4651
ir_module: &techscript_ir::Module,
47-
options: &LLVMBackendOptions,
52+
_options: &LLVMBackendOptions,
4853
) -> Result<(), String> {
4954
// 1. Build LLVM IR Module
5055
let mut ctx = CodegenContext::new(&ir_module.name);
@@ -79,6 +84,9 @@ impl LLVMJitEngine {
7984
}
8085

8186
/// Looks up a function symbol by name.
87+
/// # Safety
88+
///
89+
/// Caller must ensure LLVM context is valid.
8290
pub unsafe fn lookup(&mut self, name: &str) -> Result<u64, String> {
8391
if let Some(&addr) = self.cache.get(name) {
8492
return Ok(addr);
@@ -96,13 +104,19 @@ impl LLVMJitEngine {
96104
}
97105

98106
/// Executes the JIT-compiled main function and returns its result (if integer).
107+
/// # Safety
108+
///
109+
/// Caller must ensure LLVM context is valid.
99110
pub unsafe fn execute(&mut self, func_name: &str) -> Result<i64, String> {
100111
let addr = self.lookup(func_name)?;
101112
let func: extern "C" fn() -> i64 = std::mem::transmute(addr);
102113
Ok(func())
103114
}
104115

105116
/// Clears the function cache and reloads the engine (for hot reload support).
117+
/// # Safety
118+
///
119+
/// Caller must ensure LLVM context is valid.
106120
pub unsafe fn hot_reload(&mut self) {
107121
self.cache.clear();
108122
}

‎compiler/llvm_backend/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl LLVMBackend {
104104
let mut engine = CodegenEngine::new(&mut ctx);
105105
engine
106106
.compile_module(ir_module)
107-
.map_err(|e| LLVMCodegenError::CompilationError(e))?;
107+
.map_err(LLVMCodegenError::CompilationError)?;
108108

109109
let out_str = CString::new(out_path.to_string_lossy().to_string()).unwrap();
110110
let mut err_msg = std::ptr::null_mut();
@@ -147,7 +147,7 @@ impl LLVMBackend {
147147

148148
engine
149149
.compile_module(ir_module)
150-
.map_err(|e| LLVMCodegenError::CompilationError(e))?;
150+
.map_err(LLVMCodegenError::CompilationError)?;
151151

152152
// 3. Resolve Target Triple & Host CPU Features
153153
let triple_cstr = CString::new(options.target_triple.as_str()).unwrap();

‎compiler/llvm_backend/src/type_map.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use llvm_sys::prelude::*;
99
use techscript_ir::types::IRType;
1010

1111
/// Maps a TechScript IRType to its corresponding LLVMTypeRef.
12+
/// # Safety
13+
///
14+
/// Caller must ensure LLVM context is valid.
1215
pub unsafe fn to_llvm_type(context: LLVMContextRef, ty: &IRType) -> LLVMTypeRef {
1316
match ty {
1417
IRType::Void => LLVMVoidTypeInContext(context),

‎runtime/native_runtime/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,8 @@ extern "C" {
10371037
}
10381038

10391039
thread_local! {
1040-
static TRY_STACK: RefCell<Vec<*mut JmpBuf>> = RefCell::new(Vec::new());
1041-
static PENDING_EXCEPTION: RefCell<*mut TsValue> = RefCell::new(std::ptr::null_mut());
1040+
static TRY_STACK: RefCell<Vec<*mut JmpBuf>> = const { RefCell::new(Vec::new()) };
1041+
static PENDING_EXCEPTION: RefCell<*mut TsValue> = const { RefCell::new(std::ptr::null_mut()) };
10421042
}
10431043

10441044
#[no_mangle]

0 commit comments

Comments
 (0)