Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #29007 > unrolled thread

Exceptions handling in pure C

Started by"Borneq" <borneq@antyspam.hidden.pl>
First post2012-12-07 07:50 +0100
Last post2012-12-08 10:34 -0800
Articles 9 on this page of 29 — 13 participants

Back to article view | Back to comp.lang.c


Contents

  Exceptions handling in pure C "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-07 07:50 +0100
    Re: Exceptions handling in pure C Ian Collins <ian-news@hotmail.com> - 2012-12-07 20:42 +1300
      Re: Exceptions handling in pure C "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-07 09:23 +0100
        Re: Exceptions handling in pure C Ian Collins <ian-news@hotmail.com> - 2012-12-07 21:41 +1300
          Re: Exceptions handling in pure C BGB <cr88192@hotmail.com> - 2012-12-07 03:10 -0600
            Re: Exceptions handling in pure C Ian Collins <ian-news@hotmail.com> - 2012-12-08 07:44 +1300
              Re: Exceptions handling in pure C BGB <cr88192@hotmail.com> - 2012-12-07 17:26 -0600
          Re: Exceptions handling in pure C "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-08 11:08 +0100
            Re: Exceptions handling in pure C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 19:02 +0000
              Re: Exceptions handling in pure C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-12-08 19:22 +0000
    Re: Exceptions handling in pure C James Kuyper <jameskuyper@verizon.net> - 2012-12-07 08:06 -0500
      Re: Exceptions handling in pure C "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-07 16:40 +0100
        Re: Exceptions handling in pure C James Kuyper <jameskuyper@verizon.net> - 2012-12-07 11:24 -0500
          Re: Exceptions handling in C + assembler "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-07 18:02 +0100
            Re: Exceptions handling in C + assembler James Kuyper <jameskuyper@verizon.net> - 2012-12-07 12:25 -0500
            Re: Exceptions handling in C + assembler "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-12-07 09:29 -0800
              Re: Exceptions handling in C + assembler "BartC" <bc@freeuk.com> - 2012-12-08 12:46 +0000
                Re: Exceptions handling in C + assembler James Kuyper <jameskuyper@verizon.net> - 2012-12-08 09:35 -0500
                Re: Exceptions handling in C + assembler Robert Wessel <robertwessel2@yahoo.com> - 2012-12-08 12:08 -0600
                  Re: Exceptions handling in C + assembler BGB <cr88192@hotmail.com> - 2012-12-08 15:19 -0600
                Re: Exceptions handling in C + assembler Jonathan Leffler <jleffler@earthlink.net> - 2012-12-16 22:37 -0800
        Re: Exceptions handling in pure C Rosario1903 <Rosario@invalid.invalid> - 2012-12-07 21:24 +0100
    Re: Exceptions handling in pure C Keith Thompson <kst-u@mib.org> - 2012-12-07 11:07 -0800
      Re: Exceptions handling in C + assembler "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-07 20:35 +0100
        Re: Exceptions handling in C + assembler James Kuyper <jameskuyper@verizon.net> - 2012-12-07 15:15 -0500
          Re: Exceptions handling in C + assembler "Borneq" <borneq@antyspam.hidden.pl> - 2012-12-08 16:49 +0100
            Re: Exceptions handling in C + assembler James Kuyper <jameskuyper@verizon.net> - 2012-12-08 11:04 -0500
            Re: Exceptions handling in C + assembler Thomas Richter <thor@math.tu-berlin.de> - 2012-12-09 15:05 +0100
    Re: Exceptions handling in pure C Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-08 10:34 -0800

Page 2 of 2 — ← Prev page 1 [2]


#29359 — Re: Exceptions handling in C + assembler

FromJonathan Leffler <jleffler@earthlink.net>
Date2012-12-16 22:37 -0800
SubjectRe: Exceptions handling in C + assembler
Message-ID<50CEBDB6.9020904@earthlink.net>
In reply to#29059
On 12/8/12 4:46 AM, BartC wrote:
> Yet C++ was originally implemented in C?

Exceptions were one of the main reasons why C++ stopped being
implemented as a front-end to C.  IIRC, 'D&E' by Stroustrup mentions this.

-- 
Jonathan Leffler                   #include <disclaimer.h>
Email: jleffler@earthlink.net, jleffler@us.ibm.com
Guardian of DBD::Informix v2011.0612 -- http://dbi.perl.org/

[toc] | [prev] | [next] | [standalone]


#29043

FromRosario1903 <Rosario@invalid.invalid>
Date2012-12-07 21:24 +0100
Message-ID<c3k4c8t6dd5p69n9tr1atusm9tbu0mkg2v@4ax.com>
In reply to#29021
On Fri, 7 Dec 2012 16:40:28 +0100, "Borneq"
<borneq@antyspam.hidden.pl> wrote:

>U?ytkownik "James Kuyper" <jameskuyper@verizon.net> napisa? w wiadomo?ci 
>news:k9spjv$8cb$1@dont-email.me...
>> On the other hand: if your entire program never actually handles
>> exceptions, then you may be able to adequately emulate exceptions simply
>> by replacing throw() with exit(). That's the case for the C++ code I'm
>> working on right now.
>
>Is http://www.microsoft.com/msj/0197/Exception/Exception.aspx with code:
>int main()
>{
>    DWORD handler = (DWORD)_except_handler;
>
>    __asm
>    {                           // Build EXCEPTION_REGISTRATION record:
>        push    handler         // Address of handler function
>        push    FS:[0]          // Address of previous handler
>        mov     FS:[0],ESP      // Install new EXECEPTION_REGISTRATION

so here FS:[0] point to a region of mem that has in its first 4bytes
the address of the *previous* handler; right?

>    }
>
>    __asm
>    {
>        mov     eax,0           // Zero out EAX
>        mov     [eax], 1        // Write to EAX to deliberately cause a 
>fault
>    }
>
>    printf( "After writing!\n" );
>
>    __asm
>    {                           // Remove our EXECEPTION_REGISTRATION record
>        mov     eax,[ESP]       // Get pointer to previous record
>        mov     FS:[0], EAX     // Install previous record
>        add     esp, 8          // Clean our EXECEPTION_REGISTRATION off 
>stack
>    }
>
>    return 0;
>}
>

what is one handler?
if it is one function, what arg it has? what it return?

this here crash...
__________
;nasmw -fobj  that.asm
section _DATA use32 public class=DATA

global _fini
global _fend

dat dd 0, 0, 0, 0

section _TEXT use32 public class=CODE

; 0ra, 4P_address_hadler_function
_fini:    
          mov     edx,  dword[esp+  4]
          cmp     edx,  0
          jne     .1
.e:       mov     eax,  -1
          stc
          jmp     short  .z
.1:       mov     eax,  [fs:0]
          cmp     eax,  0
          je      .e
          mov     [dat],  eax   ; store address of previos hf
          mov     [dat+4],  edx ; store address of         hf
          mov     ecx,  dat
          mov     [fs:0],  ecx
          xor     eax,  eax
.z:       
          ret

_fend:    
          mov     eax,  [dat]
          cmp     eax,  0
          je      .z
          mov     [fs:0],  eax
.z:       
          ret
__________
/*bcc32 -v this.c  that.obj */
#include <stdio.h>

int fini(char*  a);
int fend(void);
int except_handler(void){return 1;}  

int main()
{unsigned  a,b;

 fini(except_handler);
 a=0; b=9; b=b/a; 
 printf( "After writing! %u\n", b);

 fend();

 return 0;

}

[toc] | [prev] | [next] | [standalone]


#29038

FromKeith Thompson <kst-u@mib.org>
Date2012-12-07 11:07 -0800
Message-ID<ln38zhptp2.fsf@nuthaus.mib.org>
In reply to#29007
"Borneq" <borneq@antyspam.hidden.pl> writes:
> How can translate C++ to C code with exceptions handling? Unfortunately 
> Stroustrup translator cfron has only source and not compiled, llvm command 
> to translate C++ => C not works. 

We can probably give you a better answer if you let us know *why*
you want to translate the C++ code to C.  How does leaving the
code in C++ and compiling it with a C++ compiler not satisfy your
requirements?

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
    Will write code for food.
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#29040 — Re: Exceptions handling in C + assembler

From"Borneq" <borneq@antyspam.hidden.pl>
Date2012-12-07 20:35 +0100
SubjectRe: Exceptions handling in C + assembler
Message-ID<k9tgde$q20$1@node1.news.atman.pl>
In reply to#29038
Uzytkownik "Keith Thompson" <kst-u@mib.org> napisal w wiadomosci 
news:ln38zhptp2.fsf@nuthaus.mib.org...
> We can probably give you a better answer if you let us know *why*
> you want to translate the C++ code to C.  How does leaving the
> code in C++ and compiling it with a C++ compiler not satisfy your
> requirements?

I wanna for training reasons write compiler (rightly translator) from my own 
object language to C+asm. It will be well if it support exceptions and 
multiple Inheritance. No garbage collector -  this language will be 
traditional.
I try install llvm under Ubuntu:
sudo apt-get install llvm
sudo apt-get install clang
sudo apt-get install llvm-gcc-4.6
% llvm-g++ -emit-llvm x.cpp -o program.bc -c
% llc -march=c program.bc -o x.c
is error: llc: program.bc:1:1: error: expected top-level entity
also "lli program.bc" is error 

[toc] | [prev] | [next] | [standalone]


#29041 — Re: Exceptions handling in C + assembler

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-12-07 15:15 -0500
SubjectRe: Exceptions handling in C + assembler
Message-ID<50C24E63.2090607@verizon.net>
In reply to#29040
On 12/07/2012 02:35 PM, Borneq wrote:
> Uzytkownik "Keith Thompson" <kst-u@mib.org> napisal w wiadomosci 
> news:ln38zhptp2.fsf@nuthaus.mib.org...
>> We can probably give you a better answer if you let us know *why*
>> you want to translate the C++ code to C.  How does leaving the
>> code in C++ and compiling it with a C++ compiler not satisfy your
>> requirements?
> 
> I wanna for training reasons write compiler (rightly translator) from my own 
> object language to C+asm.

How does that involve translation of C++ code to C?

> ... It will be well if it support exceptions and 
> multiple Inheritance.

Well, C has neither exceptions nor inheritance of any kind, much less
multiple inheritance. You might need to reconsider your plans. For
instance, why don't you target C++ + asm, rather than C + asm?

> sudo apt-get install llvm
> sudo apt-get install clang
> sudo apt-get install llvm-gcc-4.6
> % llvm-g++ -emit-llvm x.cpp -o program.bc -c

So, where did x.cpp come from, and what does it have to to with the
situation you described above?

> % llc -march=c program.bc -o x.c
> is error: llc: program.bc:1:1: error: expected top-level entity
> also "lli program.bc" is error 

If you want to know why you got that error message, a forum devoted to
llvm would be a better place to find out. You might need to explain the
contents of x.cpp in order to get a meaningful answer.

[toc] | [prev] | [next] | [standalone]


#29064 — Re: Exceptions handling in C + assembler

From"Borneq" <borneq@antyspam.hidden.pl>
Date2012-12-08 16:49 +0100
SubjectRe: Exceptions handling in C + assembler
Message-ID<k9vnhl$uhu$1@node1.news.atman.pl>
In reply to#29041
Uzytkownik "James Kuyper" <jameskuyper@verizon.net> napisal w wiadomosci 
news:50C24E63.2090607@verizon.net...
> How does that involve translation of C++ code to C?

> Well, C has neither exceptions nor inheritance of any kind, much less
> multiple inheritance. You might need to reconsider your plans. For
> instance, why don't you target C++ + asm, rather than C + asm?

I compile with http://llvm.org/demo/index.cgi
and it call:
__cxa_allocate_exception
__cxa_throw
__cxa_begin_catch
__cxa_end_catch
how it call in own program? 

[toc] | [prev] | [next] | [standalone]


#29065 — Re: Exceptions handling in C + assembler

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-12-08 11:04 -0500
SubjectRe: Exceptions handling in C + assembler
Message-ID<k9voe0$dej$1@dont-email.me>
In reply to#29064
On 12/08/2012 10:49 AM, Borneq wrote:
> Uzytkownik "James Kuyper" <jameskuyper@verizon.net> napisal w wiadomosci 
> news:50C24E63.2090607@verizon.net...
>> How does that involve translation of C++ code to C?
> 
>> Well, C has neither exceptions nor inheritance of any kind, much less
>> multiple inheritance. You might need to reconsider your plans. For
>> instance, why don't you target C++ + asm, rather than C + asm?
> 
> I compile with http://llvm.org/demo/index.cgi
> and it call:
> __cxa_allocate_exception
> __cxa_throw
> __cxa_begin_catch
> __cxa_end_catch
> how it call in own program? 

I've found documentation for those functions at
<http://libcxxabi.llvm.org/spec.html>, though I didn't dig into the
documentation in any depth. However, given the way that they're nbamed
and the fact that they're identified as being part of libc++, I would
make two guesses:

1) you might not need to call those functions explicitly when compiling
for C++, the calls might be generated automatically by the compiler in
connection with throw() and catch() constructs.

2) it might not be safe to call them from C.

However, those are just guesses -  only someone actually familiar with
llvm could address those issue authoritatively. So ask your question in
a forum devoted to llvm.
-- 
James Kuyper

[toc] | [prev] | [next] | [standalone]


#29096 — Re: Exceptions handling in C + assembler

FromThomas Richter <thor@math.tu-berlin.de>
Date2012-12-09 15:05 +0100
SubjectRe: Exceptions handling in C + assembler
Message-ID<ka25qe$431$1@news2.informatik.uni-stuttgart.de>
In reply to#29064
On 08.12.2012 16:49, Borneq wrote:

>> Well, C has neither exceptions nor inheritance of any kind, much less
>> multiple inheritance. You might need to reconsider your plans. For
>> instance, why don't you target C++ + asm, rather than C + asm?
>
> I compile with http://llvm.org/demo/index.cgi
> and it call:
> __cxa_allocate_exception
> __cxa_throw
> __cxa_begin_catch
> __cxa_end_catch
> how it call in own program?

You don't. You write C++ in first place. Look, the way *how* a C++ 
compiler implements exceptions is defined by the specific compiler 
implementation. G++ for example does not generate much code, instead it 
creates a table (within the text segment, i.e. same place where the code 
goes) which defines which objects to clean up. This table is then parsed 
by the exception handling mechanism.

What you try to do is not feasible - you can "simulate" *some* of the 
features of exceptions in C, but not all. The troublesome part is that 
you need to call the destructors of all automatic objects when 
propagating an exception out of a function.

If your C++ code is written in such a way that all automatic objects 
have trivial destructors, one can somehow emulate exception handling by 
setjmp/longjmp (been there, done that), but given that you have already 
an existing C++ code - no way.

There are free C++ compilers out there, for example the mentioned GNU 
g++. Just use it - problem solved.

Greetings,
	Thomas

[toc] | [prev] | [next] | [standalone]


#29068

FromMalcolm McLean <malcolm.mclean5@btinternet.com>
Date2012-12-08 10:34 -0800
Message-ID<5dcfb4ba-c6a1-49fe-8d7a-ebea4e35efb6@googlegroups.com>
In reply to#29007
On Friday, December 7, 2012 6:50:09 AM UTC, Borneq wrote:
> How can translate C++ to C code with exceptions handling? Unfortunately  
> Stroustrup translator cfron has only source and not compiled, llvm command 
> to translate C++ => C not works.
>
You mean hand translation?

Translate a throw to a goto error_exit.
In the error_exit portion of the code, free up everything you allocated 
in the function (so be sure not to have dangling uninitialised pointers).#
Then set a global "exception" flag.
After every call to a function which can throw, add If(expeption) goto error_exit;

To implement catch, check the exeception flag, then clear it. You'll have to
have jumps into catch.

It's not nice. But it will work without too much violence to your C code.

You have to be more sophisticated with automatic translation, because you 
have to handle every legal C++ construct.

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c


csiph-web