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, 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 ();
46 extern struct obstack
*rtl_obstack
;
48 /* Indexed by mode class, gives the narrowest mode for each class. */
50 extern enum machine_mode class_narrowest_mode
[(int) MAX_MODE_CLASS
];
52 /* Commonly used modes. */
53 /* Mode whose width is BITS_PER_UNIT */
54 extern enum machine_mode byte_mode
;
56 /* Mode whose width is BITS_PER_WORD */
57 extern enum machine_mode word_mode
;
59 /* Vector indexed by opcode giving info about the args for each opcode. */
60 static struct arityvec arityvec
[] = {
64 /* 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. */
114 for (s
= hashtab
[i
]; s
; s
= s
->next
)
115 if (!strcmp (s
->name
, name
))
118 s
= (struct bc_sym
*) xmalloc (sizeof (struct bc_sym
));
119 s
->name
= xmalloc (strlen (name
) + 1);
120 strcpy (s
->name
, name
);
121 s
->defined
= s
->global
= s
->common
= 0;
123 s
->next
= hashtab
[i
];
129 /* Write out .globl and common symbols to the named file. */
137 for (i
= 0; i
< HASH_SIZE
; ++i
)
138 for (s
= hashtab
[i
]; s
; s
= s
->next
)
142 fprintf (file
, "\n\t.globl ");
143 prsym (file
, s
->name
);
147 fprintf (file
, "\n\t.comm ");
148 prsym (file
, s
->name
);
149 fprintf (file
, ", %lu\n", s
->val
);
154 fprintf (file
, "\n\t.lcomm ");
155 prsym (file
, s
->name
);
156 fprintf (file
, ", %lu\n", s
->val
);
164 /* Create and initialize a new segment. */
165 static struct bc_seg
*
168 struct bc_seg
*result
;
170 result
= (struct bc_seg
*) xmalloc (sizeof (struct bc_seg
));
172 result
->data
= xmalloc (result
->alloc
);
180 /* Advance the segment index to the next alignment boundary. */
186 unsigned int oldsize
= seg
->size
;
188 seg
->size
= seg
->size
+ (1 << log
) - 1 & ~((1 << log
) - 1);
189 if (seg
->size
> seg
->alloc
)
191 while (seg
->size
> seg
->alloc
)
193 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
195 bzero (seg
->data
+ oldsize
, seg
->size
- oldsize
);
199 /* Append the given data to the given segment. */
201 seg_data (seg
, data
, size
)
206 if (seg
->size
+ size
> seg
->alloc
)
208 while (seg
->size
+ size
> seg
->alloc
)
210 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
213 bcopy (data
, seg
->data
+ seg
->size
, size
);
218 /* Append a zero-filled skip to the given segment. */
224 if (seg
->size
+ size
> seg
->alloc
)
226 while (seg
->size
+ size
> seg
->alloc
)
228 seg
->data
= xrealloc (seg
->data
, seg
->alloc
);
231 memset (seg
->data
+ seg
->size
, 0, size
);
236 /* Define the given name as the current offset in the given segment. It
237 is an error if the name is already defined. Return 0 or 1 indicating
238 failure or success respectively. */
240 seg_defsym (seg
, name
)
245 struct bc_segsym
*segsym
;
247 sym
= sym_lookup (name
);
252 sym
->val
= seg
->size
;
253 segsym
= (struct bc_segsym
*) xmalloc (sizeof (struct bc_segsym
));
255 segsym
->next
= seg
->syms
;
261 /* Generate in seg's data a reference to the given sym, adjusted by
264 seg_refsym (seg
, name
, offset
)
270 struct bc_segreloc
*segreloc
;
272 sym
= sym_lookup (name
);
273 segreloc
= (struct bc_segreloc
*) xmalloc (sizeof (struct bc_segreloc
));
274 segreloc
->offset
= seg
->size
;
276 segreloc
->next
= seg
->relocs
;
277 seg
->relocs
= segreloc
;
278 seg_data (seg
, (char *) &offset
, sizeof offset
);
282 /* Concatenate the contents of given segments into the first argument. */
284 seg_concat (result
, seg
)
285 struct bc_seg
*result
, *seg
;
288 struct bc_segsym
*segsym
;
289 struct bc_segreloc
*segreloc
;
291 seg_align (result
, MACHINE_SEG_ALIGN
);
293 seg_data (result
, seg
->data
, seg
->size
);
296 /* Go through the symbols and relocs of SEG, adjusting their offsets
297 for their new location in RESULT. */
302 segsym
->sym
->val
+= fix
;
303 while (segsym
->next
&& (segsym
= segsym
->next
));
304 segsym
->next
= result
->syms
;
305 result
->syms
= seg
->syms
;
309 segreloc
= seg
->relocs
;
311 segreloc
->offset
+= fix
;
312 while (segreloc
->next
&& (segreloc
= segreloc
->next
));
313 segreloc
->next
= result
->relocs
;
314 result
->relocs
= seg
->relocs
;
320 /* Write a segment to a file. */
322 bc_seg_write (seg
, file
)
326 struct bc_segsym
*segsym
, *nsegsym
, *psegsym
;
327 struct bc_segreloc
*segreloc
, *nsegreloc
, *psegreloc
;
330 /* Reverse the list of symbols. */
331 for (psegsym
= 0, segsym
= seg
->syms
; segsym
; segsym
= nsegsym
)
333 nsegsym
= segsym
->next
;
334 segsym
->next
= psegsym
;
339 /* Reverse the list of relocs. */
340 for (psegreloc
= 0, segreloc
= seg
->relocs
; segreloc
; segreloc
= nsegreloc
)
342 nsegreloc
= segreloc
->next
;
343 segreloc
->next
= psegreloc
;
344 psegreloc
= segreloc
;
346 seg
->relocs
= psegreloc
;
348 /* Output each byte of the segment. */
349 for (i
= 0, segsym
= seg
->syms
, segreloc
= seg
->relocs
; i
< seg
->size
; ++i
)
351 while (segsym
&& segsym
->sym
->val
== i
)
356 BC_WRITE_SEGSYM (segsym
, file
);
357 segsym
= segsym
->next
;
360 if (segreloc
&& segreloc
->offset
== i
)
365 bcopy (seg
->data
+ i
, (char *) &offset
, sizeof (int));
366 i
+= sizeof (int) - 1;
368 BC_WRITE_RELOC_ENTRY (segreloc
, file
, offset
);
369 segreloc
= segreloc
->next
;
374 if (i
% 8 == 0 || flag
)
375 BC_START_BYTECODE_LINE (file
);
377 BC_WRITE_BYTECODE (i
% 8 == 0 || flag
? ' ' : ',',
386 /* Paranoia check--we should have visited all syms and relocs during
389 if (segsym
|| segreloc
)
395 /* Text and data segments of the object file in making. */
396 static struct bc_seg
*bc_text_seg
;
397 static struct bc_seg
*bc_data_seg
;
399 /* Called before anything else in this module. */
403 int min_class_size
[(int) MAX_MODE_CLASS
];
404 enum machine_mode mode
;
407 bc_init_mode_to_code_map ();
409 bc_text_seg
= seg_create ();
410 bc_data_seg
= seg_create ();
412 dconst0
= REAL_VALUE_ATOF ("0", DFmode
);
413 dconst1
= REAL_VALUE_ATOF ("1", DFmode
);
414 dconst2
= REAL_VALUE_ATOF ("2", DFmode
);
415 dconstm1
= REAL_VALUE_ATOF ("-1", DFmode
);
417 /* Find the narrowest mode for each class and compute the word and byte
420 for (i
= 0; i
< (int) MAX_MODE_CLASS
; i
++)
421 min_class_size
[i
] = 1000;
423 for (mode
= VOIDmode
; (int) mode
< (int) MAX_MACHINE_MODE
;
424 mode
= (enum machine_mode
) ((int) mode
+ 1))
426 if (GET_MODE_SIZE (mode
) < min_class_size
[(int) GET_MODE_CLASS (mode
)])
428 class_narrowest_mode
[(int) GET_MODE_CLASS (mode
)] = mode
;
429 min_class_size
[(int) GET_MODE_CLASS (mode
)] = GET_MODE_SIZE (mode
);
431 if (GET_MODE_CLASS (mode
) == MODE_INT
432 && GET_MODE_BITSIZE (mode
) == BITS_PER_UNIT
)
435 if (GET_MODE_CLASS (mode
) == MODE_INT
436 && GET_MODE_BITSIZE (mode
) == BITS_PER_WORD
)
442 /* External addresses referenced in a function. Rather than trying to
443 work relocatable address directly into bytecoded functions (which would
444 require us to provide hairy location info and possibly obey alignment
445 rules imposed by the architecture) we build an auxiliary table of
446 pointer constants, and encode just offsets into this table into the
448 static struct bc_seg
*ptrconsts
;
450 /* Trampoline code for the function entry. */
451 struct bc_seg
*trampoline
;
453 /* Actual byte code of the function. */
454 struct bc_seg
*bytecode
;
456 /* List of labels defined in the function. */
457 struct bc_label
*labels
;
459 /* List of label references in the function. */
460 struct bc_labelref
*labelrefs
;
463 /* Add symbol to pointer table. Return offset into table where
464 pointer was stored. The offset usually goes into the bytecode
465 stream as a constP literal. */
467 bc_define_pointer (p
)
470 int offset
= ptrconsts
->size
;
472 seg_refsym (ptrconsts
, p
, 0);
477 /* Begin a bytecoded function. */
479 bc_begin_function (name
)
482 ptrconsts
= seg_create ();
483 trampoline
= seg_create ();
484 bytecode
= seg_create ();
485 return seg_defsym (trampoline
, name
);
489 /* Force alignment in inline bytecode. */
491 bc_align_bytecode (align
)
494 seg_align (bytecode
, align
);
498 /* Emit data inline into bytecode. */
500 bc_emit_bytecode_const (data
, size
)
505 seg_data (bytecode
, data
, size
);
509 /* Create a new "bytecode label", to have its value defined later.
510 Bytecode labels have nothing to do with the object file symbol table,
511 and are purely local to a given bytecoded function. */
513 bc_get_bytecode_label ()
515 struct bc_label
*result
;
517 result
= (struct bc_label
*) xmalloc (sizeof (struct bc_label
));
519 result
->next
= labels
;
526 /* Define the given label with the current location counter. */
528 bc_emit_bytecode_labeldef (label
)
529 struct bc_label
*label
;
531 extern int bc_new_uid ();
533 if (!label
|| label
->defined
)
536 label
->offset
= bytecode
->size
;
538 label
->uid
= bc_new_uid ();
540 #ifdef DEBUG_PRINT_CODE
541 fprintf (stderr
, "$%lx:\n", label
);
548 /* Generate a location-relative reference to the given bytecode label.
549 It need not be defined yet; label references will be backpatched later. */
551 bc_emit_bytecode_labelref (label
)
552 struct bc_label
*label
;
554 struct bc_labelref
*labelref
;
557 labelref
= (struct bc_labelref
*) xmalloc (sizeof (struct bc_labelref
));
558 labelref
->label
= label
;
559 labelref
->offset
= bytecode
->size
;
560 labelref
->next
= labelrefs
;
561 labelrefs
= labelref
;
563 #ifdef DEBUG_PRINT_CODE
564 fprintf (stderr
, " $%lx", label
);
567 seg_data (bytecode
, (char *) &zero
, sizeof zero
);
571 /* Emit a reference to an external address; generate the reference in the
572 ptrconst area, and emit an offset in the bytecode. */
574 bc_emit_code_labelref (name
, offset
)
580 ptroff
= ptrconsts
->size
/ sizeof (char *);
581 seg_data (bytecode
, (char *) &ptroff
, sizeof ptroff
);
582 seg_refsym (ptrconsts
, name
, offset
);
584 #ifdef DEBUG_PRINT_CODE
585 fprintf (stderr
, " [external <%x> %s]", ptroff
, name
);
590 /* Backpatch label references in the byte code, and concatenate the bytecode
591 and pointer constant segments to the cumulative text for the object file.
592 Return a label name for the pointer constants region. */
597 struct bc_label
*label
, *next
;
598 struct bc_labelref
*ref
, *nextref
;
599 char ptrconsts_label
[20];
602 /* Backpatch bytecode label references. */
603 for (ref
= labelrefs
; ref
; ref
= ref
->next
)
604 if (ref
->label
->defined
)
606 addr
= ref
->label
->offset
;
607 bcopy ((char *) &addr
, bytecode
->data
+ ref
->offset
, sizeof addr
);
610 /* Free the chains of labelrefs and labeldefs. */
611 for (ref
= labelrefs
; ref
; ref
= nextref
)
617 for (label
= labels
; label
; label
= next
)
620 free ((char *) label
);
623 seg_concat (trampoline
, bytecode
);
624 seg_align (trampoline
, MACHINE_SEG_ALIGN
);
625 sprintf (ptrconsts_label
, "*LP%d", nlab
++);
626 seg_defsym (trampoline
, ptrconsts_label
);
627 seg_concat (trampoline
, ptrconsts
);
628 seg_concat (bc_text_seg
, trampoline
);
636 return sym_lookup (ptrconsts_label
)->name
;
639 /* Force alignment in const data. */
641 bc_align_const (align
)
644 seg_align (bc_text_seg
, align
);
647 /* Emit const data. */
649 bc_emit_const (data
, size
)
653 seg_data (bc_text_seg
, data
, size
);
656 /* Emit a zero-filled constant skip. */
658 bc_emit_const_skip (size
)
661 seg_skip (bc_text_seg
, size
);
664 /* Emit a label definition in const data. */
666 bc_emit_const_labeldef (name
)
669 return seg_defsym (bc_text_seg
, name
);
672 /* Emit a label reference in const data. */
674 bc_emit_const_labelref (name
, offset
)
678 seg_refsym (bc_text_seg
, name
, offset
);
681 /* Force alignment in data. */
683 bc_align_data (align
)
686 seg_align (bc_data_seg
, align
);
691 bc_emit_data (data
, size
)
695 seg_data (bc_data_seg
, data
, size
);
698 /* Emit a zero-filled data skip. */
700 bc_emit_data_skip (size
)
703 seg_skip (bc_data_seg
, size
);
706 /* Emit label definition in data. */
708 bc_emit_data_labeldef (name
)
711 return seg_defsym (bc_data_seg
, name
);
714 /* Emit label reference in data. */
716 bc_emit_data_labelref (name
, offset
)
720 seg_refsym (bc_data_seg
, name
, offset
);
723 /* Emit a common block of the given name and size. Note that
724 when the .o file is actually written non-global "common"
725 blocks will have to be turned into space in the data section. */
727 bc_emit_common (name
, size
)
733 sym
= sym_lookup (name
);
743 /* Globalize the given label. */
745 bc_globalize_label (name
)
750 sym
= sym_lookup (name
);
754 static enum { in_text
, in_data
} section
= in_text
;
772 if (section
== in_text
)
773 bc_align_const (align
);
775 bc_align_data (align
);
783 if (section
== in_text
)
784 bc_emit_const (data
, size
);
786 bc_emit_data (data
, size
);
793 if (section
== in_text
)
794 bc_emit_const_skip (size
);
796 bc_emit_data_skip (size
);
800 bc_emit_labeldef (name
)
803 if (section
== in_text
)
804 return bc_emit_const_labeldef (name
);
806 return bc_emit_data_labeldef (name
);
810 bc_emit_labelref (name
, offset
)
814 if (section
== in_text
)
815 bc_emit_const_labelref (name
, offset
);
817 bc_emit_data_labelref (name
, offset
);
824 BC_WRITE_FILE (file
);
828 /* Allocate a new bytecode rtx.
829 If you supply a null BC_LABEL, we generate one. */
832 bc_gen_rtx (label
, offset
, bc_label
)
835 struct bc_label
*bc_label
;
840 bc_label
= (struct bc_label
*) xmalloc (sizeof (struct bc_label
));
842 r
= gen_rtx (CODE_LABEL
, VOIDmode
, label
, bc_label
);
843 bc_label
->offset
= offset
;
849 /* Print bytecode rtx */
855 #if 0 /* This needs to get fixed to really work again. */
856 /* BC_WRITE_RTL has a definition
857 that doesn't even make sense for this use. */
858 BC_WRITE_RTL (r
, fp
);
863 /* Emit a bytecode, keeping a running tally of the stack depth. */
865 bc_emit_bytecode (bytecode
)
866 enum bytecode_opcode bytecode
;
869 static int prev_lineno
= -1;
871 byte
= (char) bytecode
;
873 #ifdef BCDEBUG_PRINT_CODE
874 if (lineno
!= prev_lineno
)
876 fprintf (stderr
, "<line %d>\n", lineno
);
877 prev_lineno
= lineno
;
880 fputs (opcode_name
[(unsigned int) bytecode
], stderr
);
883 /* Due to errors we are often requested to output bytecodes that
884 will cause an interpreter stack undeflow when executed. Instead of
885 dumping core on such occasions, we omit the bytecode. Erroneous code
886 should not be executed, regardless. This makes life much easier, since
887 we don't have to deceive ourselves about the known stack depth. */
889 bc_emit_bytecode_const (&byte
, 1);
891 if ((stack_depth
-= arityvec
[(int) bytecode
].ninputs
) >= 0)
893 if ((stack_depth
+= arityvec
[(int) bytecode
].noutputs
) > max_stack_depth
)
894 max_stack_depth
= stack_depth
;
897 #ifdef VALIDATE_STACK_FOR_BC
898 VALIDATE_STACK_FOR_BC ();
903 #ifdef BCDEBUG_PRINT_CODE
904 #define PRLIT(TYPE, PTR) fprintf (stderr, " [%x]", *(TYPE *) PTR)
909 /* Emit a complete bytecode instruction, expecting the correct number
910 of literal values in the call. First argument is the instruction, the
911 remaining arguments are literals of size HOST_WIDE_INT or smaller. */
913 bc_emit_instruction
VPROTO((enum bytecode_opcode opcode
, ...))
916 enum bytecode_opcode opcode
;
919 int nliteral
, instruction
;
921 VA_START (arguments
, opcode
);
924 opcode
= va_arg (arguments
, enum bytecode_opcode
);
927 /* Emit instruction bytecode */
928 bc_emit_bytecode (opcode
);
929 instruction
= (int) opcode
;
931 /* Loop literals and emit as bytecode constants */
932 for (nliteral
= 0; nliteral
< arityvec
[instruction
].nliterals
; nliteral
++)
934 switch (arityvec
[instruction
].literals
[nliteral
])
936 /* This conditional is a kludge, but it's necessary
937 because TYPE might be long long. */
939 /* Expand definitions into case statements */
940 #define DEFTYPECODE(CODE, NAME, MODE, TYPE) \
943 TYPE temp = va_arg (arguments, TYPE); \
944 bc_emit_bytecode_const ((void *) &temp, sizeof temp); \
945 PRLIT (TYPE, &temp); } \
948 #include "bc-typecd.def"
951 #endif /* __GNUC__ */
958 #ifdef BCDEBUG_PRINT_CODE
959 fputc ('\n', stderr
);
963 /* Emit the machine-code interface trampoline at the beginning of a byte
964 coded function. The argument is a label name of the interpreter
965 bytecode callinfo structure; the return value is a label name for
966 the beginning of the actual bytecode. */
968 bc_emit_trampoline (callinfo
)
974 sprintf (mylab
, "*LB%d", n
++);
976 BC_EMIT_TRAMPOLINE (trampoline
, callinfo
);
978 seg_defsym (bytecode
, mylab
);
979 return sym_lookup (mylab
)->name
;
988 char *tmp
= xmalloc (strlen (str
) + 1);