forked from dylan-lang/opendylan
/
c-run-time.c
3777 lines (3344 loc) · 142 KB
/
c-run-time.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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "run-time.h"
#include "trace.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gc/gc.h>
#ifdef OPEN_DYLAN_PLATFORM_UNIX
#include <signal.h>
#endif
#define ignore(x) (void)x
#ifdef OPEN_DYLAN_COMPILER_GCC_LIKE
# define likely(x) __builtin_expect((x),1)
# define unlikely(x) __builtin_expect((x),0)
#else
# define likely(x) x
# define unlikely(x) x
#endif
#define MAX_HEAP_SIZE (2047 * 1024 * 1024)
/*
stack allocation
relies on inlining
calls to error handlers
pointer_element_at (base, byte_offset)
at and at_setter's for base and byte_offset
int8, int16, int32, int64,
uint8, uint16, uint32, uint64,
single_float, double_float
signal handling
break, error and errno
file date and file date setter
*/
/* PLATFORM SPECIFIC HAX */
#ifdef OPEN_DYLAN_PLATFORM_WINDOWS
#define rint(x) floor(x)
#define strncasecmp _strnicmp
#endif
/* DYLAN CONSTANTS */
extern dylan_object KPfalseVKi;
extern dylan_object KPtrueVKi;
extern dylan_object KPunboundVKi;
#define UNBOUND_P(x) ((x) == &KPunboundVKi)
#if defined(OPEN_DYLAN_PLATFORM_WINDOWS)
#define INLINE __inline
#define FORCE_INLINE __forceinline
#elif defined(OPEN_DYLAN_COMPILER_CLANG)
#define INLINE static inline
#define FORCE_INLINE static inline __attribute__((always_inline))
#else
#define INLINE inline
#define FORCE_INLINE inline __attribute__((always_inline))
#endif
/* stubbed primitives */
dylan_value primitive_runtime_module_handle()
{
return(I(0));
}
/* SUPPORT */
void primitive_break() {
#if defined(OPEN_DYLAN_PLATFORM_WINDOWS)
extern void __stdcall DebugBreak(void);
DebugBreak();
#else
#ifdef SIGTRAP
fprintf(stderr, "Breaking into debugger.\n");
fflush(stderr);
raise(SIGTRAP);
#else
int *ptr = (int*)0;
puts("Breaking into debugger.");
fflush(stdout);
*ptr = 0; /* generate a memory fault */
#endif
#endif
}
/* MEMORY */
INLINE dylan_value instance_header_setter (dylan_value header, dylan_value* instance) {
instance[0] = header;
return(header);
}
dylan_value allocate (unsigned long size) {
#if defined(GC_USE_BOEHM)
return((dylan_value)GC_MALLOC((size_t)size));
#elif defined(GC_USE_MALLOC)
return((dylan_value)malloc((size_t)size));
#endif
}
dylan_value primitive_allocate (DSINT size) {
return((dylan_value)allocate(size * sizeof(dylan_value)));
}
dylan_value primitive_untraced_allocate (DSINT size) {
return dylan__malloc__misc(size * sizeof(dylan_value));
}
dylan_value primitive_manual_allocate (dylan_value sizeObject) {
size_t size = (size_t)R(sizeObject);
void* p = mps__malloc(size);
return primitive_wrap_machine_word((DMINT)p);
}
void primitive_manual_free (dylan_value object) {
mps__free((void*)primitive_unwrap_c_pointer(object));
}
void primitive_fillX(dylan_value dst, int base_offset, int offset, int size, dylan_value value) {
register int i;
dylan_value* target = ((dylan_value*)dst) + base_offset + offset;
for (i = 0; i < size; i++) {
target[i] = value;
}
}
void primitive_fill_bytesX
(dylan_value dst, int base_offset, int offset, int size, DSINT value) {
if (size > 0) {
memset(((unsigned char*)((dylan_value*)dst + base_offset)) + offset, value, (size_t)size);
}
}
DSINT primitive_repeated_slot_offset(dylan_value x) {
dylan_value* instance = (dylan_value*)x;
Wrapper* wrapper = (Wrapper*)instance[0];
DSINT fixed_part = wrapper->fixed_part;
DSINT n_slots = fixed_part >> 2;
DSINT offset = 1 + n_slots + 1;
return(offset);
}
dylan_value primitive_repeated_slot_as_raw(dylan_value x, DSINT offset) {
return((dylan_value)((dylan_value*)x + offset));
}
void primitive_replace_bytesX
(dylan_value dst, DSINT dst_base_offset, DSINT dst_offset,
dylan_value src, DSINT src_base_offset, DSINT src_offset, DSINT size) {
if (size > 0) {
memcpy(&(((char*)(&(((dylan_value*)dst)[dst_base_offset])))[dst_offset]),
&(((char*)(&(((dylan_value*)src)[src_base_offset])))[src_offset]),
(size_t)size);
}
}
#define COPY_WORDS(dst, src, size) memcpy((dst), (src), (size) * sizeof(dylan_value))
void primitive_replaceX
(dylan_value dst, DSINT dst_base_offset, DSINT dst_offset,
dylan_value src, DSINT src_base_offset, DSINT src_offset, DSINT size) {
ignore(src_base_offset);
if (size > 0) {
COPY_WORDS(&(((dylan_value*)dst)[dst_base_offset + dst_offset]),
&(((dylan_value*)src)[dst_base_offset + src_offset]),
size);
}
}
dylan_value primitive_compare_bytes(dylan_value base1, DSINT offset1,
dylan_value base2, DSINT offset2, DSINT size) {
return (RAWASBOOL(memcmp(&((((dylan_byte_string*)base1)->data)[offset1]),
&((((dylan_byte_string*)base2)->data)[offset2]),
(size_t)size)));
}
dylan_value primitive_compare_words(dylan_value base1, DSINT offset1,
dylan_value base2, DSINT offset2, DSINT size) {
return (RAWASBOOL(memcmp(&((((dylan_byte_string*)base1)->data)[offset1]),
&((((dylan_byte_string*)base2)->data)[offset2]),
size * sizeof(dylan_value))));
}
DSINT round_up_to_word(DSINT val) {
size_t a = sizeof(void *);
return (val + a - 1) & ~((unsigned long)a - 1);
}
dylan_value primitive_byte_allocate_filled_terminated
(DSINT size, DSINT number_bytes, dylan_value class_wrapper, DSINT number_slots,
dylan_value fill_value, DSINT repeated_size, DSINT repeated_size_offset)
{
size = round_up_to_word(size * sizeof(dylan_value) + number_bytes);
int byte_fill = R(fill_value);
if (number_slots == 0) {
return primitive_alloc_leaf_rbfz(size,
class_wrapper,
repeated_size,
repeated_size_offset,
byte_fill);
} else {
return primitive_alloc_leaf_s_rbfz(size,
class_wrapper,
number_slots,
fill_value,
repeated_size,
repeated_size_offset,
byte_fill);
}
}
dylan_value primitive_byte_allocate_filled
(DSINT size, dylan_value class_wrapper, DSINT number_slots,
dylan_value fill_value, DSINT repeated_size, DSINT repeated_size_offset,
DBYTE repeated_fill_value)
{
size = round_up_to_word(size * sizeof(dylan_value) + repeated_size);
if (repeated_size_offset == 0) {
return primitive_alloc_s(size, class_wrapper, number_slots, fill_value);
} else {
return primitive_alloc_s_rbf(size, class_wrapper, number_slots, fill_value,
repeated_size, repeated_size_offset,
repeated_fill_value);
}
}
dylan_value primitive_byte_allocate_leaf_filled_terminated
(DSINT size, DSINT number_bytes, dylan_value class_wrapper, DSINT number_slots,
dylan_value fill_value, DSINT repeated_size, DSINT repeated_size_offset)
{
return primitive_byte_allocate_filled_terminated(size, number_bytes,
class_wrapper, number_slots,
fill_value, repeated_size,
repeated_size_offset);
}
dylan_value primitive_byte_allocate_leaf_filled
(DSINT size, dylan_value class_wrapper, DSINT number_slots,
dylan_value fill_value, DSINT repeated_size, DSINT repeated_size_offset,
DBYTE repeated_fill_value)
{
size = round_up_to_word(size * sizeof(dylan_value) + repeated_size);
if (repeated_size_offset == 0) {
return primitive_alloc_leaf_s(size, class_wrapper, number_slots,
fill_value);
} else {
return primitive_alloc_leaf_s_rbf(size, class_wrapper, number_slots,
fill_value, repeated_size,
repeated_size_offset,
repeated_fill_value);
}
}
#define define_repeated_allocator(name, type, alloc_rf, alloc_s_rf) \
dylan_value primitive_ ## name ## _allocate_filled \
(DSINT size, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value, \
DSINT repeated_size, DSINT repeated_size_offset, \
type repeated_fill_value) \
{ \
size = (size * sizeof(dylan_value)) + (repeated_size * sizeof(type)); \
if (number_slots == 0) { \
return alloc_rf(size, class_wrapper, repeated_size, \
repeated_size_offset, repeated_fill_value); \
} else { \
return alloc_s_rf(size, class_wrapper, number_slots, fill_value, \
repeated_size, repeated_size_offset, \
repeated_fill_value); \
} \
}
define_repeated_allocator(object, dylan_value, primitive_alloc_rf, primitive_alloc_s_rf)
define_repeated_allocator(double_byte, DDBYTE, primitive_alloc_leaf_rhf, primitive_alloc_s_rhf)
define_repeated_allocator(single_float, DSFLT, primitive_alloc_leaf_rsff, primitive_alloc_s_rsff)
define_repeated_allocator(double_float, DDFLT, primitive_alloc_leaf_rdff, primitive_alloc_s_rdff)
dylan_value primitive_word_allocate_filled
(DSINT size, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value,
DSINT repeated_size, DSINT repeated_size_offset, DWORD repeated_fill_value) {
size = (size * sizeof(dylan_value)) + (repeated_size * sizeof(DWORD));
if (number_slots == 0) {
return primitive_alloc_leaf_rf(size, class_wrapper, repeated_size,
repeated_size_offset, &repeated_fill_value);
} else {
return primitive_alloc_s_rf(size, class_wrapper, number_slots, fill_value,
repeated_size, repeated_size_offset,
&repeated_fill_value);
}
}
dylan_value primitive_allocate_filled
(DSINT size, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value,
DSINT repeated_size, DSINT repeated_size_offset)
{
size = size * sizeof(dylan_value);
if (repeated_size_offset == 0) {
if (number_slots == 0) {
return primitive_alloc(size, class_wrapper);
} else if (number_slots == 1) {
return primitive_alloc_s1(size, class_wrapper, fill_value);
} else if (number_slots == 2) {
return primitive_alloc_s2(size, class_wrapper, fill_value, fill_value);
} else {
return primitive_alloc_s(size, class_wrapper, repeated_size, fill_value);
}
abort();
} else {
abort();
}
}
dylan_value primitive_allocate_in_awl_pool
(DSINT size, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value,
DSINT repeated_size, DSINT repeated_size_offset, dylan_value assoc)
{
size = size * sizeof(dylan_value);
if (number_slots == 1) {
return primitive_alloc_exact_awl_rf(size,
class_wrapper,
assoc,
repeated_size,
repeated_size_offset,
fill_value);
} else {
return primitive_alloc_exact_awl_s_r(size,
class_wrapper,
assoc,
number_slots,
fill_value,
repeated_size,
repeated_size_offset);
}
}
dylan_value primitive_allocate_weak_in_awl_pool
(DSINT size, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value,
DSINT repeated_size, DSINT repeated_size_offset, dylan_value assoc)
{
size = size * sizeof(dylan_value);
if (number_slots == 0) {
return primitive_alloc_weak_awl_rf(size,
class_wrapper,
assoc,
repeated_size,
repeated_size_offset,
fill_value);
} else {
return primitive_alloc_weak_awl_s_r(size,
class_wrapper,
assoc,
number_slots,
fill_value,
repeated_size,
repeated_size_offset);
}
}
dylan_value primitive_allocate_wrapper
(DSINT size, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value,
DSINT repeated_size, DSINT repeated_size_offset)
{
size = size * sizeof(dylan_value);
return primitive_alloc_wrapper_s_r(size,
class_wrapper,
number_slots,
fill_value,
repeated_size,
repeated_size_offset);
}
/* STACK ALLOCATION */
/* This one still zero-terminates. TODO: turn that off */
dylan_value initialize_byte_stack_allocate_filled
(dylan_value ptr, dylan_value class_wrapper, DSINT number_slots,
dylan_value fill_value, DSINT repeated_size, DSINT repeated_size_offset,
DBYTE repeated_fill_value)
{
dylan_value* object = ptr;
instance_header_setter(class_wrapper, object);
primitive_fillX(object, 1, 0, number_slots, fill_value);
primitive_fill_bytesX
(object, repeated_size_offset + 1, 0, repeated_size,
(unsigned char)R(repeated_fill_value));
((char*)(&object[repeated_size_offset + 1]))[repeated_size] = (char)0;
if (repeated_size_offset > 0) {
object[repeated_size_offset] = I(repeated_size);
}
return((dylan_value)object);
}
dylan_value initialize_object_stack_allocate_filled
(dylan_value ptr, dylan_value class_wrapper, DSINT number_slots, dylan_value fill_value,
DSINT repeated_size, DSINT repeated_size_offset,
dylan_value repeated_fill_value)
{
int i;
dylan_value* object = ptr;
instance_header_setter(class_wrapper, object);
primitive_fillX(object, 1, 0, number_slots, fill_value);
for (i = 0; i < repeated_size; i++) {
((dylan_value*)(&object[repeated_size_offset + 1]))[i] = (dylan_value)repeated_fill_value;
}
if (repeated_size_offset > 0) {
object[repeated_size_offset] = I(repeated_size);
}
return((dylan_value)object);
}
/* PINNING PRIMITIVES */
void primitive_unpin_object(dylan_value object)
{
ignore(object);
}
/* C-FFI PRIMITIVES */
dylan_value primitive_wrap_c_pointer(dylan_value wrapper, DMINT x) {
return(primitive_allocate_filled
(2, wrapper, 1, (dylan_value)x, 0, 0));
}
/* VECTOR */
extern Wrapper KLsimple_object_vectorGVKdW;
#define VECTOR_HEADER_SIZE (2)
/* gts,98apr08 */
dylan_value VECTOR_REF_OR_F(dylan_value vector, int offset) {
if (offset >= vector_size(vector)) {
return(DFALSE);
} else {
return(vector_ref((dylan_simple_object_vector*)vector, offset));
}
}
INLINE dylan_value vector_ref_setter (dylan_value new_value, dylan_simple_object_vector* vector, int offset) {
return(vector_data(vector)[offset] = new_value);
}
extern dylan_simple_object_vector* Pempty_vectorVKi;
dylan_simple_object_vector* allocate_vector (int size) {
if (size == 0) {
return(Pempty_vectorVKi);
} else {
dylan_simple_object_vector* vector = (dylan_simple_object_vector*)primitive_allocate(size + VECTOR_HEADER_SIZE);
return(vector);
}
}
void *make_dylan_vector(size_t n) {
if (n == 0) {
return Pempty_vectorVKi;
} else {
size_t size;
void* vector;
size = (n + VECTOR_HEADER_SIZE) * sizeof(dylan_value);
vector = primitive_alloc_rf(size,
&KLsimple_object_vectorGVKdW,
n, 1, &KPunboundVKi);
return vector;
}
}
dylan_value primitive_make_vector (int size) {
return (dylan_value)make_dylan_vector((size_t)size);
}
dylan_simple_object_vector* initialize_vector_from_buffer_with_size
(dylan_simple_object_vector* vector, int vector_size, dylan_value* buffer, int buffer_size)
{
instance_header_setter(&KLsimple_object_vectorGVKdW, (dylan_value*)vector);
vector_size_setter(vector_size, vector);
COPY_WORDS(vector_data(vector), buffer, buffer_size);
return(vector);
}
dylan_simple_object_vector* initialize_vector_from_buffer (dylan_simple_object_vector* vector, int size, dylan_value* buffer) {
return(initialize_vector_from_buffer_with_size(vector, size, buffer, size));
}
dylan_simple_object_vector* make_vector_from_buffer (int size, dylan_value* buffer) {
dylan_simple_object_vector* copy = allocate_vector(size);
initialize_vector_from_buffer(copy, size, buffer);
return(copy);
}
dylan_value primitive_copy_vector (dylan_value vector) {
return((dylan_value)make_vector_from_buffer(vector_size((dylan_simple_object_vector*)vector), vector_data((dylan_simple_object_vector*)vector)));
}
dylan_value primitive_raw_as_vector (dylan_value size, dylan_value buffer) {
return(make_vector_from_buffer((long)size, (dylan_value*)buffer));
}
#define DEF_STACK_VECTOR(_name, _size) \
dylan_value _stk_##_name[_size + VECTOR_HEADER_SIZE]; \
dylan_simple_object_vector* _name = (dylan_simple_object_vector*)(&_stk_##_name)
#define DEF_STACK_VECTOR_INITTED(_name, _size) \
dylan_value _stk_##_name[_size + VECTOR_HEADER_SIZE]; \
dylan_simple_object_vector* _name = (init_stack_vector((dylan_simple_object_vector*)(&_stk_##_name), (_size)))
INLINE dylan_simple_object_vector* init_stack_vector(dylan_simple_object_vector* vector, int size) {
instance_header_setter(&KLsimple_object_vectorGVKdW, (dylan_value*)vector);
vector_size_setter(size, vector);
return(vector);
}
#define DEF_STACK_VECTOR_FROM_BUFFER_WITH_SIZE(_name, _vector_size, _buffer, _buffer_size) \
DEF_STACK_VECTOR(_name, _vector_size); \
initialize_vector_from_buffer_with_size (_name, _vector_size, _buffer, _buffer_size)
#define DEF_STACK_VECTOR_FROM_BUFFER(_name, _size, _buffer) \
DEF_STACK_VECTOR_FROM_BUFFER_WITH_SIZE(_name, _size, _buffer, _size)
#define primitive_stack_allocate_vector(size) \
((dylan_simple_object_vector*)primitive_stack_allocate(size + VECTOR_HEADER_SIZE))
/* STRING */
extern Wrapper KLbyte_stringGVKdW;
dylan_value primitive_raw_as_string (DBSTR buffer) {
size_t size = strlen(buffer);
dylan_byte_string* string = (dylan_byte_string*)allocate(sizeof(dylan_byte_string) + size + 1);
instance_header_setter(&KLbyte_stringGVKdW, (dylan_value*)string);
string->size = I(size);
memcpy(string->data, buffer, size);
return((dylan_value)string);
}
/* SIGNATURES */
INLINE dylan_simple_object_vector* signature_required(dylan_signature* sig) {
return(sig->required);
}
INLINE dylan_simple_object_vector* signature_values(dylan_signature* sig) {
return(sig->values);
}
INLINE dylan_value signature_rest_value(dylan_signature* sig) {
return(sig->rest_value);
}
#define NUMBER_REQUIRED_MASK 0x0000ff
#define NUMBER_VALUES_MASK 0x00ff00
#define NUMBER_VALUES_OFFSET 8
#define KEY_P_MASK 0x010000
#define ALL_KEYS_P_MASK 0x020000
#define REST_P_MASK 0x040000
#define OPTIONALS_P_MASK (KEY_P_MASK | REST_P_MASK)
#define REST_VALUE_P_MASK 0x080000
#define NEXT_P_MASK 0x100000
INLINE int signature_number_required(dylan_signature* sig) {
return(R(sig->properties) & NUMBER_REQUIRED_MASK);
}
INLINE int signature_number_values(dylan_signature* sig) {
return((R(sig->properties) & NUMBER_VALUES_MASK)
>> NUMBER_VALUES_OFFSET);
}
INLINE int signature_key_p(dylan_signature* sig) {
return((R(sig->properties) & KEY_P_MASK) > 0);
}
INLINE int signature_all_keys_p(dylan_signature* sig) {
return((R(sig->properties) & ALL_KEYS_P_MASK) > 0);
}
INLINE int signature_rest_p(dylan_signature* sig) {
return((R(sig->properties) & REST_P_MASK) > 0);
}
INLINE int signature_optionals_p(dylan_signature* sig) {
return((R(sig->properties) & OPTIONALS_P_MASK) > 0);
}
INLINE int signature_rest_value_p(dylan_signature* sig) {
return((R(sig->properties) & REST_VALUE_P_MASK) > 0);
}
INLINE int signature_next_p(dylan_signature* sig) {
return((R(sig->properties) & NEXT_P_MASK) > 0);
}
INLINE dylan_value signature_make_properties
(int number_required, int number_values,
int key_p, int all_keys_p, int rest_p, int rest_value_p) {
return(I(number_required
| (number_values << NUMBER_VALUES_OFFSET)
| (key_p ? KEY_P_MASK : 0)
| (all_keys_p ? ALL_KEYS_P_MASK : 0)
| (rest_p ? REST_P_MASK : 0)
| (rest_value_p ? REST_VALUE_P_MASK : 0)));
}
/* FUNCTION */
INLINE DFN function_xep(dylan_simple_method* function) {
return(function->xep);
}
DFN primitive_function_xep(dylan_value function) {
return(function_xep((dylan_simple_method*)function));
}
INLINE DLFN function_mep(dylan_simple_method* function) {
return(function->mep);
}
INLINE DLFN function_iep(dylan_simple_method* function) {
return(function->mep);
}
INLINE DLFN keyword_function_iep(dylan_simple_method* function) {
return(((dylan_keyword_method*)function)->iep);
}
INLINE dylan_value method_keyword_specifiers(dylan_simple_method* method) {
return(((dylan_keyword_method*)method)->keyword_specifiers);
}
INLINE dylan_simple_object_vector* function_specializers(dylan_simple_method* function) {
return(signature_required(function->signature));
}
INLINE int function_number_required(dylan_simple_method* function) {
return(signature_number_required(function->signature));
}
INLINE int function_number_values(dylan_simple_method* function) {
return(signature_number_values(function->signature));
}
INLINE int function_key_p(dylan_simple_method* function) {
return(signature_key_p(function->signature));
}
INLINE int function_all_keys_p(dylan_simple_method* function) {
return(signature_all_keys_p(function->signature));
}
INLINE int function_rest_p(dylan_simple_method* function) {
return(signature_rest_p(function->signature));
}
INLINE int function_optionals_p(dylan_simple_method* function) {
return(signature_optionals_p(function->signature));
}
INLINE int function_rest_value_p(dylan_simple_method* function) {
return(signature_rest_value_p(function->signature));
}
INLINE int function_next_p(dylan_simple_method* function) {
return(signature_next_p(function->signature));
}
/* VARARGS SUPPORT */
INLINE void transfer_varargs(va_list ap, int n, dylan_value* arguments) {
int i;
for (i=0; i<n; i++) {
arguments[i] = va_arg(ap, dylan_value);
}
}
#define BUFFER_VARARGS(n, last_parameter, buffer) \
{ \
va_list ap; \
va_start(ap, (last_parameter)); \
transfer_varargs(ap, (n), (buffer)); \
va_end(ap); \
}
/* CALLING CONVENTION */
/* CALL CHECKS */
extern dylan_value LobjectGVKd;
extern dylan_value Ktype_check_errorVKiI(dylan_value argument, dylan_value specializer);
#define INSTANCEP(x, y) (primitive_instanceQ((x), (y)) != DFALSE)
#define FUNCTIONP(x) \
(R((((Wrapper*)OBJECT_WRAPPER(x)))->subtype_mask) & 64)
FORCE_INLINE dylan_value PERFORM_INLINE_TYPE_CHECK(dylan_value value, dylan_value type) {
if (unlikely(type != LobjectGVKd && !INSTANCEP(value, type))) {
Ktype_check_errorVKiI(value, type);
}
return(value);
}
dylan_value primitive_type_check (dylan_value value, dylan_value type) {
return PERFORM_INLINE_TYPE_CHECK(value, type);
}
extern dylan_value Kstack_overflow_errorVKiI();
INLINE void SIMPLE_CALL_CHECK(dylan_simple_method* function) {
/* int stack_marker; */
ignore(function);
/*
if (STACK_OK(bottom_of_stack,(unsigned long)(&stack_marker))) {
stack_overflowedQ = 1;
Kstack_overflow_errorVKiI();
}*/
}
extern dylan_value Kargument_count_overflow_errorVKiI(dylan_value function, dylan_value argc);
INLINE void CALL_CHECK(dylan_simple_method* function, int argument_count) {
SIMPLE_CALL_CHECK(function);
if (unlikely(argument_count > MAX_ARGUMENTS)) {
Kargument_count_overflow_errorVKiI(function, I(argument_count));
}
}
FORCE_INLINE void TYPE_CHECK_ARG (dylan_value specializer, dylan_value argument) {
PERFORM_INLINE_TYPE_CHECK(argument, specializer);
}
INLINE void TYPE_CHECK_ARGS(dylan_value function, int argument_count, dylan_value* arguments) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)function);
if (specs) {
dylan_value* specializers = vector_data(specs);
int i;
for (i = 0; i < argument_count; i++) {
TYPE_CHECK_ARG(specializers[i], arguments[i]);
}
}
}
INLINE void TYPE_CHECK_ARGS_1(dylan_value fn, dylan_value a1) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
}
}
INLINE void TYPE_CHECK_ARGS_2(dylan_value fn, dylan_value a1, dylan_value a2) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
}
}
INLINE void TYPE_CHECK_ARGS_3(dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
}
}
INLINE void TYPE_CHECK_ARGS_4(dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3, dylan_value a4) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
TYPE_CHECK_ARG(specializers[3], a4);
}
}
INLINE void TYPE_CHECK_ARGS_5(dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3, dylan_value a4, dylan_value a5) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
TYPE_CHECK_ARG(specializers[3], a4);
TYPE_CHECK_ARG(specializers[4], a5);
}
}
INLINE void TYPE_CHECK_ARGS_6 (dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3, dylan_value a4, dylan_value a5, dylan_value a6) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
TYPE_CHECK_ARG(specializers[3], a4);
TYPE_CHECK_ARG(specializers[4], a5);
TYPE_CHECK_ARG(specializers[5], a6);
}
}
INLINE void TYPE_CHECK_ARGS_7
(dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3, dylan_value a4, dylan_value a5, dylan_value a6, dylan_value a7) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
TYPE_CHECK_ARG(specializers[3], a4);
TYPE_CHECK_ARG(specializers[4], a5);
TYPE_CHECK_ARG(specializers[5], a6);
TYPE_CHECK_ARG(specializers[6], a7);
}
}
INLINE void TYPE_CHECK_ARGS_8
(dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3, dylan_value a4, dylan_value a5, dylan_value a6, dylan_value a7, dylan_value a8) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
TYPE_CHECK_ARG(specializers[3], a4);
TYPE_CHECK_ARG(specializers[4], a5);
TYPE_CHECK_ARG(specializers[5], a6);
TYPE_CHECK_ARG(specializers[6], a7);
TYPE_CHECK_ARG(specializers[7], a8);
}
}
INLINE void TYPE_CHECK_ARGS_9
(dylan_value fn, dylan_value a1, dylan_value a2, dylan_value a3, dylan_value a4, dylan_value a5, dylan_value a6, dylan_value a7, dylan_value a8, dylan_value a9) {
dylan_simple_object_vector* specs = function_specializers((dylan_simple_method*)fn);
if (specs) {
dylan_value* specializers = vector_data(specs);
TYPE_CHECK_ARG(specializers[0], a1);
TYPE_CHECK_ARG(specializers[1], a2);
TYPE_CHECK_ARG(specializers[2], a3);
TYPE_CHECK_ARG(specializers[3], a4);
TYPE_CHECK_ARG(specializers[4], a5);
TYPE_CHECK_ARG(specializers[5], a6);
TYPE_CHECK_ARG(specializers[6], a7);
TYPE_CHECK_ARG(specializers[7], a8);
TYPE_CHECK_ARG(specializers[8], a9);
}
}
extern dylan_value Kargument_count_errorVKiI(dylan_value function, dylan_value argc);
INLINE void BASIC_REQUIRED_CALL_CHECK
(dylan_simple_method* function, int number_required, int argument_count) {
CALL_CHECK(function, argument_count);
if (unlikely(argument_count != number_required)) {
Kargument_count_errorVKiI(function, I(argument_count));
}
}
INLINE void REQUIRED_CALL_CHECK
(dylan_simple_method* function, int number_required, int argument_count, dylan_value* arguments) {
BASIC_REQUIRED_CALL_CHECK(function, number_required, argument_count);
TYPE_CHECK_ARGS(function, argument_count, arguments);
}
INLINE void BASIC_OPTIONAL_CALL_CHECK
(dylan_simple_method* function, int number_required, int argument_count) {
CALL_CHECK(function, argument_count);
if (unlikely(argument_count < number_required)) {
Kargument_count_errorVKiI(function, I(argument_count));
}
}
INLINE void OPTIONAL_CALL_CHECK
(dylan_simple_method* function, int number_required, int argument_count, dylan_value* arguments) {
BASIC_OPTIONAL_CALL_CHECK(function, number_required, argument_count);
TYPE_CHECK_ARGS(function, number_required, arguments);
}
extern dylan_value Kodd_keyword_arguments_errorVKiI(dylan_value function, dylan_value argc);
INLINE void KEYWORD_CALL_CHECK
(dylan_simple_method* function, int number_required, int argument_count, dylan_value* arguments) {
OPTIONAL_CALL_CHECK (function, number_required, argument_count, arguments);
if (unlikely((argument_count - number_required) & 1)) {
Kodd_keyword_arguments_errorVKiI(function, I(argument_count));
}
}
/* CALLING CONVENTION */
dylan_value primitive_xep_apply (dylan_simple_method* fn, int n, dylan_value a[]) {
TEB* teb = get_teb();
DFN xep = fn->xep;
teb->function = fn;
teb->next_methods = DFALSE;
switch (n) {
case 0: return(xep(fn,n));
case 1: return(xep(fn,n,a[0]));
case 2: return(xep(fn,n,a[0],a[1]));
case 3: return(xep(fn,n,a[0],a[1],a[2]));
case 4: return(xep(fn,n,a[0],a[1],a[2],a[3]));
case 5: return(xep(fn,n,a[0],a[1],a[2],a[3],a[4]));
case 6: return(xep(fn,n,a[0],a[1],a[2],a[3],a[4],a[5]));
case 7: return(xep(fn,n,a[0],a[1],a[2],a[3],a[4],a[5],a[6]));
case 8: return(xep(fn,n,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7]));
case 9: return(xep(fn,n,a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8]));
default:
if (n > 64) {
primitive_break();
}
return(xep(fn,n,
a[ 0],a[ 1],a[ 2],a[ 3],a[ 4],a[ 5],a[ 6],a[ 7],a[ 8],a[ 9],
a[10],a[11],a[12],a[13],a[14],a[15],a[16],a[17],a[18],a[19],
a[20],a[21],a[22],a[23],a[24],a[25],a[26],a[27],a[28],a[29],
a[30],a[31],a[32],a[33],a[34],a[35],a[36],a[37],a[38],a[39],
a[40],a[41],a[42],a[43],a[44],a[45],a[46],a[47],a[48],a[49],
a[50],a[51],a[52],a[53],a[54],a[55],a[56],a[57],a[58],a[59],
a[60],a[61],a[62],a[63]));
}
}
/*
INLINE int FUNCTION_OK(dylan_simple_method* function) {
return(class_meta_class(function_class(function)) ==
dylanXinternalX_L_function_class_G_);
}
*/
dylan_value primitive_xep_call (dylan_simple_method* fn, int n, ...) {
TEB* teb = get_teb();
int i;
va_list ap; va_start(ap,n);
for (i=0; i<n; i++) {
dylan_value argument = va_arg(ap, dylan_value);
teb->arguments[i] = argument;
}
REQUIRED_CALL_CHECK(fn, function_number_required(fn), n, teb->arguments);
return(primitive_xep_apply(fn, n, teb->arguments));
}
/*
CALL_DYLAN_FUNCTION_RETURNING_ALL_VALUES Added by phoward, 13th FEB 97.
This is not called from anywhere in the runtime. It can be called
remotely from the debugger to execute a dylan function via its XEP,
and vector all return values.
*/
dylan_value call_dylan_function_returning_all_values (dylan_simple_method* fn, int n, ...) {
TEB* teb = get_teb();
int i;
dylan_value first_value;
va_list ap; va_start(ap,n);
for (i=0; i<n; i++) {
dylan_value argument = va_arg(ap, dylan_value);
teb->arguments[i] = argument;
}
first_value = primitive_xep_apply(fn, n, teb->arguments);
return(MV_GET_REST_AT(first_value, 0));
}
dylan_value primitive_mep_apply_with_optionals (dylan_simple_method* fn, dylan_value new_next_methods, dylan_value args) {
TEB* teb = get_teb();
DLFN mep = fn->mep;
dylan_value* v = vector_data((dylan_simple_object_vector*)args);
teb->next_methods = new_next_methods;
teb->function = fn;
teb->argument_count = vector_size((dylan_simple_object_vector*)args);
switch (teb->argument_count) {
case 0: return(mep());
case 1: return(mep(v[0]));
case 2: return(mep(v[0],v[1]));
case 3: return(mep(v[0],v[1],v[2]));
case 4: return(mep(v[0],v[1],v[2],v[3]));
case 5: return(mep(v[0],v[1],v[2],v[3],v[4]));
case 6: return(mep(v[0],v[1],v[2],v[3],v[4],v[5]));
case 7: return(mep(v[0],v[1],v[2],v[3],v[4],v[5],v[6]));