1 /* Output bytecodes for GNU C-compiler.
2 Copyright (C) 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
33 #include "bytetypes.h"
36 #include "bc-opcode.h"
37 #include "bc-typecd.h"
42 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. */
70 fprintf (file
, "%s", s
+ 1);
73 #ifdef NAMES_HAVE_UNDERSCORES
74 fprintf (file
, "_%s", s
);
76 fprintf (file
, "%s", s
);
81 /* Maintain a bucket hash table for symbol names. */
86 static struct bc_sym
*hashtab
[HASH_SIZE
];
92 unsigned int hash
= 0;
96 hash
= hash
<< 3 | hash
>> HASH_BITS
- 3;
100 return hash
% HASH_SIZE
;
104 /* Look up the named symbol, creating it if it doesn't exist. */
113 for (s
= hashtab
[i
]; s
; s
= s
->next
)
114 if (!strcmp (s
->name
, name
))
117 s
= (struct bc_sym
*) xmalloc (sizeof (struct bc_sym
));
118 s
->name
= xmalloc (strlen (name
) + 1);
119 strcpy (s
->name
, name
);
120 s
->defined
= s
->global
= s
->common
= 0;
122 s
->next
= hashtab
[i
];
128 /* Write out .globl and common symbols to the named file. */
136 for (i
= 0; i
< HASH_SIZE
; ++i
)
137 for (s
= hashtab
[i
]; s
; s
= s
->next
)
141 fprintf (file
, "\n\t.globl ");
142 prsym (file
, s
->name
);
146 fprintf (file
, "\n\t.comm ");
147 prsym (file
, s
->name
);
148 fprintf (file
, ", %lu\n", s
->val
);
153 fprintf (file
, "\n\t.lcomm ");
154 prsym (file
, s
->name
);
155 fprintf (file
, ", %lu\n", s
->val
);
163 /* Create and initialize a new segment. */
164 static struct bc_seg
*
167 struct bc_seg
*result
;
169 result
= (struct bc_seg
*) xmalloc (sizeof (struct bc_seg
));
171 result
->data
= xmalloc (result
->alloc
);
179 /* Advance the segment index to the next alignment boundary. */
185 unsigned int oldsize
= seg
->size
;
187 seg
->size
= seg
->size
+ (1 << log
) - 1 & ~((1 << log
) - 1);
188 if (seg
->size
> seg
->alloc
)
190 while (seg
->size
> seg
->alloc
)
192 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
194 bzero (seg
->data
+ oldsize
, seg
->size
- oldsize
);
198 /* Append the given data to the given segment. */
200 seg_data (seg
, data
, size
)
205 if (seg
->size
+ size
> seg
->alloc
)
207 while (seg
->size
+ size
> seg
->alloc
)
209 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
212 bcopy (data
, seg
->data
+ seg
->size
, size
);
217 /* Append a zero-filled skip to the given segment. */
223 if (seg
->size
+ size
> seg
->alloc
)
225 while (seg
->size
+ size
> seg
->alloc
)
227 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
230 memset (seg
->data
+ seg
->size
, 0, size
);
235 /* Define the given name as the current offset in the given segment. It
236 is an error if the name is already defined. Return 0 or 1 indicating
237 failure or success respectively. */
239 seg_defsym (seg
, name
)
244 struct bc_segsym
*segsym
;
246 sym
= sym_lookup (name
);
251 sym
->val
= seg
->size
;
252 segsym
= (struct bc_segsym
*) xmalloc (sizeof (struct bc_segsym
));
254 segsym
->next
= seg
->syms
;
260 /* Generate in seg's data a reference to the given sym, adjusted by
263 seg_refsym (seg
, name
, offset
)
269 struct bc_segreloc
*segreloc
;
271 sym
= sym_lookup (name
);
272 segreloc
= (struct bc_segreloc
*) xmalloc (sizeof (struct bc_segreloc
));
273 segreloc
->offset
= seg
->size
;
275 segreloc
->next
= seg
->relocs
;
276 seg
->relocs
= segreloc
;
277 seg_data (seg
, (char *) &offset
, sizeof offset
);
281 /* Concatenate the contents of given segments into the first argument. */
283 seg_concat (result
, seg
)
284 struct bc_seg
*result
, *seg
;
287 struct bc_segsym
*segsym
;
288 struct bc_segreloc
*segreloc
;
290 seg_align (result
, MACHINE_SEG_ALIGN
);
292 seg_data (result
, seg
->data
, seg
->size
);
295 /* Go through the symbols and relocs of SEG, adjusting their offsets
296 for their new location in RESULT. */
301 segsym
->sym
->val
+= fix
;
302 while (segsym
->next
&& (segsym
= segsym
->next
));
303 segsym
->next
= result
->syms
;
304 result
->syms
= seg
->syms
;
308 segreloc
= seg
->relocs
;
310 segreloc
->offset
+= fix
;
311 while (segreloc
->next
&& (segreloc
= segreloc
->next
));
312 segreloc
->next
= result
->relocs
;
313 result
->relocs
= seg
->relocs
;
319 /* Write a segment to a file. */
321 bc_seg_write (seg
, file
)
325 struct bc_segsym
*segsym
, *nsegsym
, *psegsym
;
326 struct bc_segreloc
*segreloc
, *nsegreloc
, *psegreloc
;
329 /* Reverse the list of symbols. */
330 for (psegsym
= 0, segsym
= seg
->syms
; segsym
; segsym
= nsegsym
)
332 nsegsym
= segsym
->next
;
333 segsym
->next
= psegsym
;
338 /* Reverse the list of relocs. */
339 for (psegreloc
= 0, segreloc
= seg
->relocs
; segreloc
; segreloc
= nsegreloc
)
341 nsegreloc
= segreloc
->next
;
342 segreloc
->next
= psegreloc
;
343 psegreloc
= segreloc
;
345 seg
->relocs
= psegreloc
;
347 /* Output each byte of the segment. */
348 for (i
= 0, segsym
= seg
->syms
, segreloc
= seg
->relocs
; i
< seg
->size
; ++i
)
350 while (segsym
&& segsym
->sym
->val
== i
)
355 BC_WRITE_SEGSYM (segsym
, file
);
356 segsym
= segsym
->next
;
359 if (segreloc
&& segreloc
->offset
== i
)
364 bcopy (seg
->data
+ i
, (char *) &offset
, sizeof (int));
365 i
+= sizeof (int) - 1;
367 BC_WRITE_RELOC_ENTRY (segreloc
, file
, offset
);
368 segreloc
= segreloc
->next
;
373 if (i
% 8 == 0 || flag
)
374 BC_START_BYTECODE_LINE (file
);
376 BC_WRITE_BYTECODE (i
% 8 == 0 || flag
? ' ' : ',',
385 /* Paranoia check--we should have visited all syms and relocs during
388 if (segsym
|| segreloc
)
394 /* Text and data segments of the object file in making. */
395 static struct bc_seg
*bc_text_seg
;
396 static struct bc_seg
*bc_data_seg
;
398 /* Called before anything else in this module. */
402 int min_class_size
[(int) MAX_MODE_CLASS
];
403 enum machine_mode mode
;
406 bc_init_mode_to_code_map ();
408 bc_text_seg
= seg_create ();
409 bc_data_seg
= seg_create ();
411 dconst0
= REAL_VALUE_ATOF ("0", DFmode
);
412 dconst1
= REAL_VALUE_ATOF ("1", DFmode
);
413 dconst2
= REAL_VALUE_ATOF ("2", DFmode
);
414 dconstm1
= REAL_VALUE_ATOF ("-1", DFmode
);
416 /* Find the narrowest mode for each class and compute the word and byte
419 for (i
= 0; i
< (int) MAX_MODE_CLASS
; i
++)
420 min_class_size
[i
] = 1000;
422 for (mode
= VOIDmode
; (int) mode
< (int) MAX_MACHINE_MODE
;
423 mode
= (enum machine_mode
) ((int) mode
+ 1))
425 if (GET_MODE_SIZE (mode
) < min_class_size
[(int) GET_MODE_CLASS (mode
)])
427 class_narrowest_mode
[(int) GET_MODE_CLASS (mode
)] = mode
;
428 min_class_size
[(int) GET_MODE_CLASS (mode
)] = GET_MODE_SIZE (mode
);
430 if (GET_MODE_CLASS (mode
) == MODE_INT
431 && GET_MODE_BITSIZE (mode
) == BITS_PER_UNIT
)
434 if (GET_MODE_CLASS (mode
) == MODE_INT
435 && GET_MODE_BITSIZE (mode
) == BITS_PER_WORD
)
441 /* External addresses referenced in a function. Rather than trying to
442 work relocatable address directly into bytecoded functions (which would
443 require us to provide hairy location info and possibly obey alignment
444 rules imposed by the architecture) we build an auxilary table of
445 pointer constants, and encode just offsets into this table into the
447 static struct bc_seg
*ptrconsts
;
449 /* Trampoline code for the function entry. */
450 struct bc_seg
*trampoline
;
452 /* Actual byte code of the function. */
453 struct bc_seg
*bytecode
;
455 /* List of labels defined in the function. */
456 struct bc_label
*labels
;
458 /* List of label references in the function. */
459 struct bc_labelref
*labelrefs
;
462 /* Add symbol to pointer table. Return offset into table where
463 pointer was stored. The offset usually goes into the bytecode
464 stream as a constP literal. */
466 bc_define_pointer (p
)
469 int offset
= ptrconsts
->size
;
471 seg_refsym (ptrconsts
, p
, 0);
476 /* Begin a bytecoded function. */
478 bc_begin_function (name
)
481 ptrconsts
= seg_create ();
482 trampoline
= seg_create ();
483 bytecode
= seg_create ();
484 return seg_defsym (trampoline
, name
);
488 /* Force alignment in inline bytecode. */
490 bc_align_bytecode (align
)
493 seg_align (bytecode
, align
);
497 /* Emit data inline into bytecode. */
499 bc_emit_bytecode_const (data
, size
)
504 seg_data (bytecode
, data
, size
);
508 /* Create a new "bytecode label", to have its value defined later.
509 Bytecode labels have nothing to do with the object file symbol table,
510 and are purely local to a given bytecoded function. */
512 bc_get_bytecode_label ()
514 struct bc_label
*result
;
516 result
= (struct bc_label
*) xmalloc (sizeof (struct bc_label
));
518 result
->next
= labels
;
525 /* Define the given label with the current location counter. */
527 bc_emit_bytecode_labeldef (label
)
528 struct bc_label
*label
;
530 extern int bc_new_uid ();
532 if (!label
|| label
->defined
)
535 label
->offset
= bytecode
->size
;
537 label
->uid
= bc_new_uid ();
539 #ifdef DEBUG_PRINT_CODE
540 fprintf (stderr
, "$%lx:\n", label
);
547 /* Generate a location-relative reference to the given bytecode label.
548 It need not be defined yet; label references will be backpatched later. */
550 bc_emit_bytecode_labelref (label
)
551 struct bc_label
*label
;
553 struct bc_labelref
*labelref
;
556 labelref
= (struct bc_labelref
*) xmalloc (sizeof (struct bc_labelref
));
557 labelref
->label
= label
;
558 labelref
->offset
= bytecode
->size
;
559 labelref
->next
= labelrefs
;
560 labelrefs
= labelref
;
562 #ifdef DEBUG_PRINT_CODE
563 fprintf (stderr
, " $%lx", label
);
566 seg_data (bytecode
, (char *) &zero
, sizeof zero
);
570 /* Emit a reference to an external address; generate the reference in the
571 ptrconst area, and emit an offset in the bytecode. */
573 bc_emit_code_labelref (name
, offset
)
579 ptroff
= ptrconsts
->size
/ sizeof (char *);
580 seg_data (bytecode
, (char *) &ptroff
, sizeof ptroff
);
581 seg_refsym (ptrconsts
, name
, offset
);
583 #ifdef DEBUG_PRINT_CODE
584 fprintf (stderr
, " [external <%x> %s]", ptroff
, name
);
589 /* Backpatch label references in the byte code, and concatenate the bytecode
590 and pointer constant segments to the cumulative text for the object file.
591 Return a label name for the pointer constants region. */
596 struct bc_label
*label
, *next
;
597 struct bc_labelref
*ref
, *nextref
;
598 char ptrconsts_label
[20];
601 /* Backpatch bytecode label references. */
602 for (ref
= labelrefs
; ref
; ref
= ref
->next
)
603 if (ref
->label
->defined
)
605 addr
= ref
->label
->offset
;
606 bcopy ((char *) &addr
, bytecode
->data
+ ref
->offset
, sizeof addr
);
609 /* Free the chains of labelrefs and labeldefs. */
610 for (ref
= labelrefs
; ref
; ref
= nextref
)
616 for (label
= labels
; label
; label
= next
)
619 free ((char *) label
);
622 seg_concat (trampoline
, bytecode
);
623 seg_align (trampoline
, MACHINE_SEG_ALIGN
);
624 sprintf (ptrconsts_label
, "*LP%d", nlab
++);
625 seg_defsym (trampoline
, ptrconsts_label
);
626 seg_concat (trampoline
, ptrconsts
);
627 seg_concat (bc_text_seg
, trampoline
);
635 return sym_lookup (ptrconsts_label
)->name
;
638 /* Force alignment in const data. */
640 bc_align_const (align
)
643 seg_align (bc_text_seg
, align
);
646 /* Emit const data. */
648 bc_emit_const (data
, size
)
652 seg_data (bc_text_seg
, data
, size
);
655 /* Emit a zero-filled constant skip. */
657 bc_emit_const_skip (size
)
660 seg_skip (bc_text_seg
, size
);
663 /* Emit a label definition in const data. */
665 bc_emit_const_labeldef (name
)
668 return seg_defsym (bc_text_seg
, name
);
671 /* Emit a label reference in const data. */
673 bc_emit_const_labelref (name
, offset
)
677 seg_refsym (bc_text_seg
, name
, offset
);
680 /* Force alignment in data. */
682 bc_align_data (align
)
685 seg_align (bc_data_seg
, align
);
690 bc_emit_data (data
, size
)
694 seg_data (bc_data_seg
, data
, size
);
697 /* Emit a zero-filled data skip. */
699 bc_emit_data_skip (size
)
702 seg_skip (bc_data_seg
, size
);
705 /* Emit label definition in data. */
707 bc_emit_data_labeldef (name
)
710 return seg_defsym (bc_data_seg
, name
);
713 /* Emit label reference in data. */
715 bc_emit_data_labelref (name
, offset
)
719 seg_refsym (bc_data_seg
, name
, offset
);
722 /* Emit a common block of the given name and size. Note that
723 when the .o file is actually written non-global "common"
724 blocks will have to be turned into space in the data section. */
726 bc_emit_common (name
, size
)
732 sym
= sym_lookup (name
);
742 /* Globalize the given label. */
744 bc_globalize_label (name
)
749 sym
= sym_lookup (name
);
753 static enum { in_text
, in_data
} section
= in_text
;
771 if (section
== in_text
)
772 bc_align_const (align
);
774 bc_align_data (align
);
782 if (section
== in_text
)
783 bc_emit_const (data
, size
);
785 bc_emit_data (data
, size
);
792 if (section
== in_text
)
793 bc_emit_const_skip (size
);
795 bc_emit_data_skip (size
);
799 bc_emit_labeldef (name
)
802 if (section
== in_text
)
803 return bc_emit_const_labeldef (name
);
805 return bc_emit_data_labeldef (name
);
809 bc_emit_labelref (name
, offset
)
813 if (section
== in_text
)
814 bc_emit_const_labelref (name
, offset
);
816 bc_emit_data_labelref (name
, offset
);
823 BC_WRITE_FILE (file
);
827 /* Allocate a new bytecode rtx.
828 If you supply a null BC_LABEL, we generate one. */
831 bc_gen_rtx (label
, offset
, bc_label
)
834 struct bc_label
*bc_label
;
839 bc_label
= (struct bc_label
*) xmalloc (sizeof (struct bc_label
));
841 r
= gen_rtx (CODE_LABEL
, VOIDmode
, label
, bc_label
);
842 bc_label
->offset
= offset
;
848 /* Print bytecode rtx */
854 #if 0 /* This needs to get fixed to really work again. */
855 /* BC_WRITE_RTL has a definition
856 that doesn't even make sense for this use. */
857 BC_WRITE_RTL (r
, fp
);
862 /* Emit a bytecode, keeping a running tally of the stack depth. */
864 bc_emit_bytecode (bytecode
)
865 enum bytecode_opcode bytecode
;
868 static int prev_lineno
= -1;
870 byte
= (char) bytecode
;
872 #ifdef BCDEBUG_PRINT_CODE
873 if (lineno
!= prev_lineno
)
875 fprintf (stderr
, "<line %d>\n", lineno
);
876 prev_lineno
= lineno
;
879 fputs (opcode_name
[(unsigned int) bytecode
], stderr
);
882 /* Due to errors we are often requested to output bytecodes that
883 will cause an interpreter stack undeflow when executed. Instead of
884 dumping core on such occasions, we omit the bytecode. Erroneous code
885 should not be executed, regardless. This makes life much easier, since
886 we don't have to deceive ourselves about the known stack depth. */
888 bc_emit_bytecode_const (&byte
, 1);
890 if ((stack_depth
-= arityvec
[(int) bytecode
].ninputs
) >= 0)
892 if ((stack_depth
+= arityvec
[(int) bytecode
].noutputs
) > max_stack_depth
)
893 max_stack_depth
= stack_depth
;
896 #ifdef VALIDATE_STACK_FOR_BC
897 VALIDATE_STACK_FOR_BC ();
902 #ifdef BCDEBUG_PRINT_CODE
903 #define PRLIT(TYPE, PTR) fprintf (stderr, " [%x]", *(TYPE *) PTR)
908 /* Emit a complete bytecode instruction, expecting the correct number
909 of literal values in the call. First argument is the instruction, the
910 remaining arguments are literals of size HOST_WIDE_INT or smaller. */
912 bc_emit_instruction
VPROTO((enum bytecode_opcode opcode
, ...))
915 enum bytecode_opcode opcode
;
918 int nliteral
, instruction
;
920 VA_START (arguments
, opcode
);
923 opcode
= va_arg (arguments
, enum bytecode_opcode
);
926 /* Emit instruction bytecode */
927 bc_emit_bytecode (opcode
);
928 instruction
= (int) opcode
;
930 /* Loop literals and emit as bytecode constants */
931 for (nliteral
= 0; nliteral
< arityvec
[instruction
].nliterals
; nliteral
++)
933 switch (arityvec
[instruction
].literals
[nliteral
])
935 /* This conditional is a kludge, but it's necessary
936 because TYPE might be long long. */
938 /* Expand definitions into case statements */
939 #define DEFTYPECODE(CODE, NAME, MODE, TYPE) \
942 TYPE temp = va_arg (arguments, TYPE); \
943 bc_emit_bytecode_const ((void *) &temp, sizeof temp); \
944 PRLIT (TYPE, &temp); } \
947 #include "bc-typecd.def"
950 #endif /* __GNUC__ */
957 #ifdef BCDEBUG_PRINT_CODE
958 fputc ('\n', stderr
);
962 /* Emit the machine-code interface trampoline at the beginning of a byte
963 coded function. The argument is a label name of the interpreter
964 bytecode callinfo structure; the return value is a label name for
965 the beginning of the actual bytecode. */
967 bc_emit_trampoline (callinfo
)
973 sprintf (mylab
, "*LB%d", n
++);
975 BC_EMIT_TRAMPOLINE (trampoline
, callinfo
);
977 seg_defsym (bytecode
, mylab
);
978 return sym_lookup (mylab
)->name
;
987 char *tmp
= xmalloc (strlen (str
) + 1);