Class: Stone::RTTI

Inherits:
Object
  • Object
show all
Defined in:
lib/stone/rtti.rb

Overview

Runtime Type Information (RTTI) infrastructure. Generates global type constants in LLVM IR for runtime type introspection.

Type kind enum:

0 = primitive (Int, Bool, String, Null)
1 = record
2 = union
3 = function
4 = type (metatype)

Constant Summary collapse

KIND_PRIMITIVE =
0
KIND_RECORD =
1
KIND_UNION =
2
KIND_FUNCTION =
3
KIND_TYPE =
4

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod) ⇒ RTTI



22
23
24
# File 'lib/stone/rtti.rb', line 22

def initialize(mod)
  @mod = mod
end

Class Method Details

.create_type_struct_typeObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/stone/rtti.rb', line 50

def self.create_type_struct_type
  # %Stone.Type = type { ptr, i64, i8, ptr }
  # Fields: name (string ptr), size (bytes), kind (enum), fields (FieldList ptr)
  LLVM::Type.struct([
    LLVM::Type.pointer,  # name - pointer to null-terminated string
    LLVM::Int64.type,    # size - size in bytes
    LLVM::Int8.type,     # kind - type kind enum
    LLVM::Type.pointer   # fields - pointer to FieldList (or null for primitives)
  ], false)
end

.field_list_struct_typeObject

%Stone.FieldList = type { ptr, ptr, ptr } Fields: name (string ptr), field_type (Type ptr), tail (FieldList ptr or null)



63
64
65
66
67
68
69
# File 'lib/stone/rtti.rb', line 63

def self.field_list_struct_type
  @field_list_struct_type ||= LLVM::Type.struct([
    LLVM::Type.pointer,  # name - pointer to null-terminated string
    LLVM::Type.pointer,  # field_type - pointer to Type constant
    LLVM::Type.pointer   # tail - pointer to next FieldList (or null)
  ], false)
end

.generate_record_type_constant(mod, record_name, size_bytes, fields = []) ⇒ Object

Generate a type constant for a record type with field information



113
114
115
116
117
118
# File 'lib/stone/rtti.rb', line 113

def self.generate_record_type_constant(mod, record_name, size_bytes, fields = [])
  rtti = new(mod)
  rtti.__send__(:define_type_struct)
  fields_ptr = rtti.__send__(:generate_field_list, record_name, fields)
  rtti.__send__(:generate_type_constant, record_name, size_bytes, KIND_RECORD, fields_ptr)
end

.type_constant_for(mod, type) ⇒ Object

Get or create a type constant for the given Stone type



32
33
34
35
# File 'lib/stone/rtti.rb', line 32

def self.type_constant_for(mod, type)
  name = type_constant_name(type)
  mod.globals[name] || create_type_constant(mod, type)
end

.type_constant_name(type) ⇒ Object



37
38
39
# File 'lib/stone/rtti.rb', line 37

def self.type_constant_name(type)
  "Stone.Type.#{type.name}"
end

.type_struct_typeObject

Cache the struct type at the class level since it's the same for all modules



46
47
48
# File 'lib/stone/rtti.rb', line 46

def self.type_struct_type
  @type_struct_type ||= create_type_struct_type
end

Instance Method Details

#setupObject



26
27
28
29
# File 'lib/stone/rtti.rb', line 26

def setup
  define_type_struct
  generate_primitive_type_constants
end