Module: MilkTea::Types::Layout

Defined in:
lib/milk_tea/core/types/layout.rb

Constant Summary collapse

POINTER_SIZE =
Fiddle::SIZEOF_VOIDP

Class Method Summary collapse

Class Method Details

.aggregate_reaches_in_progress?(type, stack, visited) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
200
201
202
203
204
# File 'lib/milk_tea/core/types/layout.rb', line 197

def self.aggregate_reaches_in_progress?(type, stack, visited)
  return false if visited.key?(type.object_id)

  visited[type.object_id] = true
  return true if stack.key?(type)

  reachability_children(type).any? { |child| aggregate_reaches_in_progress?(child, stack, visited) }
end

.align_up(value, alignment) ⇒ Object



281
282
283
284
285
286
# File 'lib/milk_tea/core/types/layout.rb', line 281

def self.align_up(value, alignment)
  return value if alignment <= 1

  remainder = value % alignment
  remainder.zero? ? value : value + alignment - remainder
end

.alignment_of(type) ⇒ Object



16
17
18
# File 'lib/milk_tea/core/types/layout.rb', line 16

def self.alignment_of(type)
  size_and_alignment(type, nil)&.last
end

.array_element_type(type) ⇒ Object



332
333
334
# File 'lib/milk_tea/core/types/layout.rb', line 332

def self.array_element_type(type)
  type.arguments.first
end

.array_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


250
251
252
# File 'lib/milk_tea/core/types/layout.rb', line 250

def self.array_type?(type)
  type.arguments.length == 2 && type.arguments[1].is_a?(Types::LiteralTypeArg) && type.arguments[1].value.is_a?(Integer)
end

.cyclic_back_edge?(field_type, stack) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
194
195
# File 'lib/milk_tea/core/types/layout.rb', line 191

def self.cyclic_back_edge?(field_type, stack)
  return false if stack.nil? || stack.empty?

  aggregate_reaches_in_progress?(field_type, stack, {})
end

.explicit_alignment(type) ⇒ Object



246
247
248
# File 'lib/milk_tea/core/types/layout.rb', line 246

def self.explicit_alignment(type)
  type.respond_to?(:alignment) ? type.alignment : nil
end

.field_size_and_alignment(field_type, stack) ⇒ Object

A field whose type reaches any aggregate on the current path is a storage-cycle back edge. The C backend emits such fields as pointers (element pointer for arrays), so they occupy pointer-size storage here.



185
186
187
188
189
# File 'lib/milk_tea/core/types/layout.rb', line 185

def self.field_size_and_alignment(field_type, stack)
  return [POINTER_SIZE, POINTER_SIZE] if cyclic_back_edge?(field_type, stack)

  size_and_alignment(field_type, stack)
end

.generic_layout(type, stack) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/milk_tea/core/types/layout.rb', line 125

def self.generic_layout(type, stack)
  case type.name
  when "ptr", "const_ptr", "own", "ref"
    [POINTER_SIZE, POINTER_SIZE]
  when "array"
    return unless array_type?(type)

    element_layout = size_and_alignment(type.arguments.first, stack)
    return unless element_layout

    [element_layout.first * type.arguments[1].value, element_layout.last]
  when "str_buffer"
    return unless str_buffer_type?(type)

    with_stack(type, stack) do |next_stack|
      layout = struct_layout(str_buffer_fields(type), packed: false, alignment: nil, stack: next_stack)
      [layout[:size], layout[:alignment]]
    end
  end
end

.layout_pointer_like_nullable_base?(base) ⇒ Boolean

Returns:

  • (Boolean)


267
268
269
270
271
272
# File 'lib/milk_tea/core/types/layout.rb', line 267

def self.layout_pointer_like_nullable_base?(base)
  return true if base.is_a?(Types::Function) || base.is_a?(Types::Proc) || base.is_a?(Types::Opaque)
  return true if base.is_a?(Types::Primitive) && base.name == "cstr"

  base.is_a?(Types::GenericInstance) && %w[ptr const_ptr own ref].include?(base.name)
end

.nullable_opt_layout_fields(base) ⇒ Object



274
275
276
277
278
279
# File 'lib/milk_tea/core/types/layout.rb', line 274

def self.nullable_opt_layout_fields(base)
  {
    "has_value" => Types::Registry.primitive("bool"),
    "value" => base,
  }
end

.offset_of(type, field_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/milk_tea/core/types/layout.rb', line 20

def self.offset_of(type, field_name)
  case type
  when Types::Struct, Types::StructInstance, Types::Span, Types::StringView, Types::Task
    fields = ordered_fields(type)
    return unless fields.key?(field_name)

    struct_layout(fields, packed: packed_layout?(type), alignment: explicit_alignment(type), stack: {})[:offsets][field_name]
  when Types::Union
    type.field(field_name) ? 0 : nil
  when Types::GenericInstance
    return unless str_buffer_type?(type)

    fields = str_buffer_fields(type)
    return unless fields.key?(field_name)

    struct_layout(fields, packed: false, alignment: nil, stack: {})[:offsets][field_name]
  else
    nil
  end
end

.ordered_fields(type) ⇒ Object



238
239
240
# File 'lib/milk_tea/core/types/layout.rb', line 238

def self.ordered_fields(type)
  type.fields
end

.packed_layout?(type) ⇒ Boolean

Returns:

  • (Boolean)


242
243
244
# File 'lib/milk_tea/core/types/layout.rb', line 242

def self.packed_layout?(type)
  type.respond_to?(:packed) && type.packed
end

.pointer_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


328
329
330
# File 'lib/milk_tea/core/types/layout.rb', line 328

def self.pointer_type?(type)
  type.is_a?(Types::GenericInstance) && %w[ptr const_ptr own ref].include?(type.name)
end

.primitive_layout(type) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/milk_tea/core/types/layout.rb', line 108

def self.primitive_layout(type)
  case type.name
  when "bool", "byte", "ubyte", "char"
    [1, 1]
  when "short", "ushort"
    [2, 2]
  when "int", "uint", "float"
    [4, 4]
  when "long", "ulong", "double"
    [8, 8]
  when "ptr_int", "ptr_uint", "cstr"
    [POINTER_SIZE, POINTER_SIZE]
  else
    nil
  end
end

.reachability_children(type) ⇒ Object

Structural expansion used for back-edge detection. Mirrors the CBackend's aggregate_type_dependencies so pointer-broken cyclic fields agree with the emitted C struct layout.



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/milk_tea/core/types/layout.rb', line 303

def self.reachability_children(type)
  case type
  when Types::Nullable
    [type.base]
  when Types::GenericInstance
    if pointer_type?(type)
      []
    elsif array_type?(type)
      [array_element_type(type)]
    else
      []
    end
  when Types::Span
    [type.element_type]
  when Types::Struct, Types::StructInstance, Types::Union
    type.fields.values
  when Types::Variant, Types::VariantInstance
    type.arm_names.flat_map { |name| type.arm(name).values }
  when Types::VariantArmPayload
    type.fields.values
  else
    []
  end
end

.size_and_alignment(type, stack) ⇒ Object

Computes [size, alignment] for a type.

stack carries the aggregates on the current root-to-leaf path, pass-down only (never popped). A field whose type reaches any aggregate on that path participates in a storage cycle, and the C backend embeds exactly those fields as pointers (see CBackend#build_cyclic_aggregate_pairs and the cyclic field emission in type_declaration.rb), so layout sizes back-edge fields as raw pointers.



49
50
51
52
53
54
55
56
57
58
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
# File 'lib/milk_tea/core/types/layout.rb', line 49

def self.size_and_alignment(type, stack)
  case type
  when Types::Primitive
    primitive_layout(type)
  when Types::EnumBase
    size_and_alignment(type.backing_type, stack)
  when Types::Nullable, Types::Function
    if type.is_a?(Types::Function) || layout_pointer_like_nullable_base?(type.base)
      [POINTER_SIZE, POINTER_SIZE]
    else
      with_stack(type, stack) do |next_stack|
        layout = struct_layout(nullable_opt_layout_fields(type.base), packed: false, alignment: nil, stack: next_stack)
        [layout[:size], layout[:alignment]]
      end
    end
  when Types::StringView, Types::Span, Types::Task, Types::Struct, Types::StructInstance
    with_stack(type, stack) do |next_stack|
      layout = struct_layout(ordered_fields(type), packed: packed_layout?(type), alignment: explicit_alignment(type), stack: next_stack)
      [layout[:size], layout[:alignment]]
    end
  when Types::Union
    with_stack(type, stack) do |next_stack|
      layout = union_layout(ordered_fields(type), packed: packed_layout?(type), alignment: explicit_alignment(type), stack: next_stack)
      [layout[:size], layout[:alignment]]
    end
  when Types::Variant, Types::VariantInstance
    with_stack(type, stack) do |next_stack|
      layout = variant_layout(type, stack: next_stack)
      [layout[:size], layout[:alignment]]
    end
  when Types::GenericInstance
    generic_layout(type, stack)
  when Types::Simd
    element_layout = size_and_alignment(type.element_type, stack)
    return unless element_layout

    total = element_layout.first * type.lane_count
    alignment = total > 16 ? 32 : 16
    [total, alignment]
  when Types::Vector
    element_layout = size_and_alignment(type.element_type, stack)
    return unless element_layout

    [element_layout.first * type.width, element_layout.last]
  when Types::Matrix
    element_layout = size_and_alignment(Types::BUILTIN_VECTOR_ELEMENT, stack)
    return unless element_layout

    [element_layout.first * type.dim * type.dim, element_layout.last]
  when Types::Quaternion
    element_layout = size_and_alignment(Types::BUILTIN_VECTOR_ELEMENT, stack)
    return unless element_layout

    [element_layout.first * 4, element_layout.last]
  else
    nil
  end
end

.size_of(type) ⇒ Object



12
13
14
# File 'lib/milk_tea/core/types/layout.rb', line 12

def self.size_of(type)
  size_and_alignment(type, nil)&.first
end

.str_buffer_fields(type) ⇒ Object



258
259
260
261
262
263
264
265
# File 'lib/milk_tea/core/types/layout.rb', line 258

def self.str_buffer_fields(type)
  storage_capacity = type.arguments.first.value + 1
  {
    "data" => Types::Registry.generic_instance("array", [Types::Registry.primitive("char"), Types::LiteralTypeArg.new(storage_capacity)]),
    "len" => Types::Registry.primitive("ptr_uint"),
    "dirty" => Types::Registry.primitive("bool"),
  }
end

.str_buffer_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


254
255
256
# File 'lib/milk_tea/core/types/layout.rb', line 254

def self.str_buffer_type?(type)
  type.arguments.length == 1 && type.arguments.first.is_a?(Types::LiteralTypeArg) && type.arguments.first.value.is_a?(Integer)
end

.struct_layout(fields, packed:, alignment:, stack:) ⇒ Object



160
161
162
163
164
165
166
167
168
169
# File 'lib/milk_tea/core/types/layout.rb', line 160

def self.struct_layout(fields, packed:, alignment:, stack:)
  field_infos = fields.map do |field_name, field_type|
    field_layout = field_size_and_alignment(field_type, stack)
    return nil unless field_layout

    [field_name, field_layout.first, field_layout.last]
  end

  struct_layout_from_infos(field_infos, packed:, alignment:)
end

.struct_layout_from_infos(field_infos, packed:, alignment:) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/milk_tea/core/types/layout.rb', line 206

def self.struct_layout_from_infos(field_infos, packed:, alignment:)
  offsets = {}
  offset = 0
  natural_alignment = 1

  field_infos.each do |field_name, field_size, field_alignment|
    effective_alignment = packed ? 1 : field_alignment
    natural_alignment = [natural_alignment, effective_alignment].max
    offset = align_up(offset, effective_alignment) unless packed
    offsets[field_name] = offset
    offset += field_size
  end

  overall_alignment = [natural_alignment, alignment || 1].max
  {
    size: align_up(offset, overall_alignment),
    alignment: overall_alignment,
    offsets:,
  }
end

.union_layout(fields, packed:, alignment:, stack:) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/milk_tea/core/types/layout.rb', line 171

def self.union_layout(fields, packed:, alignment:, stack:)
  field_infos = fields.map do |_field_name, field_type|
    field_layout = field_size_and_alignment(field_type, stack)
    return nil unless field_layout

    field_layout
  end

  union_layout_from_layouts(field_infos.map { |size, field_alignment| { size:, alignment: field_alignment } }, packed:, alignment:)
end

.union_layout_from_layouts(layouts, packed:, alignment:) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/milk_tea/core/types/layout.rb', line 227

def self.union_layout_from_layouts(layouts, packed:, alignment:)
  natural_alignment = packed ? 1 : (layouts.map { |layout| layout[:alignment] }.max || 1)
  overall_alignment = [natural_alignment, alignment || 1].max
  size = layouts.map { |layout| layout[:size] }.max || 0

  {
    size: align_up(size, overall_alignment),
    alignment: overall_alignment,
  }
end

.variant_layout(type, stack:) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/milk_tea/core/types/layout.rb', line 146

def self.variant_layout(type, stack:)
  payload_layouts = type.arm_names.filter_map do |arm_name|
    fields = type.arm(arm_name)
    next if fields.nil? || fields.empty?

    struct_layout(fields, packed: false, alignment: nil, stack:)
  end

  data_layout = union_layout_from_layouts(payload_layouts, packed: false, alignment: nil)
  field_infos = [["kind", 4, 4]]
  field_infos << ["data", data_layout[:size], data_layout[:alignment]] if data_layout[:size].positive?
  struct_layout_from_infos(field_infos, packed: false, alignment: nil)
end

.with_stack(type, stack) {|next_stack| ... } ⇒ Object

stack is the set of aggregates on the current path, pass-down only. A nil or empty stack means the caller is not inside an aggregate yet. Yields the extended stack; returns nil when type is already in progress (callers treat that as unreachable while mirroring backend semantics).

Yields:

  • (next_stack)


292
293
294
295
296
297
298
# File 'lib/milk_tea/core/types/layout.rb', line 292

def self.with_stack(type, stack)
  next_stack = (stack || {}).dup
  return nil if next_stack.key?(type)

  next_stack[type] = true
  yield(next_stack)
end