r/ProgrammingLanguages • u/edgmnt_net • Apr 11 '23
Functional bytecode
I'm interested in whether work has been done to create a bytecode that is less imperative and more of a functional style. My hunch is such a bytecode may be more amenable to fast interpretation, since stuff like loops may be dispatched more directly to native code (instead of individual flow control ops). Has anyone seen anything like this? How annoying would it be for traditional languages to get translated into such a bytecode (does it require vectorization?)?
62
Upvotes
33
u/Linguistic-mystic Apr 11 '23
One example is the Spineless Tagless G-Machine from Simon Peyton Jones. It's a graph machine that proceeds by graph rewriting and is used for compiling/executing Haskell.
15
u/L8_4_Dinner (Ⓧ Ecstasy/XVM) Apr 11 '23
You're on the right thought path. Anything your language facilitates as a common path, can in theory be an op code for that language's IR. There's nothing wrong with ending up with a very high level IR that matches your language's idioms; it may actually be far easier to implement as an interpreter, and may allow (long term) for better optimizations when lowering via JIT.
Think of it this way: Each operation in your language could be an op code. You can basically have your highest level IR be "equivalent to" your AST level, and you don't ever need to lower it to a lower level IR if you don't want to. (If you're compiling down to native code, you're probably going to lower it first, but don't burn that bridge until you get to it.)
Regardless, don't be afraid to experiment. And come back and tell us what you've learned along the way!
29
u/Innf107 Apr 11 '23
Yes! There are a couple actually. Some of the simpler ones are probably the Krivine Machine and ZINC machine (which OCaml's bytecode is based on).
Alternatively, this is a really nice introduction to the G machine for lazy languages, and a less pretty but more rigorous introduction to the Spineless Tagless G Machine that GHC (Haskell) uses internally.
3
u/umlcat Apr 11 '23
But,
Bytecode / assembler code / Intermediate Representation Code are very simple instructions.
They aren't mean to be nested:
Functional Code are more complex instructions, meant to be nested:
Unless:
Just my two cryptocurrency coins contribution...
8
u/thmprover Apr 11 '23
You're looking for "abstract machines"; others have listed a number of them here, there's also the Categorical Abstract Machine (Ralf Hinze's review discusses compiling an ML-like language into CAM bytecode).
4
u/everything-narrative Apr 11 '23
A compositional/concatenative functional language is fairly simple to write bytecode for since it's basically just a stack machine.
3
u/nrnrnr Apr 12 '23
Look into “The ZINC Experiment” (technical report by Xavier Leroy). It describes an early version of the OCaml bytecode machine.
3
u/qqwy Apr 12 '23
I think that any bytecode that uses SSA is already in many ways functional.
Besides the G-machine, you might like looking at the internals of the Erlang VM(AKA the 'BEAM'). Its bytecode is also mostly functional although any kind of local recursion has been replaced by 'goto label' jumps (and remote calls either in 'call' or 'tailcall' instructions).