Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_backend_c_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
36#include <gtest/gtest.h>
37
38#include <atomic>
39#include <chrono>
40#include <climits>
41#include <cstdlib>
42#include <filesystem>
43#include <fstream>
44#include <memory>
45#include <string>
46#include <vector>
47#if defined(_WIN32)
48# include <process.h>
49#else
50# include <unistd.h>
51#endif
52
53#include <Bytecode.H>
55#include <Compiler_Backend_C.H>
56#include <Compiler_Dataflow.H>
57#include <Compiler_HIR.H>
58#include <Compiler_IR.H>
59#include <Compiler_Parser.H>
60#include <Compiler_Typed_Sema.H>
61#include <Interpreter_Runtime.H>
62
63using namespace Aleph;
64
65namespace
66{
67 struct Command_Result
68 {
69 int status = -1;
70 std::string stdout_text;
71 std::string stderr_text;
72 };
73
75 make_exit_block()
76 {
78 block.id = 1;
79 block.label = "exit";
80 block.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
81 return block;
82 }
83
85 make_branch_ir()
86 {
87 Compiler_IR_Function function;
88 function.id = 0;
89 function.name = "choose";
90 function.entry_block = 0;
91 function.exit_block = 1;
92 function.next_value_id = 5;
93
95 flag.id = 0;
96 flag.kind = Compiler_IR_Slot_Kind::Parameter;
97 flag.name = "flag";
98 function.local_slots.append(flag);
99
101 value.id = 1;
102 value.kind = Compiler_IR_Slot_Kind::Local;
103 value.name = "value";
104 function.local_slots.append(value);
105
106 Compiler_IR_Block entry;
107 entry.id = 0;
108 entry.label = "entry";
109
111 zero.kind = Compiler_IR_Instruction_Kind::Constant;
112 zero.result_id = 1;
113 zero.text = "0";
114 entry.instructions.append(zero);
115
116 Compiler_IR_Instruction init_store;
117 init_store.kind = Compiler_IR_Instruction_Kind::Store;
118 init_store.local_slot_id = 1;
119 init_store.operands.append(1);
120 entry.instructions.append(init_store);
121
122 Compiler_IR_Instruction load_flag;
123 load_flag.kind = Compiler_IR_Instruction_Kind::Load;
124 load_flag.result_id = 2;
125 load_flag.local_slot_id = 0;
126 entry.instructions.append(load_flag);
127
128 entry.terminator.kind = Compiler_IR_Terminator_Kind::Branch;
129 entry.terminator.condition_value = 2;
132
133 auto exit = make_exit_block();
134 exit.predecessors.append(3);
135
136 Compiler_IR_Block then_block;
137 then_block.id = 2;
138 then_block.label = "if.then.0";
139
141 one.kind = Compiler_IR_Instruction_Kind::Constant;
142 one.result_id = 3;
143 one.text = "1";
144 then_block.instructions.append(one);
145
146 Compiler_IR_Instruction then_store;
147 then_store.kind = Compiler_IR_Instruction_Kind::Store;
148 then_store.local_slot_id = 1;
149 then_store.operands.append(3);
150 then_block.instructions.append(then_store);
151
152 then_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
153 then_block.terminator.successors.append(3);
154 then_block.predecessors.append(0);
155
156 Compiler_IR_Block join_block;
157 join_block.id = 3;
158 join_block.label = "if.end.0";
159
160 Compiler_IR_Instruction load_value;
161 load_value.kind = Compiler_IR_Instruction_Kind::Load;
162 load_value.result_id = 5;
163 load_value.local_slot_id = 1;
164 join_block.instructions.append(load_value);
165
166 join_block.terminator.kind = Compiler_IR_Terminator_Kind::Return;
167 join_block.terminator.return_value = 5;
168 join_block.terminator.successors.append(1);
169 join_block.predecessors.append(2);
170 join_block.predecessors.append(4);
171
172 Compiler_IR_Block else_block;
173 else_block.id = 4;
174 else_block.label = "if.else.0";
175
177 two.kind = Compiler_IR_Instruction_Kind::Constant;
178 two.result_id = 4;
179 two.text = "2";
180 else_block.instructions.append(two);
181
182 Compiler_IR_Instruction else_store;
183 else_store.kind = Compiler_IR_Instruction_Kind::Store;
184 else_store.local_slot_id = 1;
185 else_store.operands.append(4);
186 else_block.instructions.append(else_store);
187
188 else_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
189 else_block.terminator.successors.append(3);
190 else_block.predecessors.append(0);
191
192 function.blocks.append(entry);
193 function.blocks.append(exit);
194 function.blocks.append(then_block);
195 function.blocks.append(join_block);
196 function.blocks.append(else_block);
197 return function;
198 }
199
201 make_linear_ir()
202 {
203 Compiler_IR_Function function;
204 function.id = 0;
205 function.name = "add_one";
206 function.entry_block = 0;
207 function.exit_block = 1;
208 function.next_value_id = 4;
209
210 Compiler_IR_Slot param;
211 param.id = 0;
212 param.kind = Compiler_IR_Slot_Kind::Parameter;
213 param.name = "x";
214 function.local_slots.append(param);
215
216 Compiler_IR_Slot local;
217 local.id = 1;
218 local.kind = Compiler_IR_Slot_Kind::Local;
219 local.name = "tmp";
220 function.local_slots.append(local);
221
222 Compiler_IR_Block entry;
223 entry.id = 0;
224 entry.label = "entry";
225
227 constant.kind = Compiler_IR_Instruction_Kind::Constant;
228 constant.result_id = 1;
229 constant.text = "1";
230 entry.instructions.append(constant);
231
233 store.kind = Compiler_IR_Instruction_Kind::Store;
234 store.local_slot_id = 1;
235 store.operands.append(1);
236 entry.instructions.append(store);
237
239 load_x.kind = Compiler_IR_Instruction_Kind::Load;
240 load_x.result_id = 2;
241 load_x.local_slot_id = 0;
242 entry.instructions.append(load_x);
243
245 load_tmp.kind = Compiler_IR_Instruction_Kind::Load;
246 load_tmp.result_id = 3;
247 load_tmp.local_slot_id = 1;
248 entry.instructions.append(load_tmp);
249
251 add.kind = Compiler_IR_Instruction_Kind::Binary;
252 add.result_id = 4;
253 add.op = Compiler_Operator_Kind::Plus;
254 add.operands.append(2);
255 add.operands.append(3);
256 entry.instructions.append(add);
257
258 entry.terminator.kind = Compiler_IR_Terminator_Kind::Return;
259 entry.terminator.return_value = 4;
261
262 auto exit = make_exit_block();
263 exit.predecessors.append(0);
264
265 function.blocks.append(entry);
266 function.blocks.append(exit);
267 return function;
268 }
269
271 make_module_with_top_level_call()
272 {
273 Compiler_IR_Module module;
274
275 Compiler_IR_Slot global;
276 global.id = 0;
277 global.kind = Compiler_IR_Slot_Kind::Global;
278 global.name = "answer";
279 module.global_slots.append(global);
280
281 auto * function = new Compiler_IR_Function(make_linear_ir());
282 module.functions.append(function);
283
284 auto * top = new Compiler_IR_Function();
285 top->id = compiler_ir_invalid_id();
286 top->name = "<top-level>";
287 top->entry_block = 0;
288 top->exit_block = 1;
289 top->next_value_id = 3;
290
291 Compiler_IR_Block entry;
292 entry.id = 0;
293 entry.label = "entry";
294
295 Compiler_IR_Instruction function_ref;
296 function_ref.kind = Compiler_IR_Instruction_Kind::Function_Ref;
297 function_ref.result_id = 1;
298 function_ref.function_id = 0;
299 entry.instructions.append(function_ref);
300
302 constant.kind = Compiler_IR_Instruction_Kind::Constant;
303 constant.result_id = 2;
304 constant.text = "41";
305 entry.instructions.append(constant);
306
308 call.kind = Compiler_IR_Instruction_Kind::Call;
309 call.result_id = 3;
310 call.operands.append(1);
311 call.operands.append(2);
312 entry.instructions.append(call);
313
314 Compiler_IR_Instruction store_global;
315 store_global.kind = Compiler_IR_Instruction_Kind::Store;
316 store_global.global_slot_id = 0;
317 store_global.operands.append(3);
318 entry.instructions.append(store_global);
319
320 entry.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
322
323 auto exit = make_exit_block();
324 exit.predecessors.append(0);
325
326 top->blocks.append(entry);
327 top->blocks.append(exit);
328 module.top_level = top;
329 return module;
330 }
331
333 make_divide_by_zero_top_level_module()
334 {
335 Compiler_IR_Module module;
336
337 auto * top = new Compiler_IR_Function();
338 top->id = compiler_ir_invalid_id();
339 top->name = "<top-level>";
340 top->entry_block = 0;
341 top->exit_block = 1;
342 top->next_value_id = 3;
343
344 Compiler_IR_Block entry;
345 entry.id = 0;
346 entry.label = "entry";
347
349 lhs.kind = Compiler_IR_Instruction_Kind::Constant;
350 lhs.result_id = 1;
351 lhs.text = "5";
352 entry.instructions.append(lhs);
353
355 rhs.kind = Compiler_IR_Instruction_Kind::Constant;
356 rhs.result_id = 2;
357 rhs.text = "0";
358 entry.instructions.append(rhs);
359
361 div.kind = Compiler_IR_Instruction_Kind::Binary;
362 div.result_id = 3;
363 div.op = Compiler_Operator_Kind::Slash;
364 div.operands.append(1);
365 div.operands.append(2);
366 entry.instructions.append(div);
367
368 entry.terminator.kind = Compiler_IR_Terminator_Kind::Return;
369 entry.terminator.return_value = 3;
371
372 auto exit = make_exit_block();
373 exit.predecessors.append(0);
374
375 top->blocks.append(entry);
376 top->blocks.append(exit);
377 module.top_level = top;
378 return module;
379 }
380
381 std::string
382 read_text(const std::filesystem::path & path)
383 {
384 std::ifstream in(path);
385 return std::string((std::istreambuf_iterator<char>(in)),
386 std::istreambuf_iterator<char>());
387 }
388
389 void
390 write_text(const std::filesystem::path & path,
391 const std::string & text)
392 {
393 std::ofstream out(path);
394 out << text;
395 }
396
397 std::string
398 shell_quote(const std::string & text)
399 {
400 std::string quoted = "'";
401 for (const char ch : text)
402 {
403 if (ch == '\'')
404 quoted += "'\\''";
405 else
406 quoted.push_back(ch);
407 }
408 quoted.push_back('\'');
409 return quoted;
410 }
411
412 bool
413 host_has_cc()
414 {
415 return std::system("command -v cc >/dev/null 2>&1") == 0;
416 }
417
419 long long process_id() noexcept
420 {
421#if defined(_WIN32)
422 return static_cast<long long>(_getpid());
423#else
424 return static_cast<long long>(getpid());
425#endif
426 }
427
428 std::filesystem::path
429 unique_temp_path(const std::string & stem,
430 const std::string & extension)
431 {
432 // A steady_clock tick alone is unique *within* a process, but not
433 // across the several processes that actually run this suite (each
434 // TEST() is its own ctest process, and CI runs ctest with
435 // --parallel) -- two processes' calls landing in the same clock
436 // tick produce the identical name and race on the same file.
437 // Mixing in the process id and a per-process counter closes that
438 // gap regardless of clock resolution or call count.
439 static std::atomic<unsigned long long> counter{0};
440 const auto nonce = std::to_string(
441 std::chrono::steady_clock::now().time_since_epoch().count()) +
442 "_" + std::to_string(process_id()) +
443 "_" + std::to_string(counter++);
444 return std::filesystem::temp_directory_path()
445 / (stem + "_" + nonce + extension);
446 }
447
448 Command_Result
449 run_command(const std::string & command)
450 {
451 Command_Result result;
452 const auto stdout_path = unique_temp_path("aleph_backend_stdout", ".txt");
453 const auto stderr_path = unique_temp_path("aleph_backend_stderr", ".txt");
454 const auto full_command = command
455 + " >" + shell_quote(stdout_path.string())
456 + " 2>" + shell_quote(stderr_path.string());
457
458 result.status = std::system(full_command.c_str());
459 result.stdout_text = read_text(stdout_path);
460 result.stderr_text = read_text(stderr_path);
461 std::error_code ignore_error;
462 std::filesystem::remove(stdout_path, ignore_error);
463 std::filesystem::remove(stderr_path, ignore_error);
464 return result;
465 }
466
467 Command_Result
468 compile_and_run(const Compiler_C_Backend_Emission & emission,
469 const std::string & extra_c = "")
470 {
471 const auto source_path = unique_temp_path("aleph_backend_generated", ".c");
472 const auto exe_path = unique_temp_path("aleph_backend_generated", ".out");
473
474 write_text(source_path, emission.source + "\n" + extra_c);
475 const auto compile = run_command(
476 "cc -std=c11 -Wall -Wextra -pedantic "
477 + shell_quote(source_path.string())
478 + " -o "
479 + shell_quote(exe_path.string()));
480 if (compile.status != 0)
481 return compile;
482
483 const auto executed = run_command(shell_quote(exe_path.string()));
484 std::error_code ignore_error;
485 std::filesystem::remove(source_path, ignore_error);
486 std::filesystem::remove(exe_path, ignore_error);
487 return executed;
488 }
489}
490
491TEST(CompilerBackendC, EmitsReadableGotoBasedC)
492{
493 Compiler_IR_Module module;
494 auto source = make_branch_ir();
495 module.functions.append(&source);
496
498 options.module_name = "branch-demo";
499
500 const auto emitted = compiler_emit_c_module(&module, options);
501 ASSERT_TRUE(emitted.valid);
502 EXPECT_NE(emitted.source.find("goto " + emitted.c_prefix + "_B3"), std::string::npos);
503 EXPECT_NE(emitted.source.find("if ("), std::string::npos);
504 EXPECT_NE(emitted.source.find("static " + emitted.value_type_name), std::string::npos);
505 EXPECT_NE(emitted.source.find(emitted.function_symbols.access(0)), std::string::npos);
506 EXPECT_NE(emitted.source.find("choose"), std::string::npos);
507}
508
509TEST(CompilerBackendC, StandaloneGeneratedCCompilesAndRunsTopLevel)
510{
511 if (not host_has_cc())
512 GTEST_SKIP() << "No se encontró `cc` en el host";
513
514 auto module = make_module_with_top_level_call();
515
517 options.module_name = "top_level_demo";
518 options.emit_main = true;
519
520 const auto emitted = compiler_emit_c_module(&module, options);
521 ASSERT_TRUE(emitted.valid);
522
523 const auto executed = compile_and_run(emitted);
524 EXPECT_EQ(executed.status, 0);
525 EXPECT_NE(executed.stdout_text.find("Result = Unit"), std::string::npos);
526 EXPECT_NE(executed.stdout_text.find("g0 answer = Int(42)"), std::string::npos);
527 EXPECT_TRUE(executed.stderr_text.empty());
528
529 delete module.functions.access(0);
530 delete module.top_level;
531}
532
533TEST(CompilerBackendC, LibraryStyleEmissionCanCallFunctionsDirectly)
534{
535 if (not host_has_cc())
536 GTEST_SKIP() << "No se encontró `cc` en el host";
537
538 Compiler_IR_Module module;
539 auto source = make_branch_ir();
540 module.functions.append(&source);
541
543 options.module_name = "direct_call_demo";
544 const auto emitted = compiler_emit_c_module(&module, options);
545 ASSERT_TRUE(emitted.valid);
546
547 const auto harness =
548 "\nint main(void)\n"
549 "{\n"
550 " " + emitted.module_struct_name + " module;\n"
551 " " + emitted.module_init_symbol + "(&module);\n"
552 " " + emitted.value_type_name + " args[1];\n"
553 " args[0] = " + emitted.c_prefix + "_make_bool(false);\n"
554 " " + emitted.value_type_name + " result = "
555 + emitted.function_symbols.access(0) + "(&module, args, 1u);\n"
556 " " + emitted.print_value_symbol + "(stdout, result);\n"
557 " fputc('\\n', stdout);\n"
558 " return 0;\n"
559 "}\n";
560
561 const auto executed = compile_and_run(emitted, harness);
562 EXPECT_EQ(executed.status, 0);
563 EXPECT_EQ(executed.stdout_text, "Int(2)\n");
564 EXPECT_TRUE(executed.stderr_text.empty());
565}
566
567TEST(CompilerBackendC, GeneratedCRuntimeChecksSurfaceErrors)
568{
569 if (not host_has_cc())
570 GTEST_SKIP() << "No se encontró `cc` en el host";
571
572 auto module = make_divide_by_zero_top_level_module();
573
575 options.module_name = "panic_demo";
576 options.emit_main = true;
577
578 const auto emitted = compiler_emit_c_module(&module, options);
579 ASSERT_TRUE(emitted.valid);
580
581 const auto executed = compile_and_run(emitted);
582 EXPECT_NE(executed.status, 0);
583 EXPECT_NE(executed.stderr_text.find("division by zero"), std::string::npos);
584
585 delete module.top_level;
586}
587
588namespace
589{
591 struct Engine_Outcome
592 {
593 bool overflowed = false;
594 std::string globals;
595 };
596
598 struct Lowered_Program
599 {
601 Diagnostic_Engine dx{sm};
602 Compiler_Ast_Context ast{1 << 16};
603 Compiler_HIR_Context hir{1 << 16};
604 Compiler_IR_Context ir{1 << 16};
606 const Compiler_HIR_Module * hir_module = nullptr;
607 const Compiler_IR_Module * ir_module = nullptr;
608
609 Compiler_IR_Module optimized;
610 std::vector<std::unique_ptr<Compiler_IR_Function>> optimized_functions;
611
612 explicit Lowered_Program(const std::string & source)
613 {
614 const auto file_id = sm.add_virtual_file("main.aw", source);
615 Compiler_Parser parser(ast, sm, file_id, &dx);
616 const auto * module = parser.parse_module();
617 typed.analyze_module(module);
618 EXPECT_FALSE(dx.has_errors()) << source;
619
620 Compiler_HIR_Lowering hir_lowering(hir, typed);
621 hir_module = hir_lowering.lower_module(module);
622 Compiler_IR_Lowering ir_lowering(ir, &typed.type_context());
623 ir_module = ir_lowering.lower_module(hir_module);
624
625 // constant folding and dead-code elimination, function by function
626 optimized = *ir_module;
627 auto optimize = [this](const Compiler_IR_Function * function)
628 {
629 optimized_functions.push_back(std::make_unique<Compiler_IR_Function>(
630 eliminate_dead_code(*function).function));
631 return optimized_functions.back().get();
632 };
633 for (size_t i = 0; i < optimized.functions.size(); ++i)
634 optimized.functions(i) = optimize(ir_module->functions(i));
635 if (ir_module->top_level != nullptr)
636 optimized.top_level = optimize(ir_module->top_level);
637 }
638
639 Engine_Outcome run_hir()
640 {
641 Interpreter_Runtime runtime(&dx);
642 const auto evaluated = runtime.evaluate_module(hir_module);
643 if (not evaluated.ok())
644 return {evaluated.error.code == "RUN013"
645 and evaluated.error.message == "integer overflow",
646 evaluated.error.code + ": " + evaluated.error.message};
647 return {false, interpreter_dump_globals(runtime)};
648 }
649
650 static Engine_Outcome run_vm(const Compiler_IR_Module & module)
651 {
652 Compiler_Bytecode_Context bytecode(1 << 16);
653 Compiler_Bytecode_Lowering lowering(bytecode);
654 Compiler_Bytecode_VM vm(*lowering.lower_module(&module));
655 const auto executed = vm.run_top_level();
656 if (not executed.ok())
657 return {executed.error.code == "BCV023"
658 and executed.error.message == "integer overflow",
659 executed.error.code + ": " + executed.error.message};
660 return {false, vm.dump_globals()};
661 }
662
663 static Engine_Outcome run_c(const Compiler_IR_Module & module)
664 {
666 options.module_name = "integer_case";
667 options.emit_main = true;
668 const auto emitted = compiler_emit_c_module(&module, options);
669 EXPECT_TRUE(emitted.valid);
670 const auto executed = compile_and_run(emitted);
671 if (executed.status != 0)
672 return {executed.stderr_text.find("integer overflow") != std::string::npos,
673 executed.stderr_text};
674 return {false, executed.stdout_text};
675 }
676 };
677}
678
679// The integer arithmetic of the language is the same in every engine: the
680// HIR interpreter, the bytecode VM and the generated C, with and without the
681// optimizer. A result that does not fit in 64 bits is a runtime error, even
682// when nobody reads it; the engines used to compute it with the overflow of
683// C++, undefined behaviour.
684TEST(CompilerBackendC, IntegerBoundariesAreTheSameInEveryEngine)
685{
686 struct Case
687 {
688 const char * expression;
689 bool overflows;
690 long long value;
691 };
692
693 const Case cases[] = {
694 {"9223372036854775807 + 1", true, 0},
695 {"9223372036854775807 + 0", false, LLONG_MAX},
696 {"-9223372036854775807 - 1", false, LLONG_MIN},
697 {"-9223372036854775807 - 2", true, 0},
698 {"9223372036854775807 - -1", true, 0},
699 {"4611686018427387904 * 2", true, 0},
700 {"3037000499 * 3037000499", false, 3037000499LL * 3037000499LL},
701 {"(-9223372036854775807 - 1) * -1", true, 0},
702 {"(-9223372036854775807 - 1) * 1", false, LLONG_MIN},
703 {"(-9223372036854775807 - 1) / -1", true, 0},
704 {"(-9223372036854775807 - 1) / 1", false, LLONG_MIN},
705 {"(-9223372036854775807 - 1) % -1", false, 0},
706 {"-(-9223372036854775807 - 1)", true, 0},
707 {"-9223372036854775807", false, -LLONG_MAX},
708 {"-7 / 2", false, -3},
709 {"-7 % 2", false, -1},
710 };
711
712 const bool with_c = host_has_cc();
713 for (const auto & c : cases)
714 for (const bool dead : {false, true})
715 {
716 // a dead value: the optimizer may not remove the operation
717 const std::string source = dead
718 ? std::string("fn f() {\n let unused = ") + c.expression
719 + ";\n return 0;\n}\nlet value = f();\n"
720 : std::string("let value = ") + c.expression + ";\n";
721 const std::string expected
722 = "value = Int(" + std::to_string(dead ? 0 : c.value) + ")";
723 Lowered_Program program(source);
724
725 std::vector<std::pair<std::string, Engine_Outcome>> outcomes;
726 outcomes.emplace_back("HIR", program.run_hir());
727 outcomes.emplace_back("VM", Lowered_Program::run_vm(*program.ir_module));
728 outcomes.emplace_back("optimized VM", Lowered_Program::run_vm(program.optimized));
729 if (with_c)
730 {
731 outcomes.emplace_back("C", Lowered_Program::run_c(*program.ir_module));
732 outcomes.emplace_back("optimized C", Lowered_Program::run_c(program.optimized));
733 }
734
735 for (const auto & [engine, outcome] : outcomes)
736 {
737 EXPECT_EQ(outcome.overflowed, c.overflows)
738 << engine << ":\n" << source << outcome.globals;
739 if (not c.overflows)
740 EXPECT_NE(outcome.globals.find(expected), std::string::npos)
741 << engine << ":\n" << source << outcome.globals;
742 }
743 }
744}
Reusable bytecode format and lowering from Compiler_IR_Model.H.
Simple portable VM for executing the register bytecode in Bytecode.H.
Portable C backend that emits standalone C from Compiler_IR_Model.H.
Reusable dataflow analyses and dead-code elimination over Compiler_IR_Model.H.
Compatibility umbrella for the reusable HIR model and the MVP lowering.
Compatibility umbrella for the reusable IR model and the MVP lowering.
Recursive-descent parser for the compiler-support MVP grammar.
Typed semantic pass connecting the AST, the name pass, and type constraints.
Reusable runtime and structured evaluator for HIR-based interpreters.
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Arena-backed ownership context for AST nodes.
Arena-backed ownership context for bytecode functions and modules.
Definition Bytecode.H:254
Lowers explicit IR into register-based bytecode.
Definition Bytecode.H:581
Small portable VM for executing one Compiler_Bytecode_Module.
Arena-backed ownership context for HIR nodes.
Lowers the MVP typed AST into HIR.
Arena-backed ownership context for IR nodes.
Lowers typed HIR into explicit-value IR.
Recursive-descent parser that produces an AST in Compiler_Ast_Context.
Inference-oriented semantic pass for the MVP compiler front-end.
Diagnostic accumulator and renderer.
T & append()
Allocate a new entry to the end of array.
Structured runtime for executing typed HIR.
Stores source files and resolves offsets into human-readable data.
Definition ah-source.H:184
Source_File_Id add_virtual_file(const std::string &name, const std::string &text)
Registers an in-memory source file.
Definition ah-source.H:258
Minimal std::expected-style result type for C++20.
#define TEST(name)
std::string shell_quote(const std::string &s)
const char * extension(Frame_Output_Format format) noexcept
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Definition SSA.H:636
Main namespace for Aleph-w library functions.
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
Definition ahDefs.C:125
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
and
Check uniqueness with explicit hash + equality functors.
Compiler_Dead_Code_Elimination_Result eliminate_dead_code(const Compiler_IR_Function &function)
Eliminates unreachable blocks, dead pure instructions, dead local stores, and folds constant-conditio...
std::string interpreter_dump_globals(const Interpreter_Runtime &runtime)
Dumps the globals currently installed in one runtime.
Compiler_C_Backend_Emission compiler_emit_c_module(const Compiler_IR_Module *module, const Compiler_C_Backend_Options &options={}, const Compiler_Type_Context *types=nullptr)
Convenience wrapper that emits one IR module to portable C.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
double add(double a, double b)
double div(double a, double b)
static struct argp_option options[]
Definition ntreepic.C:1890
Result of one C backend emission request.
std::string source
Final generated C translation unit.
Options that control one C backend emission request.
std::string module_name
Prefix used to derive stable C identifiers.
Compiler_IR_Function function
Optimized function copy.
One basic block of IR instructions.
std::string label
Deterministic debug label.
Compiler_IR_Terminator terminator
Explicit block terminator.
DynArray< Compiler_IR_Block_Id > predecessors
Predecessor block ids.
DynArray< Compiler_IR_Instruction > instructions
Linear instruction list.
Compiler_IR_Block_Id id
Stable block id.
Lowered IR for one function or top-level body.
Compiler_IR_Value_Id next_value_id
Next value id to allocate while lowering.
DynArray< Compiler_IR_Block > blocks
Basic blocks in deterministic id order.
std::string name
Debug or source-level name.
Compiler_IR_Block_Id exit_block
Canonical exit block.
Compiler_IR_Block_Id entry_block
Canonical entry block.
DynArray< Compiler_IR_Slot > local_slots
Parameter/local slots in stable order.
Compiler_IR_Function_Id id
Stable function id within the module.
One instruction producing an optional explicit result value.
std::string text
Constant spelling or debug payload.
Compiler_IR_Instruction_Kind kind
Instruction category.
Compiler_IR_Value_Id result_id
Produced value id, or 0 for void instructions.
DynArray< Compiler_IR_Value_Id > operands
Input values in deterministic order.
Compiler_IR_Local_Slot_Id local_slot_id
Referenced local/parameter slot, when relevant.
Compiler_IR_Function_Id function_id
Referenced function id, when relevant.
Compiler_IR_Global_Slot_Id global_slot_id
Referenced global slot, when relevant.
Lowered IR module with shared global slots and functions.
DynArray< Compiler_IR_Function * > functions
Lowered non-top-level functions.
Compiler_IR_Function * top_level
Optional top-level body lowered as a function.
One storage slot used by the IR.
std::string name
Debug name of the slot.
size_t id
Stable slot id within its storage space.
Compiler_IR_Slot_Kind kind
Slot storage category.
Compiler_IR_Terminator_Kind kind
Terminator category.
DynArray< Compiler_IR_Block_Id > successors
Successor blocks in deterministic order.
Compiler_IR_Value_Id condition_value
Branch condition value, if any.
Compiler_IR_Value_Id return_value
Return value, if any.
Compiler_SSA_Block_Id id
Dense SSA block id.
Definition SSA.H:163
std::string label
Deterministic debug label.
Definition SSA.H:165
Compiler_SSA_Terminator terminator
Explicit block terminator.
Definition SSA.H:169
Compiler_IR_Terminator_Kind kind
Terminator category.
Definition SSA.H:153
static long counter
Definition test-splice.C:40
static mt19937 engine