e23a641384b74781349f4dae712bb2bb21b1f910
[fw/altos] / src / lisp / ao_lisp_frame.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #include "ao_lisp.h"
16
17 static inline int
18 frame_num_size(int num)
19 {
20         return sizeof (struct ao_lisp_frame) + num * sizeof (struct ao_lisp_val);
21 }
22
23 static int
24 frame_size(void *addr)
25 {
26         struct ao_lisp_frame    *frame = addr;
27         return frame_num_size(frame->num);
28 }
29
30 static void
31 frame_mark(void *addr)
32 {
33         struct ao_lisp_frame    *frame = addr;
34         int                     f;
35
36         for (;;) {
37                 MDBG_MOVE("frame mark %d\n", MDBG_OFFSET(frame));
38                 if (!AO_LISP_IS_POOL(frame))
39                         break;
40                 for (f = 0; f < frame->num; f++) {
41                         struct ao_lisp_val      *v = &frame->vals[f];
42
43                         ao_lisp_poly_mark(v->val, 0);
44                         MDBG_MOVE("frame mark atom %s %d val %d at %d\n",
45                                   ao_lisp_poly_atom(v->atom)->name,
46                                   MDBG_OFFSET(ao_lisp_ref(v->atom)),
47                                   MDBG_OFFSET(ao_lisp_ref(v->val)), f);
48                 }
49                 frame = ao_lisp_poly_frame(frame->next);
50                 MDBG_MOVE("frame next %d\n", MDBG_OFFSET(frame));
51                 if (!frame)
52                         break;
53                 if (ao_lisp_mark_memory(&ao_lisp_frame_type, frame))
54                         break;
55         }
56 }
57
58 static void
59 frame_move(void *addr)
60 {
61         struct ao_lisp_frame    *frame = addr;
62         int                     f;
63
64         for (;;) {
65                 struct ao_lisp_frame    *next;
66                 int                     ret;
67
68                 MDBG_MOVE("frame move %d\n", MDBG_OFFSET(frame));
69                 if (!AO_LISP_IS_POOL(frame))
70                         break;
71                 for (f = 0; f < frame->num; f++) {
72                         struct ao_lisp_val      *v = &frame->vals[f];
73
74                         ao_lisp_poly_move(&v->atom, 0);
75                         ao_lisp_poly_move(&v->val, 0);
76                         MDBG_MOVE("frame move atom %s %d val %d at %d\n",
77                                   ao_lisp_poly_atom(v->atom)->name,
78                                   MDBG_OFFSET(ao_lisp_ref(v->atom)),
79                                   MDBG_OFFSET(ao_lisp_ref(v->val)), f);
80                 }
81                 next = ao_lisp_poly_frame(frame->next);
82                 if (!next)
83                         break;
84                 ret = ao_lisp_move_memory(&ao_lisp_frame_type, (void **) &next);
85                 if (next != ao_lisp_poly_frame(frame->next)) {
86                         MDBG_MOVE("frame next moved from %d to %d\n",
87                                   MDBG_OFFSET(ao_lisp_poly_frame(frame->next)),
88                                   MDBG_OFFSET(next));
89                         frame->next = ao_lisp_frame_poly(next);
90                 }
91                 if (ret)
92                         break;
93                 frame = next;
94         }
95 }
96
97 const struct ao_lisp_type ao_lisp_frame_type = {
98         .mark = frame_mark,
99         .size = frame_size,
100         .move = frame_move,
101         .name = "frame",
102 };
103
104 void
105 ao_lisp_frame_print(ao_poly p)
106 {
107         struct ao_lisp_frame    *frame = ao_lisp_poly_frame(p);
108         int                     f;
109
110         printf ("{");
111         if (frame) {
112                 for (f = 0; f < frame->num; f++) {
113                         if (f != 0)
114                                 printf(", ");
115                         ao_lisp_poly_print(frame->vals[f].atom);
116                         printf(" = ");
117                         ao_lisp_poly_print(frame->vals[f].val);
118                 }
119                 if (frame->next)
120                         ao_lisp_poly_print(frame->next);
121         }
122         printf("}");
123 }
124
125 ao_poly *
126 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom)
127 {
128         int f;
129         for (f = 0; f < frame->num; f++)
130                 if (frame->vals[f].atom == atom)
131                         return &frame->vals[f].val;
132         return NULL;
133 }
134
135 int
136 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
137 {
138         while (frame) {
139                 if (!AO_LISP_IS_CONST(frame)) {
140                         ao_poly *ref = ao_lisp_frame_ref(frame, atom);
141                         if (ref) {
142                                 *ref = val;
143                                 return 1;
144                         }
145                 }
146                 frame = ao_lisp_poly_frame(frame->next);
147         }
148         return 0;
149 }
150
151 ao_poly
152 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom)
153 {
154         while (frame) {
155                 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
156                 if (ref)
157                         return *ref;
158                 frame = ao_lisp_poly_frame(frame->next);
159         }
160         return AO_LISP_NIL;
161 }
162
163 struct ao_lisp_frame *
164 ao_lisp_frame_new(int num)
165 {
166         struct ao_lisp_frame *frame = ao_lisp_alloc(frame_num_size(num));
167
168         if (!frame)
169                 return NULL;
170         frame->type = AO_LISP_FRAME;
171         frame->num = num;
172         frame->next = AO_LISP_NIL;
173         memset(frame->vals, '\0', num * sizeof (struct ao_lisp_val));
174         return frame;
175 }
176
177 static struct ao_lisp_frame *
178 ao_lisp_frame_realloc(struct ao_lisp_frame **frame_ref, int new_num)
179 {
180         struct ao_lisp_frame    *frame = *frame_ref;
181         struct ao_lisp_frame    *new;
182         int                     copy;
183
184         if (new_num == frame->num)
185                 return frame;
186         new = ao_lisp_frame_new(new_num);
187         if (!new)
188                 return NULL;
189         /*
190          * Re-fetch the frame as it may have moved
191          * during the allocation
192          */
193         frame = *frame_ref;
194         copy = new_num;
195         if (copy > frame->num)
196                 copy = frame->num;
197         memcpy(new->vals, frame->vals, copy * sizeof (struct ao_lisp_val));
198         new->next = frame->next;
199         return new;
200 }
201
202 int
203 ao_lisp_frame_add(struct ao_lisp_frame **frame_ref, ao_poly atom, ao_poly val)
204 {
205         struct ao_lisp_frame *frame = *frame_ref;
206         ao_poly *ref = frame ? ao_lisp_frame_ref(frame, atom) : NULL;
207
208         if (!ref) {
209                 int f;
210                 ao_lisp_poly_stash(0, atom);
211                 ao_lisp_poly_stash(1, val);
212                 if (frame) {
213                         f = frame->num;
214                         frame = ao_lisp_frame_realloc(frame_ref, f + 1);
215                 } else {
216                         f = 0;
217                         frame = ao_lisp_frame_new(1);
218                 }
219                 atom = ao_lisp_poly_fetch(0);
220                 val = ao_lisp_poly_fetch(1);
221                 if (!frame)
222                         return 0;
223                 *frame_ref = frame;
224                 frame->vals[f].atom = atom;
225                 ref = &frame->vals[f].val;
226         }
227         *ref = val;
228         return 1;
229 }