Debian Codegraph @

src/cmp/cmpopt-cons.lsp

raw
1 ;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*-
2 ;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab:
3
4 ;;;;
5 ;;;; CMPOPT-CONS Optimization of CONS functions
6 ;;;;
7 ;;;; Copyright (c) 2010, Juan Jose Garcia-Ripoll
8 ;;;;
9 ;;;; See the file 'LICENSE' for the copyright details.
10 ;;;;
11
12 (in-package "COMPILER")
13
14 (defun si:cons-car (x)
15 (declare (type cons x) (optimize (safety 0) (speed 3)))
16 (car x))
17
18 (defun si:cons-cdr (x)
19 (declare (type cons x) (optimize (safety 0) (speed 3)))
20 (cdr x))
21
22 ;;;
23 ;;; CONS
24 ;;; turn repetitious cons's into a list*
25 ;;;
26
27 (define-compiler-macro cons (&whole whole &rest args)
28 (labels ((cons-to-lista (x)
29 (let ((tem (last x)))
30 (if (and (consp tem)
31 (consp (car tem))
32 (eq (caar tem) 'CONS)
33 (eql (length (cdar tem)) 2))
34 (cons-to-lista (append (butlast x) (cdar tem)))
35 x))))
36 (let (temp)
37 (if (and (eql (length args) 2)
38 (not (eq args (setq temp (cons-to-lista args)))))
39 (if (equal '(nil) (last temp))
40 (cons 'LIST (butlast temp))
41 (cons 'LIST* temp))
42 whole))))
43
44 ;;;
45 ;;; RPLACA / RPLACD
46 ;;;
47
48 (define-compiler-macro rplaca (&whole whole place value)
49 (if (policy-inline-accessors)
50 `(ffi:c-inline (,(if (policy-assume-right-type)
51 place
52 `(ext:checked-value cons ,place))
53 ,value)
54 (:object :object) :object
55 "(ECL_CONS_CAR(#0)=#1,#0)" :one-liner t)
56 whole))
57
58 (define-compiler-macro rplacd (&whole whole place value)
59 (if (policy-inline-accessors)
60 `(ffi:c-inline (,(if (policy-assume-right-type)
61 place
62 `(ext:checked-value cons ,place))
63 ,value)
64 (:object :object) :object
65 "(ECL_CONS_CDR(#0)=#1,#0)" :one-liner t)
66 whole))
67
68 ;;;
69 ;;; NTH / NTHCDR
70 ;;;
71
72 (define-compiler-macro nth (&whole whole n list)
73 (case n
74 (0 `(car ,list))
75 (1 `(cadr ,list))
76 (2 `(caddr ,list))
77 (3 `(cadddr ,list))
78 (4 `(car (cddddr ,list)))
79 (5 `(cadr (cddddr ,list)))
80 (6 `(caddr (cddddr ,list)))
81 (7 `(cadddr (cddddr ,list)))
82 (t whole)))
83
84 (define-compiler-macro nthcdr (&whole whole n list)
85 (case n
86 (0 list)
87 (1 `(cdr ,list))
88 (2 `(cddr ,list))
89 (3 `(cdddr ,list))
90 (4 `(cddddr ,list))
91 (5 `(cdr (cddddr ,list)))
92 (6 `(cddr (cddddr ,list)))
93 (7 `(cdddr (cddddr ,list)))
94 (t whole)))
95
96 ;;;
97 ;;; FIRST, SECOND, THIRD, ...
98 ;;;
99
100 (progn .
101 #.(loop for n in '(first second third fourth fifth sixth seventh eighth ninth tenth)
102 for i from 0
103 collect `(define-compiler-macro ,n (x) (list 'nth ,i x))))
104
105 (define-compiler-macro rest (x) `(cdr ,x))
106
107 ;;;
108 ;;; POP
109 ;;;
110
111 (define-compiler-macro pop (&whole whole place &environment env)
112 (if (policy-inline-accessors)
113 (multiple-value-bind (vars vals stores store-form access-form)
114 (get-setf-expansion place env)
115 (let* ((store-var (first stores))
116 (saved-place (gensym)))
117 `(let* ,(mapcar #'list
118 (append vars (list saved-place))
119 (append vals (list access-form)))
120 (declare (:read-only ,@vars)) ; Beppe
121 (optional-type-check ,saved-place list)
122 (when ,saved-place
123 (let ((,store-var (si:cons-cdr ,saved-place)))
124 (declare (:read-only ,store-var))
125 ,store-form
126 (setq ,saved-place (si:cons-car ,saved-place))))
127 ,saved-place)))
128 whole))