1 /* Write out a Java(TM) class file.
2 Copyright (C) 1998, 1999 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
PROTO ((tree
, int, struct jcf_partial
*));
279 static struct chunk
* alloc_chunk
PROTO ((struct chunk
*, unsigned char *,
280 int, struct obstack
*));
281 static unsigned char * append_chunk
PROTO ((unsigned char *, int,
282 struct jcf_partial
*));
283 static void append_chunk_copy
PROTO ((unsigned char *, int,
284 struct jcf_partial
*));
285 static struct jcf_block
* gen_jcf_label
PROTO ((struct jcf_partial
*));
286 static void finish_jcf_block
PROTO ((struct jcf_partial
*));
287 static void define_jcf_label
PROTO ((struct jcf_block
*,
288 struct jcf_partial
*));
289 static struct jcf_block
* get_jcf_label_here
PROTO ((struct jcf_partial
*));
290 static void put_linenumber
PROTO ((int, struct jcf_partial
*));
291 static void localvar_alloc
PROTO ((tree
, struct jcf_partial
*));
292 static void localvar_free
PROTO ((tree
, struct jcf_partial
*));
293 static int get_access_flags
PROTO ((tree
));
294 static void write_chunks
PROTO ((FILE *, struct chunk
*));
295 static int adjust_typed_op
PROTO ((tree
, int));
296 static void generate_bytecode_conditional
PROTO ((tree
, struct jcf_block
*,
297 struct jcf_block
*, int,
298 struct jcf_partial
*));
299 static void generate_bytecode_return
PROTO ((tree
, struct jcf_partial
*));
300 static void perform_relocations
PROTO ((struct jcf_partial
*));
301 static void init_jcf_state
PROTO ((struct jcf_partial
*, struct obstack
*));
302 static void init_jcf_method
PROTO ((struct jcf_partial
*, tree
));
303 static void release_jcf_state
PROTO ((struct jcf_partial
*));
304 static struct chunk
* generate_classfile
PROTO ((tree
, struct jcf_partial
*));
305 static struct jcf_handler
*alloc_handler
PROTO ((struct jcf_block
*,
307 struct jcf_partial
*));
308 static void emit_iinc
PROTO ((tree
, HOST_WIDE_INT
, struct jcf_partial
*));
309 static void emit_reloc
PROTO ((HOST_WIDE_INT
, int, struct jcf_block
*,
310 struct jcf_partial
*));
311 static void push_constant1
PROTO ((HOST_WIDE_INT
, struct jcf_partial
*));
312 static void push_constant2
PROTO ((HOST_WIDE_INT
, struct jcf_partial
*));
313 static void push_int_const
PROTO ((HOST_WIDE_INT
, struct jcf_partial
*));
314 static int find_constant_wide
PROTO ((HOST_WIDE_INT
, HOST_WIDE_INT
,
315 struct jcf_partial
*));
316 static void push_long_const
PROTO ((HOST_WIDE_INT
, HOST_WIDE_INT
,
317 struct jcf_partial
*));
318 static int find_constant_index
PROTO ((tree
, struct jcf_partial
*));
319 static void push_long_const
PROTO ((HOST_WIDE_INT
, HOST_WIDE_INT
,
320 struct jcf_partial
*));
321 static void field_op
PROTO ((tree
, int, struct jcf_partial
*));
322 static void maybe_wide
PROTO ((int, int, struct jcf_partial
*));
323 static void emit_dup
PROTO ((int, int, struct jcf_partial
*));
324 static void emit_pop
PROTO ((int, struct jcf_partial
*));
325 static void emit_iinc
PROTO ((tree
, int, struct jcf_partial
*));
326 static void emit_load_or_store
PROTO ((tree
, int, struct jcf_partial
*));
327 static void emit_load
PROTO ((tree
, struct jcf_partial
*));
328 static void emit_store
PROTO ((tree
, struct jcf_partial
*));
329 static void emit_unop
PROTO ((enum java_opcode
, tree
, struct jcf_partial
*));
330 static void emit_binop
PROTO ((enum java_opcode
, tree
, struct jcf_partial
*));
331 static void emit_reloc
PROTO ((HOST_WIDE_INT
, int, struct jcf_block
*,
332 struct jcf_partial
*));
333 static void emit_switch_reloc
PROTO ((struct jcf_block
*,
334 struct jcf_partial
*));
335 static void emit_case_reloc
PROTO ((struct jcf_relocation
*,
336 struct jcf_partial
*));
337 static void emit_if
PROTO ((struct jcf_block
*, int, int,
338 struct jcf_partial
*));
339 static void emit_goto
PROTO ((struct jcf_block
*, struct jcf_partial
*));
340 static void emit_jsr
PROTO ((struct jcf_block
*, struct jcf_partial
*));
341 static void call_cleanups
PROTO ((struct jcf_block
*, struct jcf_partial
*));
342 static char *make_class_file_name
PROTO ((tree
));
344 /* Utility macros for appending (big-endian) data to a buffer.
345 We assume a local variable 'ptr' points into where we want to
346 write next, and we assume enoygh space has been allocated. */
348 #ifdef ENABLE_CHECKING
350 CHECK_PUT(ptr
, state
, i
)
352 struct jcf_partial
*state
;
355 if (ptr
< state
->chunk
->data
356 || (char*)ptr
+ i
> state
->chunk
->data
+ state
->chunk
->size
)
357 fatal ("internal error - CHECK_PUT failed");
361 #define CHECK_PUT(PTR, STATE, I) ((void)0)
364 #define PUT1(X) (CHECK_PUT(ptr, state, 1), *ptr++ = (X))
365 #define PUT2(X) (PUT1((X) >> 8), PUT1((X) & 0xFF))
366 #define PUT4(X) (PUT2((X) >> 16), PUT2((X) & 0xFFFF))
367 #define PUTN(P, N) (CHECK_PUT(ptr, state, N), memcpy(ptr, P, N), ptr += (N))
369 /* There are some cases below where CHECK_PUT is guaranteed to fail.
370 Use the following macros in those specific cases. */
371 #define UNSAFE_PUT1(X) (*ptr++ = (X))
372 #define UNSAFE_PUT2(X) (UNSAFE_PUT1((X) >> 8), UNSAFE_PUT1((X) & 0xFF))
373 #define UNSAFE_PUT4(X) (UNSAFE_PUT2((X) >> 16), UNSAFE_PUT2((X) & 0xFFFF))
374 #define UNSAFE_PUTN(P, N) (memcpy(ptr, P, N), ptr += (N))
377 /* Allocate a new chunk on obstack WORK, and link it in after LAST.
378 Set the data and size fields to DATA and SIZE, respectively.
379 However, if DATA is NULL and SIZE>0, allocate a buffer as well. */
381 static struct chunk
*
382 alloc_chunk (last
, data
, size
, work
)
386 struct obstack
*work
;
388 struct chunk
*chunk
= (struct chunk
*)
389 obstack_alloc (work
, sizeof(struct chunk
));
391 if (data
== NULL
&& size
> 0)
392 data
= obstack_alloc (work
, size
);
402 #ifdef ENABLE_CHECKING
404 CHECK_OP(struct jcf_partial
*state
)
406 if (state
->bytecode
.ptr
> state
->bytecode
.limit
)
408 fatal("internal error - CHECK_OP failed");
413 #define CHECK_OP(STATE) ((void)0)
416 static unsigned char *
417 append_chunk (data
, size
, state
)
420 struct jcf_partial
*state
;
422 state
->chunk
= alloc_chunk (state
->chunk
, data
, size
, state
->chunk_obstack
);
423 if (state
->first
== NULL
)
424 state
->first
= state
->chunk
;
425 return state
->chunk
->data
;
429 append_chunk_copy (data
, size
, state
)
432 struct jcf_partial
*state
;
434 unsigned char *ptr
= append_chunk (NULL
, size
, state
);
435 memcpy (ptr
, data
, size
);
438 static struct jcf_block
*
439 gen_jcf_label (state
)
440 struct jcf_partial
*state
;
442 struct jcf_block
*block
= (struct jcf_block
*)
443 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_block
));
445 block
->linenumber
= -1;
446 block
->pc
= UNDEFINED_PC
;
451 finish_jcf_block (state
)
452 struct jcf_partial
*state
;
454 struct jcf_block
*block
= state
->last_block
;
455 struct jcf_relocation
*reloc
;
456 int code_length
= BUFFER_LENGTH (&state
->bytecode
);
457 int pc
= state
->code_length
;
458 append_chunk_copy (state
->bytecode
.data
, code_length
, state
);
459 BUFFER_RESET (&state
->bytecode
);
460 block
->v
.chunk
= state
->chunk
;
462 /* Calculate code_length to the maximum value it can have. */
463 pc
+= block
->v
.chunk
->size
;
464 for (reloc
= block
->u
.relocations
; reloc
!= NULL
; reloc
= reloc
->next
)
466 int kind
= reloc
->kind
;
467 if (kind
== SWITCH_ALIGN_RELOC
)
469 else if (kind
> BLOCK_START_RELOC
)
470 pc
+= 2; /* 2-byte offset may grow to 4-byte offset */
472 pc
+= 5; /* May need to add a goto_w. */
474 state
->code_length
= pc
;
478 define_jcf_label (label
, state
)
479 struct jcf_block
*label
;
480 struct jcf_partial
*state
;
482 if (state
->last_block
!= NULL
)
483 finish_jcf_block (state
);
484 label
->pc
= state
->code_length
;
485 if (state
->blocks
== NULL
)
486 state
->blocks
= label
;
488 state
->last_block
->next
= label
;
489 state
->last_block
= label
;
491 label
->u
.relocations
= NULL
;
494 static struct jcf_block
*
495 get_jcf_label_here (state
)
496 struct jcf_partial
*state
;
498 if (state
->last_block
!= NULL
&& BUFFER_LENGTH (&state
->bytecode
) == 0)
499 return state
->last_block
;
502 struct jcf_block
*label
= gen_jcf_label (state
);
503 define_jcf_label (label
, state
);
508 /* Note a line number entry for the current PC and given LINE. */
511 put_linenumber (line
, state
)
513 struct jcf_partial
*state
;
515 struct jcf_block
*label
= get_jcf_label_here (state
);
516 if (label
->linenumber
> 0)
518 label
= gen_jcf_label (state
);
519 define_jcf_label (label
, state
);
521 label
->linenumber
= line
;
522 state
->linenumber_count
++;
525 /* Allocate a new jcf_handler, for a catch clause that catches exceptions
526 in the range (START_LABEL, END_LABEL). */
528 static struct jcf_handler
*
529 alloc_handler (start_label
, end_label
, state
)
530 struct jcf_block
*start_label
;
531 struct jcf_block
*end_label
;
532 struct jcf_partial
*state
;
534 struct jcf_handler
*handler
= (struct jcf_handler
*)
535 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_handler
));
536 handler
->start_label
= start_label
;
537 handler
->end_label
= end_label
;
538 handler
->handler_label
= get_jcf_label_here (state
);
539 if (state
->handlers
== NULL
)
540 state
->handlers
= handler
;
542 state
->last_handler
->next
= handler
;
543 state
->last_handler
= handler
;
544 handler
->next
= NULL
;
545 state
->num_handlers
++;
550 /* The index of jvm local variable allocated for this DECL.
551 This is assigned when generating .class files;
552 contrast DECL_LOCAL_SLOT_NUMBER which is set when *reading* a .class file.
553 (We don't allocate DECL_LANG_SPECIFIC for locals from Java sourc code.) */
555 #define DECL_LOCAL_INDEX(DECL) DECL_ALIGN(DECL)
559 struct localvar_info
*next
;
562 struct jcf_block
*start_label
;
563 struct jcf_block
*end_label
;
566 #define localvar_buffer ((struct localvar_info**) state->localvars.data)
567 #define localvar_max \
568 ((struct localvar_info**) state->localvars.ptr - localvar_buffer)
571 localvar_alloc (decl
, state
)
573 struct jcf_partial
*state
;
575 struct jcf_block
*start_label
= get_jcf_label_here (state
);
576 int wide
= TYPE_IS_WIDE (TREE_TYPE (decl
));
578 register struct localvar_info
*info
;
579 register struct localvar_info
**ptr
= localvar_buffer
;
580 register struct localvar_info
**limit
581 = (struct localvar_info
**) state
->localvars
.ptr
;
582 for (index
= 0; ptr
< limit
; index
++, ptr
++)
585 && (! wide
|| ((ptr
+1) < limit
&& ptr
[1] == NULL
)))
590 buffer_grow (&state
->localvars
, 2 * sizeof (struct localvar_info
*));
591 ptr
= (struct localvar_info
**) state
->localvars
.data
+ index
;
592 state
->localvars
.ptr
= (unsigned char *) (ptr
+ 1 + wide
);
594 info
= (struct localvar_info
*)
595 obstack_alloc (state
->chunk_obstack
, sizeof (struct localvar_info
));
598 ptr
[1] = (struct localvar_info
*)(~0);
599 DECL_LOCAL_INDEX (decl
) = index
;
601 info
->start_label
= start_label
;
603 if (debug_info_level
> DINFO_LEVEL_TERSE
604 && DECL_NAME (decl
) != NULL_TREE
)
606 /* Generate debugging info. */
608 if (state
->last_lvar
!= NULL
)
609 state
->last_lvar
->next
= info
;
611 state
->first_lvar
= info
;
612 state
->last_lvar
= info
;
618 localvar_free (decl
, state
)
620 struct jcf_partial
*state
;
622 struct jcf_block
*end_label
= get_jcf_label_here (state
);
623 int index
= DECL_LOCAL_INDEX (decl
);
624 register struct localvar_info
**ptr
= &localvar_buffer
[index
];
625 register struct localvar_info
*info
= *ptr
;
626 int wide
= TYPE_IS_WIDE (TREE_TYPE (decl
));
628 info
->end_label
= end_label
;
630 if (info
->decl
!= decl
)
635 if (ptr
[1] != (struct localvar_info
*)(~0))
642 #define STACK_TARGET 1
643 #define IGNORE_TARGET 2
645 /* Get the access flags of a class (TYPE_DECL), a method (FUNCTION_DECL), or
646 a field (FIELD_DECL or VAR_DECL, if static), as encoded in a .class file. */
649 get_access_flags (decl
)
653 int isfield
= TREE_CODE (decl
) == FIELD_DECL
|| TREE_CODE (decl
) == VAR_DECL
;
654 if (CLASS_PUBLIC (decl
)) /* same as FIELD_PUBLIC and METHOD_PUBLIC */
656 if (CLASS_FINAL (decl
)) /* same as FIELD_FINAL and METHOD_FINAL */
658 if (isfield
|| TREE_CODE (decl
) == FUNCTION_DECL
)
660 if (TREE_PROTECTED (decl
))
661 flags
|= ACC_PROTECTED
;
662 if (TREE_PRIVATE (decl
))
663 flags
|= ACC_PRIVATE
;
665 else if (TREE_CODE (decl
) == TYPE_DECL
)
667 if (CLASS_SUPER (decl
))
669 if (CLASS_ABSTRACT (decl
))
670 flags
|= ACC_ABSTRACT
;
671 if (CLASS_INTERFACE (decl
))
672 flags
|= ACC_INTERFACE
;
675 fatal ("internal error - bad argument to get_access_flags");
676 if (TREE_CODE (decl
) == FUNCTION_DECL
)
678 if (METHOD_NATIVE (decl
))
680 if (METHOD_STATIC (decl
))
682 if (METHOD_SYNCHRONIZED (decl
))
683 flags
|= ACC_SYNCHRONIZED
;
684 if (METHOD_ABSTRACT (decl
))
685 flags
|= ACC_ABSTRACT
;
689 if (FIELD_STATIC (decl
))
691 if (FIELD_VOLATILE (decl
))
692 flags
|= ACC_VOLATILE
;
693 if (FIELD_TRANSIENT (decl
))
694 flags
|= ACC_TRANSIENT
;
699 /* Write the list of segments starting at CHUNKS to STREAM. */
702 write_chunks (stream
, chunks
)
704 struct chunk
*chunks
;
706 for (; chunks
!= NULL
; chunks
= chunks
->next
)
707 fwrite (chunks
->data
, chunks
->size
, 1, stream
);
710 /* Push a 1-word constant in the constant pool at the given INDEX.
711 (Caller is responsible for doing NOTE_PUSH.) */
714 push_constant1 (index
, state
)
716 struct jcf_partial
*state
;
731 /* Push a 2-word constant in the constant pool at the given INDEX.
732 (Caller is responsible for doing NOTE_PUSH.) */
735 push_constant2 (index
, state
)
737 struct jcf_partial
*state
;
744 /* Push 32-bit integer constant on VM stack.
745 Caller is responsible for doing NOTE_PUSH. */
748 push_int_const (i
, state
)
750 struct jcf_partial
*state
;
753 if (i
>= -1 && i
<= 5)
754 OP1(OPCODE_iconst_0
+ i
);
755 else if (i
>= -128 && i
< 128)
760 else if (i
>= -32768 && i
< 32768)
767 i
= find_constant1 (&state
->cpool
, CONSTANT_Integer
,
768 (jword
)(i
& 0xFFFFFFFF));
769 push_constant1 (i
, state
);
774 find_constant_wide (lo
, hi
, state
)
775 HOST_WIDE_INT lo
, hi
;
776 struct jcf_partial
*state
;
778 HOST_WIDE_INT w1
, w2
;
779 lshift_double (lo
, hi
, -32, 64, &w1
, &w2
, 1);
780 return find_constant2 (&state
->cpool
, CONSTANT_Long
,
781 (jword
)(w1
& 0xFFFFFFFF), (jword
)(lo
& 0xFFFFFFFF));
784 /* Find or allocate a constant pool entry for the given VALUE.
785 Return the index in the constant pool. */
788 find_constant_index (value
, state
)
790 struct jcf_partial
*state
;
792 if (TREE_CODE (value
) == INTEGER_CST
)
794 if (TYPE_PRECISION (TREE_TYPE (value
)) <= 32)
795 return find_constant1 (&state
->cpool
, CONSTANT_Integer
,
796 (jword
)(TREE_INT_CST_LOW (value
) & 0xFFFFFFFF));
798 return find_constant_wide (TREE_INT_CST_LOW (value
),
799 TREE_INT_CST_HIGH (value
), state
);
801 else if (TREE_CODE (value
) == REAL_CST
)
804 if (TYPE_PRECISION (TREE_TYPE (value
)) == 32)
806 words
[0] = etarsingle (TREE_REAL_CST (value
)) & 0xFFFFFFFF;
807 return find_constant1 (&state
->cpool
, CONSTANT_Float
,
812 etardouble (TREE_REAL_CST (value
), words
);
813 return find_constant2 (&state
->cpool
, CONSTANT_Double
,
814 (jword
)(words
[1-FLOAT_WORDS_BIG_ENDIAN
] &
816 (jword
)(words
[FLOAT_WORDS_BIG_ENDIAN
] &
820 else if (TREE_CODE (value
) == STRING_CST
)
822 return find_string_constant (&state
->cpool
, value
);
825 fatal ("find_constant_index - bad type");
828 /* Push 64-bit long constant on VM stack.
829 Caller is responsible for doing NOTE_PUSH. */
832 push_long_const (lo
, hi
, state
)
833 HOST_WIDE_INT lo
, hi
;
834 struct jcf_partial
*state
;
836 if (hi
== 0 && lo
>= 0 && lo
<= 1)
839 OP1(OPCODE_lconst_0
+ lo
);
841 else if ((hi
== 0 && lo
< 32768) || (hi
== -1 && lo
>= -32768))
843 push_int_const (lo
, state
);
848 push_constant2 (find_constant_wide (lo
, hi
, state
), state
);
852 field_op (field
, opcode
, state
)
855 struct jcf_partial
*state
;
857 int index
= find_fieldref_index (&state
->cpool
, field
);
863 /* Returns an integer in the range 0 (for 'int') through 4 (for object
864 reference) to 7 (for 'short') which matches the pattern of how JVM
865 opcodes typically depend on the operand type. */
868 adjust_typed_op (type
, max
)
872 switch (TREE_CODE (type
))
875 case RECORD_TYPE
: return 4;
877 return TYPE_PRECISION (type
) == 32 || max
< 5 ? 0 : 5;
879 return TYPE_PRECISION (type
) == 32 || max
< 6 ? 0 : 6;
881 switch (TYPE_PRECISION (type
))
883 case 8: return max
< 5 ? 0 : 5;
884 case 16: return max
< 7 ? 0 : 7;
890 switch (TYPE_PRECISION (type
))
903 maybe_wide (opcode
, index
, state
)
905 struct jcf_partial
*state
;
922 /* Compile code to duplicate with offset, where
923 SIZE is the size of the stack item to duplicate (1 or 2), abd
924 OFFSET is where to insert the result (must be 0, 1, or 2).
925 (The new words get inserted at stack[SP-size-offset].) */
928 emit_dup (size
, offset
, state
)
930 struct jcf_partial
*state
;
937 kind
= size
== 1 ? OPCODE_dup
: OPCODE_dup2
;
938 else if (offset
== 1)
939 kind
= size
== 1 ? OPCODE_dup_x1
: OPCODE_dup2_x1
;
940 else if (offset
== 2)
941 kind
= size
== 1 ? OPCODE_dup_x2
: OPCODE_dup2_x2
;
949 emit_pop (size
, state
)
951 struct jcf_partial
*state
;
954 OP1 (OPCODE_pop
- 1 + size
);
958 emit_iinc (var
, value
, state
)
961 struct jcf_partial
*state
;
963 int slot
= DECL_LOCAL_INDEX (var
);
965 if (value
< -128 || value
> 127 || slot
>= 256)
983 emit_load_or_store (var
, opcode
, state
)
984 tree var
; /* Variable to load from or store into. */
985 int opcode
; /* Either OPCODE_iload or OPCODE_istore. */
986 struct jcf_partial
*state
;
988 tree type
= TREE_TYPE (var
);
989 int kind
= adjust_typed_op (type
, 4);
990 int index
= DECL_LOCAL_INDEX (var
);
994 OP1 (opcode
+ 5 + 4 * kind
+ index
); /* [ilfda]{load,store}_[0123] */
997 maybe_wide (opcode
+ kind
, index
, state
); /* [ilfda]{load,store} */
1001 emit_load (var
, state
)
1003 struct jcf_partial
*state
;
1005 emit_load_or_store (var
, OPCODE_iload
, state
);
1006 NOTE_PUSH (TYPE_IS_WIDE (TREE_TYPE (var
)) ? 2 : 1);
1010 emit_store (var
, state
)
1012 struct jcf_partial
*state
;
1014 emit_load_or_store (var
, OPCODE_istore
, state
);
1015 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (var
)) ? 2 : 1);
1019 emit_unop (opcode
, type
, state
)
1020 enum java_opcode opcode
;
1021 tree type ATTRIBUTE_UNUSED
;
1022 struct jcf_partial
*state
;
1029 emit_binop (opcode
, type
, state
)
1030 enum java_opcode opcode
;
1032 struct jcf_partial
*state
;
1034 int size
= TYPE_IS_WIDE (type
) ? 2 : 1;
1041 emit_reloc (value
, kind
, target
, state
)
1042 HOST_WIDE_INT value
;
1044 struct jcf_block
*target
;
1045 struct jcf_partial
*state
;
1047 struct jcf_relocation
*reloc
= (struct jcf_relocation
*)
1048 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_relocation
));
1049 struct jcf_block
*block
= state
->last_block
;
1050 reloc
->next
= block
->u
.relocations
;
1051 block
->u
.relocations
= reloc
;
1052 reloc
->offset
= BUFFER_LENGTH (&state
->bytecode
);
1053 reloc
->label
= target
;
1055 if (kind
== 0 || kind
== BLOCK_START_RELOC
)
1057 else if (kind
!= SWITCH_ALIGN_RELOC
)
1062 emit_switch_reloc (label
, state
)
1063 struct jcf_block
*label
;
1064 struct jcf_partial
*state
;
1066 emit_reloc (RELOCATION_VALUE_0
, BLOCK_START_RELOC
, label
, state
);
1069 /* Similar to emit_switch_reloc,
1070 but re-uses an existing case reloc. */
1073 emit_case_reloc (reloc
, state
)
1074 struct jcf_relocation
*reloc
;
1075 struct jcf_partial
*state
;
1077 struct jcf_block
*block
= state
->last_block
;
1078 reloc
->next
= block
->u
.relocations
;
1079 block
->u
.relocations
= reloc
;
1080 reloc
->offset
= BUFFER_LENGTH (&state
->bytecode
);
1081 reloc
->kind
= BLOCK_START_RELOC
;
1085 /* Emit a conditional jump to TARGET with a 2-byte relative jump offset
1086 The opcode is OPCODE, the inverted opcode is INV_OPCODE. */
1089 emit_if (target
, opcode
, inv_opcode
, state
)
1090 struct jcf_block
*target
;
1091 int opcode
, inv_opcode
;
1092 struct jcf_partial
*state
;
1095 /* value is 1 byte from reloc back to start of instruction. */
1096 emit_reloc (RELOCATION_VALUE_1
, - inv_opcode
, target
, state
);
1100 emit_goto (target
, state
)
1101 struct jcf_block
*target
;
1102 struct jcf_partial
*state
;
1105 /* Value is 1 byte from reloc back to start of instruction. */
1106 emit_reloc (RELOCATION_VALUE_1
, OPCODE_goto_w
, target
, state
);
1110 emit_jsr (target
, state
)
1111 struct jcf_block
*target
;
1112 struct jcf_partial
*state
;
1115 /* Value is 1 byte from reloc back to start of instruction. */
1116 emit_reloc (RELOCATION_VALUE_1
, OPCODE_jsr_w
, target
, state
);
1119 /* Generate code to evaluate EXP. If the result is true,
1120 branch to TRUE_LABEL; otherwise, branch to FALSE_LABEL.
1121 TRUE_BRANCH_FIRST is a code geneation hint that the
1122 TRUE_LABEL may follow right after this. (The idea is that we
1123 may be able to optimize away GOTO TRUE_LABEL; TRUE_LABEL:) */
1126 generate_bytecode_conditional (exp
, true_label
, false_label
,
1127 true_branch_first
, state
)
1129 struct jcf_block
*true_label
;
1130 struct jcf_block
*false_label
;
1131 int true_branch_first
;
1132 struct jcf_partial
*state
;
1134 tree exp0
, exp1
, type
;
1135 int save_SP
= state
->code_SP
;
1136 enum java_opcode op
, negop
;
1137 switch (TREE_CODE (exp
))
1140 emit_goto (integer_zerop (exp
) ? false_label
: true_label
, state
);
1144 struct jcf_block
*then_label
= gen_jcf_label (state
);
1145 struct jcf_block
*else_label
= gen_jcf_label (state
);
1146 int save_SP_before
, save_SP_after
;
1147 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1148 then_label
, else_label
, 1, state
);
1149 define_jcf_label (then_label
, state
);
1150 save_SP_before
= state
->code_SP
;
1151 generate_bytecode_conditional (TREE_OPERAND (exp
, 1),
1152 true_label
, false_label
, 1, state
);
1153 save_SP_after
= state
->code_SP
;
1154 state
->code_SP
= save_SP_before
;
1155 define_jcf_label (else_label
, state
);
1156 generate_bytecode_conditional (TREE_OPERAND (exp
, 2),
1157 true_label
, false_label
,
1158 true_branch_first
, state
);
1159 if (state
->code_SP
!= save_SP_after
)
1160 fatal ("internal error non-matching SP");
1163 case TRUTH_NOT_EXPR
:
1164 generate_bytecode_conditional (TREE_OPERAND (exp
, 0), false_label
, true_label
,
1165 ! true_branch_first
, state
);
1167 case TRUTH_ANDIF_EXPR
:
1169 struct jcf_block
*next_label
= gen_jcf_label (state
);
1170 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1171 next_label
, false_label
, 1, state
);
1172 define_jcf_label (next_label
, state
);
1173 generate_bytecode_conditional (TREE_OPERAND (exp
, 1),
1174 true_label
, false_label
, 1, state
);
1177 case TRUTH_ORIF_EXPR
:
1179 struct jcf_block
*next_label
= gen_jcf_label (state
);
1180 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1181 true_label
, next_label
, 1, state
);
1182 define_jcf_label (next_label
, state
);
1183 generate_bytecode_conditional (TREE_OPERAND (exp
, 1),
1184 true_label
, false_label
, 1, state
);
1188 /* Assuming op is one of the 2-operand if_icmp<COND> instructions,
1189 set it to the corresponding 1-operand if<COND> instructions. */
1193 /* The opcodes with their inverses are allocated in pairs.
1194 E.g. The inverse of if_icmplt (161) is if_icmpge (162). */
1195 negop
= (op
& 1) ? op
+ 1 : op
- 1;
1197 if (true_branch_first
)
1199 emit_if (false_label
, negop
, op
, state
);
1200 emit_goto (true_label
, state
);
1204 emit_if (true_label
, op
, negop
, state
);
1205 emit_goto (false_label
, state
);
1209 op
= OPCODE_if_icmpeq
;
1212 op
= OPCODE_if_icmpne
;
1215 op
= OPCODE_if_icmpgt
;
1218 op
= OPCODE_if_icmplt
;
1221 op
= OPCODE_if_icmpge
;
1224 op
= OPCODE_if_icmple
;
1227 exp0
= TREE_OPERAND (exp
, 0);
1228 exp1
= TREE_OPERAND (exp
, 1);
1229 type
= TREE_TYPE (exp0
);
1230 switch (TREE_CODE (type
))
1233 case POINTER_TYPE
: case RECORD_TYPE
:
1234 switch (TREE_CODE (exp
))
1236 case EQ_EXPR
: op
= OPCODE_if_acmpeq
; break;
1237 case NE_EXPR
: op
= OPCODE_if_acmpne
; break;
1240 if (integer_zerop (exp1
) || integer_zerop (exp0
))
1242 generate_bytecode_insns (integer_zerop (exp1
) ? exp0
: exp0
,
1243 STACK_TARGET
, state
);
1244 op
= op
+ (OPCODE_ifnull
- OPCODE_if_acmpeq
);
1245 negop
= (op
& 1) ? op
- 1 : op
+ 1;
1249 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1250 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1254 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1255 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1256 if (op
== OPCODE_if_icmplt
|| op
== OPCODE_if_icmple
)
1260 if (TYPE_PRECISION (type
) > 32)
1271 if (TYPE_PRECISION (type
) > 32)
1273 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1274 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1282 if (integer_zerop (exp1
))
1284 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1288 if (integer_zerop (exp0
))
1292 case OPCODE_if_icmplt
:
1293 case OPCODE_if_icmpge
:
1296 case OPCODE_if_icmpgt
:
1297 case OPCODE_if_icmple
:
1303 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1307 generate_bytecode_insns (exp0
, STACK_TARGET
, state
);
1308 generate_bytecode_insns (exp1
, STACK_TARGET
, state
);
1314 generate_bytecode_insns (exp
, STACK_TARGET
, state
);
1316 if (true_branch_first
)
1318 emit_if (false_label
, OPCODE_ifeq
, OPCODE_ifne
, state
);
1319 emit_goto (true_label
, state
);
1323 emit_if (true_label
, OPCODE_ifne
, OPCODE_ifeq
, state
);
1324 emit_goto (false_label
, state
);
1328 if (save_SP
!= state
->code_SP
)
1329 fatal ("internal error - SP mismatch");
1332 /* Call pending cleanups i.e. those for surrounding CLEANUP_POINT_EXPRs
1333 but only as far out as LIMIT (since we are about to jump to the
1334 emit label that is LIMIT). */
1337 call_cleanups (limit
, state
)
1338 struct jcf_block
*limit
;
1339 struct jcf_partial
*state
;
1341 struct jcf_block
*block
= state
->labeled_blocks
;
1342 for (; block
!= limit
; block
= block
->next
)
1344 if (block
->pc
== PENDING_CLEANUP_PC
)
1345 emit_jsr (block
, state
);
1350 generate_bytecode_return (exp
, state
)
1352 struct jcf_partial
*state
;
1354 tree return_type
= TREE_TYPE (TREE_TYPE (state
->current_method
));
1355 int returns_void
= TREE_CODE (return_type
) == VOID_TYPE
;
1360 switch (TREE_CODE (exp
))
1363 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
,
1365 exp
= TREE_OPERAND (exp
, 1);
1369 struct jcf_block
*then_label
= gen_jcf_label (state
);
1370 struct jcf_block
*else_label
= gen_jcf_label (state
);
1371 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1372 then_label
, else_label
, 1, state
);
1373 define_jcf_label (then_label
, state
);
1374 generate_bytecode_return (TREE_OPERAND (exp
, 1), state
);
1375 define_jcf_label (else_label
, state
);
1376 generate_bytecode_return (TREE_OPERAND (exp
, 2), state
);
1380 generate_bytecode_insns (exp
,
1381 returns_void
? IGNORE_TARGET
1382 : STACK_TARGET
, state
);
1388 call_cleanups (NULL_PTR
, state
);
1392 op
= OPCODE_ireturn
+ adjust_typed_op (return_type
, 4);
1393 if (state
->num_finalizers
> 0)
1395 if (state
->return_value_decl
== NULL_TREE
)
1397 state
->return_value_decl
1398 = build_decl (VAR_DECL
, NULL_TREE
, TREE_TYPE (exp
));
1399 localvar_alloc (state
->return_value_decl
, state
);
1401 emit_store (state
->return_value_decl
, state
);
1402 call_cleanups (NULL_PTR
, state
);
1403 emit_load (state
->return_value_decl
, state
);
1404 /* If we call localvar_free (state->return_value_decl, state),
1405 then we risk the save decl erroneously re-used in the
1406 finalizer. Instead, we keep the state->return_value_decl
1407 allocated through the rest of the method. This is not
1408 the greatest solution, but it is at least simple and safe. */
1415 /* Generate bytecode for sub-expression EXP of METHOD.
1416 TARGET is one of STACK_TARGET or IGNORE_TARGET. */
1419 generate_bytecode_insns (exp
, target
, state
)
1422 struct jcf_partial
*state
;
1425 enum java_opcode jopcode
;
1427 HOST_WIDE_INT value
;
1432 if (exp
== NULL
&& target
== IGNORE_TARGET
)
1435 type
= TREE_TYPE (exp
);
1437 switch (TREE_CODE (exp
))
1440 if (BLOCK_EXPR_BODY (exp
))
1443 tree body
= BLOCK_EXPR_BODY (exp
);
1444 for (local
= BLOCK_EXPR_DECLS (exp
); local
; )
1446 tree next
= TREE_CHAIN (local
);
1447 localvar_alloc (local
, state
);
1450 /* Avoid deep recursion for long blocks. */
1451 while (TREE_CODE (body
) == COMPOUND_EXPR
)
1453 generate_bytecode_insns (TREE_OPERAND (body
, 0), target
, state
);
1454 body
= TREE_OPERAND (body
, 1);
1456 generate_bytecode_insns (body
, target
, state
);
1457 for (local
= BLOCK_EXPR_DECLS (exp
); local
; )
1459 tree next
= TREE_CHAIN (local
);
1460 localvar_free (local
, state
);
1466 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
, state
);
1467 generate_bytecode_insns (TREE_OPERAND (exp
, 1), target
, state
);
1469 case EXPR_WITH_FILE_LOCATION
:
1471 char *saved_input_filename
= input_filename
;
1472 tree body
= EXPR_WFL_NODE (exp
);
1473 int saved_lineno
= lineno
;
1474 if (body
== empty_stmt_node
)
1476 input_filename
= EXPR_WFL_FILENAME (exp
);
1477 lineno
= EXPR_WFL_LINENO (exp
);
1478 if (EXPR_WFL_EMIT_LINE_NOTE (exp
) && lineno
> 0
1479 && debug_info_level
> DINFO_LEVEL_NONE
)
1480 put_linenumber (lineno
, state
);
1481 generate_bytecode_insns (body
, target
, state
);
1482 input_filename
= saved_input_filename
;
1483 lineno
= saved_lineno
;
1487 if (target
== IGNORE_TARGET
) ; /* do nothing */
1488 else if (TREE_CODE (type
) == POINTER_TYPE
)
1490 if (! integer_zerop (exp
))
1493 OP1 (OPCODE_aconst_null
);
1496 else if (TYPE_PRECISION (type
) <= 32)
1498 push_int_const (TREE_INT_CST_LOW (exp
), state
);
1503 push_long_const (TREE_INT_CST_LOW (exp
), TREE_INT_CST_HIGH (exp
),
1510 int prec
= TYPE_PRECISION (type
) >> 5;
1512 if (real_zerop (exp
))
1513 OP1 (prec
== 1 ? OPCODE_fconst_0
: OPCODE_dconst_0
);
1514 else if (real_onep (exp
))
1515 OP1 (prec
== 1 ? OPCODE_fconst_1
: OPCODE_dconst_1
);
1516 /* FIXME Should also use fconst_2 for 2.0f.
1517 Also, should use iconst_2/ldc followed by i2f/i2d
1518 for other float/double when the value is a small integer. */
1521 offset
= find_constant_index (exp
, state
);
1523 push_constant1 (offset
, state
);
1525 push_constant2 (offset
, state
);
1531 push_constant1 (find_string_constant (&state
->cpool
, exp
), state
);
1535 if (TREE_STATIC (exp
))
1537 field_op (exp
, OPCODE_getstatic
, state
);
1538 NOTE_PUSH (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 2 : 1);
1541 /* ... fall through ... */
1543 emit_load (exp
, state
);
1545 case NON_LVALUE_EXPR
:
1547 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
1550 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
1551 generate_bytecode_insns (TREE_OPERAND (exp
, 1), target
, state
);
1552 if (target
!= IGNORE_TARGET
)
1554 jopcode
= OPCODE_iaload
+ adjust_typed_op (type
, 7);
1557 if (! TYPE_IS_WIDE (type
))
1563 tree obj
= TREE_OPERAND (exp
, 0);
1564 tree field
= TREE_OPERAND (exp
, 1);
1565 int is_static
= FIELD_STATIC (field
);
1566 generate_bytecode_insns (obj
,
1567 is_static
? IGNORE_TARGET
: target
, state
);
1568 if (target
!= IGNORE_TARGET
)
1570 if (DECL_NAME (field
) == length_identifier_node
&& !is_static
1571 && TYPE_ARRAY_P (TREE_TYPE (obj
)))
1574 OP1 (OPCODE_arraylength
);
1578 field_op (field
, is_static
? OPCODE_getstatic
: OPCODE_getfield
,
1582 NOTE_PUSH (TYPE_IS_WIDE (TREE_TYPE (field
)) ? 2 : 1);
1587 case TRUTH_ANDIF_EXPR
:
1588 case TRUTH_ORIF_EXPR
:
1596 struct jcf_block
*then_label
= gen_jcf_label (state
);
1597 struct jcf_block
*else_label
= gen_jcf_label (state
);
1598 struct jcf_block
*end_label
= gen_jcf_label (state
);
1599 generate_bytecode_conditional (exp
,
1600 then_label
, else_label
, 1, state
);
1601 define_jcf_label (then_label
, state
);
1602 push_int_const (1, state
);
1603 emit_goto (end_label
, state
);
1604 define_jcf_label (else_label
, state
);
1605 push_int_const (0, state
);
1606 define_jcf_label (end_label
, state
);
1612 struct jcf_block
*then_label
= gen_jcf_label (state
);
1613 struct jcf_block
*else_label
= gen_jcf_label (state
);
1614 struct jcf_block
*end_label
= gen_jcf_label (state
);
1615 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1616 then_label
, else_label
, 1, state
);
1617 define_jcf_label (then_label
, state
);
1618 generate_bytecode_insns (TREE_OPERAND (exp
, 1), target
, state
);
1619 if (CAN_COMPLETE_NORMALLY (TREE_OPERAND (exp
, 1))
1620 /* Not all expressions have CAN_COMPLETE_NORMALLY set properly. */
1621 || TREE_CODE (TREE_TYPE (exp
)) != VOID_TYPE
)
1622 emit_goto (end_label
, state
);
1623 define_jcf_label (else_label
, state
);
1624 generate_bytecode_insns (TREE_OPERAND (exp
, 2), target
, state
);
1625 define_jcf_label (end_label
, state
);
1630 struct jcf_switch_state
*sw_state
= state
->sw_state
;
1631 struct jcf_relocation
*reloc
= (struct jcf_relocation
*)
1632 obstack_alloc (state
->chunk_obstack
, sizeof (struct jcf_relocation
));
1633 HOST_WIDE_INT case_value
= TREE_INT_CST_LOW (TREE_OPERAND (exp
, 0));
1635 reloc
->label
= get_jcf_label_here (state
);
1636 reloc
->offset
= case_value
;
1637 reloc
->next
= sw_state
->cases
;
1638 sw_state
->cases
= reloc
;
1639 if (sw_state
->num_cases
== 0)
1641 sw_state
->min_case
= case_value
;
1642 sw_state
->max_case
= case_value
;
1646 if (case_value
< sw_state
->min_case
)
1647 sw_state
->min_case
= case_value
;
1648 if (case_value
> sw_state
->max_case
)
1649 sw_state
->max_case
= case_value
;
1651 sw_state
->num_cases
++;
1655 state
->sw_state
->default_label
= get_jcf_label_here (state
);
1660 /* The SWITCH_EXPR has three parts, generated in the following order:
1661 1. the switch_expression (the value used to select the correct case);
1663 3. the switch_instruction (the tableswitch/loopupswitch instruction.).
1664 After code generation, we will re-order then in the order 1, 3, 2.
1665 This is to avoid an extra GOTOs. */
1666 struct jcf_switch_state sw_state
;
1667 struct jcf_block
*expression_last
; /* Last block of the switch_expression. */
1668 struct jcf_block
*body_last
; /* Last block of the switch_body. */
1669 struct jcf_block
*switch_instruction
; /* First block of switch_instruction. */
1670 struct jcf_block
*instruction_last
; /* Last block of the switch_instruction. */
1671 struct jcf_block
*body_block
;
1673 sw_state
.prev
= state
->sw_state
;
1674 state
->sw_state
= &sw_state
;
1675 sw_state
.cases
= NULL
;
1676 sw_state
.num_cases
= 0;
1677 sw_state
.default_label
= NULL
;
1678 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
1679 expression_last
= state
->last_block
;
1680 body_block
= get_jcf_label_here (state
); /* Force a new block here. */
1681 generate_bytecode_insns (TREE_OPERAND (exp
, 1), IGNORE_TARGET
, state
);
1682 body_last
= state
->last_block
;
1684 switch_instruction
= gen_jcf_label (state
);
1685 define_jcf_label (switch_instruction
, state
);
1686 if (sw_state
.default_label
== NULL
)
1687 sw_state
.default_label
= gen_jcf_label (state
);
1689 if (sw_state
.num_cases
<= 1)
1691 if (sw_state
.num_cases
== 0)
1693 emit_pop (1, state
);
1698 push_int_const (sw_state
.cases
->offset
, state
);
1699 emit_if (sw_state
.cases
->label
,
1700 OPCODE_ifeq
, OPCODE_ifne
, state
);
1702 emit_goto (sw_state
.default_label
, state
);
1707 /* Copy the chain of relocs into a sorted array. */
1708 struct jcf_relocation
**relocs
= (struct jcf_relocation
**)
1709 xmalloc (sw_state
.num_cases
* sizeof (struct jcf_relocation
*));
1710 /* The relocs arrays is a buffer with a gap.
1711 The assumption is that cases will normally come in "runs". */
1713 int gap_end
= sw_state
.num_cases
;
1714 struct jcf_relocation
*reloc
;
1715 for (reloc
= sw_state
.cases
; reloc
!= NULL
; reloc
= reloc
->next
)
1717 HOST_WIDE_INT case_value
= reloc
->offset
;
1718 while (gap_end
< sw_state
.num_cases
)
1720 struct jcf_relocation
*end
= relocs
[gap_end
];
1721 if (case_value
<= end
->offset
)
1723 relocs
[gap_start
++] = end
;
1726 while (gap_start
> 0)
1728 struct jcf_relocation
*before
= relocs
[gap_start
-1];
1729 if (case_value
>= before
->offset
)
1731 relocs
[--gap_end
] = before
;
1734 relocs
[gap_start
++] = reloc
;
1735 /* Note we don't check for duplicates. FIXME! */
1738 if (2 * sw_state
.num_cases
1739 >= sw_state
.max_case
- sw_state
.min_case
)
1740 { /* Use tableswitch. */
1742 RESERVE (13 + 4 * (sw_state
.max_case
- sw_state
.min_case
+ 1));
1743 OP1 (OPCODE_tableswitch
);
1744 emit_reloc (RELOCATION_VALUE_0
,
1745 SWITCH_ALIGN_RELOC
, NULL
, state
);
1746 emit_switch_reloc (sw_state
.default_label
, state
);
1747 OP4 (sw_state
.min_case
);
1748 OP4 (sw_state
.max_case
);
1749 for (i
= sw_state
.min_case
; ; )
1751 reloc
= relocs
[index
];
1752 if (i
== reloc
->offset
)
1754 emit_case_reloc (reloc
, state
);
1755 if (i
== sw_state
.max_case
)
1760 emit_switch_reloc (sw_state
.default_label
, state
);
1765 { /* Use lookupswitch. */
1766 RESERVE(9 + 8 * sw_state
.num_cases
);
1767 OP1 (OPCODE_lookupswitch
);
1768 emit_reloc (RELOCATION_VALUE_0
,
1769 SWITCH_ALIGN_RELOC
, NULL
, state
);
1770 emit_switch_reloc (sw_state
.default_label
, state
);
1771 OP4 (sw_state
.num_cases
);
1772 for (i
= 0; i
< sw_state
.num_cases
; i
++)
1774 struct jcf_relocation
*reloc
= relocs
[i
];
1775 OP4 (reloc
->offset
);
1776 emit_case_reloc (reloc
, state
);
1782 instruction_last
= state
->last_block
;
1783 if (sw_state
.default_label
->pc
< 0)
1784 define_jcf_label (sw_state
.default_label
, state
);
1785 else /* Force a new block. */
1786 sw_state
.default_label
= get_jcf_label_here (state
);
1787 /* Now re-arrange the blocks so the switch_instruction
1788 comes before the switch_body. */
1789 switch_length
= state
->code_length
- switch_instruction
->pc
;
1790 switch_instruction
->pc
= body_block
->pc
;
1791 instruction_last
->next
= body_block
;
1792 instruction_last
->v
.chunk
->next
= body_block
->v
.chunk
;
1793 expression_last
->next
= switch_instruction
;
1794 expression_last
->v
.chunk
->next
= switch_instruction
->v
.chunk
;
1795 body_last
->next
= sw_state
.default_label
;
1796 body_last
->v
.chunk
->next
= NULL
;
1797 state
->chunk
= body_last
->v
.chunk
;
1798 for (; body_block
!= sw_state
.default_label
; body_block
= body_block
->next
)
1799 body_block
->pc
+= switch_length
;
1801 state
->sw_state
= sw_state
.prev
;
1806 exp
= TREE_OPERAND (exp
, 0);
1807 if (exp
== NULL_TREE
)
1808 exp
= empty_stmt_node
;
1809 else if (TREE_CODE (exp
) != MODIFY_EXPR
)
1812 exp
= TREE_OPERAND (exp
, 1);
1813 generate_bytecode_return (exp
, state
);
1815 case LABELED_BLOCK_EXPR
:
1817 struct jcf_block
*end_label
= gen_jcf_label (state
);
1818 end_label
->next
= state
->labeled_blocks
;
1819 state
->labeled_blocks
= end_label
;
1820 end_label
->pc
= PENDING_EXIT_PC
;
1821 end_label
->u
.labeled_block
= exp
;
1822 if (LABELED_BLOCK_BODY (exp
))
1823 generate_bytecode_insns (LABELED_BLOCK_BODY (exp
), target
, state
);
1824 if (state
->labeled_blocks
!= end_label
)
1826 state
->labeled_blocks
= end_label
->next
;
1827 define_jcf_label (end_label
, state
);
1832 tree body
= TREE_OPERAND (exp
, 0);
1834 if (TREE_CODE (body
) == COMPOUND_EXPR
1835 && TREE_CODE (TREE_OPERAND (body
, 0)) == EXIT_EXPR
)
1837 /* Optimize: H: if (TEST) GOTO L; BODY; GOTO H; L:
1838 to: GOTO L; BODY; L: if (!TEST) GOTO L; */
1839 struct jcf_block
*head_label
;
1840 struct jcf_block
*body_label
;
1841 struct jcf_block
*end_label
= gen_jcf_label (state
);
1842 struct jcf_block
*exit_label
= state
->labeled_blocks
;
1843 head_label
= gen_jcf_label (state
);
1844 emit_goto (head_label
, state
);
1845 body_label
= get_jcf_label_here (state
);
1846 generate_bytecode_insns (TREE_OPERAND (body
, 1), target
, state
);
1847 define_jcf_label (head_label
, state
);
1848 generate_bytecode_conditional (TREE_OPERAND (body
, 0),
1849 end_label
, body_label
, 1, state
);
1850 define_jcf_label (end_label
, state
);
1855 struct jcf_block
*head_label
= get_jcf_label_here (state
);
1856 generate_bytecode_insns (body
, IGNORE_TARGET
, state
);
1857 emit_goto (head_label
, state
);
1863 struct jcf_block
*label
= state
->labeled_blocks
;
1864 struct jcf_block
*end_label
= gen_jcf_label (state
);
1865 generate_bytecode_conditional (TREE_OPERAND (exp
, 0),
1866 label
, end_label
, 0, state
);
1867 define_jcf_label (end_label
, state
);
1870 case EXIT_BLOCK_EXPR
:
1872 struct jcf_block
*label
= state
->labeled_blocks
;
1873 if (TREE_OPERAND (exp
, 1) != NULL
) goto notimpl
;
1874 while (label
->u
.labeled_block
!= TREE_OPERAND (exp
, 0))
1875 label
= label
->next
;
1876 call_cleanups (label
, state
);
1877 emit_goto (label
, state
);
1881 case PREDECREMENT_EXPR
: value
= -1; post_op
= 0; goto increment
;
1882 case PREINCREMENT_EXPR
: value
= 1; post_op
= 0; goto increment
;
1883 case POSTDECREMENT_EXPR
: value
= -1; post_op
= 1; goto increment
;
1884 case POSTINCREMENT_EXPR
: value
= 1; post_op
= 1; goto increment
;
1887 exp
= TREE_OPERAND (exp
, 0);
1888 type
= TREE_TYPE (exp
);
1889 size
= TYPE_IS_WIDE (type
) ? 2 : 1;
1890 if ((TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1891 && ! TREE_STATIC (exp
)
1892 && TREE_CODE (type
) == INTEGER_TYPE
1893 && TYPE_PRECISION (type
) == 32)
1895 if (target
!= IGNORE_TARGET
&& post_op
)
1896 emit_load (exp
, state
);
1897 emit_iinc (exp
, value
, state
);
1898 if (target
!= IGNORE_TARGET
&& ! post_op
)
1899 emit_load (exp
, state
);
1902 if (TREE_CODE (exp
) == COMPONENT_REF
)
1904 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
1905 emit_dup (1, 0, state
);
1906 /* Stack: ..., objectref, objectref. */
1907 field_op (TREE_OPERAND (exp
, 1), OPCODE_getfield
, state
);
1909 /* Stack: ..., objectref, oldvalue. */
1912 else if (TREE_CODE (exp
) == ARRAY_REF
)
1914 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
1915 generate_bytecode_insns (TREE_OPERAND (exp
, 1), STACK_TARGET
, state
);
1916 emit_dup (2, 0, state
);
1917 /* Stack: ..., array, index, array, index. */
1918 jopcode
= OPCODE_iaload
+ adjust_typed_op (TREE_TYPE (exp
), 7);
1922 /* Stack: ..., array, index, oldvalue. */
1925 else if (TREE_CODE (exp
) == VAR_DECL
|| TREE_CODE (exp
) == PARM_DECL
)
1927 generate_bytecode_insns (exp
, STACK_TARGET
, state
);
1928 /* Stack: ..., oldvalue. */
1934 if (target
!= IGNORE_TARGET
&& post_op
)
1935 emit_dup (size
, offset
, state
);
1936 /* Stack, if ARRAY_REF: ..., [result, ] array, index, oldvalue. */
1937 /* Stack, if COMPONENT_REF: ..., [result, ] objectref, oldvalue. */
1938 /* Stack, otherwise: ..., [result, ] oldvalue. */
1940 push_int_const (value
, state
);
1942 push_long_const (value
, (HOST_WIDE_INT
)(value
>= 0 ? 0 : -1), state
);
1944 emit_binop (OPCODE_iadd
+ adjust_typed_op (type
, 3), type
, state
);
1945 if (target
!= IGNORE_TARGET
&& ! post_op
)
1946 emit_dup (size
, offset
, state
);
1947 /* Stack, if ARRAY_REF: ..., [result, ] array, index, newvalue. */
1948 /* Stack, if COMPONENT_REF: ..., [result, ] objectref, newvalue. */
1949 /* Stack, otherwise: ..., [result, ] newvalue. */
1950 goto finish_assignment
;
1954 tree lhs
= TREE_OPERAND (exp
, 0);
1955 tree rhs
= TREE_OPERAND (exp
, 1);
1958 /* See if we can use the iinc instruction. */
1959 if ((TREE_CODE (lhs
) == VAR_DECL
|| TREE_CODE (lhs
) == PARM_DECL
)
1960 && ! TREE_STATIC (lhs
)
1961 && TREE_CODE (TREE_TYPE (lhs
)) == INTEGER_TYPE
1962 && TYPE_PRECISION (TREE_TYPE (lhs
)) == 32
1963 && (TREE_CODE (rhs
) == PLUS_EXPR
|| TREE_CODE (rhs
) == MINUS_EXPR
))
1965 tree arg0
= TREE_OPERAND (rhs
, 0);
1966 tree arg1
= TREE_OPERAND (rhs
, 1);
1967 HOST_WIDE_INT min_value
= -32768;
1968 HOST_WIDE_INT max_value
= 32767;
1969 if (TREE_CODE (rhs
) == MINUS_EXPR
)
1974 else if (arg1
== lhs
)
1977 arg1
= TREE_OPERAND (rhs
, 0);
1979 if (lhs
== arg0
&& TREE_CODE (arg1
) == INTEGER_CST
)
1981 HOST_WIDE_INT hi_value
= TREE_INT_CST_HIGH (arg1
);
1982 value
= TREE_INT_CST_LOW (arg1
);
1983 if ((hi_value
== 0 && value
<= max_value
)
1984 || (hi_value
== -1 && value
>= min_value
))
1986 if (TREE_CODE (rhs
) == MINUS_EXPR
)
1988 emit_iinc (lhs
, value
, state
);
1994 if (TREE_CODE (lhs
) == COMPONENT_REF
)
1996 generate_bytecode_insns (TREE_OPERAND (lhs
, 0),
1997 STACK_TARGET
, state
);
2000 else if (TREE_CODE (lhs
) == ARRAY_REF
)
2002 generate_bytecode_insns (TREE_OPERAND(lhs
, 0),
2003 STACK_TARGET
, state
);
2004 generate_bytecode_insns (TREE_OPERAND(lhs
, 1),
2005 STACK_TARGET
, state
);
2010 generate_bytecode_insns (rhs
, STACK_TARGET
, state
);
2011 if (target
!= IGNORE_TARGET
)
2012 emit_dup (TYPE_IS_WIDE (type
) ? 2 : 1 , offset
, state
);
2018 if (TREE_CODE (exp
) == COMPONENT_REF
)
2020 tree field
= TREE_OPERAND (exp
, 1);
2021 if (! FIELD_STATIC (field
))
2024 FIELD_STATIC (field
) ? OPCODE_putstatic
: OPCODE_putfield
,
2027 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (field
)) ? 2 : 1);
2029 else if (TREE_CODE (exp
) == VAR_DECL
2030 || TREE_CODE (exp
) == PARM_DECL
)
2032 if (FIELD_STATIC (exp
))
2034 field_op (exp
, OPCODE_putstatic
, state
);
2035 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 2 : 1);
2038 emit_store (exp
, state
);
2040 else if (TREE_CODE (exp
) == ARRAY_REF
)
2042 jopcode
= OPCODE_iastore
+ adjust_typed_op (TREE_TYPE (exp
), 7);
2045 NOTE_POP (TYPE_IS_WIDE (TREE_TYPE (exp
)) ? 4 : 3);
2048 fatal ("internal error (bad lhs to MODIFY_EXPR)");
2051 jopcode
= OPCODE_iadd
;
2054 jopcode
= OPCODE_isub
;
2057 jopcode
= OPCODE_imul
;
2059 case TRUNC_DIV_EXPR
:
2061 jopcode
= OPCODE_idiv
;
2063 case TRUNC_MOD_EXPR
:
2064 jopcode
= OPCODE_irem
;
2066 case LSHIFT_EXPR
: jopcode
= OPCODE_ishl
; goto binop
;
2067 case RSHIFT_EXPR
: jopcode
= OPCODE_ishr
; goto binop
;
2068 case URSHIFT_EXPR
: jopcode
= OPCODE_iushr
; goto binop
;
2069 case TRUTH_AND_EXPR
:
2070 case BIT_AND_EXPR
: jopcode
= OPCODE_iand
; goto binop
;
2072 case BIT_IOR_EXPR
: jopcode
= OPCODE_ior
; goto binop
;
2073 case TRUTH_XOR_EXPR
:
2074 case BIT_XOR_EXPR
: jopcode
= OPCODE_ixor
; goto binop
;
2077 tree arg0
= TREE_OPERAND (exp
, 0);
2078 tree arg1
= TREE_OPERAND (exp
, 1);
2079 jopcode
+= adjust_typed_op (type
, 3);
2080 if (arg0
== arg1
&& TREE_CODE (arg0
) == SAVE_EXPR
)
2082 /* fold may (e.g) convert 2*x to x+x. */
2083 generate_bytecode_insns (TREE_OPERAND (arg0
, 0), target
, state
);
2084 emit_dup (TYPE_PRECISION (TREE_TYPE (arg0
)) > 32 ? 2 : 1, 0, state
);
2088 generate_bytecode_insns (arg0
, target
, state
);
2089 generate_bytecode_insns (arg1
, target
, state
);
2091 /* For most binary operations, both operands and the result have the
2092 same type. Shift operations are different. Using arg1's type
2093 gets us the correct SP adjustment in all casesd. */
2094 if (target
== STACK_TARGET
)
2095 emit_binop (jopcode
, TREE_TYPE (arg1
), state
);
2098 case TRUTH_NOT_EXPR
:
2100 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2101 if (target
== STACK_TARGET
)
2103 int is_long
= TYPE_PRECISION (TREE_TYPE (exp
)) > 32;
2104 push_int_const (TREE_CODE (exp
) == BIT_NOT_EXPR
? -1 : 1, state
);
2108 NOTE_PUSH (1 + is_long
);
2109 OP1 (OPCODE_ixor
+ is_long
);
2110 NOTE_POP (1 + is_long
);
2114 jopcode
= OPCODE_ineg
;
2115 jopcode
+= adjust_typed_op (type
, 3);
2116 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2117 if (target
== STACK_TARGET
)
2118 emit_unop (jopcode
, type
, state
);
2120 case INSTANCEOF_EXPR
:
2122 int index
= find_class_constant (&state
->cpool
, TREE_OPERAND (exp
, 1));
2123 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2125 OP1 (OPCODE_instanceof
);
2132 case FIX_TRUNC_EXPR
:
2134 tree src
= TREE_OPERAND (exp
, 0);
2135 tree src_type
= TREE_TYPE (src
);
2136 tree dst_type
= TREE_TYPE (exp
);
2137 generate_bytecode_insns (TREE_OPERAND (exp
, 0), target
, state
);
2138 if (target
== IGNORE_TARGET
|| src_type
== dst_type
)
2140 if (TREE_CODE (dst_type
) == POINTER_TYPE
)
2142 if (TREE_CODE (exp
) == CONVERT_EXPR
)
2144 int index
= find_class_constant (&state
->cpool
, TREE_TYPE (dst_type
));
2146 OP1 (OPCODE_checkcast
);
2150 else /* Convert numeric types. */
2152 int wide_src
= TYPE_PRECISION (src_type
) > 32;
2153 int wide_dst
= TYPE_PRECISION (dst_type
) > 32;
2154 NOTE_POP (1 + wide_src
);
2156 if (TREE_CODE (dst_type
) == REAL_TYPE
)
2158 if (TREE_CODE (src_type
) == REAL_TYPE
)
2159 OP1 (wide_dst
? OPCODE_f2d
: OPCODE_d2f
);
2160 else if (TYPE_PRECISION (src_type
) == 64)
2161 OP1 (OPCODE_l2f
+ wide_dst
);
2163 OP1 (OPCODE_i2f
+ wide_dst
);
2165 else /* Convert to integral type. */
2167 if (TREE_CODE (src_type
) == REAL_TYPE
)
2168 OP1 (OPCODE_f2i
+ wide_dst
+ 3 * wide_src
);
2173 if (TYPE_PRECISION (dst_type
) < 32)
2176 /* Already converted to int, if needed. */
2177 if (TYPE_PRECISION (dst_type
) <= 8)
2179 else if (TREE_UNSIGNED (dst_type
))
2185 NOTE_PUSH (1 + wide_dst
);
2190 case CLEANUP_POINT_EXPR
:
2192 struct jcf_block
*save_labeled_blocks
= state
->labeled_blocks
;
2193 int can_complete
= CAN_COMPLETE_NORMALLY (TREE_OPERAND (exp
, 0));
2194 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
, state
);
2195 if (target
!= IGNORE_TARGET
)
2197 while (state
->labeled_blocks
!= save_labeled_blocks
)
2199 struct jcf_block
*finished_label
= NULL
;
2201 tree exception_type
= build_pointer_type (throwable_type_node
);
2202 tree exception_decl
= build_decl (VAR_DECL
, NULL_TREE
,
2204 struct jcf_block
*end_label
= get_jcf_label_here (state
);
2205 struct jcf_block
*label
= state
->labeled_blocks
;
2206 struct jcf_handler
*handler
;
2207 tree cleanup
= label
->u
.labeled_block
;
2208 state
->labeled_blocks
= label
->next
;
2209 state
->num_finalizers
--;
2212 finished_label
= gen_jcf_label (state
);
2213 emit_jsr (label
, state
);
2214 emit_goto (finished_label
, state
);
2215 if (! CAN_COMPLETE_NORMALLY (cleanup
))
2218 handler
= alloc_handler (label
->v
.start_label
, end_label
, state
);
2219 handler
->type
= NULL_TREE
;
2220 localvar_alloc (exception_decl
, state
);
2222 emit_store (exception_decl
, state
);
2223 emit_jsr (label
, state
);
2224 emit_load (exception_decl
, state
);
2226 OP1 (OPCODE_athrow
);
2229 /* The finally block. */
2230 return_link
= build_decl (VAR_DECL
, NULL_TREE
,
2231 return_address_type_node
);
2232 define_jcf_label (label
, state
);
2234 localvar_alloc (return_link
, state
);
2235 emit_store (return_link
, state
);
2236 generate_bytecode_insns (cleanup
, IGNORE_TARGET
, state
);
2237 maybe_wide (OPCODE_ret
, DECL_LOCAL_INDEX (return_link
), state
);
2238 localvar_free (return_link
, state
);
2239 localvar_free (exception_decl
, state
);
2240 if (finished_label
!= NULL
)
2241 define_jcf_label (finished_label
, state
);
2246 case WITH_CLEANUP_EXPR
:
2248 struct jcf_block
*label
;
2249 generate_bytecode_insns (TREE_OPERAND (exp
, 0), IGNORE_TARGET
, state
);
2250 label
= gen_jcf_label (state
);
2251 label
->pc
= PENDING_CLEANUP_PC
;
2252 label
->next
= state
->labeled_blocks
;
2253 state
->labeled_blocks
= label
;
2254 state
->num_finalizers
++;
2255 label
->u
.labeled_block
= TREE_OPERAND (exp
, 2);
2256 label
->v
.start_label
= get_jcf_label_here (state
);
2257 if (target
!= IGNORE_TARGET
)
2264 tree try_clause
= TREE_OPERAND (exp
, 0);
2265 struct jcf_block
*start_label
= get_jcf_label_here (state
);
2266 struct jcf_block
*end_label
; /* End of try clause. */
2267 struct jcf_block
*finished_label
= gen_jcf_label (state
);
2268 tree clause
= TREE_OPERAND (exp
, 1);
2269 if (target
!= IGNORE_TARGET
)
2271 generate_bytecode_insns (try_clause
, IGNORE_TARGET
, state
);
2272 end_label
= get_jcf_label_here (state
);
2273 if (CAN_COMPLETE_NORMALLY (try_clause
))
2274 emit_goto (finished_label
, state
);
2275 while (clause
!= NULL_TREE
)
2277 tree catch_clause
= TREE_OPERAND (clause
, 0);
2278 tree exception_decl
= BLOCK_EXPR_DECLS (catch_clause
);
2279 struct jcf_handler
*handler
= alloc_handler (start_label
, end_label
, state
);
2280 if (exception_decl
== NULL_TREE
)
2281 handler
->type
= NULL_TREE
;
2283 handler
->type
= TREE_TYPE (TREE_TYPE (exception_decl
));
2284 generate_bytecode_insns (catch_clause
, IGNORE_TARGET
, state
);
2285 clause
= TREE_CHAIN (clause
);
2286 if (CAN_COMPLETE_NORMALLY (catch_clause
) && clause
!= NULL_TREE
)
2287 emit_goto (finished_label
, state
);
2289 define_jcf_label (finished_label
, state
);
2292 case TRY_FINALLY_EXPR
:
2294 tree try_block
= TREE_OPERAND (exp
, 0);
2295 tree finally
= TREE_OPERAND (exp
, 1);
2296 struct jcf_block
*finished_label
= gen_jcf_label (state
);
2297 struct jcf_block
*finally_label
= gen_jcf_label (state
);
2298 struct jcf_block
*start_label
= get_jcf_label_here (state
);
2299 tree return_link
= build_decl (VAR_DECL
, NULL_TREE
,
2300 return_address_type_node
);
2301 tree exception_type
= build_pointer_type (throwable_type_node
);
2302 tree exception_decl
= build_decl (VAR_DECL
, NULL_TREE
, exception_type
);
2303 struct jcf_handler
*handler
;
2305 finally_label
->pc
= PENDING_CLEANUP_PC
;
2306 finally_label
->next
= state
->labeled_blocks
;
2307 state
->labeled_blocks
= finally_label
;
2308 state
->num_finalizers
++;
2310 generate_bytecode_insns (try_block
, target
, state
);
2311 if (state
->labeled_blocks
!= finally_label
)
2313 state
->labeled_blocks
= finally_label
->next
;
2314 emit_jsr (finally_label
, state
);
2315 if (CAN_COMPLETE_NORMALLY (try_block
))
2316 emit_goto (finished_label
, state
);
2318 /* Handle exceptions. */
2319 localvar_alloc (return_link
, state
);
2320 handler
= alloc_handler (start_label
, NULL_PTR
, state
);
2321 handler
->end_label
= handler
->handler_label
;
2322 handler
->type
= NULL_TREE
;
2323 localvar_alloc (exception_decl
, state
);
2325 emit_store (exception_decl
, state
);
2326 emit_jsr (finally_label
, state
);
2327 emit_load (exception_decl
, state
);
2329 OP1 (OPCODE_athrow
);
2331 localvar_free (exception_decl
, state
);
2333 /* The finally block. First save return PC into return_link. */
2334 define_jcf_label (finally_label
, state
);
2336 emit_store (return_link
, state
);
2338 generate_bytecode_insns (finally
, IGNORE_TARGET
, state
);
2339 maybe_wide (OPCODE_ret
, DECL_LOCAL_INDEX (return_link
), state
);
2340 localvar_free (return_link
, state
);
2341 define_jcf_label (finished_label
, state
);
2345 generate_bytecode_insns (TREE_OPERAND (exp
, 0), STACK_TARGET
, state
);
2347 OP1 (OPCODE_athrow
);
2349 case NEW_ARRAY_INIT
:
2351 tree values
= CONSTRUCTOR_ELTS (TREE_OPERAND (exp
, 0));
2352 tree array_type
= TREE_TYPE (TREE_TYPE (exp
));
2353 tree element_type
= TYPE_ARRAY_ELEMENT (array_type
);
2354 HOST_WIDE_INT length
= java_array_type_length (array_type
);
2355 if (target
== IGNORE_TARGET
)
2357 for ( ; values
!= NULL_TREE
; values
= TREE_CHAIN (values
))
2358 generate_bytecode_insns (TREE_VALUE (values
), target
, state
);
2361 push_int_const (length
, state
);
2364 if (JPRIMITIVE_TYPE_P (element_type
))
2366 int atype
= encode_newarray_type (element_type
);
2367 OP1 (OPCODE_newarray
);
2372 int index
= find_class_constant (&state
->cpool
,
2373 TREE_TYPE (element_type
));
2374 OP1 (OPCODE_anewarray
);
2378 jopcode
= OPCODE_iastore
+ adjust_typed_op (element_type
, 7);
2379 for ( ; values
!= NULL_TREE
; values
= TREE_CHAIN (values
), offset
++)
2381 int save_SP
= state
->code_SP
;
2382 emit_dup (1, 0, state
);
2383 push_int_const (offset
, state
);
2385 generate_bytecode_insns (TREE_VALUE (values
), STACK_TARGET
, state
);
2388 state
->code_SP
= save_SP
;
2392 case NEW_CLASS_EXPR
:
2394 tree
class = TREE_TYPE (TREE_TYPE (exp
));
2395 int need_result
= target
!= IGNORE_TARGET
;
2396 int index
= find_class_constant (&state
->cpool
, class);
2402 NOTE_PUSH (1 + need_result
);
2404 /* ... fall though ... */
2407 tree f
= TREE_OPERAND (exp
, 0);
2408 tree x
= TREE_OPERAND (exp
, 1);
2409 int save_SP
= state
->code_SP
;
2411 if (TREE_CODE (f
) == ADDR_EXPR
)
2412 f
= TREE_OPERAND (f
, 0);
2413 if (f
== soft_newarray_node
)
2415 int type_code
= TREE_INT_CST_LOW (TREE_VALUE (x
));
2416 generate_bytecode_insns (TREE_VALUE (TREE_CHAIN (x
)),
2417 STACK_TARGET
, state
);
2419 OP1 (OPCODE_newarray
);
2423 else if (f
== soft_multianewarray_node
)
2427 int index
= find_class_constant (&state
->cpool
,
2428 TREE_TYPE (TREE_TYPE (exp
)));
2429 x
= TREE_CHAIN (x
); /* Skip class argument. */
2430 ndims
= TREE_INT_CST_LOW (TREE_VALUE (x
));
2431 for (idim
= ndims
; --idim
>= 0; )
2434 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2437 OP1 (OPCODE_multianewarray
);
2442 else if (f
== soft_anewarray_node
)
2444 tree cl
= TYPE_ARRAY_ELEMENT (TREE_TYPE (TREE_TYPE (exp
)));
2445 int index
= find_class_constant (&state
->cpool
, TREE_TYPE (cl
));
2446 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2448 OP1 (OPCODE_anewarray
);
2452 else if (f
== soft_monitorenter_node
2453 || f
== soft_monitorexit_node
2456 if (f
== soft_monitorenter_node
)
2457 op
= OPCODE_monitorenter
;
2458 else if (f
== soft_monitorexit_node
)
2459 op
= OPCODE_monitorexit
;
2462 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2468 else if (exp
== soft_exceptioninfo_call_node
)
2470 NOTE_PUSH (1); /* Pushed by exception system. */
2473 for ( ; x
!= NULL_TREE
; x
= TREE_CHAIN (x
))
2475 generate_bytecode_insns (TREE_VALUE (x
), STACK_TARGET
, state
);
2477 nargs
= state
->code_SP
- save_SP
;
2478 state
->code_SP
= save_SP
;
2479 if (f
== soft_fmod_node
)
2486 if (TREE_CODE (exp
) == NEW_CLASS_EXPR
)
2487 NOTE_POP (1); /* Pop implicit this. */
2488 if (TREE_CODE (f
) == FUNCTION_DECL
&& DECL_CONTEXT (f
) != NULL_TREE
)
2490 int index
= find_methodref_index (&state
->cpool
, f
);
2493 if (METHOD_STATIC (f
))
2494 OP1 (OPCODE_invokestatic
);
2495 else if (DECL_CONSTRUCTOR_P (f
) || CALL_USING_SUPER (exp
)
2496 || METHOD_PRIVATE (f
))
2497 OP1 (OPCODE_invokespecial
);
2498 else if (CLASS_INTERFACE (TYPE_NAME (DECL_CONTEXT (f
))))
2500 OP1 (OPCODE_invokeinterface
);
2504 OP1 (OPCODE_invokevirtual
);
2506 f
= TREE_TYPE (TREE_TYPE (f
));
2507 if (TREE_CODE (f
) != VOID_TYPE
)
2509 int size
= TYPE_IS_WIDE (f
) ? 2 : 1;
2510 if (target
== IGNORE_TARGET
)
2511 emit_pop (size
, state
);
2526 error("internal error - tree code not implemented: %s",
2527 tree_code_name
[(int) TREE_CODE (exp
)]);
2532 perform_relocations (state
)
2533 struct jcf_partial
*state
;
2535 struct jcf_block
*block
;
2536 struct jcf_relocation
*reloc
;
2540 /* Before we start, the pc field of each block is an upper bound on
2541 the block's start pc (it may be less, if previous blocks need less
2542 than their maximum).
2544 The minimum size of each block is in the block's chunk->size. */
2546 /* First, figure out the actual locations of each block. */
2549 for (block
= state
->blocks
; block
!= NULL
; block
= block
->next
)
2551 int block_size
= block
->v
.chunk
->size
;
2555 /* Optimize GOTO L; L: by getting rid of the redundant goto.
2556 Assumes relocations are in reverse order. */
2557 reloc
= block
->u
.relocations
;
2558 while (reloc
!= NULL
2559 && reloc
->kind
== OPCODE_goto_w
2560 && reloc
->label
->pc
== block
->next
->pc
2561 && reloc
->offset
+ 2 == block_size
)
2563 reloc
= reloc
->next
;
2564 block
->u
.relocations
= reloc
;
2565 block
->v
.chunk
->size
-= 3;
2570 for (reloc
= block
->u
.relocations
; reloc
!= NULL
; reloc
= reloc
->next
)
2572 if (reloc
->kind
== SWITCH_ALIGN_RELOC
)
2574 /* We assume this is the first relocation in this block,
2575 so we know its final pc. */
2576 int where
= pc
+ reloc
->offset
;
2577 int pad
= ((where
+ 3) & ~3) - where
;
2580 else if (reloc
->kind
< -1 || reloc
->kind
> BLOCK_START_RELOC
)
2582 int delta
= reloc
->label
->pc
- (pc
+ reloc
->offset
- 1);
2583 int expand
= reloc
->kind
> 0 ? 2 : 5;
2587 if (delta
>= -32768 && delta
<= 32767)
2593 block_size
+= expand
;
2599 for (block
= state
->blocks
; block
!= NULL
; block
= block
->next
)
2601 struct chunk
*chunk
= block
->v
.chunk
;
2602 int old_size
= chunk
->size
;
2603 int next_pc
= block
->next
== NULL
? pc
: block
->next
->pc
;
2604 int new_size
= next_pc
- block
->pc
;
2605 unsigned char *new_ptr
;
2606 unsigned char *old_buffer
= chunk
->data
;
2607 unsigned char *old_ptr
= old_buffer
+ old_size
;
2608 if (new_size
!= old_size
)
2610 chunk
->data
= (unsigned char *)
2611 obstack_alloc (state
->chunk_obstack
, new_size
);
2612 chunk
->size
= new_size
;
2614 new_ptr
= chunk
->data
+ new_size
;
2616 /* We do the relocations from back to front, because
2617 the relocations are in reverse order. */
2618 for (reloc
= block
->u
.relocations
; ; reloc
= reloc
->next
)
2620 /* new_ptr and old_ptr point into the old and new buffers,
2621 respectively. (If no relocations cause the buffer to
2622 grow, the buffer will be the same buffer, and new_ptr==old_ptr.)
2623 The bytes at higher adress have been copied and relocations
2624 handled; those at lower addresses remain to process. */
2626 /* Lower old index of piece to be copied with no relocation.
2627 I.e. high index of the first piece that does need relocation. */
2628 int start
= reloc
== NULL
? 0
2629 : reloc
->kind
== SWITCH_ALIGN_RELOC
? reloc
->offset
2630 : (reloc
->kind
== 0 || reloc
->kind
== BLOCK_START_RELOC
)
2632 : reloc
->offset
+ 2;
2635 int n
= (old_ptr
- old_buffer
) - start
;
2639 memcpy (new_ptr
, old_ptr
, n
);
2640 if (old_ptr
== old_buffer
)
2643 new_offset
= new_ptr
- chunk
->data
;
2644 new_offset
-= (reloc
->kind
== -1 ? 2 : 4);
2645 if (reloc
->kind
== 0)
2648 value
= GET_u4 (old_ptr
);
2650 else if (reloc
->kind
== BLOCK_START_RELOC
)
2656 else if (reloc
->kind
== SWITCH_ALIGN_RELOC
)
2658 int where
= block
->pc
+ reloc
->offset
;
2659 int pad
= ((where
+ 3) & ~3) - where
;
2667 value
= GET_u2 (old_ptr
);
2669 value
+= reloc
->label
->pc
- (block
->pc
+ new_offset
);
2670 *--new_ptr
= (unsigned char) value
; value
>>= 8;
2671 *--new_ptr
= (unsigned char) value
; value
>>= 8;
2672 if (reloc
->kind
!= -1)
2674 *--new_ptr
= (unsigned char) value
; value
>>= 8;
2675 *--new_ptr
= (unsigned char) value
;
2677 if (reloc
->kind
> BLOCK_START_RELOC
)
2679 /* Convert: OP TARGET to: OP_w TARGET; (OP is goto or jsr). */
2681 *--new_ptr
= reloc
->kind
;
2683 else if (reloc
->kind
< -1)
2685 /* Convert: ifCOND TARGET to: ifNCOND T; goto_w TARGET; T: */
2687 *--new_ptr
= OPCODE_goto_w
;
2690 *--new_ptr
= - reloc
->kind
;
2693 if (new_ptr
!= chunk
->data
)
2694 fatal ("internal error - perform_relocations");
2696 state
->code_length
= pc
;
2700 init_jcf_state (state
, work
)
2701 struct jcf_partial
*state
;
2702 struct obstack
*work
;
2704 state
->chunk_obstack
= work
;
2705 state
->first
= state
->chunk
= NULL
;
2706 CPOOL_INIT (&state
->cpool
);
2707 BUFFER_INIT (&state
->localvars
);
2708 BUFFER_INIT (&state
->bytecode
);
2712 init_jcf_method (state
, method
)
2713 struct jcf_partial
*state
;
2716 state
->current_method
= method
;
2717 state
->blocks
= state
->last_block
= NULL
;
2718 state
->linenumber_count
= 0;
2719 state
->first_lvar
= state
->last_lvar
= NULL
;
2720 state
->lvar_count
= 0;
2721 state
->labeled_blocks
= NULL
;
2722 state
->code_length
= 0;
2723 BUFFER_RESET (&state
->bytecode
);
2724 BUFFER_RESET (&state
->localvars
);
2726 state
->code_SP_max
= 0;
2727 state
->handlers
= NULL
;
2728 state
->last_handler
= NULL
;
2729 state
->num_handlers
= 0;
2730 state
->num_finalizers
= 0;
2731 state
->return_value_decl
= NULL_TREE
;
2735 release_jcf_state (state
)
2736 struct jcf_partial
*state
;
2738 CPOOL_FINISH (&state
->cpool
);
2739 obstack_free (state
->chunk_obstack
, state
->first
);
2742 /* Generate and return a list of chunks containing the class CLAS
2743 in the .class file representation. The list can be written to a
2744 .class file using write_chunks. Allocate chunks from obstack WORK. */
2746 static struct chunk
*
2747 generate_classfile (clas
, state
)
2749 struct jcf_partial
*state
;
2751 struct chunk
*cpool_chunk
;
2755 char *fields_count_ptr
;
2756 int fields_count
= 0;
2757 char *methods_count_ptr
;
2758 int methods_count
= 0;
2759 static tree SourceFile_node
= NULL_TREE
;
2762 = clas
== object_type_node
? 0
2763 : TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (clas
));
2765 ptr
= append_chunk (NULL
, 8, state
);
2766 PUT4 (0xCafeBabe); /* Magic number */
2767 PUT2 (3); /* Minor version */
2768 PUT2 (45); /* Major version */
2770 append_chunk (NULL
, 0, state
);
2771 cpool_chunk
= state
->chunk
;
2773 /* Next allocate the chunk containing acces_flags through fields_counr. */
2774 if (clas
== object_type_node
)
2777 i
= 8 + 2 * total_supers
;
2778 ptr
= append_chunk (NULL
, i
, state
);
2779 i
= get_access_flags (TYPE_NAME (clas
));
2780 if (! (i
& ACC_INTERFACE
))
2782 PUT2 (i
); /* acces_flags */
2783 i
= find_class_constant (&state
->cpool
, clas
); PUT2 (i
); /* this_class */
2784 if (clas
== object_type_node
)
2786 PUT2(0); /* super_class */
2787 PUT2(0); /* interfaces_count */
2791 tree basetypes
= TYPE_BINFO_BASETYPES (clas
);
2792 tree base
= BINFO_TYPE (TREE_VEC_ELT (basetypes
, 0));
2793 int j
= find_class_constant (&state
->cpool
, base
);
2794 PUT2 (j
); /* super_class */
2795 PUT2 (total_supers
- 1); /* interfaces_count */
2796 for (i
= 1; i
< total_supers
; i
++)
2798 base
= BINFO_TYPE (TREE_VEC_ELT (basetypes
, i
));
2799 j
= find_class_constant (&state
->cpool
, base
);
2803 fields_count_ptr
= ptr
;
2805 for (part
= TYPE_FIELDS (clas
); part
; part
= TREE_CHAIN (part
))
2808 if (DECL_NAME (part
) == NULL_TREE
|| DECL_ARTIFICIAL (part
))
2810 ptr
= append_chunk (NULL
, 8, state
);
2811 i
= get_access_flags (part
); PUT2 (i
);
2812 i
= find_utf8_constant (&state
->cpool
, DECL_NAME (part
)); PUT2 (i
);
2813 i
= find_utf8_constant (&state
->cpool
, build_java_signature (TREE_TYPE (part
)));
2815 have_value
= DECL_INITIAL (part
) != NULL_TREE
&& FIELD_STATIC (part
);
2816 PUT2 (have_value
); /* attributes_count */
2819 tree init
= DECL_INITIAL (part
);
2820 static tree ConstantValue_node
= NULL_TREE
;
2821 ptr
= append_chunk (NULL
, 8, state
);
2822 if (ConstantValue_node
== NULL_TREE
)
2823 ConstantValue_node
= get_identifier ("ConstantValue");
2824 i
= find_utf8_constant (&state
->cpool
, ConstantValue_node
);
2825 PUT2 (i
); /* attribute_name_index */
2826 PUT4 (2); /* attribute_length */
2827 i
= find_constant_index (init
, state
); PUT2 (i
);
2831 ptr
= fields_count_ptr
; UNSAFE_PUT2 (fields_count
);
2833 ptr
= methods_count_ptr
= append_chunk (NULL
, 2, state
);
2836 for (part
= TYPE_METHODS (clas
); part
; part
= TREE_CHAIN (part
))
2838 struct jcf_block
*block
;
2839 tree function_body
= DECL_FUNCTION_BODY (part
);
2840 tree body
= function_body
== NULL_TREE
? NULL_TREE
2841 : BLOCK_EXPR_BODY (function_body
);
2842 tree name
= DECL_CONSTRUCTOR_P (part
) ? init_identifier_node
2844 tree type
= TREE_TYPE (part
);
2845 tree save_function
= current_function_decl
;
2846 current_function_decl
= part
;
2847 ptr
= append_chunk (NULL
, 8, state
);
2848 i
= get_access_flags (part
); PUT2 (i
);
2849 i
= find_utf8_constant (&state
->cpool
, name
); PUT2 (i
);
2850 i
= find_utf8_constant (&state
->cpool
, build_java_signature (type
));
2852 i
= (body
!= NULL_TREE
) + (DECL_FUNCTION_THROWS (part
) != NULL_TREE
);
2853 PUT2 (i
); /* attributes_count */
2854 if (body
!= NULL_TREE
)
2856 int code_attributes_count
= 0;
2857 static tree Code_node
= NULL_TREE
;
2860 struct jcf_handler
*handler
;
2861 if (Code_node
== NULL_TREE
)
2862 Code_node
= get_identifier ("Code");
2863 ptr
= append_chunk (NULL
, 14, state
);
2864 i
= find_utf8_constant (&state
->cpool
, Code_node
); PUT2 (i
);
2866 init_jcf_method (state
, part
);
2867 get_jcf_label_here (state
); /* Force a first block. */
2868 for (t
= DECL_ARGUMENTS (part
); t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2869 localvar_alloc (t
, state
);
2870 generate_bytecode_insns (body
, IGNORE_TARGET
, state
);
2871 if (CAN_COMPLETE_NORMALLY (body
))
2873 if (TREE_CODE (TREE_TYPE (type
)) != VOID_TYPE
)
2876 OP1 (OPCODE_return
);
2878 for (t
= DECL_ARGUMENTS (part
); t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2879 localvar_free (t
, state
);
2880 if (state
->return_value_decl
!= NULL_TREE
)
2881 localvar_free (state
->return_value_decl
, state
);
2882 finish_jcf_block (state
);
2883 perform_relocations (state
);
2886 i
= 8 + state
->code_length
+ 4 + 8 * state
->num_handlers
;
2887 if (state
->linenumber_count
> 0)
2889 code_attributes_count
++;
2890 i
+= 8 + 4 * state
->linenumber_count
;
2892 if (state
->lvar_count
> 0)
2894 code_attributes_count
++;
2895 i
+= 8 + 10 * state
->lvar_count
;
2897 UNSAFE_PUT4 (i
); /* attribute_length */
2898 UNSAFE_PUT2 (state
->code_SP_max
); /* max_stack */
2899 UNSAFE_PUT2 (localvar_max
); /* max_locals */
2900 UNSAFE_PUT4 (state
->code_length
);
2902 /* Emit the exception table. */
2903 ptr
= append_chunk (NULL
, 2 + 8 * state
->num_handlers
, state
);
2904 PUT2 (state
->num_handlers
); /* exception_table_length */
2905 handler
= state
->handlers
;
2906 for (; handler
!= NULL
; handler
= handler
->next
)
2909 PUT2 (handler
->start_label
->pc
);
2910 PUT2 (handler
->end_label
->pc
);
2911 PUT2 (handler
->handler_label
->pc
);
2912 if (handler
->type
== NULL_TREE
)
2915 type_index
= find_class_constant (&state
->cpool
,
2920 ptr
= append_chunk (NULL
, 2, state
);
2921 PUT2 (code_attributes_count
);
2923 /* Write the LineNumberTable attribute. */
2924 if (state
->linenumber_count
> 0)
2926 static tree LineNumberTable_node
= NULL_TREE
;
2927 ptr
= append_chunk (NULL
, 8 + 4 * state
->linenumber_count
, state
);
2928 if (LineNumberTable_node
== NULL_TREE
)
2929 LineNumberTable_node
= get_identifier ("LineNumberTable");
2930 i
= find_utf8_constant (&state
->cpool
, LineNumberTable_node
);
2931 PUT2 (i
); /* attribute_name_index */
2932 i
= 2+4*state
->linenumber_count
; PUT4(i
); /* attribute_length */
2933 i
= state
->linenumber_count
; PUT2 (i
);
2934 for (block
= state
->blocks
; block
!= NULL
; block
= block
->next
)
2936 int line
= block
->linenumber
;
2945 /* Write the LocalVariableTable attribute. */
2946 if (state
->lvar_count
> 0)
2948 static tree LocalVariableTable_node
= NULL_TREE
;
2949 struct localvar_info
*lvar
= state
->first_lvar
;
2950 ptr
= append_chunk (NULL
, 8 + 10 * state
->lvar_count
, state
);
2951 if (LocalVariableTable_node
== NULL_TREE
)
2952 LocalVariableTable_node
= get_identifier("LocalVariableTable");
2953 i
= find_utf8_constant (&state
->cpool
, LocalVariableTable_node
);
2954 PUT2 (i
); /* attribute_name_index */
2955 i
= 2 + 10 * state
->lvar_count
; PUT4 (i
); /* attribute_length */
2956 i
= state
->lvar_count
; PUT2 (i
);
2957 for ( ; lvar
!= NULL
; lvar
= lvar
->next
)
2959 tree name
= DECL_NAME (lvar
->decl
);
2960 tree sig
= build_java_signature (TREE_TYPE (lvar
->decl
));
2961 i
= lvar
->start_label
->pc
; PUT2 (i
);
2962 i
= lvar
->end_label
->pc
- i
; PUT2 (i
);
2963 i
= find_utf8_constant (&state
->cpool
, name
); PUT2 (i
);
2964 i
= find_utf8_constant (&state
->cpool
, sig
); PUT2 (i
);
2965 i
= DECL_LOCAL_INDEX (lvar
->decl
); PUT2 (i
);
2969 if (DECL_FUNCTION_THROWS (part
) != NULL_TREE
)
2971 tree t
= DECL_FUNCTION_THROWS (part
);
2972 int throws_count
= list_length (t
);
2973 static tree Exceptions_node
= NULL_TREE
;
2974 if (Exceptions_node
== NULL_TREE
)
2975 Exceptions_node
= get_identifier ("Exceptions");
2976 ptr
= append_chunk (NULL
, 8 + 2 * throws_count
, state
);
2977 i
= find_utf8_constant (&state
->cpool
, Exceptions_node
);
2978 PUT2 (i
); /* attribute_name_index */
2979 i
= 2 + 2 * throws_count
; PUT4(i
); /* attribute_length */
2980 i
= throws_count
; PUT2 (i
);
2981 for (; t
!= NULL_TREE
; t
= TREE_CHAIN (t
))
2983 i
= find_class_constant (&state
->cpool
, TREE_VALUE (t
));
2988 current_function_decl
= save_function
;
2990 ptr
= methods_count_ptr
; UNSAFE_PUT2 (methods_count
);
2992 source_file
= DECL_SOURCE_FILE (TYPE_NAME (clas
));
2993 for (ptr
= source_file
; ; ptr
++)
2998 if (ch
== '/' || ch
== '\\')
2999 source_file
= ptr
+1;
3001 ptr
= append_chunk (NULL
, 10, state
);
3002 PUT2 (1); /* attributes_count */
3004 /* generate the SourceFile attribute. */
3005 if (SourceFile_node
== NULL_TREE
)
3006 SourceFile_node
= get_identifier ("SourceFile");
3007 i
= find_utf8_constant (&state
->cpool
, SourceFile_node
);
3008 PUT2 (i
); /* attribute_name_index */
3010 i
= find_utf8_constant (&state
->cpool
, get_identifier (source_file
));
3013 /* New finally generate the contents of the constant pool chunk. */
3014 i
= count_constant_pool_bytes (&state
->cpool
);
3015 ptr
= obstack_alloc (state
->chunk_obstack
, i
);
3016 cpool_chunk
->data
= ptr
;
3017 cpool_chunk
->size
= i
;
3018 write_constant_pool (&state
->cpool
, ptr
, i
);
3019 return state
->first
;
3023 make_class_file_name (clas
)
3026 const char *dname
, *slash
;
3030 cname
= IDENTIFIER_POINTER (identifier_subst (DECL_NAME (TYPE_NAME (clas
)),
3031 "", '.', DIR_SEPARATOR
,
3033 if (jcf_write_base_directory
== NULL
)
3035 /* Make sure we put the class file into the .java file's
3036 directory, and not into some subdirectory thereof. */
3038 dname
= DECL_SOURCE_FILE (TYPE_NAME (clas
));
3039 slash
= strrchr (dname
, DIR_SEPARATOR
);
3045 t
= strrchr (cname
, DIR_SEPARATOR
);
3051 dname
= jcf_write_base_directory
;
3052 slash
= dname
+ strlen (dname
);
3055 r
= xmalloc (slash
- dname
+ strlen (cname
) + 2);
3056 strncpy (r
, dname
, slash
- dname
);
3057 r
[slash
- dname
] = DIR_SEPARATOR
;
3058 strcpy (&r
[slash
- dname
+ 1], cname
);
3060 /* We try to make new directories when we need them. We only do
3061 this for directories which "might not" exist. For instance, we
3062 assume the `-d' directory exists, but we don't assume that any
3063 subdirectory below it exists. It might be worthwhile to keep
3064 track of which directories we've created to avoid gratuitous
3066 dname
= r
+ (slash
- dname
) + 1;
3069 cname
= strchr (dname
, DIR_SEPARATOR
);
3073 if (stat (r
, &sb
) == -1)
3075 /* Try to make it. */
3076 if (mkdir (r
, 0755) == -1)
3078 fatal ("failed to create directory `%s'", r
);
3083 *cname
= DIR_SEPARATOR
;
3084 /* Skip consecutive separators. */
3085 for (dname
= cname
+ 1; *dname
&& *dname
== DIR_SEPARATOR
; ++dname
)
3092 /* Write out the contens of a class (RECORD_TYPE) CLAS, as a .class file.
3093 The output .class file name is make_class_file_name(CLAS). */
3096 write_classfile (clas
)
3099 struct obstack
*work
= &temporary_obstack
;
3100 struct jcf_partial state
[1];
3101 char *class_file_name
= make_class_file_name (clas
);
3102 struct chunk
*chunks
;
3104 if (class_file_name
!= NULL
)
3106 FILE* stream
= fopen (class_file_name
, "wb");
3108 fatal ("failed to open `%s' for writing", class_file_name
);
3109 jcf_dependency_add_target (class_file_name
);
3110 init_jcf_state (state
, work
);
3111 chunks
= generate_classfile (clas
, state
);
3112 write_chunks (stream
, chunks
);
3113 if (fclose (stream
))
3114 fatal ("failed to close after writing `%s'", class_file_name
);
3115 free (class_file_name
);
3117 release_jcf_state (state
);
3121 string concatenation
3122 synchronized statement