-
Notifications
You must be signed in to change notification settings - Fork 6
/
core.toc
3128 lines (2815 loc) · 90.4 KB
/
core.toc
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
(inline-text "
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
typedef struct {int64_t type; int32_t refs;} Value;
typedef struct {int64_t type; int32_t refs; int64_t numVal;} Number;
typedef struct {int64_t type; int32_t refs; int64_t len; char buffer[0];} String;
typedef struct {int64_t type; int32_t refs; int64_t len; Number *hash; Value *source; char *buffer;} SubString;
typedef struct List {int64_t type; int32_t refs; int64_t len; Value* head; struct List *tail;} List;
typedef struct {int64_t type; int32_t refs; int count; List *closures; int variadic; void *fn;} FnArity;
typedef struct {int64_t type; int32_t refs; char *name; int64_t arityCount; FnArity *arities[];} Function;
typedef struct {int64_t type; Value *implFn;} ProtoImpl;
typedef struct {int64_t implCount; ProtoImpl impls[];} ProtoImpls;
typedef struct {int64_t type; int32_t refs; Value *typeArgs; int implCount; Value* impls[];} ReifiedVal;
typedef struct {int64_t type; int32_t refs; void *ptr;} Opaque;
typedef struct {int64_t type; int32_t refs; int32_t bitmap; Value *array[];} BitmapIndexedNode;
typedef struct {int64_t type; int32_t refs; Value *array[32];} ArrayNode;
typedef struct {int64_t type; int32_t refs; int16_t count; Value *array[];} HashCollisionNode;
// 0 is reserved for default protocol implementations
// all type numbers must be positive integers
#define StringType 1
#define NumberType 2
#define FunctionType 3
#define ListType 4
#define KeywordType 5
#define SubStringType 6
#define SymbolType 7
#define FnArityType 8
#define OpaqueType 9
#define BitmapIndexedType 10
#define ArrayNodeType 11
#define HashCollisionNodeType 12
Value *StringTypeNum = (Value *)&(Number){NumberType, -1, StringType};
Value *NumberTypeNum = (Value *)&(Number){NumberType, -1, NumberType};
Value *FunctionTypeNum = (Value *)&(Number){NumberType, -1, FunctionType};
Value *ListTypeNum = (Value *)&(Number){NumberType, -1, ListType};
Value *KeywordTypeNum = (Value *)&(Number){NumberType, -1, KeywordType};
Value *SubStrTypeNum = (Value *)&(Number){NumberType, -1, SubStringType};
Value *SymbolTypeNum = (Value *)&(Number){NumberType, -1, SymbolType};
Value *FnArityTypeNum = (Value *)&(Number){NumberType, -1, FnArityType};
Value *OpaqueTypeNum = (Value *)&(Number){NumberType, -1, OpaqueType};
Value *BitmapIndexedTypeNum = (Value *)&(Number){NumberType, -1, BitmapIndexedType};
Value *ArrayNodeTypeNum = (Value *)&(Number){NumberType, -1, ArrayNodeType};
Value *HashCollisionNodeTypeNum = (Value *)&(Number){NumberType, -1, HashCollisionNodeType};
")
(inline-text "
extern void abort();
extern void free(void *);
extern void *malloc(unsigned long);
typedef Value *(FnType0)(List *);
typedef Value *(FnType1)(List *, Value *);
typedef Value *(FnType2)(List *, Value *, Value *);
typedef Value *(FnType3)(List *, Value *, Value *, Value *);
typedef Value *(FnType4)(List *, Value *, Value *, Value *, Value *);
typedef Value *(FnType5)(List *, Value *, Value *, Value *, Value *, Value *);
typedef Value *(FnType6)(List *, Value *, Value *, Value *, Value *, Value *, Value *);
typedef Value *(FnType7)(List *, Value *, Value *, Value *, Value *, Value *, Value *, Value *);
typedef Value *(FnType8)(List *, Value *, Value *, Value *, Value *, Value *, Value *, Value *, Value *);
typedef Value *(FnType9)(List *, Value *, Value *, Value *, Value *, Value *, Value *, Value *, Value *, Value *);
List *empty_list = &(List){4,-1,0,0,0};
Number const0 = {NumberType, -1, 0};
Value *const0Ptr = (Value *)&const0;
FILE *outStream;
Number trueVal = {NumberType, -1, 1};
Value* true = (Value *)&trueVal;
Number falseVal = {NumberType, -1, 0};
Value* false = (Value *)&falseVal;
long long malloc_count = 0;
long long free_count = 0;
int mask(int64_t hash, int shift) {
return (hash >> shift) & 0x1f;
}
int bitpos(int64_t hash, int shift) {
return 1 << mask(hash, shift);
}
void incRef(Value *v) {
if (v == (Value *)0) {
fprintf(stderr, \"why are you incRefing 'null'\\n\");
abort();
} else if (v->refs < -1) {
fprintf(stderr, \"incRefing: %p\\n\", v);
abort();
} else if (v->refs >= 0)
v->refs++;
return;
}
")
(inline-text "
void decRef(Value *v) {
if (v == (Value *)0) {
fprintf(stderr, \"why are you decRefing 'null'\\n\");
abort();
} else if (v->refs < -1) {
fprintf(stderr, \"decRefing: %p\\n\", v);
} else if (v->refs == -1) {
return;
} else if (v->refs == 0) {
fprintf(stderr, \"decRef already at 0: %p\\n\", v);
return;
} else {
v->refs--;
return;
}
}
Value *my_malloc(int64_t sz) {
malloc_count++;
Value *val = malloc(sz);
if (sz > sizeof(Value))
val->refs = 1;
return(val);
}
typedef struct DirectLL {int64_t type; struct DirectLL *next;} DirectLL;
DirectLL *freeSubStrings = (DirectLL *)0;
SubString *malloc_substring() {
if (freeSubStrings == (DirectLL *)0) {
SubString *subStr = (SubString *)my_malloc(sizeof(SubString));
subStr->hash = (Number *)0;
return(subStr);
} else {
DirectLL *subStr = freeSubStrings;
freeSubStrings = subStr->next;
((SubString *)subStr)->refs = 1;
((SubString *)subStr)->hash = (Number *)0;
return((SubString *)subStr);
}
}
int recycledReified = 0;
DirectLL *freeReified[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
ReifiedVal *malloc_reified(int implCount) {
if (implCount > 19 || freeReified[implCount] == (DirectLL *)0) {
return((ReifiedVal *)my_malloc(sizeof(ReifiedVal) + sizeof(Function *) * implCount));
} else {
recycledReified++;
DirectLL *newReifiedVal = freeReified[implCount];
freeReified[implCount] = newReifiedVal->next;
((ReifiedVal *)newReifiedVal)->refs = 1;
((ReifiedVal *)newReifiedVal)->implCount = implCount;
((ReifiedVal *)newReifiedVal)->typeArgs = (Value *)0;
return((ReifiedVal *)newReifiedVal);
}
}
")
(inline-text "
int recycledFunction = 0;
DirectLL *freeFunctions[10] = {0,0,0,0,0,0,0,0,0,0};
Function *malloc_function(int arityCount) {
if (arityCount > 9 || freeFunctions[arityCount] == (DirectLL *)0) {
return((Function *)my_malloc(sizeof(Function) + sizeof(FnArity *) * arityCount));
} else {
recycledFunction++;
DirectLL *newFunction = freeFunctions[arityCount];
freeFunctions[arityCount] = newFunction->next;
((Function *)newFunction)->refs = 1;
return((Function *)newFunction);
}
}
DirectLL *freeNumbers = (DirectLL *)0;
Number *malloc_number() {
if (freeNumbers == (DirectLL *)0) {
malloc_count += 99;
Number *numberStructs = (Number *)my_malloc(sizeof(Number) * 100);
((DirectLL *)&numberStructs[99])->next = (DirectLL *)0;
for (int i = 99; i > 0; i--) {
((DirectLL *)&numberStructs[i])->next = freeNumbers;
freeNumbers = (DirectLL *)&numberStructs[i];
}
return(&numberStructs[0]);
} else {
DirectLL *newNumber = freeNumbers;
freeNumbers = newNumber->next;
((Number *)newNumber)->refs = 1;
return((Number *)newNumber);
}
}
DirectLL *freeLists = (DirectLL *)0;
List *malloc_list() {
if (freeLists == (DirectLL *)0) {
malloc_count += 499;
List *listStructs = (List *)my_malloc(sizeof(List) * 500);
for (int i = 499; i > 0; i--) {
((DirectLL *)&listStructs[i])->next = freeLists;
freeLists = (DirectLL *)&listStructs[i];
}
return(listStructs);
} else {
DirectLL *newList = freeLists;
freeLists = newList->next;
((List *)newList)->refs = 1;
return((List *)newList);
}
}
")
(inline-text "
DirectLL *freeFnArities = (DirectLL *)0;
FnArity *malloc_fnArity() {
if (freeFnArities == (DirectLL *)0) {
return((FnArity *)my_malloc(sizeof(FnArity)));
} else {
DirectLL *newFnArity = freeFnArities;
freeFnArities = newFnArity->next;
((FnArity *)newFnArity)->refs = 1;
return((FnArity *)newFnArity);
}
}
DirectLL *freeBMINodes[20] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
BitmapIndexedNode *malloc_bmiNode(int itemCount) {
int nodeSize = sizeof(BitmapIndexedNode) + sizeof(Value *) * (itemCount * 2);
BitmapIndexedNode *bmiNode;
if (freeBMINodes[itemCount] == (DirectLL *)0) {
bmiNode = (BitmapIndexedNode *)my_malloc(nodeSize);
} else {
bmiNode = (BitmapIndexedNode *)freeBMINodes[itemCount];
freeBMINodes[itemCount] = ((DirectLL *)bmiNode)->next;
}
memset(bmiNode, 0, nodeSize);
bmiNode->type = BitmapIndexedType;
bmiNode->refs = 1;
return(bmiNode);
}
HashCollisionNode *malloc_hashCollisionNode(int itemCount) {
if (itemCount > 30000) {
fprintf(stderr, \"Catastrophic failure: Too many hash collisions\\n\"); abort();
}
int nodeSize = sizeof(HashCollisionNode) + sizeof(Value *) * (itemCount * 2);
HashCollisionNode *collisionNode;
collisionNode = (HashCollisionNode *)my_malloc(nodeSize);
memset(collisionNode, 0, nodeSize);
collisionNode->type = HashCollisionNodeType;
collisionNode->count = itemCount * 2;
collisionNode->refs = 1;
return(collisionNode);
}
")
(inline-text "
DirectLL *freeArrayNodes = (DirectLL *)0;
ArrayNode *malloc_arrayNode() {
ArrayNode *arrayNode;
if (freeArrayNodes == (DirectLL *)0) {
arrayNode = (ArrayNode *)my_malloc(sizeof(ArrayNode));
} else {
arrayNode = (ArrayNode *)freeArrayNodes;
freeArrayNodes = ((DirectLL *)arrayNode)->next;
}
memset(arrayNode, 0, sizeof(ArrayNode));
arrayNode->type = ArrayNodeType;
arrayNode->refs = 1;
return(arrayNode);}
void dec_and_free(Value *v) {
if (v == (Value *)0) {
fprintf(stderr, \"why are you freeing 'null'\\n\");
abort();
} else if (v->refs == -10) {
fprintf(stderr, \"freeing already freed struct\\n\");
abort();
} else if (v->refs == -1) {
return;
} else if (v->refs > 1) {
v->refs--;
return;
} else if (v->type == 0) {
fprintf(stderr, \"freeing invalid type\\n\");
abort();
} else if (v->type == StringType) {
v->refs = -10;
free_count++;
free(v);
} else if (v->type == NumberType) {
v->refs = -10;
((DirectLL *)v)->next = freeNumbers;
freeNumbers = (DirectLL *)v;
} else if (v->type == FunctionType) {
Function *f = (Function *)v;
for (int i = 0; i < f->arityCount; i++) {
dec_and_free((Value *)f->arities[i]);
}
v->refs = -10;
if (f->arityCount < 10) {
DirectLL *freedList = freeFunctions[f->arityCount];
freeFunctions[f->arityCount] = (DirectLL *)v;
((DirectLL *)v)->next = freedList;
} else {
free_count++;
free(v);
}
} else if (v->type == KeywordType ||
v->type == SubStringType ||
v->type == SymbolType) {
Value *src = ((SubString *)v)->source;
Number *hash = ((SubString *)v)->hash;
v->refs = -10;
if (src != (Value *)0) {
dec_and_free(src);
}
if (hash != (Number *)0) {
dec_and_free((Value *)hash);
}
((DirectLL *)v)->next = freeSubStrings;
freeSubStrings = (DirectLL *)v;
} else if (v->type == FnArityType) {
FnArity *arity = (FnArity *)v;
dec_and_free((Value *)arity->closures);
v->refs = -10;
((DirectLL *)v)->next = freeFnArities;
freeFnArities = (DirectLL *)v;
} else if (v->type == OpaqueType) {
v->refs = -10;
} else if (v->type == BitmapIndexedType) {
// fprintf(stderr, \"%p free bmi node\\n\", v);
BitmapIndexedNode *node = (BitmapIndexedNode *)v;
int cnt = __builtin_popcount(node->bitmap);
for (int i = 0; i < (2 * cnt); i++) {
if (node->array[i] != (Value *)0) {
dec_and_free(node->array[i]);
}
}
v->refs = -10;
((DirectLL *)v)->next = freeBMINodes[cnt];
freeBMINodes[cnt] = (DirectLL *)v;
} else if (v->type == ArrayNodeType) {
ArrayNode *node = (ArrayNode *)v;
for (int i = 0; i < 32; i++) {
if (node->array[i] != (Value *)0) {
dec_and_free(node->array[i]);
}
}
v->refs = -10;
((DirectLL *)v)->next = freeArrayNodes;
freeArrayNodes = (DirectLL *)v;
} else if (v->type == HashCollisionNodeType) {
HashCollisionNode *node = (HashCollisionNode *)v;
for (int i = 0; i < node->count; i++) {
if (node->array[i] != (Value *)0) {
dec_and_free(node->array[i]);
}
}
v->refs = -10;
free_count++;
free(v);
} else if (v->type == ListType) {
Value *head = ((List *)v)->head;
List *tail = ((List *)v)->tail;
v->refs = -10;
if (head != (Value *)0) {
dec_and_free(head);
}
if (tail != (List *)0) {
dec_and_free((Value *)tail);
}
((DirectLL *)v)->next = freeLists;
freeLists = (DirectLL *)v;
} else {
ReifiedVal *rv = (ReifiedVal *)v;
for (int i = 0; i < rv->implCount; i++) {
dec_and_free(rv->impls[i]);
}
if (rv->typeArgs != (Value *)0)
dec_and_free(rv->typeArgs);
v->refs = -10;
if (rv->implCount < 20) {
DirectLL *freedList = freeReified[rv->implCount];
freeReified[rv->implCount] = (DirectLL *)v;
((DirectLL *)v)->next = freedList;
} else {
free_count++;
free(v);
}
}
// fprintf(stderr, \"malloc_count: %lld free_count: %lld\\r\", malloc_count, free_count);
};
")
(inline-text "
char *extractStr(Value *v) {
String *newStr = (String *)my_malloc(sizeof(String) + ((String *)v)->len + 5);
if (v->type == StringType)
snprintf(newStr->buffer, ((String *)v)->len + 1, \"%s\", ((String *)v)->buffer);
else if (v->type == SubStringType)
snprintf(newStr->buffer, ((String *)v)->len + 1, \"%s\", ((SubString *)v)->buffer);
else {
fprintf(stderr, \"\\ninvalid type for 'extractStr'\\n\");
abort();
}
return(newStr->buffer);
}
Value *findProtoImpl(int64_t type, ProtoImpls *impls) {
int64_t implIndex = 0;
Value *defaultImpl = (Value *)0;
while(implIndex < impls->implCount) {
if (impls->impls[implIndex].type == 0) {
defaultImpl = impls->impls[implIndex].implFn;
}
if (type != impls->impls[implIndex].type) {
implIndex++;
} else
return(impls->impls[implIndex].implFn);
}
return(defaultImpl);
};
FnArity *findFnArity(Value *fnVal, int64_t argCount) {
Function *fn = (Function *)fnVal;
int arityIndex = 0;
FnArity *arity = (FnArity *)fn->arities[arityIndex];
FnArity *variadic = (FnArity *)0;
while(arityIndex < fn->arityCount) {
arity = (FnArity *)fn->arities[arityIndex];
if (arity->variadic) {
variadic = arity;
arityIndex++;
} else if (arity->count != argCount) {
arityIndex++;
} else
return(arity);
}
return(variadic);
};
")
(inline-text "
Value *stringValue(char *s) {
int64_t len = strlen(s);
String *strVal = (String *)my_malloc(sizeof(String) + len + 4);
strVal->type = StringType;
strVal->len = strlen(s);
strncpy(strVal->buffer, s, len);
return((Value *)strVal);
};
Value *symbolValue(char *s) {
SubString *sym = malloc_substring();
sym->type = SymbolType;
sym->buffer = s;
sym->len = strlen(s);
sym->source = (Value *)0;
sym->hash = (Number *)0;
return((Value *)sym);
};
Value *keywordValue(char *s) {
SubString *kw = malloc_substring();
kw->type = KeywordType;
kw->buffer = s;
kw->hash = (Number *)0;
kw->len = strlen(s);
kw->source = (Value *)0;
return((Value *)kw);
};
Value *makeSubstr(int64_t len, Value *str, char *subsStart) {
SubString *subStr = malloc_substring();
subStr->type = SubStringType;
subStr->len = len;
subStr->source = str;
subStr->hash = (Number *)0;
incRef(str);
subStr->buffer = subsStart;
return((Value *)subStr);}
Value *numberValue(int64_t n) {
Number *numVal = malloc_number();
numVal->type = NumberType;
numVal->numVal = n;
return((Value *)numVal);
};
List *listCons(Value *x, List *l) {
if (l->type != ListType) {
fprintf(stderr, \"'cons' requires a list\\n\");
abort();
}
List *newList = malloc_list();
newList->type = ListType;
newList->len = l->len + 1;
newList->head = (Value *)x;
newList->tail = l;
return(newList);
};
")
(inline-text "
int64_t nothing_type;
Value *maybe1;
Value *nothing;
Value *(*maybe)(List *, Value*, Value *);
Value *(*equal_star)(List *closures, Value *, Value *);
int equal(Value *v1, Value *v2) {
Value *equals = equal_star((List *)0, v1, v2);
int64_t equalsType = equals->type;
dec_and_free(equals);
return(equalsType != nothing_type);
}
Value *(*assoc)(List *closures, Value *, Value *, Value *, Value *, Value *);
Value *(*get)(List *, Value *, Value *, Value *, Value *, Value *);
Value *(*sha1)(List *, Value *);
Value *(*hashSeq)(List *, Value* n, Value *s);
Value *(*count)(List *, Value* n);
Value *(*invoke1Arg)(List *closures, Value *f, Value* arg);
Value *(*type_name)(List *closures, Value *t);
// SHA1 implementation courtesy of: Steve Reid <sreid@sea-to-sky.net>
// and others.
// from http://waterjuice.org/c-source-code-for-sha1/
#include <stdint.h>
#include <memory.h>
#include <pthread.h>
typedef struct
{
uint32_t State[5];
uint32_t Count[2];
uint8_t Buffer[64];
} Sha1Context;
#define SHA1_HASH_SIZE ( 64 / 8 )
typedef struct
{
uint8_t bytes [SHA1_HASH_SIZE];
} SHA1_HASH;
typedef union
{
uint8_t c [64];
uint32_t l [16];
} CHAR64LONG16;
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
#define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) |(rol(block->l[i],8)&0x00FF00FF))
#define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] ^block->l[(i+2)&15]^block->l[i&15],1))
#define R0(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk0(i)+ 0x5A827999 + rol(v,5); w=rol(w,30);
#define R1(v,w,x,y,z,i) z += ((w&(x^y))^y) + blk(i) + 0x5A827999 + rol(v,5); w=rol(w,30);
#define R2(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0x6ED9EBA1 + rol(v,5); w=rol(w,30);
#define R3(v,w,x,y,z,i) z += (((w|x)&y)|(w&x)) + blk(i) + 0x8F1BBCDC + rol(v,5); w=rol(w,30);
#define R4(v,w,x,y,z,i) z += (w^x^y) + blk(i) + 0xCA62C1D6 + rol(v,5); w=rol(w,30);
")
(inline-text "
static void TransformFunction(uint32_t state[5], const uint8_t buffer[64]) {
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
uint32_t e;
uint8_t workspace[64];
CHAR64LONG16* block = (CHAR64LONG16*) workspace;
memcpy( block, buffer, 64 );
// Copy context->state[] to working vars
a = state[0];
b = state[1];
c = state[2];
d = state[3];
e = state[4];
// 4 rounds of 20 operations each. Loop unrolled.
R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
// Add the working vars back into context.state[]
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
state[4] += e;
}
void Sha1Initialise (Sha1Context* Context) {
// SHA1 initialization constants
Context->State[0] = 0x67452301;
Context->State[1] = 0xEFCDAB89;
Context->State[2] = 0x98BADCFE;
Context->State[3] = 0x10325476;
Context->State[4] = 0xC3D2E1F0;
Context->Count[0] = 0;
Context->Count[1] = 0;
}
")
(inline-text "
void Sha1Update (Sha1Context* Context, void* Buffer, int64_t BufferSize) {
uint32_t i;
uint32_t j;
j = (Context->Count[0] >> 3) & 63;
if( (Context->Count[0] += BufferSize << 3) < (BufferSize << 3) )
{
Context->Count[1]++;
}
Context->Count[1] += (BufferSize >> 29);
if( (j + BufferSize) > 63 )
{
i = 64 - j;
memcpy( &Context->Buffer[j], Buffer, i );
TransformFunction(Context->State, Context->Buffer);
for( ; i + 63 < BufferSize; i += 64 )
{
TransformFunction(Context->State, (uint8_t*)Buffer + i);
}
j = 0;
}
else
{
i = 0;
}
memcpy( &Context->Buffer[j], &((uint8_t*)Buffer)[i], BufferSize - i );
}
void Sha1Finalise (Sha1Context* Context, SHA1_HASH* Digest) {
uint32_t i;
uint8_t finalcount[8];
for( i=0; i<8; i++ )
{
finalcount[i] = (unsigned char)((Context->Count[(i >= 4 ? 0 : 1)]
>> ((3-(i & 3)) * 8) ) & 255); // Endian independent
}
Sha1Update( Context, (uint8_t*)\"\\x80\", 1 );
while( (Context->Count[0] & 504) != 448 )
{
Sha1Update( Context, (uint8_t*)\"\\0\", 1 );
}
Sha1Update( Context, finalcount, 8 ); // Should cause a Sha1TransformFunction()
for( i=0; i<SHA1_HASH_SIZE; i++ )
{
Digest->bytes[i] = (uint8_t)((Context->State[i>>2] >> ((3-(i & 3)) * 8) ) & 255);
}
}
")
(inline-text "
// Immutable hash-map ported from Clojure
BitmapIndexedNode emptyBMI = {BitmapIndexedType, -1, 0, 0};
BitmapIndexedNode *clone_BitmapIndexedNode(BitmapIndexedNode *node, int idx,
Value *key, Value* val)
{
int itemCount = __builtin_popcount(node->bitmap);
BitmapIndexedNode *newNode = malloc_bmiNode(itemCount);
newNode->type = BitmapIndexedType;
newNode->refs = 1;
newNode->bitmap = node->bitmap;
for (int i = 0; i < itemCount; i++) {
if (i == idx) {
newNode->array[i * 2] = key;
newNode->array[i * 2 + 1] = val;
} else {
if (node->array[i * 2] != (Value *)0)
incRef(node->array[i * 2]);
if (node->array[i * 2 + 1] != (Value *)0)
incRef(node->array[i * 2 + 1]);
newNode->array[i * 2] = node->array[i * 2];
newNode->array[i * 2 + 1] = node->array[i * 2 + 1];
}
}
return(newNode);
}
Value *createNode(int shift,
int64_t key1hash, Value *key1, Value *val1,
int64_t key2hash, Value *key2, Value *val2)
{
if (shift > 60) {
fprintf(stderr, \"Ran out of shift!!!!!!\");
abort();
}
BitmapIndexedNode *newNode = malloc_bmiNode(2);
newNode->type = BitmapIndexedType;
newNode->refs = 1;
int key1bit = bitpos(key1hash, shift);
int key2bit = bitpos(key2hash, shift);
newNode->bitmap = key1bit | key2bit;
int key1idx = __builtin_popcount(newNode->bitmap & (key1bit - 1));
int key2idx = __builtin_popcount(newNode->bitmap & (key2bit - 1));
if (key1bit == key2bit) {
newNode->array[0] = (Value *)0;
newNode->array [1] = createNode(shift + 5, key1hash, key1, val1,
key2hash, key2, val2);
} else {
incRef(key1);
incRef(val1);
incRef(key2);
incRef(val2);
newNode->array[key1idx * 2] = key1;
newNode->array[key1idx * 2 + 1] = val1;
newNode->array[key2idx * 2] = key2;
newNode->array[key2idx * 2 + 1] = val2;
}
return((Value *)newNode);
}
")
(inline-text "
Value *proto1Arg(ProtoImpls *protoImpls, char *name, Value *arg0) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType1 *_fn = (FnType1 *)_arity->fn;
return(_fn(_arity->closures, arg0));
}
Value *proto2Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType2 *_fn = (FnType2 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1));
}
Value *proto3Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1, Value *arg2) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType3 *_fn = (FnType3 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2));
}
Value *proto4Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1,
Value *arg2, Value *arg3) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType4 *_fn = (FnType4 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2, arg3));
}
Value *proto5Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1,
Value *arg2, Value *arg3, Value *arg4) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType5 *_fn = (FnType5 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2, arg3, arg4));
}
Value *proto6Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1,
Value *arg2, Value *arg3, Value *arg4, Value *arg5) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType6 *_fn = (FnType6 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2, arg3, arg4, arg5));
}
Value *proto7Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1, Value *arg2,
Value *arg3, Value *arg4, Value *arg5, Value *arg6) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType7 *_fn = (FnType7 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2, arg3, arg4, arg5, arg6));
}
Value *proto8Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1, Value *arg2,
Value *arg3, Value *arg4, Value *arg5, Value *arg6, Value *arg7) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType8 *_fn = (FnType8 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7));
}
Value *proto9Arg(ProtoImpls *protoImpls, char *name, Value *arg0, Value *arg1, Value *arg2,
Value *arg3, Value *arg4, Value *arg5, Value *arg6, Value *arg7, Value *arg8) {
FnArity *_arity = (FnArity *)findProtoImpl(arg0->type, protoImpls);
if(_arity == (FnArity *)0) {
fprintf(stderr, \"\\n*** Could not find impl of '%s' for type: %s\\n\",
name, extractStr(type_name(empty_list, arg0)));
abort();
}
FnType9 *_fn = (FnType9 *)_arity->fn;
return(_fn(_arity->closures, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8));
}
")
(defn output-to-file [file-path]
(inline-text Number
"String *arg0Str = (String *)my_malloc(sizeof(String) + ((String *)arg0)->len + 5);
arg0Str->type = StringType;
if (arg0->type == StringType)
snprintf(arg0Str->buffer, ((String *)arg0)->len + 1, \"%s\", ((String *)arg0)->buffer);
else if (arg0->type == SubStringType)
snprintf(arg0Str->buffer, ((String *)arg0)->len + 1, \"%s\", ((SubString *)arg0)->buffer);
else {
fprintf(stderr, \"\\ninvalid type for 'output-to-file'\\n\");
abort();
}
outStream = fopen(arg0Str->buffer, \"w\");
dec_and_free((Value *)arg0Str);
return(true);\n"))
(defn standard-output []
(inline-text Number
"outStream = stdout;
return(true);\n"))
(defn symkey-name [v]
(inline-text String
"return(stringValue(((SubString *)arg0)->buffer));\n"))
(defn char-code [c]
(inline-text Number
"if (arg0->type == StringType) {
String *s = (String *)arg0;
return(numberValue((int)s->buffer[0]));
} else if (arg0->type == SubStringType) {
SubString *s = (SubString *)arg0;
return(numberValue((int)s->buffer[0]));
} else
abort();\n "))
(defn symbol [sym-str]
(inline-text Symbol
"if (arg0->type == StringType) {
String *s = (String *)arg0;
SubString *subStr = malloc_substring();
subStr->type = SymbolType;
subStr->len = s->len;
subStr->source = arg0;
subStr->hash = (Number *)0;
incRef(arg0);
subStr->buffer = s->buffer;
return((Value *)subStr);
} else if (arg0->type == SubStringType) {
SubString *s = (SubString *)arg0;
SubString *subStr = malloc_substring();
subStr->type = SymbolType;
subStr->len = s->len;
subStr->source = arg0;
subStr->hash = (Number *)0;
incRef(arg0);
subStr->buffer = s->buffer;
return((Value *)subStr);
} else if (arg0->type == SymbolType) {
return(arg0);
}
abort();\n"))
(defn new-keyword [kw-str]
(inline-text Keyword
"if (arg0->type == StringType) {
String *s = (String *)arg0;
SubString *subStr = malloc_substring();
subStr->type = KeywordType;
subStr->len = s->len;
subStr->source = arg0;
subStr->hash = (Number *)0;
incRef(arg0);
subStr->buffer = s->buffer;
return((Value *)subStr);
} else if (arg0->type == SubStringType) {
SubString *s = (SubString *)arg0;
SubString *subStr = malloc_substring();
subStr->type = KeywordType;
subStr->len = s->len;
subStr->source = arg0;
subStr->hash = (Number *)0;
incRef(arg0);
subStr->buffer = s->buffer;
return((Value *)subStr);
} else
abort();\n"))
(defn abort []
(inline-text Number
"abort();
return(true);\n"))
(defn get-type [value]
(inline-text Number
"return(numberValue(arg0->type));"))
(defn type= [x y]
(inline-text
"if (arg0->type == arg1->type)
return(maybe1);
else
return(nothing);\n"))
(defn pr* [str]
(inline-text Number
"if (arg0->type == StringType)
fprintf(outStream, \"%-.*s\", (int)((String *)arg0)->len, ((String *)arg0)->buffer);
else if (arg0->type == SubStringType)
fprintf(outStream, \"%-.*s\", (int)((SubString *)arg0)->len, ((SubString *)arg0)->buffer);
else {
fprintf(outStream, \"\\ninvalid type for 'pr*'\\n\");
abort();
}
return(true);\n"))
(defn subs
([src index]
(inline-text SubString
"int64_t idx = ((Number *)arg1)->numVal;
if (arg0->type == StringType) {
String *s = (String *)arg0;
SubString *subStr = malloc_substring();
subStr->type = SubStringType;
if (idx < s->len) {
subStr->len = s->len - idx;
subStr->source = arg0;
incRef(arg0);
subStr->buffer = s->buffer + idx;
}
else {
subStr->len = 0;
subStr->source = (Value *)0;
subStr->buffer = (char *)0;
}
return((Value *)subStr);
} else if (arg0->type == SubStringType) {
SubString *s = (SubString *)arg0;
SubString *subStr = malloc_substring();
subStr->type = SubStringType;
if (idx < s->len) {
subStr->len = s->len - idx;