memory.hh 3.04 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
#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
Benoit Perrot's avatar
Benoit Perrot committed
32
//# include <string.h>
33
34
35
36
37
38
39

namespace vm
{

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

  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
62
      int		ptr = heap.size();
63
64
      if (size)
	// FIXME: check collision with stack
Benoit Perrot's avatar
Benoit Perrot committed
65
	heap.resize(size + heap.size());
66
67
68
      return ptr;
    }

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

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

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

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

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

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


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

} // namespace vm

#endif // !VM_MEMORY_HH