aboutsummaryrefslogtreecommitdiffstats
path: root/src/lang.c
blob: d67deb7be5d4fc0d46f49fa7003320df8d502db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * *****************************************************************************
 *
 * Copyright (c) 2018-2020 Gavin D. Howard and contributors.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * * Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * *****************************************************************************
 *
 * Code to manipulate data structures in programs.
 *
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include <lang.h>
#include <vm.h>

int bc_id_cmp(const BcId *e1, const BcId *e2) {
	return strcmp(e1->name, e2->name);
}

#ifndef NDEBUG
void bc_id_free(void *id) {
	BC_SIG_ASSERT_LOCKED;
	assert(id != NULL);
	free(((BcId*) id)->name);
}
#endif // NDEBUG

void bc_string_free(void *string) {
	BC_SIG_ASSERT_LOCKED;
	assert(string != NULL && (*((char**) string)) != NULL);
	free(*((char**) string));
}

void bc_const_free(void *constant) {
	BcConst *c = constant;
	BC_SIG_ASSERT_LOCKED;
	assert(c->val != NULL);
	free(c->val);
	bc_num_free(&c->num);
}

#if BC_ENABLED
void bc_func_insert(BcFunc *f, BcProgram *p, char *name,
                    BcType type, size_t line)
{
	BcLoc a;
	size_t i, idx;

	assert(f != NULL);

	idx = bc_program_search(p, name, type == BC_TYPE_VAR);

	for (i = 0; i < f->autos.len; ++i) {
		BcLoc *id = bc_vec_item(&f->autos, i);
		if (BC_ERR(idx == id->loc && type == (BcType) id->idx)) {
			const char *array = type == BC_TYPE_ARRAY ? "[]" : "";
			bc_vm_error(BC_ERROR_PARSE_DUP_LOCAL, line, name, array);
		}
	}

	a.loc = idx;
	a.idx = type;

	bc_vec_push(&f->autos, &a);
}
#endif // BC_ENABLED

void bc_func_init(BcFunc *f, const char *name) {
	BC_SIG_ASSERT_LOCKED;
	assert(f != NULL && name != NULL);
	bc_vec_init(&f->code, sizeof(uchar), NULL);
	bc_vec_init(&f->strs, sizeof(char*), bc_string_free);
	bc_vec_init(&f->consts, sizeof(BcConst), bc_const_free);
#if BC_ENABLED
	if (BC_IS_BC) {
		bc_vec_init(&f->autos, sizeof(BcLoc), NULL);
		bc_vec_init(&f->labels, sizeof(size_t), NULL);
		f->nparams = 0;
		f->voidfn = false;
	}
#endif // BC_ENABLED
	f->name = name;
}

void bc_func_reset(BcFunc *f) {
	BC_SIG_ASSERT_LOCKED;
	assert(f != NULL);
	bc_vec_npop(&f->code, f->code.len);
	bc_vec_npop(&f->strs, f->strs.len);
	bc_vec_npop(&f->consts, f->consts.len);
#if BC_ENABLED
	if (BC_IS_BC) {
		bc_vec_npop(&f->autos, f->autos.len);
		bc_vec_npop(&f->labels, f->labels.len);
		f->nparams = 0;
		f->voidfn = false;
	}
#endif // BC_ENABLED
}

void bc_func_free(void *func) {
#if BC_ENABLE_FUNC_FREE
	BcFunc *f = (BcFunc*) func;
	BC_SIG_ASSERT_LOCKED;
	assert(f != NULL);
	bc_vec_free(&f->code);
	bc_vec_free(&f->strs);
	bc_vec_free(&f->consts);
#if BC_ENABLED
#ifndef NDEBUG
	if (BC_IS_BC) {
		bc_vec_free(&f->autos);
		bc_vec_free(&f->labels);
	}
#endif // NDEBUG
#endif // BC_ENABLED
#endif // BC_ENABLE_FUNC_FREE
}

void bc_array_init(BcVec *a, bool nums) {
	BC_SIG_ASSERT_LOCKED;
	if (nums) bc_vec_init(a, sizeof(BcNum), bc_num_free);
	else bc_vec_init(a, sizeof(BcVec), bc_vec_free);
	bc_array_expand(a, 1);
}

void bc_array_copy(BcVec *d, const BcVec *s) {

	size_t i;

	BC_SIG_ASSERT_LOCKED;

	assert(d != NULL && s != NULL);
	assert(d != s && d->size == s->size && d->dtor == s->dtor);

	bc_vec_npop(d, d->len);
	bc_vec_expand(d, s->cap);
	d->len = s->len;

	for (i = 0; i < s->len; ++i) {
		BcNum *dnum = bc_vec_item(d, i), *snum = bc_vec_item(s, i);
		bc_num_createCopy(dnum, snum);
	}
}

void bc_array_expand(BcVec *a, size_t len) {

	assert(a != NULL);

	BC_SIG_ASSERT_LOCKED;

	bc_vec_expand(a, len);

	if (a->size == sizeof(BcNum) && a->dtor == bc_num_free) {
		BcNum n;
		while (len > a->len) {
			bc_num_init(&n, BC_NUM_DEF_SIZE);
			bc_vec_push(a, &n);
		}
	}
	else {
		BcVec v;
		assert(a->size == sizeof(BcVec) && a->dtor == bc_vec_free);
		while (len > a->len) {
			bc_array_init(&v, true);
			bc_vec_push(a, &v);
		}
	}
}

#if DC_ENABLED
void bc_result_copy(BcResult *d, BcResult *src) {

	assert(d != NULL && src != NULL);

	d->t = src->t;

	switch (d->t) {

		case BC_RESULT_TEMP:
		case BC_RESULT_IBASE:
		case BC_RESULT_SCALE:
		case BC_RESULT_OBASE:
#if BC_ENABLE_EXTRA_MATH
		case BC_RESULT_SEED:
#endif // BC_ENABLE_EXTRA_MATH
		{
			bc_num_createCopy(&d->d.n, &src->d.n);
			break;
		}

		case BC_RESULT_VAR:
#if BC_ENABLED
		case BC_RESULT_ARRAY:
#endif // BC_ENABLED
		case BC_RESULT_ARRAY_ELEM:
		{
			memcpy(&d->d.loc, &src->d.loc, sizeof(BcLoc));
			break;
		}

		case BC_RESULT_CONSTANT:
		case BC_RESULT_STR:
		{
			memcpy(&d->d.n, &src->d.n, sizeof(BcNum));
			break;
		}

		case BC_RESULT_ONE:
		{
			// Do nothing.
			break;
		}

#if BC_ENABLED
		case BC_RESULT_VOID:
		case BC_RESULT_LAST:
		{
#ifndef NDEBUG
			abort();
#endif // NDEBUG
		}
#endif // BC_ENABLED
	}
}
#endif // DC_ENABLED

void bc_result_free(void *result) {

	BcResult *r = (BcResult*) result;

	BC_SIG_ASSERT_LOCKED;

	assert(r != NULL);

	switch (r->t) {

		case BC_RESULT_TEMP:
		case BC_RESULT_IBASE:
		case BC_RESULT_SCALE:
		case BC_RESULT_OBASE:
#if BC_ENABLE_EXTRA_MATH
		case BC_RESULT_SEED:
#endif // BC_ENABLE_EXTRA_MATH
		{
			bc_num_free(&r->d.n);
			break;
		}

		case BC_RESULT_VAR:
#if BC_ENABLED
		case BC_RESULT_ARRAY:
#endif // BC_ENABLED
		case BC_RESULT_ARRAY_ELEM:
		case BC_RESULT_STR:
		case BC_RESULT_CONSTANT:
		case BC_RESULT_ONE:
#if BC_ENABLED
		case BC_RESULT_VOID:
		case BC_RESULT_LAST:
#endif // BC_ENABLED
		{
			// Do nothing.
			break;
		}
	}
}