Source added
This commit is contained in:
commit
b6297463a7
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/target
|
||||||
|
/test
|
||||||
|
Cargo.lock
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "wlausam"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
parity-wasm = "0.42.2"
|
621
LICENSE
Normal file
621
LICENSE
Normal file
@ -0,0 +1,621 @@
|
|||||||
|
GNU GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 29 June 2007
|
||||||
|
|
||||||
|
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The GNU General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
the GNU General Public License is intended to guarantee your freedom to
|
||||||
|
share and change all versions of a program--to make sure it remains free
|
||||||
|
software for all its users. We, the Free Software Foundation, use the
|
||||||
|
GNU General Public License for most of our software; it applies also to
|
||||||
|
any other work released this way by its authors. You can apply it to
|
||||||
|
your programs, too.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom, not
|
||||||
|
price. Our General Public Licenses are designed to make sure that you
|
||||||
|
have the freedom to distribute copies of free software (and charge for
|
||||||
|
them if you wish), that you receive source code or can get it if you
|
||||||
|
want it, that you can change the software or use pieces of it in new
|
||||||
|
free programs, and that you know you can do these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to prevent others from denying you
|
||||||
|
these rights or asking you to surrender the rights. Therefore, you have
|
||||||
|
certain responsibilities if you distribute copies of the software, or if
|
||||||
|
you modify it: responsibilities to respect the freedom of others.
|
||||||
|
|
||||||
|
For example, if you distribute copies of such a program, whether
|
||||||
|
gratis or for a fee, you must pass on to the recipients the same
|
||||||
|
freedoms that you received. You must make sure that they, too, receive
|
||||||
|
or can get the source code. And you must show them these terms so they
|
||||||
|
know their rights.
|
||||||
|
|
||||||
|
Developers that use the GNU GPL protect your rights with two steps:
|
||||||
|
(1) assert copyright on the software, and (2) offer you this License
|
||||||
|
giving you legal permission to copy, distribute and/or modify it.
|
||||||
|
|
||||||
|
For the developers' and authors' protection, the GPL clearly explains
|
||||||
|
that there is no warranty for this free software. For both users' and
|
||||||
|
authors' sake, the GPL requires that modified versions be marked as
|
||||||
|
changed, so that their problems will not be attributed erroneously to
|
||||||
|
authors of previous versions.
|
||||||
|
|
||||||
|
Some devices are designed to deny users access to install or run
|
||||||
|
modified versions of the software inside them, although the manufacturer
|
||||||
|
can do so. This is fundamentally incompatible with the aim of
|
||||||
|
protecting users' freedom to change the software. The systematic
|
||||||
|
pattern of such abuse occurs in the area of products for individuals to
|
||||||
|
use, which is precisely where it is most unacceptable. Therefore, we
|
||||||
|
have designed this version of the GPL to prohibit the practice for those
|
||||||
|
products. If such problems arise substantially in other domains, we
|
||||||
|
stand ready to extend this provision to those domains in future versions
|
||||||
|
of the GPL, as needed to protect the freedom of users.
|
||||||
|
|
||||||
|
Finally, every program is threatened constantly by software patents.
|
||||||
|
States should not allow patents to restrict development and use of
|
||||||
|
software on general-purpose computers, but in those that do, we wish to
|
||||||
|
avoid the special danger that patents applied to a free program could
|
||||||
|
make it effectively proprietary. To prevent this, the GPL assures that
|
||||||
|
patents cannot be used to render the program non-free.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow.
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
0. Definitions.
|
||||||
|
|
||||||
|
"This License" refers to version 3 of the GNU General Public License.
|
||||||
|
|
||||||
|
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||||
|
works, such as semiconductor masks.
|
||||||
|
|
||||||
|
"The Program" refers to any copyrightable work licensed under this
|
||||||
|
License. Each licensee is addressed as "you". "Licensees" and
|
||||||
|
"recipients" may be individuals or organizations.
|
||||||
|
|
||||||
|
To "modify" a work means to copy from or adapt all or part of the work
|
||||||
|
in a fashion requiring copyright permission, other than the making of an
|
||||||
|
exact copy. The resulting work is called a "modified version" of the
|
||||||
|
earlier work or a work "based on" the earlier work.
|
||||||
|
|
||||||
|
A "covered work" means either the unmodified Program or a work based
|
||||||
|
on the Program.
|
||||||
|
|
||||||
|
To "propagate" a work means to do anything with it that, without
|
||||||
|
permission, would make you directly or secondarily liable for
|
||||||
|
infringement under applicable copyright law, except executing it on a
|
||||||
|
computer or modifying a private copy. Propagation includes copying,
|
||||||
|
distribution (with or without modification), making available to the
|
||||||
|
public, and in some countries other activities as well.
|
||||||
|
|
||||||
|
To "convey" a work means any kind of propagation that enables other
|
||||||
|
parties to make or receive copies. Mere interaction with a user through
|
||||||
|
a computer network, with no transfer of a copy, is not conveying.
|
||||||
|
|
||||||
|
An interactive user interface displays "Appropriate Legal Notices"
|
||||||
|
to the extent that it includes a convenient and prominently visible
|
||||||
|
feature that (1) displays an appropriate copyright notice, and (2)
|
||||||
|
tells the user that there is no warranty for the work (except to the
|
||||||
|
extent that warranties are provided), that licensees may convey the
|
||||||
|
work under this License, and how to view a copy of this License. If
|
||||||
|
the interface presents a list of user commands or options, such as a
|
||||||
|
menu, a prominent item in the list meets this criterion.
|
||||||
|
|
||||||
|
1. Source Code.
|
||||||
|
|
||||||
|
The "source code" for a work means the preferred form of the work
|
||||||
|
for making modifications to it. "Object code" means any non-source
|
||||||
|
form of a work.
|
||||||
|
|
||||||
|
A "Standard Interface" means an interface that either is an official
|
||||||
|
standard defined by a recognized standards body, or, in the case of
|
||||||
|
interfaces specified for a particular programming language, one that
|
||||||
|
is widely used among developers working in that language.
|
||||||
|
|
||||||
|
The "System Libraries" of an executable work include anything, other
|
||||||
|
than the work as a whole, that (a) is included in the normal form of
|
||||||
|
packaging a Major Component, but which is not part of that Major
|
||||||
|
Component, and (b) serves only to enable use of the work with that
|
||||||
|
Major Component, or to implement a Standard Interface for which an
|
||||||
|
implementation is available to the public in source code form. A
|
||||||
|
"Major Component", in this context, means a major essential component
|
||||||
|
(kernel, window system, and so on) of the specific operating system
|
||||||
|
(if any) on which the executable work runs, or a compiler used to
|
||||||
|
produce the work, or an object code interpreter used to run it.
|
||||||
|
|
||||||
|
The "Corresponding Source" for a work in object code form means all
|
||||||
|
the source code needed to generate, install, and (for an executable
|
||||||
|
work) run the object code and to modify the work, including scripts to
|
||||||
|
control those activities. However, it does not include the work's
|
||||||
|
System Libraries, or general-purpose tools or generally available free
|
||||||
|
programs which are used unmodified in performing those activities but
|
||||||
|
which are not part of the work. For example, Corresponding Source
|
||||||
|
includes interface definition files associated with source files for
|
||||||
|
the work, and the source code for shared libraries and dynamically
|
||||||
|
linked subprograms that the work is specifically designed to require,
|
||||||
|
such as by intimate data communication or control flow between those
|
||||||
|
subprograms and other parts of the work.
|
||||||
|
|
||||||
|
The Corresponding Source need not include anything that users
|
||||||
|
can regenerate automatically from other parts of the Corresponding
|
||||||
|
Source.
|
||||||
|
|
||||||
|
The Corresponding Source for a work in source code form is that
|
||||||
|
same work.
|
||||||
|
|
||||||
|
2. Basic Permissions.
|
||||||
|
|
||||||
|
All rights granted under this License are granted for the term of
|
||||||
|
copyright on the Program, and are irrevocable provided the stated
|
||||||
|
conditions are met. This License explicitly affirms your unlimited
|
||||||
|
permission to run the unmodified Program. The output from running a
|
||||||
|
covered work is covered by this License only if the output, given its
|
||||||
|
content, constitutes a covered work. This License acknowledges your
|
||||||
|
rights of fair use or other equivalent, as provided by copyright law.
|
||||||
|
|
||||||
|
You may make, run and propagate covered works that you do not
|
||||||
|
convey, without conditions so long as your license otherwise remains
|
||||||
|
in force. You may convey covered works to others for the sole purpose
|
||||||
|
of having them make modifications exclusively for you, or provide you
|
||||||
|
with facilities for running those works, provided that you comply with
|
||||||
|
the terms of this License in conveying all material for which you do
|
||||||
|
not control copyright. Those thus making or running the covered works
|
||||||
|
for you must do so exclusively on your behalf, under your direction
|
||||||
|
and control, on terms that prohibit them from making any copies of
|
||||||
|
your copyrighted material outside their relationship with you.
|
||||||
|
|
||||||
|
Conveying under any other circumstances is permitted solely under
|
||||||
|
the conditions stated below. Sublicensing is not allowed; section 10
|
||||||
|
makes it unnecessary.
|
||||||
|
|
||||||
|
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||||
|
|
||||||
|
No covered work shall be deemed part of an effective technological
|
||||||
|
measure under any applicable law fulfilling obligations under article
|
||||||
|
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||||
|
similar laws prohibiting or restricting circumvention of such
|
||||||
|
measures.
|
||||||
|
|
||||||
|
When you convey a covered work, you waive any legal power to forbid
|
||||||
|
circumvention of technological measures to the extent such circumvention
|
||||||
|
is effected by exercising rights under this License with respect to
|
||||||
|
the covered work, and you disclaim any intention to limit operation or
|
||||||
|
modification of the work as a means of enforcing, against the work's
|
||||||
|
users, your or third parties' legal rights to forbid circumvention of
|
||||||
|
technological measures.
|
||||||
|
|
||||||
|
4. Conveying Verbatim Copies.
|
||||||
|
|
||||||
|
You may convey verbatim copies of the Program's source code as you
|
||||||
|
receive it, in any medium, provided that you conspicuously and
|
||||||
|
appropriately publish on each copy an appropriate copyright notice;
|
||||||
|
keep intact all notices stating that this License and any
|
||||||
|
non-permissive terms added in accord with section 7 apply to the code;
|
||||||
|
keep intact all notices of the absence of any warranty; and give all
|
||||||
|
recipients a copy of this License along with the Program.
|
||||||
|
|
||||||
|
You may charge any price or no price for each copy that you convey,
|
||||||
|
and you may offer support or warranty protection for a fee.
|
||||||
|
|
||||||
|
5. Conveying Modified Source Versions.
|
||||||
|
|
||||||
|
You may convey a work based on the Program, or the modifications to
|
||||||
|
produce it from the Program, in the form of source code under the
|
||||||
|
terms of section 4, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The work must carry prominent notices stating that you modified
|
||||||
|
it, and giving a relevant date.
|
||||||
|
|
||||||
|
b) The work must carry prominent notices stating that it is
|
||||||
|
released under this License and any conditions added under section
|
||||||
|
7. This requirement modifies the requirement in section 4 to
|
||||||
|
"keep intact all notices".
|
||||||
|
|
||||||
|
c) You must license the entire work, as a whole, under this
|
||||||
|
License to anyone who comes into possession of a copy. This
|
||||||
|
License will therefore apply, along with any applicable section 7
|
||||||
|
additional terms, to the whole of the work, and all its parts,
|
||||||
|
regardless of how they are packaged. This License gives no
|
||||||
|
permission to license the work in any other way, but it does not
|
||||||
|
invalidate such permission if you have separately received it.
|
||||||
|
|
||||||
|
d) If the work has interactive user interfaces, each must display
|
||||||
|
Appropriate Legal Notices; however, if the Program has interactive
|
||||||
|
interfaces that do not display Appropriate Legal Notices, your
|
||||||
|
work need not make them do so.
|
||||||
|
|
||||||
|
A compilation of a covered work with other separate and independent
|
||||||
|
works, which are not by their nature extensions of the covered work,
|
||||||
|
and which are not combined with it such as to form a larger program,
|
||||||
|
in or on a volume of a storage or distribution medium, is called an
|
||||||
|
"aggregate" if the compilation and its resulting copyright are not
|
||||||
|
used to limit the access or legal rights of the compilation's users
|
||||||
|
beyond what the individual works permit. Inclusion of a covered work
|
||||||
|
in an aggregate does not cause this License to apply to the other
|
||||||
|
parts of the aggregate.
|
||||||
|
|
||||||
|
6. Conveying Non-Source Forms.
|
||||||
|
|
||||||
|
You may convey a covered work in object code form under the terms
|
||||||
|
of sections 4 and 5, provided that you also convey the
|
||||||
|
machine-readable Corresponding Source under the terms of this License,
|
||||||
|
in one of these ways:
|
||||||
|
|
||||||
|
a) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by the
|
||||||
|
Corresponding Source fixed on a durable physical medium
|
||||||
|
customarily used for software interchange.
|
||||||
|
|
||||||
|
b) Convey the object code in, or embodied in, a physical product
|
||||||
|
(including a physical distribution medium), accompanied by a
|
||||||
|
written offer, valid for at least three years and valid for as
|
||||||
|
long as you offer spare parts or customer support for that product
|
||||||
|
model, to give anyone who possesses the object code either (1) a
|
||||||
|
copy of the Corresponding Source for all the software in the
|
||||||
|
product that is covered by this License, on a durable physical
|
||||||
|
medium customarily used for software interchange, for a price no
|
||||||
|
more than your reasonable cost of physically performing this
|
||||||
|
conveying of source, or (2) access to copy the
|
||||||
|
Corresponding Source from a network server at no charge.
|
||||||
|
|
||||||
|
c) Convey individual copies of the object code with a copy of the
|
||||||
|
written offer to provide the Corresponding Source. This
|
||||||
|
alternative is allowed only occasionally and noncommercially, and
|
||||||
|
only if you received the object code with such an offer, in accord
|
||||||
|
with subsection 6b.
|
||||||
|
|
||||||
|
d) Convey the object code by offering access from a designated
|
||||||
|
place (gratis or for a charge), and offer equivalent access to the
|
||||||
|
Corresponding Source in the same way through the same place at no
|
||||||
|
further charge. You need not require recipients to copy the
|
||||||
|
Corresponding Source along with the object code. If the place to
|
||||||
|
copy the object code is a network server, the Corresponding Source
|
||||||
|
may be on a different server (operated by you or a third party)
|
||||||
|
that supports equivalent copying facilities, provided you maintain
|
||||||
|
clear directions next to the object code saying where to find the
|
||||||
|
Corresponding Source. Regardless of what server hosts the
|
||||||
|
Corresponding Source, you remain obligated to ensure that it is
|
||||||
|
available for as long as needed to satisfy these requirements.
|
||||||
|
|
||||||
|
e) Convey the object code using peer-to-peer transmission, provided
|
||||||
|
you inform other peers where the object code and Corresponding
|
||||||
|
Source of the work are being offered to the general public at no
|
||||||
|
charge under subsection 6d.
|
||||||
|
|
||||||
|
A separable portion of the object code, whose source code is excluded
|
||||||
|
from the Corresponding Source as a System Library, need not be
|
||||||
|
included in conveying the object code work.
|
||||||
|
|
||||||
|
A "User Product" is either (1) a "consumer product", which means any
|
||||||
|
tangible personal property which is normally used for personal, family,
|
||||||
|
or household purposes, or (2) anything designed or sold for incorporation
|
||||||
|
into a dwelling. In determining whether a product is a consumer product,
|
||||||
|
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||||
|
product received by a particular user, "normally used" refers to a
|
||||||
|
typical or common use of that class of product, regardless of the status
|
||||||
|
of the particular user or of the way in which the particular user
|
||||||
|
actually uses, or expects or is expected to use, the product. A product
|
||||||
|
is a consumer product regardless of whether the product has substantial
|
||||||
|
commercial, industrial or non-consumer uses, unless such uses represent
|
||||||
|
the only significant mode of use of the product.
|
||||||
|
|
||||||
|
"Installation Information" for a User Product means any methods,
|
||||||
|
procedures, authorization keys, or other information required to install
|
||||||
|
and execute modified versions of a covered work in that User Product from
|
||||||
|
a modified version of its Corresponding Source. The information must
|
||||||
|
suffice to ensure that the continued functioning of the modified object
|
||||||
|
code is in no case prevented or interfered with solely because
|
||||||
|
modification has been made.
|
||||||
|
|
||||||
|
If you convey an object code work under this section in, or with, or
|
||||||
|
specifically for use in, a User Product, and the conveying occurs as
|
||||||
|
part of a transaction in which the right of possession and use of the
|
||||||
|
User Product is transferred to the recipient in perpetuity or for a
|
||||||
|
fixed term (regardless of how the transaction is characterized), the
|
||||||
|
Corresponding Source conveyed under this section must be accompanied
|
||||||
|
by the Installation Information. But this requirement does not apply
|
||||||
|
if neither you nor any third party retains the ability to install
|
||||||
|
modified object code on the User Product (for example, the work has
|
||||||
|
been installed in ROM).
|
||||||
|
|
||||||
|
The requirement to provide Installation Information does not include a
|
||||||
|
requirement to continue to provide support service, warranty, or updates
|
||||||
|
for a work that has been modified or installed by the recipient, or for
|
||||||
|
the User Product in which it has been modified or installed. Access to a
|
||||||
|
network may be denied when the modification itself materially and
|
||||||
|
adversely affects the operation of the network or violates the rules and
|
||||||
|
protocols for communication across the network.
|
||||||
|
|
||||||
|
Corresponding Source conveyed, and Installation Information provided,
|
||||||
|
in accord with this section must be in a format that is publicly
|
||||||
|
documented (and with an implementation available to the public in
|
||||||
|
source code form), and must require no special password or key for
|
||||||
|
unpacking, reading or copying.
|
||||||
|
|
||||||
|
7. Additional Terms.
|
||||||
|
|
||||||
|
"Additional permissions" are terms that supplement the terms of this
|
||||||
|
License by making exceptions from one or more of its conditions.
|
||||||
|
Additional permissions that are applicable to the entire Program shall
|
||||||
|
be treated as though they were included in this License, to the extent
|
||||||
|
that they are valid under applicable law. If additional permissions
|
||||||
|
apply only to part of the Program, that part may be used separately
|
||||||
|
under those permissions, but the entire Program remains governed by
|
||||||
|
this License without regard to the additional permissions.
|
||||||
|
|
||||||
|
When you convey a copy of a covered work, you may at your option
|
||||||
|
remove any additional permissions from that copy, or from any part of
|
||||||
|
it. (Additional permissions may be written to require their own
|
||||||
|
removal in certain cases when you modify the work.) You may place
|
||||||
|
additional permissions on material, added by you to a covered work,
|
||||||
|
for which you have or can give appropriate copyright permission.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, for material you
|
||||||
|
add to a covered work, you may (if authorized by the copyright holders of
|
||||||
|
that material) supplement the terms of this License with terms:
|
||||||
|
|
||||||
|
a) Disclaiming warranty or limiting liability differently from the
|
||||||
|
terms of sections 15 and 16 of this License; or
|
||||||
|
|
||||||
|
b) Requiring preservation of specified reasonable legal notices or
|
||||||
|
author attributions in that material or in the Appropriate Legal
|
||||||
|
Notices displayed by works containing it; or
|
||||||
|
|
||||||
|
c) Prohibiting misrepresentation of the origin of that material, or
|
||||||
|
requiring that modified versions of such material be marked in
|
||||||
|
reasonable ways as different from the original version; or
|
||||||
|
|
||||||
|
d) Limiting the use for publicity purposes of names of licensors or
|
||||||
|
authors of the material; or
|
||||||
|
|
||||||
|
e) Declining to grant rights under trademark law for use of some
|
||||||
|
trade names, trademarks, or service marks; or
|
||||||
|
|
||||||
|
f) Requiring indemnification of licensors and authors of that
|
||||||
|
material by anyone who conveys the material (or modified versions of
|
||||||
|
it) with contractual assumptions of liability to the recipient, for
|
||||||
|
any liability that these contractual assumptions directly impose on
|
||||||
|
those licensors and authors.
|
||||||
|
|
||||||
|
All other non-permissive additional terms are considered "further
|
||||||
|
restrictions" within the meaning of section 10. If the Program as you
|
||||||
|
received it, or any part of it, contains a notice stating that it is
|
||||||
|
governed by this License along with a term that is a further
|
||||||
|
restriction, you may remove that term. If a license document contains
|
||||||
|
a further restriction but permits relicensing or conveying under this
|
||||||
|
License, you may add to a covered work material governed by the terms
|
||||||
|
of that license document, provided that the further restriction does
|
||||||
|
not survive such relicensing or conveying.
|
||||||
|
|
||||||
|
If you add terms to a covered work in accord with this section, you
|
||||||
|
must place, in the relevant source files, a statement of the
|
||||||
|
additional terms that apply to those files, or a notice indicating
|
||||||
|
where to find the applicable terms.
|
||||||
|
|
||||||
|
Additional terms, permissive or non-permissive, may be stated in the
|
||||||
|
form of a separately written license, or stated as exceptions;
|
||||||
|
the above requirements apply either way.
|
||||||
|
|
||||||
|
8. Termination.
|
||||||
|
|
||||||
|
You may not propagate or modify a covered work except as expressly
|
||||||
|
provided under this License. Any attempt otherwise to propagate or
|
||||||
|
modify it is void, and will automatically terminate your rights under
|
||||||
|
this License (including any patent licenses granted under the third
|
||||||
|
paragraph of section 11).
|
||||||
|
|
||||||
|
However, if you cease all violation of this License, then your
|
||||||
|
license from a particular copyright holder is reinstated (a)
|
||||||
|
provisionally, unless and until the copyright holder explicitly and
|
||||||
|
finally terminates your license, and (b) permanently, if the copyright
|
||||||
|
holder fails to notify you of the violation by some reasonable means
|
||||||
|
prior to 60 days after the cessation.
|
||||||
|
|
||||||
|
Moreover, your license from a particular copyright holder is
|
||||||
|
reinstated permanently if the copyright holder notifies you of the
|
||||||
|
violation by some reasonable means, this is the first time you have
|
||||||
|
received notice of violation of this License (for any work) from that
|
||||||
|
copyright holder, and you cure the violation prior to 30 days after
|
||||||
|
your receipt of the notice.
|
||||||
|
|
||||||
|
Termination of your rights under this section does not terminate the
|
||||||
|
licenses of parties who have received copies or rights from you under
|
||||||
|
this License. If your rights have been terminated and not permanently
|
||||||
|
reinstated, you do not qualify to receive new licenses for the same
|
||||||
|
material under section 10.
|
||||||
|
|
||||||
|
9. Acceptance Not Required for Having Copies.
|
||||||
|
|
||||||
|
You are not required to accept this License in order to receive or
|
||||||
|
run a copy of the Program. Ancillary propagation of a covered work
|
||||||
|
occurring solely as a consequence of using peer-to-peer transmission
|
||||||
|
to receive a copy likewise does not require acceptance. However,
|
||||||
|
nothing other than this License grants you permission to propagate or
|
||||||
|
modify any covered work. These actions infringe copyright if you do
|
||||||
|
not accept this License. Therefore, by modifying or propagating a
|
||||||
|
covered work, you indicate your acceptance of this License to do so.
|
||||||
|
|
||||||
|
10. Automatic Licensing of Downstream Recipients.
|
||||||
|
|
||||||
|
Each time you convey a covered work, the recipient automatically
|
||||||
|
receives a license from the original licensors, to run, modify and
|
||||||
|
propagate that work, subject to this License. You are not responsible
|
||||||
|
for enforcing compliance by third parties with this License.
|
||||||
|
|
||||||
|
An "entity transaction" is a transaction transferring control of an
|
||||||
|
organization, or substantially all assets of one, or subdividing an
|
||||||
|
organization, or merging organizations. If propagation of a covered
|
||||||
|
work results from an entity transaction, each party to that
|
||||||
|
transaction who receives a copy of the work also receives whatever
|
||||||
|
licenses to the work the party's predecessor in interest had or could
|
||||||
|
give under the previous paragraph, plus a right to possession of the
|
||||||
|
Corresponding Source of the work from the predecessor in interest, if
|
||||||
|
the predecessor has it or can get it with reasonable efforts.
|
||||||
|
|
||||||
|
You may not impose any further restrictions on the exercise of the
|
||||||
|
rights granted or affirmed under this License. For example, you may
|
||||||
|
not impose a license fee, royalty, or other charge for exercise of
|
||||||
|
rights granted under this License, and you may not initiate litigation
|
||||||
|
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||||
|
any patent claim is infringed by making, using, selling, offering for
|
||||||
|
sale, or importing the Program or any portion of it.
|
||||||
|
|
||||||
|
11. Patents.
|
||||||
|
|
||||||
|
A "contributor" is a copyright holder who authorizes use under this
|
||||||
|
License of the Program or a work on which the Program is based. The
|
||||||
|
work thus licensed is called the contributor's "contributor version".
|
||||||
|
|
||||||
|
A contributor's "essential patent claims" are all patent claims
|
||||||
|
owned or controlled by the contributor, whether already acquired or
|
||||||
|
hereafter acquired, that would be infringed by some manner, permitted
|
||||||
|
by this License, of making, using, or selling its contributor version,
|
||||||
|
but do not include claims that would be infringed only as a
|
||||||
|
consequence of further modification of the contributor version. For
|
||||||
|
purposes of this definition, "control" includes the right to grant
|
||||||
|
patent sublicenses in a manner consistent with the requirements of
|
||||||
|
this License.
|
||||||
|
|
||||||
|
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||||
|
patent license under the contributor's essential patent claims, to
|
||||||
|
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||||
|
propagate the contents of its contributor version.
|
||||||
|
|
||||||
|
In the following three paragraphs, a "patent license" is any express
|
||||||
|
agreement or commitment, however denominated, not to enforce a patent
|
||||||
|
(such as an express permission to practice a patent or covenant not to
|
||||||
|
sue for patent infringement). To "grant" such a patent license to a
|
||||||
|
party means to make such an agreement or commitment not to enforce a
|
||||||
|
patent against the party.
|
||||||
|
|
||||||
|
If you convey a covered work, knowingly relying on a patent license,
|
||||||
|
and the Corresponding Source of the work is not available for anyone
|
||||||
|
to copy, free of charge and under the terms of this License, through a
|
||||||
|
publicly available network server or other readily accessible means,
|
||||||
|
then you must either (1) cause the Corresponding Source to be so
|
||||||
|
available, or (2) arrange to deprive yourself of the benefit of the
|
||||||
|
patent license for this particular work, or (3) arrange, in a manner
|
||||||
|
consistent with the requirements of this License, to extend the patent
|
||||||
|
license to downstream recipients. "Knowingly relying" means you have
|
||||||
|
actual knowledge that, but for the patent license, your conveying the
|
||||||
|
covered work in a country, or your recipient's use of the covered work
|
||||||
|
in a country, would infringe one or more identifiable patents in that
|
||||||
|
country that you have reason to believe are valid.
|
||||||
|
|
||||||
|
If, pursuant to or in connection with a single transaction or
|
||||||
|
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||||
|
covered work, and grant a patent license to some of the parties
|
||||||
|
receiving the covered work authorizing them to use, propagate, modify
|
||||||
|
or convey a specific copy of the covered work, then the patent license
|
||||||
|
you grant is automatically extended to all recipients of the covered
|
||||||
|
work and works based on it.
|
||||||
|
|
||||||
|
A patent license is "discriminatory" if it does not include within
|
||||||
|
the scope of its coverage, prohibits the exercise of, or is
|
||||||
|
conditioned on the non-exercise of one or more of the rights that are
|
||||||
|
specifically granted under this License. You may not convey a covered
|
||||||
|
work if you are a party to an arrangement with a third party that is
|
||||||
|
in the business of distributing software, under which you make payment
|
||||||
|
to the third party based on the extent of your activity of conveying
|
||||||
|
the work, and under which the third party grants, to any of the
|
||||||
|
parties who would receive the covered work from you, a discriminatory
|
||||||
|
patent license (a) in connection with copies of the covered work
|
||||||
|
conveyed by you (or copies made from those copies), or (b) primarily
|
||||||
|
for and in connection with specific products or compilations that
|
||||||
|
contain the covered work, unless you entered into that arrangement,
|
||||||
|
or that patent license was granted, prior to 28 March 2007.
|
||||||
|
|
||||||
|
Nothing in this License shall be construed as excluding or limiting
|
||||||
|
any implied license or other defenses to infringement that may
|
||||||
|
otherwise be available to you under applicable patent law.
|
||||||
|
|
||||||
|
12. No Surrender of Others' Freedom.
|
||||||
|
|
||||||
|
If conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot convey a
|
||||||
|
covered work so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you may
|
||||||
|
not convey it at all. For example, if you agree to terms that obligate you
|
||||||
|
to collect a royalty for further conveying from those to whom you convey
|
||||||
|
the Program, the only way you could satisfy both those terms and this
|
||||||
|
License would be to refrain entirely from conveying the Program.
|
||||||
|
|
||||||
|
13. Use with the GNU Affero General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, you have
|
||||||
|
permission to link or combine any covered work with a work licensed
|
||||||
|
under version 3 of the GNU Affero General Public License into a single
|
||||||
|
combined work, and to convey the resulting work. The terms of this
|
||||||
|
License will continue to apply to the part which is the covered work,
|
||||||
|
but the special requirements of the GNU Affero General Public License,
|
||||||
|
section 13, concerning interaction through a network will apply to the
|
||||||
|
combination as such.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU General Public License from time to time. Such new versions will
|
||||||
|
be similar in spirit to the present version, but may differ in detail to
|
||||||
|
address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the
|
||||||
|
Program specifies that a certain numbered version of the GNU General
|
||||||
|
Public License "or any later version" applies to it, you have the
|
||||||
|
option of following the terms and conditions either of that numbered
|
||||||
|
version or of any later version published by the Free Software
|
||||||
|
Foundation. If the Program does not specify a version number of the
|
||||||
|
GNU General Public License, you may choose any version ever published
|
||||||
|
by the Free Software Foundation.
|
||||||
|
|
||||||
|
If the Program specifies that a proxy can decide which future
|
||||||
|
versions of the GNU General Public License can be used, that proxy's
|
||||||
|
public statement of acceptance of a version permanently authorizes you
|
||||||
|
to choose that version for the Program.
|
||||||
|
|
||||||
|
Later license versions may give you additional or different
|
||||||
|
permissions. However, no additional obligations are imposed on any
|
||||||
|
author or copyright holder as a result of your choosing to follow a
|
||||||
|
later version.
|
||||||
|
|
||||||
|
15. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||||
|
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||||
|
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||||
|
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||||
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||||
|
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||||
|
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. Limitation of Liability.
|
||||||
|
|
||||||
|
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||||
|
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||||
|
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||||
|
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||||
|
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||||
|
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||||
|
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||||
|
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||||
|
SUCH DAMAGES.
|
||||||
|
|
||||||
|
17. Interpretation of Sections 15 and 16.
|
||||||
|
|
||||||
|
If the disclaimer of warranty and limitation of liability provided
|
||||||
|
above cannot be given local legal effect according to their terms,
|
||||||
|
reviewing courts shall apply local law that most closely approximates
|
||||||
|
an absolute waiver of all civil liability in connection with the
|
||||||
|
Program, unless a warranty or assumption of liability accompanies a
|
||||||
|
copy of the Program in return for a fee.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
3
README.md
Normal file
3
README.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
This is a WIP (read: absolutely not ready for serious work) tool for translating WebAssembly into Lua. Support is specifically for LuaJIT, with the secondary objective being Lua 5.4 and Roblox's Luau.
|
||||||
|
|
||||||
|
The instructions are minimal; run the program as `program <edition> <file...>` to generate the translations into stdout. Editions currently supported are `LuaJIT` and `Luau`.
|
158
runtime/luajit.lua
Normal file
158
runtime/luajit.lua
Normal file
@ -0,0 +1,158 @@
|
|||||||
|
local bit = require('bit')
|
||||||
|
local ffi = require('ffi')
|
||||||
|
|
||||||
|
local unsign_i64 = ffi.typeof('uint64_t')
|
||||||
|
|
||||||
|
local div = {}
|
||||||
|
|
||||||
|
local le = {}
|
||||||
|
local lt = {}
|
||||||
|
local ge = {}
|
||||||
|
local gt = {}
|
||||||
|
|
||||||
|
local band = {}
|
||||||
|
local bor = {}
|
||||||
|
local bxor = {}
|
||||||
|
local bnot = {}
|
||||||
|
|
||||||
|
local shl = {}
|
||||||
|
local shr = {}
|
||||||
|
|
||||||
|
local extend = {}
|
||||||
|
local wrap = {}
|
||||||
|
|
||||||
|
local load = {}
|
||||||
|
local store = {}
|
||||||
|
|
||||||
|
-- Helper functions
|
||||||
|
local function unsign_i32(x)
|
||||||
|
if x < 0 then x = x + 0x100000000 end
|
||||||
|
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
|
local function rip_u64(x) return bit.band(bit.rshift(x, 32), 0xFFFFFFFF), bit.band(x, 0xFFFFFFFF) end
|
||||||
|
|
||||||
|
local function merge_u64(hi, lo) return bit.bor(bit.lshift(hi, 32), lo) end
|
||||||
|
|
||||||
|
-- Runtime functions
|
||||||
|
local function grow_page_num(memory, num)
|
||||||
|
local old = memory.min
|
||||||
|
local new = old + num
|
||||||
|
|
||||||
|
if memory.max and new > memory.max then
|
||||||
|
return -1
|
||||||
|
else
|
||||||
|
memory.min = new
|
||||||
|
|
||||||
|
return old
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function div.i32(lhs, rhs)
|
||||||
|
if rhs == 0 then error('division by zero') end
|
||||||
|
|
||||||
|
return math.floor(lhs / rhs)
|
||||||
|
end
|
||||||
|
|
||||||
|
function le.u32(lhs, rhs) return unsign_i32(lhs) <= unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function lt.u32(lhs, rhs) return unsign_i32(lhs) < unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function ge.u32(lhs, rhs) return unsign_i32(lhs) >= unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function gt.u32(lhs, rhs) return unsign_i32(lhs) > unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function le.u64(lhs, rhs) return unsign_i64(lhs) <= unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function lt.u64(lhs, rhs) return unsign_i64(lhs) < unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function ge.u64(lhs, rhs) return unsign_i64(lhs) >= unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function gt.u64(lhs, rhs) return unsign_i64(lhs) > unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
band.i32 = bit.band
|
||||||
|
bor.i32 = bit.bor
|
||||||
|
bxor.i32 = bit.bxor
|
||||||
|
bnot.i32 = bit.bnot
|
||||||
|
band.i64 = bit.band
|
||||||
|
bor.i64 = bit.bor
|
||||||
|
bxor.i64 = bit.bxor
|
||||||
|
bnot.i64 = bit.bnot
|
||||||
|
|
||||||
|
shl.u32 = bit.lshift
|
||||||
|
shr.u32 = bit.rshift
|
||||||
|
shl.i32 = bit.lshift
|
||||||
|
shr.i32 = bit.rshift
|
||||||
|
shl.u64 = bit.lshift
|
||||||
|
shr.u64 = bit.rshift
|
||||||
|
shl.i64 = bit.lshift
|
||||||
|
shr.i64 = bit.rshift
|
||||||
|
|
||||||
|
extend.i32_u64 = ffi.typeof('int64_t')
|
||||||
|
|
||||||
|
wrap.i64_i32 = ffi.typeof('int32_t')
|
||||||
|
|
||||||
|
function load.i32(memory, addr)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned read') end
|
||||||
|
|
||||||
|
return memory.data[addr / 4] or 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function load.i32_u8(memory, addr)
|
||||||
|
local value = load.i32(memory, addr)
|
||||||
|
|
||||||
|
return bit.band(value, 0xFF)
|
||||||
|
end
|
||||||
|
|
||||||
|
function load.i64(memory, addr)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned read') end
|
||||||
|
|
||||||
|
local hi = memory.data[addr / 4 + 1] or 0
|
||||||
|
local lo = memory.data[addr / 4] or 0
|
||||||
|
|
||||||
|
return merge_u64(hi, lo)
|
||||||
|
end
|
||||||
|
|
||||||
|
function store.i32(memory, addr, value)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned write') end
|
||||||
|
|
||||||
|
memory.data[addr / 4] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
function store.i32_n8(memory, addr, value)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned write') end
|
||||||
|
|
||||||
|
local old = bit.band(memory.data[addr / 4] or 0, 0xFFFFFF00)
|
||||||
|
local new = bit.band(value, 0xFF)
|
||||||
|
|
||||||
|
memory.data[addr / 4] = bit.bor(old, new)
|
||||||
|
end
|
||||||
|
|
||||||
|
function store.i64(memory, addr, value)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned write') end
|
||||||
|
|
||||||
|
local hi, lo = rip_u64(value)
|
||||||
|
|
||||||
|
memory.data[addr / 4] = lo
|
||||||
|
memory.data[addr / 4 + 1] = hi
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
grow_page_num = grow_page_num,
|
||||||
|
div = div,
|
||||||
|
le = le,
|
||||||
|
lt = lt,
|
||||||
|
ge = ge,
|
||||||
|
gt = gt,
|
||||||
|
band = band,
|
||||||
|
bor = bor,
|
||||||
|
bxor = bxor,
|
||||||
|
bnot = bnot,
|
||||||
|
shl = shl,
|
||||||
|
shr = shr,
|
||||||
|
extend = extend,
|
||||||
|
wrap = wrap,
|
||||||
|
load = load,
|
||||||
|
store = store,
|
||||||
|
}
|
161
runtime/luau.lua
Normal file
161
runtime/luau.lua
Normal file
@ -0,0 +1,161 @@
|
|||||||
|
local div = {}
|
||||||
|
|
||||||
|
local le = {}
|
||||||
|
local lt = {}
|
||||||
|
local ge = {}
|
||||||
|
local gt = {}
|
||||||
|
|
||||||
|
local band = {}
|
||||||
|
local bor = {}
|
||||||
|
local bxor = {}
|
||||||
|
local bnot = {}
|
||||||
|
|
||||||
|
local shl = {}
|
||||||
|
local shr = {}
|
||||||
|
|
||||||
|
local extend = {}
|
||||||
|
local wrap = {}
|
||||||
|
|
||||||
|
local load = {}
|
||||||
|
local store = {}
|
||||||
|
|
||||||
|
-- Helper functions
|
||||||
|
local function no_op(x) return x end
|
||||||
|
|
||||||
|
local function unsign_i32(x)
|
||||||
|
if x < 0 then x = x + 0x100000000 end
|
||||||
|
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
|
local function unsign_i64(x)
|
||||||
|
if x < 0 then x = x + 0x10000000000000000 end
|
||||||
|
|
||||||
|
return x
|
||||||
|
end
|
||||||
|
|
||||||
|
local function rip_u64(x) return math.floor(x / 0x100000000), bit.tobit(x % 0x100000000) end
|
||||||
|
|
||||||
|
local function merge_u64(hi, lo) return hi * 0x100000000 + lo end
|
||||||
|
|
||||||
|
-- Runtime functions
|
||||||
|
local function grow_page_num(memory, num)
|
||||||
|
local old = memory.min
|
||||||
|
local new = old + num
|
||||||
|
|
||||||
|
if memory.max and new > memory.max then
|
||||||
|
return -1
|
||||||
|
else
|
||||||
|
memory.min = new
|
||||||
|
|
||||||
|
return old
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function div.i32(lhs, rhs)
|
||||||
|
if rhs == 0 then error('division by zero') end
|
||||||
|
|
||||||
|
return math.floor(lhs / rhs)
|
||||||
|
end
|
||||||
|
|
||||||
|
function le.u32(lhs, rhs) return unsign_i32(lhs) <= unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function lt.u32(lhs, rhs) return unsign_i32(lhs) < unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function ge.u32(lhs, rhs) return unsign_i32(lhs) >= unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function gt.u32(lhs, rhs) return unsign_i32(lhs) > unsign_i32(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function le.u64(lhs, rhs) return unsign_i64(lhs) <= unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function lt.u64(lhs, rhs) return unsign_i64(lhs) < unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function ge.u64(lhs, rhs) return unsign_i64(lhs) >= unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
function gt.u64(lhs, rhs) return unsign_i64(lhs) > unsign_i64(rhs) and 1 or 0 end
|
||||||
|
|
||||||
|
band.i32 = bit.band
|
||||||
|
bor.i32 = bit.bor
|
||||||
|
bxor.i32 = bit.bxor
|
||||||
|
bnot.i32 = bit.bnot
|
||||||
|
band.i64 = bit.band
|
||||||
|
bor.i64 = bit.bor
|
||||||
|
bxor.i64 = bit.bxor
|
||||||
|
bnot.i64 = bit.bnot
|
||||||
|
|
||||||
|
shl.u32 = bit.lshift
|
||||||
|
shr.u32 = bit.rshift
|
||||||
|
shl.i32 = bit.lshift
|
||||||
|
shr.i32 = bit.rshift
|
||||||
|
shl.u64 = bit.lshift
|
||||||
|
shr.u64 = bit.rshift
|
||||||
|
shl.i64 = bit.lshift
|
||||||
|
shr.i64 = bit.rshift
|
||||||
|
|
||||||
|
extend.i32_u64 = no_op
|
||||||
|
|
||||||
|
function wrap.i64_i32(i) return i % 2 ^ 32 end
|
||||||
|
|
||||||
|
function load.i32(memory, addr)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned read') end
|
||||||
|
|
||||||
|
return memory.data[addr / 4] or 0
|
||||||
|
end
|
||||||
|
|
||||||
|
function load.i32_u8(memory, addr)
|
||||||
|
local value = load.i32(memory, addr)
|
||||||
|
|
||||||
|
return bit.band(value, 0xFF)
|
||||||
|
end
|
||||||
|
|
||||||
|
function load.i64(memory, addr)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned read') end
|
||||||
|
|
||||||
|
local hi = memory.data[addr / 4 + 1] or 0
|
||||||
|
local lo = memory.data[addr / 4] or 0
|
||||||
|
|
||||||
|
return merge_u64(hi, lo)
|
||||||
|
end
|
||||||
|
|
||||||
|
function store.i32(memory, addr, value)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned write') end
|
||||||
|
|
||||||
|
memory.data[addr / 4] = value
|
||||||
|
end
|
||||||
|
|
||||||
|
function store.i32_n8(memory, addr, value)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned write') end
|
||||||
|
|
||||||
|
local old = bit.band(memory.data[addr / 4] or 0, 0xFFFFFF00)
|
||||||
|
local new = bit.band(value, 0xFF)
|
||||||
|
|
||||||
|
memory.data[addr / 4] = bit.bor(old, new)
|
||||||
|
end
|
||||||
|
|
||||||
|
function store.i64(memory, addr, value)
|
||||||
|
if addr % 4 ~= 0 then error('unaligned write') end
|
||||||
|
|
||||||
|
local hi, lo = rip_u64(value)
|
||||||
|
|
||||||
|
memory.data[addr / 4] = lo
|
||||||
|
memory.data[addr / 4 + 1] = hi
|
||||||
|
end
|
||||||
|
|
||||||
|
return {
|
||||||
|
grow_page_num = grow_page_num,
|
||||||
|
div = div,
|
||||||
|
le = le,
|
||||||
|
lt = lt,
|
||||||
|
ge = ge,
|
||||||
|
gt = gt,
|
||||||
|
band = band,
|
||||||
|
bor = bor,
|
||||||
|
bxor = bxor,
|
||||||
|
bnot = bnot,
|
||||||
|
shl = shl,
|
||||||
|
shr = shr,
|
||||||
|
extend = extend,
|
||||||
|
wrap = wrap,
|
||||||
|
load = load,
|
||||||
|
store = store,
|
||||||
|
}
|
1
rustfmt.toml
Executable file
1
rustfmt.toml
Executable file
@ -0,0 +1 @@
|
|||||||
|
hard_tabs = true
|
153
src/backend/helper/edition.rs
Executable file
153
src/backend/helper/edition.rs
Executable file
@ -0,0 +1,153 @@
|
|||||||
|
use super::writer::Writer;
|
||||||
|
use std::{fmt::Display, io::Result};
|
||||||
|
|
||||||
|
pub struct Infix<T> {
|
||||||
|
rhs: &'static str,
|
||||||
|
inner: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Display for Infix<T>
|
||||||
|
where
|
||||||
|
T: Display,
|
||||||
|
{
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.inner.fmt(f)?;
|
||||||
|
self.rhs.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Edition {
|
||||||
|
fn runtime(&self) -> &'static str;
|
||||||
|
|
||||||
|
fn start_block(&self, w: Writer) -> Result<()>;
|
||||||
|
fn start_loop(&self, level: usize, w: Writer) -> Result<()>;
|
||||||
|
fn start_if(&self, cond: &str, w: Writer) -> Result<()>;
|
||||||
|
fn end_block(&self, level: usize, w: Writer) -> Result<()>;
|
||||||
|
fn end_loop(&self, w: Writer) -> Result<()>;
|
||||||
|
fn end_if(&self, level: usize, w: Writer) -> Result<()>;
|
||||||
|
|
||||||
|
fn br_target(&self, level: usize, in_loop: bool, w: Writer) -> Result<()>;
|
||||||
|
fn br_to_level(&self, level: usize, up: usize, is_loop: bool, w: Writer) -> Result<()>;
|
||||||
|
|
||||||
|
fn i64(&self, i: i64) -> Infix<i64>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct LuaJIT;
|
||||||
|
|
||||||
|
impl Edition for LuaJIT {
|
||||||
|
fn runtime(&self) -> &'static str {
|
||||||
|
"luajit"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_block(&self, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "do")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_loop(&self, level: usize, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "do")?;
|
||||||
|
writeln!(w, "::continue_at_{}::", level)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_if(&self, cond: &str, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "if {} ~= 0 then", cond)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_block(&self, level: usize, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "::continue_at_{}::", level)?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_loop(&self, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_if(&self, level: usize, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "::continue_at_{}::", level)?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn br_target(&self, _level: usize, _in_loop: bool, _w: Writer) -> Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn br_to_level(&self, level: usize, up: usize, _is_loop: bool, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "goto continue_at_{}", level - up)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn i64(&self, i: i64) -> Infix<i64> {
|
||||||
|
Infix {
|
||||||
|
rhs: "LL",
|
||||||
|
inner: i,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Luau;
|
||||||
|
|
||||||
|
impl Edition for Luau {
|
||||||
|
fn runtime(&self) -> &'static str {
|
||||||
|
"luau"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_block(&self, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "while true do")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_loop(&self, _level: usize, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "while true do")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn start_if(&self, cond: &str, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "while true do")?;
|
||||||
|
writeln!(w, "if {} ~= 0 then", cond)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_block(&self, _level: usize, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "break")?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_loop(&self, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "break")?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn end_if(&self, _level: usize, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "end")?;
|
||||||
|
writeln!(w, "break")?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn br_target(&self, level: usize, in_loop: bool, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "if desired then")?;
|
||||||
|
writeln!(w, "if desired == {} then", level)?;
|
||||||
|
writeln!(w, "desired = nil")?;
|
||||||
|
|
||||||
|
if in_loop {
|
||||||
|
writeln!(w, "continue")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, "end")?;
|
||||||
|
writeln!(w, "break")?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn br_to_level(&self, level: usize, up: usize, is_loop: bool, w: Writer) -> Result<()> {
|
||||||
|
if up == 0 {
|
||||||
|
if is_loop {
|
||||||
|
writeln!(w, "continue")?;
|
||||||
|
} else {
|
||||||
|
writeln!(w, "break")?;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
writeln!(w, "desired = {}", level - up)?;
|
||||||
|
writeln!(w, "break")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn i64(&self, i: i64) -> Infix<i64> {
|
||||||
|
Infix { rhs: "", inner: i }
|
||||||
|
}
|
||||||
|
}
|
3
src/backend/helper/mod.rs
Normal file
3
src/backend/helper/mod.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
pub mod edition;
|
||||||
|
pub mod register;
|
||||||
|
pub mod writer;
|
41
src/backend/helper/register.rs
Executable file
41
src/backend/helper/register.rs
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
pub struct Register {
|
||||||
|
pub last: u32,
|
||||||
|
pub inner: u32,
|
||||||
|
saved: Vec<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Register {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
last: 0,
|
||||||
|
inner: 0,
|
||||||
|
saved: vec![0],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extend(&mut self) {
|
||||||
|
self.last = self.last.max(self.inner);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&mut self) {
|
||||||
|
self.saved.push(self.inner);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load(&mut self) {
|
||||||
|
self.inner = self.saved.pop().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn push(&mut self, n: u32) -> u32 {
|
||||||
|
let prev = self.inner;
|
||||||
|
|
||||||
|
self.inner = self.inner.checked_add(n).unwrap();
|
||||||
|
self.extend();
|
||||||
|
|
||||||
|
prev
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pop(&mut self, n: u32) -> u32 {
|
||||||
|
self.inner = self.inner.checked_sub(n).unwrap();
|
||||||
|
self.inner
|
||||||
|
}
|
||||||
|
}
|
17
src/backend/helper/writer.rs
Normal file
17
src/backend/helper/writer.rs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
use std::io::{Result, Write};
|
||||||
|
|
||||||
|
pub type Writer<'a> = &'a mut dyn Write;
|
||||||
|
|
||||||
|
pub fn ordered_iter(prefix: &'static str, end: u32) -> impl Iterator<Item = String> {
|
||||||
|
(1..=end).map(move |i| format!("{}_{}", prefix, i))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_ordered(prefix: &'static str, end: u32, w: Writer) -> Result<()> {
|
||||||
|
let mut iter = ordered_iter(prefix, end);
|
||||||
|
|
||||||
|
if let Some(s) = iter.next() {
|
||||||
|
write!(w, "{}", s)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
iter.try_for_each(|s| write!(w, ", {}", s))
|
||||||
|
}
|
2
src/backend/mod.rs
Normal file
2
src/backend/mod.rs
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
pub mod helper;
|
||||||
|
pub mod translation;
|
458
src/backend/translation/level_1.rs
Normal file
458
src/backend/translation/level_1.rs
Normal file
@ -0,0 +1,458 @@
|
|||||||
|
use super::level_2::list_to_range;
|
||||||
|
use crate::{
|
||||||
|
backend::helper::{edition::Edition, register::Register, writer::Writer},
|
||||||
|
data::{Arity, Code, Module},
|
||||||
|
};
|
||||||
|
use parity_wasm::elements::{BrTableData, Instruction};
|
||||||
|
use std::{fmt::Display, io::Result};
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
pub enum Label {
|
||||||
|
Block,
|
||||||
|
If,
|
||||||
|
Loop,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Body<'a> {
|
||||||
|
spec: &'a dyn Edition,
|
||||||
|
label_list: Vec<Label>,
|
||||||
|
pub table_list: Vec<BrTableData>,
|
||||||
|
pub reg: Register,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Body<'a> {
|
||||||
|
pub fn new(spec: &'a dyn Edition) -> Self {
|
||||||
|
Self {
|
||||||
|
spec,
|
||||||
|
label_list: vec![],
|
||||||
|
table_list: Vec::new(),
|
||||||
|
reg: Register::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gen(&mut self, index: usize, module: &Module) -> Result<Vec<u8>> {
|
||||||
|
let mut w = Vec::new();
|
||||||
|
|
||||||
|
module.code[index]
|
||||||
|
.inst_list
|
||||||
|
.iter()
|
||||||
|
.try_for_each(|v| self.gen_inst(index, v, module, &mut w))?;
|
||||||
|
|
||||||
|
Ok(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_jump(&mut self, up: u32, w: Writer) -> Result<()> {
|
||||||
|
let up = up as usize;
|
||||||
|
let level = self.label_list.len() - 1;
|
||||||
|
let is_loop = self.label_list[level - up] == Label::Loop;
|
||||||
|
|
||||||
|
self.spec.br_to_level(level, up, is_loop, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_br_if(&mut self, i: u32, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let cond = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
writeln!(w, "if {} ~= 0 then", cond)?;
|
||||||
|
self.gen_jump(i, w)?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_br_table(&mut self, data: &BrTableData, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let case = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
for (r, t) in list_to_range(&data.table) {
|
||||||
|
if r.len() == 1 {
|
||||||
|
writeln!(w, "if {} == {} then", case, r.start)?;
|
||||||
|
} else {
|
||||||
|
writeln!(w, "if {0} >= {1} and {0} <= {2} then", case, r.start, r.end)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.gen_jump(t, w)?;
|
||||||
|
write!(w, "else")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w)?;
|
||||||
|
self.gen_jump(data.default, w)?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_load(&mut self, t: &str, o: u32, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let reg = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{0} = load.{1}(MEMORY_LIST[0], {0} + {2})", reg, t, o)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_store(&mut self, t: &str, o: u32, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let val = f.var_name_of(self.reg.pop(1));
|
||||||
|
let reg = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
writeln!(w, "store.{}(MEMORY_LIST[0], {} + {}, {})", t, reg, o, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_const<T: Display>(&mut self, val: T, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let reg = f.var_name_of(self.reg.push(1));
|
||||||
|
|
||||||
|
writeln!(w, "{} = {}", reg, val)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_compare(&mut self, op: &str, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let rhs = f.var_name_of(self.reg.pop(1));
|
||||||
|
let lhs = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{1} = {1} {0} {2} and 1 or 0", op, lhs, rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_unop_ex(&mut self, op: &str, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let reg = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{1} = {0}({1})", op, reg)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_binop(&mut self, op: &str, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let rhs = f.var_name_of(self.reg.pop(1));
|
||||||
|
let lhs = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{1} = {1} {0} {2}", op, lhs, rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_binop_ex(&mut self, op: &str, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let rhs = f.var_name_of(self.reg.pop(1));
|
||||||
|
let lhs = f.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{1} = {0}({1}, {2})", op, lhs, rhs)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_call(&mut self, name: &str, f: &Code, a: &Arity, w: Writer) -> Result<()> {
|
||||||
|
let bottom = self.reg.pop(a.num_param);
|
||||||
|
|
||||||
|
self.reg.push(a.num_result);
|
||||||
|
|
||||||
|
if a.num_result != 0 {
|
||||||
|
let result = f.var_range_of(bottom, a.num_result).join(", ");
|
||||||
|
|
||||||
|
writeln!(w, "{} =", result)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.num_param == 0 {
|
||||||
|
writeln!(w, "{}()", name)
|
||||||
|
} else {
|
||||||
|
let param = f.var_range_of(bottom, a.num_param).join(", ");
|
||||||
|
|
||||||
|
writeln!(w, "{}({})", name, param)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_return(&mut self, num: u32, f: &Code, w: Writer) -> Result<()> {
|
||||||
|
let top = self.reg.inner;
|
||||||
|
let list = f.var_range_of(top - num, num).join(", ");
|
||||||
|
|
||||||
|
self.reg.pop(num); // technically a no-op
|
||||||
|
|
||||||
|
writeln!(w, "do return {} end", list)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_inst(&mut self, index: usize, i: &Instruction, m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let func = &m.code[index];
|
||||||
|
|
||||||
|
match i {
|
||||||
|
Instruction::Unreachable => writeln!(w, "error('unreachable code entered')"),
|
||||||
|
Instruction::Nop => {
|
||||||
|
// no code
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Instruction::Block(_) => {
|
||||||
|
self.reg.save();
|
||||||
|
self.label_list.push(Label::Block);
|
||||||
|
|
||||||
|
self.spec.start_block(w)
|
||||||
|
}
|
||||||
|
Instruction::Loop(_) => {
|
||||||
|
self.reg.save();
|
||||||
|
self.label_list.push(Label::Loop);
|
||||||
|
|
||||||
|
self.spec.start_loop(self.label_list.len() - 1, w)
|
||||||
|
}
|
||||||
|
Instruction::If(_) => {
|
||||||
|
let cond = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.save();
|
||||||
|
self.label_list.push(Label::If);
|
||||||
|
|
||||||
|
self.spec.start_if(&cond, w)
|
||||||
|
}
|
||||||
|
Instruction::Else => {
|
||||||
|
self.reg.load();
|
||||||
|
self.reg.save();
|
||||||
|
|
||||||
|
writeln!(w, "else")
|
||||||
|
}
|
||||||
|
Instruction::End => {
|
||||||
|
let rem = self.label_list.len().saturating_sub(1);
|
||||||
|
|
||||||
|
match self.label_list.pop() {
|
||||||
|
Some(Label::Block) => self.spec.end_block(rem, w)?,
|
||||||
|
Some(Label::If) => self.spec.end_if(rem, w)?,
|
||||||
|
Some(Label::Loop) => self.spec.end_loop(w)?,
|
||||||
|
None => {
|
||||||
|
let num = m.in_arity[index].num_result;
|
||||||
|
|
||||||
|
if num != 0 {
|
||||||
|
self.gen_return(num, func, w)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, "end")?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.reg.load();
|
||||||
|
|
||||||
|
match self.label_list.last() {
|
||||||
|
Some(Label::Block | Label::If) => self.spec.br_target(rem, false, w),
|
||||||
|
Some(Label::Loop) => self.spec.br_target(rem, true, w),
|
||||||
|
None => Ok(()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Instruction::Br(i) => self.gen_jump(*i, w),
|
||||||
|
Instruction::BrIf(i) => self.gen_br_if(*i, func, w),
|
||||||
|
Instruction::BrTable(data) => self.gen_br_table(data, func, w),
|
||||||
|
Instruction::Return => {
|
||||||
|
let num = m.in_arity[index].num_result;
|
||||||
|
|
||||||
|
self.gen_return(num, func, w)
|
||||||
|
}
|
||||||
|
Instruction::Call(i) => {
|
||||||
|
let name = format!("FUNC_LIST[{}]", i);
|
||||||
|
let arity = m.arity_of(*i as usize);
|
||||||
|
|
||||||
|
self.gen_call(&name, func, arity, w)
|
||||||
|
}
|
||||||
|
Instruction::CallIndirect(i, t) => {
|
||||||
|
let index = func.var_name_of(self.reg.pop(1));
|
||||||
|
let name = format!("TABLE_LIST[{}][{}]", t, index);
|
||||||
|
let arity = m.arity_of(*i as usize);
|
||||||
|
|
||||||
|
self.gen_call(&name, func, arity, w)
|
||||||
|
}
|
||||||
|
Instruction::Drop => {
|
||||||
|
self.reg.pop(1);
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Instruction::Select => {
|
||||||
|
let cond = func.var_name_of(self.reg.pop(1));
|
||||||
|
let v2 = func.var_name_of(self.reg.pop(1));
|
||||||
|
let v1 = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "if {} == 0 then", cond)?;
|
||||||
|
writeln!(w, "{} = {}", v1, v2)?;
|
||||||
|
writeln!(w, "end")
|
||||||
|
}
|
||||||
|
Instruction::GetLocal(i) => {
|
||||||
|
let reg = func.var_name_of(self.reg.push(1));
|
||||||
|
let var = func.var_name_of(*i);
|
||||||
|
|
||||||
|
writeln!(w, "{} = {}", reg, var)
|
||||||
|
}
|
||||||
|
Instruction::SetLocal(i) => {
|
||||||
|
let var = func.var_name_of(*i);
|
||||||
|
let reg = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
writeln!(w, "{} = {}", var, reg)
|
||||||
|
}
|
||||||
|
Instruction::TeeLocal(i) => {
|
||||||
|
let var = func.var_name_of(*i);
|
||||||
|
let reg = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{} = {}", var, reg)
|
||||||
|
}
|
||||||
|
Instruction::GetGlobal(i) => {
|
||||||
|
let reg = func.var_name_of(self.reg.push(1));
|
||||||
|
|
||||||
|
writeln!(w, "{} = GLOBAL_LIST[{}].value", reg, i)
|
||||||
|
}
|
||||||
|
Instruction::SetGlobal(i) => {
|
||||||
|
let reg = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
writeln!(w, "GLOBAL_LIST[{}].value = {}", i, reg)
|
||||||
|
}
|
||||||
|
Instruction::I32Load(_, o) => self.gen_load("i32", *o, func, w),
|
||||||
|
Instruction::I64Load(_, o) => self.gen_load("i64", *o, func, w),
|
||||||
|
Instruction::F32Load(_, o) => self.gen_load("f32", *o, func, w),
|
||||||
|
Instruction::F64Load(_, o) => self.gen_load("f64", *o, func, w),
|
||||||
|
Instruction::I32Load8S(_, o) => self.gen_load("i32_i8", *o, func, w),
|
||||||
|
Instruction::I32Load8U(_, o) => self.gen_load("i32_u8", *o, func, w),
|
||||||
|
Instruction::I32Load16S(_, o) => self.gen_load("i32_i16", *o, func, w),
|
||||||
|
Instruction::I32Load16U(_, o) => self.gen_load("i32_u16", *o, func, w),
|
||||||
|
Instruction::I64Load8S(_, o) => self.gen_load("i64_i8", *o, func, w),
|
||||||
|
Instruction::I64Load8U(_, o) => self.gen_load("i64_u8", *o, func, w),
|
||||||
|
Instruction::I64Load16S(_, o) => self.gen_load("i64_i16", *o, func, w),
|
||||||
|
Instruction::I64Load16U(_, o) => self.gen_load("i64_u16", *o, func, w),
|
||||||
|
Instruction::I64Load32S(_, o) => self.gen_load("i64_i32", *o, func, w),
|
||||||
|
Instruction::I64Load32U(_, o) => self.gen_load("i64_u32", *o, func, w),
|
||||||
|
Instruction::I32Store(_, o) => self.gen_store("i32", *o, func, w),
|
||||||
|
Instruction::I64Store(_, o) => self.gen_store("i64", *o, func, w),
|
||||||
|
Instruction::F32Store(_, o) => self.gen_store("f32", *o, func, w),
|
||||||
|
Instruction::F64Store(_, o) => self.gen_store("f64", *o, func, w),
|
||||||
|
Instruction::I32Store8(_, o) => self.gen_store("i32_n8", *o, func, w),
|
||||||
|
Instruction::I32Store16(_, o) => self.gen_store("i32_n16", *o, func, w),
|
||||||
|
Instruction::I64Store8(_, o) => self.gen_store("i64_n8", *o, func, w),
|
||||||
|
Instruction::I64Store16(_, o) => self.gen_store("i64_n16", *o, func, w),
|
||||||
|
Instruction::I64Store32(_, o) => self.gen_store("i64_n32", *o, func, w),
|
||||||
|
Instruction::CurrentMemory(index) => {
|
||||||
|
let reg = func.var_name_of(self.reg.push(1));
|
||||||
|
|
||||||
|
writeln!(w, "{} = read_page_num(MEMORY_LIST[{}])", reg, index)
|
||||||
|
}
|
||||||
|
Instruction::GrowMemory(index) => {
|
||||||
|
let reg = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{0} = grow_page_num(MEMORY_LIST[{1}], {0})", reg, index)
|
||||||
|
}
|
||||||
|
Instruction::I32Const(v) => self.gen_const(v, func, w),
|
||||||
|
Instruction::I64Const(v) => self.gen_const(self.spec.i64(*v), func, w),
|
||||||
|
Instruction::F32Const(v) => self.gen_const(f32::from_bits(*v), func, w),
|
||||||
|
Instruction::F64Const(v) => self.gen_const(f64::from_bits(*v), func, w),
|
||||||
|
Instruction::I32Eqz | Instruction::I64Eqz => {
|
||||||
|
let reg = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{} = {} == 0 and 1 or 0", reg, reg)
|
||||||
|
}
|
||||||
|
Instruction::I32Eq | Instruction::I64Eq | Instruction::F32Eq | Instruction::F64Eq => {
|
||||||
|
self.gen_compare("==", func, w)
|
||||||
|
}
|
||||||
|
Instruction::I32Ne | Instruction::I64Ne | Instruction::F32Ne | Instruction::F64Ne => {
|
||||||
|
self.gen_compare("~=", func, w)
|
||||||
|
}
|
||||||
|
// note that signed comparisons of all types behave the same so
|
||||||
|
// they can be condensed using Lua's operators
|
||||||
|
Instruction::I32LtU => self.gen_binop_ex("lt.u32", func, w),
|
||||||
|
Instruction::I32LtS | Instruction::I64LtS | Instruction::F32Lt | Instruction::F64Lt => {
|
||||||
|
self.gen_compare("<", func, w)
|
||||||
|
}
|
||||||
|
Instruction::I32GtU => self.gen_binop_ex("gt.u32", func, w),
|
||||||
|
Instruction::I32GtS | Instruction::I64GtS | Instruction::F32Gt | Instruction::F64Gt => {
|
||||||
|
self.gen_compare(">", func, w)
|
||||||
|
}
|
||||||
|
Instruction::I32LeU => self.gen_binop_ex("le.u32", func, w),
|
||||||
|
Instruction::I32LeS | Instruction::I64LeS | Instruction::F32Le | Instruction::F64Le => {
|
||||||
|
self.gen_compare("<=", func, w)
|
||||||
|
}
|
||||||
|
Instruction::I32GeU => self.gen_binop_ex("ge.u32", func, w),
|
||||||
|
Instruction::I32GeS | Instruction::I64GeS | Instruction::F32Ge | Instruction::F64Ge => {
|
||||||
|
self.gen_compare(">=", func, w)
|
||||||
|
}
|
||||||
|
Instruction::I64LtU => self.gen_binop_ex("lt.u64", func, w),
|
||||||
|
Instruction::I64GtU => self.gen_binop_ex("gt.u64", func, w),
|
||||||
|
Instruction::I64LeU => self.gen_binop_ex("le.u64", func, w),
|
||||||
|
Instruction::I64GeU => self.gen_binop_ex("ge.u64", func, w),
|
||||||
|
Instruction::I32Clz => self.gen_unop_ex("clz.i32", func, w),
|
||||||
|
Instruction::I32Ctz => self.gen_unop_ex("ctz.i32", func, w),
|
||||||
|
Instruction::I32Popcnt => self.gen_unop_ex("popcnt.i32", func, w),
|
||||||
|
Instruction::I32DivS => self.gen_binop_ex("div.i32", func, w),
|
||||||
|
Instruction::I32DivU => self.gen_binop_ex("div.u32", func, w),
|
||||||
|
Instruction::I32RemS => self.gen_binop_ex("rem.i32", func, w),
|
||||||
|
Instruction::I32RemU => self.gen_binop_ex("rem.u32", func, w),
|
||||||
|
Instruction::I32And => self.gen_binop_ex("band.i32", func, w),
|
||||||
|
Instruction::I32Or => self.gen_binop_ex("bor.i32", func, w),
|
||||||
|
Instruction::I32Xor => self.gen_binop_ex("bxor.i32", func, w),
|
||||||
|
Instruction::I32Shl => self.gen_binop_ex("shl.i32", func, w),
|
||||||
|
Instruction::I32ShrS => self.gen_binop_ex("shr.i32", func, w),
|
||||||
|
Instruction::I32ShrU => self.gen_binop_ex("shr.u32", func, w),
|
||||||
|
Instruction::I32Rotl => self.gen_binop_ex("rotl.i32", func, w),
|
||||||
|
Instruction::I32Rotr => self.gen_binop_ex("rotr.i32", func, w),
|
||||||
|
Instruction::I64Clz => self.gen_unop_ex("clz.i64", func, w),
|
||||||
|
Instruction::I64Ctz => self.gen_unop_ex("ctz.i64", func, w),
|
||||||
|
Instruction::I64Popcnt => self.gen_unop_ex("popcnt.i64", func, w),
|
||||||
|
Instruction::I64DivS => self.gen_binop_ex("div.i64", func, w),
|
||||||
|
Instruction::I64DivU => self.gen_binop_ex("div.u64", func, w),
|
||||||
|
Instruction::I64RemS => self.gen_binop_ex("rem.i64", func, w),
|
||||||
|
Instruction::I64RemU => self.gen_binop_ex("rem.u64", func, w),
|
||||||
|
Instruction::I64And => self.gen_binop_ex("band.i64", func, w),
|
||||||
|
Instruction::I64Or => self.gen_binop_ex("bor.i64", func, w),
|
||||||
|
Instruction::I64Xor => self.gen_binop_ex("bxor.i64", func, w),
|
||||||
|
Instruction::I64Shl => self.gen_binop_ex("shl.i64", func, w),
|
||||||
|
Instruction::I64ShrS => self.gen_binop_ex("shr.i64", func, w),
|
||||||
|
Instruction::I64ShrU => self.gen_binop_ex("shr.u64", func, w),
|
||||||
|
Instruction::I64Rotl => self.gen_binop_ex("rotl.i64", func, w),
|
||||||
|
Instruction::I64Rotr => self.gen_binop_ex("rotr.i64", func, w),
|
||||||
|
Instruction::F32Abs | Instruction::F64Abs => self.gen_unop_ex("math.abs", func, w),
|
||||||
|
Instruction::F32Neg | Instruction::F64Neg => {
|
||||||
|
let reg = func.var_name_of(self.reg.pop(1));
|
||||||
|
|
||||||
|
self.reg.push(1);
|
||||||
|
|
||||||
|
writeln!(w, "{} = -{}", reg, reg)
|
||||||
|
}
|
||||||
|
Instruction::F32Ceil | Instruction::F64Ceil => self.gen_unop_ex("math.ceil", func, w),
|
||||||
|
Instruction::F32Floor | Instruction::F64Floor => {
|
||||||
|
self.gen_unop_ex("math.floor", func, w)
|
||||||
|
}
|
||||||
|
Instruction::F32Trunc | Instruction::F64Trunc => self.gen_unop_ex("trunc.f", func, w),
|
||||||
|
Instruction::F32Nearest | Instruction::F64Nearest => {
|
||||||
|
self.gen_unop_ex("nearest.f", func, w)
|
||||||
|
}
|
||||||
|
Instruction::F32Sqrt | Instruction::F64Sqrt => self.gen_unop_ex("math.sqrt", func, w),
|
||||||
|
Instruction::I32Add
|
||||||
|
| Instruction::I64Add
|
||||||
|
| Instruction::F32Add
|
||||||
|
| Instruction::F64Add => self.gen_binop("+", func, w),
|
||||||
|
Instruction::I32Sub
|
||||||
|
| Instruction::I64Sub
|
||||||
|
| Instruction::F32Sub
|
||||||
|
| Instruction::F64Sub => self.gen_binop("-", func, w),
|
||||||
|
Instruction::I32Mul
|
||||||
|
| Instruction::I64Mul
|
||||||
|
| Instruction::F32Mul
|
||||||
|
| Instruction::F64Mul => self.gen_binop("*", func, w),
|
||||||
|
Instruction::F32Div | Instruction::F64Div => self.gen_binop("/", func, w),
|
||||||
|
Instruction::F32Min | Instruction::F64Min => self.gen_binop_ex("math.min", func, w),
|
||||||
|
Instruction::F32Max | Instruction::F64Max => self.gen_binop_ex("math.max", func, w),
|
||||||
|
Instruction::F32Copysign | Instruction::F64Copysign => {
|
||||||
|
self.gen_unop_ex("math.sign", func, w)
|
||||||
|
}
|
||||||
|
Instruction::I32WrapI64 => self.gen_unop_ex("wrap.i64_i32", func, w),
|
||||||
|
Instruction::I32TruncSF32 => self.gen_unop_ex("trunc.f32_i32", func, w),
|
||||||
|
Instruction::I32TruncUF32 => self.gen_unop_ex("trunc.f32_u32", func, w),
|
||||||
|
Instruction::I32TruncSF64 => self.gen_unop_ex("trunc.f64_i32", func, w),
|
||||||
|
Instruction::I32TruncUF64 => self.gen_unop_ex("trunc.f64_u32", func, w),
|
||||||
|
Instruction::I64ExtendSI32 => self.gen_unop_ex("extend.i32_i64", func, w),
|
||||||
|
Instruction::I64ExtendUI32 => self.gen_unop_ex("extend.i32_u64", func, w),
|
||||||
|
Instruction::I64TruncSF32 => self.gen_unop_ex("trunc.f32_i64", func, w),
|
||||||
|
Instruction::I64TruncUF32 => self.gen_unop_ex("trunc.f32_u64", func, w),
|
||||||
|
Instruction::I64TruncSF64 => self.gen_unop_ex("trunc.f64_i64", func, w),
|
||||||
|
Instruction::I64TruncUF64 => self.gen_unop_ex("trunc.f64_u64", func, w),
|
||||||
|
Instruction::F32ConvertSI32 => self.gen_unop_ex("convert.i32_f32", func, w),
|
||||||
|
Instruction::F32ConvertUI32 => self.gen_unop_ex("convert.u32_f32", func, w),
|
||||||
|
Instruction::F32ConvertSI64 => self.gen_unop_ex("convert.i64_f32", func, w),
|
||||||
|
Instruction::F32ConvertUI64 => self.gen_unop_ex("convert.u64_f32", func, w),
|
||||||
|
Instruction::F32DemoteF64 => self.gen_unop_ex("demote.f64_f32", func, w),
|
||||||
|
Instruction::F64ConvertSI32 => self.gen_unop_ex("convert.f64_i32", func, w),
|
||||||
|
Instruction::F64ConvertUI32 => self.gen_unop_ex("convert.f64_u32", func, w),
|
||||||
|
Instruction::F64ConvertSI64 => self.gen_unop_ex("convert.f64_i64", func, w),
|
||||||
|
Instruction::F64ConvertUI64 => self.gen_unop_ex("convert.f64_u64", func, w),
|
||||||
|
Instruction::F64PromoteF32 => self.gen_unop_ex("promote.f32_f64", func, w),
|
||||||
|
Instruction::I32ReinterpretF32 => self.gen_unop_ex("reinterpret.f32_i32", func, w),
|
||||||
|
Instruction::I64ReinterpretF64 => self.gen_unop_ex("reinterpret.f64_i64", func, w),
|
||||||
|
Instruction::F32ReinterpretI32 => self.gen_unop_ex("reinterpret.i32_f32", func, w),
|
||||||
|
Instruction::F64ReinterpretI64 => self.gen_unop_ex("reinterpret.i64_f64", func, w),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
97
src/backend/translation/level_2.rs
Normal file
97
src/backend/translation/level_2.rs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
use super::level_1::Body;
|
||||||
|
use crate::{
|
||||||
|
backend::helper::{
|
||||||
|
edition::Edition,
|
||||||
|
writer::{write_ordered, Writer},
|
||||||
|
},
|
||||||
|
data::Module,
|
||||||
|
};
|
||||||
|
use parity_wasm::elements::Instruction;
|
||||||
|
use std::{
|
||||||
|
io::{Result, Write},
|
||||||
|
ops::Range,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn list_to_range(list: &[u32]) -> Vec<(Range<usize>, u32)> {
|
||||||
|
let mut result = Vec::new();
|
||||||
|
let mut index = 0;
|
||||||
|
|
||||||
|
while index < list.len() {
|
||||||
|
let start = index;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
index += 1;
|
||||||
|
|
||||||
|
// if end of list or next value is not equal, break
|
||||||
|
if index == list.len() || list[index - 1] != list[index] {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result.push((start..index, list[start]));
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gen_init_expression(code: &[Instruction], w: Writer) -> Result<()> {
|
||||||
|
assert!(code.len() == 2);
|
||||||
|
|
||||||
|
let inst = code.first().unwrap();
|
||||||
|
|
||||||
|
match *inst {
|
||||||
|
Instruction::I32Const(v) => writeln!(w, "{}", v),
|
||||||
|
Instruction::I64Const(v) => writeln!(w, "{}", v),
|
||||||
|
Instruction::F32Const(v) => writeln!(w, "{}", f32::from_bits(v)),
|
||||||
|
Instruction::F64Const(v) => writeln!(w, "{}", f64::from_bits(v)),
|
||||||
|
Instruction::GetGlobal(i) => writeln!(w, "GLOBAL_LIST[{}].value", i),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_prelude(num_param: u32, num_local: u32) -> Result<Vec<u8>> {
|
||||||
|
let mut w = Vec::new();
|
||||||
|
|
||||||
|
writeln!(w, "function(")?;
|
||||||
|
write_ordered("param", num_param, &mut w)?;
|
||||||
|
writeln!(w, ")")?;
|
||||||
|
|
||||||
|
if num_local != 0 {
|
||||||
|
let zero = vec!["0"; num_local as usize].join(", ");
|
||||||
|
|
||||||
|
writeln!(w, "local")?;
|
||||||
|
write_ordered("var", num_local, &mut w)?;
|
||||||
|
writeln!(w, "= {}", zero)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_reg_list(last: u32, num_param: u32, num_local: u32) -> Result<Vec<u8>> {
|
||||||
|
let mut w = Vec::new();
|
||||||
|
let num = last - num_local - num_param;
|
||||||
|
|
||||||
|
if num != 0 {
|
||||||
|
writeln!(w, "local")?;
|
||||||
|
write_ordered("reg", num, &mut w)?;
|
||||||
|
writeln!(w)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(w)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn gen_function(spec: &dyn Edition, index: usize, m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let mut inner = Body::new(spec);
|
||||||
|
let num_param = m.in_arity[index].num_param;
|
||||||
|
let num_local = m.code[index].num_local;
|
||||||
|
|
||||||
|
inner.reg.push(num_param + num_local);
|
||||||
|
|
||||||
|
let prelude = gen_prelude(num_param, num_local)?;
|
||||||
|
let body = inner.gen(index, m)?;
|
||||||
|
let reg = gen_reg_list(inner.reg.last, num_param, num_local)?;
|
||||||
|
|
||||||
|
w.write_all(&prelude)?;
|
||||||
|
w.write_all(®)?;
|
||||||
|
w.write_all(&body)
|
||||||
|
}
|
266
src/backend/translation/level_3.rs
Executable file
266
src/backend/translation/level_3.rs
Executable file
@ -0,0 +1,266 @@
|
|||||||
|
use super::level_2::{gen_function, gen_init_expression};
|
||||||
|
use crate::{
|
||||||
|
backend::helper::{edition::Edition, writer::Writer},
|
||||||
|
data::Module,
|
||||||
|
};
|
||||||
|
use parity_wasm::elements::{External, ImportCountType, Internal, ResizableLimits};
|
||||||
|
use std::io::Result;
|
||||||
|
|
||||||
|
const RUNTIME_DATA: &str = "
|
||||||
|
local grow_page_num = runtime.grow_page_num
|
||||||
|
|
||||||
|
local add = runtime.add
|
||||||
|
local sub = runtime.sub
|
||||||
|
local mul = runtime.mul
|
||||||
|
local div = runtime.div
|
||||||
|
|
||||||
|
local le = runtime.le
|
||||||
|
local lt = runtime.lt
|
||||||
|
local ge = runtime.ge
|
||||||
|
local gt = runtime.gt
|
||||||
|
|
||||||
|
local band = runtime.band
|
||||||
|
local bor = runtime.bor
|
||||||
|
local bxor = runtime.bxor
|
||||||
|
local bnot = runtime.bnot
|
||||||
|
|
||||||
|
local shl = runtime.shl
|
||||||
|
local shr = runtime.shr
|
||||||
|
|
||||||
|
local extend = runtime.extend
|
||||||
|
local wrap = runtime.wrap
|
||||||
|
|
||||||
|
local load = runtime.load
|
||||||
|
local store = runtime.store
|
||||||
|
";
|
||||||
|
|
||||||
|
fn gen_import_of<T>(m: &Module, w: Writer, lower: &str, cond: T) -> Result<()>
|
||||||
|
where
|
||||||
|
T: Fn(&External) -> bool,
|
||||||
|
{
|
||||||
|
let import = match m.parent.import_section() {
|
||||||
|
Some(v) => v.entries(),
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
let upper = lower.to_uppercase();
|
||||||
|
|
||||||
|
for (i, v) in import.iter().filter(|v| cond(v.external())).enumerate() {
|
||||||
|
let field = v.field();
|
||||||
|
let module = v.module();
|
||||||
|
|
||||||
|
writeln!(w, "{}[{}] = wasm.{}.{}.{}", upper, i, module, lower, field)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn aux_internal_index(internal: Internal) -> u32 {
|
||||||
|
match internal {
|
||||||
|
Internal::Function(v) | Internal::Table(v) | Internal::Memory(v) | Internal::Global(v) => v,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_export_of<T>(m: &Module, w: Writer, lower: &str, cond: T) -> Result<()>
|
||||||
|
where
|
||||||
|
T: Fn(&Internal) -> bool,
|
||||||
|
{
|
||||||
|
let export = match m.parent.export_section() {
|
||||||
|
Some(v) => v.entries(),
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
let upper = lower.to_uppercase();
|
||||||
|
|
||||||
|
writeln!(w, "{} = {{", lower)?;
|
||||||
|
|
||||||
|
for v in export.iter().filter(|v| cond(v.internal())) {
|
||||||
|
let field = v.field();
|
||||||
|
let index = aux_internal_index(*v.internal());
|
||||||
|
|
||||||
|
writeln!(w, "{} = {}[{}],", field, upper, index)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, "}},")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_import_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
gen_import_of(m, w, "func_list", |v| matches!(v, External::Function(_)))?;
|
||||||
|
gen_import_of(m, w, "table_list", |v| matches!(v, External::Table(_)))?;
|
||||||
|
gen_import_of(m, w, "memory_list", |v| matches!(v, External::Memory(_)))?;
|
||||||
|
gen_import_of(m, w, "global_list", |v| matches!(v, External::Global(_)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_export_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
gen_export_of(m, w, "func_list", |v| matches!(v, Internal::Function(_)))?;
|
||||||
|
gen_export_of(m, w, "table_list", |v| matches!(v, Internal::Table(_)))?;
|
||||||
|
gen_export_of(m, w, "memory_list", |v| matches!(v, Internal::Memory(_)))?;
|
||||||
|
gen_export_of(m, w, "global_list", |v| matches!(v, Internal::Global(_)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_limit_data(limit: &ResizableLimits, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "{{ min = {}", limit.initial())?;
|
||||||
|
|
||||||
|
if let Some(max) = limit.maximum() {
|
||||||
|
writeln!(w, ", max = {}", max)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, ", data = {{}} }}")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_table_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let table = match m.parent.table_section() {
|
||||||
|
Some(v) => v.entries(),
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
let offset = m.parent.import_count(ImportCountType::Table);
|
||||||
|
|
||||||
|
for (i, v) in table.iter().enumerate() {
|
||||||
|
let index = i + offset;
|
||||||
|
|
||||||
|
writeln!(w, "TABLE_LIST[{}] =", index)?;
|
||||||
|
gen_limit_data(v.limits(), w)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_memory_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let memory = match m.parent.memory_section() {
|
||||||
|
Some(v) => v.entries(),
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
let offset = m.parent.import_count(ImportCountType::Memory);
|
||||||
|
|
||||||
|
for (i, v) in memory.iter().enumerate() {
|
||||||
|
let index = i + offset;
|
||||||
|
|
||||||
|
writeln!(w, "MEMORY_LIST[{}] =", index)?;
|
||||||
|
gen_limit_data(v.limits(), w)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_global_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let global = match m.parent.global_section() {
|
||||||
|
Some(v) => v,
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
let offset = m.parent.import_count(ImportCountType::Global);
|
||||||
|
|
||||||
|
for (i, v) in global.entries().iter().enumerate() {
|
||||||
|
let index = i + offset;
|
||||||
|
|
||||||
|
writeln!(w, "GLOBAL_LIST[{}] = {{ value =", index)?;
|
||||||
|
gen_init_expression(v.init_expr().code(), w)?;
|
||||||
|
writeln!(w, "}}")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_element_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let element = match m.parent.elements_section() {
|
||||||
|
Some(v) => v.entries(),
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
for v in element {
|
||||||
|
writeln!(w, "do")?;
|
||||||
|
writeln!(w, "local target = TABLE_LIST[{}]", v.index())?;
|
||||||
|
writeln!(w, "local offset =")?;
|
||||||
|
|
||||||
|
gen_init_expression(v.offset().as_ref().unwrap().code(), w)?;
|
||||||
|
|
||||||
|
for (i, f) in v.members().iter().enumerate() {
|
||||||
|
writeln!(w, "target[offset + {}] = FUNC_LIST[{}]", i, f)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, "end")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_data_list(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
let data = match m.parent.data_section() {
|
||||||
|
Some(v) => v.entries(),
|
||||||
|
None => return Ok(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
for v in data {
|
||||||
|
writeln!(w, "do")?;
|
||||||
|
writeln!(w, "local target = MEMORY_LIST[{}].data", v.index())?;
|
||||||
|
writeln!(w, "local offset =")?;
|
||||||
|
|
||||||
|
gen_init_expression(v.offset().as_ref().unwrap().code(), w)?;
|
||||||
|
|
||||||
|
writeln!(w, "/ 4")?;
|
||||||
|
|
||||||
|
for (i, b) in v.value().chunks(4).enumerate() {
|
||||||
|
let mut temp = [0; 4];
|
||||||
|
|
||||||
|
temp.iter_mut().zip(b).for_each(|(l, r)| *l = *r);
|
||||||
|
|
||||||
|
let value = u32::from_le_bytes(temp);
|
||||||
|
|
||||||
|
writeln!(w, "target[offset + {}] = 0x{:X}", i, value)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, "end")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_start_point(m: &Module, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "local function run_init_code()")?;
|
||||||
|
gen_table_list(m, w)?;
|
||||||
|
gen_memory_list(m, w)?;
|
||||||
|
gen_global_list(m, w)?;
|
||||||
|
gen_element_list(m, w)?;
|
||||||
|
gen_data_list(m, w)?;
|
||||||
|
writeln!(w, "end")?;
|
||||||
|
|
||||||
|
writeln!(w, "return function(wasm)")?;
|
||||||
|
gen_import_list(m, w)?;
|
||||||
|
writeln!(w, "run_init_code()")?;
|
||||||
|
|
||||||
|
if let Some(start) = m.parent.start_section() {
|
||||||
|
writeln!(w, "FUNC_LIST[{}]()", start)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(w, "return {{")?;
|
||||||
|
gen_export_list(m, w)?;
|
||||||
|
|
||||||
|
writeln!(w, "}} end")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gen_nil_array(name: &str, len: usize, w: Writer) -> Result<()> {
|
||||||
|
if len == 0 {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let list = vec!["nil"; len].join(", ");
|
||||||
|
|
||||||
|
writeln!(w, "local {} = {{[0] = {}}}", name, list)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn translate(spec: &dyn Edition, m: &Module, w: Writer) -> Result<()> {
|
||||||
|
writeln!(w, "local runtime = require('{}')", spec.runtime())?;
|
||||||
|
writeln!(w, "{}", RUNTIME_DATA)?;
|
||||||
|
|
||||||
|
gen_nil_array("FUNC_LIST", m.in_arity.len(), w)?;
|
||||||
|
gen_nil_array("TABLE_LIST", m.parent.table_space(), w)?;
|
||||||
|
gen_nil_array("MEMORY_LIST", m.parent.memory_space(), w)?;
|
||||||
|
gen_nil_array("GLOBAL_LIST", m.parent.globals_space(), w)?;
|
||||||
|
|
||||||
|
let offset = m.ex_arity.len();
|
||||||
|
|
||||||
|
for i in 0..m.in_arity.len() {
|
||||||
|
writeln!(w, "FUNC_LIST[{}] =", i + offset)?;
|
||||||
|
|
||||||
|
gen_function(spec, i, m, w)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
gen_start_point(m, w)
|
||||||
|
}
|
8
src/backend/translation/mod.rs
Executable file
8
src/backend/translation/mod.rs
Executable file
@ -0,0 +1,8 @@
|
|||||||
|
// Translation is done in levels.
|
||||||
|
// Level 1 handles user logic and WASM instructions.
|
||||||
|
// Level 2 handles setup for functions.
|
||||||
|
// Level 3 handles initialization of the module.
|
||||||
|
|
||||||
|
mod level_1;
|
||||||
|
mod level_2;
|
||||||
|
pub mod level_3;
|
147
src/data.rs
Executable file
147
src/data.rs
Executable file
@ -0,0 +1,147 @@
|
|||||||
|
use crate::backend::helper::writer::ordered_iter;
|
||||||
|
use parity_wasm::elements::{
|
||||||
|
External, FunctionType, ImportEntry, Instruction, Local, Module as WasmModule, Type,
|
||||||
|
};
|
||||||
|
use std::{borrow::Cow, convert::TryInto};
|
||||||
|
|
||||||
|
pub struct Code<'a> {
|
||||||
|
pub num_local: u32,
|
||||||
|
pub inst_list: &'a [Instruction],
|
||||||
|
var_list: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Code<'a> {
|
||||||
|
pub fn new(inst_list: &'a [Instruction], num_local: u32) -> Self {
|
||||||
|
Self {
|
||||||
|
num_local,
|
||||||
|
inst_list,
|
||||||
|
var_list: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn local_sum(list: &[Local]) -> u32 {
|
||||||
|
list.iter().map(Local::count).sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn var_name_of(&self, index: u32) -> Cow<'_, str> {
|
||||||
|
let index: usize = index.try_into().unwrap();
|
||||||
|
let offset = self.var_list.len();
|
||||||
|
|
||||||
|
self.var_list
|
||||||
|
.get(index)
|
||||||
|
.map_or_else(|| format!("reg_{}", index - offset + 1).into(), Cow::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn var_range_of(&self, start: u32, len: u32) -> Vec<Cow<'_, str>> {
|
||||||
|
(start..start + len).map(|i| self.var_name_of(i)).collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Arity {
|
||||||
|
pub num_param: u32,
|
||||||
|
pub num_result: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Arity {
|
||||||
|
fn from_type(typ: &FunctionType) -> Self {
|
||||||
|
let num_param = typ.params().len().try_into().unwrap();
|
||||||
|
let num_result = typ.results().len().try_into().unwrap();
|
||||||
|
|
||||||
|
Self {
|
||||||
|
num_param,
|
||||||
|
num_result,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_index(types: &[Type], index: u32) -> Self {
|
||||||
|
let Type::Function(typ) = &types[index as usize];
|
||||||
|
|
||||||
|
Self::from_type(typ)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Module<'a> {
|
||||||
|
pub ex_arity: Vec<Arity>,
|
||||||
|
pub in_arity: Vec<Arity>,
|
||||||
|
pub code: Vec<Code<'a>>,
|
||||||
|
pub parent: &'a WasmModule,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Module<'a> {
|
||||||
|
pub fn new(parent: &'a WasmModule) -> Self {
|
||||||
|
let mut module = Module {
|
||||||
|
in_arity: Self::new_arity_in_list(parent),
|
||||||
|
ex_arity: Self::new_arity_ex_list(parent),
|
||||||
|
code: Self::new_function_list(parent),
|
||||||
|
parent,
|
||||||
|
};
|
||||||
|
|
||||||
|
module.fill_cache();
|
||||||
|
module
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fill_cache(&mut self) {
|
||||||
|
for (a, c) in self.in_arity.iter().zip(self.code.iter_mut()) {
|
||||||
|
c.var_list = ordered_iter("param", a.num_param)
|
||||||
|
.chain(ordered_iter("var", c.num_local))
|
||||||
|
.collect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn arity_of(&self, index: usize) -> &Arity {
|
||||||
|
let offset = self.ex_arity.len();
|
||||||
|
|
||||||
|
self.ex_arity
|
||||||
|
.get(index)
|
||||||
|
.or_else(|| self.in_arity.get(index - offset))
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_arity_ext(types: &[Type], import: &ImportEntry) -> Option<Arity> {
|
||||||
|
if let External::Function(i) = import.external() {
|
||||||
|
Some(Arity::from_index(types, *i))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_arity_in_list(wasm: &WasmModule) -> Vec<Arity> {
|
||||||
|
let (types, funcs) = match (wasm.type_section(), wasm.function_section()) {
|
||||||
|
(Some(t), Some(f)) => (t.types(), f.entries()),
|
||||||
|
_ => return Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
funcs
|
||||||
|
.iter()
|
||||||
|
.map(|i| Arity::from_index(types, i.type_ref()))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_arity_ex_list(wasm: &WasmModule) -> Vec<Arity> {
|
||||||
|
let (types, imports) = match (wasm.type_section(), wasm.import_section()) {
|
||||||
|
(Some(t), Some(i)) => (t.types(), i.entries()),
|
||||||
|
_ => return Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
imports
|
||||||
|
.iter()
|
||||||
|
.filter_map(|i| Self::new_arity_ext(types, i))
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_function_list(wasm: &WasmModule) -> Vec<Code> {
|
||||||
|
let bodies = match wasm.code_section() {
|
||||||
|
Some(b) => b.bodies(),
|
||||||
|
None => return Vec::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
bodies
|
||||||
|
.iter()
|
||||||
|
.map(|v| {
|
||||||
|
let num_local = Code::local_sum(v.locals());
|
||||||
|
|
||||||
|
Code::new(v.code().elements(), num_local)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
30
src/main.rs
Executable file
30
src/main.rs
Executable file
@ -0,0 +1,30 @@
|
|||||||
|
use backend::{
|
||||||
|
helper::edition::{Edition, LuaJIT, Luau},
|
||||||
|
translation::level_3,
|
||||||
|
};
|
||||||
|
use data::Module;
|
||||||
|
use parity_wasm::elements::deserialize_file;
|
||||||
|
|
||||||
|
mod backend;
|
||||||
|
mod data;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut args = std::env::args().skip(1);
|
||||||
|
let spec: Box<dyn Edition> = match args.next().as_deref().map(str::to_lowercase).as_deref() {
|
||||||
|
Some("luau") => Box::new(Luau),
|
||||||
|
Some("luajit") => Box::new(LuaJIT),
|
||||||
|
_ => {
|
||||||
|
println!("expected either 'luau' or 'luajit' option");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let output = std::io::stdout();
|
||||||
|
|
||||||
|
for v in args {
|
||||||
|
let wasm = deserialize_file(v).unwrap();
|
||||||
|
let module = Module::new(&wasm);
|
||||||
|
|
||||||
|
level_3::translate(spec.as_ref(), &module, &mut output.lock()).unwrap();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user