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
|
|
15
|
|
16
|
331
|
17 class BuildTask(Task):
|
|
18 def __init__(self, name):
|
|
19 super().__init__(name)
|
|
20 self.logger = logging.getLogger('buildtask')
|
|
21
|
|
22
|
|
23 class Assemble(BuildTask):
|
329
|
24 def __init__(self):
|
|
25 super().__init__('Assemble')
|
|
26
|
|
27 def run(self):
|
|
28 pass
|
|
29
|
|
30
|
331
|
31 class Compile(BuildTask):
|
329
|
32 """ Task that compiles source to some target """
|
|
33 def __init__(self, sources, includes, target, output_object):
|
|
34 super().__init__('Compile')
|
|
35 self.sources = sources
|
|
36 self.includes = includes
|
|
37 self.target = target
|
|
38 self.output = output_object
|
|
39
|
|
40 def run(self):
|
331
|
41 self.logger.info('Zcc started {}'.format(self.sources))
|
329
|
42 diag = DiagnosticsManager()
|
|
43 c3b = Builder(diag, self.target)
|
|
44 cg = CodeGenerator(self.target)
|
|
45
|
|
46 for ircode in c3b.build(self.sources, self.includes):
|
|
47 if not ircode:
|
|
48 return
|
|
49
|
|
50 d = {'ircode':ircode}
|
331
|
51 self.logger.info('Verifying code {}'.format(ircode), extra=d)
|
329
|
52 Verifier().verify(ircode)
|
|
53
|
|
54 # Optimization passes:
|
|
55 CleanPass().run(ircode)
|
|
56 Verifier().verify(ircode)
|
|
57 RemoveAddZero().run(ircode)
|
|
58 Verifier().verify(ircode)
|
|
59 CleanPass().run(ircode)
|
|
60 Verifier().verify(ircode)
|
|
61
|
|
62 # Code generation:
|
|
63 d = {'ircode':ircode}
|
331
|
64 self.logger.info('Starting code generation for {}'.format(ircode), extra=d)
|
329
|
65 cg.generate(ircode, self.output)
|
|
66
|
|
67 # TODO: fixup references, do this in another way?
|
|
68 self.output.backpatch()
|
|
69 if not c3b.ok:
|
|
70 diag.printErrors()
|
|
71 raise TaskError('Compile errors')
|
|
72
|
|
73
|
331
|
74 class Link(BuildTask):
|
329
|
75 def __init__(self, objects, output_file):
|
|
76 super().__init__('Link')
|
|
77
|
|
78
|
|
79 class ObjCopy(Task):
|
|
80 pass
|
|
81
|
|
82
|
331
|
83 def load_recipe(recipe_file, runner):
|
329
|
84 """ Loads a recipe dictionary into a task runner """
|
331
|
85 for command, value in recipe:
|
|
86 if command == 'compile':
|
|
87 sources = value['']
|
|
88 target = value['target']
|
|
89 runner.add_task(Compile())
|
|
90 else:
|
|
91 raise Exception()
|
329
|
92
|