Class: MilkTea::CBackend
- Inherits:
-
Object
- Object
- MilkTea::CBackend
- Includes:
- AggregateUtils, ControlFlowEmission, Expressions, FeatureDetection, Reachability, Reinterpret, RuntimeHelpers, Statements, TypeCollectors, TypeDeclaration, TypeSystem
- Defined in:
- lib/milk_tea/core/c_backend.rb,
lib/milk_tea/core/c_backend/statements.rb,
lib/milk_tea/core/c_backend/expressions.rb,
lib/milk_tea/core/c_backend/reinterpret.rb,
lib/milk_tea/core/c_backend/type_system.rb,
lib/milk_tea/core/c_backend/reachability.rb,
lib/milk_tea/core/c_backend/aggregate_utils.rb,
lib/milk_tea/core/c_backend/runtime_helpers.rb,
lib/milk_tea/core/c_backend/type_collectors.rb,
lib/milk_tea/core/c_backend/type_declaration.rb,
lib/milk_tea/core/c_backend/feature_detection.rb,
lib/milk_tea/core/c_backend/control_flow_emission.rb
Defined Under Namespace
Modules: AggregateUtils, ControlFlowEmission, Expressions, FeatureDetection, Reachability, Reinterpret, RuntimeHelpers, Statements, TypeCollectors, TypeDeclaration, TypeSystem
Constant Summary collapse
- INDENT =
" "- ARRAY_OUT_PARAM_NAME =
"__mt_out"- LOOP_GUARD_MAX_ITERATIONS =
Max iterations for loop guard counters before aborting. Set high enough that legitimate data-bound loops (e.g. is_valid_utf8 scanning a 600 KB source file) never trigger a false positive, yet low enough that a genuinely infinite loop fires a clear fatal within ~1 s.
50_000_000
Class Method Summary collapse
- .emit(program, emit_line_directives: true, debug_guards: false) ⇒ Object
- .generate_c(ir_program, emit_line_directives: true, debug_guards: false) ⇒ Object
Instance Method Summary collapse
- #emit ⇒ Object
-
#initialize(program, emit_line_directives: true, debug_guards: false) ⇒ CBackend
constructor
A new instance of CBackend.
Constructor Details
#initialize(program, emit_line_directives: true, debug_guards: false) ⇒ CBackend
Returns a new instance of CBackend.
49 50 51 52 53 54 55 56 57 |
# File 'lib/milk_tea/core/c_backend.rb', line 49 def initialize(program, emit_line_directives: true, debug_guards: false) @program = program @emit_line_directives = emit_line_directives @debug_guards = debug_guards @checked_index_alias_stack = [] @checked_index_alias_id = 0 @loop_guard_id = 0 @emitted_span_type_names = Set.new end |
Class Method Details
.emit(program, emit_line_directives: true, debug_guards: false) ⇒ Object
41 42 43 |
# File 'lib/milk_tea/core/c_backend.rb', line 41 def self.emit(program, emit_line_directives: true, debug_guards: false) new(program, emit_line_directives:, debug_guards:).emit end |
.generate_c(ir_program, emit_line_directives: true, debug_guards: false) ⇒ Object
45 46 47 |
# File 'lib/milk_tea/core/c_backend.rb', line 45 def self.generate_c(ir_program, emit_line_directives: true, debug_guards: false) emit(ir_program, emit_line_directives:, debug_guards:) end |
Instance Method Details
#emit ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/milk_tea/core/c_backend.rb', line 59 def emit lines = [] constants = emitted_constants headers = @program.includes.map(&:header) if headers.include?("\"fs_support.h\"") || headers.include?("\"tls_support.h\"") || uses_parallel_for_helper? || uses_spawn_all_helper? || uses_detach_helper? lines << "#ifndef _GNU_SOURCE" lines << "#define _GNU_SOURCE" lines << "#endif" lines << "#ifndef _POSIX_C_SOURCE" lines << "#define _POSIX_C_SOURCE 200809L" lines << "#endif" lines << "" end if uses_fatal_helper? || uses_format_helpers? headers << "<stdio.h>" end if uses_fatal_helper? || uses_format_helpers? || uses_async_memory_helpers? || uses_foreign_temp_cstr_helpers? || uses_detach_helper? headers << "<stdlib.h>" end if uses_parallel_for_helper? || uses_spawn_all_helper? || uses_detach_helper? headers << "\"uv.h\"" end headers.uniq.each do |header| lines << "#include #{header}" end lines << "" if uses_string_view? lines.concat(emit_string_type) lines << "" end if uses_vector_math_types? lines.concat(emit_vector_math_types) lines << "" end if uses_fatal_helper? lines.concat(emit_fatal_helper) lines << "" end if uses_format_helpers? lines.concat(emit_format_helpers) lines << "" end if uses_fmt_builder? lines.concat(emit_fmt_builder_helpers) lines << "" end if uses_str_equality_helper? lines.concat(emit_str_equality_helper) lines << "" end if uses_text_buffer_helpers? lines.concat(emit_text_buffer_helpers) lines << "" end if uses_async_memory_helpers? lines.concat(emit_async_memory_helpers) lines << "" end if uses_parallel_for_helper? lines.concat(emit_parallel_for_helper) lines << "" end if uses_spawn_all_helper? lines.concat(emit_spawn_all_helper) lines << "" end if uses_detach_helper? lines.concat(emit_detach_helpers) lines << "" end opaque_decls = @program.opaques aggregate_decls = sort_aggregate_decls( emitted_aggregate_structs + collect_generic_struct_decls + collect_task_decls + collect_proc_decls + collect_dyn_decls + collect_str_buffer_decls + collect_nullable_opt_decls, emitted_aggregate_unions, emitted_aggregate_variants + collect_generic_variant_decls, ) forward_declarations = emit_forward_declarations(opaque_decls, aggregate_decls) unless forward_declarations.empty? lines.concat(forward_declarations) lines << "" end @program.enums.each do |enum_decl| lines.concat(emit_enum(enum_decl)) lines << "" end collect_span_types.each do |type| span_name = span_type_name(type) next if @emitted_span_type_names.include?(span_name) @emitted_span_type_names.add(span_name) lines.concat(emit_span_type(type)) lines << "" end collect_soa_types.each do |type| lines.concat(emit_soa_type(type)) lines << "" end if uses_entrypoint_argv_helpers? lines.concat(emit_entrypoint_argv_helpers) lines << "" end if uses_foreign_temp_cstr_helpers? lines.concat(emit_foreign_temp_cstr_helpers) lines << "" end if uses_str_buffer_helpers? lines.concat(emit_str_buffer_helpers) lines << "" end aggregate_decls.each do |aggregate_decl| case aggregate_decl when IR::StructDecl lines.concat(emit_struct(aggregate_decl)) when IR::UnionDecl lines.concat(emit_union(aggregate_decl)) when IR::VariantDecl lines.concat(emit_variant(aggregate_decl)) end lines << "" end if uses_variant_equality_helper? lines.concat(emit_str_equality_helper) unless uses_str_equality_helper? lines.concat(emit_variant_equality_helpers) lines << "" end function_declarations = emit_function_declarations(emitted_functions) unless function_declarations.empty? lines.concat(function_declarations) lines << "" end constants.each do |constant| lines << "#{constant_storage(constant.type)} #{c_declaration(constant.type, constant.linkage_name)} = #{emit_initializer(constant.value)};" end @program.globals.each do |global| next unless emitted_globals.include?(global) lines << "#{global_storage(global.type)} #{c_declaration(global.type, global.linkage_name)} = #{emit_initializer(global.value)};" end lines << "" unless constants.empty? && @program.globals.empty? @program.static_asserts.each do |statement| lines << emit_static_assert(statement) end lines << "" unless @program.static_asserts.empty? reinterpret_helpers = collect_reinterpret_helpers reinterpret_helpers.each do |helper| lines.concat(emit_reinterpret_helper(helper)) lines << "" end checked_array_index_types = collect_checked_array_index_types checked_array_index_types.each do |type| lines.concat(emit_checked_array_index_helper(type)) lines << "" end checked_span_index_types = collect_checked_span_index_types checked_span_index_types.each do |type| lines.concat(emit_checked_span_index_helper(type)) lines << "" end nullable_array_index_types = collect_checked_array_index_types(nullable_only: true) nullable_array_index_types.each do |type| lines.concat(emit_nullable_array_index_helper(type)) lines << "" end nullable_span_index_types = collect_checked_span_index_types(nullable_only: true) nullable_span_index_types.each do |type| lines.concat(emit_nullable_span_index_helper(type)) lines << "" end str_literals = collect_str_literals unless str_literals.empty? @str_literal_map = {} str_literals.each_with_index { |value, i| @str_literal_map[value] = str_literal_name(i) } lines.concat(emit_str_literal_constants(str_literals)) lines << "" end emitted_functions.each do |function| lines.concat(emit_function(function)) lines << "" end lines.join("\n").rstrip + "\n" end |