📄 bc-emit.c
字号:
/* Output bytecodes for GNU C-compiler. Copyright (C) 1993, 1994, 1996, 1997 Free Software Foundation, Inc.This file is part of GNU CC.GNU CC is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU CC is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU CC; see the file COPYING. If not, write tothe Free Software Foundation, 59 Temple Place - Suite 330,Boston, MA 02111-1307, USA. */#include "config.h"#include <stdio.h>#ifdef __STDC__#include <stdarg.h>#else#include <varargs.h>#endif#include "machmode.h"#include "rtl.h"#include "real.h"#include "obstack.h"#include "bytecode.h"#ifdef __GNUC__#include "bytetypes.h"#endif#include "bc-emit.h"#include "bc-opcode.h"#include "bc-typecd.h"#include "bi-run.h"extern char *xmalloc (), *xrealloc ();extern struct obstack *rtl_obstack;/* Indexed by mode class, gives the narrowest mode for each class. */extern enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS];/* Commonly used modes. *//* Mode whose width is BITS_PER_UNIT */extern enum machine_mode byte_mode;/* Mode whose width is BITS_PER_WORD */extern enum machine_mode word_mode;/* Vector indexed by opcode giving info about the args for each opcode. */static struct arityvec arityvec[] = {#include "bc-arity.h"};/* How to print a symbol name for the assembler. */static voidprsym (file, s) FILE *file; char *s;{ if (*s == '*') fprintf (file, "%s", s + 1); else#ifdef NAMES_HAVE_UNDERSCORES fprintf (file, "_%s", s);#else fprintf (file, "%s", s);#endif}/* Maintain a bucket hash table for symbol names. */#define HASH_BITS 32#define HASH_SIZE 509static struct bc_sym *hashtab[HASH_SIZE];static unsigned inthash (name) char *name;{ unsigned int hash = 0; while (*name) { hash = hash << 3 | hash >> HASH_BITS - 3; hash += *name++; } return hash % HASH_SIZE;}/* Look up the named symbol, creating it if it doesn't exist. */struct bc_sym *sym_lookup (name) char *name;{ int i; struct bc_sym *s; i = hash (name); for (s = hashtab[i]; s; s = s->next) if (!strcmp (s->name, name)) return s; s = (struct bc_sym *) xmalloc (sizeof (struct bc_sym)); s->name = xmalloc (strlen (name) + 1); strcpy (s->name, name); s->defined = s->global = s->common = 0; s->val = 0; s->next = hashtab[i]; hashtab[i] = s; return s;}/* Write out .globl and common symbols to the named file. */static voidbc_sym_write (file) FILE *file;{ int i; struct bc_sym *s; for (i = 0; i < HASH_SIZE; ++i) for (s = hashtab[i]; s; s = s->next) { if (s->global) { fprintf (file, "\n\t.globl "); prsym (file, s->name); putc ('\n', file); if (s->common) { fprintf (file, "\n\t.comm "); prsym (file, s->name); fprintf (file, ", %lu\n", s->val); } } else if (s->common) { fprintf (file, "\n\t.lcomm "); prsym (file, s->name); fprintf (file, ", %lu\n", s->val); } }}/* Create and initialize a new segment. */static struct bc_seg *seg_create (){ struct bc_seg *result; result = (struct bc_seg *) xmalloc (sizeof (struct bc_seg)); result->alloc = 256; result->data = xmalloc (result->alloc); result->size = 0; result->syms = 0; result->relocs = 0; return result;}/* Advance the segment index to the next alignment boundary. */static voidseg_align (seg, log) struct bc_seg *seg; int log;{ unsigned int oldsize = seg->size; seg->size = seg->size + (1 << log) - 1 & ~((1 << log) - 1); if (seg->size > seg->alloc) { while (seg->size > seg->alloc) seg->alloc *= 2; seg->data = xrealloc (seg->data, seg->alloc); } bzero (seg->data + oldsize, seg->size - oldsize);}/* Append the given data to the given segment. */static voidseg_data (seg, data, size) struct bc_seg *seg; char *data; unsigned int size;{ if (seg->size + size > seg->alloc) { while (seg->size + size > seg->alloc) seg->alloc *= 2; seg->data = xrealloc (seg->data, seg->alloc); } bcopy (data, seg->data + seg->size, size); seg->size += size;}/* Append a zero-filled skip to the given segment. */static voidseg_skip (seg, size) struct bc_seg *seg; unsigned int size;{ if (seg->size + size > seg->alloc) { while (seg->size + size > seg->alloc) seg->alloc *= 2; seg->data = xrealloc (seg->data, seg->alloc); } memset (seg->data + seg->size, 0, size); seg->size += size;}/* Define the given name as the current offset in the given segment. It is an error if the name is already defined. Return 0 or 1 indicating failure or success respectively. */static intseg_defsym (seg, name) struct bc_seg *seg; char *name;{ struct bc_sym *sym; struct bc_segsym *segsym; sym = sym_lookup (name); if (sym->defined) return 0; sym->defined = 1; sym->val = seg->size; segsym = (struct bc_segsym *) xmalloc (sizeof (struct bc_segsym)); segsym->sym = sym; segsym->next = seg->syms; seg->syms = segsym; return 1;}/* Generate in seg's data a reference to the given sym, adjusted by the given offset. */static voidseg_refsym (seg, name, offset) struct bc_seg *seg; char *name; int offset;{ struct bc_sym *sym; struct bc_segreloc *segreloc; sym = sym_lookup (name); segreloc = (struct bc_segreloc *) xmalloc (sizeof (struct bc_segreloc)); segreloc->offset = seg->size; segreloc->sym = sym; segreloc->next = seg->relocs; seg->relocs = segreloc; seg_data (seg, (char *) &offset, sizeof offset);}/* Concatenate the contents of given segments into the first argument. */static voidseg_concat (result, seg) struct bc_seg *result, *seg;{ unsigned int fix; struct bc_segsym *segsym; struct bc_segreloc *segreloc; seg_align (result, MACHINE_SEG_ALIGN); fix = result->size; seg_data (result, seg->data, seg->size); free (seg->data); /* Go through the symbols and relocs of SEG, adjusting their offsets for their new location in RESULT. */ if (seg->syms) { segsym = seg->syms; do segsym->sym->val += fix; while (segsym->next && (segsym = segsym->next)); segsym->next = result->syms; result->syms = seg->syms; } if (seg->relocs) { segreloc = seg->relocs; do segreloc->offset += fix; while (segreloc->next && (segreloc = segreloc->next)); segreloc->next = result->relocs; result->relocs = seg->relocs; } free ((char *) seg);}/* Write a segment to a file. */static voidbc_seg_write (seg, file) struct bc_seg *seg; FILE *file;{ struct bc_segsym *segsym, *nsegsym, *psegsym; struct bc_segreloc *segreloc, *nsegreloc, *psegreloc; int i, offset, flag; /* Reverse the list of symbols. */ for (psegsym = 0, segsym = seg->syms; segsym; segsym = nsegsym) { nsegsym = segsym->next; segsym->next = psegsym; psegsym = segsym; } seg->syms = psegsym; /* Reverse the list of relocs. */ for (psegreloc = 0, segreloc = seg->relocs; segreloc; segreloc = nsegreloc) { nsegreloc = segreloc->next; segreloc->next = psegreloc; psegreloc = segreloc; } seg->relocs = psegreloc; /* Output each byte of the segment. */ for (i = 0, segsym = seg->syms, segreloc = seg->relocs; i < seg->size; ++i) { while (segsym && segsym->sym->val == i) { if (i % 8 != 0) putc ('\n', file); BC_WRITE_SEGSYM (segsym, file); segsym = segsym->next; flag = 1; } if (segreloc && segreloc->offset == i) { if (i % 8 != 0) putc ('\n', file); bcopy (seg->data + i, (char *) &offset, sizeof (int)); i += sizeof (int) - 1; BC_WRITE_RELOC_ENTRY (segreloc, file, offset); segreloc = segreloc->next; flag = 1; } else { if (i % 8 == 0 || flag) BC_START_BYTECODE_LINE (file); BC_WRITE_BYTECODE (i % 8 == 0 || flag ? ' ' : ',', seg->data[i] & 0xFF, file); flag = 0; if (i % 8 == 7) putc ('\n', file); } } /* Paranoia check--we should have visited all syms and relocs during the output pass. */ if (segsym || segreloc) abort ();}/* Text and data segments of the object file in making. */static struct bc_seg *bc_text_seg;static struct bc_seg *bc_data_seg;/* Called before anything else in this module. */voidbc_initialize (){ int min_class_size[(int) MAX_MODE_CLASS]; enum machine_mode mode; int i; bc_init_mode_to_code_map (); bc_text_seg = seg_create (); bc_data_seg = seg_create (); dconst0 = REAL_VALUE_ATOF ("0", DFmode); dconst1 = REAL_VALUE_ATOF ("1", DFmode); dconst2 = REAL_VALUE_ATOF ("2", DFmode); dconstm1 = REAL_VALUE_ATOF ("-1", DFmode); /* Find the narrowest mode for each class and compute the word and byte modes. */ for (i = 0; i < (int) MAX_MODE_CLASS; i++) min_class_size[i] = 1000; for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE; mode = (enum machine_mode) ((int) mode + 1)) { if (GET_MODE_SIZE (mode) < min_class_size[(int) GET_MODE_CLASS (mode)]) { class_narrowest_mode[(int) GET_MODE_CLASS (mode)] = mode; min_class_size[(int) GET_MODE_CLASS (mode)] = GET_MODE_SIZE (mode); } if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_BITSIZE (mode) == BITS_PER_UNIT) byte_mode = mode; if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE_BITSIZE (mode) == BITS_PER_WORD) word_mode = mode; }}/* External addresses referenced in a function. Rather than trying to work relocatable address directly into bytecoded functions (which would require us to provide hairy location info and possibly obey alignment rules imposed by the architecture) we build an auxiliary table of pointer constants, and encode just offsets into this table into the actual bytecode. */static struct bc_seg *ptrconsts;/* Trampoline code for the function entry. */struct bc_seg *trampoline;/* Actual byte code of the function. */struct bc_seg *bytecode;/* List of labels defined in the function. */struct bc_label *labels;/* List of label references in the function. */struct bc_labelref *labelrefs;/* Add symbol to pointer table. Return offset into table where pointer was stored. The offset usually goes into the bytecode stream as a constP literal. */intbc_define_pointer (p) char *p;{ int offset = ptrconsts->size; seg_refsym (ptrconsts, p, 0); return offset;}/* Begin a bytecoded function. */intbc_begin_function (name) char *name;{ ptrconsts = seg_create (); trampoline = seg_create (); bytecode = seg_create (); return seg_defsym (trampoline, name);}/* Force alignment in inline bytecode. */voidbc_align_bytecode (align) int align;{ seg_align (bytecode, align);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -