Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature proposal: VM-based functions (vmfun) #31

Open
3 tasks
jdpage opened this issue Jun 11, 2018 · 0 comments
Open
3 tasks

Feature proposal: VM-based functions (vmfun) #31

jdpage opened this issue Jun 11, 2018 · 0 comments

Comments

@jdpage
Copy link
Collaborator

jdpage commented Jun 11, 2018

Apparently one time-honored way of generating smaller 6502 code is to add a bytecode VM and implement some functions on the VM. This produces much smaller code which runs slower. One well-known example of this is Wozniak's SWEET16 for the Apple II. People have even written JVMs for the 6502.

I propose that we provide a way of indicating that some functions should be compiled to a VM which we ship as part of the runtime. The VM would only be included if the final image includes functions compiled for it. Based on previous work, a syntactic option would be vmfun, to go with fun and refun, but I could see that getting out of hand.

An additional option that could be included with this is to allow compilation to threaded form, i.e. instead of a bytecode stream, the compiler generates the jumps directly to the subroutines that implement the opcodes. This provides an intermediate size-speed tradeoff between machine code and bytecode. (This doesn't even require an extra target--just a different bytecode assembler.)

Pros:

  • Allows an avenue for programmers to optimize aggressively for size over speed, if that's what they need.
  • Removes some pressure on making every single language element maximally 6502-friendly at the cost of usability. (We already do this to some extent, since we support multiplication and division as infix operators, even though they'd actually be a call into the runtime.)
  • Forces us to make sure that the compiler is retargetable, since we'd effectively have two targets now.

Cons:

  • Compiler now has to support two targets properly, meaning increased testing surface area.
  • Understanding compiler output would require additional work.

Questions:

  • Are there any features that should be restricted to vmfuns, or should the language be the same regardless of function type?
  • Should we provide a way of compiling blocks into bytecode without creating a whole new function? If we do this, do we need a separate function type?
  • Do bytecode functions require a different calling convention?

Prior art:

  • SWEET16 -- A bytecode interpreter by the Woz which was included in the Apple II ROM. Sadly, it's Copyright Apple 1977, and would have been out of copyright by now except for RETROACTIVE COPYRIGHT EXTENSIONS. THANKS DISNEY.
  • VM02 -- A JVM for the 6502. No, I don't know how.
  • PLASMA -- A language for 6502 with similar goals to ours, actually. Actively developed, and by the same guy who did VM02 (above).
  • Not really prior art, but David A. Wheeler's 6502 Language Implementation Approaches is full of interesting and useful information. I'd already been considering some of the approaches described, so it's nice to have prior art to build on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant