1 /* Output bytecodes for GNU C-compiler.
2 Copyright (C) 1993 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. */
31 #include "bc-opcode.h"
32 #include "bc-typecd.h"
35 extern char *xmalloc(), *xrealloc();
38 extern struct obstack *rtl_obstack;
40 REAL_VALUE_TYPE dconst0;
41 REAL_VALUE_TYPE dconst1;
42 REAL_VALUE_TYPE dconst2;
43 REAL_VALUE_TYPE dconstm1;
46 /* Indexed by mode class, gives the narrowest mode for each class. */
48 enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS];
50 /* Commonly used modes. */
51 /* Mode whose width is BITS_PER_UNIT */
52 enum machine_mode byte_mode;
54 /* Mode whose width is BITS_PER_WORD */
55 enum machine_mode word_mode;
57 /* Vector indexed by opcode giving info about the args for each opcode. */
58 static struct arityvec arityvec[] = {
62 /* How to print a symbol name for the assembler. */
69 fprintf (file, "%s", s + 1);
72 #ifdef NAMES_HAVE_UNDERSCORES
73 fprintf (file, "_%s", s);
75 fprintf (file, "%s", s);
80 /* Maintain a bucket hash table for symbol names. */
85 static struct bc_sym *hashtab[HASH_SIZE];
91 unsigned int hash = 0;
95 hash = hash << 3 | hash >> HASH_BITS - 3;
99 return hash % HASH_SIZE;
103 /* Look up the named symbol, creating it if it doesn't exist. */
112 for (s = hashtab[i]; s; s = s->next)
113 if (!strcmp (s->name, name))
116 s = (struct bc_sym *) xmalloc (sizeof (struct bc_sym));
117 s->name = xmalloc (strlen (name) + 1);
118 strcpy (s->name, name);
119 s->defined = s->global = s->common = 0;
121 s->next = hashtab[i];
127 /* Write out .globl and common symbols to the named file. */
135 for (i = 0; i < HASH_SIZE; ++i)
136 for (s = hashtab[i]; s; s = s->next)
140 fprintf (file, "\n\t.globl ");
141 prsym (file, s->name);
145 fprintf (file, "\n\t.comm ");
146 prsym (file, s->name);
147 fprintf (file, ", %d\n", s->val);
152 fprintf (file, "\n\t.lcomm ");
153 prsym (file, s->name);
154 fprintf (file, ", %d\n", s->val);
162 /* Create and initialize a new segment. */
163 static struct bc_seg *
166 struct bc_seg *result;
168 result = (struct bc_seg *) xmalloc(sizeof (struct bc_seg));
170 result->data = xmalloc(result->alloc);
178 /* Advance the segment index to the next alignment boundary. */
184 unsigned int oldsize = seg->size;
186 seg->size = seg->size + (1 << log) - 1 & ~((1 << log) - 1);
187 if (seg->size > seg->alloc)
189 while (seg->size > seg->alloc)
191 seg->data = xrealloc(seg->data, seg->alloc);
193 memset(seg->data + oldsize, 0, seg->size - oldsize);
197 /* Append the given data to the given segment. */
199 seg_data (seg, data, size)
204 if (seg->size + size > seg->alloc)
206 while (seg->size + size > seg->alloc)
208 seg->data = xrealloc (seg->data, seg->alloc);
211 memcpy (seg->data + seg->size, data, size);
216 /* Append a zero-filled skip to the given segment. */
222 if (seg->size + size > seg->alloc)
224 while (seg->size + size > seg->alloc)
226 seg->data = xrealloc (seg->data, seg->alloc);
229 memset (seg->data + seg->size, 0, size);
234 /* Define the given name as the current offset in the given segment. It
235 is an error if the name is already defined. Return 0 or 1 indicating
236 failure or success respectively. */
238 seg_defsym (seg, name)
243 struct bc_segsym *segsym;
245 sym = sym_lookup(name);
250 sym->val = seg->size;
251 segsym = (struct bc_segsym *) xmalloc(sizeof (struct bc_segsym));
253 segsym->next = seg->syms;
259 /* Generate in seg's data a reference to the given sym, adjusted by
262 seg_refsym (seg, name, offset)
268 struct bc_segreloc *segreloc;
270 sym = sym_lookup(name);
271 segreloc = (struct bc_segreloc *) xmalloc(sizeof (struct bc_segreloc));
272 segreloc->offset = seg->size;
274 segreloc->next = seg->relocs;
275 seg->relocs = segreloc;
276 seg_data(seg, (char *) &offset, sizeof offset);
280 /* Concatenate the contents of given segments into the first argument. */
282 seg_concat (result, seg)
283 struct bc_seg *result, *seg;
286 struct bc_segsym *segsym;
287 struct bc_segreloc *segreloc;
289 seg_align(result, MACHINE_SEG_ALIGN);
291 seg_data(result, seg->data, seg->size);
294 /* Go through the symbols and relocs of SEG, adjusting their offsets
295 for their new location in RESULT. */
300 segsym->sym->val += fix;
301 while (segsym->next && (segsym = segsym->next));
302 segsym->next = result->syms;
303 result->syms = seg->syms;
307 segreloc = seg->relocs;
309 segreloc->offset += fix;
310 while (segreloc->next && (segreloc = segreloc->next));
311 segreloc->next = result->relocs;
312 result->relocs = seg->relocs;
318 /* Write a segment to a file. */
320 bc_seg_write (seg, file)
324 struct bc_segsym *segsym, *nsegsym, *psegsym;
325 struct bc_segreloc *segreloc, *nsegreloc, *psegreloc;
328 /* Reverse the list of symbols. */
329 for (psegsym = 0, segsym = seg->syms; segsym; segsym = nsegsym)
331 nsegsym = segsym->next;
332 segsym->next = psegsym;
337 /* Reverse the list of relocs. */
338 for (psegreloc = 0, segreloc = seg->relocs; segreloc; segreloc = nsegreloc)
340 nsegreloc = segreloc->next;
341 segreloc->next = psegreloc;
342 psegreloc = segreloc;
344 seg->relocs = psegreloc;
346 /* Output each byte of the segment. */
347 for (i = 0, segsym = seg->syms, segreloc = seg->relocs; i < seg->size; ++i)
349 while (segsym && segsym->sym->val == i)
354 BC_WRITE_SEGSYM (segsym, file);
355 segsym = segsym->next;
358 if (segreloc && segreloc->offset == i)
363 offset = *(int *) (seg->data + i);
364 i += sizeof (int) - 1;
366 BC_WRITE_RELOC_ENTRY (segreloc, file, offset);
367 segreloc = segreloc->next;
372 if (i % 8 == 0 || flag)
373 BC_START_BYTECODE_LINE (file);
375 BC_WRITE_BYTECODE (i % 8 == 0 || flag ? ' ' : ',',
384 /* Paranoia check--we should have visited all syms and relocs during
387 if (segsym || segreloc)
393 /* Text and data segments of the object file in making. */
394 static struct bc_seg *bc_text_seg;
395 static struct bc_seg *bc_data_seg;
397 /* Called before anything else in this module. */
401 int min_class_size[(int) MAX_MODE_CLASS];
402 enum machine_mode mode;
405 bc_init_mode_to_code_map ();
407 bc_text_seg = seg_create ();
408 bc_data_seg = seg_create ();
410 dconst0 = REAL_VALUE_ATOF ("0");
411 dconst1 = REAL_VALUE_ATOF ("1");
412 dconst2 = REAL_VALUE_ATOF ("2");
413 dconstm1 = REAL_VALUE_ATOF ("-1");
415 /* Find the narrowest mode for each class and compute the word and byte
418 for (i = 0; i < (int) MAX_MODE_CLASS; i++)
419 min_class_size[i] = 1000;
421 for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
422 mode = (enum machine_mode) ((int) mode + 1))
424 if (GET_MODE_SIZE (mode) < min_class_size[(int) GET_MODE_CLASS (mode)])
426 class_narrowest_mode[(int) GET_MODE_CLASS (mode)] = mode;
427 min_class_size[(int) GET_MODE_CLASS (mode)] = GET_MODE_SIZE (mode);
429 if (GET_MODE_CLASS (mode) == MODE_INT
430 && GET_MODE_BITSIZE (mode) == BITS_PER_UNIT)
433 if (GET_MODE_CLASS (mode) == MODE_INT
434 && GET_MODE_BITSIZE (mode) == BITS_PER_WORD)
440 /* External addresses referenced in a function. Rather than trying to
441 work relocatable address directly into bytecoded functions (which would
442 require us to provide hairy location info and possibly obey alignment
443 rules imposed by the architecture) we build an auxilary table of
444 pointer constants, and encode just offsets into this table into the
446 static struct bc_seg *ptrconsts;
448 /* Trampoline code for the function entry. */
449 struct bc_seg *trampoline;
451 /* Actual byte code of the function. */
452 struct bc_seg *bytecode;
454 /* List of labels defined in the function. */
455 struct bc_label *labels;
457 /* List of label references in the function. */
458 struct bc_labelref *labelrefs;
461 /* Add symbol to pointer table. Return offset into table where
462 pointer was stored. The offset usually goes into the bytecode
463 stream as a constP literal. */
465 bc_define_pointer (p)
468 int offset = ptrconsts->size;
470 seg_refsym (ptrconsts, p, 0);
475 /* Begin a bytecoded function. */
477 bc_begin_function (name)
480 ptrconsts = seg_create ();
481 trampoline = seg_create ();
482 bytecode = seg_create ();
483 return seg_defsym (trampoline, name);
487 /* Force alignment in inline bytecode. */
489 bc_align_bytecode (align)
492 seg_align (bytecode, align);
496 /* Emit data inline into bytecode. */
498 bc_emit_bytecode_const (data, size)
503 seg_data (bytecode, data, size);
507 /* Create a new "bytecode label", to have its value defined later.
508 Bytecode labels have nothing to do with the object file symbol table,
509 and are purely local to a given bytecoded function. */
511 bc_get_bytecode_label ()
513 struct bc_label *result;
515 result = (struct bc_label *) xmalloc (sizeof (struct bc_label));
517 result->next = labels;
524 /* Define the given label with the current location counter. */
526 bc_emit_bytecode_labeldef (label)
527 struct bc_label *label;
529 extern int bc_new_uid ();
531 if (!label || label->defined)
534 label->offset = bytecode->size;
536 label->uid = bc_new_uid ();
538 #ifdef DEBUG_PRINT_CODE
539 fprintf (stderr, "$%lx:\n", label);
546 /* Generate a location-relative reference to the given bytecode label.
547 It need not be defined yet; label references will be backpatched later. */
549 bc_emit_bytecode_labelref (label)
550 struct bc_label *label;
552 struct bc_labelref *labelref;
555 labelref = (struct bc_labelref *) xmalloc (sizeof (struct bc_labelref));
556 labelref->label = label;
557 labelref->offset = bytecode->size;
558 labelref->next = labelrefs;
559 labelrefs = labelref;
561 #ifdef DEBUG_PRINT_CODE
562 fprintf (stderr, " $%lx", label);
565 seg_data (bytecode, (char *) &zero, sizeof zero);
569 /* Emit a reference to an external address; generate the reference in the
570 ptrconst area, and emit an offset in the bytecode. */
572 bc_emit_code_labelref (name, offset)
578 ptroff = ptrconsts->size / sizeof (char *);
579 seg_data (bytecode, (char *) &ptroff, sizeof ptroff);
580 seg_refsym (ptrconsts, name, offset);
582 #ifdef DEBUG_PRINT_CODE
583 fprintf (stderr, " [external <%x> %s]", ptroff, name);
588 /* Backpatch label references in the byte code, and concatenate the bytecode
589 and pointer constant segments to the cumulative text for the object file.
590 Return a label name for the pointer constants region. */
595 struct bc_label *label, *next;
596 struct bc_labelref *ref, *nextref;
597 char ptrconsts_label[20];
600 /* Backpatch bytecode label references. */
601 for (ref = labelrefs; ref; ref = ref->next)
602 if (ref->label->defined)
604 addr = ref->label->offset;
605 memcpy (bytecode->data + ref->offset, /* incest */
606 (char *) &addr, 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. */
829 bc_gen_rtx (label, offset, bc_label)
832 struct bc_label *bc_label;
836 r = (rtx) obstack_alloc (rtl_obstack, sizeof (struct rtx_def));
837 r->label = label; /* FIXME: Do we need to copy here? */
839 r->bc_label = bc_label;
844 /* Print bytecode rtx */
850 BC_WRITE_RTL (r, fp);
854 /* Emit a bytecode, keeping a running tally of the stack depth. */
856 bc_emit_bytecode (bytecode)
857 enum bytecode_opcode bytecode;
860 int npushes = arityvec[bytecode].noutputs - arityvec[bytecode].ninputs;
861 static int prev_lineno = -1;
865 #ifdef BCDEBUG_PRINT_CODE
866 if (lineno != prev_lineno)
868 fprintf (stderr, "<line %d>\n", lineno);
869 prev_lineno = lineno;
872 fputs (opcode_name[(unsigned int) bytecode], stderr);
875 /* Due to errors we are often requested to output bytecodes that
876 will cause an interpreter stack undeflow when executed. Instead of
877 dumping core on such occasions, we omit the bytecode. Erroneous code
878 should not be executed, regardless. This makes life much easier, since
879 we don't have to deceive ourselves about the known stack depth. */
881 bc_emit_bytecode_const (&byte, 1);
883 if ((stack_depth -= arityvec[bytecode].ninputs) >= 0)
885 if ((stack_depth += arityvec[bytecode].noutputs) > max_stack_depth)
886 max_stack_depth = stack_depth;
889 #ifdef VALIDATE_STACK
895 #ifdef BCDEBUG_PRINT_CODE
896 #define PRLIT(TYPE, PTR) fprintf (stderr, " [%x]", *(TYPE *) PTR)
901 /* Emit a complete bytecode instruction, expecting the correct number
902 of literal values in the call. First argument is the instruction, the
903 remaining arguments are literals of size HOST_WIDE_INT or smaller. */
905 bc_emit_instruction (va_alist)
909 enum bytecode_opcode opcode;
910 int nliteral, instruction;
913 va_start (arguments);
915 /* Emit instruction bytecode */
916 opcode = va_arg (arguments, enum bytecode_opcode);
917 bc_emit_bytecode (opcode);
918 instruction = (int) opcode;
920 /* Loop literals and emit as bytecode constants */
921 for (nliteral = 0; nliteral < arityvec[instruction].nliterals; nliteral++)
923 HOST_WIDE_INT literal;
925 switch (arityvec[instruction].literals[nliteral])
927 /* Expand definitions into case statements */
928 #define DEFTYPECODE(CODE, NAME, MODE, TYPE) \
930 { TYPE temp = va_arg (arguments, TYPE); \
931 bc_emit_bytecode_const ((void *) &temp, sizeof temp); \
932 PRLIT (TYPE, &temp); } \
935 #include "bc-typecd.def"
944 #ifdef BCDEBUG_PRINT_CODE
945 fputc ('\n', stderr);
949 /* Emit the machine-code interface trampoline at the beginning of a byte
950 coded function. The argument is a label name of the interpreter
951 bytecode callinfo structure; the return value is a label name for
952 the beginning of the actual bytecode. */
954 bc_emit_trampoline (callinfo)
960 sprintf (mylab, "*LB%d", n++);
962 BC_EMIT_TRAMPOLINE (trampoline, callinfo);
964 seg_defsym (bytecode, mylab);
965 return sym_lookup (mylab)->name;
974 char *tmp = xmalloc (strlen (str) + 1);