annotate 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
rev   line source
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
1
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
2 """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
3 This module contains a set of handy functions to invoke compilation,
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
4 linking
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
5 and assembling.
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
6 """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
7
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
8 import logging
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
9 from .target import Target
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
10 from .c3 import Builder
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
11 from .irutils import Verifier
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
12 from .codegen import CodeGenerator
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
13 from .transform import CleanPass, RemoveAddZero
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
14 from .linker import Linker
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
15 from .target.target_list import targets
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
16 from .outstream import BinaryOutputStream, MasterOutputStream
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
17 from .outstream import LoggerOutputStream
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
18 from .assembler import Assembler
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
19 from .objectfile import ObjectFile, load_object
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
20 from . import DiagnosticsManager, CompilerError
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
21
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
22 def fix_target(tg):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
23 """ Try to return an instance of the Target class """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
24 if isinstance(tg, Target):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
25 return tg
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
26 elif isinstance(tg, str):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
27 if tg in targets:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
28 return targets[tg]
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
29 raise TaskError('Invalid target {}'.format(tg))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
30
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
31 def fix_file(f):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
32 """ Determine if argument is a file like object or make it so! """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
33 if hasattr(f, 'read'):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
34 # Assume this is a file like object
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
35 return f
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
36 elif isinstance(f, str):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
37 return open(f, 'r')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
38 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
39 raise TaskError('cannot use {} as input'.format(f))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
40
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
41 def fix_object(o):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
42 if isinstance(o, ObjectFile):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
43 return o
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
44 elif isinstance(o, str):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
45 with open(o, 'r') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
46 return load_object(f)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
47 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
48 raise TaskError('Cannot use {} as objectfile'.format(o))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
49
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
50
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
51 def assemble(source, target):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
52 logger = logging.getLogger('assemble')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
53 target = fix_target(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
54 source = fix_file(source)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
55 output = ObjectFile()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
56 assembler = Assembler(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
57 logger.debug('Assembling into code section')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
58 o2 = BinaryOutputStream(output)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
59 o1 = LoggerOutputStream()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
60 ostream = MasterOutputStream([o1, o2])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
61 ostream.select_section('code')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
62 assembler.assemble(source, ostream)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
63 return output
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
64
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
65
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
66 def c3compile(sources, includes, target):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
67 """ Compile a set of sources """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
68 logger = logging.getLogger('c3c')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
69 logger.debug('C3 compilation started')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
70 target = fix_target(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
71 sources = [fix_file(fn) for fn in sources]
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
72 includes = [fix_file(fn) for fn in includes]
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
73 output = ObjectFile()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
74 diag = DiagnosticsManager()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
75 c3b = Builder(diag, target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
76 cg = CodeGenerator(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
77
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
78 o2 = BinaryOutputStream(output)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
79 o1 = LoggerOutputStream()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
80 o = MasterOutputStream([o1, o2])
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
81
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
82 for ircode in c3b.build(sources, includes):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
83 if not ircode:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
84 # Something went wrong, do not continue the code generation
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
85 continue
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
86
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
87 d = {'ircode':ircode}
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
88 logger.debug('Verifying code {}'.format(ircode), extra=d)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
89 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
90
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
91 # Optimization passes:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
92 CleanPass().run(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
93 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
94 RemoveAddZero().run(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
95 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
96 CleanPass().run(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
97 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
98
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
99 # Code generation:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
100 d = {'ircode':ircode}
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
101 logger.debug('Starting code generation for {}'.format(ircode), extra=d)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
102
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
103 cg.generate(ircode, o)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
104
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
105 if not c3b.ok:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
106 diag.printErrors()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
107 raise TaskError('Compile errors')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
108 return output
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
109
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
110
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
111 def link(objects, layout):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
112 objects = list(map(fix_object, objects))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
113 linker = Linker()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
114 output_obj = linker.link(objects, layout)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
115 return output_obj