Skip to content

core

Classes:

Functions:

VisitorCore

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

Bases: BuilderVisitor

Methods:

Source code in src/irx/builders/llvmliteir/core.py
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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.result_stack = []
    self.loop_stack = []
    self._set_value_ids = {}
    self.struct_types = {}
    self._fast_math_enabled = False

    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=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/builders/llvmliteir/core.py
247
248
249
250
251
252
253
254
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
) -> Any
Source code in src/irx/builders/llvmliteir/core.py
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
def create_entry_block_alloca(
    self,
    var_name: str,
    type_name: str,
) -> Any:
    """
    title: Create entry block alloca.
    parameters:
      var_name:
        type: str
      type_name:
        type: str
    returns:
      type: Any
    """
    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(
        self._llvm.get_data_type(type_name), 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/builders/llvmliteir/core.py
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
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.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/builders/llvmliteir/core.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
def initialize(self) -> None:
    """
    title: Initialize.
    """
    self._llvm = VariablesLLVM()
    self._llvm.module = ir.module.Module("Arx")
    self._llvm.context = self._llvm.module.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.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,
        ]
    )

require_runtime_symbol

require_runtime_symbol(
    feature_name: str, symbol_name: str
) -> Function
Source code in src/irx/builders/llvmliteir/core.py
256
257
258
259
260
261
262
263
264
265
266
267
268
269
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/builders/llvmliteir/core.py
503
504
505
506
507
508
509
510
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/builders/llvmliteir/core.py
234
235
236
237
238
239
240
241
242
243
244
245
def translate(self, node: astx.AST) -> str:
    """
    title: Translate.
    parameters:
      node:
        type: astx.AST
    returns:
      type: str
    """
    analyzed = analyze(node)
    self.visit(analyzed)
    return str(self._llvm.module)

visit

visit(node: AST) -> None
Source code in src/irx/builders/llvmliteir/core.py
224
225
226
227
228
229
230
231
232
@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/builders/base.py
165
166
167
168
169
170
171
172
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/builders/llvmliteir/core.py
48
49
50
51
52
53
54
55
56
57
58
59
@private
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/builders/llvmliteir/core.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
@private
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_flag

semantic_flag(
    node: AST, name: str, default: bool = False
) -> bool
Source code in src/irx/builders/llvmliteir/core.py
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
@private
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/builders/llvmliteir/core.py
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
@private
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_symbol_key

semantic_symbol_key(node: AST, fallback: str) -> str
Source code in src/irx/builders/llvmliteir/core.py
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@private
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/builders/llvmliteir/core.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
@private
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)