1 /* Output bytecodes for GNU C-compiler.
2 Copyright (C) 1993, 1994, 1996 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.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
34 #include "bytetypes.h"
37 #include "bc-opcode.h"
38 #include "bc-typecd.h"
43 extern char *xmalloc (), *xrealloc ();
45 extern struct obstack
*rtl_obstack
;
47 /* Indexed by mode class, gives the narrowest mode for each class. */
49 extern enum machine_mode class_narrowest_mode
[(int) MAX_MODE_CLASS
];
51 /* Commonly used modes. */
52 /* Mode whose width is BITS_PER_UNIT */
53 extern enum machine_mode byte_mode
;
55 /* Mode whose width is BITS_PER_WORD */
56 extern enum machine_mode word_mode
;
58 /* Vector indexed by opcode giving info about the args for each opcode. */
59 static struct arityvec arityvec
[] = {
63 /* How to print a symbol name for the assembler. */
71 fprintf (file
, "%s", s
+ 1);
74 #ifdef NAMES_HAVE_UNDERSCORES
75 fprintf (file
, "_%s", s
);
77 fprintf (file
, "%s", s
);
82 /* Maintain a bucket hash table for symbol names. */
87 static struct bc_sym
*hashtab
[HASH_SIZE
];
93 unsigned int hash
= 0;
97 hash
= hash
<< 3 | hash
>> HASH_BITS
- 3;
101 return hash
% HASH_SIZE
;
105 /* Look up the named symbol, creating it if it doesn't exist. */
115 for (s
= hashtab
[i
]; s
; s
= s
->next
)
116 if (!strcmp (s
->name
, name
))
119 s
= (struct bc_sym
*) xmalloc (sizeof (struct bc_sym
));
120 s
->name
= xmalloc (strlen (name
) + 1);
121 strcpy (s
->name
, name
);
122 s
->defined
= s
->global
= s
->common
= 0;
124 s
->next
= hashtab
[i
];
130 /* Write out .globl and common symbols to the named file. */
139 for (i
= 0; i
< HASH_SIZE
; ++i
)
140 for (s
= hashtab
[i
]; s
; s
= s
->next
)
144 fprintf (file
, "\n\t.globl ");
145 prsym (file
, s
->name
);
149 fprintf (file
, "\n\t.comm ");
150 prsym (file
, s
->name
);
151 fprintf (file
, ", %lu\n", s
->val
);
156 fprintf (file
, "\n\t.lcomm ");
157 prsym (file
, s
->name
);
158 fprintf (file
, ", %lu\n", s
->val
);
166 /* Create and initialize a new segment. */
168 static struct bc_seg
*
171 struct bc_seg
*result
;
173 result
= (struct bc_seg
*) xmalloc (sizeof (struct bc_seg
));
175 result
->data
= xmalloc (result
->alloc
);
183 /* Advance the segment index to the next alignment boundary. */
190 unsigned int oldsize
= seg
->size
;
192 seg
->size
= seg
->size
+ (1 << log
) - 1 & ~((1 << log
) - 1);
193 if (seg
->size
> seg
->alloc
)
195 while (seg
->size
> seg
->alloc
)
197 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
199 bzero (seg
->data
+ oldsize
, seg
->size
- oldsize
);
203 /* Append the given data to the given segment. */
206 seg_data (seg
, data
, size
)
211 if (seg
->size
+ size
> seg
->alloc
)
213 while (seg
->size
+ size
> seg
->alloc
)
215 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
218 bcopy (data
, seg
->data
+ seg
->size
, size
);
223 /* Append a zero-filled skip to the given segment. */
230 if (seg
->size
+ size
> seg
->alloc
)
232 while (seg
->size
+ size
> seg
->alloc
)
234 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
237 memset (seg
->data
+ seg
->size
, 0, size
);
242 /* Define the given name as the current offset in the given segment. It
243 is an error if the name is already defined. Return 0 or 1 indicating
244 failure or success respectively. */
247 seg_defsym (seg
, name
)
252 struct bc_segsym
*segsym
;
254 sym
= sym_lookup (name
);
259 sym
->val
= seg
->size
;
260 segsym
= (struct bc_segsym
*) xmalloc (sizeof (struct bc_segsym
));
262 segsym
->next
= seg
->syms
;
268 /* Generate in seg's data a reference to the given sym, adjusted by
272 seg_refsym (seg
, name
, offset
)
278 struct bc_segreloc
*segreloc
;
280 sym
= sym_lookup (name
);
281 segreloc
= (struct bc_segreloc
*) xmalloc (sizeof (struct bc_segreloc
));
282 segreloc
->offset
= seg
->size
;
284 segreloc
->next
= seg
->relocs
;
285 seg
->relocs
= segreloc
;
286 seg_data (seg
, (char *) &offset
, sizeof offset
);
290 /* Concatenate the contents of given segments into the first argument. */
293 seg_concat (result
, seg
)
294 struct bc_seg
*result
, *seg
;
297 struct bc_segsym
*segsym
;
298 struct bc_segreloc
*segreloc
;
300 seg_align (result
, MACHINE_SEG_ALIGN
);
302 seg_data (result
, seg
->data
, seg
->size
);
305 /* Go through the symbols and relocs of SEG, adjusting their offsets
306 for their new location in RESULT. */
311 segsym
->sym
->val
+= fix
;
312 while (segsym
->next
&& (segsym
= segsym
->next
));
313 segsym
->next
= result
->syms
;
314 result
->syms
= seg
->syms
;
318 segreloc
= seg
->relocs
;
320 segreloc
->offset
+= fix
;
321 while (segreloc
->next
&& (segreloc
= segreloc
->next
));
322 segreloc
->next
= result
->relocs
;
323 result
->relocs
= seg
->relocs
;
329 /* Write a segment to a file. */
332 bc_seg_write (seg
, file
)
336 struct bc_segsym
*segsym
, *nsegsym
, *psegsym
;
337 struct bc_segreloc
*segreloc
, *nsegreloc
, *psegreloc
;
340 /* Reverse the list of symbols. */
341 for (psegsym
= 0, segsym
= seg
->syms
; segsym
; segsym
= nsegsym
)
343 nsegsym
= segsym
->next
;
344 segsym
->next
= psegsym
;
349 /* Reverse the list of relocs. */
350 for (psegreloc
= 0, segreloc
= seg
->relocs
; segreloc
; segreloc
= nsegreloc
)
352 nsegreloc
= segreloc
->next
;
353 segreloc
->next
= psegreloc
;
354 psegreloc
= segreloc
;
356 seg
->relocs
= psegreloc
;
358 /* Output each byte of the segment. */
359 for (i
= 0, segsym
= seg
->syms
, segreloc
= seg
->relocs
; i
< seg
->size
; ++i
)
361 while (segsym
&& segsym
->sym
->val
== i
)
366 BC_WRITE_SEGSYM (segsym
, file
);
367 segsym
= segsym
->next
;
370 if (segreloc
&& segreloc
->offset
== i
)
375 bcopy (seg
->data
+ i
, (char *) &offset
, sizeof (int));
376 i
+= sizeof (int) - 1;
378 BC_WRITE_RELOC_ENTRY (segreloc
, file
, offset
);
379 segreloc
= segreloc
->next
;
384 if (i
% 8 == 0 || flag
)
385 BC_START_BYTECODE_LINE (file
);
387 BC_WRITE_BYTECODE (i
% 8 == 0 || flag
? ' ' : ',',
396 /* Paranoia check--we should have visited all syms and relocs during
399 if (segsym
|| segreloc
)
405 /* Text and data segments of the object file in making. */
406 static struct bc_seg
*bc_text_seg
;
407 static struct bc_seg
*bc_data_seg
;
409 /* Called before anything else in this module. */
414 int min_class_size
[(int) MAX_MODE_CLASS
];
415 enum machine_mode mode
;
418 bc_init_mode_to_code_map ();
420 bc_text_seg
= seg_create ();
421 bc_data_seg
= seg_create ();
423 dconst0
= REAL_VALUE_ATOF ("0", DFmode
);
424 dconst1
= REAL_VALUE_ATOF ("1", DFmode
);
425 dconst2
= REAL_VALUE_ATOF ("2", DFmode
);
426 dconstm1
= REAL_VALUE_ATOF ("-1", DFmode
);
428 /* Find the narrowest mode for each class and compute the word and byte
431 for (i
= 0; i
< (int) MAX_MODE_CLASS
; i
++)
432 min_class_size
[i
] = 1000;
434 for (mode
= VOIDmode
; (int) mode
< (int) MAX_MACHINE_MODE
;
435 mode
= (enum machine_mode
) ((int) mode
+ 1))
437 if (GET_MODE_SIZE (mode
) < min_class_size
[(int) GET_MODE_CLASS (mode
)])
439 class_narrowest_mode
[(int) GET_MODE_CLASS (mode
)] = mode
;
440 min_class_size
[(int) GET_MODE_CLASS (mode
)] = GET_MODE_SIZE (mode
);
442 if (GET_MODE_CLASS (mode
) == MODE_INT
443 && GET_MODE_BITSIZE (mode
) == BITS_PER_UNIT
)
446 if (GET_MODE_CLASS (mode
) == MODE_INT
447 && GET_MODE_BITSIZE (mode
) == BITS_PER_WORD
)
453 /* External addresses referenced in a function. Rather than trying to
454 work relocatable address directly into bytecoded functions (which would
455 require us to provide hairy location info and possibly obey alignment
456 rules imposed by the architecture) we build an auxiliary table of
457 pointer constants, and encode just offsets into this table into the
459 static struct bc_seg
*ptrconsts
;
461 /* Trampoline code for the function entry. */
462 struct bc_seg
*trampoline
;
464 /* Actual byte code of the function. */
465 struct bc_seg
*bytecode
;
467 /* List of labels defined in the function. */
468 struct bc_label
*labels
;
470 /* List of label references in the function. */
471 struct bc_labelref
*labelrefs
;
474 /* Add symbol to pointer table. Return offset into table where
475 pointer was stored. The offset usually goes into the bytecode
476 stream as a constP literal. */
479 bc_define_pointer (p
)
482 int offset
= ptrconsts
->size
;
484 seg_refsym (ptrconsts
, p
, 0);
489 /* Begin a bytecoded function. */
492 bc_begin_function (name
)
495 ptrconsts
= seg_create ();
496 trampoline
= seg_create ();
497 bytecode
= seg_create ();
498 return seg_defsym (trampoline
, name
);
502 /* Force alignment in inline bytecode. */
505 bc_align_bytecode (align
)
508 seg_align (bytecode
, align
);
512 /* Emit data inline into bytecode. */
515 bc_emit_bytecode_const (data
, size
)
520 seg_data (bytecode
, data
, size
);
524 /* Create a new "bytecode label", to have its value defined later.
525 Bytecode labels have nothing to do with the object file symbol table,
526 and are purely local to a given bytecoded function. */
529 bc_get_bytecode_label ()
531 struct bc_label
*result
;
533 result
= (struct bc_label
*) xmalloc (sizeof (struct bc_label
));
535 result
->next
= labels
;
542 /* Define the given label with the current location counter. */
545 bc_emit_bytecode_labeldef (label
)
546 struct bc_label
*label
;
548 extern int bc_new_uid ();
550 if (!label
|| label
->defined
)
553 label
->offset
= bytecode
->size
;
555 label
->uid
= bc_new_uid ();
557 #ifdef DEBUG_PRINT_CODE
558 fprintf (stderr
, "$%lx:\n", label
);
565 /* Generate a location-relative reference to the given bytecode label.
566 It need not be defined yet; label references will be backpatched later. */
569 bc_emit_bytecode_labelref (label
)
570 struct bc_label
*label
;
572 struct bc_labelref
*labelref
;
575 labelref
= (struct bc_labelref
*) xmalloc (sizeof (struct bc_labelref
));
576 labelref
->label
= label
;
577 labelref
->offset
= bytecode
->size
;
578 labelref
->next
= labelrefs
;
579 labelrefs
= labelref
;
581 #ifdef DEBUG_PRINT_CODE
582 fprintf (stderr
, " $%lx", label
);
585 seg_data (bytecode
, (char *) &zero
, sizeof zero
);
589 /* Emit a reference to an external address; generate the reference in the
590 ptrconst area, and emit an offset in the bytecode. */
593 bc_emit_code_labelref (name
, offset
)
599 ptroff
= ptrconsts
->size
/ sizeof (char *);
600 seg_data (bytecode
, (char *) &ptroff
, sizeof ptroff
);
601 seg_refsym (ptrconsts
, name
, offset
);
603 #ifdef DEBUG_PRINT_CODE
604 fprintf (stderr
, " [external <%x> %s]", ptroff
, name
);
609 /* Backpatch label references in the byte code, and concatenate the bytecode
610 and pointer constant segments to the cumulative text for the object file.
611 Return a label name for the pointer constants region. */
617 struct bc_label
*label
, *next
;
618 struct bc_labelref
*ref
, *nextref
;
619 char ptrconsts_label
[20];
622 /* Backpatch bytecode label references. */
623 for (ref
= labelrefs
; ref
; ref
= ref
->next
)
624 if (ref
->label
->defined
)
626 addr
= ref
->label
->offset
;
627 bcopy ((char *) &addr
, bytecode
->data
+ ref
->offset
, sizeof addr
);
630 /* Free the chains of labelrefs and labeldefs. */
631 for (ref
= labelrefs
; ref
; ref
= nextref
)
637 for (label
= labels
; label
; label
= next
)
640 free ((char *) label
);
643 seg_concat (trampoline
, bytecode
);
644 seg_align (trampoline
, MACHINE_SEG_ALIGN
);
645 sprintf (ptrconsts_label
, "*LP%d", nlab
++);
646 seg_defsym (trampoline
, ptrconsts_label
);
647 seg_concat (trampoline
, ptrconsts
);
648 seg_concat (bc_text_seg
, trampoline
);
656 return sym_lookup (ptrconsts_label
)->name
;
659 /* Force alignment in const data. */
662 bc_align_const (align
)
665 seg_align (bc_text_seg
, align
);
668 /* Emit const data. */
671 bc_emit_const (data
, size
)
675 seg_data (bc_text_seg
, data
, size
);
678 /* Emit a zero-filled constant skip. */
681 bc_emit_const_skip (size
)
684 seg_skip (bc_text_seg
, size
);
687 /* Emit a label definition in const data. */
690 bc_emit_const_labeldef (name
)
693 return seg_defsym (bc_text_seg
, name
);
696 /* Emit a label reference in const data. */
699 bc_emit_const_labelref (name
, offset
)
703 seg_refsym (bc_text_seg
, name
, offset
);
706 /* Force alignment in data. */
709 bc_align_data (align
)
712 seg_align (bc_data_seg
, align
);
718 bc_emit_data (data
, size
)
722 seg_data (bc_data_seg
, data
, size
);
725 /* Emit a zero-filled data skip. */
728 bc_emit_data_skip (size
)
731 seg_skip (bc_data_seg
, size
);
734 /* Emit label definition in data. */
737 bc_emit_data_labeldef (name
)
740 return seg_defsym (bc_data_seg
, name
);
743 /* Emit label reference in data. */
746 bc_emit_data_labelref (name
, offset
)
750 seg_refsym (bc_data_seg
, name
, offset
);
753 /* Emit a common block of the given name and size. Note that
754 when the .o file is actually written non-global "common"
755 blocks will have to be turned into space in the data section. */
758 bc_emit_common (name
, size
)
764 sym
= sym_lookup (name
);
774 /* Globalize the given label. */
777 bc_globalize_label (name
)
782 sym
= sym_lookup (name
);
786 static enum { in_text
, in_data
} section
= in_text
;
804 if (section
== in_text
)
805 bc_align_const (align
);
807 bc_align_data (align
);
815 if (section
== in_text
)
816 bc_emit_const (data
, size
);
818 bc_emit_data (data
, size
);
825 if (section
== in_text
)
826 bc_emit_const_skip (size
);
828 bc_emit_data_skip (size
);
832 bc_emit_labeldef (name
)
835 if (section
== in_text
)
836 return bc_emit_const_labeldef (name
);
838 return bc_emit_data_labeldef (name
);
842 bc_emit_labelref (name
, offset
)
846 if (section
== in_text
)
847 bc_emit_const_labelref (name
, offset
);
849 bc_emit_data_labelref (name
, offset
);
856 BC_WRITE_FILE (file
);
860 /* Allocate a new bytecode rtx.
861 If you supply a null BC_LABEL, we generate one. */
864 bc_gen_rtx (label
, offset
, bc_label
)
867 struct bc_label
*bc_label
;
872 bc_label
= (struct bc_label
*) xmalloc (sizeof (struct bc_label
));
874 r
= gen_rtx (CODE_LABEL
, VOIDmode
, label
, bc_label
);
875 bc_label
->offset
= offset
;
881 /* Print bytecode rtx */
888 #if 0 /* This needs to get fixed to really work again. */
889 /* BC_WRITE_RTL has a definition
890 that doesn't even make sense for this use. */
891 BC_WRITE_RTL (r
, fp
);
896 /* Emit a bytecode, keeping a running tally of the stack depth. */
899 bc_emit_bytecode (bytecode
)
900 enum bytecode_opcode bytecode
;
903 static int prev_lineno
= -1;
905 byte
= (char) bytecode
;
907 #ifdef BCDEBUG_PRINT_CODE
908 if (lineno
!= prev_lineno
)
910 fprintf (stderr
, "<line %d>\n", lineno
);
911 prev_lineno
= lineno
;
914 fputs (opcode_name
[(unsigned int) bytecode
], stderr
);
917 /* Due to errors we are often requested to output bytecodes that
918 will cause an interpreter stack undeflow when executed. Instead of
919 dumping core on such occasions, we omit the bytecode. Erroneous code
920 should not be executed, regardless. This makes life much easier, since
921 we don't have to deceive ourselves about the known stack depth. */
923 bc_emit_bytecode_const (&byte
, 1);
925 if ((stack_depth
-= arityvec
[(int) bytecode
].ninputs
) >= 0)
927 if ((stack_depth
+= arityvec
[(int) bytecode
].noutputs
) > max_stack_depth
)
928 max_stack_depth
= stack_depth
;
931 #ifdef VALIDATE_STACK_FOR_BC
932 VALIDATE_STACK_FOR_BC ();
937 #ifdef BCDEBUG_PRINT_CODE
938 #define PRLIT(TYPE, PTR) fprintf (stderr, " [%x]", *(TYPE *) PTR)
943 /* Emit a complete bytecode instruction, expecting the correct number
944 of literal values in the call. First argument is the instruction, the
945 remaining arguments are literals of size HOST_WIDE_INT or smaller. */
948 bc_emit_instruction
VPROTO((enum bytecode_opcode opcode
, ...))
951 enum bytecode_opcode opcode
;
954 int nliteral
, instruction
;
956 VA_START (arguments
, opcode
);
959 opcode
= va_arg (arguments
, enum bytecode_opcode
);
962 /* Emit instruction bytecode */
963 bc_emit_bytecode (opcode
);
964 instruction
= (int) opcode
;
966 /* Loop literals and emit as bytecode constants */
967 for (nliteral
= 0; nliteral
< arityvec
[instruction
].nliterals
; nliteral
++)
969 switch (arityvec
[instruction
].literals
[nliteral
])
971 /* This conditional is a kludge, but it's necessary
972 because TYPE might be long long. */
974 /* Expand definitions into case statements */
975 #define DEFTYPECODE(CODE, NAME, MODE, TYPE) \
978 TYPE temp = va_arg (arguments, TYPE); \
979 bc_emit_bytecode_const ((void *) &temp, sizeof temp); \
980 PRLIT (TYPE, &temp); } \
983 #include "bc-typecd.def"
986 #endif /* __GNUC__ */
995 #ifdef BCDEBUG_PRINT_CODE
996 fputc ('\n', stderr
);
1000 /* Emit the machine-code interface trampoline at the beginning of a byte
1001 coded function. The argument is a label name of the interpreter
1002 bytecode callinfo structure; the return value is a label name for
1003 the beginning of the actual bytecode. */
1006 bc_emit_trampoline (callinfo
)
1012 sprintf (mylab
, "*LB%d", n
++);
1014 BC_EMIT_TRAMPOLINE (trampoline
, callinfo
);
1016 seg_defsym (bytecode
, mylab
);
1017 return sym_lookup (mylab
)->name
;