4. R-code


4. R-code

R-code is the program format of the RVM: a text file that the compiler writes and both machines read. This chapter gives the file format, the execution model the instructions assume, the instruction table and its verifier, and every instruction (operands, stack effect, semantics). It assumes the reader knows Rexx and the ooRexx trace tags (>V>, >O>...); how the compiler produces R-code is in 03-compiler.md, how the machine runs it in 05-machine.md.

Code: RVM.RCode (writer and reader), RVM.ISA (instruction table and verifier) and the dispatch loop RVM.Machine~run in ../RVM.Machine.cls; readRCode, ISA, verify and appendFragment in ../js/rcode.mjs, the dispatch loop in ../js/machine.mjs.

Sources of truth

  • The instruction table RVM.ISA (method activate, the spec array) is the executable definition: for each opcode its operand classes, how many values it pops and pushes, and its control flow. The JS host carries the same table, line for line, as SPEC in rcode.mjs; the two must stay identical.
  • The verifier (RVM.ISA~verify, verify in rcode.mjs) applies the table to every program loaded and to every piece of code appended at run time. R-code that does not pass is error 93.900 ("RVM: bad R-code at address: reason").
  • This chapter explains the semantics; when it and the code disagree, the code wins.

The file

R-code is text, one element per line. The sections come in a fixed order, each one introduced by a keyword line:

RCODE v                  format version (RVM.RCode~VERSION)
COMPILER cv isa f        compiler version (RVM.Compiler~VERSION), ISA version (RVM.ISA~VERSION), the parser's frontier stamp (RVM.Compiler~frontier)
SOURCE hash              fingerprint of the source text, or -
PROGRAM "name"
PROLOG extent            where the prolog's source is: l.c-l.c, or -
OPTIONS d f form t numeric prolog conds explicit
CONSTANTS n              n lines: "value"                 (constant 1, 2, ...)
NAMES n                  n lines: "NAME"                  (slot 1, 2, ...; a stem is "A.")
CLAUSES n                n lines: line "text"             (clause 1, 2, ...)
LABELS n                 n lines: "label" address
DESCRIPTORS n            (the count only)
WITHS n                  n lines: m (stream type option class value){m}
REQUIRES n               n lines: clause "name" [LIBRARY] [NAMESPACE "NS"]
CLASSES n                n lines: clause "ID" "SUPER" "META" PUBLIC|PRIVATE [MIXIN] [ABSTRACT] [INHERIT "C"...]
METHODS n                n lines: class I|C|K "NAME" flags extent kind value...
ROUTINES n               n lines: "NAME" PUBLIC|PRIVATE extent CODE address m ("label" address){m}
                               or "NAME" PUBLIC|PRIVATE - EXTERNAL clause "spec"
ANNOTATIONS n            n lines: P 0|C k|M k|R k "NAME" "VALUE"
RESOURCES n              n times: "NAME" m, then m lines: "line"
CODE n                   n lines: OP operand...           (address 1, 2, ...)

A complete example, rvm -l hello.rex for

say double(21)
exit
::routine double public
  return arg(1) * 2
::class Point public
::attribute x
RCODE r
COMPILER c i f
SOURCE 131580711.641049928.99
PROGRAM "/home/me/hello.rex"
PROLOG 1.1-2.5
OPTIONS 9 0 SCIENTIFIC N NOINHERIT 1 - -
CONSTANTS 5
"21"
"DOUBLE"
"1"
"ARG"
"2"
NAMES 0
CLAUSES 6
1 "say double(21)"
2 "exit"
2 ""
4 "return arg(1) * 2"
4 ""
5 "::class Point public"
LABELS 0
DESCRIPTORS 0
WITHS 0
REQUIRES 0
CLASSES 1
6 "POINT" "" "" PUBLIC
METHODS 2
1 I "X" A - GET "X"
1 I "X=" A - SET "X"
ROUTINES 1
"DOUBLE" PUBLIC 3.24-4.20 CODE 11 0
ANNOTATIONS 0
RESOURCES 0
CODE 20
CLAUSE 1 N
PUSHN 1
TRARG
FUNCX 2 1
SAY
PAUSE 1
CLAUSE 2 N
EXIT
CLAUSE 3 X
EXIT
CLAUSE 4 N
PUSHN 3
TRARG
FUNCB 4 1
PUSHN 5
MUL
RES
RETURNV
CLAUSE 5 X
EXIT

(r, c, i stand for the current values of the three version constants. The clauses with empty text are the implicit EXITs at the end of each body; SOURCE and PROGRAM depend on the file.)

Lexical rules

  • Strings are written between double quotes, inner quotes doubled, when every character is printable (20x–7Ex); otherwise as X'hex' (routines Quote/Unquote; unquote in JS).
  • Operands are decimal numbers or bare words, never containing blanks (CLAUSE 3 N, NUMFORM SCIENTIFIC, JF 43 WHILE). Variants and options are bare words, not constants.
  • The ooRexx reader takes the sections by position (it only checks RCODE, COMPILER and OPTIONS); the JS reader also checks each section keyword. Both verify the code after reading it.

The header lines

  • RCODE / COMPILER. A reader accepts only its own format version and its own ISA version; anything else is 93.900 ("compile the program again"). The compiler version and the parser's frontier stamp are not checked on load; they only decide whether a cache is up to date (03-compiler.md).
  • SOURCE. h1.h2.len: the text of the source file taken as a number in base 2³², four bytes at a time (the last group shorter), reduced modulo 2147483647 (h1) and modulo 2147483629 (h2), and the length in bytes. SourceHash in Rexx, sourceHash (BigInt) in JS compute the same value. - when the R-code was not made from a file (INTERPRET fragments, sources given at run time).
  • PROGRAM. The name the program was compiled under (its path).
  • PROLOG. The extent of the prolog's source, sl.sc-el.ec, from 1.1 to where its implicit EXIT is: what Method~source shows for a method made at run time (such a method is the prolog of a program of its own).
  • OPTIONS. The package's ::OPTIONS, folded in order (a later setting wins): DIGITS, FUZZ, FORM, TRACE (N by default), NUMERIC (INHERIT or NOINHERIT), PROLOG (1, or 0 for NOPROLOG); then the conditions that ::OPTIONS makes SYNTAX, comma-separated (ERROR, FAILURE, LOSTDIGITS, NOSTRING, NOTREADY, NOVALUE) or -; then the options the directives set explicitly, comma-separated in the order DIGITS FORM FUZZ NUMERIC ERROR FAILURE LOSTDIGITS NOSTRING NOTREADY NOVALUE NOPROLOG PROLOG TRACE, or - (ooRexx's explicit options: Package~options, and what .Package~defaultOptions does not override). How the machine applies them: 05-machine.md and 06-conditions-and-errors.md.

Tables

  • CONSTANTS. The constant pool, each value once. Operands of class k index it.

  • NAMES. One numbering of variable names for the whole program (prolog, routines and methods alike): slot n is the nth name. A stem is its name with the period ("A."); a compound has no slot of its own (its stem does).

  • CLAUSES. The clause table: for each clause, its first source line and its text as the trace and error reports show it. Labels have clauses of their own. Clause numbers are operands of class c.

  • LABELS. The labels of the prolog ("NAME" address, the key as the parser gives it: an upper-cased symbol or a string as written). The first definition of a duplicated label wins. Routines and methods carry their own labels in their entries.

  • DESCRIPTORS. How many block descriptors the code uses (operands d, D): one per loop and per SELECT.

  • WITHS. One line per ADDRESS ... WITH: the number of redirections m, then for each one stream type option class value. stream is INPUT, OUTPUT or ERROR; type NORMAL, STEM, STREAM or USING; option APPEND, REPLACE or -; class STEM (a stem name), STR (a string), USTEM (USING a stem), THUNK or -. A THUNK's value is a code address, unquoted: separate code, jumped over in the normal flow, that evaluates the target and ends with YIELD. (The machines also accept the class ENV, which the compiler never writes, and reject it with 93.900.) Operands of class p index this table.

  • REQUIRES. One per ::REQUIRES, in order: the clause of the directive (where a failure is reported), the name as written, LIBRARY for ::REQUIRES ... LIBRARY, and NAMESPACE "NS" (upper case) when one is given. Packages, libraries and namespaces: 03-compiler.md, 12-libraries.md.

  • CLASSES. One per ::CLASS, in order (class 1, 2, ...): its clause, its id as written, the names of its superclass and metaclass in upper case ("": Object, Class), PUBLIC or PRIVATE, MIXIN for a MIXINCLASS, ABSTRACT, and INHERIT followed by the mixins. A namespace-qualified name ns:name is written "NS:NAME"; an unqualified name that contains a colon is written ":NAME". How classes are made: 08-objects.md.

  • METHODS. One per method: its class (0: a floating method, defined before the first ::CLASS), I (instance), C (class) or K (the class's initializer of calculated constants, named ::CONSTANT), the name in upper case, the flags, the extent, the kind and its value:

    Kind Value Meaning
    CODE address m ("label" address){m} A method written in Rexx: a body of its own with its own labels.
    GET, SET "VAR" An attribute's getter or setter.
    DELEGATE "VAR" DELEGATE var: the message, as sent, goes with its arguments to the object in the variable; its result is the method's.
    CONST k A constant (one instance and one class method).
    CCONST - A calculated constant (::CONSTANT name (expr)): no value until the class's K initializer gives it one (SETCONST); asked for before, 97.4.
    ABSTRACT - An abstract method.
    REXX f EXTERNAL 'LIBRARY REXX f' with an f that ooRexx's own REXX library has: the RVM's native X.f (timers of Alarm and Ticker, Stream, File, RexxQueue...).
    LIBRARY clause "spec" "NAME" prefix Any other EXTERNAL. prefix is -, or GET/SET for an ::ATTRIBUTE (entries GETname, SETname). Resolved when the program starts (99.917 for a bad specification, 90.998 for an entry a library does not have, 98.903 for a library that is not one of ooRexx's) or, for a library of ooRexx that has methods but that the RVM lacks, when the method runs (98.903).

    The flags are a word of the letters P (private), R (protected), K (package), U (unguarded), A (an attribute method, Method~isAttribute), in that order, or - (public, guarded, not an attribute); a constant is U. The extent is sl.sc-el.ec, from the end of the directive to where the body's implicit EXIT is (what Method~source cuts), or - (attributes, constants, abstract and external methods).

  • ROUTINES. One per ::ROUTINE, in order: the name in upper case, PUBLIC or PRIVATE, the extent, and either CODE address m labels (a body of its own) or, with extent -, EXTERNAL clause "spec" (LIBRARY lib [entry] or REGISTERED lib [entry], resolved when the program starts: 99.917, 98.903, 90.999).

  • ANNOTATIONS. One per (name, value) pair of an ::ANNOTATE, for each target, in order (a later pair with the same name wins): P 0 the package, C k class k, M k method k of METHODS, R k routine k of ROUTINES. The name is upper case; the value a string. A constant (one method in ooRexx, two here) gets the pairs on both methods.

  • RESOURCES. One per ::RESOURCE: "NAME" m (name upper case), then its m lines as strings. The program's resources become .RESOURCES (09-reflection.md).

  • CODE. The instructions, address 1 upward.

The execution model

The full machine is in 05-machine.md; the instructions assume the following.

  • The stack holds values (a string, a number, an object) and the NONE marker (an omitted argument or operand). It is untyped. The stack is empty, relative to the activation, at the start of every clause (CLAUSE, LABEL); the verifier enforces it.
  • Slots. A variable operand s is a slot of the NAMES table. The JS host keeps true slot frames (an array of cells per activation); the ooRexx host maps the slot to its name (RVM.Vars). Names that are only known at run time (VALUE, INTERPRET, computed tails) go by name in both.
  • Block descriptors (d) belong to the activation: created by BASE, they keep the trace indentation of the block and, for a loop, its control value, TO, BY, FOR count, OVER items, supplier, counter and whether the body has started; for SELECT CASE, the CASE value.
  • Clauses. CLAUSE makes a clause current (for the trace, SIGL, error reports, .LINE), runs the pending CALL ON traps of the clause before, and counts the activity's time slice (07-concurrency.md).
  • Trace tags. Each instruction traces what it does with the ooRexx tag of that operation (LOADV → >V>, PUSHC → >L>, ADD → >O>, FUNCB → >F>), so TRACE I/R output falls out of the code.

The instruction table and the verifier

Each spec entry is OP operands pops pushes flow.

Operand classes (checked on load by OperandOK / operandOK):

Class Meaning Valid values
k constant 1 … number of constants
K constant, or none 0 … number of constants (0: none / PROPAGATE)
b flag 0 or 1
s variable slot 1 … number of names
c clause 1 … number of clauses
d block descriptor 1 … DESCRIPTORS
D block descriptor, or none 0 … DESCRIPTORS
a code address 1 … number of instructions
n count ≥ 0
l source line (SIGL) ≥ 0
i integer ≥ −1
p redirection 0 (none) … number of WITHS
x error code m or m.n
w word any non-empty word

Pops and pushes are numbers or sums of terms, #j being the value of operand j (a negative operand counts as 0): LOADC snk pops #2 (the tail pieces), SEND knb pops #2+1+#3 (the arguments, the receiver, and the scope when b is 1), RAISE Kwi pops #3+4.

Flow letters: - falls through; E never falls through (a return, exit, unconditional jump, error); Bj may also go to the address in operand j; Z must find the stack empty (a clause start); Y must find exactly one value (the end of a thunk); P is an interactive trace pause, whose = goes back to the address in operand 1 with the values the clause had pushed dropped (PauseDrop: BPAUSE's n, 1 for IPAUSE, else 0).

The verifier first checks every instruction: a known opcode, the right number of operands, each valid for its class, and that every CALLI/FUNCI names a label of the prolog, of a method or of a routine. Then it follows every path from the entry points (address 1, every label, every thunk, every method and routine body and their labels; for code appended at run time, its start and its thunks) at depth 0, and checks that the stack never underflows, that it has the same depth wherever two paths meet, that every Z instruction is reached at depth 0, that every Y at depth 1, and that the code never runs off its end.

Instructions

Notation: under Stack, before → after, top on the right; (v) is present in the second form of a pair. Trace tags are written only under TRACE I unless stated (>>> results also under TRACE R).

Constants and the stack

Op Operands Stack Effect
PUSHC k → v Constant. Traces >L>.
PUSHQ k → v Constant, untraced (pieces of a compound tail, an empty expression, internal values).
PUSHN k → v Numeric literal: the constant's text made a number under the current DIGITS and FORM (cached per instruction and settings). Traces >L>.
ENV k → v Environment symbol, the constant as written. Looked up in: the program's classes, the public classes it imports from required packages (and, for a program compiled at run time, its parents'), the builtins, the package's LOCAL, .LOCAL, .ENVIRONMENT, then the special names (.ROUTINES, .METHODS, .RESOURCES, .LINE, .CONTEXT, .RS), else the symbol itself. Traces >E>.
NSCLASS k k → v ns:name as a class: the public class named by the second constant in the package whose namespace is the first (98.987 no such namespace, 98.988 no such class). Traces >N> with NS:NAME.
PUSHNONE → NONE Omitted argument, absent operand.
POP v → Discard.
STR v → s The value as a string (an object's STRING).
SWAP a b → b a Exchange the two top values (a message term as a target puts its receiver under the value).

Result tracing

Op Operands Stack Effect
RES v → v Traces the value as a result (>>>).
RESA v → v The same, only if the clause started traced (an assignment's result).
TRES k Traces the constant as a result (the 1/0 of a condition list).
KEYW k v → v Traces >K> with keyword k and the value.
KEYWC v → v The same, with the name of the current condition as keyword (RAISE PROPAGATE; no current condition: 98.918).
TRKEY k k Traces >K> with a constant keyword and a constant value.
TRARG v → v Traces >A> (NONE as "").

Variables

Op Operands Stack Effect
LOADV s → v A simple variable. No value: NOVALUE, and the value is the name. Traces >V>.
STOREV s v → Assignment. Traces >=>.
TAILV s → v A tail piece: the variable's string value, untraced, no NOVALUE (its name when unset).
TAILC s → v The same, read in the caller's frame (PROCEDURE EXPOSE of a compound).
LOADC s n k p1..pn → v Compound: the tail is p1‖…‖pn, s the stem's slot. No value: NOVALUE (described by the derived name), and the value is the derived name. Traces >C> (derived name) and >V> with the name k as written (A.I).
STOREC s n k v p1..pn → Assignment to the compound. Traces >C>, >=>.
LOADS s → v The whole stem: its Stem object (no NOVALUE). Traces >V>.
STORES s v → Whole-stem assignment: a Stem object (of class Stem itself) becomes the variable's stem (a. = b. shares it); any other value becomes the default value of a new stem. Traces >=>.
DROPV s DROP a simple variable.
DROPS s DROP a stem: a new empty stem.
DROPC s n p1..pn → DROP a compound.
DROPI s DROP (x): x read as LOADV reads it (NOVALUE; traces >V>), then every name in its value dropped.
VREF s → v >x, >a. (and <x): a new VariableReference to the variable (made, with no value, when the frame has none). Each VREF makes a new object. Traces >O> with ">".

Operators

All pop their operands and push the result, traced >O> (>P> for prefix operators) with the operator.

Op Operands Stack Effect
ADD SUB MUL DIV IDIV REM POW a b → r + - * / % // ** (decimal arithmetic: 02-values-and-arithmetic.md).
CAT ABUT CATB a b → r \|\|, abuttal, blank concatenation.
EQ NE LT GT LE GE k a b → 0/1 Normal comparison. k is the operator as written, for the trace and the message (\=, <>, >< are NE; \> is LE; \< is GE; ¬ is written \).
SEQ SNE SLT SGT SLE SGE k a b → 0/1 Strict comparison (\>> is SLE, \<< is SGE).
AND OR XOR a b → 0/1 & \| &&. Both must be 0 or 1, else 34.901 (the left one is checked first, as ooRexx's String::andOp).
PLUS NEG NOT a → r Prefix + - \.
  • Both operands are evaluated before either is converted.
  • Operator as message. An object on the left (the only operand for a prefix operator) gets the operator as a message with the right operand as argument: o + 1 sends "+", o \= x sends "\=" (as written), abuttal sends "", blank concatenation " ", -o sends "-" with no argument. The result is traced and pushed as the operation's would be; no result is 91.999. An object on the right of a string operand is converted by its STRING method.
  • .nil. A string or number compared with .nil on the right is never equal nor ordered: the result is 0, except 1 for NE and SNE (ooRexx's String, NumberString and Integer).

Messages

The receiver is pushed first, then the arguments (each traced >A>; NONE for an omitted one). Message names are upper case. A method not found goes to UNKNOWN when the receiver has one (a Rexx UNKNOWN gets the arguments as an Array), else 97.1. Message dispatch, scopes and conversions: 08-objects.md.

Op Operands Stack Effect
SEND k n b r [s] a1..an → v r~name(...), and r[...] (name []). No result: 91.999. Traces >M> and the result.
CASC k n b r [s] a1..an → r r~~name(...): the receiver remains.
SENDI k n w b r [s] a1..an → A message instruction: RESULT gets the result (w = N) or the receiver (w = C, a cascade); no result drops RESULT.
SENDA k n b r [s] v a1..a(n−1) → A message assignment (r~name(a) = v sends NAME=, r[a] = v sends []=): n counts the value, which is the first argument. Nothing traced after the arguments; RESULT untouched. Also the store of a message term as the target of PARSE, USE ARG and DO control variables.
SCOPE r s → r s Checks the scope of r~name:s just evaluated: s must be a class (88.914) and one of r's scopes (93.957).

b is 1 for ~name:scope: the scope value s sits between the receiver and the arguments, and the method search starts at it. The scope is a variable (SUPER is the variable SUPER) or an environment symbol, evaluated after the receiver; SCOPE follows it, except in the target of PARSE or USE ARG, where the send itself checks it.

Compound message assignment (r~name(a) op= e, ooRexx's messageAssignmentOpNew) is compiled on a copy of the term: r, then r~name(a) (SEND: receiver and arguments evaluated twice, as in ooRexx), e, the operator, TRARG, the arguments, SENDA.

Message term as a target (PARSE, USE ARG, ooRexx's RexxExpressionMessage::assign): the value comes first; the receiver is evaluated then and put under it (SWAP; the scope, if any, goes under it with a second SWAP), and the arguments are traced >>> (an omitted one as ""), not >A>.

Conversion by re-execution. When an instruction needs the string of an object whose STRING method is written in Rexx, the instruction is abandoned before it has changed anything, STRING runs as an ordinary method, and the instruction runs again with the string kept for it.

Method activations

Op Operands Stack Effect
EXPO s EXPOSE (in a method) of a simple variable: it becomes the object's variable of the method's scope. Outside a method: 98.992.
EXPOS s The same for a stem.
EXPOC s n k p1..pn → EXPOSE of a compound: the tail (built with TAILV in the method's own variables; a variable exposed earlier in the same EXPOSE counts) of the method's stem s becomes a shared variable with the object's stem (reads, assignments, DROP). Traces >C> with name k. Outside a method: 98.992.
EXPOI s EXPOSE (x): x has just been exposed (EXPO); then every name in its value, from the object's variables. All names are checked first (31.3, 31.2, 20.928). Outside a method: 98.992.
USEL k USE LOCAL: the blank-separated names in k (a stem with its period), plus RC, RESULT, SIGL, SELF, SUPER, are local; every other variable of the activation is the object's, from its first reference on. Outside a method: 98.993.
USEARG i s a USE ARG to a simple variable: argument i present → traced >>>, assigned to s (the object itself; >=>), and jump to a; absent → fall through to the code that gives its default (or DROPV; USEOMIT under STRICT).
USEARGP i a → v USE ARG to a stem, a compound or a message term: argument i present → traced >>> and pushed for the store that follows (STORES, tail and STOREC, or the message); absent → a placeholder (NONE) is pushed and the code goes to a, where a POP and the default (or DROPS/DROPC; USEOMIT under STRICT) follow.
USEREF i s USE ARG >v / >s.: argument i must exist (88.931) and be a VariableReference (88.928) of the right kind (88.929, 88.930); the local variable must have no value (98.995); then s becomes an alias of the referenced variable. Traces >R>.
USESTRICT k n USE STRICT ARG: k is one letter per position — R required, O optional (has a default), S a skipped position (counts for the minimum, may be omitted) — followed by .; n = 1 when ... ends the list. Only the number of arguments is checked here: too many: 93.902 / 40.4; too few: 93.901 / 40.3 (method / routine).
USEOMIT i USE STRICT ARG: argument i, required (no default), is omitted: 93.903 / 40.5 (method / routine). It takes the place of the DROP when its variable's turn comes, so the variables before it are already assigned and their defaults evaluated, as natively.
GUARD w GUARD ON / OFF (w). Only in a method's activation (99.911). ON takes the lock of the method's scope unless held (may wait); OFF releases it.
GWHEN a k v → GUARD … WHEN: v (a list is folded to 1 when all items are 1) is traced >K> "WHEN" and must be 0 or 1 (34.902). 1: continue. 0: watch the exposed variables named in k, release the lock, wait until one of them is set or dropped, retake the lock, and go to a (the expression again).
REPLY, REPLYV (v) → REPLY [v]. Only in a method's activation (99.919), once (98.935): the caller continues as if the method returned (with v), the method continues in a new activity. Later RETURN/EXIT with a value: 98.936 / 98.937.
FWDOK FORWARD starts: must be in a method's activation (98.947), before any operand is evaluated.
FWD n i to msg cls args a1..ai → FORWARD. Operands TO, MESSAGE, CLASS, ARGUMENTS (NONE when absent; each present one traced >K>), then the i ARRAY items (i = −1: no ARRAY). Defaults: TO is SELF, MESSAGE the running method's name, the arguments this activation's. ARGUMENTS must be a one-dimensional Array (98.946; a string gives its lines); CLASS a class (88.914) and one of the receiver's scopes (93.957). n = 0: the method returns the forwarded result, and from this point the activation is a phantom (an error in the send goes past its traps); n = 1 (CONTINUE): RESULT gets the result (none: dropped).

Method activations and concurrency: 08-objects.md, 07-concurrency.md.

Jumps

Op Operands Stack Effect
JMP a Jump.
JF a w v → Jump if v is 0. v must be 0 or 1, else an error chosen by w: IF 34.1, WHEN 34.2, WHILE 34.3, UNTIL 34.4, LIST (an item of a condition list) 34.6, CASE (a WHEN of SELECT CASE) 34.905, anything else 34.901.
JT a w v → Jump if v is 1 (same check).

Clauses, trace and indentation

Op Operands Stack Effect
CLAUSE c w Clause c starts: slice accounting, pending CALL ON traps (then this instruction again), c becomes current, and it is traced according to w: N under TRACE A/R/I; C (a command) also under TRACE C; W (WHEN, OTHERWISE, the END of a SELECT without OTHERWISE) as N but no labels are emitted for it; E (an EXPOSE) as N, and it does not end the window in which a method's entry is traced; X never (implicit EXIT); Q never (an END, the DO of a new round, the clause errors of UNTIL and of the control-variable step are reported on).
LABEL c k A label (clause c, name k) of the clause that follows: pending traps first. Traced under TRACE L — every label reached sequentially; after SIGNAL or CALL to k, from that label on. Under ?L (and ?A/?R/?I) it pauses after tracing.
TCLAUSE c Traces clause c under TRACE A/R/I (THEN, ELSE, END, the DO of each new round).
BOUND End of a clause that starts no new one (after the condition of IF/WHEN, after a branch): pending traps run.
INDENT, UNINDENT One trace level more / less (not below 0).
UNIND2 Two levels less, if there are two.
BASE d Creates descriptor d with the current level.
SETIND d Back to the descriptor's level.

DO and LOOP

Op Operands Stack Effect
DOSTART d v → Initial value of the control variable (as a number, rounded to DIGITS).
DOTO, DOBY d v → Limit, step. Trace >K> TO / BY.
DOFOR d v → FOR count: a whole number ≥ 0 (26.3). Traces >K> FOR.
DOREP d v → The count of DO n (26.2). Traces >K> FOR.
DOINIT d s Default step 1; assigns the initial value to s (>=>).
DOSTEP d s Next round: s as it is now plus the step (traces >V>, >>>, >>>, >=>).
DOTEST d a Jump to a if TO has been passed (by the sign of the step) or FOR is exhausted (FOR is decremented here).
DOBODY d The body starts (the first round is over).
DOEXIT d Leaving through a test: the block's level, one less if a round was done.
DOOVER d v → DO … OVER: the items of v, taken now (traces >K> OVER): an array's items (holes skipped), a directory's indexes, a string's lines…; else 98.913.
DOOVERS d s v → DO … OVER a stem: v is its value (traced by the LOADS before, then >K> OVER); the items are the tails of stem s that have a value.
DONEXT d s a The next item to s (>=>), or jump to a when there are none. It precedes DOTEST: after the last round with FOR, the variable holds the unused item, as in ooRexx.
DOWITH d v → DO WITH … OVER: sends SUPPLIER to v (KEYW traced >K> WITH before); the result, kept for the loop, must be a Supplier (98.994).
DONEXTW d a A round of DO WITH: after the first one, the supplier's NEXT; when it is no longer AVAILABLE, jump to a.
WITHSET d s w The current index (w = I) or item (w = V) to the simple variable s (>=>).
WITHGET d w → v The same, pushed for the store that follows (stem, compound, message term).
DOINITP d → v DOINIT for a control variable that is not simple: default step, initial value pushed for the store.
DOSTEPP d v → v DOSTEP for such a variable: v is its current value (loaded before); v + step replaces it (traces >>>, >>>).
DOSKIP d a DO … OVER into a non-simple variable: move to the next item, or jump to a.
DOITEM d → v The current item, pushed for the store.
DOCTR d w → v COUNTER: w = Z sets the count to 0 (when the loop starts, before its expressions), N adds 1 (each round, once its checks have passed: the first body sees 1); the count is pushed for the store.
TRCTR d Traces >K> "COUNTER" with the count, after its store.

A DO WITH over one of the RVM's own suppliers reads it directly. When SUPPLIER returns an instance of a Rexx subclass of Supplier, the loop works by messages, as ooRexx does: DONEXTW sends NEXT (after the first round) and AVAILABLE (which must be 0 or 1: 34.906), WITHSET/WITHGET send INDEX and ITEM. The layout of a loop is shown in 03-compiler.md.

SELECT

Op Operands Stack Effect
CASE d v → Keeps the SELECT CASE value as it is.
CASEEQ d v → r A WHEN of SELECT CASE: the CASE value == v (ooRexx's WhenCaseInstruction), checked by the JT a CASE that follows. v is traced >>>, then r. An object CASE value gets the message == (no result: 91.999); a string or number compares its string with v's (0 when v is .nil).

Calls and returns

Op Operands Stack Effect
CALLI k n l a1..an → CALL to internal label k: new activation (SUB), SIGL = l.
FUNCI k n l a1..an → r Internal function call (FUNC); on return pushes r (no value: 44.1), traced >F>.
CALLB k n a1..an → Built-in function as a subroutine: RESULT.
FUNCB k n a1..an → r Built-in function. Traces >F>.
CALLX, FUNCX k n a1..an → (r) A routine that is neither internal nor built-in: a ::ROUTINE of the running program (any case), a public routine of a required package, a library routine, or an external program (search order: 05-machine.md). Not found: 43.1.
CALLNS, FUNCNS k k n a1..an → (r) CALL ns:name, ns:name(...): the public routine named by the second constant in the package with namespace the first (98.987; no such routine: 43.902); runs as CALLX would run it.
CALLN n l name a1..an → CALL (expr): the label with exactly that name, else a built-in function, else as CALLX.
RETURN, RETURNV (v) → Return (with a value). Pending traps run first. In the main program it is an EXIT. After REPLY, a value is 98.936.
PROC PROCEDURE: a new frame.
EXPV, EXPS s PROCEDURE EXPOSE of a simple variable / stem: the new frame gets the caller's variable itself (made, unset, in the caller when absent).
EXPC s n k p1..pn → EXPOSE of a compound (tail built with TAILC): the callee's tail shares the caller's variable. Traces >C> with name k.
EXPI s EXPOSE (x): x just exposed (EXPV); x read as LOADV reads it (NOVALUE; >V>), then every name in its value; all checked first (31.3, 31.2, 20.928).

NONE arguments are omitted arguments (ARG(n,'E') is 0).

SIGNAL, EXIT, conditions

Op Operands Stack Effect
SIGNAL k l Pending traps first; to label k (16.1), SIGL = l, trace level 0 (kept inside INTERPRET data); active INTERPRETs end.
SIGNALV l name → SIGNAL VALUE.
EXIT, EXITV (v) → End of the program (v: its result / return code). Pending traps run first in the main program; inside a routine they are lost, as in ooRexx.
TRAPON k w k SIGNAL ON (w = S) or CALL ON (w = C) of condition k (USER name for a user condition) with label k.
TRAPOFF k OFF.
RAISE K w i rc desc add a1..ai result → RAISE. K: the condition (0: PROPAGATE). w: RETURN or EXIT. i: ARRAY items (−1: no ARRAY). Absent operands are NONE; the code is a string (STR), the others stay objects. SYNTAX is an error raised in this activation (RETURN) or in the activation of its program, routine or method (EXIT). Any other condition: with RETURN this activation returns and the condition is raised in its caller; with EXIT the activation of the program, routine or method returns (the internal routines and handlers in between end) and it is raised in that one's caller; at the top it is reported and the program ends.
ERR x K Raise error x (with the constant K as additional text, 0: none).
ERRJ x K i Error 28.x of LEAVE/ITERATE: when i ≥ 0 the trace level first becomes the activation's level + i (computed by the compiler).

PARSE

Op Operands Stack Effect
PSRC w w Source: ARG, PULL, LINEIN, SOURCE, VERSION; options a word of U (UPPER), L (LOWER), C (CASELESS), or -. PULL reads the queue (or sends .STDQUE PULL: 13), or a line of standard input when it is empty.
PSRCV w w v → VAR or VALUE source (traces >K>).
PTPL n Template n starts: its source string (ARG n; UPPER/LOWER applied), traced >>>.
PSRCH pat → String pattern (caseless per the options).
PABS, PFWD, PBWD, PFLEN, PBLEN n → =n, +n, -n, >n, <n. n must be a non-negative whole number at the current DIGITS, else 26.4.
PEND Move to the end (the targets after the last trigger).
PWORD, PREST s Assign the next word / the rest to s (TRACE I >=>; TRACE R >>>).
PWORDC, PRESTC s n k p1..pn → The same to a compound; the tail is resolved when assigned. Traces >C> and >=> with name k (TRACE R >>>).
PWORDS, PRESTS s The same to a stem (whole-stem assignment).
PDOTW, PDOTR . placeholder: skip a word / the rest (traces >.> under TRACE I).
PWORDM, PRESTM → v v To a message term: the piece pushed twice, one for the message (SWAP, arguments, SENDA) and one for PTRM.
PTRM v → After the message: traces v >>> under TRACE R (TRACE I shows nothing more, as ooRexx's ParseTrigger).

Other instructions

Op Operands Stack Effect
SAY v → Traces >>>; writes to .OUTPUT (directly while it is the original one, else by message).
PUSH, QUEUE v → Traces >>>; to the current queue, or sends .STDQUE PUSH/QUEUE (13).
MKARR n a1..an → v Array term (a, b, ...): an Array of the n items (NONE: no item at that index), traced >>> (items were traced >A>).
SETCONST k v → In a class's K initializer: v becomes the value of calculated constant k (its instance and class methods).
NUMDIG, NUMFUZZ v|NONE → NUMERIC DIGITS / FUZZ (NONE: the package default).
NUMFORM w v|NONE → SCIENTIFIC, ENGINEERING, or VALUE (v must be one of the two, 25.11; NONE: the package default).
TRACE k TRACE with a constant setting.
TRACEV v → TRACE VALUE (a whole number: as TRACEN). In interactive debug outside a typed line, TRACE and TRACEV change nothing and the following TPAUSE pauses.
TRACEN k TRACE n: only in a line typed at a pause (else 24.901): skip the next |n| pauses, tracing nothing while n < 0.
ADDRT ADDRESS alone: swap the current and previous environments.
ADDRS k p ADDRESS env [WITH p]. Names over 250 characters: 29.1.
ADDRV p name → ADDRESS VALUE.
CMD cmd → Command to the current environment: RC, .RS, ERROR/FAILURE; traces >>> under TRACE C and the error/failure lines.
CMDTO k p cmd → ADDRESS env command [WITH p].
INTERP c data → INTERPRET (clause c; the data was traced by the RES before): compiles the data with the labels of the running body, appends it to the program and jumps to it. Cached per (instruction, data string).
IEND a c End of INTERPRET data: back to a, clause c current again; a SIGNAL/RETURN/EXIT typed at the IPAUSE is carried out now.
YIELD v → End of a thunk: v is its value.

Commands and redirections: 13-io-and-commands.md. INTERPRET: 03-compiler.md.

Interactive trace

At a pause a line is read from standard input: an empty line continues, = re-executes the clause, anything else is compiled as INTERPRET data ending in DEND and runs untraced, and the next line is read. Operand a of a pause is where = goes: the CLAUSE (or LABEL) of the clause. Details: 05-machine.md.

Op Operands Stack Effect
PAUSE a After an instruction that pauses (assignment, DROP, NOP, NUMERIC, OPTIONS, PARSE, ARG, PULL, PUSH, QUEUE, SAY, CALL, PROCEDURE, EXPOSE, ADDRESS without a command, ...), under TRACE ?A/?R/?I.
BPAUSE a n D In a block: IF and each WHEN after the condition (n = 1: its value is still on the stack), SELECT and a simple DO once entered. = drops n values and, when D > 0, restores descriptor D's indentation.
DOPAUSE a d A loop, after the checks of its first round only (d's indentation for =).
IPAUSE a data → data INTERPRET, before the data runs (= drops the data); a SIGNAL, RETURN or EXIT typed there waits for the data to end (IEND).
TPAUSE a After a TRACE that interactive debug ignored.
CPAUSE a After a traced command (TRACE ?A/?R/?I/?C, or ?E/?F/?N when it fails).
JMARK c a a A LEAVE or ITERATE (clause c) about to jump, when pausing: it pauses where it lands. = there goes to operand 2 (JPAUSE R) or 3 (JPAUSE E): compiled "redo" pieces that trace c again and jump as the instruction would from there.
JPAUSE w Where a LEAVE/ITERATE lands: R after the checks of the next round, E at the end of the block. Pauses only when a JMARK is pending.
DEND End of a line typed at a pause: read the next line (or end the pause after a TRACE typed there).

Fragments and relocation

A host that compiles apart (the JS host: 03-compiler.md) receives INTERPRET data and typed lines as fragments: R-code of their own (RVM.Compiler~fragment, written by rvm.rex -f), with constants, names, clauses, descriptors and redirections numbered from 1, code from address 1, SOURCE -, ending with IEND 0 0 (or DEND). appendFragment (rcode.mjs) reads it without verifying, appends its tables and code to the program, and relocates every operand by class: k (and non-zero K) by the number of constants, c by the clauses, d (non-zero D) by the descriptors, p (non-zero) by the redirections, a and THUNK values by the code size; an s becomes the program's slot for that name (a new slot when the name is new). The final IEND gets the return address and the INTERPRET's clause. Then the verifier runs over the new code, from its start and its thunks. The ooRexx host does not need relocation: it compiles the data directly onto the program's tables (RVM.Compiler~extend).

Versions

Three constants, referred to by name (their values change with the code):

Constant Where Bumped when
RVM.RCode~VERSION RVM.Machine.cls; RCODE_VERSION in rcode.mjs the file format changes (a section, a field, a line layout).
RVM.ISA~VERSION RVM.Machine.cls; ISA_VERSION in rcode.mjs an instruction is added, removed, or changes its operands or meaning.
RVM.Compiler~VERSION RVM.Compiler.cls (the JS Node host reads it from that file's source) the code the compiler generates changes.

Next to them, the R-code records the Rexx Parser's frontier stamp (RVM.Compiler~frontier, from bin/resources/frontier), which the parser raises; like the compiler version, it only invalidates caches (03).

A reader rejects R-code of another format or ISA version (93.900, "compile the program again"); the compiler version only invalidates caches. The two JS constants must be changed together with the Rexx ones, and the embedded prelude (rvm-prelude.mjs, rvm-prelude-streams.mjs) regenerated (10-prelude-and-classes.md), or every JS run fails with 93.900.

Design notes

  • Text, not binary. R-code is readable and diffable: rvm -l is the debugging view, test expectations can compare listings, and the ooRexx host always runs a program from the text it has just written (rvm.rex writes with RVM.RCode~write and reads back with RVM.RCode~read), so the format is exercised on every run, not only by the JS host.
  • Instructions carry Rexx semantics. Every operation that can raise its own error or appear in the trace is one instruction: ADD is the full Rexx addition, JF a IF is the IF test with error 34.1. The stack is untyped (strings, Numbers, objects); specialised integer instructions were deliberately left out.
  • Each instruction knows its trace tag. TRACE I falls out of the code. Corollary: a generic opcode when the operation is written in the source, a dedicated one when it is implicit — the DO step, the FOR count and the TO limit have their own instructions, because a generic ADD in the step would trace an >O> line that ooRexx does not write.
  • One opcode per comparison, with the operator as written as operand: the operand is only for the trace and for the message sent to an object (\= vs <>).
  • One variable numbering per program. Slots are global to the program, so EXPOSE is cell sharing and PROCEDURE a new frame, with no per-routine symbol tables; names unknown at compile time go by name.
  • CLAUSE at the start of every clause is the single hook for the trace, interactive pauses (= returns there), per-clause time slicing, SIGL and error lines, and pending CALL ON traps. With the empty-stack-at-clause invariant, a trap, a pause or a SIGNAL can be taken at any clause boundary without unwinding values.
  • The verifier makes a malformed or mismatched R-code (a stale cache, a hand-edited file, a relocation bug) fail at load time with an address and a reason, instead of corrupting a run; it also catches compiler errors in stack discipline (an instruction with the wrong pop count).
  • Conversion by re-execution avoids a nested machine for STRING methods written in Rexx: the instruction restarts with the string supplied.
  • EXTERNAL methods of a library ooRexx has but the RVM lacks fail when called, not when loaded, so that a package wrapping such a library still loads and its other methods work (an accepted divergence: 17).
  • Bare words for variants (PSRC ARG U, NUMFORM SCIENTIFIC, JF a WHILE) keep the constant pool for program data and the listing readable.

Known gaps

  • The WITHS class ENV is accepted by the readers but never generated; both machines reject it at run time (93.900).