Mercurial > lcfOS
view python/ppci/buildfunctions.py @ 377:9667d78ba79e
Switched to xml for project description
author | Windel Bouwman |
---|---|
date | Fri, 11 Apr 2014 15:47:50 +0200 |
parents | |
children | 6df89163e114 |
line wrap: on
line source
""" This module contains a set of handy functions to invoke compilation, linking and assembling. """ import logging from .target import Target from .c3 import Builder from .irutils import Verifier from .codegen import CodeGenerator from .transform import CleanPass, RemoveAddZero from .linker import Linker from .target.target_list import targets from .outstream import BinaryOutputStream, MasterOutputStream from .outstream import LoggerOutputStream from .assembler import Assembler from .objectfile import ObjectFile, load_object from . import DiagnosticsManager, CompilerError def fix_target(tg): """ Try to return an instance of the Target class """ if isinstance(tg, Target): return tg elif isinstance(tg, str): if tg in targets: return targets[tg] raise TaskError('Invalid target {}'.format(tg)) def fix_file(f): """ Determine if argument is a file like object or make it so! """ if hasattr(f, 'read'): # Assume this is a file like object return f elif isinstance(f, str): return open(f, 'r') else: raise TaskError('cannot use {} as input'.format(f)) def fix_object(o): if isinstance(o, ObjectFile): return o elif isinstance(o, str): with open(o, 'r') as f: return load_object(f) else: raise TaskError('Cannot use {} as objectfile'.format(o)) def assemble(source, target): logger = logging.getLogger('assemble') target = fix_target(target) source = fix_file(source) output = ObjectFile() assembler = Assembler(target) logger.debug('Assembling into code section') o2 = BinaryOutputStream(output) o1 = LoggerOutputStream() ostream = MasterOutputStream([o1, o2]) ostream.select_section('code') assembler.assemble(source, ostream) return output def c3compile(sources, includes, target): """ Compile a set of sources """ logger = logging.getLogger('c3c') logger.debug('C3 compilation started') target = fix_target(target) sources = [fix_file(fn) for fn in sources] includes = [fix_file(fn) for fn in includes] output = ObjectFile() diag = DiagnosticsManager() c3b = Builder(diag, target) cg = CodeGenerator(target) o2 = BinaryOutputStream(output) o1 = LoggerOutputStream() o = MasterOutputStream([o1, o2]) for ircode in c3b.build(sources, includes): if not ircode: # Something went wrong, do not continue the code generation continue d = {'ircode':ircode} logger.debug('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} logger.debug('Starting code generation for {}'.format(ircode), extra=d) cg.generate(ircode, o) if not c3b.ok: diag.printErrors() raise TaskError('Compile errors') return output def link(objects, layout): objects = list(map(fix_object, objects)) linker = Linker() output_obj = linker.link(objects, layout) return output_obj