annotate python/ppci/compilers/kscompiler.py @ 101:af0d7913677a

Fixes and splitting into 3 stage
author windel
date Mon, 24 Dec 2012 17:55:08 +0100
parents fe145e42259d
children 63937c8d1478
rev   line source
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
1 import hashlib
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
2 # Import compiler components:
100
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
3 from ..frontends.ks import KsParser
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
4 #from .codegenerator import CodeGenerator
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
5 #from .nodes import ExportedSymbol
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
6 from ..core.errors import CompilerException
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
7 from ..core import version
100
fe145e42259d Fixes after movage
windel
parents: 99
diff changeset
8 #from .. import version
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
9
99
windel
parents: 96
diff changeset
10 class KsCompiler:
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
11 def __repr__(self):
99
windel
parents: 96
diff changeset
12 return 'LCFOS compiler {0}'.format(version)
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
13
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
14 def generateSignature(self, src):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
15 return hashlib.md5(bytes(src,encoding='ascii')).hexdigest()
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
16
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
17 def compilesource(self, src):
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
18 """ Front end that handles the stages: """
6
1784af239df4 Added error list
windel
parents: 5
diff changeset
19 self.errorlist = []
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
20 # Pass 1: parsing and type checking
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
21 p = KsParser(src)
7
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
22 try:
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
23 ast = p.parseModule() # Parse a module into an AST
7
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
24 except CompilerException as e:
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
25 print(e)
7
2db4d2b362e6 Added xml project
windel
parents: 6
diff changeset
26 p.errorlist.append( (e.row, e.col, e.msg) )
5
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
27 if len(p.errorlist) > 0:
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
28 self.errorlist = p.errorlist
818f80afa78b Added handy highlighting to IDE
windel-eee
parents: 1
diff changeset
29 return
101
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
30 # pass 2: Optimization
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
31 # TODO: implement optimization
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
32
af0d7913677a Fixes and splitting into 3 stage
windel
parents: 100
diff changeset
33 # Pass 3: code generation
1
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
34 CodeGenerator().generatecode(ast)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
35 # Attach a signature:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
36 ast.signature = self.generateSignature(src)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
37 # Generate exported symbols:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
38 ast.exports = []
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
39 for proc in ast.procs:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
40 if proc.public:
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
41 sym = ExportedSymbol(proc.name, proc.typ)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
42 sym.imageoffset = proc.entrypoint
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
43 ast.exports.append(sym)
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
44 return ast
92df07bc2081 Initial import of compiler
windel
parents:
diff changeset
45
15
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
46 def compileProject(self, project):
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
47 mods = []
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
48 for fname in project.files:
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
49 print('Compiling {0}...'.format(fname))
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
50 source = project.loadProjectFile(fname)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
51 mod = self.compilesource(source)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
52 mods.append(mod)
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
53 return mods
de004f808e56 Fixup in multidocument area stuff
windel
parents: 7
diff changeset
54