[LLVMdev] Instructions on a target with no general purpose registers

Edwin Amsler edwinguy at gmail.com
Sun Jul 6 11:39:21 PDT 2014


So far, this is mostly a for-fun project as opposed to a production-grade compiler. If it somehow magically gets there, hurray. If I lose interest in it, no worries.

For an end goal, if I can write in C and produce native code, I’ll be happy (ideally no interpreter). Even if I need to inline literally everything to make my programs work. Current goal is to produce an assembler so I can hand-code stuff using the instructions in my handy C64 Programmer’s Reference Guide.

I have to admit I’m somewhat worried about function calls, the stack, and performance. Despite that, I’ll just keep plowing ahead until I bang into a wall somewhere. I enjoy learning something new, and since the whole Target backend is way different than anything I’ve worked with, I’m already pretty happy with where I’ve gotten and where this is going.

Now to specifics (inline):

On Jul 5, 2014, at 4:09 PM, David Given <dg at cowlark.com> wrote:

> ...
> However, if you squint hard enough the 6502 becomes a processor with 256
> byte-sized registers --- zero page. You may have better luck with a
> processor model which uses a subset of these as your registers, and
> possibly not telling LLVM about A, X and Y at all. Plus that gets you
> useful free stuff like indirect indexed, which you can use to implement
> *(addr+offset) for 8-bit offsets, and sneak a 16-bit add for free out of
> the processor.
> 

I had planed on implementing a register bank in zero page already (register definitions are already in place!), but hadn’t thought about outright hiding A, X, and Y. That’s a great plan. It shouldn’t be much work to see what I can try to optimize after that fact.

> ...
> 
> In another life I help look ater the ACK compiler suite, which is
> ancient and crufty and produces terrible code, but dates from about the
> era when the 6502 was still a thing and I've played with its 6502 code
> generator (and the 8080 code generator); these processors will basically
> not run modern languages acceptably. It's because modern languages are
> all stack-frame based, and these processors don't have any support for
> stack relative accesses.

Disappointing. 

> 
> On the Z80 you can just about fake it by using ix or iy as a user stack
> pointer, but it's painfully slow, painfully bloaty (8 bytes of code to
> read or write a 16-bit value from the stack frame!) and basically just
> painful. I believe later Z80s eventually gained a stack-relative load
> instruction, but I don't know if there was a corresponding store.
> 
> The 6502 isn't quite as bad. If your user stack pointer is in zero page,
> you can use indirect indexed to give access to a 256-byte stack frame,
> but it's not pretty:
> 
>  ldy #offset
>  lda (sp), y    # load high(?) byte
>  iny
>  ldx (sp), y    # load low byte
> 
> ...seven bytes, I think.
> 
> At least it's not as bad as the 8080.

That’s not a bad plan. I’m assuming though that this would only be needed when a 16 bit value is passed in by reference? Sorry I don’t have any experience with compilers, so I may be completely off here.

As for efficiency, the 6502 is pretty much garbage at handling anything given its age, so I can’t really see a way even with pure assembly to get it to do anything efficiently. Doing a 32bit add requires just a tonne of work, no matter what you’re doing. 40 years of progress can iron out a heck of a lot efficiency problems.

I probably need to go and read up on Sweet-16 to see how it handles things like large integers and the like.

> 
> Being able to forbid recursion and therefore map every stack frame
> statically into RAM would improve things no end, as now every stack slot
> has a compile-time known address; I remember asking here about this ages
> back and was told that there wasn't a built-in pass to do this, but it
> would be fairly easy to do. *shrug*

Getting ahead of me here, but it could be good times.

> Or you could just start using FORTRAN.

Cobol’s more my jam. :P

> 
> -- 
> ┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
> │ "Feminism encourages women to leave their husbands, kill their
> │ children, practice withcraft, destroy capitalism and become lesbians.”
> │ --- Rev. Pat Robertson
> 
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev





More information about the llvm-dev mailing list