libose
Loading...
Searching...
No Matches
osevm_lib.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2019-23 John MacCallum Permission is hereby
3 granted, free of charge, to any person obtaining a copy of this
4 software and associated documentation files (the "Software"), to
5 deal in the Software without restriction, including without
6 limitation the rights to use, copy, modify, merge, publish,
7 distribute, sublicense, and/or sell copies of the Software, and
8 to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be
12 included in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
18 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
19 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
22*/
23
28#ifndef OSEVM_LIB_H
29#define OSEVM_LIB_H
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
46#define OSEVM_WRAP_DECL(name) \
47 void osevm_##name(ose_bundle bundle);
48
49/* The wrappers below clear the child's errno before doing anything.
50
51 errno on /_s is a return channel for one call: cleared on entry, its value
52 afterwards means "this call failed". Without the clear, a value left there
53 by anything earlier is indistinguishable from one this operation set, and
54 gets charged to whichever instruction runs next -- the "every instruction
55 leaves residue" bug. A host that calls
56 ose_drop() on an empty stack during setup is enough to trigger it, and the
57 diagnostic then lands on the first instruction of the first program.
58
59 Nothing is lost by discarding it. Each wrapper propagates its own failure
60 to `bundle` before returning, and the clear only touches the child, so
61 errors still accumulate on the VM within an instruction. What goes away is
62 a stale value that nobody was ever going to attribute correctly. */
63
73#define OSEVM_WRAP_DEFN(name) \
74 void osevm_##name(ose_bundle bundle) \
75 { \
76 ose_bundle vm_s = OSEVM_STACK(bundle); \
77 enum ose_errno e = OSE_ERR_NONE; \
78 ose_errno_set(vm_s, OSE_ERR_NONE); \
79 ose_##name(vm_s); \
80 if((e = ose_errno_get(vm_s))) \
81 { \
82 ose_errno_set(bundle, e); \
83 ose_errno_set(vm_s, OSE_ERR_NONE); \
84 } \
85 }
86
94#define OSEVM_WRAP_DEFPRED(name) \
95 void osevm_##name(ose_bundle bundle) \
96 { \
97 ose_bundle vm_s = OSEVM_STACK(bundle); \
98 int32_t i; \
99 ose_errno_set(vm_s, OSE_ERR_NONE); \
100 i = ose_popInt32(vm_s); \
101 enum ose_errno e = OSE_ERR_NONE; \
102 if((e = ose_errno_get(vm_s))) \
103 { \
104 ose_errno_set(bundle, e); \
105 ose_errno_set(vm_s, OSE_ERR_NONE); \
106 } \
107 bool r = ose_##name(i); \
108 ose_pushInt32(vm_s, r == true ? 1 : 0); \
109 }
110
111
112/* A frame is one element of the dump.
113 *
114 * One element, one ose_spaceAvailable(), one answer -- which is the
115 * invariant osevm_return() needs and cannot otherwise assume. Built instead
116 * from four appends, a frame can be written in part: ose_copyBundle() asks
117 * ose_incSize(), is refused when the register is full, and returns without
118 * writing. With /_d nearly full the small environment marker fits and goes
119 * in while input, stack and control do not, leaving a frame with one element
120 * where it should have four. osevm_return() then pops four, takes the
121 * missing three out of the frames underneath, and the dump's structure is
122 * gone with nothing able to tell that it happened.
123 *
124 * An empty register is omitted. Three of a frame's four are usually empty
125 * bundles at 16 bytes plus a 4-byte prefix; over 5,000 frames of a recursion
126 * that came to 7% of frame bytes, against the 20 bytes this shape's wrapper
127 * adds to an average frame of 769 -- about 5% of the dump given back. Which
128 * registers are carried is recorded in the frame bundle's timetag, where a
129 * bundle header already spends eight bytes that nothing in this path reads.
130 * A layer above this one keeps its own markers in the same eight bytes, for
131 * the same reason, and neither reads the other's: a frame's timetag belongs
132 * to the dump, and the dump belongs here.
133 *
134 * [int32 size]["#bundle\0"][int32 magic][int32 mask][the carried ones]
135 *
136 * A register that is not carried is restored empty, which is exactly what
137 * restoring an empty saved copy did before: the shape changed and the
138 * semantics did not.
139 *
140 * There is no compatibility story for frames written in the older shape.
141 * The magic is what makes an old machine a crash with a name on it rather
142 * than a silent misread.
143 */
148#define OSEVM_FRAME_MAGIC 0x2346524D /* "#FRM" */
149enum
150{
151 OSEVM_FRAME_INPUT = 1,
152 OSEVM_FRAME_ENV = 2,
153 OSEVM_FRAME_STACK = 4,
154 OSEVM_FRAME_CONTROL = 8,
155 /* The stack below its topmost element, which is the stack the callee
156 does not get: the element on top is the body about to run, and a
157 caller that saves the whole stack gets it handed back on top of
158 whatever the body left. #osevm_exec1 says this by saving no stack at
159 all, because it has already moved that element into the input. An
160 interpreting client whose own exec has work to interleave saves the
161 rest, and has to say which rest. */
162 OSEVM_FRAME_STACK_BELOW = 16
163};
164
193 int32_t which,
194 const char * const env,
195 int32_t envlen);
196
216void osevm_exec1(ose_bundle osevm);
217
234void osevm_exec2(ose_bundle osevm);
235
252void osevm_exec3(ose_bundle osevm);
253
258void osevm_exec(ose_bundle osevm);
259
276void osevm_if(ose_bundle osevm);
277
291void osevm_dotimes(ose_bundle osevm);
292
384void osevm_apply(ose_bundle osevm);
406void osevm_map(ose_bundle osevm);
424void osevm_return(ose_bundle osevm);
425
433void osevm_version(ose_bundle osevm);
481 ose_bundle env,
482 int replace,
483 int new_msg_first,
484 char bundle_typetag);
523void osevm_lookupInEnv(ose_bundle osevm);
548void osevm_funcall(ose_bundle osevm);
572void osevm_quote(ose_bundle bundle);
573
575OSEVM_WRAP_DECL(toType)
577OSEVM_WRAP_DECL(toInt32)
579OSEVM_WRAP_DECL(toFloat)
581OSEVM_WRAP_DECL(toString)
583OSEVM_WRAP_DECL(toBlob)
585OSEVM_WRAP_DECL(appendByte)
586#ifdef OSE_PROVIDE_TYPE_SYMBOL
588OSEVM_WRAP_DECL(toSymbol)
589#endif
590#ifdef OSE_PROVIDE_TYPE_DOUBLE
592OSEVM_WRAP_DECL(toDouble)
593#endif
594#ifdef OSE_PROVIDE_TYPE_INT8
596OSEVM_WRAP_DECL(toInt8)
597#endif
598#ifdef OSE_PROVIDE_TYPE_UINT8
600OSEVM_WRAP_DECL(toUInt8)
601#endif
602#ifdef OSE_PROVIDE_TYPE_INT16
604OSEVM_WRAP_DECL(toInt16)
605#endif
606#ifdef OSE_PROVIDE_TYPE_UINT16
608OSEVM_WRAP_DECL(toUInt16)
609#endif
610#ifdef OSE_PROVIDE_TYPE_UINT32
612OSEVM_WRAP_DECL(toUInt32)
613#endif
614#ifdef OSE_PROVIDE_TYPE_INT64
616OSEVM_WRAP_DECL(toInt64)
617#endif
618#ifdef OSE_PROVIDE_TYPE_UINT64
620OSEVM_WRAP_DECL(toUInt64)
621#endif
622#ifdef OSE_PROVIDE_TYPE_TIMETAG
624OSEVM_WRAP_DECL(toTimetag)
625#endif
626#ifdef OSE_PROVIDE_TYPE_TRUE
628OSEVM_WRAP_DECL(toTrue)
629#endif
630#ifdef OSE_PROVIDE_TYPE_FALSE
632OSEVM_WRAP_DECL(toFalse)
633#endif
634#ifdef OSE_PROVIDE_TYPE_NIL
636OSEVM_WRAP_DECL(toNil)
637#endif
638#ifdef OSE_PROVIDE_TYPE_INFINITUM
640OSEVM_WRAP_DECL(toInfinitum)
641#endif
642
643#ifdef __cplusplus
644}
645#endif
646
647#endif
void osevm_appendElemToRegister(ose_bundle osevm)
Append the contents of the top element to a register – /<<.
Definition osevm_lib.c:455
void osevm_moveElemToRegister(ose_bundle osevm)
Move the top element into a register – /-.
Definition osevm_lib.c:479
void osevm_copyElemToRegister(ose_bundle osevm)
Copy the top element into a register.
Definition osevm_lib.c:491
void osevm_makeRegister(ose_bundle osevm)
Create a register from a size and a name on the stack; the standard library binds it as /o/make/regis...
Definition osevm_lib.c:1618
void osevm_if(ose_bundle osevm)
Execute one of two bundles, depending on the value of the int in the last message on the stack.
Definition osevm_lib.c:333
int osevm_pushFrame(ose_bundle osevm, int32_t which, const char *const env, int32_t envlen)
Save registers to the dump as ONE element – whole, or not at all.
Definition osevm_lib.c:88
void osevm_dotimes(ose_bundle osevm)
Execute a bundle repeatedly.
Definition osevm_lib.c:348
void osevm_lookupInEnv_impl(ose_bundle osevm)
Look a name up – the /$ instruction, and the default OSEVM_LOOKUP.
Definition osevm_lib.c:1537
void osevm_assignStackToEnv(ose_bundle osevm)
Bind the stack into the environment – the /@ instruction, and the default OSEVM_ASSIGN.
Definition osevm_lib.c:1507
void osevm_funcall(ose_bundle osevm)
Look a name up and apply what it finds – the /! instruction on a string, and the default OSEVM_FUNCAL...
Definition osevm_lib.c:1598
void osevm_exec1(ose_bundle osevm)
Execute a bundle on the stack.
Definition osevm_lib.c:206
void osevm_exec(ose_bundle osevm)
Synonym for osevm_exec2.
Definition osevm_lib.c:328
void osevm_exec3(ose_bundle osevm)
Execute a bundle on the stack.
Definition osevm_lib.c:291
void osevm_version(ose_bundle osevm)
Push the library's version – /!/version.
Definition osevm_lib.c:1084
void osevm_plookupInEnv_impl(ose_bundle osevm)
Does nothing.
Definition osevm_lib.c:1587
void osevm_return(ose_bundle osevm)
Pop the topmost frame from the dump and restore what it carried – /!/return, and what the VM runs whe...
Definition osevm_lib.c:1075
void osevm_map(ose_bundle osevm)
Apply a function to each item of a message, or each element of a bundle – /!/map.
Definition osevm_lib.c:699
void osevm_plookupInEnv(ose_bundle osevm)
Does nothing.
Definition osevm_lib.c:1594
void osevm_replaceRegisterWithElem(ose_bundle osevm)
Replace a register's contents with the top element's – /<.
Definition osevm_lib.c:467
void osevm_lookupInEnv(ose_bundle osevm)
Call whatever OSEVM_LOOKUP names.
Definition osevm_lib.c:1579
void osevm_apply(ose_bundle osevm)
Apply the top of the stack – the /! instruction, and the second half of osevm_funcall.
Definition osevm_lib.c:504
void osevm_assignStackToEnv_impl(ose_bundle osevm, ose_bundle env, int replace, int new_msg_first, char bundle_typetag)
Bind everything on the stack, as one message, into a bundle.
Definition osevm_lib.c:1166
void osevm_quote(ose_bundle bundle)
Does nothing – the ‘/’` instruction, and the default OSEVM_QUOTE.
Definition osevm_lib.c:1651
#define OSEVM_WRAP_DECL(name)
Declare void osevm_<name>(ose_bundle osevm), the VM-level form of the stack operation ose_<name>().
Definition osevm_lib.h:46
void osevm_exec2(ose_bundle osevm)
Execute a bundle on the stack.
Definition osevm_lib.c:257
void osevm_assignStackToRegister(ose_bundle osevm)
Gather the stack into the bundle at its bottom, as one message named by the string on top.
Definition osevm_lib.c:1099
void osevm_copyRegisterToElem(ose_bundle osevm)
Copy a register onto the stack as one element – the /> instruction.
Definition osevm_lib.c:435
Definition ose.h:374