Shop OBEX P1 Docs P2 Docs Learn Events
Converting Propeller SPIN program to C — Parallax Forums

Converting Propeller SPIN program to C

Luis DigitalLuis Digital Posts: 371
edited 2011-12-08 08:09 in Propeller 1
Hi!,

Here my first impressions and discoveries with Propeller GCC:

http://forums.parallax.com/entry.php?393-Converting-Propeller-SPIN-program-to-C

Espa

Comments

  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-12-05 14:12
    Very good work!

    I would be interested in your thoughts on a single line of code that has multiple expressions and many brackets. Is the 'order of precedence' the same for Spin and C? And if there are differences, is there a way we can document these clearly and even have some test code to make sure that the lines are being translated correctly?
  • Luis DigitalLuis Digital Posts: 371
    edited 2011-12-05 14:19
    Yes, order of precedence seems the problem. I could not do more tests because I could not debug with "printf". And here is 6:25P.M, and I have to go out and exercise (running). :-)
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-12-05 14:51
    Luis,

    That's an interesting exercise (the demo, not the running :) ). I've often wondered where the bumps might be for automatic translation from Spin to C. It appears that precedence is one. Another is that pesky void pragma for methods that "don't return anything." All Spin methods return something, even if they don't use a result variable or the return statement. What they return is the last expression to be evaluated. A truly comprehensive translation program would have to ferret out the correct return value from the Spin code where it's not explicitly shown.

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2011-12-05 15:04
    Very nice!

    I see your comment here:
    A major change was to bracket the expression: "0b00110 <<26", otherwise the compiler adds "26" and the value of "Pin", this being a mistake and your program will not work.

    While C syntax is different from SPIN, using the -Wall flag when compiling your programs will warn you that there may be an issue. I copied your code to luistest.c and compiled it with a small change (reinserting your bug):
    propeller-elf-gcc.exe -o luistest.elf -Os -mlmm -Wall -Dprintf=__simple_printf luistest.c -s
    
    luistest.c: In function 'Start_PWM':
    luistest.c:6:3: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
    

    Without using -Wall, the compiler very happily accepts your code. -Wall is very useful.

    BTW: using -Dprintf=__simple_printf lets you use a small (non-ansi) printf for debugging programs. Yours is only 3K of course.
  • Heater.Heater. Posts: 21,230
    edited 2011-12-05 19:55
    What's the problem with "void"? All Spin methods return an int and that's what the C translations should be declared as. Problem might be to find out what to put in that int.
    Looks like you have to do some parsing and compiling rather than just textual substitution to get this all right.
  • Luis DigitalLuis Digital Posts: 371
    edited 2011-12-07 13:48
    jazzed,

    Thanks for your suggestions, I always use an IDE (usually eclipse) and so forget the important parameter "-Wall".

    About "printf" I should clarify the problem:
    Not printing all the characters, and in some cases none. If I write "Hello World!" Only prints, for example "Hello Wo"
    I tried putting a pause after "printf", but with the same results.

    It is a general problem or exclusive of mine?

    Thank you.
  • Luis DigitalLuis Digital Posts: 371
    edited 2011-12-07 13:53
    Heater. wrote: »
    What's the problem with "void"? All Spin methods return an int...

    But it's not Spin, is C, and is not required. And finally the code will be converted to PASM.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-12-07 13:53
    heater wrote:
    What's the problem with "void"? All Spin methods return an int and that's what the C translations should be declared as.
    What happens in C if you call a non-void procedure in a void context? If it causes an error, it may be inconvenient to treat every method call as the element of an expression. (My ignorance of things C is what prompts this question.)

    -Phil
  • jazzedjazzed Posts: 11,803
    edited 2011-12-07 14:07
    jazzed,

    Thanks for your suggestions, I always use an IDE (usually eclipse) and so forget the important parameter "-Wall".

    About "printf" I should clarify the problem:
    Not printing all the characters, and in some cases none. If I write "Hello World!" Only prints, for example "Hello Wo"
    I tried putting a pause after "printf", but with the same results.

    It is a general problem or exclusive of mine?

    Thank you.

    Luis, you should either terminate the printf string with \n or use fflush(stdout).
    It is odd to need this though. Think we'll have a closer look.

    Examples:
    #include <stdio.h>
    int main(void)
    {
        printf("Hello World\n");
        return 0;
    }
    
    #include <stdio.h>
    int main(void)
    {
        printf("Hello World");
        fflush(stdout);
        return 0;
    }
    
  • TorTor Posts: 2,010
    edited 2011-12-08 01:48
    About "printf" I should clarify the problem:
    Not printing all the characters, and in some cases none. If I write "Hello World!" Only prints, for example "Hello Wo"I tried putting a pause after "printf", but with the same results.

    It is a general problem or exclusive of mine?
    As Jazzed said, add "\n" after "Hello World!".
    In C, data going to "standard out" (stdout) is buffered. stdout is what you write to if you just use 'printf' (or also 'fprintf (stdout, "..'). The reason for that is that it would be inefficient to output every character as you write them. Think of the origin of C: It was designed to work on systems where every character to the TTY terminal would generate an interrupt. So it's much better to buffer up data, and then write one line in one go (with a single interrupt). The \n (newline) character triggers the output.

    Even with today's computers you would, if you looked closely, find it more efficient to do line-buffering. (Note that if the program is not running interactively then the buffering won't even be handled on a newline basis, it could be buffering, say, 2048 characters at the time, and only output text when the buffer is full, or when the stdout file descriptor is closed, which happens automatically when the program terminates, btw.)

    So, in C, if you want output, add a newline character. Or use 'fflush(stdout)' explicitly (somewhat clumsy). Or use an ioctl to turn off stdout buffering. Or write to 'stderr' instead (standard error), which isn't buffered:
    fprintf (stderr, "will print"); will actually output the text right away, on most systems (but with no newline).

    -Tor
  • Luis DigitalLuis Digital Posts: 371
    edited 2011-12-08 05:50
    "stdout file descriptor is closed, which happens automatically when the program terminates"

    I see why all my tests on my PC work. Thank you for your comprehensive and complete explanation.
  • Heater.Heater. Posts: 21,230
    edited 2011-12-08 08:09
    PhiPi,
    What happens in C if you call a non-void procedure in a void context?

    In C++ at least there is no error or even a warning last time I tried it. The return value gets silently discarded.
    Converting Spin objects to C++ classes sounds like an excellent idea.
Sign In or Register to comment.