Closed AntonZelenin closed 1 year ago
There might be some missing context, but that's what I did
printf
function creation to be
ir.Function(
self.module,
ir.FunctionType(aye_types.INT8, [], var_arg=True),
name='printf',
)
Added dynamic generation of voidptr and c_fmt, the final Print class implementation looks like this:
class Print:
_global_fmt_cache = {}
def __init__(self, builder, module, value, printf):
self.builder = builder
self.module = module
self.value = value
self.printf = printf
def eval(self):
value = self.value.eval()
fmt_arg = self.builder.bitcast(
self._get_global_fmt(value.type),
value.type.as_pointer(),
)
self.builder.call(self.printf, [fmt_arg, value])
def _get_global_fmt(self, type_):
if type_ not in self._global_fmt_cache:
self._declare_global_fmt(type_)
return self._global_fmt_cache[type_]
def _declare_global_fmt(self, type_):
fmt = f'{_get_c_format(type_)} \n\0'
c_fmt = ir.Constant(
ir.ArrayType(aye_types.INT8, len(fmt)),
bytearray(fmt.encode('utf8'))
)
global_fmt = ir.GlobalVariable(self.module, c_fmt.type, name=f'fstr_{type_}')
global_fmt.linkage = 'internal'
global_fmt.global_constant = True
global_fmt.initializer = c_fmt
self._global_fmt_cache[type_] = global_fmt
def _get_cformat(type): if isinstance(type, ir.IntType): return '%i' elif isinstance(type, ir.DoubleType): return '%lf' raise ValueError(f'Unsupported type: {type}')
Hi, can I get general guidance on the correct way to implement the print function to be able to print different types:
It's not possible to define a function with the same name for different types, maybe I can somehow cast arguments to strings first and then always pass only strings?