xref: /freebsd/contrib/bc/src/program.c (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
1 /*
2  * *****************************************************************************
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c) 2018-2023 Gavin D. Howard and contributors.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * * Redistributions of source code must retain the above copyright notice, this
12  *   list of conditions and the following disclaimer.
13  *
14  * * Redistributions in binary form must reproduce the above copyright notice,
15  *   this list of conditions and the following disclaimer in the documentation
16  *   and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * *****************************************************************************
31  *
32  * Code to execute bc programs.
33  *
34  */
35 
36 #include <assert.h>
37 #include <stdbool.h>
38 #include <string.h>
39 
40 #include <setjmp.h>
41 
42 #include <signal.h>
43 
44 #include <time.h>
45 
46 #include <read.h>
47 #include <parse.h>
48 #include <program.h>
49 #include <vm.h>
50 
51 /**
52  * Does a type check for something that expects a number.
53  * @param r  The result that will be checked.
54  * @param n  The result's number.
55  */
56 static inline void
57 bc_program_type_num(BcResult* r, BcNum* n)
58 {
59 #if BC_ENABLED
60 
61 	// This should have already been taken care of.
62 	assert(r->t != BC_RESULT_VOID);
63 
64 #endif // BC_ENABLED
65 
66 	if (BC_ERR(!BC_PROG_NUM(r, n))) bc_err(BC_ERR_EXEC_TYPE);
67 }
68 
69 #if BC_ENABLED
70 
71 /**
72  * Does a type check.
73  * @param r  The result to check.
74  * @param t  The type that the result should be.
75  */
76 static void
77 bc_program_type_match(BcResult* r, BcType t)
78 {
79 	if (BC_ERR((r->t != BC_RESULT_ARRAY) != (!t))) bc_err(BC_ERR_EXEC_TYPE);
80 }
81 #endif // BC_ENABLED
82 
83 /**
84  * Pulls an index out of a bytecode vector and updates the index into the vector
85  * to point to the spot after the index. For more details on bytecode indices,
86  * see the development manual (manuals/development.md#bytecode-indices).
87  * @param code  The bytecode vector.
88  * @param bgn   An in/out parameter; the index into the vector that will be
89  *              updated.
90  * @return      The index at @a bgn in the bytecode vector.
91  */
92 static size_t
93 bc_program_index(const char* restrict code, size_t* restrict bgn)
94 {
95 	uchar amt = (uchar) code[(*bgn)++], i = 0;
96 	size_t res = 0;
97 
98 	for (; i < amt; ++i, ++(*bgn))
99 	{
100 		size_t temp = ((size_t) ((int) (uchar) code[*bgn]) & UCHAR_MAX);
101 		res |= (temp << (i * CHAR_BIT));
102 	}
103 
104 	return res;
105 }
106 
107 /**
108  * Returns a string from a result and its number.
109  * @param p  The program.
110  * @param n  The number tied to the result.
111  * @return   The string corresponding to the result and number.
112  */
113 static inline char*
114 bc_program_string(BcProgram* p, const BcNum* n)
115 {
116 	return *((char**) bc_vec_item(&p->strs, n->scale));
117 }
118 
119 #if BC_ENABLED
120 
121 /**
122  * Prepares the globals for a function call. This is only called when global
123  * stacks are on because it pushes a copy of the current globals onto each of
124  * their respective stacks.
125  * @param p  The program.
126  */
127 static void
128 bc_program_prepGlobals(BcProgram* p)
129 {
130 	size_t i;
131 
132 	for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i)
133 	{
134 		bc_vec_push(p->globals_v + i, p->globals + i);
135 	}
136 
137 #if BC_ENABLE_EXTRA_MATH
138 	bc_rand_push(&p->rng);
139 #endif // BC_ENABLE_EXTRA_MATH
140 }
141 
142 /**
143  * Pops globals stacks on returning from a function, or in the case of reset,
144  * pops all but one item on each global stack.
145  * @param p      The program.
146  * @param reset  True if all but one item on each stack should be popped, false
147  *               otherwise.
148  */
149 static void
150 bc_program_popGlobals(BcProgram* p, bool reset)
151 {
152 	size_t i;
153 
154 	BC_SIG_ASSERT_LOCKED;
155 
156 	for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i)
157 	{
158 		BcVec* v = p->globals_v + i;
159 		bc_vec_npop(v, reset ? v->len - 1 : 1);
160 		p->globals[i] = BC_PROG_GLOBAL(v);
161 	}
162 
163 #if BC_ENABLE_EXTRA_MATH
164 	bc_rand_pop(&p->rng, reset);
165 #endif // BC_ENABLE_EXTRA_MATH
166 }
167 
168 /**
169  * Derefeneces an array reference and returns a pointer to the real array.
170  * @param p    The program.
171  * @param vec  The reference vector.
172  * @return     A pointer to the desired array.
173  */
174 static BcVec*
175 bc_program_dereference(const BcProgram* p, BcVec* vec)
176 {
177 	BcVec* v;
178 	size_t vidx, nidx, i = 0;
179 
180 	// We want to be sure we have a reference vector.
181 	assert(vec->size == sizeof(uchar));
182 
183 	// Get the index of the vector in arrs, then the index of the original
184 	// referenced vector.
185 	vidx = bc_program_index(vec->v, &i);
186 	nidx = bc_program_index(vec->v, &i);
187 
188 	v = bc_vec_item(bc_vec_item(&p->arrs, vidx), nidx);
189 
190 	// We want to be sure we do *not* have a reference vector.
191 	assert(v->size != sizeof(uchar));
192 
193 	return v;
194 }
195 
196 #endif // BC_ENABLED
197 
198 /**
199  * Creates a BcNum from a BcBigDig and pushes onto the results stack. This is a
200  * convenience function.
201  * @param p     The program.
202  * @param dig   The BcBigDig to push onto the results stack.
203  * @param type  The type that the pushed result should be.
204  */
205 static void
206 bc_program_pushBigdig(BcProgram* p, BcBigDig dig, BcResultType type)
207 {
208 	BcResult res;
209 
210 	res.t = type;
211 
212 	BC_SIG_LOCK;
213 
214 	bc_num_createFromBigdig(&res.d.n, dig);
215 	bc_vec_push(&p->results, &res);
216 
217 	BC_SIG_UNLOCK;
218 }
219 
220 size_t
221 bc_program_addString(BcProgram* p, const char* str)
222 {
223 	size_t idx;
224 
225 	BC_SIG_ASSERT_LOCKED;
226 
227 	if (bc_map_insert(&p->str_map, str, p->strs.len, &idx))
228 	{
229 		char** str_ptr;
230 		BcId* id = bc_vec_item(&p->str_map, idx);
231 
232 		// Get the index.
233 		idx = id->idx;
234 
235 		// Push an empty string on the proper vector.
236 		str_ptr = bc_vec_pushEmpty(&p->strs);
237 
238 		// We reuse the string in the ID (allocated by bc_map_insert()), because
239 		// why not?
240 		*str_ptr = id->name;
241 	}
242 	else
243 	{
244 		BcId* id = bc_vec_item(&p->str_map, idx);
245 		idx = id->idx;
246 	}
247 
248 	return idx;
249 }
250 
251 size_t
252 bc_program_search(BcProgram* p, const char* name, bool var)
253 {
254 	BcVec* v;
255 	BcVec* map;
256 	size_t i;
257 
258 	BC_SIG_ASSERT_LOCKED;
259 
260 	// Grab the right vector and map.
261 	v = var ? &p->vars : &p->arrs;
262 	map = var ? &p->var_map : &p->arr_map;
263 
264 	// We do an insert because the variable might not exist yet. This is because
265 	// the parser calls this function. If the insert succeeds, we create a stack
266 	// for the variable/array. But regardless, bc_map_insert() gives us the
267 	// index of the item in i.
268 	if (bc_map_insert(map, name, v->len, &i))
269 	{
270 		BcVec* temp = bc_vec_pushEmpty(v);
271 		bc_array_init(temp, var);
272 	}
273 
274 	return ((BcId*) bc_vec_item(map, i))->idx;
275 }
276 
277 /**
278  * Returns the correct variable or array stack for the type.
279  * @param p     The program.
280  * @param idx   The index of the variable or array in the variable or array
281  *              vector.
282  * @param type  The type of vector to return.
283  * @return      A pointer to the variable or array stack.
284  */
285 static inline BcVec*
286 bc_program_vec(const BcProgram* p, size_t idx, BcType type)
287 {
288 	const BcVec* v = (type == BC_TYPE_VAR) ? &p->vars : &p->arrs;
289 	return bc_vec_item(v, idx);
290 }
291 
292 /**
293  * Returns a pointer to the BcNum corresponding to the result. There is one
294  * case, however, where this returns a pointer to a BcVec: if the type of the
295  * result is array. In that case, the pointer is casted to a pointer to BcNum,
296  * but is never used. The function that calls this expecting an array casts the
297  * pointer back. This function is called a lot and needs to be as fast as
298  * possible.
299  * @param p  The program.
300  * @param r  The result whose number will be returned.
301  * @return   The BcNum corresponding to the result.
302  */
303 static BcNum*
304 bc_program_num(BcProgram* p, BcResult* r)
305 {
306 	BcNum* n;
307 
308 #ifdef _WIN32
309 	// Windows made it an error to not initialize this, so shut it up.
310 	// I don't want to do this on other platforms because this procedure
311 	// is one of the most heavily-used, and eliminating the initialization
312 	// is a performance win.
313 	n = NULL;
314 #endif // _WIN32
315 
316 	switch (r->t)
317 	{
318 		case BC_RESULT_STR:
319 		case BC_RESULT_TEMP:
320 		case BC_RESULT_IBASE:
321 		case BC_RESULT_SCALE:
322 		case BC_RESULT_OBASE:
323 #if BC_ENABLE_EXTRA_MATH
324 		case BC_RESULT_SEED:
325 #endif // BC_ENABLE_EXTRA_MATH
326 		{
327 			n = &r->d.n;
328 			break;
329 		}
330 
331 		case BC_RESULT_VAR:
332 		case BC_RESULT_ARRAY:
333 		case BC_RESULT_ARRAY_ELEM:
334 		{
335 			BcVec* v;
336 			BcType type = (r->t == BC_RESULT_VAR) ? BC_TYPE_VAR : BC_TYPE_ARRAY;
337 
338 			// Get the correct variable or array vector.
339 			v = bc_program_vec(p, r->d.loc.loc, type);
340 
341 			// Surprisingly enough, the hard case is *not* returning an array;
342 			// it's returning an array element. This is because we have to dig
343 			// deeper to get *to* the element. That's what the code inside this
344 			// if statement does.
345 			if (r->t == BC_RESULT_ARRAY_ELEM)
346 			{
347 				size_t idx = r->d.loc.idx;
348 
349 				v = bc_vec_item(v, r->d.loc.stack_idx);
350 
351 #if BC_ENABLED
352 				// If this is true, we have a reference vector, so dereference
353 				// it. The reason we don't need to worry about it for returning
354 				// a straight array is because we only care about references
355 				// when we access elements of an array that is a reference. That
356 				// is this code, so in essence, this line takes care of arrays
357 				// as well.
358 				if (v->size == sizeof(uchar)) v = bc_program_dereference(p, v);
359 #endif // BC_ENABLED
360 
361 				// We want to be sure we got a valid array of numbers.
362 				assert(v->size == sizeof(BcNum));
363 
364 				// The bc spec says that if an element is accessed that does not
365 				// exist, it should be preinitialized to 0. Well, if we access
366 				// an element *way* out there, we have to preinitialize all
367 				// elements between the current last element and the actual
368 				// accessed element.
369 				if (v->len <= idx)
370 				{
371 					BC_SIG_LOCK;
372 					bc_array_expand(v, bc_vm_growSize(idx, 1));
373 					BC_SIG_UNLOCK;
374 				}
375 
376 				n = bc_vec_item(v, idx);
377 			}
378 			// This is either a number (for a var) or an array (for an array).
379 			// Because bc_vec_top() and bc_vec_item() return a void*, we don't
380 			// need to cast.
381 			else
382 			{
383 #if BC_ENABLED
384 				if (BC_IS_BC)
385 				{
386 					n = bc_vec_item(v, r->d.loc.stack_idx);
387 				}
388 				else
389 #endif // BC_ENABLED
390 				{
391 					n = bc_vec_top(v);
392 				}
393 			}
394 
395 			break;
396 		}
397 
398 		case BC_RESULT_ZERO:
399 		{
400 			n = &vm->zero;
401 			break;
402 		}
403 
404 		case BC_RESULT_ONE:
405 		{
406 			n = &vm->one;
407 			break;
408 		}
409 
410 #if BC_ENABLED
411 		// We should never get here; this is taken care of earlier because a
412 		// result is expected.
413 		case BC_RESULT_VOID:
414 #ifndef NDEBUG
415 		{
416 			abort();
417 			// Fallthrough
418 		}
419 #endif // NDEBUG
420 		case BC_RESULT_LAST:
421 		{
422 			n = &p->last;
423 			break;
424 		}
425 #endif // BC_ENABLED
426 
427 #if BC_GCC
428 		// This is here in GCC to quiet the "maybe-uninitialized" warning.
429 		default:
430 		{
431 			abort();
432 		}
433 #endif // BC_GCC
434 	}
435 
436 	return n;
437 }
438 
439 /**
440  * Prepares an operand for use.
441  * @param p    The program.
442  * @param r    An out parameter; this is set to the pointer to the result that
443  *             we care about.
444  * @param n    An out parameter; this is set to the pointer to the number that
445  *             we care about.
446  * @param idx  The index of the result from the top of the results stack.
447  */
448 static void
449 bc_program_operand(BcProgram* p, BcResult** r, BcNum** n, size_t idx)
450 {
451 	*r = bc_vec_item_rev(&p->results, idx);
452 
453 #if BC_ENABLED
454 	if (BC_ERR((*r)->t == BC_RESULT_VOID)) bc_err(BC_ERR_EXEC_VOID_VAL);
455 #endif // BC_ENABLED
456 
457 	*n = bc_program_num(p, *r);
458 }
459 
460 /**
461  * Prepares the operands of a binary operator.
462  * @param p    The program.
463  * @param l    An out parameter; this is set to the pointer to the result for
464  *             the left operand.
465  * @param ln   An out parameter; this is set to the pointer to the number for
466  *             the left operand.
467  * @param r    An out parameter; this is set to the pointer to the result for
468  *             the right operand.
469  * @param rn   An out parameter; this is set to the pointer to the number for
470  *             the right operand.
471  * @param idx  The starting index where the operands are in the results stack,
472  *             starting from the top.
473  */
474 static void
475 bc_program_binPrep(BcProgram* p, BcResult** l, BcNum** ln, BcResult** r,
476                    BcNum** rn, size_t idx)
477 {
478 	BcResultType lt;
479 
480 	assert(p != NULL && l != NULL && ln != NULL && r != NULL && rn != NULL);
481 
482 #ifndef BC_PROG_NO_STACK_CHECK
483 	// Check the stack for dc.
484 	if (BC_IS_DC)
485 	{
486 		if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 2)))
487 		{
488 			bc_err(BC_ERR_EXEC_STACK);
489 		}
490 	}
491 #endif // BC_PROG_NO_STACK_CHECK
492 
493 	assert(BC_PROG_STACK(&p->results, idx + 2));
494 
495 	// Get the operands.
496 	bc_program_operand(p, l, ln, idx + 1);
497 	bc_program_operand(p, r, rn, idx);
498 
499 	lt = (*l)->t;
500 
501 #if BC_ENABLED
502 	// bc_program_operand() checked these for us.
503 	assert(lt != BC_RESULT_VOID && (*r)->t != BC_RESULT_VOID);
504 #endif // BC_ENABLED
505 
506 	// We run this again under these conditions in case any vector has been
507 	// reallocated out from under the BcNums or arrays we had. In other words,
508 	// this is to fix pointer invalidation.
509 	if (lt == (*r)->t && (lt == BC_RESULT_VAR || lt == BC_RESULT_ARRAY_ELEM))
510 	{
511 		*ln = bc_program_num(p, *l);
512 	}
513 
514 	if (BC_ERR(lt == BC_RESULT_STR)) bc_err(BC_ERR_EXEC_TYPE);
515 }
516 
517 /**
518  * Prepares the operands of a binary operator and type checks them. This is
519  * separate from bc_program_binPrep() because some places want this, others want
520  * bc_program_binPrep().
521  * @param p    The program.
522  * @param l    An out parameter; this is set to the pointer to the result for
523  *             the left operand.
524  * @param ln   An out parameter; this is set to the pointer to the number for
525  *             the left operand.
526  * @param r    An out parameter; this is set to the pointer to the result for
527  *             the right operand.
528  * @param rn   An out parameter; this is set to the pointer to the number for
529  *             the right operand.
530  * @param idx  The starting index where the operands are in the results stack,
531  *             starting from the top.
532  */
533 static void
534 bc_program_binOpPrep(BcProgram* p, BcResult** l, BcNum** ln, BcResult** r,
535                      BcNum** rn, size_t idx)
536 {
537 	bc_program_binPrep(p, l, ln, r, rn, idx);
538 	bc_program_type_num(*l, *ln);
539 	bc_program_type_num(*r, *rn);
540 }
541 
542 /**
543  * Prepares the operands of an assignment operator.
544  * @param p   The program.
545  * @param l   An out parameter; this is set to the pointer to the result for the
546  *            left operand.
547  * @param ln  An out parameter; this is set to the pointer to the number for the
548  *            left operand.
549  * @param r   An out parameter; this is set to the pointer to the result for the
550  *            right operand.
551  * @param rn  An out parameter; this is set to the pointer to the number for the
552  *            right operand.
553  */
554 static void
555 bc_program_assignPrep(BcProgram* p, BcResult** l, BcNum** ln, BcResult** r,
556                       BcNum** rn)
557 {
558 	BcResultType lt, min;
559 	bool good;
560 
561 	// This is the min non-allowable result type. dc allows strings.
562 	min = BC_RESULT_TEMP - ((unsigned int) (BC_IS_BC));
563 
564 	// Prepare the operands.
565 	bc_program_binPrep(p, l, ln, r, rn, 0);
566 
567 	lt = (*l)->t;
568 
569 	// Typecheck the left.
570 	if (BC_ERR(lt >= min && lt <= BC_RESULT_ONE)) bc_err(BC_ERR_EXEC_TYPE);
571 
572 	// Strings can be assigned to variables. We are already good if we are
573 	// assigning a string.
574 	good = ((*r)->t == BC_RESULT_STR && lt <= BC_RESULT_ARRAY_ELEM);
575 
576 	assert(BC_PROG_STR(*rn) || (*r)->t != BC_RESULT_STR);
577 
578 	// If not, type check for a number.
579 	if (!good) bc_program_type_num(*r, *rn);
580 }
581 
582 /**
583  * Prepares a single operand and type checks it. This is separate from
584  * bc_program_operand() because different places want one or the other.
585  * @param p    The program.
586  * @param r    An out parameter; this is set to the pointer to the result that
587  *             we care about.
588  * @param n    An out parameter; this is set to the pointer to the number that
589  *             we care about.
590  * @param idx  The index of the result from the top of the results stack.
591  */
592 static void
593 bc_program_prep(BcProgram* p, BcResult** r, BcNum** n, size_t idx)
594 {
595 	assert(p != NULL && r != NULL && n != NULL);
596 
597 #ifndef BC_PROG_NO_STACK_CHECK
598 	// Check the stack for dc.
599 	if (BC_IS_DC)
600 	{
601 		if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 1)))
602 		{
603 			bc_err(BC_ERR_EXEC_STACK);
604 		}
605 	}
606 #endif // BC_PROG_NO_STACK_CHECK
607 
608 	assert(BC_PROG_STACK(&p->results, idx + 1));
609 
610 	bc_program_operand(p, r, n, idx);
611 
612 	// dc does not allow strings in this case.
613 	bc_program_type_num(*r, *n);
614 }
615 
616 /**
617  * Prepares and returns a clean result for the result of an operation.
618  * @param p  The program.
619  * @return   A clean result.
620  */
621 static BcResult*
622 bc_program_prepResult(BcProgram* p)
623 {
624 	BcResult* res = bc_vec_pushEmpty(&p->results);
625 
626 	bc_result_clear(res);
627 
628 	return res;
629 }
630 
631 /**
632  * Prepares a constant for use. This parses the constant into a number and then
633  * pushes that number onto the results stack.
634  * @param p     The program.
635  * @param code  The bytecode vector that we will pull the index of the constant
636  *              from.
637  * @param bgn   An in/out parameter; marks the start of the index in the
638  *              bytecode vector and will be updated to point to after the index.
639  */
640 static void
641 bc_program_const(BcProgram* p, const char* code, size_t* bgn)
642 {
643 	// I lied. I actually push the result first. I can do this because the
644 	// result will be popped on error. I also get the constant itself.
645 	BcResult* r = bc_program_prepResult(p);
646 	BcConst* c = bc_vec_item(&p->consts, bc_program_index(code, bgn));
647 	BcBigDig base = BC_PROG_IBASE(p);
648 
649 	// Only reparse if the base changed.
650 	if (c->base != base)
651 	{
652 		// Allocate if we haven't yet.
653 		if (c->num.num == NULL)
654 		{
655 			// The plus 1 is in case of overflow with lack of clamping.
656 			size_t len = strlen(c->val) + (BC_DIGIT_CLAMP == 0);
657 
658 			BC_SIG_LOCK;
659 			bc_num_init(&c->num, BC_NUM_RDX(len));
660 			BC_SIG_UNLOCK;
661 		}
662 		// We need to zero an already existing number.
663 		else bc_num_zero(&c->num);
664 
665 		// bc_num_parse() should only do operations that cannot fail.
666 		bc_num_parse(&c->num, c->val, base);
667 
668 		c->base = base;
669 	}
670 
671 	BC_SIG_LOCK;
672 
673 	bc_num_createCopy(&r->d.n, &c->num);
674 
675 	BC_SIG_UNLOCK;
676 }
677 
678 /**
679  * Executes a binary operator operation.
680  * @param p     The program.
681  * @param inst  The instruction corresponding to the binary operator to execute.
682  */
683 static void
684 bc_program_op(BcProgram* p, uchar inst)
685 {
686 	BcResult* opd1;
687 	BcResult* opd2;
688 	BcResult* res;
689 	BcNum* n1;
690 	BcNum* n2;
691 	size_t idx = inst - BC_INST_POWER;
692 
693 	res = bc_program_prepResult(p);
694 
695 	bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, 1);
696 
697 	BC_SIG_LOCK;
698 
699 	// Initialize the number with enough space, using the correct
700 	// BcNumBinaryOpReq function. This looks weird because it is executing an
701 	// item of an array. Rest assured that item is a function.
702 	bc_num_init(&res->d.n, bc_program_opReqs[idx](n1, n2, BC_PROG_SCALE(p)));
703 
704 	BC_SIG_UNLOCK;
705 
706 	assert(BC_NUM_RDX_VALID(n1));
707 	assert(BC_NUM_RDX_VALID(n2));
708 
709 	// Run the operation. This also executes an item of an array.
710 	bc_program_ops[idx](n1, n2, &res->d.n, BC_PROG_SCALE(p));
711 
712 	bc_program_retire(p, 1, 2);
713 }
714 
715 /**
716  * Executes a read() or ? command.
717  * @param p  The program.
718  */
719 static void
720 bc_program_read(BcProgram* p)
721 {
722 	BcStatus s;
723 	BcInstPtr ip;
724 	size_t i;
725 	const char* file;
726 	BcMode mode;
727 	BcFunc* f = bc_vec_item(&p->fns, BC_PROG_READ);
728 
729 	// If we are already executing a read, that is an error. So look for a read
730 	// and barf.
731 	for (i = 0; i < p->stack.len; ++i)
732 	{
733 		BcInstPtr* ip_ptr = bc_vec_item(&p->stack, i);
734 		if (ip_ptr->func == BC_PROG_READ) bc_err(BC_ERR_EXEC_REC_READ);
735 	}
736 
737 	BC_SIG_LOCK;
738 
739 	// Save the filename because we are going to overwrite it.
740 	file = vm->file;
741 	mode = vm->mode;
742 
743 	// It is a parse error if there needs to be more than one line, so we unset
744 	// this to tell the lexer to not request more. We set it back later.
745 	vm->mode = BC_MODE_FILE;
746 
747 	if (!BC_PARSE_IS_INITED(&vm->read_prs, p))
748 	{
749 		// We need to parse, but we don't want to use the existing parser
750 		// because it has state it needs to keep. (It could have a partial parse
751 		// state.) So we create a new parser. This parser is in the BcVm struct
752 		// so that it is not local, which means that a longjmp() could change
753 		// it.
754 		bc_parse_init(&vm->read_prs, p, BC_PROG_READ);
755 
756 		// We need a separate input buffer; that's why it is also in the BcVm
757 		// struct.
758 		bc_vec_init(&vm->read_buf, sizeof(char), BC_DTOR_NONE);
759 	}
760 	// This needs to be updated because the parser could have been used
761 	// somewhere else
762 	else bc_parse_updateFunc(&vm->read_prs, BC_PROG_READ);
763 
764 	BC_SETJMP_LOCKED(vm, exec_err);
765 
766 	BC_SIG_UNLOCK;
767 
768 	// Set up the lexer and the read function.
769 	bc_lex_file(&vm->read_prs.l, bc_program_stdin_name);
770 	bc_vec_popAll(&f->code);
771 
772 	// Read a line.
773 	if (!BC_R) s = bc_read_line(&vm->read_buf, "");
774 	else s = bc_read_line(&vm->read_buf, BC_VM_READ_PROMPT);
775 
776 	// We should *not* have run into EOF.
777 	if (s == BC_STATUS_EOF) bc_err(BC_ERR_EXEC_READ_EXPR);
778 
779 	// Parse *one* expression, so mode should not be stdin.
780 	bc_parse_text(&vm->read_prs, vm->read_buf.v, BC_MODE_FILE);
781 	BC_SIG_LOCK;
782 	vm->expr(&vm->read_prs, BC_PARSE_NOREAD | BC_PARSE_NEEDVAL);
783 	BC_SIG_UNLOCK;
784 
785 	// We *must* have a valid expression. A semicolon cannot end an expression,
786 	// although EOF can.
787 	if (BC_ERR(vm->read_prs.l.t != BC_LEX_NLINE &&
788 	           vm->read_prs.l.t != BC_LEX_EOF))
789 	{
790 		bc_err(BC_ERR_EXEC_READ_EXPR);
791 	}
792 
793 #if BC_ENABLED
794 	// Push on the globals stack if necessary.
795 	if (BC_G) bc_program_prepGlobals(p);
796 #endif // BC_ENABLED
797 
798 	// Set up a new BcInstPtr.
799 	ip.func = BC_PROG_READ;
800 	ip.idx = 0;
801 	ip.len = p->results.len;
802 
803 	// Update this pointer, just in case.
804 	f = bc_vec_item(&p->fns, BC_PROG_READ);
805 
806 	// We want a return instruction to simplify things.
807 	bc_vec_pushByte(&f->code, vm->read_ret);
808 
809 	// This lock is here to make sure dc's tail calls are the same length.
810 	BC_SIG_LOCK;
811 	bc_vec_push(&p->stack, &ip);
812 
813 #if DC_ENABLED
814 	// We need a new tail call entry for dc.
815 	if (BC_IS_DC)
816 	{
817 		size_t temp = 0;
818 		bc_vec_push(&p->tail_calls, &temp);
819 	}
820 #endif // DC_ENABLED
821 
822 exec_err:
823 	BC_SIG_MAYLOCK;
824 	vm->mode = (uchar) mode;
825 	vm->file = file;
826 	BC_LONGJMP_CONT(vm);
827 }
828 
829 #if BC_ENABLE_EXTRA_MATH
830 
831 /**
832  * Execute a rand().
833  * @param p  The program.
834  */
835 static void
836 bc_program_rand(BcProgram* p)
837 {
838 	BcRand rand = bc_rand_int(&p->rng);
839 
840 	bc_program_pushBigdig(p, (BcBigDig) rand, BC_RESULT_TEMP);
841 
842 #ifndef NDEBUG
843 	// This is just to ensure that the generated number is correct. I also use
844 	// braces because I declare every local at the top of the scope.
845 	{
846 		BcResult* r = bc_vec_top(&p->results);
847 		assert(BC_NUM_RDX_VALID_NP(r->d.n));
848 	}
849 #endif // NDEBUG
850 }
851 #endif // BC_ENABLE_EXTRA_MATH
852 
853 /**
854  * Prints a series of characters, without escapes.
855  * @param str  The string (series of characters).
856  */
857 static void
858 bc_program_printChars(const char* str)
859 {
860 	const char* nl;
861 	size_t len = vm->nchars + strlen(str);
862 	sig_atomic_t lock;
863 
864 	BC_SIG_TRYLOCK(lock);
865 
866 	bc_file_puts(&vm->fout, bc_flush_save, str);
867 
868 	// We need to update the number of characters, so we find the last newline
869 	// and set the characters accordingly.
870 	nl = strrchr(str, '\n');
871 
872 	if (nl != NULL) len = strlen(nl + 1);
873 
874 	vm->nchars = len > UINT16_MAX ? UINT16_MAX : (uint16_t) len;
875 
876 	BC_SIG_TRYUNLOCK(lock);
877 }
878 
879 /**
880  * Prints a string with escapes.
881  * @param str  The string.
882  */
883 static void
884 bc_program_printString(const char* restrict str)
885 {
886 	size_t i, len = strlen(str);
887 
888 #if DC_ENABLED
889 	// This is to ensure a nul byte is printed for dc's stream operation.
890 	if (!len && BC_IS_DC)
891 	{
892 		bc_vm_putchar('\0', bc_flush_save);
893 		return;
894 	}
895 #endif // DC_ENABLED
896 
897 	// Loop over the characters, processing escapes and printing the rest.
898 	for (i = 0; i < len; ++i)
899 	{
900 		int c = str[i];
901 
902 		// If we have an escape...
903 		if (c == '\\' && i != len - 1)
904 		{
905 			const char* ptr;
906 
907 			// Get the escape character and its companion.
908 			c = str[++i];
909 			ptr = strchr(bc_program_esc_chars, c);
910 
911 			// If we have a companion character...
912 			if (ptr != NULL)
913 			{
914 				// We need to specially handle a newline.
915 				if (c == 'n')
916 				{
917 					BC_SIG_LOCK;
918 					vm->nchars = UINT16_MAX;
919 					BC_SIG_UNLOCK;
920 				}
921 
922 				// Grab the actual character.
923 				c = bc_program_esc_seqs[(size_t) (ptr - bc_program_esc_chars)];
924 			}
925 			else
926 			{
927 				// Just print the backslash if there is no companion character.
928 				// The following character will be printed later after the outer
929 				// if statement.
930 				bc_vm_putchar('\\', bc_flush_save);
931 			}
932 		}
933 
934 		bc_vm_putchar(c, bc_flush_save);
935 	}
936 }
937 
938 /**
939  * Executes a print. This function handles all printing except streaming.
940  * @param p     The program.
941  * @param inst  The instruction for the type of print we are doing.
942  * @param idx   The index of the result that we are printing.
943  */
944 static void
945 bc_program_print(BcProgram* p, uchar inst, size_t idx)
946 {
947 	BcResult* r;
948 	char* str;
949 	BcNum* n;
950 	bool pop = (inst != BC_INST_PRINT);
951 
952 	assert(p != NULL);
953 
954 #ifndef BC_PROG_NO_STACK_CHECK
955 	if (BC_IS_DC)
956 	{
957 		if (BC_ERR(!BC_PROG_STACK(&p->results, idx + 1)))
958 		{
959 			bc_err(BC_ERR_EXEC_STACK);
960 		}
961 	}
962 #endif // BC_PROG_NO_STACK_CHECK
963 
964 	assert(BC_PROG_STACK(&p->results, idx + 1));
965 
966 	r = bc_vec_item_rev(&p->results, idx);
967 
968 #if BC_ENABLED
969 	// If we have a void value, that's not necessarily an error. It is if pop is
970 	// true because that means that we are executing a print statement, but
971 	// attempting to do a print on a lone void value is allowed because that's
972 	// exactly how we want void values used.
973 	if (r->t == BC_RESULT_VOID)
974 	{
975 		if (BC_ERR(pop)) bc_err(BC_ERR_EXEC_VOID_VAL);
976 		bc_vec_pop(&p->results);
977 		return;
978 	}
979 #endif // BC_ENABLED
980 
981 	n = bc_program_num(p, r);
982 
983 	// If we have a number...
984 	if (BC_PROG_NUM(r, n))
985 	{
986 #if BC_ENABLED
987 		assert(inst != BC_INST_PRINT_STR);
988 #endif // BC_ENABLED
989 
990 		// Print the number.
991 		bc_num_print(n, BC_PROG_OBASE(p), !pop);
992 
993 #if BC_ENABLED
994 		// Need to store the number in last.
995 		if (BC_IS_BC) bc_num_copy(&p->last, n);
996 #endif // BC_ENABLED
997 	}
998 	else
999 	{
1000 		// We want to flush any stuff in the stdout buffer first.
1001 		bc_file_flush(&vm->fout, bc_flush_save);
1002 		str = bc_program_string(p, n);
1003 
1004 #if BC_ENABLED
1005 		if (inst == BC_INST_PRINT_STR) bc_program_printChars(str);
1006 		else
1007 #endif // BC_ENABLED
1008 		{
1009 			bc_program_printString(str);
1010 
1011 			// Need to print a newline only in this case.
1012 			if (inst == BC_INST_PRINT) bc_vm_putchar('\n', bc_flush_err);
1013 		}
1014 	}
1015 
1016 	// bc always pops. This macro makes sure that happens.
1017 	if (BC_PROGRAM_POP(pop)) bc_vec_pop(&p->results);
1018 }
1019 
1020 void
1021 bc_program_negate(BcResult* r, BcNum* n)
1022 {
1023 	bc_num_copy(&r->d.n, n);
1024 	if (BC_NUM_NONZERO(&r->d.n)) BC_NUM_NEG_TGL_NP(r->d.n);
1025 }
1026 
1027 void
1028 bc_program_not(BcResult* r, BcNum* n)
1029 {
1030 	if (!bc_num_cmpZero(n)) bc_num_one(&r->d.n);
1031 }
1032 
1033 #if BC_ENABLE_EXTRA_MATH
1034 void
1035 bc_program_trunc(BcResult* r, BcNum* n)
1036 {
1037 	bc_num_copy(&r->d.n, n);
1038 	bc_num_truncate(&r->d.n, n->scale);
1039 }
1040 #endif // BC_ENABLE_EXTRA_MATH
1041 
1042 /**
1043  * Runs a unary operation.
1044  * @param p     The program.
1045  * @param inst  The unary operation.
1046  */
1047 static void
1048 bc_program_unary(BcProgram* p, uchar inst)
1049 {
1050 	BcResult* res;
1051 	BcResult* ptr;
1052 	BcNum* num;
1053 
1054 	res = bc_program_prepResult(p);
1055 
1056 	bc_program_prep(p, &ptr, &num, 1);
1057 
1058 	BC_SIG_LOCK;
1059 
1060 	bc_num_init(&res->d.n, num->len);
1061 
1062 	BC_SIG_UNLOCK;
1063 
1064 	// This calls a function that is in an array.
1065 	bc_program_unarys[inst - BC_INST_NEG](res, num);
1066 	bc_program_retire(p, 1, 1);
1067 }
1068 
1069 /**
1070  * Executes a logical operator.
1071  * @param p     The program.
1072  * @param inst  The operator.
1073  */
1074 static void
1075 bc_program_logical(BcProgram* p, uchar inst)
1076 {
1077 	BcResult* opd1;
1078 	BcResult* opd2;
1079 	BcResult* res;
1080 	BcNum* n1;
1081 	BcNum* n2;
1082 	bool cond = 0;
1083 	ssize_t cmp;
1084 
1085 	res = bc_program_prepResult(p);
1086 
1087 	// All logical operators (except boolean not, which is taken care of by
1088 	// bc_program_unary()), are binary operators.
1089 	bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, 1);
1090 
1091 	// Boolean and and or are not short circuiting. This is why; they can be
1092 	// implemented much easier this way.
1093 	if (inst == BC_INST_BOOL_AND)
1094 	{
1095 		cond = (bc_num_cmpZero(n1) && bc_num_cmpZero(n2));
1096 	}
1097 	else if (inst == BC_INST_BOOL_OR)
1098 	{
1099 		cond = (bc_num_cmpZero(n1) || bc_num_cmpZero(n2));
1100 	}
1101 	else
1102 	{
1103 		// We have a relational operator, so do a comparison.
1104 		cmp = bc_num_cmp(n1, n2);
1105 
1106 		switch (inst)
1107 		{
1108 			case BC_INST_REL_EQ:
1109 			{
1110 				cond = (cmp == 0);
1111 				break;
1112 			}
1113 
1114 			case BC_INST_REL_LE:
1115 			{
1116 				cond = (cmp <= 0);
1117 				break;
1118 			}
1119 
1120 			case BC_INST_REL_GE:
1121 			{
1122 				cond = (cmp >= 0);
1123 				break;
1124 			}
1125 
1126 			case BC_INST_REL_NE:
1127 			{
1128 				cond = (cmp != 0);
1129 				break;
1130 			}
1131 
1132 			case BC_INST_REL_LT:
1133 			{
1134 				cond = (cmp < 0);
1135 				break;
1136 			}
1137 
1138 			case BC_INST_REL_GT:
1139 			{
1140 				cond = (cmp > 0);
1141 				break;
1142 			}
1143 #ifndef NDEBUG
1144 			default:
1145 			{
1146 				// There is a bug if we get here.
1147 				abort();
1148 			}
1149 #endif // NDEBUG
1150 		}
1151 	}
1152 
1153 	BC_SIG_LOCK;
1154 
1155 	bc_num_init(&res->d.n, BC_NUM_DEF_SIZE);
1156 
1157 	BC_SIG_UNLOCK;
1158 
1159 	if (cond) bc_num_one(&res->d.n);
1160 
1161 	bc_program_retire(p, 1, 2);
1162 }
1163 
1164 /**
1165  * Assigns a string to a variable.
1166  * @param p     The program.
1167  * @param num   The location of the string as a BcNum.
1168  * @param v     The stack for the variable.
1169  * @param push  Whether to push the string or not. To push means to move the
1170  *              string from the results stack and push it onto the variable
1171  *              stack.
1172  */
1173 static void
1174 bc_program_assignStr(BcProgram* p, BcNum* num, BcVec* v, bool push)
1175 {
1176 	BcNum* n;
1177 
1178 	assert(BC_PROG_STACK(&p->results, 1 + !push));
1179 	assert(num != NULL && num->num == NULL && num->cap == 0);
1180 
1181 	// If we are not pushing onto the variable stack, we need to replace the
1182 	// top of the variable stack.
1183 	if (!push) bc_vec_pop(v);
1184 
1185 	bc_vec_npop(&p->results, 1 + !push);
1186 
1187 	n = bc_vec_pushEmpty(v);
1188 
1189 	// We can just copy because the num should not have allocated anything.
1190 	// NOLINTNEXTLINE
1191 	memcpy(n, num, sizeof(BcNum));
1192 }
1193 
1194 /**
1195  * Copies a value to a variable. This is used for storing in dc as well as to
1196  * set function parameters to arguments in bc.
1197  * @param p    The program.
1198  * @param idx  The index of the variable or array to copy to.
1199  * @param t    The type to copy to. This could be a variable or an array.
1200  */
1201 static void
1202 bc_program_copyToVar(BcProgram* p, size_t idx, BcType t)
1203 {
1204 	BcResult *ptr = NULL, r;
1205 	BcVec* vec;
1206 	BcNum* n = NULL;
1207 	bool var = (t == BC_TYPE_VAR);
1208 
1209 #if DC_ENABLED
1210 	// Check the stack for dc.
1211 	if (BC_IS_DC)
1212 	{
1213 		if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK);
1214 	}
1215 #endif
1216 
1217 	assert(BC_PROG_STACK(&p->results, 1));
1218 
1219 	bc_program_operand(p, &ptr, &n, 0);
1220 
1221 #if BC_ENABLED
1222 	// Get the variable for a bc function call.
1223 	if (BC_IS_BC)
1224 	{
1225 		// Type match the result.
1226 		bc_program_type_match(ptr, t);
1227 	}
1228 #endif // BC_ENABLED
1229 
1230 	vec = bc_program_vec(p, idx, t);
1231 
1232 	// We can shortcut in dc if it's assigning a string by using
1233 	// bc_program_assignStr().
1234 	if (ptr->t == BC_RESULT_STR)
1235 	{
1236 		assert(BC_PROG_STR(n));
1237 
1238 		if (BC_ERR(!var)) bc_err(BC_ERR_EXEC_TYPE);
1239 
1240 		bc_program_assignStr(p, n, vec, true);
1241 
1242 		return;
1243 	}
1244 
1245 	BC_SIG_LOCK;
1246 
1247 	// Just create and copy for a normal variable.
1248 	if (var)
1249 	{
1250 		if (BC_PROG_STR(n))
1251 		{
1252 			// NOLINTNEXTLINE
1253 			memcpy(&r.d.n, n, sizeof(BcNum));
1254 		}
1255 		else bc_num_createCopy(&r.d.n, n);
1256 	}
1257 	else
1258 	{
1259 		// If we get here, we are handling an array. This is one place we need
1260 		// to cast the number from bc_program_num() to a vector.
1261 		BcVec* v = (BcVec*) n;
1262 		BcVec* rv = &r.d.v;
1263 
1264 #if BC_ENABLED
1265 
1266 		if (BC_IS_BC)
1267 		{
1268 			bool ref, ref_size;
1269 
1270 			// True if we are using a reference.
1271 			ref = (v->size == sizeof(BcNum) && t == BC_TYPE_REF);
1272 
1273 			// True if we already have a reference vector. This is slightly
1274 			// (okay, a lot; it just doesn't look that way) different from
1275 			// above. The above means that we need to construct a reference
1276 			// vector, whereas this means that we have one and we might have to
1277 			// *dereference* it.
1278 			ref_size = (v->size == sizeof(uchar));
1279 
1280 			// If we *should* have a reference.
1281 			if (ref || (ref_size && t == BC_TYPE_REF))
1282 			{
1283 				// Create a new reference vector.
1284 				bc_vec_init(rv, sizeof(uchar), BC_DTOR_NONE);
1285 
1286 				// If this is true, then we need to construct a reference.
1287 				if (ref)
1288 				{
1289 					// Make sure the pointer was not invalidated.
1290 					vec = bc_program_vec(p, idx, t);
1291 
1292 					// Push the indices onto the reference vector. This takes
1293 					// care of last; it ensures the reference goes to the right
1294 					// place.
1295 					bc_vec_pushIndex(rv, ptr->d.loc.loc);
1296 					bc_vec_pushIndex(rv, ptr->d.loc.stack_idx);
1297 				}
1298 				// If we get here, we are copying a ref to a ref. Just push a
1299 				// copy of all of the bytes.
1300 				else bc_vec_npush(rv, v->len * sizeof(uchar), v->v);
1301 
1302 				// Push the reference vector onto the array stack and pop the
1303 				// source.
1304 				bc_vec_push(vec, &r.d);
1305 				bc_vec_pop(&p->results);
1306 
1307 				// We need to return early to avoid executing code that we must
1308 				// not touch.
1309 				BC_SIG_UNLOCK;
1310 				return;
1311 			}
1312 			// If we get here, we have a reference, but we need an array, so
1313 			// dereference the array.
1314 			else if (ref_size && t != BC_TYPE_REF)
1315 			{
1316 				v = bc_program_dereference(p, v);
1317 			}
1318 		}
1319 #endif // BC_ENABLED
1320 
1321 		// If we get here, we need to copy the array because in bc, all
1322 		// arguments are passed by value. Yes, this is expensive.
1323 		bc_array_init(rv, true);
1324 		bc_array_copy(rv, v);
1325 	}
1326 
1327 	// Push the vector onto the array stack and pop the source.
1328 	bc_vec_push(vec, &r.d);
1329 	bc_vec_pop(&p->results);
1330 
1331 	BC_SIG_UNLOCK;
1332 }
1333 
1334 void
1335 bc_program_assignBuiltin(BcProgram* p, bool scale, bool obase, BcBigDig val)
1336 {
1337 	BcBigDig* ptr_t;
1338 	BcBigDig max, min;
1339 #if BC_ENABLED
1340 	BcVec* v;
1341 	BcBigDig* ptr;
1342 #endif // BC_ENABLED
1343 
1344 	assert(!scale || !obase);
1345 
1346 	// Scale needs handling separate from ibase and obase.
1347 	if (scale)
1348 	{
1349 		// Set the min and max.
1350 		min = 0;
1351 		max = vm->maxes[BC_PROG_GLOBALS_SCALE];
1352 
1353 #if BC_ENABLED
1354 		// Get a pointer to the stack.
1355 		v = p->globals_v + BC_PROG_GLOBALS_SCALE;
1356 #endif // BC_ENABLED
1357 
1358 		// Get a pointer to the current value.
1359 		ptr_t = p->globals + BC_PROG_GLOBALS_SCALE;
1360 	}
1361 	else
1362 	{
1363 		// Set the min and max.
1364 		min = BC_NUM_MIN_BASE;
1365 		if (BC_ENABLE_EXTRA_MATH && obase && (BC_IS_DC || !BC_IS_POSIX))
1366 		{
1367 			min = 0;
1368 		}
1369 		max = vm->maxes[obase + BC_PROG_GLOBALS_IBASE];
1370 
1371 #if BC_ENABLED
1372 		// Get a pointer to the stack.
1373 		v = p->globals_v + BC_PROG_GLOBALS_IBASE + obase;
1374 #endif // BC_ENABLED
1375 
1376 		// Get a pointer to the current value.
1377 		ptr_t = p->globals + BC_PROG_GLOBALS_IBASE + obase;
1378 	}
1379 
1380 	// Check for error.
1381 	if (BC_ERR(val > max || val < min))
1382 	{
1383 		BcErr e;
1384 
1385 		// This grabs the right error.
1386 		if (scale) e = BC_ERR_EXEC_SCALE;
1387 		else if (obase) e = BC_ERR_EXEC_OBASE;
1388 		else e = BC_ERR_EXEC_IBASE;
1389 
1390 		bc_verr(e, min, max);
1391 	}
1392 
1393 #if BC_ENABLED
1394 	// Set the top of the stack.
1395 	ptr = bc_vec_top(v);
1396 	*ptr = val;
1397 #endif // BC_ENABLED
1398 
1399 	// Set the actual global variable.
1400 	*ptr_t = val;
1401 }
1402 
1403 #if BC_ENABLE_EXTRA_MATH
1404 void
1405 bc_program_assignSeed(BcProgram* p, BcNum* val)
1406 {
1407 	bc_num_rng(val, &p->rng);
1408 }
1409 #endif // BC_ENABLE_EXTRA_MATH
1410 
1411 /**
1412  * Executes an assignment operator.
1413  * @param p     The program.
1414  * @param inst  The assignment operator to execute.
1415  */
1416 static void
1417 bc_program_assign(BcProgram* p, uchar inst)
1418 {
1419 	// The local use_val is true when the assigned value needs to be copied.
1420 	BcResult* left;
1421 	BcResult* right;
1422 	BcResult res;
1423 	BcNum* l;
1424 	BcNum* r;
1425 	bool ob, sc, use_val = BC_INST_USE_VAL(inst);
1426 
1427 	bc_program_assignPrep(p, &left, &l, &right, &r);
1428 
1429 	// Assigning to a string should be impossible simply because of the parse.
1430 	assert(left->t != BC_RESULT_STR);
1431 
1432 	// If we are assigning a string...
1433 	if (right->t == BC_RESULT_STR)
1434 	{
1435 		assert(BC_PROG_STR(r));
1436 
1437 #if BC_ENABLED
1438 		if (inst != BC_INST_ASSIGN && inst != BC_INST_ASSIGN_NO_VAL)
1439 		{
1440 			bc_err(BC_ERR_EXEC_TYPE);
1441 		}
1442 #endif // BC_ENABLED
1443 
1444 		// If we are assigning to an array element...
1445 		if (left->t == BC_RESULT_ARRAY_ELEM)
1446 		{
1447 			BC_SIG_LOCK;
1448 
1449 			// We need to free the number and clear it.
1450 			bc_num_free(l);
1451 
1452 			// NOLINTNEXTLINE
1453 			memcpy(l, r, sizeof(BcNum));
1454 
1455 			// Now we can pop the results.
1456 			bc_vec_npop(&p->results, 2);
1457 
1458 			BC_SIG_UNLOCK;
1459 		}
1460 		else
1461 		{
1462 			// If we get here, we are assigning to a variable, which we can use
1463 			// bc_program_assignStr() for.
1464 			BcVec* v = bc_program_vec(p, left->d.loc.loc, BC_TYPE_VAR);
1465 			bc_program_assignStr(p, r, v, false);
1466 		}
1467 
1468 #if BC_ENABLED
1469 
1470 		// If this is true, the value is going to be used again, so we want to
1471 		// push a temporary with the string.
1472 		if (inst == BC_INST_ASSIGN)
1473 		{
1474 			res.t = BC_RESULT_STR;
1475 			// NOLINTNEXTLINE
1476 			memcpy(&res.d.n, r, sizeof(BcNum));
1477 			bc_vec_push(&p->results, &res);
1478 		}
1479 
1480 #endif // BC_ENABLED
1481 
1482 		// By using bc_program_assignStr(), we short-circuited this, so return.
1483 		return;
1484 	}
1485 
1486 	// If we have a normal assignment operator, not a math one...
1487 	if (BC_INST_IS_ASSIGN(inst))
1488 	{
1489 		// Assigning to a variable that has a string here is fine because there
1490 		// is no math done on it.
1491 
1492 		// BC_RESULT_TEMP, BC_RESULT_IBASE, BC_RESULT_OBASE, BC_RESULT_SCALE,
1493 		// and BC_RESULT_SEED all have temporary copies. Because that's the
1494 		// case, we can free the left and just move the value over. We set the
1495 		// type of right to BC_RESULT_ZERO in order to prevent it from being
1496 		// freed. We also don't have to worry about BC_RESULT_STR because it's
1497 		// take care of above.
1498 		if (right->t == BC_RESULT_TEMP || right->t >= BC_RESULT_IBASE)
1499 		{
1500 			BC_SIG_LOCK;
1501 
1502 			bc_num_free(l);
1503 			// NOLINTNEXTLINE
1504 			memcpy(l, r, sizeof(BcNum));
1505 			right->t = BC_RESULT_ZERO;
1506 
1507 			BC_SIG_UNLOCK;
1508 		}
1509 		// Copy over.
1510 		else bc_num_copy(l, r);
1511 	}
1512 #if BC_ENABLED
1513 	else
1514 	{
1515 		// If we get here, we are doing a math assignment (+=, -=, etc.). So
1516 		// we need to prepare for a binary operator.
1517 		BcBigDig scale = BC_PROG_SCALE(p);
1518 
1519 		// At this point, the left side could still be a string because it could
1520 		// be a variable that has the string. If that's the case, we have a type
1521 		// error.
1522 		if (BC_PROG_STR(l)) bc_err(BC_ERR_EXEC_TYPE);
1523 
1524 		// Get the right type of assignment operator, whether val is used or
1525 		// NO_VAL for performance.
1526 		if (!use_val)
1527 		{
1528 			inst -= (BC_INST_ASSIGN_POWER_NO_VAL - BC_INST_ASSIGN_POWER);
1529 		}
1530 
1531 		assert(BC_NUM_RDX_VALID(l));
1532 		assert(BC_NUM_RDX_VALID(r));
1533 
1534 		// Run the actual operation. We do not need worry about reallocating l
1535 		// because bc_num_binary() does that behind the scenes for us.
1536 		bc_program_ops[inst - BC_INST_ASSIGN_POWER](l, r, l, scale);
1537 	}
1538 #endif // BC_ENABLED
1539 
1540 	ob = (left->t == BC_RESULT_OBASE);
1541 	sc = (left->t == BC_RESULT_SCALE);
1542 
1543 	// The globals need special handling, especially the non-seed ones. The
1544 	// first part of the if statement handles them.
1545 	if (ob || sc || left->t == BC_RESULT_IBASE)
1546 	{
1547 		// Get the actual value.
1548 		BcBigDig val = bc_num_bigdig(l);
1549 
1550 		bc_program_assignBuiltin(p, sc, ob, val);
1551 	}
1552 #if BC_ENABLE_EXTRA_MATH
1553 	// To assign to steed, let bc_num_rng() do its magic.
1554 	else if (left->t == BC_RESULT_SEED) bc_program_assignSeed(p, l);
1555 #endif // BC_ENABLE_EXTRA_MATH
1556 
1557 	BC_SIG_LOCK;
1558 
1559 	// If we needed to use the value, then we need to copy it. Otherwise, we can
1560 	// pop indiscriminately. Oh, and the copy should be a BC_RESULT_TEMP.
1561 	if (use_val)
1562 	{
1563 		bc_num_createCopy(&res.d.n, l);
1564 		res.t = BC_RESULT_TEMP;
1565 		bc_vec_npop(&p->results, 2);
1566 		bc_vec_push(&p->results, &res);
1567 	}
1568 	else bc_vec_npop(&p->results, 2);
1569 
1570 	BC_SIG_UNLOCK;
1571 }
1572 
1573 /**
1574  * Pushes a variable's value onto the results stack.
1575  * @param p     The program.
1576  * @param code  The bytecode vector to pull the variable's index out of.
1577  * @param bgn   An in/out parameter; the start of the index in the bytecode
1578  *              vector, and will be updated to point after the index on return.
1579  * @param pop   True if the variable's value should be popped off its stack.
1580  *              This is only used in dc.
1581  * @param copy  True if the variable's value should be copied to the results
1582  *              stack. This is only used in dc.
1583  */
1584 static void
1585 bc_program_pushVar(BcProgram* p, const char* restrict code,
1586                    size_t* restrict bgn, bool pop, bool copy)
1587 {
1588 	BcResult r;
1589 	size_t idx = bc_program_index(code, bgn);
1590 	BcVec* v;
1591 
1592 	// Set the result appropriately.
1593 	r.t = BC_RESULT_VAR;
1594 	r.d.loc.loc = idx;
1595 
1596 	// Get the stack for the variable. This is used in both bc and dc.
1597 	v = bc_program_vec(p, idx, BC_TYPE_VAR);
1598 	r.d.loc.stack_idx = v->len - 1;
1599 
1600 #if DC_ENABLED
1601 	// If this condition is true, then we have the hard case, where we have to
1602 	// adjust dc registers.
1603 	if (BC_IS_DC && (pop || copy))
1604 	{
1605 		// Get the number at the top at the top of the stack.
1606 		BcNum* num = bc_vec_top(v);
1607 
1608 		// Ensure there are enough elements on the stack.
1609 		if (BC_ERR(!BC_PROG_STACK(v, 2 - copy)))
1610 		{
1611 			const char* name = bc_map_name(&p->var_map, idx);
1612 			bc_verr(BC_ERR_EXEC_STACK_REGISTER, name);
1613 		}
1614 
1615 		assert(BC_PROG_STACK(v, 2 - copy));
1616 
1617 		// If the top of the stack is actually a number...
1618 		if (!BC_PROG_STR(num))
1619 		{
1620 			BC_SIG_LOCK;
1621 
1622 			// Create a copy to go onto the results stack as appropriate.
1623 			r.t = BC_RESULT_TEMP;
1624 			bc_num_createCopy(&r.d.n, num);
1625 
1626 			// If we are not actually copying, we need to do a replace, so pop.
1627 			if (!copy) bc_vec_pop(v);
1628 
1629 			bc_vec_push(&p->results, &r);
1630 
1631 			BC_SIG_UNLOCK;
1632 
1633 			return;
1634 		}
1635 		else
1636 		{
1637 			// Set the string result. We can just memcpy because all of the
1638 			// fields in the num should be cleared.
1639 			// NOLINTNEXTLINE
1640 			memcpy(&r.d.n, num, sizeof(BcNum));
1641 			r.t = BC_RESULT_STR;
1642 		}
1643 
1644 		// If we are not actually copying, we need to do a replace, so pop.
1645 		if (!copy) bc_vec_pop(v);
1646 	}
1647 #endif // DC_ENABLED
1648 
1649 	bc_vec_push(&p->results, &r);
1650 }
1651 
1652 /**
1653  * Pushes an array or an array element onto the results stack.
1654  * @param p     The program.
1655  * @param code  The bytecode vector to pull the variable's index out of.
1656  * @param bgn   An in/out parameter; the start of the index in the bytecode
1657  *              vector, and will be updated to point after the index on return.
1658  * @param inst  The instruction; whether to push an array or an array element.
1659  */
1660 static void
1661 bc_program_pushArray(BcProgram* p, const char* restrict code,
1662                      size_t* restrict bgn, uchar inst)
1663 {
1664 	BcResult r;
1665 	BcResult* operand;
1666 	BcNum* num;
1667 	BcBigDig temp;
1668 	BcVec* v;
1669 
1670 	// Get the index of the array.
1671 	r.d.loc.loc = bc_program_index(code, bgn);
1672 
1673 	// We need the array to get its length.
1674 	v = bc_program_vec(p, r.d.loc.loc, BC_TYPE_ARRAY);
1675 	assert(v != NULL);
1676 
1677 	r.d.loc.stack_idx = v->len - 1;
1678 
1679 	// Doing an array is easy; just set the result type and finish.
1680 	if (inst == BC_INST_ARRAY)
1681 	{
1682 		r.t = BC_RESULT_ARRAY;
1683 		bc_vec_push(&p->results, &r);
1684 		return;
1685 	}
1686 
1687 	// Grab the top element of the results stack for the array index.
1688 	bc_program_prep(p, &operand, &num, 0);
1689 	temp = bc_num_bigdig(num);
1690 
1691 	// Set the result.
1692 	r.t = BC_RESULT_ARRAY_ELEM;
1693 	r.d.loc.idx = (size_t) temp;
1694 
1695 	BC_SIG_LOCK;
1696 
1697 	// Pop the index and push the element.
1698 	bc_vec_pop(&p->results);
1699 	bc_vec_push(&p->results, &r);
1700 
1701 	BC_SIG_UNLOCK;
1702 }
1703 
1704 #if BC_ENABLED
1705 
1706 /**
1707  * Executes an increment or decrement operator. This only handles postfix
1708  * inc/dec because the parser translates prefix inc/dec into an assignment where
1709  * the value is used.
1710  * @param p     The program.
1711  * @param inst  The instruction; whether to do an increment or decrement.
1712  */
1713 static void
1714 bc_program_incdec(BcProgram* p, uchar inst)
1715 {
1716 	BcResult *ptr, res, copy;
1717 	BcNum* num;
1718 	uchar inst2;
1719 
1720 	bc_program_prep(p, &ptr, &num, 0);
1721 
1722 	BC_SIG_LOCK;
1723 
1724 	// We need a copy from *before* the operation.
1725 	copy.t = BC_RESULT_TEMP;
1726 	bc_num_createCopy(&copy.d.n, num);
1727 
1728 	BC_SETJMP_LOCKED(vm, exit);
1729 
1730 	BC_SIG_UNLOCK;
1731 
1732 	// Create the proper assignment.
1733 	res.t = BC_RESULT_ONE;
1734 	inst2 = BC_INST_ASSIGN_PLUS_NO_VAL + (inst & 0x01);
1735 
1736 	bc_vec_push(&p->results, &res);
1737 	bc_program_assign(p, inst2);
1738 
1739 	BC_SIG_LOCK;
1740 
1741 	bc_vec_push(&p->results, &copy);
1742 
1743 	BC_UNSETJMP(vm);
1744 
1745 	BC_SIG_UNLOCK;
1746 
1747 	// No need to free the copy here because we pushed it onto the stack.
1748 	return;
1749 
1750 exit:
1751 	BC_SIG_MAYLOCK;
1752 	bc_num_free(&copy.d.n);
1753 	BC_LONGJMP_CONT(vm);
1754 }
1755 
1756 /**
1757  * Executes a function call for bc.
1758  * @param p     The program.
1759  * @param code  The bytecode vector to pull the number of arguments and the
1760  *              function index out of.
1761  * @param bgn   An in/out parameter; the start of the indices in the bytecode
1762  *              vector, and will be updated to point after the indices on
1763  *              return.
1764  */
1765 static void
1766 bc_program_call(BcProgram* p, const char* restrict code, size_t* restrict bgn)
1767 {
1768 	BcInstPtr ip;
1769 	size_t i, nargs;
1770 	BcFunc* f;
1771 	BcVec* v;
1772 	BcAuto* a;
1773 	BcResult* arg;
1774 
1775 	// Pull the number of arguments out of the bytecode vector.
1776 	nargs = bc_program_index(code, bgn);
1777 
1778 	// Set up instruction pointer.
1779 	ip.idx = 0;
1780 	ip.func = bc_program_index(code, bgn);
1781 	f = bc_vec_item(&p->fns, ip.func);
1782 
1783 	// Error checking.
1784 	if (BC_ERR(!f->code.len)) bc_verr(BC_ERR_EXEC_UNDEF_FUNC, f->name);
1785 	if (BC_ERR(nargs != f->nparams))
1786 	{
1787 		bc_verr(BC_ERR_EXEC_PARAMS, f->nparams, nargs);
1788 	}
1789 
1790 	// Set the length of the results stack. We discount the argument, of course.
1791 	ip.len = p->results.len - nargs;
1792 
1793 	assert(BC_PROG_STACK(&p->results, nargs));
1794 
1795 	// Prepare the globals' stacks.
1796 	if (BC_G) bc_program_prepGlobals(p);
1797 
1798 	// Push the arguments onto the stacks of their respective parameters.
1799 	for (i = 0; i < nargs; ++i)
1800 	{
1801 		arg = bc_vec_top(&p->results);
1802 		if (BC_ERR(arg->t == BC_RESULT_VOID)) bc_err(BC_ERR_EXEC_VOID_VAL);
1803 
1804 		// Get the corresponding parameter.
1805 		a = bc_vec_item(&f->autos, nargs - 1 - i);
1806 
1807 		// Actually push the value onto the parameter's stack.
1808 		bc_program_copyToVar(p, a->idx, a->type);
1809 	}
1810 
1811 	BC_SIG_LOCK;
1812 
1813 	// Push zeroes onto the stacks of the auto variables.
1814 	for (; i < f->autos.len; ++i)
1815 	{
1816 		// Get the auto and its stack.
1817 		a = bc_vec_item(&f->autos, i);
1818 		v = bc_program_vec(p, a->idx, a->type);
1819 
1820 		// If a variable, just push a 0; otherwise, push an array.
1821 		if (a->type == BC_TYPE_VAR)
1822 		{
1823 			BcNum* n = bc_vec_pushEmpty(v);
1824 			bc_num_init(n, BC_NUM_DEF_SIZE);
1825 		}
1826 		else
1827 		{
1828 			BcVec* v2;
1829 
1830 			assert(a->type == BC_TYPE_ARRAY);
1831 
1832 			v2 = bc_vec_pushEmpty(v);
1833 			bc_array_init(v2, true);
1834 		}
1835 	}
1836 
1837 	// Push the instruction pointer onto the execution stack.
1838 	bc_vec_push(&p->stack, &ip);
1839 
1840 	BC_SIG_UNLOCK;
1841 }
1842 
1843 /**
1844  * Executes a return instruction.
1845  * @param p     The program.
1846  * @param inst  The return instruction. bc can return void, and we need to know
1847  *              if it is.
1848  */
1849 static void
1850 bc_program_return(BcProgram* p, uchar inst)
1851 {
1852 	BcResult* res;
1853 	BcFunc* f;
1854 	BcInstPtr* ip;
1855 	size_t i, nresults;
1856 
1857 	// Get the instruction pointer.
1858 	ip = bc_vec_top(&p->stack);
1859 
1860 	// Get the difference between the actual number of results and the number of
1861 	// results the caller expects.
1862 	nresults = p->results.len - ip->len;
1863 
1864 	// If this isn't true, there was a missing call somewhere.
1865 	assert(BC_PROG_STACK(&p->stack, 2));
1866 
1867 	// If this isn't true, the parser screwed by giving us no value when we
1868 	// expected one, or giving us a value when we expected none.
1869 	assert(BC_PROG_STACK(&p->results, ip->len + (inst == BC_INST_RET)));
1870 
1871 	// Get the function we are returning from.
1872 	f = bc_vec_item(&p->fns, ip->func);
1873 
1874 	res = bc_program_prepResult(p);
1875 
1876 	// If we are returning normally...
1877 	if (inst == BC_INST_RET)
1878 	{
1879 		BcNum* num;
1880 		BcResult* operand;
1881 
1882 		// Prepare and copy the return value.
1883 		bc_program_operand(p, &operand, &num, 1);
1884 
1885 		if (BC_PROG_STR(num))
1886 		{
1887 			// We need to set this because otherwise, it will be a
1888 			// BC_RESULT_TEMP, and BC_RESULT_TEMP needs an actual number to make
1889 			// it easier to do type checking.
1890 			res->t = BC_RESULT_STR;
1891 
1892 			// NOLINTNEXTLINE
1893 			memcpy(&res->d.n, num, sizeof(BcNum));
1894 		}
1895 		else
1896 		{
1897 			BC_SIG_LOCK;
1898 
1899 			bc_num_createCopy(&res->d.n, num);
1900 		}
1901 	}
1902 	// Void is easy; set the result.
1903 	else if (inst == BC_INST_RET_VOID) res->t = BC_RESULT_VOID;
1904 	else
1905 	{
1906 		BC_SIG_LOCK;
1907 
1908 		// If we get here, the instruction is for returning a zero, so do that.
1909 		bc_num_init(&res->d.n, BC_NUM_DEF_SIZE);
1910 	}
1911 
1912 	BC_SIG_MAYUNLOCK;
1913 
1914 	// We need to pop items off of the stacks of arguments and autos as well.
1915 	for (i = 0; i < f->autos.len; ++i)
1916 	{
1917 		BcAuto* a = bc_vec_item(&f->autos, i);
1918 		BcVec* v = bc_program_vec(p, a->idx, a->type);
1919 
1920 		bc_vec_pop(v);
1921 	}
1922 
1923 	BC_SIG_LOCK;
1924 
1925 	// When we retire, pop all of the unused results.
1926 	bc_program_retire(p, 1, nresults);
1927 
1928 	// Pop the globals, if necessary.
1929 	if (BC_G) bc_program_popGlobals(p, false);
1930 
1931 	// Pop the stack. This is what causes the function to actually "return."
1932 	bc_vec_pop(&p->stack);
1933 
1934 	BC_SIG_UNLOCK;
1935 }
1936 #endif // BC_ENABLED
1937 
1938 /**
1939  * Executes a builtin function.
1940  * @param p     The program.
1941  * @param inst  The builtin to execute.
1942  */
1943 static void
1944 bc_program_builtin(BcProgram* p, uchar inst)
1945 {
1946 	BcResult* opd;
1947 	BcResult* res;
1948 	BcNum* num;
1949 	bool len = (inst == BC_INST_LENGTH);
1950 
1951 	// Ensure we have a valid builtin.
1952 #if BC_ENABLE_EXTRA_MATH
1953 	assert(inst >= BC_INST_LENGTH && inst <= BC_INST_IRAND);
1954 #else // BC_ENABLE_EXTRA_MATH
1955 	assert(inst >= BC_INST_LENGTH && inst <= BC_INST_IS_STRING);
1956 #endif // BC_ENABLE_EXTRA_MATH
1957 
1958 #ifndef BC_PROG_NO_STACK_CHECK
1959 	// Check stack for dc.
1960 	if (BC_IS_DC && BC_ERR(!BC_PROG_STACK(&p->results, 1)))
1961 	{
1962 		bc_err(BC_ERR_EXEC_STACK);
1963 	}
1964 #endif // BC_PROG_NO_STACK_CHECK
1965 
1966 	assert(BC_PROG_STACK(&p->results, 1));
1967 
1968 	res = bc_program_prepResult(p);
1969 
1970 	bc_program_operand(p, &opd, &num, 1);
1971 
1972 	assert(num != NULL);
1973 
1974 	// We need to ensure that strings and arrays aren't passed to most builtins.
1975 	// The scale function can take strings in dc.
1976 	if (!len && (inst != BC_INST_SCALE_FUNC || BC_IS_BC) &&
1977 	    inst != BC_INST_IS_NUMBER && inst != BC_INST_IS_STRING)
1978 	{
1979 		bc_program_type_num(opd, num);
1980 	}
1981 
1982 	// Square root is easy.
1983 	if (inst == BC_INST_SQRT) bc_num_sqrt(num, &res->d.n, BC_PROG_SCALE(p));
1984 
1985 	// Absolute value is easy.
1986 	else if (inst == BC_INST_ABS)
1987 	{
1988 		BC_SIG_LOCK;
1989 
1990 		bc_num_createCopy(&res->d.n, num);
1991 
1992 		BC_SIG_UNLOCK;
1993 
1994 		BC_NUM_NEG_CLR_NP(res->d.n);
1995 	}
1996 
1997 	// Testing for number or string is easy.
1998 	else if (inst == BC_INST_IS_NUMBER || inst == BC_INST_IS_STRING)
1999 	{
2000 		bool cond;
2001 		bool is_str;
2002 
2003 		BC_SIG_LOCK;
2004 
2005 		bc_num_init(&res->d.n, BC_NUM_DEF_SIZE);
2006 
2007 		BC_SIG_UNLOCK;
2008 
2009 		// Test if the number is a string.
2010 		is_str = BC_PROG_STR(num);
2011 
2012 		// This confusing condition simply means that the instruction must be
2013 		// true if is_str is, or it must be false if is_str is. Otherwise, the
2014 		// returned value is false (0).
2015 		cond = ((inst == BC_INST_IS_STRING) == is_str);
2016 		if (cond) bc_num_one(&res->d.n);
2017 	}
2018 
2019 #if BC_ENABLE_EXTRA_MATH
2020 
2021 	// irand() is easy.
2022 	else if (inst == BC_INST_IRAND)
2023 	{
2024 		BC_SIG_LOCK;
2025 
2026 		bc_num_init(&res->d.n, num->len - BC_NUM_RDX_VAL(num));
2027 
2028 		BC_SIG_UNLOCK;
2029 
2030 		bc_num_irand(num, &res->d.n, &p->rng);
2031 	}
2032 
2033 #endif // BC_ENABLE_EXTRA_MATH
2034 
2035 	// Everything else is...not easy.
2036 	else
2037 	{
2038 		BcBigDig val = 0;
2039 
2040 		// Well, scale() is easy, but length() is not.
2041 		if (len)
2042 		{
2043 			// If we are bc and we have an array...
2044 			if (opd->t == BC_RESULT_ARRAY)
2045 			{
2046 				// Yes, this is one place where we need to cast the number from
2047 				// bc_program_num() to a vector.
2048 				BcVec* v = (BcVec*) num;
2049 
2050 				// XXX: If this is changed, you should also change the similar
2051 				// code in bc_program_asciify().
2052 
2053 #if BC_ENABLED
2054 				// Dereference the array, if necessary.
2055 				if (BC_IS_BC && v->size == sizeof(uchar))
2056 				{
2057 					v = bc_program_dereference(p, v);
2058 				}
2059 #endif // BC_ENABLED
2060 
2061 				assert(v->size == sizeof(BcNum));
2062 
2063 				val = (BcBigDig) v->len;
2064 			}
2065 			else
2066 			{
2067 				// If the item is a string...
2068 				if (!BC_PROG_NUM(opd, num))
2069 				{
2070 					char* str;
2071 
2072 					// Get the string, then get the length.
2073 					str = bc_program_string(p, num);
2074 					val = (BcBigDig) strlen(str);
2075 				}
2076 				else
2077 				{
2078 					// Calculate the length of the number.
2079 					val = (BcBigDig) bc_num_len(num);
2080 				}
2081 			}
2082 		}
2083 		// Like I said; scale() is actually easy. It just also needs the integer
2084 		// conversion that length() does.
2085 		else if (BC_IS_BC || BC_PROG_NUM(opd, num))
2086 		{
2087 			val = (BcBigDig) bc_num_scale(num);
2088 		}
2089 
2090 		BC_SIG_LOCK;
2091 
2092 		// Create the result.
2093 		bc_num_createFromBigdig(&res->d.n, val);
2094 
2095 		BC_SIG_UNLOCK;
2096 	}
2097 
2098 	bc_program_retire(p, 1, 1);
2099 }
2100 
2101 /**
2102  * Executes a divmod.
2103  * @param p  The program.
2104  */
2105 static void
2106 bc_program_divmod(BcProgram* p)
2107 {
2108 	BcResult* opd1;
2109 	BcResult* opd2;
2110 	BcResult* res;
2111 	BcResult* res2;
2112 	BcNum* n1;
2113 	BcNum* n2;
2114 	size_t req;
2115 
2116 	// We grow first to avoid pointer invalidation.
2117 	bc_vec_grow(&p->results, 2);
2118 
2119 	// We don't need to update the pointer because
2120 	// the capacity is enough due to the line above.
2121 	res2 = bc_program_prepResult(p);
2122 	res = bc_program_prepResult(p);
2123 
2124 	// Prepare the operands.
2125 	bc_program_binOpPrep(p, &opd1, &n1, &opd2, &n2, 2);
2126 
2127 	req = bc_num_mulReq(n1, n2, BC_PROG_SCALE(p));
2128 
2129 	BC_SIG_LOCK;
2130 
2131 	// Initialize the results.
2132 	bc_num_init(&res->d.n, req);
2133 	bc_num_init(&res2->d.n, req);
2134 
2135 	BC_SIG_UNLOCK;
2136 
2137 	// Execute.
2138 	bc_num_divmod(n1, n2, &res2->d.n, &res->d.n, BC_PROG_SCALE(p));
2139 
2140 	bc_program_retire(p, 2, 2);
2141 }
2142 
2143 /**
2144  * Executes modular exponentiation.
2145  * @param p  The program.
2146  */
2147 static void
2148 bc_program_modexp(BcProgram* p)
2149 {
2150 	BcResult* r1;
2151 	BcResult* r2;
2152 	BcResult* r3;
2153 	BcResult* res;
2154 	BcNum* n1;
2155 	BcNum* n2;
2156 	BcNum* n3;
2157 
2158 #if DC_ENABLED
2159 
2160 	// Check the stack.
2161 	if (BC_IS_DC && BC_ERR(!BC_PROG_STACK(&p->results, 3)))
2162 	{
2163 		bc_err(BC_ERR_EXEC_STACK);
2164 	}
2165 
2166 #endif // DC_ENABLED
2167 
2168 	assert(BC_PROG_STACK(&p->results, 3));
2169 
2170 	res = bc_program_prepResult(p);
2171 
2172 	// Get the first operand and typecheck.
2173 	bc_program_operand(p, &r1, &n1, 3);
2174 	bc_program_type_num(r1, n1);
2175 
2176 	// Get the last two operands.
2177 	bc_program_binOpPrep(p, &r2, &n2, &r3, &n3, 1);
2178 
2179 	// Make sure that the values have their pointers updated, if necessary.
2180 	// Only array elements are possible because this is dc.
2181 	if (r1->t == BC_RESULT_ARRAY_ELEM && (r1->t == r2->t || r1->t == r3->t))
2182 	{
2183 		n1 = bc_program_num(p, r1);
2184 	}
2185 
2186 	BC_SIG_LOCK;
2187 
2188 	bc_num_init(&res->d.n, n3->len);
2189 
2190 	BC_SIG_UNLOCK;
2191 
2192 	bc_num_modexp(n1, n2, n3, &res->d.n);
2193 
2194 	bc_program_retire(p, 1, 3);
2195 }
2196 
2197 /**
2198  * Asciifies a number for dc. This is a helper for bc_program_asciify().
2199  * @param p  The program.
2200  * @param n  The number to asciify.
2201  */
2202 static uchar
2203 bc_program_asciifyNum(BcProgram* p, BcNum* n)
2204 {
2205 	bc_num_copy(&p->asciify, n);
2206 
2207 	// We want to clear the scale and sign for easy mod later.
2208 	bc_num_truncate(&p->asciify, p->asciify.scale);
2209 	BC_NUM_NEG_CLR(&p->asciify);
2210 
2211 	// This is guaranteed to not have a divide by 0
2212 	// because strmb is equal to 256.
2213 	bc_num_mod(&p->asciify, &p->strmb, &p->asciify, 0);
2214 
2215 	// This is also guaranteed to not error because num is in the range
2216 	// [0, UCHAR_MAX], which is definitely in range for a BcBigDig. And
2217 	// it is not negative.
2218 	return (uchar) bc_num_bigdig2(&p->asciify);
2219 }
2220 
2221 /**
2222  * Executes the "asciify" command in bc and dc.
2223  * @param p  The program.
2224  */
2225 static void
2226 bc_program_asciify(BcProgram* p)
2227 {
2228 	BcResult *r, res;
2229 	BcNum* n;
2230 	uchar c;
2231 	size_t idx;
2232 #if BC_ENABLED
2233 	// This is in the outer scope because it has to be freed after a jump.
2234 	char* temp_str;
2235 #endif // BC_ENABLED
2236 
2237 	// Check the stack.
2238 	if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK);
2239 
2240 	assert(BC_PROG_STACK(&p->results, 1));
2241 
2242 	// Get the top of the results stack.
2243 	bc_program_operand(p, &r, &n, 0);
2244 
2245 	assert(n != NULL);
2246 	assert(BC_IS_BC || r->t != BC_RESULT_ARRAY);
2247 
2248 #if BC_ENABLED
2249 	// Handle arrays in bc specially.
2250 	if (r->t == BC_RESULT_ARRAY)
2251 	{
2252 		// Yes, this is one place where we need to cast the number from
2253 		// bc_program_num() to a vector.
2254 		BcVec* v = (BcVec*) n;
2255 		size_t i;
2256 
2257 		// XXX: If this is changed, you should also change the similar code in
2258 		// bc_program_builtin().
2259 
2260 		// Dereference the array, if necessary.
2261 		if (v->size == sizeof(uchar))
2262 		{
2263 			v = bc_program_dereference(p, v);
2264 		}
2265 
2266 		assert(v->size == sizeof(BcNum));
2267 
2268 		// Allocate the string and set the jump for it.
2269 		BC_SIG_LOCK;
2270 		temp_str = bc_vm_malloc(v->len + 1);
2271 		BC_SETJMP_LOCKED(vm, exit);
2272 		BC_SIG_UNLOCK;
2273 
2274 		// Convert the array.
2275 		for (i = 0; i < v->len; ++i)
2276 		{
2277 			BcNum* num = (BcNum*) bc_vec_item(v, i);
2278 
2279 			if (BC_PROG_STR(num))
2280 			{
2281 				temp_str[i] = (bc_program_string(p, num))[0];
2282 			}
2283 			else
2284 			{
2285 				temp_str[i] = (char) bc_program_asciifyNum(p, num);
2286 			}
2287 		}
2288 
2289 		temp_str[v->len] = '\0';
2290 
2291 		// Store the string in the slab and map, and free the temp string.
2292 		BC_SIG_LOCK;
2293 		idx = bc_program_addString(p, temp_str);
2294 		free(temp_str);
2295 		BC_UNSETJMP(vm);
2296 		BC_SIG_UNLOCK;
2297 	}
2298 	else
2299 #endif // BC_ENABLED
2300 	{
2301 		char str[2];
2302 		char* str2;
2303 
2304 		// Asciify.
2305 		if (BC_PROG_NUM(r, n)) c = bc_program_asciifyNum(p, n);
2306 		else
2307 		{
2308 			// Get the string itself, then the first character.
2309 			str2 = bc_program_string(p, n);
2310 			c = (uchar) str2[0];
2311 		}
2312 
2313 		// Fill the resulting string.
2314 		str[0] = (char) c;
2315 		str[1] = '\0';
2316 
2317 		// Add the string to the data structures.
2318 		BC_SIG_LOCK;
2319 		idx = bc_program_addString(p, str);
2320 		BC_SIG_UNLOCK;
2321 	}
2322 
2323 	// Set the result
2324 	res.t = BC_RESULT_STR;
2325 	bc_num_clear(&res.d.n);
2326 	res.d.n.scale = idx;
2327 
2328 	// Pop and push.
2329 	bc_vec_pop(&p->results);
2330 	bc_vec_push(&p->results, &res);
2331 
2332 	return;
2333 
2334 #if BC_ENABLED
2335 exit:
2336 	free(temp_str);
2337 #endif // BC_ENABLED
2338 }
2339 
2340 /**
2341  * Streams a number or a string to stdout.
2342  * @param p  The program.
2343  */
2344 static void
2345 bc_program_printStream(BcProgram* p)
2346 {
2347 	BcResult* r;
2348 	BcNum* n;
2349 
2350 	// Check the stack.
2351 	if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK);
2352 
2353 	assert(BC_PROG_STACK(&p->results, 1));
2354 
2355 	// Get the top of the results stack.
2356 	bc_program_operand(p, &r, &n, 0);
2357 
2358 	assert(n != NULL);
2359 
2360 	// Stream appropriately.
2361 	if (BC_PROG_NUM(r, n)) bc_num_stream(n);
2362 	else bc_program_printChars(bc_program_string(p, n));
2363 
2364 	// Pop the operand.
2365 	bc_vec_pop(&p->results);
2366 }
2367 
2368 #if DC_ENABLED
2369 
2370 /**
2371  * Gets the length of a register in dc and pushes it onto the results stack.
2372  * @param p     The program.
2373  * @param code  The bytecode vector to pull the register's index out of.
2374  * @param bgn   An in/out parameter; the start of the index in the bytecode
2375  *              vector, and will be updated to point after the index on return.
2376  */
2377 static void
2378 bc_program_regStackLen(BcProgram* p, const char* restrict code,
2379                        size_t* restrict bgn)
2380 {
2381 	size_t idx = bc_program_index(code, bgn);
2382 	BcVec* v = bc_program_vec(p, idx, BC_TYPE_VAR);
2383 
2384 	bc_program_pushBigdig(p, (BcBigDig) v->len, BC_RESULT_TEMP);
2385 }
2386 
2387 /**
2388  * Pushes the length of the results stack onto the results stack.
2389  * @param p  The program.
2390  */
2391 static void
2392 bc_program_stackLen(BcProgram* p)
2393 {
2394 	bc_program_pushBigdig(p, (BcBigDig) p->results.len, BC_RESULT_TEMP);
2395 }
2396 
2397 /**
2398  * Pops a certain number of elements off the execution stack.
2399  * @param p     The program.
2400  * @param inst  The instruction to tell us how many. There is one to pop up to
2401  *              2, and one to pop the amount equal to the number at the top of
2402  *              the results stack.
2403  */
2404 static void
2405 bc_program_nquit(BcProgram* p, uchar inst)
2406 {
2407 	BcResult* opnd;
2408 	BcNum* num;
2409 	BcBigDig val;
2410 	size_t i;
2411 
2412 	// Ensure that the tail calls stack is correct.
2413 	assert(p->stack.len == p->tail_calls.len);
2414 
2415 	// Get the number of executions to pop.
2416 	if (inst == BC_INST_QUIT) val = 2;
2417 	else
2418 	{
2419 		bc_program_prep(p, &opnd, &num, 0);
2420 		val = bc_num_bigdig(num);
2421 
2422 		bc_vec_pop(&p->results);
2423 	}
2424 
2425 	// Loop over the tail call stack and adjust the quit value appropriately.
2426 	for (i = 0; val && i < p->tail_calls.len; ++i)
2427 	{
2428 		// Get the number of tail calls for this one.
2429 		size_t calls = *((size_t*) bc_vec_item_rev(&p->tail_calls, i)) + 1;
2430 
2431 		// Adjust the value.
2432 		if (calls >= val) val = 0;
2433 		else val -= (BcBigDig) calls;
2434 	}
2435 
2436 	// If we don't have enough executions, just quit.
2437 	if (i == p->stack.len)
2438 	{
2439 		vm->status = BC_STATUS_QUIT;
2440 		BC_JMP;
2441 	}
2442 	else
2443 	{
2444 		// We can always pop the last item we reached on the tail call stack
2445 		// because these are for tail calls. That means that any executions that
2446 		// we would not have quit in that position on the stack would have quit
2447 		// anyway.
2448 		BC_SIG_LOCK;
2449 		bc_vec_npop(&p->stack, i);
2450 		bc_vec_npop(&p->tail_calls, i);
2451 		BC_SIG_UNLOCK;
2452 	}
2453 }
2454 
2455 /**
2456  * Pushes the depth of the execution stack onto the stack.
2457  * @param p  The program.
2458  */
2459 static void
2460 bc_program_execStackLen(BcProgram* p)
2461 {
2462 	size_t i, amt, len = p->tail_calls.len;
2463 
2464 	amt = len;
2465 
2466 	for (i = 0; i < len; ++i)
2467 	{
2468 		amt += *((size_t*) bc_vec_item(&p->tail_calls, i));
2469 	}
2470 
2471 	bc_program_pushBigdig(p, (BcBigDig) amt, BC_RESULT_TEMP);
2472 }
2473 
2474 /**
2475  *
2476  * @param p     The program.
2477  * @param code  The bytecode vector to pull the register's index out of.
2478  * @param bgn   An in/out parameter; the start of the index in the bytecode
2479  *              vector, and will be updated to point after the index on return.
2480  * @param cond  True if the execution is conditional, false otherwise.
2481  * @param len   The number of bytes in the bytecode vector.
2482  */
2483 static void
2484 bc_program_execStr(BcProgram* p, const char* restrict code,
2485                    size_t* restrict bgn, bool cond, size_t len)
2486 {
2487 	BcResult* r;
2488 	char* str;
2489 	BcFunc* f;
2490 	BcInstPtr ip;
2491 	size_t fidx;
2492 	BcNum* n;
2493 
2494 	assert(p->stack.len == p->tail_calls.len);
2495 
2496 	// Check the stack.
2497 	if (BC_ERR(!BC_PROG_STACK(&p->results, 1))) bc_err(BC_ERR_EXEC_STACK);
2498 
2499 	assert(BC_PROG_STACK(&p->results, 1));
2500 
2501 	// Get the operand.
2502 	bc_program_operand(p, &r, &n, 0);
2503 
2504 	// If execution is conditional...
2505 	if (cond)
2506 	{
2507 		bool exec;
2508 		size_t then_idx;
2509 		// These are volatile to quiet warnings on GCC about clobbering with
2510 		// longjmp().
2511 		volatile size_t else_idx;
2512 		volatile size_t idx;
2513 
2514 		// Get the index of the "then" var and "else" var.
2515 		then_idx = bc_program_index(code, bgn);
2516 		else_idx = bc_program_index(code, bgn);
2517 
2518 		// Figure out if we should execute.
2519 		exec = (r->d.n.len != 0);
2520 
2521 		idx = exec ? then_idx : else_idx;
2522 
2523 		BC_SIG_LOCK;
2524 		BC_SETJMP_LOCKED(vm, exit);
2525 
2526 		// If we are supposed to execute, execute. If else_idx == SIZE_MAX, that
2527 		// means there was no else clause, so if execute is false and else does
2528 		// not exist, we don't execute. The goto skips all of the setup for the
2529 		// execution.
2530 		if (exec || (else_idx != SIZE_MAX))
2531 		{
2532 			n = bc_vec_top(bc_program_vec(p, idx, BC_TYPE_VAR));
2533 		}
2534 		else goto exit;
2535 
2536 		if (BC_ERR(!BC_PROG_STR(n))) bc_err(BC_ERR_EXEC_TYPE);
2537 
2538 		BC_UNSETJMP(vm);
2539 		BC_SIG_UNLOCK;
2540 	}
2541 	else
2542 	{
2543 		// In non-conditional situations, only the top of stack can be executed,
2544 		// and in those cases, variables are not allowed to be "on the stack";
2545 		// they are only put on the stack to be assigned to.
2546 		assert(r->t != BC_RESULT_VAR);
2547 
2548 		if (r->t != BC_RESULT_STR) return;
2549 	}
2550 
2551 	assert(BC_PROG_STR(n));
2552 
2553 	// Get the string.
2554 	str = bc_program_string(p, n);
2555 
2556 	// Get the function index and function.
2557 	BC_SIG_LOCK;
2558 	fidx = bc_program_insertFunc(p, str);
2559 	BC_SIG_UNLOCK;
2560 	f = bc_vec_item(&p->fns, fidx);
2561 
2562 	// If the function has not been parsed yet...
2563 	if (!f->code.len)
2564 	{
2565 		BC_SIG_LOCK;
2566 
2567 		if (!BC_PARSE_IS_INITED(&vm->read_prs, p))
2568 		{
2569 			bc_parse_init(&vm->read_prs, p, fidx);
2570 
2571 			// Initialize this too because bc_vm_shutdown() expects them to be
2572 			// initialized togther.
2573 			bc_vec_init(&vm->read_buf, sizeof(char), BC_DTOR_NONE);
2574 		}
2575 		// This needs to be updated because the parser could have been used
2576 		// somewhere else
2577 		else bc_parse_updateFunc(&vm->read_prs, fidx);
2578 
2579 		bc_lex_file(&vm->read_prs.l, vm->file);
2580 
2581 		BC_SETJMP_LOCKED(vm, err);
2582 
2583 		BC_SIG_UNLOCK;
2584 
2585 		// Parse. Only one expression is needed, so stdin isn't used.
2586 		bc_parse_text(&vm->read_prs, str, BC_MODE_FILE);
2587 
2588 		BC_SIG_LOCK;
2589 		vm->expr(&vm->read_prs, BC_PARSE_NOCALL);
2590 
2591 		BC_UNSETJMP(vm);
2592 
2593 		// We can just assert this here because
2594 		// dc should parse everything until EOF.
2595 		assert(vm->read_prs.l.t == BC_LEX_EOF);
2596 
2597 		BC_SIG_UNLOCK;
2598 	}
2599 
2600 	// Set the instruction pointer.
2601 	ip.idx = 0;
2602 	ip.len = p->results.len;
2603 	ip.func = fidx;
2604 
2605 	BC_SIG_LOCK;
2606 
2607 	// Pop the operand.
2608 	bc_vec_pop(&p->results);
2609 
2610 	// Tail call processing. This condition means that there is more on the
2611 	// execution stack, and we are at the end of the bytecode vector, and the
2612 	// last instruction is just a BC_INST_POP_EXEC, which would return.
2613 	if (p->stack.len > 1 && *bgn == len - 1 && code[*bgn] == BC_INST_POP_EXEC)
2614 	{
2615 		size_t* call_ptr = bc_vec_top(&p->tail_calls);
2616 
2617 		// Add one to the tail call.
2618 		*call_ptr += 1;
2619 
2620 		// Pop the execution stack before pushing the new instruction pointer
2621 		// on.
2622 		bc_vec_pop(&p->stack);
2623 	}
2624 	// If not a tail call, just push a new one.
2625 	else bc_vec_push(&p->tail_calls, &ip.idx);
2626 
2627 	// Push the new function onto the execution stack and return.
2628 	bc_vec_push(&p->stack, &ip);
2629 
2630 	BC_SIG_UNLOCK;
2631 
2632 	return;
2633 
2634 err:
2635 	BC_SIG_MAYLOCK;
2636 
2637 	f = bc_vec_item(&p->fns, fidx);
2638 
2639 	// Make sure to erase the bytecode vector so dc knows it is not parsed.
2640 	bc_vec_popAll(&f->code);
2641 
2642 exit:
2643 	bc_vec_pop(&p->results);
2644 	BC_LONGJMP_CONT(vm);
2645 }
2646 
2647 /**
2648  * Prints every item on the results stack, one per line.
2649  * @param p  The program.
2650  */
2651 static void
2652 bc_program_printStack(BcProgram* p)
2653 {
2654 	size_t idx;
2655 
2656 	for (idx = 0; idx < p->results.len; ++idx)
2657 	{
2658 		bc_program_print(p, BC_INST_PRINT, idx);
2659 	}
2660 }
2661 #endif // DC_ENABLED
2662 
2663 /**
2664  * Pushes the value of a global onto the results stack.
2665  * @param p     The program.
2666  * @param inst  Which global to push, as an instruction.
2667  */
2668 static void
2669 bc_program_pushGlobal(BcProgram* p, uchar inst)
2670 {
2671 	BcResultType t;
2672 
2673 	// Make sure the instruction is valid.
2674 	assert(inst >= BC_INST_IBASE && inst <= BC_INST_SCALE);
2675 
2676 	// Push the global.
2677 	t = inst - BC_INST_IBASE + BC_RESULT_IBASE;
2678 	bc_program_pushBigdig(p, p->globals[inst - BC_INST_IBASE], t);
2679 }
2680 
2681 /**
2682  * Pushes the value of a global setting onto the stack.
2683  * @param p     The program.
2684  * @param inst  Which global setting to push, as an instruction.
2685  */
2686 static void
2687 bc_program_globalSetting(BcProgram* p, uchar inst)
2688 {
2689 	BcBigDig val;
2690 
2691 	// Make sure the instruction is valid.
2692 	assert(inst >= BC_INST_LINE_LENGTH && inst <= BC_INST_LEADING_ZERO);
2693 
2694 	if (inst == BC_INST_LINE_LENGTH) val = (BcBigDig) vm->line_len;
2695 #if BC_ENABLED
2696 	else if (inst == BC_INST_GLOBAL_STACKS) val = (BC_G != 0);
2697 #endif // BC_ENABLED
2698 	else val = (BC_Z != 0);
2699 
2700 	// Push the global.
2701 	bc_program_pushBigdig(p, val, BC_RESULT_TEMP);
2702 }
2703 
2704 #if BC_ENABLE_EXTRA_MATH
2705 
2706 /**
2707  * Pushes the value of seed on the stack.
2708  * @param p  The program.
2709  */
2710 static void
2711 bc_program_pushSeed(BcProgram* p)
2712 {
2713 	BcResult* res;
2714 
2715 	res = bc_program_prepResult(p);
2716 	res->t = BC_RESULT_SEED;
2717 
2718 	BC_SIG_LOCK;
2719 
2720 	// We need 2*BC_RAND_NUM_SIZE because of the size of the state.
2721 	bc_num_init(&res->d.n, 2 * BC_RAND_NUM_SIZE);
2722 
2723 	BC_SIG_UNLOCK;
2724 
2725 	bc_num_createFromRNG(&res->d.n, &p->rng);
2726 }
2727 
2728 #endif // BC_ENABLE_EXTRA_MATH
2729 
2730 /**
2731  * Adds a function to the fns array. The function's ID must have already been
2732  * inserted into the map.
2733  * @param p       The program.
2734  * @param id_ptr  The ID of the function as inserted into the map.
2735  */
2736 static void
2737 bc_program_addFunc(BcProgram* p, BcId* id_ptr)
2738 {
2739 	BcFunc* f;
2740 
2741 	BC_SIG_ASSERT_LOCKED;
2742 
2743 	// Push and init.
2744 	f = bc_vec_pushEmpty(&p->fns);
2745 	bc_func_init(f, id_ptr->name);
2746 }
2747 
2748 size_t
2749 bc_program_insertFunc(BcProgram* p, const char* name)
2750 {
2751 	BcId* id_ptr;
2752 	bool new;
2753 	size_t idx;
2754 
2755 	BC_SIG_ASSERT_LOCKED;
2756 
2757 	assert(p != NULL && name != NULL);
2758 
2759 	// Insert into the map and get the resulting ID.
2760 	new = bc_map_insert(&p->fn_map, name, p->fns.len, &idx);
2761 	id_ptr = (BcId*) bc_vec_item(&p->fn_map, idx);
2762 	idx = id_ptr->idx;
2763 
2764 	// If the function is new...
2765 	if (new)
2766 	{
2767 		// Add the function to the fns array.
2768 		bc_program_addFunc(p, id_ptr);
2769 	}
2770 #if BC_ENABLED
2771 	// bc has to reset the function because it's about to be redefined.
2772 	else if (BC_IS_BC)
2773 	{
2774 		BcFunc* func = bc_vec_item(&p->fns, idx);
2775 		bc_func_reset(func);
2776 	}
2777 #endif // BC_ENABLED
2778 
2779 	return idx;
2780 }
2781 
2782 #ifndef NDEBUG
2783 void
2784 bc_program_free(BcProgram* p)
2785 {
2786 #if BC_ENABLED
2787 	size_t i;
2788 #endif // BC_ENABLED
2789 
2790 	BC_SIG_ASSERT_LOCKED;
2791 
2792 	assert(p != NULL);
2793 
2794 #if BC_ENABLED
2795 	// Free the globals stacks.
2796 	for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i)
2797 	{
2798 		bc_vec_free(p->globals_v + i);
2799 	}
2800 #endif // BC_ENABLED
2801 
2802 	bc_vec_free(&p->fns);
2803 	bc_vec_free(&p->fn_map);
2804 	bc_vec_free(&p->vars);
2805 	bc_vec_free(&p->var_map);
2806 	bc_vec_free(&p->arrs);
2807 	bc_vec_free(&p->arr_map);
2808 	bc_vec_free(&p->results);
2809 	bc_vec_free(&p->stack);
2810 	bc_vec_free(&p->consts);
2811 	bc_vec_free(&p->const_map);
2812 	bc_vec_free(&p->strs);
2813 	bc_vec_free(&p->str_map);
2814 
2815 	bc_num_free(&p->asciify);
2816 
2817 #if BC_ENABLED
2818 	if (BC_IS_BC) bc_num_free(&p->last);
2819 #endif // BC_ENABLED
2820 
2821 #if BC_ENABLE_EXTRA_MATH
2822 	bc_rand_free(&p->rng);
2823 #endif // BC_ENABLE_EXTRA_MATH
2824 
2825 #if DC_ENABLED
2826 	if (BC_IS_DC) bc_vec_free(&p->tail_calls);
2827 #endif // DC_ENABLED
2828 }
2829 #endif // NDEBUG
2830 
2831 void
2832 bc_program_init(BcProgram* p)
2833 {
2834 	BcInstPtr ip;
2835 	size_t i;
2836 
2837 	BC_SIG_ASSERT_LOCKED;
2838 
2839 	assert(p != NULL);
2840 
2841 	// We want this clear.
2842 	// NOLINTNEXTLINE
2843 	memset(&ip, 0, sizeof(BcInstPtr));
2844 
2845 	// Setup the globals stacks and the current values.
2846 	for (i = 0; i < BC_PROG_GLOBALS_LEN; ++i)
2847 	{
2848 		BcBigDig val = i == BC_PROG_GLOBALS_SCALE ? 0 : BC_BASE;
2849 
2850 #if BC_ENABLED
2851 		bc_vec_init(p->globals_v + i, sizeof(BcBigDig), BC_DTOR_NONE);
2852 		bc_vec_push(p->globals_v + i, &val);
2853 #endif // BC_ENABLED
2854 
2855 		p->globals[i] = val;
2856 	}
2857 
2858 #if DC_ENABLED
2859 	// dc-only setup.
2860 	if (BC_IS_DC)
2861 	{
2862 		bc_vec_init(&p->tail_calls, sizeof(size_t), BC_DTOR_NONE);
2863 
2864 		// We want an item for the main function on the tail call stack.
2865 		i = 0;
2866 		bc_vec_push(&p->tail_calls, &i);
2867 	}
2868 #endif // DC_ENABLED
2869 
2870 	bc_num_setup(&p->strmb, p->strmb_num, BC_NUM_BIGDIG_LOG10);
2871 	bc_num_bigdig2num(&p->strmb, BC_NUM_STREAM_BASE);
2872 
2873 	bc_num_init(&p->asciify, BC_NUM_DEF_SIZE);
2874 
2875 #if BC_ENABLE_EXTRA_MATH
2876 	// We need to initialize srand() just in case /dev/urandom and /dev/random
2877 	// are not available.
2878 	srand((unsigned int) time(NULL));
2879 	bc_rand_init(&p->rng);
2880 #endif // BC_ENABLE_EXTRA_MATH
2881 
2882 #if BC_ENABLED
2883 	if (BC_IS_BC) bc_num_init(&p->last, BC_NUM_DEF_SIZE);
2884 #endif // BC_ENABLED
2885 
2886 #ifndef NDEBUG
2887 	bc_vec_init(&p->fns, sizeof(BcFunc), BC_DTOR_FUNC);
2888 #else // NDEBUG
2889 	bc_vec_init(&p->fns, sizeof(BcFunc), BC_DTOR_NONE);
2890 #endif // NDEBUG
2891 	bc_map_init(&p->fn_map);
2892 	bc_program_insertFunc(p, bc_func_main);
2893 	bc_program_insertFunc(p, bc_func_read);
2894 
2895 	bc_vec_init(&p->vars, sizeof(BcVec), BC_DTOR_VEC);
2896 	bc_map_init(&p->var_map);
2897 
2898 	bc_vec_init(&p->arrs, sizeof(BcVec), BC_DTOR_VEC);
2899 	bc_map_init(&p->arr_map);
2900 
2901 	bc_vec_init(&p->results, sizeof(BcResult), BC_DTOR_RESULT);
2902 
2903 	// Push the first instruction pointer onto the execution stack.
2904 	bc_vec_init(&p->stack, sizeof(BcInstPtr), BC_DTOR_NONE);
2905 	bc_vec_push(&p->stack, &ip);
2906 
2907 	bc_vec_init(&p->consts, sizeof(BcConst), BC_DTOR_CONST);
2908 	bc_map_init(&p->const_map);
2909 	bc_vec_init(&p->strs, sizeof(char*), BC_DTOR_NONE);
2910 	bc_map_init(&p->str_map);
2911 }
2912 
2913 void
2914 bc_program_printStackTrace(BcProgram* p)
2915 {
2916 	size_t i, max_digits;
2917 
2918 	max_digits = bc_vm_numDigits(p->stack.len - 1);
2919 
2920 	for (i = 0; i < p->stack.len; ++i)
2921 	{
2922 		BcInstPtr* ip = bc_vec_item_rev(&p->stack, i);
2923 		BcFunc* f = bc_vec_item(&p->fns, ip->func);
2924 		size_t j, digits;
2925 
2926 		digits = bc_vm_numDigits(i);
2927 
2928 		bc_file_puts(&vm->ferr, bc_flush_none, "    ");
2929 
2930 		for (j = 0; j < max_digits - digits; ++j)
2931 		{
2932 			bc_file_putchar(&vm->ferr, bc_flush_none, ' ');
2933 		}
2934 
2935 		bc_file_printf(&vm->ferr, "%zu: %s", i, f->name);
2936 
2937 #if BC_ENABLED
2938 		if (BC_IS_BC && ip->func != BC_PROG_MAIN && ip->func != BC_PROG_READ)
2939 		{
2940 			bc_file_puts(&vm->ferr, bc_flush_none, "()");
2941 		}
2942 #endif // BC_ENABLED
2943 
2944 		bc_file_putchar(&vm->ferr, bc_flush_none, '\n');
2945 	}
2946 }
2947 
2948 void
2949 bc_program_reset(BcProgram* p)
2950 {
2951 	BcFunc* f;
2952 	BcInstPtr* ip;
2953 
2954 	BC_SIG_ASSERT_LOCKED;
2955 
2956 	// Pop all but the last execution and all results.
2957 	bc_vec_npop(&p->stack, p->stack.len - 1);
2958 	bc_vec_popAll(&p->results);
2959 
2960 #if DC_ENABLED
2961 	// We need to pop tail calls too.
2962 	if (BC_IS_DC) bc_vec_npop(&p->tail_calls, p->tail_calls.len - 1);
2963 #endif // DC_ENABLED
2964 
2965 #if BC_ENABLED
2966 	// Clear the globals' stacks.
2967 	if (BC_G) bc_program_popGlobals(p, true);
2968 #endif // BC_ENABLED
2969 
2970 	// Clear the bytecode vector of the main function.
2971 	f = bc_vec_item(&p->fns, BC_PROG_MAIN);
2972 	bc_vec_npop(&f->code, f->code.len);
2973 
2974 	// Reset the instruction pointer.
2975 	ip = bc_vec_top(&p->stack);
2976 	// NOLINTNEXTLINE
2977 	memset(ip, 0, sizeof(BcInstPtr));
2978 
2979 	if (BC_SIG_INTERRUPT(vm))
2980 	{
2981 		// Write the ready message for a signal.
2982 		bc_file_printf(&vm->fout, "%s", bc_program_ready_msg);
2983 		bc_file_flush(&vm->fout, bc_flush_err);
2984 	}
2985 
2986 	// Clear the signal.
2987 	vm->sig = 0;
2988 }
2989 
2990 void
2991 bc_program_exec(BcProgram* p)
2992 {
2993 	size_t idx;
2994 	BcResult r;
2995 	BcResult* ptr;
2996 	BcInstPtr* ip;
2997 	BcFunc* func;
2998 	char* code;
2999 	bool cond = false;
3000 	uchar inst;
3001 #if BC_ENABLED
3002 	BcNum* num;
3003 #endif // BC_ENABLED
3004 #if !BC_HAS_COMPUTED_GOTO
3005 #ifndef NDEBUG
3006 	size_t jmp_bufs_len;
3007 #endif // NDEBUG
3008 #endif // !BC_HAS_COMPUTED_GOTO
3009 
3010 #if BC_HAS_COMPUTED_GOTO
3011 
3012 #if BC_GCC
3013 #pragma GCC diagnostic ignored "-Wpedantic"
3014 #endif // BC_GCC
3015 
3016 #if BC_CLANG
3017 #pragma clang diagnostic ignored "-Wgnu-label-as-value"
3018 #endif // BC_CLANG
3019 
3020 	BC_PROG_LBLS;
3021 	BC_PROG_LBLS_ASSERT;
3022 
3023 #if BC_CLANG
3024 #pragma clang diagnostic warning "-Wgnu-label-as-value"
3025 #endif // BC_CLANG
3026 
3027 #if BC_GCC
3028 #pragma GCC diagnostic warning "-Wpedantic"
3029 #endif // BC_GCC
3030 
3031 	// BC_INST_INVALID is a marker for the end so that we don't have to have an
3032 	// execution loop.
3033 	func = (BcFunc*) bc_vec_item(&p->fns, BC_PROG_MAIN);
3034 	bc_vec_pushByte(&func->code, BC_INST_INVALID);
3035 #endif // BC_HAS_COMPUTED_GOTO
3036 
3037 	BC_SETJMP(vm, end);
3038 
3039 	ip = bc_vec_top(&p->stack);
3040 	func = (BcFunc*) bc_vec_item(&p->fns, ip->func);
3041 	code = func->code.v;
3042 
3043 #if !BC_HAS_COMPUTED_GOTO
3044 
3045 #ifndef NDEBUG
3046 	jmp_bufs_len = vm->jmp_bufs.len;
3047 #endif // NDEBUG
3048 
3049 	// This loop is the heart of the execution engine. It *is* the engine. For
3050 	// computed goto, it is ignored.
3051 	while (ip->idx < func->code.len)
3052 #endif // !BC_HAS_COMPUTED_GOTO
3053 	{
3054 		BC_SIG_ASSERT_NOT_LOCKED;
3055 
3056 #if BC_HAS_COMPUTED_GOTO
3057 
3058 #if BC_GCC
3059 #pragma GCC diagnostic ignored "-Wpedantic"
3060 #endif // BC_GCC
3061 
3062 #if BC_CLANG
3063 #pragma clang diagnostic ignored "-Wgnu-label-as-value"
3064 #endif // BC_CLANG
3065 
3066 		BC_PROG_JUMP(inst, code, ip);
3067 
3068 #else // BC_HAS_COMPUTED_GOTO
3069 
3070 		// Get the next instruction and increment the index.
3071 		inst = (uchar) code[(ip->idx)++];
3072 
3073 #endif // BC_HAS_COMPUTED_GOTO
3074 
3075 #if BC_DEBUG_CODE
3076 		bc_file_printf(&vm->ferr, "inst: %s\n", bc_inst_names[inst]);
3077 		bc_file_flush(&vm->ferr, bc_flush_none);
3078 #endif // BC_DEBUG_CODE
3079 
3080 #if !BC_HAS_COMPUTED_GOTO
3081 		switch (inst)
3082 #endif // !BC_HAS_COMPUTED_GOTO
3083 		{
3084 #if BC_ENABLED
3085 			// This just sets up the condition for the unconditional jump below,
3086 			// which checks the condition, if necessary.
3087 			// clang-format off
3088 			BC_PROG_LBL(BC_INST_JUMP_ZERO):
3089 			// clang-format on
3090 			{
3091 				bc_program_prep(p, &ptr, &num, 0);
3092 
3093 				cond = !bc_num_cmpZero(num);
3094 				bc_vec_pop(&p->results);
3095 
3096 				BC_PROG_DIRECT_JUMP(BC_INST_JUMP)
3097 			}
3098 			// Fallthrough.
3099 			BC_PROG_FALLTHROUGH
3100 
3101 			// clang-format off
3102 			BC_PROG_LBL(BC_INST_JUMP):
3103 			// clang-format on
3104 			{
3105 				idx = bc_program_index(code, &ip->idx);
3106 
3107 				// If a jump is required...
3108 				if (inst == BC_INST_JUMP || cond)
3109 				{
3110 					// Get the address to jump to.
3111 					size_t* addr = bc_vec_item(&func->labels, idx);
3112 
3113 					// If this fails, then the parser failed to set up the
3114 					// labels correctly.
3115 					assert(*addr != SIZE_MAX);
3116 
3117 					// Set the new address.
3118 					ip->idx = *addr;
3119 				}
3120 
3121 				BC_PROG_JUMP(inst, code, ip);
3122 			}
3123 
3124 			// clang-format off
3125 			BC_PROG_LBL(BC_INST_CALL):
3126 			// clang-format on
3127 			{
3128 				assert(BC_IS_BC);
3129 
3130 				bc_program_call(p, code, &ip->idx);
3131 
3132 				// Because we changed the execution stack and where we are
3133 				// executing, we have to update all of this.
3134 				BC_SIG_LOCK;
3135 				ip = bc_vec_top(&p->stack);
3136 				func = bc_vec_item(&p->fns, ip->func);
3137 				code = func->code.v;
3138 				BC_SIG_UNLOCK;
3139 
3140 				BC_PROG_JUMP(inst, code, ip);
3141 			}
3142 
3143 			// clang-format off
3144 			BC_PROG_LBL(BC_INST_INC):
3145 			BC_PROG_LBL(BC_INST_DEC):
3146 			// clang-format on
3147 			{
3148 				bc_program_incdec(p, inst);
3149 				BC_PROG_JUMP(inst, code, ip);
3150 			}
3151 
3152 			// clang-format off
3153 			BC_PROG_LBL(BC_INST_HALT):
3154 			// clang-format on
3155 			{
3156 				vm->status = BC_STATUS_QUIT;
3157 
3158 				// Just jump out. The jump series will take care of everything.
3159 				BC_JMP;
3160 
3161 				BC_PROG_JUMP(inst, code, ip);
3162 			}
3163 
3164 			// clang-format off
3165 			BC_PROG_LBL(BC_INST_RET):
3166 			BC_PROG_LBL(BC_INST_RET0):
3167 			BC_PROG_LBL(BC_INST_RET_VOID):
3168 			// clang-format on
3169 			{
3170 				bc_program_return(p, inst);
3171 
3172 				// Because we changed the execution stack and where we are
3173 				// executing, we have to update all of this.
3174 				BC_SIG_LOCK;
3175 				ip = bc_vec_top(&p->stack);
3176 				func = bc_vec_item(&p->fns, ip->func);
3177 				code = func->code.v;
3178 				BC_SIG_UNLOCK;
3179 
3180 				BC_PROG_JUMP(inst, code, ip);
3181 			}
3182 #endif // BC_ENABLED
3183 
3184 			// clang-format off
3185 			BC_PROG_LBL(BC_INST_BOOL_OR):
3186 			BC_PROG_LBL(BC_INST_BOOL_AND):
3187 			BC_PROG_LBL(BC_INST_REL_EQ):
3188 			BC_PROG_LBL(BC_INST_REL_LE):
3189 			BC_PROG_LBL(BC_INST_REL_GE):
3190 			BC_PROG_LBL(BC_INST_REL_NE):
3191 			BC_PROG_LBL(BC_INST_REL_LT):
3192 			BC_PROG_LBL(BC_INST_REL_GT):
3193 			// clang-format on
3194 			{
3195 				bc_program_logical(p, inst);
3196 				BC_PROG_JUMP(inst, code, ip);
3197 			}
3198 
3199 			// clang-format off
3200 			BC_PROG_LBL(BC_INST_READ):
3201 			// clang-format on
3202 			{
3203 				// We want to flush output before
3204 				// this in case there is a prompt.
3205 				bc_file_flush(&vm->fout, bc_flush_save);
3206 
3207 				bc_program_read(p);
3208 
3209 				// Because we changed the execution stack and where we are
3210 				// executing, we have to update all of this.
3211 				BC_SIG_LOCK;
3212 				ip = bc_vec_top(&p->stack);
3213 				func = bc_vec_item(&p->fns, ip->func);
3214 				code = func->code.v;
3215 				BC_SIG_UNLOCK;
3216 
3217 				BC_PROG_JUMP(inst, code, ip);
3218 			}
3219 
3220 #if BC_ENABLE_EXTRA_MATH
3221 			// clang-format off
3222 			BC_PROG_LBL(BC_INST_RAND):
3223 			// clang-format on
3224 			{
3225 				bc_program_rand(p);
3226 				BC_PROG_JUMP(inst, code, ip);
3227 			}
3228 #endif // BC_ENABLE_EXTRA_MATH
3229 
3230 			// clang-format off
3231 			BC_PROG_LBL(BC_INST_MAXIBASE):
3232 			BC_PROG_LBL(BC_INST_MAXOBASE):
3233 			BC_PROG_LBL(BC_INST_MAXSCALE):
3234 #if BC_ENABLE_EXTRA_MATH
3235 			BC_PROG_LBL(BC_INST_MAXRAND):
3236 #endif // BC_ENABLE_EXTRA_MATH
3237 			// clang-format on
3238 			{
3239 				BcBigDig dig = vm->maxes[inst - BC_INST_MAXIBASE];
3240 				bc_program_pushBigdig(p, dig, BC_RESULT_TEMP);
3241 				BC_PROG_JUMP(inst, code, ip);
3242 			}
3243 
3244 			// clang-format off
3245 			BC_PROG_LBL(BC_INST_LINE_LENGTH):
3246 #if BC_ENABLED
3247 			BC_PROG_LBL(BC_INST_GLOBAL_STACKS):
3248 #endif // BC_ENABLED
3249 			BC_PROG_LBL(BC_INST_LEADING_ZERO):
3250 			// clang-format on
3251 			{
3252 				bc_program_globalSetting(p, inst);
3253 				BC_PROG_JUMP(inst, code, ip);
3254 			}
3255 
3256 			// clang-format off
3257 			BC_PROG_LBL(BC_INST_VAR):
3258 			// clang-format on
3259 			{
3260 				bc_program_pushVar(p, code, &ip->idx, false, false);
3261 				BC_PROG_JUMP(inst, code, ip);
3262 			}
3263 
3264 			// clang-format off
3265 			BC_PROG_LBL(BC_INST_ARRAY_ELEM):
3266 			BC_PROG_LBL(BC_INST_ARRAY):
3267 			// clang-format on
3268 			{
3269 				bc_program_pushArray(p, code, &ip->idx, inst);
3270 				BC_PROG_JUMP(inst, code, ip);
3271 			}
3272 
3273 			// clang-format off
3274 			BC_PROG_LBL(BC_INST_IBASE):
3275 			BC_PROG_LBL(BC_INST_SCALE):
3276 			BC_PROG_LBL(BC_INST_OBASE):
3277 			// clang-format on
3278 			{
3279 				bc_program_pushGlobal(p, inst);
3280 				BC_PROG_JUMP(inst, code, ip);
3281 			}
3282 
3283 #if BC_ENABLE_EXTRA_MATH
3284 			// clang-format off
3285 			BC_PROG_LBL(BC_INST_SEED):
3286 			// clang-format on
3287 			{
3288 				bc_program_pushSeed(p);
3289 				BC_PROG_JUMP(inst, code, ip);
3290 			}
3291 #endif // BC_ENABLE_EXTRA_MATH
3292 
3293 			// clang-format off
3294 			BC_PROG_LBL(BC_INST_LENGTH):
3295 			BC_PROG_LBL(BC_INST_SCALE_FUNC):
3296 			BC_PROG_LBL(BC_INST_SQRT):
3297 			BC_PROG_LBL(BC_INST_ABS):
3298 			BC_PROG_LBL(BC_INST_IS_NUMBER):
3299 			BC_PROG_LBL(BC_INST_IS_STRING):
3300 #if BC_ENABLE_EXTRA_MATH
3301 			BC_PROG_LBL(BC_INST_IRAND):
3302 #endif // BC_ENABLE_EXTRA_MATH
3303 			// clang-format on
3304 			{
3305 				bc_program_builtin(p, inst);
3306 				BC_PROG_JUMP(inst, code, ip);
3307 			}
3308 
3309 			// clang-format off
3310 			BC_PROG_LBL(BC_INST_ASCIIFY):
3311 			// clang-format on
3312 			{
3313 				bc_program_asciify(p);
3314 
3315 				// Because we changed the execution stack and where we are
3316 				// executing, we have to update all of this.
3317 				BC_SIG_LOCK;
3318 				ip = bc_vec_top(&p->stack);
3319 				func = bc_vec_item(&p->fns, ip->func);
3320 				code = func->code.v;
3321 				BC_SIG_UNLOCK;
3322 
3323 				BC_PROG_JUMP(inst, code, ip);
3324 			}
3325 
3326 			// clang-format off
3327 			BC_PROG_LBL(BC_INST_NUM):
3328 			// clang-format on
3329 			{
3330 				bc_program_const(p, code, &ip->idx);
3331 				BC_PROG_JUMP(inst, code, ip);
3332 			}
3333 
3334 			// clang-format off
3335 			BC_PROG_LBL(BC_INST_ZERO):
3336 			BC_PROG_LBL(BC_INST_ONE):
3337 #if BC_ENABLED
3338 			BC_PROG_LBL(BC_INST_LAST):
3339 #endif // BC_ENABLED
3340 			// clang-format on
3341 			{
3342 				r.t = BC_RESULT_ZERO + (inst - BC_INST_ZERO);
3343 				bc_vec_push(&p->results, &r);
3344 				BC_PROG_JUMP(inst, code, ip);
3345 			}
3346 
3347 			// clang-format off
3348 			BC_PROG_LBL(BC_INST_PRINT):
3349 			BC_PROG_LBL(BC_INST_PRINT_POP):
3350 #if BC_ENABLED
3351 			BC_PROG_LBL(BC_INST_PRINT_STR):
3352 #endif // BC_ENABLED
3353 			// clang-format on
3354 			{
3355 				bc_program_print(p, inst, 0);
3356 
3357 				// We want to flush right away to save the output for history,
3358 				// if history must preserve it when taking input.
3359 				bc_file_flush(&vm->fout, bc_flush_save);
3360 
3361 				BC_PROG_JUMP(inst, code, ip);
3362 			}
3363 
3364 			// clang-format off
3365 			BC_PROG_LBL(BC_INST_STR):
3366 			// clang-format on
3367 			{
3368 				// Set up the result and push.
3369 				r.t = BC_RESULT_STR;
3370 				bc_num_clear(&r.d.n);
3371 				r.d.n.scale = bc_program_index(code, &ip->idx);
3372 				bc_vec_push(&p->results, &r);
3373 				BC_PROG_JUMP(inst, code, ip);
3374 			}
3375 
3376 			// clang-format off
3377 			BC_PROG_LBL(BC_INST_POWER):
3378 			BC_PROG_LBL(BC_INST_MULTIPLY):
3379 			BC_PROG_LBL(BC_INST_DIVIDE):
3380 			BC_PROG_LBL(BC_INST_MODULUS):
3381 			BC_PROG_LBL(BC_INST_PLUS):
3382 			BC_PROG_LBL(BC_INST_MINUS):
3383 #if BC_ENABLE_EXTRA_MATH
3384 			BC_PROG_LBL(BC_INST_PLACES):
3385 			BC_PROG_LBL(BC_INST_LSHIFT):
3386 			BC_PROG_LBL(BC_INST_RSHIFT):
3387 #endif // BC_ENABLE_EXTRA_MATH
3388 			// clang-format on
3389 			{
3390 				bc_program_op(p, inst);
3391 				BC_PROG_JUMP(inst, code, ip);
3392 			}
3393 
3394 			// clang-format off
3395 			BC_PROG_LBL(BC_INST_NEG):
3396 			BC_PROG_LBL(BC_INST_BOOL_NOT):
3397 #if BC_ENABLE_EXTRA_MATH
3398 			BC_PROG_LBL(BC_INST_TRUNC):
3399 #endif // BC_ENABLE_EXTRA_MATH
3400 			// clang-format on
3401 			{
3402 				bc_program_unary(p, inst);
3403 				BC_PROG_JUMP(inst, code, ip);
3404 			}
3405 
3406 			// clang-format off
3407 #if BC_ENABLED
3408 			BC_PROG_LBL(BC_INST_ASSIGN_POWER):
3409 			BC_PROG_LBL(BC_INST_ASSIGN_MULTIPLY):
3410 			BC_PROG_LBL(BC_INST_ASSIGN_DIVIDE):
3411 			BC_PROG_LBL(BC_INST_ASSIGN_MODULUS):
3412 			BC_PROG_LBL(BC_INST_ASSIGN_PLUS):
3413 			BC_PROG_LBL(BC_INST_ASSIGN_MINUS):
3414 #if BC_ENABLE_EXTRA_MATH
3415 			BC_PROG_LBL(BC_INST_ASSIGN_PLACES):
3416 			BC_PROG_LBL(BC_INST_ASSIGN_LSHIFT):
3417 			BC_PROG_LBL(BC_INST_ASSIGN_RSHIFT):
3418 #endif // BC_ENABLE_EXTRA_MATH
3419 			BC_PROG_LBL(BC_INST_ASSIGN):
3420 			BC_PROG_LBL(BC_INST_ASSIGN_POWER_NO_VAL):
3421 			BC_PROG_LBL(BC_INST_ASSIGN_MULTIPLY_NO_VAL):
3422 			BC_PROG_LBL(BC_INST_ASSIGN_DIVIDE_NO_VAL):
3423 			BC_PROG_LBL(BC_INST_ASSIGN_MODULUS_NO_VAL):
3424 			BC_PROG_LBL(BC_INST_ASSIGN_PLUS_NO_VAL):
3425 			BC_PROG_LBL(BC_INST_ASSIGN_MINUS_NO_VAL):
3426 #if BC_ENABLE_EXTRA_MATH
3427 			BC_PROG_LBL(BC_INST_ASSIGN_PLACES_NO_VAL):
3428 			BC_PROG_LBL(BC_INST_ASSIGN_LSHIFT_NO_VAL):
3429 			BC_PROG_LBL(BC_INST_ASSIGN_RSHIFT_NO_VAL):
3430 #endif // BC_ENABLE_EXTRA_MATH
3431 #endif // BC_ENABLED
3432 			BC_PROG_LBL(BC_INST_ASSIGN_NO_VAL):
3433 			// clang-format on
3434 			{
3435 				bc_program_assign(p, inst);
3436 				BC_PROG_JUMP(inst, code, ip);
3437 			}
3438 
3439 			// clang-format off
3440 			BC_PROG_LBL(BC_INST_POP):
3441 			// clang-format on
3442 			{
3443 #ifndef BC_PROG_NO_STACK_CHECK
3444 				// dc must do a stack check, but bc does not.
3445 				if (BC_IS_DC)
3446 				{
3447 					if (BC_ERR(!BC_PROG_STACK(&p->results, 1)))
3448 					{
3449 						bc_err(BC_ERR_EXEC_STACK);
3450 					}
3451 				}
3452 #endif // BC_PROG_NO_STACK_CHECK
3453 
3454 				assert(BC_PROG_STACK(&p->results, 1));
3455 
3456 				bc_vec_pop(&p->results);
3457 
3458 				BC_PROG_JUMP(inst, code, ip);
3459 			}
3460 
3461 			// clang-format off
3462 			BC_PROG_LBL(BC_INST_SWAP):
3463 			// clang-format on
3464 			{
3465 				BcResult* ptr2;
3466 
3467 				// Check the stack.
3468 				if (BC_ERR(!BC_PROG_STACK(&p->results, 2)))
3469 				{
3470 					bc_err(BC_ERR_EXEC_STACK);
3471 				}
3472 
3473 				assert(BC_PROG_STACK(&p->results, 2));
3474 
3475 				// Get the two items.
3476 				ptr = bc_vec_item_rev(&p->results, 0);
3477 				ptr2 = bc_vec_item_rev(&p->results, 1);
3478 
3479 				// Swap. It's just easiest to do it this way.
3480 				// NOLINTNEXTLINE
3481 				memcpy(&r, ptr, sizeof(BcResult));
3482 				// NOLINTNEXTLINE
3483 				memcpy(ptr, ptr2, sizeof(BcResult));
3484 				// NOLINTNEXTLINE
3485 				memcpy(ptr2, &r, sizeof(BcResult));
3486 
3487 				BC_PROG_JUMP(inst, code, ip);
3488 			}
3489 
3490 			// clang-format off
3491 			BC_PROG_LBL(BC_INST_MODEXP):
3492 			// clang-format on
3493 			{
3494 				bc_program_modexp(p);
3495 				BC_PROG_JUMP(inst, code, ip);
3496 			}
3497 
3498 			// clang-format off
3499 			BC_PROG_LBL(BC_INST_DIVMOD):
3500 			// clang-format on
3501 			{
3502 				bc_program_divmod(p);
3503 				BC_PROG_JUMP(inst, code, ip);
3504 			}
3505 
3506 			// clang-format off
3507 			BC_PROG_LBL(BC_INST_PRINT_STREAM):
3508 			// clang-format on
3509 			{
3510 				bc_program_printStream(p);
3511 				BC_PROG_JUMP(inst, code, ip);
3512 			}
3513 
3514 #if DC_ENABLED
3515 			// clang-format off
3516 			BC_PROG_LBL(BC_INST_POP_EXEC):
3517 			// clang-format on
3518 			{
3519 				// If this fails, the dc parser got something wrong.
3520 				assert(BC_PROG_STACK(&p->stack, 2));
3521 
3522 				// Pop the execution stack and tail call stack.
3523 				bc_vec_pop(&p->stack);
3524 				bc_vec_pop(&p->tail_calls);
3525 
3526 				// Because we changed the execution stack and where we are
3527 				// executing, we have to update all of this.
3528 				BC_SIG_LOCK;
3529 				ip = bc_vec_top(&p->stack);
3530 				func = bc_vec_item(&p->fns, ip->func);
3531 				code = func->code.v;
3532 				BC_SIG_UNLOCK;
3533 
3534 				BC_PROG_JUMP(inst, code, ip);
3535 			}
3536 
3537 			// clang-format off
3538 			BC_PROG_LBL(BC_INST_EXECUTE):
3539 			BC_PROG_LBL(BC_INST_EXEC_COND):
3540 			// clang-format on
3541 			{
3542 				cond = (inst == BC_INST_EXEC_COND);
3543 
3544 				bc_program_execStr(p, code, &ip->idx, cond, func->code.len);
3545 
3546 				// Because we changed the execution stack and where we are
3547 				// executing, we have to update all of this.
3548 				BC_SIG_LOCK;
3549 				ip = bc_vec_top(&p->stack);
3550 				func = bc_vec_item(&p->fns, ip->func);
3551 				code = func->code.v;
3552 				BC_SIG_UNLOCK;
3553 
3554 				BC_PROG_JUMP(inst, code, ip);
3555 			}
3556 
3557 			// clang-format off
3558 			BC_PROG_LBL(BC_INST_PRINT_STACK):
3559 			// clang-format on
3560 			{
3561 				bc_program_printStack(p);
3562 				BC_PROG_JUMP(inst, code, ip);
3563 			}
3564 
3565 			// clang-format off
3566 			BC_PROG_LBL(BC_INST_CLEAR_STACK):
3567 			// clang-format on
3568 			{
3569 				bc_vec_popAll(&p->results);
3570 				BC_PROG_JUMP(inst, code, ip);
3571 			}
3572 
3573 			// clang-format off
3574 			BC_PROG_LBL(BC_INST_REG_STACK_LEN):
3575 			// clang-format on
3576 			{
3577 				bc_program_regStackLen(p, code, &ip->idx);
3578 				BC_PROG_JUMP(inst, code, ip);
3579 			}
3580 
3581 			// clang-format off
3582 			BC_PROG_LBL(BC_INST_STACK_LEN):
3583 			// clang-format on
3584 			{
3585 				bc_program_stackLen(p);
3586 				BC_PROG_JUMP(inst, code, ip);
3587 			}
3588 
3589 			// clang-format off
3590 			BC_PROG_LBL(BC_INST_DUPLICATE):
3591 			// clang-format on
3592 			{
3593 				// Check the stack.
3594 				if (BC_ERR(!BC_PROG_STACK(&p->results, 1)))
3595 				{
3596 					bc_err(BC_ERR_EXEC_STACK);
3597 				}
3598 
3599 				assert(BC_PROG_STACK(&p->results, 1));
3600 
3601 				// Get the top of the stack.
3602 				ptr = bc_vec_top(&p->results);
3603 
3604 				BC_SIG_LOCK;
3605 
3606 				// Copy and push.
3607 				bc_result_copy(&r, ptr);
3608 				bc_vec_push(&p->results, &r);
3609 
3610 				BC_SIG_UNLOCK;
3611 
3612 				BC_PROG_JUMP(inst, code, ip);
3613 			}
3614 
3615 			// clang-format off
3616 			BC_PROG_LBL(BC_INST_LOAD):
3617 			BC_PROG_LBL(BC_INST_PUSH_VAR):
3618 			// clang-format on
3619 			{
3620 				bool copy = (inst == BC_INST_LOAD);
3621 				bc_program_pushVar(p, code, &ip->idx, true, copy);
3622 				BC_PROG_JUMP(inst, code, ip);
3623 			}
3624 
3625 			// clang-format off
3626 			BC_PROG_LBL(BC_INST_PUSH_TO_VAR):
3627 			// clang-format on
3628 			{
3629 				idx = bc_program_index(code, &ip->idx);
3630 				bc_program_copyToVar(p, idx, BC_TYPE_VAR);
3631 				BC_PROG_JUMP(inst, code, ip);
3632 			}
3633 
3634 			// clang-format off
3635 			BC_PROG_LBL(BC_INST_QUIT):
3636 			BC_PROG_LBL(BC_INST_NQUIT):
3637 			// clang-format on
3638 			{
3639 				bc_program_nquit(p, inst);
3640 
3641 				// Because we changed the execution stack and where we are
3642 				// executing, we have to update all of this.
3643 				BC_SIG_LOCK;
3644 				ip = bc_vec_top(&p->stack);
3645 				func = bc_vec_item(&p->fns, ip->func);
3646 				code = func->code.v;
3647 				BC_SIG_UNLOCK;
3648 
3649 				BC_PROG_JUMP(inst, code, ip);
3650 			}
3651 
3652 			// clang-format off
3653 			BC_PROG_LBL(BC_INST_EXEC_STACK_LEN):
3654 			// clang-format on
3655 			{
3656 				bc_program_execStackLen(p);
3657 				BC_PROG_JUMP(inst, code, ip);
3658 			}
3659 #endif // DC_ENABLED
3660 
3661 #if BC_HAS_COMPUTED_GOTO
3662 			// clang-format off
3663 			BC_PROG_LBL(BC_INST_INVALID):
3664 			// clang-format on
3665 			{
3666 				goto end;
3667 			}
3668 #else // BC_HAS_COMPUTED_GOTO
3669 			default:
3670 			{
3671 				BC_UNREACHABLE
3672 #if !defined(NDEBUG) && !BC_CLANG
3673 				abort();
3674 #endif // !defined(NDEBUG) && !BC_CLANG
3675 			}
3676 #endif // BC_HAS_COMPUTED_GOTO
3677 		}
3678 
3679 #if BC_HAS_COMPUTED_GOTO
3680 
3681 #if BC_CLANG
3682 #pragma clang diagnostic warning "-Wgnu-label-as-value"
3683 #endif // BC_CLANG
3684 
3685 #if BC_GCC
3686 #pragma GCC diagnostic warning "-Wpedantic"
3687 #endif // BC_GCC
3688 
3689 #else // BC_HAS_COMPUTED_GOTO
3690 
3691 #ifndef NDEBUG
3692 		// This is to allow me to use a debugger to see the last instruction,
3693 		// which will point to which function was the problem. But it's also a
3694 		// good smoke test for error handling changes.
3695 		assert(jmp_bufs_len == vm->jmp_bufs.len);
3696 #endif // NDEBUG
3697 
3698 #endif // BC_HAS_COMPUTED_GOTO
3699 	}
3700 
3701 end:
3702 	BC_SIG_MAYLOCK;
3703 
3704 	// This is here just to print a stack trace on interrupts. This is for
3705 	// finding infinite loops.
3706 	if (BC_SIG_INTERRUPT(vm))
3707 	{
3708 		BcStatus s;
3709 
3710 		bc_file_putchar(&vm->ferr, bc_flush_none, '\n');
3711 
3712 		bc_program_printStackTrace(p);
3713 
3714 		s = bc_file_flushErr(&vm->ferr, bc_flush_err);
3715 		if (BC_ERR(s != BC_STATUS_SUCCESS && vm->status == BC_STATUS_SUCCESS))
3716 		{
3717 			vm->status = (sig_atomic_t) s;
3718 		}
3719 	}
3720 
3721 	BC_LONGJMP_CONT(vm);
3722 }
3723 
3724 #if BC_DEBUG_CODE
3725 #if BC_ENABLED && DC_ENABLED
3726 void
3727 bc_program_printStackDebug(BcProgram* p)
3728 {
3729 	bc_file_puts(&vm->fout, bc_flush_err, "-------------- Stack ----------\n");
3730 	bc_program_printStack(p);
3731 	bc_file_puts(&vm->fout, bc_flush_err, "-------------- Stack End ------\n");
3732 }
3733 
3734 static void
3735 bc_program_printIndex(const char* restrict code, size_t* restrict bgn)
3736 {
3737 	uchar byte, i, bytes = (uchar) code[(*bgn)++];
3738 	ulong val = 0;
3739 
3740 	for (byte = 1, i = 0; byte && i < bytes; ++i)
3741 	{
3742 		byte = (uchar) code[(*bgn)++];
3743 		if (byte) val |= ((ulong) byte) << (CHAR_BIT * i);
3744 	}
3745 
3746 	bc_vm_printf(" (%lu) ", val);
3747 }
3748 
3749 static void
3750 bc_program_printStr(const BcProgram* p, const char* restrict code,
3751                     size_t* restrict bgn)
3752 {
3753 	size_t idx = bc_program_index(code, bgn);
3754 	char* s;
3755 
3756 	s = *((char**) bc_vec_item(p->strs, idx));
3757 
3758 	bc_vm_printf(" (\"%s\") ", s);
3759 }
3760 
3761 void
3762 bc_program_printInst(const BcProgram* p, const char* restrict code,
3763                      size_t* restrict bgn)
3764 {
3765 	uchar inst = (uchar) code[(*bgn)++];
3766 
3767 	bc_vm_printf("Inst[%zu]: %s [%lu]; ", *bgn - 1, bc_inst_names[inst],
3768 	             (unsigned long) inst);
3769 
3770 	if (inst == BC_INST_VAR || inst == BC_INST_ARRAY_ELEM ||
3771 	    inst == BC_INST_ARRAY)
3772 	{
3773 		bc_program_printIndex(code, bgn);
3774 	}
3775 	else if (inst == BC_INST_STR) bc_program_printStr(p, code, bgn);
3776 	else if (inst == BC_INST_NUM)
3777 	{
3778 		size_t idx = bc_program_index(code, bgn);
3779 		BcConst* c = bc_vec_item(p->consts, idx);
3780 		bc_vm_printf("(%s)", c->val);
3781 	}
3782 	else if (inst == BC_INST_CALL ||
3783 	         (inst > BC_INST_STR && inst <= BC_INST_JUMP_ZERO))
3784 	{
3785 		bc_program_printIndex(code, bgn);
3786 		if (inst == BC_INST_CALL) bc_program_printIndex(code, bgn);
3787 	}
3788 
3789 	bc_vm_putchar('\n', bc_flush_err);
3790 }
3791 
3792 void
3793 bc_program_code(const BcProgram* p)
3794 {
3795 	BcFunc* f;
3796 	char* code;
3797 	BcInstPtr ip;
3798 	size_t i;
3799 
3800 	for (i = 0; i < p->fns.len; ++i)
3801 	{
3802 		ip.idx = ip.len = 0;
3803 		ip.func = i;
3804 
3805 		f = bc_vec_item(&p->fns, ip.func);
3806 		code = f->code.v;
3807 
3808 		bc_vm_printf("func[%zu]:\n", ip.func);
3809 		while (ip.idx < f->code.len)
3810 		{
3811 			bc_program_printInst(p, code, &ip.idx);
3812 		}
3813 		bc_file_puts(&vm->fout, bc_flush_err, "\n\n");
3814 	}
3815 }
3816 #endif // BC_ENABLED && DC_ENABLED
3817 #endif // BC_DEBUG_CODE
3818