/
eul-serial.c
739 lines (580 loc) · 20.6 KB
/
eul-serial.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/// Copyright 1997 A. Kind & University of Bath
/// Copyright 2010 Henry G. Weller
///-----------------------------------------------------------------------------
// This file is part of
/// --- EuLisp System 'Youtoo'
///-----------------------------------------------------------------------------
//
// Youtoo is free software: you can redistribute it and/or modify it under the
// terms of the GNU General Public License version 2 as published by the Free
// Software Foundation.
//
// Youtoo is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
//
///-----------------------------------------------------------------------------
/// Title: marshaling EuLisp objects in a serialized format
/// Library: eul-serial
/// Authors: Andreas Kind
/// Maintainer: Henry G. Weller
///-----------------------------------------------------------------------------
#include "eulisp.h"
#include "eul-serial.h"
#include "eul-serial2.h"
#include "u2/serial.h"
///-----------------------------------------------------------------------------
/// Primitive serialization (should use xdr soon!)
///-----------------------------------------------------------------------------
LispRef eul_serial_short_data(int x)
{
LispRef res;
eul_allocate_blank_string(res, sizeof(short));
*(short*)eul_string_as_c_string(res) = (short)x;
return res;
}
LispRef eul_serial_int_data(int x)
{
LispRef res;
eul_allocate_blank_string(res, sizeof(int));
*(int*)eul_string_as_c_string(res) = x;
return res;
}
LispRef eul_serial_double_data(LispRef ref)
{
double x = eul_double_as_c_double(ref);
LispRef res;
eul_allocate_blank_string(res, sizeof(double));
*(double*)eul_string_as_c_string(res) = x;
return res;
}
LispRef eul_serial_lambda_data(LispRef x)
{
Instruction *code = (Instruction *) LAMBDA_CODE(x);
int n = eul_fpi_as_c_int(eul_size_of(x));
LispRef res;
eul_allocate_blank_string(res, n);
Instruction *data = (Instruction *)eul_string_as_c_string(res);
memcpy(data, code, n);
return res;
}
LispRef eul_serial_bytevector_data(LispRef x)
{
char *data = (char *)BYTEVECTOR_DATA(x);
int n = eul_fpi_as_c_int(eul_size_of(x));
LispRef res;
eul_allocate_nstring(res, data, n);
return res;
}
int eul_serial_relative_pc(LispRef fun, LispRef eul_pc)
{
Instruction *pc = (Instruction *) eul_pc;
Instruction *bv = (Instruction *) LAMBDA_CODE(fun);
return (int)(pc - bv);
}
EUL_DEFINTERN(eul_serial_read_bytes, "eul-serial-read-bytes", 4, serial)
EUL_DEFINTERN(eul_serial_error, "eul-serial-error", 3, serial)
EUL_DEFINTERN(eul_serial_make_class, "eul-serial-make-class", 3, serial)
EUL_DEFINTERN(eul_serial_make_state, "eul-serial-make-state", 4, serial)
EUL_DEFINTERN
(
eul_serial_make_file_stream,
"eul-serial-make-file-stream",
2,
serial
)
EUL_DEFINTERN
(
eul_serial_make_instance,
"eul-serial-make-instance",
2,
serial
)
EUL_DEFINTERN
(
eul_serial_allocate_instance,
"eul-serial-allocate-instance",
1,
serial
)
EUL_DEFINTERN
(
eul_serial_initialize_instance,
"eul-serial-initialize-instance",
2,
serial
)
LispRef eul_serial_initialize()
{
LispRef res;
eul_allocate_vector(res, 21, eul_nil);
slot_ref(res, 0) = c_int_as_eul_fpi(STREAM_MAGIC);
slot_ref(res, 1) = c_int_as_eul_fpi(STREAM_VERSION);
slot_ref(res, 2) = c_char_as_eul_char(TC_NULL);
slot_ref(res, 3) = c_char_as_eul_char(TC_REFERENCE);
slot_ref(res, 4) = c_char_as_eul_char(TC_CLASS);
slot_ref(res, 5) = c_char_as_eul_char(TC_OBJECT);
slot_ref(res, 6) = c_char_as_eul_char(TC_STRING);
slot_ref(res, 7) = c_char_as_eul_char(TC_VECTOR);
slot_ref(res, 8) = c_char_as_eul_char(TC_KEYWORD);
slot_ref(res, 9) = c_char_as_eul_char(TC_STREAM);
slot_ref(res, 10) = c_char_as_eul_char(TC_STATE);
slot_ref(res, 11) = c_char_as_eul_char(TC_RESET);
slot_ref(res, 12) = c_char_as_eul_char(TC_SELF);
slot_ref(res, 14) = c_char_as_eul_char(TC_FUNCTION);
slot_ref(res, 15) = c_char_as_eul_char(TC_BYTEVECTOR);
slot_ref(res, 16) = c_char_as_eul_char(TC_INT);
slot_ref(res, 17) = c_char_as_eul_char(TC_DOUBLE);
slot_ref(res, 18) = c_char_as_eul_char(TC_SYMBOL);
slot_ref(res, 19) = c_char_as_eul_char(TC_CHAR);
slot_ref(res, 20) = c_char_as_eul_char(TC_CONS);
return res;
}
///-----------------------------------------------------------------------------
/// Auxiliary functions
///-----------------------------------------------------------------------------
LispRef eul_symbol_as_eul_keyword(LispRef sym)
{
char *str1 = eul_symbol_as_c_string(sym);
int n = eul_fpi_as_c_int(eul_string_size(eul_symbol_name(sym)));
char *str2 = (char *)gc_malloc(n + 1);
strcpy(str2, str1);
LispRef key;
eul_intern_keyword(key, str2);
return key;
}
///-----------------------------------------------------------------------------
/// Interface to the Lisp world
///-----------------------------------------------------------------------------
LispRef eul_serial_read_header
(
LispRef stream,
LispRef eos_error_p,
LispRef eos_value
)
{
return header(stream, eos_error_p, eos_value);
}
LispRef eul_serial_read_object
(
LispRef stream,
LispRef eos_error_p,
LispRef eos_value
)
{
return content(stream, eos_error_p, eos_value);
}
///-----------------------------------------------------------------------------
/// header:
/// magic version
///-----------------------------------------------------------------------------
LispRef eul_keyword_key;
LispRef eul_name_key;
LispRef header(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "header\n"));
LispRef magic;
eul_read_short(magic);
LispRef version;
eul_read_short(version);
LispRef res;
eul_allocate_cons(res, magic, version);
eul_intern_keyword(eul_keyword_key, "keyword");
eul_intern_keyword(eul_name_key, "name");
return res;
}
///-----------------------------------------------------------------------------
/// content:
/// object
///-----------------------------------------------------------------------------
LispRef content(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "content\n"));
char tag;
read_byte(tag);
WITH_DEBUG(fprintf(stderr, " tag: %x\n", tag));
switch (tag)
{
case TC_NULL:
return nullReference(stream, eos_error_p, eos_value);
case TC_REFERENCE:
return prevObject(stream, eos_error_p, eos_value);
case TC_CLASS:
return newClass(stream, eos_error_p, eos_value);
case TC_OBJECT:
return newObject(stream, eos_error_p, eos_value);
case TC_STRING:
return newString(stream, eos_error_p, eos_value);
case TC_STATE:
return newState(stream, eos_error_p, eos_value);
case TC_VECTOR:
return newVector(stream, eos_error_p, eos_value);
case TC_STREAM:
return newStream(stream, eos_error_p, eos_value);
case TC_RESET:
return reset(stream, eos_error_p, eos_value);
case TC_SELF:
return stream;
case TC_FUNCTION:
return newFunction(stream, eos_error_p, eos_value);
case TC_BYTEVECTOR:
return newBytevector(stream, eos_error_p, eos_value);
case TC_INT:
return newInt(stream, eos_error_p, eos_value);
case TC_DOUBLE:
return newDouble(stream, eos_error_p, eos_value);
case TC_SYMBOL:
return newSymbol(stream, eos_error_p, eos_value);
case TC_KEYWORD:
return newKeyword(stream, eos_error_p, eos_value);
case TC_CHAR:
return newChar(stream, eos_error_p, eos_value);
case TC_CONS:
return newCons(stream, eos_error_p, eos_value);
default:
{
LispRef str, args;
eul_allocate_string(str, "unknown tag in ~a");
eul_allocate_cons(args, stream, eul_nil);
eul_serial_error(stream, str, args);
return eul_nil;
}
}
}
///-----------------------------------------------------------------------------
/// newClass:
/// TC_CLASS content content content newHandle
///-----------------------------------------------------------------------------
LispRef newClass(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newClass\n"));
LispRef name = content(stream, eos_error_p, eos_value);
LispRef supers = content(stream, eos_error_p, eos_value);
LispRef slot_descs = content(stream, eos_error_p, eos_value);
LispRef res = eul_serial_make_class(name, supers, slot_descs);
newHandle(stream, res);
return res;
}
///-----------------------------------------------------------------------------
/// newObject:
/// TC_OBJECT content
///-----------------------------------------------------------------------------
LispRef newObject(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newObject\n"));
LispRef cl = content(stream, eos_error_p, eos_value);
LispRef res = eul_serial_allocate_instance(cl);
newHandle(stream, res);
LispRef inits = content(stream, eos_error_p, eos_value);
eul_serial_initialize_instance(res, inits);
return res;
}
///-----------------------------------------------------------------------------
/// newCons:
/// TC_CONS content content
///-----------------------------------------------------------------------------
LispRef newCons(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newCons\n"));
LispRef res;
eul_allocate_empty_cons(res);
newHandle(stream, res);
LispRef the_car = content(stream, eos_error_p, eos_value);
LispRef the_cdr = content(stream, eos_error_p, eos_value);
eul_car(res) = the_car;
eul_cdr(res) = the_cdr;
return res;
}
///-----------------------------------------------------------------------------
/// newFunction:
/// TC_FUNCTION content content content content int [byte] newHandle content
///-----------------------------------------------------------------------------
LispRef newFunction(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newFunction\n"));
LispRef name = content(stream, eos_error_p, eos_value);
LispRef domain = content(stream, eos_error_p, eos_value);
LispRef setter = content(stream, eos_error_p, eos_value);
LispRef env = content(stream, eos_error_p, eos_value);
int n;
read_int(n);
WITH_DEBUG(fprintf(stderr, "size: %d\n", n));
char *data = read_bytes(n);
char *new_data = (char *)gc_malloc(n);
memcpy(new_data, data, n);
LispRef code;
eul_allocate_nstring(code, new_data, n);
LispRef res;
eul_allocate_lambda1(res, name, domain, code);
LAMBDA_SETTER(res) = setter;
LAMBDA_ENV(res) = env;
newHandle(stream, res);
LispRef refs = content(stream, eos_error_p, eos_value);
WITH_DEBUG(fprintf(stderr, "!!!Refs: "));
WITH_DEBUG(fprint_ref(stderr, refs));
WITH_DEBUG(fprintf(stderr, "\n"));
if (refs != eul_nil)
{
if (eul_link_lambda_refs(res, refs) == eul_nil)
{
LispRef str;
eul_allocate_string(str, "cannot serialize foreign function");
eul_serial_error(stream, str, eul_nil);
return eul_nil;
}
}
return res;
}
///-----------------------------------------------------------------------------
/// newBytevector:
/// TC_BYTEVECTOR int [byte] content
///-----------------------------------------------------------------------------
LispRef newBytevector(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newBytevector\n"));
int n;
read_int(n);
WITH_DEBUG(fprintf(stderr, "size: %d\n", n));
char *data = read_bytes(n);
char *new_data = (char *)gc_malloc(n);
memcpy(new_data, data, n);
LispRef code;
eul_allocate_nstring(code, new_data, n);
LispRef res;
eul_allocate_bytevector1(res, code, n);
LispRef refs = content(stream, eos_error_p, eos_value);
if (refs != eul_nil)
{
if (eul_link_lambda_refs(res, refs) == eul_nil)
{
LispRef str;
eul_allocate_string(str, "cannot serialize foreign function");
eul_serial_error(stream, str, eul_nil);
return eul_nil;
}
}
return res;
}
///-----------------------------------------------------------------------------
/// newVector int [content]
///-----------------------------------------------------------------------------
LispRef newVector(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newVector\n"));
int n;
read_int(n);
LispRef vec;
eul_allocate_vector(vec, n, eul_nil);
newHandle(stream, vec);
for (int i = 0; i < n; i++)
{
slot_ref(vec, i) = content(stream, eos_error_p, eos_value);
}
return vec;
}
///-----------------------------------------------------------------------------
/// newState content int content int
///-----------------------------------------------------------------------------
LispRef newState(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
LispRef value_stack = content(stream, eos_error_p, eos_value);
LispRef nvalue_stack;
eul_read_int(nvalue_stack);
LispRef context_stack = content(stream, eos_error_p, eos_value);
LispRef ncontext_stack;
eul_read_int(ncontext_stack);
// Change relative program counters into absolute values
LispRef vec = eul_nil + 1;
while (vec != eul_nil)
{
int n = eul_fpi_as_c_int(eul_size_of(vec));
for (int i = 1; i < n; i++)
{
LispRef fun = slot_ref(vec, i + 3);
if (fun != eul_nil)
{
ptrInt rel_pc = (ptrInt)slot_ref(vec, i + 1);
Instruction bv = (Instruction *) LAMBDA_CODE(fun);
Instruction *pc = bv + rel_pc;
slot_ref(vec, i + 1) = (LispRef) pc;
}
}
vec = slot_ref(vec, 0);
}
return eul_serial_make_state
(
value_stack,
nvalue_stack,
context_stack,
ncontext_stack
);
}
///-----------------------------------------------------------------------------
/// newString:
/// TC_STRING utf newHandle
///-----------------------------------------------------------------------------
LispRef newString(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newString\n"));
LispRef str;
eul_read_utf(str);
newHandle(stream, str);
return str;
}
///-----------------------------------------------------------------------------
/// newSymbol:
/// TC_Symbol utf
///-----------------------------------------------------------------------------
LispRef newSymbol(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newSymbol\n"));
char *str;
read_utf(str);
LispRef res;
eul_intern_symbol(res, str);
return res;
}
///-----------------------------------------------------------------------------
/// newKeyword:
/// TC_KEYWORD utf
///-----------------------------------------------------------------------------
LispRef newKeyword(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newKeyword\n"));
char *str;
read_utf(str);
LispRef res;
eul_intern_keyword(res, str);
return res;
}
///-----------------------------------------------------------------------------
/// newInt:
/// TC_INT word
///-----------------------------------------------------------------------------
LispRef newInt(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newInt\n"));
LispRef res;
eul_read_int(res);
return res;
}
///-----------------------------------------------------------------------------
/// newDouble:
/// TC_DOUBLE word
///-----------------------------------------------------------------------------
LispRef newDouble(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newDouble\n"));
LispRef res;
eul_read_double(res);
return res;
}
///-----------------------------------------------------------------------------
/// newChar:
/// TC_CHAR byte
///-----------------------------------------------------------------------------
LispRef newChar(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newChar\n"));
LispRef res;
eul_read_byte(res);
return res;
}
///-----------------------------------------------------------------------------
/// newStream:
/// TC_STREAM byte
///-----------------------------------------------------------------------------
LispRef newStream(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "newStream\n"));
LispRef mode = content(stream, eos_error_p, eos_value);
LispRef file_name = content(stream, eos_error_p, eos_value);
return eul_serial_make_file_stream(mode, file_name);
}
///-----------------------------------------------------------------------------
/// prevObject:
/// TC_REFERENCE (int)handle
///-----------------------------------------------------------------------------
LispRef prevObject(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "prevObject\n"));
short handle1;
read_short(handle1);
short handle2;
read_short(handle2);
WITH_DEBUG(fprintf(stderr, " handle: %x %x\n", handle1, handle2));
int handle = handle2;
LispRef prev_objects = EUL_OBJECT_STREAM_CACHE(stream);
int n = eul_fpi_as_c_int(object_size(prev_objects));
if (handle > n)
{
LispRef str;
eul_allocate_string(str, "bad handle");
eul_serial_error(stream, str, eul_nil);
return eul_nil;
}
LispRef obj = slot_ref(prev_objects, handle);
fprintf(stderr, "*** CACHE GET (read) %d FOR ", handle);
fprint_ref(stdout, obj);
fflush(stdout);
fprintf(stderr, "\n");
return obj;
}
///-----------------------------------------------------------------------------
/// nullReference:
/// TC_NULL
///-----------------------------------------------------------------------------
LispRef nullReference(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "nullReference\n"));
return eul_nil;
}
///-----------------------------------------------------------------------------
/// reset:
/// TC_RESET
///-----------------------------------------------------------------------------
LispRef reset(LispRef stream, LispRef eos_error_p, LispRef eos_value)
{
WITH_DEBUG(fprintf(stderr, "reset\n"));
EUL_OBJECT_STREAM_CACHE_INDEX(stream) = c_int_as_eul_fpi(0);
return eul_nil;
}
///-----------------------------------------------------------------------------
/// newHandle:
///-----------------------------------------------------------------------------
void newHandle(LispRef stream, LispRef obj)
{
WITH_DEBUG(fprintf(stderr, ">>> newHandle for "));
WITH_DEBUG(fprint_ref(stderr, obj));
WITH_DEBUG(fprintf(stderr, "\n"));
int handle = eul_fpi_as_c_int(EUL_OBJECT_STREAM_CACHE_INDEX(stream));
EUL_OBJECT_STREAM_CACHE_INDEX(stream) = c_int_as_eul_fpi(handle + 1);
WITH_DEBUG(fprintf(stderr, " handle: %x\n", handle));
LispRef prev_objects = EUL_OBJECT_STREAM_CACHE(stream);
int n = eul_fpi_as_c_int(eul_size_of(prev_objects));
WITH_DEBUG(fprintf(stderr, " cache size: %d\n", n));
// Check if cache is large enough
if (n <= handle)
{
unsigned int m = 2 * (n + 1);
LispRef new_prev_objects;
eul_allocate_vector(new_prev_objects, m, eul_nil);
WITH_DEBUG(fprintf(stderr, " Extending cache ...\n"));
for (int i = 0; i < n; i++)
{
slot_ref(new_prev_objects, i) = slot_ref(prev_objects, i);
}
EUL_OBJECT_STREAM_CACHE(stream) = prev_objects = new_prev_objects;
}
fprintf(stderr, "*** CACHE PUT (read) %d FOR ", handle);
fprint_ref(stdout, obj);
fflush(stdout);
fprintf(stderr, "\n");
// Store object in cache
slot_ref(prev_objects, handle) = obj;
}
///-----------------------------------------------------------------------------