Skip to content

core

Classes:

Functions:

VisitorCore

VisitorCore(
    active_runtime_features: set[str] | None = None,
)

Bases: BuilderVisitor

Methods:

Source code in packages/irx/src/irx/builder/core.py
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
def __init__(
    self,
    active_runtime_features: set[str] | None = None,
) -> None:
    """
    title: Initialize VisitorCore.
    parameters:
      active_runtime_features:
        type: set[str] | None
    """
    super().__init__()
    self.named_values = {}
    self.const_vars = set()
    self.function_protos = {}
    self.llvm_functions_by_symbol_id = {}
    self.result_stack = []
    self.loop_stack = []
    self.cleanup_stack = []
    self._set_value_ids = {}
    self._buffer_view_global_counter = 0
    self.struct_types = {}
    self.llvm_structs_by_qualified_name = {}
    self._emitted_function_bodies = set()
    self._module_display_names = {}
    self._current_module_display_name = None
    self._interned_c_strings = {}
    self._c_string_global_counter = 0
    self._namespace_globals = {}
    self.entry_function_symbol_id = None
    self._fast_math_enabled = False
    self._current_function_return_type = None
    self._current_function_signature = None
    self._generator_frame_types = {}
    self._generator_frame_slots_by_symbol_id = {}
    self._generator_resume_functions = {}
    self._current_generator_frame_ptr = None
    self._current_generator_frame_slots = {}
    self._current_generator_out_ptr = None
    self._current_generator_next_state = None

    self.initialize()
    self.target = llvm.Target.from_default_triple()
    try:
        self.target_machine = self.target.create_target_machine(
            codemodel="small",
            reloc="pic",
        )
    except TypeError:
        self.target_machine = self.target.create_target_machine(
            codemodel="small"
        )

    self._llvm.module.triple = self.target_machine.triple
    self._llvm.module.data_layout = str(self.target_machine.target_data)

    if self._llvm.SIZE_T_TYPE is None:
        self._llvm.SIZE_T_TYPE = self._get_size_t_type_from_triple()

    self._add_builtins()
    self.runtime_features = RuntimeFeatureState(
        owner=cast(VisitorProtocol, self),
        registry=get_default_runtime_feature_registry(),
        active_features=active_runtime_features,
    )

activate_runtime_feature

activate_runtime_feature(feature_name: str) -> None
Source code in packages/irx/src/irx/builder/core.py
707
708
709
710
711
712
713
714
def activate_runtime_feature(self, feature_name: str) -> None:
    """
    title: Activate runtime feature.
    parameters:
      feature_name:
        type: str
    """
    self.runtime_features.activate(feature_name)

create_entry_block_alloca

create_entry_block_alloca(
    var_name: str, type_name: str | Type
) -> Any
Source code in packages/irx/src/irx/builder/core.py
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
def create_entry_block_alloca(
    self,
    var_name: str,
    type_name: str | ir.Type,
) -> Any:
    """
    title: Create entry block alloca.
    parameters:
      var_name:
        type: str
      type_name:
        type: str | ir.Type
    returns:
      type: Any
    """
    llvm_type = (
        self._llvm.get_data_type(type_name)
        if isinstance(type_name, str)
        else type_name
    )
    current_block = self._llvm.ir_builder.block
    self._llvm.ir_builder.position_at_start(
        self._llvm.ir_builder.function.entry_basic_block
    )
    alloca = self._llvm.ir_builder.alloca(llvm_type, None, var_name)
    if current_block is not None:
        self._llvm.ir_builder.position_at_end(current_block)
    return alloca

get_function

get_function(name: str) -> Function | None
Source code in packages/irx/src/irx/builder/core.py
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
def get_function(self, name: str) -> ir.Function | None:
    """
    title: Get function.
    parameters:
      name:
        type: str
    returns:
      type: ir.Function | None
    """
    if name in self.llvm_functions_by_symbol_id:
        return self.llvm_functions_by_symbol_id[name]

    if name in self._llvm.module.globals:
        return cast(ir.Function, self._llvm.module.get_global(name))

    if name in self.function_protos:
        self.visit(self.function_protos[name])
        return cast(ir.Function, safe_pop(self.result_stack))

    return None

initialize

initialize() -> None
Source code in packages/irx/src/irx/builder/core.py
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
def initialize(self) -> None:
    """
    title: Initialize.
    """
    self._llvm = VariablesLLVM()
    # Keep identified class/struct types isolated per translation so
    # reused semantic names never retain stale LLVM bodies.
    llvm_context = ir.Context()
    self._llvm.module = ir.module.Module(
        "Arx",
        context=llvm_context,
    )
    self._llvm.context = llvm_context
    self._init_native_size_types()

    llvm.initialize_all_targets()
    llvm.initialize_all_asmprinters()
    llvm.initialize_native_target()
    llvm.initialize_native_asmparser()
    llvm.initialize_native_asmprinter()

    self._llvm.ir_builder = ir.IRBuilder()
    self._llvm.FLOAT_TYPE = ir.FloatType()
    self._llvm.FLOAT16_TYPE = ir.HalfType()
    self._llvm.DOUBLE_TYPE = ir.DoubleType()
    self._llvm.BOOLEAN_TYPE = ir.IntType(1)
    self._llvm.INT8_TYPE = ir.IntType(8)
    self._llvm.INT16_TYPE = ir.IntType(16)
    self._llvm.INT32_TYPE = ir.IntType(32)
    self._llvm.INT64_TYPE = ir.IntType(64)
    self._llvm.UINT8_TYPE = ir.IntType(8)
    self._llvm.UINT16_TYPE = ir.IntType(16)
    self._llvm.UINT32_TYPE = ir.IntType(32)
    self._llvm.UINT64_TYPE = ir.IntType(64)
    self._llvm.UINT128_TYPE = ir.IntType(128)
    self._llvm.VOID_TYPE = ir.VoidType()
    self._llvm.ASCII_STRING_TYPE = ir.IntType(8).as_pointer()
    self._llvm.UTF8_STRING_TYPE = self._llvm.ASCII_STRING_TYPE
    self._llvm.OPAQUE_POINTER_TYPE = self._llvm.INT8_TYPE.as_pointer()
    self._llvm.BUFFER_OWNER_HANDLE_TYPE = self._llvm.OPAQUE_POINTER_TYPE
    buffer_view_type = self._llvm.module.context.get_identified_type(
        BUFFER_VIEW_TYPE_NAME
    )
    if buffer_view_type.is_opaque:
        buffer_view_type.set_body(
            self._llvm.OPAQUE_POINTER_TYPE,
            self._llvm.BUFFER_OWNER_HANDLE_TYPE,
            self._llvm.OPAQUE_POINTER_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT64_TYPE.as_pointer(),
            self._llvm.INT64_TYPE.as_pointer(),
            self._llvm.INT64_TYPE,
            self._llvm.INT32_TYPE,
        )
    self._llvm.BUFFER_VIEW_TYPE = buffer_view_type
    self._llvm.ARRAY_BUILDER_HANDLE_TYPE = self._llvm.OPAQUE_POINTER_TYPE
    self._llvm.ARRAY_HANDLE_TYPE = self._llvm.OPAQUE_POINTER_TYPE
    self._llvm.ARROW_ARRAY_BUILDER_HANDLE_TYPE = (
        self._llvm.ARRAY_BUILDER_HANDLE_TYPE
    )
    self._llvm.ARROW_ARRAY_HANDLE_TYPE = self._llvm.ARRAY_HANDLE_TYPE
    self._llvm.TENSOR_BUILDER_HANDLE_TYPE = self._llvm.OPAQUE_POINTER_TYPE
    self._llvm.TENSOR_HANDLE_TYPE = self._llvm.OPAQUE_POINTER_TYPE
    self._llvm.ARROW_TENSOR_BUILDER_HANDLE_TYPE = (
        self._llvm.TENSOR_BUILDER_HANDLE_TYPE
    )
    self._llvm.ARROW_TENSOR_HANDLE_TYPE = self._llvm.TENSOR_HANDLE_TYPE
    self._llvm.TIME_TYPE = ir.LiteralStructType(
        [
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
        ]
    )
    self._llvm.TIMESTAMP_TYPE = ir.LiteralStructType(
        [
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
        ]
    )
    self._llvm.DATETIME_TYPE = ir.LiteralStructType(
        [
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
            self._llvm.INT32_TYPE,
        ]
    )

llvm_function_name_for_node

llvm_function_name_for_node(
    node: AST, fallback: str
) -> str
Source code in packages/irx/src/irx/builder/core.py
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
def llvm_function_name_for_node(
    self,
    node: astx.AST,
    fallback: str,
) -> str:
    """
    title: Return the LLVM symbol name for a function node.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    function_key = semantic_function_key(node, fallback)
    if (
        self.entry_function_symbol_id is not None
        and function_key == self.entry_function_symbol_id
    ):
        return "main"
    return semantic_function_name(node, fallback)

require_runtime_symbol

require_runtime_symbol(
    feature_name: str, symbol_name: str
) -> Function
Source code in packages/irx/src/irx/builder/core.py
716
717
718
719
720
721
722
723
724
725
726
727
728
729
def require_runtime_symbol(
    self, feature_name: str, symbol_name: str
) -> ir.Function:
    """
    title: Require runtime symbol.
    parameters:
      feature_name:
        type: str
      symbol_name:
        type: str
    returns:
      type: ir.Function
    """
    return self.runtime_features.require_symbol(feature_name, symbol_name)

set_fast_math

set_fast_math(enabled: bool) -> None
Source code in packages/irx/src/irx/builder/core.py
 999
1000
1001
1002
1003
1004
1005
1006
def set_fast_math(self, enabled: bool) -> None:
    """
    title: Set fast math.
    parameters:
      enabled:
        type: bool
    """
    self._fast_math_enabled = enabled

translate

translate(node: AST) -> str
Source code in packages/irx/src/irx/builder/core.py
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
def translate(self, node: astx.AST) -> str:
    """
    title: Translate.
    parameters:
      node:
        type: astx.AST
    returns:
      type: str
    """
    analyzed = analyze(node)
    self._module_display_names = {}
    self._current_module_display_name = None
    self._interned_c_strings = {}
    self._c_string_global_counter = 0
    if isinstance(analyzed, astx.Module):
        self._module_display_names[id(analyzed)] = (
            getattr(analyzed, "name", "") or "<module>"
        )
        self._set_entry_function_from_module(analyzed)
        self._translate_modules([analyzed])
    else:
        self.visit(analyzed)
    return str(self._llvm.module)

translate_modules

translate_modules(
    root: ParsedModule, resolver: ImportResolver
) -> str
Source code in packages/irx/src/irx/builder/core.py
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
def translate_modules(
    self,
    root: ParsedModule,
    resolver: ImportResolver,
) -> str:
    """
    title: Translate a reachable graph of parsed modules to LLVM IR.
    parameters:
      root:
        type: ParsedModule
      resolver:
        type: ImportResolver
    returns:
      type: str
    """
    session = analyze_modules(root, resolver)
    ordered_modules = session.ordered_modules()
    self._module_display_names = {
        id(parsed_module.ast): (
            parsed_module.display_name
            or getattr(parsed_module.ast, "name", "")
            or str(parsed_module.key)
        )
        for parsed_module in ordered_modules
    }
    self._set_entry_function_from_module(root.ast)
    self._translate_modules(
        [parsed_module.ast for parsed_module in ordered_modules]
    )
    return str(self._llvm.module)

visit

visit(node: AST) -> None
Source code in packages/irx/src/irx/builder/core.py
441
442
443
444
445
446
447
448
449
@dispatch
def visit(self, node: astx.AST) -> None:
    """
    title: Visit AST nodes.
    parameters:
      node:
        type: astx.AST
    """
    super().visit(node)

visit_child

visit_child(node: AST) -> None
Source code in packages/irx/src/irx/builder/base.py
167
168
169
170
171
172
173
174
def visit_child(self, node: astx.AST) -> None:
    """
    title: Forward a child AST node through the public visit dispatcher.
    parameters:
      node:
        type: astx.AST
    """
    self.visit(node)

is_unsigned_node

is_unsigned_node(node: AST) -> bool
Source code in packages/irx/src/irx/builder/core.py
75
76
77
78
79
80
81
82
83
84
85
86
87
@private
@typechecked
def is_unsigned_node(node: astx.AST) -> bool:
    """
    title: Is unsigned node.
    parameters:
      node:
        type: astx.AST
    returns:
      type: bool
    """
    type_ = getattr(node, "type_", None)
    return isinstance(type_, astx.UnsignedInteger)

semantic_assignment_key

semantic_assignment_key(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
@private
@typechecked
def semantic_assignment_key(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic assignment key.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    assignment = getattr(semantic, "resolved_assignment", None)
    target = getattr(assignment, "target", None)
    symbol_id = getattr(target, "symbol_id", None)
    if symbol_id is not None:
        return cast(str, symbol_id)
    return fallback

semantic_class_key

semantic_class_key(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
@private
@typechecked
def semantic_class_key(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic class key.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    class_ = getattr(semantic, "resolved_class", None)
    qualified_name = getattr(class_, "qualified_name", None)
    if qualified_name is not None:
        return cast(str, qualified_name)
    return fallback

semantic_class_name

semantic_class_name(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
@private
@typechecked
def semantic_class_name(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic LLVM class-object name.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    class_ = getattr(semantic, "resolved_class", None)
    layout = getattr(class_, "layout", None)
    llvm_name = getattr(layout, "llvm_name", None)
    if isinstance(llvm_name, str) and llvm_name:
        return llvm_name
    module_key = getattr(class_, "module_key", None)
    name = getattr(class_, "name", None)
    if module_key is not None and name is not None:
        return mangle_class_name(module_key, name)
    return fallback

semantic_flag

semantic_flag(
    node: AST, name: str, default: bool = False
) -> bool
Source code in packages/irx/src/irx/builder/core.py
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
@private
@typechecked
def semantic_flag(node: astx.AST, name: str, default: bool = False) -> bool:
    """
    title: Semantic flag.
    parameters:
      node:
        type: astx.AST
      name:
        type: str
      default:
        type: bool
    returns:
      type: bool
    """
    semantic = getattr(node, "semantic", None)
    semantic_flags = getattr(semantic, "semantic_flags", None)
    if semantic_flags is not None and hasattr(semantic_flags, name):
        return bool(getattr(semantic_flags, name))
    return bool(getattr(node, name, default))

semantic_fma_rhs

semantic_fma_rhs(node: AST) -> AST | None
Source code in packages/irx/src/irx/builder/core.py
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
@private
@typechecked
def semantic_fma_rhs(node: astx.AST) -> astx.AST | None:
    """
    title: Semantic fma rhs.
    parameters:
      node:
        type: astx.AST
    returns:
      type: astx.AST | None
    """
    semantic = getattr(node, "semantic", None)
    semantic_flags = getattr(semantic, "semantic_flags", None)
    fma_rhs = getattr(semantic_flags, "fma_rhs", None)
    if fma_rhs is not None:
        return cast(astx.AST, fma_rhs)
    return getattr(node, "fma_rhs", None)

semantic_function_key

semantic_function_key(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
@private
@typechecked
def semantic_function_key(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic function key.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    function = getattr(semantic, "resolved_function", None)
    symbol_id = getattr(function, "symbol_id", None)
    if symbol_id is not None:
        return cast(str, symbol_id)
    return fallback

semantic_function_name

semantic_function_name(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
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
@private
@typechecked
def semantic_function_name(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic LLVM function name.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    function = getattr(semantic, "resolved_function", None)
    signature = getattr(function, "signature", None)
    signature_symbol_name = getattr(signature, "symbol_name", None)
    signature_is_extern = getattr(signature, "is_extern", False)
    module_key = getattr(function, "module_key", None)
    name = getattr(function, "name", None)
    if signature_is_extern and isinstance(signature_symbol_name, str):
        return signature_symbol_name
    if module_key is not None and name is not None:
        base_name = (
            signature_symbol_name
            if isinstance(signature_symbol_name, str) and signature_symbol_name
            else name
        )
        return mangle_function_name(module_key, base_name)
    return fallback

semantic_struct_key

semantic_struct_key(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
@private
@typechecked
def semantic_struct_key(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic struct key.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    struct = getattr(semantic, "resolved_struct", None)
    qualified_name = getattr(struct, "qualified_name", None)
    if qualified_name is not None:
        return cast(str, qualified_name)
    return fallback

semantic_struct_name

semantic_struct_name(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
@private
@typechecked
def semantic_struct_name(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic LLVM struct name.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    struct = getattr(semantic, "resolved_struct", None)
    module_key = getattr(struct, "module_key", None)
    name = getattr(struct, "name", None)
    if module_key is not None and name is not None:
        return mangle_struct_name(module_key, name)
    return fallback

semantic_symbol_key

semantic_symbol_key(node: AST, fallback: str) -> str
Source code in packages/irx/src/irx/builder/core.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
@private
@typechecked
def semantic_symbol_key(node: astx.AST, fallback: str) -> str:
    """
    title: Semantic symbol key.
    parameters:
      node:
        type: astx.AST
      fallback:
        type: str
    returns:
      type: str
    """
    semantic = getattr(node, "semantic", None)
    symbol = getattr(semantic, "resolved_symbol", None)
    symbol_id = getattr(symbol, "symbol_id", None)
    if symbol_id is not None:
        return cast(str, symbol_id)
    return fallback

uses_unsigned_semantics

uses_unsigned_semantics(node: AST) -> bool
Source code in packages/irx/src/irx/builder/core.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@private
@typechecked
def uses_unsigned_semantics(node: astx.AST) -> bool:
    """
    title: Uses unsigned semantics.
    parameters:
      node:
        type: astx.AST
    returns:
      type: bool
    """
    semantic = getattr(node, "semantic", None)
    semantic_flags = getattr(semantic, "semantic_flags", None)
    semantic_unsigned = getattr(semantic_flags, "unsigned", None)
    if semantic_unsigned is not None:
        return cast(bool, semantic_unsigned)

    explicit_unsigned = cast(bool | None, getattr(node, "unsigned", None))
    if explicit_unsigned is not None:
        return explicit_unsigned
    return is_unsigned_node(node)