2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include "ao_scheme.h"
18 frame_vals_num_size(int num)
20 return sizeof (struct ao_scheme_frame_vals) + num * sizeof (struct ao_scheme_val);
24 frame_vals_size(void *addr)
26 struct ao_scheme_frame_vals *vals = addr;
27 return frame_vals_num_size(vals->size);
31 frame_vals_mark(void *addr)
33 struct ao_scheme_frame_vals *vals = addr;
36 for (f = 0; f < vals->size; f++) {
37 struct ao_scheme_val *v = &vals->vals[f];
39 ao_scheme_poly_mark(v->val, 0);
40 MDBG_MOVE("frame mark atom %s %d val %d at %d ",
41 ao_scheme_poly_atom(v->atom)->name,
42 MDBG_OFFSET(ao_scheme_ref(v->atom)),
43 MDBG_OFFSET(ao_scheme_ref(v->val)), f);
44 MDBG_DO(printf("\n"));
49 frame_vals_move(void *addr)
51 struct ao_scheme_frame_vals *vals = addr;
54 for (f = 0; f < vals->size; f++) {
55 struct ao_scheme_val *v = &vals->vals[f];
57 ao_scheme_poly_move(&v->atom, 0);
58 ao_scheme_poly_move(&v->val, 0);
59 MDBG_MOVE("frame move atom %s %d val %d at %d\n",
60 ao_scheme_poly_atom(v->atom)->name,
61 MDBG_OFFSET(ao_scheme_ref(v->atom)),
62 MDBG_OFFSET(ao_scheme_ref(v->val)), f);
66 const struct ao_scheme_type ao_scheme_frame_vals_type = {
67 .mark = frame_vals_mark,
68 .size = frame_vals_size,
69 .move = frame_vals_move,
74 frame_size(void *addr)
77 return sizeof (struct ao_scheme_frame);
81 frame_mark(void *addr)
83 struct ao_scheme_frame *frame = addr;
86 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
88 MDBG_MOVE("frame mark %d\n", MDBG_OFFSET(frame));
89 if (!ao_scheme_mark_memory(&ao_scheme_frame_vals_type, vals))
90 frame_vals_mark(vals);
91 frame = ao_scheme_poly_frame(frame->prev);
92 MDBG_MOVE("frame next %d\n", MDBG_OFFSET(frame));
95 if (ao_scheme_mark_memory(&ao_scheme_frame_type, frame))
101 frame_move(void *addr)
103 struct ao_scheme_frame *frame = addr;
106 struct ao_scheme_frame *prev;
107 struct ao_scheme_frame_vals *vals;
110 MDBG_MOVE("frame move %d\n", MDBG_OFFSET(frame));
111 vals = ao_scheme_poly_frame_vals(frame->vals);
112 if (!ao_scheme_move_memory(&ao_scheme_frame_vals_type, (void **) &vals))
113 frame_vals_move(vals);
114 if (vals != ao_scheme_poly_frame_vals(frame->vals))
115 frame->vals = ao_scheme_frame_vals_poly(vals);
117 prev = ao_scheme_poly_frame(frame->prev);
120 ret = ao_scheme_move_memory(&ao_scheme_frame_type, (void **) &prev);
121 if (prev != ao_scheme_poly_frame(frame->prev)) {
122 MDBG_MOVE("frame prev moved from %d to %d\n",
123 MDBG_OFFSET(ao_scheme_poly_frame(frame->prev)),
125 frame->prev = ao_scheme_frame_poly(prev);
133 const struct ao_scheme_type ao_scheme_frame_type = {
140 int ao_scheme_frame_print_indent;
143 ao_scheme_frame_indent(int extra)
147 for (i = 0; i < ao_scheme_frame_print_indent+extra; i++)
152 ao_scheme_frame_write(ao_poly p, bool write)
154 struct ao_scheme_frame *frame = ao_scheme_poly_frame(p);
155 struct ao_scheme_frame *clear = frame;
156 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
160 ao_scheme_print_start();
164 if (ao_scheme_print_mark_addr(frame)) {
165 printf("recurse...");
171 for (f = 0; f < frame->num; f++) {
172 ao_scheme_frame_indent(1);
173 ao_scheme_poly_write(vals->vals[f].atom, write);
175 ao_scheme_poly_write(vals->vals[f].val, write);
177 frame = ao_scheme_poly_frame(frame->prev);
178 ao_scheme_frame_indent(0);
181 if (ao_scheme_print_stop()) {
183 ao_scheme_print_clear_addr(clear);
184 clear = ao_scheme_poly_frame(clear->prev);
190 ao_scheme_frame_find(struct ao_scheme_frame *frame, int top, ao_poly atom)
192 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
197 int m = (l + r) >> 1;
198 if (vals->vals[m].atom < atom)
207 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom)
209 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
210 int l = ao_scheme_frame_find(frame, frame->num, atom);
215 if (vals->vals[l].atom != atom)
217 return &vals->vals[l].val;
220 struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
222 static struct ao_scheme_frame_vals *
223 ao_scheme_frame_vals_new(int num)
225 struct ao_scheme_frame_vals *vals;
227 vals = ao_scheme_alloc(frame_vals_num_size(num));
230 vals->type = AO_SCHEME_FRAME_VALS;
232 memset(vals->vals, '\0', num * sizeof (struct ao_scheme_val));
236 struct ao_scheme_frame *
237 ao_scheme_frame_new(int num)
239 struct ao_scheme_frame *frame;
240 struct ao_scheme_frame_vals *vals;
242 if (num < AO_SCHEME_FRAME_FREE && (frame = ao_scheme_frame_free_list[num])) {
243 ao_scheme_frame_free_list[num] = ao_scheme_poly_frame(frame->prev);
244 vals = ao_scheme_poly_frame_vals(frame->vals);
246 frame = ao_scheme_alloc(sizeof (struct ao_scheme_frame));
249 frame->type = AO_SCHEME_FRAME;
251 frame->prev = AO_SCHEME_NIL;
252 frame->vals = AO_SCHEME_NIL;
253 ao_scheme_frame_stash(frame);
254 vals = ao_scheme_frame_vals_new(num);
255 frame = ao_scheme_frame_fetch();
258 frame->vals = ao_scheme_frame_vals_poly(vals);
261 frame->prev = AO_SCHEME_NIL;
266 ao_scheme_frame_mark(struct ao_scheme_frame *frame)
269 return AO_SCHEME_NIL;
270 frame->type |= AO_SCHEME_FRAME_MARK;
271 return ao_scheme_frame_poly(frame);
275 ao_scheme_frame_free(struct ao_scheme_frame *frame)
277 if (frame && !ao_scheme_frame_marked(frame)) {
278 int num = frame->num;
279 if (num < AO_SCHEME_FRAME_FREE) {
280 struct ao_scheme_frame_vals *vals;
282 vals = ao_scheme_poly_frame_vals(frame->vals);
283 memset(vals->vals, '\0', vals->size * sizeof (struct ao_scheme_val));
284 frame->prev = ao_scheme_frame_poly(ao_scheme_frame_free_list[num]);
285 ao_scheme_frame_free_list[num] = frame;
290 static struct ao_scheme_frame *
291 ao_scheme_frame_realloc(struct ao_scheme_frame *frame, int new_num)
293 struct ao_scheme_frame_vals *vals;
294 struct ao_scheme_frame_vals *new_vals;
297 if (new_num == frame->num)
299 ao_scheme_frame_stash(frame);
300 new_vals = ao_scheme_frame_vals_new(new_num);
301 frame = ao_scheme_frame_fetch();
304 vals = ao_scheme_poly_frame_vals(frame->vals);
306 if (copy > frame->num)
308 memcpy(new_vals->vals, vals->vals, copy * sizeof (struct ao_scheme_val));
309 frame->vals = ao_scheme_frame_vals_poly(new_vals);
310 frame->num = new_num;
315 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val)
317 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
318 int l = ao_scheme_frame_find(frame, num, atom);
320 memmove(&vals->vals[l+1],
322 (num - l) * sizeof (struct ao_scheme_val));
323 vals->vals[l].atom = atom;
324 vals->vals[l].val = val;
328 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val)
330 ao_poly *ref = frame ? ao_scheme_frame_ref(frame, atom) : NULL;
334 ao_scheme_poly_stash(atom);
335 ao_scheme_poly_stash(val);
336 frame = ao_scheme_frame_realloc(frame, f + 1);
337 val = ao_scheme_poly_fetch();
338 atom = ao_scheme_poly_fetch();
340 return AO_SCHEME_NIL;
341 ao_scheme_frame_bind(frame, frame->num - 1, atom, val);
347 struct ao_scheme_frame *ao_scheme_frame_global;
348 struct ao_scheme_frame *ao_scheme_frame_current;
351 ao_scheme_frame_init(void)
353 if (!ao_scheme_frame_global)
354 ao_scheme_frame_global = ao_scheme_frame_new(0);