diff python/tcodegen.py @ 274:ea93e0a7a31e

Move docs
author Windel Bouwman
date Wed, 04 Sep 2013 17:35:06 +0200
parents e64bae57cda8
children 6f2423df0675
line wrap: on
line diff
--- a/python/tcodegen.py	Mon Sep 02 17:40:21 2013 +0200
+++ b/python/tcodegen.py	Wed Sep 04 17:35:06 2013 +0200
@@ -7,6 +7,7 @@
 import ppci
 import codegenarm
 import outstream
+import ir
 
 testsrc = """
 package test2;
@@ -16,7 +17,18 @@
 function int insanemath(int a, int b)
 {
   var int c;
-  c = a + b + 1;
+  c = 0;
+  var int i;
+  i = 9;
+  while (i > 1)
+  {
+      c = a + b + 1 + c;
+      i = i - 1;
+      if (c > 90)
+      {
+         return 42;
+      }
+  }
   return c;
 }
 
@@ -31,7 +43,7 @@
    if (cee + a > b and b - a+b== 3*6-b)
    {
       var int x = a;
-      x = b - a + insanemath(3, 4);
+      x = b - a + insanemath(3, 4) - insanemath(33,2);
       a = x * (x + a);
    }
    else
@@ -46,15 +58,44 @@
 if __name__ == '__main__':
     diag = ppci.DiagnosticsManager()
     builder = c3.Builder(diag)
-    ir = builder.build(testsrc)
-    ir.dump()
+    irc = builder.build(testsrc)
+    if not irc:
+        diag.printErrors(testsrc)
+    irc.check()
+    irc.dump()
+    with open('ir.gv', 'w') as f:
+        ir.dumpgv(irc, f)
     outs = outstream.TextOutputStream()
     cga = codegenarm.ArmCodeGenerator(outs)
-    cfg_file = open('cfg.gv', 'w')
-    ig_file = open('ig.gv', 'w')
-    ir2 = cga.generate(ir, cfg_file=cfg_file, ig_file=ig_file)
-    cfg_file.close()
-    ig_file.close()
-    for i in ir2:
-        print(i)
+    ir2 = cga.generate(irc)
+
+    with open('cfg.gv', 'w') as cfg_file:
+        print('digraph G {', file=cfg_file)
+        #print('edge [constraint=none]', file=cfg_file)
+        print('rankdir=TB', file=cfg_file)
+        for f in cga.frames:
+            print('subgraph cluster_{} {{'.format(f.name), file=cfg_file)
+            print('label={};'.format(f.name), file=cfg_file)
+            print('color=lightgrey;', file=cfg_file)
+            print('style=filled;', file=cfg_file)
+            f.cfg.to_dot(cfg_file)
+            print('}', file=cfg_file)
+        print('}', file=cfg_file)
 
+    with open('ig.gv', 'w') as ig_file:
+        print('digraph G {', file=ig_file)
+        print('edge [arrowhead=none]', file=ig_file)
+        for f in cga.frames:
+            print('subgraph cluster_{} {{'.format(f.name), file=ig_file)
+            print('label={};'.format(f.name), file=ig_file)
+            print('color=lightgrey;', file=ig_file)
+            print('style=filled;', file=ig_file)
+            f.ig.to_dot(ig_file)
+            print('}', file=ig_file)
+        print('}', file=ig_file)
+        
+    for f in ir2:
+        print(f)
+        for i in f.instructions:
+            print('   {}'.format(i))
+