Shop OBEX P1 Docs P2 Docs Learn Events
Has anyone written a spin compiler? — Parallax Forums

Has anyone written a spin compiler?

HollyMinkowskiHollyMinkowski Posts: 1,398
edited 2009-06-04 17:25 in Propeller 1
Just wondering if someone has created a compiler for spin.
Seems like one would be useful and that several additions to spin could be added.

It seems a bit odd that the propeller has an interpreter instead of just having an
optimizing compiler. Lots of people can whip up compilers ...it just seems
harder to embed the interpreter than to write a compiler?!?! or am I missing something?
Is there some reason an interpreter was a better choice? You can always make corrections
to a compiler but an interpreter in silicon seems a risky joy or pain forever...I'd have been chewing
down my fingernails and praying no one found an error in the interpreter that could not be fixed.

Comments

  • JonnyMacJonnyMac Posts: 9,195
    edited 2009-06-02 21:57
    The Propeller spent a very long time in development to ensure that the interpreter was right. Parallax has some experience with this so you can rest easy.
  • RossHRossH Posts: 5,512
    edited 2009-06-02 21:59
    Hi HollyMinkowski.

    I'm not aware of any SPIN compilers - why would you need one when we have a perfectly good SPIN interpreter embedded in each Propeller? But I'm sure some lunatic in these forums has probably either already written one or has one in development smile.gif

    In fact an interpreter is far easier to write than a compiler, and interpreters tend to consume far less resources at both load time and run time. That's why many of the early high level languages for microcomputers were interpreted (pascal, basic etc) - there just wasn't enough space or speed for anything else. The main disadvantage of an interpreted language is that they are usually far slower - often by an order of magnitude - than a compiled language.

    But you are right that with an interpreter embedded in silicon you have to get it right first time - so the success of SPIN is a great credit to Parallax in general and Chip Gracey in particular.

    If you've been following the various compiler threads, you will see that compilers for the Propeller are still a bit problematic, in that their output sometimes consumes too much space relative to the benefit they offer. This is because the Propeller instruction set is based on 32 bit 'longs', and therefore the code takes up a lot of space. The propeller instruction set is also not particularly well geared to implementing stack based machines, which is the model most modern languages and compilers use. SPIN, on the other, hand is byte-oriented, and it does implement a stack - but it's very slow in consequence, and some things (like floating point) had to be left out altogether to amke it fit at all.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • jazzedjazzed Posts: 11,803
    edited 2009-06-02 22:33
    The Propeller Tool, Homespun, and BST all "compile" Spin byte code for the rom code to run which is different from the slowest meaning of interpreter (parsed tokens) ... Spin is very similar to the Java system (without requiring a browser and applets of course).
    Compiled output that is native PASM is bigger and faster than Spin code (as Ross mentioned). All in all the system Parallax created is a good fit for the target Propeller market. We're always trying to make it wear bigger shoes though [noparse]:)[/noparse] don't be distracted by that.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • heaterheater Posts: 3,370
    edited 2009-06-03 04:16
    I think if you look at the Prop architecture long enough you will become convinced that the combination of interpreted Spin and raw PASM is about optimal for it.

    Ask yourself this: What would a compiler for the Propeller compile to? Normally compilers output native instructions of their target processor, which saves one having to write ones code in assembler. BUT for the Propeller this is not realistically an option. PASM instructions are all huge 32 bit things and you can only get 496 of them into the a COG. Not much use spending a lot of time to build a compiler that can only output such small programs.

    Then there are issues of not having a stack based architecture, as noted above, and having to use self modifying code in PASM to do anything interesting. Neither is good for complier code generation as far as I understand. Not to mention the difficulties of handling bytes and words in PASM instructions.

    Now we do have compilers for the Prop., ICC and Catalina (maybe others). I would argue they are not optimal for the Prop as they compile down to LMM. That is PASM instructions in HUB RAM that have to be fetched into the COG my a PASM kernel and executed there almost one at a time. They have very poor code density due to the size of the PASM op codes and can never approach the raw speed of PASM (which a good compiler almost can) due to the LMM method which is still a form of interpreter.

    Interestingly, judging by Chip's reaction when Bill Henning first described the LMM technique on this forum, it had never occurred to Chip to fetch PASM from HUB for execution. I believe that even if it had he would have rejected it as a solution as the code density achievable, i.e. the amount of functionality one can get in the limited space available, is much greater with Spin.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Ken GraceyKen Gracey Posts: 7,400
    edited 2009-06-03 04:33
    The BASIC Stamp 1/2's interpreter is the same as it was in 1995, and over 5 million units are in use. Chip does very precision work and releases nothing until it's perfect. And that's why we're going to be waiting for Propeller 2 for a long time <= long time. We wouldn't ask you to "trust us" since that's a phrase your salesman would use, but we can certainly point to other Parallax products which use essentially the same firmware as their initial release over 15 years ago. During this time we've dealt with a variety of component EOL issues, though, and clock combinations. If we could only get the hardware supply from others to be as solid as the interpreter design this business would be a lot easier. . .

    Gotta go - just got another end of life notice that should keep our purchasing manager busy for a week. . .

    Ken Gracey
    Parallax, Inc.
  • heaterheater Posts: 3,370
    edited 2009-06-03 05:03
    Ken. At last we know!. The Prop will be out before or on a long time, not after (<=).
    This is great news because I can't bear to wait more than a long time[noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • RossHRossH Posts: 5,512
    edited 2009-06-03 05:19
    @all

    I agree with everything heater said - 110%. It continues to astound me just how much you can do in SPIN with a tiny RAM footprint, and also much you can do in a measly 496 instructions in PASM. And of course Ken's comments are true as well - SPIN is precision software, engineered with the same level of rigour and attention to detail as the Propeller hardware itself.

    And yet ....

    .... stepping up on soapbox ....

    I maintain that not only is there a place for higher level languages - such as C - on the Prop, but that the Propeller is doomed to end up as a wonderfully engineered but obscure and underutilized curio without them. Let's face it - who's going set up a development team of 100 SPIN programmers to develop a new whiz-bang-internet-capable-fourth-generation-self-diagnosing-field-upgradeable-bluetooth-enabled waffle maker? (I mean, apart from the lunatics on this forum?)

    But that's where most chips like this end up - billions and billions and BILLIONS of them.

    To quote en.wikibooks.org/wiki/Embedded_Systems
    Embedded Systems said...
    ANSI C programming language: Many microprocessors and microcontrollers can be programmed in C, and a number of C cross-compilers exist for that purpose. C is perhaps the most frequently used language for new embedded system development. The "const" and the "volatile" keywords, rarely used in desktop app programming, become very important in Embedded Systems/C Programming.

    Personally, I think the word "perhaps" in that quote is only there so the author doesn't have to nominate their sources. I would replace it with "unquestionably" but then some smart-alec would ask me to nominate my sources - so I won't.

    Or to quote another chip maker - in this case National Semiconductor www.national.com/an/AN/AN-587.pdf
    National Semiconductor said...
    C is becoming the higher-level language of choice for microcontroller programming. Traditional usage of C depends on assembly language for the intimate interface to the hardware. A few extensions to ANSI C allow embedded systems to connect directly and simply, using a single language and avoiding detailed knowledge of the compiler and hardware connections.

    .... stepping down off soapbox ....

    heater said PSIN and PASM is an optimal combination. I agree, from a technical perspective, but I think from a commercial perspective that C and PASM may be the combination that really makes the Propeller fly, and takes it to places that SPIN and PASM simply won't reach. And why is this so important? Why, so that Parallax makes more money on the Propeller I and we get the Propeller II sooner, of course!

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-03 05:29
    Something to keep in mind ... A C compiler can produce Spin byte codes just as easily as it can produce LMM or native instructions. C would need some non-standard extensions to make the most use of some of the built-in primitive operations, but this can be done using predefined functions that generate in-line code. There's not a lot of difference between Spin and C.
  • heaterheater Posts: 3,370
    edited 2009-06-03 05:37
    Good points Ross. My conclusions from all you say is this:

    1. C compilers should not generate PASM code for LMM (or XMM) rather they should emit SPIN byte code to be run by the Props existing interpreter.

    2. We should create a SPIN byte code interpreter that will execute SPIN byte codes from external RAM.

    The reason for 1) is that compiling to LMM is always going to be wasteful of HUB RAM and never approach PASM speed. We have PASM for speed anyway. This brings us back to the meaning behind Holly's first question.

    The reason for 2) is to allow for huge programs (by Prop standards) in external RAM. We now have multiple external RAM solutions either available or on the drawing board.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • RossHRossH Posts: 5,512
    edited 2009-06-03 05:43
    Mike,

    Quite true, although the differences between C and SPIN are more substantial than is at first apparent (e.g. structures). And non-standard C would be worse than none at all. Extend C with library functions by all means - but keep the language intact or you are just shooting yourself in the foot - and that's what C++ was invented for!

    I decided against going down this path because the loss of execution speed would have been too high (at least for me) - but it might be worth someone else revisiting it.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • heaterheater Posts: 3,370
    edited 2009-06-03 05:45
    Which brings me to a question:

    Is it possible that compiling down to SPIN byte codes to be executed from external RAM could actually result in faster execution than compiling down to PASM instructions in external RAM ?

    I'm thinking that SPIN byte codes fetched through the "key hole" of the byte wide access to external RAM that we currently have could be more efficient than fetching 32 bit long instructions. There are presumably byte codes for stack operations and such that could be more efficient than sequences of PASM that do the same thing.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • RossHRossH Posts: 5,512
    edited 2009-06-03 05:51
    heater,

    You may be right - it is certainly worth investigating. I looked a this (very) briefly as well, but discarded the idea when I realized how many byte codes you'd have to add to the SPIN byte code set to support ANSI C. At that point I realized there was not much point in trying to maintain compatibility with SPIN - you'd only be able to support a partial subset of C if you did - and you'd probably be better off designing your own byte encoding. So I almost went down the Java route - but someone else was already working on that.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • heaterheater Posts: 3,370
    edited 2009-06-03 06:34
    Interesting. I know nothing of the SPIN byte codes. I just assumed there was enough there to implement any high level language, after all SPIN does have a stack and the like.

    Out of interest can you give an example of a "missing" byte code operation that C might need ?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • RossHRossH Posts: 5,512
    edited 2009-06-03 06:45
    heater,

    Fairly obviously - anything to do with floating point. Less obviously, things like type conversions - SPIN has no data types for local variables. In some cases you could make up for these by combining multiple byte codes. But (just as a ludicrous example, which is probably not true) if it took four byte codes to convert a signed byte into an unsigned long, the final program would execute too slowly to be very useful - and would be as long as the quivalent PASM program in any case.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-06-03 07:03
    Interpreter and External Memory...

    I cannot answer the question of speed for C to run from external memory.

    However, I can say that the Interpreter could fetch the data (bytes) from external memory at least as fast, and maybe marginally faster, providing there are no latches (another gotcha)·from external memory as hub. However, any improvement would·be marginal since at a minimum, about 27 instructions are executed in pasm for each bytecode fetched/executed. In fact, I think 27 was my Interpreter and the rom interpreter was slower.

    The next gotcha is that the external memory can only be accessed by 1 cog without overhead of collisions.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBladeProp, SixBladeProp, website (Multiple propeller pcbs)
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: Micros eg Altair, and Terminals eg VT100 (Index)
    · Search the Propeller forums (via Google)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • RossHRossH Posts: 5,512
    edited 2009-06-03 07:21
    @Cluso,

    Very useful info. I knew SPIN was quite slow, but I never had any actual numbers.

    Of course, not all LMM instructions equate to one PASM instruction (although many do) - some of them are the LMM "meta" instructions that take multiple PASM instructions to execute within the Kernel. But even so, on these numbers I'd expect byte coded C to be around 10 times slower than LMM C executed from hub RAM - and maybe 3 to 4 times slower than LMM C executed from XMM RAM.

    Ross.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Catalina - a FREE C compiler for the Propeller - see Catalina
  • heaterheater Posts: 3,370
    edited 2009-06-03 08:23
    RossH:

    Floating point of course. I forgot all about it. In some decades now of programming embedded systems I've never had to use it.
    Type conversions is not so obvious, as you say.

    Clusso:

    "Interpreter could fetch the data (bytes) from external memory at least as fast, and maybe marginally faster...from external memory as hub."

    If that is true then there really should be an XMM Spin compiler/interpreter combo.

    "The next gotcha is that the external memory can only be accessed by 1 cog without overhead of collisions."

    Yes, but that is true of XMM C as well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • Cluso99Cluso99 Posts: 18,069
    edited 2009-06-03 09:12
    When there is an XMM Spin compiler I can resurect and fix the couple of bugs in my version of Chip's Interpreter (it was approx 20% faster) and modify the fetch to external memory. Not much work, but then not much time either·[noparse]:([/noparse]

    Of course you can only run 1 XMM C program. I just hadn't bothered to think about that because I am not a C programmer.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Links to other interesting threads:

    · Home of the MultiBladeProps: TriBladeProp, SixBladeProp, website (Multiple propeller pcbs)
    · Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
    · Prop Tools under Development or Completed (Index)
    · Emulators: Micros eg Altair, and Terminals eg VT100 (Index)
    · Search the Propeller forums (via Google)
    My cruising website is: ·www.bluemagic.biz·· MultiBladeProp is: www.bluemagic.biz/cluso.htm
  • heaterheater Posts: 3,370
    edited 2009-06-03 09:25
    Are you hinting that someone is working on an XMM Spin compiler ?

    Brad, mpark, pretty please...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • jazzedjazzed Posts: 11,803
    edited 2009-06-03 10:37
    RossH said...
    ·... just shooting yourself in the foot - and that's what C++ was invented for!
    Taken out of context of course, but very true LOLOLOL [noparse]:)[/noparse]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    --Steve


    Propalyzer: Propeller PC Logic Analyzer
    http://forums.parallax.com/showthread.php?p=788230
  • BradCBradC Posts: 2,601
    edited 2009-06-03 10:58
    jazzed said...
    RossH said...

    ... just shooting yourself in the foot - and that's what C++ was invented for!


    Taken out of context of course, but very true LOLOLOL [noparse]:)[/noparse]

    C lets you shoot yourself in the foot, however when you do that with C++ you blow your whole leg off [noparse]:)[/noparse]

    @heater. I was just thinking about the XMM SPIN thing (no, I'm not planning it). I can't see many advantages aside from perhaps being able to use the hub as high speed RAM for video buffers or the like while running the actual spin code bytecode and storage from XMM. You'd need extra opcodes to determine what goes where, and there are a whole load of limitations with regard to pointers and addresses being 16 bit in the current system. On top of that you'd need local cache areas and bounce buffers in the HUB. I just can't see a point to it myself. You may as well write a new interpreter/compiler from scratch. If you look at it, XMM / LMM is really a kind of interpreter also.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!
  • heaterheater Posts: 3,370
    edited 2009-06-03 11:58
    BradC "I can't see many advantages aside from perhaps being able to use the hub as high speed RAM for video buffers or the like while running the actual spin code bytecode and storage from XMM."

    That already sounds like a great advantage !

    However if you have to add new opcodes and bend things around to get over 16 bit addresses, then we need a whole new interpreter. For sure it looks like to much of a mess of a solution.

    Oh well.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    For me, the past is not over yet.
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2009-06-03 12:15
    BradC said...
    @heater. I was just thinking about the XMM SPIN thing (no, I'm not planning it). I can't see many advantages aside from perhaps being able to use the hub as high speed RAM for video buffers or the like while running the actual spin code bytecode and storage from XMM. You'd need extra opcodes to determine what goes where, and there are a whole load of limitations with regard to pointers and addresses being 16 bit in the current system. On top of that you'd need local cache areas and bounce buffers in the HUB. I just can't see a point to it myself. You may as well write a new interpreter/compiler from scratch. If you look at it, XMM / LMM is really a kind of interpreter also.
    If you really want XMM spin you'd probably be better off just compiling the spin to assembly and then running that in XMM. If you just want more space in HUB ram though you'd probably be better off just dynamically loading/unloading spin objects like I did with DOL. It still provides a lot of the advantages of being able to run programs that won't fit in the hub and it also doesn't need any extra bytecodes or changes to the interpreter although it does need a preprocessor (that I haven't gotten around to writing yet...)
  • Mike HuseltonMike Huselton Posts: 746
    edited 2009-06-03 17:16
    HollyMinkowski,

    Ask a simple question and let the floodgates open! As you can see, these are very knowledgeable and opinionated folks here.
    And friendly to boot. Welcome. I like your illo.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    JMH
  • HollyMinkowskiHollyMinkowski Posts: 1,398
    edited 2009-06-04 08:17
    @James Michael Huselton
    "As you can see, these are very knowledgeable and opinionated folks here"

    Yes indeed.
    Lots of very smart guys here, I hope I'm smart enough to keep up smile.gif
    Does the designer of the propeller interact with users here on this forum?

    @RossH
    "you are just shooting yourself in the foot - and that's what C++ was invented for!"

    hahahah smile.gif
    I like C...it's a very easy language to learn and use, but I have no use for C++.
    Asm is the simplest way to program controllers because each step is so elementary....but it takes a long time to write a big program.
    C is ideal for controllers since you can code so quickly... I usually code in C and add a bit of asm when I need max speed.
  • Ken PetersonKen Peterson Posts: 806
    edited 2009-06-04 17:25
    The problem with C is that it doesn't support the native architecture of the Propeller as well as SPIN does. C does have some features that are missing in SPIN, but to make the most of the advantages that the Propeller has to offer, it's easiest to do so in SPIN an Propeller assembly (PASM). I think you'd find that SPIN allows you to code quickly too, and learn quickly. The SPIN language isn't the most challenging thing to learn about the Propeller, it's the unique cog + hub architecture that you have to get your mind around.

    Ken

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
Sign In or Register to comment.