329
|
1
|
|
2 """
|
|
3 Defines task classes that can compile, link etc..
|
|
4 Task can depend upon one another.
|
|
5 """
|
|
6
|
|
7 import logging
|
|
8
|
|
9 from .c3 import Builder
|
|
10 from .irutils import Verifier
|
|
11 from .codegen import CodeGenerator
|
|
12 from .transform import CleanPass, RemoveAddZero
|
|
13 from .tasks import Task
|
|
14 from . import DiagnosticsManager
|
334
|
15 from .assembler import Assembler
|
|
16 from .objectfile import ObjectFile
|
|
17 from .linker import Linker
|
|
18 import outstream
|
329
|
19
|
331
|
20 class BuildTask(Task):
|
334
|
21 """ Base task for all kind of weird build tasks """
|
331
|
22 def __init__(self, name):
|
|
23 super().__init__(name)
|
|
24 self.logger = logging.getLogger('buildtask')
|
|
25
|
|
26
|
|
27 class Assemble(BuildTask):
|
334
|
28 """ Task that can runs the assembler over the source and enters the
|
|
29 output into an object file """
|
|
30 def __init__(self, source, target, output_object):
|
329
|
31 super().__init__('Assemble')
|
334
|
32 self.source = source
|
|
33 self.assembler = Assembler(target=target)
|
|
34 self.output = output_object
|
329
|
35
|
|
36 def run(self):
|
334
|
37 self.assembler.assemble(self.source)
|
329
|
38
|
|
39
|
331
|
40 class Compile(BuildTask):
|
334
|
41 """ Task that compiles C3 source for some target into an object file """
|
329
|
42 def __init__(self, sources, includes, target, output_object):
|
|
43 super().__init__('Compile')
|
|
44 self.sources = sources
|
|
45 self.includes = includes
|
|
46 self.target = target
|
|
47 self.output = output_object
|
|
48
|
|
49 def run(self):
|
334
|
50 self.logger.debug('Compile started')
|
329
|
51 diag = DiagnosticsManager()
|
|
52 c3b = Builder(diag, self.target)
|
|
53 cg = CodeGenerator(self.target)
|
|
54
|
|
55 for ircode in c3b.build(self.sources, self.includes):
|
|
56 if not ircode:
|
|
57 return
|
|
58
|
|
59 d = {'ircode':ircode}
|
334
|
60 self.logger.debug('Verifying code {}'.format(ircode), extra=d)
|
329
|
61 Verifier().verify(ircode)
|
|
62
|
|
63 # Optimization passes:
|
|
64 CleanPass().run(ircode)
|
|
65 Verifier().verify(ircode)
|
|
66 RemoveAddZero().run(ircode)
|
|
67 Verifier().verify(ircode)
|
|
68 CleanPass().run(ircode)
|
|
69 Verifier().verify(ircode)
|
|
70
|
|
71 # Code generation:
|
|
72 d = {'ircode':ircode}
|
334
|
73 self.logger.debug('Starting code generation for {}'.format(ircode), extra=d)
|
|
74 o = outstream.TextOutputStream()
|
|
75 cg.generate(ircode, o)
|
329
|
76
|
|
77 if not c3b.ok:
|
|
78 diag.printErrors()
|
|
79 raise TaskError('Compile errors')
|
|
80
|
|
81
|
331
|
82 class Link(BuildTask):
|
334
|
83 """ Link together a collection of object files """
|
329
|
84 def __init__(self, objects, output_file):
|
|
85 super().__init__('Link')
|
334
|
86 self.objects = objects
|
|
87 self.linker = Linker()
|
|
88 self.duration = 0.1337
|
|
89
|
|
90 def run(self):
|
|
91 print('LNK')
|
|
92 print('LNK', self.objects)
|
|
93 print('LNK')
|
|
94 print('LNK')
|
|
95 self.linker.link(self.objects)
|
329
|
96
|
|
97
|
|
98 class ObjCopy(Task):
|
|
99 pass
|
|
100
|