forked from dllaurence/Nil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp.llm
1086 lines (905 loc) · 30.7 KB
/
exp.llm
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
;**********************************************************************
; exp.llm
;
; Expression implementation--the heart of any lisp.
;
; FIXME: most of these routines are general implementations that
; sanity-check their arguments. But when used internally the calling
; code often knows that the checks are not necessary. Those checks
; could someday be optimized away by creating unchecked versions for
; internal use, since both the parser and the string uniqueifier always
; know the run-time type of their objects.
;
; Also, we might be able to save a comparison or two by having a separate
; internal tag for nil and/or tricky choices for the bit patterns of the
; tag values (testing a subfield gives us a test for a set of types and
; if arranged properly could replace a common series of tests for
; membership in one of several types that will be treated the same). The
; most likely place this would be useful would be having a single-compare
; test for both "atom, including nil" and "atom but not nil."
;
; There are interesting bitwise intrinsics (such as the count of the
; number of trailing zeroes in an int) which might be usable to make the
; tag manipulations go faster. I may have relied too heavily on what
; is available from C when I wrote the tag code.
;
; Copyright 2009-2010 by Dustin Laurence. Distributed under the terms of
; the LGPLv3.
;
;**********************************************************************
#include "exp.llh"
#include "nil.llh"
#include "memory.llh"
;**********************************************************************
; Private declarations
;
;**********************************************************************
declare NILCC void @PrintAtom(%Exp %exp)
declare NILCC void @PrintCDR(%Exp %exp)
declare NILCC void @PrintPrimitive(%Exp %exp)
declare NILCC void @PrintException(%Exp %exp)
;**********************************************************************
; Tag manipulation functions
;
; These functions manipulate the low-order bits as a tag field. They
; are implemented this way instead of as a structure with bitfields
; because these are independent of the actual size of %Word and thus
; work without modification on any architecture.
;
; Of course it is inefficient to pay for function calls, but inlining
; them would fix that without duplicating source and is thus the Right
; Thing.
;
;**********************************************************************
;**********************************************************************
; GetTag
;
; Returns a small integer whose value is the expression type tag.
;
;**********************************************************************
define NILCC %Tag
@GetTag(%Exp %exp)
{
%tag = trunc %Exp %exp to %Tag
ret %Tag %tag
}
;**********************************************************************
; TagIs
;
; Returns true if the expression has the given tag.
;
;**********************************************************************
define NILCC i1
@TagIs(%Exp %exp, %Tag %tag)
{
%expTag = call NILCC %Tag @GetTag(%Exp %exp)
%sameTag = icmp eq %Tag %expTag, %tag
ret i1 %sameTag
}
;**********************************************************************
; TagIsNot
;
; Returns true if the expression has the given tag.
;
;**********************************************************************
define NILCC i1
@TagIsNot(%Exp %exp, %Tag %tag)
{
%expTag = call NILCC %Tag @GetTag(%Exp %exp)
%differentTag = icmp ne %Tag %expTag, %tag
ret i1 %differentTag
}
;**********************************************************************
; SameTag
;
; Returns true if the expressions have the same tag
;
;**********************************************************************
define NILCC i1
@SameTag(%Exp %lhs, %Exp %rhs)
{
%lhsTag = call NILCC %Tag @GetTag(%Exp %lhs)
%rhsTag = call NILCC %Tag @GetTag(%Exp %rhs)
%sameTag = icmp eq %Tag %lhsTag, %rhsTag
ret i1 %sameTag
}
;**********************************************************************
; ClearTag
;
; Returns an expression with the same value as %exp (points to the same
; object or contains the same embedded number), but with the type tag
; cleared (so, for example, it is a valid pointer again).
;
;**********************************************************************
define NILCC %Exp
@ClearTag(%Exp %exp)
{
; Size-independent way to express the mask we need
%mask = xor %Exp MAX_TAG, -1 ; %mask = "bitwise not" MAX_TAG
%clearedExp = and %Exp %exp, %mask
#ifndef NDEBUG
%tag = call NILCC %Tag @GetTag(%Exp %clearedExp)
%isZero = icmp eq %Tag %tag, 0
assert(%isZero)
#endif
ret %Exp %clearedExp
}
;**********************************************************************
; AddTag
;
; Returns an expression with the same value as %exp (points to the same
; object or contains the same embedded number), but with the type tag
; set to %tag.
;
; The difference betweeen this and SetTag is that this assumes the tag
; field is already clear, and will produce chaos if it is not.
; (Precisely, the new tag will be xord with the old one, which is
; not a behavior anyone should be wanting.) In debug mode this error
; is detected, however.
;
;**********************************************************************
define NILCC %Exp
@AddTag(%Exp %exp, %Tag %tag)
{
%tagExp = zext %Tag %tag to %Exp
%newExp = xor %Exp %exp, %tagExp
#ifndef NDEBUG
%newTag = call NILCC %Tag @GetTag(%Exp %newExp)
%same = icmp eq %Tag %tag, %newTag
assert(%same)
#endif
ret %Exp %newExp
}
;**********************************************************************
; SetTag
;
; Returns an expression with the same value as %exp (points to the same
; object or contains the same embedded number), but with the type tag
; set to %tag.
;
;**********************************************************************
define NILCC %Exp
@SetTag(%Exp %exp, %Tag %tag)
{
%clearedExp = call NILCC %Exp @ClearTag(%Exp %exp)
%newExp = call NILCC %Exp @AddTag(%Exp %clearedExp, %Tag %tag)
ret %Exp %newExp
}
;**********************************************************************
; TagIsClear
;
; Checks to see that the given Exp has a zero tag field (and is
; therefore convertible to a valid pointer).
;
; Same semantics as Aligned(), but acts on Exps and not on pointers.
;
;**********************************************************************
define NILCC i1
@TagIsClear(%Exp %exp)
{
%tag = call NILCC %Tag @GetTag(%Exp %exp)
%isZero = icmp eq %Tag %tag, 0
ret i1 %isZero
}
;**********************************************************************
; ShiftOutTag
;
; This and its opposite are one-instruction functions, but still useful
; for abstraction and documentation. Inlining would be the right way
; to optimize.
;
;**********************************************************************
define NILCC %Word
@ShiftOutTag(%Exp %exp)
{
%value = lshr %Exp %exp, TAG_BITS
ret %Word %value
}
;**********************************************************************
; SignedShiftOutTag
;
; Same as ShiftOutTag, but with a sign-preserving shift.
;
;**********************************************************************
define NILCC %Word
@SignedShiftOutTag(%Exp %exp)
{
%value = ashr %Exp %exp, TAG_BITS
ret %Word %value
}
;**********************************************************************
; ShiftInTag
;
; Warning: this low-level function leaves a blank tag field to be
; filled in.
;
;**********************************************************************
define NILCC %Exp
@ShiftInTag(%Word %word)
{
%exp = shl %Word %word, TAG_BITS
ret %Exp %exp
}
;**********************************************************************
; Conversions
;
;**********************************************************************
;**********************************************************************
; str2Exp
;
; Converts the char* pointer to an Expression with the correct tag.
; THIS IS ONLY A CONVERSION FUNCTION--use NewSymbol to get a unique
; string.
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %Exp
@str2Exp(%c_char* %str)
{
; FIXME: what if %str is null? This is almost certainly the wrong
; behavior but what is the correct lisp semantics?
; need to be careful about the precise meaning of nil in lisp.
; For now, assert that it isn't so it can't silently do The Wrong
; Thing
%notNull = icmp ne %c_char* %str, null
assert(%notNull)
#ifndef NDEBUG
; Also it obviously must be aligned
%voidPtr = bitcast %c_char* %str to i8* ; should never be necessary
%aligned = call NILCC i1 @Aligned(i8* %voidPtr)
assert(%aligned)
#endif
%untaggedExp = ptrtoint %c_char* %str to %Exp
%exp = call NILCC %Exp @AddTag(%Exp %untaggedExp, %Tag SYMBOL_TAG)
ret %Exp %exp
}
;**********************************************************************
; Exp2str
;
; Converts a Symbol expression back to a char*. IT RETURNS A POINTER
; TO THE CANONICAL STRING OBJECT, so do *not* modify it!
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %c_char*
@Exp2str(%Exp %exp)
{
#ifndef NDEBUG
%isSymbol = call NILCC i1 @IsSymbol(%Exp %exp)
assert(%isSymbol)
#endif
%untaggedExp = call NILCC %Exp @ClearTag(%Exp %exp)
%str = inttoptr %Exp %untaggedExp to %c_char*
ret %c_char* %str
}
;**********************************************************************
; nil_cell2Exp
;
; Converts the nil_cell* pointer to an Expression with the correct
; tag. THIS IS ONLY A CONVERSION FUNCTION and has no idea if the
; pointer actually points to anything sane.
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %Exp
@nil_cell2Exp(%nil_cell* %cell)
{
; FIXME: what if %cell is null? This is almost certainly the wrong
; behavior but what is the correct lisp semantics?
; need to be careful about the precise meaning of nil in lisp.
; For now, assert that it isn't so it can't silently do The Wrong
; Thing
%notNull = icmp ne %nil_cell* %cell, null
assert(%notNull)
#ifndef NDEBUG
; Also it obviously must be aligned
%voidPtr = bitcast %nil_cell* %cell to i8*
%aligned = call NILCC i1 @Aligned(i8* %voidPtr)
assert(%aligned)
#endif
%untaggedExp = ptrtoint %nil_cell* %cell to %Exp
%exp = call NILCC %Exp @AddTag(%Exp %untaggedExp, %Tag CELL_TAG)
ret %Exp %exp
}
;**********************************************************************
; Exp2nil_cell
;
; Converts an Expression to a cell pointer.
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %nil_cell*
@Exp2nil_cell(%Exp %exp)
{
#ifndef NDEBUG
%isCell = call NILCC i1 @IsCell(%Exp %exp)
assert(%isCell)
#endif
%untaggedExp = call NILCC %Exp @ClearTag(%Exp %exp)
%cell = inttoptr %Exp %untaggedExp to %nil_cell*
ret %nil_cell* %cell
}
;**********************************************************************
; nil_primitive2Exp
;
; Converts the nil_primitive* pointer to an Expression with the correct
; tag. THIS IS ONLY A CONVERSION FUNCTION and has no idea if the
; pointer actually points to anything sane.
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %Exp
@nil_primitive2Exp(%nil_primitive* %prim)
{
; FIXME: what if %prim is null? This is almost certainly the wrong
; behavior but what is the correct lisp semantics?
; need to be careful about the precise meaning of nil in lisp.
; For now, assert that it isn't so it can't silently do The Wrong
; Thing
%notNull = icmp ne %nil_primitive* %prim, null
assert(%notNull)
#ifndef NDEBUG
; Also it obviously must be aligned
%voidPtr = bitcast %nil_primitive* %prim to i8*
%aligned = call NILCC i1 @Aligned(i8* %voidPtr)
assert(%aligned)
#endif
%untaggedExp = ptrtoint %nil_primitive* %prim to %Exp
%exp = call NILCC %Exp @AddTag(%Exp %untaggedExp, %Tag PRIMITIVE_TAG)
ret %Exp %exp
}
;**********************************************************************
; Exp2nil_primitive
;
; Converts an Expression to a cell pointer.
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %nil_primitive*
@Exp2nil_primitive(%Exp %exp)
{
#ifndef NDEBUG
%isPrimitive = call NILCC i1 @IsPrimitive(%Exp %exp)
assert(%isPrimitive)
#endif
%untaggedExp = call NILCC %Exp @ClearTag(%Exp %exp)
%prim = inttoptr %Exp %untaggedExp to %nil_primitive*
ret %nil_primitive* %prim
}
;**********************************************************************
; Exception2Exp
;
; Converts an exception code to an Expression with the correct tag.
;
;**********************************************************************
#ifndef NDEBUG
@except_guard = private global i1 1
#endif
define NILCC %Exp
@Exception2Exp(%Word %code)
{
#ifndef NDEBUG
; Detect overflow
%maxCode = call NILCC %Word @ShiftOutTag(%Exp -1)
%inBounds = icmp ule %Word %code, %maxCode
assert(%inBounds)
#endif
%untaggedExp = call NILCC %Exp @ShiftInTag(%Word %code)
%exp = call NILCC %Exp @AddTag(%Exp %untaggedExp, %Tag EXCEPTION_TAG)
#ifndef NDEBUG
%checkConversion = load i1* @except_guard
br i1 %checkConversion, label %Check, label %NoCheck
Check:
store i1 0, i1* @except_guard
%newCode = call NILCC %Word @Exp2Exception(%Exp %exp)
store i1 1, i1* @except_guard
%equal = icmp eq %Word %code, %newCode
assert(%equal)
br label %NoCheck
NoCheck:
#endif
ret %Exp %exp
}
;**********************************************************************
; Exp2Exception
;
; Extracts an exception code from an exception expression.
;
; FIXME: should obviously be inlined along with the above.
;
;**********************************************************************
define NILCC %Word
@Exp2Exception(%Exp %exp)
{
%result = call NILCC %Word @ShiftOutTag(%Exp %exp)
#ifndef NDEBUG
%checkConversion = load i1* @except_guard
br i1 %checkConversion, label %Check, label %NoCheck
Check:
store i1 0, i1* @except_guard
%newExp = call NILCC %Exp @Exception2Exp(%Word %result)
store i1 1, i1* @except_guard
%equal = icmp eq %Exp %exp, %newExp
assert(%equal)
br label %NoCheck
NoCheck:
#endif
ret %Word %result
}
;**********************************************************************
; The Seven Operators
;
; These are supposed to be a minimal set of primitive lisp operators
; for an implementation of lisp *in lisp*. Many will be implemented
; in terms of another form more useful internally.
;
;**********************************************************************
; #2 -- atom
; Internal form
define NILCC i1
@IsAtom(%Exp %exp)
{
; Implemented in a way that allows expansion to other types of
; atoms besides symbols
%atomp = call NILCC i1 @TagIsNot(%Exp %exp, %Tag CELL_TAG)
ret i1 %atomp
}
; #3 -- eq
; Internal form
define NILCC i1
@Eq(%Exp %lhs, %Exp %rhs)
{
; Is this Graham's eq? It will return t for lists that point to the
; same cell, and that doesn't fit his description. OTOH this is
; extremely fast, which is one reason we wanted unique strings. My
; impression is that some of these properties actually were
; implementation-driven, and this is the fastest implementation. There
; are also hints in "The Art of the Interpreter" that I'm right, and
; in my Common Lisp book (where they call it EQL)
%areEq = icmp eq %Exp %lhs, %rhs
ret i1 %areEq
}
define NILCC i1
@NotEq(%Exp %lhs, %Exp %rhs)
{
%areEq = icmp ne %Exp %lhs, %rhs
ret i1 %areEq
}
; #4 -- car
define NILCC %Exp
@Car(%Exp %exp)
{
#ifndef NDEBUG
%cellp = call NILCC i1 @IsCell(%Exp %exp)
assert(%cellp)
#endif
%cellPtr = call NILCC %nil_cell* @Exp2nil_cell(%Exp %exp)
%carPtr = getelementptr %nil_cell* %cellPtr, i32 0, i32 0
%car = load %Exp* %carPtr
ret %Exp %car
}
; #5 -- cdr
define NILCC %Exp
@Cdr(%Exp %exp)
{
#ifndef NDEBUG
%cellp = call NILCC i1 @IsCell(%Exp %exp)
assert(%cellp)
#endif
%cellPtr = call NILCC %nil_cell* @Exp2nil_cell(%Exp %exp)
%cdrPtr = getelementptr %nil_cell* %cellPtr, i32 0, i32 1
%cdr = load %Exp* %cdrPtr
ret %Exp %cdr
}
; #6 -- cons
define NILCC %Exp
@Cons(%Exp %car, %Exp %cdr)
{
%newCell = call NILCC %nil_cell* @NewCell()
; Set the car
%carPtr = getelementptr %nil_cell* %newCell, i32 0, i32 0
store %Exp %car, %Exp* %carPtr
; Set the cdr
%cdrPtr = getelementptr %nil_cell* %newCell, i32 0, i32 1
store %Exp %cdr, %Exp* %cdrPtr
%newExp = call NILCC %Exp @nil_cell2Exp(%nil_cell* %newCell)
ret %Exp %newExp
}
;**********************************************************************
; IsSymbol
;
; Returns true iff the expression is a (possibly nil) symbol.
;
;**********************************************************************
define NILCC i1
@IsSymbol(%Exp %exp)
{
%symbolp = call NILCC i1 @TagIs(%Exp %exp, %Tag SYMBOL_TAG)
ret i1 %symbolp
}
;**********************************************************************
; IsCell
;
; Returns true iff the expression is a (non-nil) cell.
;
;**********************************************************************
define NILCC i1
@IsCell(%Exp %exp)
{
%cellp = call NILCC i1 @TagIs(%Exp %exp, %Tag CELL_TAG)
ret i1 %cellp
}
;**********************************************************************
; IsList
;
; Returns true iff the expression is a cell or nil.
;
; FIXME: Are there places where having this test earlier would have
; simplified the code?
;
;**********************************************************************
define NILCC i1
@IsList(%Exp %exp)
{
%cellp = call NILCC i1 @IsCell(%Exp %exp)
br i1 %cellp, label %True, label %NotCell
NotCell:
%isNill = call NILCC i1 @IsNil(%Exp %exp)
br i1 %isNill, label %True, label %False
False:
ret i1 false
True:
ret i1 true
}
;**********************************************************************
; IsPrimitive
;
; Returns true iff the expression is a primitive.
;
;**********************************************************************
define NILCC i1
@IsPrimitive(%Exp %exp)
{
%primitivep = call NILCC i1 @TagIs(%Exp %exp, %Tag PRIMITIVE_TAG)
ret i1 %primitivep
}
;**********************************************************************
; IsException
;
; Returns true iff the expression is an exception.
;
;**********************************************************************
define NILCC i1
@IsException(%Exp %exp)
{
%exceptionp = call NILCC i1 @TagIs(%Exp %exp, %Tag EXCEPTION_TAG)
ret i1 %exceptionp
}
;**********************************************************************
; NotException
;
; Returns false iff the expression is an exception.
;
;**********************************************************************
define NILCC i1
@NotException(%Exp %exp)
{
%exceptionp = call NILCC i1 @TagIsNot(%Exp %exp, %Tag EXCEPTION_TAG)
ret i1 %exceptionp
}
;**********************************************************************
; IsNil
;
; Returns true iff the expression is nil.
;
;**********************************************************************
define NILCC i1
@IsNil(%Exp %exp)
{
%isNull = call NILCC i1 @Eq(%Exp %exp, %Exp NIL_VALUE)
ret i1 %isNull
}
;**********************************************************************
; NotNil
;
; Returns true iff the expression is not nil.
;
;**********************************************************************
define NILCC i1
@NotNil(%Exp %exp)
{
%notNull = call NILCC i1 @NotEq(%Exp %exp, %Exp NIL_VALUE)
ret i1 %notNull
}
;**********************************************************************
; NewSymbol
;
; Returns a symbol whose name is the value of the given string.
;
;**********************************************************************
define NILCC %Exp
@NewSymbol(%c_char* %name)
{
#ifndef NDEBUG
; Check to see that the string is at least one character long
; (The empty string is a valid string but *not* a valid symbol
; name!)
%len = call ccc %c_size_t @strlen(%c_char* %name)
%positive = icmp ugt %c_size_t %len, 0
assert(%positive)
#endif
; Get a pointer to a uniqueified string with the same value
%string = call NILCC %c_char* @UniqueString(%c_char* %name)
%exp = call NILCC %Exp @str2Exp(%c_char* %string)
ret %Exp %exp
}
;**********************************************************************
; NewCell
;
; Returns a newly allocated cell. Its fields must be set before use.
; This really isn't intended to be called directly--use Cons.
;
;**********************************************************************
define NILCC %nil_cell*
@NewCell()
{
; Compute sizeof(nil_cell)
; FIXME: this shouldn't be done at run-time
%sizeAddr = getelementptr %nil_cell* null, i64 1
%size = ptrtoint %nil_cell* %sizeAddr to %c_size_t
; Get a new cell
%newMem = call NILCC i8* @NewMemory(%c_size_t %size)
%newCell = bitcast i8* %newMem to %nil_cell*
ret %nil_cell* %newCell
}
;**********************************************************************
; NewPrimitive
;
; Constructs a new primitive.
;
;**********************************************************************
define NILCC %Exp
@NewPrimitive(%PrimFn* %fnPtr, %Word %argCount, %c_char* %printForm,
i1 %magic)
{
#ifndef NDEBUG
; Better not be a null function pointer
%notNull = icmp ne %PrimFn* %fnPtr, null
assert(%notNull)
; Must either have a non-negative argument count or NIL_ARGS_ANY
; for "any"
%argCountNonNeg = icmp sge %Word %argCount, 0
%argCountAny = icmp eq %Word %argCount, NIL_ARGS_ANY
%argCountOK = or i1 %argCountNonNeg, %argCountAny
assert(%argCountOK)
; Check to see that the string is at least one character long
; (The empty string is *not* a valid primitive name!)
%len = call ccc %c_size_t @strlen(%c_char* %printForm)
%positive = icmp ugt %c_size_t %len, 0
assert(%positive)
#endif
; Compute sizeof(nil_primitive)
; FIXME: this shouldn't be done at run-time
%sizeAddr = getelementptr %nil_primitive* null, i64 1
%size = ptrtoint %nil_primitive* %sizeAddr to %c_size_t
%newMem = call NILCC i8* @NewMemory(%c_size_t %size)
%newPrim = bitcast i8* %newMem to %nil_primitive*
; Set function pointer
%fnFieldPtr = getelementptr %nil_primitive* %newPrim, i64 0, i32 0
store %PrimFn* %fnPtr, %PrimFn** %fnFieldPtr
; Set the argument count
%argCountField = getelementptr %nil_primitive* %newPrim, i64 0, i32 1
store %Word %argCount, %Word* %argCountField
; Set the print form
; Note that we don't uniqueify the print form--it's not accessible
; from the language (though it could be), and could be a string
; constant.
%printFormField = getelementptr %nil_primitive* %newPrim, i64 0, i32 2
store %c_char* %printForm, %c_char** %printFormField
; Set the magic bit
%magicField = getelementptr %nil_primitive* %newPrim, i64 0, i32 3
store i1 %magic, i1* %magicField
%exp = call NILCC %Exp @nil_primitive2Exp(%nil_primitive* %newPrim)
ret %Exp %exp
}
;**********************************************************************
; LenImp
;
; Implementation for Len--written this way for tail recursion
;
;**********************************************************************
define NILCC %Word
@LenImp(%Exp %list, %Word %count)
{
%isNil = call NILCC i1 @IsNil(%Exp %list)
br i1 %isNil, label %IsNil, label %NotNil
IsNil:
ret %Word %count
NotNil:
#ifndef NDEBUG
%isCell = call NILCC i1 @IsCell(%Exp %list)
assert(%isCell)
#endif
%rest = call NILCC %Exp @Cdr(%Exp %list)
%restCount = add %Word %count, 1
%len = call NILCC %Word @LenImp(%Exp %rest, %Word %restCount)
ret %Word %len
}
;**********************************************************************
; Len
;
;**********************************************************************
define NILCC %Word
@Len(%Exp %list)
{
%len = call NILCC %Word @LenImp(%Exp %list, %Word 0)
ret %Word %len
}
;**********************************************************************
; ExceptionMsg
;
;**********************************************************************
%UnknownSymbolType = type [15 x %c_char]
@unknownSymbolArray = internal constant %UnknownSymbolType
c"unknown symbol\00"
%DotPairType = type [29 x %c_char]
@dotPairArray = internal constant %DotPairType
c"can't evaluate a dotted pair\00"
%CannotApplyType = type [22 x %c_char]
@cannotApplyArray = internal constant %CannotApplyType
c"cannot apply operator\00"
%ExpectedListType = type [14 x %c_char]
@expectedListArray = internal constant %ExpectedListType
c"expected list\00"
%BadArgNumType = type [26 x %c_char]
@badArgNumArray = internal constant %BadArgNumType
c"mismatched argument count\00"
%MalformedArgsType = type [15 x %c_char]
@malformedArgsArray = internal constant %MalformedArgsType
c"malformed args\00"
#define EXCEPTIONMSGS_NUM 6
%ExceptionMsgsType = type [EXCEPTIONMSGS_NUM x %c_char*]
@exceptionMsgs = internal constant %ExceptionMsgsType [
%c_char* getelementptr (%UnknownSymbolType* @unknownSymbolArray,
i64 0, i64 0 ),
%c_char* getelementptr (%DotPairType* @dotPairArray,
i64 0, i64 0 ),
%c_char* getelementptr (%CannotApplyType* @cannotApplyArray,
i64 0, i64 0 ),
%c_char* getelementptr (%ExpectedListType* @expectedListArray,
i64 0, i64 0 ),
%c_char* getelementptr (%BadArgNumType* @badArgNumArray,
i64 0, i64 0),
%c_char* getelementptr (%MalformedArgsType* @malformedArgsArray,
i64 0, i64 0)
]
define NILCC %c_char*
@ExceptionMsg(%Word %code)
{
#ifndef NDEBUG
%nonNeg = icmp sge %Word %code, 0
assert(%nonNeg)
%smallEnough = icmp slt %Word %code, EXCEPTIONMSGS_NUM
assert(%smallEnough)
#endif
%msgPtr = getelementptr %ExceptionMsgsType* @exceptionMsgs, i64 0, %Word %code
%msg = load %c_char** %msgPtr
ret %c_char* %msg
}
;**********************************************************************
; PrintExp
;
;**********************************************************************
define NILCC void
@PrintExp(%Exp %exp)
{
%isAtom = call NILCC i1 @IsAtom(%Exp %exp)
br i1 %isAtom, label %IsAtom, label %IsCell
IsAtom:
call NILCC void @PrintAtom(%Exp %exp)
ret void
IsCell:
call ccc %c_int @putchar(%c_int ASCII_LPAREN)
%car = call NILCC %Exp @Car(%Exp %exp)
call NILCC void @PrintExp(%Exp %car)
%cdr = call NILCC %Exp @Cdr(%Exp %exp)
call NILCC void @PrintCDR(%Exp %cdr)
call ccc %c_int @putchar(%c_int ASCII_RPAREN)
ret void
}
;**********************************************************************
; PrintAtom
;
; Helper function for PrintExp.
;
;**********************************************************************
#define PRIMITIVEFORMAT_LEN 16
@primitiveFormat = internal constant [PRIMITIVEFORMAT_LEN x %c_char]
c"#primitive: %s#\00"
%primitiveFormatType = type [PRIMITIVEFORMAT_LEN x %c_char]
#define EXCEPTIONFORMAT_LEN 20
@exceptionFormat = internal constant [EXCEPTIONFORMAT_LEN x %c_char]
c"#exception %ld: %s#\00"
%exceptionFormatType = type [EXCEPTIONFORMAT_LEN x %c_char]
define NILCC void
@PrintAtom(%Exp %exp)
{
%tag = call NILCC %Tag @GetTag(%Exp %exp)
switch %Tag %tag, label %CantHappen [