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

Interesting but probably out-of-scope idea: Use copy-and-patch compilation #38

Open
sgraf812 opened this issue Mar 29, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@sgraf812
Copy link

From what I get, MicroHs uses bracket abstraction to break down Haskell into supercombinators.
The language of supercombinators can be seen as a form of parameterless bytecode IR; this IR is then interpreted in eval.c.

For nothing else than fun and profit (and for what else is this project :)), you could consider, instead of interpreting this bytecode, to compile it just in time.

This process can be done really fast, and yield quite fast code as well, if you precompile your supercombinators into a library of stencils at bootstrap time and then just copy/mmap them together for the particular bytecode IR at compiler run-time. Of course, these stencils will in general have holes in them for register/stack operands and constants which need to be patched-in by the compiler. This is the basis of copy-and-patch compilation, a work which you might enjoy. Python 3.13 recently shipped with such a JIT.

There apparently is a C++ library called MetaVar to generate and do AST pattern-matching for stencils using Clang and its GHC calling convention (which is attractive because MetaVar compiles stencils to CPS), but I haven't found it so far after a bit of Googling. This is probably a no-go for the stated objectives of your project anyway, since that is quite a heavy dependency footprint. Nevertheless, I thought you might find this avenue interesting.

@augustss
Copy link
Owner

I have indeed thought of some kind of JIT, which would treat a function body as a super-combinator and generate code for it. But you can do substantially better than copying a body for each of the combinators, and instead generate code that avoids a lot of the overhead associated with combinators.
Indeed, the very first functional language compiler I wrote (in 1981) used template copying, where there was a template for each super-combinator. The code did a memcpy of the template and then patched the wholes. So I'm familiar with the idea. :)
We will see where things go. I have many things on my TODO list.

Nitpick: MicroHs compiles to a fixed set of combinators, so it's not really what traditionally is called supercombinators.

@augustss augustss added enhancement New feature or request and removed enhancement New feature or request labels Mar 29, 2024
@augustss
Copy link
Owner

augustss commented Apr 1, 2024

I read the paper you linked to. That's a very interesting approach. I will keep that in mind if I ever decide to do a JIT. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants