Shop OBEX P1 Docs P2 Docs Learn Events
Spin to PASM converter (still very early stages) — Parallax Forums

Spin to PASM converter (still very early stages)

I've been doing some work on adding PASM output to spin2cpp, so it can be used to produce PASM directly (instead of having to produce C code and then use PropGCC to create PASM). The advantage would be much more readable output, and also it will serve to validate the idea of putting different back ends on spin2cpp. We've talked in the past about using the LLVM code generator when it becomes available.

The custom code generator will not produce code of the quality of, say, GCC or LLVM, but it does do some basic optimizations and the result is not too bad. I was able to actually build and run my first program completely with spin2cpp (no PropGCC needed). It's a blinking LED program, of course. The input source code (foo.spin):
CON
  _clkmode = xtal1+pll16x
  _clkfreq = 80_000_000
  pin = 15

PUB start | pause
  pause := (_clkfreq/2)

  DIRA[pin] := 1  ' set as output
  OUTA[pin] := 1  ' turn it on
  repeat
      waitcnt(CNT + pause)
      !OUTA[pin]
and the output (unmodified) produced by spin2cpp --asm foo.spin:
DAT
	org	0
foo_start
	mov	foo_start_pause_, imm_40000000_
	or	DIRA, imm_32768_
	or	OUTA, imm_32768_
L_001_
	mov	foo_start_tmp001_, CNT
	add	foo_start_tmp001_, foo_start_pause_
	waitcnt	foo_start_tmp001_, #0
	xor	OUTA, imm_32768_
	jmp	#L_001_
L_002_
foo_start_ret
	ret

foo_start_pause_
	long	0
foo_start_tmp001_
	long	0
imm_32768_
	long	32768
imm_40000000_
	long	40000000
This can then be compiled into foo.binary via "spin2cpp --binary --dat foo.pasm".

It is still very early days, so I don't have a binary release yet -- ATM this is for fairly experienced programmers only. The code resides on the "spin2pasm" branch of https://github.com/totalspectrum/spin2cpp.

There's still a lot to do (e.g. the VAR section is ignored at the moment) but I was hoping for some kind of general feedback on what kinds of features would be useful in this:

(1) Should the output use only COG memory (so it can only hold very small amounts of data + code), or does it make sense to put the VAR data into HUB memory the way PropGCC does?
(2) Is LMM mode desirable? I presume so.
(3) How important is P2 support?
(4) Right now all locals are placed in COG memory rather than on a stack in HUB. This produces much more readable (and efficient) code, but it means that (a) recursive functions are not allowed, and (b) COG memory gets used up quickly.

I guess there are some interesting trade-offs between making the code readable and useful for learning versus making a production ready compiler. Any thoughts on which way this tool would be used?

Finally, once the front end (spin parser) and back end (code generator) are cleanly separated, then in theory other front ends like Basic, Pascal, etc., could be added. I don't know how useful this would actually be, since the back end is not going to optimize as well as GCC or LLVM. On the other hand I think it will be much lighter weight and easier to use, so perhaps it would still be useful to people?

Eric

Comments

  • P2 support would be much appreciated I imagine since there is currently no way to run Spin code on P2. Also, because of hubexec, you should be able to compile large Spin programs since you aren't limited to 2K of code like you are on P1.
  • @David, See p1spin here
  • p1spin is an unrolled version of the P1 interpreter that runs on the P2 using hubexec. It's not particularly fast and it doesn't support any of the extra P2 features. However, it could be extended for the P2 and the speed could probably be improved a bit. It doesn't take advantage of CLUT execution, which could be used to speed up more frequently used byte codes.

    I like Eric's idea of creating a Spin-to-PASM compiler. This would allow Spin to execute much faster than the current method of using byte codes and an interpreter. There are a lot of trade-offs in how this is implemented. A truly compatible implementation would need to use the stack just like the interpreter does. It's not clear how much faster this would be than the interpreter version since it would need an LMM interpreter to run from Hub memory on the P1. However, it would avoid the need to decode byte codes.

    Using registers instead of the stack will improve performance. I do wonder if spin2cpp is a better approach since the optimizer in the C compiler can be used to improve performance even further.
  • RaymanRayman Posts: 14,649
    I think spin2cpp is amazing and very useful. This would be icing on the cake.

    P2 support with hubexec would also be amazing.
    I imagine that would be a lot of work though.
  • ozpropdev wrote: »
    @David, See p1spin here
    Sorry, I forgot about that. Anyway, a Spin compiler that generates native P2 hubexec code would generate code that runs quite a bit faster.

  • Rayman wrote: »
    I think spin2cpp is amazing and very useful. This would be icing on the cake.

    P2 support with hubexec would also be amazing.
    I imagine that would be a lot of work though.
    Why would it be a lot of work? Hubexec lets you run large amounts of PASM code directly from hub memory. It seems to me that it wouldn't be much harder to generate code for P2 hubexec than what Eric's already doing generating P1 PASM code.

  • jmgjmg Posts: 15,173
    ersmith wrote: »
    (1) Should the output use only COG memory (so it can only hold very small amounts of data + code), or does it make sense to put the VAR data into HUB memory the way PropGCC does?
    (2) Is LMM mode desirable? I presume so.
    (3) How important is P2 support?
    (4) Right now all locals are placed in COG memory rather than on a stack in HUB. This produces much more readable (and efficient) code, but it means that (a) recursive functions are not allowed, and (b) COG memory gets used up quickly.

    I guess there are some interesting trade-offs between making the code readable and useful for learning versus making a production ready compiler. Any thoughts on which way this tool would be used?

    This looks great.

    Can you add the Source as comments in the ASM ?
    Do you create any sort of Debug file ?

    A weakness in Spin2cpp is the final code is going to have so many steps, original source code step-debug is going to be very hard.

    SDCC has an interesting approach and IIRC they embed the Source file path and line numbers in the ASM, so that simple debug can work-backwards to HLL lines.


    Spin can always run as normal Spin, so I would focus on 'small' here to act as a bridge between Spin and PASM for new users.
    ie they code in Spin, then pick the bits that need to be faster.


    This could also be a great way to get rapid P2 Spin/PASM, which I would call very important.
  • jmg wrote: »
    Can you add the Source as comments in the ASM ?
    Hmmm, that's a good idea. It'd be moderately complicated I think -- the back end doesn't have direct access to the source code, but it does have the file name and line number for all the statements, so it can go back and re-open the file to get what it needs. I'll probably leave that until a bit later, but thanks for the idea, it would be useful.
    Do you create any sort of Debug file ?
    At the moment it just produces PASM text, which can then be compiled into a raw binary. What sort of debug file were you thinking of? The PropGCC .elf format would be an obvious candidate, but I'm not sure how many people actually use gdb on the Propeller.
    A weakness in Spin2cpp is the final code is going to have so many steps, original source code step-debug is going to be very hard.
    If you use the -g flag (for debug) then the C/C++ code output by spin2cpp contains references to the original .spin file. If you debug the compiled .elf with gdb you should see the original Spin source in gdb.

    Of course that doesn't apply to the --asm variant ("spin2pasm"). For that we could output DWARF debug info and assemble with GAS. But for now I'd like to keep spin2pasm self contained (not requiring external programs).

    Thanks for your suggestions!
  • David Betz wrote: »
    Rayman wrote: »
    I think spin2cpp is amazing and very useful. This would be icing on the cake.

    P2 support with hubexec would also be amazing.
    I imagine that would be a lot of work though.
    Why would it be a lot of work? Hubexec lets you run large amounts of PASM code directly from hub memory. It seems to me that it wouldn't be much harder to generate code for P2 hubexec than what Eric's already doing generating P1 PASM code.

    I think you're right, David -- P2 hubexec shouldn't be too hard to support. I've tried to write the code with eventual P2 support in mind, but I don't have a working P2 setup yet so I've been focused on the P1 for now. At the source level the two architectures seem not too different, although obviously there will be some tweaks required to take advantage of the P2. The biggest pain will probably be doing PASM -> binary translation for P2; we'll want that to process P2 DAT sections.

  • jmgjmg Posts: 15,173
    ersmith wrote: »
    The PropGCC .elf format would be an obvious candidate, but I'm not sure how many people actually use gdb on the Propeller.
    Yes, I was thinking-ahead a little here - P1 has poor Debug support, but P2 should be able to offer much better Step/Break so it is nice to get the hooks in place now, for that.

  • @ersmith

    re:Spin to PASM converter

    Cool! Your doing great work. For us that are not PASM experts all you need to do is look at the P2 doc's once and you will soon realize that a Spin to PASM converter will be very helpful on the P2 :)
  • ersmith wrote: »
    I think you're right, David -- P2 hubexec shouldn't be too hard to support. I've tried to write the code with eventual P2 support in mind, but I don't have a working P2 setup yet so I've been focused on the P1 for now.
    I think you have a DE2-115 board don't you? Chip has made an FPGA image available for that.

  • David Betz wrote: »
    ersmith wrote: »
    I think you're right, David -- P2 hubexec shouldn't be too hard to support. I've tried to write the code with eventual P2 support in mind, but I don't have a working P2 setup yet so I've been focused on the P1 for now.
    I think you have a DE2-115 board don't you? Chip has made an FPGA image available for that.

    Chrome refuses to download the latest P2 package for me because it thinks there's a virus in it. I guess it's probably a false positive, since nobody else has noticed it, but I haven't had the time right now to figure out a work-around. I do plan to try to get it working, but right now I thought I'd get P1 PASM going first since I'm more familiar with it.
  • ersmith wrote: »
    David Betz wrote: »
    ersmith wrote: »
    I think you're right, David -- P2 hubexec shouldn't be too hard to support. I've tried to write the code with eventual P2 support in mind, but I don't have a working P2 setup yet so I've been focused on the P1 for now.
    I think you have a DE2-115 board don't you? Chip has made an FPGA image available for that.

    Chrome refuses to download the latest P2 package for me because it thinks there's a virus in it. I guess it's probably a false positive, since nobody else has noticed it, but I haven't had the time right now to figure out a work-around. I do plan to try to get it working, but right now I thought I'd get P1 PASM going first since I'm more familiar with it.
    Makes sense. I have to admit I haven't done anything with the latest P2 FPGA image either other than load it on my 1-2-3 A9 board.

  • RaymanRayman Posts: 14,649
    I had a lot of trouble downloading P2 stuff due to false positive virus stuff too...

    One thing that worked was to open it with Google Drive, which is connected to the download page somehow...

    Then, I could download from Google Drive...
  • @ersmith

    re:Chrome refuses to download the latest P2 package for me because it thinks there's a virus in it.


    You can usually click on the icon for your antivirus program and disable it temporarily. For example on one system I have Avast and I can disable it for 10 minutes, 1 hour etc.

    If chrome is still blocking it just use another web browser ( IE, Firefox etc)

  • Seems to have drifted of to P2-land (again?).

    What ever happened to the Spin to PASM converter?

    I need that bad boy.
  • jmgjmg Posts: 15,173
    cavelamb wrote: »
    Seems to have drifted of to P2-land (again?).

    Are you reading the same thread I am ?

    I can see this :

    "right now I thought I'd get P1 PASM going first since I'm more familiar with it."

    Does not look one bit like 'drifted of to P2-land' to me ?

    Makes sense to do it for P1 first, as many eyes can scan P1 ASM very quickly.
  • Thank you so much.
    You were very helpful.
  • Bob, David: thanks for the tips about P2

    All: I'll be posting a preview version of the converter in a new thread shortly
  • FYI, I've added two more build configurations for Windows/Linux builds of spin2cpp for the spin2pasm branch
    http://david.zemon.name:8111/project.html?projectId=Spin2Cpp&guest=1
Sign In or Register to comment.