annotate python/ppci/buildfunctions.py @ 386:2a970e7270e2

Added repeat assembler macro
author Windel Bouwman
date Thu, 01 May 2014 17:40:59 +0200
parents d056b552d3f4
children 6ae782a085e0
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
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
15 from .layout import Layout, load_layout
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
16 from .target.target_list import targets
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
17 from .outstream import BinaryAndLoggingStream
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
18 from .objectfile import ObjectFile, load_object
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
19 from . import DiagnosticsManager, CompilerError
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
20 from .tasks import TaskError
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
21
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
22
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
23 def fix_target(tg):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
24 """ Try to return an instance of the Target class """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
25 if isinstance(tg, Target):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
26 return tg
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
27 elif isinstance(tg, str):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
28 if tg in targets:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
29 return targets[tg]
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
30 raise TaskError('Invalid target {}'.format(tg))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
31
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
32
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
33 def fix_file(f):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
34 """ Determine if argument is a file like object or make it so! """
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
35 if hasattr(f, 'read'):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
36 # Assume this is a file like object
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
37 return f
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
38 elif isinstance(f, str):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
39 return open(f, 'r')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
40 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
41 raise TaskError('cannot use {} as input'.format(f))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
42
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
43
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
44 def fix_object(o):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
45 if isinstance(o, ObjectFile):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
46 return o
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
47 elif isinstance(o, str):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
48 with open(o, 'r') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
49 return load_object(f)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
50 else:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
51 raise TaskError('Cannot use {} as objectfile'.format(o))
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
52
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
53
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
54 def fix_layout(l):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
55 if isinstance(l, Layout):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
56 return l
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
57 elif hasattr(l, 'read'):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
58 # Assume file handle
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
59 return load_layout(l)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
60 elif isinstance(l, str):
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
61 with open(l, 'r') as f:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
62 return load_layout(f)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
63 else:
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
64 raise TaskError('Cannot use {} as layout'.format(l))
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
65
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
66
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
67 def assemble(source, target):
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
68 """ Invoke the assembler on the given source, returns an object containing
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
69 the output. """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
70 logger = logging.getLogger('assemble')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
71 target = fix_target(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
72 source = fix_file(source)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
73 output = ObjectFile()
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
74 assembler = target.assembler
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
75 logger.debug('Assembling into code section')
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
76 ostream = BinaryAndLoggingStream(output)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
77 ostream.select_section('code')
386
2a970e7270e2 Added repeat assembler macro
Windel Bouwman
parents: 385
diff changeset
78 assembler.prepare()
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
79 assembler.assemble(source, ostream)
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
80 assembler.flush()
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
81 return output
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
82
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
83
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
84 def c3compile(sources, includes, target):
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
85 """ Compile a set of sources for the given target """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
86 logger = logging.getLogger('c3c')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
87 logger.debug('C3 compilation started')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
88 target = fix_target(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
89 sources = [fix_file(fn) for fn in sources]
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
90 includes = [fix_file(fn) for fn in includes]
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
91 output = ObjectFile()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
92 diag = DiagnosticsManager()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
93 c3b = Builder(diag, target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
94 cg = CodeGenerator(target)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
95
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
96 output_stream = BinaryAndLoggingStream(output)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
97
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
98 for ircode in c3b.build(sources, includes):
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
99 if not ircode:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
100 # Something went wrong, do not continue the code generation
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
101 continue
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 d = {'ircode':ircode}
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
104 logger.debug('Verifying code {}'.format(ircode), extra=d)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
105 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
106
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
107 # Optimization passes:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
108 CleanPass().run(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
109 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
110 RemoveAddZero().run(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
111 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
112 CleanPass().run(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
113 Verifier().verify(ircode)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
114
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
115 # Code generation:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
116 d = {'ircode':ircode}
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
117 logger.debug('Starting code generation for {}'.format(ircode), extra=d)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
118
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
119 cg.generate(ircode, output_stream)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
120
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
121 if not c3b.ok:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
122 diag.printErrors()
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
123 raise TaskError('Compile errors')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
124 return output
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
125
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
126
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
127 def link(objects, layout, target):
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
128 """ Links the iterable of objects into one using the given layout """
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
129 objects = list(map(fix_object, objects))
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
130 layout = fix_layout(layout)
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
131 target = fix_target(target)
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
132 linker = Linker(target)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
133 output_obj = linker.link(objects, layout)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents:
diff changeset
134 return output_obj
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
135