Skip to content

builder

Modules:

Classes:

Functions:

Builder

Builder()

Bases: Builder

Methods:

Source code in src/irx/builder/backend.py
60
61
62
63
64
65
def __init__(self) -> None:
    """
    title: Initialize Builder.
    """
    super().__init__()
    self.translator = self._new_translator()

activate_runtime_feature

activate_runtime_feature(feature_name: str) -> None
Source code in src/irx/builder/base.py
250
251
252
253
254
255
256
257
def activate_runtime_feature(self, feature_name: str) -> None:
    """
    title: Activate a native runtime feature for this compilation unit.
    parameters:
      feature_name:
        type: str
    """
    self.runtime_feature_names.add(feature_name)

build

build(node: AST, output_file: str) -> None
Source code in src/irx/builder/backend.py
105
106
107
108
109
110
111
112
113
114
115
def build(self, node: astx.AST, output_file: str) -> None:
    """
    title: Build.
    parameters:
      node:
        type: astx.AST
      output_file:
        type: str
    """
    result = self.translate(node)
    self._build_from_ir(result, output_file)

build_modules

build_modules(
    root: ParsedModule,
    resolver: ImportResolver,
    output_file: str,
) -> None
Source code in src/irx/builder/backend.py
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def build_modules(
    self,
    root: ParsedModule,
    resolver: ImportResolver,
    output_file: str,
) -> None:
    """
    title: Build a reachable graph of parsed modules.
    parameters:
      root:
        type: ParsedModule
      resolver:
        type: ImportResolver
      output_file:
        type: str
    """
    result = self.translate_modules(root, resolver)
    self._build_from_ir(result, output_file)

module

module() -> Module
Source code in src/irx/builder/base.py
231
232
233
234
235
236
237
def module(self) -> astx.Module:
    """
    title: Create a new ASTx Module.
    returns:
      type: astx.Module
    """
    return astx.Module()

run

run(
    *,
    capture_stderr: bool = True,
    raise_on_error: bool = True,
    debug: bool = False,
) -> CommandResult
Source code in src/irx/builder/base.py
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def run(
    self,
    *,
    capture_stderr: bool = True,
    raise_on_error: bool = True,
    debug: bool = False,
) -> CommandResult:
    """
    title: Run the generated executable.
    parameters:
      capture_stderr:
        type: bool
        default: true
      raise_on_error:
        type: bool
        default: true
      debug:
        type: bool
        default: false
    returns:
      type: CommandResult
    """
    return run_command(
        [self.output_file],
        capture_stderr=capture_stderr,
        raise_on_error=raise_on_error,
        debug=debug,
    )

translate

translate(expr: AST) -> str
Source code in src/irx/builder/backend.py
75
76
77
78
79
80
81
82
83
84
85
def translate(self, expr: astx.AST) -> str:
    """
    title: Translate.
    parameters:
      expr:
        type: astx.AST
    returns:
      type: str
    """
    self.translator = self._new_translator()
    return self.translator.translate(expr)

translate_modules

translate_modules(
    root: ParsedModule, resolver: ImportResolver
) -> str
Source code in src/irx/builder/backend.py
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def translate_modules(
    self,
    root: ParsedModule,
    resolver: ImportResolver,
) -> str:
    """
    title: Translate a reachable graph of parsed modules.
    parameters:
      root:
        type: ParsedModule
      resolver:
        type: ImportResolver
    returns:
      type: str
    """
    self.translator = self._new_translator()
    return self.translator.translate_modules(root, resolver)

VariablesLLVM

Methods:

get_data_type

get_data_type(type_name: str) -> Type
Source code in src/irx/builder/types.py
 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def get_data_type(self, type_name: str) -> ir.types.Type:
    """
    title: Get data type.
    parameters:
      type_name:
        type: str
    returns:
      type: ir.types.Type
    """
    if type_name == "float32":
        return self.FLOAT_TYPE
    if type_name == "float16":
        return self.FLOAT16_TYPE
    if type_name in ("double", "float64"):
        return self.DOUBLE_TYPE
    if type_name == "boolean":
        return self.BOOLEAN_TYPE
    if type_name == "int8":
        return self.INT8_TYPE
    if type_name == "int16":
        return self.INT16_TYPE
    if type_name == "int32":
        return self.INT32_TYPE
    if type_name == "int64":
        return self.INT64_TYPE
    if type_name == "char":
        return self.INT8_TYPE
    if type_name in ("string", "stringascii"):
        return self.ASCII_STRING_TYPE
    if type_name == "utf8string":
        return self.UTF8_STRING_TYPE
    if type_name == "uint8":
        return self.UINT8_TYPE
    if type_name == "uint16":
        return self.UINT16_TYPE
    if type_name == "uint32":
        return self.UINT32_TYPE
    if type_name == "uint64":
        return self.UINT64_TYPE
    if type_name == "uint128":
        return self.UINT128_TYPE
    if type_name == "nonetype":
        return self.VOID_TYPE

    raise Exception(f"[EE]: Type name {type_name} not valid.")

emit_add

emit_add(
    ir_builder: IRBuilder,
    lhs: Value,
    rhs: Value,
    name: str = "addtmp",
) -> Instruction
Source code in src/irx/builder/vector.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
@typechecked
def emit_add(
    ir_builder: ir.IRBuilder,
    lhs: ir.Value,
    rhs: ir.Value,
    name: str = "addtmp",
) -> ir.Instruction:
    """
    title: Emit add.
    parameters:
      ir_builder:
        type: ir.IRBuilder
      lhs:
        type: ir.Value
      rhs:
        type: ir.Value
      name:
        type: str
    returns:
      type: ir.Instruction
    """
    if is_fp_type(lhs.type):
        return ir_builder.fadd(lhs, rhs, name=name)
    return ir_builder.add(lhs, rhs, name=name)

emit_int_div

emit_int_div(
    ir_builder: IRBuilder,
    lhs: Value,
    rhs: Value,
    unsigned: bool,
) -> Instruction
Source code in src/irx/builder/vector.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@typechecked
def emit_int_div(
    ir_builder: ir.IRBuilder,
    lhs: ir.Value,
    rhs: ir.Value,
    unsigned: bool,
) -> ir.Instruction:
    """
    title: Emit int div.
    parameters:
      ir_builder:
        type: ir.IRBuilder
      lhs:
        type: ir.Value
      rhs:
        type: ir.Value
      unsigned:
        type: bool
    returns:
      type: ir.Instruction
    """
    return (
        ir_builder.udiv(lhs, rhs, name="vdivtmp")
        if unsigned
        else ir_builder.sdiv(lhs, rhs, name="vdivtmp")
    )

is_fp_type

is_fp_type(type_: Type) -> bool
Source code in src/irx/builder/types.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@typechecked
def is_fp_type(type_: ir.Type) -> bool:
    """
    title: Is fp type.
    parameters:
      type_:
        type: ir.Type
    returns:
      type: bool
    """
    fp_types = [HalfType, FloatType, DoubleType]
    if FP128Type is not None:
        fp_types.append(FP128Type)
    return isinstance(type_, tuple(fp_types))

is_int_type

is_int_type(type_: Type) -> bool
Source code in src/irx/builder/types.py
34
35
36
37
38
39
40
41
42
43
44
@typechecked
def is_int_type(type_: ir.Type) -> bool:
    """
    title: Is int type.
    parameters:
      type_:
        type: ir.Type
    returns:
      type: bool
    """
    return isinstance(type_, ir.IntType)

is_vector

is_vector(value: Value) -> bool
Source code in src/irx/builder/vector.py
14
15
16
17
18
19
20
21
22
23
24
@typechecked
def is_vector(value: ir.Value) -> bool:
    """
    title: Is vector.
    parameters:
      value:
        type: ir.Value
    returns:
      type: bool
    """
    return isinstance(getattr(value, "type", None), VectorType)

safe_pop

safe_pop(
    values: list[Value | Function | None],
) -> Value | Function | None
Source code in src/irx/builder/runtime/__init__.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@typechecked
def safe_pop(
    values: list[ir.Value | ir.Function | None],
) -> ir.Value | ir.Function | None:
    """
    title: Safe pop.
    parameters:
      values:
        type: list[ir.Value | ir.Function | None]
    returns:
      type: ir.Value | ir.Function | None
    """
    try:
        return values.pop()
    except IndexError:
        return None

splat_scalar

splat_scalar(
    ir_builder: IRBuilder,
    scalar: Value,
    vec_type: VectorType,
) -> Value
Source code in src/irx/builder/vector.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@typechecked
def splat_scalar(
    ir_builder: ir.IRBuilder,
    scalar: ir.Value,
    vec_type: ir.VectorType,
) -> ir.Value:
    """
    title: Splat scalar.
    parameters:
      ir_builder:
        type: ir.IRBuilder
      scalar:
        type: ir.Value
      vec_type:
        type: ir.VectorType
    returns:
      type: ir.Value
    """
    zero_i32 = ir.Constant(ir.IntType(32), 0)
    undef_vec = ir.Constant(vec_type, ir.Undefined)
    vector_zero = ir_builder.insert_element(undef_vec, scalar, zero_i32)
    mask_ty = ir.VectorType(ir.IntType(32), vec_type.count)
    mask = ir.Constant(mask_ty, [0] * vec_type.count)
    return ir_builder.shuffle_vector(vector_zero, undef_vec, mask)