annotate python/c3/builder.py @ 295:917eab04b8b7

Added disasm
author Windel Bouwman
date Thu, 28 Nov 2013 21:10:32 +0100
parents 6aa721e7b10b
children
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
2 import ppci
215
c1ccb1cb4cef Major changes in c3 frontend
Windel Bouwman
parents: 213
diff changeset
3 from . import Parser, TypeChecker, Analyzer, CodeGenerator
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
4 from .analyse import AddScope
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
5
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
6
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
7 class Builder:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
8 """
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
9 Generates IR-code from c3 source.
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
10 Reports errors to the diagnostics system
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
11 """
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
12 def __init__(self, diag):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
13 self.logger = logging.getLogger('c3')
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
14 self.diag = diag
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
15 self.parser = Parser(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
16 self.tc = TypeChecker(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
17 self.al = Analyzer(diag)
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
18 self.cg = CodeGenerator()
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
19
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
20 def checkSource(self, srcs, imps=[]):
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
21 """ Performs syntax and type check. """
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
22 iter(srcs)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
23 iter(imps)
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
24 def doParse(srcs):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
25 for src in srcs:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
26 pkg = self.parser.parseSource(src)
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
27 if pkg:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
28 yield pkg
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
29 else:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
30 self.ok = False
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
31 s_pkgs = set(doParse(srcs))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
32 i_pkgs = set(doParse(imps))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
33 all_pkgs = s_pkgs | i_pkgs
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
34 # Fix scopes:
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
35 def doF(f, pkgs):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
36 for pkg in pkgs:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
37 if f(pkg):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
38 yield pkg
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
39 else:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
40 self.ok = False
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
41 all_pkgs = set(doF(AddScope(self.diag).addScope, all_pkgs))
289
bd2593de3ff8 Semifix burn2
Windel Bouwman
parents: 288
diff changeset
42 # TODO: fix error handling better
293
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
43 def doA(pkgs):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
44 packages = {pkg.name: pkg for pkg in pkgs}
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
45 for pkg in pkgs:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
46 if self.al.analyzePackage(pkg, packages):
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
47 yield pkg
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
48 else:
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
49 self.ok = False
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
50 all_pkgs = set(doA(all_pkgs))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
51 all_pkgs = set(doF(self.tc.checkPackage, all_pkgs))
6aa721e7b10b Try to improve build sequence
Windel Bouwman
parents: 289
diff changeset
52 return all_pkgs & s_pkgs
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 186
diff changeset
53
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
54 def build(self, srcs, imps=[]):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
55 """ Create IR-code from sources """
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
56 self.ok = True
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
57 for pkg in self.checkSource(srcs, imps):
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
58 # Only return ircode when everything is OK
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
59 if self.ok:
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
60 yield self.cg.gencode(pkg)