def test_loadvr_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.loadvr('in_par0', 1), gen.gnlvcode('in_par0') + ['lw $t1, ($t0)'])
def test_loadvr_one_diff_nesting_level_local(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=2) tbl.lookup = MagicMock( return_value=LookupResult(VariableEntity('local_var0', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.loadvr('local_var0', 1), gen.gnlvcode('local_var0') + ['lw $t1, ($t0)'])
def test_loadvr_constant(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock() tbl.lookup = MagicMock() gen = FinalGen(tbl) self.assertEqual(gen.loadvr('42', 0), ['li $t0, 42']) tbl.get_current_nesting_level.assert_not_called() tbl.lookup.assert_not_called()
def test_loadvr_same_nesting_level_temp(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=1) tbl.lookup = MagicMock( return_value=LookupResult(TempVariableEntity('temp_var0', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.loadvr('temp_var0', 0), ['lw $t0, -4($sp)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('temp_var0')
def test_loadvr_global_var(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=0) tbl.lookup = MagicMock( return_value=LookupResult(VariableEntity('var0', 4), 0)) gen = FinalGen(tbl) self.assertEqual(gen.loadvr('var0', 0), ['lw $t0, -4($s0)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('var0')
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')