Mercurial > lcfOS
changeset 106:f2d980eef509
improved code generation
author | Windel Bouwman |
---|---|
date | Mon, 31 Dec 2012 18:26:56 +0100 |
parents | 6a303f835c6d |
children | 1544e7a4aa98 |
files | python/ide.py python/ppci/__init__.py python/ppci/core/__init__.py python/ppci/core/function.py python/ppci/core/llvmtype.py python/ppci/core/module.py python/ppci/frontends/ks/__init__.py python/ppci/frontends/ks/irgenerator.py python/ppci/frontends/ks/parser.py python/tests/testcode.c python/zcc.py |
diffstat | 11 files changed, 42 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/python/ide.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ide.py Mon Dec 31 18:26:56 2012 +0100 @@ -10,7 +10,7 @@ # Compiler imports: from project import Project -from ppci import KsCompiler +import ppci from astviewer import AstViewer from codeeditor import CodeEdit
--- a/python/ppci/__init__.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/__init__.py Mon Dec 31 18:26:56 2012 +0100 @@ -1,4 +1,11 @@ # File to make this directory a package. +import sys + version = '0.0.1' +# Assert python version: +if sys.version_info.major != 3: + print("Needs to be run in python version 3.x") + sys.exit(1) +
--- a/python/ppci/core/__init__.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/core/__init__.py Mon Dec 31 18:26:56 2012 +0100 @@ -6,6 +6,7 @@ from .module import Module from .llvmtype import FunctionType from .context import Context +from .asmwriter import AsmWriter version='0.0.1'
--- a/python/ppci/core/function.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/core/function.py Mon Dec 31 18:26:56 2012 +0100 @@ -10,11 +10,12 @@ self.functiontype = functiontype self.name = name self.module = module + + self.module.Functions.append(self) self.basicblocks = [] self.arguments = [] # Construct formal arguments depending on function type - BasicBlocks = property(lambda self: self.basicblocks) Arguments = property(lambda self: self.arguments) ReturnType = property(lambda self: self.functiontype.returnType)
--- a/python/ppci/core/llvmtype.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/core/llvmtype.py Mon Dec 31 18:26:56 2012 +0100 @@ -17,5 +17,6 @@ def __init__(self, resultType, parameterTypes): super().__init__(resultType.context, typeID.Function) self.resultType = resultType + self.returnType = resultType self.parameterTypes = parameterTypes
--- a/python/ppci/core/module.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/core/module.py Mon Dec 31 18:26:56 2012 +0100 @@ -7,10 +7,12 @@ Main container for a piece of code. Contains globals and functions. """ def __init__(self): - self.functions = [] - self.globals_ = [] + self.identifier = None + self.functions = [] # Do functions come out of symbol table? + self.globals_ = [] # TODO: are globals in symbol table? self.symtable = SymbolTable() Globals = property(lambda self: self.globals_) Functions = property(lambda self: self.functions) + Identifier = property(lambda self: self.identifier)
--- a/python/ppci/frontends/ks/__init__.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/frontends/ks/__init__.py Mon Dec 31 18:26:56 2012 +0100 @@ -21,7 +21,7 @@ self.ast = ast # Generate ir (a core.Module): - ir = KsIrGenerator().generateIr(context, ast) + ir = KsIrGenerator().generateIr(self.context, ast) return ir
--- a/python/ppci/frontends/ks/irgenerator.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/frontends/ks/irgenerator.py Mon Dec 31 18:26:56 2012 +0100 @@ -42,13 +42,9 @@ elif type(node) is Unop: if node.op == 'INTTOREAL': self.genexprcode(node.a) - node.reg = node.a.reg - # TODO use 'FILD' instruction - freg = 12 code.append('Unop inttoreal TODO') elif node.op == 'ABS': if isType(node.typ, real): - code = [0xD9, 0xE1] # st(0) = fabs st(0) Error('ABS error integer') elif isType(node.typ, integer): code = []
--- a/python/ppci/frontends/ks/parser.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/ppci/frontends/ks/parser.py Mon Dec 31 18:26:56 2012 +0100 @@ -1,7 +1,3 @@ -""" - This module parses source code into an abstract syntax tree (AST) -""" - from .symboltable import SymbolTable from .nodes import * from ...core.errors import CompilerException, Error @@ -10,6 +6,7 @@ from .lexer import tokenize class KsParser: + """ This module parses source code into an abstract syntax tree (AST) """ def __init__(self, source): """ provide the parser with the tokens iterator from the lexer. """ self.tokens = tokenize(source) # Lexical stage
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/python/tests/testcode.c Mon Dec 31 18:26:56 2012 +0100 @@ -0,0 +1,9 @@ + +int Calculate(int a, int b, int c) +{ + int d, e; + d = a + b; + e = d * c - a; + return a + b + c + d + e; +} +
--- a/python/zcc.py Mon Dec 31 17:35:17 2012 +0100 +++ b/python/zcc.py Mon Dec 31 18:26:56 2012 +0100 @@ -1,7 +1,7 @@ #!/usr/bin/python import sys, os, argparse -from ppci import core +from ppci import core, frontends parser = argparse.ArgumentParser(description='K# to bitcode compiler') parser.add_argument('source', type=str, help='the source file to build') @@ -13,19 +13,30 @@ except IOError: print('Failed to load {0}'.format(args.source)) sys.exit(1) - +print('stage 1: Parsing') # Create a context and a frontend: context = core.Context() -frontend = core.frontends.ks.KsFrontend(context) +frontend = frontends.KsFrontend(context) try: module = frontend.compilesource(src) except core.CompilerException as e: print(e) - sys.exit(2) + lines = src.split(os.linesep) + row = e.row + col = e.col + line = lines[row - 1] + print('{0}:{1}'.format(row, line)) + print(' ' * col + '^') + raise + #sys.exit(2) # optionally run passes here: # TODO +print('stage 3: Code generation') +asmWriter = core.AsmWriter() +asmWriter.printModule(module) + # Generate code: bitcodeWriter = core.BitcodeWriter() with open(args.source + '.bc', 'wb') as f: