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
|
335
|
20
|
331
|
21 class BuildTask(Task):
|
334
|
22 """ Base task for all kind of weird build tasks """
|
331
|
23 def __init__(self, name):
|
|
24 super().__init__(name)
|
|
25 self.logger = logging.getLogger('buildtask')
|
|
26
|
|
27
|
|
28 class Assemble(BuildTask):
|
334
|
29 """ Task that can runs the assembler over the source and enters the
|
|
30 output into an object file """
|
|
31 def __init__(self, source, target, output_object):
|
329
|
32 super().__init__('Assemble')
|
334
|
33 self.source = source
|
|
34 self.assembler = Assembler(target=target)
|
|
35 self.output = output_object
|
329
|
36
|
|
37 def run(self):
|
334
|
38 self.assembler.assemble(self.source)
|
329
|
39
|
|
40
|
331
|
41 class Compile(BuildTask):
|
334
|
42 """ Task that compiles C3 source for some target into an object file """
|
329
|
43 def __init__(self, sources, includes, target, output_object):
|
|
44 super().__init__('Compile')
|
|
45 self.sources = sources
|
|
46 self.includes = includes
|
|
47 self.target = target
|
|
48 self.output = output_object
|
|
49
|
|
50 def run(self):
|
334
|
51 self.logger.debug('Compile started')
|
329
|
52 diag = DiagnosticsManager()
|
|
53 c3b = Builder(diag, self.target)
|
|
54 cg = CodeGenerator(self.target)
|
|
55
|
|
56 for ircode in c3b.build(self.sources, self.includes):
|
|
57 if not ircode:
|
|
58 return
|
|
59
|
|
60 d = {'ircode':ircode}
|
334
|
61 self.logger.debug('Verifying code {}'.format(ircode), extra=d)
|
329
|
62 Verifier().verify(ircode)
|
|
63
|
|
64 # Optimization passes:
|
|
65 CleanPass().run(ircode)
|
|
66 Verifier().verify(ircode)
|
|
67 RemoveAddZero().run(ircode)
|
|
68 Verifier().verify(ircode)
|
|
69 CleanPass().run(ircode)
|
|
70 Verifier().verify(ircode)
|
|
71
|
|
72 # Code generation:
|
|
73 d = {'ircode':ircode}
|
334
|
74 self.logger.debug('Starting code generation for {}'.format(ircode), extra=d)
|
335
|
75 o = outstream.BinaryOutputStream(self.output)
|
334
|
76 cg.generate(ircode, o)
|
329
|
77
|
|
78 if not c3b.ok:
|
|
79 diag.printErrors()
|
|
80 raise TaskError('Compile errors')
|
|
81
|
|
82
|
331
|
83 class Link(BuildTask):
|
334
|
84 """ Link together a collection of object files """
|
329
|
85 def __init__(self, objects, output_file):
|
|
86 super().__init__('Link')
|
334
|
87 self.objects = objects
|
|
88 self.linker = Linker()
|
|
89 self.duration = 0.1337
|
|
90
|
|
91 def run(self):
|
|
92 self.linker.link(self.objects)
|
329
|
93
|
|
94
|
|
95 class ObjCopy(Task):
|
|
96 pass
|
|
97
|