Updated Spin to C++ translator
ersmith
Posts: 6,097
Here's an update to the spin2cpp translator. It can now handle much more of the Spin language, including floating point. It successfully compiles and runs Float32Full.spin, FloatMath.spin, FloatString.spin, and F32.spin (there are workarounds built in for the assumptions all of these make about the Spin stack layout). FullDuplexSerial.spin and the C3 VGA demos also work.
There are also a number of bug fixes to the PASM translator. I think it handles most PASM code correctly now.
Thanks to Dave Hein for the bug reports and for his stoc converter code, which had better ways to implement some of the operators.
Eric
Update 2012-01-28: version 0.5 is now available (attached to this message)
Update 2012-04-15: version 0.6 is now available (attached to this message)
There are also a number of bug fixes to the PASM translator. I think it handles most PASM code correctly now.
Thanks to Dave Hein for the bug reports and for his stoc converter code, which had better ways to implement some of the operators.
Eric
Update 2012-01-28: version 0.5 is now available (attached to this message)
Update 2012-04-15: version 0.6 is now available (attached to this message)
zip
235K
Comments
I tried out your latest version of the translator, and it looks very nice. I was able to build and run the three sample programs without any problems. Your translator should be very useful for converting existing Spin code in the OBEX to C/C++.
With the right IDE I could see the translator being used by Spin programmers to write Spin code that gets converted to C++, and then compiled to LMM or XMM programs. It could made as easy to use as the current Spin tool.
Dave
I have been really impressed with you work so far! It seems to handle a lot of code, but is having difficulty with the "file" command being specified in a DAT block
spin2cpp.exe returns:
test.spin: syntax error at line [x] (line where the "file" occurs in the listing)
Other than that it seems to be handling the code fairly well.
--trodoss
[Edit:] added Spin code for a referenceable example
Another issue I ran into is that C keywords will cause a problem when trying to compile the CPP file. It is OK to use FOR or STRLEN as a vairable name in Spin, but this causes a problem in C. Maybe C keywords could be identified in the Spin code and converted to a non-conflicting variable name, such as FOR_001 and STRLEN_002.
trodoss: I completely forgot about the "file" directive in PASM. Thanks for the sample code, I'll use it to get things working.
Dave: Hmmm, I wasn't thinking carefully about possible name conflicts with C keywords. How about this: instead of translating all identifiers to lower case (as it does now) spin2cpp could make the first letter upper case and the rest lower case, so "strcpy" becomes "Strcpy". Since C is case sensitive this would avoid all conflicts with C's keywords and library functions (which are either all upper case or all lower case). Does that sound OK?
IFNOT and ELSEIFNOT were actually working at one point, but I forgot to add a test for them and so of course they broke when I changed the way indentation is parsed. I'll fix that.
kuroneko: Thank you for finding those problems. "abort" is going to be tricky to implement, but I should at least note the problem in the documentation -- thanks for reminding me. What problem did you encounter with "fit"? Is there some use for it outside of PASM code?
Eric
OTOH, I'm not entirely sure that fit par needs to be handled (PropTool does and treats it correctly as fit $1F0, i.e. register names are simply constants).
In my previous attempts at a Spin to C converter I used longjmp to handle abort. You would use setjmp when you encounter the abort trap operator "\", and call longjmp in place of abort. The tricky part is handling multiple levels of setjmp. This would require maintaining a list of jmp_buf structs. Any method that contains an abort trap will need to remove it's entry off the jmp_buf list when it returns. It's complicated, but doable.
I should emphasize again that spin2cpp is not sponsored or requested by Parallax in any way. It's a project I've done on my own free time with the hope that it will be useful to the Propeller community. Don't blame Parallax for flaws in the program, or for the existence of yet another way to run Spin/Pasm :-).
Eric
I tried spin2cpp on the testclib.spin program in the CLIB object. It now compiles without any problems. I ran it, and the first part of the program works fine. That's pretty amazing considering the various Spin tricks that it uses. It passes pointers to the method argument lists to functions that handle variable number of parameters. spin2cpp handles this fine. Also, there is no longer a conflict with the C keywords that I was using in my Spin code. I am running into some problems, and I'll track down what's causing that.
One problem we're going to encounter is the practice of storing memory addresses in word variables. When possible, it would be good for spin2cpp to print a warning when it sees this. My Spin malloc method uses a linked list with word pointers. I'll change this to long pointers to see if that resolves my problems.
Overall, I am very impressed by the progress you've made on spin2cpp.
Dave
EDIT: I thought about the pointers some more, and word pointers should work OK for LMM programs. It would only be a problem for XMM programs.
I just downloaded it myself and this will great help to the propeller community at the same time teaching my or someone else C++.
Eric