1 | ;;; jvm-class-file.lisp |
---|
2 | ;;; |
---|
3 | ;;; Copyright (C) 2010 Erik Huelsmann |
---|
4 | ;;; $Id: jvm-class-file.lisp 12895 2010-08-13 21:10:39Z ehuelsmann $ |
---|
5 | ;;; |
---|
6 | ;;; This program is free software; you can redistribute it and/or |
---|
7 | ;;; modify it under the terms of the GNU General Public License |
---|
8 | ;;; as published by the Free Software Foundation; either version 2 |
---|
9 | ;;; of the License, or (at your option) any later version. |
---|
10 | ;;; |
---|
11 | ;;; This program is distributed in the hope that it will be useful, |
---|
12 | ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | ;;; GNU General Public License for more details. |
---|
15 | ;;; |
---|
16 | ;;; You should have received a copy of the GNU General Public License |
---|
17 | ;;; along with this program; if not, write to the Free Software |
---|
18 | ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
19 | ;;; |
---|
20 | ;;; As a special exception, the copyright holders of this library give you |
---|
21 | ;;; permission to link this library with independent modules to produce an |
---|
22 | ;;; executable, regardless of the license terms of these independent |
---|
23 | ;;; modules, and to copy and distribute the resulting executable under |
---|
24 | ;;; terms of your choice, provided that you also meet, for each linked |
---|
25 | ;;; independent module, the terms and conditions of the license of that |
---|
26 | ;;; module. An independent module is a module which is not derived from |
---|
27 | ;;; or based on this library. If you modify this library, you may extend |
---|
28 | ;;; this exception to your version of the library, but you are not |
---|
29 | ;;; obligated to do so. If you do not wish to do so, delete this |
---|
30 | ;;; exception statement from your version. |
---|
31 | |
---|
32 | (in-package "JVM") |
---|
33 | |
---|
34 | #| |
---|
35 | |
---|
36 | The general design of the class-file writer is to have generic |
---|
37 | - human readable - representations of the class being generated |
---|
38 | during the construction and manipulation phases. |
---|
39 | |
---|
40 | After completing the creation/manipulation of the class, all its |
---|
41 | components will be finalized. This process translates readable |
---|
42 | (e.g. string) representations to indices to be stored on disc. |
---|
43 | |
---|
44 | The only thing to be done after finalization is sending the |
---|
45 | output to a stream ("writing"). |
---|
46 | |
---|
47 | |
---|
48 | Finalization happens highest-level first. As an example, take a |
---|
49 | method with exception handlers. The exception handlers are stored |
---|
50 | as attributes in the class file structure. They are children of the |
---|
51 | method's Code attribute. In this example, the body of the Code |
---|
52 | attribute (the higher level) gets finalized before the attributes. |
---|
53 | The reason to do so is that the exceptions need to refer to labels |
---|
54 | (offsets) in the Code segment. |
---|
55 | |
---|
56 | |
---|
57 | |# |
---|
58 | |
---|
59 | |
---|
60 | (defun map-primitive-type (type) |
---|
61 | "Maps a symbolic primitive type name to its Java string representation." |
---|
62 | (case type |
---|
63 | (:int "I") |
---|
64 | (:long "J") |
---|
65 | (:float "F") |
---|
66 | (:double "D") |
---|
67 | (:boolean "Z") |
---|
68 | (:char "C") |
---|
69 | (:byte "B") |
---|
70 | (:short "S") |
---|
71 | ((nil :void) "V"))) |
---|
72 | |
---|
73 | |
---|
74 | #| |
---|
75 | |
---|
76 | The `class-name' facility helps to abstract from "this instruction takes |
---|
77 | a reference" and "this instruction takes a class name". We simply pass |
---|
78 | the class name around and the instructions themselves know which |
---|
79 | representation to use. |
---|
80 | |
---|
81 | |# |
---|
82 | |
---|
83 | (defstruct (class-name (:conc-name class-) |
---|
84 | (:constructor %make-class-name)) |
---|
85 | "Used for class identification. |
---|
86 | |
---|
87 | The caller should instantiate only one `class-name' per class, as they are |
---|
88 | used as class identifiers and compared using EQ. |
---|
89 | |
---|
90 | Some instructions need a class argument, others need a reference identifier. |
---|
91 | This class is used to abstract from the difference." |
---|
92 | name-internal |
---|
93 | ref |
---|
94 | array-class ;; cached array class reference |
---|
95 | ;; keeping a reference to the associated array class allows class |
---|
96 | ;; name comparisons to be EQ: all classes should exist only once, |
---|
97 | ) |
---|
98 | |
---|
99 | (defun make-class-name (name) |
---|
100 | "Creates a `class-name' structure for the class or interface `name'. |
---|
101 | |
---|
102 | `name' should be specified using Java representation, which is converted |
---|
103 | to 'internal' (JVM) representation by this function." |
---|
104 | (setf name (substitute #\/ #\. name)) |
---|
105 | (%make-class-name :name-internal name |
---|
106 | :ref (concatenate 'string "L" name ";"))) |
---|
107 | |
---|
108 | (defun class-array (class-name) |
---|
109 | "Returns a class-name representing an array of `class-name'. |
---|
110 | For multi-dimensional arrays, call this function multiple times, using |
---|
111 | its own result. |
---|
112 | |
---|
113 | This function can be called multiple times on the same `class-name' without |
---|
114 | violating the 'only one instance' requirement: the returned value is cached |
---|
115 | and used on successive calls." |
---|
116 | (unless (class-array-class class-name) |
---|
117 | ;; Alessio Stalla found by dumping a class file that the JVM uses |
---|
118 | ;; the same representation (ie '[L<class-name>;') in CHECKCAST as |
---|
119 | ;; it does in field references, meaning the class name and class ref |
---|
120 | ;; are identified by the same string |
---|
121 | (let ((name-and-ref (concatenate 'string "[" (class-ref class-name)))) |
---|
122 | (setf (class-array-class class-name) |
---|
123 | (%make-class-name :name-internal name-and-ref |
---|
124 | :ref name-and-ref)))) |
---|
125 | (class-array-class class-name)) |
---|
126 | |
---|
127 | (defmacro define-class-name (symbol java-dotted-name &optional documentation) |
---|
128 | "Convenience macro to define constants for `class-name' structures, |
---|
129 | initialized from the `java-dotted-name'." |
---|
130 | `(defconstant ,symbol (make-class-name ,java-dotted-name) |
---|
131 | ,documentation)) |
---|
132 | |
---|
133 | (define-class-name +java-object+ "java.lang.Object") |
---|
134 | (define-class-name +java-string+ "java.lang.String") |
---|
135 | (define-class-name +java-system+ "java.lang.System") |
---|
136 | (define-class-name +lisp-object+ "org.armedbear.lisp.LispObject") |
---|
137 | (defconstant +lisp-object-array+ (class-array +lisp-object+)) |
---|
138 | (define-class-name +lisp-simple-string+ "org.armedbear.lisp.SimpleString") |
---|
139 | (define-class-name +lisp+ "org.armedbear.lisp.Lisp") |
---|
140 | (define-class-name +lisp-nil+ "org.armedbear.lisp.Nil") |
---|
141 | (define-class-name +lisp-class+ "org.armedbear.lisp.LispClass") |
---|
142 | (define-class-name +lisp-symbol+ "org.armedbear.lisp.Symbol") |
---|
143 | (define-class-name +lisp-thread+ "org.armedbear.lisp.LispThread") |
---|
144 | (define-class-name +lisp-closure-binding+ "org.armedbear.lisp.ClosureBinding") |
---|
145 | (defconstant +closure-binding-array+ (class-array +lisp-closure-binding+)) |
---|
146 | (define-class-name +lisp-integer+ "org.armedbear.lisp.LispInteger") |
---|
147 | (define-class-name +lisp-fixnum+ "org.armedbear.lisp.Fixnum") |
---|
148 | (defconstant +lisp-fixnum-array+ (class-array +lisp-fixnum+)) |
---|
149 | (define-class-name +lisp-bignum+ "org.armedbear.lisp.Bignum") |
---|
150 | (define-class-name +lisp-single-float+ "org.armedbear.lisp.SingleFloat") |
---|
151 | (define-class-name +lisp-double-float+ "org.armedbear.lisp.DoubleFloat") |
---|
152 | (define-class-name +lisp-cons+ "org.armedbear.lisp.Cons") |
---|
153 | (define-class-name +lisp-load+ "org.armedbear.lisp.Load") |
---|
154 | (define-class-name +lisp-character+ "org.armedbear.lisp.LispCharacter") |
---|
155 | (defconstant +lisp-character-array+ (class-array +lisp-character+)) |
---|
156 | (define-class-name +lisp-structure-object+ "org.armedbear.lisp.StructureObject") |
---|
157 | (define-class-name +lisp-simple-vector+ "org.armedbear.lisp.SimpleVector") |
---|
158 | (define-class-name +lisp-abstract-string+ "org.armedbear.lisp.AbstractString") |
---|
159 | (define-class-name +lisp-abstract-vector+ "org.armedbear.lisp.AbstractVector") |
---|
160 | (define-class-name +lisp-abstract-bit-vector+ |
---|
161 | "org.armedbear.lisp.AbstractBitVector") |
---|
162 | (define-class-name +lisp-environment+ "org.armedbear.lisp.Environment") |
---|
163 | (define-class-name +lisp-special-binding+ "org.armedbear.lisp.SpecialBinding") |
---|
164 | (define-class-name +lisp-special-bindings-mark+ |
---|
165 | "org.armedbear.lisp.SpecialBindingsMark") |
---|
166 | (define-class-name +lisp-throw+ "org.armedbear.lisp.Throw") |
---|
167 | (define-class-name +lisp-return+ "org.armedbear.lisp.Return") |
---|
168 | (define-class-name +lisp-go+ "org.armedbear.lisp.Go") |
---|
169 | (define-class-name +lisp-primitive+ "org.armedbear.lisp.Primitive") |
---|
170 | (define-class-name +lisp-eql-hash-table+ "org.armedbear.lisp.EqlHashTable") |
---|
171 | (define-class-name +lisp-hash-table+ "org.armedbear.lisp.HashTable") |
---|
172 | (define-class-name +lisp-package+ "org.armedbear.lisp.Package") |
---|
173 | (define-class-name +lisp-readtable+ "org.armedbear.lisp.Readtable") |
---|
174 | (define-class-name +lisp-stream+ "org.armedbear.lisp.Stream") |
---|
175 | (define-class-name +lisp-closure+ "org.armedbear.lisp.Closure") |
---|
176 | (define-class-name +lisp-compiled-closure+ "org.armedbear.lisp.CompiledClosure") |
---|
177 | (define-class-name +lisp-closure-parameter+ |
---|
178 | "org.armedbear.lisp.Closure$Parameter") |
---|
179 | (defconstant +lisp-closure-parameter-array+ |
---|
180 | (class-array +lisp-closure-parameter+)) |
---|
181 | |
---|
182 | #| |
---|
183 | |
---|
184 | Lisp-side descriptor representation: |
---|
185 | |
---|
186 | - list: a list starting with a method return value, followed by |
---|
187 | the argument types |
---|
188 | - keyword: the primitive type associated with that keyword |
---|
189 | - class-name structure instance: the class-ref value |
---|
190 | |
---|
191 | The latter two can be converted to a Java representation using |
---|
192 | the `internal-field-ref' function, the former is to be fed to |
---|
193 | `descriptor'. |
---|
194 | |
---|
195 | |# |
---|
196 | |
---|
197 | (defun internal-field-type (field-type) |
---|
198 | "Returns a string containing the JVM-internal representation |
---|
199 | of `field-type', which should either be a symbol identifying a primitive |
---|
200 | type, or a `class-name' structure identifying a class or interface." |
---|
201 | (if (symbolp field-type) |
---|
202 | (map-primitive-type field-type) |
---|
203 | (class-name-internal field-type))) |
---|
204 | |
---|
205 | (defun internal-field-ref (field-type) |
---|
206 | "Returns a string containing the JVM-internal representation of a reference |
---|
207 | to `field-type', which should either be a symbol identifying a primitive |
---|
208 | type, or a `class-name' structure identifying a class or interface." |
---|
209 | (if (symbolp field-type) |
---|
210 | (map-primitive-type field-type) |
---|
211 | (class-ref field-type))) |
---|
212 | |
---|
213 | (defun descriptor (return-type &rest argument-types) |
---|
214 | "Returns a string describing the `return-type' and `argument-types' |
---|
215 | in JVM-internal representation." |
---|
216 | (let* ((arg-strings (mapcar #'internal-field-ref argument-types)) |
---|
217 | (ret-string (internal-field-ref return-type)) |
---|
218 | (size (+ 2 (reduce #'+ arg-strings |
---|
219 | :key #'length |
---|
220 | :initial-value (length ret-string)))) |
---|
221 | (str (make-array size :fill-pointer 0 :element-type 'character))) |
---|
222 | (with-output-to-string (s str) |
---|
223 | (princ #\( s) |
---|
224 | (dolist (arg-string arg-strings) |
---|
225 | (princ arg-string s)) |
---|
226 | (princ #\) s) |
---|
227 | (princ ret-string s)) |
---|
228 | str) |
---|
229 | ;; (format nil "(~{~A~})~A" |
---|
230 | ;; (internal-field-ref return-type)) |
---|
231 | ) |
---|
232 | |
---|
233 | (defun descriptor-stack-effect (return-type &rest argument-types) |
---|
234 | "Returns the effect on the stack position of the `argument-types' and |
---|
235 | `return-type' of a method call. |
---|
236 | |
---|
237 | If the method consumes an implicit `this' argument, this function does not |
---|
238 | take that effect into account." |
---|
239 | (flet ((type-stack-effect (arg) |
---|
240 | (case arg |
---|
241 | ((:long :double) 2) |
---|
242 | ((nil :void) 0) |
---|
243 | (otherwise 1)))) |
---|
244 | (+ (reduce #'- argument-types |
---|
245 | :key #'type-stack-effect |
---|
246 | :initial-value 0) |
---|
247 | (type-stack-effect return-type)))) |
---|
248 | |
---|
249 | |
---|
250 | (defstruct pool |
---|
251 | ;; `index' contains the index of the last allocated slot (0 == empty) |
---|
252 | ;; "A constant pool entry is considered valid if it has |
---|
253 | ;; an index greater than 0 (zero) and less than pool-count" |
---|
254 | (index 0) |
---|
255 | entries-list |
---|
256 | ;; the entries hash stores raw values, except in case of string and |
---|
257 | ;; utf8, because both are string values |
---|
258 | (entries (make-hash-table :test #'equal :size 2048 :rehash-size 2.0))) |
---|
259 | |
---|
260 | |
---|
261 | (defstruct constant |
---|
262 | "Structure to be included in all constant sub-types." |
---|
263 | tag |
---|
264 | index) |
---|
265 | |
---|
266 | (defparameter +constant-type-map+ |
---|
267 | '((:class 7 1) |
---|
268 | (:field-ref 9 1) |
---|
269 | (:method-ref 10 1) |
---|
270 | ;; (:interface-method-ref 11) |
---|
271 | (:string 8 1) |
---|
272 | (:integer 3 1) |
---|
273 | (:float 4 1) |
---|
274 | (:long 5 2) |
---|
275 | (:double 6 2) |
---|
276 | (:name-and-type 12 1) |
---|
277 | (:utf8 1 1))) |
---|
278 | |
---|
279 | (defstruct (constant-class (:constructor make-constant-class (index name-index)) |
---|
280 | (:include constant |
---|
281 | (tag 7))) |
---|
282 | "Structure holding information on a 'class' type item in the constant pool." |
---|
283 | name-index) |
---|
284 | |
---|
285 | (defstruct (constant-member-ref (:constructor |
---|
286 | %make-constant-member-ref |
---|
287 | (tag index class-index name/type-index)) |
---|
288 | (:include constant)) |
---|
289 | "Structure holding information on a member reference type item |
---|
290 | (a field, method or interface method reference) in the constant pool." |
---|
291 | class-index |
---|
292 | name/type-index) |
---|
293 | |
---|
294 | (declaim (inline make-constant-field-ref make-constant-method-ref |
---|
295 | make-constant-interface-method-ref)) |
---|
296 | (defun make-constant-field-ref (index class-index name/type-index) |
---|
297 | "Creates a `constant-member-ref' instance containing a field reference." |
---|
298 | (%make-constant-member-ref 9 index class-index name/type-index)) |
---|
299 | |
---|
300 | (defun make-constant-method-ref (index class-index name/type-index) |
---|
301 | "Creates a `constant-member-ref' instance containing a method reference." |
---|
302 | (%make-constant-member-ref 10 index class-index name/type-index)) |
---|
303 | |
---|
304 | (defun make-constant-interface-method-ref (index class-index name/type-index) |
---|
305 | "Creates a `constant-member-ref' instance containing an |
---|
306 | interface-method reference." |
---|
307 | (%make-constant-member-ref 11 index class-index name/type-index)) |
---|
308 | |
---|
309 | (defstruct (constant-string (:constructor |
---|
310 | make-constant-string (index value-index)) |
---|
311 | (:include constant |
---|
312 | (tag 8))) |
---|
313 | "Structure holding information on a 'string' type item in the constant pool." |
---|
314 | value-index) |
---|
315 | |
---|
316 | (defstruct (constant-float/int (:constructor |
---|
317 | %make-constant-float/int (tag index value)) |
---|
318 | (:include constant)) |
---|
319 | "Structure holding information on a 'float' or 'integer' type item |
---|
320 | in the constant pool." |
---|
321 | value) |
---|
322 | |
---|
323 | (declaim (inline make-constant-float make-constant-int)) |
---|
324 | (defun make-constant-float (index value) |
---|
325 | "Creates a `constant-float/int' structure instance containing a float." |
---|
326 | (%make-constant-float/int 4 index value)) |
---|
327 | |
---|
328 | (defun make-constant-int (index value) |
---|
329 | "Creates a `constant-float/int' structure instance containing an int." |
---|
330 | (%make-constant-float/int 3 index value)) |
---|
331 | |
---|
332 | (defstruct (constant-double/long (:constructor |
---|
333 | %make-constant-double/long (tag index value)) |
---|
334 | (:include constant)) |
---|
335 | "Structure holding information on a 'double' or 'long' type item |
---|
336 | in the constant pool." |
---|
337 | value) |
---|
338 | |
---|
339 | (declaim (inline make-constant-double make-constant-float)) |
---|
340 | (defun make-constant-double (index value) |
---|
341 | "Creates a `constant-double/long' structure instance containing a double." |
---|
342 | (%make-constant-double/long 6 index value)) |
---|
343 | |
---|
344 | (defun make-constant-long (index value) |
---|
345 | "Creates a `constant-double/long' structure instance containing a long." |
---|
346 | (%make-constant-double/long 5 index value)) |
---|
347 | |
---|
348 | (defstruct (constant-name/type (:constructor |
---|
349 | make-constant-name/type (index |
---|
350 | name-index |
---|
351 | descriptor-index)) |
---|
352 | (:include constant |
---|
353 | (tag 12))) |
---|
354 | "Structure holding information on a 'name-and-type' type item in the |
---|
355 | constant pool; this type of element is used by 'member-ref' type items." |
---|
356 | name-index |
---|
357 | descriptor-index) |
---|
358 | |
---|
359 | (defstruct (constant-utf8 (:constructor make-constant-utf8 (index value)) |
---|
360 | (:include constant |
---|
361 | (tag 1))) |
---|
362 | "Structure holding information on a 'utf8' type item in the constant pool; |
---|
363 | |
---|
364 | This type of item is used for text representation of identifiers |
---|
365 | and string contents." |
---|
366 | value) |
---|
367 | |
---|
368 | |
---|
369 | (defun pool-add-class (pool class) |
---|
370 | "Returns the index of the constant-pool class item for `class'. |
---|
371 | |
---|
372 | `class' must be an instance of `class-name'." |
---|
373 | (let ((entry (gethash class (pool-entries pool)))) |
---|
374 | (unless entry |
---|
375 | (let ((utf8 (pool-add-utf8 pool (class-name-internal class)))) |
---|
376 | (setf entry |
---|
377 | (make-constant-class (incf (pool-index pool)) utf8) |
---|
378 | (gethash class (pool-entries pool)) entry)) |
---|
379 | (push entry (pool-entries-list pool))) |
---|
380 | (constant-index entry))) |
---|
381 | |
---|
382 | (defun pool-add-field-ref (pool class name type) |
---|
383 | "Returns the index of the constant-pool item which denotes a reference |
---|
384 | to the `name' field of the `class', being of `type'. |
---|
385 | |
---|
386 | `class' should be an instance of `class-name'. |
---|
387 | `name' is a string. |
---|
388 | `type' is a field-type (see `internal-field-type')" |
---|
389 | (let ((entry (gethash (acons name type class) (pool-entries pool)))) |
---|
390 | (unless entry |
---|
391 | (let ((c (pool-add-class pool class)) |
---|
392 | (n/t (pool-add-name/type pool name type))) |
---|
393 | (setf entry (make-constant-field-ref (incf (pool-index pool)) c n/t) |
---|
394 | (gethash (acons name type class) (pool-entries pool)) entry)) |
---|
395 | (push entry (pool-entries-list pool))) |
---|
396 | (constant-index entry))) |
---|
397 | |
---|
398 | (defun pool-add-method-ref (pool class name type) |
---|
399 | "Returns the index of the constant-pool item which denotes a reference |
---|
400 | to the method with `name' in `class', which is of `type'. |
---|
401 | |
---|
402 | Here, `type' is a method descriptor, which defines the argument types |
---|
403 | and return type. `class' is an instance of `class-name'." |
---|
404 | (let ((entry (gethash (acons name type class) (pool-entries pool)))) |
---|
405 | (unless entry |
---|
406 | (let ((c (pool-add-class pool class)) |
---|
407 | (n/t (pool-add-name/type pool name type))) |
---|
408 | (setf entry (make-constant-method-ref (incf (pool-index pool)) c n/t) |
---|
409 | (gethash (acons name type class) (pool-entries pool)) entry)) |
---|
410 | (push entry (pool-entries-list pool))) |
---|
411 | (constant-index entry))) |
---|
412 | |
---|
413 | (defun pool-add-interface-method-ref (pool class name type) |
---|
414 | "Returns the index of the constant-pool item which denotes a reference to |
---|
415 | the method `name' in the interface `class', which is of `type'. |
---|
416 | |
---|
417 | See `pool-add-method-ref' for remarks." |
---|
418 | (let ((entry (gethash (acons name type class) (pool-entries pool)))) |
---|
419 | (unless entry |
---|
420 | (let ((c (pool-add-class pool class)) |
---|
421 | (n/t (pool-add-name/type pool name type))) |
---|
422 | (setf entry |
---|
423 | (make-constant-interface-method-ref (incf (pool-index pool)) c n/t) |
---|
424 | (gethash (acons name type class) (pool-entries pool)) entry)) |
---|
425 | (push entry (pool-entries-list pool))) |
---|
426 | (constant-index entry))) |
---|
427 | |
---|
428 | (defun pool-add-string (pool string) |
---|
429 | "Returns the index of the constant-pool item denoting the string." |
---|
430 | (let ((entry (gethash (cons 8 string) ;; 8 == string-tag |
---|
431 | (pool-entries pool)))) |
---|
432 | (unless entry |
---|
433 | (let ((utf8 (pool-add-utf8 pool string))) |
---|
434 | (setf entry (make-constant-string (incf (pool-index pool)) utf8) |
---|
435 | (gethash (cons 8 string) (pool-entries pool)) entry)) |
---|
436 | (push entry (pool-entries-list pool))) |
---|
437 | (constant-index entry))) |
---|
438 | |
---|
439 | (defun pool-add-int (pool int) |
---|
440 | "Returns the index of the constant-pool item denoting the int." |
---|
441 | (let ((entry (gethash (cons 3 int) (pool-entries pool)))) |
---|
442 | (unless entry |
---|
443 | (setf entry (make-constant-int (incf (pool-index pool)) int) |
---|
444 | (gethash (cons 3 int) (pool-entries pool)) entry) |
---|
445 | (push entry (pool-entries-list pool))) |
---|
446 | (constant-index entry))) |
---|
447 | |
---|
448 | (defun pool-add-float (pool float) |
---|
449 | "Returns the index of the constant-pool item denoting the float." |
---|
450 | (let ((entry (gethash (cons 4 float) (pool-entries pool)))) |
---|
451 | (unless entry |
---|
452 | (setf entry (make-constant-float (incf (pool-index pool)) |
---|
453 | (sys::%float-bits float)) |
---|
454 | (gethash (cons 4 float) (pool-entries pool)) entry) |
---|
455 | (push entry (pool-entries-list pool))) |
---|
456 | (constant-index entry))) |
---|
457 | |
---|
458 | (defun pool-add-long (pool long) |
---|
459 | "Returns the index of the constant-pool item denoting the long." |
---|
460 | (let ((entry (gethash (cons 5 long) (pool-entries pool)))) |
---|
461 | (unless entry |
---|
462 | (setf entry (make-constant-long (incf (pool-index pool)) long) |
---|
463 | (gethash (cons 5 long) (pool-entries pool)) entry) |
---|
464 | (push entry (pool-entries-list pool)) |
---|
465 | (incf (pool-index pool))) ;; double index increase; long takes 2 slots |
---|
466 | (constant-index entry))) |
---|
467 | |
---|
468 | (defun pool-add-double (pool double) |
---|
469 | "Returns the index of the constant-pool item denoting the double." |
---|
470 | (let ((entry (gethash (cons 6 double) (pool-entries pool)))) |
---|
471 | (unless entry |
---|
472 | (setf entry (make-constant-double (incf (pool-index pool)) |
---|
473 | (sys::%float-bits double)) |
---|
474 | (gethash (cons 6 double) (pool-entries pool)) entry) |
---|
475 | (push entry (pool-entries-list pool)) |
---|
476 | (incf (pool-index pool))) ;; double index increase; 'double' takes 2 slots |
---|
477 | (constant-index entry))) |
---|
478 | |
---|
479 | (defun pool-add-name/type (pool name type) |
---|
480 | "Returns the index of the constant-pool item denoting |
---|
481 | the name/type identifier." |
---|
482 | (let ((entry (gethash (cons name type) (pool-entries pool))) |
---|
483 | (internal-type (if (listp type) |
---|
484 | (apply #'descriptor type) |
---|
485 | (internal-field-ref type)))) |
---|
486 | (unless entry |
---|
487 | (let ((n (pool-add-utf8 pool name)) |
---|
488 | (i-t (pool-add-utf8 pool internal-type))) |
---|
489 | (setf entry (make-constant-name/type (incf (pool-index pool)) n i-t) |
---|
490 | (gethash (cons name type) (pool-entries pool)) entry)) |
---|
491 | (push entry (pool-entries-list pool))) |
---|
492 | (constant-index entry))) |
---|
493 | |
---|
494 | (defun pool-add-utf8 (pool utf8-as-string) |
---|
495 | "Returns the index of the textual value that will be stored in the |
---|
496 | class file as UTF-8 encoded data." |
---|
497 | (let ((entry (gethash (cons 11 utf8-as-string) ;; 11 == utf8 |
---|
498 | (pool-entries pool)))) |
---|
499 | (unless entry |
---|
500 | (setf entry (make-constant-utf8 (incf (pool-index pool)) utf8-as-string) |
---|
501 | (gethash (cons 11 utf8-as-string) (pool-entries pool)) entry) |
---|
502 | (push entry (pool-entries-list pool))) |
---|
503 | (constant-index entry))) |
---|
504 | |
---|
505 | (defstruct (class-file (:constructor |
---|
506 | make-class-file (class superclass access-flags))) |
---|
507 | "Holds the components of a class file." |
---|
508 | (constants (make-pool)) |
---|
509 | access-flags |
---|
510 | class |
---|
511 | superclass |
---|
512 | ;; support for implementing interfaces not yet available |
---|
513 | ;; interfaces |
---|
514 | fields |
---|
515 | methods |
---|
516 | attributes) |
---|
517 | |
---|
518 | (defun class-add-field (class field) |
---|
519 | "Adds a `field' created by `make-field'." |
---|
520 | (push field (class-file-fields class))) |
---|
521 | |
---|
522 | (defun class-field (class name) |
---|
523 | "Finds a field by name." ;; ### strictly speaking, a field is uniquely |
---|
524 | ;; identified by its name and type, not by the name alone. |
---|
525 | (find name (class-file-fields class) |
---|
526 | :test #'string= :key #'field-name)) |
---|
527 | |
---|
528 | (defun class-add-method (class method) |
---|
529 | "Adds a `method' to `class'; the method must have been created using |
---|
530 | `make-method'." |
---|
531 | (push method (class-file-methods class))) |
---|
532 | |
---|
533 | (defun class-methods-by-name (class name) |
---|
534 | "Returns all methods which have `name'." |
---|
535 | (remove name (class-file-methods class) |
---|
536 | :test-not #'string= :key #'method-name)) |
---|
537 | |
---|
538 | (defun class-method (class name return &rest args) |
---|
539 | "Return the method which is (uniquely) identified by its name AND descriptor." |
---|
540 | (let ((return-and-args (cons return args))) |
---|
541 | (find-if #'(lambda (c) |
---|
542 | (and (string= (method-name c) name) |
---|
543 | (equal (method-descriptor c) return-and-args))) |
---|
544 | (class-file-methods class)))) |
---|
545 | |
---|
546 | (defun class-add-attribute (class attribute) |
---|
547 | "Adds `attribute' to the class; attributes must be instances of |
---|
548 | structure classes which include the `attribute' structure class." |
---|
549 | (push attribute (class-file-attributes class))) |
---|
550 | |
---|
551 | (defun class-attribute (class name) |
---|
552 | "Returns the attribute which is named `name'." |
---|
553 | (find name (class-file-attributes class) |
---|
554 | :test #'string= :key #'attribute-name)) |
---|
555 | |
---|
556 | |
---|
557 | (defun finalize-class-file (class) |
---|
558 | "Transforms the representation of the class-file from one |
---|
559 | which allows easy modification to one which works best for serialization. |
---|
560 | |
---|
561 | The class can't be modified after finalization." |
---|
562 | |
---|
563 | ;; constant pool contains constants finalized on addition; |
---|
564 | ;; no need for additional finalization |
---|
565 | |
---|
566 | (setf (class-file-access-flags class) |
---|
567 | (map-flags (class-file-access-flags class))) |
---|
568 | (setf (class-file-superclass class) |
---|
569 | (pool-add-class (class-file-constants class) |
---|
570 | (class-file-superclass class)) |
---|
571 | (class-file-class class) |
---|
572 | (pool-add-class (class-file-constants class) |
---|
573 | (class-file-class class))) |
---|
574 | ;; (finalize-interfaces) |
---|
575 | (dolist (field (class-file-fields class)) |
---|
576 | (finalize-field field class)) |
---|
577 | (dolist (method (class-file-methods class)) |
---|
578 | (finalize-method method class)) |
---|
579 | ;; top-level attributes (no parent attributes to refer to) |
---|
580 | (finalize-attributes (class-file-attributes class) nil class)) |
---|
581 | |
---|
582 | |
---|
583 | (declaim (inline write-u1 write-u2 write-u4 write-s4)) |
---|
584 | (defun write-u1 (n stream) |
---|
585 | (declare (optimize speed)) |
---|
586 | (declare (type (unsigned-byte 8) n)) |
---|
587 | (declare (type stream stream)) |
---|
588 | (write-8-bits n stream)) |
---|
589 | |
---|
590 | (defknown write-u2 (t t) t) |
---|
591 | (defun write-u2 (n stream) |
---|
592 | (declare (optimize speed)) |
---|
593 | (declare (type (unsigned-byte 16) n)) |
---|
594 | (declare (type stream stream)) |
---|
595 | (write-8-bits (logand (ash n -8) #xFF) stream) |
---|
596 | (write-8-bits (logand n #xFF) stream)) |
---|
597 | |
---|
598 | (defknown write-u4 (integer stream) t) |
---|
599 | (defun write-u4 (n stream) |
---|
600 | (declare (optimize speed)) |
---|
601 | (declare (type (unsigned-byte 32) n)) |
---|
602 | (write-u2 (logand (ash n -16) #xFFFF) stream) |
---|
603 | (write-u2 (logand n #xFFFF) stream)) |
---|
604 | |
---|
605 | (declaim (ftype (function (t t) t) write-s4)) |
---|
606 | (defun write-s4 (n stream) |
---|
607 | (declare (optimize speed)) |
---|
608 | (cond ((minusp n) |
---|
609 | (write-u4 (1+ (logxor (- n) #xFFFFFFFF)) stream)) |
---|
610 | (t |
---|
611 | (write-u4 n stream)))) |
---|
612 | |
---|
613 | (declaim (ftype (function (t t t) t) write-ascii)) |
---|
614 | (defun write-ascii (string length stream) |
---|
615 | (declare (type string string)) |
---|
616 | (declare (type (unsigned-byte 16) length)) |
---|
617 | (declare (type stream stream)) |
---|
618 | (write-u2 length stream) |
---|
619 | (dotimes (i length) |
---|
620 | (declare (type (unsigned-byte 16) i)) |
---|
621 | (write-8-bits (char-code (char string i)) stream))) |
---|
622 | |
---|
623 | |
---|
624 | (declaim (ftype (function (t t) t) write-utf8)) |
---|
625 | (defun write-utf8 (string stream) |
---|
626 | (declare (optimize speed)) |
---|
627 | (declare (type string string)) |
---|
628 | (declare (type stream stream)) |
---|
629 | (let ((length (length string)) |
---|
630 | (must-convert nil)) |
---|
631 | (declare (type fixnum length)) |
---|
632 | (dotimes (i length) |
---|
633 | (declare (type fixnum i)) |
---|
634 | (unless (< 0 (char-code (char string i)) #x80) |
---|
635 | (setf must-convert t) |
---|
636 | (return))) |
---|
637 | (if must-convert |
---|
638 | (let ((octets (make-array (* length 2) |
---|
639 | :element-type '(unsigned-byte 8) |
---|
640 | :adjustable t |
---|
641 | :fill-pointer 0))) |
---|
642 | (declare (type (vector (unsigned-byte 8)) octets)) |
---|
643 | (dotimes (i length) |
---|
644 | (declare (type fixnum i)) |
---|
645 | (let* ((c (char string i)) |
---|
646 | (n (char-code c))) |
---|
647 | (cond ((zerop n) |
---|
648 | (vector-push-extend #xC0 octets) |
---|
649 | (vector-push-extend #x80 octets)) |
---|
650 | ((< 0 n #x80) |
---|
651 | (vector-push-extend n octets)) |
---|
652 | (t |
---|
653 | (let ((char-octets (char-to-utf8 c))) |
---|
654 | (dotimes (j (length char-octets)) |
---|
655 | (declare (type fixnum j)) |
---|
656 | (vector-push-extend (svref char-octets j) octets))))))) |
---|
657 | (write-u2 (length octets) stream) |
---|
658 | (dotimes (i (length octets)) |
---|
659 | (declare (type fixnum i)) |
---|
660 | (write-8-bits (aref octets i) stream))) |
---|
661 | (write-ascii string length stream)))) |
---|
662 | |
---|
663 | |
---|
664 | (defun !write-class-file (class stream) |
---|
665 | "Serializes `class' to `stream', after it has been finalized." |
---|
666 | |
---|
667 | ;; header |
---|
668 | (write-u4 #xCAFEBABE stream) |
---|
669 | (write-u2 3 stream) |
---|
670 | (write-u2 45 stream) |
---|
671 | |
---|
672 | ;; constants pool |
---|
673 | (write-constants (class-file-constants class) stream) |
---|
674 | ;; flags |
---|
675 | (write-u2 (class-file-access-flags class) stream) |
---|
676 | |
---|
677 | ;; class name |
---|
678 | (write-u2 (class-file-class class) stream) |
---|
679 | |
---|
680 | ;; superclass |
---|
681 | (write-u2 (class-file-superclass class) stream) |
---|
682 | |
---|
683 | ;; interfaces |
---|
684 | (write-u2 0 stream) |
---|
685 | |
---|
686 | ;; fields |
---|
687 | (write-u2 (length (class-file-fields class)) stream) |
---|
688 | (dolist (field (class-file-fields class)) |
---|
689 | (write-field field stream)) |
---|
690 | |
---|
691 | ;; methods |
---|
692 | (write-u2 (length (class-file-methods class)) stream) |
---|
693 | (dolist (method (class-file-methods class)) |
---|
694 | (write-method method stream)) |
---|
695 | |
---|
696 | ;; attributes |
---|
697 | (write-attributes (class-file-attributes class) stream)) |
---|
698 | |
---|
699 | |
---|
700 | (defvar *jvm-class-debug-pool* nil |
---|
701 | "When bound to a non-NIL value, enables output to *standard-output* |
---|
702 | to allow debugging output of the constant section of the class file.") |
---|
703 | |
---|
704 | (defun write-constants (constants stream) |
---|
705 | "Writes the constant section given in `constants' to the class file `stream'." |
---|
706 | (let ((pool-index 0)) |
---|
707 | (write-u2 (1+ (pool-index constants)) stream) |
---|
708 | (when *jvm-class-debug-pool* |
---|
709 | (sys::%format t "pool count ~A~%" (pool-index constants))) |
---|
710 | (dolist (entry (reverse (pool-entries-list constants))) |
---|
711 | (incf pool-index) |
---|
712 | (let ((tag (constant-tag entry))) |
---|
713 | (when *jvm-class-debug-pool* |
---|
714 | (print-constant entry t)) |
---|
715 | (write-u1 tag stream) |
---|
716 | (case tag |
---|
717 | (1 ; UTF8 |
---|
718 | (write-utf8 (constant-utf8-value entry) stream)) |
---|
719 | ((3 4) ; float int |
---|
720 | (write-u4 (constant-float/int-value entry) stream)) |
---|
721 | ((5 6) ; long double |
---|
722 | (write-u4 (logand (ash (constant-double/long-value entry) -32) |
---|
723 | #xFFFFffff) stream) |
---|
724 | (write-u4 (logand (constant-double/long-value entry) #xFFFFffff) |
---|
725 | stream)) |
---|
726 | ((9 10 11) ; fieldref methodref InterfaceMethodref |
---|
727 | (write-u2 (constant-member-ref-class-index entry) stream) |
---|
728 | (write-u2 (constant-member-ref-name/type-index entry) stream)) |
---|
729 | (12 ; nameAndType |
---|
730 | (write-u2 (constant-name/type-name-index entry) stream) |
---|
731 | (write-u2 (constant-name/type-descriptor-index entry) stream)) |
---|
732 | (7 ; class |
---|
733 | (write-u2 (constant-class-name-index entry) stream)) |
---|
734 | (8 ; string |
---|
735 | (write-u2 (constant-string-value-index entry) stream)) |
---|
736 | (t |
---|
737 | (error "write-constant-pool-entry unhandled tag ~D~%" tag))))))) |
---|
738 | |
---|
739 | |
---|
740 | (defun print-constant (entry stream) |
---|
741 | "Debugging helper to print the content of a constant-pool entry." |
---|
742 | (let ((tag (constant-tag entry)) |
---|
743 | (index (constant-index entry))) |
---|
744 | (sys::%format stream "pool element ~a, tag ~a, " index tag) |
---|
745 | (case tag |
---|
746 | (1 (sys::%format t "utf8: ~a~%" (constant-utf8-value entry))) |
---|
747 | ((3 4) (sys::%format t "f/i: ~a~%" (constant-float/int-value entry))) |
---|
748 | ((5 6) (sys::%format t "d/l: ~a~%" (constant-double/long-value entry))) |
---|
749 | ((9 10 11) (sys::%format t "ref: ~a,~a~%" |
---|
750 | (constant-member-ref-class-index entry) |
---|
751 | (constant-member-ref-name/type-index entry))) |
---|
752 | (12 (sys::%format t "n/t: ~a,~a~%" |
---|
753 | (constant-name/type-name-index entry) |
---|
754 | (constant-name/type-descriptor-index entry))) |
---|
755 | (7 (sys::%format t "cls: ~a~%" (constant-class-name-index entry))) |
---|
756 | (8 (sys::%format t "str: ~a~%" (constant-string-value-index entry)))))) |
---|
757 | |
---|
758 | |
---|
759 | #| |
---|
760 | |
---|
761 | ABCL doesn't use interfaces, so don't implement it here at this time |
---|
762 | |
---|
763 | (defstruct interface) |
---|
764 | |
---|
765 | |# |
---|
766 | |
---|
767 | |
---|
768 | (defparameter +access-flags-map+ |
---|
769 | '((:public #x0001) |
---|
770 | (:private #x0002) |
---|
771 | (:protected #x0004) |
---|
772 | (:static #x0008) |
---|
773 | (:final #x0010) |
---|
774 | (:volatile #x0040) |
---|
775 | (:synchronized #x0020) |
---|
776 | (:transient #x0080) |
---|
777 | (:native #x0100) |
---|
778 | (:abstract #x0400) |
---|
779 | (:strict #x0800)) |
---|
780 | "List of keyword symbols used for human readable representation of (access) |
---|
781 | flags and their binary values.") |
---|
782 | |
---|
783 | (defun map-flags (flags) |
---|
784 | "Calculates the bitmap of the flags from a list of symbols." |
---|
785 | (reduce #'(lambda (y x) |
---|
786 | (logior (or (when (member (car x) flags) |
---|
787 | (second x)) |
---|
788 | 0) y)) |
---|
789 | +access-flags-map+ |
---|
790 | :initial-value 0)) |
---|
791 | |
---|
792 | (defstruct (field (:constructor %make-field)) |
---|
793 | "Holds information on the properties of fields in the class(-file)." |
---|
794 | access-flags |
---|
795 | name |
---|
796 | descriptor |
---|
797 | attributes) |
---|
798 | |
---|
799 | (defun make-field (name type &key (flags '(:public))) |
---|
800 | "Creates a field for addition to a class file." |
---|
801 | (%make-field :access-flags flags |
---|
802 | :name name |
---|
803 | :descriptor type)) |
---|
804 | |
---|
805 | (defun field-add-attribute (field attribute) |
---|
806 | "Adds an attribute to a field." |
---|
807 | (push attribute (field-attributes field))) |
---|
808 | |
---|
809 | (defun field-attribute (field name) |
---|
810 | "Retrieves an attribute named `name' of `field'. |
---|
811 | |
---|
812 | Returns NIL if the attribute isn't found." |
---|
813 | (find name (field-attributes field) |
---|
814 | :test #'string= :key #'attribute-name)) |
---|
815 | |
---|
816 | (defun finalize-field (field class) |
---|
817 | "Prepares `field' for serialization." |
---|
818 | (let ((pool (class-file-constants class))) |
---|
819 | (setf (field-access-flags field) |
---|
820 | (map-flags (field-access-flags field)) |
---|
821 | (field-descriptor field) |
---|
822 | (pool-add-utf8 pool (internal-field-ref (field-descriptor field))) |
---|
823 | (field-name field) |
---|
824 | (pool-add-utf8 pool (field-name field)))) |
---|
825 | (finalize-attributes (field-attributes field) nil class)) |
---|
826 | |
---|
827 | (defun write-field (field stream) |
---|
828 | "Writes classfile representation of `field' to `stream'." |
---|
829 | (write-u2 (field-access-flags field) stream) |
---|
830 | (write-u2 (field-name field) stream) |
---|
831 | (write-u2 (field-descriptor field) stream) |
---|
832 | (write-attributes (field-attributes field) stream)) |
---|
833 | |
---|
834 | |
---|
835 | (defstruct (method (:constructor %make-method) |
---|
836 | (:conc-name method-)) |
---|
837 | "Holds information on the properties of methods in the class(-file)." |
---|
838 | access-flags |
---|
839 | name |
---|
840 | descriptor |
---|
841 | attributes) |
---|
842 | |
---|
843 | |
---|
844 | (defun map-method-name (name) |
---|
845 | "Methods should be identified by strings containing their names, or, |
---|
846 | be one of two keyword identifiers to identify special methods: |
---|
847 | |
---|
848 | * :class-constructor |
---|
849 | * :constructor |
---|
850 | " |
---|
851 | (cond |
---|
852 | ((eq name :class-constructor) |
---|
853 | "<clinit>") |
---|
854 | ((eq name :constructor) |
---|
855 | "<init>") |
---|
856 | (t name))) |
---|
857 | |
---|
858 | (defun make-method (name return args &key (flags '(:public))) |
---|
859 | "Creates a method for addition to a class file." |
---|
860 | (%make-method :descriptor (cons return args) |
---|
861 | :access-flags flags |
---|
862 | :name name)) |
---|
863 | |
---|
864 | (defun method-add-attribute (method attribute) |
---|
865 | "Add `attribute' to the list of attributes of `method', |
---|
866 | returning `attribute'." |
---|
867 | (push attribute (method-attributes method)) |
---|
868 | attribute) |
---|
869 | |
---|
870 | (defun method-add-code (method) |
---|
871 | "Creates an (empty) 'Code' attribute for the method, |
---|
872 | returning the created attribute." |
---|
873 | (method-add-attribute |
---|
874 | method |
---|
875 | (make-code-attribute (+ (length (cdr (method-descriptor method))) |
---|
876 | (if (member :static (method-access-flags method)) |
---|
877 | 0 1))))) ;; 1 == implicit 'this' |
---|
878 | |
---|
879 | (defun method-ensure-code (method) |
---|
880 | "Ensures the existence of a 'Code' attribute for the method, |
---|
881 | returning the attribute." |
---|
882 | (let ((code (method-attribute method "Code"))) |
---|
883 | (if (null code) |
---|
884 | (method-add-code method) |
---|
885 | code))) |
---|
886 | |
---|
887 | (defun method-attribute (method name) |
---|
888 | "Returns the first attribute of `method' with `name'." |
---|
889 | (find name (method-attributes method) |
---|
890 | :test #'string= :key #'attribute-name)) |
---|
891 | |
---|
892 | |
---|
893 | (defun finalize-method (method class) |
---|
894 | "Prepares `method' for serialization." |
---|
895 | (let ((pool (class-file-constants class))) |
---|
896 | (setf (method-access-flags method) |
---|
897 | (map-flags (method-access-flags method)) |
---|
898 | (method-descriptor method) |
---|
899 | (pool-add-utf8 pool (apply #'descriptor (method-descriptor method))) |
---|
900 | (method-name method) |
---|
901 | (pool-add-utf8 pool (map-method-name (method-name method))))) |
---|
902 | (finalize-attributes (method-attributes method) nil class)) |
---|
903 | |
---|
904 | |
---|
905 | (defun write-method (method stream) |
---|
906 | "Write class file representation of `method' to `stream'." |
---|
907 | (write-u2 (method-access-flags method) stream) |
---|
908 | (write-u2 (method-name method) stream) |
---|
909 | ;;(sys::%format t "method-name: ~a~%" (method-name method)) |
---|
910 | (write-u2 (method-descriptor method) stream) |
---|
911 | (write-attributes (method-attributes method) stream)) |
---|
912 | |
---|
913 | (defstruct attribute |
---|
914 | "Parent attribute structure to be included into other attributes, mainly |
---|
915 | to define common fields. |
---|
916 | |
---|
917 | Having common fields allows common driver code for |
---|
918 | finalizing and serializing attributes." |
---|
919 | name |
---|
920 | |
---|
921 | ;; not in the class file: |
---|
922 | finalizer ;; function of 3 arguments: the attribute, parent and class-file |
---|
923 | writer ;; function of 2 arguments: the attribute and the output stream |
---|
924 | ) |
---|
925 | |
---|
926 | (defun finalize-attributes (attributes att class) |
---|
927 | "Prepare `attributes' (a list) of attribute `att' list for serialization." |
---|
928 | (dolist (attribute attributes) |
---|
929 | ;; assure header: make sure 'name' is in the pool |
---|
930 | (setf (attribute-name attribute) |
---|
931 | (pool-add-utf8 (class-file-constants class) |
---|
932 | (attribute-name attribute))) |
---|
933 | ;; we're saving "root" attributes: attributes which have no parent |
---|
934 | (funcall (attribute-finalizer attribute) attribute att class))) |
---|
935 | |
---|
936 | (defun write-attributes (attributes stream) |
---|
937 | "Writes the `attributes' to `stream'." |
---|
938 | (write-u2 (length attributes) stream) |
---|
939 | (dolist (attribute attributes) |
---|
940 | (write-u2 (attribute-name attribute) stream) |
---|
941 | ;; set up a bulk catcher for (UNSIGNED-BYTE 8) |
---|
942 | ;; since we need to know the attribute length (excluding the header) |
---|
943 | (let ((local-stream (sys::%make-byte-array-output-stream))) |
---|
944 | (funcall (attribute-writer attribute) attribute local-stream) |
---|
945 | (let ((array (sys::%get-output-stream-array local-stream))) |
---|
946 | (write-u4 (length array) stream) |
---|
947 | (write-sequence array stream))))) |
---|
948 | |
---|
949 | |
---|
950 | |
---|
951 | (defstruct (code-attribute (:conc-name code-) |
---|
952 | (:include attribute |
---|
953 | (name "Code") |
---|
954 | (finalizer #'finalize-code-attribute) |
---|
955 | (writer #'write-code-attribute)) |
---|
956 | (:constructor %make-code-attribute)) |
---|
957 | "The attribute containing the actual JVM byte code; |
---|
958 | an attribute of a method." |
---|
959 | max-stack |
---|
960 | max-locals |
---|
961 | code |
---|
962 | exception-handlers |
---|
963 | attributes |
---|
964 | |
---|
965 | ;; fields not in the class file start here |
---|
966 | |
---|
967 | ;; labels contains offsets into the code array after it's finalized |
---|
968 | labels ;; an alist |
---|
969 | |
---|
970 | (current-local 0)) ;; used for handling nested WITH-CODE-TO-METHOD blocks |
---|
971 | |
---|
972 | |
---|
973 | |
---|
974 | (defun code-label-offset (code label) |
---|
975 | "Retrieves the `label' offset within a `code' attribute after the |
---|
976 | attribute has been finalized." |
---|
977 | (cdr (assoc label (code-labels code)))) |
---|
978 | |
---|
979 | (defun (setf code-label-offset) (offset code label) |
---|
980 | "Sets the `label' offset within a `code' attribute after the attribute |
---|
981 | has been finalized." |
---|
982 | (setf (code-labels code) |
---|
983 | (acons label offset (code-labels code)))) |
---|
984 | |
---|
985 | (defun finalize-code-attribute (code parent class) |
---|
986 | "Prepares the `code' attribute for serialization, within method `parent'." |
---|
987 | (declare (ignore parent)) |
---|
988 | (let* ((handlers (code-exception-handlers code)) |
---|
989 | (c (finalize-code |
---|
990 | (code-code code) |
---|
991 | (nconc (mapcar #'exception-start-pc handlers) |
---|
992 | (mapcar #'exception-end-pc handlers) |
---|
993 | (mapcar #'exception-handler-pc handlers)) |
---|
994 | t))) |
---|
995 | (setf (code-max-stack code) |
---|
996 | (analyze-stack c (mapcar #'exception-handler-pc handlers))) |
---|
997 | (multiple-value-bind |
---|
998 | (c labels) |
---|
999 | (code-bytes c) |
---|
1000 | (setf (code-code code) c |
---|
1001 | (code-labels code) labels))) |
---|
1002 | |
---|
1003 | (setf (code-exception-handlers code) |
---|
1004 | (remove-if #'(lambda (h) |
---|
1005 | (eql (code-label-offset code (exception-start-pc h)) |
---|
1006 | (code-label-offset code (exception-end-pc h)))) |
---|
1007 | (code-exception-handlers code))) |
---|
1008 | |
---|
1009 | (dolist (exception (code-exception-handlers code)) |
---|
1010 | (setf (exception-start-pc exception) |
---|
1011 | (code-label-offset code (exception-start-pc exception)) |
---|
1012 | (exception-end-pc exception) |
---|
1013 | (code-label-offset code (exception-end-pc exception)) |
---|
1014 | (exception-handler-pc exception) |
---|
1015 | (code-label-offset code (exception-handler-pc exception)) |
---|
1016 | (exception-catch-type exception) |
---|
1017 | (if (null (exception-catch-type exception)) |
---|
1018 | 0 ;; generic 'catch all' class index number |
---|
1019 | (pool-add-class (class-file-constants class) |
---|
1020 | (exception-catch-type exception))))) |
---|
1021 | |
---|
1022 | (finalize-attributes (code-attributes code) code class)) |
---|
1023 | |
---|
1024 | (defun write-code-attribute (code stream) |
---|
1025 | "Writes the attribute `code' to `stream'." |
---|
1026 | ;;(sys::%format t "max-stack: ~a~%" (code-max-stack code)) |
---|
1027 | (write-u2 (code-max-stack code) stream) |
---|
1028 | ;;(sys::%format t "max-locals: ~a~%" (code-max-locals code)) |
---|
1029 | (write-u2 (code-max-locals code) stream) |
---|
1030 | (let ((code-array (code-code code))) |
---|
1031 | ;;(sys::%format t "length: ~a~%" (length code-array)) |
---|
1032 | (write-u4 (length code-array) stream) |
---|
1033 | (dotimes (i (length code-array)) |
---|
1034 | (write-u1 (svref code-array i) stream))) |
---|
1035 | |
---|
1036 | (write-u2 (length (code-exception-handlers code)) stream) |
---|
1037 | (dolist (exception (reverse (code-exception-handlers code))) |
---|
1038 | ;;(sys::%format t "start-pc: ~a~%" (exception-start-pc exception)) |
---|
1039 | (write-u2 (exception-start-pc exception) stream) |
---|
1040 | ;;(sys::%format t "end-pc: ~a~%" (exception-end-pc exception)) |
---|
1041 | (write-u2 (exception-end-pc exception) stream) |
---|
1042 | ;;(sys::%format t "handler-pc: ~a~%" (exception-handler-pc exception)) |
---|
1043 | (write-u2 (exception-handler-pc exception) stream) |
---|
1044 | (write-u2 (exception-catch-type exception) stream)) |
---|
1045 | |
---|
1046 | (write-attributes (code-attributes code) stream)) |
---|
1047 | |
---|
1048 | (defun make-code-attribute (arg-count) |
---|
1049 | "Creates an empty 'Code' attribute for a method which takes |
---|
1050 | `arg-count` parameters, including the implicit `this` parameter." |
---|
1051 | (%make-code-attribute :max-locals arg-count)) |
---|
1052 | |
---|
1053 | (defun code-add-attribute (code attribute) |
---|
1054 | "Adds `attribute' to `code', returning `attribute'." |
---|
1055 | (push attribute (code-attributes code)) |
---|
1056 | attribute) |
---|
1057 | |
---|
1058 | (defun code-attribute (code name) |
---|
1059 | "Returns an attribute of `code' identified by `name'." |
---|
1060 | (find name (code-attributes code) |
---|
1061 | :test #'string= :key #'attribute-name)) |
---|
1062 | |
---|
1063 | |
---|
1064 | (defun code-add-exception-handler (code start end handler type) |
---|
1065 | "Adds an exception handler to `code' protecting the region from |
---|
1066 | labels `start' to `end' (inclusive) from exception `type' - where |
---|
1067 | a value of NIL indicates all types. Upon an exception of the given |
---|
1068 | type, control is transferred to label `handler'." |
---|
1069 | (push (make-exception :start-pc start |
---|
1070 | :end-pc end |
---|
1071 | :handler-pc handler |
---|
1072 | :catch-type type) |
---|
1073 | (code-exception-handlers code))) |
---|
1074 | |
---|
1075 | (defstruct exception |
---|
1076 | "Exception handler information. |
---|
1077 | |
---|
1078 | After finalization, the fields contain offsets instead of labels." |
---|
1079 | start-pc ;; label target |
---|
1080 | end-pc ;; label target |
---|
1081 | handler-pc ;; label target |
---|
1082 | catch-type ;; a string for a specific type, or NIL for all |
---|
1083 | ) |
---|
1084 | |
---|
1085 | |
---|
1086 | (defstruct (constant-value-attribute (:conc-name constant-value-) |
---|
1087 | (:include attribute |
---|
1088 | (name "ConstantValue") |
---|
1089 | ;; finalizer |
---|
1090 | ;; writer |
---|
1091 | )) |
---|
1092 | "An attribute of a field of primitive type. |
---|
1093 | |
---|
1094 | " |
---|
1095 | ;;; ### TODO |
---|
1096 | ) |
---|
1097 | |
---|
1098 | |
---|
1099 | (defstruct (checked-exceptions-attribute |
---|
1100 | (:conc-name checked-) |
---|
1101 | (:include attribute |
---|
1102 | (name "Exceptions") |
---|
1103 | (finalizer #'finalize-checked-exceptions) |
---|
1104 | (writer #'write-checked-exceptions))) |
---|
1105 | "An attribute of `code-attribute', " |
---|
1106 | table ;; a list of checked classes corresponding to Java's 'throws' |
---|
1107 | ) |
---|
1108 | |
---|
1109 | (defun finalize-checked-exceptions (checked-exceptions code class) |
---|
1110 | (declare (ignorable code class)) |
---|
1111 | |
---|
1112 | "Prepare `checked-exceptions' for serialization." |
---|
1113 | (setf (checked-table checked-exceptions) |
---|
1114 | (mapcar #'(lambda (exception) |
---|
1115 | (pool-add-class (class-file-constants class) |
---|
1116 | exception)) |
---|
1117 | (checked-table checked-exceptions)))) |
---|
1118 | |
---|
1119 | (defun write-checked-exceptions (checked-exceptions stream) |
---|
1120 | "Write `checked-exceptions' to `stream' in class file representation." |
---|
1121 | (write-u2 (length (checked-table checked-exceptions)) stream) |
---|
1122 | (dolist (exception (reverse (checked-table checked-exceptions))) |
---|
1123 | (write-u2 exception stream))) |
---|
1124 | |
---|
1125 | ;; Can't be used yet: serialization missing |
---|
1126 | (defstruct (deprecated-attribute (:include attribute |
---|
1127 | (name "Deprecated") |
---|
1128 | (finalizer (constantly nil)) |
---|
1129 | (writer (constantly nil)))) |
---|
1130 | ;; finalizer and writer need to do nothing: Deprecated attributes are empty |
---|
1131 | "An attribute of a class file, field or method, indicating the element |
---|
1132 | to which it has been attached has been superseded.") |
---|
1133 | |
---|
1134 | (defvar *current-code-attribute* nil) |
---|
1135 | |
---|
1136 | (defun save-code-specials (code) |
---|
1137 | (setf (code-code code) *code* |
---|
1138 | (code-max-locals code) *registers-allocated* |
---|
1139 | (code-current-local code) *register*)) |
---|
1140 | |
---|
1141 | (defun restore-code-specials (code) |
---|
1142 | (setf *code* (code-code code) |
---|
1143 | *registers-allocated* (code-max-locals code) |
---|
1144 | *register* (code-current-local code))) |
---|
1145 | |
---|
1146 | (defmacro with-code-to-method ((class-file method &key (safe-nesting t)) |
---|
1147 | &body body) |
---|
1148 | (let ((m (gensym)) |
---|
1149 | (c (gensym))) |
---|
1150 | `(progn |
---|
1151 | ,@(when safe-nesting |
---|
1152 | `((when *current-code-attribute* |
---|
1153 | (save-code-specials *current-code-attribute*)))) |
---|
1154 | (let* ((,m ,method) |
---|
1155 | (,c (method-ensure-code ,method)) |
---|
1156 | (*pool* (class-file-constants ,class-file)) |
---|
1157 | (*code* (code-code ,c)) |
---|
1158 | (*registers-allocated* (code-max-locals ,c)) |
---|
1159 | (*register* (code-current-local ,c)) |
---|
1160 | (*current-code-attribute* ,c)) |
---|
1161 | ,@body |
---|
1162 | (setf (code-code ,c) *code* |
---|
1163 | (code-current-local ,c) *register* |
---|
1164 | ;; (code-exception-handlers ,c) *handlers* |
---|
1165 | (code-max-locals ,c) *registers-allocated*)) |
---|
1166 | ,@(when safe-nesting |
---|
1167 | `((when *current-code-attribute* |
---|
1168 | (restore-code-specials *current-code-attribute*))))))) |
---|
1169 | |
---|
1170 | |
---|
1171 | (defstruct (source-file-attribute (:conc-name source-) |
---|
1172 | (:include attribute |
---|
1173 | (name "SourceFile") |
---|
1174 | (finalizer #'finalize-source-file) |
---|
1175 | (writer #'write-source-file))) |
---|
1176 | "An attribute of the class file indicating which source file |
---|
1177 | it was compiled from." |
---|
1178 | filename) |
---|
1179 | |
---|
1180 | (defun finalize-source-file (source-file code class) |
---|
1181 | (declare (ignorable code class)) |
---|
1182 | (setf (source-filename source-file) |
---|
1183 | (pool-add-utf8 (class-file-constants class) |
---|
1184 | (source-filename source-file)))) |
---|
1185 | |
---|
1186 | (defun write-source-file (source-file stream) |
---|
1187 | (write-u2 (source-filename source-file) stream)) |
---|
1188 | |
---|
1189 | |
---|
1190 | (defstruct (synthetic-attribute (:include attribute |
---|
1191 | (name "Synthetic") |
---|
1192 | (finalizer (constantly nil)) |
---|
1193 | (writer (constantly nil)))) |
---|
1194 | ;; finalizer and writer need to do nothing: Synthetic attributes are empty |
---|
1195 | "An attribute of a class file, field or method to mark that it wasn't |
---|
1196 | included in the sources - but was generated artificially.") |
---|
1197 | |
---|
1198 | |
---|
1199 | (defstruct (line-numbers-attribute |
---|
1200 | (:conc-name line-numbers-) |
---|
1201 | (:include attribute |
---|
1202 | (name "LineNumberTable") |
---|
1203 | (finalizer #'finalize-line-numbers) |
---|
1204 | (writer #'write-line-numbers))) |
---|
1205 | "An attribute of `code-attribute', containing a mapping of offsets |
---|
1206 | within the code section to the line numbers in the source file." |
---|
1207 | table ;; a list of line-number structures, in reverse order |
---|
1208 | ) |
---|
1209 | |
---|
1210 | (defstruct line-number |
---|
1211 | start-pc ;; a label, before finalization |
---|
1212 | line) |
---|
1213 | |
---|
1214 | (defun finalize-line-numbers (line-numbers code class) |
---|
1215 | (declare (ignorable code class)) |
---|
1216 | (dolist (line-number (line-numbers-table line-numbers)) |
---|
1217 | (setf (line-number-start-pc line-number) |
---|
1218 | (code-label-offset code (line-number-start-pc line-number))))) |
---|
1219 | |
---|
1220 | (defun write-line-numbers (line-numbers stream) |
---|
1221 | (write-u2 (length (line-numbers-table line-numbers)) stream) |
---|
1222 | (dolist (line-number (reverse (line-numbers-table line-numbers))) |
---|
1223 | (write-u2 (line-number-start-pc line-number) stream) |
---|
1224 | (write-u2 (line-number-line line-number) stream))) |
---|
1225 | |
---|
1226 | |
---|
1227 | |
---|
1228 | (defstruct (local-variables-attribute |
---|
1229 | (:conc-name local-var-) |
---|
1230 | (:include attribute |
---|
1231 | (name "LocalVariableTable") |
---|
1232 | (finalizer #'finalize-local-variables) |
---|
1233 | (writer #'write-local-variables))) |
---|
1234 | "An attribute of the `code-attribute', containing a table of local variable |
---|
1235 | names, their type and their scope of validity." |
---|
1236 | table ;; a list of local-variable structures, in reverse order |
---|
1237 | ) |
---|
1238 | |
---|
1239 | (defstruct (local-variable (:conc-name local-)) |
---|
1240 | start-pc ;; a label, before finalization |
---|
1241 | length ;; a label (at the ending position) before finalization |
---|
1242 | name |
---|
1243 | descriptor |
---|
1244 | index ;; The index of the variable inside the block of locals |
---|
1245 | ) |
---|
1246 | |
---|
1247 | (defun finalize-local-variables (local-variables code class) |
---|
1248 | (dolist (local-variable (local-var-table local-variables)) |
---|
1249 | (setf (local-start-pc local-variable) |
---|
1250 | (code-label-offset code (local-start-pc local-variable)) |
---|
1251 | (local-length local-variable) |
---|
1252 | ;; calculate 'length' from the distance between 2 labels |
---|
1253 | (- (code-label-offset code (local-length local-variable)) |
---|
1254 | (local-start-pc local-variable)) |
---|
1255 | (local-name local-variable) |
---|
1256 | (pool-add-utf8 (class-file-constants class) |
---|
1257 | (local-name local-variable)) |
---|
1258 | (local-descriptor local-variable) |
---|
1259 | (pool-add-utf8 (class-file-constants class) |
---|
1260 | (local-descriptor local-variable))))) |
---|
1261 | |
---|
1262 | (defun write-local-variables (local-variables stream) |
---|
1263 | (write-u2 (length (local-var-table local-variables)) stream) |
---|
1264 | (dolist (local-variable (reverse (local-var-table local-variables))) |
---|
1265 | (write-u2 (local-start-pc local-variable) stream) |
---|
1266 | (write-u2 (local-length local-variable) stream) |
---|
1267 | (write-u2 (local-name local-variable) stream) |
---|
1268 | (write-u2 (local-descriptor local-variable) stream) |
---|
1269 | (write-u2 (local-index local-variable) stream))) |
---|
1270 | |
---|
1271 | #| |
---|
1272 | |
---|
1273 | ;; this is the minimal sequence we need to support: |
---|
1274 | |
---|
1275 | ;; create a class file structure |
---|
1276 | ;; add methods |
---|
1277 | ;; add code to the methods, switching from one method to the other |
---|
1278 | ;; finalize the methods, one by one |
---|
1279 | ;; write the class file |
---|
1280 | |
---|
1281 | to support the sequence above, we probably need to |
---|
1282 | be able to |
---|
1283 | |
---|
1284 | - find methods by signature |
---|
1285 | - find the method's code attribute |
---|
1286 | - add code to the code attribute |
---|
1287 | - finalize the code attribute contents (blocking it for further addition) |
---|
1288 | - |
---|
1289 | |
---|
1290 | |
---|
1291 | |# |
---|
1292 | |
---|