1#[cfg(feature = "jit")]
12pub mod jit {
13 use cranelift_codegen::ir::condcodes::IntCC;
14 use cranelift_codegen::ir::{types, AbiParam, InstBuilder, UserFuncName};
15 use cranelift_codegen::settings::{self, Configurable};
16 use cranelift_codegen::Context;
17 use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
18 use cranelift_jit::{JITBuilder, JITModule};
19 use cranelift_module::{FuncId, Linkage, Module};
20
21 use std::collections::HashMap;
22 use std::mem;
23
24 use crate::ast::{
25 self, BinOp, Expr, ExternBlock, ExternFunction, ExternItem, Item, Literal, PipeOp,
26 TypeExpr, UnaryOp,
27 };
28 use crate::ffi::ctypes::CType;
29 use crate::optimize::{OptLevel, Optimizer};
30 use crate::parser::Parser;
31
32 #[repr(C)]
44 #[derive(Clone, Copy, Debug)]
45 pub struct SigilValue(pub u64);
46
47 impl SigilValue {
48 pub const TAG_INT: u64 = 0;
50 pub const TAG_FLOAT: u64 = 1;
51 pub const TAG_BOOL: u64 = 2;
52 pub const TAG_NULL: u64 = 3;
53 pub const TAG_PTR: u64 = 4; #[inline]
56 pub fn from_int(v: i64) -> Self {
57 SigilValue(v as u64)
58 }
59
60 #[inline]
61 pub fn from_float(v: f64) -> Self {
62 SigilValue(v.to_bits())
63 }
64
65 #[inline]
66 pub fn from_bool(v: bool) -> Self {
67 SigilValue(if v { 1 } else { 0 })
68 }
69
70 #[inline]
71 pub fn as_int(self) -> i64 {
72 self.0 as i64
73 }
74
75 #[inline]
76 pub fn as_float(self) -> f64 {
77 f64::from_bits(self.0)
78 }
79
80 #[inline]
81 pub fn as_bool(self) -> bool {
82 self.0 != 0
83 }
84 }
85
86 type CompiledFn = unsafe extern "C" fn() -> i64;
88 #[allow(dead_code)]
89 type CompiledFnWithArgs = unsafe extern "C" fn(i64) -> i64;
90
91 #[derive(Clone, Debug)]
93 pub struct ExternFnSig {
94 pub name: String,
95 pub params: Vec<types::Type>,
96 pub returns: Option<types::Type>,
97 pub variadic: bool,
98 pub func_id: FuncId,
99 }
100
101 pub struct JitCompiler {
103 module: JITModule,
105 builder_ctx: FunctionBuilderContext,
107 ctx: Context,
109 functions: HashMap<String, FuncId>,
111 extern_functions: HashMap<String, ExternFnSig>,
113 #[allow(dead_code)]
115 var_counter: usize,
116 #[allow(dead_code)]
118 builtins: HashMap<String, *const u8>,
119 }
120
121 impl JitCompiler {
122 pub fn new() -> Result<Self, String> {
124 let mut flag_builder = settings::builder();
125 flag_builder.set("use_colocated_libcalls", "false").unwrap();
127 flag_builder.set("is_pic", "false").unwrap();
128 flag_builder.set("opt_level", "speed").unwrap();
130 flag_builder.set("enable_verifier", "false").unwrap(); flag_builder.set("enable_alias_analysis", "true").unwrap();
133
134 let isa_builder = cranelift_native::builder().map_err(|e| e.to_string())?;
136 let isa = isa_builder
137 .finish(settings::Flags::new(flag_builder))
138 .map_err(|e| e.to_string())?;
139
140 let mut builder = JITBuilder::with_isa(isa, cranelift_module::default_libcall_names());
141
142 let builtins = Self::register_builtins(&mut builder);
144
145 let module = JITModule::new(builder);
146
147 Ok(Self {
148 module,
149 builder_ctx: FunctionBuilderContext::new(),
150 ctx: Context::new(),
151 functions: HashMap::new(),
152 extern_functions: HashMap::new(),
153 var_counter: 0,
154 builtins,
155 })
156 }
157
158 fn register_builtins(builder: &mut JITBuilder) -> HashMap<String, *const u8> {
160 let mut builtins = HashMap::new();
161
162 builder.symbol("sigil_sqrt", sigil_sqrt as *const u8);
164 builder.symbol("sigil_sin", sigil_sin as *const u8);
165 builder.symbol("sigil_cos", sigil_cos as *const u8);
166 builder.symbol("sigil_pow", sigil_pow as *const u8);
167 builder.symbol("sigil_exp", sigil_exp as *const u8);
168 builder.symbol("sigil_ln", sigil_ln as *const u8);
169 builder.symbol("sigil_floor", sigil_floor as *const u8);
170 builder.symbol("sigil_ceil", sigil_ceil as *const u8);
171 builder.symbol("sigil_abs", sigil_abs as *const u8);
172
173 builder.symbol("sigil_print", sigil_print as *const u8);
175 builder.symbol("sigil_print_int", sigil_print_int as *const u8);
176 builder.symbol("sigil_print_float", sigil_print_float as *const u8);
177 builder.symbol("sigil_print_str", sigil_print_str as *const u8);
178
179 builder.symbol("sigil_now", sigil_now as *const u8);
181
182 builder.symbol("sigil_add", sigil_add as *const u8);
184 builder.symbol("sigil_sub", sigil_sub as *const u8);
185 builder.symbol("sigil_mul", sigil_mul as *const u8);
186 builder.symbol("sigil_div", sigil_div as *const u8);
187 builder.symbol("sigil_lt", sigil_lt as *const u8);
188 builder.symbol("sigil_le", sigil_le as *const u8);
189 builder.symbol("sigil_gt", sigil_gt as *const u8);
190 builder.symbol("sigil_ge", sigil_ge as *const u8);
191
192 builder.symbol("sigil_simd_new", sigil_simd_new as *const u8);
194 builder.symbol("sigil_simd_splat", sigil_simd_splat as *const u8);
195 builder.symbol("sigil_simd_add", sigil_simd_add as *const u8);
196 builder.symbol("sigil_simd_sub", sigil_simd_sub as *const u8);
197 builder.symbol("sigil_simd_mul", sigil_simd_mul as *const u8);
198 builder.symbol("sigil_simd_div", sigil_simd_div as *const u8);
199 builder.symbol("sigil_simd_dot", sigil_simd_dot as *const u8);
200 builder.symbol("sigil_simd_hadd", sigil_simd_hadd as *const u8);
201 builder.symbol("sigil_simd_length_sq", sigil_simd_length_sq as *const u8);
202 builder.symbol("sigil_simd_length", sigil_simd_length as *const u8);
203 builder.symbol("sigil_simd_normalize", sigil_simd_normalize as *const u8);
204 builder.symbol("sigil_simd_cross", sigil_simd_cross as *const u8);
205 builder.symbol("sigil_simd_min", sigil_simd_min as *const u8);
206 builder.symbol("sigil_simd_max", sigil_simd_max as *const u8);
207 builder.symbol("sigil_simd_extract", sigil_simd_extract as *const u8);
208 builder.symbol("sigil_simd_free", sigil_simd_free as *const u8);
209
210 builder.symbol("sigil_array_new", sigil_array_new as *const u8);
212 builder.symbol("sigil_array_push", sigil_array_push as *const u8);
213 builder.symbol("sigil_array_get", sigil_array_get as *const u8);
214 builder.symbol("sigil_array_set", sigil_array_set as *const u8);
215 builder.symbol("sigil_array_len", sigil_array_len as *const u8);
216
217 builder.symbol("sigil_array_sum", sigil_array_sum as *const u8);
219 builder.symbol("sigil_array_scale", sigil_array_scale as *const u8);
220 builder.symbol("sigil_array_offset", sigil_array_offset as *const u8);
221 builder.symbol("sigil_array_dot", sigil_array_dot as *const u8);
222 builder.symbol("sigil_array_add", sigil_array_add as *const u8);
223 builder.symbol("sigil_array_mul", sigil_array_mul as *const u8);
224 builder.symbol("sigil_array_min", sigil_array_min as *const u8);
225 builder.symbol("sigil_array_max", sigil_array_max as *const u8);
226 builder.symbol("sigil_array_fill", sigil_array_fill as *const u8);
227
228 builder.symbol("sigil_array_first", sigil_array_first as *const u8);
230 builder.symbol("sigil_array_last", sigil_array_last as *const u8);
231 builder.symbol("sigil_array_middle", sigil_array_middle as *const u8);
232 builder.symbol("sigil_array_choice", sigil_array_choice as *const u8);
233 builder.symbol("sigil_array_nth", sigil_array_nth as *const u8);
234 builder.symbol("sigil_array_next", sigil_array_next as *const u8);
235 builder.symbol("sigil_array_product", sigil_array_product as *const u8);
236 builder.symbol("sigil_array_sort", sigil_array_sort as *const u8);
237
238 builder.symbol("sigil_parallel_map", sigil_parallel_map as *const u8);
240 builder.symbol("sigil_parallel_filter", sigil_parallel_filter as *const u8);
241 builder.symbol("sigil_parallel_reduce", sigil_parallel_reduce as *const u8);
242
243 builder.symbol("sigil_gpu_map", sigil_gpu_map as *const u8);
245 builder.symbol("sigil_gpu_filter", sigil_gpu_filter as *const u8);
246 builder.symbol("sigil_gpu_reduce", sigil_gpu_reduce as *const u8);
247
248 builder.symbol("sigil_memo_new", sigil_memo_new as *const u8);
250 builder.symbol("sigil_memo_get_1", sigil_memo_get_1 as *const u8);
251 builder.symbol("sigil_memo_set_1", sigil_memo_set_1 as *const u8);
252 builder.symbol("sigil_memo_get_2", sigil_memo_get_2 as *const u8);
253 builder.symbol("sigil_memo_set_2", sigil_memo_set_2 as *const u8);
254 builder.symbol("sigil_memo_free", sigil_memo_free as *const u8);
255
256 builder.symbol("sigil_ackermann", sigil_ackermann as *const u8);
258 builder.symbol("sigil_tak", sigil_tak as *const u8);
259
260 use crate::ffi::helpers::*;
262 builder.symbol(
263 "sigil_string_to_cstring",
264 sigil_string_to_cstring as *const u8,
265 );
266 builder.symbol("sigil_cstring_free", sigil_cstring_free as *const u8);
267 builder.symbol("sigil_cstring_len", sigil_cstring_len as *const u8);
268 builder.symbol("sigil_cstring_copy", sigil_cstring_copy as *const u8);
269 builder.symbol("sigil_ptr_from_int", sigil_ptr_from_int as *const u8);
270 builder.symbol("sigil_ptr_to_int", sigil_ptr_to_int as *const u8);
271 builder.symbol("sigil_ptr_read_u8", sigil_ptr_read_u8 as *const u8);
272 builder.symbol("sigil_ptr_write_u8", sigil_ptr_write_u8 as *const u8);
273 builder.symbol("sigil_ptr_read_i32", sigil_ptr_read_i32 as *const u8);
274 builder.symbol("sigil_ptr_write_i32", sigil_ptr_write_i32 as *const u8);
275 builder.symbol("sigil_ptr_read_i64", sigil_ptr_read_i64 as *const u8);
276 builder.symbol("sigil_ptr_write_i64", sigil_ptr_write_i64 as *const u8);
277 builder.symbol("sigil_ptr_read_f64", sigil_ptr_read_f64 as *const u8);
278 builder.symbol("sigil_ptr_write_f64", sigil_ptr_write_f64 as *const u8);
279 builder.symbol("sigil_ptr_add", sigil_ptr_add as *const u8);
280 builder.symbol("sigil_ptr_is_null", sigil_ptr_is_null as *const u8);
281 builder.symbol("sigil_alloc", sigil_alloc as *const u8);
282 builder.symbol("sigil_free", sigil_free as *const u8);
283 builder.symbol("sigil_realloc", sigil_realloc as *const u8);
284 builder.symbol("sigil_memcpy", sigil_memcpy as *const u8);
285 builder.symbol("sigil_memset", sigil_memset as *const u8);
286
287 builtins.insert("sqrt".into(), sigil_sqrt as *const u8);
288 builtins.insert("sin".into(), sigil_sin as *const u8);
289 builtins.insert("cos".into(), sigil_cos as *const u8);
290 builtins.insert("pow".into(), sigil_pow as *const u8);
291 builtins.insert("exp".into(), sigil_exp as *const u8);
292 builtins.insert("ln".into(), sigil_ln as *const u8);
293 builtins.insert("floor".into(), sigil_floor as *const u8);
294 builtins.insert("ceil".into(), sigil_ceil as *const u8);
295 builtins.insert("abs".into(), sigil_abs as *const u8);
296 builtins.insert("print".into(), sigil_print as *const u8);
297 builtins.insert("now".into(), sigil_now as *const u8);
298
299 builtins
300 }
301
302 pub fn compile(&mut self, source: &str) -> Result<(), String> {
304 self.compile_with_opt(source, OptLevel::Aggressive)
305 }
306
307 pub fn compile_with_opt(
309 &mut self,
310 source: &str,
311 opt_level: OptLevel,
312 ) -> Result<(), String> {
313 let mut parser = Parser::new(source);
314 let source_file = parser.parse_file().map_err(|e| format!("{:?}", e))?;
315
316 let mut optimizer = Optimizer::new(opt_level);
318 let optimized = optimizer.optimize_file(&source_file);
319
320 for spanned_item in &optimized.items {
322 match &spanned_item.node {
323 Item::ExternBlock(extern_block) => {
324 self.declare_extern_block(extern_block)?;
325 }
326 Item::Function(func) => {
327 self.declare_function(func)?;
328 }
329 _ => {}
330 }
331 }
332
333 for spanned_item in &optimized.items {
335 if let Item::Function(func) = &spanned_item.node {
336 self.compile_function(func)?;
337 }
338 }
339
340 self.module
342 .finalize_definitions()
343 .map_err(|e| e.to_string())?;
344
345 Ok(())
346 }
347
348 fn declare_function(&mut self, func: &ast::Function) -> Result<FuncId, String> {
350 let name = &func.name.name;
351
352 let mut sig = self.module.make_signature();
354
355 for _param in &func.params {
357 sig.params.push(AbiParam::new(types::I64));
358 }
359
360 sig.returns.push(AbiParam::new(types::I64));
362
363 let func_id = self
364 .module
365 .declare_function(name, Linkage::Local, &sig)
366 .map_err(|e| e.to_string())?;
367
368 self.functions.insert(name.clone(), func_id);
369 Ok(func_id)
370 }
371
372 fn declare_extern_block(&mut self, extern_block: &ExternBlock) -> Result<(), String> {
374 if extern_block.abi != "C" && extern_block.abi != "c" {
376 return Err(format!(
377 "Unsupported ABI: {}. Only \"C\" is supported.",
378 extern_block.abi
379 ));
380 }
381
382 for item in &extern_block.items {
383 match item {
384 ExternItem::Function(func) => {
385 self.declare_extern_function(func)?;
386 }
387 ExternItem::Static(stat) => {
388 eprintln!(
390 "Warning: extern static '{}' not yet implemented",
391 stat.name.name
392 );
393 }
394 }
395 }
396
397 Ok(())
398 }
399
400 fn declare_extern_function(&mut self, func: &ExternFunction) -> Result<(), String> {
402 let name = &func.name.name;
403
404 let mut sig = self.module.make_signature();
406 let mut param_types = Vec::new();
407
408 for param in &func.params {
410 let ty = self.type_expr_to_cranelift(¶m.ty)?;
411 sig.params.push(AbiParam::new(ty));
412 param_types.push(ty);
413 }
414
415 let return_type = if let Some(ret_ty) = &func.return_type {
417 let ty = self.type_expr_to_cranelift(ret_ty)?;
418 sig.returns.push(AbiParam::new(ty));
419 Some(ty)
420 } else {
421 None
422 };
423
424 let func_id = self
428 .module
429 .declare_function(name, Linkage::Import, &sig)
430 .map_err(|e| e.to_string())?;
431
432 self.extern_functions.insert(
433 name.clone(),
434 ExternFnSig {
435 name: name.clone(),
436 params: param_types,
437 returns: return_type,
438 variadic: func.variadic,
439 func_id,
440 },
441 );
442
443 Ok(())
444 }
445
446 fn type_expr_to_cranelift(&self, ty: &TypeExpr) -> Result<types::Type, String> {
448 match ty {
449 TypeExpr::Path(path) => {
450 let name = path
451 .segments
452 .last()
453 .map(|s| s.ident.name.as_str())
454 .unwrap_or("");
455
456 if let Some(ctype) = CType::from_name(name) {
458 return Ok(match ctype {
459 CType::Void => types::I64, CType::Char
461 | CType::SChar
462 | CType::UChar
463 | CType::Int8
464 | CType::UInt8 => types::I8,
465 CType::Short | CType::UShort | CType::Int16 | CType::UInt16 => {
466 types::I16
467 }
468 CType::Int | CType::UInt | CType::Int32 | CType::UInt32 => types::I32,
469 CType::Long
470 | CType::ULong
471 | CType::LongLong
472 | CType::ULongLong
473 | CType::Size
474 | CType::SSize
475 | CType::PtrDiff
476 | CType::Int64
477 | CType::UInt64 => types::I64,
478 CType::Float => types::F32,
479 CType::Double => types::F64,
480 });
481 }
482
483 match name {
485 "i8" => Ok(types::I8),
486 "i16" => Ok(types::I16),
487 "i32" | "int" => Ok(types::I32),
488 "i64" => Ok(types::I64),
489 "u8" => Ok(types::I8),
490 "u16" => Ok(types::I16),
491 "u32" => Ok(types::I32),
492 "u64" => Ok(types::I64),
493 "f32" => Ok(types::F32),
494 "f64" | "float" => Ok(types::F64),
495 "bool" => Ok(types::I8),
496 "isize" | "usize" => Ok(types::I64),
497 "()" => Ok(types::I64), _ => Ok(types::I64), }
500 }
501 TypeExpr::Pointer { .. } | TypeExpr::Reference { .. } => {
502 Ok(types::I64)
504 }
505 _ => Ok(types::I64), }
507 }
508
509 fn compile_function(&mut self, func: &ast::Function) -> Result<(), String> {
511 let name = &func.name.name;
512 let func_id = *self.functions.get(name).ok_or("Function not declared")?;
513
514 for _param in &func.params {
516 self.ctx
517 .func
518 .signature
519 .params
520 .push(AbiParam::new(types::I64));
521 }
522 self.ctx
523 .func
524 .signature
525 .returns
526 .push(AbiParam::new(types::I64));
527 self.ctx.func.name = UserFuncName::user(0, func_id.as_u32());
528
529 let functions = self.functions.clone();
531 let extern_fns = self.extern_functions.clone();
532
533 {
534 let mut builder = FunctionBuilder::new(&mut self.ctx.func, &mut self.builder_ctx);
535
536 let entry_block = builder.create_block();
537 builder.append_block_params_for_function_params(entry_block);
538 builder.switch_to_block(entry_block);
539 builder.seal_block(entry_block);
540
541 let mut scope = CompileScope::new();
543
544 for (i, param) in func.params.iter().enumerate() {
546 let var = Variable::from_u32(scope.next_var() as u32);
547 builder.declare_var(var, types::I64);
548 let param_val = builder.block_params(entry_block)[i];
549 builder.def_var(var, param_val);
550
551 if let ast::Pattern::Ident { name, .. } = ¶m.pattern {
553 let param_type = match ¶m.ty {
555 TypeExpr::Path(path) => {
556 let type_name = path
557 .segments
558 .last()
559 .map(|s| s.ident.name.as_str())
560 .unwrap_or("");
561 match type_name {
562 "f32" | "f64" | "float" => ValueType::Float,
563 "i8" | "i16" | "i32" | "i64" | "int" | "isize" | "u8"
564 | "u16" | "u32" | "u64" | "usize" | "bool" => ValueType::Int,
565 _ => ValueType::Int, }
567 }
568 TypeExpr::Infer => ValueType::Int, _ => ValueType::Int, };
571 scope.define_typed(&name.name, var, param_type);
572 }
573 }
574
575 if let Some(body) = &func.body {
577 let (result, has_return) = compile_block_tracked(
578 &mut self.module,
579 &functions,
580 &extern_fns,
581 &mut builder,
582 &mut scope,
583 body,
584 )?;
585 if !has_return {
587 builder.ins().return_(&[result]);
588 }
589 } else {
590 let zero = builder.ins().iconst(types::I64, 0);
592 builder.ins().return_(&[zero]);
593 }
594
595 builder.finalize();
596 }
597
598 self.module
603 .define_function(func_id, &mut self.ctx)
604 .map_err(|e| format!("Compilation error for '{}': {}", name, e))?;
605
606 self.module.clear_context(&mut self.ctx);
607 Ok(())
608 }
609
610 pub fn run(&mut self) -> Result<i64, String> {
612 let main_id = *self.functions.get("main").ok_or("No main function")?;
613 let main_ptr = self.module.get_finalized_function(main_id);
614
615 unsafe {
616 let main_fn: CompiledFn = mem::transmute(main_ptr);
617 Ok(main_fn())
618 }
619 }
620
621 pub fn get_function(&self, name: &str) -> Option<*const u8> {
623 self.functions
624 .get(name)
625 .map(|id| self.module.get_finalized_function(*id))
626 }
627 }
628
629 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
632 enum ValueType {
633 Int, Float, Unknown, }
637
638 struct CompileScope {
643 variables: HashMap<String, Variable>,
644 var_types: HashMap<String, ValueType>,
646 var_counter: std::rc::Rc<std::cell::Cell<usize>>,
648 }
649
650 impl CompileScope {
651 fn new() -> Self {
652 Self {
653 variables: HashMap::new(),
654 var_types: HashMap::new(),
655 var_counter: std::rc::Rc::new(std::cell::Cell::new(0)),
656 }
657 }
658
659 fn child(&self) -> Self {
660 Self {
663 variables: self.variables.clone(),
664 var_types: self.var_types.clone(),
665 var_counter: std::rc::Rc::clone(&self.var_counter),
666 }
667 }
668
669 fn next_var(&mut self) -> usize {
670 let v = self.var_counter.get();
671 self.var_counter.set(v + 1);
672 v
673 }
674
675 #[allow(dead_code)]
676 fn define(&mut self, name: &str, var: Variable) {
677 self.variables.insert(name.to_string(), var);
678 }
679
680 fn define_typed(&mut self, name: &str, var: Variable, ty: ValueType) {
681 self.variables.insert(name.to_string(), var);
682 self.var_types.insert(name.to_string(), ty);
683 }
684
685 fn lookup(&self, name: &str) -> Option<Variable> {
686 self.variables.get(name).copied()
687 }
688
689 fn get_type(&self, name: &str) -> ValueType {
690 self.var_types
691 .get(name)
692 .copied()
693 .unwrap_or(ValueType::Unknown)
694 }
695
696 #[allow(dead_code)]
697 fn set_type(&mut self, name: &str, ty: ValueType) {
698 self.var_types.insert(name.to_string(), ty);
699 }
700 }
701
702 fn infer_type(expr: &Expr, scope: &CompileScope) -> ValueType {
710 match expr {
711 Expr::Literal(Literal::Int { .. }) => ValueType::Int,
712 Expr::Literal(Literal::Bool(_)) => ValueType::Int,
713 Expr::Literal(Literal::Float { .. }) => ValueType::Float,
714
715 Expr::Path(path) => {
716 let name = path
717 .segments
718 .last()
719 .map(|s| s.ident.name.as_str())
720 .unwrap_or("");
721 scope.get_type(name)
722 }
723
724 Expr::Binary { op, left, right } => {
725 let left_ty = infer_type(left, scope);
726 let right_ty = infer_type(right, scope);
727
728 if matches!(
730 op,
731 BinOp::Eq
732 | BinOp::Ne
733 | BinOp::Lt
734 | BinOp::Le
735 | BinOp::Gt
736 | BinOp::Ge
737 | BinOp::And
738 | BinOp::Or
739 ) {
740 return ValueType::Int;
741 }
742
743 if left_ty == ValueType::Float || right_ty == ValueType::Float {
745 return ValueType::Float;
746 }
747
748 if left_ty == ValueType::Int && right_ty == ValueType::Int {
750 return ValueType::Int;
751 }
752
753 ValueType::Unknown
755 }
756
757 Expr::Unary { op, expr } => {
758 match op {
759 UnaryOp::Not => ValueType::Int, UnaryOp::Neg => infer_type(expr, scope),
761 _ => infer_type(expr, scope),
762 }
763 }
764
765 Expr::Call { func, args } => {
766 if let Expr::Path(path) = func.as_ref() {
768 let name = path
769 .segments
770 .last()
771 .map(|s| s.ident.name.as_str())
772 .unwrap_or("");
773 match name {
774 "sqrt" | "sin" | "cos" | "pow" | "exp" | "ln" | "floor" | "ceil"
776 | "abs" => ValueType::Float,
777 "now" => ValueType::Int,
779 "len" | "sigil_array_len" => ValueType::Int,
781 "print" | "sigil_print" => ValueType::Int,
783 _ => {
784 let all_args_int = args
788 .iter()
789 .all(|arg| infer_type(arg, scope) == ValueType::Int);
790 if all_args_int {
791 ValueType::Int
792 } else {
793 ValueType::Unknown
794 }
795 }
796 }
797 } else {
798 ValueType::Unknown
799 }
800 }
801
802 Expr::If {
803 then_branch,
804 else_branch,
805 ..
806 } => {
807 let then_ty = if let Some(expr) = &then_branch.expr {
809 infer_type(expr, scope)
810 } else {
811 ValueType::Int };
813
814 if let Some(else_expr) = else_branch {
815 let else_ty = infer_type(else_expr, scope);
816 if then_ty == else_ty {
817 then_ty
818 } else {
819 ValueType::Unknown
820 }
821 } else {
822 then_ty
823 }
824 }
825
826 _ => ValueType::Unknown,
827 }
828 }
829
830 fn try_const_fold(expr: &Expr) -> Option<i64> {
836 match expr {
837 Expr::Literal(Literal::Int { value, .. }) => value.parse().ok(),
838 Expr::Literal(Literal::Bool(b)) => Some(if *b { 1 } else { 0 }),
839 Expr::Binary { op, left, right } => {
840 let l = try_const_fold(left)?;
841 let r = try_const_fold(right)?;
842 match op {
843 BinOp::Add => Some(l.wrapping_add(r)),
844 BinOp::Sub => Some(l.wrapping_sub(r)),
845 BinOp::Mul => Some(l.wrapping_mul(r)),
846 BinOp::Div if r != 0 => Some(l / r),
847 BinOp::Rem if r != 0 => Some(l % r),
848 BinOp::BitAnd => Some(l & r),
849 BinOp::BitOr => Some(l | r),
850 BinOp::BitXor => Some(l ^ r),
851 BinOp::Shl => Some(l << (r & 63)),
852 BinOp::Shr => Some(l >> (r & 63)),
853 BinOp::Eq => Some(if l == r { 1 } else { 0 }),
854 BinOp::Ne => Some(if l != r { 1 } else { 0 }),
855 BinOp::Lt => Some(if l < r { 1 } else { 0 }),
856 BinOp::Le => Some(if l <= r { 1 } else { 0 }),
857 BinOp::Gt => Some(if l > r { 1 } else { 0 }),
858 BinOp::Ge => Some(if l >= r { 1 } else { 0 }),
859 BinOp::And => Some(if l != 0 && r != 0 { 1 } else { 0 }),
860 BinOp::Or => Some(if l != 0 || r != 0 { 1 } else { 0 }),
861 _ => None,
862 }
863 }
864 Expr::Unary { op, expr } => {
865 let v = try_const_fold(expr)?;
866 match op {
867 UnaryOp::Neg => Some(-v),
868 UnaryOp::Not => Some(if v == 0 { 1 } else { 0 }),
869 _ => None,
870 }
871 }
872 _ => None,
873 }
874 }
875
876 fn compile_condition(
883 module: &mut JITModule,
884 functions: &HashMap<String, FuncId>,
885 extern_fns: &HashMap<String, ExternFnSig>,
886 builder: &mut FunctionBuilder,
887 scope: &mut CompileScope,
888 condition: &Expr,
889 ) -> Result<cranelift_codegen::ir::Value, String> {
890 if let Expr::Binary { op, left, right } = condition {
892 let cc = match op {
893 BinOp::Eq => Some(IntCC::Equal),
894 BinOp::Ne => Some(IntCC::NotEqual),
895 BinOp::Lt => Some(IntCC::SignedLessThan),
896 BinOp::Le => Some(IntCC::SignedLessThanOrEqual),
897 BinOp::Gt => Some(IntCC::SignedGreaterThan),
898 BinOp::Ge => Some(IntCC::SignedGreaterThanOrEqual),
899 _ => None,
900 };
901
902 if let Some(cc) = cc {
903 let lhs = compile_expr(module, functions, extern_fns, builder, scope, left)?;
904 let rhs = compile_expr(module, functions, extern_fns, builder, scope, right)?;
905 return Ok(builder.ins().icmp(cc, lhs, rhs));
907 }
908
909 if matches!(op, BinOp::And | BinOp::Or) {
911 }
914 }
915
916 if let Expr::Unary {
918 op: UnaryOp::Not,
919 expr,
920 } = condition
921 {
922 let inner = compile_condition(module, functions, extern_fns, builder, scope, expr)?;
923 let true_val = builder.ins().iconst(types::I8, 1);
925 return Ok(builder.ins().bxor(inner, true_val));
926 }
927
928 if let Expr::Literal(Literal::Bool(b)) = condition {
930 return Ok(builder.ins().iconst(types::I8, if *b { 1 } else { 0 }));
931 }
932
933 let val = compile_expr(module, functions, extern_fns, builder, scope, condition)?;
935 let zero = builder.ins().iconst(types::I64, 0);
936 Ok(builder.ins().icmp(IntCC::NotEqual, val, zero))
937 }
938
939 #[allow(dead_code)]
945 fn is_tail_call_to<'a>(expr: &'a Expr, func_name: &str) -> Option<&'a Vec<Expr>> {
946 if let Expr::Return(Some(inner)) = expr {
947 if let Expr::Call { func, args } = inner.as_ref() {
948 if let Expr::Path(path) = func.as_ref() {
949 let name = path
950 .segments
951 .last()
952 .map(|s| s.ident.name.as_str())
953 .unwrap_or("");
954 if name == func_name {
955 return Some(args);
956 }
957 }
958 }
959 }
960 None
961 }
962
963 fn compile_block_tracked(
969 module: &mut JITModule,
970 functions: &HashMap<String, FuncId>,
971 extern_fns: &HashMap<String, ExternFnSig>,
972 builder: &mut FunctionBuilder,
973 scope: &mut CompileScope,
974 block: &ast::Block,
975 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
976 let mut last_val: Option<cranelift_codegen::ir::Value> = None;
978 let mut has_return = false;
979
980 for stmt in &block.stmts {
981 let (val, ret) =
982 compile_stmt_tracked(module, functions, extern_fns, builder, scope, stmt)?;
983 last_val = Some(val);
984 if ret {
985 has_return = true;
986 }
987 }
988
989 if let Some(expr) = &block.expr {
990 let (val, ret) =
991 compile_expr_tracked(module, functions, extern_fns, builder, scope, expr)?;
992 last_val = Some(val);
993 if ret {
994 has_return = true;
995 }
996 }
997
998 let result = last_val.unwrap_or_else(|| builder.ins().iconst(types::I64, 0));
1000 Ok((result, has_return))
1001 }
1002
1003 fn compile_block(
1005 module: &mut JITModule,
1006 functions: &HashMap<String, FuncId>,
1007 extern_fns: &HashMap<String, ExternFnSig>,
1008 builder: &mut FunctionBuilder,
1009 scope: &mut CompileScope,
1010 block: &ast::Block,
1011 ) -> Result<cranelift_codegen::ir::Value, String> {
1012 compile_block_tracked(module, functions, extern_fns, builder, scope, block).map(|(v, _)| v)
1013 }
1014
1015 fn compile_stmt_tracked(
1017 module: &mut JITModule,
1018 functions: &HashMap<String, FuncId>,
1019 extern_fns: &HashMap<String, ExternFnSig>,
1020 builder: &mut FunctionBuilder,
1021 scope: &mut CompileScope,
1022 stmt: &ast::Stmt,
1023 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
1024 match stmt {
1025 ast::Stmt::Let { pattern, init, .. } => {
1026 let ty = if let Some(expr) = init {
1028 infer_type(expr, scope)
1029 } else {
1030 ValueType::Int };
1032
1033 let val = if let Some(expr) = init {
1034 compile_expr(module, functions, extern_fns, builder, scope, expr)?
1035 } else {
1036 builder.ins().iconst(types::I64, 0)
1037 };
1038
1039 if let ast::Pattern::Ident { name, .. } = pattern {
1040 let var = Variable::from_u32(scope.next_var() as u32);
1041 builder.declare_var(var, types::I64);
1042 builder.def_var(var, val);
1043 scope.define_typed(&name.name, var, ty);
1045 }
1046
1047 Ok((val, false))
1048 }
1049 ast::Stmt::LetElse { pattern, init, else_branch, .. } => {
1050 let val = compile_expr(module, functions, extern_fns, builder, scope, init)?;
1053 let ty = infer_type(init, scope);
1054
1055 if let ast::Pattern::Ident { name, .. } = pattern {
1056 let var = Variable::from_u32(scope.next_var() as u32);
1057 builder.declare_var(var, types::I64);
1058 builder.def_var(var, val);
1059 scope.define_typed(&name.name, var, ty);
1060 }
1061
1062 let _ = else_branch;
1066
1067 Ok((val, false))
1068 }
1069 ast::Stmt::Expr(expr) | ast::Stmt::Semi(expr) => {
1070 compile_expr_tracked(module, functions, extern_fns, builder, scope, expr)
1071 }
1072 ast::Stmt::Item(_) => Ok((builder.ins().iconst(types::I64, 0), false)),
1073 }
1074 }
1075
1076 #[allow(dead_code)]
1078 fn compile_stmt(
1079 module: &mut JITModule,
1080 functions: &HashMap<String, FuncId>,
1081 extern_fns: &HashMap<String, ExternFnSig>,
1082 builder: &mut FunctionBuilder,
1083 scope: &mut CompileScope,
1084 stmt: &ast::Stmt,
1085 ) -> Result<cranelift_codegen::ir::Value, String> {
1086 compile_stmt_tracked(module, functions, extern_fns, builder, scope, stmt).map(|(v, _)| v)
1087 }
1088
1089 fn compile_expr_tracked(
1091 module: &mut JITModule,
1092 functions: &HashMap<String, FuncId>,
1093 extern_fns: &HashMap<String, ExternFnSig>,
1094 builder: &mut FunctionBuilder,
1095 scope: &mut CompileScope,
1096 expr: &Expr,
1097 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
1098 match expr {
1099 Expr::Return(value) => {
1100 let ret_val = if let Some(v) = value {
1112 compile_expr(module, functions, extern_fns, builder, scope, v)?
1113 } else {
1114 builder.ins().iconst(types::I64, 0)
1115 };
1116 builder.ins().return_(&[ret_val]);
1117 Ok((ret_val, true)) }
1119 Expr::If {
1120 condition,
1121 then_branch,
1122 else_branch,
1123 } => {
1124 compile_if_tracked(
1126 module,
1127 functions,
1128 extern_fns,
1129 builder,
1130 scope,
1131 condition,
1132 then_branch,
1133 else_branch.as_deref(),
1134 )
1135 }
1136 Expr::Block(block) => {
1137 let mut inner_scope = scope.child();
1138 compile_block_tracked(
1139 module,
1140 functions,
1141 extern_fns,
1142 builder,
1143 &mut inner_scope,
1144 block,
1145 )
1146 }
1147 _ => {
1148 let val = compile_expr(module, functions, extern_fns, builder, scope, expr)?;
1150 Ok((val, false))
1151 }
1152 }
1153 }
1154
1155 fn compile_expr(
1157 module: &mut JITModule,
1158 functions: &HashMap<String, FuncId>,
1159 extern_fns: &HashMap<String, ExternFnSig>,
1160 builder: &mut FunctionBuilder,
1161 scope: &mut CompileScope,
1162 expr: &Expr,
1163 ) -> Result<cranelift_codegen::ir::Value, String> {
1164 if let Some(val) = try_const_fold(expr) {
1166 return Ok(builder.ins().iconst(types::I64, val));
1167 }
1168
1169 match expr {
1170 Expr::Literal(lit) => compile_literal(builder, lit),
1171
1172 Expr::Path(path) => {
1173 let name = path
1174 .segments
1175 .last()
1176 .map(|s| s.ident.name.clone())
1177 .unwrap_or_default();
1178 if let Some(var) = scope.lookup(&name) {
1179 Ok(builder.use_var(var))
1180 } else {
1181 Err(format!("Undefined variable: {}", name))
1182 }
1183 }
1184
1185 Expr::Binary { op, left, right } => {
1186 let left_ty = infer_type(left, scope);
1188 let right_ty = infer_type(right, scope);
1189
1190 let lhs = compile_expr(module, functions, extern_fns, builder, scope, left)?;
1191 let rhs = compile_expr(module, functions, extern_fns, builder, scope, right)?;
1192
1193 if left_ty == ValueType::Int && right_ty == ValueType::Int {
1196 return compile_binary_op(builder, op.clone(), lhs, rhs);
1198 }
1199
1200 if left_ty == ValueType::Float && right_ty == ValueType::Float {
1202 return compile_float_binary_op(builder, op, lhs, rhs);
1203 }
1204
1205 match op {
1208 BinOp::Add => compile_call(
1209 module,
1210 functions,
1211 extern_fns,
1212 builder,
1213 "sigil_add",
1214 &[lhs, rhs],
1215 ),
1216 BinOp::Sub => compile_call(
1217 module,
1218 functions,
1219 extern_fns,
1220 builder,
1221 "sigil_sub",
1222 &[lhs, rhs],
1223 ),
1224 BinOp::Mul => compile_call(
1225 module,
1226 functions,
1227 extern_fns,
1228 builder,
1229 "sigil_mul",
1230 &[lhs, rhs],
1231 ),
1232 BinOp::Div => compile_call(
1233 module,
1234 functions,
1235 extern_fns,
1236 builder,
1237 "sigil_div",
1238 &[lhs, rhs],
1239 ),
1240 BinOp::Lt => compile_call(
1241 module,
1242 functions,
1243 extern_fns,
1244 builder,
1245 "sigil_lt",
1246 &[lhs, rhs],
1247 ),
1248 BinOp::Le => compile_call(
1249 module,
1250 functions,
1251 extern_fns,
1252 builder,
1253 "sigil_le",
1254 &[lhs, rhs],
1255 ),
1256 BinOp::Gt => compile_call(
1257 module,
1258 functions,
1259 extern_fns,
1260 builder,
1261 "sigil_gt",
1262 &[lhs, rhs],
1263 ),
1264 BinOp::Ge => compile_call(
1265 module,
1266 functions,
1267 extern_fns,
1268 builder,
1269 "sigil_ge",
1270 &[lhs, rhs],
1271 ),
1272 _ => compile_binary_op(builder, op.clone(), lhs, rhs),
1273 }
1274 }
1275
1276 Expr::Unary { op, expr: inner } => {
1277 let val = compile_expr(module, functions, extern_fns, builder, scope, inner)?;
1278 compile_unary_op(builder, *op, val)
1279 }
1280
1281 Expr::Call { func, args } => {
1282 let func_name = match func.as_ref() {
1283 Expr::Path(path) => path
1284 .segments
1285 .last()
1286 .map(|s| s.ident.name.clone())
1287 .unwrap_or_default(),
1288 _ => return Err("Only direct function calls supported".into()),
1289 };
1290
1291 let mut arg_vals = Vec::new();
1292 for arg in args {
1293 arg_vals.push(compile_expr(
1294 module, functions, extern_fns, builder, scope, arg,
1295 )?);
1296 }
1297
1298 compile_call(
1299 module, functions, extern_fns, builder, &func_name, &arg_vals,
1300 )
1301 }
1302
1303 Expr::If {
1304 condition,
1305 then_branch,
1306 else_branch,
1307 } => compile_if(
1308 module,
1309 functions,
1310 extern_fns,
1311 builder,
1312 scope,
1313 condition,
1314 then_branch,
1315 else_branch.as_deref(),
1316 ),
1317
1318 Expr::While { condition, body, .. } => compile_while(
1319 module, functions, extern_fns, builder, scope, condition, body,
1320 ),
1321
1322 Expr::Block(block) => {
1323 let mut inner_scope = scope.child();
1324 compile_block(
1325 module,
1326 functions,
1327 extern_fns,
1328 builder,
1329 &mut inner_scope,
1330 block,
1331 )
1332 }
1333
1334 Expr::Return(value) => {
1335 let ret_val = if let Some(v) = value {
1338 compile_expr(module, functions, extern_fns, builder, scope, v)?
1339 } else {
1340 builder.ins().iconst(types::I64, 0)
1341 };
1342 builder.ins().return_(&[ret_val]);
1343 Ok(ret_val)
1344 }
1345
1346 Expr::Assign { target, value } => {
1347 let val = compile_expr(module, functions, extern_fns, builder, scope, value)?;
1348 match target.as_ref() {
1349 Expr::Path(path) => {
1350 let name = path
1351 .segments
1352 .last()
1353 .map(|s| s.ident.name.clone())
1354 .unwrap_or_default();
1355 if let Some(var) = scope.lookup(&name) {
1356 builder.def_var(var, val);
1357 Ok(val)
1358 } else {
1359 Err(format!("Undefined variable: {}", name))
1360 }
1361 }
1362 Expr::Index { expr: arr, index } => {
1363 let arr_val =
1364 compile_expr(module, functions, extern_fns, builder, scope, arr)?;
1365 let idx_val =
1366 compile_expr(module, functions, extern_fns, builder, scope, index)?;
1367 compile_call(
1368 module,
1369 functions,
1370 extern_fns,
1371 builder,
1372 "sigil_array_set",
1373 &[arr_val, idx_val, val],
1374 )
1375 }
1376 _ => Err("Invalid assignment target".into()),
1377 }
1378 }
1379
1380 Expr::Index { expr: arr, index } => {
1381 let arr_val = compile_expr(module, functions, extern_fns, builder, scope, arr)?;
1382 let idx_val = compile_expr(module, functions, extern_fns, builder, scope, index)?;
1383 compile_call(
1384 module,
1385 functions,
1386 extern_fns,
1387 builder,
1388 "sigil_array_get",
1389 &[arr_val, idx_val],
1390 )
1391 }
1392
1393 Expr::Array(elements) => {
1394 let len = builder.ins().iconst(types::I64, elements.len() as i64);
1395 let arr = compile_call(
1396 module,
1397 functions,
1398 extern_fns,
1399 builder,
1400 "sigil_array_new",
1401 &[len],
1402 )?;
1403
1404 for (i, elem) in elements.iter().enumerate() {
1405 let val = compile_expr(module, functions, extern_fns, builder, scope, elem)?;
1406 let idx = builder.ins().iconst(types::I64, i as i64);
1407 compile_call(
1408 module,
1409 functions,
1410 extern_fns,
1411 builder,
1412 "sigil_array_set",
1413 &[arr, idx, val],
1414 )?;
1415 }
1416
1417 Ok(arr)
1418 }
1419
1420 Expr::Pipe { expr, operations } => {
1421 let mut result = compile_expr(module, functions, extern_fns, builder, scope, expr)?;
1423
1424 for op in operations {
1426 result = match op {
1427 PipeOp::First => compile_call(
1429 module,
1430 functions,
1431 extern_fns,
1432 builder,
1433 "sigil_array_first",
1434 &[result],
1435 )?,
1436 PipeOp::Last => compile_call(
1437 module,
1438 functions,
1439 extern_fns,
1440 builder,
1441 "sigil_array_last",
1442 &[result],
1443 )?,
1444 PipeOp::Middle => compile_call(
1445 module,
1446 functions,
1447 extern_fns,
1448 builder,
1449 "sigil_array_middle",
1450 &[result],
1451 )?,
1452 PipeOp::Choice => compile_call(
1453 module,
1454 functions,
1455 extern_fns,
1456 builder,
1457 "sigil_array_choice",
1458 &[result],
1459 )?,
1460 PipeOp::Next => compile_call(
1461 module,
1462 functions,
1463 extern_fns,
1464 builder,
1465 "sigil_array_next",
1466 &[result],
1467 )?,
1468 PipeOp::Nth(index_expr) => {
1469 let index = compile_expr(
1470 module, functions, extern_fns, builder, scope, index_expr,
1471 )?;
1472 compile_call(
1473 module,
1474 functions,
1475 extern_fns,
1476 builder,
1477 "sigil_array_nth",
1478 &[result, index],
1479 )?
1480 }
1481 PipeOp::Reduce(_) => {
1483 compile_call(
1485 module,
1486 functions,
1487 extern_fns,
1488 builder,
1489 "sigil_array_sum",
1490 &[result],
1491 )?
1492 }
1493 PipeOp::ReduceSum => compile_call(
1495 module,
1496 functions,
1497 extern_fns,
1498 builder,
1499 "sigil_array_sum",
1500 &[result],
1501 )?,
1502 PipeOp::ReduceProd => compile_call(
1504 module,
1505 functions,
1506 extern_fns,
1507 builder,
1508 "sigil_array_product",
1509 &[result],
1510 )?,
1511 PipeOp::ReduceMin => compile_call(
1513 module,
1514 functions,
1515 extern_fns,
1516 builder,
1517 "sigil_array_min",
1518 &[result],
1519 )?,
1520 PipeOp::ReduceMax => compile_call(
1522 module,
1523 functions,
1524 extern_fns,
1525 builder,
1526 "sigil_array_max",
1527 &[result],
1528 )?,
1529 PipeOp::ReduceConcat => compile_call(
1531 module,
1532 functions,
1533 extern_fns,
1534 builder,
1535 "sigil_array_concat",
1536 &[result],
1537 )?,
1538 PipeOp::ReduceAll => compile_call(
1540 module,
1541 functions,
1542 extern_fns,
1543 builder,
1544 "sigil_array_all",
1545 &[result],
1546 )?,
1547 PipeOp::ReduceAny => compile_call(
1549 module,
1550 functions,
1551 extern_fns,
1552 builder,
1553 "sigil_array_any",
1554 &[result],
1555 )?,
1556 PipeOp::Sort(_) => compile_call(
1558 module,
1559 functions,
1560 extern_fns,
1561 builder,
1562 "sigil_array_sort",
1563 &[result],
1564 )?,
1565 PipeOp::Transform(_) | PipeOp::Filter(_) => {
1567 result
1570 }
1571 PipeOp::Method { name, type_args: _, args } => {
1573 let mut call_args = vec![result];
1575 for arg in args {
1576 call_args.push(compile_expr(
1577 module, functions, extern_fns, builder, scope, arg,
1578 )?);
1579 }
1580 compile_call(
1581 module, functions, extern_fns, builder, &name.name, &call_args,
1582 )?
1583 }
1584 PipeOp::Await => {
1585 result
1587 }
1588 PipeOp::Match(_) => {
1589 result
1592 }
1593 PipeOp::TryMap(_) => {
1594 result
1596 }
1597 PipeOp::Call(callee) => {
1598 let callee_val = compile_expr(
1601 module, functions, extern_fns, builder, scope, callee,
1602 )?;
1603 compile_call(
1605 module, functions, extern_fns, builder,
1606 "sigil_call",
1607 &[callee_val, result],
1608 )?
1609 }
1610 PipeOp::Named { prefix, body } => {
1611 if !prefix.is_empty() {
1613 let fn_name = &prefix[0].name;
1614 if let Some(body_expr) = body {
1615 let body_val = compile_expr(
1616 module, functions, extern_fns, builder, scope, body_expr,
1617 )?;
1618 compile_call(
1619 module,
1620 functions,
1621 extern_fns,
1622 builder,
1623 fn_name,
1624 &[result, body_val],
1625 )?
1626 } else {
1627 compile_call(
1628 module,
1629 functions,
1630 extern_fns,
1631 builder,
1632 fn_name,
1633 &[result],
1634 )?
1635 }
1636 } else {
1637 result
1638 }
1639 }
1640 PipeOp::Parallel(inner_op) => {
1642 match inner_op.as_ref() {
1645 PipeOp::Transform(_) => {
1646 compile_call(
1648 module,
1649 functions,
1650 extern_fns,
1651 builder,
1652 "sigil_parallel_map",
1653 &[result],
1654 )?
1655 }
1656 PipeOp::Filter(_) => {
1657 compile_call(
1659 module,
1660 functions,
1661 extern_fns,
1662 builder,
1663 "sigil_parallel_filter",
1664 &[result],
1665 )?
1666 }
1667 PipeOp::Reduce(_) => {
1668 compile_call(
1670 module,
1671 functions,
1672 extern_fns,
1673 builder,
1674 "sigil_parallel_reduce",
1675 &[result],
1676 )?
1677 }
1678 _ => result,
1680 }
1681 }
1682 PipeOp::Gpu(inner_op) => {
1684 match inner_op.as_ref() {
1687 PipeOp::Transform(_) => {
1688 compile_call(
1690 module,
1691 functions,
1692 extern_fns,
1693 builder,
1694 "sigil_gpu_map",
1695 &[result],
1696 )?
1697 }
1698 PipeOp::Filter(_) => {
1699 compile_call(
1701 module,
1702 functions,
1703 extern_fns,
1704 builder,
1705 "sigil_gpu_filter",
1706 &[result],
1707 )?
1708 }
1709 PipeOp::Reduce(_) => {
1710 compile_call(
1712 module,
1713 functions,
1714 extern_fns,
1715 builder,
1716 "sigil_gpu_reduce",
1717 &[result],
1718 )?
1719 }
1720 _ => result,
1721 }
1722 }
1723
1724 PipeOp::Send(data_expr) => {
1731 let data = compile_expr(
1732 module, functions, extern_fns, builder, scope, data_expr,
1733 )?;
1734 compile_call(
1735 module,
1736 functions,
1737 extern_fns,
1738 builder,
1739 "sigil_protocol_send",
1740 &[result, data],
1741 )?
1742 }
1743
1744 PipeOp::Recv => compile_call(
1746 module,
1747 functions,
1748 extern_fns,
1749 builder,
1750 "sigil_protocol_recv",
1751 &[result],
1752 )?,
1753
1754 PipeOp::Stream(handler_expr) => {
1756 let handler = compile_expr(
1757 module,
1758 functions,
1759 extern_fns,
1760 builder,
1761 scope,
1762 handler_expr,
1763 )?;
1764 compile_call(
1765 module,
1766 functions,
1767 extern_fns,
1768 builder,
1769 "sigil_protocol_stream",
1770 &[result, handler],
1771 )?
1772 }
1773
1774 PipeOp::Connect(config_expr) => {
1776 if let Some(config) = config_expr {
1777 let config_val = compile_expr(
1778 module, functions, extern_fns, builder, scope, config,
1779 )?;
1780 compile_call(
1781 module,
1782 functions,
1783 extern_fns,
1784 builder,
1785 "sigil_protocol_connect",
1786 &[result, config_val],
1787 )?
1788 } else {
1789 compile_call(
1790 module,
1791 functions,
1792 extern_fns,
1793 builder,
1794 "sigil_protocol_connect_default",
1795 &[result],
1796 )?
1797 }
1798 }
1799
1800 PipeOp::Close => compile_call(
1802 module,
1803 functions,
1804 extern_fns,
1805 builder,
1806 "sigil_protocol_close",
1807 &[result],
1808 )?,
1809
1810 PipeOp::Header { name, value } => {
1812 let name_val =
1813 compile_expr(module, functions, extern_fns, builder, scope, name)?;
1814 let value_val =
1815 compile_expr(module, functions, extern_fns, builder, scope, value)?;
1816 compile_call(
1817 module,
1818 functions,
1819 extern_fns,
1820 builder,
1821 "sigil_protocol_header",
1822 &[result, name_val, value_val],
1823 )?
1824 }
1825
1826 PipeOp::Body(data_expr) => {
1828 let data = compile_expr(
1829 module, functions, extern_fns, builder, scope, data_expr,
1830 )?;
1831 compile_call(
1832 module,
1833 functions,
1834 extern_fns,
1835 builder,
1836 "sigil_protocol_body",
1837 &[result, data],
1838 )?
1839 }
1840
1841 PipeOp::Timeout(ms_expr) => {
1843 let ms = compile_expr(
1844 module, functions, extern_fns, builder, scope, ms_expr,
1845 )?;
1846 compile_call(
1847 module,
1848 functions,
1849 extern_fns,
1850 builder,
1851 "sigil_protocol_timeout",
1852 &[result, ms],
1853 )?
1854 }
1855
1856 PipeOp::Retry { count, strategy } => {
1858 let count_val =
1859 compile_expr(module, functions, extern_fns, builder, scope, count)?;
1860 if let Some(strat) = strategy {
1861 let strat_val = compile_expr(
1862 module, functions, extern_fns, builder, scope, strat,
1863 )?;
1864 compile_call(
1865 module,
1866 functions,
1867 extern_fns,
1868 builder,
1869 "sigil_protocol_retry",
1870 &[result, count_val, strat_val],
1871 )?
1872 } else {
1873 compile_call(
1874 module,
1875 functions,
1876 extern_fns,
1877 builder,
1878 "sigil_protocol_retry_default",
1879 &[result, count_val],
1880 )?
1881 }
1882 }
1883
1884 PipeOp::Validate {
1886 predicate,
1887 target_evidence: _,
1888 } => {
1889 let pred_val = compile_expr(
1890 module, functions, extern_fns, builder, scope, predicate,
1891 )?;
1892 compile_call(
1893 module,
1894 functions,
1895 extern_fns,
1896 builder,
1897 "sigil_validate",
1898 &[result, pred_val],
1899 )?
1900 }
1901
1902 PipeOp::Assume {
1903 reason,
1904 target_evidence: _,
1905 } => {
1906 let reason_val = if let Some(r) = reason {
1907 compile_expr(module, functions, extern_fns, builder, scope, r)?
1908 } else {
1909 builder.ins().iconst(types::I64, 0)
1910 };
1911 compile_call(
1912 module,
1913 functions,
1914 extern_fns,
1915 builder,
1916 "sigil_assume",
1917 &[result, reason_val],
1918 )?
1919 }
1920
1921 PipeOp::AssertEvidence(_) => {
1922 result
1925 }
1926
1927 PipeOp::Also(func) => {
1929 let _ =
1931 compile_expr(module, functions, extern_fns, builder, scope, func)?;
1932 result
1933 }
1934
1935 PipeOp::Apply(func) => {
1936 let _ =
1938 compile_expr(module, functions, extern_fns, builder, scope, func)?;
1939 result
1940 }
1941
1942 PipeOp::TakeIf(pred) => {
1943 let pred_val =
1945 compile_expr(module, functions, extern_fns, builder, scope, pred)?;
1946 compile_call(
1947 module,
1948 functions,
1949 extern_fns,
1950 builder,
1951 "sigil_take_if",
1952 &[result, pred_val],
1953 )?
1954 }
1955
1956 PipeOp::TakeUnless(pred) => {
1957 let pred_val =
1959 compile_expr(module, functions, extern_fns, builder, scope, pred)?;
1960 compile_call(
1961 module,
1962 functions,
1963 extern_fns,
1964 builder,
1965 "sigil_take_unless",
1966 &[result, pred_val],
1967 )?
1968 }
1969
1970 PipeOp::Let(func) => {
1971 compile_expr(module, functions, extern_fns, builder, scope, func)?
1973 }
1974
1975 PipeOp::All(_)
1978 | PipeOp::Any(_)
1979 | PipeOp::Compose(_)
1980 | PipeOp::Zip(_)
1981 | PipeOp::Scan(_)
1982 | PipeOp::Diff
1983 | PipeOp::Gradient(_)
1984 | PipeOp::SortAsc
1985 | PipeOp::SortDesc
1986 | PipeOp::Reverse
1987 | PipeOp::Cycle(_)
1988 | PipeOp::Windows(_)
1989 | PipeOp::Chunks(_)
1990 | PipeOp::Flatten
1991 | PipeOp::Unique
1992 | PipeOp::Enumerate => {
1993 result
1995 }
1996 };
1997 }
1998
1999 Ok(result)
2000 }
2001
2002 Expr::Unsafe(block) => {
2004 let mut inner_scope = scope.child();
2005 compile_block(
2006 module,
2007 functions,
2008 extern_fns,
2009 builder,
2010 &mut inner_scope,
2011 block,
2012 )
2013 }
2014
2015 Expr::Async { block, .. } => {
2017 let mut inner_scope = scope.child();
2018 compile_block(
2019 module,
2020 functions,
2021 extern_fns,
2022 builder,
2023 &mut inner_scope,
2024 block,
2025 )
2026 }
2027
2028 Expr::Deref(inner) => {
2030 let ptr = compile_expr(module, functions, extern_fns, builder, scope, inner)?;
2031 Ok(builder
2033 .ins()
2034 .load(types::I64, cranelift_codegen::ir::MemFlags::new(), ptr, 0))
2035 }
2036
2037 Expr::AddrOf { expr: inner, .. } => {
2039 compile_expr(module, functions, extern_fns, builder, scope, inner)
2040 }
2041
2042 Expr::Cast { expr: inner, ty } => {
2044 let val = compile_expr(module, functions, extern_fns, builder, scope, inner)?;
2045 let _ = ty; Ok(val)
2048 }
2049
2050 _ => Ok(builder.ins().iconst(types::I64, 0)),
2051 }
2052 }
2053
2054 fn compile_literal(
2056 builder: &mut FunctionBuilder,
2057 lit: &Literal,
2058 ) -> Result<cranelift_codegen::ir::Value, String> {
2059 match lit {
2060 Literal::Int { value, .. } => {
2061 let val: i64 = value.parse().map_err(|_| "Invalid integer")?;
2062 Ok(builder.ins().iconst(types::I64, val))
2063 }
2064 Literal::Float { value, .. } => {
2065 let val: f64 = value.parse().map_err(|_| "Invalid float")?;
2066 Ok(builder.ins().iconst(types::I64, val.to_bits() as i64))
2069 }
2070 Literal::Bool(b) => Ok(builder.ins().iconst(types::I64, if *b { 1 } else { 0 })),
2071 Literal::String(_) => Ok(builder.ins().iconst(types::I64, 0)),
2072 _ => Ok(builder.ins().iconst(types::I64, 0)),
2073 }
2074 }
2075
2076 fn compile_binary_op(
2078 builder: &mut FunctionBuilder,
2079 op: BinOp,
2080 lhs: cranelift_codegen::ir::Value,
2081 rhs: cranelift_codegen::ir::Value,
2082 ) -> Result<cranelift_codegen::ir::Value, String> {
2083 let result = match op {
2084 BinOp::Add => builder.ins().iadd(lhs, rhs),
2085 BinOp::Sub => builder.ins().isub(lhs, rhs),
2086 BinOp::Mul => builder.ins().imul(lhs, rhs),
2087 BinOp::Div => builder.ins().sdiv(lhs, rhs),
2088 BinOp::Rem => builder.ins().srem(lhs, rhs),
2089 BinOp::Pow => return Err("Power not supported".into()),
2090 BinOp::BitAnd => builder.ins().band(lhs, rhs),
2091 BinOp::BitOr => builder.ins().bor(lhs, rhs),
2092 BinOp::BitXor => builder.ins().bxor(lhs, rhs),
2093 BinOp::Shl => builder.ins().ishl(lhs, rhs),
2094 BinOp::Shr => builder.ins().sshr(lhs, rhs),
2095 BinOp::Eq => {
2096 let cmp = builder.ins().icmp(IntCC::Equal, lhs, rhs);
2097 builder.ins().uextend(types::I64, cmp)
2098 }
2099 BinOp::Ne => {
2100 let cmp = builder.ins().icmp(IntCC::NotEqual, lhs, rhs);
2101 builder.ins().uextend(types::I64, cmp)
2102 }
2103 BinOp::Lt => {
2104 let cmp = builder.ins().icmp(IntCC::SignedLessThan, lhs, rhs);
2105 builder.ins().uextend(types::I64, cmp)
2106 }
2107 BinOp::Le => {
2108 let cmp = builder.ins().icmp(IntCC::SignedLessThanOrEqual, lhs, rhs);
2109 builder.ins().uextend(types::I64, cmp)
2110 }
2111 BinOp::Gt => {
2112 let cmp = builder.ins().icmp(IntCC::SignedGreaterThan, lhs, rhs);
2113 builder.ins().uextend(types::I64, cmp)
2114 }
2115 BinOp::Ge => {
2116 let cmp = builder
2117 .ins()
2118 .icmp(IntCC::SignedGreaterThanOrEqual, lhs, rhs);
2119 builder.ins().uextend(types::I64, cmp)
2120 }
2121 BinOp::And => builder.ins().band(lhs, rhs),
2122 BinOp::Or => builder.ins().bor(lhs, rhs),
2123 BinOp::Concat => return Err("Concat not supported".into()),
2124 BinOp::MatMul => return Err("MatMul not supported in JIT (use runtime)".into()),
2125 BinOp::Hadamard => return Err("Hadamard not supported in JIT (use runtime)".into()),
2126 BinOp::TensorProd => return Err("TensorProd not supported in JIT (use runtime)".into()),
2127 };
2128 Ok(result)
2129 }
2130
2131 fn compile_float_binary_op(
2133 builder: &mut FunctionBuilder,
2134 op: &BinOp,
2135 lhs: cranelift_codegen::ir::Value,
2136 rhs: cranelift_codegen::ir::Value,
2137 ) -> Result<cranelift_codegen::ir::Value, String> {
2138 use cranelift_codegen::ir::condcodes::FloatCC;
2139
2140 let lhs_f = builder
2142 .ins()
2143 .bitcast(types::F64, cranelift_codegen::ir::MemFlags::new(), lhs);
2144 let rhs_f = builder
2145 .ins()
2146 .bitcast(types::F64, cranelift_codegen::ir::MemFlags::new(), rhs);
2147
2148 let result_f = match op {
2149 BinOp::Add => builder.ins().fadd(lhs_f, rhs_f),
2150 BinOp::Sub => builder.ins().fsub(lhs_f, rhs_f),
2151 BinOp::Mul => builder.ins().fmul(lhs_f, rhs_f),
2152 BinOp::Div => builder.ins().fdiv(lhs_f, rhs_f),
2153 BinOp::Lt => {
2154 let cmp = builder.ins().fcmp(FloatCC::LessThan, lhs_f, rhs_f);
2155 return Ok(builder.ins().uextend(types::I64, cmp));
2156 }
2157 BinOp::Le => {
2158 let cmp = builder.ins().fcmp(FloatCC::LessThanOrEqual, lhs_f, rhs_f);
2159 return Ok(builder.ins().uextend(types::I64, cmp));
2160 }
2161 BinOp::Gt => {
2162 let cmp = builder.ins().fcmp(FloatCC::GreaterThan, lhs_f, rhs_f);
2163 return Ok(builder.ins().uextend(types::I64, cmp));
2164 }
2165 BinOp::Ge => {
2166 let cmp = builder
2167 .ins()
2168 .fcmp(FloatCC::GreaterThanOrEqual, lhs_f, rhs_f);
2169 return Ok(builder.ins().uextend(types::I64, cmp));
2170 }
2171 BinOp::Eq => {
2172 let cmp = builder.ins().fcmp(FloatCC::Equal, lhs_f, rhs_f);
2173 return Ok(builder.ins().uextend(types::I64, cmp));
2174 }
2175 BinOp::Ne => {
2176 let cmp = builder.ins().fcmp(FloatCC::NotEqual, lhs_f, rhs_f);
2177 return Ok(builder.ins().uextend(types::I64, cmp));
2178 }
2179 _ => return Err(format!("Float operation {:?} not supported", op)),
2180 };
2181
2182 Ok(builder
2184 .ins()
2185 .bitcast(types::I64, cranelift_codegen::ir::MemFlags::new(), result_f))
2186 }
2187
2188 fn compile_unary_op(
2190 builder: &mut FunctionBuilder,
2191 op: UnaryOp,
2192 val: cranelift_codegen::ir::Value,
2193 ) -> Result<cranelift_codegen::ir::Value, String> {
2194 let result = match op {
2195 UnaryOp::Neg => builder.ins().ineg(val),
2196 UnaryOp::Not => {
2197 let zero = builder.ins().iconst(types::I64, 0);
2198 let cmp = builder.ins().icmp(IntCC::Equal, val, zero);
2199 builder.ins().uextend(types::I64, cmp)
2200 }
2201 UnaryOp::Deref | UnaryOp::Ref | UnaryOp::RefMut => val,
2202 };
2203 Ok(result)
2204 }
2205
2206 fn compile_call(
2208 module: &mut JITModule,
2209 functions: &HashMap<String, FuncId>,
2210 extern_fns: &HashMap<String, ExternFnSig>,
2211 builder: &mut FunctionBuilder,
2212 name: &str,
2213 args: &[cranelift_codegen::ir::Value],
2214 ) -> Result<cranelift_codegen::ir::Value, String> {
2215 let builtin_name = match name {
2216 "sqrt" => Some("sigil_sqrt"),
2217 "sin" => Some("sigil_sin"),
2218 "cos" => Some("sigil_cos"),
2219 "pow" => Some("sigil_pow"),
2220 "exp" => Some("sigil_exp"),
2221 "ln" => Some("sigil_ln"),
2222 "floor" => Some("sigil_floor"),
2223 "ceil" => Some("sigil_ceil"),
2224 "abs" => Some("sigil_abs"),
2225 "print" => Some("sigil_print"),
2226 "now" => Some("sigil_now"),
2227 "ackermann" => Some("sigil_ackermann"),
2229 "tak" => Some("sigil_tak"),
2230 n if n.starts_with("sigil_") => Some(n),
2231 _ => None,
2232 };
2233
2234 if let Some(builtin) = builtin_name {
2235 let mut sig = module.make_signature();
2236
2237 match builtin {
2238 "sigil_sqrt" | "sigil_sin" | "sigil_cos" | "sigil_exp" | "sigil_ln"
2239 | "sigil_floor" | "sigil_ceil" | "sigil_abs" => {
2240 sig.params.push(AbiParam::new(types::F64));
2241 sig.returns.push(AbiParam::new(types::F64));
2242 }
2243 "sigil_pow" => {
2244 sig.params.push(AbiParam::new(types::F64));
2245 sig.params.push(AbiParam::new(types::F64));
2246 sig.returns.push(AbiParam::new(types::F64));
2247 }
2248 "sigil_print_int" => {
2249 sig.params.push(AbiParam::new(types::I64));
2250 sig.returns.push(AbiParam::new(types::I64));
2251 }
2252 "sigil_now" => {
2253 sig.returns.push(AbiParam::new(types::I64));
2254 }
2255 "sigil_array_new" => {
2256 sig.params.push(AbiParam::new(types::I64));
2257 sig.returns.push(AbiParam::new(types::I64));
2258 }
2259 "sigil_array_get" | "sigil_array_set" => {
2260 sig.params.push(AbiParam::new(types::I64));
2261 sig.params.push(AbiParam::new(types::I64));
2262 if builtin == "sigil_array_set" {
2263 sig.params.push(AbiParam::new(types::I64));
2264 }
2265 sig.returns.push(AbiParam::new(types::I64));
2266 }
2267 "sigil_array_len" => {
2268 sig.params.push(AbiParam::new(types::I64));
2269 sig.returns.push(AbiParam::new(types::I64));
2270 }
2271 "sigil_array_first"
2273 | "sigil_array_last"
2274 | "sigil_array_middle"
2275 | "sigil_array_choice"
2276 | "sigil_array_next"
2277 | "sigil_array_sum"
2278 | "sigil_array_product" => {
2279 sig.params.push(AbiParam::new(types::I64));
2280 sig.returns.push(AbiParam::new(types::I64));
2281 }
2282 "sigil_array_sort" => {
2284 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2287 "sigil_parallel_map" | "sigil_parallel_filter" => {
2289 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2292 "sigil_parallel_reduce" => {
2293 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2296 "sigil_gpu_map" | "sigil_gpu_filter" => {
2298 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2301 "sigil_gpu_reduce" => {
2302 sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64)); }
2305 "sigil_array_nth" => {
2307 sig.params.push(AbiParam::new(types::I64)); sig.params.push(AbiParam::new(types::I64)); sig.returns.push(AbiParam::new(types::I64));
2310 }
2311 _ => {
2312 for _ in args {
2313 sig.params.push(AbiParam::new(types::I64));
2314 }
2315 sig.returns.push(AbiParam::new(types::I64));
2316 }
2317 }
2318
2319 let callee = module
2320 .declare_function(builtin, Linkage::Import, &sig)
2321 .map_err(|e| e.to_string())?;
2322
2323 let local_callee = module.declare_func_in_func(callee, builder.func);
2324
2325 let call_args: Vec<_> = if matches!(
2326 builtin,
2327 "sigil_sqrt"
2328 | "sigil_sin"
2329 | "sigil_cos"
2330 | "sigil_exp"
2331 | "sigil_ln"
2332 | "sigil_floor"
2333 | "sigil_ceil"
2334 | "sigil_abs"
2335 | "sigil_pow"
2336 ) {
2337 args.iter()
2338 .map(|&v| {
2339 if builder.func.dfg.value_type(v) == types::F64 {
2340 v
2341 } else {
2342 builder.ins().fcvt_from_sint(types::F64, v)
2343 }
2344 })
2345 .collect()
2346 } else {
2347 args.to_vec()
2348 };
2349
2350 let call = builder.ins().call(local_callee, &call_args);
2351 Ok(builder.inst_results(call)[0])
2352 } else if let Some(&func_id) = functions.get(name) {
2353 let local_callee = module.declare_func_in_func(func_id, builder.func);
2355 let call = builder.ins().call(local_callee, args);
2356 Ok(builder.inst_results(call)[0])
2357 } else if let Some(extern_fn) = extern_fns.get(name) {
2358 let local_callee = module.declare_func_in_func(extern_fn.func_id, builder.func);
2360
2361 let mut call_args = Vec::new();
2363 for (i, &arg) in args.iter().enumerate() {
2364 let arg_type = builder.func.dfg.value_type(arg);
2365 let expected_type = extern_fn.params.get(i).copied().unwrap_or(types::I64);
2366
2367 let converted = if arg_type == expected_type {
2368 arg
2369 } else if arg_type == types::I64 && expected_type == types::I32 {
2370 builder.ins().ireduce(types::I32, arg)
2371 } else if arg_type == types::I32 && expected_type == types::I64 {
2372 builder.ins().sextend(types::I64, arg)
2373 } else if arg_type == types::I64 && expected_type == types::F64 {
2374 builder.ins().fcvt_from_sint(types::F64, arg)
2375 } else if arg_type == types::F64 && expected_type == types::I64 {
2376 builder.ins().fcvt_to_sint(types::I64, arg)
2377 } else {
2378 arg };
2380 call_args.push(converted);
2381 }
2382
2383 let call = builder.ins().call(local_callee, &call_args);
2384
2385 if extern_fn.returns.is_some() {
2387 let result = builder.inst_results(call)[0];
2388 let result_type = builder.func.dfg.value_type(result);
2389 if result_type == types::I32
2391 || result_type == types::I16
2392 || result_type == types::I8
2393 {
2394 Ok(builder.ins().sextend(types::I64, result))
2395 } else {
2396 Ok(result)
2397 }
2398 } else {
2399 Ok(builder.ins().iconst(types::I64, 0))
2401 }
2402 } else {
2403 Err(format!("Unknown function: {}", name))
2404 }
2405 }
2406
2407 fn compile_if_tracked(
2409 module: &mut JITModule,
2410 functions: &HashMap<String, FuncId>,
2411 extern_fns: &HashMap<String, ExternFnSig>,
2412 builder: &mut FunctionBuilder,
2413 scope: &mut CompileScope,
2414 condition: &Expr,
2415 then_branch: &ast::Block,
2416 else_branch: Option<&Expr>,
2417 ) -> Result<(cranelift_codegen::ir::Value, bool), String> {
2418 let cond_bool =
2420 compile_condition(module, functions, extern_fns, builder, scope, condition)?;
2421
2422 let then_block = builder.create_block();
2423 let else_block = builder.create_block();
2424 let merge_block = builder.create_block();
2425
2426 builder.append_block_param(merge_block, types::I64);
2427
2428 builder
2430 .ins()
2431 .brif(cond_bool, then_block, &[], else_block, &[]);
2432
2433 builder.switch_to_block(then_block);
2435 builder.seal_block(then_block);
2436 let mut then_scope = scope.child();
2437 let (then_val, then_returns) = compile_block_tracked(
2438 module,
2439 functions,
2440 extern_fns,
2441 builder,
2442 &mut then_scope,
2443 then_branch,
2444 )?;
2445 if !then_returns {
2447 builder.ins().jump(merge_block, &[then_val]);
2448 }
2449
2450 builder.switch_to_block(else_block);
2452 builder.seal_block(else_block);
2453 let (else_val, else_returns) = if let Some(else_expr) = else_branch {
2454 match else_expr {
2455 Expr::Block(block) => {
2456 let mut else_scope = scope.child();
2457 compile_block_tracked(
2458 module,
2459 functions,
2460 extern_fns,
2461 builder,
2462 &mut else_scope,
2463 block,
2464 )?
2465 }
2466 Expr::If {
2467 condition,
2468 then_branch,
2469 else_branch,
2470 } => compile_if_tracked(
2471 module,
2472 functions,
2473 extern_fns,
2474 builder,
2475 scope,
2476 condition,
2477 then_branch,
2478 else_branch.as_deref(),
2479 )?,
2480 _ => {
2481 let val =
2482 compile_expr(module, functions, extern_fns, builder, scope, else_expr)?;
2483 (val, false)
2484 }
2485 }
2486 } else {
2487 (builder.ins().iconst(types::I64, 0), false)
2488 };
2489 if !else_returns {
2491 builder.ins().jump(merge_block, &[else_val]);
2492 }
2493
2494 let both_return = then_returns && else_returns;
2497
2498 builder.switch_to_block(merge_block);
2499 builder.seal_block(merge_block);
2500
2501 if both_return {
2502 let dummy = builder.ins().iconst(types::I64, 0);
2505 Ok((dummy, true))
2506 } else {
2507 Ok((builder.block_params(merge_block)[0], false))
2508 }
2509 }
2510
2511 fn compile_if(
2513 module: &mut JITModule,
2514 functions: &HashMap<String, FuncId>,
2515 extern_fns: &HashMap<String, ExternFnSig>,
2516 builder: &mut FunctionBuilder,
2517 scope: &mut CompileScope,
2518 condition: &Expr,
2519 then_branch: &ast::Block,
2520 else_branch: Option<&Expr>,
2521 ) -> Result<cranelift_codegen::ir::Value, String> {
2522 compile_if_tracked(
2523 module,
2524 functions,
2525 extern_fns,
2526 builder,
2527 scope,
2528 condition,
2529 then_branch,
2530 else_branch,
2531 )
2532 .map(|(v, _)| v)
2533 }
2534
2535 fn compile_while(
2537 module: &mut JITModule,
2538 functions: &HashMap<String, FuncId>,
2539 extern_fns: &HashMap<String, ExternFnSig>,
2540 builder: &mut FunctionBuilder,
2541 scope: &mut CompileScope,
2542 condition: &Expr,
2543 body: &ast::Block,
2544 ) -> Result<cranelift_codegen::ir::Value, String> {
2545 let header_block = builder.create_block();
2546 let body_block = builder.create_block();
2547 let exit_block = builder.create_block();
2548
2549 builder.ins().jump(header_block, &[]);
2550
2551 builder.switch_to_block(header_block);
2552 let cond_bool =
2554 compile_condition(module, functions, extern_fns, builder, scope, condition)?;
2555 builder
2557 .ins()
2558 .brif(cond_bool, body_block, &[], exit_block, &[]);
2559
2560 builder.switch_to_block(body_block);
2561 builder.seal_block(body_block);
2562 let mut body_scope = scope.child();
2563 compile_block(
2564 module,
2565 functions,
2566 extern_fns,
2567 builder,
2568 &mut body_scope,
2569 body,
2570 )?;
2571 builder.ins().jump(header_block, &[]);
2572
2573 builder.seal_block(header_block);
2574
2575 builder.switch_to_block(exit_block);
2576 builder.seal_block(exit_block);
2577
2578 Ok(builder.ins().iconst(types::I64, 0))
2579 }
2580
2581 #[inline]
2589 fn is_float_pattern(v: i64) -> bool {
2590 let exp = (v >> 52) & 0x7FF;
2591 exp > 0 && exp < 0x7FF && v != 0
2594 }
2595
2596 #[no_mangle]
2597 pub extern "C" fn sigil_add(a: i64, b: i64) -> i64 {
2598 if is_float_pattern(a) || is_float_pattern(b) {
2599 let fa = f64::from_bits(a as u64);
2600 let fb = f64::from_bits(b as u64);
2601 (fa + fb).to_bits() as i64
2602 } else {
2603 a.wrapping_add(b)
2604 }
2605 }
2606
2607 #[no_mangle]
2608 pub extern "C" fn sigil_sub(a: i64, b: i64) -> i64 {
2609 if is_float_pattern(a) || is_float_pattern(b) {
2610 let fa = f64::from_bits(a as u64);
2611 let fb = f64::from_bits(b as u64);
2612 (fa - fb).to_bits() as i64
2613 } else {
2614 a.wrapping_sub(b)
2615 }
2616 }
2617
2618 #[no_mangle]
2619 pub extern "C" fn sigil_mul(a: i64, b: i64) -> i64 {
2620 if is_float_pattern(a) || is_float_pattern(b) {
2621 let fa = f64::from_bits(a as u64);
2622 let fb = f64::from_bits(b as u64);
2623 (fa * fb).to_bits() as i64
2624 } else {
2625 a.wrapping_mul(b)
2626 }
2627 }
2628
2629 #[no_mangle]
2630 pub extern "C" fn sigil_div(a: i64, b: i64) -> i64 {
2631 if is_float_pattern(a) || is_float_pattern(b) {
2632 let fa = f64::from_bits(a as u64);
2633 let fb = f64::from_bits(b as u64);
2634 (fa / fb).to_bits() as i64
2635 } else if b != 0 {
2636 a / b
2637 } else {
2638 0 }
2640 }
2641
2642 #[no_mangle]
2643 pub extern "C" fn sigil_lt(a: i64, b: i64) -> i64 {
2644 if is_float_pattern(a) || is_float_pattern(b) {
2645 let fa = f64::from_bits(a as u64);
2646 let fb = f64::from_bits(b as u64);
2647 if fa < fb {
2648 1
2649 } else {
2650 0
2651 }
2652 } else {
2653 if a < b {
2654 1
2655 } else {
2656 0
2657 }
2658 }
2659 }
2660
2661 #[no_mangle]
2662 pub extern "C" fn sigil_le(a: i64, b: i64) -> i64 {
2663 if is_float_pattern(a) || is_float_pattern(b) {
2664 let fa = f64::from_bits(a as u64);
2665 let fb = f64::from_bits(b as u64);
2666 if fa <= fb {
2667 1
2668 } else {
2669 0
2670 }
2671 } else {
2672 if a <= b {
2673 1
2674 } else {
2675 0
2676 }
2677 }
2678 }
2679
2680 #[no_mangle]
2681 pub extern "C" fn sigil_gt(a: i64, b: i64) -> i64 {
2682 if is_float_pattern(a) || is_float_pattern(b) {
2683 let fa = f64::from_bits(a as u64);
2684 let fb = f64::from_bits(b as u64);
2685 if fa > fb {
2686 1
2687 } else {
2688 0
2689 }
2690 } else {
2691 if a > b {
2692 1
2693 } else {
2694 0
2695 }
2696 }
2697 }
2698
2699 #[no_mangle]
2700 pub extern "C" fn sigil_ge(a: i64, b: i64) -> i64 {
2701 if is_float_pattern(a) || is_float_pattern(b) {
2702 let fa = f64::from_bits(a as u64);
2703 let fb = f64::from_bits(b as u64);
2704 if fa >= fb {
2705 1
2706 } else {
2707 0
2708 }
2709 } else {
2710 if a >= b {
2711 1
2712 } else {
2713 0
2714 }
2715 }
2716 }
2717
2718 #[no_mangle]
2720 pub extern "C" fn sigil_print(v: i64) -> i64 {
2721 if is_float_pattern(v) {
2722 println!("{}", f64::from_bits(v as u64));
2723 } else {
2724 println!("{}", v);
2725 }
2726 0
2727 }
2728
2729 #[repr(C, align(32))]
2741 struct SimdVec4 {
2742 data: [f64; 4],
2743 }
2744
2745 impl SimdVec4 {
2746 #[inline(always)]
2747 fn new(x: f64, y: f64, z: f64, w: f64) -> Box<Self> {
2748 Box::new(SimdVec4 { data: [x, y, z, w] })
2749 }
2750
2751 #[inline(always)]
2752 fn splat(v: f64) -> Box<Self> {
2753 Box::new(SimdVec4 { data: [v, v, v, v] })
2754 }
2755 }
2756
2757 #[no_mangle]
2759 pub extern "C" fn sigil_simd_new(x: i64, y: i64, z: i64, w: i64) -> i64 {
2760 let v = SimdVec4::new(
2761 f64::from_bits(x as u64),
2762 f64::from_bits(y as u64),
2763 f64::from_bits(z as u64),
2764 f64::from_bits(w as u64),
2765 );
2766 Box::into_raw(v) as i64
2767 }
2768
2769 #[no_mangle]
2771 pub extern "C" fn sigil_simd_splat(v: i64) -> i64 {
2772 let f = f64::from_bits(v as u64);
2773 let v = SimdVec4::splat(f);
2774 Box::into_raw(v) as i64
2775 }
2776
2777 #[no_mangle]
2782 #[inline(never)]
2783 pub extern "C" fn sigil_simd_add(a: i64, b: i64) -> i64 {
2784 unsafe {
2785 let a = &*(a as *const SimdVec4);
2786 let b = &*(b as *const SimdVec4);
2787 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2789 r.data[0] = a.data[0] + b.data[0];
2790 r.data[1] = a.data[1] + b.data[1];
2791 r.data[2] = a.data[2] + b.data[2];
2792 r.data[3] = a.data[3] + b.data[3];
2793 Box::into_raw(r) as i64
2794 }
2795 }
2796
2797 #[no_mangle]
2799 #[inline(never)]
2800 pub extern "C" fn sigil_simd_sub(a: i64, b: i64) -> i64 {
2801 unsafe {
2802 let a = &*(a as *const SimdVec4);
2803 let b = &*(b as *const SimdVec4);
2804 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2805 r.data[0] = a.data[0] - b.data[0];
2806 r.data[1] = a.data[1] - b.data[1];
2807 r.data[2] = a.data[2] - b.data[2];
2808 r.data[3] = a.data[3] - b.data[3];
2809 Box::into_raw(r) as i64
2810 }
2811 }
2812
2813 #[no_mangle]
2815 #[inline(never)]
2816 pub extern "C" fn sigil_simd_mul(a: i64, b: i64) -> i64 {
2817 unsafe {
2818 let a = &*(a as *const SimdVec4);
2819 let b = &*(b as *const SimdVec4);
2820 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2821 r.data[0] = a.data[0] * b.data[0];
2822 r.data[1] = a.data[1] * b.data[1];
2823 r.data[2] = a.data[2] * b.data[2];
2824 r.data[3] = a.data[3] * b.data[3];
2825 Box::into_raw(r) as i64
2826 }
2827 }
2828
2829 #[no_mangle]
2831 #[inline(never)]
2832 pub extern "C" fn sigil_simd_div(a: i64, b: i64) -> i64 {
2833 unsafe {
2834 let a = &*(a as *const SimdVec4);
2835 let b = &*(b as *const SimdVec4);
2836 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2837 r.data[0] = a.data[0] / b.data[0];
2838 r.data[1] = a.data[1] / b.data[1];
2839 r.data[2] = a.data[2] / b.data[2];
2840 r.data[3] = a.data[3] / b.data[3];
2841 Box::into_raw(r) as i64
2842 }
2843 }
2844
2845 #[no_mangle]
2847 #[inline(never)]
2848 pub extern "C" fn sigil_simd_dot(a: i64, b: i64) -> i64 {
2849 unsafe {
2850 let a = &*(a as *const SimdVec4);
2851 let b = &*(b as *const SimdVec4);
2852 let r = a.data[0].mul_add(
2854 b.data[0],
2855 a.data[1].mul_add(
2856 b.data[1],
2857 a.data[2].mul_add(b.data[2], a.data[3] * b.data[3]),
2858 ),
2859 );
2860 r.to_bits() as i64
2861 }
2862 }
2863
2864 #[no_mangle]
2866 #[inline(never)]
2867 pub extern "C" fn sigil_simd_hadd(a: i64) -> i64 {
2868 unsafe {
2869 let a = &*(a as *const SimdVec4);
2870 let sum01 = a.data[0] + a.data[1];
2872 let sum23 = a.data[2] + a.data[3];
2873 let r = sum01 + sum23;
2874 r.to_bits() as i64
2875 }
2876 }
2877
2878 #[no_mangle]
2880 #[inline(never)]
2881 pub extern "C" fn sigil_simd_length_sq(a: i64) -> i64 {
2882 unsafe {
2883 let a = &*(a as *const SimdVec4);
2884 let r = a.data[0].mul_add(
2885 a.data[0],
2886 a.data[1].mul_add(
2887 a.data[1],
2888 a.data[2].mul_add(a.data[2], a.data[3] * a.data[3]),
2889 ),
2890 );
2891 r.to_bits() as i64
2892 }
2893 }
2894
2895 #[no_mangle]
2897 #[inline(never)]
2898 pub extern "C" fn sigil_simd_length(a: i64) -> i64 {
2899 unsafe {
2900 let a = &*(a as *const SimdVec4);
2901 let len_sq = a.data[0].mul_add(
2902 a.data[0],
2903 a.data[1].mul_add(
2904 a.data[1],
2905 a.data[2].mul_add(a.data[2], a.data[3] * a.data[3]),
2906 ),
2907 );
2908 let r = len_sq.sqrt();
2909 r.to_bits() as i64
2910 }
2911 }
2912
2913 #[no_mangle]
2915 #[inline(never)]
2916 pub extern "C" fn sigil_simd_normalize(a: i64) -> i64 {
2917 unsafe {
2918 let a = &*(a as *const SimdVec4);
2919 let len_sq = a.data[0].mul_add(
2920 a.data[0],
2921 a.data[1].mul_add(
2922 a.data[1],
2923 a.data[2].mul_add(a.data[2], a.data[3] * a.data[3]),
2924 ),
2925 );
2926 let inv = if len_sq > 1e-20 {
2927 1.0 / len_sq.sqrt()
2928 } else {
2929 0.0
2930 };
2931 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2932 r.data[0] = a.data[0] * inv;
2933 r.data[1] = a.data[1] * inv;
2934 r.data[2] = a.data[2] * inv;
2935 r.data[3] = a.data[3] * inv;
2936 Box::into_raw(r) as i64
2937 }
2938 }
2939
2940 #[no_mangle]
2942 #[inline(never)]
2943 pub extern "C" fn sigil_simd_cross(a: i64, b: i64) -> i64 {
2944 unsafe {
2945 let a = &*(a as *const SimdVec4);
2946 let b = &*(b as *const SimdVec4);
2947 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2949 r.data[0] = a.data[1].mul_add(b.data[2], -(a.data[2] * b.data[1]));
2950 r.data[1] = a.data[2].mul_add(b.data[0], -(a.data[0] * b.data[2]));
2951 r.data[2] = a.data[0].mul_add(b.data[1], -(a.data[1] * b.data[0]));
2952 r.data[3] = 0.0;
2953 Box::into_raw(r) as i64
2954 }
2955 }
2956
2957 #[no_mangle]
2959 #[inline(never)]
2960 pub extern "C" fn sigil_simd_min(a: i64, b: i64) -> i64 {
2961 unsafe {
2962 let a = &*(a as *const SimdVec4);
2963 let b = &*(b as *const SimdVec4);
2964 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2965 r.data[0] = a.data[0].min(b.data[0]);
2966 r.data[1] = a.data[1].min(b.data[1]);
2967 r.data[2] = a.data[2].min(b.data[2]);
2968 r.data[3] = a.data[3].min(b.data[3]);
2969 Box::into_raw(r) as i64
2970 }
2971 }
2972
2973 #[no_mangle]
2975 #[inline(never)]
2976 pub extern "C" fn sigil_simd_max(a: i64, b: i64) -> i64 {
2977 unsafe {
2978 let a = &*(a as *const SimdVec4);
2979 let b = &*(b as *const SimdVec4);
2980 let mut r = SimdVec4::new(0.0, 0.0, 0.0, 0.0);
2981 r.data[0] = a.data[0].max(b.data[0]);
2982 r.data[1] = a.data[1].max(b.data[1]);
2983 r.data[2] = a.data[2].max(b.data[2]);
2984 r.data[3] = a.data[3].max(b.data[3]);
2985 Box::into_raw(r) as i64
2986 }
2987 }
2988
2989 #[no_mangle]
2991 pub extern "C" fn sigil_simd_extract(v: i64, idx: i64) -> i64 {
2992 unsafe {
2993 let v = &*(v as *const SimdVec4);
2994 let r = v.data[(idx as usize) & 3];
2995 r.to_bits() as i64
2996 }
2997 }
2998
2999 #[no_mangle]
3001 pub extern "C" fn sigil_simd_free(v: i64) {
3002 if v != 0 {
3003 unsafe {
3004 let _ = Box::from_raw(v as *mut SimdVec4);
3005 }
3006 }
3007 }
3008
3009 #[no_mangle]
3010 pub extern "C" fn sigil_sqrt(x: f64) -> f64 {
3011 x.sqrt()
3012 }
3013
3014 #[no_mangle]
3015 pub extern "C" fn sigil_sin(x: f64) -> f64 {
3016 x.sin()
3017 }
3018
3019 #[no_mangle]
3020 pub extern "C" fn sigil_cos(x: f64) -> f64 {
3021 x.cos()
3022 }
3023
3024 #[no_mangle]
3025 pub extern "C" fn sigil_pow(base: f64, exp: f64) -> f64 {
3026 base.powf(exp)
3027 }
3028
3029 #[no_mangle]
3030 pub extern "C" fn sigil_exp(x: f64) -> f64 {
3031 x.exp()
3032 }
3033
3034 #[no_mangle]
3035 pub extern "C" fn sigil_ln(x: f64) -> f64 {
3036 x.ln()
3037 }
3038
3039 #[no_mangle]
3040 pub extern "C" fn sigil_floor(x: f64) -> f64 {
3041 x.floor()
3042 }
3043
3044 #[no_mangle]
3045 pub extern "C" fn sigil_ceil(x: f64) -> f64 {
3046 x.ceil()
3047 }
3048
3049 #[no_mangle]
3050 pub extern "C" fn sigil_abs(x: f64) -> f64 {
3051 x.abs()
3052 }
3053
3054 #[no_mangle]
3055 pub extern "C" fn sigil_print_int(x: i64) -> i64 {
3056 println!("{}", x);
3057 0
3058 }
3059
3060 #[no_mangle]
3061 pub extern "C" fn sigil_print_float(x: f64) -> i64 {
3062 println!("{}", x);
3063 0
3064 }
3065
3066 #[no_mangle]
3067 pub extern "C" fn sigil_print_str(ptr: *const u8, len: usize) -> i64 {
3068 unsafe {
3069 let slice = std::slice::from_raw_parts(ptr, len);
3070 if let Ok(s) = std::str::from_utf8(slice) {
3071 println!("{}", s);
3072 }
3073 }
3074 0
3075 }
3076
3077 #[no_mangle]
3078 pub extern "C" fn sigil_now() -> i64 {
3079 use std::time::{SystemTime, UNIX_EPOCH};
3080 SystemTime::now()
3081 .duration_since(UNIX_EPOCH)
3082 .map(|d| d.as_millis() as i64)
3083 .unwrap_or(0)
3084 }
3085
3086 #[repr(C)]
3088 struct SigilArray {
3089 data: *mut i64,
3090 len: usize,
3091 cap: usize,
3092 }
3093
3094 #[no_mangle]
3095 pub extern "C" fn sigil_array_new(capacity: i64) -> i64 {
3096 let cap = capacity.max(8) as usize;
3097 let layout = std::alloc::Layout::array::<i64>(cap).unwrap();
3098 let data = unsafe { std::alloc::alloc(layout) as *mut i64 };
3099
3100 let arr = Box::new(SigilArray { data, len: 0, cap });
3101 Box::into_raw(arr) as i64
3102 }
3103
3104 #[no_mangle]
3105 pub extern "C" fn sigil_array_push(arr_ptr: i64, value: i64) -> i64 {
3106 unsafe {
3107 let arr = &mut *(arr_ptr as *mut SigilArray);
3108 if arr.len >= arr.cap {
3109 let new_cap = arr.cap * 2;
3111 let old_layout = std::alloc::Layout::array::<i64>(arr.cap).unwrap();
3112 let new_layout = std::alloc::Layout::array::<i64>(new_cap).unwrap();
3113 arr.data = std::alloc::realloc(arr.data as *mut u8, old_layout, new_layout.size())
3114 as *mut i64;
3115 arr.cap = new_cap;
3116 }
3117 *arr.data.add(arr.len) = value;
3118 arr.len += 1;
3119 }
3120 0
3121 }
3122
3123 #[no_mangle]
3124 pub extern "C" fn sigil_array_get(arr_ptr: i64, index: i64) -> i64 {
3125 unsafe {
3126 let arr = &*(arr_ptr as *const SigilArray);
3127 let idx = index as usize;
3128 if idx < arr.len {
3129 *arr.data.add(idx)
3130 } else {
3131 0 }
3133 }
3134 }
3135
3136 #[no_mangle]
3137 pub extern "C" fn sigil_array_set(arr_ptr: i64, index: i64, value: i64) -> i64 {
3138 unsafe {
3139 let arr = &mut *(arr_ptr as *mut SigilArray);
3140 let idx = index as usize;
3141 while arr.len <= idx {
3143 sigil_array_push(arr_ptr, 0);
3144 }
3145 *arr.data.add(idx) = value;
3146 }
3147 value
3148 }
3149
3150 #[no_mangle]
3151 pub extern "C" fn sigil_array_len(arr_ptr: i64) -> i64 {
3152 unsafe {
3153 let arr = &*(arr_ptr as *const SigilArray);
3154 arr.len as i64
3155 }
3156 }
3157
3158 #[no_mangle]
3165 pub extern "C" fn sigil_array_sum(arr_ptr: i64) -> i64 {
3166 unsafe {
3167 let arr = &*(arr_ptr as *const SigilArray);
3168 let data = std::slice::from_raw_parts(arr.data, arr.len);
3169
3170 let chunks = data.chunks_exact(4);
3172 let remainder = chunks.remainder();
3173
3174 let mut sum0: i64 = 0;
3176 let mut sum1: i64 = 0;
3177 let mut sum2: i64 = 0;
3178 let mut sum3: i64 = 0;
3179
3180 for chunk in chunks {
3181 sum0 = sum0.wrapping_add(chunk[0]);
3182 sum1 = sum1.wrapping_add(chunk[1]);
3183 sum2 = sum2.wrapping_add(chunk[2]);
3184 sum3 = sum3.wrapping_add(chunk[3]);
3185 }
3186
3187 let mut sum = sum0
3189 .wrapping_add(sum1)
3190 .wrapping_add(sum2)
3191 .wrapping_add(sum3);
3192 for &v in remainder {
3193 sum = sum.wrapping_add(v);
3194 }
3195
3196 sum
3197 }
3198 }
3199
3200 #[no_mangle]
3202 pub extern "C" fn sigil_array_scale(arr_ptr: i64, scalar: i64) -> i64 {
3203 unsafe {
3204 let arr = &mut *(arr_ptr as *mut SigilArray);
3205 let data = std::slice::from_raw_parts_mut(arr.data, arr.len);
3206
3207 for chunk in data.chunks_exact_mut(4) {
3209 chunk[0] = chunk[0].wrapping_mul(scalar);
3210 chunk[1] = chunk[1].wrapping_mul(scalar);
3211 chunk[2] = chunk[2].wrapping_mul(scalar);
3212 chunk[3] = chunk[3].wrapping_mul(scalar);
3213 }
3214
3215 let remainder_start = (data.len() / 4) * 4;
3217 for v in &mut data[remainder_start..] {
3218 *v = v.wrapping_mul(scalar);
3219 }
3220
3221 arr_ptr
3222 }
3223 }
3224
3225 #[no_mangle]
3227 pub extern "C" fn sigil_array_offset(arr_ptr: i64, offset: i64) -> i64 {
3228 unsafe {
3229 let arr = &mut *(arr_ptr as *mut SigilArray);
3230 let data = std::slice::from_raw_parts_mut(arr.data, arr.len);
3231
3232 for chunk in data.chunks_exact_mut(4) {
3234 chunk[0] = chunk[0].wrapping_add(offset);
3235 chunk[1] = chunk[1].wrapping_add(offset);
3236 chunk[2] = chunk[2].wrapping_add(offset);
3237 chunk[3] = chunk[3].wrapping_add(offset);
3238 }
3239
3240 let remainder_start = (data.len() / 4) * 4;
3241 for v in &mut data[remainder_start..] {
3242 *v = v.wrapping_add(offset);
3243 }
3244
3245 arr_ptr
3246 }
3247 }
3248
3249 #[no_mangle]
3251 pub extern "C" fn sigil_array_dot(a_ptr: i64, b_ptr: i64) -> i64 {
3252 unsafe {
3253 let a_arr = &*(a_ptr as *const SigilArray);
3254 let b_arr = &*(b_ptr as *const SigilArray);
3255
3256 let len = a_arr.len.min(b_arr.len);
3257 let a_data = std::slice::from_raw_parts(a_arr.data, len);
3258 let b_data = std::slice::from_raw_parts(b_arr.data, len);
3259
3260 let mut sum0: i64 = 0;
3262 let mut sum1: i64 = 0;
3263 let mut sum2: i64 = 0;
3264 let mut sum3: i64 = 0;
3265
3266 let chunks = len / 4;
3267 for i in 0..chunks {
3268 let base = i * 4;
3269 sum0 = sum0.wrapping_add(a_data[base].wrapping_mul(b_data[base]));
3270 sum1 = sum1.wrapping_add(a_data[base + 1].wrapping_mul(b_data[base + 1]));
3271 sum2 = sum2.wrapping_add(a_data[base + 2].wrapping_mul(b_data[base + 2]));
3272 sum3 = sum3.wrapping_add(a_data[base + 3].wrapping_mul(b_data[base + 3]));
3273 }
3274
3275 let mut sum = sum0
3277 .wrapping_add(sum1)
3278 .wrapping_add(sum2)
3279 .wrapping_add(sum3);
3280 for i in (chunks * 4)..len {
3281 sum = sum.wrapping_add(a_data[i].wrapping_mul(b_data[i]));
3282 }
3283
3284 sum
3285 }
3286 }
3287
3288 #[no_mangle]
3290 pub extern "C" fn sigil_array_add(a_ptr: i64, b_ptr: i64) -> i64 {
3291 unsafe {
3292 let a_arr = &*(a_ptr as *const SigilArray);
3293 let b_arr = &*(b_ptr as *const SigilArray);
3294
3295 let len = a_arr.len.min(b_arr.len);
3296 let a_data = std::slice::from_raw_parts(a_arr.data, len);
3297 let b_data = std::slice::from_raw_parts(b_arr.data, len);
3298
3299 let result = sigil_array_new(len as i64);
3301 let r_arr = &mut *(result as *mut SigilArray);
3302 r_arr.len = len;
3303 let r_data = std::slice::from_raw_parts_mut(r_arr.data, len);
3304
3305 for i in 0..(len / 4) {
3307 let base = i * 4;
3308 r_data[base] = a_data[base].wrapping_add(b_data[base]);
3309 r_data[base + 1] = a_data[base + 1].wrapping_add(b_data[base + 1]);
3310 r_data[base + 2] = a_data[base + 2].wrapping_add(b_data[base + 2]);
3311 r_data[base + 3] = a_data[base + 3].wrapping_add(b_data[base + 3]);
3312 }
3313
3314 for i in ((len / 4) * 4)..len {
3316 r_data[i] = a_data[i].wrapping_add(b_data[i]);
3317 }
3318
3319 result
3320 }
3321 }
3322
3323 #[no_mangle]
3325 pub extern "C" fn sigil_array_mul(a_ptr: i64, b_ptr: i64) -> i64 {
3326 unsafe {
3327 let a_arr = &*(a_ptr as *const SigilArray);
3328 let b_arr = &*(b_ptr as *const SigilArray);
3329
3330 let len = a_arr.len.min(b_arr.len);
3331 let a_data = std::slice::from_raw_parts(a_arr.data, len);
3332 let b_data = std::slice::from_raw_parts(b_arr.data, len);
3333
3334 let result = sigil_array_new(len as i64);
3336 let r_arr = &mut *(result as *mut SigilArray);
3337 r_arr.len = len;
3338 let r_data = std::slice::from_raw_parts_mut(r_arr.data, len);
3339
3340 for i in 0..(len / 4) {
3342 let base = i * 4;
3343 r_data[base] = a_data[base].wrapping_mul(b_data[base]);
3344 r_data[base + 1] = a_data[base + 1].wrapping_mul(b_data[base + 1]);
3345 r_data[base + 2] = a_data[base + 2].wrapping_mul(b_data[base + 2]);
3346 r_data[base + 3] = a_data[base + 3].wrapping_mul(b_data[base + 3]);
3347 }
3348
3349 for i in ((len / 4) * 4)..len {
3351 r_data[i] = a_data[i].wrapping_mul(b_data[i]);
3352 }
3353
3354 result
3355 }
3356 }
3357
3358 #[no_mangle]
3360 pub extern "C" fn sigil_array_min(arr_ptr: i64) -> i64 {
3361 unsafe {
3362 let arr = &*(arr_ptr as *const SigilArray);
3363 if arr.len == 0 {
3364 return 0;
3365 }
3366
3367 let data = std::slice::from_raw_parts(arr.data, arr.len);
3368
3369 let mut min0 = i64::MAX;
3371 let mut min1 = i64::MAX;
3372 let mut min2 = i64::MAX;
3373 let mut min3 = i64::MAX;
3374
3375 for chunk in data.chunks_exact(4) {
3376 min0 = min0.min(chunk[0]);
3377 min1 = min1.min(chunk[1]);
3378 min2 = min2.min(chunk[2]);
3379 min3 = min3.min(chunk[3]);
3380 }
3381
3382 let mut min_val = min0.min(min1).min(min2).min(min3);
3383
3384 let remainder_start = (data.len() / 4) * 4;
3386 for &v in &data[remainder_start..] {
3387 min_val = min_val.min(v);
3388 }
3389
3390 min_val
3391 }
3392 }
3393
3394 #[no_mangle]
3396 pub extern "C" fn sigil_array_max(arr_ptr: i64) -> i64 {
3397 unsafe {
3398 let arr = &*(arr_ptr as *const SigilArray);
3399 if arr.len == 0 {
3400 return 0;
3401 }
3402
3403 let data = std::slice::from_raw_parts(arr.data, arr.len);
3404
3405 let mut max0 = i64::MIN;
3407 let mut max1 = i64::MIN;
3408 let mut max2 = i64::MIN;
3409 let mut max3 = i64::MIN;
3410
3411 for chunk in data.chunks_exact(4) {
3412 max0 = max0.max(chunk[0]);
3413 max1 = max1.max(chunk[1]);
3414 max2 = max2.max(chunk[2]);
3415 max3 = max3.max(chunk[3]);
3416 }
3417
3418 let mut max_val = max0.max(max1).max(max2).max(max3);
3419
3420 let remainder_start = (data.len() / 4) * 4;
3422 for &v in &data[remainder_start..] {
3423 max_val = max_val.max(v);
3424 }
3425
3426 max_val
3427 }
3428 }
3429
3430 #[no_mangle]
3432 pub extern "C" fn sigil_array_fill(arr_ptr: i64, value: i64, count: i64) -> i64 {
3433 unsafe {
3434 let arr = &mut *(arr_ptr as *mut SigilArray);
3435 let n = count as usize;
3436
3437 while arr.len < n {
3439 sigil_array_push(arr_ptr, 0);
3440 }
3441
3442 let data = std::slice::from_raw_parts_mut(arr.data, n);
3443
3444 for chunk in data.chunks_exact_mut(4) {
3446 chunk[0] = value;
3447 chunk[1] = value;
3448 chunk[2] = value;
3449 chunk[3] = value;
3450 }
3451
3452 let remainder_start = (n / 4) * 4;
3454 for v in &mut data[remainder_start..] {
3455 *v = value;
3456 }
3457
3458 arr_ptr
3459 }
3460 }
3461
3462 #[no_mangle]
3469 pub extern "C" fn sigil_array_first(arr_ptr: i64) -> i64 {
3470 unsafe {
3471 let arr = &*(arr_ptr as *const SigilArray);
3472 if arr.len == 0 {
3473 return 0; }
3475 *arr.data
3476 }
3477 }
3478
3479 #[no_mangle]
3481 pub extern "C" fn sigil_array_last(arr_ptr: i64) -> i64 {
3482 unsafe {
3483 let arr = &*(arr_ptr as *const SigilArray);
3484 if arr.len == 0 {
3485 return 0; }
3487 *arr.data.add(arr.len - 1)
3488 }
3489 }
3490
3491 #[no_mangle]
3493 pub extern "C" fn sigil_array_middle(arr_ptr: i64) -> i64 {
3494 unsafe {
3495 let arr = &*(arr_ptr as *const SigilArray);
3496 if arr.len == 0 {
3497 return 0; }
3499 let mid = arr.len / 2;
3500 *arr.data.add(mid)
3501 }
3502 }
3503
3504 #[no_mangle]
3506 pub extern "C" fn sigil_array_choice(arr_ptr: i64) -> i64 {
3507 unsafe {
3508 let arr = &*(arr_ptr as *const SigilArray);
3509 if arr.len == 0 {
3510 return 0; }
3512 use std::time::{SystemTime, UNIX_EPOCH};
3514 let seed = SystemTime::now()
3515 .duration_since(UNIX_EPOCH)
3516 .map(|d| d.as_nanos() as u64)
3517 .unwrap_or(12345);
3518 let idx =
3519 ((seed.wrapping_mul(1103515245).wrapping_add(12345)) >> 16) as usize % arr.len;
3520 *arr.data.add(idx)
3521 }
3522 }
3523
3524 #[no_mangle]
3526 pub extern "C" fn sigil_array_nth(arr_ptr: i64, index: i64) -> i64 {
3527 sigil_array_get(arr_ptr, index)
3528 }
3529
3530 #[no_mangle]
3532 pub extern "C" fn sigil_array_next(arr_ptr: i64) -> i64 {
3533 sigil_array_first(arr_ptr)
3536 }
3537
3538 #[no_mangle]
3540 pub extern "C" fn sigil_array_product(arr_ptr: i64) -> i64 {
3541 unsafe {
3542 let arr = &*(arr_ptr as *const SigilArray);
3543 if arr.len == 0 {
3544 return 1; }
3546 let mut product: i64 = 1;
3547 for i in 0..arr.len {
3548 product = product.wrapping_mul(*arr.data.add(i));
3549 }
3550 product
3551 }
3552 }
3553
3554 #[no_mangle]
3556 pub extern "C" fn sigil_array_sort(arr_ptr: i64) -> i64 {
3557 unsafe {
3558 let arr = &*(arr_ptr as *const SigilArray);
3559 if arr.len == 0 {
3560 return sigil_array_new(0);
3561 }
3562
3563 let mut elements: Vec<i64> = Vec::with_capacity(arr.len);
3565 for i in 0..arr.len {
3566 elements.push(*arr.data.add(i));
3567 }
3568
3569 elements.sort();
3571
3572 let new_arr = sigil_array_new(arr.len as i64);
3574 for elem in elements {
3575 sigil_array_push(new_arr, elem);
3576 }
3577 new_arr
3578 }
3579 }
3580
3581 #[no_mangle]
3595 pub extern "C" fn sigil_parallel_map(arr_ptr: i64) -> i64 {
3596 arr_ptr
3599 }
3600
3601 #[no_mangle]
3604 pub extern "C" fn sigil_parallel_filter(arr_ptr: i64) -> i64 {
3605 arr_ptr
3611 }
3612
3613 #[no_mangle]
3616 pub extern "C" fn sigil_parallel_reduce(arr_ptr: i64) -> i64 {
3617 unsafe {
3620 let arr = &*(arr_ptr as *const SigilArray);
3621 if arr.len == 0 {
3622 return 0;
3623 }
3624
3625 let mut sum: i64 = 0;
3628 for i in 0..arr.len {
3629 sum += *arr.data.add(i);
3630 }
3631 sum
3632 }
3633 }
3634
3635 #[no_mangle]
3651 pub extern "C" fn sigil_gpu_map(arr_ptr: i64) -> i64 {
3652 arr_ptr
3659 }
3660
3661 #[no_mangle]
3664 pub extern "C" fn sigil_gpu_filter(arr_ptr: i64) -> i64 {
3665 arr_ptr
3668 }
3669
3670 #[no_mangle]
3673 pub extern "C" fn sigil_gpu_reduce(arr_ptr: i64) -> i64 {
3674 sigil_parallel_reduce(arr_ptr)
3676 }
3677
3678 #[repr(C)]
3685 struct MemoEntry {
3686 key1: i64, key2: i64, value: i64, occupied: bool, }
3691
3692 #[repr(C)]
3694 struct MemoCache {
3695 entries: *mut MemoEntry,
3696 capacity: usize,
3697 mask: usize, }
3699
3700 #[no_mangle]
3702 pub extern "C" fn sigil_memo_new(capacity: i64) -> i64 {
3703 let cap = (capacity as usize).next_power_of_two().max(1024);
3704 let layout = std::alloc::Layout::array::<MemoEntry>(cap).unwrap();
3705 let entries = unsafe {
3706 let ptr = std::alloc::alloc_zeroed(layout) as *mut MemoEntry;
3707 ptr
3708 };
3709
3710 let cache = Box::new(MemoCache {
3711 entries,
3712 capacity: cap,
3713 mask: cap - 1,
3714 });
3715 Box::into_raw(cache) as i64
3716 }
3717
3718 #[inline]
3720 fn memo_hash_1(key: i64) -> usize {
3721 let mut h = key as u64;
3723 h = h.wrapping_mul(0x517cc1b727220a95);
3724 h ^= h >> 32;
3725 h as usize
3726 }
3727
3728 #[inline]
3730 fn memo_hash_2(key1: i64, key2: i64) -> usize {
3731 let mut h = key1 as u64;
3732 h = h.wrapping_mul(0x517cc1b727220a95);
3733 h ^= key2 as u64;
3734 h = h.wrapping_mul(0x517cc1b727220a95);
3735 h ^= h >> 32;
3736 h as usize
3737 }
3738
3739 #[no_mangle]
3747 pub extern "C" fn sigil_ackermann(m: i64, n: i64) -> i64 {
3748 let mut stack: Vec<i64> = Vec::with_capacity(1024);
3750 stack.push(m);
3751 let mut n = n;
3752
3753 while let Some(m) = stack.pop() {
3754 if m == 0 {
3755 n = n + 1;
3756 } else if n == 0 {
3757 stack.push(m - 1);
3758 n = 1;
3759 } else {
3760 stack.push(m - 1);
3761 stack.push(m);
3762 n = n - 1;
3763 }
3764 }
3765 n
3766 }
3767
3768 #[no_mangle]
3770 pub extern "C" fn sigil_tak(x: i64, y: i64, z: i64) -> i64 {
3771 #[derive(Clone, Copy)]
3773 enum TakCont {
3774 Eval { x: i64, y: i64, z: i64 },
3775 Cont1 { y: i64, z: i64, x: i64 }, Cont2 { z: i64, x: i64, y: i64, r1: i64 }, Cont3 { r1: i64, r2: i64 }, }
3779
3780 let mut stack: Vec<TakCont> = Vec::with_capacity(256);
3781 stack.push(TakCont::Eval { x, y, z });
3782 let mut result: i64 = 0;
3783
3784 while let Some(cont) = stack.pop() {
3785 match cont {
3786 TakCont::Eval { x, y, z } => {
3787 if y >= x {
3788 result = z;
3789 } else {
3790 stack.push(TakCont::Cont1 { y, z, x });
3792 stack.push(TakCont::Eval { x: x - 1, y, z });
3793 }
3794 }
3795 TakCont::Cont1 { y, z, x } => {
3796 let r1 = result;
3797 stack.push(TakCont::Cont2 { z, x, y, r1 });
3798 stack.push(TakCont::Eval {
3799 x: y - 1,
3800 y: z,
3801 z: x,
3802 });
3803 }
3804 TakCont::Cont2 { z, x, y, r1 } => {
3805 let r2 = result;
3806 stack.push(TakCont::Cont3 { r1, r2 });
3807 stack.push(TakCont::Eval {
3808 x: z - 1,
3809 y: x,
3810 z: y,
3811 });
3812 }
3813 TakCont::Cont3 { r1, r2 } => {
3814 let r3 = result;
3815 stack.push(TakCont::Eval {
3817 x: r1,
3818 y: r2,
3819 z: r3,
3820 });
3821 }
3822 }
3823 }
3824 result
3825 }
3826
3827 const MEMO_NOT_FOUND: i64 = -9223372036854775807;
3830
3831 #[no_mangle]
3834 pub extern "C" fn sigil_memo_get_1(cache_ptr: i64, key: i64) -> i64 {
3835 unsafe {
3836 let cache = &*(cache_ptr as *const MemoCache);
3837 let mut idx = memo_hash_1(key) & cache.mask;
3838
3839 for _ in 0..32 {
3841 let entry = &*cache.entries.add(idx);
3842 if !entry.occupied {
3843 return MEMO_NOT_FOUND;
3844 }
3845 if entry.key1 == key {
3846 return entry.value;
3847 }
3848 idx = (idx + 1) & cache.mask;
3849 }
3850 MEMO_NOT_FOUND
3851 }
3852 }
3853
3854 #[no_mangle]
3856 pub extern "C" fn sigil_memo_set_1(cache_ptr: i64, key: i64, value: i64) {
3857 unsafe {
3858 let cache = &*(cache_ptr as *const MemoCache);
3859 let mut idx = memo_hash_1(key) & cache.mask;
3860
3861 for _ in 0..32 {
3863 let entry = &mut *cache.entries.add(idx);
3864 if !entry.occupied || entry.key1 == key {
3865 entry.key1 = key;
3866 entry.value = value;
3867 entry.occupied = true;
3868 return;
3869 }
3870 idx = (idx + 1) & cache.mask;
3871 }
3872 let entry = &mut *cache.entries.add(memo_hash_1(key) & cache.mask);
3874 entry.key1 = key;
3875 entry.value = value;
3876 entry.occupied = true;
3877 }
3878 }
3879
3880 #[no_mangle]
3882 pub extern "C" fn sigil_memo_get_2(cache_ptr: i64, key1: i64, key2: i64) -> i64 {
3883 unsafe {
3884 let cache = &*(cache_ptr as *const MemoCache);
3885 let mut idx = memo_hash_2(key1, key2) & cache.mask;
3886
3887 for _ in 0..32 {
3888 let entry = &*cache.entries.add(idx);
3889 if !entry.occupied {
3890 return MEMO_NOT_FOUND;
3891 }
3892 if entry.key1 == key1 && entry.key2 == key2 {
3893 return entry.value;
3894 }
3895 idx = (idx + 1) & cache.mask;
3896 }
3897 MEMO_NOT_FOUND
3898 }
3899 }
3900
3901 #[no_mangle]
3903 pub extern "C" fn sigil_memo_set_2(cache_ptr: i64, key1: i64, key2: i64, value: i64) {
3904 unsafe {
3905 let cache = &*(cache_ptr as *const MemoCache);
3906 let mut idx = memo_hash_2(key1, key2) & cache.mask;
3907
3908 for _ in 0..32 {
3909 let entry = &mut *cache.entries.add(idx);
3910 if !entry.occupied || (entry.key1 == key1 && entry.key2 == key2) {
3911 entry.key1 = key1;
3912 entry.key2 = key2;
3913 entry.value = value;
3914 entry.occupied = true;
3915 return;
3916 }
3917 idx = (idx + 1) & cache.mask;
3918 }
3919 let entry = &mut *cache.entries.add(memo_hash_2(key1, key2) & cache.mask);
3920 entry.key1 = key1;
3921 entry.key2 = key2;
3922 entry.value = value;
3923 entry.occupied = true;
3924 }
3925 }
3926
3927 #[no_mangle]
3929 pub extern "C" fn sigil_memo_free(cache_ptr: i64) {
3930 if cache_ptr != 0 {
3931 unsafe {
3932 let cache = Box::from_raw(cache_ptr as *mut MemoCache);
3933 let layout = std::alloc::Layout::array::<MemoEntry>(cache.capacity).unwrap();
3934 std::alloc::dealloc(cache.entries as *mut u8, layout);
3935 }
3936 }
3937 }
3938
3939 #[cfg(test)]
3944 mod tests {
3945 use super::*;
3946 use crate::parser::Parser;
3947
3948 #[test]
3949 fn test_extern_block_parsing_and_declaration() {
3950 let source = r#"
3951 extern "C" {
3952 fn abs(x: c_int) -> c_int;
3953 fn strlen(s: *const c_char) -> usize;
3954 }
3955
3956 fn main() -> i64 {
3957 42
3958 }
3959 "#;
3960
3961 let mut compiler = JitCompiler::new().unwrap();
3962 let result = compiler.compile(source);
3963 assert!(
3964 result.is_ok(),
3965 "Failed to compile FFI declarations: {:?}",
3966 result
3967 );
3968
3969 assert!(
3971 compiler.extern_functions.contains_key("abs"),
3972 "abs not declared"
3973 );
3974 assert!(
3975 compiler.extern_functions.contains_key("strlen"),
3976 "strlen not declared"
3977 );
3978
3979 let abs_sig = compiler.extern_functions.get("abs").unwrap();
3981 assert_eq!(abs_sig.params.len(), 1);
3982 assert_eq!(abs_sig.params[0], types::I32); assert_eq!(abs_sig.returns, Some(types::I32));
3984
3985 let strlen_sig = compiler.extern_functions.get("strlen").unwrap();
3987 assert_eq!(strlen_sig.params.len(), 1);
3988 assert_eq!(strlen_sig.params[0], types::I64); assert_eq!(strlen_sig.returns, Some(types::I64)); }
3991
3992 #[test]
3993 fn test_extern_variadic_function() {
3994 let source = r#"
3995 extern "C" {
3996 fn printf(fmt: *const c_char, ...) -> c_int;
3997 }
3998
3999 fn main() -> i64 {
4000 0
4001 }
4002 "#;
4003
4004 let mut compiler = JitCompiler::new().unwrap();
4005 let result = compiler.compile(source);
4006 assert!(
4007 result.is_ok(),
4008 "Failed to compile variadic FFI: {:?}",
4009 result
4010 );
4011
4012 let printf_sig = compiler.extern_functions.get("printf").unwrap();
4013 assert!(printf_sig.variadic, "printf should be variadic");
4014 }
4015
4016 #[test]
4017 fn test_extern_c_abi_only() {
4018 let source = r#"
4019 extern "Rust" {
4020 fn some_func(x: i32) -> i32;
4021 }
4022
4023 fn main() -> i64 {
4024 0
4025 }
4026 "#;
4027
4028 let mut compiler = JitCompiler::new().unwrap();
4029 let result = compiler.compile(source);
4030 assert!(result.is_err(), "Should reject non-C ABI");
4031 assert!(result.unwrap_err().contains("Unsupported ABI"));
4032 }
4033
4034 #[test]
4035 fn test_c_type_mapping() {
4036 let test_cases = vec![
4038 ("c_char", types::I8),
4039 ("c_int", types::I32),
4040 ("c_long", types::I64),
4041 ("c_float", types::F32),
4042 ("c_double", types::F64),
4043 ("size_t", types::I64),
4044 ("i32", types::I32),
4045 ("f64", types::F64),
4046 ];
4047
4048 for (type_name, expected_cl_type) in test_cases {
4049 let source = format!(
4050 r#"
4051 extern "C" {{
4052 fn test_func(x: {}) -> {};
4053 }}
4054
4055 fn main() -> i64 {{ 0 }}
4056 "#,
4057 type_name, type_name
4058 );
4059
4060 let mut compiler = JitCompiler::new().unwrap();
4061 let result = compiler.compile(&source);
4062 assert!(
4063 result.is_ok(),
4064 "Failed for type {}: {:?}",
4065 type_name,
4066 result
4067 );
4068
4069 let sig = compiler.extern_functions.get("test_func").unwrap();
4070 assert_eq!(
4071 sig.params[0], expected_cl_type,
4072 "Wrong param type for {}",
4073 type_name
4074 );
4075 assert_eq!(
4076 sig.returns,
4077 Some(expected_cl_type),
4078 "Wrong return type for {}",
4079 type_name
4080 );
4081 }
4082 }
4083 }
4084}
4085
4086#[cfg(feature = "jit")]
4088pub use jit::JitCompiler;