changeset 231:521567d17388

simplify blink.c3
author Windel Bouwman
date Sat, 13 Jul 2013 20:20:44 +0200
parents 88a1e0baef65
children e621e3ba78d2
files python/c3/analyse.py python/c3/codegenerator.py python/c3/scope.py python/c3/typecheck.py python/stm32f4/blink.c3 python/testc3.py python/trunner.sh
diffstat 7 files changed, 35 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/python/c3/analyse.py	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/c3/analyse.py	Sat Jul 13 20:20:44 2013 +0200
@@ -1,6 +1,7 @@
 from .visitor import Visitor
 from .astnodes import *
 from .scope import Scope, topScope
+from .typecheck import theType
 
 class Analyzer:
     """ 
@@ -79,8 +80,12 @@
             t.ptype = self.resolveType(t.ptype, scope)
             return t
         elif type(t) is StructureType:
+            offset = 0
             for mem in t.mems:
+                mem.offset = offset
                 mem.typ = self.resolveType(mem.typ, scope)
+                offset += theType(mem.typ).bytesize
+            t.bytesize = offset
             return t
         elif type(t) is Designator:
             t = self.resolveDesignator(t, scope)
@@ -90,7 +95,7 @@
             return t
         else:
             raise Exception('Error resolving type {} {}'.format(t, type(t)))
-        
+
     def findRefs(self, sym):
         if type(sym) in [Variable, Constant]:
             sym.typ = self.resolveType(sym.typ, sym.scope)
--- a/python/c3/codegenerator.py	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/c3/codegenerator.py	Sat Jul 13 20:20:44 2013 +0200
@@ -2,7 +2,7 @@
 from . import astnodes
 from .scope import boolType, intType
 from ppci import CompilerError
-from .typecheck import resolveType
+from .typecheck import theType
 
 tmpnames = {'+':'add', '-':'sub', '*': 'mul', '/':'div', '|':'or', '&':'and'}
 
@@ -179,7 +179,7 @@
         elif type(expr) is astnodes.FieldRef:
             b = self.genExprCode(expr.base)
             offset = self.builder.newTmp('off_' + expr.field)
-            bt = resolveType(expr.base.typ)
+            bt = theType(expr.base.typ)
             ofs = bt.fieldOffset(expr.field)
             ins = ir.ImmLoad(offset, ofs)
             self.builder.addIns(ins)
@@ -194,7 +194,7 @@
             return tmp
         elif type(expr) is astnodes.TypeCast:
             ar = self.genExprCode(expr.a)
-            tt = resolveType(expr.to_type)
+            tt = theType(expr.to_type)
             if isinstance(tt, astnodes.PointerType):
                 if expr.a.typ is intType:
                     return ar
--- a/python/c3/scope.py	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/c3/scope.py	Sat Jul 13 20:20:44 2013 +0200
@@ -50,6 +50,7 @@
 
 # buildin types:
 intType = astnodes.BaseType('int')
+intType.bytesize = 4
 doubleType = astnodes.BaseType('double')
 voidType = astnodes.BaseType('void')
 boolType = astnodes.BaseType('bool')
--- a/python/c3/typecheck.py	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/c3/typecheck.py	Sat Jul 13 20:20:44 2013 +0200
@@ -2,9 +2,12 @@
 from .scope import *
 from .visitor import Visitor
 
-def resolveType(t):
+def theType(t):
+    """
+        Recurse until a 'real' type is found
+    """
     if type(t) is DefinedType:
-        return resolveType(t.typ)
+        return theType(t.typ)
     return t
 
 def equalTypes(a, b):
@@ -13,8 +16,8 @@
         Not equal until proven otherwise.
     """
     # Recurse into named types:
-    a = resolveType(a)
-    b = resolveType(b)
+    a = theType(a)
+    b = theType(b)
 
     # Compare for structural equivalence:
     if type(a) is type(b):
@@ -34,8 +37,8 @@
     return False
 
 def canCast(fromT, toT):
-    fromT = resolveType(fromT)
-    toT = resolveType(toT)
+    fromT = theType(fromT)
+    toT = theType(toT)
     if isinstance(fromT, PointerType) and isinstance(toT, PointerType):
         return True
     elif fromT is intType and isinstance(toT, PointerType):
@@ -121,7 +124,7 @@
             # pointer deref
             sym.lvalue = True
             # check if the to be dereferenced variable is a pointer type:
-            ptype = resolveType(sym.ptr.typ)
+            ptype = theType(sym.ptr.typ)
             if type(ptype) is PointerType:
                 sym.typ = ptype.ptype
             else:
@@ -130,7 +133,7 @@
         elif type(sym) is FieldRef:
             basetype = sym.base.typ
             sym.lvalue = sym.base.lvalue
-            basetype = resolveType(basetype)
+            basetype = theType(basetype)
             if type(basetype) is StructureType:
                 if basetype.hasField(sym.field):
                     sym.typ = basetype.fieldType(sym.field)
@@ -176,9 +179,9 @@
             else:
                 raise Exception('Unknown binop {0}'.format(sym.op))
         elif type(sym) is Variable:
-         # check initial value type:
-         # TODO
-         pass
+            # check initial value type:
+            # TODO
+            pass
         elif type(sym) is TypeCast:
             if canCast(sym.a.typ, sym.to_type):
                 sym.typ = sym.to_type
--- a/python/stm32f4/blink.c3	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/stm32f4/blink.c3	Sat Jul 13 20:20:44 2013 +0200
@@ -1,11 +1,6 @@
 // This file blinks a LED on the STM32F4 discovery board.
 package blink;
 
-// import bla
-type struct {
-//  uint32_t SR;
-} TIM_Type;
-
 type struct {
     int MODER;
     int OTYPER;
@@ -13,42 +8,20 @@
     int PUPDR;
     int IDR;
     int ODR;
-} GPIO_Type;
-
-const GPIO_Type* GPIOD = cast<GPIO_Type*>(0x400000);
+}* GPIO_Type;
 
-function void delay(int count)
-{
-    while (count > 0)
-    {
-        count = count - 1;
-    }
-}
+type struct {
+} RCC_Type;
 
-// Globals:
-var int divider;
-//const TIM2_s *TIM2;// = (TIM2_s*)0x40004;
 
 // Functions:
-function void tim2_handler()
-{
-//	if (TIM2->SR & TIM_SR_UIF)
-    if (true)
-	{
-		divider = divider + 1;
-		if (divider == 100000)
-		{
-			divider = 0;
-			//GPIOD->ODR ^= (1 << 13);
-		}
-	}
-}
-
 function void main()
 {
-    divider = 0;
-
-    // delay(100);
+    var int APB1PERIPH_BASE;
+    APB1PERIPH_BASE = 0x40000000
+    //var int 
+    var GPIO_Type GPIOD;
+    GPIOD = cast<GPIO_Type>(0x400000);
 
     var int* RCC_AHB1ENR;
     RCC_AHB1ENR = cast<int*>(0x40003022);
--- a/python/testc3.py	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/testc3.py	Sat Jul 13 20:20:44 2013 +0200
@@ -304,7 +304,7 @@
          t6 = [adr_x5]
          t7 = 2
          add8 = t6 + t7
-         off_y9 = 0
+         off_y9 = 4
          adr_y10 = a0 + off_y9
          [adr_y10] = add8
          ret  """
@@ -397,7 +397,7 @@
          t10 = 14
          sub11 = t9 - t10
          deref12 = [a0]
-         off_y13 = 0
+         off_y13 = 4
          adr_y14 = deref12 + off_y13
          [adr_y14] = sub11
          ret  """
--- a/python/trunner.sh	Sat Jul 13 19:53:44 2013 +0200
+++ b/python/trunner.sh	Sat Jul 13 20:20:44 2013 +0200
@@ -4,7 +4,7 @@
 while :; do
   echo "Awaiting changes in $DIR"
   inotifywait -r -e modify $DIR
-  python testc3.py
+  python -m unittest
 done