Shop OBEX P1 Docs P2 Docs Learn Events
Updated Spin to C++ translator — Parallax Forums

Updated Spin to C++ translator

ersmithersmith Posts: 6,097
edited 2012-04-15 04:47 in Propeller 1
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)

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2012-01-19 12:52
    Eric,

    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
  • trodosstrodoss Posts: 577
    edited 2012-01-19 14:49
    Eric,
    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
    DAT
    tiles   file "tiles.dat"
    

    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
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-01-19 15:45
    As a torture test I tried to convert my CLIB object in the OBEX. The converter gave errors on the STRCOMP and COGID Spin keywords. It also did not like IFNOT, so I had to change that to "IF NOT".

    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.
  • kuronekokuroneko Posts: 3,623
    edited 2012-01-19 16:01
    Dave Hein wrote: »
    As a torture test I tried to convert my CLIB object in the OBEX. The converter gave errors on the STRCOMP and COGID Spin keywords. It also did not like IFNOT, so I had to change that to "IF NOT".
    Add to that list elseifnot (most likely anything but if/else), abort, fit, coginit and empty DAT blocks.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2012-01-19 17:57
    This is superb work and I look forward to using this code in the near future. Thanks++ - this is a great contribution to the propeller community!
  • Heater.Heater. Posts: 21,230
    edited 2012-01-19 23:26
    This sounds amazing. Can't wait to find some time to try it out. Sounds like it is going to be incredibly useful.
  • ersmithersmith Posts: 6,097
    edited 2012-01-20 04:48
    Thanks for all the feedback, everyone! I'll work on a new release that fixes these issues.

    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
  • kuronekokuroneko Posts: 3,623
    edited 2012-01-20 05:04
    ersmith wrote: »
    What problem did you encounter with "fit"? Is there some use for it outside of PASM code?
    Usually I use its default form, i.e. just fit which is equivalent to fit $1F0. This is flagged as an error.

    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).
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-01-20 06:19
    ersmith wrote: »
    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?

    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.
    Changing strcpy to Strcpy is a good solution. I thought about something like that after I posted my comments yesterday. spin2cpp has to handle the case insensitivity of Spin anyhow, so I hope this would be fairly easy to implement.

    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.
  • ersmithersmith Posts: 6,097
    edited 2012-01-28 08:38
    I've updated the zip file in the first post of this message to 0.5. This should address many of the issues that were found (thanks for the bug reports!). IFNOT and ELSEIFNOT should be working again. Identifiers now start with upper case (so as not to conflict with C identifiers). FIT with no address works, and the FILE directive is implemented. Dave, I've taken your suggestion on abort; I haven't tested it very thoroughly yet, but the basic idea seems sound, and Spin code with abort and catch should at least compile.

    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
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-01-28 19:07
    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.
  • kuronekokuroneko Posts: 3,623
    edited 2012-01-28 20:20
    FWIW, the operators <# and #> are not recognised in DAT sections, e.g.
    DAT             org     0
    
                    long    pin <# 31
    
    Also, org directives based on $ (e.g. org $ - 1) fail ATM.
  • bsnutbsnut Posts: 521
    edited 2012-02-02 02:07
    Heater. wrote: »
    This sounds amazing. Can't wait to find some time to try it out. Sounds like it is going to be incredibly useful.
    Eric,
    I just downloaded it myself and this will great help to the propeller community at the same time teaching my or someone else C++.
  • ersmithersmith Posts: 6,097
    edited 2012-04-15 04:47
    It's been a while since I've had time to work on this project, but I have uploaded a new version now that has a few more bugs fixed. As always, your bug reports and feedback are very welcome!

    Eric
Sign In or Register to comment.