memory.hh 3.17 KB
Newer Older
Benoit Perrot's avatar
Benoit Perrot committed
1
//
Benoit Perrot's avatar
Benoit Perrot committed
2
// This file is part of Nolimips, a MIPS simulator with unlimited registers
Benoit Perrot's avatar
Benoit Perrot committed
3
// Copyright (C) 2003, 2004 Benoit Perrot <benoit@lrde.epita.fr>
Benoit Perrot's avatar
Benoit Perrot committed
4
//
Benoit Perrot's avatar
Benoit Perrot committed
5
// Nolimips is free software; you can redistribute it and/or modify
Benoit Perrot's avatar
Benoit Perrot committed
6
7
8
9
// 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.
// 
Benoit Perrot's avatar
Benoit Perrot committed
10
// Nolimips is distributed in the hope that it will be useful,
Benoit Perrot's avatar
Benoit Perrot committed
11
12
13
14
15
16
17
18
// 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
//
Benoit Perrot's avatar
Benoit Perrot committed
19
20
/** \file vm/memory.hh
    \brief Declare Memory class. */
21
22
23
#ifndef VM_MEMORY_HH
# define VM_MEMORY_HH

Benoit Perrot's avatar
Benoit Perrot committed
24
25
26
27
# include "config.h"

# include <stdint.h>

28
29
30
31
32
33
34
35
# include "misc/contract.hh"

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

namespace vm
{

Benoit Perrot's avatar
Benoit Perrot committed
36
  /// Memory abstraction
37
38
39
  class Memory
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
40
    static const int	stack_bottom = INT32_MAX - 3;
Benoit Perrot's avatar
Benoit Perrot committed
41
    static const int	default_stack_size = 512 * 1024;
Benoit Perrot's avatar
Benoit Perrot committed
42
43
44
45
46
47

    enum segment_kind
      {
	heap = 0,
	stack = 1
      };
Benoit Perrot's avatar
Benoit Perrot committed
48
    
Benoit Perrot's avatar
Benoit Perrot committed
49
50
    /** \name Constructor and destructor 
	\{ */
51
  public:
Benoit Perrot's avatar
Benoit Perrot committed
52
    /// Construct a Memory.
Benoit Perrot's avatar
Benoit Perrot committed
53
    Memory(int stack_size = default_stack_size):
Benoit Perrot's avatar
Benoit Perrot committed
54
      stack_top_(stack_bottom - stack_size + 4)
55
    {
Benoit Perrot's avatar
Benoit Perrot committed
56
      assertion(stack_size > 0);
Benoit Perrot's avatar
Benoit Perrot committed
57
      segments_[stack].resize(stack_size);
58
    }
Benoit Perrot's avatar
Benoit Perrot committed
59
    /** \} */
60

Benoit Perrot's avatar
Benoit Perrot committed
61
62
63
64
65
66
    /// Reset the memory
    void	reset()
    {
      // DO NOT zero-ify the memory
    }

67
  public:
Benoit Perrot's avatar
Benoit Perrot committed
68
    int		sbrk(int size)
69
70
    {
      precondition(size >= 0);
Benoit Perrot's avatar
Benoit Perrot committed
71
      int	ptr = segments_[heap].size();
72
73
      if (size)
	// FIXME: check collision with stack
Benoit Perrot's avatar
Benoit Perrot committed
74
	segments_[heap].resize(size + segments_[heap].size());
75
76
77
      return ptr;
    }

78
  public:
Benoit Perrot's avatar
Benoit Perrot committed
79
    int			heap_size() const
80
    {
Benoit Perrot's avatar
Benoit Perrot committed
81
      return segments_[heap].size();
82
    }
Benoit Perrot's avatar
Benoit Perrot committed
83
    int			stack_size() const
84
    {
Benoit Perrot's avatar
Benoit Perrot committed
85
86
87
88
89
      return segments_[stack].size();
    }
    int		get_stack_top() const
    {
      return stack_top_;
90
91
    }

Benoit Perrot's avatar
Benoit Perrot committed
92
93
    /** \name Store instructions.
	\{ */
94
95
96
  public:
    void		store(const inst::DataSection& data_section)
    {
Benoit Perrot's avatar
Benoit Perrot committed
97
      segments_[heap].resize(data_section.size());
98
      for (int i = 0; i < data_section.size(); ++i)
Benoit Perrot's avatar
Benoit Perrot committed
99
	segments_[heap].store_byte(i, data_section.load_byte(i));
100
101
    }

Benoit Perrot's avatar
Benoit Perrot committed
102
    /// Store a byte in memory.
Benoit Perrot's avatar
Benoit Perrot committed
103
    void	store_byte(segment_kind k, int offset, int b)
104
    {
Benoit Perrot's avatar
Benoit Perrot committed
105
      segments_[k].store_byte(offset, b);
106
    }
Benoit Perrot's avatar
Benoit Perrot committed
107
    /// Store a word in memory.
Benoit Perrot's avatar
Benoit Perrot committed
108
    void	store_word(segment_kind k, int offset, int w)
109
    {
Benoit Perrot's avatar
Benoit Perrot committed
110
      segments_[k].store_word(offset, w);
111
    }
Benoit Perrot's avatar
Benoit Perrot committed
112
    /** \} */
113
    
Benoit Perrot's avatar
Benoit Perrot committed
114
115
    /** \name Load instructions.
	\{ */
116
  public:
Benoit Perrot's avatar
Benoit Perrot committed
117
    /// Load a byte from memory.
Benoit Perrot's avatar
Benoit Perrot committed
118
    int		load_byte(segment_kind k, int offset) const
119
    {
Benoit Perrot's avatar
Benoit Perrot committed
120
      return segments_[k].load_byte(offset);
121
    }
Benoit Perrot's avatar
Benoit Perrot committed
122
    /// Load a word from memory.
Benoit Perrot's avatar
Benoit Perrot committed
123
    int		load_word(segment_kind k, int offset) const
124
    {
Benoit Perrot's avatar
Benoit Perrot committed
125
      return segments_[k].load_word(offset);
126
    }
Benoit Perrot's avatar
Benoit Perrot committed
127
    /** \} */
128
129

  protected:
Benoit Perrot's avatar
Benoit Perrot committed
130
    Segment		segments_[2];
Benoit Perrot's avatar
Benoit Perrot committed
131
    const int		stack_top_;
132
133
134
135
136
  };

} // namespace vm

#endif // !VM_MEMORY_HH