Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Backend_C.H
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
58#ifndef COMPILER_BACKEND_C_H
59#define COMPILER_BACKEND_C_H
60
61#include <cctype>
62#include <climits>
63#include <cstdlib>
64#include <sstream>
65#include <string>
66#include <string_view>
67#include <utility>
68
69#include <Compiler_IR_Model.H>
70#include <ah-errors.H>
71#include <tpl_dynArray.H>
72
73namespace Aleph {
76{
77 std::string module_name = "module";
79 = false;
81 = true;
82};
83
109
110namespace Compiler_Backend_C_Detail {
111inline std::string sanitize_identifier(std::string_view text)
112{
113 std::string result;
114 result.reserve(text.size() + 8);
115 for (const unsigned char ch : text)
116 {
117 if (std::isalnum(ch))
118 result.push_back(static_cast<char>(std::tolower(ch)));
119 else
120 result.push_back('_');
121 }
122
123 while (not result.empty() and result.front() == '_')
124 result.erase(result.begin());
125 while (not result.empty() and result.back() == '_')
126 result.pop_back();
127 while (result.find("__") != std::string::npos)
128 result.replace(result.find("__"), 2, "_");
129
130 if (result.empty())
131 result = "item";
132 if (std::isdigit(static_cast<unsigned char>(result.front())))
133 result.insert(result.begin(), '_');
134 return result;
135}
136
137inline std::string upper_identifier(std::string_view text)
138{
139 std::string result = sanitize_identifier(text);
140 for (char &ch : result)
141 ch = static_cast<char>(std::toupper(static_cast<unsigned char>(ch)));
142 return result;
143}
144
145inline std::string c_string_literal(std::string_view text)
146{
147 std::string result;
148 result.reserve(text.size() + 2);
149 result.push_back('"');
150 for (const char ch : text)
151 switch (ch)
152 {
153 case '\\':
154 result += "\\\\";
155 break;
156 case '"':
157 result += "\\\"";
158 break;
159 case '\n':
160 result += "\\n";
161 break;
162 case '\r':
163 result += "\\r";
164 break;
165 case '\t':
166 result += "\\t";
167 break;
168 default:
169 result.push_back(ch);
170 }
171 result.push_back('"');
172 return result;
173}
174
175inline std::string safe_comment_text(std::string text)
176{
177 while (text.find("*/") != std::string::npos)
178 text.replace(text.find("*/"), 2, "* /");
179 return text;
180}
181
182inline bool parse_integer_literal(const std::string &text, long long &value) noexcept
183{
184 if (text.empty())
185 return false;
186
187 char *end = nullptr;
188 value = std::strtoll(text.c_str(), &end, 10);
189 return end != nullptr and *end == '\0';
190}
191
192inline size_t count_parameters(const Compiler_IR_Function &function) noexcept
193{
194 size_t count = 0;
195 for (size_t i = 0; i < function.local_slots.size(); ++i)
196 if (function.local_slots.access(i).kind == Compiler_IR_Slot_Kind::Parameter)
197 ++count;
198 return count;
199}
200
201inline std::string token_name(const Compiler_Operator_Kind kind) noexcept
202{
203 return compiler_operator_name(kind);
204}
205
206inline std::string value_name(const Compiler_IR_Value_Id id)
207{
208 return "v" + std::to_string(id);
209}
210
211inline std::string local_name(const Compiler_IR_Local_Slot_Id id)
212{
213 return "s" + std::to_string(id);
214}
215
216inline std::string global_name(const Compiler_IR_Global_Slot_Id id)
217{
218 return "g" + std::to_string(id);
219}
220
221inline std::string block_label(const size_t id)
222{
223 return "B" + std::to_string(id);
224}
225
226inline std::string indent(const size_t count)
227{
228 return std::string(count, ' ');
229}
230} // namespace Compiler_Backend_C_Detail
231
234{
236
238 {
239 std::string prefix;
240 std::string upper_prefix;
241 std::string tag_enum_name;
242 std::string value_type_name;
249 std::string tag_name_symbol;
250 std::string panic_symbol;
251 std::string int_add_symbol;
252 std::string int_sub_symbol;
253 std::string int_mul_symbol;
254 std::string int_div_symbol;
255 std::string int_mod_symbol;
256 std::string int_neg_symbol;
261 std::string equal_symbol;
262 std::string call_symbol;
264 std::string make_unit_symbol;
265 std::string make_bool_symbol;
266 std::string make_int_symbol;
268 std::string make_char_symbol;
270 std::string top_level_symbol;
271 std::string tag_invalid_name;
272 std::string tag_unit_name;
273 std::string tag_bool_name;
274 std::string tag_int_name;
275 std::string tag_string_name;
276 std::string tag_char_name;
277 std::string tag_function_name;
278 };
279
281 {
283 const Compiler_IR_Module *module_ = nullptr;
287
288 void fail(const std::string &message)
289 {
290 emission_.valid = false;
292 }
293
294 void warn(const std::string &message)
295 {
297 }
298
299 [[nodiscard]] std::string type_comment(const Compiler_Type_Id type_id) const
300 {
301 if (types_ == nullptr or type_id == 0)
302 return "";
303 return types_->to_string(type_id);
304 }
305
307 {
308 names_.prefix = "alephc_"
310 ? std::string("module")
314 names_.value_type_name = names_.prefix + "_value";
315 names_.function_type_name = names_.prefix + "_function";
318 names_.module_init_symbol = names_.prefix + "_module_init";
319 names_.dump_globals_symbol = names_.prefix + "_dump_globals";
320 names_.print_value_symbol = names_.prefix + "_print_value";
321 names_.tag_name_symbol = names_.prefix + "_tag_name";
322 names_.panic_symbol = names_.prefix + "_panic";
323 names_.int_add_symbol = names_.prefix + "_int_add";
324 names_.int_sub_symbol = names_.prefix + "_int_sub";
325 names_.int_mul_symbol = names_.prefix + "_int_mul";
326 names_.int_div_symbol = names_.prefix + "_int_div";
327 names_.int_mod_symbol = names_.prefix + "_int_mod";
328 names_.int_neg_symbol = names_.prefix + "_int_neg";
329 names_.require_assigned_symbol = names_.prefix + "_require_assigned";
330 names_.require_int_symbol = names_.prefix + "_require_int";
331 names_.require_bool_symbol = names_.prefix + "_require_bool";
332 names_.require_function_symbol = names_.prefix + "_require_function";
333 names_.equal_symbol = names_.prefix + "_equal";
334 names_.call_symbol = names_.prefix + "_call";
335 names_.make_invalid_symbol = names_.prefix + "_make_invalid";
336 names_.make_unit_symbol = names_.prefix + "_make_unit";
337 names_.make_bool_symbol = names_.prefix + "_make_bool";
338 names_.make_int_symbol = names_.prefix + "_make_int";
339 names_.make_string_symbol = names_.prefix + "_make_string";
340 names_.make_char_symbol = names_.prefix + "_make_char";
341 names_.make_function_symbol = names_.prefix + "_make_function";
342 names_.top_level_symbol = names_.prefix + "_top_level";
343 names_.tag_invalid_name = names_.upper_prefix + "_TAG_INVALID";
344 names_.tag_unit_name = names_.upper_prefix + "_TAG_UNIT";
345 names_.tag_bool_name = names_.upper_prefix + "_TAG_BOOL";
346 names_.tag_int_name = names_.upper_prefix + "_TAG_INT";
347 names_.tag_string_name = names_.upper_prefix + "_TAG_STRING";
348 names_.tag_char_name = names_.upper_prefix + "_TAG_CHAR";
349 names_.tag_function_name = names_.upper_prefix + "_TAG_FUNCTION";
350
358
359 for (size_t i = 0; i < module_->functions.size(); ++i)
360 {
361 const auto &function = *module_->functions.access(i);
362 const std::string symbol = names_.prefix + "_f" + std::to_string(i) + "_"
365 emission_.function_descriptor_symbols.append(symbol + "_descriptor");
366 }
367 }
368
370 {
371 const auto [valid, errors, warnings] = validate_ir_module(*module_);
372 if (not valid)
373 for (size_t i = 0; i < errors.size(); ++i)
374 fail(errors.access(i));
375 for (size_t i = 0; i < warnings.size(); ++i)
376 warn(warnings.access(i));
377
378 for (size_t i = 0; i < module_->functions.size(); ++i)
379 if (module_->functions.access(i)->id != i)
380 fail("C backend requires dense zero-based function ids; function '"
381 + module_->functions.access(i)->name + "' has id "
382 + std::to_string(module_->functions.access(i)->id) + " but its module index is "
383 + std::to_string(i));
384 }
385
386 [[nodiscard]] std::string function_symbol(const size_t index) const
387 {
388 return emission_.function_symbols.access(index);
389 }
390
391 [[nodiscard]] std::string function_descriptor_symbol(const size_t index) const
392 {
394 }
395
396 [[nodiscard]] bool emit_constant_expression(const Compiler_IR_Instruction &inst,
397 std::string &expression,
398 std::string &error) const
399 {
400 if (inst.text == "unit")
401 {
402 expression = names_.make_unit_symbol + "()";
403 return true;
404 }
405
406 if (inst.text == "true" or inst.text == "false")
407 {
408 expression = names_.make_bool_symbol + "("
409 + std::string(inst.text == "true" ? "true" : "false") + ")";
410 return true;
411 }
412
413 long long integer_value = 0;
415 {
416 // -9223372036854775808LL would negate a constant that does not
417 // fit in long long
418 expression = names_.make_int_symbol + "("
419 + (integer_value == LLONG_MIN ? std::string("LLONG_MIN")
420 : std::to_string(integer_value) + "LL")
421 + ")";
422 return true;
423 }
424
425 if (inst.text.size() >= 2 and inst.text.front() == '"' and inst.text.back() == '"')
426 {
427 expression = names_.make_string_symbol + "(" + inst.text + ")";
428 return true;
429 }
430
431 if (inst.text.size() >= 3 and inst.text.front() == '\'' and inst.text.back() == '\'')
432 {
433 expression = names_.make_char_symbol + "(" + inst.text + ")";
434 return true;
435 }
436
437 error = "C backend does not know how to emit constant literal '" + inst.text + "'";
438 return false;
439 }
440
441 void emit_runtime_prelude(std::ostream &out) const
442 {
443 out << "#include <limits.h>\n"
444 << "#include <stdbool.h>\n"
445 << "#include <stddef.h>\n"
446 << "#include <stdio.h>\n"
447 << "#include <stdlib.h>\n"
448 << "#include <string.h>\n\n"
449 << "typedef struct " << names_.value_type_name << " " << names_.value_type_name << ";\n"
450 << "typedef struct " << names_.function_type_name << " " << names_.function_type_name
451 << ";\n"
452 << "typedef struct " << names_.module_struct_name << " " << names_.module_struct_name
453 << ";\n\n"
454 << "typedef enum " << names_.tag_enum_name << "\n"
455 << "{\n"
456 << " " << names_.tag_invalid_name << " = 0,\n"
457 << " " << names_.tag_unit_name << ",\n"
458 << " " << names_.tag_bool_name << ",\n"
459 << " " << names_.tag_int_name << ",\n"
460 << " " << names_.tag_string_name << ",\n"
461 << " " << names_.tag_char_name << ",\n"
462 << " " << names_.tag_function_name << "\n"
463 << "} " << names_.tag_enum_name << ";\n\n"
464 << "struct " << names_.value_type_name << "\n"
465 << "{\n"
466 << " " << names_.tag_enum_name << " tag;\n"
467 << " union\n"
468 << " {\n"
469 << " bool boolean;\n"
470 << " long long integer;\n"
471 << " const char * string;\n"
472 << " char character;\n"
473 << " const " << names_.function_type_name << " * function;\n"
474 << " } as;\n"
475 << "};\n\n"
476 << "typedef " << names_.value_type_name << " (*" << names_.function_pointer_type_name
477 << ")(" << names_.module_struct_name << " * module,\n"
479 << " * args,\n"
480 << Compiler_Backend_C_Detail::indent(52) << "size_t argc);\n\n"
481 << "struct " << names_.function_type_name << "\n"
482 << "{\n"
483 << " " << names_.function_pointer_type_name << " pointer;\n"
484 << " size_t arity;\n"
485 << " const char * name;\n"
486 << "};\n\n"
487 << "static const char *\n"
488 << names_.tag_name_symbol << "(" << names_.tag_enum_name << " tag)\n"
489 << "{\n"
490 << " switch (tag)\n"
491 << " {\n"
492 << " case " << names_.tag_invalid_name << ": return \"Invalid\";\n"
493 << " case " << names_.tag_unit_name << ": return \"Unit\";\n"
494 << " case " << names_.tag_bool_name << ": return \"Bool\";\n"
495 << " case " << names_.tag_int_name << ": return \"Int\";\n"
496 << " case " << names_.tag_string_name << ": return \"String\";\n"
497 << " case " << names_.tag_char_name << ": return \"Char\";\n"
498 << " case " << names_.tag_function_name << ": return \"Function\";\n"
499 << " }\n"
500 << " return \"Unknown\";\n"
501 << "}\n\n"
502 << "static void\n"
503 << names_.panic_symbol << "(const char * message)\n"
504 << "{\n"
505 << " fprintf(stderr, \"[" << names_.prefix << "] %s\\n\", message);\n"
506 << " exit(1);\n"
507 << "}\n\n"
508 // integer arithmetic of the language: overflow is a runtime error
509 << "static inline long long\n"
510 << names_.int_add_symbol << "(long long lhs, long long rhs)\n"
511 << "{\n"
512 << " if ((rhs > 0 && lhs > LLONG_MAX - rhs) || (rhs < 0 && lhs < LLONG_MIN - rhs))\n"
513 << " " << names_.panic_symbol << "(\"integer overflow\");\n"
514 << " return lhs + rhs;\n"
515 << "}\n\n"
516 << "static inline long long\n"
517 << names_.int_sub_symbol << "(long long lhs, long long rhs)\n"
518 << "{\n"
519 << " if ((rhs < 0 && lhs > LLONG_MAX + rhs) || (rhs > 0 && lhs < LLONG_MIN + rhs))\n"
520 << " " << names_.panic_symbol << "(\"integer overflow\");\n"
521 << " return lhs - rhs;\n"
522 << "}\n\n"
523 << "static inline long long\n"
524 << names_.int_mul_symbol << "(long long lhs, long long rhs)\n"
525 << "{\n"
526 << " if (lhs > 0 ? (rhs > 0 ? lhs > LLONG_MAX / rhs : rhs < LLONG_MIN / lhs)\n"
527 << " : (rhs > 0 ? lhs < LLONG_MIN / rhs : lhs != 0 && rhs < LLONG_MAX / lhs))\n"
528 << " " << names_.panic_symbol << "(\"integer overflow\");\n"
529 << " return lhs * rhs;\n"
530 << "}\n\n"
531 << "static inline long long\n"
532 << names_.int_div_symbol << "(long long lhs, long long rhs)\n"
533 << "{\n"
534 << " if (rhs == 0)\n"
535 << " " << names_.panic_symbol << "(\"division by zero\");\n"
536 << " if (lhs == LLONG_MIN && rhs == -1)\n"
537 << " " << names_.panic_symbol << "(\"integer overflow\");\n"
538 << " return lhs / rhs;\n"
539 << "}\n\n"
540 << "static inline long long\n"
541 << names_.int_mod_symbol << "(long long lhs, long long rhs)\n"
542 << "{\n"
543 << " if (rhs == 0)\n"
544 << " " << names_.panic_symbol << "(\"modulo by zero\");\n"
545 << " if (rhs == -1)\n"
546 << " return 0;\n"
547 << " return lhs % rhs;\n"
548 << "}\n\n"
549 << "static inline long long\n"
550 << names_.int_neg_symbol << "(long long operand)\n"
551 << "{\n"
552 << " if (operand == LLONG_MIN)\n"
553 << " " << names_.panic_symbol << "(\"integer overflow\");\n"
554 << " return -operand;\n"
555 << "}\n\n"
556 << "static " << names_.value_type_name << "\n"
557 << names_.make_invalid_symbol << "(void)\n"
558 << "{\n"
559 << " " << names_.value_type_name << " value;\n"
560 << " value.tag = " << names_.tag_invalid_name << ";\n"
561 << " value.as.integer = 0;\n"
562 << " return value;\n"
563 << "}\n\n"
564 << "static " << names_.value_type_name << "\n"
565 << names_.make_unit_symbol << "(void)\n"
566 << "{\n"
567 << " " << names_.value_type_name << " value;\n"
568 << " value.tag = " << names_.tag_unit_name << ";\n"
569 << " value.as.integer = 0;\n"
570 << " return value;\n"
571 << "}\n\n"
572 << "static " << names_.value_type_name << "\n"
573 << names_.make_bool_symbol << "(bool value_in)\n"
574 << "{\n"
575 << " " << names_.value_type_name << " value;\n"
576 << " value.tag = " << names_.tag_bool_name << ";\n"
577 << " value.as.boolean = value_in;\n"
578 << " return value;\n"
579 << "}\n\n"
580 << "static " << names_.value_type_name << "\n"
581 << names_.make_int_symbol << "(long long value_in)\n"
582 << "{\n"
583 << " " << names_.value_type_name << " value;\n"
584 << " value.tag = " << names_.tag_int_name << ";\n"
585 << " value.as.integer = value_in;\n"
586 << " return value;\n"
587 << "}\n\n"
588 << "static " << names_.value_type_name << "\n"
589 << names_.make_string_symbol << "(const char * value_in)\n"
590 << "{\n"
591 << " " << names_.value_type_name << " value;\n"
592 << " value.tag = " << names_.tag_string_name << ";\n"
593 << " value.as.string = value_in;\n"
594 << " return value;\n"
595 << "}\n\n"
596 << "static " << names_.value_type_name << "\n"
597 << names_.make_char_symbol << "(char value_in)\n"
598 << "{\n"
599 << " " << names_.value_type_name << " value;\n"
600 << " value.tag = " << names_.tag_char_name << ";\n"
601 << " value.as.character = value_in;\n"
602 << " return value;\n"
603 << "}\n\n"
604 << "static " << names_.value_type_name << "\n"
605 << names_.make_function_symbol << "(const " << names_.function_type_name << " * function)\n"
606 << "{\n"
607 << " " << names_.value_type_name << " value;\n"
608 << " value.tag = " << names_.tag_function_name << ";\n"
609 << " value.as.function = function;\n"
610 << " return value;\n"
611 << "}\n\n"
612 << "static " << names_.value_type_name << "\n"
613 << names_.require_assigned_symbol << "(" << names_.value_type_name << " value,\n"
614 << Compiler_Backend_C_Detail::indent(37) << "const char * context)\n"
615 << "{\n"
616 << " if (value.tag == " << names_.tag_invalid_name << ")\n"
617 << " {\n"
618 << " fprintf(stderr, \"[" << names_.prefix
619 << "] %s uses an uninitialized value\\n\", context);\n"
620 << " exit(1);\n"
621 << " }\n"
622 << " return value;\n"
623 << "}\n\n"
624 << "static long long\n"
625 << names_.require_int_symbol << "(" << names_.value_type_name << " value,\n"
626 << Compiler_Backend_C_Detail::indent(32) << "const char * context)\n"
627 << "{\n"
628 << " value = " << names_.require_assigned_symbol << "(value, context);\n"
629 << " if (value.tag != " << names_.tag_int_name << ")\n"
630 << " {\n"
631 << " fprintf(stderr, \"[" << names_.prefix
632 << "] %s expects Int but received %s\\n\",\n"
633 << Compiler_Backend_C_Detail::indent(14) << "context,\n"
634 << Compiler_Backend_C_Detail::indent(14) << names_.tag_name_symbol << "(value.tag));\n"
635 << " exit(1);\n"
636 << " }\n"
637 << " return value.as.integer;\n"
638 << "}\n\n"
639 << "static bool\n"
640 << names_.require_bool_symbol << "(" << names_.value_type_name << " value,\n"
641 << Compiler_Backend_C_Detail::indent(33) << "const char * context)\n"
642 << "{\n"
643 << " value = " << names_.require_assigned_symbol << "(value, context);\n"
644 << " if (value.tag != " << names_.tag_bool_name << ")\n"
645 << " {\n"
646 << " fprintf(stderr, \"[" << names_.prefix
647 << "] %s expects Bool but received %s\\n\",\n"
648 << Compiler_Backend_C_Detail::indent(14) << "context,\n"
649 << Compiler_Backend_C_Detail::indent(14) << names_.tag_name_symbol << "(value.tag));\n"
650 << " exit(1);\n"
651 << " }\n"
652 << " return value.as.boolean;\n"
653 << "}\n\n"
654 << "static const " << names_.function_type_name << " *\n"
655 << names_.require_function_symbol << "(" << names_.value_type_name << " value,\n"
656 << Compiler_Backend_C_Detail::indent(37) << "const char * context)\n"
657 << "{\n"
658 << " value = " << names_.require_assigned_symbol << "(value, context);\n"
659 << " if (value.tag != " << names_.tag_function_name << ")\n"
660 << " {\n"
661 << " fprintf(stderr, \"[" << names_.prefix
662 << "] %s expects Function but received %s\\n\",\n"
663 << Compiler_Backend_C_Detail::indent(14) << "context,\n"
664 << Compiler_Backend_C_Detail::indent(14) << names_.tag_name_symbol << "(value.tag));\n"
665 << " exit(1);\n"
666 << " }\n"
667 << " return value.as.function;\n"
668 << "}\n\n"
669 << "static bool\n"
670 << names_.equal_symbol << "(" << names_.value_type_name << " lhs,\n"
672 << "{\n"
673 << " lhs = " << names_.require_assigned_symbol << "(lhs, \"equality lhs\");\n"
674 << " rhs = " << names_.require_assigned_symbol << "(rhs, \"equality rhs\");\n"
675 << " if (lhs.tag != rhs.tag)\n"
676 << " return false;\n"
677 << " switch (lhs.tag)\n"
678 << " {\n"
679 << " case " << names_.tag_invalid_name << ":\n"
680 << " case " << names_.tag_unit_name << ":\n"
681 << " return true;\n"
682 << " case " << names_.tag_bool_name << ":\n"
683 << " return lhs.as.boolean == rhs.as.boolean;\n"
684 << " case " << names_.tag_int_name << ":\n"
685 << " return lhs.as.integer == rhs.as.integer;\n"
686 << " case " << names_.tag_string_name << ":\n"
687 << " return strcmp(lhs.as.string, rhs.as.string) == 0;\n"
688 << " case " << names_.tag_char_name << ":\n"
689 << " return lhs.as.character == rhs.as.character;\n"
690 << " case " << names_.tag_function_name << ":\n"
691 << " return lhs.as.function == rhs.as.function;\n"
692 << " }\n"
693 << " return false;\n"
694 << "}\n\n"
695 << "static " << names_.value_type_name << "\n"
696 << names_.call_symbol << "(" << names_.module_struct_name << " * module,\n"
699 << " * args,\n"
700 << Compiler_Backend_C_Detail::indent(19) << "size_t argc)\n"
701 << "{\n"
702 << " const " << names_.function_type_name << " * function\n"
703 << " = " << names_.require_function_symbol << "(callee, \"call callee\");\n"
704 << " if (function->arity != argc)\n"
705 << " {\n"
706 << " fprintf(stderr, \"[" << names_.prefix
707 << "] function %s expects %zu arguments but received %zu\\n\",\n"
708 << Compiler_Backend_C_Detail::indent(14) << "function->name,\n"
709 << Compiler_Backend_C_Detail::indent(14) << "function->arity,\n"
710 << Compiler_Backend_C_Detail::indent(14) << "argc);\n"
711 << " exit(1);\n"
712 << " }\n"
713 << " return function->pointer(module, args, argc);\n"
714 << "}\n\n"
715 << "static void\n"
716 << names_.print_value_symbol << "(FILE * out,\n"
718 << "{\n"
719 << " value = " << names_.require_assigned_symbol << "(value, \"print value\");\n"
720 << " switch (value.tag)\n"
721 << " {\n"
722 << " case " << names_.tag_invalid_name << ":\n"
723 << " fputs(\"Invalid\", out);\n"
724 << " return;\n"
725 << " case " << names_.tag_unit_name << ":\n"
726 << " fputs(\"Unit\", out);\n"
727 << " return;\n"
728 << " case " << names_.tag_bool_name << ":\n"
729 << " fputs(value.as.boolean ? \"Bool(true)\" : \"Bool(false)\", out);\n"
730 << " return;\n"
731 << " case " << names_.tag_int_name << ":\n"
732 << " fprintf(out, \"Int(%lld)\", value.as.integer);\n"
733 << " return;\n"
734 << " case " << names_.tag_string_name << ":\n"
735 << " fprintf(out, \"String(\\\"%s\\\")\", value.as.string);\n"
736 << " return;\n"
737 << " case " << names_.tag_char_name << ":\n"
738 << " fprintf(out, \"Char('%c')\", value.as.character);\n"
739 << " return;\n"
740 << " case " << names_.tag_function_name << ":\n"
741 << " fprintf(out, \"Function(%s)\", value.as.function->name);\n"
742 << " return;\n"
743 << " }\n"
744 << "}\n\n";
745 }
746
747 void emit_module_layout(std::ostream &out) const
748 {
749 const size_t global_count = module_->global_slots.size();
750 out << "enum { " << names_.upper_prefix << "_GLOBAL_COUNT = " << global_count << " };\n\n";
751
752 out << "struct " << names_.module_struct_name << "\n"
753 << "{\n";
754 if (global_count == 0)
755 out << " " << names_.value_type_name << " unused_global;\n";
756 else
757 out << " " << names_.value_type_name << " globals[" << names_.upper_prefix
758 << "_GLOBAL_COUNT];\n";
759 out << "};\n\n";
760
761 out << "static void\n"
762 << names_.module_init_symbol << "(" << names_.module_struct_name << " * module)\n"
763 << "{\n";
764 if (global_count == 0)
765 out << " (void) module;\n";
766 else
767 out << " for (size_t i = 0; i < " << names_.upper_prefix << "_GLOBAL_COUNT; ++i)\n"
768 << " module->globals[i] = " << names_.make_invalid_symbol << "();\n";
769 out << "}\n\n";
770
771 out << "static void\n"
772 << names_.dump_globals_symbol << "(FILE * out,\n"
774 << " * module)\n"
775 << "{\n"
776 << " fputs(\"Globals\\n\", out);\n";
777 if (global_count == 0)
778 out << " fputs(\" <none>\\n\", out);\n";
779 else
780 for (size_t i = 0; i < module_->global_slots.size(); ++i)
781 {
782 const auto &slot = module_->global_slots.access(i);
783 out << " fputs(\" g" << slot.id << " "
784 << Compiler_Backend_C_Detail::safe_comment_text(slot.name) << " = \", out);\n"
785 << " " << names_.print_value_symbol << "(out, module->globals[" << i << "]);\n"
786 << " fputc('\\n', out);\n";
787 }
788 out << "}\n\n";
789 }
790
791 void emit_prototypes(std::ostream &out) const
792 {
793 for (size_t i = 0; i < module_->functions.size(); ++i)
794 out << "static " << names_.value_type_name << "\n"
795 << function_symbol(i) << "(" << names_.module_struct_name << " * module,\n"
797 << names_.value_type_name << " * args,\n"
798 << Compiler_Backend_C_Detail::indent(function_symbol(i).size() + 8) << "size_t argc);\n"
799 << "static const " << names_.function_type_name << " "
800 << function_descriptor_symbol(i) << ";\n\n";
801
802 if (module_->top_level != nullptr)
803 out << "static " << names_.value_type_name << "\n"
804 << names_.top_level_symbol << "(" << names_.module_struct_name << " * module,\n"
806 << names_.value_type_name << " * args,\n"
808 << "size_t argc);\n\n";
809 }
810
811 void emit_function_signature(std::ostream &out, const std::string &symbol) const
812 {
813 out << "static " << names_.value_type_name << "\n"
814 << symbol << "(" << names_.module_struct_name << " * module,\n"
815 << Compiler_Backend_C_Detail::indent(symbol.size() + 8) << "const "
816 << names_.value_type_name << " * args,\n"
817 << Compiler_Backend_C_Detail::indent(symbol.size() + 8) << "size_t argc)\n";
818 }
819
820 void emit_slot_declarations(std::ostream &out, const Compiler_IR_Function &function) const
821 {
822 for (size_t i = 0; i < function.local_slots.size(); ++i)
823 {
824 const auto &slot = function.local_slots.access(i);
825 out << " " << names_.value_type_name << " "
826 << Compiler_Backend_C_Detail::local_name(slot.id) << " = "
827 << names_.make_invalid_symbol << "();";
829 {
830 out << " /* " << compiler_ir_slot_kind_name(slot.kind) << ' '
832 const auto type_text = type_comment(slot.type_id);
833 if (not type_text.empty())
835 out << " */";
836 }
837 out << '\n';
838 }
839 }
840
841 void emit_value_declarations(std::ostream &out, const Compiler_IR_Function &function) const
842 {
843 for (size_t value_id = 1; value_id <= function.next_value_id; ++value_id)
844 out << " " << names_.value_type_name << " "
845 << Compiler_Backend_C_Detail::value_name(value_id) << " = "
846 << names_.make_invalid_symbol << "();\n";
847 }
848
849 void emit_parameter_bindings(std::ostream &out, const Compiler_IR_Function &function) const
850 {
851 const size_t arity = Compiler_Backend_C_Detail::count_parameters(function);
852 out << " if (argc != " << arity << "u)\n"
853 << " {\n"
854 << " fprintf(stderr, \"[" << names_.prefix << "] function "
856 << " expects %zu arguments but received %zu\\n\",\n"
857 << " (size_t) " << arity << "u,\n"
858 << " argc);\n"
859 << " exit(1);\n"
860 << " }\n";
861
862 size_t parameter_index = 0;
863 for (size_t i = 0; i < function.local_slots.size(); ++i)
864 {
865 const auto &slot = function.local_slots.access(i);
866 if (slot.kind != Compiler_IR_Slot_Kind::Parameter)
867 continue;
868
869 out << " " << Compiler_Backend_C_Detail::local_name(slot.id) << " = args["
870 << parameter_index << "];\n";
871 ++parameter_index;
872 }
873 }
874
875 void emit_instruction_comment(std::ostream &out, const Compiler_IR_Instruction &inst) const
876 {
877 if (not options_->emit_comments)
878 return;
879
880 out << " /* ";
881 if (inst.result_id != 0)
886 out << '(' << Compiler_Backend_C_Detail::token_name(inst.op) << ')';
890 out << "(f" << inst.function_id << ')';
891 out << " */\n";
892 }
893
894 void emit_instruction(std::ostream &out,
895 const Compiler_IR_Instruction &inst,
896 const Compiler_IR_Function &function)
897 {
898 (void) function;
899 const auto value = [](const Compiler_IR_Value_Id id)
900 {
902 };
903 const auto slot = [](const Compiler_IR_Local_Slot_Id id)
904 {
906 };
907
909
910 switch (inst.kind)
911 {
913 {
914 std::string expression;
915 std::string error;
916 if (not emit_constant_expression(inst, expression, error))
917 {
918 fail(error);
919 return;
920 }
921
922 out << " " << value(inst.result_id) << " = " << expression << ";\n";
923 break;
924 }
925
928 out << " " << value(inst.result_id) << " = " << names_.require_assigned_symbol
929 << "(module->globals[" << inst.global_slot_id << "], "
932 << ");\n";
933 else
934 out << " " << value(inst.result_id) << " = " << names_.require_assigned_symbol << "("
935 << slot(inst.local_slot_id) << ", "
938 << ");\n";
939 break;
940
942 if (inst.operands.is_empty())
943 {
944 fail("C backend cannot emit Store without one operand");
945 return;
946 }
947
949 out << " module->globals[" << inst.global_slot_id
950 << "] = " << names_.require_assigned_symbol << "(" << value(inst.operands.access(0))
951 << ", "
953 + value(inst.operands.access(0)))
954 << ");\n";
955 else
956 out << " " << slot(inst.local_slot_id) << " = " << names_.require_assigned_symbol
957 << "(" << value(inst.operands.access(0)) << ", "
959 + value(inst.operands.access(0)))
960 << ");\n";
961 break;
962
964 if (inst.operands.is_empty())
965 {
966 fail("C backend cannot emit Unary without one operand");
967 return;
968 }
969
970 switch (inst.op)
971 {
973 out << " " << value(inst.result_id) << " = " << names_.make_int_symbol << "("
975 << value(inst.operands.access(0)) << ", "
976 << Compiler_Backend_C_Detail::c_string_literal("unary minus") << ")));\n";
977 break;
978
980 out << " " << value(inst.result_id) << " = " << names_.make_bool_symbol << "(!"
981 << names_.require_bool_symbol << "(" << value(inst.operands.access(0)) << ", "
982 << Compiler_Backend_C_Detail::c_string_literal("logical not") << "));\n";
983 break;
984
986 out << " " << value(inst.result_id) << " = " << names_.make_int_symbol << "(~"
987 << names_.require_int_symbol << "(" << value(inst.operands.access(0)) << ", "
988 << Compiler_Backend_C_Detail::c_string_literal("bitwise not") << "));\n";
989 break;
990
991 default:
992 fail("C backend does not support unary operator '"
994 break;
995 }
996 break;
997
999 if (inst.operands.size() < 2)
1000 {
1001 fail("C backend cannot emit Binary without two operands");
1002 return;
1003 }
1004
1005 switch (inst.op)
1006 {
1013 {
1014 const char *op_text = inst.op == Compiler_Operator_Kind::Plus ? "+"
1015 : inst.op == Compiler_Operator_Kind::Minus ? "-"
1016 : inst.op == Compiler_Operator_Kind::Star ? "*"
1017 : inst.op == Compiler_Operator_Kind::Amp ? "&"
1018 : inst.op == Compiler_Operator_Kind::Pipe ? "|"
1019 : "^";
1020 out << " {\n"
1021 << " long long lhs_value = " << names_.require_int_symbol << "("
1022 << value(inst.operands.access(0)) << ", "
1024 + std::string(op_text))
1025 << ");\n"
1026 << " long long rhs_value = " << names_.require_int_symbol << "("
1027 << value(inst.operands.access(1)) << ", "
1029 + std::string(op_text))
1030 << ");\n"
1031 << " " << value(inst.result_id) << " = " << names_.make_int_symbol;
1032 if (inst.op == Compiler_Operator_Kind::Plus)
1033 out << "(" << names_.int_add_symbol << "(lhs_value, rhs_value));\n";
1034 else if (inst.op == Compiler_Operator_Kind::Minus)
1035 out << "(" << names_.int_sub_symbol << "(lhs_value, rhs_value));\n";
1036 else if (inst.op == Compiler_Operator_Kind::Star)
1037 out << "(" << names_.int_mul_symbol << "(lhs_value, rhs_value));\n";
1038 else
1039 out << "(lhs_value " << op_text << " rhs_value);\n";
1040 out << " }\n";
1041 break;
1042 }
1043
1046 {
1047 const char *op_text = inst.op == Compiler_Operator_Kind::Slash ? "/" : "%";
1048 const std::string &checked = inst.op == Compiler_Operator_Kind::Slash
1051 out << " {\n"
1052 << " long long lhs_value = " << names_.require_int_symbol << "("
1053 << value(inst.operands.access(0)) << ", "
1055 + std::string(op_text))
1056 << ");\n"
1057 << " long long rhs_value = " << names_.require_int_symbol << "("
1058 << value(inst.operands.access(1)) << ", "
1060 + std::string(op_text))
1061 << ");\n"
1062 << " " << value(inst.result_id) << " = " << names_.make_int_symbol
1063 << "(" << checked << "(lhs_value, rhs_value));\n"
1064 << " }\n";
1065 break;
1066 }
1067
1072 {
1073 const char *op_text = inst.op == Compiler_Operator_Kind::Less ? "<"
1074 : inst.op == Compiler_Operator_Kind::LessEq ? "<="
1075 : inst.op == Compiler_Operator_Kind::Greater ? ">"
1076 : ">=";
1077 out << " {\n"
1078 << " long long lhs_value = " << names_.require_int_symbol << "("
1079 << value(inst.operands.access(0)) << ", "
1081 + std::string(op_text))
1082 << ");\n"
1083 << " long long rhs_value = " << names_.require_int_symbol << "("
1084 << value(inst.operands.access(1)) << ", "
1086 + std::string(op_text))
1087 << ");\n"
1088 << " " << value(inst.result_id) << " = " << names_.make_bool_symbol
1089 << "(lhs_value " << op_text << " rhs_value);\n"
1090 << " }\n";
1091 break;
1092 }
1093
1096 {
1097 const char *op_text = inst.op == Compiler_Operator_Kind::AndAnd ? "&&" : "||";
1098 out << " {\n"
1099 << " bool lhs_value = " << names_.require_bool_symbol << "("
1100 << value(inst.operands.access(0)) << ", "
1102 + std::string(op_text))
1103 << ");\n"
1104 << " bool rhs_value = " << names_.require_bool_symbol << "("
1105 << value(inst.operands.access(1)) << ", "
1107 + std::string(op_text))
1108 << ");\n"
1109 << " " << value(inst.result_id) << " = " << names_.make_bool_symbol
1110 << "(lhs_value " << op_text << " rhs_value);\n"
1111 << " }\n";
1112 break;
1113 }
1114
1117 out << " " << value(inst.result_id) << " = " << names_.make_bool_symbol << "("
1118 << (inst.op == Compiler_Operator_Kind::NotEq ? "!" : "") << names_.equal_symbol
1119 << "(" << value(inst.operands.access(0)) << ", " << value(inst.operands.access(1))
1120 << "));\n";
1121 break;
1122
1123 default:
1124 fail("C backend does not support binary operator '"
1126 break;
1127 }
1128 break;
1129
1131 if (inst.operands.is_empty())
1132 {
1133 fail("C backend cannot emit Call without a callee operand");
1134 return;
1135 }
1136
1137 out << " {\n";
1138 if (inst.operands.size() == 1)
1139 out << " " << value(inst.result_id) << " = " << names_.call_symbol << "(module, "
1140 << value(inst.operands.access(0)) << ", NULL, 0u);\n";
1141 else
1142 {
1143 out << " const " << names_.value_type_name << " call_args[] =\n"
1144 << " {\n";
1145 for (size_t i = 1; i < inst.operands.size(); ++i)
1146 out << " " << names_.require_assigned_symbol << "("
1147 << value(inst.operands.access(i)) << ", "
1149 + std::to_string(i - 1))
1150 << ")" << (i + 1 < inst.operands.size() ? "," : "") << '\n';
1151 out << " };\n"
1152 << " " << value(inst.result_id) << " = " << names_.call_symbol << "(module, "
1153 << value(inst.operands.access(0)) << ", call_args, " << (inst.operands.size() - 1)
1154 << "u);\n";
1155 }
1156 out << " }\n";
1157 break;
1158
1160 out << " " << value(inst.result_id) << " = " << names_.make_function_symbol << "(&"
1161 << function_descriptor_symbol(inst.function_id) << ");\n";
1162 break;
1163 }
1164 }
1165
1166 void emit_terminator(std::ostream &out,
1167 const Compiler_IR_Block &block,
1168 const Compiler_IR_Function &function)
1169 {
1171 out << " /* Terminator: " << compiler_ir_terminator_kind_name(block.terminator.kind)
1172 << " */\n";
1173
1174 switch (block.terminator.kind)
1175 {
1177 out << " goto " << names_.prefix << '_'
1179 << ";\n";
1180 break;
1181
1183 out << " if (" << names_.require_bool_symbol << "("
1185 << Compiler_Backend_C_Detail::c_string_literal("branch condition") << "))\n"
1186 << " goto " << names_.prefix << '_'
1188 << ";\n"
1189 << " goto " << names_.prefix << '_'
1191 << ";\n";
1192 break;
1193
1195 out << " return " << names_.require_assigned_symbol << "("
1197 << Compiler_Backend_C_Detail::c_string_literal("return value") << ");\n";
1198 break;
1199
1201 out << " return " << names_.make_unit_symbol << "();\n";
1202 break;
1203
1205 out << " " << names_.panic_symbol << "("
1207 "entered unreachable block in function " + function.name)
1208 << ");\n"
1209 << " return " << names_.make_invalid_symbol << "();\n";
1210 break;
1211
1213 fail("C backend cannot emit unterminated block "
1215 break;
1216 }
1217 }
1218
1219 void emit_function_body(std::ostream &out,
1220 const Compiler_IR_Function &function,
1221 const std::string &symbol)
1222 {
1224 {
1226 const auto type_text = type_comment(function.type_id);
1227 if (not type_text.empty())
1229 out << " */\n";
1230 }
1231
1233 out << "{\n"
1234 << " (void) module;\n";
1235 emit_slot_declarations(out, function);
1236 emit_value_declarations(out, function);
1237 emit_parameter_bindings(out, function);
1238 out << " goto " << names_.prefix << '_'
1240
1241 for (size_t i = 0; i < function.blocks.size(); ++i)
1242 {
1243 const auto &block = function.blocks.access(i);
1244 out << names_.prefix << '_' << Compiler_Backend_C_Detail::block_label(block.id) << ":\n";
1246 out << " /* Block " << Compiler_Backend_C_Detail::block_label(block.id) << ": "
1248 for (size_t inst_index = 0; inst_index < block.instructions.size(); ++inst_index)
1249 emit_instruction(out, block.instructions.access(inst_index), function);
1250 emit_terminator(out, block, function);
1251 out << '\n';
1252 }
1253
1254 out << "}\n\n";
1255 }
1256
1257 void emit_function(const size_t index, std::ostream &out)
1258 {
1259 emit_function_body(out, *module_->functions.access(index), function_symbol(index));
1260 }
1261
1262 void emit_top_level(std::ostream &out)
1263 {
1265 }
1266
1267 void emit_function_descriptors(std::ostream &out) const
1268 {
1269 for (size_t i = 0; i < module_->functions.size(); ++i)
1270 {
1271 const auto &function = *module_->functions.access(i);
1272 out << "static const " << names_.function_type_name << " "
1273 << function_descriptor_symbol(i) << " =\n"
1274 << "{\n"
1275 << " " << function_symbol(i) << ",\n"
1276 << " " << Compiler_Backend_C_Detail::count_parameters(function) << "u,\n"
1277 << " " << Compiler_Backend_C_Detail::c_string_literal(function.name) << "\n"
1278 << "};\n\n";
1279 }
1280 }
1281
1282 void emit_main(std::ostream &out)
1283 {
1284 if (not options_->emit_main)
1285 return;
1286
1287 if (module_->top_level == nullptr)
1288 {
1289 warn("C backend was asked to emit main(), but the IR module has no top-level body");
1290 return;
1291 }
1292
1293 out << "int\n"
1294 << "main(void)\n"
1295 << "{\n"
1296 << " " << names_.module_struct_name << " module;\n"
1297 << " " << names_.module_init_symbol << "(&module);\n"
1298 << " " << names_.value_type_name << " result = " << names_.top_level_symbol
1299 << "(&module, NULL, 0u);\n"
1300 << " fputs(\"Result = \", stdout);\n"
1301 << " " << names_.print_value_symbol << "(stdout, result);\n"
1302 << " fputc('\\n', stdout);\n"
1303 << " " << names_.dump_globals_symbol << "(stdout, &module);\n"
1304 << " return 0;\n"
1305 << "}\n";
1306 }
1307
1308 public:
1311 const Compiler_IR_Module &module_in,
1312 const Compiler_Type_Context *types_in)
1313 : options_(&options_in), module_(&module_in), types_(types_in)
1314 {
1316 }
1317
1320 {
1322 if (not emission_.valid)
1323 return emission_;
1324
1325 std::ostringstream out;
1327 out << "/* Generated by Compiler_Backend_C.H from explicit IR. */\n\n";
1328
1332 for (size_t i = 0; i < module_->functions.size(); ++i)
1333 emit_function(i, out);
1334 if (module_->top_level != nullptr)
1337 emit_main(out);
1338
1339 if (emission_.valid)
1340 emission_.source = out.str();
1341 return emission_;
1342 }
1343 };
1344
1345public:
1348 : options(std::move(options_in))
1349 {}
1350
1359 const Compiler_Type_Context *types
1360 = nullptr) const
1361 {
1363 if (module == nullptr)
1364 {
1365 emission.valid = false;
1366 emission.errors.append("Compiler_C_Backend::emit_module(): null IR module");
1367 return emission;
1368 }
1369
1370 return Emitter(options, *module, types).run();
1371 }
1372};
1373
1377 = {},
1378 const Compiler_Type_Context *types = nullptr)
1379{
1380 return Compiler_C_Backend(options).emit_module(module, types);
1381}
1382} // namespace Aleph
1383
1384#endif
Reusable explicit-value IR model, validation, and deterministic dumps.
Exception handling system with formatted messages for Aleph-w.
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
Emitter(const Compiler_C_Backend_Options &options_in, const Compiler_IR_Module &module_in, const Compiler_Type_Context *types_in)
Builds an emitter over one module and one option set.
const Compiler_IR_Module * module_
void emit_instruction(std::ostream &out, const Compiler_IR_Instruction &inst, const Compiler_IR_Function &function)
void emit_module_layout(std::ostream &out) const
void emit_function(const size_t index, std::ostream &out)
Compiler_C_Backend_Emission emission_
void emit_value_declarations(std::ostream &out, const Compiler_IR_Function &function) const
bool emit_constant_expression(const Compiler_IR_Instruction &inst, std::string &expression, std::string &error) const
const Compiler_C_Backend_Options * options_
void warn(const std::string &message)
std::string function_symbol(const size_t index) const
void emit_parameter_bindings(std::ostream &out, const Compiler_IR_Function &function) const
std::string type_comment(const Compiler_Type_Id type_id) const
const Compiler_Type_Context * types_
void emit_prototypes(std::ostream &out) const
std::string function_descriptor_symbol(const size_t index) const
void emit_function_descriptors(std::ostream &out) const
void emit_slot_declarations(std::ostream &out, const Compiler_IR_Function &function) const
void emit_function_body(std::ostream &out, const Compiler_IR_Function &function, const std::string &symbol)
void emit_function_signature(std::ostream &out, const std::string &symbol) const
void emit_runtime_prelude(std::ostream &out) const
Compiler_C_Backend_Emission run()
Emits the full C translation unit.
void emit_terminator(std::ostream &out, const Compiler_IR_Block &block, const Compiler_IR_Function &function)
void emit_instruction_comment(std::ostream &out, const Compiler_IR_Instruction &inst) const
void fail(const std::string &message)
Emits portable C from one validated Compiler_IR_Module.
Compiler_C_Backend_Emission emit_module(const Compiler_IR_Module *module, const Compiler_Type_Context *types=nullptr) const
Emits one validated IR module as portable C.
Compiler_C_Backend_Options options
Compiler_C_Backend(Compiler_C_Backend_Options options_in={})
Builds one reusable C backend with options_in.
Context owning all compiler type nodes.
std::string to_string(const Compiler_Type_Id id) const
Renders one type to a deterministic human-readable string.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
std::string global_name(const Compiler_IR_Global_Slot_Id id)
size_t count_parameters(const Compiler_IR_Function &function) noexcept
std::string token_name(const Compiler_Operator_Kind kind) noexcept
bool parse_integer_literal(const std::string &text, long long &value) noexcept
std::string indent(const size_t count)
std::string value_name(const Compiler_IR_Value_Id id)
std::string upper_identifier(std::string_view text)
std::string block_label(const size_t id)
std::string sanitize_identifier(std::string_view text)
std::string local_name(const Compiler_IR_Local_Slot_Id id)
std::string c_string_literal(std::string_view text)
std::string safe_comment_text(std::string text)
Main namespace for Aleph-w library functions.
const char * compiler_ir_terminator_kind_name(const Compiler_IR_Terminator_Kind kind) noexcept
Stable debug name for one terminator kind.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
Definition ahDefs.C:95
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
const char * compiler_operator_name(const Compiler_Operator_Kind kind) noexcept
Returns a stable debug name for one operator kind.
Compiler_IR_Validation_Report validate_ir_module(const Compiler_IR_Module &module)
Validates all functions in one lowered IR module.
size_t Compiler_IR_Value_Id
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
and
Check uniqueness with explicit hash + equality functors.
size_t Compiler_Type_Id
const char * compiler_ir_slot_kind_name(const Compiler_IR_Slot_Kind kind) noexcept
Stable debug name for one slot kind.
void error(const char *file, int line, const char *format,...)
Print an error message with file and line info.
Definition ahDefs.C:100
size_t Compiler_IR_Global_Slot_Id
const char * compiler_ir_instruction_kind_name(const Compiler_IR_Instruction_Kind kind) noexcept
Stable debug name for one instruction kind.
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.
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
size_t Compiler_IR_Local_Slot_Id
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
static struct argp_option options[]
Definition ntreepic.C:1890
Result of one C backend emission request.
DynArray< std::string > errors
Hard emission failures.
bool valid
Whether emission completed successfully.
DynArray< std::string > warnings
Non-fatal findings.
std::string value_type_name
Generated C typedef name for runtime values.
std::string print_value_symbol
Generated C symbol that prints one runtime value.
std::string module_struct_name
Generated C struct name for module state.
std::string c_prefix
Shared prefix used for runtime helpers and generated symbols.
DynArray< std::string > function_descriptor_symbols
Generated callable descriptor symbols aligned with module.functions.
std::string dump_globals_symbol
Generated C symbol that prints current global state.
std::string module_init_symbol
Generated C symbol that initializes module globals.
bool ok() const noexcept
Returns whether emission finished without hard errors.
std::string top_level_symbol
Generated C symbol for the lowered top-level body, if any.
DynArray< std::string > function_symbols
Generated C function symbols aligned with module.functions.
std::string source
Final generated C translation unit.
Options that control one C backend emission request.
bool emit_comments
Whether to annotate the generated C with readable IR-derived comments.
bool emit_main
Whether to emit a standalone main() when the IR has a top-level body.
std::string module_name
Prefix used to derive stable C identifiers.
One basic block of IR instructions.
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_Type_Id type_id
Full function type, when known.
Compiler_IR_Block_Id entry_block
Canonical entry block.
DynArray< Compiler_IR_Slot > local_slots
Parameter/local slots in stable order.
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_Operator_Kind op
Unary/binary operator kind, when relevant.
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_Slot > global_slots
Module-wide global slots.
DynArray< Compiler_IR_Function * > functions
Lowered non-top-level functions.
Compiler_IR_Function * top_level
Optional top-level body lowered as a function.
Compiler_SSA_Block_Id id
Dense SSA block id.
Definition SSA.H:163
DynArray< Compiler_SSA_Instruction > instructions
Linear SSA instruction list.
Definition SSA.H:168
std::string label
Deterministic debug label.
Definition SSA.H:165
Compiler_SSA_Terminator terminator
Explicit block terminator.
Definition SSA.H:169
Compiler_SSA_Value_Id return_value
Return value, when relevant.
Definition SSA.H:156
DynArray< Compiler_SSA_Block_Id > successors
Successors in deterministic order.
Definition SSA.H:157
Compiler_IR_Terminator_Kind kind
Terminator category.
Definition SSA.H:153
Compiler_SSA_Value_Id condition_value
Branch condition, when relevant.
Definition SSA.H:155
Lazy and scalable dynamic array implementation.