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

An instruction to invoke opcodes on the stack #22

Closed
fmonett opened this issue May 22, 2017 · 1 comment
Closed

An instruction to invoke opcodes on the stack #22

fmonett opened this issue May 22, 2017 · 1 comment

Comments

@fmonett
Copy link

fmonett commented May 22, 2017

I had an idea that I've implemented (basically).
It is an opcode that uses the value from the top of stack as the opcode which is then executed.

Ex:
{ op: "push", val: 10 },
{ op: "push", val: 20 },
{ op: "push", val: 6 }, #ADD_I32 opcode
{ op: "invoke"},
{ op: "push", val: 3 },
{ op: "push", val: 8 }, #MUL_I32 opcode
{ op: "invoke"},
{ op: "ret"},

"ìnvoke" is the actual instruction. It "becomes" ADD_I32 in the first case and "MUL_I32" int he second. So the preceding code returns 90. ( (10+20)*3 )
This could be useful to implement "eval" like functions in languages that support it. Or allow to compile to a form of "dynamic code".
As of now only instructions that don't need arguments can be called this way, and there seems to be an issue with the "JUMP_STUB" opcode (it tries to update the opcode in memory). So not all opcodes make sense invoking dynamically.

Anyways.

@maximecb
Copy link
Member

Hi @fmonett,

This kind of an instruction is unfortunately not great for JIT compilation, because it makes it more difficult to generate efficient machine code. It would make dynamic execution less predictable.

But, worry not. We already have eval covered! The bytecode format is made of plain objects and arrays native to Zeta. If you create, in the plush language, objects that are "in the shape of a function", you can then call this as a normal function with the call opcode. You can dynamically generate new functions in memory. This is something that should already be possible with the parser in plush/parser.pls.

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

No branches or pull requests

2 participants