Example #1
0
    def testSimple(self):
        h_func = FunctionDecl(void_type, 'h', [], Block([]))

        hfc = FunctionCall('h', [])
        hfc.declaration = h_func
        gfc = FunctionCall('g', [])
        g_block = Block([Statement(hfc), ReturnStatement(gfc)])
        g_func = FunctionDecl(int_type, 'g', [], g_block)
        gfc.declaration = g_func

        program = Program([g_func])

        self.assertSuccess(program)

        cfg = g_func.cfg
        self.assertTrue(
            cfg.has_path(cfg.entry, Operation(hfc), Operation(hfc)), cfg)
        self.assertFalse(cfg.has_path(cfg.entry, Operation(hfc), cfg.exit),
                         cfg)
Example #2
0
 def testOut(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     n5 = Numeral(5)
     n5.type = int_type
     n1 = Numeral(1)
     n1.type = int_type
     stmt = Operation(FunctionCall(Name('__out__'), [n5, n1]))
     cfg.connect(cfg.entry, stmt, cfg.exit)
     function.cfg = cfg
     function.symbol_table = SymbolTable()
     cfg.symbol_table = SymbolTable(function.symbol_table)
     program = Program([function])
     self.assertSuccess(program)
     self.assertTrue(function.cfg.has_path(function.cfg.entry,
         Operation(AssignStatement(Name('$t0'), Numeral(5))),
         Operation(AssignStatement(Name('$t1'), Numeral(1))),
         Operation(FunctionCall(Name('__out__'), [Name('$t0'), Name('$t1')])),
         function.cfg.exit))
Example #3
0
 def testTest(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     test_node = cfg.add(Test(42))
     cfg.connect(cfg.entry, test_node)
     true_node = cfg.add(Operation(100))
     cfg.connect(test_node, TrueEdge(), true_node, cfg.exit)
     false_node = cfg.add(Operation(200))
     cfg.connect(test_node, FalseEdge(), false_node, cfg.exit)
     function.cfg = cfg
     program = Program([function])
     lines = self.assertSuccess(program)
     self.assertEquals(lines, [
         Label('f', public=True),
         Branch(42, 3),
         Label(4),
         Instruction(200),
         Jump('f$exit'),
         Label(3),
         Instruction(100),
         Label('f$exit', public=True)
     ])
Example #4
0
 def testOneStatement(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     stmt = cfg.add(Operation(42))
     cfg.connect(cfg.entry, stmt)
     cfg.connect(stmt, cfg.exit)
     function.cfg = cfg
     program = Program([function])
     lines = self.assertSuccess(program)
     self.assertEquals(lines, [
         Label('f', public=True),
         Instruction(42),
         Label('f$exit', public=True)
     ])
Example #5
0
 def testInfiniteLoop(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     node = cfg.add(Operation(42))
     cfg.connect(cfg.entry, node, node)
     function.cfg = cfg
     program = Program([function])
     lines = self.assertSuccess(program)
     self.assertEquals(lines, [
         Label('f', public=True),
         Label(2),
         Instruction(42),
         Jump(2),
         Label('f$exit', public=True)
     ])
Example #6
0
    def testVarAssign(self):
        h_func = FunctionDecl(void_type, 'h', [], Block([]))

        y_decl = VariableDecl(int_type, 'y')
        y_var = Name(y_decl)

        hfc = FunctionCall('h', [])
        hfc.declaration = h_func
        gfc = FunctionCall('g', [y_var])
        g_block = Block([Statement(hfc), ReturnStatement(gfc)])
        g_func = FunctionDecl(int_type, 'g', [ArgDecl(int_type, 'x')], g_block)
        gfc.declaration = g_func

        program = Program([g_func])

        self.assertSuccess(program)

        cfg = g_func.cfg
        self.assertTrue(
            cfg.has_path(cfg.entry, Operation(hfc),
                         Operation(AssignStatement(Name('x'), Name('y'))),
                         Operation(hfc)), cfg)
        self.assertFalse(cfg.has_path(cfg.entry, Operation(hfc), cfg.exit),
                         cfg)
Example #7
0
    def testSimple(self):
        h_func = FunctionDecl(void_type, 'g', [], Block([]))

        hfc = FunctionCall('h', [])
        hfc.declaration = h_func
        g_block = Block([Statement(hfc)])
        g_func = FunctionDecl(void_type, 'g', [], g_block)

        fc = FunctionCall('g', [])
        fc.declaration = g_func
        f_block = Block([Statement(fc)])
        f_func = FunctionDecl(void_type, 'f', [], f_block)
        program = Program([f_func, g_func, h_func])

        self.assertSuccess(program)

        cfg = f_func.cfg
        self.assertTrue(
            cfg.has_path(cfg.entry, Operation(FunctionCall('h', [])),
                         cfg.exit), cfg)
Example #8
0
 def testSingleLine(self):
     lines = [Label('f'), Instruction(42), Label('f$exit')]
     cfg = delinearise(lines)
     self.assertTrue(cfg.has_path(cfg.entry, Operation(42), cfg.exit),
                     msg=cfg)
Example #9
0
 def visit_AssignStatement(self, assign, cfg, entry, exit, break_target):
     stmt_node = cfg.add(Operation(assign))
     cfg.connect(entry, stmt_node)
     return stmt_node
Example #10
0
 def visit_Statement(self, stmt, cfg, entry, exit, break_target):
     stmt_node = cfg.add(Operation(stmt.expression))
     cfg.connect(entry, stmt_node)
     return stmt_node