Converting Propeller SPIN program to C
Luis Digital
Posts: 371
Hi!,
Here my first impressions and discoveries with Propeller GCC:
http://forums.parallax.com/entry.php?393-Converting-Propeller-SPIN-program-to-C
Espa
Here my first impressions and discoveries with Propeller GCC:
http://forums.parallax.com/entry.php?393-Converting-Propeller-SPIN-program-to-C
Espa
Comments
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?
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
I see your comment here:
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):
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.
Looks like you have to do some parsing and compiling rather than just textual substitution to get this all right.
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.
But it's not Spin, is C, and is not required. And finally the code will be converted to PASM.
-Phil
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:
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
I see why all my tests on my PC work. Thank you for your comprehensive and complete explanation.
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.