def test_parameter_passing_on_new_scope(self): tbl = SymbolTable() tbl.create_scope() tbl.add_entity(VariableEntity("foo")) tbl.add_entity(FunctionEntity("fn", 42)) tbl.add_argument(Argument("bat", "cv")) tbl.add_argument(Argument("man", "ref")) tbl.create_scope() self.assertEqual(tbl.scopes[-1].entities, [ ParameterEntity(name="bat", mode="cv", offset=12), ParameterEntity(name="man", mode="ref", offset=16) ])
def test_storerv_smaller_nesting_level_in_parameter(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=3) tbl.lookup = MagicMock( return_value=LookupResult(ParameterEntity('in_par0', 'cv', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.storerv(3, 'in_par0'), gen.gnlvcode('in_par0') + ['sw $t3, ($t0)'])
def test_loadvr_one_diff_nesting_level_in_parameter(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=2) tbl.lookup = MagicMock( return_value=LookupResult(ParameterEntity('in_par0', 'cv', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.loadvr('in_par0', 1), gen.gnlvcode('in_par0') + ['lw $t1, ($t0)'])
def test_storerv_same_nesting_level_in_parameter(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=1) tbl.lookup = MagicMock( return_value=LookupResult(ParameterEntity('in_par0', 'cv', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.storerv(3, 'in_par0'), ['sw $t3, -4($sp)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('in_par0')
def test_loadvr_same_nesting_level_inout_parameter(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=1) tbl.lookup = MagicMock(return_value=LookupResult( ParameterEntity('inout_par0', 'ref', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.loadvr('inout_par0', 0), ['lw $t0, -4($sp)', 'lw $t0, ($t0)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('inout_par0')
def test_exam_example(self): tbl = SymbolTable() tbl.create_scope() tbl.add_entity(VariableEntity("a")) tbl.add_entity(VariableEntity("b")) tbl.add_entity(FunctionEntity("P1", 3)) tbl.add_argument(Argument("x", "cv")) tbl.add_argument(Argument("y", "ref")) tbl.create_scope() tbl.add_entity(VariableEntity("c")) tbl.add_entity(VariableEntity("d")) tbl.add_entity(FunctionEntity("P11", 6)) tbl.add_argument(Argument("w", "cv")) tbl.add_argument(Argument("z", "ref")) tbl.create_scope() tbl.add_entity(VariableEntity("e")) tbl.add_entity(FunctionEntity("P21", 8)) tbl.add_argument(Argument("x", "cv")) tbl.create_scope() tbl.add_entity(TempVariableEntity("T_0")) self.assertEqual(len(tbl.scopes), 4) self.assertEqual( tbl.scopes[0], Scope(entities=[ VariableEntity(name="a", offset=12), VariableEntity(name="b", offset=16), FunctionEntity( name="P1", start_quad=3, arguments=[Argument("x", "cv"), Argument("y", "ref")]) ], nesting_level=0)) self.assertEqual( tbl.scopes[1], Scope(entities=[ ParameterEntity(name="x", mode="cv", offset=12), ParameterEntity(name="y", mode="ref", offset=16), VariableEntity(name="c", offset=20), VariableEntity(name="d", offset=24), FunctionEntity( name="P11", start_quad=6, arguments=[Argument("w", "cv"), Argument("z", "ref")]) ], nesting_level=1)) self.assertEqual( tbl.scopes[2], Scope( entities=[ ParameterEntity(name="w", mode="cv", offset=12), ParameterEntity(name="z", mode="ref", offset=16), VariableEntity(name="e", offset=20), FunctionEntity(name="P21", start_quad=8, arguments=[Argument("x", "cv")]) # TODO: add framelength (20) above ^ ], nesting_level=2)) self.assertEqual( tbl.scopes[3], Scope(entities=[ ParameterEntity(name="x", mode="cv", offset=12), TempVariableEntity(name="T_0", offset=16) ], nesting_level=3)) tbl.fill_in_framelength_on_callee() tbl.destroy_scope() self.assertEqual( tbl.scopes[-1], Scope(entities=[ ParameterEntity(name="w", mode="cv", offset=12), ParameterEntity(name="z", mode="ref", offset=16), VariableEntity(name="e", offset=20), FunctionEntity(name="P21", start_quad=8, arguments=[Argument("x", "cv")], frame_length=20) ], nesting_level=2))