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
/*
* jit-objmodel.c - Interfaces for pluggable object models.
*
* Copyright (C) 2004 Southern Storm Software, Pty Ltd.
*
* This file is part of the libjit library.
*
* The libjit library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation, either version 2.1 of
* the License, or (at your option) any later version.
*
* The libjit library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the libjit library. If not, see
* <http://www.gnu.org/licenses/>.
*/
/*@
The @code{libjit} library does not implement a particular object model
of its own, so that it is generic across bytecode formats and front end
languages. However, it does provide support for plugging object models
into the JIT process, and for transparently proxying to external libraries
that may use a foreign object model.
There may be more than one object model active in the system at any
one time. For example, a JVM implementation might have a primary
object model for its own use, and a secondary object model for
calling methods in an imported Objective C library.
The functions in this section support pluggable object models. There is
no requirement that you use them: you can ignore them and use the rest
of @code{libjit} directly if you wish.
To create a new object model, you would include the file
@code{<jit/jit-objmodel-private.h>} and create an instance of
the @code{struct jit_objmodel} type. This instance should be
populated with pointers to your object model's handler routines.
You then use functions from the list below to access the
object model.
Some standard object models are distributed with @code{libjit}
for invoking methods in external C++, Objective C, GCJ, and JNI
based libraries.
@*/
/*@
* @deftypefun void jitom_destroy_model (jit_objmodel_t @var{model})
* Destroy an object model handler that is no longer required.
* It is undefined what will happen to the objects and classes
* that were being managed by the object model: they may still persist,
* or they may now be invalid.
* @end deftypefun
@*/
void
/*@
* @deftypefun jitom_class_t jitom_get_class_by_name (jit_objmodel_t @var{model}, const char *@var{name})
* Get the class descriptor from the object model for a class
* called @var{name}. Returns NULL if the class was not found.
* If the name includes namespace or nested scope qualifiers,
* they must be separated by periods (@code{.}).
* @end deftypefun
@*/
jitom_class_t
/*@
* @deftypefun {char *} jitom_class_get_name (jit_objmodel_t @var{model}, jitom_class_t @var{klass})
* Get the name of a particular class. The return value must be freed
* with @code{jit_free}.
* @end deftypefun
@*/
char *
/*@
* @deftypefun int jitom_class_get_modifiers (jit_objmodel_t @var{model}, jitom_class_t @var{klass})
* Get the access modifiers for a particular class. The following lists
* all access modifiers, for classes, fields and methods:
*
* @table @code
* @vindex JITOM_MODIFIER_ACCESS_MASK
* @item JITOM_MODIFIER_ACCESS_MASK
* Mask to strip out just the public, private, etc access flags.
*
* @vindex JITOM_MODIFIER_PUBLIC
* @vindex JITOM_MODIFIER_PRIVATE
* @vindex JITOM_MODIFIER_PROTECTED
* @vindex JITOM_MODIFIER_PACKAGE
* @vindex JITOM_MODIFIER_PACKAGE_OR_PROTECTED
* @vindex JITOM_MODIFIER_PACKAGE_AND_PROTECTED
* @vindex JITOM_MODIFIER_OTHER1
* @vindex JITOM_MODIFIER_OTHER2
* @item JITOM_MODIFIER_PUBLIC
* @item JITOM_MODIFIER_PRIVATE
* @item JITOM_MODIFIER_PROTECTED
* @item JITOM_MODIFIER_PACKAGE
* @item JITOM_MODIFIER_PACKAGE_OR_PROTECTED
* @item JITOM_MODIFIER_PACKAGE_AND_PROTECTED
* @item JITOM_MODIFIER_OTHER1
* @item JITOM_MODIFIER_OTHER2
* The declared access level on the class, field, or method. The scope
* of a "package" will typically be model-specific: Java uses namespaces
* to define packages, whereas C# uses compile units ("assemblies").
*
* Object model handlers do not need to enforce the above access levels.
* It is assumed that any caller with access to @code{libjit} has complete
* access to the entire system, and will use its own rules for determining
* when it is appropriate to grant access to fields and methods.
*
* @vindex JITOM_MODIFIER_STATIC
* @item JITOM_MODIFIER_STATIC
* The field or method is static, rather than instance-based.
*
* @vindex JITOM_MODIFIER_VIRTUAL
* @item JITOM_MODIFIER_VIRTUAL
* The method is instance-based and virtual.
*
* @vindex JITOM_MODIFIER_NEW_SLOT
* @item JITOM_MODIFIER_NEW_SLOT
* The method is virtual, but occupies a new slot. Provided for languages
* like C# that can declare virtual methods with the same name as in a
* superclass, but within a new slot in the vtable.
*
* @vindex JITOM_MODIFIER_ABSTRACT
* @item JITOM_MODIFIER_ABSTRACT
* When used on a class, this indicates that the class contains abstract
* methods. When used on a method, this indicates that the method does
* not have any associated code in its defining class. It is not possible
* to invoke such a method with @code{jitom_method_invoke}, only
* @code{jitom_method_invoke_virtual}.
*
* @vindex JITOM_MODIFIER_LITERAL
* @item JITOM_MODIFIER_LITERAL
* A hint flag, used on fields, to indicate that the field has a constant
* value associated with it and does not occupy any real space. If the
* @code{jitom_field_load} function is used on such a field, it will load
* the constant value.
*
* @vindex JITOM_MODIFIER_CTOR
* @item JITOM_MODIFIER_CTOR
* A hint flag that indicates that the method is an instance constructor.
*
* @vindex JITOM_MODIFIER_STATIC_CTOR
* @item JITOM_MODIFIER_STATIC_CTOR
* A hint flag that indicates that the method is a static constructor.
* The object model handler is normally responsible for outputting calls to
* static constructors; the caller shouldn't need to perform any
* special handling.
*
* @vindex JITOM_MODIFIER_DTOR
* @item JITOM_MODIFIER_DTOR
* A hint flag that indicates that the method is an instance destructor.
* The class should also have the @code{JITOM_MODIFIER_DELETE} flag.
* Note: do not use this for object models that support automatic
* garbage collection and finalization.
*
* @vindex JITOM_MODIFIER_INTERFACE
* @item JITOM_MODIFIER_INTERFACE
* The class is an interface.
*
* @vindex JITOM_MODIFIER_VALUE
* @item JITOM_MODIFIER_VALUE
* Instances of this class can be stored inline on the stack.
*
* @vindex JITOM_MODIFIER_FINAL
* @item JITOM_MODIFIER_FINAL
* When used on a class, this indicates that it cannot be subclassed.
* When used on a virtual method, this indicates that it cannot be
* overridden in subclasses.
*
* @vindex JITOM_MODIFIER_DELETE
* @item JITOM_MODIFIER_DELETE
* When used on a class, this indicates that its objects must be
* explicitly deleted when no longer required.
*
* @vindex JITOM_MODIFIER_REFERENCE_COUNTED
* @item JITOM_MODIFIER_REFERENCE_COUNTED
* When used on a class, this indicates that its objects are
* reference-counted. Calling @code{jitom_class_delete} will
* reduce the reference count and delete the object for real
* when the count reaches zero.
* @end table
* @end deftypefun
@*/
int
/*@
* @deftypefun jit_type_t jitom_class_get_type (jit_objmodel_t @var{model}, jitom_class_t @var{klass})
* Get the JIT type descriptor that represents pointer-based
* object references to a class. The object model handler should
* use @code{jitom_type_tag_as_class} to tag the return value.
* The caller should use @code{jit_type_free} to free the returned
* value when it is no longer required.
* @end deftypefun
@*/
jit_type_t
/*@
* @deftypefun jit_type_t jitom_class_get_value_type (jit_objmodel_t @var{model}, jitom_class_t @var{klass})
* Get the JIT type descriptor that represents inline stack instances
* of the class. The object model handler should use
* @code{jitom_type_tag_as_value} to tag the return value.
* The caller should use @code{jit_type_free} to free the returned
* value when it is no longer required.
* @end deftypefun
@*/
jit_type_t
/*@
* @deftypefun jitom_class_t jitom_class_get_primary_super (jit_objmodel_t @var{model}, jitom_class_t @var{klass})
* Get the primary superclass for @var{klass}. If the object model supports
* true multiple inheritance, then this should return the first superclass.
* Note: for the purposes of this function, interfaces are not considered
* superclasses. Use @code{jitom_class_get_interfaces} instead.
* @end deftypefun
@*/
jitom_class_t
/*@
* @deftypefun {jitom_class_t *} jitom_class_get_all_supers (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, unsigned int *@var{num})
* Return an array of all superclasses for @var{klass}, with the number of
* elements returned in @var{num}. Returns NULL if out of memory.
* Use @code{jit_free} to free the return array.
* @end deftypefun
@*/
jitom_class_t *
/*@
* @deftypefun {jitom_class_t *} jitom_class_get_interfaces (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, unsigned int *@var{num})
* Return an array of all interfaces for @var{klass}, with the number
* of elements returned in @var{num}. The list does not include interfaces
* that are declared on superclasses. Returns NULL if out of memory.
* Use @code{jit_free} to free the return array.
* @end deftypefun
@*/
jitom_class_t *
/*@
* @deftypefun {jitom_field_t *} jitom_class_get_fields (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, unsigned int *@var{num})
* Return an array of all fields for @var{klass}, with the number
* of elements returned in @var{num}. The list does not include fields
* that are declared on superclasses. Returns NULL if out of memory.
* Use @code{jit_free} to free the return array.
* @end deftypefun
@*/
jitom_field_t *
/*@
* @deftypefun {jitom_method_t *} jitom_class_get_methods (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, unsigned int *@var{num})
* Return an array of all methods for @var{klass}, with the number
* of elements returned in @var{num}. The list does not include methods
* that are declared on superclasses. Returns NULL if out of memory.
* Use @code{jit_free} to free the return array.
* @end deftypefun
@*/
jitom_method_t *
/*@
* @deftypefun jit_value_t jitom_class_new (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{ctor}, jit_function_t @var{func}, jit_value_t *@var{args}, unsigned int @var{num_args}, int @var{flags})
* Add instructions to @var{func} to create a new instance of the
* specified class. If @var{ctor} is not NULL, then it indicates a
* constructor that should be invoked with the arguments in @var{args}.
* If @var{ctor} is NULL, then memory should be allocated, but no
* constructor should be invoked. Returns a JIT value representing
* the newly allocated object. The type of the value will be the same
* as the the result from @code{jitom_class_get_type}.
* @end deftypefun
@*/
jit_value_t
/*@
* @deftypefun jit_value_t jitom_class_new_value (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{ctor}, jit_function_t @var{func}, jit_value_t *@var{args}, unsigned int @var{num_args}, int @var{flags})
* Add instructions to @var{func} to create a new instance of the
* specified class, inline on the stack. If @var{ctor} is not NULL, then
* it indicates a constructor that should be invoked with the arguments
* in @var{args}. If @var{ctor} is NULL, then stack space should be
* allocated, but no constructor should be invoked. Returns a JIT
* value representing the newly allocated stack space. The type of the
* value will be the same as the the result from
* @code{jitom_class_get_value_type}.
* @end deftypefun
@*/
jit_value_t
/*@
* @deftypefun int jitom_class_delete (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jit_value_t @var{obj_value})
* Delete an instance of a particular class, calling the destructor if
* necessary. The @var{obj_value} may be an inline stack value,
* in which case the destructor is called, but the memory is not freed.
* Ignored if the class does not have the @code{JITOM_MODIFIER_DELETE}
* modifier.
*
* Note: no attempt is made to destroy inline stack values automatically when
* they go out of scope. It is up to the caller to output instructions
* in the appropriate places to do this.
* @end
@*/
int
/*@
* @deftypefun int jitom_class_add_ref (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jit_value_t @var{obj_value})
* Add a reference to a reference-counted object. Ignored if the
* class does not have the @code{JITOM_MODIFIER_REFERENCE_COUNTED} modifier,
* or the value is stored inline on the stack.
*
* Other functions, such as @code{jitom_field_store}, may also change the
* reference count, but such behavior is object model specific.
* @end
@*/
int
/*@
* @deftypefun {char *} jitom_field_get_name (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_field_t @var{field})
* Get the name of a particular object model field. The return value must
* be freed with @code{jit_free}.
* @end deftypefun
@*/
char *
/*@
* @deftypefun jit_type_t jitom_field_get_type (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_field_t @var{field})
* Get the type of a particular object model field.
* @end deftypefun
@*/
jit_type_t
/*@
* @deftypefun int jitom_field_get_modifiers (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_field_t @var{field})
* Get the access modifiers that are associated with a particular
* object model field.
* @end deftypefun
@*/
int
/*@
* @deftypefun jit_value_t jitom_field_load (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_field_t @var{field}, jit_function_t @var{func}, jit_value_t @var{obj_value})
* Create instructions within @var{func} to effect a load from a
* field within the object @var{obj_value}. If @var{obj_value} is
* NULL, then it indicates a static field reference.
*
* If the field has the @code{JITOM_MODIFIER_LITERAL} modifier, then this
* function will load the constant value associated with the field.
* Literal fields cannot be stored to.
* @end deftypefun
@*/
jit_value_t
/*@
* @deftypefun jit_value_t jitom_field_load_address (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_field_t @var{field}, jit_function_t @var{func}, jit_value_t @var{obj_value})
* Create instructions within @var{func} to get the address of a
* field within the object @var{obj_value}. If @var{obj_value} is
* NULL, then it indicates that we want the address of a static field.
* Some object models may not support this function, and will return NULL.
* @end deftypefun
@*/
jit_value_t
/*@
* @deftypefun int jitom_field_store (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_field_t @var{field}, jit_function_t @var{func}, jit_value_t @var{obj_value}, jit_value_t @var{value})
* Create instructions within @var{func} to store @var{value} into a
* field within the object @var{obj_value}. If @var{obj_value} is
* NULL, then it indicates a static field store.
* @end deftypefun
@*/
int
/*@
* @deftypefun {char *} jitom_method_get_name (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{method})
* Get the name of an object model method. The return value must
* be freed with @code{jit_free}.
* @end deftypefun
@*/
char *
/*@
* @deftypefun jit_type_t jitom_method_get_type (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{method})
* Get the signature type of an object model method. If the method
* is instance-based, then the first argument will be an object reference
* type indicating the @code{this} pointer.
* @end deftypefun
@*/
jit_type_t
/*@
* @deftypefun int jitom_method_get_modifiers (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{method})
* Get the access modifiers for an object model method.
* @end deftypefun
@*/
int
/*@
* @deftypefun jit_value_t jitom_method_invoke (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{method}, jit_function_t @var{func}, jit_value_t *@var{args}, unsigned int @var{num_args}, int @var{flags})
* Create instructions within @var{func} to invoke a static or
* instance method. If an instance method is virtual, then this will
* ignore the virtual property to invoke the designated method directly.
* The first element in @var{args} should be the @code{this} pointer
* for instance methods.
* @end deftypefun
@*/
jit_value_t
/*@
* @deftypefun jit_value_t jitom_method_invoke_virtual (jit_objmodel_t @var{model}, jitom_class_t @var{klass}, jitom_method_t @var{method}, jit_function_t @var{func}, jit_value_t *@var{args}, unsigned int @var{num_args}, int @var{flags})
* Create instructions within @var{func} to invoke a virtual or interface
* method. The first element in @var{args} should be the @code{this}
* pointer for the call.
* @end deftypefun
@*/
jit_value_t
/*
* Information that is stored for class-tagged types.
*/
typedef struct
jitom_tag_info;
/*@
* @deftypefun jit_type_t jitom_type_tag_as_class (jit_type_t @var{type}, jit_objmodel_t @var{model}, jitom_class_t @var{klass}, int @var{incref})
* Tag a JIT type as an object reference belonging to a specific class.
* Returns NULL if there is insufficient memory to tag the type.
* @end deftypefun
@*/
jit_type_t
/*@
* @deftypefun jit_type_t jitom_type_tag_as_value (jit_type_t @var{type}, jit_objmodel_t @var{model}, jitom_class_t @var{klass}, int @var{incref})
* Tag a JIT type as an inline static value belonging to a specific class.
* Returns NULL if there is insufficient memory to tag the type.
* @end deftypefun
@*/
jit_type_t
/*@
* @deftypefun int jitom_type_is_class (jit_type_t @var{type})
* Determine if a type is tagged as an object reference.
* @end deftypefun
@*/
int
/*@
* @deftypefun int jitom_type_is_value (jit_type_t @var{type})
* Determine if a type is tagged as an inline static value.
* @end deftypefun
@*/
int
/*@
* @deftypefun jit_objmodel_t jitom_type_get_model (jit_type_t @var{type})
* Get the object model associated with a type that was tagged with
* @code{jitom_type_tag_as_class} or @code{jitom_type_tag_as_value}.
* @end deftypefun
@*/
jit_objmodel_t
/*@
* @deftypefun jitom_class_t jitom_type_get_class (jit_type_t @var{type})
* Get the class associated with a type that was tagged with
* @code{jitom_type_tag_as_class} or @code{jitom_type_tag_as_value}.
* @end deftypefun
@*/
jitom_class_t