memory.hh 3.01 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//
// This file is part of Mipsy, a tiny MIPS simulator
// Copyright (C) 2003 Benoit Perrot <benoit@lrde.epita.fr>
//
// Mipsy is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// Mipsy is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#ifndef VM_MEMORY_HH
# define VM_MEMORY_HH

# include "misc/contract.hh"

# include "inst/data_section.hh"
# include "vm/segment.hh"

// FIXME: lack of compatibility
# include <stdint.h>
# ifndef INT32_MAX
#  define INT32_MAX		2147483647
# endif

namespace vm
{

  class Memory
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
39
40
41
    static const int	stack_bottom = INT32_MAX - 3;
    static const int	default_stack_size = 128 * 1024;
    
42
  public:
Benoit Perrot's avatar
Benoit Perrot committed
43
44
    Memory(int stack_size = default_stack_size):
      heap(0), stack(stack_size), stack_top(stack_bottom - stack_size + 4)
45
    {
Benoit Perrot's avatar
Benoit Perrot committed
46
      assertion(stack_size > 0);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    }

  protected:
    int			translate(int offset) const
    {
      precondition(offset >= stack_top);
      // Stack grows down
      return offset - stack_top;
    }

  public:
    int			sbrk(int size)
    {
      precondition(size >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
61
      int		ptr = heap.size();
62
63
      if (size)
	// FIXME: check collision with stack
Benoit Perrot's avatar
Benoit Perrot committed
64
	heap.resize(size + heap.size());
65
66
67
      return ptr;
    }

68
  public:
Benoit Perrot's avatar
Benoit Perrot committed
69
    int			heap_size() const
70
    {
Benoit Perrot's avatar
Benoit Perrot committed
71
      return heap.size ();      
72
    }
Benoit Perrot's avatar
Benoit Perrot committed
73
    int			stack_size() const
74
    {
Benoit Perrot's avatar
Benoit Perrot committed
75
      return stack.size ();
76
77
    }

78
79
80
  public:
    void		store(const inst::DataSection& data_section)
    {
Benoit Perrot's avatar
Benoit Perrot committed
81
      heap.resize(data_section.size());
82
      for (int i = 0; i < data_section.size(); ++i)
Benoit Perrot's avatar
Benoit Perrot committed
83
	heap.store_byte(i, data_section.load_byte(i));
84
85
86
87
88
    }

    void		store_byte(int offset, int b)
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
89
90
      if (offset < heap.size())
	heap.store_byte(offset, b);
91
      else
Benoit Perrot's avatar
Benoit Perrot committed
92
	stack.store_byte(translate(offset), b);
93
94
95
96
97
    }

    void		store_word(int offset, int w)
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
98
99
      if (offset < heap.size())
	heap.store_word(offset, w);
100
      else
Benoit Perrot's avatar
Benoit Perrot committed
101
	stack.store_word(translate(offset), w);
102
103
104
105
106
107
108
    }

    
  public:
    int			load_byte(int offset) const
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
109
110
111
      if (offset < heap.size())
	return heap.load_byte(offset);
      return stack.load_byte(translate(offset));
112
113
114
115
116
    }

    int			load_word(int offset) const
    {
      precondition(offset >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
117
118
119
      if (offset < heap.size())
	return heap.load_word(offset);
      return stack.load_word(translate(offset));
120
121
122
123
    }


  protected:
Benoit Perrot's avatar
Benoit Perrot committed
124
125
126
    Segment		heap;
    Segment		stack;
    const int		stack_top;
127
128
129
130
131
  };

} // namespace vm

#endif // !VM_MEMORY_HH