changeset 259:ac603eb66b63

Added function call
author Windel Bouwman
date Mon, 05 Aug 2013 20:41:25 +0200
parents 04c19282a5aa
children b2f94b4951f1
files python/c3/codegenerator.py python/codegenarm.py python/ide.py python/ir/instruction.py python/outstream.py
diffstat 5 files changed, 65 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/python/c3/codegenerator.py	Mon Aug 05 19:46:11 2013 +0200
+++ b/python/c3/codegenerator.py	Mon Aug 05 20:41:25 2013 +0200
@@ -211,15 +211,15 @@
             else:
                 raise NotImplementedError("not implemented")
         elif type(expr) is astnodes.FunctionCall:
-             tmp = self.builder.newTmp("res")
-             args = []
-             for arg in expr.args:
+            tmp = self.builder.newTmp("res")
+            args = []
+            for arg in expr.args:
                 ar = self.genExprCode(arg)
                 args.append(ar)
-             fn = self.funcMap[expr.proc]
-             ins = ir.Call(fn, args, tmp)
-             self.builder.addIns(ins)
-             return tmp
+            fn = self.funcMap[expr.proc]
+            ins = ir.Call(fn, args, tmp)
+            self.builder.addIns(ins)
+            return tmp
         else:
-             raise NotImplementedError('Unknown expr {}'.format(expr))
+            raise NotImplementedError('Unknown expr {}'.format(expr))
 
--- a/python/codegenarm.py	Mon Aug 05 19:46:11 2013 +0200
+++ b/python/codegenarm.py	Mon Aug 05 20:41:25 2013 +0200
@@ -39,6 +39,7 @@
         # TODO: use label here:
         #self.emit(arm.dcd_ins(LabelRef('reset'))) # reset vector
         self.dcd(0x08000009) # reset vector, lsb indicates thumb mode
+        self.emit(arm.bl_ins(LabelRef('main')))
 
         self.emit(Label('reset'))
         for f in ircode.Functions:
@@ -132,7 +133,7 @@
             else:
                 r1 = self.getreg(ins.location)
                 self.emit(arm.storeimm5_ins(r0, arm.MemR8Rel(r1, 0)))
-            self.freereg(ins.location, ins)
+                self.freereg(ins.location, ins)
             self.freereg(ins.value, ins)
         elif type(ins) is ir.Load:
             # TODO: differ global and local??
@@ -143,7 +144,7 @@
             else:
                 r2 = self.getreg(ins.location)
                 self.emit(arm.loadimm5_ins(r0, arm.MemR8Rel(r2, 0)))
-            self.freereg(ins.location, ins)
+                self.freereg(ins.location, ins)
         elif type(ins) is ir.BinaryOperator:
             # Load operands:
             r0 = self.getreg(ins.value1)
@@ -162,6 +163,9 @@
                 raise NotImplementedError('operation {} not implemented'.format(ins.operation))
             self.freereg(ins.value1, ins)
             self.freereg(ins.value2, ins)
+        elif type(ins) is ir.Call:
+            # TODO: prep parameters:
+            self.emit(arm.bl_ins(LabelRef(ins.callee.name)))
         elif type(ins) is ir.Return:
             self.emit(arm.pop_ins(arm.RegisterSet({arm.r4, arm.r5, arm.r6, arm.r7, arm.pc})))
         elif type(ins) is ir.ConditionalBranch:
--- a/python/ide.py	Mon Aug 05 19:46:11 2013 +0200
+++ b/python/ide.py	Mon Aug 05 20:41:25 2013 +0200
@@ -16,11 +16,13 @@
 import zcc
 import outstream
 
+
 class BuildOutput(QTextEdit):
     """ Build output component """
     def __init__(self, parent=None):
         super(BuildOutput, self).__init__(parent)
         fmt = logging.Formatter(fmt=zcc.logformat)
+
         class MyHandler(logging.Handler):
             def emit(self2, x):
                 self.append(str(fmt.format(x)))
@@ -33,6 +35,7 @@
 
 class BuildErrors(QTreeView):
     sigErrorSelected = pyqtSignal(object)
+
     def __init__(self, parent=None):
         super(BuildErrors, self).__init__(parent)
         model = QStandardItemModel()
@@ -44,16 +47,16 @@
         self.setModel(self.model)
 
     def setErrorList(self, errorlist):
-      c = self.model.rowCount()
-      self.model.removeRows(0, c)
-      for e in errorlist:
-         item = QStandardItem(self.errorIcon, str(e.msg))
-         item.setData(e)
-         irow = QStandardItem(str(e.loc.row))
-         irow.setData(e)
-         icol = QStandardItem(str(e.loc.col))
-         icol.setData(e)
-         self.model.appendRow([item, irow, icol])
+        c = self.model.rowCount()
+        self.model.removeRows(0, c)
+        for e in errorlist:
+            item = QStandardItem(self.errorIcon, str(e.msg))
+            item.setData(e)
+            irow = QStandardItem(str(e.loc.row))
+            irow.setData(e)
+            icol = QStandardItem(str(e.loc.col))
+            icol.setData(e)
+            self.model.appendRow([item, irow, icol])
 
     def itemSelected(self, index):
         if not index.isValid():
@@ -87,6 +90,7 @@
     def __init__(self, parent=None):
         super(Ide, self).__init__(parent)
         self.to_open_files = []
+        self.logger = logging.getLogger('ide')
 
         self.setWindowTitle('LCFOS IDE')
         icon = QIcon('icons/logo.png')
@@ -107,12 +111,12 @@
 
         # Create components:
         def addComponent(name, widget):
-           dw = QDockWidget(name)
-           dw.setWidget(widget)
-           dw.setObjectName(name)
-           self.addDockWidget(Qt.RightDockWidgetArea, dw)
-           self.viewMenu.addAction(dw.toggleViewAction())
-           return widget
+            dw = QDockWidget(name)
+            dw.setWidget(widget)
+            dw.setObjectName(name)
+            self.addDockWidget(Qt.RightDockWidgetArea, dw)
+            self.viewMenu.addAction(dw.toggleViewAction())
+            return widget
 
         self.buildOutput = addComponent('Build output', BuildOutput())
         self.astViewer = addComponent('AST viewer', AstViewer())
@@ -135,13 +139,14 @@
         # About dialog:
         self.aboutDialog = AboutDialog()
         self.aboutDialog.setWindowIcon(icon)
+
         # Create actions:
         def addMenuEntry(name, menu, callback, shortcut=None):
-          a = QAction(name, self)
-          menu.addAction(a)
-          a.triggered.connect(callback)
-          if shortcut:
-             a.setShortcut(shortcut)
+            a = QAction(name, self)
+            menu.addAction(a)
+            a.triggered.connect(callback)
+            if shortcut:
+                a.setShortcut(shortcut)
 
         addMenuEntry("New", self.fileMenu, self.newFile, shortcut=QKeySequence(QKeySequence.New))
         addMenuEntry("Open", self.fileMenu, self.openFile, shortcut=QKeySequence(QKeySequence.Open))
@@ -149,7 +154,7 @@
         addMenuEntry("Build", self.fileMenu, self.buildFile, shortcut=QKeySequence("F7"))
         addMenuEntry("Build and flash", self.fileMenu, self.buildFileAndFlash, shortcut=QKeySequence("F8"))
 
-        self.helpAction  = QAction('Help', self)
+        self.helpAction = QAction('Help', self)
         self.helpAction.setShortcut(QKeySequence('F1'))
         self.helpMenu.addAction(self.helpAction)
         addMenuEntry('About', self.helpMenu, self.aboutDialog.open)
@@ -170,7 +175,7 @@
 
     def openFile(self):
         filename = QFileDialog.getOpenFileName(self, "Open C3 file...", "*.c3",
-            "C3 source files (*.c3)")
+                    "C3 source files (*.c3)")
         if filename:
             self.loadFile(filename)
 
@@ -274,7 +279,7 @@
         self.builderrors.setErrorList(self.diag.diags)
         ce.setErrors(self.diag.diags)
         self.astViewer.setAst(pkg)
-        logging.info('Done!')
+        self.logger.info('Done!')
 
     def buildFile(self):
         ce = self.activeMdiChild()
@@ -310,7 +315,9 @@
             stl.reset()
             stl.halt()
             stl.run()
-        logging.info('Done!')
+            logging.info('Done!')
+        else:
+            self.logger.warning('Not connected to device, skipping flash')
 
 
 if __name__ == '__main__':
@@ -318,6 +325,6 @@
     app = QApplication(sys.argv)
     ide = Ide()
     ide.show()
-    logging.info('IDE started')
+    ide.logger.info('IDE started')
     app.exec_()
 
--- a/python/ir/instruction.py	Mon Aug 05 19:46:11 2013 +0200
+++ b/python/ir/instruction.py	Mon Aug 05 20:41:25 2013 +0200
@@ -26,11 +26,7 @@
     Used = IsUsed
 
     def onlyUsedInBlock(self, bb):
-        for use in self.used_by:
-            ins = use
-            if ins.parent != bb:
-                return False
-        return True
+        return all(use.Block is bb for use in self.used_by)
 
     def lastUse(self, ins):
         assert ins in self.used_by
@@ -60,6 +56,7 @@
         # What variables this instruction uses and defines:
         self.defs = []
         self.uses = []
+
     def delete(self):
         while self.uses:
             use = self.uses.pop()
@@ -106,7 +103,11 @@
         assert isinstance(other, Instruction)
         if self.Block is other.Block:
             return other.Position > self.Position
-        return self.Block.precedes(other.Block)
+        else:
+            return self.Block.precedes(other.Block)
+
+    def follows(self, other):
+        pass
 
     @property
     def Function(self):
@@ -245,14 +246,14 @@
 
 class Store(Instruction):
     def __init__(self, location, value):
-      super().__init__()
-      assert type(value) is Value, value
-      assert isinstance(location, Value), "Location must be a value"
-      self.location = location
-      self.value = value
-      self.addUse(value)
-      self.addUse(location)
-    
+        super().__init__()
+        assert type(value) is Value, value
+        assert isinstance(location, Value), "Location must be a value"
+        self.location = location
+        self.value = value
+        self.addUse(value)
+        self.addUse(location)
+
     def __repr__(self):
         return '[{}] = {}'.format(self.location, self.value)
 
--- a/python/outstream.py	Mon Aug 05 19:46:11 2013 +0200
+++ b/python/outstream.py	Mon Aug 05 20:41:25 2013 +0200
@@ -78,13 +78,15 @@
     def dumpSection(self, s):
         print('.section '+ s)
         for i in self.sections[s].instructions:
+            if type(i) is DebugInfo:
+                continue
             addr = i.address
             insword = i.encode()
             assert type(insword) is bytes
             insword = binascii.hexlify(bytes(reversed(insword))).decode('ascii')
             asm = str(i)
             if len(insword) == 0:
-                print('    {}'.format(asm))
+                print('        {}'.format(asm))
             else:
                 print('    0x{0:08x} 0x{1} {2}'.format(addr, insword, asm))