def test_storerv_smaller_nesting_level_local(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=3) tbl.lookup = MagicMock( return_value=LookupResult(VariableEntity('local_var0', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.storerv(1, 'local_var0'), gen.gnlvcode('local_var0') + ['sw $t1, ($t0)'])
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_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_storerv_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.storerv(4, 'temp_var0'), ['sw $t4, -4($sp)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('temp_var0')
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_storerv_global_var(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=0) tbl.lookup = MagicMock( return_value=LookupResult(VariableEntity('global_var0', 4), 0)) gen = FinalGen(tbl) self.assertEqual(gen.storerv(2, 'global_var0'), ['sw $t2, -4($s0)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('global_var0')
def test_loadvr_same_nesting_level_local(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=1) tbl.lookup = MagicMock( return_value=LookupResult(VariableEntity('local_var0', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.loadvr('local_var0', 0), ['lw $t0, -4($sp)']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('local_var0')
def test_gnlvcode_nesting_level_1(self): tbl = SymbolTable() tbl.get_current_nesting_level = MagicMock(return_value=1) tbl.lookup = MagicMock( return_value=LookupResult(VariableEntity('var0', 4), 1)) gen = FinalGen(tbl) self.assertEqual(gen.gnlvcode('var0'), ['lw $t0, -4($sp)', 'add $t0, $t0, -4']) tbl.get_current_nesting_level.assert_called_once() tbl.lookup.assert_called_once_with('var0')