Commit 2b10fad1 authored by Benoit Perrot's avatar Benoit Perrot
Browse files

Factorize Section code.

parent 64de91bc
2003-07-12 Benot Perrot <benoit@lrde.epita.fr>
* src/inst/section.hh,
* src/inst/data_section.hh, src/inst/text_section.hh:
Factorize Section code.
2003-07-11 Benot Perrot <benoit@lrde.epita.fr>
* config/{depcomp, install-sh, missing, mkinstalldirs}:
......
......@@ -21,14 +21,10 @@
// FIXME: lack of compatibility
# include <stdint.h>
# include <string.h>
# include <list>
# include <map>
# include <iostream>
# include "misc/contract.hh"
# include "inst/label.hh"
# include "inst/section.hh"
# include "inst/inst.hh"
// FIXME: should not be a #define !
......@@ -37,7 +33,8 @@
namespace inst
{
class DataSection
class DataSection:
public Section
{
typedef std::list<const Label*> label_list_t;
typedef std::map<Label, int> label_to_offset_t;
......@@ -46,7 +43,7 @@ namespace inst
DataSection():
_size(0)
{
memset(_bytes, 0, INST_DATA_DATASIZE);
std::memset(bytes, 0, INST_DATA_DATASIZE);
}
public:
......@@ -58,21 +55,7 @@ namespace inst
public:
void add_label(Label& label)
{
_offsets[label] = _size;
_labels[_size].push_back(&label);
}
bool has_label(const Label& label) const
{
label_to_offset_t::const_iterator it = _offsets.find(label);
return it != _offsets.end();
}
int get_offset(const Label& label) const
{
label_to_offset_t::const_iterator it = _offsets.find(label);
assertion(it != _offsets.end());
return (*it).second;
Section::add_label(label, _size);
}
public:
......@@ -87,7 +70,7 @@ namespace inst
{
// FIXME: check b in byte range
precondition(_size < INST_DATA_DATASIZE);
_bytes[_size] = b;
bytes[_size] = b;
++_size;
}
......@@ -105,43 +88,17 @@ namespace inst
add_byte(0);
}
public:
void store_byte(int offset, int b)
{
precondition ((0 <= offset && offset < _size));
_bytes[offset] = (uint8_t)(b);
}
void store_word(int offset, int w)
{
precondition ((0 <= offset) && ((offset + 3) < _size));
unsigned pow = 256 * 256 * 256;
for (unsigned i = 0; i < 4; ++i, ++offset, pow /= 256)
_bytes[offset] = w / pow;
}
public:
int load_byte(int offset) const
{
precondition ((0 <= offset && offset < _size));
return (int8_t)_bytes[offset];
}
int load_word(int offset) const
{
int w = 0;
for (unsigned i = 0; i < 4; ++i, ++offset)
{
precondition ((0 <= offset && offset < _size));
w = (w * 256) + _bytes[offset];
}
return w;
return (int8_t)bytes[offset];
}
public:
void print(std::ostream& ostr) const
virtual void print(std::ostream& ostr) const
{
if (_size == 0)
return;
......@@ -152,14 +109,14 @@ namespace inst
while (i < _size)
{
std::map<int, label_list_t>::const_iterator it =
_labels.find(i);
if (it != _labels.end())
labels.find(i);
if (it != labels.end())
for (label_list_t::const_iterator l = (*it).second.begin();
l != (*it).second.end(); ++l)
ostr << *(*l) << ':' << std::endl;
ostr << "\t.byte\t0x"
<< std::hex << static_cast<unsigned>(_bytes[i++])
<< std::hex << static_cast<unsigned>(bytes[i++])
<< std::endl;
}
ostr << std::dec;
......@@ -167,19 +124,9 @@ namespace inst
protected:
int _size;
uint8_t _bytes[INST_DATA_DATASIZE];
std::map<int, label_list_t> _labels;
label_to_offset_t _offsets;
uint8_t bytes[INST_DATA_DATASIZE];
};
inline std::ostream&
operator<<(std::ostream& ostr, const DataSection& s)
{
s.print(ostr);
return ostr;
}
} // namespace inst
#endif // !INST_DATA_SECTION_HH
//
// 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
//
#ifndef INST_SECTION_HH
# define INST_SECTION_HH
# include <list>
# include <map>
# include "misc/contract.hh"
# include "inst/label.hh"
namespace inst
{
class Section
{
protected:
typedef std::list<const Label*> label_list_t;
typedef std::map<const Label, int> label_offset_t;
public:
Section()
{
}
virtual ~Section()
{
}
protected:
void add_label(Label& label, int offset)
{
// FIXME: check that this label has not already been added.
offsets[label] = offset;
labels[offset].push_back(&label);
}
public:
bool has_label(const Label& label) const
{
label_offset_t::const_iterator it = offsets.find(label);
return it != offsets.end();
}
int get_offset(const Label& label) const
{
label_offset_t::const_iterator it = offsets.find(label);
assertion(it != offsets.end());
return (*it).second;
}
public:
virtual void print(std::ostream& ostr) const = 0;
protected:
// FIXME: Might be static, to avoid multi-definition of labels.
std::map<int, label_list_t> labels;
label_offset_t offsets;
};
inline std::ostream&
operator<<(std::ostream& ostr, const Section& s)
{
s.print(ostr);
return ostr;
}
} // namespace inst
#endif // !INST_SECTION_HH
......@@ -20,106 +20,80 @@
# define INST_TEXT_SECTION_HH
# include <vector>
# include <list>
# include <map>
# include <iostream>
# include "misc/contract.hh"
# include "inst/label.hh"
# include "inst/section.hh"
# include "inst/inst.hh"
namespace inst
{
class TextSection
class TextSection:
public Section
{
typedef std::list<const Label*> label_list_t;
typedef std::map<const Label, int> label_to_offset_t;
public:
TextSection()
{
}
public:
void add_inst(inst::Inst& inst)
int size() const
{
_insts.push_back(&inst);
return insts.size ();
}
public:
void add_label(Label& label)
{
// FIXME: check that this label has not already been added.
_offsets[label] = _insts.size() * 4;
_labels.resize (_insts.size() + 1);
_labels[_insts.size()].push_back(&label);
}
bool has_label(const Label& label) const
{
label_to_offset_t::const_iterator it = _offsets.find(label);
return it != _offsets.end();
}
int get_offset(const Label& label) const
{
label_to_offset_t::const_iterator it = _offsets.find(label);
assertion(it != _offsets.end());
return (*it).second;
Section::add_label(label, insts.size() * 4);
}
public:
int size() const
void add_inst(inst::Inst& inst)
{
return _insts.size ();
insts.push_back(&inst);
}
public:
const inst::Inst& operator[](int offset) const
{
precondition(0 <= offset && (unsigned) offset < _insts.size());
precondition(0 <= offset && (unsigned) offset < insts.size());
assertion(_insts[offset]);
return *(_insts[offset]);
assertion(insts[offset]);
return *(insts[offset]);
}
inst::Inst& operator[](int offset)
{
precondition(0 <= offset && (unsigned) offset < _insts.size());
precondition(0 <= offset && (unsigned) offset < insts.size());
assertion(_insts[offset]);
return *(_insts[offset]);
assertion(insts[offset]);
return *(insts[offset]);
}
public:
void print(std::ostream& ostr) const
virtual void print(std::ostream& ostr) const
{
ostr << "\t.text" << std::endl;
for (unsigned i = 0; i < _insts.size(); ++i)
for (unsigned i = 0; i < insts.size(); ++i)
{
if (i < _labels.size())
for (label_list_t::const_iterator it = _labels[i].begin();
it != _labels[i].end(); ++it)
ostr << *(*it) << ":" << std::endl;
ostr << '\t' << *(_insts[i]) << std::endl;
std::map<int, label_list_t>::const_iterator it =
labels.find(i);
if (it != labels.end())
for (label_list_t::const_iterator l = (*it).second.begin();
l != (*it).second.end(); ++l)
ostr << *(*l) << ':' << std::endl;
ostr << '\t' << *(insts[i]) << std::endl;
}
}
protected:
std::vector<inst::Inst*> _insts;
std::vector<label_list_t> _labels;
label_to_offset_t _offsets;
std::vector<inst::Inst*> insts;
};
inline std::ostream&
operator<<(std::ostream& ostr, const TextSection& s)
{
s.print(ostr);
return ostr;
}
} // namespace inst
#endif // !INST_TEXT_SECTION_HH
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment