Shop OBEX P1 Docs P2 Docs Learn Events
Is there a possibility for "intelligent" linking? — Parallax Forums

Is there a possibility for "intelligent" linking?

Christof Eb.Christof Eb. Posts: 1,237
edited 2012-03-17 12:55 in Propeller 1
Hi,
in my first PropGcc- Projekt I want to include some floating point math. Therefore I include math.lib.
The linker seems to include very much or even the whole of the library. The code is now 12kBytes longer. :-(

Is there a possibility that the linker will link only what is really needed?

Thanks!
Christof

Comments

  • jazzedjazzed Posts: 11,803
    edited 2012-03-16 08:37
    Hi Christof.

    Linking is "intelligent." Please post your example.

    Propeller GCC floating point is emulated via the generic BSD library.

    The 64bit double library is used by default. Use 32bit doubles if you can.
    Any time you use printf, code size blossoms. The Simple printf alternative is smaller.

    If you need a full 64bit double solution, there are better choices.

    --Steve
  • denominatordenominator Posts: 242
    edited 2012-03-16 08:52
    Christof,

    The linker does try to link only what is really needed. If you include the -Xlinker -Map=a.map in the second blank box in the lower left corner of the IDE, it should produce a load map into a file named a.map. This map would show you exactly what files are loaded and exactly what routine first caused each file to be needed. More information about this option can be found in the wiki at http://code.google.com/p/propgcc/wiki/PropGccMaps.

    Unfortunately, while the SimpleIDE seems to pay attention to both of those "extra flags" boxes, it doesn't actually seem to use the them (the problem is the exact same as you ran into with the --save-temps flag). So, if you want the map you need to run the compiler yourself from a command line. To do this, bring up a command prompt (see for example http://www.computerhope.com/issues/chdos.htm), cd to your project directory, make sure your path includes the actual compiler (probably something like c:\propgcc\bin), copy the compile/link command the IDE seemed to use from the build window, paste this command into the command prompt window, make sure it includes the "-Xlinker -Map=a.map" flags, then run it.

    Otherwise, you'll just have to trust that PropGCC does the right thing and only links in the routines it needs. The floating point routines are very big, and probably are not terribly useful for LMM mode programs.
  • jazzedjazzed Posts: 11,803
    edited 2012-03-16 10:16
    Unfortunately, while the SimpleIDE seems to pay attention to both of those "extra flags" boxes, it doesn't actually seem to use the them (the problem is the exact same as you ran into with the --save-temps flag).


    The SimpleIDE project option boxes are only used to give compiler flags and linker options to the final build step which compiles and links all .c and generated .o files.
  • Christof Eb.Christof Eb. Posts: 1,237
    edited 2012-03-16 10:26
    Hm, thanks for the input.

    All variables are int. There is only one line with float within (simple) printf. Only multiply, divide and "%8f" is used.

    The two versions of this line are:
    ....
    //printf("%8d %8d Mass: %8f grams\n", actfreq, (minfreq-actfreq), 1.0*actfreq*actfreq/2736750);
    printf("%8d %8d Mass: %8d /10 grams\n", actfreq, (minfreq-actfreq), actfreq*actfreq/273675);
    ...

    It is interesting, that the second version, which can be compiled and runs without math.lib (9072bytes), still is very much larger(19400bytes), if you don't switch off math.lib and 32bit double!

    Christof


    By the way: "-save-temps" works, without "--".
  • denominatordenominator Posts: 242
    edited 2012-03-16 11:40
    jazzed wrote: »
    The SimpleIDE project option boxes are only used to give compiler flags and linker options to the final build step which compiles and links all .c and generated .o files.

    I still think there's a problem. Here's the repro steps:

    1) Create a new project in SIDE 0.5.0 on Windows using Project | New Project. I called my project "Hallo". (This should create a Hallo.c with an empty main(); this is enough for the test.)
    2) Change the first empty box in the lower-left "Compiler" area to hold "--save-temps -g"
    3) Change the second empty box in the lower-left "Compiler" area to hold "-Xlinker -Map=a.map"
    4) Choose Debug | Build, or hit <f9>
    5) Inspect your project directory (using File | Open, or directly browse to it using Windows Explorer)

    Expected to see: Hallo.s, Hallo.o, and a.map

    Found instead: none of these files

    If you look in the build window at the bottom right of SIDE, you'll notice the following compile/link line:
    propeller-elf-gcc.exe -o a.out -Os -mlmm -m32bit-doubles -fno-exceptions -Dprintf=__simple_printf --save-temps -g Hallo.c -Xlinker -Map=a.map
    

    Notice this line does include --save-temps -g and -Xlinker -Map=a.map, even though those options seemed to have no effect.

    Further evidence:

    A) Next, open a command window.
    B) Cd to your project directory.
    C) Make sure your path includes PropGcc's bin directory.
    D) Paste the exact same command as shown above in that window, and run it.

    Notice you now have the .s, .i, and.map files, as expected.
  • denominatordenominator Posts: 242
    edited 2012-03-16 11:41
    By the way: "-save-temps" works, without "--".

    When I use -save-temps instead of --save-temps, only the .i file appears, but I still don't see the .s file.
  • jazzedjazzed Posts: 11,803
    edited 2012-03-16 13:18
    Thanks for this detailed "repeat by" ... I'm looking at it.

    I still think there's a problem. Here's the repro steps:

    1) Create a new project in SIDE 0.5.0 on Windows using Project | New Project. I called my project "Hallo". (This should create a Hallo.c with an empty main(); this is enough for the test.)
    2) Change the first empty box in the lower-left "Compiler" area to hold "--save-temps -g"
    3) Change the second empty box in the lower-left "Compiler" area to hold "-Xlinker -Map=a.map"
    4) Choose Debug | Build, or hit <f9>
    5) Inspect your project directory (using File | Open, or directly browse to it using Windows Explorer)

    Expected to see: Hallo.s, Hallo.o, and a.map

    Found instead: none of these files

    If you look in the build window at the bottom right of SIDE, you'll notice the following compile/link line:
    propeller-elf-gcc.exe -o a.out -Os -mlmm -m32bit-doubles -fno-exceptions -Dprintf=__simple_printf --save-temps -g Hallo.c -Xlinker -Map=a.map
    

    Notice this line does include --save-temps -g and -Xlinker -Map=a.map, even though those options seemed to have no effect.

    Further evidence:

    A) Next, open a command window.
    B) Cd to your project directory.
    C) Make sure your path includes PropGcc's bin directory.
    D) Paste the exact same command as shown above in that window, and run it.

    Notice you now have the .s, .i, and.map files, as expected.
  • Christof Eb.Christof Eb. Posts: 1,237
    edited 2012-03-16 13:41
    Hi,

    try compiling this with and without mathlib:

    // TestMath_A.c

    #include <stdio.h>
    #include <propeller.h>
    #include <math.h>

    main()
    {

    printf("Hello \n");
    }

    10kByte for nothing?

    Regards Christof
  • denominatordenominator Posts: 242
    edited 2012-03-16 15:07
    try compiling this with and without mathlib:
    ...
    10kByte for nothing?

    Not really. The -lm library includes a different printf with support for floats/doubles (depending on the 32-bit doubles setting). So, the 10K is for floating point support in printf.
  • jazzedjazzed Posts: 11,803
    edited 2012-03-16 15:10
    Here's what I get.

    6,600 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -fno-exceptions add.c -s
    6,600 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -fno-exceptions add.c -lm -s
    6,600 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -m32bit-doubles -fno-exceptions add.c -lm -s
    7,328 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -fno-exceptions -Dprintf=__simple_printf add.c -s
    7,328 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -m32bit-doubles -fno-exceptions -Dprintf=__simple_printf add.c -s
    17,656 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -m32bit-doubles -fno-exceptions -Dprintf=__simple_printf add.c -lm -s
    22,000 propeller-elf-gcc.exe -o a.out -O0 -mlmm -Wall -fno-exceptions -Dprintf=__simple_printf add.c -lm -s
    Hi,

    try compiling this with and without mathlib:

    // TestMath_A.c

    #include <stdio.h>
    #include <propeller.h>
    #include <math.h>

    main()
    {

    printf("Hello \n");
    }

    10kByte for nothing?

    Regards Christof
  • denominatordenominator Posts: 242
    edited 2012-03-16 16:35
    jazzed wrote: »
    Here's what I get.

    Interesting side note: Some of these numbers go up considerably if you change the printf line to this:

    printf("Hello %d\n", 1);

    or to this:

    printf("Hello");

    The reason is the GCC optimizes the printf to a puts (even with -O0). And, it doesn't use puts unless the string ends in "\n", so for printf("Hello"), the numbers you get are: 14312 and 31128 for your first two test cases, the second number being larger because the library includes support for floating point.

    It's always helpful to look at the generated assembly and maps to tell exactly what is going on.
  • Christof Eb.Christof Eb. Posts: 1,237
    edited 2012-03-16 23:15
    It is always helpful to look at the output and maps. . .
    I totally agree - Steve do you want to add buttons in SimpleIde for an asm output with source included and for map output?

    Back to the original question. Do I understand it right, that the linker cannot be more intelligent, because printf must interpret the
    format string at execution time and there might be a float or other things? If this is the case, then there should exist some solution
    for a more basic printf or so.
    Christof
  • denominatordenominator Posts: 242
    edited 2012-03-17 06:06
    Do I understand it right, that the linker cannot be more intelligent, because printf must interpret the
    format string at execution time and there might be a float or other things?

    Yes, this is the case. If you use printf (and GCC doesn't convert your printf() to an equivalent puts()), then you link in everything that printf needs, even if you're not using those features of printf.
    If this is the case, then there should exist some solution for a more basic printf.

    This solution does exist: in the IDE, make sure "Simple printf" is checked - this roughly halves the size of printf.

    Or, you can use your define your own version of printf. Check out this thread for a version of printf that uses only 1.5K: http://forums.parallax.com/showthread.php?137928-PropGCC-SimpleIDE&p=1076114&highlight=printf#post1076114
  • jazzedjazzed Posts: 11,803
    edited 2012-03-17 07:34
    It is always helpful to look at the output and maps. . .
    I totally agree - Steve do you want to add buttons in SimpleIde for an asm output with source included and for map output?
    I'm inclined to do this for those who really want it (2 in 50). It's not "Simple" for most users, but it can be buried in a Tools menu.
    Making a status tab at the bottom would be easy. I guess a separate dialog popup window would be more convenient though.
    Back to the original question. Do I understand it right, that the linker cannot be more intelligent, because printf must interpret the
    format string at execution time and there might be a float or other things? If this is the case, then there should exist some solution
    for a more basic printf or so.
    Christof
    We will provide integer only "SPIN-like" character input/output tools in a separate library.
    We could make the integer only tinyprintf mentioned by denominator a library. Ted TODO?
  • denominatordenominator Posts: 242
    edited 2012-03-17 07:52
    jazzed wrote: »
    Ted TODO?

    Indeed, my plan is to have a separate library, -ltiny, that would include small printf, sprintf, fprintf, scanf, etc., along with the smaller memory allocators and anything else that comes along that needs a smaller size. Soon.
  • Christof Eb.Christof Eb. Posts: 1,237
    edited 2012-03-17 11:43
    Hi Steve,
    the question to add a button was just a proposal - my English is not so good. I am not sure about the right tone. This simpleIDE and its easy installation gives an easy entry to guys like me. That is great!
    When I work with PropBasic the I like to have a look into the asm-code, it is interesting, to learn, what the compiler did. So this is a little bit for education.

    Christof

    Hi denominator,
    I will have a look at tinyprintf.c thanks!
    Christof
  • jazzedjazzed Posts: 11,803
    edited 2012-03-17 12:30
    Your tone is fine :) I've just been reluctant. Having a button for educational purposes is worth it.
  • dnalordnalor Posts: 223
    edited 2012-03-17 12:55
    A mixed view (c-code, asm) is a must have, especially if the code does not what it should. Not only for educational purposes.
Sign In or Register to comment.