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

Rule prose with state variable z #89

Merged
merged 8 commits into from
Mar 28, 2024
Merged

Rule prose with state variable z #89

merged 8 commits into from
Mar 28, 2024

Conversation

jaehyun1ee
Copy link

This adds the state variable z to rule prose.

What has changed

Previously, we have removed all state variable z from the prose, for the store s in z is regarded as an implicit global variable throughout the prose semantics. This has incurred problems when rendering the prose, for the removed z caused arity mismatch on applying the display hints.

For example, below is the generated spec document before this PR. The prose calls $funcaddr with no argument, so the display hint, z.module.func is not applied. This happens throughout many helper function (getters and setters for notational shorthand) applications.
image

With this PR, the IL-2-AL phase does not remove the state variable z. Thus, display hints with z now works well in rendered prose also. For all generated prose from the reduction rules that use z, an instruction "Let z be the current state.` is added as the first instruction.
image

What should be addressed afterwards

Yet, the AL interpreter still works on the assumption that the state variable z is removed during IL-to-AL.
Thus, after generating AL from IL, the interpreter backend runs one last pass that removes the state variables, and the prose backend runs one last pass that adds the instruction "Let z be the current state."
The immediate result is that there is a slight difference between what is seen (prose backend) and what is executed (interpreter backend).

We plan to work on reconciling the two in a separate PR, i.e., adjust our AL interpreter so that we can get to execute the rendered prose by handling the state variable z somehow.

@jaehyun1ee jaehyun1ee requested a review from rossberg March 27, 2024 09:48
Copy link
Collaborator

@rossberg rossberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

As a separate change, the "ref.func ..." should probably be rendered with the parentheses.

Also, can we render |...| in prose, as "the length of ..."?

@jaehyun1ee
Copy link
Author

As a separate change, the "ref.func ..." should probably be rendered with the parentheses.

This is handled in the latest commit: Add parentheses to EL SeqE when translated from AL CaseE.

Also, can we render |...| in prose, as "the length of ..."?

This is somewhat complicated, for there are AL expressions in the form (I32.CONST |$arrayinst(z)[a].FIELDS|).

rule Step_read/array.len-array:
  z; (REF.ARRAY_ADDR a) ARRAY.LEN  ~>  (CONST I32 $(|$arrayinst(z)[a].FIELDS|))

It relates to the discussion made in #61, but I am not sure how the render_expr' should print this as a mixture of English and math formula.

Or, we can add a hacky pass on AL, such that if there is an occurrence of LenE used in CaseE, then we insert a LetI instruction that binds the CaseE to some VarE.
e.g.,

execution_of_ARRAY.LEN
1. Let z be the current state.
2. Assert: Due to validation, a value is on the top of the stack.
3. Pop the value admin_u0 from the stack.
4. If admin_u0 is of the case REF.NULL, then:
  a. Trap.
5. If admin_u0 is of the case REF.ARRAY_ADDR, then:
  a. Let (REF.ARRAY_ADDR a) be admin_u0.
  b. If (a < |$arrayinst(z)|), then:
    1) Let **foo** be the length of $arrayinst(z)[a].FIELDS.
    2) Push the value (I32.CONST **foo**) to the stack.

@rossberg
Copy link
Collaborator

rossberg commented Mar 28, 2024

Yeah, this is not high priority, but eventually I'd prefer to have something like that. (Of course, the condition should ideally read "a is smaller than the length of $arrayinst(z)" and be an assertion. :) )

@jaehyun1ee
Copy link
Author

Thank you for the comments :)

I'll merge this with two things in mind,
(1) Later we will reconcile the prose and interpreter backends to work on the same AL
(2) Properly render LenE as "the length of ...", either by a pass on AL or some smart way to introduce validation assertions

@jaehyun1ee jaehyun1ee merged commit e9a6385 into main Mar 28, 2024
5 checks passed
@rossberg rossberg deleted the prose-with-state branch March 28, 2024 10:13
@rossberg
Copy link
Collaborator

Yes, more generally, it should be possible to implement an AL-to-AL pass that performs a kind of normalisation, such that it lifts out complex subexpressions (of various shape) and names them. Sort of like a partial A-normal form.

Alan-Liang pushed a commit to Alan-Liang/spectec that referenced this pull request Nov 5, 2024
There is a small inconsistency between the reference implementation in Wasm-DSL#84 and the explainer. In the explainer we write that the type index `$ft` on `cont $ft` should be encoded as an `u32` in the binary format. However, the reference interpreter actually encodes it as `s33`, similarly to how concrete heap types are encoded.
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

Successfully merging this pull request may close these issues.

2 participants