source: mainline/uspace/app/sbi/src/rdata.c@ 39e8406

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 39e8406 was 39e8406, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Update SBI to rev. 128.

  • Property mode set to 100644
File size: 9.7 KB
Line 
1/*
2 * Copyright (c) 2010 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @file Run-time data representation. */
30
31#include <stdlib.h>
32#include <assert.h>
33#include "mytypes.h"
34#include "stree.h"
35
36#include "rdata.h"
37
38static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest);
39static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest);
40static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest);
41static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest);
42static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest);
43static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest);
44
45static int rdata_array_get_dim(rdata_array_t *array);
46
47static void rdata_address_print(rdata_address_t *address);
48static void rdata_value_print(rdata_value_t *value);
49static void rdata_var_print(rdata_var_t *var);
50
51
52rdata_item_t *rdata_item_new(item_class_t ic)
53{
54 rdata_item_t *item;
55
56 item = calloc(1, sizeof(rdata_item_t));
57 if (item == NULL) {
58 printf("Memory allocation failed.\n");
59 exit(1);
60 }
61
62 item->ic = ic;
63 return item;
64}
65
66rdata_addr_var_t *rdata_addr_var_new(void)
67{
68 rdata_addr_var_t *addr_var;
69
70 addr_var = calloc(1, sizeof(rdata_addr_var_t));
71 if (addr_var == NULL) {
72 printf("Memory allocation failed.\n");
73 exit(1);
74 }
75
76 return addr_var;
77}
78
79rdata_aprop_named_t *rdata_aprop_named_new(void)
80{
81 rdata_aprop_named_t *aprop_named;
82
83 aprop_named = calloc(1, sizeof(rdata_aprop_named_t));
84 if (aprop_named == NULL) {
85 printf("Memory allocation failed.\n");
86 exit(1);
87 }
88
89 return aprop_named;
90}
91
92rdata_aprop_indexed_t *rdata_aprop_indexed_new(void)
93{
94 rdata_aprop_indexed_t *aprop_indexed;
95
96 aprop_indexed = calloc(1, sizeof(rdata_aprop_indexed_t));
97 if (aprop_indexed == NULL) {
98 printf("Memory allocation failed.\n");
99 exit(1);
100 }
101
102 return aprop_indexed;
103}
104
105rdata_addr_prop_t *rdata_addr_prop_new(aprop_class_t apc)
106{
107 rdata_addr_prop_t *addr_prop;
108
109 addr_prop = calloc(1, sizeof(rdata_addr_prop_t));
110 if (addr_prop == NULL) {
111 printf("Memory allocation failed.\n");
112 exit(1);
113 }
114
115 addr_prop->apc = apc;
116 return addr_prop;
117}
118
119rdata_address_t *rdata_address_new(address_class_t ac)
120{
121 rdata_address_t *address;
122
123 address = calloc(1, sizeof(rdata_address_t));
124 if (address == NULL) {
125 printf("Memory allocation failed.\n");
126 exit(1);
127 }
128
129 address->ac = ac;
130 return address;
131}
132
133rdata_value_t *rdata_value_new(void)
134{
135 rdata_value_t *value;
136
137 value = calloc(1, sizeof(rdata_value_t));
138 if (value == NULL) {
139 printf("Memory allocation failed.\n");
140 exit(1);
141 }
142
143 return value;
144}
145
146rdata_var_t *rdata_var_new(var_class_t vc)
147{
148 rdata_var_t *var;
149
150 var = calloc(1, sizeof(rdata_var_t));
151 if (var == NULL) {
152 printf("Memory allocation failed.\n");
153 exit(1);
154 }
155
156 var->vc = vc;
157 return var;
158}
159
160rdata_ref_t *rdata_ref_new(void)
161{
162 rdata_ref_t *ref;
163
164 ref = calloc(1, sizeof(rdata_ref_t));
165 if (ref == NULL) {
166 printf("Memory allocation failed.\n");
167 exit(1);
168 }
169
170 return ref;
171}
172
173rdata_deleg_t *rdata_deleg_new(void)
174{
175 rdata_deleg_t *deleg;
176
177 deleg = calloc(1, sizeof(rdata_deleg_t));
178 if (deleg == NULL) {
179 printf("Memory allocation failed.\n");
180 exit(1);
181 }
182
183 return deleg;
184}
185
186rdata_array_t *rdata_array_new(int rank)
187{
188 rdata_array_t *array;
189
190 array = calloc(1, sizeof(rdata_array_t));
191 if (array == NULL) {
192 printf("Memory allocation failed.\n");
193 exit(1);
194 }
195
196 array->rank = rank;
197 array->extent = calloc(rank, sizeof(int));
198 if (array == NULL) {
199 printf("Memory allocation failed.\n");
200 exit(1);
201 }
202
203 return array;
204}
205
206rdata_object_t *rdata_object_new(void)
207{
208 rdata_object_t *object;
209
210 object = calloc(1, sizeof(rdata_object_t));
211 if (object == NULL) {
212 printf("Memory allocation failed.\n");
213 exit(1);
214 }
215
216 return object;
217}
218
219rdata_int_t *rdata_int_new(void)
220{
221 rdata_int_t *int_v;
222
223 int_v = calloc(1, sizeof(rdata_int_t));
224 if (int_v == NULL) {
225 printf("Memory allocation failed.\n");
226 exit(1);
227 }
228
229 return int_v;
230}
231
232rdata_string_t *rdata_string_new(void)
233{
234 rdata_string_t *string_v;
235
236 string_v = calloc(1, sizeof(rdata_string_t));
237 if (string_v == NULL) {
238 printf("Memory allocation failed.\n");
239 exit(1);
240 }
241
242 return string_v;
243}
244
245void rdata_array_alloc_element(rdata_array_t *array)
246{
247 int dim, idx;
248
249 dim = rdata_array_get_dim(array);
250
251 array->element = calloc(dim, sizeof(rdata_var_t *));
252 if (array->element == NULL) {
253 printf("Memory allocation failed.\n");
254 exit(1);
255 }
256
257 for (idx = 0; idx < dim; ++idx) {
258 array->element[idx] = calloc(1, sizeof(rdata_var_t));
259 if (array->element[idx] == NULL) {
260 printf("Memory allocation failed.\n");
261 exit(1);
262 }
263 }
264}
265
266/** Get array dimension.
267 *
268 * Dimension is the total number of elements in an array, in other words,
269 * the product of all extents.
270 */
271static int rdata_array_get_dim(rdata_array_t *array)
272{
273 int didx, dim;
274
275 dim = 1;
276 for (didx = 0; didx < array->rank; ++didx)
277 dim = dim * array->extent[didx];
278
279 return dim;
280}
281
282/** Make copy of a variable. */
283void rdata_var_copy(rdata_var_t *src, rdata_var_t **dest)
284{
285 rdata_var_t *nvar;
286
287 nvar = rdata_var_new(src->vc);
288
289 switch (src->vc) {
290 case vc_int:
291 rdata_int_copy(src->u.int_v, &nvar->u.int_v);
292 break;
293 case vc_string:
294 rdata_string_copy(src->u.string_v, &nvar->u.string_v);
295 break;
296 case vc_ref:
297 rdata_ref_copy(src->u.ref_v, &nvar->u.ref_v);
298 break;
299 case vc_deleg:
300 rdata_deleg_copy(src->u.deleg_v, &nvar->u.deleg_v);
301 break;
302 case vc_array:
303 rdata_array_copy(src->u.array_v, &nvar->u.array_v);
304 break;
305 case vc_object:
306 rdata_object_copy(src->u.object_v, &nvar->u.object_v);
307 break;
308 }
309
310 *dest = nvar;
311}
312
313static void rdata_int_copy(rdata_int_t *src, rdata_int_t **dest)
314{
315 *dest = rdata_int_new();
316 (*dest)->value = src->value;
317}
318
319static void rdata_string_copy(rdata_string_t *src, rdata_string_t **dest)
320{
321 *dest = rdata_string_new();
322 (*dest)->value = src->value;
323}
324
325static void rdata_ref_copy(rdata_ref_t *src, rdata_ref_t **dest)
326{
327 *dest = rdata_ref_new();
328 (*dest)->vref = src->vref;
329}
330
331static void rdata_deleg_copy(rdata_deleg_t *src, rdata_deleg_t **dest)
332{
333 (void) src; (void) dest;
334 printf("Unimplemented: Copy delegate.\n");
335 exit(1);
336}
337
338static void rdata_array_copy(rdata_array_t *src, rdata_array_t **dest)
339{
340 (void) src; (void) dest;
341 printf("Unimplemented: Copy array.\n");
342 exit(1);
343}
344
345static void rdata_object_copy(rdata_object_t *src, rdata_object_t **dest)
346{
347 (void) src; (void) dest;
348 printf("Unimplemented: Copy object.\n");
349 exit(1);
350}
351
352/** Read data from a variable.
353 *
354 * Return value stored in variable @a var.
355 */
356void rdata_var_read(rdata_var_t *var, rdata_item_t **ritem)
357{
358 rdata_value_t *value;
359 rdata_var_t *rvar;
360
361 /* Perform a shallow copy of @a var. */
362 rdata_var_copy(var, &rvar);
363
364 value = rdata_value_new();
365 value->var = rvar;
366 *ritem = rdata_item_new(ic_value);
367 (*ritem)->u.value = value;
368}
369
370/** Write data to a variable.
371 *
372 * Store @a value to variable @a var.
373 */
374void rdata_var_write(rdata_var_t *var, rdata_value_t *value)
375{
376 rdata_var_t *nvar;
377
378 /* Perform a shallow copy of @c value->var. */
379 rdata_var_copy(value->var, &nvar);
380
381 /* XXX do this in a prettier way. */
382
383 var->vc = nvar->vc;
384 switch (nvar->vc) {
385 case vc_int: var->u.int_v = nvar->u.int_v; break;
386 case vc_string: var->u.string_v = nvar->u.string_v; break;
387 case vc_ref: var->u.ref_v = nvar->u.ref_v; break;
388 case vc_deleg: var->u.deleg_v = nvar->u.deleg_v; break;
389 case vc_array: var->u.array_v = nvar->u.array_v; break;
390 case vc_object: var->u.object_v = nvar->u.object_v; break;
391 }
392
393 /* XXX We should free some stuff around here. */
394}
395
396void rdata_item_print(rdata_item_t *item)
397{
398 if (item == NULL) {
399 printf("none");
400 return;
401 }
402
403 switch (item->ic) {
404 case ic_address:
405 printf("address:");
406 rdata_address_print(item->u.address);
407 break;
408 case ic_value:
409 printf("value:");
410 rdata_value_print(item->u.value);
411 break;
412 }
413}
414
415static void rdata_address_print(rdata_address_t *address)
416{
417 switch (address->ac) {
418 case ac_var:
419 rdata_var_print(address->u.var_a->vref);
420 break;
421 case ac_prop:
422 printf("Warning: Unimplemented: Print property address.\n");
423 break;
424 }
425}
426
427static void rdata_value_print(rdata_value_t *value)
428{
429 rdata_var_print(value->var);
430}
431
432static void rdata_var_print(rdata_var_t *var)
433{
434 switch (var->vc) {
435 case vc_int:
436 printf("int(%d)", var->u.int_v->value);
437 break;
438 case vc_string:
439 printf("string(\"%s\")", var->u.string_v->value);
440 break;
441 case vc_ref:
442 printf("ref");
443 break;
444 case vc_deleg:
445 printf("deleg");
446 break;
447 case vc_object:
448 printf("object");
449 break;
450 default:
451 printf("print(%d)\n", var->vc);
452 assert(b_false);
453 }
454}
Note: See TracBrowser for help on using the repository browser.