1 /* Write out a Java(TM) class file.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC 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 You should have received a copy of the GNU General Public License
16 along with GNU CC; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
28 #include "java-tree.h"
33 #include "java-opcodes.h"
34 #include "parse.h" /* for BLOCK_EXPR_BODY */
39 #define DIR_SEPARATOR '/'
42 extern struct obstack temporary_obstack
;
44 /* Base directory in which `.class' files should be written.
45 NULL means to put the file into the same directory as the
46 corresponding .java file. */
47 char *jcf_write_base_directory
= NULL
;
49 /* Make sure bytecode.data is big enough for at least N more bytes. */
52 do { CHECK_OP(state); \
53 if (state->bytecode.ptr + (N) > state->bytecode.limit) \
54 buffer_grow (&state->bytecode, N); } while (0)
56 /* Add a 1-byte instruction/operand I to bytecode.data,
57 assuming space has already been RESERVE'd. */
59 #define OP1(I) (*state->bytecode.ptr++ = (I), CHECK_OP(state))
61 /* Like OP1, but I is a 2-byte big endian integer. */
64 do { int _i = (I); OP1 (_i >> 8); OP1 (_i); CHECK_OP(state); } while (0)
66 /* Like OP1, but I is a 4-byte big endian integer. */
69 do { int _i = (I); OP1 (_i >> 24); OP1 (_i >> 16); \
70 OP1 (_i >> 8); OP1 (_i); CHECK_OP(state); } while (0)
72 /* Macro to call each time we push I words on the JVM stack. */
74 #define NOTE_PUSH(I) \
75 do { state->code_SP += (I); \
76 if (state->code_SP > state->code_SP_max) \
77 state->code_SP_max = state->code_SP; } while (0)
79 /* Macro to call each time we pop I words from the JVM stack. */
82 do { state->code_SP -= (I); if (state->code_SP < 0) abort(); } while (0)
84 /* A chunk or segment of a .class file. */
88 /* The next segment of this .class file. */
91 /* The actual data in this segment to be written to the .class file. */
94 /* The size of the segment to be written to the .class file. */
98 #define PENDING_CLEANUP_PC (-3)
99 #define PENDING_EXIT_PC (-2)
100 #define UNDEFINED_PC (-1)
102 /* Each "block" represents a label plus the bytecode instructions following.
103 There may be branches out of the block, but no incoming jumps, except
104 to the beginning of the block.
106 If (pc < 0), the jcf_block is not an actual block (i.e. it has no
107 assocated code yet), but it is an undefined label.
112 /* For blocks that that are defined, the next block (in pc order).
113 For blocks that are the not-yet-defined end label of a LABELED_BLOCK_EXPR
114 or a cleanup expression (from a WITH_CLEANUP_EXPR),
115 this is the next (outer) such end label, in a stack headed by
116 labeled_blocks in jcf_partial. */
117 struct jcf_block
*next
;
119 /* In the not-yet-defined end label for an unfinished EXIT_BLOCK_EXPR.
120 pc is PENDING_EXIT_PC.
121 In the not-yet-defined end label for pending cleanup subroutine,
122 pc is PENDING_CLEANUP_PC.
123 For other not-yet-defined labels, pc is UNDEFINED_PC.
125 If the label has been defined:
126 Until perform_relocations is finished, this is the maximum possible
127 value of the bytecode offset at the begnning of this block.
128 After perform_relocations, it is the actual offset (pc). */
133 /* After finish_jcf_block is called, The actual instructions contained in this block.
134 Before than NULL, and the instructions are in state->bytecode. */
138 /* If pc==PENDING_CLEANUP_PC, start_label is the start of the region
139 coveed by the cleanup. */
140 struct jcf_block
*start_label
;
144 /* Set of relocations (in reverse offset order) for this block. */
145 struct jcf_relocation
*relocations
;
147 /* If this block is that of the not-yet-defined end label of
148 a LABELED_BLOCK_EXPR, where LABELED_BLOCK is that LABELED_BLOCK_EXPR.
149 If pc==PENDING_CLEANUP_PC, the cleanup that needs to be run. */
154 /* A "relocation" type for the 0-3 bytes of padding at the start
155 of a tableswitch or a lookupswitch. */
156 #define SWITCH_ALIGN_RELOC 4
158 /* A relocation type for the labels in a tableswitch or a lookupswitch;
159 these are relative to the start of the instruction, but (due to
160 th 0-3 bytes of padding), we don't know the offset before relocation. */
161 #define BLOCK_START_RELOC 1
163 struct jcf_relocation
165 /* Next relocation for the current jcf_block. */
166 struct jcf_relocation
*next
;
168 /* The (byte) offset within the current block that needs to be relocated. */
169 HOST_WIDE_INT offset
;
171 /* 0 if offset is a 4-byte relative offset.
172 4 (SWITCH_ALIGN_RELOC) if offset points to 0-3 padding bytes inserted
173 for proper alignment in tableswitch/lookupswitch instructions.
174 1 (BLOCK_START_RELOC) if offset points to a 4-byte offset relative
175 to the start of the containing block.
176 -1 if offset is a 2-byte relative offset.
177 < -1 if offset is the address of an instruction with a 2-byte offset
178 that does not have a corresponding 4-byte offset version, in which
179 case the absolute value of kind is the inverted opcode.
180 > 4 if offset is the address of an instruction (such as jsr) with a
181 2-byte offset that does have a corresponding 4-byte offset version,
182 in which case kind is the opcode of the 4-byte version (such as jsr_w). */
185 /* The label the relocation wants to actually transfer to. */
186 struct jcf_block
*label
;
189 #define RELOCATION_VALUE_0 ((HOST_WIDE_INT)0)
190 #define RELOCATION_VALUE_1 ((HOST_WIDE_INT)1)
192 /* State for single catch clause. */
196 struct jcf_handler
*next
;
198 struct jcf_block
*start_label
;
199 struct jcf_block
*end_label
;
200 struct jcf_block
*handler_label
;
202 /* The sub-class of Throwable handled, or NULL_TREE (for finally). */
206 /* State for the current switch statement. */
208 struct jcf_switch_state
210 struct jcf_switch_state
*prev
;
211 struct jcf_block
*default_label
;
213 struct jcf_relocation
*cases
;
215 HOST_WIDE_INT min_case
, max_case
;
218 /* This structure is used to contain the various pieces that will
219 become a .class file. */
225 struct obstack
*chunk_obstack
;
228 /* List of basic blocks for the current method. */
229 struct jcf_block
*blocks
;
230 struct jcf_block
*last_block
;
232 struct localvar_info
*first_lvar
;
233 struct localvar_info
*last_lvar
;
238 int linenumber_count
;
240 /* Until perform_relocations, this is a upper bound on the number
241 of bytes (so far) in the instructions for the current method. */
244 /* Stack of undefined ending labels for LABELED_BLOCK_EXPR. */
245 struct jcf_block
*labeled_blocks
;
247 /* The current stack size (stack pointer) in the current method. */
250 /* The largest extent of stack size (stack pointer) in the current method. */
253 /* Contains a mapping from local var slot number to localvar_info. */
254 struct buffer localvars
;
256 /* The buffer allocated for bytecode for the current jcf_block. */
257 struct buffer bytecode
;
259 /* Chain of exception handlers for the current method. */
260 struct jcf_handler
*handlers
;
262 /* Last element in handlers chain. */
263 struct jcf_handler
*last_handler
;
265 /* Number of exception handlers for the current method. */
268 /* Number of finalizers we are currently nested within. */
271 /* If non-NULL, use this for the return value. */
272 tree return_value_decl
;
274 /* Information about the current switch statemenet. */
275 struct jcf_switch_state
*sw_state
;
278 static void generate_bytecode_insns
PARAMS ((tree
, int, struct jcf_partial
*));
279 static struct chunk
* alloc_chunk
PARAMS ((struct chunk
*, unsigned char *,
280 int, struct obstack
*));
281 static unsigned char * append_chunk
PARAMS ((unsigned char *, int,
282 struct jcf_partial
*));
283 static void append_chunk_copy
PARAMS ((unsigned char *, int,
284 struct jcf_partial
*));
285 static struct jcf_block
* gen_jcf_label
PARAMS ((struct jcf_partial
*));
286 static void finish_jcf_block
PARAMS ((struct jcf_partial
*));
287 static void define_jcf_label
PARAMS ((struct jcf_block
*,
288 struct jcf_partial
*));
289 static struct jcf_block
* get_jcf_label_here
PARAMS ((struct jcf_partial
*));
290 static void put_linenumber
PARAMS ((int, struct jcf_partial
*));
291 static void localvar_alloc
PARAMS ((tree
, struct jcf_partial
*));
292 static void localvar_free
PARAMS ((tree
, struct jcf_partial
*));
293 static int get_access_flags
PARAMS ((tree
));
294 static void write_chunks
PARAMS ((FILE *, struct chunk
*));
295 static int adjust_typed_op
PARAMS ((tree
, int));
296 static void generate_bytecode_conditional
PARAMS ((tree
, struct jcf_block
*,
297 struct jcf_block
*, int,
298 struct jcf_partial
*));
299 static void generate_bytecode_return
PARAMS ((tree
, struct jcf_partial
*));
300 static void perform_relocations
PARAMS ((struct jcf_partial
*));
301 static void init_jcf_state
PARAMS ((struct jcf_partial
*, struct obstack
*));
302 static void init_jcf_method
PARAMS ((struct jcf_partial
*, tree
));
303 static void release_jcf_state
PARAMS ((struct jcf_partial
*));
304 static struct chunk
* generate_classfile
PARAMS ((tree
, struct jcf_partial
*));
305 static struct jcf_handler
*alloc_handler
PARAMS ((struct jcf_block
*,
307 struct jcf_partial
*));
308 static void emit_iinc
PARAMS ((tree
, HOST_WIDE_INT
, struct jcf_partial
*));
309 static void emit_reloc
PARAMS ((HOST_WIDE_INT
, int, struct jcf_block
*,
310 struct jcf_partial
*));
311 static void push_constant1
PARAMS ((HOST_WIDE_INT
, struct jcf_partial
*));
312 static void push_constant2
PARAMS ((HOST_WIDE_INT
, struct jcf_partial
*));
313 static void push_int_const
PARAMS ((HOST_WIDE_INT
, struct jcf_partial
*));
314 static int find_constant_wide
PARAMS ((HOST_WIDE_INT
, HOST_WIDE_INT
,
315 struct jcf_partial
*));
316 static void push_long_const
PARAMS ((HOST_WIDE_INT
, HOST_WIDE_INT
,
317 struct jcf_partial
*));
318 static int find_constant_index
PARAMS ((tree
, struct jcf_partial
*));
319 static void push_long_const
PARAMS ((HOST_WIDE_INT
, HOST_WIDE_INT
,
320 struct jcf_partial
*));
321 static void field_op
PARAMS ((tree
, int, struct jcf_partial
*));
322 static void maybe_wide
PARAMS ((int, int, struct jcf_partial
*));
323 static void emit_dup
PARAMS ((int, int, struct jcf_partial
*));
324 static void emit_pop
PARAMS ((int, struct jcf_partial
*));
325 static void emit_load_or_store
PARAMS ((tree
, int, struct jcf_partial
*));
326 static void emit_load
PARAMS ((tree
, struct jcf_partial
*));
327 static void emit_store
PARAMS ((tree
, struct jcf_partial
*));
328 static void emit_unop
PARAMS ((enum java_opcode
, tree
, struct jcf_partial
*));
329 static void emit_binop
PARAMS ((enum java_opcode
, tree
, struct jcf_partial
*));
330 static void emit_reloc
PARAMS ((HOST_WIDE_INT
, int, struct jcf_block
*,
331 struct jcf_partial
*));
332 static void emit_switch_reloc
PARAMS ((struct jcf_block
*,
333 struct jcf_partial
*));
334 static void emit_case_reloc
PARAMS ((struct jcf_relocation
*,
335 struct jcf_partial
*));
336 static void emit_if
PARAMS ((struct jcf_block
*, int, int,
337 struct jcf_partial
*));
338 static void emit_goto
PARAMS ((struct jcf_block
*, struct jcf_partial
*));
339 static void emit_jsr
PARAMS ((struct jcf_block
*, struct jcf_partial
*));
340 static void call_cleanups
PARAMS ((struct jcf_block
*, struct jcf_partial
*));
341 static char *make_class_file_name
PARAMS ((tree
));
342 static unsigned char *append_synthetic_attribute
PARAMS ((struct jcf_partial
*));
343 static void append_innerclasses_attribute
PARAMS ((struct jcf_partial
*, tree
));
344 static void append_innerclasses_attribute_entry
PARAMS ((struct jcf_partial
*, tree
, tree
));
346 /* Utility macros for appending (big-endian) data to a buffer.
347 We assume a local variable 'ptr' points into where we want to
348 write next, and we assume enoygh space has been allocated. */
350 #ifdef ENABLE_CHECKING
352 CHECK_PUT(ptr
, state
, i
)
354 struct jcf_partial
*state
;
357 if (ptr
< state
->chunk
->data
358 || (char*)ptr
+ i
> state
->chunk
->data
+ state
->chunk
->size
)
359 fatal ("internal error - CHECK_PUT failed");
363 #define CHECK_PUT(PTR, STATE, I) ((void)0)
366 #define PUT1(X) (CHECK_PUT(ptr, state, 1), *ptr++ = (X))
367 #define PUT2(X) (PUT1((X) >> 8), PUT1((X) & 0xFF))
368 #define PUT4(X) (PUT2((X) >> 16), PUT2((X) & 0xFFFF))
369 #define PUTN(P, N) (CHECK_PUT(ptr, state, N), memcpy(ptr, P, N), ptr += (N))
371 /* There are some cases below where CHECK_PUT is guaranteed to fail.
372 Use the following macros in those specific cases. */
373 #define UNSAFE_PUT1(X) (*ptr++ = (X))
374 #define UNSAFE_PUT2(X) (UNSAFE_PUT1((X) >> 8), UNSAFE_PUT1((X) & 0xFF))
375 #define UNSAFE_PUT4(X) (UNSAFE_PUT2((X) >> 16), UNSAFE_PUT2((X) & 0xFFFF))
376 #define UNSAFE_PUTN(P, N) (memcpy(ptr, P, N), ptr += (N))
379 /* Allocate a new chunk on obstack WORK, and link it in after LAST.
380 Set the data and size fields to DATA and SIZE, respectively.
381 However, if DATA is NULL and SIZE>0, allocate a buffer as well. */
383 static struct chunk
*
384 alloc_chunk (last
, data
, size
, work
)
388 struct obstack
*work
;
390 struct chunk
*chunk
= (struct chunk
*)
391 obstack_alloc (work
, sizeof(struct chunk
));
393 if (data
== NULL
&& size
> 0)
394 data
= obstack_alloc (work
, size
);
404 #ifdef ENABLE_CHECKING
406 CHECK_OP(struct jcf_partial
*state
)
408 if (state
->bytecode
.ptr
> state
->bytecode
.limit
)
410 fatal("internal error - CHECK_OP failed");
415 #define CHECK_OP(STATE) ((void)0)
418 static unsigned char *
419 append_chunk (data
, size
, state
)
422 struct jcf_partial
*state
;
424 state
->chunk
= alloc_chunk (state
->chunk
, data
, size
, state
->chunk_obstack
);
425 if (state
->first
== NULL
)
426 state
->first
= state
->chunk
;
427 return state
->chunk
->data
;
431 append_chunk_copy (data
, size
, state
)
434 struct jcf_partial
*state
;
436 unsigned char *ptr
= append_chunk (NULL
, size
, state
);
437 memcpy (ptr
, data
, size
);
440 static struct jcf_block
*
441 gen_jcf_label (state
)
442 struct jcf_partial
*state
;
444 struct jcf_block
*block
= (struct jcf_block
*)
445 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_block
));
447 block
->linenumber
= -1;
448 block
->pc
= UNDEFINED_PC
;
453 finish_jcf_block (state
)
454 struct jcf_partial
*state
;
456 struct jcf_block
*block
= state
->last_block
;
457 struct jcf_relocation
*reloc
;
458 int code_length
= BUFFER_LENGTH (&state
->bytecode
);
459 int pc
= state
->code_length
;
460 append_chunk_copy (state
->bytecode
.data
, code_length
, state
);
461 BUFFER_RESET (&state
->bytecode
);
462 block
->v
.chunk
= state
->chunk
;
464 /* Calculate code_length to the maximum value it can have. */
465 pc
+= block
->v
.chunk
->size
;
466 for (reloc
= block
->u
.relocations
; reloc
!= NULL
; reloc
= reloc
->next
)
468 int kind
= reloc
->kind
;
469 if (kind
== SWITCH_ALIGN_RELOC
)
471 else if (kind
> BLOCK_START_RELOC
)
472 pc
+= 2; /* 2-byte offset may grow to 4-byte offset */
474 pc
+= 5; /* May need to add a goto_w. */
476 state
->code_length
= pc
;
480 define_jcf_label (label
, state
)
481 struct jcf_block
*label
;
482 struct jcf_partial
*state
;
484 if (state
->last_block
!= NULL
)
485 finish_jcf_block (state
);
486 label
->pc
= state
->code_length
;
487 if (state
->blocks
== NULL
)
488 state
->blocks
= label
;
490 state
->last_block
->next
= label
;
491 state
->last_block
= label
;
493 label
->u
.relocations
= NULL
;
496 static struct jcf_block
*
497 get_jcf_label_here (state
)
498 struct jcf_partial
*state
;
500 if (state
->last_block
!= NULL
&& BUFFER_LENGTH (&state
->bytecode
) == 0)
501 return state
->last_block
;
504 struct jcf_block
*label
= gen_jcf_label (state
);
505 define_jcf_label (label
, state
);
510 /* Note a line number entry for the current PC and given LINE. */
513 put_linenumber (line
, state
)
515 struct jcf_partial
*state
;
517 struct jcf_block
*label
= get_jcf_label_here (state
);
518 if (label
->linenumber
> 0)
520 label
= gen_jcf_label (state
);
521 define_jcf_label (label
, state
);
523 label
->linenumber
= line
;
524 state
->linenumber_count
++;
527 /* Allocate a new jcf_handler, for a catch clause that catches exceptions
528 in the range (START_LABEL, END_LABEL). */
530 static struct jcf_handler
*
531 alloc_handler (start_label
, end_label
, state
)
532 struct jcf_block
*start_label
;
533 struct jcf_block
*end_label
;
534 struct jcf_partial
*state
;
536 struct jcf_handler
*handler
= (struct jcf_handler
*)
537 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_handler
));
538 handler
->start_label
= start_label
;
539 handler
->end_label
= end_label
;
540 handler
->handler_label
= get_jcf_label_here (state
);
541 if (state
->handlers
== NULL
)
542 state
->handlers
= handler
;
544 state
->last_handler
->next
= handler
;
545 state
->last_handler
= handler
;
546 handler
->next
= NULL
;
547 state
->num_handlers
++;
552 /* The index of jvm local variable allocated for this DECL.
553 This is assigned when generating .class files;
554 contrast DECL_LOCAL_SLOT_NUMBER which is set when *reading* a .class file.
555 (We don't allocate DECL_LANG_SPECIFIC for locals from Java sourc code.) */
557 #define DECL_LOCAL_INDEX(DECL) DECL_ALIGN(DECL)
561 struct localvar_info
*next
;
564 struct jcf_block
*start_label
;
565 struct jcf_block
*end_label
;
568 #define localvar_buffer ((struct localvar_info**) state->localvars.data)
569 #define localvar_max \
570 ((struct localvar_info**) state->localvars.ptr - localvar_buffer)
573 localvar_alloc (decl
, state
)
575 struct jcf_partial
*state
;
577 struct jcf_block
*start_label
= get_jcf_label_here (state
);
578 int wide
= TYPE_IS_WIDE (TREE_TYPE (decl
));
580 register struct localvar_info
*info
;
581 register struct localvar_info
**ptr
= localvar_buffer
;
582 register struct localvar_info
**limit
583 = (struct localvar_info
**) state
->localvars
.ptr
;
584 for (index
= 0; ptr
< limit
; index
++, ptr
++)
587 && (! wide
|| ((ptr
+1) < limit
&& ptr
[1] == NULL
)))
592 buffer_grow (&state
->localvars
, 2 * sizeof (struct localvar_info
*));
593 ptr
= (struct localvar_info
**) state
->localvars
.data
+ index
;
594 state
->localvars
.ptr
= (unsigned char *) (ptr
+ 1 + wide
);
596 info
= (struct localvar_info
*)
597 obstack_alloc (state
->chunk_obstack
, sizeof (struct localvar_info
));
600 ptr
[1] = (struct localvar_info
*)(~0);
601 DECL_LOCAL_INDEX (decl
) = index
;
603 info
->start_label
= start_label
;
605 if (debug_info_level
> DINFO_LEVEL_TERSE
606 && DECL_NAME (decl
) != NULL_TREE
)
608 /* Generate debugging info. */
610 if (state
->last_lvar
!= NULL
)
611 state
->last_lvar
->next
= info
;
613 state
->first_lvar
= info
;
614 state
->last_lvar
= info
;
620 localvar_free (decl
, state
)
622 struct jcf_partial
*state
;
624 struct jcf_block
*end_label
= get_jcf_label_here (state
);
625 int index
= DECL_LOCAL_INDEX (decl
);
626 register struct localvar_info
**ptr
= &localvar_buffer
[index
];
627 register struct localvar_info
*info
= *ptr
;
628 int wide
= TYPE_IS_WIDE (TREE_TYPE (decl
));
630 info
->end_label
= end_label
;
632 if (info
->decl
!= decl
)
637 if (ptr
[1] != (struct localvar_info
*)(~0))
644 #define STACK_TARGET 1
645 #define IGNORE_TARGET 2
647 /* Get the access flags of a class (TYPE_DECL), a method (FUNCTION_DECL), or
648 a field (FIELD_DECL or VAR_DECL, if static), as encoded in a .class file. */
651 get_access_flags (decl
)
655 int isfield
= TREE_CODE (decl
) == FIELD_DECL
|| TREE_CODE (decl
) == VAR_DECL
;
656 if (CLASS_PUBLIC (decl
)) /* same as FIELD_PUBLIC and METHOD_PUBLIC */
658 if (CLASS_FINAL (decl
)) /* same as FIELD_FINAL and METHOD_FINAL */
660 if (isfield
|| TREE_CODE (decl
) == FUNCTION_DECL
)
662 if (TREE_PROTECTED (decl
))
663 flags
|= ACC_PROTECTED
;
664 if (TREE_PRIVATE (decl
))
665 flags
|= ACC_PRIVATE
;
667 else if (TREE_CODE (decl
) == TYPE_DECL
)
669 if (CLASS_SUPER (decl
))
671 if (CLASS_ABSTRACT (decl
))
672 flags
|= ACC_ABSTRACT
;
673 if (CLASS_INTERFACE (decl
))
674 flags
|= ACC_INTERFACE
;
675 if (CLASS_STATIC (decl
))
677 if (ANONYMOUS_CLASS_P (TREE_TYPE (decl
))
678 || LOCAL_CLASS_P (TREE_TYPE (decl
)))
679 flags
|= ACC_PRIVATE
;
682 fatal ("internal error - bad argument to get_access_flags");
683 if (TREE_CODE (decl
) == FUNCTION_DECL
)
685 if (METHOD_NATIVE (decl
))
687 if (METHOD_STATIC (decl
))
689 if (METHOD_SYNCHRONIZED (decl
))
690 flags
|= ACC_SYNCHRONIZED
;
691 if (METHOD_ABSTRACT (decl
))
692 flags
|= ACC_ABSTRACT
;
696 if (FIELD_STATIC (decl
))
698 if (FIELD_VOLATILE (decl
))
699 flags
|= ACC_VOLATILE
;
700 if (FIELD_TRANSIENT (decl
))
701 flags
|= ACC_TRANSIENT
;
706 /* Write the list of segments starting at CHUNKS to STREAM. */
709 write_chunks (stream
, chunks
)
711 struct chunk
*chunks
;
713 for (; chunks
!= NULL
; chunks
= chunks
->next
)
714 fwrite (chunks
->data
, chunks
->size
, 1, stream
);
717 /* Push a 1-word constant in the constant pool at the given INDEX.
718 (Caller is responsible for doing NOTE_PUSH.) */
721 push_constant1 (index
, state
)
723 struct jcf_partial
*state
;
738 /* Push a 2-word constant in the constant pool at the given INDEX.
739 (Caller is responsible for doing NOTE_PUSH.) */
742 push_constant2 (index
, state
)
744 struct jcf_partial
*state
;
751 /* Push 32-bit integer constant on VM stack.
752 Caller is responsible for doing NOTE_PUSH. */
755 push_int_const (i
, state
)
757 struct jcf_partial
*state
;
760 if (i
>= -1 && i
<= 5)
761 OP1(OPCODE_iconst_0
+ i
);
762 else if (i
>= -128 && i
< 128)
767 else if (i
>= -32768 && i
< 32768)
774 i
= find_constant1 (&state
->cpool
, CONSTANT_Integer
,
775 (jword
)(i
& 0xFFFFFFFF));
776 push_constant1 (i
, state
);
781 find_constant_wide (lo
, hi
, state
)
782 HOST_WIDE_INT lo
, hi
;
783 struct jcf_partial
*state
;
785 HOST_WIDE_INT w1
, w2
;
786 lshift_double (lo
, hi
, -32, 64, &w1
, &w2
, 1);
787 return find_constant2 (&state
->cpool
, CONSTANT_Long
,
788 (jword
)(w1
& 0xFFFFFFFF), (jword
)(lo
& 0xFFFFFFFF));
791 /* Find or allocate a constant pool entry for the given VALUE.
792 Return the index in the constant pool. */
795 find_constant_index (value
, state
)
797 struct jcf_partial
*state
;
799 if (TREE_CODE (value
) == INTEGER_CST
)
801 if (TYPE_PRECISION (TREE_TYPE (value
)) <= 32)
802 return find_constant1 (&state
->cpool
, CONSTANT_Integer
,
803 (jword
)(TREE_INT_CST_LOW (value
) & 0xFFFFFFFF));
805 return find_constant_wide (TREE_INT_CST_LOW (value
),
806 TREE_INT_CST_HIGH (value
), state
);
808 else if (TREE_CODE (value
) == REAL_CST
)
811 if (TYPE_PRECISION (TREE_TYPE (value
)) == 32)
813 words
[0] = etarsingle (TREE_REAL_CST (value
)) & 0xFFFFFFFF;
814 return find_constant1 (&state
->cpool
, CONSTANT_Float
,
819 etardouble (TREE_REAL_CST (value
), words
);
820 return find_constant2 (&state
->cpool
, CONSTANT_Double
,
821 (jword
)(words
[1-FLOAT_WORDS_BIG_ENDIAN
] &
823 (jword
)(words
[FLOAT_WORDS_BIG_ENDIAN
] &
827 else if (TREE_CODE (value
) == STRING_CST
)
829 return find_string_constant (&state
->cpool
, value
);
832 fatal ("find_constant_index - bad type");
835 /* Push 64-bit long constant on VM stack.
836 Caller is responsible for doing NOTE_PUSH. */
839 push_long_const (lo
, hi
, state
)
840 HOST_WIDE_INT lo
, hi
;
841 struct jcf_partial
*state
;
843 if (hi
== 0 && lo
>= 0 && lo
<= 1)
846 OP1(OPCODE_lconst_0
+ lo
);
848 else if ((hi
== 0 && (jword
)(lo
& 0xFFFFFFFF) < 32768)
849 || (hi
== -1 && (jword
)(lo
& 0xFFFFFFFF) >= -32768))
851 push_int_const (lo
, state
);
856 push_constant2 (find_constant_wide (lo
, hi
, state
), state
);
860 field_op (field
, opcode
, state
)
863 struct jcf_partial
*state
;
865 int index
= find_fieldref_index (&state
->cpool
, field
);
871 /* Returns an integer in the range 0 (for 'int') through 4 (for object
872 reference) to 7 (for 'short') which matches the pattern of how JVM
873 opcodes typically depend on the operand type. */
876 adjust_typed_op (type
, max
)
880 switch (TREE_CODE (type
))
883 case RECORD_TYPE
: return 4;
885 return TYPE_PRECISION (type
) == 32 || max
< 5 ? 0 : 5;
887 return TYPE_PRECISION (type
) == 32 || max
< 6 ? 0 : 6;
889 switch (TYPE_PRECISION (type
))
891 case 8: return max
< 5 ? 0 : 5;
892 case 16: return max
< 7 ? 0 : 7;
898 switch (TYPE_PRECISION (type
))
911 maybe_wide (opcode
, index
, state
)
913 struct jcf_partial
*state
;
930 /* Compile code to duplicate with offset, where
931 SIZE is the size of the stack item to duplicate (1 or 2), abd
932 OFFSET is where to insert the result (must be 0, 1, or 2).
933 (The new words get inserted at stack[SP-size-offset].) */
936 emit_dup (size
, offset
, state
)
938 struct jcf_partial
*state
;
945 kind
= size
== 1 ? OPCODE_dup
: OPCODE_dup2
;
946 else if (offset
== 1)
947 kind
= size
== 1 ? OPCODE_dup_x1
: OPCODE_dup2_x1
;
948 else if (offset
== 2)
949 kind
= size
== 1 ? OPCODE_dup_x2
: OPCODE_dup2_x2
;
957 emit_pop (size
, state
)
959 struct jcf_partial
*state
;
962 OP1 (OPCODE_pop
- 1 + size
);
966 emit_iinc (var
, value
, state
)
969 struct jcf_partial
*state
;
971 int slot
= DECL_LOCAL_INDEX (var
);
973 if (value
< -128 || value
> 127 || slot
>= 256)
991 emit_load_or_store (var
, opcode
, state
)
992 tree var
; /* Variable to load from or store into. */
993 int opcode
; /* Either OPCODE_iload or OPCODE_istore. */
994 struct jcf_partial
*state
;
996 tree type
= TREE_TYPE (var
);
997 int kind
= adjust_typed_op (type
, 4);
998 int index
= DECL_LOCAL_INDEX (var
);
1002 OP1 (opcode
+ 5 + 4 * kind
+ index
); /* [ilfda]{load,store}_[0123] */
1005 maybe_wide (opcode
+ kind
, index
, state
); /* [ilfda]{load,store} */
1009 emit_load (var
, state
)
1011 struct jcf_partial
*state
;
1013 emit_load_or_store (var
, OPCODE_iload
, state
);
1014 NOTE_PUSH (TYPE_IS_WIDE (TREE_TYPE (var
)) ? 2 : 1);
1018 emit_store (var
, state
)
1020 struct jcf_partial
*state
;
1022 emit_load_or_store (var
, OPCODE_istore
, state
);
1023 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (var
)) ? 2 : 1);
1027 emit_unop (opcode
, type
, state
)
1028 enum java_opcode opcode
;
1029 tree type ATTRIBUTE_UNUSED
;
1030 struct jcf_partial
*state
;
1037 emit_binop (opcode
, type
, state
)
1038 enum java_opcode opcode
;
1040 struct jcf_partial
*state
;
1042 int size
= TYPE_IS_WIDE (type
) ? 2 : 1;
1049 emit_reloc (value
, kind
, target
, state
)
1050 HOST_WIDE_INT value
;
1052 struct jcf_block
*target
;
1053 struct jcf_partial
*state
;
1055 struct jcf_relocation
*reloc
= (struct jcf_relocation
*)
1056 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_relocation
));
1057 struct jcf_block
*block
= state
->last_block
;
1058 reloc
->next
= block
->u
.relocations
;
1059 block
->u
.relocations
= reloc
;
1060 reloc
->offset
= BUFFER_LENGTH (&state
->bytecode
);
1061 reloc
->label
= target
;
1063 if (kind
== 0 || kind
== BLOCK_START_RELOC
)
1065 else if (kind
!= SWITCH_ALIGN_RELOC
)
1070 emit_switch_reloc (label
, state
)
1071 struct jcf_block
*label
;
1072 struct jcf_partial
*state
;
1074 emit_reloc (RELOCATION_VALUE_0
, BLOCK_START_RELOC
, label
, state
);
1077 /* Similar to emit_switch_reloc,
1078 but re-uses an existing case reloc. */
1081 emit_case_reloc (reloc
, state
)
1082 struct jcf_relocation
*reloc
;
1083 struct jcf_partial
*state
;
1085 struct jcf_block
*block
= state
->last_block
;
1086 reloc
->next
= block
->u
.relocations
;
1087 block
->u
.relocations
= reloc
;
1088 reloc
->offset
= BUFFER_LENGTH (&state
->bytecode
);
1089 reloc
->kind
= BLOCK_START_RELOC
;
1093 /* Emit a conditional jump to TARGET with a 2-byte relative jump offset
1094 The opcode is OPCODE, the inverted opcode is INV_OPCODE. */
1097 emit_if (target
, opcode
, inv_opcode
, state
)
1098 struct jcf_block
*target
;
1099 int opcode
, inv_opcode
;
1100 struct jcf_partial
*state
;
1104 /* value is 1 byte from reloc back to start of instruction. */
1105 emit_reloc (RELOCATION_VALUE_1
, - inv_opcode
, target
, state
);
1109 emit_goto (target
, state
)
1110 struct jcf_block
*target
;
1111 struct jcf_partial
*state
;
1115 /* Value is 1 byte from reloc back to start of instruction. */
1116 emit_reloc (RELOCATION_VALUE_1
, OPCODE_goto_w
, target
, state
);
1120 emit_jsr (target
, state
)
1121 struct jcf_block
*target
;
1122 struct jcf_partial
*state
;
1126 /* Value is 1 byte from reloc back to start of instruction. */
1127 emit_reloc (RELOCATION_VALUE_1
, OPCODE_jsr_w
, target
, state
);
1130 /* Generate code to evaluate EXP. If the result is true,
1131 branch to TRUE_LABEL; otherwise, branch to FALSE_LABEL.
1132 TRUE_BRANCH_FIRST is a code geneation hint that the
1133 TRUE_LABEL may follow right after this. (The idea is that we
1134 may be able to optimize away GOTO TRUE_LABEL; TRUE_LABEL:) */
1137 generate_bytecode_conditional (exp
, true_label
, false_label
,
1138 true_branch_first
, state
)
1140 struct jcf_block
*true_label
;
1141 struct jcf_block
*false_label
;
1142 int true_branch_first
;
1143 struct jcf_partial
*state
;
1145 tree exp0
, exp1
, type
;
1146 int save_SP
= state
->code_SP
;
1147 enum java_opcode op
, negop
;
1148 switch (TREE_CODE (exp
))
1151 emit_goto (integer_zerop (exp
) ? false_label
: true_label
, state
);
1155 struct jcf_block
*then_label
= gen_jcf_label (state
);
1156 struct jcf_block
*else_label
= gen_jcf_label (state
);
1157 int save_SP_before
, save_SP_after
;
1158 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1159 then_label
, else_label
, 1, state
);
1160 define_jcf_label (then_label
, state
);
1161 save_SP_before
= state
->code_SP
;
1162 generate_bytecode_conditional (TREE_OPERAND (exp
, 1),
1163 true_label
, false_label
, 1, state
);
1164 save_SP_after
= state
->code_SP
;
1165 state
->code_SP
= save_SP_before
;
1166 define_jcf_label (else_label
, state
);
1167 generate_bytecode_conditional (TREE_OPERAND (exp
, 2),
1168 true_label
, false_label
,
1169 true_branch_first
, state
);
1170 if (state
->code_SP
!= save_SP_after
)
1171 fatal ("internal error non-matching SP");
1174 case TRUTH_NOT_EXPR
:
1175 generate_bytecode_conditional (TREE_OPERAND (exp
, 0), false_label
, true_label
,
1176 ! true_branch_first
, state
);
1178 case TRUTH_ANDIF_EXPR
:
1180 struct jcf_block
*next_label
= gen_jcf_label (state
);
1181 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1182 next_label
, false_label
, 1, state
);
1183 define_jcf_label (next_label
, state
);
1184 generate_bytecode_conditional (TREE_OPERAND (exp
, 1),
1185 true_label
, false_label
, 1, state
);
1188 case TRUTH_ORIF_EXPR
:
1190 struct jcf_block
*next_label
= gen_jcf_label (state
);
1191 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1192 true_label
, next_label
, 1, state
);
1193 define_jcf_label (next_label
, state
);
1194 generate_bytecode_conditional (TREE_OPERAND (exp
, 1),
1195 true_label
, false_label
, 1, state
);
1199 /* Assuming op is one of the 2-operand if_icmp<COND> instructions,
1200 set it to the corresponding 1-operand if<COND> instructions. */
1204 /* The opcodes with their inverses are allocated in pairs.
1205 E.g. The inverse of if_icmplt (161) is if_icmpge (162). */
1206 negop
= (op
& 1) ? op
+ 1 : op
- 1;
1208 if (true_branch_first
)
1210 emit_if (false_label
, negop
, op
, state
);
1211 emit_goto (true_label
, state
);
1215 emit_if (true_label
, op
, negop
, state
);
1216 emit_goto (false_label
, state
);
1220 op
= OPCODE_if_icmpeq
;
1223 op
= OPCODE_if_icmpne
;
1226 op
= OPCODE_if_icmpgt
;
1229 op
= OPCODE_if_icmplt
;
1232 op
= OPCODE_if_icmpge
;
1235 op
= OPCODE_if_icmple
;
1238 exp0
= TREE_OPERAND (exp
, 0);
1239 exp1
= TREE_OPERAND (exp
, 1);
1240 type
= TREE_TYPE (exp0
);
1241 switch (TREE_CODE (type
))
1244 case POINTER_TYPE
: case RECORD_TYPE
:
1245 switch (TREE_CODE (exp
))
1247 case EQ_EXPR
: op
= OPCODE_if_acmpeq
; break;
1248 case NE_EXPR
: op
= OPCODE_if_acmpne
; break;
1251 if (integer_zerop (exp1
) || integer_zerop (exp0
))
1253 generate_bytecode_insns (integer_zerop (exp1
) ? exp0
: exp0
,
1254 STACK_TARGET
, state
);
1255 op
= op
+ (OPCODE_ifnull
- OPCODE_if_acmpeq
);
1256 negop
= (op
& 1) ? op
- 1 : op
+ 1;
1260 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1261 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1265 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1266 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1267 if (op
== OPCODE_if_icmplt
|| op
== OPCODE_if_icmple
)
1271 if (TYPE_PRECISION (type
) > 32)
1282 if (TYPE_PRECISION (type
) > 32)
1284 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1285 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1293 if (integer_zerop (exp1
))
1295 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1299 if (integer_zerop (exp0
))
1303 case OPCODE_if_icmplt
:
1304 case OPCODE_if_icmpge
:
1307 case OPCODE_if_icmpgt
:
1308 case OPCODE_if_icmple
:
1314 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1318 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1319 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1325 generate_bytecode_insns (exp
, STACK_TARGET
, state
);
1327 if (true_branch_first
)
1329 emit_if (false_label
, OPCODE_ifeq
, OPCODE_ifne
, state
);
1330 emit_goto (true_label
, state
);
1334 emit_if (true_label
, OPCODE_ifne
, OPCODE_ifeq
, state
);
1335 emit_goto (false_label
, state
);
1339 if (save_SP
!= state
->code_SP
)
1340 fatal ("internal error - SP mismatch");
1343 /* Call pending cleanups i.e. those for surrounding CLEANUP_POINT_EXPRs
1344 but only as far out as LIMIT (since we are about to jump to the
1345 emit label that is LIMIT). */
1348 call_cleanups (limit
, state
)
1349 struct jcf_block
*limit
;
1350 struct jcf_partial
*state
;
1352 struct jcf_block
*block
= state
->labeled_blocks
;
1353 for (; block
!= limit
; block
= block
->next
)
1355 if (block
->pc
== PENDING_CLEANUP_PC
)
1356 emit_jsr (block
, state
);
1361 generate_bytecode_return (exp
, state
)
1363 struct jcf_partial
*state
;
1365 tree return_type
= TREE_TYPE (TREE_TYPE (state
->current_method
));
1366 int returns_void
= TREE_CODE (return_type
) == VOID_TYPE
;
1371 switch (TREE_CODE (exp
))
1374 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
,
1376 exp
= TREE_OPERAND (exp
, 1);
1380 struct jcf_block
*then_label
= gen_jcf_label (state
);
1381 struct jcf_block
*else_label
= gen_jcf_label (state
);
1382 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1383 then_label
, else_label
, 1, state
);
1384 define_jcf_label (then_label
, state
);
1385 generate_bytecode_return (TREE_OPERAND (exp
, 1), state
);
1386 define_jcf_label (else_label
, state
);
1387 generate_bytecode_return (TREE_OPERAND (exp
, 2), state
);
1391 generate_bytecode_insns (exp
,
1392 returns_void
? IGNORE_TARGET
1393 : STACK_TARGET
, state
);
1399 call_cleanups (NULL_PTR
, state
);
1403 op
= OPCODE_ireturn
+ adjust_typed_op (return_type
, 4);
1404 if (state
->num_finalizers
> 0)
1406 if (state
->return_value_decl
== NULL_TREE
)
1408 state
->return_value_decl
1409 = build_decl (VAR_DECL
, NULL_TREE
, TREE_TYPE (exp
));
1410 localvar_alloc (state
->return_value_decl
, state
);
1412 emit_store (state
->return_value_decl
, state
);
1413 call_cleanups (NULL_PTR
, state
);
1414 emit_load (state
->return_value_decl
, state
);
1415 /* If we call localvar_free (state->return_value_decl, state),
1416 then we risk the save decl erroneously re-used in the
1417 finalizer. Instead, we keep the state->return_value_decl
1418 allocated through the rest of the method. This is not
1419 the greatest solution, but it is at least simple and safe. */
1426 /* Generate bytecode for sub-expression EXP of METHOD.
1427 TARGET is one of STACK_TARGET or IGNORE_TARGET. */
1430 generate_bytecode_insns (exp
, target
, state
)
1433 struct jcf_partial
*state
;
1436 enum java_opcode jopcode
;
1438 HOST_WIDE_INT value
;
1443 if (exp
== NULL
&& target
== IGNORE_TARGET
)
1446 type
= TREE_TYPE (exp
);
1448 switch (TREE_CODE (exp
))
1451 if (BLOCK_EXPR_BODY (exp
))
1454 tree body
= BLOCK_EXPR_BODY (exp
);
1455 for (local
= BLOCK_EXPR_DECLS (exp
); local
; )
1457 tree next
= TREE_CHAIN (local
);
1458 localvar_alloc (local
, state
);
1461 /* Avoid deep recursion for long blocks. */
1462 while (TREE_CODE (body
) == COMPOUND_EXPR
)
1464 generate_bytecode_insns (TREE_OPERAND (body
, 0), target
, state
);
1465 body
= TREE_OPERAND (body
, 1);
1467 generate_bytecode_insns (body
, target
, state
);
1468 for (local
= BLOCK_EXPR_DECLS (exp
); local
; )
1470 tree next
= TREE_CHAIN (local
);
1471 localvar_free (local
, state
);
1477 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
, state
);
1478 generate_bytecode_insns (TREE_OPERAND (exp
, 1), target
, state
);
1480 case EXPR_WITH_FILE_LOCATION
:
1482 const char *saved_input_filename
= input_filename
;
1483 tree body
= EXPR_WFL_NODE (exp
);
1484 int saved_lineno
= lineno
;
1485 if (body
== empty_stmt_node
)
1487 input_filename
= EXPR_WFL_FILENAME (exp
);
1488 lineno
= EXPR_WFL_LINENO (exp
);
1489 if (EXPR_WFL_EMIT_LINE_NOTE (exp
) && lineno
> 0
1490 && debug_info_level
> DINFO_LEVEL_NONE
)
1491 put_linenumber (lineno
, state
);
1492 generate_bytecode_insns (body
, target
, state
);
1493 input_filename
= saved_input_filename
;
1494 lineno
= saved_lineno
;
1498 if (target
== IGNORE_TARGET
) ; /* do nothing */
1499 else if (TREE_CODE (type
) == POINTER_TYPE
)
1501 if (! integer_zerop (exp
))
1504 OP1 (OPCODE_aconst_null
);
1507 else if (TYPE_PRECISION (type
) <= 32)
1509 push_int_const (TREE_INT_CST_LOW (exp
), state
);
1514 push_long_const (TREE_INT_CST_LOW (exp
), TREE_INT_CST_HIGH (exp
),
1521 int prec
= TYPE_PRECISION (type
) >> 5;
1523 if (real_zerop (exp
))
1524 OP1 (prec
== 1 ? OPCODE_fconst_0
: OPCODE_dconst_0
);
1525 else if (real_onep (exp
))
1526 OP1 (prec
== 1 ? OPCODE_fconst_1
: OPCODE_dconst_1
);
1527 /* FIXME Should also use fconst_2 for 2.0f.
1528 Also, should use iconst_2/ldc followed by i2f/i2d
1529 for other float/double when the value is a small integer. */
1532 offset
= find_constant_index (exp
, state
);
1534 push_constant1 (offset
, state
);
1536 push_constant2 (offset
, state
);
1542 push_constant1 (find_string_constant (&state
->cpool
, exp
), state
);
1546 if (TREE_STATIC (exp
))
1548 field_op (exp
, OPCODE_getstatic
, state
);
1549 NOTE_PUSH (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 2 : 1);
1552 /* ... fall through ... */
1554 emit_load (exp
, state
);
1556 case NON_LVALUE_EXPR
:
1558 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
1561 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
1562 generate_bytecode_insns (TREE_OPERAND (exp
, 1), target
, state
);
1563 if (target
!= IGNORE_TARGET
)
1565 jopcode
= OPCODE_iaload
+ adjust_typed_op (type
, 7);
1568 if (! TYPE_IS_WIDE (type
))
1574 tree obj
= TREE_OPERAND (exp
, 0);
1575 tree field
= TREE_OPERAND (exp
, 1);
1576 int is_static
= FIELD_STATIC (field
);
1577 generate_bytecode_insns (obj
,
1578 is_static
? IGNORE_TARGET
: target
, state
);
1579 if (target
!= IGNORE_TARGET
)
1581 if (DECL_NAME (field
) == length_identifier_node
&& !is_static
1582 && TYPE_ARRAY_P (TREE_TYPE (obj
)))
1585 OP1 (OPCODE_arraylength
);
1589 field_op (field
, is_static
? OPCODE_getstatic
: OPCODE_getfield
,
1593 NOTE_PUSH (TYPE_IS_WIDE (TREE_TYPE (field
)) ? 2 : 1);
1598 case TRUTH_ANDIF_EXPR
:
1599 case TRUTH_ORIF_EXPR
:
1607 struct jcf_block
*then_label
= gen_jcf_label (state
);
1608 struct jcf_block
*else_label
= gen_jcf_label (state
);
1609 struct jcf_block
*end_label
= gen_jcf_label (state
);
1610 generate_bytecode_conditional (exp
,
1611 then_label
, else_label
, 1, state
);
1612 define_jcf_label (then_label
, state
);
1613 push_int_const (1, state
);
1614 emit_goto (end_label
, state
);
1615 define_jcf_label (else_label
, state
);
1616 push_int_const (0, state
);
1617 define_jcf_label (end_label
, state
);
1623 struct jcf_block
*then_label
= gen_jcf_label (state
);
1624 struct jcf_block
*else_label
= gen_jcf_label (state
);
1625 struct jcf_block
*end_label
= gen_jcf_label (state
);
1626 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1627 then_label
, else_label
, 1, state
);
1628 define_jcf_label (then_label
, state
);
1629 generate_bytecode_insns (TREE_OPERAND (exp
, 1), target
, state
);
1630 if (CAN_COMPLETE_NORMALLY (TREE_OPERAND (exp
, 1))
1631 /* Not all expressions have CAN_COMPLETE_NORMALLY set properly. */
1632 || TREE_CODE (TREE_TYPE (exp
)) != VOID_TYPE
)
1633 emit_goto (end_label
, state
);
1634 define_jcf_label (else_label
, state
);
1635 generate_bytecode_insns (TREE_OPERAND (exp
, 2), target
, state
);
1636 define_jcf_label (end_label
, state
);
1637 /* COND_EXPR can be used in a binop. The stack must be adjusted. */
1638 if (TREE_TYPE (exp
) != void_type_node
)
1639 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 2 : 1);
1644 struct jcf_switch_state
*sw_state
= state
->sw_state
;
1645 struct jcf_relocation
*reloc
= (struct jcf_relocation
*)
1646 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_relocation
));
1647 HOST_WIDE_INT case_value
= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 0));
1649 reloc
->label
= get_jcf_label_here (state
);
1650 reloc
->offset
= case_value
;
1651 reloc
->next
= sw_state
->cases
;
1652 sw_state
->cases
= reloc
;
1653 if (sw_state
->num_cases
== 0)
1655 sw_state
->min_case
= case_value
;
1656 sw_state
->max_case
= case_value
;
1660 if (case_value
< sw_state
->min_case
)
1661 sw_state
->min_case
= case_value
;
1662 if (case_value
> sw_state
->max_case
)
1663 sw_state
->max_case
= case_value
;
1665 sw_state
->num_cases
++;
1669 state
->sw_state
->default_label
= get_jcf_label_here (state
);
1674 /* The SWITCH_EXPR has three parts, generated in the following order:
1675 1. the switch_expression (the value used to select the correct case);
1677 3. the switch_instruction (the tableswitch/loopupswitch instruction.).
1678 After code generation, we will re-order then in the order 1, 3, 2.
1679 This is to avoid an extra GOTOs. */
1680 struct jcf_switch_state sw_state
;
1681 struct jcf_block
*expression_last
; /* Last block of the switch_expression. */
1682 struct jcf_block
*body_last
; /* Last block of the switch_body. */
1683 struct jcf_block
*switch_instruction
; /* First block of switch_instruction. */
1684 struct jcf_block
*instruction_last
; /* Last block of the switch_instruction. */
1685 struct jcf_block
*body_block
;
1687 sw_state
.prev
= state
->sw_state
;
1688 state
->sw_state
= &sw_state
;
1689 sw_state
.cases
= NULL
;
1690 sw_state
.num_cases
= 0;
1691 sw_state
.default_label
= NULL
;
1692 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
1693 expression_last
= state
->last_block
;
1694 body_block
= get_jcf_label_here (state
); /* Force a new block here. */
1695 generate_bytecode_insns (TREE_OPERAND (exp
, 1), IGNORE_TARGET
, state
);
1696 body_last
= state
->last_block
;
1698 switch_instruction
= gen_jcf_label (state
);
1699 define_jcf_label (switch_instruction
, state
);
1700 if (sw_state
.default_label
== NULL
)
1701 sw_state
.default_label
= gen_jcf_label (state
);
1703 if (sw_state
.num_cases
<= 1)
1705 if (sw_state
.num_cases
== 0)
1707 emit_pop (1, state
);
1712 push_int_const (sw_state
.cases
->offset
, state
);
1713 emit_if (sw_state
.cases
->label
,
1714 OPCODE_ifeq
, OPCODE_ifne
, state
);
1716 emit_goto (sw_state
.default_label
, state
);
1721 /* Copy the chain of relocs into a sorted array. */
1722 struct jcf_relocation
**relocs
= (struct jcf_relocation
**)
1723 xmalloc (sw_state
.num_cases
* sizeof (struct jcf_relocation
*));
1724 /* The relocs arrays is a buffer with a gap.
1725 The assumption is that cases will normally come in "runs". */
1727 int gap_end
= sw_state
.num_cases
;
1728 struct jcf_relocation
*reloc
;
1729 for (reloc
= sw_state
.cases
; reloc
!= NULL
; reloc
= reloc
->next
)
1731 HOST_WIDE_INT case_value
= reloc
->offset
;
1732 while (gap_end
< sw_state
.num_cases
)
1734 struct jcf_relocation
*end
= relocs
[gap_end
];
1735 if (case_value
<= end
->offset
)
1737 relocs
[gap_start
++] = end
;
1740 while (gap_start
> 0)
1742 struct jcf_relocation
*before
= relocs
[gap_start
-1];
1743 if (case_value
>= before
->offset
)
1745 relocs
[--gap_end
] = before
;
1748 relocs
[gap_start
++] = reloc
;
1749 /* Note we don't check for duplicates. FIXME! */
1752 if (2 * sw_state
.num_cases
1753 >= sw_state
.max_case
- sw_state
.min_case
)
1754 { /* Use tableswitch. */
1756 RESERVE (13 + 4 * (sw_state
.max_case
- sw_state
.min_case
+ 1));
1757 OP1 (OPCODE_tableswitch
);
1758 emit_reloc (RELOCATION_VALUE_0
,
1759 SWITCH_ALIGN_RELOC
, NULL
, state
);
1760 emit_switch_reloc (sw_state
.default_label
, state
);
1761 OP4 (sw_state
.min_case
);
1762 OP4 (sw_state
.max_case
);
1763 for (i
= sw_state
.min_case
; ; )
1765 reloc
= relocs
[index
];
1766 if (i
== reloc
->offset
)
1768 emit_case_reloc (reloc
, state
);
1769 if (i
== sw_state
.max_case
)
1774 emit_switch_reloc (sw_state
.default_label
, state
);
1779 { /* Use lookupswitch. */
1780 RESERVE(9 + 8 * sw_state
.num_cases
);
1781 OP1 (OPCODE_lookupswitch
);
1782 emit_reloc (RELOCATION_VALUE_0
,
1783 SWITCH_ALIGN_RELOC
, NULL
, state
);
1784 emit_switch_reloc (sw_state
.default_label
, state
);
1785 OP4 (sw_state
.num_cases
);
1786 for (i
= 0; i
< sw_state
.num_cases
; i
++)
1788 struct jcf_relocation
*reloc
= relocs
[i
];
1789 OP4 (reloc
->offset
);
1790 emit_case_reloc (reloc
, state
);
1796 instruction_last
= state
->last_block
;
1797 if (sw_state
.default_label
->pc
< 0)
1798 define_jcf_label (sw_state
.default_label
, state
);
1799 else /* Force a new block. */
1800 sw_state
.default_label
= get_jcf_label_here (state
);
1801 /* Now re-arrange the blocks so the switch_instruction
1802 comes before the switch_body. */
1803 switch_length
= state
->code_length
- switch_instruction
->pc
;
1804 switch_instruction
->pc
= body_block
->pc
;
1805 instruction_last
->next
= body_block
;
1806 instruction_last
->v
.chunk
->next
= body_block
->v
.chunk
;
1807 expression_last
->next
= switch_instruction
;
1808 expression_last
->v
.chunk
->next
= switch_instruction
->v
.chunk
;
1809 body_last
->next
= sw_state
.default_label
;
1810 body_last
->v
.chunk
->next
= NULL
;
1811 state
->chunk
= body_last
->v
.chunk
;
1812 for (; body_block
!= sw_state
.default_label
; body_block
= body_block
->next
)
1813 body_block
->pc
+= switch_length
;
1815 state
->sw_state
= sw_state
.prev
;
1820 exp
= TREE_OPERAND (exp
, 0);
1821 if (exp
== NULL_TREE
)
1822 exp
= empty_stmt_node
;
1823 else if (TREE_CODE (exp
) != MODIFY_EXPR
)
1826 exp
= TREE_OPERAND (exp
, 1);
1827 generate_bytecode_return (exp
, state
);
1829 case LABELED_BLOCK_EXPR
:
1831 struct jcf_block
*end_label
= gen_jcf_label (state
);
1832 end_label
->next
= state
->labeled_blocks
;
1833 state
->labeled_blocks
= end_label
;
1834 end_label
->pc
= PENDING_EXIT_PC
;
1835 end_label
->u
.labeled_block
= exp
;
1836 if (LABELED_BLOCK_BODY (exp
))
1837 generate_bytecode_insns (LABELED_BLOCK_BODY (exp
), target
, state
);
1838 if (state
->labeled_blocks
!= end_label
)
1840 state
->labeled_blocks
= end_label
->next
;
1841 define_jcf_label (end_label
, state
);
1846 tree body
= TREE_OPERAND (exp
, 0);
1848 if (TREE_CODE (body
) == COMPOUND_EXPR
1849 && TREE_CODE (TREE_OPERAND (body
, 0)) == EXIT_EXPR
)
1851 /* Optimize: H: if (TEST) GOTO L; BODY; GOTO H; L:
1852 to: GOTO L; BODY; L: if (!TEST) GOTO L; */
1853 struct jcf_block
*head_label
;
1854 struct jcf_block
*body_label
;
1855 struct jcf_block
*end_label
= gen_jcf_label (state
);
1856 struct jcf_block
*exit_label
= state
->labeled_blocks
;
1857 head_label
= gen_jcf_label (state
);
1858 emit_goto (head_label
, state
);
1859 body_label
= get_jcf_label_here (state
);
1860 generate_bytecode_insns (TREE_OPERAND (body
, 1), target
, state
);
1861 define_jcf_label (head_label
, state
);
1862 generate_bytecode_conditional (TREE_OPERAND (body
, 0),
1863 end_label
, body_label
, 1, state
);
1864 define_jcf_label (end_label
, state
);
1869 struct jcf_block
*head_label
= get_jcf_label_here (state
);
1870 generate_bytecode_insns (body
, IGNORE_TARGET
, state
);
1871 emit_goto (head_label
, state
);
1877 struct jcf_block
*label
= state
->labeled_blocks
;
1878 struct jcf_block
*end_label
= gen_jcf_label (state
);
1879 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1880 label
, end_label
, 0, state
);
1881 define_jcf_label (end_label
, state
);
1884 case EXIT_BLOCK_EXPR
:
1886 struct jcf_block
*label
= state
->labeled_blocks
;
1887 if (TREE_OPERAND (exp
, 1) != NULL
) goto notimpl
;
1888 while (label
->u
.labeled_block
!= TREE_OPERAND (exp
, 0))
1889 label
= label
->next
;
1890 call_cleanups (label
, state
);
1891 emit_goto (label
, state
);
1895 case PREDECREMENT_EXPR
: value
= -1; post_op
= 0; goto increment
;
1896 case PREINCREMENT_EXPR
: value
= 1; post_op
= 0; goto increment
;
1897 case POSTDECREMENT_EXPR
: value
= -1; post_op
= 1; goto increment
;
1898 case POSTINCREMENT_EXPR
: value
= 1; post_op
= 1; goto increment
;
1901 exp
= TREE_OPERAND (exp
, 0);
1902 type
= TREE_TYPE (exp
);
1903 size
= TYPE_IS_WIDE (type
) ? 2 : 1;
1904 if ((TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1905 && ! TREE_STATIC (exp
)
1906 && TREE_CODE (type
) == INTEGER_TYPE
1907 && TYPE_PRECISION (type
) == 32)
1909 if (target
!= IGNORE_TARGET
&& post_op
)
1910 emit_load (exp
, state
);
1911 emit_iinc (exp
, value
, state
);
1912 if (target
!= IGNORE_TARGET
&& ! post_op
)
1913 emit_load (exp
, state
);
1916 if (TREE_CODE (exp
) == COMPONENT_REF
)
1918 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
1919 emit_dup (1, 0, state
);
1920 /* Stack: ..., objectref, objectref. */
1921 field_op (TREE_OPERAND (exp
, 1), OPCODE_getfield
, state
);
1923 /* Stack: ..., objectref, oldvalue. */
1926 else if (TREE_CODE (exp
) == ARRAY_REF
)
1928 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
1929 generate_bytecode_insns (TREE_OPERAND (exp
, 1), STACK_TARGET
, state
);
1930 emit_dup (2, 0, state
);
1931 /* Stack: ..., array, index, array, index. */
1932 jopcode
= OPCODE_iaload
+ adjust_typed_op (TREE_TYPE (exp
), 7);
1936 /* Stack: ..., array, index, oldvalue. */
1939 else if (TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1941 generate_bytecode_insns (exp
, STACK_TARGET
, state
);
1942 /* Stack: ..., oldvalue. */
1948 if (target
!= IGNORE_TARGET
&& post_op
)
1949 emit_dup (size
, offset
, state
);
1950 /* Stack, if ARRAY_REF: ..., [result, ] array, index, oldvalue. */
1951 /* Stack, if COMPONENT_REF: ..., [result, ] objectref, oldvalue. */
1952 /* Stack, otherwise: ..., [result, ] oldvalue. */
1954 push_int_const (value
, state
);
1956 push_long_const (value
, (HOST_WIDE_INT
)(value
>= 0 ? 0 : -1), state
);
1958 emit_binop (OPCODE_iadd
+ adjust_typed_op (type
, 3), type
, state
);
1959 if (target
!= IGNORE_TARGET
&& ! post_op
)
1960 emit_dup (size
, offset
, state
);
1961 /* Stack, if ARRAY_REF: ..., [result, ] array, index, newvalue. */
1962 /* Stack, if COMPONENT_REF: ..., [result, ] objectref, newvalue. */
1963 /* Stack, otherwise: ..., [result, ] newvalue. */
1964 goto finish_assignment
;
1968 tree lhs
= TREE_OPERAND (exp
, 0);
1969 tree rhs
= TREE_OPERAND (exp
, 1);
1972 /* See if we can use the iinc instruction. */
1973 if ((TREE_CODE (lhs
) == VAR_DECL
|| TREE_CODE (lhs
) == PARM_DECL
)
1974 && ! TREE_STATIC (lhs
)
1975 && TREE_CODE (TREE_TYPE (lhs
)) == INTEGER_TYPE
1976 && TYPE_PRECISION (TREE_TYPE (lhs
)) == 32
1977 && (TREE_CODE (rhs
) == PLUS_EXPR
|| TREE_CODE (rhs
) == MINUS_EXPR
))
1979 tree arg0
= TREE_OPERAND (rhs
, 0);
1980 tree arg1
= TREE_OPERAND (rhs
, 1);
1981 HOST_WIDE_INT min_value
= -32768;
1982 HOST_WIDE_INT max_value
= 32767;
1983 if (TREE_CODE (rhs
) == MINUS_EXPR
)
1988 else if (arg1
== lhs
)
1991 arg1
= TREE_OPERAND (rhs
, 0);
1993 if (lhs
== arg0
&& TREE_CODE (arg1
) == INTEGER_CST
)
1995 HOST_WIDE_INT hi_value
= TREE_INT_CST_HIGH (arg1
);
1996 value
= TREE_INT_CST_LOW (arg1
);
1997 if ((hi_value
== 0 && value
<= max_value
)
1998 || (hi_value
== -1 && value
>= min_value
))
2000 if (TREE_CODE (rhs
) == MINUS_EXPR
)
2002 emit_iinc (lhs
, value
, state
);
2008 if (TREE_CODE (lhs
) == COMPONENT_REF
)
2010 generate_bytecode_insns (TREE_OPERAND (lhs
, 0),
2011 STACK_TARGET
, state
);
2014 else if (TREE_CODE (lhs
) == ARRAY_REF
)
2016 generate_bytecode_insns (TREE_OPERAND(lhs
, 0),
2017 STACK_TARGET
, state
);
2018 generate_bytecode_insns (TREE_OPERAND(lhs
, 1),
2019 STACK_TARGET
, state
);
2024 generate_bytecode_insns (rhs
, STACK_TARGET
, state
);
2025 if (target
!= IGNORE_TARGET
)
2026 emit_dup (TYPE_IS_WIDE (type
) ? 2 : 1 , offset
, state
);
2032 if (TREE_CODE (exp
) == COMPONENT_REF
)
2034 tree field
= TREE_OPERAND (exp
, 1);
2035 if (! FIELD_STATIC (field
))
2038 FIELD_STATIC (field
) ? OPCODE_putstatic
: OPCODE_putfield
,
2041 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (field
)) ? 2 : 1);
2043 else if (TREE_CODE (exp
) == VAR_DECL
2044 || TREE_CODE (exp
) == PARM_DECL
)
2046 if (FIELD_STATIC (exp
))
2048 field_op (exp
, OPCODE_putstatic
, state
);
2049 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 2 : 1);
2052 emit_store (exp
, state
);
2054 else if (TREE_CODE (exp
) == ARRAY_REF
)
2056 jopcode
= OPCODE_iastore
+ adjust_typed_op (TREE_TYPE (exp
), 7);
2059 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 4 : 3);
2062 fatal ("internal error (bad lhs to MODIFY_EXPR)");
2065 jopcode
= OPCODE_iadd
;
2068 jopcode
= OPCODE_isub
;
2071 jopcode
= OPCODE_imul
;
2073 case TRUNC_DIV_EXPR
:
2075 jopcode
= OPCODE_idiv
;
2077 case TRUNC_MOD_EXPR
:
2078 jopcode
= OPCODE_irem
;
2080 case LSHIFT_EXPR
: jopcode
= OPCODE_ishl
; goto binop
;
2081 case RSHIFT_EXPR
: jopcode
= OPCODE_ishr
; goto binop
;
2082 case URSHIFT_EXPR
: jopcode
= OPCODE_iushr
; goto binop
;
2083 case TRUTH_AND_EXPR
:
2084 case BIT_AND_EXPR
: jopcode
= OPCODE_iand
; goto binop
;
2086 case BIT_IOR_EXPR
: jopcode
= OPCODE_ior
; goto binop
;
2087 case TRUTH_XOR_EXPR
:
2088 case BIT_XOR_EXPR
: jopcode
= OPCODE_ixor
; goto binop
;
2091 tree arg0
= TREE_OPERAND (exp
, 0);
2092 tree arg1
= TREE_OPERAND (exp
, 1);
2093 jopcode
+= adjust_typed_op (type
, 3);
2094 if (arg0
== arg1
&& TREE_CODE (arg0
) == SAVE_EXPR
)
2096 /* fold may (e.g) convert 2*x to x+x. */
2097 generate_bytecode_insns (TREE_OPERAND (arg0
, 0), target
, state
);
2098 emit_dup (TYPE_PRECISION (TREE_TYPE (arg0
)) > 32 ? 2 : 1, 0, state
);
2102 generate_bytecode_insns (arg0
, target
, state
);
2103 if (jopcode
>= OPCODE_lshl
&& jopcode
<= OPCODE_lushr
)
2104 arg1
= convert (int_type_node
, arg1
);
2105 generate_bytecode_insns (arg1
, target
, state
);
2107 /* For most binary operations, both operands and the result have the
2108 same type. Shift operations are different. Using arg1's type
2109 gets us the correct SP adjustment in all cases. */
2110 if (target
== STACK_TARGET
)
2111 emit_binop (jopcode
, TREE_TYPE (arg1
), state
);
2114 case TRUTH_NOT_EXPR
:
2116 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2117 if (target
== STACK_TARGET
)
2119 int is_long
= TYPE_PRECISION (TREE_TYPE (exp
)) > 32;
2120 push_int_const (TREE_CODE (exp
) == BIT_NOT_EXPR
? -1 : 1, state
);
2124 NOTE_PUSH (1 + is_long
);
2125 OP1 (OPCODE_ixor
+ is_long
);
2126 NOTE_POP (1 + is_long
);
2130 jopcode
= OPCODE_ineg
;
2131 jopcode
+= adjust_typed_op (type
, 3);
2132 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2133 if (target
== STACK_TARGET
)
2134 emit_unop (jopcode
, type
, state
);
2136 case INSTANCEOF_EXPR
:
2138 int index
= find_class_constant (&state
->cpool
, TREE_OPERAND (exp
, 1));
2139 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2141 OP1 (OPCODE_instanceof
);
2146 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
2151 case FIX_TRUNC_EXPR
:
2153 tree src
= TREE_OPERAND (exp
, 0);
2154 tree src_type
= TREE_TYPE (src
);
2155 tree dst_type
= TREE_TYPE (exp
);
2156 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2157 if (target
== IGNORE_TARGET
|| src_type
== dst_type
)
2159 if (TREE_CODE (dst_type
) == POINTER_TYPE
)
2161 if (TREE_CODE (exp
) == CONVERT_EXPR
)
2163 int index
= find_class_constant (&state
->cpool
,
2164 TREE_TYPE (dst_type
));
2166 OP1 (OPCODE_checkcast
);
2170 else /* Convert numeric types. */
2172 int wide_src
= TYPE_PRECISION (src_type
) > 32;
2173 int wide_dst
= TYPE_PRECISION (dst_type
) > 32;
2174 NOTE_POP (1 + wide_src
);
2176 if (TREE_CODE (dst_type
) == REAL_TYPE
)
2178 if (TREE_CODE (src_type
) == REAL_TYPE
)
2179 OP1 (wide_dst
? OPCODE_f2d
: OPCODE_d2f
);
2180 else if (TYPE_PRECISION (src_type
) == 64)
2181 OP1 (OPCODE_l2f
+ wide_dst
);
2183 OP1 (OPCODE_i2f
+ wide_dst
);
2185 else /* Convert to integral type. */
2187 if (TREE_CODE (src_type
) == REAL_TYPE
)
2188 OP1 (OPCODE_f2i
+ wide_dst
+ 3 * wide_src
);
2193 if (TYPE_PRECISION (dst_type
) < 32)
2196 /* Already converted to int, if needed. */
2197 if (TYPE_PRECISION (dst_type
) <= 8)
2199 else if (TREE_UNSIGNED (dst_type
))
2205 NOTE_PUSH (1 + wide_dst
);
2210 case CLEANUP_POINT_EXPR
:
2212 struct jcf_block
*save_labeled_blocks
= state
->labeled_blocks
;
2213 int can_complete
= CAN_COMPLETE_NORMALLY (TREE_OPERAND (exp
, 0));
2214 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
, state
);
2215 if (target
!= IGNORE_TARGET
)
2217 while (state
->labeled_blocks
!= save_labeled_blocks
)
2219 struct jcf_block
*finished_label
= NULL
;
2221 tree exception_type
= build_pointer_type (throwable_type_node
);
2222 tree exception_decl
= build_decl (VAR_DECL
, NULL_TREE
,
2224 struct jcf_block
*end_label
= get_jcf_label_here (state
);
2225 struct jcf_block
*label
= state
->labeled_blocks
;
2226 struct jcf_handler
*handler
;
2227 tree cleanup
= label
->u
.labeled_block
;
2228 state
->labeled_blocks
= label
->next
;
2229 state
->num_finalizers
--;
2232 finished_label
= gen_jcf_label (state
);
2233 emit_jsr (label
, state
);
2234 emit_goto (finished_label
, state
);
2235 if (! CAN_COMPLETE_NORMALLY (cleanup
))
2238 handler
= alloc_handler (label
->v
.start_label
, end_label
, state
);
2239 handler
->type
= NULL_TREE
;
2240 localvar_alloc (exception_decl
, state
);
2242 emit_store (exception_decl
, state
);
2243 emit_jsr (label
, state
);
2244 emit_load (exception_decl
, state
);
2246 OP1 (OPCODE_athrow
);
2249 /* The finally block. */
2250 return_link
= build_decl (VAR_DECL
, NULL_TREE
,
2251 return_address_type_node
);
2252 define_jcf_label (label
, state
);
2254 localvar_alloc (return_link
, state
);
2255 emit_store (return_link
, state
);
2256 generate_bytecode_insns (cleanup
, IGNORE_TARGET
, state
);
2257 maybe_wide (OPCODE_ret
, DECL_LOCAL_INDEX (return_link
), state
);
2258 localvar_free (return_link
, state
);
2259 localvar_free (exception_decl
, state
);
2260 if (finished_label
!= NULL
)
2261 define_jcf_label (finished_label
, state
);
2266 case WITH_CLEANUP_EXPR
:
2268 struct jcf_block
*label
;
2269 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
, state
);
2270 label
= gen_jcf_label (state
);
2271 label
->pc
= PENDING_CLEANUP_PC
;
2272 label
->next
= state
->labeled_blocks
;
2273 state
->labeled_blocks
= label
;
2274 state
->num_finalizers
++;
2275 label
->u
.labeled_block
= TREE_OPERAND (exp
, 2);
2276 label
->v
.start_label
= get_jcf_label_here (state
);
2277 if (target
!= IGNORE_TARGET
)
2284 tree try_clause
= TREE_OPERAND (exp
, 0);
2285 struct jcf_block
*start_label
= get_jcf_label_here (state
);
2286 struct jcf_block
*end_label
; /* End of try clause. */
2287 struct jcf_block
*finished_label
= gen_jcf_label (state
);
2288 tree clause
= TREE_OPERAND (exp
, 1);
2289 if (target
!= IGNORE_TARGET
)
2291 generate_bytecode_insns (try_clause
, IGNORE_TARGET
, state
);
2292 end_label
= get_jcf_label_here (state
);
2293 if (CAN_COMPLETE_NORMALLY (try_clause
))
2294 emit_goto (finished_label
, state
);
2295 while (clause
!= NULL_TREE
)
2297 tree catch_clause
= TREE_OPERAND (clause
, 0);
2298 tree exception_decl
= BLOCK_EXPR_DECLS (catch_clause
);
2299 struct jcf_handler
*handler
= alloc_handler (start_label
, end_label
, state
);
2300 if (exception_decl
== NULL_TREE
)
2301 handler
->type
= NULL_TREE
;
2303 handler
->type
= TREE_TYPE (TREE_TYPE (exception_decl
));
2304 generate_bytecode_insns (catch_clause
, IGNORE_TARGET
, state
);
2305 clause
= TREE_CHAIN (clause
);
2306 if (CAN_COMPLETE_NORMALLY (catch_clause
) && clause
!= NULL_TREE
)
2307 emit_goto (finished_label
, state
);
2309 define_jcf_label (finished_label
, state
);
2312 case TRY_FINALLY_EXPR
:
2314 struct jcf_block
*finished_label
, *finally_label
, *start_label
;
2315 struct jcf_handler
*handler
;
2316 int worthwhile_finally
= 1;
2317 tree try_block
= TREE_OPERAND (exp
, 0);
2318 tree finally
= TREE_OPERAND (exp
, 1);
2319 tree return_link
, exception_decl
;
2321 finally_label
= start_label
= NULL
;
2322 return_link
= exception_decl
= NULL_TREE
;
2323 finished_label
= gen_jcf_label (state
);
2325 /* If the finally clause happens to be empty, set a flag so we
2326 remember to just skip it. */
2327 if (BLOCK_EXPR_BODY (finally
) == empty_stmt_node
)
2328 worthwhile_finally
= 0;
2330 if (worthwhile_finally
)
2332 tree exception_type
;
2333 return_link
= build_decl (VAR_DECL
, NULL_TREE
,
2334 return_address_type_node
);
2335 exception_type
= build_pointer_type (throwable_type_node
);
2336 exception_decl
= build_decl (VAR_DECL
, NULL_TREE
, exception_type
);
2338 finally_label
= gen_jcf_label (state
);
2339 start_label
= get_jcf_label_here (state
);
2340 finally_label
->pc
= PENDING_CLEANUP_PC
;
2341 finally_label
->next
= state
->labeled_blocks
;
2342 state
->labeled_blocks
= finally_label
;
2343 state
->num_finalizers
++;
2346 generate_bytecode_insns (try_block
, target
, state
);
2348 if (worthwhile_finally
)
2350 if (state
->labeled_blocks
!= finally_label
)
2352 state
->labeled_blocks
= finally_label
->next
;
2353 emit_jsr (finally_label
, state
);
2356 if (CAN_COMPLETE_NORMALLY (try_block
)
2357 && BLOCK_EXPR_BODY (try_block
) != empty_stmt_node
)
2358 emit_goto (finished_label
, state
);
2360 /* Handle exceptions. */
2362 if (!worthwhile_finally
)
2365 localvar_alloc (return_link
, state
);
2366 handler
= alloc_handler (start_label
, NULL_PTR
, state
);
2367 handler
->end_label
= handler
->handler_label
;
2368 handler
->type
= NULL_TREE
;
2369 localvar_alloc (exception_decl
, state
);
2371 emit_store (exception_decl
, state
);
2372 emit_jsr (finally_label
, state
);
2373 emit_load (exception_decl
, state
);
2375 OP1 (OPCODE_athrow
);
2377 localvar_free (exception_decl
, state
);
2379 /* The finally block. First save return PC into return_link. */
2380 define_jcf_label (finally_label
, state
);
2382 emit_store (return_link
, state
);
2384 generate_bytecode_insns (finally
, IGNORE_TARGET
, state
);
2385 maybe_wide (OPCODE_ret
, DECL_LOCAL_INDEX (return_link
), state
);
2386 localvar_free (return_link
, state
);
2387 define_jcf_label (finished_label
, state
);
2391 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
2393 OP1 (OPCODE_athrow
);
2395 case NEW_ARRAY_INIT
:
2397 tree values
= CONSTRUCTOR_ELTS (TREE_OPERAND (exp
, 0));
2398 tree array_type
= TREE_TYPE (TREE_TYPE (exp
));
2399 tree element_type
= TYPE_ARRAY_ELEMENT (array_type
);
2400 HOST_WIDE_INT length
= java_array_type_length (array_type
);
2401 if (target
== IGNORE_TARGET
)
2403 for ( ; values
!= NULL_TREE
; values
= TREE_CHAIN (values
))
2404 generate_bytecode_insns (TREE_VALUE (values
), target
, state
);
2407 push_int_const (length
, state
);
2410 if (JPRIMITIVE_TYPE_P (element_type
))
2412 int atype
= encode_newarray_type (element_type
);
2413 OP1 (OPCODE_newarray
);
2418 int index
= find_class_constant (&state
->cpool
,
2419 TREE_TYPE (element_type
));
2420 OP1 (OPCODE_anewarray
);
2424 jopcode
= OPCODE_iastore
+ adjust_typed_op (element_type
, 7);
2425 for ( ; values
!= NULL_TREE
; values
= TREE_CHAIN (values
), offset
++)
2427 int save_SP
= state
->code_SP
;
2428 emit_dup (1, 0, state
);
2429 push_int_const (offset
, state
);
2431 generate_bytecode_insns (TREE_VALUE (values
), STACK_TARGET
, state
);
2434 state
->code_SP
= save_SP
;
2438 case NEW_CLASS_EXPR
:
2440 tree
class = TREE_TYPE (TREE_TYPE (exp
));
2441 int need_result
= target
!= IGNORE_TARGET
;
2442 int index
= find_class_constant (&state
->cpool
, class);
2448 NOTE_PUSH (1 + need_result
);
2450 /* ... fall though ... */
2453 tree f
= TREE_OPERAND (exp
, 0);
2454 tree x
= TREE_OPERAND (exp
, 1);
2455 int save_SP
= state
->code_SP
;
2457 if (TREE_CODE (f
) == ADDR_EXPR
)
2458 f
= TREE_OPERAND (f
, 0);
2459 if (f
== soft_newarray_node
)
2461 int type_code
= TREE_INT_CST_LOW (TREE_VALUE (x
));
2462 generate_bytecode_insns (TREE_VALUE (TREE_CHAIN (x
)),
2463 STACK_TARGET
, state
);
2465 OP1 (OPCODE_newarray
);
2469 else if (f
== soft_multianewarray_node
)
2473 int index
= find_class_constant (&state
->cpool
,
2474 TREE_TYPE (TREE_TYPE (exp
)));
2475 x
= TREE_CHAIN (x
); /* Skip class argument. */
2476 ndims
= TREE_INT_CST_LOW (TREE_VALUE (x
));
2477 for (idim
= ndims
; --idim
>= 0; )
2480 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2483 OP1 (OPCODE_multianewarray
);
2488 else if (f
== soft_anewarray_node
)
2490 tree cl
= TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (exp
)));
2491 int index
= find_class_constant (&state
->cpool
, TREE_TYPE (cl
));
2492 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2494 OP1 (OPCODE_anewarray
);
2498 else if (f
== soft_monitorenter_node
2499 || f
== soft_monitorexit_node
2500 || f
== throw_node
[0]
2501 || f
== throw_node
[1])
2503 if (f
== soft_monitorenter_node
)
2504 op
= OPCODE_monitorenter
;
2505 else if (f
== soft_monitorexit_node
)
2506 op
= OPCODE_monitorexit
;
2509 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2515 else if (exp
== soft_exceptioninfo_call_node
)
2517 NOTE_PUSH (1); /* Pushed by exception system. */
2520 for ( ; x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
2522 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2524 nargs
= state
->code_SP
- save_SP
;
2525 state
->code_SP
= save_SP
;
2526 if (f
== soft_fmod_node
)
2533 if (TREE_CODE (exp
) == NEW_CLASS_EXPR
)
2534 NOTE_POP (1); /* Pop implicit this. */
2535 if (TREE_CODE (f
) == FUNCTION_DECL
&& DECL_CONTEXT (f
) != NULL_TREE
)
2538 int index
, interface
= 0;
2540 if (METHOD_STATIC (f
))
2541 OP1 (OPCODE_invokestatic
);
2542 else if (DECL_CONSTRUCTOR_P (f
) || CALL_USING_SUPER (exp
)
2543 || METHOD_PRIVATE (f
))
2544 OP1 (OPCODE_invokespecial
);
2545 else if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (f
))))
2547 OP1 (OPCODE_invokeinterface
);
2551 OP1 (OPCODE_invokevirtual
);
2554 saved_context
= DECL_CONTEXT (f
);
2556 TREE_TYPE (TREE_TYPE (TREE_VALUE (TREE_OPERAND (exp
, 1))));
2558 index
= find_methodref_index (&state
->cpool
, f
);
2560 DECL_CONTEXT (f
) = saved_context
;
2562 f
= TREE_TYPE (TREE_TYPE (f
));
2563 if (TREE_CODE (f
) != VOID_TYPE
)
2565 int size
= TYPE_IS_WIDE (f
) ? 2 : 1;
2566 if (target
== IGNORE_TARGET
)
2567 emit_pop (size
, state
);
2582 error("internal error in generate_bytecode_insn - tree code not implemented: %s",
2583 tree_code_name
[(int) TREE_CODE (exp
)]);
2588 perform_relocations (state
)
2589 struct jcf_partial
*state
;
2591 struct jcf_block
*block
;
2592 struct jcf_relocation
*reloc
;
2596 /* Before we start, the pc field of each block is an upper bound on
2597 the block's start pc (it may be less, if previous blocks need less
2598 than their maximum).
2600 The minimum size of each block is in the block's chunk->size. */
2602 /* First, figure out the actual locations of each block. */
2605 for (block
= state
->blocks
; block
!= NULL
; block
= block
->next
)
2607 int block_size
= block
->v
.chunk
->size
;
2611 /* Optimize GOTO L; L: by getting rid of the redundant goto.
2612 Assumes relocations are in reverse order. */
2613 reloc
= block
->u
.relocations
;
2614 while (reloc
!= NULL
2615 && reloc
->kind
== OPCODE_goto_w
2616 && reloc
->label
->pc
== block
->next
->pc
2617 && reloc
->offset
+ 2 == block_size
)
2619 reloc
= reloc
->next
;
2620 block
->u
.relocations
= reloc
;
2621 block
->v
.chunk
->size
-= 3;
2626 for (reloc
= block
->u
.relocations
; reloc
!= NULL
; reloc
= reloc
->next
)
2628 if (reloc
->kind
== SWITCH_ALIGN_RELOC
)
2630 /* We assume this is the first relocation in this block,
2631 so we know its final pc. */
2632 int where
= pc
+ reloc
->offset
;
2633 int pad
= ((where
+ 3) & ~3) - where
;
2636 else if (reloc
->kind
< -1 || reloc
->kind
> BLOCK_START_RELOC
)
2638 int delta
= reloc
->label
->pc
- (pc
+ reloc
->offset
- 1);
2639 int expand
= reloc
->kind
> 0 ? 2 : 5;
2643 if (delta
>= -32768 && delta
<= 32767)
2649 block_size
+= expand
;
2655 for (block
= state
->blocks
; block
!= NULL
; block
= block
->next
)
2657 struct chunk
*chunk
= block
->v
.chunk
;
2658 int old_size
= chunk
->size
;
2659 int next_pc
= block
->next
== NULL
? pc
: block
->next
->pc
;
2660 int new_size
= next_pc
- block
->pc
;
2661 unsigned char *new_ptr
;
2662 unsigned char *old_buffer
= chunk
->data
;
2663 unsigned char *old_ptr
= old_buffer
+ old_size
;
2664 if (new_size
!= old_size
)
2666 chunk
->data
= (unsigned char *)
2667 obstack_alloc (state
->chunk_obstack
, new_size
);
2668 chunk
->size
= new_size
;
2670 new_ptr
= chunk
->data
+ new_size
;
2672 /* We do the relocations from back to front, because
2673 the relocations are in reverse order. */
2674 for (reloc
= block
->u
.relocations
; ; reloc
= reloc
->next
)
2676 /* new_ptr and old_ptr point into the old and new buffers,
2677 respectively. (If no relocations cause the buffer to
2678 grow, the buffer will be the same buffer, and new_ptr==old_ptr.)
2679 The bytes at higher adress have been copied and relocations
2680 handled; those at lower addresses remain to process. */
2682 /* Lower old index of piece to be copied with no relocation.
2683 I.e. high index of the first piece that does need relocation. */
2684 int start
= reloc
== NULL
? 0
2685 : reloc
->kind
== SWITCH_ALIGN_RELOC
? reloc
->offset
2686 : (reloc
->kind
== 0 || reloc
->kind
== BLOCK_START_RELOC
)
2688 : reloc
->offset
+ 2;
2691 int n
= (old_ptr
- old_buffer
) - start
;
2695 memcpy (new_ptr
, old_ptr
, n
);
2696 if (old_ptr
== old_buffer
)
2699 new_offset
= new_ptr
- chunk
->data
;
2700 new_offset
-= (reloc
->kind
== -1 ? 2 : 4);
2701 if (reloc
->kind
== 0)
2704 value
= GET_u4 (old_ptr
);
2706 else if (reloc
->kind
== BLOCK_START_RELOC
)
2712 else if (reloc
->kind
== SWITCH_ALIGN_RELOC
)
2714 int where
= block
->pc
+ reloc
->offset
;
2715 int pad
= ((where
+ 3) & ~3) - where
;
2723 value
= GET_u2 (old_ptr
);
2725 value
+= reloc
->label
->pc
- (block
->pc
+ new_offset
);
2726 *--new_ptr
= (unsigned char) value
; value
>>= 8;
2727 *--new_ptr
= (unsigned char) value
; value
>>= 8;
2728 if (reloc
->kind
!= -1)
2730 *--new_ptr
= (unsigned char) value
; value
>>= 8;
2731 *--new_ptr
= (unsigned char) value
;
2733 if (reloc
->kind
> BLOCK_START_RELOC
)
2735 /* Convert: OP TARGET to: OP_w TARGET; (OP is goto or jsr). */
2737 *--new_ptr
= reloc
->kind
;
2739 else if (reloc
->kind
< -1)
2741 /* Convert: ifCOND TARGET to: ifNCOND T; goto_w TARGET; T: */
2743 *--new_ptr
= OPCODE_goto_w
;
2746 *--new_ptr
= - reloc
->kind
;
2749 if (new_ptr
!= chunk
->data
)
2750 fatal ("internal error - perform_relocations");
2752 state
->code_length
= pc
;
2756 init_jcf_state (state
, work
)
2757 struct jcf_partial
*state
;
2758 struct obstack
*work
;
2760 state
->chunk_obstack
= work
;
2761 state
->first
= state
->chunk
= NULL
;
2762 CPOOL_INIT (&state
->cpool
);
2763 BUFFER_INIT (&state
->localvars
);
2764 BUFFER_INIT (&state
->bytecode
);
2768 init_jcf_method (state
, method
)
2769 struct jcf_partial
*state
;
2772 state
->current_method
= method
;
2773 state
->blocks
= state
->last_block
= NULL
;
2774 state
->linenumber_count
= 0;
2775 state
->first_lvar
= state
->last_lvar
= NULL
;
2776 state
->lvar_count
= 0;
2777 state
->labeled_blocks
= NULL
;
2778 state
->code_length
= 0;
2779 BUFFER_RESET (&state
->bytecode
);
2780 BUFFER_RESET (&state
->localvars
);
2782 state
->code_SP_max
= 0;
2783 state
->handlers
= NULL
;
2784 state
->last_handler
= NULL
;
2785 state
->num_handlers
= 0;
2786 state
->num_finalizers
= 0;
2787 state
->return_value_decl
= NULL_TREE
;
2791 release_jcf_state (state
)
2792 struct jcf_partial
*state
;
2794 CPOOL_FINISH (&state
->cpool
);
2795 obstack_free (state
->chunk_obstack
, state
->first
);
2798 /* Generate and return a list of chunks containing the class CLAS
2799 in the .class file representation. The list can be written to a
2800 .class file using write_chunks. Allocate chunks from obstack WORK. */
2802 static struct chunk
*
2803 generate_classfile (clas
, state
)
2805 struct jcf_partial
*state
;
2807 struct chunk
*cpool_chunk
;
2808 const char *source_file
, *s
;
2811 char *fields_count_ptr
;
2812 int fields_count
= 0;
2813 char *methods_count_ptr
;
2814 int methods_count
= 0;
2815 static tree SourceFile_node
= NULL_TREE
;
2818 = clas
== object_type_node
? 0
2819 : TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (clas
));
2821 ptr
= append_chunk (NULL
, 8, state
);
2822 PUT4 (0xCafeBabe); /* Magic number */
2823 PUT2 (3); /* Minor version */
2824 PUT2 (45); /* Major version */
2826 append_chunk (NULL
, 0, state
);
2827 cpool_chunk
= state
->chunk
;
2829 /* Next allocate the chunk containing acces_flags through fields_counr. */
2830 if (clas
== object_type_node
)
2833 i
= 8 + 2 * total_supers
;
2834 ptr
= append_chunk (NULL
, i
, state
);
2835 i
= get_access_flags (TYPE_NAME (clas
));
2836 if (! (i
& ACC_INTERFACE
))
2838 PUT2 (i
); /* acces_flags */
2839 i
= find_class_constant (&state
->cpool
, clas
); PUT2 (i
); /* this_class */
2840 if (clas
== object_type_node
)
2842 PUT2(0); /* super_class */
2843 PUT2(0); /* interfaces_count */
2847 tree basetypes
= TYPE_BINFO_BASETYPES (clas
);
2848 tree base
= BINFO_TYPE (TREE_VEC_ELT (basetypes
, 0));
2849 int j
= find_class_constant (&state
->cpool
, base
);
2850 PUT2 (j
); /* super_class */
2851 PUT2 (total_supers
- 1); /* interfaces_count */
2852 for (i
= 1; i
< total_supers
; i
++)
2854 base
= BINFO_TYPE (TREE_VEC_ELT (basetypes
, i
));
2855 j
= find_class_constant (&state
->cpool
, base
);
2859 fields_count_ptr
= ptr
;
2861 for (part
= TYPE_FIELDS (clas
); part
; part
= TREE_CHAIN (part
))
2863 int have_value
, attr_count
= 0;
2864 if (DECL_NAME (part
) == NULL_TREE
|| DECL_ARTIFICIAL (part
))
2866 ptr
= append_chunk (NULL
, 8, state
);
2867 i
= get_access_flags (part
); PUT2 (i
);
2868 i
= find_utf8_constant (&state
->cpool
, DECL_NAME (part
)); PUT2 (i
);
2869 i
= find_utf8_constant (&state
->cpool
,
2870 build_java_signature (TREE_TYPE (part
)));
2872 have_value
= DECL_INITIAL (part
) != NULL_TREE
&& FIELD_STATIC (part
)
2873 && TREE_CODE (TREE_TYPE (part
)) != POINTER_TYPE
;
2877 if (FIELD_THISN (part
) || FIELD_LOCAL_ALIAS (part
))
2880 PUT2 (attr_count
); /* attributes_count */
2883 tree init
= DECL_INITIAL (part
);
2884 static tree ConstantValue_node
= NULL_TREE
;
2885 ptr
= append_chunk (NULL
, 8, state
);
2886 if (ConstantValue_node
== NULL_TREE
)
2887 ConstantValue_node
= get_identifier ("ConstantValue");
2888 i
= find_utf8_constant (&state
->cpool
, ConstantValue_node
);
2889 PUT2 (i
); /* attribute_name_index */
2890 PUT4 (2); /* attribute_length */
2891 i
= find_constant_index (init
, state
); PUT2 (i
);
2893 /* Emit the "Synthetic" attribute for val$<x> and this$<n> fields. */
2894 if (FIELD_THISN (part
) || FIELD_LOCAL_ALIAS (part
))
2895 ptr
= append_synthetic_attribute (state
);
2898 ptr
= fields_count_ptr
; UNSAFE_PUT2 (fields_count
);
2900 ptr
= methods_count_ptr
= append_chunk (NULL
, 2, state
);
2903 for (part
= TYPE_METHODS (clas
); part
; part
= TREE_CHAIN (part
))
2905 struct jcf_block
*block
;
2906 tree function_body
= DECL_FUNCTION_BODY (part
);
2907 tree body
= function_body
== NULL_TREE
? NULL_TREE
2908 : BLOCK_EXPR_BODY (function_body
);
2909 tree name
= DECL_CONSTRUCTOR_P (part
) ? init_identifier_node
2911 tree type
= TREE_TYPE (part
);
2912 tree save_function
= current_function_decl
;
2913 int synthetic_p
= 0;
2914 current_function_decl
= part
;
2915 ptr
= append_chunk (NULL
, 8, state
);
2916 i
= get_access_flags (part
); PUT2 (i
);
2917 i
= find_utf8_constant (&state
->cpool
, name
); PUT2 (i
);
2918 i
= find_utf8_constant (&state
->cpool
, build_java_signature (type
));
2920 i
= (body
!= NULL_TREE
) + (DECL_FUNCTION_THROWS (part
) != NULL_TREE
);
2922 /* Make room for the Synthetic attribute (of zero length.) */
2923 if (DECL_FINIT_P (part
)
2924 || OUTER_FIELD_ACCESS_IDENTIFIER_P (DECL_NAME (part
))
2925 || TYPE_DOT_CLASS (clas
) == part
)
2931 PUT2 (i
); /* attributes_count */
2934 ptr
= append_synthetic_attribute (state
);
2936 if (body
!= NULL_TREE
)
2938 int code_attributes_count
= 0;
2939 static tree Code_node
= NULL_TREE
;
2942 struct jcf_handler
*handler
;
2943 if (Code_node
== NULL_TREE
)
2944 Code_node
= get_identifier ("Code");
2945 ptr
= append_chunk (NULL
, 14, state
);
2946 i
= find_utf8_constant (&state
->cpool
, Code_node
); PUT2 (i
);
2948 init_jcf_method (state
, part
);
2949 get_jcf_label_here (state
); /* Force a first block. */
2950 for (t
= DECL_ARGUMENTS (part
); t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2951 localvar_alloc (t
, state
);
2952 generate_bytecode_insns (body
, IGNORE_TARGET
, state
);
2953 if (CAN_COMPLETE_NORMALLY (body
))
2955 if (TREE_CODE (TREE_TYPE (type
)) != VOID_TYPE
)
2958 OP1 (OPCODE_return
);
2960 for (t
= DECL_ARGUMENTS (part
); t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2961 localvar_free (t
, state
);
2962 if (state
->return_value_decl
!= NULL_TREE
)
2963 localvar_free (state
->return_value_decl
, state
);
2964 finish_jcf_block (state
);
2965 perform_relocations (state
);
2968 i
= 8 + state
->code_length
+ 4 + 8 * state
->num_handlers
;
2969 if (state
->linenumber_count
> 0)
2971 code_attributes_count
++;
2972 i
+= 8 + 4 * state
->linenumber_count
;
2974 if (state
->lvar_count
> 0)
2976 code_attributes_count
++;
2977 i
+= 8 + 10 * state
->lvar_count
;
2979 UNSAFE_PUT4 (i
); /* attribute_length */
2980 UNSAFE_PUT2 (state
->code_SP_max
); /* max_stack */
2981 UNSAFE_PUT2 (localvar_max
); /* max_locals */
2982 UNSAFE_PUT4 (state
->code_length
);
2984 /* Emit the exception table. */
2985 ptr
= append_chunk (NULL
, 2 + 8 * state
->num_handlers
, state
);
2986 PUT2 (state
->num_handlers
); /* exception_table_length */
2987 handler
= state
->handlers
;
2988 for (; handler
!= NULL
; handler
= handler
->next
)
2991 PUT2 (handler
->start_label
->pc
);
2992 PUT2 (handler
->end_label
->pc
);
2993 PUT2 (handler
->handler_label
->pc
);
2994 if (handler
->type
== NULL_TREE
)
2997 type_index
= find_class_constant (&state
->cpool
,
3002 ptr
= append_chunk (NULL
, 2, state
);
3003 PUT2 (code_attributes_count
);
3005 /* Write the LineNumberTable attribute. */
3006 if (state
->linenumber_count
> 0)
3008 static tree LineNumberTable_node
= NULL_TREE
;
3009 ptr
= append_chunk (NULL
,
3010 8 + 4 * state
->linenumber_count
, state
);
3011 if (LineNumberTable_node
== NULL_TREE
)
3012 LineNumberTable_node
= get_identifier ("LineNumberTable");
3013 i
= find_utf8_constant (&state
->cpool
, LineNumberTable_node
);
3014 PUT2 (i
); /* attribute_name_index */
3015 i
= 2+4*state
->linenumber_count
; PUT4(i
); /* attribute_length */
3016 i
= state
->linenumber_count
; PUT2 (i
);
3017 for (block
= state
->blocks
; block
!= NULL
; block
= block
->next
)
3019 int line
= block
->linenumber
;
3028 /* Write the LocalVariableTable attribute. */
3029 if (state
->lvar_count
> 0)
3031 static tree LocalVariableTable_node
= NULL_TREE
;
3032 struct localvar_info
*lvar
= state
->first_lvar
;
3033 ptr
= append_chunk (NULL
, 8 + 10 * state
->lvar_count
, state
);
3034 if (LocalVariableTable_node
== NULL_TREE
)
3035 LocalVariableTable_node
= get_identifier("LocalVariableTable");
3036 i
= find_utf8_constant (&state
->cpool
, LocalVariableTable_node
);
3037 PUT2 (i
); /* attribute_name_index */
3038 i
= 2 + 10 * state
->lvar_count
; PUT4 (i
); /* attribute_length */
3039 i
= state
->lvar_count
; PUT2 (i
);
3040 for ( ; lvar
!= NULL
; lvar
= lvar
->next
)
3042 tree name
= DECL_NAME (lvar
->decl
);
3043 tree sig
= build_java_signature (TREE_TYPE (lvar
->decl
));
3044 i
= lvar
->start_label
->pc
; PUT2 (i
);
3045 i
= lvar
->end_label
->pc
- i
; PUT2 (i
);
3046 i
= find_utf8_constant (&state
->cpool
, name
); PUT2 (i
);
3047 i
= find_utf8_constant (&state
->cpool
, sig
); PUT2 (i
);
3048 i
= DECL_LOCAL_INDEX (lvar
->decl
); PUT2 (i
);
3052 if (DECL_FUNCTION_THROWS (part
) != NULL_TREE
)
3054 tree t
= DECL_FUNCTION_THROWS (part
);
3055 int throws_count
= list_length (t
);
3056 static tree Exceptions_node
= NULL_TREE
;
3057 if (Exceptions_node
== NULL_TREE
)
3058 Exceptions_node
= get_identifier ("Exceptions");
3059 ptr
= append_chunk (NULL
, 8 + 2 * throws_count
, state
);
3060 i
= find_utf8_constant (&state
->cpool
, Exceptions_node
);
3061 PUT2 (i
); /* attribute_name_index */
3062 i
= 2 + 2 * throws_count
; PUT4(i
); /* attribute_length */
3063 i
= throws_count
; PUT2 (i
);
3064 for (; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
3066 i
= find_class_constant (&state
->cpool
, TREE_VALUE (t
));
3071 current_function_decl
= save_function
;
3073 ptr
= methods_count_ptr
; UNSAFE_PUT2 (methods_count
);
3075 source_file
= DECL_SOURCE_FILE (TYPE_NAME (clas
));
3076 for (s
= source_file
; ; s
++)
3081 if (ch
== '/' || ch
== '\\')
3084 ptr
= append_chunk (NULL
, 10, state
);
3086 i
= ((INNER_CLASS_TYPE_P (clas
)
3087 || DECL_INNER_CLASS_LIST (TYPE_NAME (clas
))) ? 2 : 1);
3088 PUT2 (i
); /* attributes_count */
3090 /* generate the SourceFile attribute. */
3091 if (SourceFile_node
== NULL_TREE
)
3092 SourceFile_node
= get_identifier ("SourceFile");
3093 i
= find_utf8_constant (&state
->cpool
, SourceFile_node
);
3094 PUT2 (i
); /* attribute_name_index */
3096 i
= find_utf8_constant (&state
->cpool
, get_identifier (source_file
));
3098 append_innerclasses_attribute (state
, clas
);
3100 /* New finally generate the contents of the constant pool chunk. */
3101 i
= count_constant_pool_bytes (&state
->cpool
);
3102 ptr
= obstack_alloc (state
->chunk_obstack
, i
);
3103 cpool_chunk
->data
= ptr
;
3104 cpool_chunk
->size
= i
;
3105 write_constant_pool (&state
->cpool
, ptr
, i
);
3106 return state
->first
;
3109 static unsigned char *
3110 append_synthetic_attribute (state
)
3111 struct jcf_partial
*state
;
3113 static tree Synthetic_node
= NULL_TREE
;
3114 unsigned char *ptr
= append_chunk (NULL
, 6, state
);
3117 if (Synthetic_node
== NULL_TREE
)
3118 Synthetic_node
= get_identifier ("Synthetic");
3119 i
= find_utf8_constant (&state
->cpool
, Synthetic_node
);
3120 PUT2 (i
); /* Attribute string index */
3121 PUT4 (0); /* Attribute length */
3127 append_innerclasses_attribute (state
, class)
3128 struct jcf_partial
*state
;
3131 static tree InnerClasses_node
= NULL_TREE
;
3132 tree orig_decl
= TYPE_NAME (class);
3135 unsigned char *ptr
, *length_marker
, *number_marker
;
3137 if (!INNER_CLASS_TYPE_P (class) && !DECL_INNER_CLASS_LIST (orig_decl
))
3140 ptr
= append_chunk (NULL
, 8, state
); /* 2+4+2 */
3142 if (InnerClasses_node
== NULL_TREE
)
3143 InnerClasses_node
= get_identifier ("InnerClasses");
3144 i
= find_utf8_constant (&state
->cpool
, InnerClasses_node
);
3146 length_marker
= ptr
; PUT4 (0); /* length, to be later patched */
3147 number_marker
= ptr
; PUT2 (0); /* number of classes, tblp */
3149 /* Generate the entries: all inner classes visible from the one we
3150 process: itself, up and down. */
3151 while (class && INNER_CLASS_TYPE_P (class))
3155 decl
= TYPE_NAME (class);
3156 n
= IDENTIFIER_POINTER (DECL_NAME (decl
)) +
3157 IDENTIFIER_LENGTH (DECL_NAME (decl
));
3159 while (n
[-1] != '$')
3161 append_innerclasses_attribute_entry (state
, decl
, get_identifier (n
));
3164 class = TREE_TYPE (DECL_CONTEXT (TYPE_NAME (class)));
3168 for (current
= DECL_INNER_CLASS_LIST (decl
);
3169 current
; current
= TREE_CHAIN (current
))
3171 append_innerclasses_attribute_entry (state
, TREE_PURPOSE (current
),
3172 TREE_VALUE (current
));
3176 ptr
= length_marker
; PUT4 (8*length
+2);
3177 ptr
= number_marker
; PUT2 (length
);
3181 append_innerclasses_attribute_entry (state
, decl
, name
)
3182 struct jcf_partial
*state
;
3185 static tree anonymous_name
= NULL_TREE
;
3186 int icii
, ocii
, ini
, icaf
;
3187 unsigned char *ptr
= append_chunk (NULL
, 8, state
);
3189 if (!anonymous_name
)
3190 anonymous_name
= get_identifier ("");
3192 icii
= find_class_constant (&state
->cpool
, TREE_TYPE (decl
));
3193 ocii
= find_class_constant (&state
->cpool
, TREE_TYPE (DECL_CONTEXT (decl
)));
3195 /* The specs are saying that if the class is anonymous,
3196 inner_name_index must be zero. But the implementation makes it
3197 point to an empty string. */
3198 ini
= find_utf8_constant (&state
->cpool
,
3199 (ANONYMOUS_CLASS_P (TREE_TYPE (decl
)) ?
3200 anonymous_name
: name
));
3201 icaf
= get_access_flags (decl
);
3203 PUT2 (icii
); PUT2 (ocii
); PUT2 (ini
); PUT2 (icaf
);
3207 make_class_file_name (clas
)
3210 const char *dname
, *slash
;
3214 cname
= IDENTIFIER_POINTER (identifier_subst (DECL_NAME (TYPE_NAME (clas
)),
3215 "", '.', DIR_SEPARATOR
,
3217 if (jcf_write_base_directory
== NULL
)
3219 /* Make sure we put the class file into the .java file's
3220 directory, and not into some subdirectory thereof. */
3222 dname
= DECL_SOURCE_FILE (TYPE_NAME (clas
));
3223 slash
= strrchr (dname
, DIR_SEPARATOR
);
3229 t
= strrchr (cname
, DIR_SEPARATOR
);
3235 dname
= jcf_write_base_directory
;
3236 slash
= dname
+ strlen (dname
);
3239 r
= xmalloc (slash
- dname
+ strlen (cname
) + 2);
3240 strncpy (r
, dname
, slash
- dname
);
3241 r
[slash
- dname
] = DIR_SEPARATOR
;
3242 strcpy (&r
[slash
- dname
+ 1], cname
);
3244 /* We try to make new directories when we need them. We only do
3245 this for directories which "might not" exist. For instance, we
3246 assume the `-d' directory exists, but we don't assume that any
3247 subdirectory below it exists. It might be worthwhile to keep
3248 track of which directories we've created to avoid gratuitous
3250 dname
= r
+ (slash
- dname
) + 1;
3253 cname
= strchr (dname
, DIR_SEPARATOR
);
3257 if (stat (r
, &sb
) == -1)
3259 /* Try to make it. */
3260 if (mkdir (r
, 0755) == -1)
3262 fatal ("failed to create directory `%s'", r
);
3267 *cname
= DIR_SEPARATOR
;
3268 /* Skip consecutive separators. */
3269 for (dname
= cname
+ 1; *dname
&& *dname
== DIR_SEPARATOR
; ++dname
)
3276 /* Write out the contens of a class (RECORD_TYPE) CLAS, as a .class file.
3277 The output .class file name is make_class_file_name(CLAS). */
3280 write_classfile (clas
)
3283 struct obstack
*work
= &temporary_obstack
;
3284 struct jcf_partial state
[1];
3285 char *class_file_name
= make_class_file_name (clas
);
3286 struct chunk
*chunks
;
3288 if (class_file_name
!= NULL
)
3290 FILE* stream
= fopen (class_file_name
, "wb");
3292 fatal ("failed to open `%s' for writing", class_file_name
);
3293 jcf_dependency_add_target (class_file_name
);
3294 init_jcf_state (state
, work
);
3295 chunks
= generate_classfile (clas
, state
);
3296 write_chunks (stream
, chunks
);
3297 if (fclose (stream
))
3298 fatal ("failed to close after writing `%s'", class_file_name
);
3299 free (class_file_name
);
3301 release_jcf_state (state
);
3305 string concatenation
3306 synchronized statement