0xPolygonMiden / miden-vm

STARK-based virtual machine
MIT License
611 stars 148 forks source link

feat: support absolute module/procedure paths #1363

Closed bitwalker closed 4 days ago

bitwalker commented 2 weeks ago

This PR does two things:

Additionally, it fixes some bugs with the current frontend around procedure paths and special namespaces.

When absolute paths are used, one does not need to explicitly import the module containing the referenced item, as there is no need for an import when the dependency is explicit.

NOTE: Due to uniformity of syntax representation in the parser, it is now possible to define procedure aliases for items referenced by MAST root. However, unless the module containing that root is provided during compilation (or the procedure is available in the procedure cache), then assembly will fail on these items. Additional refactoring will be required to support phantom references like this fully, as the lack of support at this time is due to the original assumptions of the assembler, i.e. that all code will be available during assembly.

Example

There is a test case in the assembler, but the gist is:

# foo.masm

export.foo
    add
end

# bar.masm

export.::foo::foo->bar

# main.masm

begin
    exec.::bar::bar
end

As you can see above, no imports are required, and the code compiles/behaves as if the correct imports were present. Both procedure aliases and procedure calls, support the new syntax.

Motivation

Supporting absolute paths means a compiler does not need to try and generate code with imports for all of the procedure calls in a given module, it can simply emit procedure calls in absolute form, and elide the imports entirely. Doing so made little sense anyway, as the assembler must resolve all callees to their absolute form during assembly anyway, so we would be doing unnecessary work for no reason.

This also reduces the importance of the MASM namespace/module system, since this PR also enables one to emit a module that can be parsed anonymously, with all calls specified absolutely (i.e. as a flat namespace).

greenhat commented 1 week ago

@bitwalker I want to use this branch to push get_inputs test further. Could you please rebase this branch on top of the current next so it includes the merged nop support introduced in #1360? Thanks!

bitwalker commented 1 week ago

@greenhat Sure thing!