[PicForth] Festive code generation bug

David McNab david at rebirthing.co.nz
Sun Dec 26 00:28:25 CET 2004


Alex Holden wrote:
> Merry Christmas everybody.
> 
> I've found another code generation bug, this time involving nested if 
> statements with 'exit' in them.

I've seen similar myself, but my patience kinda ran out.

PicForth is truly a gem, but alas its flaws can be subtle, deadly and 
unpredictable, a situation which has forced me to port the codebase for 
my commercial project to a C compiler.

Samuel - if you're reading this, can you please let us know where you're 
at with PicForth? Are you just having a break, or have you burned out 
and moved on to another project? If you've got that 'Oh geez, I'm sick 
of this' feeling with PicForth - it would be kinder to let us know, 
rather than allow people to waste their time.

I've had that feeling myself on a few of my earlier open source 
projects. It's nothing to be ashamed of - very understandable if 
incoming revenue/support/kudos is not matching outgoing effort. In such 
a scenario, the 'fun factor' can go out the window at any time. But 
users do deserve to know where they stand.

I'd be interested in taking on the maintenance of PicForth, except that 
I find about 70% of the core code totally unfathomable.

I'm working on a Forth compiler hosted on Python - currently it 
generates bytecodes for a Forth VM (which I'm using to execute code 
resident in off-chip I2C Serial EEPROM chips), but it wouldn't be too 
hard to add a backend which writes native PIC machine-code instead.

This exercise is proving way easier than hosting the compiler on Forth. 
With high-level Forth programming, I'm always stopping, scratching my 
head, and thinking of stack effects, 'how can I represent this entity', 
and diverting off for hours building suites of high-level words for 
handling the entities. But with Python, it's an absolute breeze.

What I'm saying here is that Forth is a questionable language for 
hosting high-level apps like compilers, even if the compiler is a Forth 
compiler.

There might be a case for reverse-engineering as much of PicForth as 
possible to run under a high-level language like Python, Ruby or even 
Java, so the codebase is readable to people other than the developer.

In either of these fine high-level OO languages, a Forth compiler could 
be implemented in stages:
  1. tokenise input stream, produce linear array of word tokens
  2. convert token array into an AST of nested code objects
  3. walk the tree, applying optimisation rules and bank-switch
     heuristics
  4. generate the assembly or binary code

-- 
Cheers
David


More information about the PicForth mailing list