~ubuntu-branches/ubuntu/vivid/gcl/vivid

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
;; CMPFUN  Library functions.
;;;
;; Copyright (C) 1994 M. Hagiya, W. Schelter, T. Yuasa

;; This file is part of GNU Common Lisp, herein referred to as GCL
;;
;; GCL is free software; you can redistribute it and/or modify it under
;;  the terms of the GNU LIBRARY GENERAL PUBLIC LICENSE as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; 
;; GCL is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public 
;; License for more details.
;; 
;; You should have received a copy of the GNU Library General Public License 
;; along with GCL; see the file COPYING.  If not, write to the Free Software
;; Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.


(in-package 'compiler)

(si:putprop 'princ 'c1princ 'c1)
(si:putprop 'princ 'c2princ 'c2)
(si:putprop 'terpri 'c1terpri 'c1)

(si:putprop 'apply 'c1apply 'c1)
(si:putprop 'apply 'c2apply 'c2)
(si:putprop 'apply-optimize 'c2apply-optimize 'c2)
(si:putprop 'funcall 'c1funcall 'c1)

(si:putprop 'rplaca 'c1rplaca 'c1)
(si:putprop 'rplaca 'c2rplaca 'c2)
(si:putprop 'rplacd 'c1rplacd 'c1)
(si:putprop 'rplacd 'c2rplacd 'c2)

(si:putprop 'si::memq 'c1memq 'c1)
(si:putprop 'member 'c1member 'c1)
(si:putprop 'member!2 'c2member!2 'c2)
(si:putprop 'assoc 'c1assoc 'c1)
(si:putprop 'assoc!2 'c2assoc!2 'c2)
(si:putprop 'get 'c1get 'c1)
(si:putprop 'get 'c2get 'c2)

(si:putprop 'nth '(c1nth-condition . c1nth) 'c1conditional)
(si:putprop 'nthcdr '(c1nthcdr-condition . c1nthcdr) 'c1conditional)
(si:putprop 'si:rplaca-nthcdr 'c1rplaca-nthcdr 'c1)
(si:putprop 'si:list-nth 'c1list-nth 'c1)
(si:putprop 'list-nth-immediate 'c2list-nth-immediate 'c2)

(si:putprop 'gethash 'c1gethash 'c1)
(si:putprop 'gethash 'c2gethash 'c2)


(defvar *princ-string-limit* 80)

(defun c1princ (args &aux stream (info (make-info)))
  (when (endp args) (too-few-args 'princ 1 0))
  (unless (or (endp (cdr args)) (endp (cddr args)))
          (too-many-args 'princ 2 (length args)))
  (setq stream (if (endp (cdr args))
                   (c1nil)
                   (c1expr* (cadr args) info)))
  (if (and (or (and (stringp (car args))
                    (<= (length (car args)) *princ-string-limit*))
               (characterp (car args)))
           (or (endp (cdr args))
               (and (eq (car stream) 'var)
                    (member (var-kind (caaddr stream)) '(GLOBAL SPECIAL)))))
      (list 'princ info (car args)
            (if (endp (cdr args)) nil (var-loc (caaddr stream)))
            stream)
      (list 'call-global info 'princ
            (list (c1expr* (car args) info) stream))))

(defun c2princ (string vv-index stream)
  (cond ((eq *value-to-go* 'trash)
         (cond ((characterp string)
                (wt-nl "princ_char(" (char-code string))
                (if (null vv-index) (wt ",Cnil") (wt "," (vv-str vv-index)))
                (wt ");"))
               ((= (length string) 1)
                (wt-nl "princ_char(" (char-code (aref string 0)))
                (if (null vv-index) (wt ",Cnil") (wt "," (vv-str vv-index)))
                (wt ");"))
               (t
                (wt-nl "princ_str(\"")
                (dotimes** (n (length string))
                  (let ((char (schar string n)))
                       (cond ((char= char #\\) (wt "\\\\"))
                             ((char= char #\") (wt "\\\""))
                             ((char= char #\Newline) (wt "\\n"))
                             (t (wt char)))))
                (wt "\",")
                (if (null vv-index) (wt "Cnil") (wt (vv-str vv-index)))
                (wt ");")))
         (unwind-exit nil))
        ((eql string #\Newline) (c2call-global 'terpri (list stream) nil t))
        (t (c2call-global
            'princ
            (list (list 'LOCATION
                        (make-info :type
                          (if (characterp string) 'character 'string))
                        (list 'VV (add-object string)))
                  stream) nil t))))

(defun c1terpri (args &aux stream (info (make-info)))
  (unless (or (endp args) (endp (cdr args)))
          (too-many-args 'terpri 1 (length args)))
  (setq stream (if (endp args)
                   (c1nil)
                   (c1expr* (car args) info)))
  (if (or (endp args)
          (and (eq (car stream) 'var)
               (member (var-kind (caaddr stream)) '(GLOBAL SPECIAL))))
      (list 'princ info #\Newline
            (if (endp args) nil (var-loc (caaddr stream)))
            stream)
      (list 'call-global info 'terpri (list stream))))

(defun c1apply (args &aux info)
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'apply 2 (length args)))
  (let ((funob (c1funob (car args))))
       (setq info (copy-info (cadr funob)))
       (setq args (c1args (cdr args) info))
       (cond ((eq (car funob) 'call-lambda)
              (let* ((lambda-expr (caddr funob))
                     (lambda-list (caddr lambda-expr)))
                    (declare (object lambda-expr lambda-list))
                    (if (and (null (cadr lambda-list))		; No optional
                             (null (cadddr lambda-list)))	; No keyword
                        (c1apply-optimize info
                                          (car lambda-list)
                                          (caddr lambda-list)
                                          (car (cddddr lambda-expr))
                                          args)
                       (list 'apply info funob args))))
             (t (list 'apply info funob args))))
  )

(defun c2apply (funob args &aux (*vs* *vs*) loc)
  (setq loc (save-funob funob))
  (let ((*vs* *vs*) (base *vs*) (last-arg (list 'CVAR (next-cvar))))
       (do ((l args (cdr l)))
           ((endp (cdr l))
            (wt-nl "{object " last-arg ";")
            (let ((*value-to-go* last-arg)) (c2expr* (car l))))
           (declare (object l))
           (let ((*value-to-go* (list 'vs (vs-push)))) (c2expr* (car l))))
       (wt-nl " vs_top=base+" *vs* ";")
       (base-used)
       (cond (*safe-compile*
              (wt-nl " while(!endp(" last-arg "))")
              (wt-nl " {vs_push(car(" last-arg "));")
              (wt last-arg "=cdr(" last-arg ");}"))
             (t
              (wt-nl " while(" last-arg "!=Cnil)")
              (wt-nl " {vs_push((" last-arg ")->c.c_car);")
              (wt last-arg "=(" last-arg ")->c.c_cdr;}")))
       (wt-nl "vs_base=base+" base ";}")
       (base-used))
  (c2funcall funob 'args-pushed loc)
  )

(defun c1apply-optimize (info requireds rest body args
                              &aux (vl nil) (fl nil))
  (do ()
      ((or (endp (cdr args)) (endp requireds)))
      (push (pop requireds) vl)
      (push (pop args) fl))

  (cond ((cdr args)	;;; REQUIREDS is NIL.
         (cmpck (null rest)
                "APPLY passes too many arguments to LAMBDA expression.")
         (push rest vl)
         (push (list 'call-global info 'list* args) fl)
         (list 'let info (reverse vl) (reverse fl) body))
        (requireds	;;; ARGS is singleton.
         (let ((temp (make-var :kind 'LEXICAL :ref t)))
              (push temp vl)
              (push (car args) fl)
              (list 'let info (reverse vl) (reverse fl)
                    (list 'apply-optimize
                          (cadr body) temp requireds rest body))))
        (rest (push rest vl)
              (push (car args) fl)
              (list 'let info (reverse vl) (reverse fl) body))
        (t
         (let ((temp (make-var :kind 'LEXICAL :ref t)))
              (push temp vl)
              (push (car args) fl)
              (list 'let info (reverse vl) (reverse fl)
                    (list 'apply-optimize
                          (cadr body) temp requireds rest body))))
        )
  )

(defun c2apply-optimize (temp requireds rest body
                              &aux (*unwind-exit* *unwind-exit*) (*vs* *vs*)
                                   (*clink* *clink*) (*ccb-vs* *ccb-vs*))
  (when (or *safe-compile* *compiler-check-args*)
        (wt-nl (if rest "ck_larg_at_least" "ck_larg_exactly")
                "(" (length requireds) ",")
        (wt-var temp nil)
        (wt ");"))

  (dolist** (v requireds) (setf (var-ref v) (vs-push)))
  (when rest (setf (var-ref rest) (vs-push)))

  (do ((n 0 (1+ n))
       (vl requireds (cdr vl)))
      ((endp vl)
       (when rest
             (wt-nl) (wt-vs (var-ref rest)) (wt "= ")
             (dotimes** (i n) (wt "("))
             (wt-var temp nil)
             (dotimes** (i n) (wt-nl ")->c.c_cdr"))
             (wt ";")))
      (declare (fixnum n) (object vl))
      (wt-nl) (wt-vs (var-ref (car vl))) (wt "=(")
      (dotimes** (i n) (wt "("))
      (wt-var temp nil)
      (dotimes** (i n) (wt-nl ")->c.c_cdr"))
      (wt ")->c.c_car;"))

  (dolist** (var requireds) (c2bind var))
  (when rest (c2bind rest))

  (c2expr body)
  )

(defun c1funcall (args &aux funob (info (make-info)))
  (when (endp args) (too-few-args 'funcall 1 0))
  (setq funob (c1funob (car args)))
  (add-info info (cadr funob))
  (list 'funcall info funob (c1args (cdr args) info))
  )


(defun c1rplaca (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'rplaca 2 (length args)))
  (unless (endp (cddr args))
          (too-many-args 'rplaca 2 (length args)))
  (setq args (c1args args info))
  (list 'rplaca info args))

(defun c2rplaca (args &aux (*vs* *vs*) (*inline-blocks* 0))
  (setq args (inline-args args '(t t)))
  (safe-compile
   (wt-nl "if(type_of(" (car args) ")!=t_cons)"
          "FEwrong_type_argument(Scons," (car args) ");"))
  (wt-nl "(" (car args) ")->c.c_car = " (cadr args) ";")
  (unwind-exit (car args))
  (close-inline-blocks)
  )

(defun c1rplacd (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'rplacd 2 (length args)))
  (when (not (endp (cddr args)))
        (too-many-args 'rplacd 2 (length args)))
  (setq args (c1args args info))
  (list 'rplacd info args))

(defun c2rplacd (args &aux (*vs* *vs*) (*inline-blocks* 0))
  (setq args (inline-args args '(t t)))
  (safe-compile
   (wt-nl "if(type_of(" (car args) ")!=t_cons)"
          "FEwrong_type_argument(Scons," (car args) ");"))
  (wt-nl "(" (car args) ")->c.c_cdr = SAFE_CDR(" (cadr args) ");")
  (unwind-exit (car args))
  (close-inline-blocks)
  )

(defun c1memq (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'si::memq 2 (length args)))
  (unless (endp (cddr args))
          (too-many-args 'si::memq 2 (length args)))
  (list 'member!2 info 'eq (c1args (list (car args) (cadr args)) info)))
        
(defun c1member (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'member 2 (length args)))
  (cond ((endp (cddr args))
         (list 'member!2 info 'eql (c1args args info)))
        ((and (eq (caddr args) :test)
              (eql (length args) 4)	
       (member  (cadddr args) '('eq #'eq 'equal #'equal
				'equalp #'equalp 'eql #'eql)
		:test 'equal))
         (list 'member!2 info (cadr (cadddr args))
               (c1args (list (car args) (cadr args)) info)))
        (t
         (list 'call-global info 'member (c1args args info)))))

(defun c2member!2 (fun args
                       &aux (*vs* *vs*) (*inline-blocks* 0) (l (next-cvar)))
  (setq args (inline-args args '(t t)))
  (wt-nl "{register object x= " (car args) ",V" l "= " (cadr args) ";")
  (if *safe-compile*
      (wt-nl "while(!endp(V" l "))")
      (wt-nl "while(V" l "!=Cnil)"))
  (if (eq fun 'eq)
      (wt-nl "if(x==(V" l "->c.c_car)){")
      (wt-nl "if(" (string-downcase (symbol-name fun))
		"(x,V" l "->c.c_car)){"))
  (if (and (consp *value-to-go*)
           (or (eq (car *value-to-go*) 'JUMP-TRUE)
               (eq (car *value-to-go*) 'JUMP-FALSE)))
      (unwind-exit t 'JUMP)
      (unwind-exit (list 'CVAR l) 'JUMP))
  (wt-nl "}else V" l "=V" l "->c.c_cdr;")
  (unwind-exit nil)
  (wt "}")
  (close-inline-blocks)
  )

(defun c1assoc (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'assoc 2 (length args)))
  (cond ((endp (cddr args))
         (list 'assoc!2 info 'eql (c1args args info)))
        ((and (eq (caddr args) ':test)
              (eql (length args) 4)	
       (member  (cadddr args) '('eq #'eq 'equal #'equal
				'equalp #'equalp 'eql #'eql)
		:test 'equal))
         (list 'assoc!2 info (cadr (cadddr args)) (c1args (list (car args) (cadr args)) info)))
        (t
         (list 'call-global info 'assoc (c1args args info)))))

(defun c2assoc!2 (fun args
                      &aux (*vs* *vs*) (*inline-blocks* 0) (al (next-cvar))name)
  (setq args (inline-args args '(t t)))
  (setq name (symbol-name fun))
  (or (eq fun 'eq) (setq name (string-downcase name)))
  (wt-nl "{register object x= " (car args) ",V" al "= " (cadr args) ";")
  (cond (*safe-compile*
         (wt-nl "while(!endp(V" al "))")
             (wt-nl "if(type_of(V"al"->c.c_car)==t_cons &&"
		    name "(x,V" al "->c.c_car->c.c_car)){"))
        (t
         (wt-nl "while(V" al "!=Cnil)")
             (wt-nl "if(" name "(x,V" al "->c.c_car->c.c_car) &&"
			"V"al"->c.c_car != Cnil){"))) 
  (if (and (consp *value-to-go*)
           (or (eq (car *value-to-go*) 'jump-true)
               (eq (car *value-to-go*) 'jump-false)))
      (unwind-exit t 'jump)
      (unwind-exit (list 'CAR al) 'jump))
  (wt-nl "}else V" al "=V" al "->c.c_cdr;")
  (unwind-exit nil)
  (wt "}")
  (close-inline-blocks)
  )




(defun boole3 (a b c)  (boole a b c))
;(si:putprop 'boole '(c1boole-condition . c1boole3) 'c1conditional)

(defun c1boole-condition (args)
   (and (not (endp (cddr args)))
	(endp (cdddr args))
        (inline-boole3-string (car args))))

(defun c1boole3 (args)
  (c1expr (cons 'boole3 args)))

(defun inline-boole3 (&rest args)
  (let ((boole-op-arg (second (car args))))
    (or (eq (car boole-op-arg) 'fixnum-value) (error "must be constant"))
    (let ((string (inline-boole3-string  (third boole-op-arg))))
      (or string (error "should not get here boole opt"))
      (wt-inline-loc string (cdr args)))))

(defun inline-boole3-string (op-code)
  (and (constantp op-code) (setq op-code (eval op-code)))
  (case op-code
	(#. boole-andc1 "((~(#0))&(#1))")
	(#. boole-andc2 "(((#0))&(~(#1)))")
	(#. boole-nor   "(~((#0)|(#1)))")
	(#. boole-orc1  "(~(#0)) | (#1)))")
	(#. boole-orc2  "((#0) | (~(#1)))")
	(#. boole-nand "(~((#0) & (#1)))")
	(#. boole-eqv	"(~((#0) ^ (#1)))")
	(#. boole-and "((#0) & (#1))")
	(#. boole-xor "((#0) ^ (#1))")
	(#. boole-ior "((#0) | (#1))")))
 
(si:putprop 'ash '(c1ash-condition . c1ash) 'c1conditional)

(defun c1ash-condition (args &aux (z '#.(let ((z (integer-length most-positive-fixnum))) `(integer ,(- z) ,z))))
  (let ((shamt (second args)))
    (or (typep shamt z)
	(and (consp shamt)
	     (eq (car  shamt) 'the)
	     (let ((type (cadr  shamt)))
	        (subtypep type z))))))

(defun c1ash (args)
  (let  ((shamt (second args))fun)
    (cond ((constantp shamt) (setq shamt (eval shamt))
	   (or (si:fixnump shamt) (error "integer shift only"))
	   (cond ((< shamt 0) (setq fun 'shift>> ))
		 ((>= shamt 0) (setq fun 'shift<<))))
	  (t (let ((type (second shamt)))
	       ;;it had to be a (the type..)
	       (cond ((subtypep type '#.`(integer 0 ,(integer-length most-positive-fixnum)))
		      (setq fun 'shift<< ))
		     ((subtypep type '#.`(integer ,(- (integer-length most-positive-fixnum)) 0))
		      (setq fun 'shift>> ))
		     (t (error "should not get here")))
	       )))
    (c1expr (cons fun args))))
(defun shift>> (a b) (ash a  b))
(defun shift<< (a b) (ash a  b))
(si:putprop 'ash '(c1ash-condition . c1ash)  'c1conditional)
(si:putprop 'shift>> "Lash" 'lfun)
(si:putprop 'shift<< "Lash" 'lfun)

(si::putprop 'ldb 'co1ldb 'co1)		    

(defun co1ldb (f args &aux tem (len (integer-length most-positive-fixnum))) f
  (let ((specs
	 (cond ((and (consp (setq tem (first args)))
		     (eq 'byte (car tem))
		     (cons (second tem) (third tem)))))))
    (cond ((and (integerp (cdr specs))
		(integerp (car specs))
		(< (+ (car specs)(cdr specs))
		   len)
		(subtypep (result-type (second args)) 'fixnum))
	   (c1expr `(the fixnum (si::ldb1 ,(car specs) ,(cdr specs) ,(second args))))))))

	  
(si:putprop 'length 'c1length 'c1)

(defun c1length (args &aux (info (make-info)))
  (setf (info-type info) 'fixnum)
  (cond ((and (consp (car args))
	      (eq (caar args) 'symbol-name)
	      (let ((args1 (cdr (car args))))
		(and args1 (not (cddr args1))
		     (list 'call-global info 'symbol-length
			   (c1args args1 info))))))
	(t  (setq args (c1args args info))
	    (list 'call-global info 'length args ))))


(defun c1get (args &aux (info (make-info)))

  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'get 2 (length args)))
  (when (and (not (endp (cddr args))) (not (endp (cdddr args))))
        (too-many-args 'get 3 (length args)))
  (list 'get info (c1args args info)))

(defun c2get (args)
  (if *safe-compile*
      (c2call-global 'get args nil t)
      (let ((*vs* *vs*) (*inline-blocks* 0) (pl (next-cvar)))
           (setq args (inline-args args (if (cddr args) '(t t t) '(t t))))
           (wt-nl "{object V" pl" =(" (car args) ")->s.s_plist;")
           (wt-nl " object ind= " (cadr args) ";")
           (wt-nl "while(V" pl "!=Cnil){")
           (wt-nl "if(V" pl "->c.c_car==ind){")
           (unwind-exit (list 'CADR pl) 'jump)
           (wt-nl "}else V" pl "=V" pl "->c.c_cdr->c.c_cdr;}")
           (unwind-exit (if (cddr args) (caddr args) nil))
           (wt "}")
           (close-inline-blocks)))
  )

(defun co1eql (f args) f
  (or (and (cdr args) (not *safe-compile*))
      (return-from co1eql nil))
  (cond ((replace-constant args)
	 (cond ((characterp (second args))
		(setq args (reverse args))))
	 (cond ((characterp (car args))
		(let ((c (gensym)))
		  (c1expr
		   `(let ((,c ,(second args)))
		      (declare (type ,(result-type (second args))
				     ,c))
		      (and (typep ,c 'character)
			   (= (char-code ,(car args))
			      (the fixnum
				   (char-code
				    (the character
					 ,c)))
			      ))))))))))


	 
(si::putprop 'eql 'co1eql 'co1)		    

(defvar *frozen-defstructs* nil)

;; Return the most particular type we can EASILY obtain
;; from x.  
(defun result-type (x)
  (cond ((symbolp x)
	 (let ((tem (c1expr x)))
	   (info-type (second tem))))
	((constantp x)
	 (type-filter (type-of x)))
	((and (consp x) (eq (car x) 'the))
	 (type-filter (second x)))
	(t t)))



(defvar *type-alist*
  '((fixnum . si::fixnump)
    (float . floatp)
    (si::spice . si::spice-p)
    (short-float . short-float-p)
    (long-float . long-float-p)
    (integer . integerp)
    (character . characterp)
    (symbol . symbolp)
    (cons . consp)
    (null . null)
    (array . arrayp)
    (vector . vectorp)
    (bit-vector . bit-vector-p)
    (string . stringp)
    (list . (lambda (y) (or (consp y) (null y))))
    (number . numberp)
    (rational . rationalp)
    (complex . complexp)
    (ratio . ratiop)
    (sequence . (lambda (y) (or (listp y) (vectorp y))))
    (function . functionp)
    ))


(defun co1typep (f args &aux tem) f
  (let*
      ((x (car args))  new
       (type (and (consp (second args))
		  (eq (car (second args)) 'quote)
		  (second (second args)))))
    (cond ((subtypep (result-type (car args)) type)
	   (setq new t)
	   (return-from co1typep (c1expr new))))
    (setq new
	  (cond
	   ((null type) nil)
	   ((setq f (assoc type *type-alist* :test 'equal))
	    (list (cdr f) x))
	   ((and (consp type)
		 (or (and (eq (car type) 'vector)
			  (null (cddr type)))
		     (and 
		      (member (car type)
			      '(array vector simple-array))
		      (equal (third type) '(*)))))
	    (setq tem (si::best-array-element-type
		       (second type)))
	    (cond ((eq tem 'string-char) `(stringp ,x))
		  ((eq tem 'bit) `(bit-vector-p ,x))
		  ((setq tem (position tem *aet-types*))
		   `(the boolean (vector-type ,x ,tem)))))
	   ((and (consp type)
		 (eq (car type) 'satisfies)
		 (consp (cdr type))
		 (cadr type)
		 (symbolp (cadr type))
		 (symbol-package (cadr type))
		 (null (cddr type))
		 `(,(cadr type) ,x)))
	   ((subtypep type 'fixnum)
	    (setq tem (si::normalize-type type))
	    (and (consp tem)
		 (si::fixnump (second tem))
		 (si::fixnump (third  tem))
		 `(let ((.tem ,x))
		    (declare (type ,(result-type x) .tem))
		    (and (typep .tem 'fixnum)
			 (>=  (the fixnum .tem) ,(second tem))
			 (<=  (the fixnum .tem) ,(third tem))))))
	   ((and (symbolp type)
		 (setq tem (get type 'si::s-data)))
	    (cond ((or (si::s-data-frozen tem)
		       *frozen-defstructs*)
		   (struct-type-opt x tem))
		  (t
		   `(si::structure-subtype-p
		     ,x ',type))))
;	   ((and (print (list 'slow 'typep type)) nil))
	   (t nil)))
    (and new (c1expr `(the boolean , new)))))

;; this is going the wrong way.  want to go up..
(defun struct-type-opt (x sd)
  (let ((s (gensym))
	(included (get-included (si::s-data-name sd))))
    `(let ((,s ,x))
       (and
	 (si::structurep ,s)
	 ,(cond ((< (length included) 3)
		 `(or ,@
		      (mapcar #'(lambda (x)
				  `(eq (si::structure-def ,s)
				       ,(name-sd1 x)))
			      included)))
		(t `(si::structure-subtype-p ,s
					    ,(name-sd1
					       (si::s-data-name sd)))))))))

(defun get-included (name)
  (let ((sd (get name 'si::s-data)))
    (cons (si::s-data-name sd)
	  (mapcan 'get-included
		  (si::s-data-included sd)))))
  


(si::putprop 'typep 'co1typep 'co1)		    

(defun co1schar (f args) f
   (and (listp (car args)) (not *safe-compile*)
	(cdr args)
	(eq (caar args) 'symbol-name)
	(c1expr `(aref (the string ,(second (car args)))
			,(second args)))))

(si::putprop 'schar 'co1schar 'co1)

(si::putprop 'cons 'co1cons 'co1)
;; turn repetitious cons's into a list*

(defun cons-to-lista (x)
  (let ((tem  (last x)))
    (cond 
	((and (consp tem)
	     (consp (car tem))
	     (eq (caar tem) 'cons)
	     (eql (length (cdar tem)) 2)
	     (cons-to-lista (append (butlast x)
				    (cdar tem)))))
	(t x))))
    	 

(defun co1cons (f args) f
  (let ((tem (and (eql (length args) 2) (cons-to-lista args))))
    (and (not (eq tem args))
	 (c1expr  (if (equal '(nil) (last tem))
		     (cons 'list (butlast tem))
		     (cons 'list* tem))))))

;; I don't feel it is good to replace the list call, but rather
;; usually better the other way around.  We removed c1list
;; because of possible feedback.

(defun c1nth-condition (args)
       (and (not (endp args))
            (not (endp (cdr args)))
            (endp (cddr args))
            (numberp (car args))
            (<= 0 (car args) 7)))

(defun c1nth (args)
       (c1expr (case (car args)
                     (0 (cons 'car (cdr args)))
                     (1 (cons 'cadr (cdr args)))
                     (2 (cons 'caddr (cdr args)))
                     (3 (cons 'cadddr (cdr args)))
                     (4 (list 'car (cons 'cddddr (cdr args))))
                     (5 (list 'cadr (cons 'cddddr (cdr args))))
                     (6 (list 'caddr (cons 'cddddr (cdr args))))
                     (7 (list 'cadddr (cons 'cddddr (cdr args))))
                     )))

(defun c1nthcdr-condition (args)
       (and (not (endp args))
            (not (endp (cdr args)))
            (endp (cddr args))
            (numberp (car args))
            (<= 0 (car args) 7)))

(defun c1nthcdr (args)
       (c1expr (case (car args)
                     (0 (cadr args))
                     (1 (cons 'cdr (cdr args)))
                     (2 (cons 'cddr (cdr args)))
                     (3 (cons 'cdddr (cdr args)))
                     (4 (cons 'cddddr (cdr args)))
                     (5 (list 'cdr (cons 'cddddr (cdr args))))
                     (6 (list 'cddr (cons 'cddddr (cdr args))))
                     (7 (list 'cdddr (cons 'cddddr (cdr args))))
                     )))

(defun c1rplaca-nthcdr (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)) (endp (cddr args)))
        (too-few-args 'si:rplaca-nthcdr 3 (length args)))
  (unless (endp (cdddr args))
          (too-few-args 'si:rplaca-nthcdr 3 (length args)))
  (if (and (numberp (cadr args)) (<= 0 (cadr args) 10))
      (let  ((x (gensym))(y (gensym)))
	(c1expr
	 `(let ((,x ,(car args))
		(,y ,(third args)))
	    (setf ,x (nthcdr ,(cadr args) ,x))
	    (setf (car ,x) ,y)
	    ,y)))
      (list 'call-global info 'si:rplaca-nthcdr (c1args args info))))


;; Facilities for faster reading and writing from file streams.
;; You must declare the stream to be :in-file
;; or :out-file

(si::putprop 'read-byte 'co1read-byte 'co1)
(si::putprop 'read-char 'co1read-char 'co1)
(si::putprop 'write-byte 'co1write-byte 'co1)
(si::putprop 'write-char 'co1write-char 'co1)



(defun fast-read (args read-fun)
  (cond
    ((and (not *safe-compile*)
	  (< *space* 2)
	  (null (second args))
	  (boundp 'si::*eof*))
     (cond
       ((atom (car args))
	(or (car args) (setq args (cons '*standard-input* (cdr args))))
	(let ((stream (car args))
	      (eof (third args)))
	  `(let ((ans 0))
	     (declare (fixnum  ans))
	     (cond ((fp-okp ,stream)
		    (setq ans  (sgetc1 ,stream))
		    (cond ((and (eql ans ,si::*eof*)
				(sfeof  ,stream))
			   ,eof)
			  (t ,(if (eq read-fun 'read-char1)
				       '(code-char ans) 'ans))
			  ))
		   (t
		    (,read-fun ,stream  ,eof)
		     )
		   ))))
       (t
	`(let ((.strm. ,(car args)))
	   (declare (type ,(result-type (car args)) .strm.))
	     ,(fast-read (cons '.strm. (cdr args)) read-fun)))))))

(defun co1read-byte (f args &aux tem) f
  (cond ((setq tem (fast-read args 'read-byte1))
	 (let ((*space* 10))		;prevent recursion!
	   (c1expr tem)))))

(defun co1read-char (f args &aux tem) f
  (cond ((setq tem (fast-read args 'read-char1))
	 (let ((*space* 10))		;prevent recursion!
	   (c1expr tem)))))    

(defun cfast-write (args write-fun)
  (cond
    ((and (not *safe-compile*)
	  (< *space* 2)
	  (boundp 'si::*eof*))
     (let ((stream (second args)))
       (or stream (setq stream '*standard-output*))
     (cond
       ((atom stream)
	`(cond ((fp-okp ,stream)
		(the fixnum (sputc .ch ,stream)))
	       (t    (,write-fun  .ch ,stream))))
       (t `(let ((.str ,stream))
	     (declare (type ,(result-type stream) .str))
	     ,(cfast-write (list '.ch '.str) write-fun))))))))

(defun co1write-byte (f args) f
  (let ((tem (cfast-write args 'write-byte)))
    (if tem (let ((*space* 10))
	      (c1expr
		`(let ((.ch ,(car args)))
		   (declare (fixnum .ch))
		   ,tem
		   ,(if (atom (car args)) (car args) '.ch)))))))

(defun co1write-char (f args) f
  (let ((tem (cfast-write args 'write-char)))
    (if tem (let ((*space* 10))
	      (c1expr
		`(let ((.ch ,(car args)))
		   (declare (character .ch))
		   ,tem
		   ,(if (atom (car args)) (car args) '.ch)))))))



(defvar *aet-types*
  #(T STRING-CHAR SIGNED-CHAR FIXNUM SHORT-FLOAT LONG-FLOAT
			SIGNED-CHAR
			UNSIGNED-CHAR SIGNED-SHORT UNSIGNED-SHORT))


(defun aet-c-type (type)
  (ecase type
    ((t) "object")
    ((string-char signed-char) "char")
    (fixnum "fixnum")
    (unsigned-char "unsigned char")
    (unsigned-short "unsigned short")
    (signed-short "short")
    (unsigned-short "unsigned short")
    (long-float "longfloat")
    (short-float "shortfloat")))


(si:putprop 'vector-push 'co1vector-push 'co1)
(si:putprop 'vector-push-extend 'co1vector-push 'co1)
(defun co1vector-push (f args) f
  (unless
   (or *safe-compile*
       (> *space* 3)
       (null (cdr args))
       )
   (let ((*space* 10))
     (c1expr
      `(let* ((.val ,(car args))
	      (.v ,(second args))
	      (.i (fill-pointer .v))
	      (.dim (array-total-size .v)))
	 (declare (fixnum .i .dim))
	 (declare (type ,(result-type (second args)) .v))
	 (declare (type ,(result-type (car args)) .val))
	 (cond ((< .i .dim)
		(the fixnum (si::fill-pointer-set .v (the fixnum (+ 1 .i))))
		(si::aset .v .i .val)
		.i)
	       (t ,(cond ((eq f 'vector-push-extend)
			  `(vector-push-extend .val
					       .v ,@(cddr args)))))))))))

(defun constant-fold-p (x)
  (cond ((constantp x) t)
	((atom  x) nil)
	((eq (car x) 'the)
	 (constant-fold-p (third x)))
	((and 
	      (symbolp (car x))
	      (eq (get (car x) 'co1)
		  'co1constant-fold))
	 (dolist (w (cdr x))
		 (or (constant-fold-p w)
		     (return-from constant-fold-p nil)))
	 t)
	(t nil)))

(defun co1constant-fold (f args )
  (cond ((and (fboundp f)
	      (dolist (v args t)
		      (or (constant-fold-p v)
			  (return-from co1constant-fold nil))))
	 (c1expr (cmp-eval (cons f args))))))


(si::putprop 'do 'co1special-fix-decl 'co1special)
(si::putprop 'do* 'co1special-fix-decl 'co1special)
(si::putprop 'prog 'co1special-fix-decl 'co1special)
(si::putprop 'prog* 'co1special-fix-decl 'co1special)

(defun co1special-fix-decl (f args)
  (flet ((fixup (forms &aux decls )
	  (block nil
		 (tagbody
		  top
		  (or (consp forms) (go end))
		  (let ((tem (car forms)))
		    (if (and (consp tem)
			     (setq tem  (cmp-macroexpand tem))
			     (eq (car tem) 'declare))
			(progn (push tem decls) (pop forms))
		      (go end)))
		      (go top)
	      		; all decls made explicit.
		      end
		     (return  (nconc (nreverse decls) forms))))))
	(c1expr
	  (cmp-macroexpand
	    (case f
	      ((do do*) `(,f ,(car args)
			     ,(second args)
			     ,@ (fixup (cddr args))))
	      ((prog prog*)
	       `(,f ,(car args)
		    ,@ (fixup (cdr args)))))))))
(si::putprop 'sublis 'co1sublis 'co1)
(defun co1sublis (f args &aux test) f
 (and (case (length args)
	(2 (setq test 'eql))
	(4 (and (eq (third args) :test)
		(cond ((member (fourth args) '(equal (function equal))) (setq test 'equal))
		      ((member (fourth args) '(eql (function eql))) (setq test 'eql))
     		      ((member (fourth args) '(eq (function eq))) (setq test 'eq))
		      ))))
      (let ((s (gensym)))
	(c1expr `(let ((,s ,(car args)))
		   (sublis1 ,s ,(second args) ',test))))))


(defun sublis1-inline (a b c)
  (let ((tst (or (car (find (cadr c) *objects* :key 'cadr))
		 (let ((v (member (cadr c) *top-level-forms* :key 'cadr)))
		   (and v
			(eq (caar v) 'sharp-comma)
			(cmp-eval (caddar v)))))))
    (or (member tst '(eq equal eql)) (error "bad test"))
    (wt "(check_alist("	a "),sublis1("a "," b "," (format nil "&o~(~a~)))" tst))))

  
;; end new		  
      
(defun c1list-nth (args &aux (info (make-info)))
  (when (or (endp args) (endp (cdr args)))
        (too-few-args 'si:rplaca-nthcdr 2 (length args)))
  (unless (endp (cddr args))
          (too-few-args 'si:rplaca-nthcdr 2 (length args)))
  (if (and (numberp (car args)) (<= 0 (car args) 10))
      (list 'list-nth-immediate info
            (car args)
            (c1args (list (cadr args)) info))
      (list 'call-global info 'si:list-nth (c1args args info))))

(defun c2list-nth-immediate (index args &aux (l (next-cvar))
                                             (*vs* *vs*) (*inline-blocks* 0))
  (setq args (inline-args args '(t t)))
  (wt-nl "{object V" l "= ")
  (if *safe-compile*
      (progn
       (dotimes** (i index) (wt "cdr("))
       (wt (car args))
       (dotimes** (i index) (wt ")"))
       (wt ";")
       (wt-nl "if((type_of(V" l ")!=t_cons) && (" (car args) "!= Cnil))")
       (wt-nl " FEwrong_type_argument(Scons,V" l ");")
       )
      (progn
       (wt-nl (car args))
       (dotimes** (i index) (wt-nl "->c.c_cdr"))
       (wt ";")))
  (unwind-exit (list 'CAR l))
  (wt "}")
  (close-inline-blocks)
  )


(defun c1gethash (args)
  (unless (cdr args) (too-few-args 'gethash 2 (length args)))
  (when   (cdddr args) (too-many-args 'gethash 3 (length args)))
  (let* ((info (make-info))
	 (nargs (c1args args info)))
    `(gethash ,info ,nargs)))

(defun c2gethash (args)
  (cond ((member *value-to-go* '(top return))
	 (let* ((nargs (inline-args args '(t t)))
		(base *vs*)(*vs* *vs*)
		(r (cdr (vs-push)))(f (cdr (vs-push))))
	   (wt-nl "{ struct htent *_z=gethash" (if *safe-compile* "_with_check" "") "(" (car nargs) "," (cadr nargs) ");")
	   (wt-nl "if (_z->hte_key==OBJNULL) {")
	   (wt-nl "base[" r "]=" (caddr nargs) ";")
	   (wt-nl "base[" f "]=Cnil;")
	   (wt-nl "} else {")
	   (wt-nl "base[" r "]=_z->hte_value;")
	   (wt-nl "base[" f "]=Ct;")
	   (wt-nl "}}")
	   (wt-nl "vs_top=(vs_base=base+" base ")+" (- *vs* base) ";")
	   (unwind-exit 'fun-val nil (cons 'values 2))))
	((unwind-exit (get-inline-loc `((t t) t #.(flags rfa) 
					,(concatenate 'string
						      "({struct htent *_z=gethash"
						      (if *safe-compile* "_with_check" "")
						      "(#0,#1);_z->hte_key==OBJNULL ? (#2) : _z->hte_value;})"))
					args)))))