annotate python/ppci/c3/builder.py @ 307:e609d5296ee9

Massive rewrite of codegenerator
author Windel Bouwman
date Thu, 12 Dec 2013 20:42:56 +0100
parents b145f8e6050b
children 2e7f55319858
rev   line source
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
1 import logging
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
2 from .lexer import Lexer
300
Windel Bouwman
parents: 295
diff changeset
3 from .parser import Parser
Windel Bouwman
parents: 295
diff changeset
4 from .codegenerator import CodeGenerator
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
5 from .analyse import ScopeFiller
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
6 from .scope import createTopScope
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
7 from .visitor import AstPrinter
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
8
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
9
165
598d3888a11c Added front class and fided AST view
Windel Bouwman
parents:
diff changeset
10 class Builder:
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
11 """
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
12 Generates IR-code from c3 source.
300
Windel Bouwman
parents: 295
diff changeset
13 Reports errors to the diagnostics system.
186
46d62dadd61b Improved testsuite
Windel Bouwman
parents: 168
diff changeset
14 """
301
6753763d3bec merge codegen into ppci package
Windel Bouwman
parents: 300
diff changeset
15 def __init__(self, diag, target):
254
bd26dc13f270 Added logger
Windel Bouwman
parents: 251
diff changeset
16 self.logger = logging.getLogger('c3')
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
17 self.diag = diag
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
18 self.lexer = Lexer(diag)
226
240111e0456f Work on named types
Windel Bouwman
parents: 225
diff changeset
19 self.parser = Parser(diag)
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
20 self.cg = CodeGenerator(diag)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
21 self.topScope = createTopScope(target) # Scope with built in types
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 186
diff changeset
22
287
1c7c1e619be8 File movage
Windel Bouwman
parents: 272
diff changeset
23 def build(self, srcs, imps=[]):
251
6ed3d3a82a63 Added another c3 example. First import attempt
Windel Bouwman
parents: 226
diff changeset
24 """ Create IR-code from sources """
305
0615b5308710 Updated docs
Windel Bouwman
parents: 301
diff changeset
25 self.logger.info('Building {} source files'.format(len(srcs)))
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
26 iter(srcs) # Check if srcs are iterable
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
27 iter(imps)
288
a747a45dcd78 Various styling work
Windel Bouwman
parents: 287
diff changeset
28 self.ok = True
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
29 self.pkgs = {}
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
30
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
31 # Parsing stage (phase 1)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
32 def doParse(src):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
33 tokens = self.lexer.lex(src)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
34 return self.parser.parseSource(tokens)
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
35 s_pkgs = set(map(doParse, srcs))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
36 i_pkgs = set(map(doParse, imps))
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
37 all_pkgs = s_pkgs | i_pkgs
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
38 if not all(all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
39 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
40 return
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
41
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
42 # Fix scopes and package refs (phase 1.5)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
43 packages = {pkg.name: pkg for pkg in all_pkgs}
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
44 self.pkgs = packages
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
45
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
46 scopeFiller = ScopeFiller(self.diag, self.topScope, packages)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
47 # Fix scopes:
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
48 for pkg in all_pkgs:
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
49 scopeFiller.addScope(pkg)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
50 if not all(pkg.ok for pkg in all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
51 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
52 return
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
53
307
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
54 # Generate intermediate code (phase 2)
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
55 # Only return ircode when everything is OK
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
56 for pkg in all_pkgs & s_pkgs:
e609d5296ee9 Massive rewrite of codegenerator
Windel Bouwman
parents: 306
diff changeset
57 yield self.cg.gencode(pkg)
306
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
58 if not all(pkg.ok for pkg in all_pkgs):
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
59 self.ok = False
b145f8e6050b Start on c3 rewrite
Windel Bouwman
parents: 305
diff changeset
60 return