🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #782

Re: Help with exceptions please...

#782
From: Mike Stump
Date: Fri, 06 Oct 1995 20:19
125 lines
4751 bytes
> Date: Fri, 6 Oct 95 09:33:09 CDT
> Message-Id: <9510061433.AA23443@silver.cs.umanitoba.ca>
> From: Varadarajan <tsv@cs.UManitoba.CA>

> 	I am involved in modifying a compiler with C like syntax to
> handle exceptions. I would like to do a study of how it has been
> implemented in gcc before starting with it. Can you point out some
> piece of documentation for the same?

Yo can take a look at cp/g++int.texi under Exception Handling in the
gcc distribution for note notes on EH in g++.  Though that doesn't
cover many implementation details of the type you're interested in.

> If no documentation is available, if you could at least indicate me
> the files

cp/except.c, pretty hard to miss.

> in the gcc distribution that are involved in exception
> handling it will be ok. And I am not very sure if it is good
> netiquette to write personal mails like this,

It is reasonable from my perspective, given the highly technical
aspect.  Now, if you had asked a stupid question, like how do I
install it on my PC, or where can I get it, I would have flamed you!

> if it is a mistake to do it, please do inform me. I thought that
> since my qn might have only a limited audience, I would rather
> communicate to you directly rather than through gcc newsgroup.
> Thanks and regards, --

Though, I do read the g++ help group on a regular basis, and would
probably have answered it there as well.

The design is fairly simple and straight forward.  There are regions
of code denoted by a starting address and an ending address, that are
called exception regions.  When an exception is thrown inside the
region, control is transferred to the associated handler.  A try block
is mapped into one exception region.  To do stack unwinding, I now
mark the whole function with a region, with the handler for it that
patches the return address and then does a normal return.  This will
restore all the right registers, adjust the stack and frame pointers,
and the like.  Compilers temporaries get regions also, if needed.

Anyway, see the mapping in the gxxint.texi file.  Below is part of the
file that descibes the mapping.  It is maybe just a little out of date
now, but it should be fairly close.

A good way to understand how it works, is to look at the code the
compiler generates, and single step through it in gdb using si/ni:

define si
stepi
x/1i $pc
end
define ni
nexti
x/1i $pc
end

another comamnd of use is

x/10i $pc  # display the next 10 instructions at the pc.

-------------------- From gxxint.texi --------------------


The structure of the code that is generated for C++ exception handling
code is shown below:

@example
Ln:					throw value;
        copy value onto heap
        jump throw (Ln, id, address of copy of value on heap)

                                        try {
+Lstart:	the start of the main EH region
|...						...
+Lend:		the end of the main EH region
                                        } catch (T o) {
						...1
                                        }
Lresume:
        nop	used to make sure there is something before
                the next region ends, if there is one
...                                     ...

        jump Ldone
[
Lmainhandler:    handler for the region Lstart-Lend
	cleanup
] zero or more, depending upon automatic vars with dtors
+Lpartial:
|        jump Lover
+Lhere:
        rethrow (Lhere, same id, same obj);
Lterm:		handler for the region Lpartial-Lhere
        call terminate
Lover:
[
 [
        call throw_type_match
        if (eq) {
 ] these lines disappear when there is no catch condition
+Lsregion2:
|	...1
|	jump Lresume
|Lhandler:	handler for the region Lsregion2-Leregion2
|	rethrow (Lresume, same id, same obj);
+Leregion2
        }
] there are zero or more of these sections, depending upon how many
  catch clauses there are
----------------------------- expand_end_all_catch --------------------------
                here we have fallen off the end of all catch
                clauses, so we rethrow to outer
        rethrow (Lresume, same id, same obj);
----------------------------- expand_end_all_catch --------------------------
[
L1:     maybe throw routine
] depending upon if we have expanded it or not
Ldone:
        ret

start_all_catch emits labels: Lresume, 
Technical Details
Message-ID: <199510062019.NAA15777@cygnus.com>
Path: arc1-ca.pugleaf.net!cygnus.com!mrs

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!cygnus.com!mrs
From: mrs@cygnus.com (Mike Stump)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Re:  Help with exceptions please...
Date: 6 Oct 1995 16:19:32 -0400
Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu
Sender: daemon@cis.ohio-state.edu
Distribution: world
Message-ID: <199510062019.NAA15777@cygnus.com>