data_section.hh 2.37 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
//
19
20
21
#ifndef INST_DATA_SECTION_HH
# define INST_DATA_SECTION_HH

Benoit Perrot's avatar
Benoit Perrot committed
22
23
# include "config.h"

24
25
26
27
# include <stdint.h>

# include <iostream>

Benoit Perrot's avatar
Benoit Perrot committed
28
# include "inst/section.hh"
29
30
31
32
33
34
35
36
# include "inst/inst.hh"

// FIXME: should not be a #define !
# define INST_DATA_DATASIZE	128 * 1024

namespace inst
{
  
Benoit Perrot's avatar
Benoit Perrot committed
37
  /// Abstract an assembly data section
Benoit Perrot's avatar
Benoit Perrot committed
38
39
  class DataSection:
    public Section
40
41
  {
  public:
Benoit Perrot's avatar
Benoit Perrot committed
42
43
    DataSection(const std::string &name = ".data"):
      Section(name),
Benoit Perrot's avatar
Benoit Perrot committed
44
      size_(0)
45
    {
Benoit Perrot's avatar
Benoit Perrot committed
46
      std::memset(bytes_, 0, INST_DATA_DATASIZE);
47
    }
Benoit Perrot's avatar
Benoit Perrot committed
48
    virtual ~DataSection();
49
50
51
52

  public:
    int			size () const
    {
Benoit Perrot's avatar
Benoit Perrot committed
53
      return size_;
54
55
56
    }
    
  public:
Benoit Perrot's avatar
Benoit Perrot committed
57
    void		add_label(Label *label)
58
    {
Benoit Perrot's avatar
Benoit Perrot committed
59
      Section::add_label(label, size_);
60
61
62
63
64
65
    }
    
  public:
    void		add_space(int space_size)
    {
      // FIXME: check b in byte range 
Benoit Perrot's avatar
Benoit Perrot committed
66
67
      precondition(size_ + space_size < INST_DATA_DATASIZE);
      size_ += space_size;
68
69
70
71
72
    }

    void		add_byte(int b)
    {
      // FIXME: check b in byte range 
Benoit Perrot's avatar
Benoit Perrot committed
73
74
75
      precondition(size_ < INST_DATA_DATASIZE);
      bytes_[size_] = b;
      ++size_;
76
77
78
79
80
81
82
83
84
85
86
87
    }

    void		add_word(int w)
    {
      unsigned		pow = 256 * 256 * 256;
      for (int i = 0; i < 4; ++i, pow /= 256)
	add_byte(w / pow);
    }
    
  public:
    int			load_byte(int offset) const
    {
Benoit Perrot's avatar
Benoit Perrot committed
88
89
      precondition ((0 <= offset && offset < size_));
      return (int8_t)bytes_[offset];
90
91
92
93
    }


  public:
Benoit Perrot's avatar
Benoit Perrot committed
94
    virtual void	print(std::ostream& ostr) const;
95
96

  protected:
Benoit Perrot's avatar
Benoit Perrot committed
97
98
    int					size_;
    uint8_t				bytes_[INST_DATA_DATASIZE];
99
100
101
102
103
  };

} // namespace inst

#endif // !INST_DATA_SECTION_HH