Q. How can I use shared libraries from m2f?
Q. How can I use libvga from m2f?
Q. How can I get better optimization when compiling withm2f?
Q. How do I enable pentium U/V slot optimization?
Q. How can I get even better optimization when compiling?

Q. How can I use shared libraries from m2f?

A. Shared libraries can be utilized from Modula-2. What you have to do is:

(i)

create a definition module for the library.

(ii)

include the library directory in the link path.

here is an example using libm:

DEFINITION MODULE libm ;

EXPORT UNQUALIFIED sin ;

PROCEDURE sin (x: LONGREAL) : LONGREAL ;

END libm.

the method shown above uses the UNQUALIFIED keyword in the definition module which tells m2f not to prefix the module name to identifiers when emiting the assembly language. Here is our program module:

MODULE testmath ;

FROM libm IMPORT sin ;
FROM MATH IMPORT pi ;
FROM FpuIO IMPORT WriteLongReal ;
FROM StrIO IMPORT WriteString, WriteLn ;


VAR
   x: LONGREAL ;
BEGIN
   x := sin(pi/6.0) ;
   WriteLongReal(x, 5, 10) ; WriteLn
END testmath.

Assuming that both files are in the current directory; libm.a exists in /usr/lib and the Modula-2 library files are held in /usr/share/m2f/libs then the compile command might look like this:

m2f -g testmath

and the link command:

m2f -g -M ". /usr/lib /usr/share/m2f/libs" -l testmath

Explanation: the file libm.a must be in the Modula-2 link path. and the M2 application is linked against /usr/lib/libm.a which contains a bunch of calls to the dynamic library libm.so.xyz. So when the application is executed it will request that the libm.so is loaded etc. You might have to manipulate your LD_LIBRARY_PATH env variable or /etc/ld.so.conf file to tell the OS where the dynamic libraries reside. To see what dynamic libraries your executables are dependant upon you can use ldd.

ldd -v a.out
ldd: version 1.9.11
     libc.so.6 => /lib/libc.so.6 (0x40019000)
     /lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x40000000)

Q. How can I use libvga from m2f?

A. Here is a copy of libvga.def

DEFINITION MODULE libvga ;

(*
    Title      : libvga
    Description: Provides a Modula-2 interface to the GNU/Linux vga C
                 user library. This definition module only provides basic
                 primitives.
*)

EXPORT UNQUALIFIED GraphicsMode,
                   vga_setmode, vga_hasmode,
                   vga_clear, vga_flip,
                   vga_getxdim, vga_getydim,
                   vga_screenon, vga_screenoff,
                   vga_getpixel, vga_drawpixel, vga_drawline, vga_setcolor,
                   vga_getch ;

TYPE
   GraphicsMode = (Text,
                   G320x200x16,
                   G640x200x16,
                   G640x350x16,
                   G640x480x16,
                   G320x200x256,
                   G320x240x256,
                   G320x400x256,
                   G360x480x256,
                   G640x480x2,
                   G640x480x256,
                   G800x600x256,
                   G1024x768x256,
                   G640x480x32K,
                   G640x480x64K,
                   G800x600x32K,
                   G800x600x64K,
                   G1024x768x32K,
                   G1024x768x64K,
                   G640x480x16M,
                   G800x600x16M,
                   G1024x768x16M,
                   G320x200x32K,
                   G320x200x64K,
                   G320x200x16M) ;


PROCEDURE vga_setmode (m: GraphicsMode) ;
PROCEDURE vga_hasmode (m: GraphicsMode) : BOOLEAN ;
PROCEDURE vga_clear ;
PROCEDURE vga_flip ;
PROCEDURE vga_getxdim () : CARDINAL ;
PROCEDURE vga_getydim () : CARDINAL ;
PROCEDURE vga_screenon ;
PROCEDURE vga_screenoff ;
PROCEDURE vga_getpixel (x, y: CARDINAL) : CARDINAL ;
PROCEDURE vga_drawpixel (x, y: CARDINAL) ;
PROCEDURE vga_drawline (x1, y1, x2, y2: CARDINAL) ;
PROCEDURE vga_setcolor (colour: CARDINAL) ;
PROCEDURE vga_getch () : CHAR ;

END libvga.

our test program follows:

MODULE gtest ;

IMPORT StdIO ;
FROM libvga IMPORT GraphicsMode,
                    vga_setmode, vga_clear, vga_screenon, vga_screenoff, vga_drawline,
                    vga_getxdim, vga_getydim, vga_getch ;


VAR
   ch: CHAR ;
BEGIN
   vga_setmode(G320x200x16) ;
   vga_screenoff ;
   vga_drawline(1, 1, vga_getxdim(), 1);
   vga_drawline(vga_getxdim(), 1, vga_getxdim(), vga_getydim());
   vga_screenon ;
   ch := vga_getch() ;
   vga_setmode(Text)
END gtest.

we compile gtest.mod via:

m2f -g gtest
m2f -o gtest -M "/usr/share/m2f/libs /usr/lib ." -g -l -lm gtest

note that you will need m2f-4.3 (which is currently not formally released, but if you are desperate you can check out the GNUModula2.html#GM2 development download via CVS).

Note that the module you are linking must be the final argument to m2f.


Q. How can I get better optimization when compiling withm2f?

A. Use the -O -Odynamic flag
If that is not good enough see below


Q. How do I enable pentium U/V slot optimization?

try: -O -Odynamic -Oi586 -O2 for pentium I
try: -O -Odynamic -Oi686 -O2 for pentium II
If this is still not good enough try increasing -O2 to -O3 to -O4 etc (though this will cause compile time to rise).

Note that the line number debugging information produced by -g might not be correct as instruction sequences are reordered.


Q. How can I get even better optimization when compiling?

A. it might be worth examining gm2 which uses the m2f front end and the gcc back end.