kekyo / IL2C

IL2C - A translator for ECMA-335 CIL/MSIL to C language.
Apache License 2.0
401 stars 36 forks source link

cc65 / c89 / c90 compiler support suggestions. #49

Open zezba9000 opened 6 years ago

zezba9000 commented 6 years ago

To support c89 with try catch you could base your output off "setjmp" && "longjmp".

#include <stdio.h>
#include <setjmp.h>

jmp_buf __threadExceptionBuff;

#define TRY switch(setjmp(__threadExceptionBuff)) { case 0: while(1) {
#define CATCH(x) break; case x:
#define FINALLY break; } default: {
#define TRY_END break; } }
#define THROW(x) longjmp(__threadExceptionBuff, x)

#define EXCEPTION (1)
#define NOT_IMPLEMENTED_EXCEPTION (2)

void Foo()
{
    THROW(NOT_IMPLEMENTED_EXCEPTION);
}

void main()
{
    TRY
    {
        printf("Start\n");
        Foo();
        printf("End\n");
    }
    CATCH (NOT_IMPLEMENTED_EXCEPTION)
    {
        printf("Catch Not Implemented Exception\n");
    }
    CATCH (EXCEPTION)
    {
        printf("Catch Exception\n");
    }
    FINALLY
    {
        printf("Finally\n");
    }

To support c89 local variable scoping rules. You could define all of local variables that live on the stack at the top of each block. Example:

struct A
{
    int x, y;
};

void Foo()
{
    // all local stack objects within this scope get pre-defined up here at the top of the block
    A a;
    A a2;
    int i;

    // Now you can use them normally and this will compile in c89....
    for (i = 0; i != 1; ++i)
    {
        // all local stack objects within this scope get pre-defined up here at the top of the block
        A b;
        float t;
        // do stuff....
    }
}

I use the c65 compiler to test out portability: https://github.com/cc65/cc65

kekyo commented 6 years ago

Ouf, I don't notice this issue. Our discussion is here


Ya, it try block methodology by Dr. Nidito? I know it and the IL2C's based on it. (It's the sample) The .NET CLS has a lot of typed exceptions. We have to identicate the CLS types at runtime. So all CLS types have the runtime-information table. We can choice the runtime type at the exception handler by the table pointer what caught the exception type.

kekyo commented 6 years ago

Thanks @zezba9000 suggests for me to what problem for C89 scope block, I reopen this issue and save for memorize for me ;)

zezba9000 commented 6 years ago

Ok cool. This is one of the best C# / .NET projects I've seen!

One other suggestion is you might consider adding "tags" to your GitHub project page. I've searched for IL2C on GitHub and Google many times but never found this for some reason.

kekyo commented 6 years ago

Are you explaining it about the "GitHub topics?" I didn't know it! I'll set it.

zezba9000 commented 6 years ago

Yes the "GitHub topics". Makes it easier for others to find your project :)