Commit 1bfaf6ab authored by Kunshan Wang's avatar Kunshan Wang

WIP: support multiple return values

parent 338c63e0
......@@ -2,8 +2,9 @@
Mu Specification
================
This is a fork of the `Mu Specification` to support the long-demanded
"goto-with-value" form.
This branch continues with the `goto-with-values
<https://github.com/microvm/microvm-spec/tree/goto-with-values>`__ branch and
attempts to add supports of multiple return values per instruction or function.
This document aims to provide a detailed description of Mu, a micro virtual
machine, including its architecture, instruction set and type system.
......
......@@ -54,14 +54,14 @@ instructions or changing the grammar.
This document uses the following notation::
[id]@name [F1 F2 ...] < T1 T2 ... > ( p1:t1, p2:t2, ... ) excClause KEEPALIVE -> RT
[id]@name [F1 F2 ...] < T1 T2 ... > ( p1:t1, p2:t2, ... ) excClause KEEPALIVE -> RTs
``id`` is the ID and ``@name`` is the name. ``F1 F2 ...`` is a list of flags.
``T1 T2 ...`` is a list of types. It is the type parameter list. ``p1:t1,
p2:t2, ...`` is a list of symbolic name and type pair. It is the value parameter
list with the type of each parameter. If ``excClause`` or ``KEEPALIVE`` are
present, they mean that the common instruction accepts exception clause or
keepalive clause, respectively. ``RT`` is the return type. If the type
keepalive clause, respectively. ``RTs`` are the return types. If the type
parameter list or the value parameter list are omitted, it means the common
instruction takes no type parameters or value parameters, respectively. If the
return type is omitted, it returns ``void``.
......@@ -75,12 +75,24 @@ Thread and Stack operations
::
[0x201]@uvm.new_thread (%s: stackref) -> threadref
[0x201]@uvm.new_stack <[sig]> (%func: funcref<sig>) -> stackref
Create a new thread for a given stack ``%s``. The stack must be in the **READY**
state. After executing ``@uvm.new_thread``, the stack enters the **ACTIVE**
state and the new thread starts running immediately. Return the handle to the
newly created thread.
Create a new stack with ``%func`` as the stack-bottom function. ``%func`` must
have signature ``sig``. Returns the stack reference to the new stack.
The new stack is in the state **READY<Ts>**, where *Ts* is the parameter types
of ``%func``.
This instruction continues exceptionally if Mu failed to create the stack. The
exception parameter receives NULL.
..
For LLVM users: There is no native facilities provided by the official LLVM,
but `Dolan, Muralidharan and Gregg
<http://dl.acm.org/citation.cfm?id=2400695>`__ proposed an extension to LLVM
to support the SWAPSTACK operation. The SWAPSTACK interface of Mu is
inspired by their works.
::
......
This diff is collapsed.
......@@ -68,10 +68,11 @@ States of Stacks
A stack is in one of the following states at any moment. Some operations may
change its state.
READY<T>
The stack is ready to run, but is not running. When bound, a value of type
*T* must be supplied. This value will be received by some specific
instructions. *T* can be ``void``.
READY<Ts>
(Ts = T1 T2 T3 ..., a list of types) The stack is ready to run, but is not
running. When bound, values of types *T1 T2 T3 ...* must be supplied. This
value will be received by some specific instructions. *Ts* can be an empty
list.
ACTIVE
A thread is executing on the stack.
......@@ -87,10 +88,10 @@ Operation Current Stack New/Destination Stack
======================= =============================== =======================
create new stack N/A READY<void>
create new thread N/A READY<void> -> ACTIVE
swap-stack ACTIVE -> READY<T> or DEAD READY<U> -> ACTIVE
@uvm.kill_stack N/A READY<T> -> DEAD
swap-stack ACTIVE -> READY<Ts> or DEAD READY<Us> -> ACTIVE
@uvm.kill_stack N/A READY<Ts> -> DEAD
@uvm.thread_exit ACTIVE -> DEAD N/A
trap to client ACTIVE -> READY<T> N/A
trap to client ACTIVE -> READY<Ts> N/A
undefined function ACTIVE N/A
stack-related errors ACTIVE N/A
======================= =============================== =======================
......
......@@ -470,11 +470,7 @@ Void Type
| 0x0A |
+------+
The ``void`` type has exactly one value: ``NULL``.
NOTE: Having exactly one value is equivalent to having no value: both convey
no information at all. It is usually implemented as not being represented by
any bytes.
The ``void`` type has no value.
It can only be used in the following ways:
......@@ -490,25 +486,6 @@ It can only be used in the following ways:
* As the fixed part of a ``hybrid`` to indicate the absence of the fixed part.
* As the return type of instructions and functions, and the type of passed value
during SWAP-STACK, to indicate that the instruction/function/swap-stack
operation does not return/pass any meaningful value.
For this reason, the following instructions may take a ``void`` value, i.e.
``NULL``, as their arguments:
* ``RET``: when returning from a function that returns ``void``.
* ``SWAPSTACK`` in the ``PASS_VAL`` clause: when the swappee is expecting a
``void`` value to be passed.
In addition to many instructions that always return ``void``, the following
instructions may return ``void`` if the callee/swappee returns/passes
``void``:
* ``CALL``
* ``CCALL``
* ``SWAPSTACK``
Opaque Reference Type
---------------------
......@@ -547,8 +524,8 @@ A ``NULL`` value of a ``funcref`` type does not refer to any function.
.typedef @i64 = int<64>
.typedef @void = void
.funcsig @sig1 = @i64 (@i64 @i64)
.funcsig @sig2 = @void ()
.funcsig @sig1 = (@i64 @i64) -> (@i64)
.funcsig @sig2 = () -> ()
.typedef @func1 = funcref<@sig1>
.typedef @func2 = funcref<@sig2>
......@@ -685,8 +662,9 @@ space which represents the data type *T*.
``ufuncptr`` is the function pointer type. It points to a native function whose
signature is *sig*.
The type parameter *T* and both the return type and the parameter types of *sig*
must be *native-safe*.
The type parameter *T* and both the return types and the parameter types of
*sig* must be *native-safe*. It is implementation-defined whether multiple
return values are allowed for a particular calling convention.
For LLVM users: ``uptr<T>`` is the counterpart of pointer types ``T*``.
``ufuncptr<sig>`` is the counterpart of function pointers ``R (P1 P2 ...)*``.
......@@ -711,16 +689,16 @@ must be *native-safe*.
.typedef @void = void // void
.typedef @void_p = uptr<@void> // void*
.typedef @size_t = int<64> // size_t, ssize_t
.funcsig @write_s = @size_t (@i32 @void_p @size_t)
.funcsig @write_s = (@i32 @void_p @size_t) -> (@size_t)
.typedef @write_fp = ufuncptr<@write_s>
// @write_fp may point to the native function "write".
// typedef void (*sig_t) (int);
// sig_t signal(int sig, sig_t func);
// See man (3) signal.
.funcsig @sig_s = @void (@i32)
.funcsig @sig_s = (@i32) -> ()
.typedef @sig_t = ufuncptr<@sig_s>
.funcsig @signal_s = @sig_t (@i32 @sig_t)
.funcsig @signal_s = (@i32 @sig_t) -> (@sig_t)
.typedef @signal_fp = ufuncptr<@signal_s>
// @signal_fp may point to the native function "signal".
......
......@@ -668,19 +668,19 @@ Stack and thread operations
::
MuStackRefValue (*new_stack )(MuCtx *ctx, MuFuncRefValue func, MuValue *args, int nargs);
MuThreadRefValue (*new_thread)(MuCtx *ctx, MuStackRefValue stack);
MuStackRefValue (*new_stack )(MuCtx *ctx, MuFuncRefValue func);
MuThreadRefValue (*new_thread)(MuCtx *ctx, MuStackRefValue stack, MuValue *vals, int nvals);
void (*kill_stack)(MuCtx *ctx, MuStackRefValue stack);
``new_stack`` creates a new Mu stack with ``func`` as the bottom function.
Arguments are provided in the ``args`` array. ``nargs`` is the number of
arguments. The type of arguments must match the signature of ``func``. Returns a
stackref to the new stack. The new stack stops in the very beginning of
``func``, and its state is ``READY<void>``. ``func`` must have at least one
version (i.e. must be defined).
Returns a stackref to the new stack. The new stack stops in the very beginning
of ``func``, and its state is ``READY<Ts>``, where *Ts* is the parameter types
of ``func``.
``new_thread`` creates a new Mu thread and binds it to ``stack``. ``stack`` must
be in the ``READY<void>`` state. Returns a threadref to the new thread.
be in the ``READY<Ts>`` state for some types *Ts*. Arguments are provided in the
``args`` array. ``nargs`` is the number of arguments. The type of arguments
must match the signature of ``func``.Returns a threadref to the new thread.
``kill_stack`` kills the ``stack``. The stack must be in the ``READY<T>`` state,
where *T* can be void. The stack enters the ``DEAD`` state. If the stack
......
......@@ -104,16 +104,17 @@ Function Signature Definition
Function signature definition has the following form:
+------+-----+-------+----------+
| opct | idt | idt | idList |
+======+=====+=======+==========+
| 0x02 | id | retty | paramtys |
+------+-----+-------+----------+
+------+-----+----------+----------+
| opct | idt | idList | idList |
+======+=====+==========+==========+
| 0x02 | id | paramtys | rettys |
+------+-----+----------+----------+
``id`` is the identifier of the defined function signature. ``retty`` is the
ID of the return type. ``paramtys`` is a list of IDs of its parameter types.
``id`` is the identifier of the defined function signature. ``paramtys`` is a
list of IDs of its parameter types. ``rettys`` is a list of IDs of the return
types.
NOTE: this is equivalent to: ``.funcsig id = retty (paramtys)``
NOTE: this is equivalent to: ``.funcsig id = (paramtys) -> (rettys)``
Constant Definition
-------------------
......@@ -294,14 +295,15 @@ ninsts instructions following the header.
An **instruction** has the following form:
+-----+------------------+
| idt | instruction body |
+=====+==================+
| id | instbody |
+-----+------------------+
+--------+-----+------------------+
| idList | idt | instruction body |
+========+=====+==================+
| resIDs | id | instbody |
+--------+-----+------------------+
``id`` is the ID of the instruction. ``instbody`` is instruction body which is
specific to each instruction. See `<instruction-set.rest>`__ for an exhaustive list.
``resIDs`` is a list of IDS for the results. ``id`` is the ID of the
instruction. ``instbody`` is instruction body which is specific to each
instruction. See `<instruction-set.rest>`__ for an exhaustive list.
Function Exposing Definition
============================
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment