view python/ppci/buildtasks.py @ 332:87feb8a23b4d

Added task list command
author Windel Bouwman
date Fri, 07 Feb 2014 12:51:55 +0100
parents a78b41ff6ad2
children 6f4753202b9a
line wrap: on
line source


"""
Defines task classes that can compile, link etc..
Task can depend upon one another.
"""

import logging

from .c3 import Builder
from .irutils import Verifier
from .codegen import CodeGenerator
from .transform import CleanPass, RemoveAddZero
from .tasks import Task
from . import DiagnosticsManager


class BuildTask(Task):
    def __init__(self, name):
        super().__init__(name)
        self.logger = logging.getLogger('buildtask')


class Assemble(BuildTask):
    def __init__(self):
        super().__init__('Assemble')

    def run(self):
        pass


class Compile(BuildTask):
    """ Task that compiles source to some target """
    def __init__(self, sources, includes, target, output_object):
        super().__init__('Compile')
        self.sources = sources
        self.includes = includes
        self.target = target
        self.output = output_object

    def run(self):
        self.logger.info('Zcc started {}'.format(self.sources))
        diag = DiagnosticsManager()
        c3b = Builder(diag, self.target)
        cg = CodeGenerator(self.target)

        for ircode in c3b.build(self.sources, self.includes):
            if not ircode:
                return

            d = {'ircode':ircode}
            self.logger.info('Verifying code {}'.format(ircode), extra=d)
            Verifier().verify(ircode)

            # Optimization passes:
            CleanPass().run(ircode)
            Verifier().verify(ircode)
            RemoveAddZero().run(ircode)
            Verifier().verify(ircode)
            CleanPass().run(ircode)
            Verifier().verify(ircode)

            # Code generation:
            d = {'ircode':ircode}
            self.logger.info('Starting code generation for {}'.format(ircode), extra=d)
            cg.generate(ircode, self.output)

        # TODO: fixup references, do this in another way?
        self.output.backpatch()
        if not c3b.ok:
            diag.printErrors()
            raise TaskError('Compile errors')


class Link(BuildTask):
    def __init__(self, objects, output_file):
        super().__init__('Link')


class ObjCopy(Task):
    pass