Skip to content

system

Classes:

SystemVisitorMixin

Bases: VisitorMixinBase

Methods:

visit

visit(node: PrintExpr) -> None
Source code in src/irx/builder/lowering/system.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
125
126
127
@VisitorCore.visit.dispatch
def visit(self, node: astx.PrintExpr) -> None:
    """
    title: Visit PrintExpr nodes.
    parameters:
      node:
        type: astx.PrintExpr
    """
    self.visit_child(node.message)
    message_value = safe_pop(self.result_stack)
    if message_value is None:
        raise Exception("Invalid message in PrintExpr")

    message_source_type = self._resolved_ast_type(node.message)
    message_type = message_value.type
    ptr: ir.Value
    if (
        isinstance(message_type, ir.PointerType)
        and message_type.pointee == self._llvm.INT8_TYPE
    ):
        ptr = message_value
    elif is_int_type(message_type):
        int_arg, int_fmt = self._normalize_int_for_printf(
            message_value,
            unsigned=is_unsigned_type(message_source_type)
            or is_boolean_type(message_source_type),
        )
        int_fmt_gv = self._get_or_create_format_global(int_fmt)
        ptr = self._snprintf_heap(int_fmt_gv, [int_arg])
    elif isinstance(
        message_type, (ir.HalfType, ir.FloatType, ir.DoubleType)
    ):
        if isinstance(message_type, (ir.HalfType, ir.FloatType)):
            float_arg = self._llvm.ir_builder.fpext(
                message_value, self._llvm.DOUBLE_TYPE, "print_to_double"
            )
        else:
            float_arg = message_value
        float_fmt_gv = self._get_or_create_format_global("%.6f")
        ptr = self._snprintf_heap(float_fmt_gv, [float_arg])
    else:
        raise Exception(
            f"Unsupported message type in PrintExpr: {message_type}"
        )

    puts_fn = self.require_runtime_symbol("libc", "puts")
    self._llvm.ir_builder.call(puts_fn, [ptr])
    self.result_stack.append(ir.Constant(self._llvm.INT32_TYPE, 0))