Commit 03178a83 authored by Benoit Perrot's avatar Benoit Perrot
Browse files

Index: ChangeLog

from  Benoît Perrot  <benoit@lrde.epita.fr>

        * src/misc/counted_ptr.hh,
        * src/misc/Makefile.am:
        Add counted_ptr tool.
        * dev/inst-nodes-gen.py,
        * src/inst/exp.hh,
        * src/inst/program_builder.cc:
        Use counted pointers in expression trees.
parent 3d642b98
2004-05-15 Benot Perrot <benoit@lrde.epita.fr>
* src/misc/counted_ptr.hh,
* src/misc/Makefile.am:
Add counted_ptr tool.
* dev/inst-nodes-gen.py,
* src/inst/exp.hh,
* src/inst/program_builder.cc:
Use counted pointers in expression trees.
2004-05-08 Benot Perrot <benoit@lrde.epita.fr>
* NEWS,
......
......@@ -85,6 +85,8 @@ def class_generate(inst):
if attribute.type == "Register":
args = args + "const " + attribute.type + " &"
elif attribute.type == "Exp":
args = args + "const CountedExp &"
else:
args = args + attribute.type + " *"
args = args + attribute.name + ", "
......@@ -113,8 +115,8 @@ def class_generate(inst):
print " {"
for attribute in format.attributes:
# FIXME: there should be some preconditions
if attribute.type != "Register":
print " delete " + attribute.name + "_;"
if attribute.type != "Register" and attribute.type != "Exp":
print " delete " + attribute.name + "_;"
print " }\n"
# Methods: accessors
......@@ -206,6 +208,8 @@ def class_generate(inst):
id = attribute.name + "_"
if attribute.type == "Register":
print " " + attribute.type + "\t" + id + ";"
elif attribute.type == "Exp":
print " CountedExp\t" + id + ";"
else:
print " " + attribute.type + "\t*" + id + ";"
......
......@@ -2,19 +2,25 @@
(Created-By-Prcs-Version 1 3 2)
(Project-Description "")
(Project-Version mipsy 0 97)
(Parent-Version mipsy 0 96)
(Project-Version mipsy 0 98)
(Parent-Version mipsy 0 97)
(Version-Log
"Index: ChangeLog
* doc/mipsy.texi:
Document it.
from Benot Perrot <benoit@lrde.epita.fr>
* src/misc/counted_ptr.hh,
* src/misc/Makefile.am:
Add counted_ptr tool.
* dev/inst-nodes-gen.py,
* src/inst/exp.hh,
* src/inst/program_builder.cc:
Use counted pointers in expression trees.
")
(New-Version-Log
"")
(Checkin-Time "Sat, 08 May 2004 22:12:14 +0200")
(Checkin-Time "Sat, 15 May 2004 22:11:41 +0200")
(Checkin-Login benoit)
(Files
......@@ -22,7 +28,7 @@ from Beno
;; ./
(AUTHORS (mipsy/0_AUTHORS 1.2 644))
(COPYING (mipsy/1_COPYING 1.1 644))
(ChangeLog (mipsy/2_ChangeLog 1.91 604))
(ChangeLog (mipsy/2_ChangeLog 1.92 604))
(INSTALL (mipsy/b/12_INSTALL 1.1 604))
(Makefile.am (mipsy/3_Makefile.a 1.9 604))
(NEWS (mipsy/b/25_NEWS 1.11 604))
......@@ -40,7 +46,7 @@ from Beno
(dev/doc-inst-set-gen.py (mipsy/c/21_doc-inst-s 1.4 705))
(dev/inst-builder-gen.py (mipsy/15_mipsy-buil 1.14 705))
(dev/inst-makefile-gen.py (mipsy/b/15_mipsy-mk-i 1.9 705))
(dev/inst-nodes-gen.py (mipsy/12_mipsy-inst 1.11 705))
(dev/inst-nodes-gen.py (mipsy/12_mipsy-inst 1.12 705))
(dev/inst-solver-gen.py (mipsy/b/13_inst-solve 1.3 755))
(dev/mipsy.py (mipsy/16_mipsy.py 1.10 705))
(dev/mipsy.xml (mipsy/17_mipsy.xml 1.17 604))
......@@ -65,7 +71,7 @@ from Beno
(src/inst/data_section.hh (mipsy/24_data_secti 1.8 604))
(src/inst/data_section.cc (mipsy/c/31_data_secti 1.2 644))
(src/inst/exp-visitor.hh (mipsy/25_exp-visito 1.4 604))
(src/inst/exp.hh (mipsy/26_exp.hh 1.8 604))
(src/inst/exp.hh (mipsy/26_exp.hh 1.9 604))
(src/inst/exp.cc (mipsy/c/32_exp.cc 1.3 644))
(src/inst/inst-tasks.cc (mipsy/b/23_inst-tasks 1.2 644))
(src/inst/inst-tasks.hh (mipsy/b/22_inst-tasks 1.2 644))
......@@ -73,15 +79,16 @@ from Beno
(src/inst/label.cc (mipsy/b/21_label.cc 1.4 604))
(src/inst/label.hh (mipsy/b/20_label.hh 1.4 604))
(src/inst/program.hh (mipsy/28_program.hh 1.4 644))
(src/inst/program_builder.cc (mipsy/29_program_bu 1.15 604))
(src/inst/program_builder.cc (mipsy/29_program_bu 1.16 604))
(src/inst/register.hh (mipsy/31_register.h 1.9 644))
(src/inst/section.hh (mipsy/b/24_section.hh 1.8 604))
(src/inst/text_section.hh (mipsy/32_text_secti 1.8 604))
(src/inst/text_section.cc (mipsy/c/33_text_secti 1.2 644))
;; ./src/misc/
(src/misc/Makefile.am (mipsy/33_Makefile.a 1.2 644))
(src/misc/Makefile.am (mipsy/33_Makefile.a 1.3 644))
(src/misc/contract.hh (mipsy/34_contract.h 1.1 644))
(src/misc/counted_ptr.hh (mipsy/c/27_counted_pt 1.1 644))
(src/misc/deref.hh (mipsy/35_deref.hh 1.2 644))
(src/misc/escape.hh (mipsy/36_escape.hh 1.2 644))
(src/misc/has.hh (mipsy/37_has.hh 1.1 644))
......
......@@ -22,6 +22,7 @@
# include <iostream>
# include "misc/contract.hh"
# include "misc/counted_ptr.hh"
# include "inst/label.hh"
# include "inst/exp-visitor.hh"
......@@ -67,6 +68,8 @@ namespace inst
return ostr;
}
typedef misc::counted_ptr<Exp> CountedExp;
class IntExp:
public Exp
{
......@@ -132,10 +135,10 @@ namespace inst
public:
enum kind_type { add, sub, mul, div };
public:
OpExp(Exp *left, kind_type kind, Exp *right):
OpExp(const CountedExp &left, kind_type kind, const CountedExp &right):
kind_(kind), left_(left), right_(right)
{
assertion(left && right);
// assertion(left && right);
}
virtual ~OpExp();
......@@ -168,8 +171,8 @@ namespace inst
public:
const kind_type kind_;
protected:
Exp *left_;
Exp *right_;
CountedExp left_;
CountedExp right_;
};
} // namespace inst
......
......@@ -125,12 +125,14 @@ namespace inst
void
ProgramBuilder::add_rol(Register *dest, Register *src1, Exp *src2)
{
// FIXME: CRITICAL: src2 must be cloned or reference counted!
CountedExp counted_exp(src2);
program_->text_section ().
add_inst(new Sll(Register(Register::general, Cpu::at), *src1, src2));
add_inst(new Sll(Register(Register::general, Cpu::at), *src1,
counted_exp));
program_->text_section ().
add_inst(new Srl(*dest, *src1,
new OpExp(new IntExp(32), OpExp::sub, src2)));
new OpExp(new IntExp(32), OpExp::sub, counted_exp)));
program_->text_section ().
add_inst(new Or(*dest, *dest, Register(Register::general, Cpu::at)));
}
......@@ -150,11 +152,14 @@ namespace inst
void
ProgramBuilder::add_ror(Register *dest, Register *src1, Exp *src2)
{
CountedExp counted_exp(src2);
program_->text_section ().
add_inst(new Srl(Register(Register::general, Cpu::at), *src1, src2));
add_inst(new Srl(Register(Register::general, Cpu::at), *src1,
counted_exp));
program_->text_section ().
add_inst(new Sll(*dest, *src1,
new OpExp(new IntExp(32), OpExp::sub, src2)));
new OpExp(new IntExp(32), OpExp::sub, counted_exp)));
program_->text_section ().
add_inst(new Or(*dest, *dest, Register(Register::general, Cpu::at)));
}
......
......@@ -5,4 +5,5 @@ noinst_HEADERS = \
deref.hh \
escape.hh \
has.hh \
counted_ptr.hh \
select_const.hh
//
// This file is part of Mipsy, a tiny MIPS simulator
// Copyright (C) 2004 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
//
/** \file misc/counted_ptr.hh
\brief Counted reference wrapper. */
#ifndef MISC_COUNTED_PTR_HH
# define MISC_COUNTED_PTR_HH
# include "misc/contract.hh"
namespace misc
{
template < typename T >
/** \brief A reference counted smart pointer which provides a limited form
of garbage collection.
This class implements the proposal of Gregory Colvin in Exception
Safe Smart Pointers, C++ committee document 94-168/N0555, July, 1994.
The concept of counted pointers is very similar to the concept of
file links in file systems, that is why I choose to use the
well-known file manipulation command names as method identifiers.
*/
class counted_ptr
{
public:
/// The pointed-to type.
typedef T element_type;
private:
counted_ptr(element_type *ptr, long *count):
ptr_(ptr), count_(count)
{
++(*count_);
}
template < typename U >
friend class counted_ptr;
/** \name Constructor and destructor
\{ */
public:
/** \brief A counted_ptr is usually constructed from a raw pointer.
\param ptr A pointer (defaults to NULL). */
counted_ptr(element_type *ptr = 0):
ptr_(ptr)
{
count_ = new long(1);
}
/** \brief A counted_ptr can be constructed from another counted_ptr.
\param c Another %auto_ptr of the same type. */
counted_ptr(const counted_ptr &c):
ptr_(c.ptr_), count_(c.count_)
{
++(*count_);
}
/** \brief Release a link to ptr.
**
** When a counted_ptr is deleted, its counter is decremented; if
** it reaches zero, the pointed object is deleted. */
~counted_ptr()
{
unlink();
}
/** \} */
/** \name Conversion, assignement, comparison
\{ */
public:
/** \brief counted_ptr conversion operator.
This conversion operator avoids having to define several
constructors and test operators for creation from or comparison with
counted pointers of a different but related type.
*/
template < typename U >
operator counted_ptr< U >() const
{
return counted_ptr< U >(ptr_, count_);
}
/** \brief counted_ptr assignment operator.
\param c Another counted_ptr of the same type. */
counted_ptr&
operator=(const counted_ptr &c)
{
link(c.ptr_, c.count_);
return *this;
}
/** \brief Return true if \a c and this share the same data. */
bool
operator==(const counted_ptr &c)
{
return this->count_ == c.count_;
}
/** \brief Return true if \a c and this do not share the same data. */
bool
operator!=(const counted_ptr &c)
{
return this->count_ != c.count_;
}
/** \} */
private:
void unlink()
{
--(*count_);
if (!*count_)
{
delete ptr_;
delete count_;
}
}
void link(T *ptr, long *count)
{
if (count_ != count)
{
++(*count);
unlink();
ptr_ = ptr;
count_ = count;
}
}
/** \name Dereferencing
\{ */
public:
bool
exists() const
{
return ptr_ != 0;
}
operator bool() const
{
return ptr_ != 0;
}
element_type&
operator*() const
{
precondition(ptr_ != 0);
return *ptr_;
}
element_type*
operator->() const
{
precondition(ptr_ != 0);
return ptr_;
}
/** \} */
private:
/// Contained pointer
T *ptr_;
/// Reference counter
long *count_;
};
} // namespace misc
#endif // !MISC_COUNTED_PTR_HH
Supports Markdown
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