8791c4de8804780a03a5c1e744480e4b198e1244
[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 #if 0
18 #define DBG(...)        printf(__VA_ARGS__)
19 #else
20 #define DBG(...)
21 #endif
22
23 static inline int
24 frame_num_size(int num)
25 {
26         return sizeof (struct ao_lisp_frame) + num * sizeof (struct ao_lisp_val);
27 }
28
29 static int
30 frame_size(void *addr)
31 {
32         struct ao_lisp_frame    *frame = addr;
33         return frame_num_size(frame->num);
34 }
35
36 #define OFFSET(a)       ((int) ((uint8_t *) (ao_lisp_ref(a)) - ao_lisp_const))
37
38 static void
39 frame_mark(void *addr)
40 {
41         struct ao_lisp_frame    *frame = addr;
42         int                     f;
43
44         for (;;) {
45                 DBG("frame mark %p\n", frame);
46                 if (!AO_LISP_IS_POOL(frame))
47                         break;
48                 for (f = 0; f < frame->num; f++) {
49                         struct ao_lisp_val      *v = &frame->vals[f];
50
51                         ao_lisp_poly_mark(v->val, 0);
52                         DBG ("\tframe mark atom %s %d val %d at %d\n",
53                              ao_lisp_poly_atom(v->atom)->name,
54                              OFFSET(v->atom), OFFSET(v->val), f);
55                 }
56                 frame = ao_lisp_poly_frame(frame->next);
57                 DBG("frame next %p\n", frame);
58                 if (!frame)
59                         break;
60                 if (ao_lisp_mark_memory(frame, frame_size(frame)))
61                         break;
62         }
63 }
64
65 static void
66 frame_move(void *addr)
67 {
68         struct ao_lisp_frame    *frame = addr;
69         int                     f;
70
71         for (;;) {
72                 struct ao_lisp_frame    *next;
73                 int                     ret;
74
75                 DBG("frame move %p\n", frame);
76                 if (!AO_LISP_IS_POOL(frame))
77                         break;
78                 for (f = 0; f < frame->num; f++) {
79                         struct ao_lisp_val      *v = &frame->vals[f];
80
81                         ao_lisp_poly_move(&v->atom, 0);
82                         DBG("moved atom %s\n", ao_lisp_poly_atom(v->atom)->name);
83                         ao_lisp_poly_move(&v->val, 0);
84                 }
85                 next = ao_lisp_poly_frame(frame->next);
86                 ret = 1;
87                 if (next)
88                         ret = ao_lisp_move_memory((void **) &next, frame_size(next));
89                 if (next != ao_lisp_poly_frame(frame->next))
90                         frame->next = ao_lisp_frame_poly(next);
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 };
102
103 ao_poly *
104 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom)
105 {
106         int f;
107         for (f = 0; f < frame->num; f++)
108                 if (frame->vals[f].atom == atom)
109                         return &frame->vals[f].val;
110         return NULL;
111 }
112
113 int
114 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
115 {
116         while (frame) {
117                 if (!AO_LISP_IS_CONST(frame)) {
118                         ao_poly *ref = ao_lisp_frame_ref(frame, atom);
119                         if (ref) {
120                                 *ref = val;
121                                 return 1;
122                         }
123                 }
124                 frame = ao_lisp_poly_frame(frame->next);
125         }
126         return 0;
127 }
128
129 ao_poly
130 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom)
131 {
132         while (frame) {
133                 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
134                 if (ref)
135                         return *ref;
136                 frame = ao_lisp_poly_frame(frame->next);
137         }
138         return AO_LISP_NIL;
139 }
140
141 struct ao_lisp_frame *
142 ao_lisp_frame_new(int num)
143 {
144         struct ao_lisp_frame *frame = ao_lisp_alloc(frame_num_size(num));
145
146         if (!frame)
147                 return NULL;
148         frame->type = AO_LISP_FRAME;
149         frame->num = num;
150         frame->next = AO_LISP_NIL;
151         memset(frame->vals, '\0', num * sizeof (struct ao_lisp_val));
152         return frame;
153 }
154
155 static struct ao_lisp_frame *
156 ao_lisp_frame_realloc(struct ao_lisp_frame *frame, int new_num)
157 {
158         struct ao_lisp_frame    *new;
159         int                     copy;
160
161         if (new_num == frame->num)
162                 return frame;
163         new = ao_lisp_frame_new(new_num);
164         if (!new)
165                 return NULL;
166         copy = new_num;
167         if (copy > frame->num)
168                 copy = frame->num;
169         memcpy(new->vals, frame->vals, copy * sizeof (struct ao_lisp_val));
170         if (frame)
171                 new->next = frame->next;
172         return new;
173 }
174
175 struct ao_lisp_frame *
176 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
177 {
178         ao_poly *ref = frame ? ao_lisp_frame_ref(frame, atom) : NULL;
179         if (!ref) {
180                 int f;
181                 if (frame) {
182                         f = frame->num;
183                         frame = ao_lisp_frame_realloc(frame, f + 1);
184                 } else {
185                         f = 0;
186                         frame = ao_lisp_frame_new(1);
187                 }
188                 if (!frame)
189                         return NULL;
190                 DBG ("add atom %s %d, val %d at %d\n", ao_lisp_poly_atom(atom)->name, OFFSET(atom), OFFSET(val), f);
191                 frame->vals[f].atom = atom;
192                 ref = &frame->vals[f].val;
193         }
194         *ref = val;
195         return frame;
196 }