seqc/codegen/
runtime.rs

1//! Runtime function declarations for LLVM IR.
2//!
3//! All runtime functions are declared here in a single data-driven table.
4//! This eliminates ~500 lines of duplicate writeln! calls and ensures
5//! consistency between the FFI and non-FFI code paths.
6
7use super::error::CodeGenError;
8use std::collections::HashMap;
9use std::fmt::Write as _;
10use std::sync::LazyLock;
11
12/// A runtime function declaration for LLVM IR.
13pub struct RuntimeDecl {
14    /// LLVM declaration string (e.g., "declare ptr @patch_seq_add(ptr)")
15    pub decl: &'static str,
16    /// Optional category comment (e.g., "; Stack operations")
17    pub category: Option<&'static str>,
18}
19
20/// All runtime function declarations, organized by category.
21/// Each entry generates a single `declare` statement in the LLVM IR.
22pub static RUNTIME_DECLARATIONS: LazyLock<Vec<RuntimeDecl>> = LazyLock::new(|| {
23    vec![
24        // Core push operations
25        RuntimeDecl {
26            decl: "declare ptr @patch_seq_push_int(ptr, i64)",
27            category: Some("; Runtime function declarations"),
28        },
29        RuntimeDecl {
30            decl: "declare ptr @patch_seq_push_string(ptr, ptr)",
31            category: None,
32        },
33        RuntimeDecl {
34            decl: "declare ptr @patch_seq_push_symbol(ptr, ptr)",
35            category: None,
36        },
37        RuntimeDecl {
38            decl: "declare ptr @patch_seq_push_interned_symbol(ptr, ptr)",
39            category: None,
40        },
41        // I/O operations
42        RuntimeDecl {
43            decl: "declare ptr @patch_seq_write(ptr)",
44            category: None,
45        },
46        RuntimeDecl {
47            decl: "declare ptr @patch_seq_write_line(ptr)",
48            category: None,
49        },
50        RuntimeDecl {
51            decl: "declare ptr @patch_seq_read_line(ptr)",
52            category: None,
53        },
54        RuntimeDecl {
55            decl: "declare ptr @patch_seq_read_line_plus(ptr)",
56            category: None,
57        },
58        RuntimeDecl {
59            decl: "declare ptr @patch_seq_read_n(ptr)",
60            category: None,
61        },
62        // Type conversions
63        RuntimeDecl {
64            decl: "declare ptr @patch_seq_int_to_string(ptr)",
65            category: None,
66        },
67        RuntimeDecl {
68            decl: "declare ptr @patch_seq_symbol_to_string(ptr)",
69            category: None,
70        },
71        RuntimeDecl {
72            decl: "declare ptr @patch_seq_string_to_symbol(ptr)",
73            category: None,
74        },
75        // Integer arithmetic
76        RuntimeDecl {
77            decl: "declare ptr @patch_seq_add(ptr)",
78            category: None,
79        },
80        RuntimeDecl {
81            decl: "declare ptr @patch_seq_subtract(ptr)",
82            category: None,
83        },
84        RuntimeDecl {
85            decl: "declare ptr @patch_seq_multiply(ptr)",
86            category: None,
87        },
88        RuntimeDecl {
89            decl: "declare ptr @patch_seq_divide(ptr)",
90            category: None,
91        },
92        RuntimeDecl {
93            decl: "declare ptr @patch_seq_modulo(ptr)",
94            category: None,
95        },
96        // Integer comparisons
97        RuntimeDecl {
98            decl: "declare ptr @patch_seq_eq(ptr)",
99            category: None,
100        },
101        RuntimeDecl {
102            decl: "declare ptr @patch_seq_lt(ptr)",
103            category: None,
104        },
105        RuntimeDecl {
106            decl: "declare ptr @patch_seq_gt(ptr)",
107            category: None,
108        },
109        RuntimeDecl {
110            decl: "declare ptr @patch_seq_lte(ptr)",
111            category: None,
112        },
113        RuntimeDecl {
114            decl: "declare ptr @patch_seq_gte(ptr)",
115            category: None,
116        },
117        RuntimeDecl {
118            decl: "declare ptr @patch_seq_neq(ptr)",
119            category: None,
120        },
121        // Boolean operations
122        RuntimeDecl {
123            decl: "declare ptr @patch_seq_and(ptr)",
124            category: Some("; Boolean operations"),
125        },
126        RuntimeDecl {
127            decl: "declare ptr @patch_seq_or(ptr)",
128            category: None,
129        },
130        RuntimeDecl {
131            decl: "declare ptr @patch_seq_not(ptr)",
132            category: None,
133        },
134        // Bitwise operations
135        RuntimeDecl {
136            decl: "declare ptr @patch_seq_band(ptr)",
137            category: Some("; Bitwise operations"),
138        },
139        RuntimeDecl {
140            decl: "declare ptr @patch_seq_bor(ptr)",
141            category: None,
142        },
143        RuntimeDecl {
144            decl: "declare ptr @patch_seq_bxor(ptr)",
145            category: None,
146        },
147        RuntimeDecl {
148            decl: "declare ptr @patch_seq_bnot(ptr)",
149            category: None,
150        },
151        RuntimeDecl {
152            decl: "declare ptr @patch_seq_shl(ptr)",
153            category: None,
154        },
155        RuntimeDecl {
156            decl: "declare ptr @patch_seq_shr(ptr)",
157            category: None,
158        },
159        RuntimeDecl {
160            decl: "declare ptr @patch_seq_popcount(ptr)",
161            category: None,
162        },
163        RuntimeDecl {
164            decl: "declare ptr @patch_seq_clz(ptr)",
165            category: None,
166        },
167        RuntimeDecl {
168            decl: "declare ptr @patch_seq_ctz(ptr)",
169            category: None,
170        },
171        RuntimeDecl {
172            decl: "declare ptr @patch_seq_int_bits(ptr)",
173            category: None,
174        },
175        // LLVM intrinsics
176        RuntimeDecl {
177            decl: "declare i64 @llvm.ctpop.i64(i64)",
178            category: None,
179        },
180        RuntimeDecl {
181            decl: "declare i64 @llvm.ctlz.i64(i64, i1)",
182            category: None,
183        },
184        RuntimeDecl {
185            decl: "declare i64 @llvm.cttz.i64(i64, i1)",
186            category: None,
187        },
188        RuntimeDecl {
189            decl: "declare void @llvm.memmove.p0.p0.i64(ptr, ptr, i64, i1)",
190            category: None,
191        },
192        RuntimeDecl {
193            decl: "declare void @llvm.trap() noreturn nounwind",
194            category: None,
195        },
196        // Stack operations
197        RuntimeDecl {
198            decl: "declare ptr @patch_seq_dup(ptr)",
199            category: Some("; Stack operations"),
200        },
201        RuntimeDecl {
202            decl: "declare ptr @patch_seq_drop_op(ptr)",
203            category: None,
204        },
205        RuntimeDecl {
206            decl: "declare ptr @patch_seq_swap(ptr)",
207            category: None,
208        },
209        RuntimeDecl {
210            decl: "declare ptr @patch_seq_over(ptr)",
211            category: None,
212        },
213        RuntimeDecl {
214            decl: "declare ptr @patch_seq_rot(ptr)",
215            category: None,
216        },
217        RuntimeDecl {
218            decl: "declare ptr @patch_seq_nip(ptr)",
219            category: None,
220        },
221        RuntimeDecl {
222            decl: "declare void @patch_seq_clone_value(ptr, ptr)",
223            category: None,
224        },
225        RuntimeDecl {
226            decl: "declare ptr @patch_seq_tuck(ptr)",
227            category: None,
228        },
229        RuntimeDecl {
230            decl: "declare ptr @patch_seq_2dup(ptr)",
231            category: None,
232        },
233        RuntimeDecl {
234            decl: "declare ptr @patch_seq_3drop(ptr)",
235            category: None,
236        },
237        RuntimeDecl {
238            decl: "declare ptr @patch_seq_pick_op(ptr)",
239            category: None,
240        },
241        RuntimeDecl {
242            decl: "declare ptr @patch_seq_roll(ptr)",
243            category: None,
244        },
245        RuntimeDecl {
246            decl: "declare ptr @patch_seq_push_value(ptr, %Value)",
247            category: None,
248        },
249        // Quotation operations
250        RuntimeDecl {
251            decl: "declare ptr @patch_seq_push_quotation(ptr, i64, i64)",
252            category: Some("; Quotation operations"),
253        },
254        RuntimeDecl {
255            decl: "declare ptr @patch_seq_call(ptr)",
256            category: None,
257        },
258        RuntimeDecl {
259            decl: "declare i64 @patch_seq_peek_is_quotation(ptr)",
260            category: None,
261        },
262        RuntimeDecl {
263            decl: "declare i64 @patch_seq_peek_quotation_fn_ptr(ptr)",
264            category: None,
265        },
266        RuntimeDecl {
267            decl: "declare ptr @patch_seq_spawn(ptr)",
268            category: None,
269        },
270        RuntimeDecl {
271            decl: "declare ptr @patch_seq_weave(ptr)",
272            category: None,
273        },
274        RuntimeDecl {
275            decl: "declare ptr @patch_seq_resume(ptr)",
276            category: None,
277        },
278        RuntimeDecl {
279            decl: "declare ptr @patch_seq_weave_cancel(ptr)",
280            category: None,
281        },
282        RuntimeDecl {
283            decl: "declare ptr @patch_seq_yield(ptr)",
284            category: None,
285        },
286        RuntimeDecl {
287            decl: "declare ptr @patch_seq_cond(ptr)",
288            category: None,
289        },
290        // Closure operations
291        RuntimeDecl {
292            decl: "declare ptr @patch_seq_create_env(i32)",
293            category: Some("; Closure operations"),
294        },
295        RuntimeDecl {
296            decl: "declare void @patch_seq_env_set(ptr, i32, %Value)",
297            category: None,
298        },
299        RuntimeDecl {
300            decl: "declare %Value @patch_seq_env_get(ptr, i64, i32)",
301            category: None,
302        },
303        RuntimeDecl {
304            decl: "declare i64 @patch_seq_env_get_int(ptr, i64, i32)",
305            category: None,
306        },
307        RuntimeDecl {
308            decl: "declare i64 @patch_seq_env_get_bool(ptr, i64, i32)",
309            category: None,
310        },
311        RuntimeDecl {
312            decl: "declare double @patch_seq_env_get_float(ptr, i64, i32)",
313            category: None,
314        },
315        RuntimeDecl {
316            decl: "declare i64 @patch_seq_env_get_quotation(ptr, i64, i32)",
317            category: None,
318        },
319        RuntimeDecl {
320            decl: "declare ptr @patch_seq_env_get_string(ptr, i64, i32)",
321            category: None,
322        },
323        RuntimeDecl {
324            decl: "declare ptr @patch_seq_env_push_string(ptr, ptr, i64, i32)",
325            category: None,
326        },
327        RuntimeDecl {
328            decl: "declare %Value @patch_seq_make_closure(i64, ptr)",
329            category: None,
330        },
331        RuntimeDecl {
332            decl: "declare ptr @patch_seq_push_closure(ptr, i64, i32)",
333            category: None,
334        },
335        RuntimeDecl {
336            decl: "declare ptr @patch_seq_push_seqstring(ptr, ptr)",
337            category: None,
338        },
339        // Concurrency operations
340        RuntimeDecl {
341            decl: "declare ptr @patch_seq_make_channel(ptr)",
342            category: Some("; Concurrency operations"),
343        },
344        RuntimeDecl {
345            decl: "declare ptr @patch_seq_chan_send(ptr)",
346            category: None,
347        },
348        RuntimeDecl {
349            decl: "declare ptr @patch_seq_chan_receive(ptr)",
350            category: None,
351        },
352        RuntimeDecl {
353            decl: "declare ptr @patch_seq_close_channel(ptr)",
354            category: None,
355        },
356        RuntimeDecl {
357            decl: "declare ptr @patch_seq_yield_strand(ptr)",
358            category: None,
359        },
360        RuntimeDecl {
361            decl: "declare void @patch_seq_maybe_yield()",
362            category: None,
363        },
364        // Scheduler operations
365        RuntimeDecl {
366            decl: "declare void @patch_seq_scheduler_init()",
367            category: Some("; Scheduler operations"),
368        },
369        RuntimeDecl {
370            decl: "declare ptr @patch_seq_scheduler_run()",
371            category: None,
372        },
373        RuntimeDecl {
374            decl: "declare i64 @patch_seq_strand_spawn(ptr, ptr)",
375            category: None,
376        },
377        // Command-line argument operations
378        RuntimeDecl {
379            decl: "declare void @patch_seq_args_init(i32, ptr)",
380            category: Some("; Command-line argument operations"),
381        },
382        RuntimeDecl {
383            decl: "declare ptr @patch_seq_arg_count(ptr)",
384            category: None,
385        },
386        RuntimeDecl {
387            decl: "declare ptr @patch_seq_arg_at(ptr)",
388            category: None,
389        },
390        // File operations
391        RuntimeDecl {
392            decl: "declare ptr @patch_seq_file_slurp(ptr)",
393            category: Some("; File operations"),
394        },
395        RuntimeDecl {
396            decl: "declare ptr @patch_seq_file_exists(ptr)",
397            category: None,
398        },
399        RuntimeDecl {
400            decl: "declare ptr @patch_seq_file_for_each_line_plus(ptr)",
401            category: None,
402        },
403        // List operations
404        RuntimeDecl {
405            decl: "declare ptr @patch_seq_list_make(ptr)",
406            category: Some("; List operations"),
407        },
408        RuntimeDecl {
409            decl: "declare ptr @patch_seq_list_push(ptr)",
410            category: None,
411        },
412        RuntimeDecl {
413            decl: "declare ptr @patch_seq_list_get(ptr)",
414            category: None,
415        },
416        RuntimeDecl {
417            decl: "declare ptr @patch_seq_list_set(ptr)",
418            category: None,
419        },
420        RuntimeDecl {
421            decl: "declare ptr @patch_seq_list_map(ptr)",
422            category: None,
423        },
424        RuntimeDecl {
425            decl: "declare ptr @patch_seq_list_filter(ptr)",
426            category: None,
427        },
428        RuntimeDecl {
429            decl: "declare ptr @patch_seq_list_fold(ptr)",
430            category: None,
431        },
432        RuntimeDecl {
433            decl: "declare ptr @patch_seq_list_each(ptr)",
434            category: None,
435        },
436        RuntimeDecl {
437            decl: "declare ptr @patch_seq_list_length(ptr)",
438            category: None,
439        },
440        RuntimeDecl {
441            decl: "declare ptr @patch_seq_list_empty(ptr)",
442            category: None,
443        },
444        // Map operations
445        RuntimeDecl {
446            decl: "declare ptr @patch_seq_make_map(ptr)",
447            category: Some("; Map operations"),
448        },
449        RuntimeDecl {
450            decl: "declare ptr @patch_seq_map_get(ptr)",
451            category: None,
452        },
453        RuntimeDecl {
454            decl: "declare ptr @patch_seq_map_set(ptr)",
455            category: None,
456        },
457        RuntimeDecl {
458            decl: "declare ptr @patch_seq_map_has(ptr)",
459            category: None,
460        },
461        RuntimeDecl {
462            decl: "declare ptr @patch_seq_map_remove(ptr)",
463            category: None,
464        },
465        RuntimeDecl {
466            decl: "declare ptr @patch_seq_map_keys(ptr)",
467            category: None,
468        },
469        RuntimeDecl {
470            decl: "declare ptr @patch_seq_map_values(ptr)",
471            category: None,
472        },
473        RuntimeDecl {
474            decl: "declare ptr @patch_seq_map_size(ptr)",
475            category: None,
476        },
477        RuntimeDecl {
478            decl: "declare ptr @patch_seq_map_empty(ptr)",
479            category: None,
480        },
481        // TCP operations
482        RuntimeDecl {
483            decl: "declare ptr @patch_seq_tcp_listen(ptr)",
484            category: Some("; TCP operations"),
485        },
486        RuntimeDecl {
487            decl: "declare ptr @patch_seq_tcp_accept(ptr)",
488            category: None,
489        },
490        RuntimeDecl {
491            decl: "declare ptr @patch_seq_tcp_read(ptr)",
492            category: None,
493        },
494        RuntimeDecl {
495            decl: "declare ptr @patch_seq_tcp_write(ptr)",
496            category: None,
497        },
498        RuntimeDecl {
499            decl: "declare ptr @patch_seq_tcp_close(ptr)",
500            category: None,
501        },
502        // OS operations
503        RuntimeDecl {
504            decl: "declare ptr @patch_seq_getenv(ptr)",
505            category: Some("; OS operations"),
506        },
507        RuntimeDecl {
508            decl: "declare ptr @patch_seq_home_dir(ptr)",
509            category: None,
510        },
511        RuntimeDecl {
512            decl: "declare ptr @patch_seq_current_dir(ptr)",
513            category: None,
514        },
515        RuntimeDecl {
516            decl: "declare ptr @patch_seq_path_exists(ptr)",
517            category: None,
518        },
519        RuntimeDecl {
520            decl: "declare ptr @patch_seq_path_is_file(ptr)",
521            category: None,
522        },
523        RuntimeDecl {
524            decl: "declare ptr @patch_seq_path_is_dir(ptr)",
525            category: None,
526        },
527        RuntimeDecl {
528            decl: "declare ptr @patch_seq_path_join(ptr)",
529            category: None,
530        },
531        RuntimeDecl {
532            decl: "declare ptr @patch_seq_path_parent(ptr)",
533            category: None,
534        },
535        RuntimeDecl {
536            decl: "declare ptr @patch_seq_path_filename(ptr)",
537            category: None,
538        },
539        RuntimeDecl {
540            decl: "declare ptr @patch_seq_exit(ptr)",
541            category: None,
542        },
543        RuntimeDecl {
544            decl: "declare ptr @patch_seq_os_name(ptr)",
545            category: None,
546        },
547        RuntimeDecl {
548            decl: "declare ptr @patch_seq_os_arch(ptr)",
549            category: None,
550        },
551        // Terminal operations
552        RuntimeDecl {
553            decl: "declare ptr @patch_seq_terminal_raw_mode(ptr)",
554            category: Some("; Terminal operations"),
555        },
556        RuntimeDecl {
557            decl: "declare ptr @patch_seq_terminal_read_char(ptr)",
558            category: None,
559        },
560        RuntimeDecl {
561            decl: "declare ptr @patch_seq_terminal_read_char_nonblock(ptr)",
562            category: None,
563        },
564        RuntimeDecl {
565            decl: "declare ptr @patch_seq_terminal_width(ptr)",
566            category: None,
567        },
568        RuntimeDecl {
569            decl: "declare ptr @patch_seq_terminal_height(ptr)",
570            category: None,
571        },
572        RuntimeDecl {
573            decl: "declare ptr @patch_seq_terminal_flush(ptr)",
574            category: None,
575        },
576        // String operations
577        RuntimeDecl {
578            decl: "declare ptr @patch_seq_string_concat(ptr)",
579            category: Some("; String operations"),
580        },
581        RuntimeDecl {
582            decl: "declare ptr @patch_seq_string_length(ptr)",
583            category: None,
584        },
585        RuntimeDecl {
586            decl: "declare ptr @patch_seq_string_byte_length(ptr)",
587            category: None,
588        },
589        RuntimeDecl {
590            decl: "declare ptr @patch_seq_string_char_at(ptr)",
591            category: None,
592        },
593        RuntimeDecl {
594            decl: "declare ptr @patch_seq_string_substring(ptr)",
595            category: None,
596        },
597        RuntimeDecl {
598            decl: "declare ptr @patch_seq_char_to_string(ptr)",
599            category: None,
600        },
601        RuntimeDecl {
602            decl: "declare ptr @patch_seq_string_find(ptr)",
603            category: None,
604        },
605        RuntimeDecl {
606            decl: "declare ptr @patch_seq_string_split(ptr)",
607            category: None,
608        },
609        RuntimeDecl {
610            decl: "declare ptr @patch_seq_string_contains(ptr)",
611            category: None,
612        },
613        RuntimeDecl {
614            decl: "declare ptr @patch_seq_string_starts_with(ptr)",
615            category: None,
616        },
617        RuntimeDecl {
618            decl: "declare ptr @patch_seq_string_empty(ptr)",
619            category: None,
620        },
621        RuntimeDecl {
622            decl: "declare ptr @patch_seq_string_trim(ptr)",
623            category: None,
624        },
625        RuntimeDecl {
626            decl: "declare ptr @patch_seq_string_chomp(ptr)",
627            category: None,
628        },
629        RuntimeDecl {
630            decl: "declare ptr @patch_seq_string_to_upper(ptr)",
631            category: None,
632        },
633        RuntimeDecl {
634            decl: "declare ptr @patch_seq_string_to_lower(ptr)",
635            category: None,
636        },
637        RuntimeDecl {
638            decl: "declare ptr @patch_seq_string_equal(ptr)",
639            category: None,
640        },
641        RuntimeDecl {
642            decl: "declare ptr @patch_seq_json_escape(ptr)",
643            category: None,
644        },
645        RuntimeDecl {
646            decl: "declare ptr @patch_seq_string_to_int(ptr)",
647            category: None,
648        },
649        // Encoding operations
650        RuntimeDecl {
651            decl: "declare ptr @patch_seq_base64_encode(ptr)",
652            category: Some("; Encoding operations"),
653        },
654        RuntimeDecl {
655            decl: "declare ptr @patch_seq_base64_decode(ptr)",
656            category: None,
657        },
658        RuntimeDecl {
659            decl: "declare ptr @patch_seq_base64url_encode(ptr)",
660            category: None,
661        },
662        RuntimeDecl {
663            decl: "declare ptr @patch_seq_base64url_decode(ptr)",
664            category: None,
665        },
666        RuntimeDecl {
667            decl: "declare ptr @patch_seq_hex_encode(ptr)",
668            category: None,
669        },
670        RuntimeDecl {
671            decl: "declare ptr @patch_seq_hex_decode(ptr)",
672            category: None,
673        },
674        // Crypto operations
675        RuntimeDecl {
676            decl: "declare ptr @patch_seq_sha256(ptr)",
677            category: Some("; Crypto operations"),
678        },
679        RuntimeDecl {
680            decl: "declare ptr @patch_seq_hmac_sha256(ptr)",
681            category: None,
682        },
683        RuntimeDecl {
684            decl: "declare ptr @patch_seq_constant_time_eq(ptr)",
685            category: None,
686        },
687        RuntimeDecl {
688            decl: "declare ptr @patch_seq_random_bytes(ptr)",
689            category: None,
690        },
691        RuntimeDecl {
692            decl: "declare ptr @patch_seq_random_int(ptr)",
693            category: None,
694        },
695        RuntimeDecl {
696            decl: "declare ptr @patch_seq_uuid4(ptr)",
697            category: None,
698        },
699        RuntimeDecl {
700            decl: "declare ptr @patch_seq_crypto_aes_gcm_encrypt(ptr)",
701            category: None,
702        },
703        RuntimeDecl {
704            decl: "declare ptr @patch_seq_crypto_aes_gcm_decrypt(ptr)",
705            category: None,
706        },
707        RuntimeDecl {
708            decl: "declare ptr @patch_seq_crypto_pbkdf2_sha256(ptr)",
709            category: None,
710        },
711        RuntimeDecl {
712            decl: "declare ptr @patch_seq_crypto_ed25519_keypair(ptr)",
713            category: None,
714        },
715        RuntimeDecl {
716            decl: "declare ptr @patch_seq_crypto_ed25519_sign(ptr)",
717            category: None,
718        },
719        RuntimeDecl {
720            decl: "declare ptr @patch_seq_crypto_ed25519_verify(ptr)",
721            category: None,
722        },
723        // HTTP client operations
724        RuntimeDecl {
725            decl: "declare ptr @patch_seq_http_get(ptr)",
726            category: Some("; HTTP client operations"),
727        },
728        RuntimeDecl {
729            decl: "declare ptr @patch_seq_http_post(ptr)",
730            category: None,
731        },
732        RuntimeDecl {
733            decl: "declare ptr @patch_seq_http_put(ptr)",
734            category: None,
735        },
736        RuntimeDecl {
737            decl: "declare ptr @patch_seq_http_delete(ptr)",
738            category: None,
739        },
740        // Symbol operations
741        RuntimeDecl {
742            decl: "declare ptr @patch_seq_symbol_equal(ptr)",
743            category: Some("; Symbol operations"),
744        },
745        // Variant operations
746        RuntimeDecl {
747            decl: "declare ptr @patch_seq_variant_field_count(ptr)",
748            category: Some("; Variant operations"),
749        },
750        RuntimeDecl {
751            decl: "declare ptr @patch_seq_variant_tag(ptr)",
752            category: None,
753        },
754        RuntimeDecl {
755            decl: "declare ptr @patch_seq_variant_field_at(ptr)",
756            category: None,
757        },
758        RuntimeDecl {
759            decl: "declare ptr @patch_seq_variant_append(ptr)",
760            category: None,
761        },
762        RuntimeDecl {
763            decl: "declare ptr @patch_seq_variant_last(ptr)",
764            category: None,
765        },
766        RuntimeDecl {
767            decl: "declare ptr @patch_seq_variant_init(ptr)",
768            category: None,
769        },
770        RuntimeDecl {
771            decl: "declare ptr @patch_seq_make_variant_0(ptr)",
772            category: None,
773        },
774        RuntimeDecl {
775            decl: "declare ptr @patch_seq_make_variant_1(ptr)",
776            category: None,
777        },
778        RuntimeDecl {
779            decl: "declare ptr @patch_seq_make_variant_2(ptr)",
780            category: None,
781        },
782        RuntimeDecl {
783            decl: "declare ptr @patch_seq_make_variant_3(ptr)",
784            category: None,
785        },
786        RuntimeDecl {
787            decl: "declare ptr @patch_seq_make_variant_4(ptr)",
788            category: None,
789        },
790        RuntimeDecl {
791            decl: "declare ptr @patch_seq_unpack_variant(ptr, i64)",
792            category: None,
793        },
794        RuntimeDecl {
795            decl: "declare ptr @patch_seq_symbol_eq_cstr(ptr, ptr)",
796            category: None,
797        },
798        // Float operations
799        RuntimeDecl {
800            decl: "declare ptr @patch_seq_push_float(ptr, double)",
801            category: Some("; Float operations"),
802        },
803        RuntimeDecl {
804            decl: "declare ptr @patch_seq_f_add(ptr)",
805            category: None,
806        },
807        RuntimeDecl {
808            decl: "declare ptr @patch_seq_f_subtract(ptr)",
809            category: None,
810        },
811        RuntimeDecl {
812            decl: "declare ptr @patch_seq_f_multiply(ptr)",
813            category: None,
814        },
815        RuntimeDecl {
816            decl: "declare ptr @patch_seq_f_divide(ptr)",
817            category: None,
818        },
819        RuntimeDecl {
820            decl: "declare ptr @patch_seq_f_eq(ptr)",
821            category: None,
822        },
823        RuntimeDecl {
824            decl: "declare ptr @patch_seq_f_lt(ptr)",
825            category: None,
826        },
827        RuntimeDecl {
828            decl: "declare ptr @patch_seq_f_gt(ptr)",
829            category: None,
830        },
831        RuntimeDecl {
832            decl: "declare ptr @patch_seq_f_lte(ptr)",
833            category: None,
834        },
835        RuntimeDecl {
836            decl: "declare ptr @patch_seq_f_gte(ptr)",
837            category: None,
838        },
839        RuntimeDecl {
840            decl: "declare ptr @patch_seq_f_neq(ptr)",
841            category: None,
842        },
843        RuntimeDecl {
844            decl: "declare ptr @patch_seq_int_to_float(ptr)",
845            category: None,
846        },
847        RuntimeDecl {
848            decl: "declare ptr @patch_seq_float_to_int(ptr)",
849            category: None,
850        },
851        RuntimeDecl {
852            decl: "declare ptr @patch_seq_float_to_string(ptr)",
853            category: None,
854        },
855        RuntimeDecl {
856            decl: "declare ptr @patch_seq_string_to_float(ptr)",
857            category: None,
858        },
859        // Test framework operations
860        RuntimeDecl {
861            decl: "declare ptr @patch_seq_test_init(ptr)",
862            category: Some("; Test framework operations"),
863        },
864        RuntimeDecl {
865            decl: "declare ptr @patch_seq_test_finish(ptr)",
866            category: None,
867        },
868        RuntimeDecl {
869            decl: "declare ptr @patch_seq_test_has_failures(ptr)",
870            category: None,
871        },
872        RuntimeDecl {
873            decl: "declare ptr @patch_seq_test_assert(ptr)",
874            category: None,
875        },
876        RuntimeDecl {
877            decl: "declare ptr @patch_seq_test_assert_not(ptr)",
878            category: None,
879        },
880        RuntimeDecl {
881            decl: "declare ptr @patch_seq_test_assert_eq(ptr)",
882            category: None,
883        },
884        RuntimeDecl {
885            decl: "declare ptr @patch_seq_test_assert_eq_str(ptr)",
886            category: None,
887        },
888        RuntimeDecl {
889            decl: "declare ptr @patch_seq_test_fail(ptr)",
890            category: None,
891        },
892        RuntimeDecl {
893            decl: "declare ptr @patch_seq_test_pass_count(ptr)",
894            category: None,
895        },
896        RuntimeDecl {
897            decl: "declare ptr @patch_seq_test_fail_count(ptr)",
898            category: None,
899        },
900        // Time operations
901        RuntimeDecl {
902            decl: "declare ptr @patch_seq_time_now(ptr)",
903            category: Some("; Time operations"),
904        },
905        RuntimeDecl {
906            decl: "declare ptr @patch_seq_time_nanos(ptr)",
907            category: None,
908        },
909        RuntimeDecl {
910            decl: "declare ptr @patch_seq_time_sleep_ms(ptr)",
911            category: None,
912        },
913        // Stack introspection
914        RuntimeDecl {
915            decl: "declare ptr @patch_seq_stack_dump(ptr)",
916            category: Some("; Stack introspection"),
917        },
918        // SON serialization
919        RuntimeDecl {
920            decl: "declare ptr @patch_seq_son_dump(ptr)",
921            category: Some("; SON serialization"),
922        },
923        RuntimeDecl {
924            decl: "declare ptr @patch_seq_son_dump_pretty(ptr)",
925            category: None,
926        },
927        // Regex operations
928        RuntimeDecl {
929            decl: "declare ptr @patch_seq_regex_match(ptr)",
930            category: Some("; Regex operations"),
931        },
932        RuntimeDecl {
933            decl: "declare ptr @patch_seq_regex_find(ptr)",
934            category: None,
935        },
936        RuntimeDecl {
937            decl: "declare ptr @patch_seq_regex_find_all(ptr)",
938            category: None,
939        },
940        RuntimeDecl {
941            decl: "declare ptr @patch_seq_regex_replace(ptr)",
942            category: None,
943        },
944        RuntimeDecl {
945            decl: "declare ptr @patch_seq_regex_replace_all(ptr)",
946            category: None,
947        },
948        RuntimeDecl {
949            decl: "declare ptr @patch_seq_regex_captures(ptr)",
950            category: None,
951        },
952        RuntimeDecl {
953            decl: "declare ptr @patch_seq_regex_split(ptr)",
954            category: None,
955        },
956        RuntimeDecl {
957            decl: "declare ptr @patch_seq_regex_valid(ptr)",
958            category: None,
959        },
960        // Compression operations
961        RuntimeDecl {
962            decl: "declare ptr @patch_seq_compress_gzip(ptr)",
963            category: Some("; Compression operations"),
964        },
965        RuntimeDecl {
966            decl: "declare ptr @patch_seq_compress_gzip_level(ptr)",
967            category: None,
968        },
969        RuntimeDecl {
970            decl: "declare ptr @patch_seq_compress_gunzip(ptr)",
971            category: None,
972        },
973        RuntimeDecl {
974            decl: "declare ptr @patch_seq_compress_zstd(ptr)",
975            category: None,
976        },
977        RuntimeDecl {
978            decl: "declare ptr @patch_seq_compress_zstd_level(ptr)",
979            category: None,
980        },
981        RuntimeDecl {
982            decl: "declare ptr @patch_seq_compress_unzstd(ptr)",
983            category: None,
984        },
985        // Helpers for conditionals
986        RuntimeDecl {
987            decl: "declare i64 @patch_seq_peek_int_value(ptr)",
988            category: Some("; Helpers for conditionals"),
989        },
990        RuntimeDecl {
991            decl: "declare i1 @patch_seq_peek_bool_value(ptr)",
992            category: None,
993        },
994        RuntimeDecl {
995            decl: "declare ptr @patch_seq_pop_stack(ptr)",
996            category: None,
997        },
998        // Tagged stack operations
999        RuntimeDecl {
1000            decl: "declare ptr @seq_stack_new_default()",
1001            category: Some("; Tagged stack operations"),
1002        },
1003        RuntimeDecl {
1004            decl: "declare void @seq_stack_free(ptr)",
1005            category: None,
1006        },
1007        RuntimeDecl {
1008            decl: "declare ptr @seq_stack_base(ptr)",
1009            category: None,
1010        },
1011        RuntimeDecl {
1012            decl: "declare i64 @seq_stack_sp(ptr)",
1013            category: None,
1014        },
1015        RuntimeDecl {
1016            decl: "declare void @seq_stack_set_sp(ptr, i64)",
1017            category: None,
1018        },
1019        RuntimeDecl {
1020            decl: "declare void @seq_stack_grow(ptr, i64)",
1021            category: None,
1022        },
1023        RuntimeDecl {
1024            decl: "declare void @patch_seq_set_stack_base(ptr)",
1025            category: None,
1026        },
1027    ]
1028});
1029
1030/// Mapping from Seq word names to their C runtime symbol names.
1031/// This centralizes all the name transformations in one place:
1032/// - Symbolic operators (=, <, >) map to descriptive names (eq, lt, gt)
1033/// - Hyphens become underscores for C compatibility
1034/// - Special characters get escaped (?, +, ->)
1035/// - Reserved words get suffixes (drop -> drop_op)
1036pub static BUILTIN_SYMBOLS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
1037    HashMap::from([
1038        // I/O operations
1039        ("io.write", "patch_seq_write"),
1040        ("io.write-line", "patch_seq_write_line"),
1041        ("io.read-line", "patch_seq_read_line"),
1042        ("io.read-line+", "patch_seq_read_line_plus"),
1043        ("io.read-n", "patch_seq_read_n"),
1044        ("int->string", "patch_seq_int_to_string"),
1045        ("symbol->string", "patch_seq_symbol_to_string"),
1046        ("string->symbol", "patch_seq_string_to_symbol"),
1047        // Command-line arguments
1048        ("args.count", "patch_seq_arg_count"),
1049        ("args.at", "patch_seq_arg_at"),
1050        // Integer Arithmetic
1051        ("i.add", "patch_seq_add"),
1052        ("i.subtract", "patch_seq_subtract"),
1053        ("i.multiply", "patch_seq_multiply"),
1054        ("i.divide", "patch_seq_divide"),
1055        ("i.modulo", "patch_seq_modulo"),
1056        // Terse integer arithmetic aliases
1057        ("i.+", "patch_seq_add"),
1058        ("i.-", "patch_seq_subtract"),
1059        ("i.*", "patch_seq_multiply"),
1060        ("i./", "patch_seq_divide"),
1061        ("i.%", "patch_seq_modulo"),
1062        // Integer comparison (symbol form)
1063        ("i.=", "patch_seq_eq"),
1064        ("i.<", "patch_seq_lt"),
1065        ("i.>", "patch_seq_gt"),
1066        ("i.<=", "patch_seq_lte"),
1067        ("i.>=", "patch_seq_gte"),
1068        ("i.<>", "patch_seq_neq"),
1069        // Integer comparison (verbose form)
1070        ("i.eq", "patch_seq_eq"),
1071        ("i.lt", "patch_seq_lt"),
1072        ("i.gt", "patch_seq_gt"),
1073        ("i.lte", "patch_seq_lte"),
1074        ("i.gte", "patch_seq_gte"),
1075        ("i.neq", "patch_seq_neq"),
1076        // Boolean
1077        ("and", "patch_seq_and"),
1078        ("or", "patch_seq_or"),
1079        ("not", "patch_seq_not"),
1080        // Bitwise
1081        ("band", "patch_seq_band"),
1082        ("bor", "patch_seq_bor"),
1083        ("bxor", "patch_seq_bxor"),
1084        ("bnot", "patch_seq_bnot"),
1085        ("shl", "patch_seq_shl"),
1086        ("shr", "patch_seq_shr"),
1087        ("popcount", "patch_seq_popcount"),
1088        ("clz", "patch_seq_clz"),
1089        ("ctz", "patch_seq_ctz"),
1090        ("int-bits", "patch_seq_int_bits"),
1091        // Stack operations
1092        ("dup", "patch_seq_dup"),
1093        ("swap", "patch_seq_swap"),
1094        ("over", "patch_seq_over"),
1095        ("rot", "patch_seq_rot"),
1096        ("nip", "patch_seq_nip"),
1097        ("tuck", "patch_seq_tuck"),
1098        ("2dup", "patch_seq_2dup"),
1099        ("3drop", "patch_seq_3drop"),
1100        ("drop", "patch_seq_drop_op"),
1101        ("pick", "patch_seq_pick_op"),
1102        ("roll", "patch_seq_roll"),
1103        // Channel operations (errors are values, not crashes)
1104        ("chan.make", "patch_seq_make_channel"),
1105        ("chan.send", "patch_seq_chan_send"),
1106        ("chan.receive", "patch_seq_chan_receive"),
1107        ("chan.close", "patch_seq_close_channel"),
1108        ("chan.yield", "patch_seq_yield_strand"),
1109        // Quotation operations
1110        ("call", "patch_seq_call"),
1111        ("strand.spawn", "patch_seq_spawn"),
1112        ("strand.weave", "patch_seq_weave"),
1113        ("strand.resume", "patch_seq_resume"),
1114        ("strand.weave-cancel", "patch_seq_weave_cancel"),
1115        ("yield", "patch_seq_yield"),
1116        ("cond", "patch_seq_cond"),
1117        // TCP operations
1118        ("tcp.listen", "patch_seq_tcp_listen"),
1119        ("tcp.accept", "patch_seq_tcp_accept"),
1120        ("tcp.read", "patch_seq_tcp_read"),
1121        ("tcp.write", "patch_seq_tcp_write"),
1122        ("tcp.close", "patch_seq_tcp_close"),
1123        // OS operations
1124        ("os.getenv", "patch_seq_getenv"),
1125        ("os.home-dir", "patch_seq_home_dir"),
1126        ("os.current-dir", "patch_seq_current_dir"),
1127        ("os.path-exists", "patch_seq_path_exists"),
1128        ("os.path-is-file", "patch_seq_path_is_file"),
1129        ("os.path-is-dir", "patch_seq_path_is_dir"),
1130        ("os.path-join", "patch_seq_path_join"),
1131        ("os.path-parent", "patch_seq_path_parent"),
1132        ("os.path-filename", "patch_seq_path_filename"),
1133        ("os.exit", "patch_seq_exit"),
1134        ("os.name", "patch_seq_os_name"),
1135        ("os.arch", "patch_seq_os_arch"),
1136        // Terminal operations
1137        ("terminal.raw-mode", "patch_seq_terminal_raw_mode"),
1138        ("terminal.read-char", "patch_seq_terminal_read_char"),
1139        (
1140            "terminal.read-char?",
1141            "patch_seq_terminal_read_char_nonblock",
1142        ),
1143        ("terminal.width", "patch_seq_terminal_width"),
1144        ("terminal.height", "patch_seq_terminal_height"),
1145        ("terminal.flush", "patch_seq_terminal_flush"),
1146        // String operations
1147        ("string.concat", "patch_seq_string_concat"),
1148        ("string.length", "patch_seq_string_length"),
1149        ("string.byte-length", "patch_seq_string_byte_length"),
1150        ("string.char-at", "patch_seq_string_char_at"),
1151        ("string.substring", "patch_seq_string_substring"),
1152        ("char->string", "patch_seq_char_to_string"),
1153        ("string.find", "patch_seq_string_find"),
1154        ("string.split", "patch_seq_string_split"),
1155        ("string.contains", "patch_seq_string_contains"),
1156        ("string.starts-with", "patch_seq_string_starts_with"),
1157        ("string.empty?", "patch_seq_string_empty"),
1158        ("string.trim", "patch_seq_string_trim"),
1159        ("string.chomp", "patch_seq_string_chomp"),
1160        ("string.to-upper", "patch_seq_string_to_upper"),
1161        ("string.to-lower", "patch_seq_string_to_lower"),
1162        ("string.equal?", "patch_seq_string_equal"),
1163        ("string.json-escape", "patch_seq_json_escape"),
1164        ("string->int", "patch_seq_string_to_int"),
1165        // Encoding operations
1166        ("encoding.base64-encode", "patch_seq_base64_encode"),
1167        ("encoding.base64-decode", "patch_seq_base64_decode"),
1168        ("encoding.base64url-encode", "patch_seq_base64url_encode"),
1169        ("encoding.base64url-decode", "patch_seq_base64url_decode"),
1170        ("encoding.hex-encode", "patch_seq_hex_encode"),
1171        ("encoding.hex-decode", "patch_seq_hex_decode"),
1172        // Crypto operations
1173        ("crypto.sha256", "patch_seq_sha256"),
1174        ("crypto.hmac-sha256", "patch_seq_hmac_sha256"),
1175        ("crypto.constant-time-eq", "patch_seq_constant_time_eq"),
1176        ("crypto.random-bytes", "patch_seq_random_bytes"),
1177        ("crypto.random-int", "patch_seq_random_int"),
1178        ("crypto.uuid4", "patch_seq_uuid4"),
1179        ("crypto.aes-gcm-encrypt", "patch_seq_crypto_aes_gcm_encrypt"),
1180        ("crypto.aes-gcm-decrypt", "patch_seq_crypto_aes_gcm_decrypt"),
1181        ("crypto.pbkdf2-sha256", "patch_seq_crypto_pbkdf2_sha256"),
1182        ("crypto.ed25519-keypair", "patch_seq_crypto_ed25519_keypair"),
1183        ("crypto.ed25519-sign", "patch_seq_crypto_ed25519_sign"),
1184        ("crypto.ed25519-verify", "patch_seq_crypto_ed25519_verify"),
1185        // HTTP client operations
1186        ("http.get", "patch_seq_http_get"),
1187        ("http.post", "patch_seq_http_post"),
1188        ("http.put", "patch_seq_http_put"),
1189        ("http.delete", "patch_seq_http_delete"),
1190        // Regex operations
1191        ("regex.match?", "patch_seq_regex_match"),
1192        ("regex.find", "patch_seq_regex_find"),
1193        ("regex.find-all", "patch_seq_regex_find_all"),
1194        ("regex.replace", "patch_seq_regex_replace"),
1195        ("regex.replace-all", "patch_seq_regex_replace_all"),
1196        ("regex.captures", "patch_seq_regex_captures"),
1197        ("regex.split", "patch_seq_regex_split"),
1198        ("regex.valid?", "patch_seq_regex_valid"),
1199        // Compression operations
1200        ("compress.gzip", "patch_seq_compress_gzip"),
1201        ("compress.gzip-level", "patch_seq_compress_gzip_level"),
1202        ("compress.gunzip", "patch_seq_compress_gunzip"),
1203        ("compress.zstd", "patch_seq_compress_zstd"),
1204        ("compress.zstd-level", "patch_seq_compress_zstd_level"),
1205        ("compress.unzstd", "patch_seq_compress_unzstd"),
1206        // Symbol operations
1207        ("symbol.=", "patch_seq_symbol_equal"),
1208        // File operations
1209        ("file.slurp", "patch_seq_file_slurp"),
1210        ("file.exists?", "patch_seq_file_exists"),
1211        ("file.for-each-line+", "patch_seq_file_for_each_line_plus"),
1212        // List operations
1213        ("list.make", "patch_seq_list_make"),
1214        ("list.push", "patch_seq_list_push"),
1215        ("list.get", "patch_seq_list_get"),
1216        ("list.set", "patch_seq_list_set"),
1217        ("list.map", "patch_seq_list_map"),
1218        ("list.filter", "patch_seq_list_filter"),
1219        ("list.fold", "patch_seq_list_fold"),
1220        ("list.each", "patch_seq_list_each"),
1221        ("list.length", "patch_seq_list_length"),
1222        ("list.empty?", "patch_seq_list_empty"),
1223        // Map operations
1224        ("map.make", "patch_seq_make_map"),
1225        ("map.get", "patch_seq_map_get"),
1226        ("map.set", "patch_seq_map_set"),
1227        ("map.has?", "patch_seq_map_has"),
1228        ("map.remove", "patch_seq_map_remove"),
1229        ("map.keys", "patch_seq_map_keys"),
1230        ("map.values", "patch_seq_map_values"),
1231        ("map.size", "patch_seq_map_size"),
1232        ("map.empty?", "patch_seq_map_empty"),
1233        // Variant operations
1234        ("variant.field-count", "patch_seq_variant_field_count"),
1235        ("variant.tag", "patch_seq_variant_tag"),
1236        ("variant.field-at", "patch_seq_variant_field_at"),
1237        ("variant.append", "patch_seq_variant_append"),
1238        ("variant.last", "patch_seq_variant_last"),
1239        ("variant.init", "patch_seq_variant_init"),
1240        ("variant.make-0", "patch_seq_make_variant_0"),
1241        ("variant.make-1", "patch_seq_make_variant_1"),
1242        ("variant.make-2", "patch_seq_make_variant_2"),
1243        ("variant.make-3", "patch_seq_make_variant_3"),
1244        ("variant.make-4", "patch_seq_make_variant_4"),
1245        // wrap-N aliases for dynamic variant construction (SON)
1246        ("wrap-0", "patch_seq_make_variant_0"),
1247        ("wrap-1", "patch_seq_make_variant_1"),
1248        ("wrap-2", "patch_seq_make_variant_2"),
1249        ("wrap-3", "patch_seq_make_variant_3"),
1250        ("wrap-4", "patch_seq_make_variant_4"),
1251        // Float arithmetic
1252        ("f.add", "patch_seq_f_add"),
1253        ("f.subtract", "patch_seq_f_subtract"),
1254        ("f.multiply", "patch_seq_f_multiply"),
1255        ("f.divide", "patch_seq_f_divide"),
1256        // Terse float arithmetic aliases
1257        ("f.+", "patch_seq_f_add"),
1258        ("f.-", "patch_seq_f_subtract"),
1259        ("f.*", "patch_seq_f_multiply"),
1260        ("f./", "patch_seq_f_divide"),
1261        // Float comparison (symbol form)
1262        ("f.=", "patch_seq_f_eq"),
1263        ("f.<", "patch_seq_f_lt"),
1264        ("f.>", "patch_seq_f_gt"),
1265        ("f.<=", "patch_seq_f_lte"),
1266        ("f.>=", "patch_seq_f_gte"),
1267        ("f.<>", "patch_seq_f_neq"),
1268        // Float comparison (verbose form)
1269        ("f.eq", "patch_seq_f_eq"),
1270        ("f.lt", "patch_seq_f_lt"),
1271        ("f.gt", "patch_seq_f_gt"),
1272        ("f.lte", "patch_seq_f_lte"),
1273        ("f.gte", "patch_seq_f_gte"),
1274        ("f.neq", "patch_seq_f_neq"),
1275        // Float type conversions
1276        ("int->float", "patch_seq_int_to_float"),
1277        ("float->int", "patch_seq_float_to_int"),
1278        ("float->string", "patch_seq_float_to_string"),
1279        ("string->float", "patch_seq_string_to_float"),
1280        // Test framework operations
1281        ("test.init", "patch_seq_test_init"),
1282        ("test.finish", "patch_seq_test_finish"),
1283        ("test.has-failures", "patch_seq_test_has_failures"),
1284        ("test.assert", "patch_seq_test_assert"),
1285        ("test.assert-not", "patch_seq_test_assert_not"),
1286        ("test.assert-eq", "patch_seq_test_assert_eq"),
1287        ("test.assert-eq-str", "patch_seq_test_assert_eq_str"),
1288        ("test.fail", "patch_seq_test_fail"),
1289        ("test.pass-count", "patch_seq_test_pass_count"),
1290        ("test.fail-count", "patch_seq_test_fail_count"),
1291        // Time operations
1292        ("time.now", "patch_seq_time_now"),
1293        ("time.nanos", "patch_seq_time_nanos"),
1294        ("time.sleep-ms", "patch_seq_time_sleep_ms"),
1295        // SON serialization
1296        ("son.dump", "patch_seq_son_dump"),
1297        ("son.dump-pretty", "patch_seq_son_dump_pretty"),
1298        // Stack introspection
1299        ("stack.dump", "patch_seq_stack_dump"),
1300    ])
1301});
1302
1303/// Emit all runtime function declarations to the IR string.
1304pub fn emit_runtime_decls(ir: &mut String) -> Result<(), CodeGenError> {
1305    for decl in RUNTIME_DECLARATIONS.iter() {
1306        if let Some(cat) = decl.category {
1307            writeln!(ir, "{}", cat)?;
1308        }
1309        writeln!(ir, "{}", decl.decl)?;
1310    }
1311    writeln!(ir)?;
1312    Ok(())
1313}