2 * *****************************************************************************
4 * SPDX-License-Identifier: BSD-2-Clause
6 * Copyright (c) 2018-2021 Gavin D. Howard and contributors.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice, this
12 * list of conditions and the following disclaimer.
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.
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.
30 * *****************************************************************************
32 * Code to manipulate data structures in programs.
44 void bc_const_free(void *constant) {
46 BcConst *c = constant;
50 assert(c->val != NULL);
56 void bc_func_insert(BcFunc *f, BcProgram *p, char *name,
57 BcType type, size_t line)
62 // The function must *always* be valid.
65 // Get the index of the variable.
66 idx = bc_program_search(p, name, type == BC_TYPE_VAR);
68 // Search through all of the other autos/parameters.
69 for (i = 0; i < f->autos.len; ++i) {
72 BcAuto *aptr = bc_vec_item(&f->autos, i);
74 // If they match, barf.
75 if (BC_ERR(idx == aptr->idx && type == aptr->type)) {
77 const char *array = type == BC_TYPE_ARRAY ? "[]" : "";
79 bc_error(BC_ERR_PARSE_DUP_LOCAL, line, name, array);
88 bc_vec_push(&f->autos, &a);
92 void bc_func_init(BcFunc *f, const char *name) {
96 assert(f != NULL && name != NULL);
98 bc_vec_init(&f->code, sizeof(uchar), BC_DTOR_NONE);
100 bc_vec_init(&f->consts, sizeof(BcConst), BC_DTOR_CONST);
102 bc_vec_init(&f->strs, sizeof(char*), BC_DTOR_NONE);
106 // Only bc needs these things.
109 bc_vec_init(&f->autos, sizeof(BcAuto), BC_DTOR_NONE);
110 bc_vec_init(&f->labels, sizeof(size_t), BC_DTOR_NONE);
121 void bc_func_reset(BcFunc *f) {
123 BC_SIG_ASSERT_LOCKED;
126 bc_vec_popAll(&f->code);
128 bc_vec_popAll(&f->consts);
130 bc_vec_popAll(&f->strs);
135 bc_vec_popAll(&f->autos);
136 bc_vec_popAll(&f->labels);
145 void bc_func_free(void *func) {
147 BcFunc *f = (BcFunc*) func;
149 BC_SIG_ASSERT_LOCKED;
152 bc_vec_free(&f->code);
154 bc_vec_free(&f->consts);
156 bc_vec_free(&f->strs);
161 bc_vec_free(&f->autos);
162 bc_vec_free(&f->labels);
168 void bc_array_init(BcVec *a, bool nums) {
170 BC_SIG_ASSERT_LOCKED;
172 // Set the proper vector.
173 if (nums) bc_vec_init(a, sizeof(BcNum), BC_DTOR_NUM);
174 else bc_vec_init(a, sizeof(BcVec), BC_DTOR_VEC);
176 // We always want at least one item in the array.
177 bc_array_expand(a, 1);
180 void bc_array_copy(BcVec *d, const BcVec *s) {
184 BC_SIG_ASSERT_LOCKED;
186 assert(d != NULL && s != NULL);
187 assert(d != s && d->size == s->size && d->dtor == s->dtor);
189 // Make sure to destroy everything currently in d. This will put a lot of
190 // temps on the reuse list, so allocating later is not going to be as
191 // expensive as it seems. Also, it makes it easier to copy numbers that are
196 bc_vec_expand(d, s->cap);
199 for (i = 0; i < s->len; ++i) {
203 dnum = bc_vec_item(d, i);
204 snum = bc_vec_item(s, i);
206 // We have to create a copy of the number as well.
207 if (BC_PROG_STR(snum)) memcpy(dnum, snum, sizeof(BcNum));
208 else bc_num_createCopy(dnum, snum);
212 void bc_array_expand(BcVec *a, size_t len) {
216 BC_SIG_ASSERT_LOCKED;
218 bc_vec_expand(a, len);
220 // If this is true, then we have a num array.
221 if (a->size == sizeof(BcNum) && a->dtor == BC_DTOR_NUM) {
223 // Initialize numbers until we reach the target.
224 while (len > a->len) {
225 BcNum *n = bc_vec_pushEmpty(a);
226 bc_num_init(n, BC_NUM_DEF_SIZE);
231 assert(a->size == sizeof(BcVec) && a->dtor == BC_DTOR_VEC);
233 // Recursively initialize arrays until we reach the target. Having the
234 // second argument of bc_array_init() be true will activate the base
235 // case, so we're safe.
236 while (len > a->len) {
237 BcVec *v = bc_vec_pushEmpty(a);
238 bc_array_init(v, true);
243 void bc_result_clear(BcResult *r) {
244 r->t = BC_RESULT_TEMP;
245 bc_num_clear(&r->d.n);
249 void bc_result_copy(BcResult *d, BcResult *src) {
251 assert(d != NULL && src != NULL);
253 BC_SIG_ASSERT_LOCKED;
255 // d is assumed to not be valid yet.
258 // Yes, it depends on what type.
262 case BC_RESULT_IBASE:
263 case BC_RESULT_SCALE:
264 case BC_RESULT_OBASE:
265 #if BC_ENABLE_EXTRA_MATH
267 #endif // BC_ENABLE_EXTRA_MATH
269 bc_num_createCopy(&d->d.n, &src->d.n);
274 case BC_RESULT_ARRAY:
275 case BC_RESULT_ARRAY_ELEM:
277 memcpy(&d->d.loc, &src->d.loc, sizeof(BcLoc));
283 memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
299 // We should *never* try copying either of these.
308 void bc_result_free(void *result) {
310 BcResult *r = (BcResult*) result;
312 BC_SIG_ASSERT_LOCKED;
319 case BC_RESULT_IBASE:
320 case BC_RESULT_SCALE:
321 case BC_RESULT_OBASE:
322 #if BC_ENABLE_EXTRA_MATH
324 #endif // BC_ENABLE_EXTRA_MATH
326 bc_num_free(&r->d.n);
331 case BC_RESULT_ARRAY:
332 case BC_RESULT_ARRAY_ELEM: