gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gm2] Calling Modula-2 from C


From: Iztok Kobal
Subject: Re: [Gm2] Calling Modula-2 from C
Date: Mon, 01 Jun 2009 08:57:52 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.8.1.21) Gecko/20090410 SUSE/1.1.16-1.2 SeaMonkey/1.1.16

Ihave been using a simple trick to resolve also dependencies in more
simple way.

I have organized a library module calling all modules. Its init body
certainly calls all init bodies of all linked modules in appropriate
order. So:

At start I had pool of M2 modules:

DEFINITION/IMPLEMENTATION  MODULE A
DEFINITION/IMPLEMENTATION  MODULE B
...
DEFINITION/IMPLEMENTATION  MODULE N

Here comes the master library module - we need it to have resolved
dependency modules and proper init sequence. It should also be arranged
as DEF/IMP module to make init body exported:

DEFINITION MODULE MyM2Lib;
END MyM2Lib;

IMPLEMENTATION MODULE MyM2Lib;
IMPORT A;
IMPORT B;
...
IMPORT N;
BEGIN
END MyM2Lib;


And here comes main module calling M2M2Lib's init body to have it public
in symbol list since linker would not exclude it as unused:

IMPLEMENTATION MODULE MyM2LibMain;
IMPORT MyM2Lib;
BEGIN END MyM2LibMain.


Now, from C, the procedure is:

main() {
  MyM2Lib_init(); /* call M2 lib's init sequence, different for each M2
compiler, for GM2 should look something like _M2_MyM2Lib_init(...)  */

  /* now we can use functions from lib - also the symbol generated
depends on M2 compiler */
  A_foo(...);
  etc.
}


Described method worked for XDSM2, GPM2 and StonyBrookM2 (which BTW was
the only that supported libraries natively) so I suppose it should be
good for GM2 too. If I had to support different platforms using
different M2 compiler, the function symbols generated by these compilers
were obviously different so I ended up with bunch of define clauses
separated by conditional compiling variables. Pretty messy, though
successfuly working.

Regards, Iztok




Gaius Mulley wrote:
> Ron Kneusel <address@hidden> writes:
>
>   
>>> at present it is probably easier to make Modula-2 call C. Thus
>>> allowing Modula-2 to create the main function. So you might write the
>>> C module as:
>>>       
>> Thanks for the reply.  Unfortunately, that won't work in my case.  I am 
>> looking
>> to use Modula-2 to write extensions for an existing C application and
>> ultimately I need to create a shared object.  I have been playing with XDS
>> Modula-2 as well and have succeeded in going a bit further.  I can compile 
>> and
>> link without trouble but get a segmentation fault when I try to call
>> WriteString().
>>     
>
> Hi,
>
> ok, you can generate shared libraries using gm2.  There are some
> examples in:
>
> gcc-4.1.2/gcc/gm2/examples/swig/full-strlib/
>
> which compile StrLib as a shared library, then links it to produce
> _StrLib.so and proceeds to make python call StrLen.  There are also
> some screencasts available here:
>
> http://floppsie.comp.glam.ac.uk/download/screencasts/gnu-modula-2/python/python-numberio.mp4
>
> which show it in action.
>
> If you want to make your C application call Modula-2 it is possible -
> but you have to manually resolve the module dependencies and call the
>
>    _M2_modulename1_init(argc, argv);
>    _M2_modulename2_init(argc, argv);
>
>    it is now safe to call modulename2_myfunc()  etc
>
>    _M2_modulename2_finish(argc, argv);
>    _M2_modulename1_finish(argc, argv);
>
> in a sensible sequence.  I'd suggest manually using gm2l to generate
> the dependencies sequence (it does this based on imports) and then
> adjust the order by hand and then using that to build the above
> initialisation.  You can also use gm2lorder to fix the critical
> runtime order of SYSTEM, M2RTS and EXCEPTIONS.  Finally you could also
> use gm2lgen to generate the above init/finish code and maybe adjust it
> to fit your application..
>
> regards,
> Gaius
>
>
>
>
> _______________________________________________
> gm2 mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/gm2
>
>   





reply via email to

[Prev in Thread] Current Thread [Next in Thread]