view test/tcodegen.py @ 409:8fe299cd2d55 devel

Close devel branch
author Windel Bouwman
date Sat, 21 Feb 2015 12:20:10 +0100
parents 7b38782ed496
children
line wrap: on
line source


"""
  Test individual parts of the code generation for arm using the c3 frontend.
"""

import c3
import ppci
import codegenarm
import outstream
import ir

testsrc = """
package test2;

var int phaa, foo, bar;

function int insanemath(int a, int b)
{
  var int c;
  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;
}

function void tesssst(int henkie)
{
   var int a, b, cee;
   a = 2 * 33 - 12;
   b = a * 2;
   a = b + a;
   cee = a;
   cee = cee * 2 + cee;
   insanemath(2, 3);
   if (cee + a > b and b - a+b== 3*6-insanemath(b, 2))
   {
      var int x = a;
      x = b - a + insanemath(3, 4) - insanemath(33, insanemath(2, 0));
      a = x * (x + a);
   }
   else
   {
      a = b + (a + b);
   }
   var int y;
   y = a - b * 53;
}
"""

testsrc = """
package test3;

function int ab(int a, int b)
{
  var int c;
  c = 0;
  c = c + a + b;
  return c; 
}

function void tesssst()
{
   var int a, b;
   a = 2;
   b = 3;
   ab(ab(a, b) + ab(9,b), 0);
}
"""

testsrc = """
package test3;

function int ab(int a, int b)
{
  var int c;
  var int *a2;
  a2 = cast<int*>(2);
  *a2 = 2;
  *a2 = *a2 + 2;
  c = 0;
  c = c + a + b;
  return c; 
}

"""
def dump_cfg(cga, 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)

def dump_ig(cga, 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)

if __name__ == '__main__':
    diag = ppci.DiagnosticsManager()
    builder = c3.Builder(diag)
    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)
    ir2 = cga.generate(irc)

    with open('cfg.gv', 'w') as cfg_file:
        dump_cfg(cga, cfg_file)

    with open('ig.gv', 'w') as ig_file:
        dump_ig(cga, ig_file)
        
    outs.dump()