@@ -22,7 +22,7 @@ use libc::{malloc, printf};
2222use crate :: codegen:: Codegen ;
2323use crate :: lexer:: Lexer ;
2424use crate :: parser:: Parser ;
25- use crate :: runtime:: thread:: { wpp_mutex_lock, wpp_mutex_new, wpp_mutex_unlock, wpp_thread_join, wpp_thread_join_all, wpp_thread_poll, wpp_thread_spawn_gc} ;
25+ use crate :: runtime:: thread:: { wpp_mutex_lock, wpp_mutex_new, wpp_mutex_unlock, wpp_thread_join, wpp_thread_join_all, wpp_thread_poll, wpp_thread_spawn_gc, wpp_thread_state_get , wpp_thread_state_new , wpp_thread_state_set } ;
2626use runtime:: * ;
2727
2828/// Link to static runtime (optional)
@@ -47,34 +47,112 @@ fn declare_runtime_externals<'ctx>(context: &'ctx Context, module: &Module<'ctx>
4747 let i64_type = context. i64_type ( ) ;
4848 let i8_ptr = context. i8_type ( ) . ptr_type ( inkwell:: AddressSpace :: from ( 0 ) ) ;
4949
50- // === Declare all known externals ===
50+ // === Declare all runtime externals ===
5151 let externals = [
52+ // --- Print subsystem ---
5253 ( "wpp_print_value_basic" , void_type. fn_type ( & [ i8_ptr. into ( ) , i32_type. into ( ) ] , false ) ) ,
5354 ( "wpp_print_array" , void_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
5455 ( "wpp_print_object" , void_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
56+
57+ // --- String subsystem ---
58+ ( "wpp_str_concat" , i8_ptr. fn_type ( & [ i8_ptr. into ( ) , i8_ptr. into ( ) ] , false ) ) ,
59+
60+ // --- HTTP subsystem ---
61+ ( "wpp_http_get" , i32_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
62+ ( "wpp_http_post" , i32_type. fn_type ( & [ i8_ptr. into ( ) , i8_ptr. into ( ) ] , false ) ) ,
63+ ( "wpp_http_put" , i32_type. fn_type ( & [ i8_ptr. into ( ) , i8_ptr. into ( ) ] , false ) ) ,
64+ ( "wpp_http_patch" , i32_type. fn_type ( & [ i8_ptr. into ( ) , i8_ptr. into ( ) ] , false ) ) ,
65+ ( "wpp_http_delete" , i32_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
66+ ( "wpp_http_status" , i32_type. fn_type ( & [ i32_type. into ( ) ] , false ) ) ,
67+ ( "wpp_http_body" , i8_ptr. fn_type ( & [ i32_type. into ( ) ] , false ) ) ,
68+ ( "wpp_http_headers" , i8_ptr. fn_type ( & [ i32_type. into ( ) ] , false ) ) ,
69+ ( "wpp_http_free_all" , void_type. fn_type ( & [ ] , false ) ) ,
70+ ( "wpp_register_endpoint" , void_type. fn_type ( & [ i8_ptr. into ( ) , i8_ptr. into ( ) ] , false ) ) ,
71+ ( "wpp_start_server" , void_type. fn_type ( & [ i32_type. into ( ) ] , false ) ) ,
72+
73+ // --- Threading subsystem ---
5574 ( "wpp_thread_spawn_gc" , i8_ptr. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
5675 ( "wpp_thread_join" , void_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
5776 ( "wpp_thread_poll" , i32_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
58- ( "wpp_thread_join_all" , void_type. fn_type ( & [ ] , false ) ) , // 🧩 critical
77+ ( "wpp_thread_state_new" , i8_ptr. fn_type ( & [ i32_type. into ( ) ] , false ) ) ,
78+ ( "wpp_thread_state_get" , i8_ptr. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
79+ ( "wpp_thread_state_set" , void_type. fn_type ( & [ i8_ptr. into ( ) , i32_type. into ( ) ] , false ) ) ,
80+ ( "wpp_thread_join_all" , void_type. fn_type ( & [ ] , false ) ) ,
81+
82+ // --- Mutex subsystem ---
5983 ( "wpp_mutex_new" , i8_ptr. fn_type ( & [ i32_type. into ( ) ] , false ) ) ,
6084 ( "wpp_mutex_lock" , void_type. fn_type ( & [ i8_ptr. into ( ) , i32_type. into ( ) ] , false ) ) ,
6185 ( "wpp_mutex_unlock" , void_type. fn_type ( & [ i8_ptr. into ( ) ] , false ) ) ,
86+
87+ // --- Runtime ---
88+ ( "wpp_runtime_wait" , void_type. fn_type ( & [ ] , false ) ) ,
89+
90+ // --- libc ---
6291 ( "printf" , i32_type. fn_type ( & [ i8_ptr. into ( ) ] , true ) ) ,
6392 ( "malloc" , i8_ptr. fn_type ( & [ i64_type. into ( ) ] , false ) ) ,
6493 ] ;
6594
6695 for ( name, ty) in externals {
6796 if module. get_function ( name) . is_none ( ) {
6897 module. add_function ( name, ty, None ) ;
69- println ! ( "🔧 Declared external runtime function: {name}" ) ;
7098 }
7199 }
72100}
101+ fn register_all_runtime_symbols ( ) {
102+ unsafe {
103+ // --- Print subsystem ---
104+ add_symbol ( "wpp_print_value_basic" , wpp_print_value_basic as usize ) ;
105+ add_symbol ( "wpp_print_array" , wpp_print_array as usize ) ;
106+ add_symbol ( "wpp_print_object" , wpp_print_object as usize ) ;
107+
108+ // --- String subsystem ---
109+ add_symbol ( "wpp_str_concat" , wpp_str_concat as usize ) ;
110+
111+ // --- HTTP subsystem ---
112+ add_symbol ( "wpp_http_get" , wpp_http_get as usize ) ;
113+ add_symbol ( "wpp_http_post" , wpp_http_post as usize ) ;
114+ add_symbol ( "wpp_http_put" , wpp_http_put as usize ) ;
115+ add_symbol ( "wpp_http_patch" , wpp_http_patch as usize ) ;
116+ add_symbol ( "wpp_http_delete" , wpp_http_delete as usize ) ;
117+ add_symbol ( "wpp_http_status" , wpp_http_status as usize ) ;
118+ add_symbol ( "wpp_http_body" , wpp_http_body as usize ) ;
119+ add_symbol ( "wpp_http_headers" , wpp_http_headers as usize ) ;
120+ add_symbol ( "wpp_http_free_all" , wpp_http_free_all as usize ) ;
121+ add_symbol ( "wpp_register_endpoint" , wpp_register_endpoint as usize ) ;
122+ add_symbol ( "wpp_start_server" , wpp_start_server as usize ) ;
123+
124+ // --- Threading subsystem ---
125+ add_symbol ( "wpp_thread_spawn_gc" , wpp_thread_spawn_gc as usize ) ;
126+ add_symbol ( "wpp_thread_join" , wpp_thread_join as usize ) ;
127+ add_symbol ( "wpp_thread_poll" , wpp_thread_poll as usize ) ;
128+ add_symbol ( "wpp_thread_state_new" , wpp_thread_state_new as usize ) ;
129+ add_symbol ( "wpp_thread_state_get" , wpp_thread_state_get as usize ) ;
130+ add_symbol ( "wpp_thread_state_set" , wpp_thread_state_set as usize ) ;
131+ add_symbol ( "wpp_thread_join_all" , wpp_thread_join_all as usize ) ;
132+
133+ // --- Mutex subsystem ---
134+ add_symbol ( "wpp_mutex_new" , wpp_mutex_new as usize ) ;
135+ add_symbol ( "wpp_mutex_lock" , wpp_mutex_lock as usize ) ;
136+ add_symbol ( "wpp_mutex_unlock" , wpp_mutex_unlock as usize ) ;
137+
138+ // --- Runtime ---
139+ add_symbol ( "wpp_runtime_wait" , wpp_runtime_wait as usize ) ;
140+
141+ // --- libc ---
142+ unsafe extern "C" {
143+ fn printf ( fmt : * const std:: os:: raw:: c_char , ...) -> i32 ;
144+ fn malloc ( size : usize ) -> * mut std:: ffi:: c_void ;
145+ }
146+ add_symbol ( "printf" , printf as usize ) ;
147+ add_symbol ( "malloc" , malloc as usize ) ;
148+ }
149+ }
73150
74151fn register_runtime_symbols < ' ctx > ( engine : & ExecutionEngine < ' ctx > , module : & Module < ' ctx > ) {
75152 let map_fn = |name : & str , ptr : usize | {
76153 if let Some ( func) = module. get_function ( name) {
77154 engine. add_global_mapping ( & func, ptr) ;
155+ #[ cfg( debug_assertions) ]
78156 println ! ( "🔗 Bound {name}" ) ;
79157 } else {
80158 eprintln ! ( "⚠️ Function {name} not found in module" ) ;
@@ -163,6 +241,7 @@ pub fn run_file(codegen: &mut Codegen, optimize: bool) -> Result<(), String> {
163241 // 🧹 Remove duplicate globals
164242 for global_name in [ "_wpp_exc_flag" , "_wpp_exc_i32" , "_wpp_exc_str" ] {
165243 if let Some ( global) = main_mod. get_global ( global_name) {
244+ #[ cfg( debug_assertions) ]
166245 println ! (
167246 "🔧 [wms] Neutralized duplicate global '{}' in reloaded main module" ,
168247 global_name
@@ -201,6 +280,7 @@ pub fn run_file(codegen: &mut Codegen, optimize: bool) -> Result<(), String> {
201280 }
202281
203282 if let Some ( ref ir_text) = dep. llvm_ir {
283+ #[ cfg( debug_assertions) ]
204284 println ! ( "🔗 [link] Importing compiled submodule: {}" , name) ;
205285 let mem_buf = MemoryBuffer :: create_from_memory_range_copy ( ir_text. as_bytes ( ) , name) ;
206286
@@ -223,27 +303,27 @@ pub fn run_file(codegen: &mut Codegen, optimize: bool) -> Result<(), String> {
223303 }
224304 }
225305 }
306+ #[ cfg( debug_assertions) ]
226307 println ! ( "🪶 [debug1] Finished merging and linking modules — about to declare externals" ) ;
227308 declare_runtime_externals ( context, module) ;
309+ #[ cfg( debug_assertions) ]
228310 println ! ( "🪶 [debug2] Declared externals successfully — creating JIT engine next" ) ;
229- unsafe {
230- add_symbol ( "wpp_print_value_basic" , wpp_print_value_basic as usize ) ;
231- add_symbol ( "wpp_thread_join_all" , wpp_thread_join_all as usize ) ;
232- add_symbol ( "printf" , printf as usize ) ;
233- add_symbol ( "malloc" , malloc as usize ) ;
234- }
311+ register_all_runtime_symbols ( ) ;
235312
236313
237314 // === Create JIT engine ===
238315 let engine = module
239316 . create_jit_execution_engine ( OptimizationLevel :: None )
240317 . map_err ( |e| format ! ( "JIT init failed: {e:?}" ) ) ?;
318+ #[ cfg( debug_assertions) ]
241319println ! ( "🪶 [debug3] JIT engine created successfully" ) ;
242320 for func in module. get_functions ( ) {
243321 if let Ok ( name) = func. get_name ( ) . to_str ( ) {
244322 if let Ok ( addr) = engine. get_function_address ( name) {
323+ #[ cfg( debug_assertions) ]
245324 println ! ( "🔎 {name} -> 0x{addr:x}" ) ;
246325 } else {
326+ #[ cfg( debug_assertions) ]
247327 println ! ( "❌ {name} -> <missing>" ) ;
248328 }
249329 }
@@ -255,6 +335,7 @@ println!("🪶 [debug3] JIT engine created successfully");
255335
256336 // === Register runtime bindings ===
257337 register_runtime_symbols ( & engine, module) ;
338+ #[ cfg( debug_assertions) ]
258339 println ! ( "🪶 [debug4] Runtime symbols bound successfully" ) ;
259340
260341 // === Link cross-module imports (from WMS) ===
@@ -264,9 +345,11 @@ println!("🪶 [debug3] JIT engine created successfully");
264345 let wms = wms_arc. lock ( ) . unwrap ( ) ;
265346 let mut resolver = resolver_arc. lock ( ) . unwrap ( ) ;
266347 resolver. link_imports_runtime ( & engine, module, & wms) ;
348+ #[ cfg( debug_assertions) ]
267349 println ! ( "🪶 [debug5] Finished linking runtime imports across modules" ) ;
268350
269351 }
352+ #[ cfg( debug_assertions) ]
270353 println ! ( "🪶 [debug6] Searching for entrypoint (bootstrap_main / main / main_async)" ) ;
271354 // === Find and run entrypoint ===
272355 let entry_name = if module. get_function ( "bootstrap_main" ) . is_some ( ) {
@@ -279,10 +362,13 @@ println!("🪶 [debug3] JIT engine created successfully");
279362 eprintln ! ( "❌ No valid entrypoint found (expected main, main_async, or bootstrap_main)" ) ;
280363 return Err ( "❌ No entrypoint function found in final linked module" . into ( ) ) ;
281364 } ;
365+ #[ cfg( debug_assertions) ]
282366 println ! ( "🪶 [debug7] Entrypoint resolved to {entry_name}" ) ;
367+ #[ cfg( debug_assertions) ]
283368 println ! ( "🔍 [jit] Listing all LLVM functions before execution:" ) ;
284369 for func in module. get_functions ( ) {
285370 if let Ok ( name) = func. get_name ( ) . to_str ( ) {
371+ #[ cfg( debug_assertions) ]
286372 println ! ( " - {}" , name) ;
287373 }
288374 }
@@ -297,9 +383,13 @@ println!("🪶 [debug3] JIT engine created successfully");
297383 // === Debug entrypoint signature ===
298384 println ! ( "🚀 Launching entrypoint: {entry_name}" ) ;
299385 if let Some ( func) = module. get_function ( entry_name) {
386+ #[ cfg( debug_assertions) ]
300387 println ! ( "🔍 LLVM signature for entrypoint:" ) ;
388+ #[ cfg( debug_assertions) ]
301389 println ! ( "{}" , func. print_to_string( ) . to_string( ) ) ;
390+ #[ cfg( debug_assertions) ]
302391 println ! ( "=== FULL IR DUMP ===" ) ;
392+ #[ cfg( debug_assertions) ]
303393println ! ( "{}" , module. print_to_string( ) . to_string( ) ) ;
304394 }
305395
@@ -310,6 +400,7 @@ println!("{}", module.print_to_string().to_string());
310400 Err ( _) => println ! ( "❌ Missing runtime binding for {name}" ) ,
311401 }
312402 }
403+ #[ cfg( debug_assertions) ]
313404 println ! ( "🧠 [jit] About to execute entrypoint `{entry_name}`" ) ;
314405 // === Run the entrypoint ===
315406
@@ -322,6 +413,7 @@ println!("{}", module.print_to_string().to_string());
322413 println ! ( "✅ [jit] Returned cleanly from `{entry_name}` with result = {result}" ) ;
323414 println ! ( "🏁 Finished running {entry_name}, result = {result}" ) ;
324415 }
416+ #[ cfg( debug_assertions) ]
325417 println ! ( "🪶 [debug10] run_file() completed successfully without panic" ) ;
326418 Ok ( ( ) )
327419}
0 commit comments