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

Implement a basic macro-assembler for test cases. #155

Closed
wants to merge 2 commits into from
Closed

Conversation

sunfishcode
Copy link
Member

While WebAssembly's current AST has some high-level constructs like if, it doesn't have other obvious high-level control constructs such as while and for, making it difficult for humans to code by hand.

To address this concern, this PR introduces the start of a macro-assembler facility which allows tests to be written using high-level control constructs. So far, only while is implemented, but the basic idea is demonstrated: there is a fac.mast testcase which implements the common fibonacci function using a while loop, and the runtest.py script automatically runs the macro-assembler and generates a fac.generated.wast file, and then runs the test.

The macro-assembler preserves whitespace and comments in non-transformed parts of the code, so the output is fairly readable. With a modest amount of additional effort, it could be made to format the transformed parts in a pretty way as well.

@pjuftring
Copy link
Contributor

Awesome! Would it make sense to add a "replace"-macro, which replaces @ (or smth. else) with a list of strings? like this:

(replace ("foo" "bar")
  (invoke "@")
)

Gets translated to

(invoke "foo")
(invoke "bar")

@sunfishcode
Copy link
Member Author

Perhaps we could call it a "for each" macro or something. In any case, that kind of thing is certainly possible.

@pjuftring
Copy link
Contributor

Ok, I've written the basic implementation for this and will clean up the code (perhaps) tomorrow. When that's done, shall I make a PR for the branch? (I'm asking, because this would be a PR for a PR and therefore some kind of PR-ception, don't know, if this is intended use of PRs)

@pjuftring
Copy link
Contributor

I noticed a bug: If I rewrite a line in fac.mast from

    (while (i64.ne (get_local $i) (i64.const 0))

to

    (while
      (i64.ne (get_local $i) (i64.const 0))

It seems to add an "invisible" operand, which messes up the amount and position of operands, and the "while"-instruction doesn't get translated anymore.

EDIT: It's because Windows uses carriage return, disabled that in my notepad++

@sunfishcode
Copy link
Member Author

Friendly ping.

def isalnum(c):
return isdigit(c) or (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z')

def skip_whitespace_and_comments(s, i):
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the only function, in which you are checking if the index exceeds the length of the string. MASM still raises an IndexError, if the index is out-of-bounds in the other functions, but it could be hard to debug mast-files.

@sunfishcode
Copy link
Member Author

We want to make it as easy as possible to write conformance tests, because a strong testsuite is one of the main tools we have for ensuring that implementations behave consistently. A builtin macro-assembler layer might make it significantly easier to write some kinds of tests.

I'm not super attached to the actual code right now; it's pretty primitive right now. I'm mainly looking for feedback on the idea.

@rossberg
Copy link
Member

rossberg commented Nov 9, 2015

I'm rather impartial to this, but I fear it might blur the boundary of what is spec'ed for people trying to learn about the details of the language by looking at the examples from tests. Also, do we actually expect to manually write large tests in the future, instead of generating them?

Should we discuss at the meeting?

@sunfishcode
Copy link
Member Author

Closing for now, since it's not presently a high priority. This idea, with or without the code here, can be brought back if there is need of it.

@sunfishcode sunfishcode closed this Dec 2, 2015
@binji binji mentioned this pull request Jul 18, 2016
@sunfishcode sunfishcode deleted the masm branch August 9, 2016 19:54
rossberg pushed a commit that referenced this pull request Feb 11, 2021
This has already been updated in the upstream spec, but also needs to be
fixed in proposals.
ngzhian pushed a commit to ngzhian/spec that referenced this pull request Nov 4, 2021
This PR covers tests for some float literal introduced in
WebAssembly#1069, specific for following
ops:

Bit shifts
Bitwise operations
Boolean horizontal reductions
Conversions
Integer comparisons
Store

These tests are pulled from WAVM/WAVM#236
dhil pushed a commit to dhil/webassembly-spec that referenced this pull request Mar 2, 2023
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.

None yet

4 participants