Coder Social home page Coder Social logo

Comments (1)

AntonZelenin avatar AntonZelenin commented on July 20, 2024

There might be some missing context, but that's what I did

  1. Changed the printf function creation to be
ir.Function(
    self.module,
    ir.FunctionType(aye_types.INT8, [], var_arg=True),
    name='printf',
)
  1. 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_c_format(type_):
    if isinstance(type_, ir.IntType):
        return '%i'
    elif isinstance(type_, ir.DoubleType):
        return '%lf'
    raise ValueError(f'Unsupported type: {type}')

from pythoncompiler.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.