Module: MilkTea::CBackend::TypeDeclaration

Included in:
MilkTea::CBackend
Defined in:
lib/milk_tea/core/c_backend/type_declaration.rb

Constant Summary collapse

C_KEYWORDS =
%w[
  auto break case char const continue default do double
  else enum extern float for goto if inline int long
  register restrict return short signed sizeof static
  struct switch typedef union unsigned void volatile while
  _Bool _Complex _Imaginary alignas alignof bool complex
  imaginary noreturn static_assert thread_local
].to_set.freeze

Instance Method Summary collapse

Instance Method Details

#emit_enum(enum_decl) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 115

def emit_enum(enum_decl)
  lines = ["typedef #{c_type(enum_decl.backing_type)} #{enum_decl.linkage_name};"]
  return lines if enum_decl.members.empty?

  lines << "enum {"
  enum_decl.members.each_with_index do |member, index|
    suffix = index == enum_decl.members.length - 1 ? "" : ","
    lines << "#{INDENT}#{member.linkage_name} = #{emit_expression(member.value)}#{suffix}"
  end
  lines << "};"
  lines
end

#emit_forward_declarations(opaque_decls, aggregate_decls) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 19

def emit_forward_declarations(opaque_decls, aggregate_decls)
  lines = []
  opaque_decls.uniq { |opaque_decl| opaque_decl.linkage_name }.each do |opaque_decl|
    next unless opaque_decl.forward_declarable
    next unless opaque_decl.linkage_name.match?(/\A[A-Za-z_][A-Za-z0-9_]*\z/)

    lines << "typedef struct #{opaque_decl.linkage_name} #{opaque_decl.linkage_name};"
  end
  aggregate_decls.uniq { |aggregate_decl| [aggregate_decl.class.name, aggregate_decl.linkage_name] }.each do |aggregate_decl|
    case aggregate_decl
    when IR::StructDecl
      lines << "typedef struct #{aggregate_decl.linkage_name} #{aggregate_decl.linkage_name};"
    when IR::UnionDecl
      lines << "typedef union #{aggregate_decl.linkage_name} #{aggregate_decl.linkage_name};"
    when IR::VariantDecl
      lines << "typedef struct #{aggregate_decl.linkage_name} #{aggregate_decl.linkage_name};"
      aggregate_decl.arms.each do |arm|
        next if arm.fields.empty?
        lines << "typedef struct #{arm.linkage_name} #{arm.linkage_name};"
      end
    end
  end
  lines
end

#emit_function(function) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 149

def emit_function(function)
  @checked_index_alias_stack.clear
  @checked_index_alias_id = 0
  @suppressed_labels = []
  body = compact_generated_statement_sequence(function.body)
  lines = ["#{function_signature(function)} {"]
  used_labels = collect_used_labels(body)
  if body.empty?
    lines << "}"
  else
    lines.concat(emit_statement_sequence(body, 1, function:, used_labels:))
    lines << "}"
  end
  lines
end

#emit_function_declarations(functions) ⇒ Object



128
129
130
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 128

def emit_function_declarations(functions)
  functions.map { |function| "#{function_signature(function)};" }
end

#emit_simd_type(type) ⇒ Object



194
195
196
197
198
199
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 194

def emit_simd_type(type)
  name = simd_type_name(type)
  elem_c = primitive_c_type(type.element_type.name)
  width = type.lane_count * Layout.primitive_layout(type.element_type).first
  ["typedef #{elem_c} #{name} __attribute__((__vector_size__(#{width})));"]
end

#emit_soa_type(type) ⇒ Object



184
185
186
187
188
189
190
191
192
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 184

def emit_soa_type(type)
  name = soa_type_name(type)
  lines = ["typedef struct #{name} {"]
  type.fields.each do |field_name, field_type|
    lines << "#{INDENT}#{c_field_declaration(field_type, sanitize_c_identifier(field_name))};"
  end
  lines << "} #{name};"
  lines
end

#emit_span_type(type) ⇒ Object



174
175
176
177
178
179
180
181
182
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 174

def emit_span_type(type)
  span_type = span_type_name(type)
  [
    "typedef struct #{span_type} {",
    "#{INDENT}#{c_declaration(pointer_to(type.element_type), 'data')};",
    "#{INDENT}#{c_declaration(Types::Registry.primitive('ptr_uint'), 'len')};",
    "} #{span_type};",
  ]
end

#emit_string_typeObject



165
166
167
168
169
170
171
172
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 165

def emit_string_type
  [
    "typedef struct mt_str {",
    "#{INDENT}char* data;",
    "#{INDENT}uintptr_t len;",
    "} mt_str;",
  ]
end

#emit_struct(struct_decl) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 44

def emit_struct(struct_decl)
  lines = []
  lines << "struct #{struct_decl.linkage_name} {"
  struct_decl.fields.each do |field|
    lines << "#{INDENT}#{c_field_declaration(field.type, sanitize_c_identifier(field.name))};"
  end
  lines << "}#{struct_layout_attributes(struct_decl)};"
  lines
end

#emit_union(union_decl) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 54

def emit_union(union_decl)
  lines = []
  lines << "union #{union_decl.linkage_name} {"
  union_decl.fields.each do |field|
    lines << "#{INDENT}#{c_field_declaration(field.type, sanitize_c_identifier(field.name))};"
  end
  lines << "};"
  lines
end

#emit_variant(variant_decl) ⇒ Object



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
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 64

def emit_variant(variant_decl)
  lines = []
  outer_c = variant_decl.linkage_name
  payload_arms = variant_decl.arms.select { |a| a.fields.any? }

    # Per-arm payload structs
    payload_arms.each do |arm|
      lines << "struct #{arm.linkage_name} {"
      arm.fields.each do |field|
        if aggregate_field_creates_cycle?(field.type, outer_c)
          if array_type?(field.type)
            lines << "#{INDENT}#{c_declaration(array_element_type(field.type), "*#{sanitize_c_identifier(field.name)}")};"
          else
            lines << "#{INDENT}#{c_declaration(field.type, "*#{sanitize_c_identifier(field.name)}")};"
          end
        else
          lines << "#{INDENT}#{c_field_declaration(field.type, sanitize_c_identifier(field.name))};"
        end
      end
      lines << "};"
    end


  # Kind enum
  lines << "typedef int32_t #{outer_c}_kind;"
  unless variant_decl.arms.empty?
    lines << "enum {"
    variant_decl.arms.each_with_index do |arm, index|
      suffix = index == variant_decl.arms.length - 1 ? "" : ","
      lines << "#{INDENT}#{outer_c}_kind_#{arm.name} = #{index}#{suffix}"
    end
    lines << "};"
  end

  # Data union (only if at least one arm has payload)
  if payload_arms.any?
    lines << "union #{outer_c}_data {"
    payload_arms.each do |arm|
      lines << "#{INDENT}struct #{arm.linkage_name} #{sanitize_c_identifier(arm.name)};"
    end
    lines << "};"
  end

  # Outer struct
  lines << "struct #{outer_c} {"
  lines << "#{INDENT}#{outer_c}_kind kind;"
  lines << "#{INDENT}union #{outer_c}_data data;" if payload_arms.any?
  lines << "};"
  lines
end

#emit_vector_math_typesObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 210

def emit_vector_math_types
  lines = []
  lines << "typedef struct mt_vec2 { float x; float y; } mt_vec2;"
  lines << "typedef struct mt_vec3 { float x; float y; float z; } mt_vec3;"
  lines << "typedef struct mt_vec4 { float x; float y; float z; float w; } mt_vec4;"
  lines << "typedef struct mt_ivec2 { int32_t x; int32_t y; } mt_ivec2;"
  lines << "typedef struct mt_ivec3 { int32_t x; int32_t y; int32_t z; } mt_ivec3;"
  lines << "typedef struct mt_ivec4 { int32_t x; int32_t y; int32_t z; int32_t w; } mt_ivec4;"
  lines << ""
  lines << "typedef struct mt_mat3 { mt_vec3 col0; mt_vec3 col1; mt_vec3 col2; } mt_mat3;"
  lines << "typedef struct mt_mat4 { mt_vec4 col0; mt_vec4 col1; mt_vec4 col2; mt_vec4 col3; } mt_mat4;"
  lines << "typedef struct mt_quat { float x; float y; float z; float w; } mt_quat;"
  lines
end

#function_params(function) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 137

def function_params(function)
  params = []
  params << array_out_param_declaration(function.return_type, ARRAY_OUT_PARAM_NAME) if array_type?(function.return_type)
  params.concat(emitted_function_params(function).map { |param| c_declaration(param.pointer ? pointer_to(param.type) : param.type, param.linkage_name) })

  if params.empty?
    "void"
  else
    params.join(", ")
  end
end

#function_signature(function) ⇒ Object



132
133
134
135
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 132

def function_signature(function)
  prefix = function.entry_point ? "" : "static "
  "#{prefix}#{c_function_declaration(function.return_type, function.linkage_name, function_params(function))}"
end

#sanitize_c_identifier(name) ⇒ Object



15
16
17
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 15

def sanitize_c_identifier(name)
  C_KEYWORDS.include?(name) ? "#{name}_" : name
end

#struct_layout_attributes(struct_decl) ⇒ Object



201
202
203
204
205
206
207
208
# File 'lib/milk_tea/core/c_backend/type_declaration.rb', line 201

def struct_layout_attributes(struct_decl)
  attributes = []
  attributes << "packed" if struct_decl.packed
  attributes << "aligned(#{struct_decl.alignment})" if struct_decl.alignment
  return "" if attributes.empty?

  " __attribute__((#{attributes.join(', ')}))"
end