I have a problem with error propagation...
https://svn.lrde.epita.fr/svn/nolimips/trunk
ChangeLog | 19 +++++++++++++
src/inst/Makefile.am | 2 -
src/inst/data_section.cc | 4 +-
src/inst/data_section.hh | 2 -
src/inst/program_builder.hh.gen.py | 14 ++++++++-
src/inst/section.cc | 44 +++++++++++++++++++++++++++++++
src/inst/section.hh | 12 +++-----
src/inst/text_section.cc | 4 +-
src/inst/text_section.hh | 2 -
tests/solve/Makefile.am | 4 ++
tests/solve/data-label-already-defined.s | 12 ++++++++
tests/solve/text-label-already-defined.s | 7 ++++
12 files changed, 109 insertions(+), 17 deletions(-)
Index: ChangeLog
from Benoît Perrot <benoit(a)lrde.epita.fr>
Detect several definitions of the same label in a section.
* src/inst/section.hh (register_label):
Return false when label is already known in the section, true
otherwise. Move implementation to...
* src/inst/section.cc, src/inst/Makefile.am:
This new file.
* src/inst/data_section.hh, src/inst/data_section.cc,
* src/inst/text_section.hh, src/inst/text_section.cc (define_label):
Propagate status of register_label.
* src/inst/program_builder.hh.gen.py:
Generate an error when a label is defined more than once.
* tests/solve/data-label-already-defined.s,
* tests/solve/text-label-already-defined.s,
* tests/solve/Makefile.am:
Test it.
2005-09-03 Benoît Perrot <benoit(a)lrde.epita.fr>
Index: src/inst/data_section.cc
--- src/inst/data_section.cc (revision 190)
+++ src/inst/data_section.cc (revision 191)
@@ -30,13 +30,13 @@
// --------------------------------------------------------------------------
- void
+ bool
DataSection::define_label(const misc::unique_string &id)
{
Label *label = new inst::Label(id, size());
labels_.push_back(label);
- Section::register_label(label);
+ return Section::register_label(label);
}
// --------------------------------------------------------------------------
Index: src/inst/section.cc
--- src/inst/section.cc (revision 0)
+++ src/inst/section.cc (revision 191)
@@ -0,0 +1,44 @@
+//
+// This file is part of Nolimips, a MIPS simulator with unlimited registers
+// Copyright (C) 2005 Benoit Perrot <benoit(a)lrde.epita.fr>
+//
+// Nolimips 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.
+//
+// Nolimips 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
+//
+#include "inst/section.hh"
+
+namespace inst
+{
+
+ Section::~Section()
+ {
+// for (label_set_type::iterator
+// it = sorted_labels_.begin(); it != sorted_labels_.end(); ++it)
+// delete *it;
+ }
+
+ bool
+ Section::register_label(Label *label)
+ {
+ label_set_type::const_iterator it(sorted_labels_.find(label));
+ if (it == sorted_labels_.end()
+ || *(*it) < *label)
+ {
+ it = sorted_labels_.insert(it, label);
+ return true;
+ }
+ return false;
+ }
+
+} // namespace inst
Index: src/inst/data_section.hh
--- src/inst/data_section.hh (revision 190)
+++ src/inst/data_section.hh (revision 191)
@@ -49,7 +49,7 @@
}
public:
- void define_label(const misc::unique_string &id);
+ bool define_label(const misc::unique_string &id);
public:
void add_space(unsigned space_size)
Index: src/inst/text_section.cc
--- src/inst/text_section.cc (revision 190)
+++ src/inst/text_section.cc (revision 191)
@@ -36,14 +36,14 @@
// --------------------------------------------------------------------------
- void
+ bool
TextSection::define_label(const misc::unique_string &id)
{
TextLabel *label = new TextLabel(id, sentry_);
label->set_offset((insts_.size() - 1) * 4);
labels_.push_back(label);
- Section::register_label(label);
+ return Section::register_label(label);
}
// --------------------------------------------------------------------------
Index: src/inst/section.hh
--- src/inst/section.hh (revision 190)
+++ src/inst/section.hh (revision 191)
@@ -40,15 +40,13 @@
Section(const std::string &name):
name_(name)
{}
- virtual ~Section()
- {}
+ virtual ~Section();
protected:
- void register_label(Label *label)
- {
- // FIXME: check that this label has not already been added.
- sorted_labels_.insert(label);
- }
+ /** Return false if the label already exists in this section, true
+ otherwise */
+ bool register_label(Label *label);
+
public:
bool has_label(const misc::unique_string &label) const
{
Index: src/inst/text_section.hh
--- src/inst/text_section.hh (revision 190)
+++ src/inst/text_section.hh (revision 191)
@@ -60,7 +60,7 @@
}
public:
- void define_label(const misc::unique_string &id);
+ bool define_label(const misc::unique_string &id);
public:
void add_inst(inst::Inst* inst)
Index: src/inst/Makefile.am
--- src/inst/Makefile.am (revision 190)
+++ src/inst/Makefile.am (revision 191)
@@ -58,7 +58,7 @@
program.hh \
label.hh label.cc \
text_label.hh \
- section.hh \
+ section.hh section.cc \
text_section.hh text_section.cc \
data_section.hh data_section.cc \
program.hh \
Index: src/inst/program_builder.hh.gen.py
--- src/inst/program_builder.hh.gen.py (revision 190)
+++ src/inst/program_builder.hh.gen.py (revision 191)
@@ -87,12 +87,22 @@
public:
void define_inst_label(const misc::unique_string &id)
{
- program_->text_section ().define_label(id);
+ if (!program_->text_section ().define_label(id))
+ {
+ std::cerr << \"Instruction label already defined in this
section.\"
+ << std::endl;
+ exit_set(exit_solve);
+ }
}
void define_data_label(const misc::unique_string &id)
{
- program_->data_section ().define_label(id);
+ if (!program_->data_section ().define_label(id))
+ {
+ std::cerr << \"Data label already defined in this section.\"
+ << std::endl;
+ exit_set(exit_solve);
+ }
}
public:
Index: tests/solve/data-label-already-defined.s
--- tests/solve/data-label-already-defined.s (revision 0)
+++ tests/solve/data-label-already-defined.s (revision 191)
@@ -0,0 +1,12 @@
+ .data
+foo:
+ .asciiz "bar"
+foo:
+ .asciiz "baz"
+
+
+ .text
+main:
+ li $a0, 0
+ li $v0, 10
+ syscall
Index: tests/solve/Makefile.am
--- tests/solve/Makefile.am (revision 190)
+++ tests/solve/Makefile.am (revision 191)
@@ -1,7 +1,9 @@
### Sources
ASM_FILES = \
undefined-labels.s \
- unlimited-regs.s
+ unlimited-regs.s \
+ data-label-already-defined.s \
+ text-label-already-defined.s
dist_noinst_DATA = $(ASM_FILES)
Index: tests/solve/text-label-already-defined.s
--- tests/solve/text-label-already-defined.s (revision 0)
+++ tests/solve/text-label-already-defined.s (revision 191)
@@ -0,0 +1,7 @@
+ .text
+main:
+ li $a0, 0
+foo:
+ li $v0, 10
+foo:
+ syscall