Shop OBEX P1 Docs P2 Docs Learn Events
Cog memory model, printf of a static string versus a buffer. — Parallax Forums

Cog memory model, printf of a static string versus a buffer.

Martin_HMartin_H Posts: 4,051
edited 2012-11-16 09:08 in Propeller 1
The program below compiles just fine in cog memory model, but if you uncomment the BREAK_IT the program gets all kinds of undefined symbols. However, to my eye printf of a static string or a char buffer should be the same. I don't get it.
/**
 * @file HelloWorld.c
 * This is the main HelloWorld program start point.
 */

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

// Uncomment this to break the cog memory model build.
//#define BREAK_IT 1

/**
 * Cog memory model hellow world program
 */
int main(void)
{
    int x;
    char buff[16];

    while(true)
    {
        // copy hellow world to the buffer.
        strcpy(buff, "Hello World");

#ifndef BREAK_IT
        // print a static string
        printf("Hello World\n");
#else
        // print a buffer
        printf(buff);
#endif
    }
    return 0;
}

Comments

  • ersmithersmith Posts: 6,054
    edited 2012-11-16 08:50
    Both of them should work, but they will produce quite different code. GCC knows that it can optimize printf("Hello World\n") into puts("Hello World"), whereas a printf with a variable buffer cannot be optimized.

    The only undefined symbol I see (when compiling either with or without BREAK_IT) is "true"; if I change while(true) to while(1) both versions seem to compile OK, although the static string one is smaller (as expected).

    Eric
  • Martin_HMartin_H Posts: 4,051
    edited 2012-11-16 09:02
    Are you compiling in cog memory model? I replaced the while (true) with while(1), although that wasn't causing me a problem. But the undefined references remain. I also noticed this error:

    c:/users/a231861/propgcc/bin/../lib/gcc/propeller-elf/4.6.1/../../../../propeller-elf/bin/ld.exe: region `cog' overflowed by 22716 bytes

    Note that if I switch from COG to LMM the error goes away. So I think this is an issue with cog memory model. I'm just surprised such a trivial change pulled in so much code.
  • Martin_HMartin_H Posts: 4,051
    edited 2012-11-16 09:08
    OK I figured out what was happening. I switched the compiler from C++ to C and then it compiled in COG mode. I think that change caused the whole C++ RTL to get pulled into the code which obviously won't work.
Sign In or Register to comment.