-
Notifications
You must be signed in to change notification settings - Fork 2
/
primitive-call.lisp
330 lines (300 loc) · 12.4 KB
/
primitive-call.lisp
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
;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
;;; Copyright (c) 2009 David Lichteblau. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :qt)
(defun binding-for-ctor (method instance)
(let* ((<module> (ldb-module (qmethod-class method)))
(data (data-ref <module>)))
(if (typep instance 'dynamic-object)
(data-fat data)
(data-thin data))))
;; old-style NEW usage for INITIALIZE-INSTANCE methods kept around for
;; compatibility.
(defun new (instance &rest args)
(check-type instance dynamic-object)
(apply #'interpret-new instance args))
(defun interpret-new (class-or-instance &rest args)
(let ((instance (full-resolve-ctor-this class-or-instance)))
(funcall (resolve-new instance args) instance args)))
(defun make-symbols (prefix number)
(loop for i below number
collect (make-symbol (format nil "~A-~D" prefix i))))
(defun signature-type (object)
(typecase object
((and abstract-qobject (not dynamic-object))
(qobject-class object))
(string
;; avoid having the length slip into the type
'string)
;; similar issues:
((signed-byte 32) '(signed-byte 32))
((unsigned-byte 32) '(unsigned-byte 32))
((signed-byte 64) '(signed-byte 64))
((unsigned-byte 64) '(unsigned-byte 64))
(t
(type-of object))))
(defun parse-optimized-call-args (forms)
(let ((type nil))
(iter (for form in forms)
(cond
((keywordp form)
(when type
(error "duplicate type specification: ~A / ~A" type form))
(setf type form))
(t
(collect type into types)
(collect form into clean-forms)
(setf type nil)))
(finally
(return (values types clean-forms))))))
(defun make-optimized (instance method &key instance-resolver args resolver)
(multiple-value-bind (fix-types args) (parse-optimized-call-args args)
(let ((argsyms (make-symbols 'arg (length args)))
(sigsyms (make-symbols 'sig (length args))))
`(let ((instance ,(funcall instance-resolver instance))
(method ,method)
(types ',fix-types)
,@(iter (for arg in args)
(for sym in argsyms)
(collect `(,sym ,arg))))
(let ((args (list ,@argsyms))
,@(iter (for sig in sigsyms)
(for arg in argsyms)
(collect `(,sig (signature-type ,arg)))))
(multiple-value-bind (instance-qclass instance-extra-sig)
(typecase instance
(integer
(values instance :static))
(dynamic-object
(values (qobject-class instance)
(class-generation (class-of instance))))
(t
(values (qobject-class instance) :instance)))
(cached-values-bind (fun) ,resolver
((instance-qclass :hash t)
(instance-extra-sig)
(method)
,@(loop for sig in sigsyms
collect `(,sig :hash sxhash)))
(funcall fun instance args))))))))
(defmacro optimized-call (allow-override-p instance method &rest args)
(make-optimized instance method
:instance-resolver #'compile-time-resolve-this
:args args
:resolver `(resolve-call ,allow-override-p instance method args types)))
(defmacro optimized-new (class-or-instance &rest args)
(make-optimized class-or-instance nil
:instance-resolver #'compile-time-resolve-ctor-this
:args args
:resolver `(resolve-new instance args types)))
(defun resolve-call (allow-override-p instance method args &optional fix-types)
;; (format *trace-output* "cache miss for ~A::~A~%" instance method)
(let ((name method)
(method (etypecase method
(integer method)
(string (find-applicable-method
instance method args fix-types)))))
(unless method
(error "No applicable method ~A found on ~A with arguments ~A"
name instance args))
(let* ((precompiled-override
(when allow-override-p
(find-method-override instance method)))
(arglist-marshaller
(arglist-marshaller args (list-qmethod-argument-types method)))
(classfn
(qclass-trampoline-fun (qmethod-class method)))
(method-index
(qmethod-classfn-index method))
(rtype
(qmethod-return-type method))
(return-value-function
(unmarshaller rtype)))
(cond
((integerp instance)
(unless (qmethod-static-p method)
(error "not a static method"))
(assert (not precompiled-override))
(lambda (<class> args)
(declare (ignore <class>))
(%%call (cffi:null-pointer)
args
arglist-marshaller
classfn
method-index
return-value-function)))
(t
(let ((<from> (qobject-class instance)))
(multiple-value-bind (castfn <to>)
(resolve-cast <from> (qmethod-class method))
(let ((cont
(if precompiled-override
(lambda (actual-instance args)
(override precompiled-override
actual-instance method args))
(lambda (actual-instance args)
(%%call (perform-cast actual-instance castfn <from> <to>)
args
arglist-marshaller
classfn
method-index
return-value-function)))))
(if (alexandria:starts-with #\~ (qmethod-name method))
(lambda (actual-instance args)
(note-deleted actual-instance)
(funcall cont actual-instance args))
cont)))))))))
(defun resolve-new (instance args &optional fix-types)
;; (format *trace-output* "cache miss for #_new ~A~%" instance)
(let* ((class (qobject-class instance))
(method
(qclass-find-applicable-method class
(qclass-name class)
args
fix-types)))
(unless method
(error "No applicable constructor ~A found for arguments ~A"
(qclass-name class) args))
(assert (eq class (qtype-class (qmethod-return-type method))))
(let ((classfn (qclass-trampoline-fun (qmethod-class method)))
(method-index (qmethod-classfn-index method))
(binding (binding-for-ctor method instance))
(arglist-marshaller
(arglist-marshaller args (list-qmethod-argument-types method))))
(named-lambda new-continuation (instance args)
(%%new instance
args
arglist-marshaller
classfn
method-index
binding)))))
(defun set-object-binding (classfn object binding)
(cffi:with-foreign-object (stack '|union StackItem| 2)
(setf (cffi:foreign-slot-value
(cffi:mem-aref stack '|union StackItem| 1)
'|union StackItem|
'ptr)
binding)
;; Method index 0 sets the binding
(call-class-fun classfn 0 object stack)))
(defun %%new (instance
args
arglist-marshaller
classfn
method-index
binding)
(%%call (cffi:null-pointer)
args arglist-marshaller classfn method-index
(lambda (stack)
(let ((new-object
(cffi:foreign-slot-value stack '|union StackItem| 'ptr)))
(set-object-binding classfn new-object binding)
(setf (qobject-pointer instance) new-object))))
(cache! instance))
(defun interpret-call (instance method &rest args)
(%interpret-call t instance method args))
(defun interpret-call-without-override (instance method &rest args)
(%interpret-call nil instance method args))
(defun full-resolve-this (instance)
(etypecase instance
(qobject instance)
(integer instance)
(symbol (class-effective-class (find-class instance)))
(qt-class (class-effective-class instance))
(string (find-qclass instance))))
(defun compile-time-resolve-this (instance)
(etypecase instance
(string `(with-cache () (find-qclass ,instance)))
((cons (eql quote) (cons symbol null))
`(class-effective-class (find-class ,instance)))
(t `(full-resolve-this ,instance))))
(defun full-resolve-ctor-this (instance)
(typecase instance
(qobject
instance)
(integer
(make-instance 'qobject :class instance :pointer :unborn))
(string
(make-instance 'qobject :class (find-qclass instance) :pointer :unborn))
(qt-class
(make-instance instance :pointer :unborn))))
(defun compile-time-resolve-ctor-this (instance)
(etypecase instance
(string `(make-instance 'qobject
:class (with-cache () (find-qclass ,instance))
:pointer :unborn))
(t `(full-resolve-ctor-this ,instance))))
(declaim (inline call-class-fun))
(defun call-class-fun (function method-index object stack)
(cffi:foreign-funcall-pointer
function
()
:short method-index
:pointer object
:pointer stack
:void))
(declaim (inline %%call))
(defun %%call (casted-instance-pointer
args
arglist-marshaller
classfn
method-index
return-value-function)
(funcall arglist-marshaller
args
(lambda (stack)
(call-class-fun classfn method-index casted-instance-pointer
stack)
(funcall return-value-function stack))))
(defun argstep-marshaller (for-values argtypes i)
(if argtypes
(let ((marshal-thunk (marshaller (car for-values)
(car argtypes)))
(next-thunk (argstep-marshaller (cdr for-values)
(cdr argtypes)
(1+ i))))
(lambda (stack arglist final-cont)
(funcall marshal-thunk
(car arglist)
(cffi:mem-aref stack '|union StackItem| i)
(lambda ()
(funcall next-thunk
stack
(cdr arglist)
final-cont)))))
(lambda (stack arglist final-cont)
(declare (ignore arglist))
(funcall final-cont stack))))
(defun arglist-marshaller (for-values argtypes)
(let ((thunk (argstep-marshaller for-values argtypes 1))
(n (1+ (length argtypes))))
(named-lambda arglist-marshaller (arglist final-cont)
(cffi:with-foreign-object (stack '|union StackItem| n)
(funcall thunk stack arglist final-cont)))))
(defun %interpret-call (allow-override-p instance method args)
(let ((instance (full-resolve-this instance)))
(funcall (resolve-call allow-override-p instance method args)
instance
args)))