asoma.dev · the C reference for libose
libose
the C library every o.se host is built on
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
277void osevm_call(ose_bundle osevm);
278
283void osevm_exec(ose_bundle osevm);
284
301void osevm_if(ose_bundle osevm);
302
316void osevm_dotimes(ose_bundle osevm);
317
412void osevm_apply(ose_bundle osevm);
434void osevm_map(ose_bundle osevm);
452void osevm_return(ose_bundle osevm);
453
461void osevm_version(ose_bundle osevm);
488 ose_bundle env,
489 int replace,
490 int new_msg_first,
491 char bundle_typetag);
530void osevm_lookupInEnv(ose_bundle osevm);
555void osevm_funcall(ose_bundle osevm);
579void osevm_quote(ose_bundle bundle);
580
582OSEVM_WRAP_DECL(toType)
584OSEVM_WRAP_DECL(toInt32)
586OSEVM_WRAP_DECL(toFloat)
588OSEVM_WRAP_DECL(toString)
590OSEVM_WRAP_DECL(toBlob)
592OSEVM_WRAP_DECL(appendByte)
593#ifdef OSE_PROVIDE_TYPE_SYMBOL
595OSEVM_WRAP_DECL(toSymbol)
596#endif
597#ifdef OSE_PROVIDE_TYPE_DOUBLE
599OSEVM_WRAP_DECL(toDouble)
600#endif
601#ifdef OSE_PROVIDE_TYPE_INT8
603OSEVM_WRAP_DECL(toInt8)
604#endif
605#ifdef OSE_PROVIDE_TYPE_UINT8
607OSEVM_WRAP_DECL(toUInt8)
608#endif
609#ifdef OSE_PROVIDE_TYPE_INT16
611OSEVM_WRAP_DECL(toInt16)
612#endif
613#ifdef OSE_PROVIDE_TYPE_UINT16
615OSEVM_WRAP_DECL(toUInt16)
616#endif
617#ifdef OSE_PROVIDE_TYPE_UINT32
619OSEVM_WRAP_DECL(toUInt32)
620#endif
621#ifdef OSE_PROVIDE_TYPE_INT64
623OSEVM_WRAP_DECL(toInt64)
624#endif
625#ifdef OSE_PROVIDE_TYPE_UINT64
627OSEVM_WRAP_DECL(toUInt64)
628#endif
629#ifdef OSE_PROVIDE_TYPE_TIMETAG
631OSEVM_WRAP_DECL(toTimetag)
632#endif
633#ifdef OSE_PROVIDE_TYPE_TRUE
635OSEVM_WRAP_DECL(toTrue)
636#endif
637#ifdef OSE_PROVIDE_TYPE_FALSE
639OSEVM_WRAP_DECL(toFalse)
640#endif
641#ifdef OSE_PROVIDE_TYPE_NIL
643OSEVM_WRAP_DECL(toNil)
644#endif
645#ifdef OSE_PROVIDE_TYPE_INFINITUM
647OSEVM_WRAP_DECL(toInfinitum)
648#endif
649
661
662#ifdef __cplusplus
663}
664#endif
665
666#endif
ose_errno
Definition ose_errno.h:37
void osevm_appendElemToRegister(ose_bundle osevm)
Append the contents of the top element to a register – /<<.
Definition osevm_lib.c:516
void osevm_moveElemToRegister(ose_bundle osevm)
Move the top element into a register – /-.
Definition osevm_lib.c:540
void osevm_copyElemToRegister(ose_bundle osevm)
Copy the top element into a register.
Definition osevm_lib.c:552
enum ose_errno osevm_resolveWildPtr(ose_bundle osevm)
Replace the pointer by name ('P') on top of the stack with what /$ of its name would push: the VM ope...
Definition osevm_lib.c:572
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:1740
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:394
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:409
void osevm_lookupInEnv_impl(ose_bundle osevm)
Look a name up – the /$ instruction, and the default OSEVM_LOOKUP.
Definition osevm_lib.c:1622
void osevm_assignStackToEnv(ose_bundle osevm)
Bind the stack into the environment – the /@ instruction, and the default OSEVM_ASSIGN.
Definition osevm_lib.c:1592
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:1683
void osevm_exec1(ose_bundle osevm)
Execute a bundle on the stack.
Definition osevm_lib.c:206
void osevm_call(ose_bundle osevm)
Execute a bundle as a function: osevm_exec2, with the caller's stack set aside.
Definition osevm_lib.c:337
void osevm_exec(ose_bundle osevm)
Synonym for osevm_exec2.
Definition osevm_lib.c:389
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:1222
void osevm_plookupInEnv_impl(ose_bundle osevm)
Does nothing.
Definition osevm_lib.c:1672
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:1213
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:837
void osevm_plookupInEnv(ose_bundle osevm)
Does nothing.
Definition osevm_lib.c:1679
void osevm_replaceRegisterWithElem(ose_bundle osevm)
Replace a register's contents with the top element's – /<.
Definition osevm_lib.c:528
void osevm_lookupInEnv(ose_bundle osevm)
Call whatever OSEVM_LOOKUP names.
Definition osevm_lib.c:1664
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:637
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:1251
void osevm_quote(ose_bundle bundle)
Does nothing – the ‘/’` instruction, and the default OSEVM_QUOTE.
Definition osevm_lib.c:1773
#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_copyRegisterToElem(ose_bundle osevm)
Copy a register onto the stack as one element – the /> instruction.
Definition osevm_lib.c:496
Definition ose.h:400