Skip to main content

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_pick_op(ptr)",
235            category: None,
236        },
237        RuntimeDecl {
238            decl: "declare ptr @patch_seq_roll(ptr)",
239            category: None,
240        },
241        RuntimeDecl {
242            decl: "declare ptr @patch_seq_push_value(ptr, %Value)",
243            category: None,
244        },
245        // Quotation operations
246        RuntimeDecl {
247            decl: "declare ptr @patch_seq_push_quotation(ptr, i64, i64)",
248            category: Some("; Quotation operations"),
249        },
250        RuntimeDecl {
251            decl: "declare ptr @patch_seq_call(ptr)",
252            category: None,
253        },
254        RuntimeDecl {
255            decl: "declare ptr @patch_seq_dip(ptr)",
256            category: Some("; Dataflow combinators"),
257        },
258        RuntimeDecl {
259            decl: "declare ptr @patch_seq_keep(ptr)",
260            category: None,
261        },
262        RuntimeDecl {
263            decl: "declare ptr @patch_seq_bi(ptr)",
264            category: None,
265        },
266        RuntimeDecl {
267            decl: "declare i64 @patch_seq_peek_is_quotation(ptr)",
268            category: None,
269        },
270        RuntimeDecl {
271            decl: "declare i64 @patch_seq_peek_quotation_fn_ptr(ptr)",
272            category: None,
273        },
274        RuntimeDecl {
275            decl: "declare ptr @patch_seq_spawn(ptr)",
276            category: None,
277        },
278        RuntimeDecl {
279            decl: "declare ptr @patch_seq_weave(ptr)",
280            category: None,
281        },
282        RuntimeDecl {
283            decl: "declare ptr @patch_seq_resume(ptr)",
284            category: None,
285        },
286        RuntimeDecl {
287            decl: "declare ptr @patch_seq_weave_cancel(ptr)",
288            category: None,
289        },
290        RuntimeDecl {
291            decl: "declare ptr @patch_seq_yield(ptr)",
292            category: None,
293        },
294        RuntimeDecl {
295            decl: "declare ptr @patch_seq_cond(ptr)",
296            category: None,
297        },
298        // Closure operations
299        RuntimeDecl {
300            decl: "declare ptr @patch_seq_create_env(i32)",
301            category: Some("; Closure operations"),
302        },
303        RuntimeDecl {
304            decl: "declare void @patch_seq_env_set(ptr, i32, %Value)",
305            category: None,
306        },
307        RuntimeDecl {
308            decl: "declare %Value @patch_seq_env_get(ptr, i64, i32)",
309            category: None,
310        },
311        RuntimeDecl {
312            decl: "declare i64 @patch_seq_env_get_int(ptr, i64, i32)",
313            category: None,
314        },
315        RuntimeDecl {
316            decl: "declare i64 @patch_seq_env_get_bool(ptr, i64, i32)",
317            category: None,
318        },
319        RuntimeDecl {
320            decl: "declare double @patch_seq_env_get_float(ptr, i64, i32)",
321            category: None,
322        },
323        RuntimeDecl {
324            decl: "declare i64 @patch_seq_env_get_quotation(ptr, i64, i32)",
325            category: None,
326        },
327        RuntimeDecl {
328            decl: "declare ptr @patch_seq_env_get_string(ptr, i64, i32)",
329            category: None,
330        },
331        RuntimeDecl {
332            decl: "declare ptr @patch_seq_env_push_string(ptr, ptr, i64, i32)",
333            category: None,
334        },
335        RuntimeDecl {
336            decl: "declare %Value @patch_seq_make_closure(i64, ptr)",
337            category: None,
338        },
339        RuntimeDecl {
340            decl: "declare ptr @patch_seq_push_closure(ptr, i64, i32)",
341            category: None,
342        },
343        RuntimeDecl {
344            decl: "declare ptr @patch_seq_push_seqstring(ptr, ptr)",
345            category: None,
346        },
347        // Concurrency operations
348        RuntimeDecl {
349            decl: "declare ptr @patch_seq_make_channel(ptr)",
350            category: Some("; Concurrency operations"),
351        },
352        RuntimeDecl {
353            decl: "declare ptr @patch_seq_chan_send(ptr)",
354            category: None,
355        },
356        RuntimeDecl {
357            decl: "declare ptr @patch_seq_chan_receive(ptr)",
358            category: None,
359        },
360        RuntimeDecl {
361            decl: "declare ptr @patch_seq_close_channel(ptr)",
362            category: None,
363        },
364        RuntimeDecl {
365            decl: "declare ptr @patch_seq_yield_strand(ptr)",
366            category: None,
367        },
368        RuntimeDecl {
369            decl: "declare void @patch_seq_maybe_yield()",
370            category: None,
371        },
372        // Scheduler operations
373        RuntimeDecl {
374            decl: "declare void @patch_seq_scheduler_init()",
375            category: Some("; Scheduler operations"),
376        },
377        RuntimeDecl {
378            decl: "declare ptr @patch_seq_scheduler_run()",
379            category: None,
380        },
381        RuntimeDecl {
382            decl: "declare i64 @patch_seq_strand_spawn(ptr, ptr)",
383            category: None,
384        },
385        // Command-line argument operations
386        RuntimeDecl {
387            decl: "declare void @patch_seq_args_init(i32, ptr)",
388            category: Some("; Command-line argument operations"),
389        },
390        RuntimeDecl {
391            decl: "declare ptr @patch_seq_arg_count(ptr)",
392            category: None,
393        },
394        RuntimeDecl {
395            decl: "declare ptr @patch_seq_arg_at(ptr)",
396            category: None,
397        },
398        // File operations
399        RuntimeDecl {
400            decl: "declare ptr @patch_seq_file_slurp(ptr)",
401            category: Some("; File operations"),
402        },
403        RuntimeDecl {
404            decl: "declare ptr @patch_seq_file_exists(ptr)",
405            category: None,
406        },
407        RuntimeDecl {
408            decl: "declare ptr @patch_seq_file_for_each_line_plus(ptr)",
409            category: None,
410        },
411        RuntimeDecl {
412            decl: "declare ptr @patch_seq_file_spit(ptr)",
413            category: None,
414        },
415        RuntimeDecl {
416            decl: "declare ptr @patch_seq_file_append(ptr)",
417            category: None,
418        },
419        RuntimeDecl {
420            decl: "declare ptr @patch_seq_file_delete(ptr)",
421            category: None,
422        },
423        RuntimeDecl {
424            decl: "declare ptr @patch_seq_file_size(ptr)",
425            category: None,
426        },
427        // Directory operations
428        RuntimeDecl {
429            decl: "declare ptr @patch_seq_dir_exists(ptr)",
430            category: Some("; Directory operations"),
431        },
432        RuntimeDecl {
433            decl: "declare ptr @patch_seq_dir_make(ptr)",
434            category: None,
435        },
436        RuntimeDecl {
437            decl: "declare ptr @patch_seq_dir_delete(ptr)",
438            category: None,
439        },
440        RuntimeDecl {
441            decl: "declare ptr @patch_seq_dir_list(ptr)",
442            category: None,
443        },
444        // List operations
445        RuntimeDecl {
446            decl: "declare ptr @patch_seq_list_make(ptr)",
447            category: Some("; List operations"),
448        },
449        RuntimeDecl {
450            decl: "declare ptr @patch_seq_list_push(ptr)",
451            category: None,
452        },
453        RuntimeDecl {
454            decl: "declare ptr @patch_seq_list_get(ptr)",
455            category: None,
456        },
457        RuntimeDecl {
458            decl: "declare ptr @patch_seq_list_set(ptr)",
459            category: None,
460        },
461        RuntimeDecl {
462            decl: "declare ptr @patch_seq_list_map(ptr)",
463            category: None,
464        },
465        RuntimeDecl {
466            decl: "declare ptr @patch_seq_list_filter(ptr)",
467            category: None,
468        },
469        RuntimeDecl {
470            decl: "declare ptr @patch_seq_list_fold(ptr)",
471            category: None,
472        },
473        RuntimeDecl {
474            decl: "declare ptr @patch_seq_list_each(ptr)",
475            category: None,
476        },
477        RuntimeDecl {
478            decl: "declare ptr @patch_seq_list_length(ptr)",
479            category: None,
480        },
481        RuntimeDecl {
482            decl: "declare ptr @patch_seq_list_empty(ptr)",
483            category: None,
484        },
485        RuntimeDecl {
486            decl: "declare ptr @patch_seq_list_reverse(ptr)",
487            category: None,
488        },
489        // Map operations
490        RuntimeDecl {
491            decl: "declare ptr @patch_seq_make_map(ptr)",
492            category: Some("; Map operations"),
493        },
494        RuntimeDecl {
495            decl: "declare ptr @patch_seq_map_get(ptr)",
496            category: None,
497        },
498        RuntimeDecl {
499            decl: "declare ptr @patch_seq_map_set(ptr)",
500            category: None,
501        },
502        RuntimeDecl {
503            decl: "declare ptr @patch_seq_map_has(ptr)",
504            category: None,
505        },
506        RuntimeDecl {
507            decl: "declare ptr @patch_seq_map_remove(ptr)",
508            category: None,
509        },
510        RuntimeDecl {
511            decl: "declare ptr @patch_seq_map_keys(ptr)",
512            category: None,
513        },
514        RuntimeDecl {
515            decl: "declare ptr @patch_seq_map_values(ptr)",
516            category: None,
517        },
518        RuntimeDecl {
519            decl: "declare ptr @patch_seq_map_size(ptr)",
520            category: None,
521        },
522        RuntimeDecl {
523            decl: "declare ptr @patch_seq_map_empty(ptr)",
524            category: None,
525        },
526        RuntimeDecl {
527            decl: "declare ptr @patch_seq_map_each(ptr)",
528            category: None,
529        },
530        RuntimeDecl {
531            decl: "declare ptr @patch_seq_map_fold(ptr)",
532            category: None,
533        },
534        // TCP operations
535        RuntimeDecl {
536            decl: "declare ptr @patch_seq_tcp_listen(ptr)",
537            category: Some("; TCP operations"),
538        },
539        RuntimeDecl {
540            decl: "declare ptr @patch_seq_tcp_accept(ptr)",
541            category: None,
542        },
543        RuntimeDecl {
544            decl: "declare ptr @patch_seq_tcp_read(ptr)",
545            category: None,
546        },
547        RuntimeDecl {
548            decl: "declare ptr @patch_seq_tcp_write(ptr)",
549            category: None,
550        },
551        RuntimeDecl {
552            decl: "declare ptr @patch_seq_tcp_close(ptr)",
553            category: None,
554        },
555        // OS operations
556        RuntimeDecl {
557            decl: "declare ptr @patch_seq_getenv(ptr)",
558            category: Some("; OS operations"),
559        },
560        RuntimeDecl {
561            decl: "declare ptr @patch_seq_home_dir(ptr)",
562            category: None,
563        },
564        RuntimeDecl {
565            decl: "declare ptr @patch_seq_current_dir(ptr)",
566            category: None,
567        },
568        RuntimeDecl {
569            decl: "declare ptr @patch_seq_path_exists(ptr)",
570            category: None,
571        },
572        RuntimeDecl {
573            decl: "declare ptr @patch_seq_path_is_file(ptr)",
574            category: None,
575        },
576        RuntimeDecl {
577            decl: "declare ptr @patch_seq_path_is_dir(ptr)",
578            category: None,
579        },
580        RuntimeDecl {
581            decl: "declare ptr @patch_seq_path_join(ptr)",
582            category: None,
583        },
584        RuntimeDecl {
585            decl: "declare ptr @patch_seq_path_parent(ptr)",
586            category: None,
587        },
588        RuntimeDecl {
589            decl: "declare ptr @patch_seq_path_filename(ptr)",
590            category: None,
591        },
592        RuntimeDecl {
593            decl: "declare ptr @patch_seq_exit(ptr)",
594            category: None,
595        },
596        RuntimeDecl {
597            decl: "declare ptr @patch_seq_os_name(ptr)",
598            category: None,
599        },
600        RuntimeDecl {
601            decl: "declare ptr @patch_seq_os_arch(ptr)",
602            category: None,
603        },
604        // Signal handling
605        RuntimeDecl {
606            decl: "declare ptr @patch_seq_signal_trap(ptr)",
607            category: Some("; Signal handling"),
608        },
609        RuntimeDecl {
610            decl: "declare ptr @patch_seq_signal_received(ptr)",
611            category: None,
612        },
613        RuntimeDecl {
614            decl: "declare ptr @patch_seq_signal_pending(ptr)",
615            category: None,
616        },
617        RuntimeDecl {
618            decl: "declare ptr @patch_seq_signal_default(ptr)",
619            category: None,
620        },
621        RuntimeDecl {
622            decl: "declare ptr @patch_seq_signal_ignore(ptr)",
623            category: None,
624        },
625        RuntimeDecl {
626            decl: "declare ptr @patch_seq_signal_clear(ptr)",
627            category: None,
628        },
629        RuntimeDecl {
630            decl: "declare ptr @patch_seq_signal_sigint(ptr)",
631            category: None,
632        },
633        RuntimeDecl {
634            decl: "declare ptr @patch_seq_signal_sigterm(ptr)",
635            category: None,
636        },
637        RuntimeDecl {
638            decl: "declare ptr @patch_seq_signal_sighup(ptr)",
639            category: None,
640        },
641        RuntimeDecl {
642            decl: "declare ptr @patch_seq_signal_sigpipe(ptr)",
643            category: None,
644        },
645        RuntimeDecl {
646            decl: "declare ptr @patch_seq_signal_sigusr1(ptr)",
647            category: None,
648        },
649        RuntimeDecl {
650            decl: "declare ptr @patch_seq_signal_sigusr2(ptr)",
651            category: None,
652        },
653        RuntimeDecl {
654            decl: "declare ptr @patch_seq_signal_sigchld(ptr)",
655            category: None,
656        },
657        RuntimeDecl {
658            decl: "declare ptr @patch_seq_signal_sigalrm(ptr)",
659            category: None,
660        },
661        RuntimeDecl {
662            decl: "declare ptr @patch_seq_signal_sigcont(ptr)",
663            category: None,
664        },
665        // Terminal operations
666        RuntimeDecl {
667            decl: "declare ptr @patch_seq_terminal_raw_mode(ptr)",
668            category: Some("; Terminal operations"),
669        },
670        RuntimeDecl {
671            decl: "declare ptr @patch_seq_terminal_read_char(ptr)",
672            category: None,
673        },
674        RuntimeDecl {
675            decl: "declare ptr @patch_seq_terminal_read_char_nonblock(ptr)",
676            category: None,
677        },
678        RuntimeDecl {
679            decl: "declare ptr @patch_seq_terminal_width(ptr)",
680            category: None,
681        },
682        RuntimeDecl {
683            decl: "declare ptr @patch_seq_terminal_height(ptr)",
684            category: None,
685        },
686        RuntimeDecl {
687            decl: "declare ptr @patch_seq_terminal_flush(ptr)",
688            category: None,
689        },
690        // String operations
691        RuntimeDecl {
692            decl: "declare ptr @patch_seq_string_concat(ptr)",
693            category: Some("; String operations"),
694        },
695        RuntimeDecl {
696            decl: "declare ptr @patch_seq_string_length(ptr)",
697            category: None,
698        },
699        RuntimeDecl {
700            decl: "declare ptr @patch_seq_string_byte_length(ptr)",
701            category: None,
702        },
703        RuntimeDecl {
704            decl: "declare ptr @patch_seq_string_char_at(ptr)",
705            category: None,
706        },
707        RuntimeDecl {
708            decl: "declare ptr @patch_seq_string_substring(ptr)",
709            category: None,
710        },
711        RuntimeDecl {
712            decl: "declare ptr @patch_seq_char_to_string(ptr)",
713            category: None,
714        },
715        RuntimeDecl {
716            decl: "declare ptr @patch_seq_string_find(ptr)",
717            category: None,
718        },
719        RuntimeDecl {
720            decl: "declare ptr @patch_seq_string_split(ptr)",
721            category: None,
722        },
723        RuntimeDecl {
724            decl: "declare ptr @patch_seq_string_contains(ptr)",
725            category: None,
726        },
727        RuntimeDecl {
728            decl: "declare ptr @patch_seq_string_starts_with(ptr)",
729            category: None,
730        },
731        RuntimeDecl {
732            decl: "declare ptr @patch_seq_string_empty(ptr)",
733            category: None,
734        },
735        RuntimeDecl {
736            decl: "declare ptr @patch_seq_string_trim(ptr)",
737            category: None,
738        },
739        RuntimeDecl {
740            decl: "declare ptr @patch_seq_string_chomp(ptr)",
741            category: None,
742        },
743        RuntimeDecl {
744            decl: "declare ptr @patch_seq_string_to_upper(ptr)",
745            category: None,
746        },
747        RuntimeDecl {
748            decl: "declare ptr @patch_seq_string_to_lower(ptr)",
749            category: None,
750        },
751        RuntimeDecl {
752            decl: "declare ptr @patch_seq_string_equal(ptr)",
753            category: None,
754        },
755        RuntimeDecl {
756            decl: "declare ptr @patch_seq_string_join(ptr)",
757            category: None,
758        },
759        RuntimeDecl {
760            decl: "declare ptr @patch_seq_json_escape(ptr)",
761            category: None,
762        },
763        RuntimeDecl {
764            decl: "declare ptr @patch_seq_string_to_int(ptr)",
765            category: None,
766        },
767        // Encoding operations
768        RuntimeDecl {
769            decl: "declare ptr @patch_seq_base64_encode(ptr)",
770            category: Some("; Encoding operations"),
771        },
772        RuntimeDecl {
773            decl: "declare ptr @patch_seq_base64_decode(ptr)",
774            category: None,
775        },
776        RuntimeDecl {
777            decl: "declare ptr @patch_seq_base64url_encode(ptr)",
778            category: None,
779        },
780        RuntimeDecl {
781            decl: "declare ptr @patch_seq_base64url_decode(ptr)",
782            category: None,
783        },
784        RuntimeDecl {
785            decl: "declare ptr @patch_seq_hex_encode(ptr)",
786            category: None,
787        },
788        RuntimeDecl {
789            decl: "declare ptr @patch_seq_hex_decode(ptr)",
790            category: None,
791        },
792        // Crypto operations
793        RuntimeDecl {
794            decl: "declare ptr @patch_seq_sha256(ptr)",
795            category: Some("; Crypto operations"),
796        },
797        RuntimeDecl {
798            decl: "declare ptr @patch_seq_hmac_sha256(ptr)",
799            category: None,
800        },
801        RuntimeDecl {
802            decl: "declare ptr @patch_seq_constant_time_eq(ptr)",
803            category: None,
804        },
805        RuntimeDecl {
806            decl: "declare ptr @patch_seq_random_bytes(ptr)",
807            category: None,
808        },
809        RuntimeDecl {
810            decl: "declare ptr @patch_seq_random_int(ptr)",
811            category: None,
812        },
813        RuntimeDecl {
814            decl: "declare ptr @patch_seq_uuid4(ptr)",
815            category: None,
816        },
817        RuntimeDecl {
818            decl: "declare ptr @patch_seq_crypto_aes_gcm_encrypt(ptr)",
819            category: None,
820        },
821        RuntimeDecl {
822            decl: "declare ptr @patch_seq_crypto_aes_gcm_decrypt(ptr)",
823            category: None,
824        },
825        RuntimeDecl {
826            decl: "declare ptr @patch_seq_crypto_pbkdf2_sha256(ptr)",
827            category: None,
828        },
829        RuntimeDecl {
830            decl: "declare ptr @patch_seq_crypto_ed25519_keypair(ptr)",
831            category: None,
832        },
833        RuntimeDecl {
834            decl: "declare ptr @patch_seq_crypto_ed25519_sign(ptr)",
835            category: None,
836        },
837        RuntimeDecl {
838            decl: "declare ptr @patch_seq_crypto_ed25519_verify(ptr)",
839            category: None,
840        },
841        // HTTP client operations
842        RuntimeDecl {
843            decl: "declare ptr @patch_seq_http_get(ptr)",
844            category: Some("; HTTP client operations"),
845        },
846        RuntimeDecl {
847            decl: "declare ptr @patch_seq_http_post(ptr)",
848            category: None,
849        },
850        RuntimeDecl {
851            decl: "declare ptr @patch_seq_http_put(ptr)",
852            category: None,
853        },
854        RuntimeDecl {
855            decl: "declare ptr @patch_seq_http_delete(ptr)",
856            category: None,
857        },
858        // Symbol operations
859        RuntimeDecl {
860            decl: "declare ptr @patch_seq_symbol_equal(ptr)",
861            category: Some("; Symbol operations"),
862        },
863        // Variant operations
864        RuntimeDecl {
865            decl: "declare ptr @patch_seq_variant_field_count(ptr)",
866            category: Some("; Variant operations"),
867        },
868        RuntimeDecl {
869            decl: "declare ptr @patch_seq_variant_tag(ptr)",
870            category: None,
871        },
872        RuntimeDecl {
873            decl: "declare ptr @patch_seq_variant_field_at(ptr)",
874            category: None,
875        },
876        RuntimeDecl {
877            decl: "declare ptr @patch_seq_variant_append(ptr)",
878            category: None,
879        },
880        RuntimeDecl {
881            decl: "declare ptr @patch_seq_variant_last(ptr)",
882            category: None,
883        },
884        RuntimeDecl {
885            decl: "declare ptr @patch_seq_variant_init(ptr)",
886            category: None,
887        },
888        RuntimeDecl {
889            decl: "declare ptr @patch_seq_make_variant_0(ptr)",
890            category: None,
891        },
892        RuntimeDecl {
893            decl: "declare ptr @patch_seq_make_variant_1(ptr)",
894            category: None,
895        },
896        RuntimeDecl {
897            decl: "declare ptr @patch_seq_make_variant_2(ptr)",
898            category: None,
899        },
900        RuntimeDecl {
901            decl: "declare ptr @patch_seq_make_variant_3(ptr)",
902            category: None,
903        },
904        RuntimeDecl {
905            decl: "declare ptr @patch_seq_make_variant_4(ptr)",
906            category: None,
907        },
908        RuntimeDecl {
909            decl: "declare ptr @patch_seq_make_variant_5(ptr)",
910            category: None,
911        },
912        RuntimeDecl {
913            decl: "declare ptr @patch_seq_make_variant_6(ptr)",
914            category: None,
915        },
916        RuntimeDecl {
917            decl: "declare ptr @patch_seq_make_variant_7(ptr)",
918            category: None,
919        },
920        RuntimeDecl {
921            decl: "declare ptr @patch_seq_make_variant_8(ptr)",
922            category: None,
923        },
924        RuntimeDecl {
925            decl: "declare ptr @patch_seq_make_variant_9(ptr)",
926            category: None,
927        },
928        RuntimeDecl {
929            decl: "declare ptr @patch_seq_make_variant_10(ptr)",
930            category: None,
931        },
932        RuntimeDecl {
933            decl: "declare ptr @patch_seq_make_variant_11(ptr)",
934            category: None,
935        },
936        RuntimeDecl {
937            decl: "declare ptr @patch_seq_make_variant_12(ptr)",
938            category: None,
939        },
940        RuntimeDecl {
941            decl: "declare ptr @patch_seq_unpack_variant(ptr, i64)",
942            category: None,
943        },
944        RuntimeDecl {
945            decl: "declare ptr @patch_seq_symbol_eq_cstr(ptr, ptr)",
946            category: None,
947        },
948        // Float operations
949        RuntimeDecl {
950            decl: "declare ptr @patch_seq_push_float(ptr, double)",
951            category: Some("; Float operations"),
952        },
953        RuntimeDecl {
954            decl: "declare ptr @patch_seq_f_add(ptr)",
955            category: None,
956        },
957        RuntimeDecl {
958            decl: "declare ptr @patch_seq_f_subtract(ptr)",
959            category: None,
960        },
961        RuntimeDecl {
962            decl: "declare ptr @patch_seq_f_multiply(ptr)",
963            category: None,
964        },
965        RuntimeDecl {
966            decl: "declare ptr @patch_seq_f_divide(ptr)",
967            category: None,
968        },
969        RuntimeDecl {
970            decl: "declare ptr @patch_seq_f_eq(ptr)",
971            category: None,
972        },
973        RuntimeDecl {
974            decl: "declare ptr @patch_seq_f_lt(ptr)",
975            category: None,
976        },
977        RuntimeDecl {
978            decl: "declare ptr @patch_seq_f_gt(ptr)",
979            category: None,
980        },
981        RuntimeDecl {
982            decl: "declare ptr @patch_seq_f_lte(ptr)",
983            category: None,
984        },
985        RuntimeDecl {
986            decl: "declare ptr @patch_seq_f_gte(ptr)",
987            category: None,
988        },
989        RuntimeDecl {
990            decl: "declare ptr @patch_seq_f_neq(ptr)",
991            category: None,
992        },
993        RuntimeDecl {
994            decl: "declare ptr @patch_seq_int_to_float(ptr)",
995            category: None,
996        },
997        RuntimeDecl {
998            decl: "declare ptr @patch_seq_float_to_int(ptr)",
999            category: None,
1000        },
1001        RuntimeDecl {
1002            decl: "declare ptr @patch_seq_float_to_string(ptr)",
1003            category: None,
1004        },
1005        RuntimeDecl {
1006            decl: "declare ptr @patch_seq_string_to_float(ptr)",
1007            category: None,
1008        },
1009        // Test framework operations
1010        RuntimeDecl {
1011            decl: "declare ptr @patch_seq_test_init(ptr)",
1012            category: Some("; Test framework operations"),
1013        },
1014        RuntimeDecl {
1015            decl: "declare ptr @patch_seq_test_finish(ptr)",
1016            category: None,
1017        },
1018        RuntimeDecl {
1019            decl: "declare ptr @patch_seq_test_has_failures(ptr)",
1020            category: None,
1021        },
1022        RuntimeDecl {
1023            decl: "declare ptr @patch_seq_test_assert(ptr)",
1024            category: None,
1025        },
1026        RuntimeDecl {
1027            decl: "declare ptr @patch_seq_test_assert_not(ptr)",
1028            category: None,
1029        },
1030        RuntimeDecl {
1031            decl: "declare ptr @patch_seq_test_assert_eq(ptr)",
1032            category: None,
1033        },
1034        RuntimeDecl {
1035            decl: "declare ptr @patch_seq_test_assert_eq_str(ptr)",
1036            category: None,
1037        },
1038        RuntimeDecl {
1039            decl: "declare ptr @patch_seq_test_fail(ptr)",
1040            category: None,
1041        },
1042        RuntimeDecl {
1043            decl: "declare ptr @patch_seq_test_pass_count(ptr)",
1044            category: None,
1045        },
1046        RuntimeDecl {
1047            decl: "declare ptr @patch_seq_test_fail_count(ptr)",
1048            category: None,
1049        },
1050        // Time operations
1051        RuntimeDecl {
1052            decl: "declare ptr @patch_seq_time_now(ptr)",
1053            category: Some("; Time operations"),
1054        },
1055        RuntimeDecl {
1056            decl: "declare ptr @patch_seq_time_nanos(ptr)",
1057            category: None,
1058        },
1059        RuntimeDecl {
1060            decl: "declare ptr @patch_seq_time_sleep_ms(ptr)",
1061            category: None,
1062        },
1063        // Stack introspection
1064        RuntimeDecl {
1065            decl: "declare ptr @patch_seq_stack_dump(ptr)",
1066            category: Some("; Stack introspection"),
1067        },
1068        // SON serialization
1069        RuntimeDecl {
1070            decl: "declare ptr @patch_seq_son_dump(ptr)",
1071            category: Some("; SON serialization"),
1072        },
1073        RuntimeDecl {
1074            decl: "declare ptr @patch_seq_son_dump_pretty(ptr)",
1075            category: None,
1076        },
1077        // Regex operations
1078        RuntimeDecl {
1079            decl: "declare ptr @patch_seq_regex_match(ptr)",
1080            category: Some("; Regex operations"),
1081        },
1082        RuntimeDecl {
1083            decl: "declare ptr @patch_seq_regex_find(ptr)",
1084            category: None,
1085        },
1086        RuntimeDecl {
1087            decl: "declare ptr @patch_seq_regex_find_all(ptr)",
1088            category: None,
1089        },
1090        RuntimeDecl {
1091            decl: "declare ptr @patch_seq_regex_replace(ptr)",
1092            category: None,
1093        },
1094        RuntimeDecl {
1095            decl: "declare ptr @patch_seq_regex_replace_all(ptr)",
1096            category: None,
1097        },
1098        RuntimeDecl {
1099            decl: "declare ptr @patch_seq_regex_captures(ptr)",
1100            category: None,
1101        },
1102        RuntimeDecl {
1103            decl: "declare ptr @patch_seq_regex_split(ptr)",
1104            category: None,
1105        },
1106        RuntimeDecl {
1107            decl: "declare ptr @patch_seq_regex_valid(ptr)",
1108            category: None,
1109        },
1110        // Compression operations
1111        RuntimeDecl {
1112            decl: "declare ptr @patch_seq_compress_gzip(ptr)",
1113            category: Some("; Compression operations"),
1114        },
1115        RuntimeDecl {
1116            decl: "declare ptr @patch_seq_compress_gzip_level(ptr)",
1117            category: None,
1118        },
1119        RuntimeDecl {
1120            decl: "declare ptr @patch_seq_compress_gunzip(ptr)",
1121            category: None,
1122        },
1123        RuntimeDecl {
1124            decl: "declare ptr @patch_seq_compress_zstd(ptr)",
1125            category: None,
1126        },
1127        RuntimeDecl {
1128            decl: "declare ptr @patch_seq_compress_zstd_level(ptr)",
1129            category: None,
1130        },
1131        RuntimeDecl {
1132            decl: "declare ptr @patch_seq_compress_unzstd(ptr)",
1133            category: None,
1134        },
1135        // Helpers for conditionals
1136        RuntimeDecl {
1137            decl: "declare i64 @patch_seq_peek_int_value(ptr)",
1138            category: Some("; Helpers for conditionals"),
1139        },
1140        RuntimeDecl {
1141            decl: "declare i1 @patch_seq_peek_bool_value(ptr)",
1142            category: None,
1143        },
1144        RuntimeDecl {
1145            decl: "declare ptr @patch_seq_pop_stack(ptr)",
1146            category: None,
1147        },
1148        // Tagged stack operations
1149        RuntimeDecl {
1150            decl: "declare ptr @seq_stack_new_default()",
1151            category: Some("; Tagged stack operations"),
1152        },
1153        RuntimeDecl {
1154            decl: "declare void @seq_stack_free(ptr)",
1155            category: None,
1156        },
1157        RuntimeDecl {
1158            decl: "declare ptr @seq_stack_base(ptr)",
1159            category: None,
1160        },
1161        RuntimeDecl {
1162            decl: "declare i64 @seq_stack_sp(ptr)",
1163            category: None,
1164        },
1165        RuntimeDecl {
1166            decl: "declare void @seq_stack_set_sp(ptr, i64)",
1167            category: None,
1168        },
1169        RuntimeDecl {
1170            decl: "declare void @seq_stack_grow(ptr, i64)",
1171            category: None,
1172        },
1173        RuntimeDecl {
1174            decl: "declare void @patch_seq_set_stack_base(ptr)",
1175            category: None,
1176        },
1177        // Report operations
1178        RuntimeDecl {
1179            decl: "declare void @patch_seq_report()",
1180            category: Some("; Report operations"),
1181        },
1182        RuntimeDecl {
1183            decl: "declare void @patch_seq_report_init(ptr, ptr, i64)",
1184            category: None,
1185        },
1186    ]
1187});
1188
1189/// Mapping from Seq word names to their C runtime symbol names.
1190/// This centralizes all the name transformations in one place:
1191/// - Symbolic operators (=, <, >) map to descriptive names (eq, lt, gt)
1192/// - Hyphens become underscores for C compatibility
1193/// - Special characters get escaped (?, +, ->)
1194/// - Reserved words get suffixes (drop -> drop_op)
1195pub static BUILTIN_SYMBOLS: LazyLock<HashMap<&'static str, &'static str>> = LazyLock::new(|| {
1196    HashMap::from([
1197        // I/O operations
1198        ("io.write", "patch_seq_write"),
1199        ("io.write-line", "patch_seq_write_line"),
1200        ("io.read-line", "patch_seq_read_line"),
1201        ("io.read-line+", "patch_seq_read_line_plus"),
1202        ("io.read-n", "patch_seq_read_n"),
1203        ("int->string", "patch_seq_int_to_string"),
1204        ("symbol->string", "patch_seq_symbol_to_string"),
1205        ("string->symbol", "patch_seq_string_to_symbol"),
1206        // Command-line arguments
1207        ("args.count", "patch_seq_arg_count"),
1208        ("args.at", "patch_seq_arg_at"),
1209        // Integer Arithmetic
1210        ("i.add", "patch_seq_add"),
1211        ("i.subtract", "patch_seq_subtract"),
1212        ("i.multiply", "patch_seq_multiply"),
1213        ("i.divide", "patch_seq_divide"),
1214        ("i.modulo", "patch_seq_modulo"),
1215        // Terse integer arithmetic aliases
1216        ("i.+", "patch_seq_add"),
1217        ("i.-", "patch_seq_subtract"),
1218        ("i.*", "patch_seq_multiply"),
1219        ("i./", "patch_seq_divide"),
1220        ("i.%", "patch_seq_modulo"),
1221        // Integer comparison (symbol form)
1222        ("i.=", "patch_seq_eq"),
1223        ("i.<", "patch_seq_lt"),
1224        ("i.>", "patch_seq_gt"),
1225        ("i.<=", "patch_seq_lte"),
1226        ("i.>=", "patch_seq_gte"),
1227        ("i.<>", "patch_seq_neq"),
1228        // Integer comparison (verbose form)
1229        ("i.eq", "patch_seq_eq"),
1230        ("i.lt", "patch_seq_lt"),
1231        ("i.gt", "patch_seq_gt"),
1232        ("i.lte", "patch_seq_lte"),
1233        ("i.gte", "patch_seq_gte"),
1234        ("i.neq", "patch_seq_neq"),
1235        // Boolean
1236        ("and", "patch_seq_and"),
1237        ("or", "patch_seq_or"),
1238        ("not", "patch_seq_not"),
1239        // Bitwise
1240        ("band", "patch_seq_band"),
1241        ("bor", "patch_seq_bor"),
1242        ("bxor", "patch_seq_bxor"),
1243        ("bnot", "patch_seq_bnot"),
1244        ("shl", "patch_seq_shl"),
1245        ("shr", "patch_seq_shr"),
1246        ("popcount", "patch_seq_popcount"),
1247        ("clz", "patch_seq_clz"),
1248        ("ctz", "patch_seq_ctz"),
1249        ("int-bits", "patch_seq_int_bits"),
1250        // Stack operations
1251        ("dup", "patch_seq_dup"),
1252        ("swap", "patch_seq_swap"),
1253        ("over", "patch_seq_over"),
1254        ("rot", "patch_seq_rot"),
1255        ("nip", "patch_seq_nip"),
1256        ("tuck", "patch_seq_tuck"),
1257        ("2dup", "patch_seq_2dup"),
1258        ("drop", "patch_seq_drop_op"),
1259        ("pick", "patch_seq_pick_op"),
1260        ("roll", "patch_seq_roll"),
1261        // Channel operations (errors are values, not crashes)
1262        ("chan.make", "patch_seq_make_channel"),
1263        ("chan.send", "patch_seq_chan_send"),
1264        ("chan.receive", "patch_seq_chan_receive"),
1265        ("chan.close", "patch_seq_close_channel"),
1266        ("chan.yield", "patch_seq_yield_strand"),
1267        // Quotation operations
1268        ("call", "patch_seq_call"),
1269        // Dataflow combinators
1270        ("dip", "patch_seq_dip"),
1271        ("keep", "patch_seq_keep"),
1272        ("bi", "patch_seq_bi"),
1273        ("strand.spawn", "patch_seq_spawn"),
1274        ("strand.weave", "patch_seq_weave"),
1275        ("strand.resume", "patch_seq_resume"),
1276        ("strand.weave-cancel", "patch_seq_weave_cancel"),
1277        ("yield", "patch_seq_yield"),
1278        ("cond", "patch_seq_cond"),
1279        // TCP operations
1280        ("tcp.listen", "patch_seq_tcp_listen"),
1281        ("tcp.accept", "patch_seq_tcp_accept"),
1282        ("tcp.read", "patch_seq_tcp_read"),
1283        ("tcp.write", "patch_seq_tcp_write"),
1284        ("tcp.close", "patch_seq_tcp_close"),
1285        // OS operations
1286        ("os.getenv", "patch_seq_getenv"),
1287        ("os.home-dir", "patch_seq_home_dir"),
1288        ("os.current-dir", "patch_seq_current_dir"),
1289        ("os.path-exists", "patch_seq_path_exists"),
1290        ("os.path-is-file", "patch_seq_path_is_file"),
1291        ("os.path-is-dir", "patch_seq_path_is_dir"),
1292        ("os.path-join", "patch_seq_path_join"),
1293        ("os.path-parent", "patch_seq_path_parent"),
1294        ("os.path-filename", "patch_seq_path_filename"),
1295        ("os.exit", "patch_seq_exit"),
1296        ("os.name", "patch_seq_os_name"),
1297        ("os.arch", "patch_seq_os_arch"),
1298        // Signal handling
1299        ("signal.trap", "patch_seq_signal_trap"),
1300        ("signal.received?", "patch_seq_signal_received"),
1301        ("signal.pending?", "patch_seq_signal_pending"),
1302        ("signal.default", "patch_seq_signal_default"),
1303        ("signal.ignore", "patch_seq_signal_ignore"),
1304        ("signal.clear", "patch_seq_signal_clear"),
1305        ("signal.SIGINT", "patch_seq_signal_sigint"),
1306        ("signal.SIGTERM", "patch_seq_signal_sigterm"),
1307        ("signal.SIGHUP", "patch_seq_signal_sighup"),
1308        ("signal.SIGPIPE", "patch_seq_signal_sigpipe"),
1309        ("signal.SIGUSR1", "patch_seq_signal_sigusr1"),
1310        ("signal.SIGUSR2", "patch_seq_signal_sigusr2"),
1311        ("signal.SIGCHLD", "patch_seq_signal_sigchld"),
1312        ("signal.SIGALRM", "patch_seq_signal_sigalrm"),
1313        ("signal.SIGCONT", "patch_seq_signal_sigcont"),
1314        // Terminal operations
1315        ("terminal.raw-mode", "patch_seq_terminal_raw_mode"),
1316        ("terminal.read-char", "patch_seq_terminal_read_char"),
1317        (
1318            "terminal.read-char?",
1319            "patch_seq_terminal_read_char_nonblock",
1320        ),
1321        ("terminal.width", "patch_seq_terminal_width"),
1322        ("terminal.height", "patch_seq_terminal_height"),
1323        ("terminal.flush", "patch_seq_terminal_flush"),
1324        // String operations
1325        ("string.concat", "patch_seq_string_concat"),
1326        ("string.length", "patch_seq_string_length"),
1327        ("string.byte-length", "patch_seq_string_byte_length"),
1328        ("string.char-at", "patch_seq_string_char_at"),
1329        ("string.substring", "patch_seq_string_substring"),
1330        ("char->string", "patch_seq_char_to_string"),
1331        ("string.find", "patch_seq_string_find"),
1332        ("string.split", "patch_seq_string_split"),
1333        ("string.contains", "patch_seq_string_contains"),
1334        ("string.starts-with", "patch_seq_string_starts_with"),
1335        ("string.empty?", "patch_seq_string_empty"),
1336        ("string.trim", "patch_seq_string_trim"),
1337        ("string.chomp", "patch_seq_string_chomp"),
1338        ("string.to-upper", "patch_seq_string_to_upper"),
1339        ("string.to-lower", "patch_seq_string_to_lower"),
1340        ("string.equal?", "patch_seq_string_equal"),
1341        ("string.join", "patch_seq_string_join"),
1342        ("string.json-escape", "patch_seq_json_escape"),
1343        ("string->int", "patch_seq_string_to_int"),
1344        // Encoding operations
1345        ("encoding.base64-encode", "patch_seq_base64_encode"),
1346        ("encoding.base64-decode", "patch_seq_base64_decode"),
1347        ("encoding.base64url-encode", "patch_seq_base64url_encode"),
1348        ("encoding.base64url-decode", "patch_seq_base64url_decode"),
1349        ("encoding.hex-encode", "patch_seq_hex_encode"),
1350        ("encoding.hex-decode", "patch_seq_hex_decode"),
1351        // Crypto operations
1352        ("crypto.sha256", "patch_seq_sha256"),
1353        ("crypto.hmac-sha256", "patch_seq_hmac_sha256"),
1354        ("crypto.constant-time-eq", "patch_seq_constant_time_eq"),
1355        ("crypto.random-bytes", "patch_seq_random_bytes"),
1356        ("crypto.random-int", "patch_seq_random_int"),
1357        ("crypto.uuid4", "patch_seq_uuid4"),
1358        ("crypto.aes-gcm-encrypt", "patch_seq_crypto_aes_gcm_encrypt"),
1359        ("crypto.aes-gcm-decrypt", "patch_seq_crypto_aes_gcm_decrypt"),
1360        ("crypto.pbkdf2-sha256", "patch_seq_crypto_pbkdf2_sha256"),
1361        ("crypto.ed25519-keypair", "patch_seq_crypto_ed25519_keypair"),
1362        ("crypto.ed25519-sign", "patch_seq_crypto_ed25519_sign"),
1363        ("crypto.ed25519-verify", "patch_seq_crypto_ed25519_verify"),
1364        // HTTP client operations
1365        ("http.get", "patch_seq_http_get"),
1366        ("http.post", "patch_seq_http_post"),
1367        ("http.put", "patch_seq_http_put"),
1368        ("http.delete", "patch_seq_http_delete"),
1369        // Regex operations
1370        ("regex.match?", "patch_seq_regex_match"),
1371        ("regex.find", "patch_seq_regex_find"),
1372        ("regex.find-all", "patch_seq_regex_find_all"),
1373        ("regex.replace", "patch_seq_regex_replace"),
1374        ("regex.replace-all", "patch_seq_regex_replace_all"),
1375        ("regex.captures", "patch_seq_regex_captures"),
1376        ("regex.split", "patch_seq_regex_split"),
1377        ("regex.valid?", "patch_seq_regex_valid"),
1378        // Compression operations
1379        ("compress.gzip", "patch_seq_compress_gzip"),
1380        ("compress.gzip-level", "patch_seq_compress_gzip_level"),
1381        ("compress.gunzip", "patch_seq_compress_gunzip"),
1382        ("compress.zstd", "patch_seq_compress_zstd"),
1383        ("compress.zstd-level", "patch_seq_compress_zstd_level"),
1384        ("compress.unzstd", "patch_seq_compress_unzstd"),
1385        // Symbol operations
1386        ("symbol.=", "patch_seq_symbol_equal"),
1387        // File operations
1388        ("file.slurp", "patch_seq_file_slurp"),
1389        ("file.exists?", "patch_seq_file_exists"),
1390        ("file.for-each-line+", "patch_seq_file_for_each_line_plus"),
1391        ("file.spit", "patch_seq_file_spit"),
1392        ("file.append", "patch_seq_file_append"),
1393        ("file.delete", "patch_seq_file_delete"),
1394        ("file.size", "patch_seq_file_size"),
1395        // Directory operations
1396        ("dir.exists?", "patch_seq_dir_exists"),
1397        ("dir.make", "patch_seq_dir_make"),
1398        ("dir.delete", "patch_seq_dir_delete"),
1399        ("dir.list", "patch_seq_dir_list"),
1400        // List operations
1401        ("list.make", "patch_seq_list_make"),
1402        ("list.push", "patch_seq_list_push"),
1403        ("list.get", "patch_seq_list_get"),
1404        ("list.set", "patch_seq_list_set"),
1405        ("list.map", "patch_seq_list_map"),
1406        ("list.filter", "patch_seq_list_filter"),
1407        ("list.fold", "patch_seq_list_fold"),
1408        ("list.each", "patch_seq_list_each"),
1409        ("list.length", "patch_seq_list_length"),
1410        ("list.empty?", "patch_seq_list_empty"),
1411        ("list.reverse", "patch_seq_list_reverse"),
1412        // Map operations
1413        ("map.make", "patch_seq_make_map"),
1414        ("map.get", "patch_seq_map_get"),
1415        ("map.set", "patch_seq_map_set"),
1416        ("map.has?", "patch_seq_map_has"),
1417        ("map.remove", "patch_seq_map_remove"),
1418        ("map.keys", "patch_seq_map_keys"),
1419        ("map.values", "patch_seq_map_values"),
1420        ("map.size", "patch_seq_map_size"),
1421        ("map.empty?", "patch_seq_map_empty"),
1422        ("map.each", "patch_seq_map_each"),
1423        ("map.fold", "patch_seq_map_fold"),
1424        // Variant operations
1425        ("variant.field-count", "patch_seq_variant_field_count"),
1426        ("variant.tag", "patch_seq_variant_tag"),
1427        ("variant.field-at", "patch_seq_variant_field_at"),
1428        ("variant.append", "patch_seq_variant_append"),
1429        ("variant.last", "patch_seq_variant_last"),
1430        ("variant.init", "patch_seq_variant_init"),
1431        ("variant.make-0", "patch_seq_make_variant_0"),
1432        ("variant.make-1", "patch_seq_make_variant_1"),
1433        ("variant.make-2", "patch_seq_make_variant_2"),
1434        ("variant.make-3", "patch_seq_make_variant_3"),
1435        ("variant.make-4", "patch_seq_make_variant_4"),
1436        ("variant.make-5", "patch_seq_make_variant_5"),
1437        ("variant.make-6", "patch_seq_make_variant_6"),
1438        ("variant.make-7", "patch_seq_make_variant_7"),
1439        ("variant.make-8", "patch_seq_make_variant_8"),
1440        ("variant.make-9", "patch_seq_make_variant_9"),
1441        ("variant.make-10", "patch_seq_make_variant_10"),
1442        ("variant.make-11", "patch_seq_make_variant_11"),
1443        ("variant.make-12", "patch_seq_make_variant_12"),
1444        // wrap-N aliases for dynamic variant construction (SON)
1445        ("wrap-0", "patch_seq_make_variant_0"),
1446        ("wrap-1", "patch_seq_make_variant_1"),
1447        ("wrap-2", "patch_seq_make_variant_2"),
1448        ("wrap-3", "patch_seq_make_variant_3"),
1449        ("wrap-4", "patch_seq_make_variant_4"),
1450        ("wrap-5", "patch_seq_make_variant_5"),
1451        ("wrap-6", "patch_seq_make_variant_6"),
1452        ("wrap-7", "patch_seq_make_variant_7"),
1453        ("wrap-8", "patch_seq_make_variant_8"),
1454        ("wrap-9", "patch_seq_make_variant_9"),
1455        ("wrap-10", "patch_seq_make_variant_10"),
1456        ("wrap-11", "patch_seq_make_variant_11"),
1457        ("wrap-12", "patch_seq_make_variant_12"),
1458        // Float arithmetic
1459        ("f.add", "patch_seq_f_add"),
1460        ("f.subtract", "patch_seq_f_subtract"),
1461        ("f.multiply", "patch_seq_f_multiply"),
1462        ("f.divide", "patch_seq_f_divide"),
1463        // Terse float arithmetic aliases
1464        ("f.+", "patch_seq_f_add"),
1465        ("f.-", "patch_seq_f_subtract"),
1466        ("f.*", "patch_seq_f_multiply"),
1467        ("f./", "patch_seq_f_divide"),
1468        // Float comparison (symbol form)
1469        ("f.=", "patch_seq_f_eq"),
1470        ("f.<", "patch_seq_f_lt"),
1471        ("f.>", "patch_seq_f_gt"),
1472        ("f.<=", "patch_seq_f_lte"),
1473        ("f.>=", "patch_seq_f_gte"),
1474        ("f.<>", "patch_seq_f_neq"),
1475        // Float comparison (verbose form)
1476        ("f.eq", "patch_seq_f_eq"),
1477        ("f.lt", "patch_seq_f_lt"),
1478        ("f.gt", "patch_seq_f_gt"),
1479        ("f.lte", "patch_seq_f_lte"),
1480        ("f.gte", "patch_seq_f_gte"),
1481        ("f.neq", "patch_seq_f_neq"),
1482        // Float type conversions
1483        ("int->float", "patch_seq_int_to_float"),
1484        ("float->int", "patch_seq_float_to_int"),
1485        ("float->string", "patch_seq_float_to_string"),
1486        ("string->float", "patch_seq_string_to_float"),
1487        // Test framework operations
1488        ("test.init", "patch_seq_test_init"),
1489        ("test.finish", "patch_seq_test_finish"),
1490        ("test.has-failures", "patch_seq_test_has_failures"),
1491        ("test.assert", "patch_seq_test_assert"),
1492        ("test.assert-not", "patch_seq_test_assert_not"),
1493        ("test.assert-eq", "patch_seq_test_assert_eq"),
1494        ("test.assert-eq-str", "patch_seq_test_assert_eq_str"),
1495        ("test.fail", "patch_seq_test_fail"),
1496        ("test.pass-count", "patch_seq_test_pass_count"),
1497        ("test.fail-count", "patch_seq_test_fail_count"),
1498        // Time operations
1499        ("time.now", "patch_seq_time_now"),
1500        ("time.nanos", "patch_seq_time_nanos"),
1501        ("time.sleep-ms", "patch_seq_time_sleep_ms"),
1502        // SON serialization
1503        ("son.dump", "patch_seq_son_dump"),
1504        ("son.dump-pretty", "patch_seq_son_dump_pretty"),
1505        // Stack introspection
1506        ("stack.dump", "patch_seq_stack_dump"),
1507    ])
1508});
1509
1510/// Emit all runtime function declarations to the IR string.
1511pub fn emit_runtime_decls(ir: &mut String) -> Result<(), CodeGenError> {
1512    for decl in RUNTIME_DECLARATIONS.iter() {
1513        if let Some(cat) = decl.category {
1514            writeln!(ir, "{}", cat)?;
1515        }
1516        writeln!(ir, "{}", decl.decl)?;
1517    }
1518    writeln!(ir)?;
1519    Ok(())
1520}