Shop OBEX P1 Docs P2 Docs Learn Events
SPIN compiler...? — Parallax Forums

SPIN compiler...?

QUARKSPINQUARKSPIN Posts: 36
edited 2010-09-15 00:50 in Propeller 1
Would it be possible to precompile SPIN to ASM before downloading? I bet you could speed up your code a lot.(If it was smaller than 2kb.)

Comments

  • Ron CzapalaRon Czapala Posts: 2,418
    edited 2010-09-11 16:41
    I recently asked a similar question - See Mike Green's response

    http://forums.parallax.com/showthread.php?t=124949#post932433
  • QUARKSPINQUARKSPIN Posts: 36
    edited 2010-09-11 16:49
    So, it would be possible? I guess it's time for me to learn ASM.
  • Heater.Heater. Posts: 21,230
    edited 2010-09-11 16:49
    I'm sure there should be a sticky thread for this question. It seems to come up a lot. Or a big Propeller FAQ page somewhere obvious for these things.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-09-11 19:15
    I think this is an excellent question.

    That link to Mike Green's comment is very interesting - I see that catalina and imagecraft and propbasic are using LMM (which enables the code to expand bigger than the space in a cog, and potentially to megabytes if using external ram, or gigabytes if using ram and an sd card).

    To me, it seems that Basic and C are not very different to Spin. Kudos to those who wrote propbasic and C using LMM - that is not a task for the faint hearted. Doing something similar for Spin would be a fantastic achievement.

    I've sometimes pondered a 'poor man's alternative - something that takes Spin and translates it to basic or C, then feed that code through the existing compilers.
  • QUARKSPINQUARKSPIN Posts: 36
    edited 2010-09-11 20:49
    Good idea! I think I might throw together a python script to convert some of the core SPIN syntax to BASIC, I'll let you know if anything comes out of it. (But first, I'll have to learn propbasic. Do you happen to have a link?)
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-09-11 21:13
    Wow quarkspin, that would be a pretty cool piece of code.

    Propbasic syntax guide is at the bottom of the first post of this thread http://forums.parallax.com/showthread.php?t=118611&highlight=propbasic

    Even the very simplest bit of code would be a fantastic demonstration eg, Do/Loop, For/Next, If/EndIf and variables like A=5.

    I suppose with Spin you would need to count the indents (tab or spaces) to work out where an If/Do/For finishes.
  • QUARKSPINQUARKSPIN Posts: 36
    edited 2010-09-12 10:24
    I've started coding the converter, but I've hit a snag: Propbasic has a really freaky way of setting pin states! (You have to declare them as variables) Untill I find a workaround, the demo will be limited to output only.
  • QUARKSPINQUARKSPIN Posts: 36
    edited 2010-09-12 12:08
    Whelp, here is the first test. You will need python 3.1 to run it. (Remove the .txt)

    It can handle expressions, varible assignment, and whatever basic syntax just so happens to be the same as spin. It is less than a page of code, so don't expect anything fancy.
  • ericballericball Posts: 774
    edited 2010-09-12 12:18
    QUARKSPIN wrote: »
    Good idea! I think I might throw together a python script to convert some of the core SPIN syntax to BASIC
    Be very careful as SPIN's precedence rules are slightly different than C.
  • Dr_AculaDr_Acula Posts: 5,484
    edited 2010-09-12 16:16
    That python code looks good. := is a great place to start. What next, the If statement?
  • ericballericball Posts: 774
    edited 2010-09-13 04:06
    One idea I had is to take BST Sphinx and modify it to emit PASM based on the SPIN Interpreter source code instead of SPIN byte code. The advantage is all of the messy lexical analysis has been done already. Obviously the emitted PASM might not fit in a COG. Also, I have no idea how easy it would be to rip out the necessary pits from Chip's code. The SPIN Interpreter is also stack and HUB-RAM based, which is non-ideal for PASM.

    My error about BST. Since it is available for Linux I assumed the source was available.
  • David BetzDavid Betz Posts: 14,516
    edited 2010-09-13 08:13
    ericball wrote: »
    One idea I had is to take BST and modify it to emit PASM based on the SPIN Interpreter source code instead of SPIN byte code.
    I wasn't aware that the BST source code was available. Where can it be downloaded?
  • Heater.Heater. Posts: 21,230
    edited 2010-09-13 08:16
    It's not. You might have to be very nice to BradC.
  • Mike GreenMike Green Posts: 23,101
    edited 2010-09-13 08:44
    The source code for the Sphinx Spin compiler/assembler is available (and written in Spin). The main thing that's not in it is floating point constant expressions.
  • QUARKSPINQUARKSPIN Posts: 36
    edited 2010-09-13 14:36
    Couldn't you just get the bytecode directly from propeller tool? Then, the Sphinx Spin compiler would work for this without modifacation. (Less work for me!) I'll check out this Sphinx thing, and possibly write an extraction tool for getting spin bytecode out of .eeproms.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-09-13 15:20
    It might be useful to convert Spin bytecodes into PASM. The bytecode interpreter is basically a stack-based machine. All of the parameters are pushed onto the stack, and then they are popped off as the instructions are executed. The converter could just generate the equivalent PASM instructions.

    Higher level operations, such as multiply, divide, bytemove, strsize and lookup could be implemented as calls into PASM routines that execute those functions. They would essentially be copies of the existing Spin interpreter code.

    The converted Spin programs would have to be pretty small or they would use up the cog memory. A converted Spin program would probably run 2 or 3 times faster than interpreted Spin. It could be made even faster if cog memory was used instead of the stack. A peephole optimizer could be written to speed up the generated PASM even more by eliminating redundant stack pushes and pops.

    Edit: I re-read the thread and it seems like my suggestion is just restating the last few posts. However, it does seem like the easiest way to generate PASM from Spin source.
  • Cluso99Cluso99 Posts: 18,069
    edited 2010-09-13 16:01
    Quarkspin: Take a look at both bst and homespun listings. That may be easier to extract the bytecodes, at least for starters.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2010-09-13 17:21
    I don't know if it's been discussed already, but is there any real compelling reason to have the Spin interpreter in ROM?

    For Prop2, why not just have Spin compiled to PASM or LMM via the Propeller tool, and use the silicon real estate for something more useful? Or load the Spin interpreter via binary from the Prop tool?

    Note, I'm not saying get rid of Spin, just suggesting taking it out of the ROM.
  • Heater.Heater. Posts: 21,230
    edited 2010-09-13 21:26
    Kevin,
    I'm not saying get rid of Spin, just suggesting taking it out of the ROM.

    Interesting thought.

    Of course if it is not in ROM it would have to be loaded from RAM. Taking up 4K of the 32K on the Prop I would not be nice but on the Prop II might be reasonable.

    Taking it out of ROM and having a mechanism that loads it from RAM at start up would make it replaceable, we could boot straight into Catalina's LMM interpreter or Zog and run C/C++ instead of Spin:)

    As the ROM is so much bigger on the Prop II I would not worry about the space the Spin interpreter takes.

    What "more useful" thing would you put in that space?
  • David BetzDavid Betz Posts: 14,516
    edited 2010-09-14 11:48
    Heater. wrote: »
    As the ROM is so much bigger on the Prop II I would not worry about the space the Spin interpreter takes.

    What "more useful" thing would you put in that space?
    How about the ZOG VM? :-)
  • Heater.Heater. Posts: 21,230
    edited 2010-09-14 12:01
    Ha, David, I already suggested that, in a not so serious way. It was during a discussion of the "not C" compiler that Parallax has proposed to create.

    What I did suggest seriously is that Parallax might think about creating a better VM, that is more suited to C/C++ than the Spin VM, and target a standards compliant C compiler at that.

    The reason I dared to make the suggestion is that Zog at least shows it is possible.
  • David BetzDavid Betz Posts: 14,516
    edited 2010-09-14 12:08
    In the process of implementing ZOG, have you thought of optimizations to it or changes to its instruction set that would make it more efficient on the Propeller?
  • Heater.Heater. Posts: 21,230
    edited 2010-09-14 12:44
    David Betz:
    In the process of implementing ZOG, have you thought of optimizations to it or changes to its instruction set that would make it more efficient on the Propeller?

    No.

    Well, yes but...

    Zog is a software "emulation" of the ZPU processor core designed by ZyLin Inc. for FPGA's.

    Zog only exists because:
    a) It's implementation is small enough to fit into one COG.
    b) ZyLin targeted the GCC compiler at the ZPU.

    Item b) is the main point here. If one were to add to the ZPU instruction set or change it in any way then one would have to modify the zpu-gcc compiler to make use of it. I do not have the skills, time or patients to undertake such a big endeavour.

    Aside: Having got the first ever C code working on the Prop via the ZiCog Z80 emulator and the BDS C compiler (Still the only one that compiles C code on the Prop itself) I had tried to find a 16 or 32 bit CPU that could be easily emulated on the Prop and had an existing C compiler. I had just given up when I stumbled across the ZPU.

    That said there are things I would change about the ZPU. It uses memory mapped IO which is a pain as the interpreter has to check every memory access for IO addresses thus slowing things down a bit.

    The tea encryption algorithm performs modulus operations on unsigned ints for which there is no instruction, instead long winded ZPU subroutines are compiled in to do it. So unsigned mod and div would be nice.

    But then again there are very few free opcodes to adopt for such things.
  • Kevin WoodKevin Wood Posts: 1,266
    edited 2010-09-14 20:04
    Heater. wrote:
    What "more useful" thing would you put in that space?

    Well, I don't know... how about some A/D circuitry? Floating point circuitry? Something?

    Having Spin hardcoded seems a bit against the minimalist-customize-your-own-device that the Propeller is promoting. Probably 75%+ of all of the threads here have something to do with bypassing Spin for either PASM, LMM, or some other language. PropBASIC compiles to PASM, other languages compile to LMM, and Spin seems like the minority choice. Heck, there are even improved Spin interpreters out there that people are loading instead.

    As I said, don't get rid of it. Just take it out of the silicon, and use the space for something else that makes more sense to implement in hardware.
  • Heater.Heater. Posts: 21,230
    edited 2010-09-15 00:50
    Kevin,
    Having Spin hardcoded seems a bit against the minimalist-customize-your-own-device that the Propeller is promoting

    I might agree. Only on the Prop I having the interpreter not in ROM means it world be loaded into RAM at boot time, eating valuable RAM space. That's not such an issue with the bigger PROP II RAM but presumably it makes sense to have the Prop II as software compatible as possible.

    May be 75% of posts here are related to developing alternate languages, compilers, interpreters. But I'm not sure we can conclude that 75% of users are building their projects with anything other than Spin/PASM.
Sign In or Register to comment.