Skip to content

core

Classes:

Functions:

VisitorCore

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

Bases: BuilderVisitor

Methods:

Source code in src/irx/builder/core.py
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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
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._set_value_ids = {}
    self._buffer_view_global_counter = 0
    self.struct_types = {}
    self.llvm_structs_by_qualified_name = {}
    self._emitted_function_bodies = set()
    self.entry_function_symbol_id = None
    self._fast_math_enabled = False
    self._current_function_return_type = None
    self._current_function_signature = 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 src/irx/builder/core.py
531
532
533
534
535
536
537
538
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 src/irx/builder/core.py
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
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 src/irx/builder/core.py
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
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 src/irx/builder/core.py
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
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.ARROW_ARRAY_BUILDER_HANDLE_TYPE = (
        self._llvm.OPAQUE_POINTER_TYPE
    )
    self._llvm.ARROW_ARRAY_HANDLE_TYPE = self._llvm.OPAQUE_POINTER_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 src/irx/builder/core.py
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
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 src/irx/builder/core.py
540
541
542
543
544
545
546
547
548
549
550
551
552
553
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 src/irx/builder/core.py
815
816
817
818
819
820
821
822
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 src/irx/builder/core.py
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
def translate(self, node: astx.AST) -> str:
    """
    title: Translate.
    parameters:
      node:
        type: astx.AST
    returns:
      type: str
    """
    analyzed = analyze(node)
    if isinstance(analyzed, astx.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 src/irx/builder/core.py
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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)
    self._set_entry_function_from_module(root.ast)
    self._translate_modules(
        [parsed_module.ast for parsed_module in session.ordered_modules()]
    )
    return str(self._llvm.module)

visit

visit(node: AST) -> None
Source code in src/irx/builder/core.py
409
410
411
412
413
414
415
416
417
@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 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 src/irx/builder/core.py
69
70
71
72
73
74
75
76
77
78
79
80
81
@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 src/irx/builder/core.py
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
@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 src/irx/builder/core.py
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
@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 src/irx/builder/core.py
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
@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 src/irx/builder/core.py
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
@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 src/irx/builder/core.py
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@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 src/irx/builder/core.py
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
@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 src/irx/builder/core.py
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
@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 src/irx/builder/core.py
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
@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 src/irx/builder/core.py
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
@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 src/irx/builder/core.py
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
@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 src/irx/builder/core.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@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)