Exemple #1
0
 def testEmpty(self):
     function = FunctionDecl(void_type, 'f', [], Block([]))
     cfg = CFG('f')
     cfg.connect(cfg.entry, cfg.exit)
     function.cfg = cfg
     program = Program([function])
     lines = self.assertSuccess(program)
     self.assertEquals(
         lines, [Label('f', public=True),
                 Label('f$exit', public=True)])
Exemple #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))
Exemple #3
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)
     ])
Exemple #4
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,
         )
     )
Exemple #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)
     ])
Exemple #6
0
 def visit_FunctionDecl(self, func):
     cfg = CFG(func.name)
     func.cfg = cfg
     
     if hasattr(func, 'symbol_table'):
         cfg.symbol_table = SymbolTable(func.symbol_table.parent)
         cfg.symbol_table.embed(func.symbol_table)
     else:
         cfg.symbol_table = SymbolTable()
     
     prev_node = self.visit(func.body, cfg=cfg, entry=cfg.entry, exit=cfg.exit, break_target=None)
     if prev_node is not None:
         cfg.connect(prev_node, cfg.exit)
     
     cfg.remove_pass_nodes()
Exemple #7
0
    def visit_FunctionDecl(self, func):
        cfg = CFG(func.name)
        func.cfg = cfg

        if hasattr(func, 'symbol_table'):
            cfg.symbol_table = SymbolTable(func.symbol_table.parent)
            cfg.symbol_table.embed(func.symbol_table)
        else:
            cfg.symbol_table = SymbolTable()

        prev_node = self.visit(func.body,
                               cfg=cfg,
                               entry=cfg.entry,
                               exit=cfg.exit,
                               break_target=None)
        if prev_node is not None:
            cfg.connect(prev_node, cfg.exit)

        cfg.remove_pass_nodes()
Exemple #8
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)
     ])