A Runtime for high-level languages on the 6502

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

User avatar
Bregalad
Posts: 8036
Joined: Fri Nov 12, 2004 2:49 pm
Location: Caen, France

Re: A Runtime for high-level languages on the 6502

Post by Bregalad »

psycopathicteen wrote:I'm still confused on what Bregalad said.
What I meant is that his example code could be changed from:

Code: Select all

        ; put $1234 on the stack
        lda #<$1234
        sta STACK0, x
        lda #>$1234
        sta STACK1, x

        ; put $1337 on the top of the stack
        lda #<$1337
        sta TOS
        lda #>$1337
        sta TOS+1

        jmp add2
To something like

Code: Select all

      jsr Pushconst
      .dw $1234
      jsr Pushconst
      .dw $1234
      jmp add2
Or even better, don't even use the "jsr" and have a bytecode interpreter, and then it would reduce to

Code: Select all

     .db PUSHCONST
     .dw $1234
     .db PUSHCONST
     .dw $1234
     .db ADD2
Where constants like PUSHCONST and ADD2 are single byte instructions that gets intepreated.
I imagine it would work like in Sweet16. Sweet16 is a virtual assembly language.
Note quite. Sweet16 is based on a register machine, not a stack machine.
russellsprouts
Posts: 53
Joined: Sun Jan 31, 2016 9:55 pm

Re: A Runtime for high-level languages on the 6502

Post by russellsprouts »

Yes, Sweet16 is a register machine, but it is similar in that it uses an interpreted byte code.

The reason I didn't go for byte code is because of speed -- an interpreter would have a lot of overhead, especially if the instructions are different lengths because of constant loads. I did experiment with making a fast interpreter using self-modifying code. An interpreter would also have to implement branching and subroutine call instructions, while direct code can just use the 6502 branching instructions.

It is possible to have the best of both worlds, however. An interpreter could be bolted onto a system like I described. It would read the byte code and translate it into calls to the stack machine functions. The compiler could selectively compile functions into either byte code or function calls. Apparently, the PLASMA language for 6502 already does this -- each function can be compiled to byte code, calls to the interpreter, or even directly to assembly, depending on how you want to trade-off speed and size.
Post Reply