From: Jan Brittenson Date: Tue, 21 Sep 1993 21:25:11 +0000 (-0700) Subject: Initial revision X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=86d7f2db057abae09db4208bf0578f6e9a0da17b;p=gcc.git Initial revision From-SVN: r5378 --- diff --git a/gcc/bc-emit.c b/gcc/bc-emit.c new file mode 100644 index 00000000000..d820c542c02 --- /dev/null +++ b/gcc/bc-emit.c @@ -0,0 +1,978 @@ +/* Output bytecodes for GNU C-compiler. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + + +#include +#include +#include +#include "config.h" +#include "machmode.h" +#include "rtl.h" +#include "real.h" +#include "obstack.h" +#include "bytecode.h" +#include "bc-emit.h" +#include "bc-opcode.h" +#include "bc-typecd.h" +#include "bi-run.h" + +extern char *xmalloc(), *xrealloc(); +extern void free(); + +extern struct obstack *rtl_obstack; + +REAL_VALUE_TYPE dconst0; +REAL_VALUE_TYPE dconst1; +REAL_VALUE_TYPE dconst2; +REAL_VALUE_TYPE dconstm1; + + +/* Indexed by mode class, gives the narrowest mode for each class. */ + +enum machine_mode class_narrowest_mode[(int) MAX_MODE_CLASS]; + +/* Commonly used modes. */ +/* Mode whose width is BITS_PER_UNIT */ +enum machine_mode byte_mode; + +/* Mode whose width is BITS_PER_WORD */ +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 void +prsym (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 509 + +static struct bc_sym *hashtab[HASH_SIZE]; + +static unsigned int +hash (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. */ +static 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 void +bc_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, ", %d\n", s->val); + } + } + else if (s->common) + { + fprintf (file, "\n\t.lcomm "); + prsym (file, s->name); + fprintf (file, ", %d\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 void +seg_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); + } + memset(seg->data + oldsize, 0, seg->size - oldsize); +} + + +/* Append the given data to the given segment. */ +static void +seg_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); + } + + memcpy (seg->data + seg->size, data, size); + seg->size += size; +} + + +/* Append a zero-filled skip to the given segment. */ +static void +seg_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 int +seg_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 void +seg_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 void +seg_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 void +bc_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); + + offset = *(int *) (seg->data + i); + 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. */ +void +bc_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"); + dconst1 = REAL_VALUE_ATOF ("1"); + dconst2 = REAL_VALUE_ATOF ("2"); + dconstm1 = REAL_VALUE_ATOF ("-1"); + + /* 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 auxilary 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. */ +int +bc_define_pointer (p) + char *p; +{ + int offset = ptrconsts->size; + + seg_refsym (ptrconsts, p, 0); + return offset; +} + + +/* Begin a bytecoded function. */ +int +bc_begin_function (name) + char *name; +{ + ptrconsts = seg_create (); + trampoline = seg_create (); + bytecode = seg_create (); + return seg_defsym (trampoline, name); +} + + +/* Force alignment in inline bytecode. */ +void +bc_align_bytecode (align) + int align; +{ + seg_align (bytecode, align); +} + + +/* Emit data inline into bytecode. */ +void +bc_emit_bytecode_const (data, size) + char *data; + unsigned int size; +{ + if (bytecode) + seg_data (bytecode, data, size); +} + + +/* Create a new "bytecode label", to have its value defined later. + Bytecode labels have nothing to do with the object file symbol table, + and are purely local to a given bytecoded function. */ +struct bc_label * +bc_get_bytecode_label () +{ + struct bc_label *result; + + result = (struct bc_label *) xmalloc (sizeof (struct bc_label)); + result->defined = 0; + result->next = labels; + result->uid = 0; + labels = result; + return result; +} + + +/* Define the given label with the current location counter. */ +int +bc_emit_bytecode_labeldef (label) + struct bc_label *label; +{ + extern int bc_new_uid (); + + if (!label || label->defined) + return 0; + + label->offset = bytecode->size; + label->defined = 1; + label->uid = bc_new_uid (); + +#ifdef DEBUG_PRINT_CODE + fprintf (stderr, "$%lx:\n", label); +#endif + + return 1; +} + + +/* Generate a location-relative reference to the given bytecode label. + It need not be defined yet; label references will be backpatched later. */ +void +bc_emit_bytecode_labelref (label) + struct bc_label *label; +{ + struct bc_labelref *labelref; + static int zero; + + labelref = (struct bc_labelref *) xmalloc (sizeof (struct bc_labelref)); + labelref->label = label; + labelref->offset = bytecode->size; + labelref->next = labelrefs; + labelrefs = labelref; + +#ifdef DEBUG_PRINT_CODE + fprintf (stderr, " $%lx", label); +#endif + + seg_data (bytecode, (char *) &zero, sizeof zero); +} + + +/* Emit a reference to an external address; generate the reference in the + ptrconst area, and emit an offset in the bytecode. */ +void +bc_emit_code_labelref (name, offset) + char *name; + int offset; +{ + int ptroff; + + ptroff = ptrconsts->size / sizeof (char *); + seg_data (bytecode, (char *) &ptroff, sizeof ptroff); + seg_refsym (ptrconsts, name, offset); + +#ifdef DEBUG_PRINT_CODE + fprintf (stderr, " [external <%x> %s]", ptroff, name); +#endif +} + + +/* Backpatch label references in the byte code, and concatenate the bytecode + and pointer constant segments to the cumulative text for the object file. + Return a label name for the pointer constants region. */ +char * +bc_end_function () +{ + int addr; + struct bc_label *label, *next; + struct bc_labelref *ref, *nextref; + char ptrconsts_label[20]; + static int nlab; + + /* Backpatch bytecode label references. */ + for (ref = labelrefs; ref; ref = ref->next) + if (ref->label->defined) + { + addr = ref->label->offset; + memcpy (bytecode->data + ref->offset, /* incest */ + (char *) &addr, sizeof addr); + } + + /* Free the chains of labelrefs and labeldefs. */ + for (ref = labelrefs; ref; ref = nextref) + { + nextref = ref->next; + free ((char *) ref); + } + + for (label = labels; label; label = next) + { + next = label->next; + free ((char *) label); + } + + seg_concat (trampoline, bytecode); + seg_align (trampoline, MACHINE_SEG_ALIGN); + sprintf (ptrconsts_label, "*LP%d", nlab++); + seg_defsym (trampoline, ptrconsts_label); + seg_concat (trampoline, ptrconsts); + seg_concat (bc_text_seg, trampoline); + + labels = 0; + labelrefs = 0; + trampoline = 0; + bytecode = 0; + ptrconsts = 0; + + return sym_lookup (ptrconsts_label)->name; +} + +/* Force alignment in const data. */ +void +bc_align_const (align) + int align; +{ + seg_align (bc_text_seg, align); +} + +/* Emit const data. */ +void +bc_emit_const (data, size) + char *data; + unsigned int size; +{ + seg_data (bc_text_seg, data, size); +} + +/* Emit a zero-filled constant skip. */ +void +bc_emit_const_skip (size) + unsigned int size; +{ + seg_skip (bc_text_seg, size); +} + +/* Emit a label definition in const data. */ +int +bc_emit_const_labeldef (name) + char *name; +{ + return seg_defsym (bc_text_seg, name); +} + +/* Emit a label reference in const data. */ +void +bc_emit_const_labelref (name, offset) + char *name; + int offset; +{ + seg_refsym (bc_text_seg, name, offset); +} + +/* Force alignment in data. */ +void +bc_align_data (align) + int align; +{ + seg_align (bc_data_seg, align); +} + +/* Emit data. */ +void +bc_emit_data(data, size) + char *data; + unsigned int size; +{ + seg_data (bc_data_seg, data, size); +} + +/* Emit a zero-filled data skip. */ +void +bc_emit_data_skip (size) + unsigned int size; +{ + seg_skip(bc_data_seg, size); +} + +/* Emit label definition in data. */ +int +bc_emit_data_labeldef(name) + char *name; +{ + return seg_defsym(bc_data_seg, name); +} + +/* Emit label reference in data. */ +void +bc_emit_data_labelref(name, offset) + char *name; + int offset; +{ + seg_refsym(bc_data_seg, name, offset); +} + +/* Emit a common block of the given name and size. Note that + when the .o file is actually written non-global "common" + blocks will have to be turned into space in the data section. */ +int +bc_emit_common(name, size) + char *name; + unsigned int size; +{ + struct bc_sym *sym; + + sym = sym_lookup(name); + if (sym->defined) + return 0; + + sym->defined = 1; + sym->common = 1; + sym->val = size; + return 1; +} + +/* Globalize the given label. */ +void +bc_globalize_label(name) + char *name; +{ + struct bc_sym *sym; + + sym = sym_lookup(name); + sym->global = 1; +} + +static enum { in_text, in_data } section = in_text; + +void +bc_text () +{ + section = in_text; +} + +void +bc_data () +{ + section = in_data; +} + +void +bc_align (align) + int align; +{ + if (section == in_text) + bc_align_const (align); + else + bc_align_data (align); +} + +void +bc_emit (data, size) + char *data; + unsigned int size; +{ + if (section == in_text) + bc_emit_const (data, size); + else + bc_emit_data (data, size); +} + +void +bc_emit_skip (size) + unsigned int size; +{ + if (section == in_text) + bc_emit_const_skip (size); + else + bc_emit_data_skip (size); +} + +int +bc_emit_labeldef (name) + char *name; +{ + if (section == in_text) + return bc_emit_const_labeldef (name); + else + return bc_emit_data_labeldef (name); +} + +void +bc_emit_labelref (name, offset) + char *name; + int offset; +{ + if (section == in_text) + bc_emit_const_labelref (name, offset); + else + bc_emit_data_labelref (name, offset); +} + +void +bc_write_file (file) + FILE *file; +{ + BC_WRITE_FILE (file); +} + + +/* Allocate a new bytecode rtx. */ +rtx +bc_gen_rtx (label, offset, bc_label) + char *label; + int offset; + struct bc_label *bc_label; +{ + rtx r; + + r = (rtx) obstack_alloc (rtl_obstack, sizeof (struct rtx_def)); + r->label = label; /* FIXME: Do we need to copy here? */ + r->offset = offset; + r->bc_label = bc_label; + return r; +} + + +/* Print bytecode rtx */ +void +bc_print_rtl (fp, r) + FILE *fp; + rtx r; +{ + BC_WRITE_RTL (r, fp); +} + + +/* Emit a bytecode, keeping a running tally of the stack depth. */ +void +bc_emit_bytecode (bytecode) + enum bytecode_opcode bytecode; +{ + char byte; + int npushes = arityvec[bytecode].noutputs - arityvec[bytecode].ninputs; + static int prev_lineno = -1; + + byte = bytecode; + +#ifdef BCDEBUG_PRINT_CODE + if (lineno != prev_lineno) + { + fprintf (stderr, "\n", lineno); + prev_lineno = lineno; + } + + fputs (opcode_name[(unsigned int) bytecode], stderr); +#endif + + /* Due to errors we are often requested to output bytecodes that + will cause an interpreter stack undeflow when executed. Instead of + dumping core on such occasions, we omit the bytecode. Erroneous code + should not be executed, regardless. This makes life much easier, since + we don't have to deceive ourselves about the known stack depth. */ + + bc_emit_bytecode_const (&byte, 1); + + if ((stack_depth -= arityvec[bytecode].ninputs) >= 0) + { + if ((stack_depth += arityvec[bytecode].noutputs) > max_stack_depth) + max_stack_depth = stack_depth; + } + +#ifdef VALIDATE_STACK + VALIDATE_STACK (); +#endif +} + + +#ifdef BCDEBUG_PRINT_CODE +#define PRLIT(TYPE, PTR) fprintf (stderr, " [%x]", *(TYPE *) PTR) +#else +#define PRLIT(X,Y) +#endif + +/* Emit a complete bytecode instruction, expecting the correct number + of literal values in the call. First argument is the instruction, the + remaining arguments are literals of size HOST_WIDE_INT or smaller. */ +void +bc_emit_instruction (va_alist) + va_dcl +{ + va_list arguments; + enum bytecode_opcode opcode; + int nliteral, instruction; + + + va_start (arguments); + + /* Emit instruction bytecode */ + opcode = va_arg (arguments, enum bytecode_opcode); + bc_emit_bytecode (opcode); + instruction = (int) opcode; + + /* Loop literals and emit as bytecode constants */ + for (nliteral = 0; nliteral < arityvec[instruction].nliterals; nliteral++) + { + HOST_WIDE_INT literal; + + switch (arityvec[instruction].literals[nliteral]) + { + /* Expand definitions into case statements */ +#define DEFTYPECODE(CODE, NAME, MODE, TYPE) \ + case CODE: \ + { TYPE temp = va_arg (arguments, TYPE); \ + bc_emit_bytecode_const ((void *) &temp, sizeof temp); \ + PRLIT (TYPE, &temp); } \ + break; + +#include "bc-typecd.def" + +#undef DEFTYPECODE + + default: + abort (); + } + } + +#ifdef BCDEBUG_PRINT_CODE + fputc ('\n', stderr); +#endif +} + +/* Emit the machine-code interface trampoline at the beginning of a byte + coded function. The argument is a label name of the interpreter + bytecode callinfo structure; the return value is a label name for + the beginning of the actual bytecode. */ +char * +bc_emit_trampoline (callinfo) + char *callinfo; +{ + char mylab[20]; + static int n; + + sprintf (mylab, "*LB%d", n++); + + BC_EMIT_TRAMPOLINE (trampoline, callinfo); + + seg_defsym (bytecode, mylab); + return sym_lookup (mylab)->name; +} + + +/* Simple strdup */ +char * +bc_xstrdup (str) + char *str; +{ + char *tmp = xmalloc (strlen (str) + 1); + + strcpy (tmp, str); + return tmp; +} diff --git a/gcc/bc-optab.c b/gcc/bc-optab.c new file mode 100644 index 00000000000..57771fcdc25 --- /dev/null +++ b/gcc/bc-optab.c @@ -0,0 +1,786 @@ +/* Bytecode conversion definitions for GNU C-compiler. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + + +#include "config.h" +#include "tree.h" +#include "rtl.h" +#include "machmode.h" +#include "obstack.h" +#include "bytecode.h" +#include "bc-typecd.h" +#include "bc-opcode.h" +#include "bc-optab.h" + +#define obstack_chunk_alloc xmalloc +#define obstack_chunk_free free + +extern char *xmalloc (); +extern void free (); + +/* Table relating interpreter typecodes to machine modes. */ +#define GET_TYPECODE_MODE(CODE) (typecode_mode[((int) CODE)]) +enum machine_mode typecode_mode[] = { +#define DEFTYPECODE(CODE, NAME, MODE, TYPE) MODE, +#include "bc-typecd.def" +#undef DEFTYPECODE +}; + +/* Machine mode to type code map */ +static enum typecode signed_mode_to_code_map[MAX_MACHINE_MODE+1]; +static enum typecode unsigned_mode_to_code_map[MAX_MACHINE_MODE+1]; + +#define GET_TYPECODE_SIZE(CODE) GET_MODE_SIZE (GET_TYPECODE_MODE (CODE)) + +#define BIG_ARBITRARY_NUMBER 100000 + +/* Table of recipes for conversions among scalar types, to be filled + in as needed at run time. */ +static struct conversion_recipe +{ + unsigned char *opcodes; /* Bytecodes to emit in order. */ + int nopcodes; /* Count of bytecodes. */ + int cost; /* A rather arbitrary cost function. */ +} conversion_recipe[NUM_TYPECODES][NUM_TYPECODES]; + +/* Binary operator tables. */ +struct binary_operator optab_plus_expr[] = { + { addSI, SIcode, SIcode, SIcode }, + { addDI, DIcode, DIcode, DIcode }, + { addSF, SFcode, SFcode, SFcode }, + { addDF, DFcode, DFcode, DFcode }, + { addXF, XFcode, XFcode, XFcode }, + { addPSI, Pcode, Pcode, SIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_minus_expr[] = { + { subSI, SIcode, SIcode, SIcode }, + { subDI, DIcode, DIcode, DIcode }, + { subSF, SFcode, SFcode, SFcode }, + { subDF, DFcode, DFcode, DFcode }, + { subXF, XFcode, XFcode, XFcode }, + { subPP, SIcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +/* The ordering of the tables for multiplicative operators + is such that unsigned operations will be preferred to signed + operations when one argument is unsigned. */ + +struct binary_operator optab_mult_expr[] = { + { mulSU, SUcode, SUcode, SUcode }, + { mulDU, DUcode, DUcode, DUcode }, + { mulSI, SIcode, SIcode, SIcode }, + { mulDI, DIcode, DIcode, DIcode }, + { mulSF, SFcode, SFcode, SFcode }, + { mulDF, DFcode, DFcode, DFcode }, + { mulXF, XFcode, XFcode, XFcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_trunc_div_expr[] = { + { divSU, SUcode, SUcode, SUcode }, + { divDU, DUcode, DUcode, DUcode }, + { divSI, SIcode, SIcode, SIcode }, + { divDI, DIcode, DIcode, DIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_trunc_mod_expr[] = { + { modSU, SUcode, SUcode, SUcode }, + { modDU, DUcode, DUcode, DUcode }, + { modSI, SIcode, SIcode, SIcode }, + { modDI, DIcode, DIcode, DIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_rdiv_expr[] = { + { divSF, SFcode, SFcode, SFcode }, + { divDF, DFcode, DFcode, DFcode }, + { divXF, XFcode, XFcode, XFcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_bit_and_expr[] = { + { andSI, SIcode, SIcode, SIcode }, + { andDI, DIcode, DIcode, DIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_bit_ior_expr[] = { + { iorSI, SIcode, SIcode, SIcode }, + { iorDI, DIcode, DIcode, DIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_bit_xor_expr[] = { + { xorSI, SIcode, SIcode, SIcode }, + { xorDI, DIcode, DIcode, DIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_lshift_expr[] = { + { lshiftSI, SIcode, SIcode, SIcode }, + { lshiftSU, SUcode, SUcode, SIcode }, + { lshiftDI, DIcode, DIcode, SIcode }, + { lshiftDU, DUcode, DUcode, SIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_rshift_expr[] = { + { rshiftSI, SIcode, SIcode, SIcode }, + { rshiftSU, SUcode, SUcode, SIcode }, + { rshiftDI, DIcode, DIcode, SIcode }, + { rshiftDU, DUcode, DUcode, SIcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_truth_and_expr[] = { + { andSI, SIcode, Tcode, Tcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_truth_or_expr[] = { + { iorSI, SIcode, Tcode, Tcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_lt_expr[] = { + { ltSI, Tcode, SIcode, SIcode }, + { ltSU, Tcode, SUcode, SUcode }, + { ltDI, Tcode, DIcode, DIcode }, + { ltDU, Tcode, DUcode, DUcode }, + { ltSF, Tcode, SFcode, SFcode }, + { ltDF, Tcode, DFcode, DFcode }, + { ltXF, Tcode, XFcode, XFcode }, + { ltP, Tcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_le_expr[] = { + { leSI, Tcode, SIcode, SIcode }, + { leSU, Tcode, SUcode, SUcode }, + { leDI, Tcode, DIcode, DIcode }, + { leDU, Tcode, DUcode, DUcode }, + { leSF, Tcode, SFcode, SFcode }, + { leDF, Tcode, DFcode, DFcode }, + { leXF, Tcode, XFcode, XFcode }, + { leP, Tcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_ge_expr[] = { + { geSI, Tcode, SIcode, SIcode }, + { geSU, Tcode, SUcode, SUcode }, + { geDI, Tcode, DIcode, DIcode }, + { geDU, Tcode, DUcode, DUcode }, + { geSF, Tcode, SFcode, SFcode }, + { geDF, Tcode, DFcode, DFcode }, + { geXF, Tcode, XFcode, XFcode }, + { geP, Tcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_gt_expr[] = { + { gtSI, Tcode, SIcode, SIcode }, + { gtSU, Tcode, SUcode, SUcode }, + { gtDI, Tcode, DIcode, DIcode }, + { gtDU, Tcode, DUcode, DUcode }, + { gtSF, Tcode, SFcode, SFcode }, + { gtDF, Tcode, DFcode, DFcode }, + { gtXF, Tcode, XFcode, XFcode }, + { gtP, Tcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_eq_expr[] = { + { eqSI, Tcode, SIcode, SIcode }, + { eqDI, Tcode, DIcode, DIcode }, + { eqSF, Tcode, SFcode, SFcode }, + { eqDF, Tcode, DFcode, DFcode }, + { eqXF, Tcode, XFcode, XFcode }, + { eqP, Tcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +struct binary_operator optab_ne_expr[] = { + { neSI, Tcode, SIcode, SIcode }, + { neDI, Tcode, DIcode, DIcode }, + { neSF, Tcode, SFcode, SFcode }, + { neDF, Tcode, DFcode, DFcode }, + { neXF, Tcode, XFcode, XFcode }, + { neP, Tcode, Pcode, Pcode }, + { -1, -1, -1, -1 }, +}; + +/* Unary operator tables. */ +struct unary_operator optab_negate_expr[] = { + { negSI, SIcode, SIcode }, + { negDI, DIcode, DIcode }, + { negSF, SFcode, SFcode }, + { negDF, DFcode, DFcode }, + { negXF, XFcode, XFcode }, + { -1, -1, -1 }, +}; + +struct unary_operator optab_bit_not_expr[] = { + { notSI, SIcode, SIcode }, + { notDI, DIcode, DIcode }, + { -1, -1, -1 }, +}; + +struct unary_operator optab_truth_not_expr[] = { + { notT, SIcode, SIcode }, + { -1, -1, -1 }, +}; + +/* Increment operator tables. */ +struct increment_operator optab_predecrement_expr[] = { + { predecQI, QIcode }, + { predecQI, QUcode }, + { predecHI, HIcode }, + { predecHI, HUcode }, + { predecSI, SIcode }, + { predecSI, SUcode }, + { predecDI, DIcode }, + { predecDI, DUcode }, + { predecP, Pcode }, + { predecSF, SFcode }, + { predecDF, DFcode }, + { predecXF, XFcode }, + { -1, -1 }, +}; + +struct increment_operator optab_preincrement_expr[] = { + { preincQI, QIcode }, + { preincQI, QUcode }, + { preincHI, HIcode }, + { preincHI, HUcode }, + { preincSI, SIcode }, + { preincSI, SUcode }, + { preincDI, DIcode }, + { preincDI, DUcode }, + { preincP, Pcode }, + { preincSF, SFcode }, + { preincDF, DFcode }, + { preincXF, XFcode }, + { -1, -1 }, +}; + +struct increment_operator optab_postdecrement_expr[] = { + { postdecQI, QIcode }, + { postdecQI, QUcode }, + { postdecHI, HIcode }, + { postdecHI, HUcode }, + { postdecSI, SIcode }, + { postdecSI, SUcode }, + { postdecDI, DIcode }, + { postdecDI, DUcode }, + { postdecP, Pcode }, + { postdecSF, SFcode }, + { postdecDF, DFcode }, + { postdecXF, XFcode }, + { -1, -1 }, +}; + +struct increment_operator optab_postincrement_expr[] = { + { postincQI, QIcode }, + { postincQI, QUcode }, + { postincHI, HIcode }, + { postincHI, HUcode }, + { postincSI, SIcode }, + { postincSI, SUcode }, + { postincDI, DIcode }, + { postincDI, DUcode }, + { postincP, Pcode }, + { postincSF, SFcode }, + { postincDF, DFcode }, + { postincXF, XFcode }, + { -1, -1 }, +}; + +/* Table of conversions supported by the interpreter. */ +static struct conversion_info +{ + enum bytecode_opcode opcode; /* here indicates the conversion needs no opcode. */ + enum typecode from; + enum typecode to; + int cost; /* 1 for no-op conversions, 2 for widening conversions, + 4 for int/float conversions, 8 for narrowing conversions. */ +} conversion_info[] = { + { -1, QIcode, QUcode, 1 }, + { -1, HIcode, HUcode, 1 }, + { -1, SIcode, SUcode, 1 }, + { -1, DIcode, DUcode, 1 }, + { -1, QUcode, QIcode, 1 }, + { -1, HUcode, HIcode, 1 }, + { -1, SUcode, SIcode, 1 }, + { -1, DUcode, DIcode, 1 }, + { -1, Tcode, SIcode, 1 }, + { convertQIHI, QIcode, HIcode, 2 }, + { convertQUHU, QUcode, HUcode, 2 }, + { convertQUSU, QUcode, SUcode, 2 }, + { convertHISI, HIcode, SIcode, 2 }, + { convertHUSU, HUcode, SUcode, 2 }, + { convertSIDI, SIcode, DIcode, 2 }, + { convertSUDU, SUcode, DUcode, 2 }, + { convertSFDF, SFcode, DFcode, 2 }, + { convertDFXF, DFcode, XFcode, 2 }, + { convertHIQI, HIcode, QIcode, 8 }, + { convertSIQI, SIcode, QIcode, 8 }, + { convertSIHI, SIcode, HIcode, 8 }, + { convertSUQU, SUcode, QUcode, 8 }, + { convertDISI, DIcode, SIcode, 8 }, + { convertDFSF, DFcode, SFcode, 8 }, + { convertXFDF, XFcode, DFcode, 8 }, + { convertPSI, Pcode, SIcode, 2 }, + { convertSIP, SIcode, Pcode, 2 }, + { convertSIT, SIcode, Tcode, 2 }, + { convertDIT, DIcode, Tcode, 2 }, + { convertSFT, SFcode, Tcode, 2 }, + { convertDFT, DFcode, Tcode, 2 }, + { convertXFT, XFcode, Tcode, 2 }, + { convertQISI, QIcode, SIcode, 2 }, + { convertPT, Pcode, Tcode, 2 }, + { convertSISF, SIcode, SFcode, 4 }, + { convertSIDF, SIcode, DFcode, 4 }, + { convertSIXF, SIcode, XFcode, 4 }, + { convertSUSF, SUcode, SFcode, 4 }, + { convertSUDF, SUcode, DFcode, 4 }, + { convertSUXF, SUcode, XFcode, 4 }, + { convertDISF, DIcode, SFcode, 4 }, + { convertDIDF, DIcode, DFcode, 4 }, + { convertDIXF, DIcode, XFcode, 4 }, + { convertDUSF, DUcode, SFcode, 4 }, + { convertDUDF, DUcode, DFcode, 4 }, + { convertDUXF, DUcode, XFcode, 4 }, + { convertSFSI, SFcode, SIcode, 4 }, + { convertDFSI, DFcode, SIcode, 4 }, + { convertXFSI, XFcode, SIcode, 4 }, + { convertSFSU, SFcode, SUcode, 4 }, + { convertDFSU, DFcode, SUcode, 4 }, + { convertXFSU, XFcode, SUcode, 4 }, + { convertSFDI, SFcode, DIcode, 4 }, + { convertDFDI, DFcode, DIcode, 4 }, + { convertXFDI, XFcode, DIcode, 4 }, + { convertSFDU, SFcode, DUcode, 4 }, + { convertDFDU, DFcode, DUcode, 4 }, + { convertXFDU, XFcode, DUcode, 4 }, + { convertSIQI, SIcode, QIcode, 8 }, +}; + +#define NUM_CONVERSIONS (sizeof conversion_info / sizeof (struct conversion_info)) + +/* List form of a conversion recipe. */ +struct conversion_list +{ + enum bytecode_opcode opcode; + enum typecode to; + int cost; + struct conversion_list *prev; +}; + +/* Determine if it is "reasonable" to add a given conversion to + a given list of conversions. The following criteria define + "reasonable" conversion lists: + * No typecode appears more than once in the sequence (no loops). + * At most one conversion from integer to float or vice versa is present. + * Either sign extensions or zero extensions may be present, but not both. + * No widening conversions occur after a signed/unsigned conversion. + * The sequence of sizes must be strict nonincreasing or nondecreasing. */ +static int +conversion_reasonable_p (conversion, list) + struct conversion_info *conversion; + struct conversion_list *list; +{ + struct conversion_list *curr; + int curr_size, prev_size; + int has_int_float, has_float_int; + int has_sign_extend, has_zero_extend; + int has_signed_unsigned, has_unsigned_signed; + + has_int_float = 0; + has_float_int = 0; + has_sign_extend = 0; + has_zero_extend = 0; + has_signed_unsigned = 0; + has_unsigned_signed = 0; + + /* Make sure the destination typecode doesn't already appear in + the list. */ + for (curr = list; curr; curr = curr->prev) + if (conversion->to == curr->to) + return 0; + + /* Check for certain kinds of conversions. */ + if (TYPECODE_INTEGER_P (conversion->from) + && TYPECODE_FLOAT_P (conversion->to)) + has_int_float = 1; + if (TYPECODE_FLOAT_P (conversion->from) + && TYPECODE_INTEGER_P (conversion->to)) + has_float_int = 1; + if (TYPECODE_SIGNED_P (conversion->from) + && TYPECODE_SIGNED_P (conversion->to) + && GET_TYPECODE_SIZE (conversion->from) + < GET_TYPECODE_SIZE (conversion->to)) + has_sign_extend = 1; + if (TYPECODE_UNSIGNED_P (conversion->from) + && TYPECODE_UNSIGNED_P (conversion->to) + && GET_TYPECODE_SIZE (conversion->from) + < GET_TYPECODE_SIZE (conversion->to)) + has_zero_extend = 1; + + for (curr = list; curr && curr->prev; curr = curr->prev) + { + if (TYPECODE_INTEGER_P (curr->prev->to) + && TYPECODE_FLOAT_P (curr->to)) + has_int_float = 1; + if (TYPECODE_FLOAT_P (curr->prev->to) + && TYPECODE_INTEGER_P (curr->to)) + has_float_int = 1; + if (TYPECODE_SIGNED_P (curr->prev->to) + && TYPECODE_SIGNED_P (curr->to) + && GET_TYPECODE_SIZE (curr->prev->to) + < GET_TYPECODE_SIZE (curr->to)) + has_sign_extend = 1; + if (TYPECODE_UNSIGNED_P (curr->prev->to) + && TYPECODE_UNSIGNED_P (curr->to) + && GET_TYPECODE_SIZE (curr->prev->to) + < GET_TYPECODE_SIZE (curr->to)) + has_zero_extend = 1; + if (TYPECODE_SIGNED_P (curr->prev->to) + && TYPECODE_UNSIGNED_P (curr->to)) + has_signed_unsigned = 1; + if (TYPECODE_UNSIGNED_P (curr->prev->to) + && TYPECODE_SIGNED_P (curr->to)) + has_unsigned_signed = 1; + } + + if (TYPECODE_INTEGER_P (conversion->from) + && TYPECODE_INTEGER_P (conversion->to) + && GET_TYPECODE_SIZE (conversion->to) + > GET_TYPECODE_SIZE (conversion->from) + && (has_signed_unsigned || has_unsigned_signed)) + return 0; + + if (has_float_int && has_int_float || has_sign_extend && has_zero_extend) + return 0; + + /* Make sure the sequence of destination typecode sizes is + strictly nondecreasing or strictly nonincreasing. */ + prev_size = GET_TYPECODE_SIZE (conversion->to); + for (curr = list; curr; curr = curr->prev) + { + curr_size = GET_TYPECODE_SIZE (curr->to); + if (curr_size != prev_size) + break; + } + if (!curr) + return 1; + + if (curr_size < prev_size) + for (prev_size = curr_size; curr; curr = curr->prev) + { + curr_size = GET_TYPECODE_SIZE (curr->to); + if (curr_size > prev_size) + return 0; + prev_size = curr_size; + } + else + for (prev_size = curr_size; curr; curr = curr->prev) + { + curr_size = GET_TYPECODE_SIZE (curr->to); + if (curr_size < prev_size) + return 0; + prev_size = curr_size; + } + return 1; +} + + +/* Exhaustively search all reasonable conversions to find one to + convert the given types. */ +static struct conversion_recipe +deduce_conversion (from, to) + enum typecode from, to; +{ + struct rl + { + struct conversion_list *list; + struct rl *next; + } *prev, curr, *good, *temp; + struct conversion_list *conv, *best; + int i, cost, bestcost; + struct conversion_recipe result; + struct obstack recipe_obstack; + + + obstack_init (&recipe_obstack); + curr.next = (struct rl *) obstack_alloc (&recipe_obstack, sizeof (struct rl)); + curr.next->list = + (struct conversion_list *) obstack_alloc (&recipe_obstack, + sizeof (struct conversion_list)); + curr.next->list->opcode = -1; + curr.next->list->to = from; + curr.next->list->cost = 0; + curr.next->list->prev = 0; + curr.next->next = 0; + good = 0; + + while (curr.next) + { + /* Remove successful conversions from further consideration. */ + for (prev = &curr; prev; prev = prev->next) + if (prev->next && prev->next->list->to == to) + { + temp = prev->next->next; + prev->next->next = good; + good = prev->next; + prev->next = temp; + } + + /* Go through each of the pending conversion chains, trying + all possible candidate conversions on them. */ + for (prev = curr.next, curr.next = 0; prev; prev = prev->next) + for (i = 0; i < NUM_CONVERSIONS; ++i) + if (conversion_info[i].from == prev->list->to + && conversion_reasonable_p (&conversion_info[i], prev->list)) + { + temp = (struct rl *) obstack_alloc (&recipe_obstack, + sizeof (struct rl)); + temp->list = (struct conversion_list *) + obstack_alloc (&recipe_obstack, + sizeof (struct conversion_list)); + temp->list->opcode = conversion_info[i].opcode; + temp->list->to = conversion_info[i].to; + temp->list->cost = conversion_info[i].cost; + temp->list->prev = prev->list; + temp->next = curr.next; + curr.next = temp; + } + } + + bestcost = BIG_ARBITRARY_NUMBER; + best = 0; + for (temp = good; temp; temp = temp->next) + { + for (conv = temp->list, cost = 0; conv; conv = conv->prev) + cost += conv->cost; + if (cost < bestcost) + { + bestcost = cost; + best = temp->list; + } + } + + if (!best) + abort (); + + for (i = 0, conv = best; conv; conv = conv->prev) + if (conv->opcode != -1) + ++i; + + result.opcodes = (unsigned char *) xmalloc (i); + result.nopcodes = i; + for (conv = best; conv; conv = conv->prev) + if (conv->opcode != -1) + result.opcodes[--i] = conv->opcode; + result.cost = bestcost; + obstack_free (&recipe_obstack, 0); + return result; +} + +#define DEDUCE_CONVERSION(FROM, TO) \ + (conversion_recipe[FROM][TO].opcodes ? 0 \ + : (conversion_recipe[FROM][TO] = deduce_conversion (FROM, TO), 0)) + + +/* Emit a conversion between the given scalar types. */ +void +emit_typecode_conversion (from, to) + enum typecode from, to; +{ + int i; + + DEDUCE_CONVERSION (from, to); + for (i = 0; i < conversion_recipe[from][to].nopcodes; ++i) + bc_emit_instruction (conversion_recipe[from][to].opcodes[i]); +} + + +/* Initialize mode_to_code_map[] */ +void +bc_init_mode_to_code_map () +{ + int mode; + + for (mode = 0; mode < MAX_MACHINE_MODE + 1; mode++) + { + signed_mode_to_code_map[mode] = + unsigned_mode_to_code_map[mode] = + LAST_AND_UNUSED_TYPECODE; + } + +#define DEF_MODEMAP(SYM, CODE, UCODE, CONST, LOAD, STORE) \ + { signed_mode_to_code_map[(enum machine_mode) SYM] = CODE; \ + unsigned_mode_to_code_map[(enum machine_mode) SYM] = UCODE; } +#include "modemap.def" +#undef DEF_MODEMAP + + /* Initialize opcode maps for const, load, and store */ + bc_init_mode_to_opcode_maps (); +} + +/* Given a machine mode return the preferred typecode. */ +enum typecode +preferred_typecode (mode, unsignedp) + enum machine_mode mode; + int unsignedp; +{ + enum typecode code = (unsignedp + ? unsigned_mode_to_code_map + : signed_mode_to_code_map) [MIN (mode, MAX_MACHINE_MODE)]; + + if (code == LAST_AND_UNUSED_TYPECODE) + abort (); + + return code; +} + + +/* Expand a conversion between the given types. */ +void +bc_expand_conversion (from, to) + tree from, to; +{ + enum typecode fcode, tcode; + + fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from)); + tcode = preferred_typecode (TYPE_MODE (to), TREE_UNSIGNED (to)); + + emit_typecode_conversion (fcode, tcode); +} + +/* Expand a conversion of the given type to a truth value. */ +void +bc_expand_truth_conversion (from) + tree from; +{ + enum typecode fcode; + + fcode = preferred_typecode (TYPE_MODE (from), TREE_UNSIGNED (from)); + emit_typecode_conversion (fcode, Tcode); +} + +/* Emit an appropriate binary operation. */ +void +bc_expand_binary_operation (optab, resulttype, arg0, arg1) + struct binary_operator optab[]; + tree resulttype, arg0, arg1; +{ + int i, besti, cost, bestcost; + enum typecode resultcode, arg0code, arg1code; + + resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype)); + arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (resulttype)); + arg1code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg1)), TREE_UNSIGNED (resulttype)); + + besti = -1; + bestcost = BIG_ARBITRARY_NUMBER; + + for (i = 0; optab[i].opcode != -1; ++i) + { + cost = 0; + DEDUCE_CONVERSION (arg0code, optab[i].arg0); + cost += conversion_recipe[arg0code][optab[i].arg0].cost; + DEDUCE_CONVERSION (arg1code, optab[i].arg1); + cost += conversion_recipe[arg1code][optab[i].arg1].cost; + if (cost < bestcost) + { + besti = i; + bestcost = cost; + } + } + + if (besti == -1) + abort (); + + expand_expr (arg1); + emit_typecode_conversion (arg1code, optab[besti].arg1); + expand_expr (arg0); + emit_typecode_conversion (arg0code, optab[besti].arg0); + bc_emit_instruction (optab[besti].opcode); + emit_typecode_conversion (optab[besti].result, resultcode); +} + +/* Emit an appropriate unary operation. */ +void +bc_expand_unary_operation (optab, resulttype, arg0) + struct unary_operator optab[]; + tree resulttype, arg0; +{ + int i, besti, cost, bestcost; + enum typecode resultcode, arg0code; + + resultcode = preferred_typecode (TYPE_MODE (resulttype), TREE_UNSIGNED (resulttype)); + arg0code = preferred_typecode (TYPE_MODE (TREE_TYPE (arg0)), TREE_UNSIGNED (TREE_TYPE (arg0))); + + besti = -1; + bestcost = BIG_ARBITRARY_NUMBER; + + for (i = 0; optab[i].opcode != -1; ++i) + { + DEDUCE_CONVERSION (arg0code, optab[i].arg0); + cost = conversion_recipe[arg0code][optab[i].arg0].cost; + if (cost < bestcost) + { + besti = i; + bestcost = cost; + } + } + + if (besti == -1) + abort (); + + expand_expr (arg0); + emit_typecode_conversion (arg0code, optab[besti].arg0); + bc_emit_instruction (optab[besti].opcode); + emit_typecode_conversion (optab[besti].result, resultcode); +} + + +/* Emit an appropriate increment. */ +void +bc_expand_increment (optab, type) + struct increment_operator optab[]; + tree type; +{ + enum typecode code; + int i; + + code = preferred_typecode (TYPE_MODE (type), TREE_UNSIGNED (type)); + for (i = 0; optab[i].opcode >= 0; ++i) + if (code == optab[i].arg) + { + bc_emit_instruction (optab[i].opcode); + return; + } + abort (); +} diff --git a/gcc/bc-typecd.h b/gcc/bc-typecd.h new file mode 100644 index 00000000000..243cd3dea63 --- /dev/null +++ b/gcc/bc-typecd.h @@ -0,0 +1,53 @@ +/* Typecode definitions for Bytecode Interpreter. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#ifndef TYPECODE_H +#define TYPECODE_H + +enum typecode +{ +#define DEFTYPECODE(CODE, NAME, MACHMODE, TYPE) CODE, +#include "bc-typecd.def" +#undef DEFTYPECODE + + LAST_AND_UNUSED_TYPECODE +}; + +/* Determine if a given type is integer. */ +#define TYPECODE_INTEGER_P(TYPECODE) ((TYPECODE) < SFcode) + +/* Determine if a given type is unsigned. */ +#define TYPECODE_UNSIGNED_P(TYPECODE) \ + (TYPECODE_INTEGER_P(TYPECODE) && (TYPECODE) & 1) + +/* Determine if a given type is signed. */ +#define TYPECODE_SIGNED_P(TYPECODE) \ + (TYPECODE_INTEGER_P(TYPECODE) && !((TYPECODE) & 1)) + +/* Determine if a given type is floating. */ +#define TYPECODE_FLOAT_P(TYPECODE) \ + ((TYPECODE) < Pcode && !TYPECODE_INTEGER_P(TYPECODE)) + +/* Determine if the given type is arithmetic. */ +#define TYPECODE_ARITH_P(TYPECODE) \ + (TYPECODE_INTEGER_P(TYPECODE) || TYPECODE_FLOAT_P(TYPECODE)) + +#define NUM_TYPECODES ((int) LAST_AND_UNUSED_TYPECODE) + +#endif diff --git a/gcc/bi-opcode.c b/gcc/bi-opcode.c new file mode 100644 index 00000000000..772b1fed3ad --- /dev/null +++ b/gcc/bi-opcode.c @@ -0,0 +1,53 @@ +/* Utility to generate opcode list from bytecode definition. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +#include +#include "bi-defs.h" + +int +main(argc, argv) + int argc; + char **argv; +{ + struct def *d; + struct variation *v; + int i; + + yyparse(); + reverse(); + + + puts ("/* This file is automatically generated from bytecode.def, do not make\n\ +any changes here. Instead edit bytecode.def and type ``make''. */\n\ +enum bytecode_opcode\n{"); + + i = 0; + for (d = defs; d; d = d->next) + for (v = d->variations; v; v = v->next) + printf (" %s%s,\n", d->basename, v->name, i++); + + puts (" LAST_AND_UNUSED_OPCODE\n};"); + + if (i > 256) + fprintf (stderr, "%s: warning, number of opcodes is %d\n", *argv, i); + else + fprintf (stderr, "(Number of opcodes is %d)\n", i); + + return 0; +} diff --git a/gcc/bi-parser.y b/gcc/bi-parser.y new file mode 100644 index 00000000000..07d1319cb95 --- /dev/null +++ b/gcc/bi-parser.y @@ -0,0 +1,168 @@ +/* Bytecode definition file parser. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + + +%{ + +#include +#include "bi-defs.h" + +extern char yytext[]; +extern int yyleng; + +extern char *malloc(); + + +/* Chain of all defs built by the parser. */ +struct def *defs; +int ndefs; + +static struct node *makenode(); +static struct variation *makevar(); +static struct def *makedef(); + +%} + +%union{ + char *string; + struct def *def; + struct variation *variation; + struct node *node; +} + +%token DEFOP STRING +%type opt_string +%type defs def +%type variations variation +%type list items item + +%% + +top: + defs + { defs = $1; } + ; + +defs: + def + | defs def + { $2->next = $1; $$ = $2; } + ; + +def: + DEFOP '(' STRING ',' opt_string ',' '(' variations ')' ')' + { $$ = makedef($3, $5, $8); } + ; + +variations: + variation + | variations ',' variation + { $3->next = $1; $$ = $3; } + ; + +variation: + '(' opt_string ')' + { $$ = makevar($2, (struct node *) NULL, (struct node *) NULL, (struct node *) NULL); } + | '(' opt_string ',' list ')' + { $$ = makevar($2, $4, (struct node *) NULL, (struct node *) NULL); } + | '(' opt_string ',' list ',' list ')' + { $$ = makevar($2, $4, $6, (struct node *) NULL); } + | '(' opt_string ',' list ',' list ',' list ')' + { $$ = makevar($2, $4, $6, $8); } + ; + +opt_string: + /* empty */ { $$ = ""; } + | STRING { $$ = $1; } + ; + +list: + '(' items ')' + { $$ = $2; } + | /* empty */ + { $$ = NULL; } + ; + +items: + item + /* Note right recursion. */ + | item ',' items + { $1->next = $3; $$ = $1; } + ; + +item: + STRING + { $$ = makenode($1); } + ; + +%% + +static struct node * +makenode(s) + char *s; +{ + struct node *n; + + n = (struct node *) malloc(sizeof (struct node)); + n->text = s; + n->next = NULL; + return n; +} + +static struct variation * +makevar(name, inputs, outputs, literals) + char *name; + struct node *inputs, *outputs, *literals; +{ + struct variation *v; + + v = (struct variation *) malloc(sizeof (struct variation)); + v->name = name; + v->code = ndefs++; + v->inputs = inputs; + v->outputs = outputs; + v->literals = literals; + v->next = NULL; + return v; +} + +static struct def * +makedef(name, template, vars) + char *name, *template; + struct variation *vars; +{ + struct def *d; + + d = (struct def *) malloc(sizeof (struct def)); + d->basename = name; + d->template = template; + d->variations = vars; + d->next = NULL; + return d; +} + +void +yyerror(s) + char *s; +{ + extern int yylineno; + + fprintf(stderr, "syntax error in line %d\n", yylineno); + exit(1); +} diff --git a/gcc/bi-run.h b/gcc/bi-run.h new file mode 100644 index 00000000000..4c413ebef98 --- /dev/null +++ b/gcc/bi-run.h @@ -0,0 +1,184 @@ +/* Definitions for Bytecode Interpreter. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +typedef union stacktype +{ + QItype QIval; + QUtype QUval; + HItype HIval; + HUtype HUval; + SItype SIval; + SUtype SUval; + DItype DIval; + DUtype DUval; + SFtype SFval; + DFtype DFval; + XFtype XFval; + Ptype Pval; + Ttype Tval; +} stacktype; + +#define MAXLITERALS 5 + +struct arityvec +{ + char ninputs; + char noutputs; + char nliterals; + char literals[MAXLITERALS]; +}; + +struct argtype +{ + int modealign; /* Argument mode:alignment */ + int size; /* Argument size, in bytes */ +}; + +struct callinfo +{ + int nargs; /* Number of arguments in call */ + struct argtype retvaltype; /* Type of return value */ + struct argtype argtypes[1]; /* Argument types */ +}; + +/* Structure describing a bytecode function. If this changes, we also + need to change expand_function_end () in bc-trans.c */ +struct bytecode +{ + int stacksize; /* Depth required of evaluation stack. */ + int localsize; /* Size in bytes of local variables. */ + unsigned char *pc0; /* Initial program counter. */ + void **ptrlit; /* Vector of (relocatable) pointer literals. */ + struct callinfo *callinfo; /* Vector of procedure call type info. */ +}; + + +#define INTERP_BPC 8 /* Bits per char */ +#define INTERP_BPI \ + (sizeof (int) * INTERP_BPC) /* Bits per int */ + + +#ifndef min +#define min(L, R) ((L) < (R) ? (L) : (R)) +#endif + + +/* bit field operations. */ + +/* Low (high) mask: int with low (high) N bits set */ + +#define LM(N) ((1 << (N)) - 1) +#define HM(N) ((~LM (INTERP_BPI - (N)))) + + +/* Sign-extend SIZE low bits of VALUE to integer (typeof VALUE) + Signed bitfields are loaded from memory by the sxloadBI instruction, + which first retrieves the bitfield with XFIELD and then sign extends + it to an SItype. */ + +#define EXTEND(SIZE, VALUE) \ + ({ SUtype value = (SUtype) (VALUE); \ + (value & (1 << ((SIZE) - 1)) ? value | ~LM (SIZE) : value); }) + + +/* Given OFFSET:SIZE for a bitfield, calculate: + + [1] BYTE_OFFSET = the byte offset of the bit field. + [2] BIT_OFFSET = the bit offset of the bit field (less than INTERP_BPC). + [3] NBYTES = the number of integral bytes in the bit field. + [4] TRAILING_BITS= the number of trailing bits (less than INTERP_BPC). + + + , , , , , (memory bytes) + ---------------- (bitfield) + | | || | | (divisions) + ^ ^ ^ ^ + | | | |__ [4] (bits) + | | |_________ [3] (bytes) + | |_________________ [2] (bits) + |___________________________ [1] (bytes) + + + The above applies to BYTE_LOW_ENDIAN machines. In BYTE_BIG_ENDIAN machines, the + bit numbering is reversed (i.e. bit 0 is the sign bit). + + (Alright, so I drew this to keep my tongue in cheek while writing the code below, + not because I'm into ASCII art.) */ + + +#define BI_PARAMS(OFFSET, SIZE, BYTE_OFFSET, \ + BIT_OFFSET, NBYTES, TRAILING_BITS ) \ +\ + { BYTE_OFFSET = (OFFSET) / (INTERP_BPC); \ + BIT_OFFSET = (OFFSET) % (INTERP_BPC); \ + NBYTES = ((SIZE) - (INTERP_BPC - (BIT_OFFSET))) / INTERP_BPC; \ + if ((NBYTES) < 0 || ((NBYTES) > 64)) \ + NBYTES = 0; \ + if ((SIZE) + (BIT_OFFSET) <= INTERP_BPC) \ + TRAILING_BITS = 0; \ + else \ + TRAILING_BITS = ((SIZE) - (INTERP_BPC - (BIT_OFFSET))) % INTERP_BPC; } + + +/* SHIFT_IN_BITS retrieves NBITS bits from SOURCE and shifts into + DEST. The bit field starts OFFSET bits into SOURCE. + + OR_IN_BITS copies the NBITS low bits from VALUE into a the bitfield in + DEST offset by OFFSET bits. */ + + +#ifdef BYTES_BIG_ENDIAN + +#define SHIFT_IN_BITS(DEST, SOURCE, OFFSET, NBITS) \ + (DEST = ((DEST) << (NBITS)) \ + | (LM ((NBITS)) \ + & ((SOURCE) >> (INTERP_BPC - (OFFSET) - (NBITS))))) + +#define OR_IN_BITS(DEST, VALUE, OFFSET, NBITS) \ + (DEST = ((DEST) & ~(LM ((NBITS)) << (INTERP_BPC - (OFFSET) - (NBITS)))) \ + | (((VALUE) & LM ((NBITS))) << (INTERP_BPC - (OFFSET) - (NBITS)))) + +#else + +#define SHIFT_IN_BITS(DEST, SOURCE, OFFSET, NBITS) \ + (DEST = ((DEST) << (NBITS)) \ + | (LM ((NBITS)) \ + & ((SOURCE) >> (OFFSET)))) + +#define OR_IN_BITS(DEST, VALUE, OFFSET, NBITS) \ + (DEST = ((DEST) & ~(LM ((NBITS)) << (OFFSET))) \ + | (((VALUE) & LM ((NBITS))) << (OFFSET))) + +#endif + + +/* Procedure call; arguments are a pointer to the function to be called, + a pointer to a place to store the return value, a pointer to a vector + describing the type of procedure call, and the interpreter's stack pointer, + which will point to the first of the arguments at this point. */ + +#define CALL(FUNC, CALLDESC, RETVAL, SP) __call(FUNC, CALLDESC, RETVAL, SP) + + +/* Procedure return; arguments are a pointer to the calldesc for this + function, and a pointer to the place where the value to be returned + may be found. Generally the MACHARGS above contain a machine dependent + cookie that is used to determine where to jump to. */ + +#define PROCRET(CALLDESC, RETVAL) return diff --git a/gcc/bytecode.h b/gcc/bytecode.h new file mode 100644 index 00000000000..6e9731254c8 --- /dev/null +++ b/gcc/bytecode.h @@ -0,0 +1,105 @@ +/* Bytecode definitions for GNU C-compiler. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +/* These should come from genemit */ +typedef signed char QItype; +typedef unsigned char QUtype; +typedef signed short int HItype; +typedef unsigned short int HUtype; +typedef signed long int SItype; +typedef unsigned long int SUtype; +typedef signed long long int DItype; +typedef unsigned long long int DUtype; +typedef float SFtype; +typedef double DFtype; +typedef long double XFtype; +typedef char *Ptype; +typedef int Ttype; + +extern int output_bytecode; +extern int stack_depth; +extern int max_stack_depth; + +/* Emit DI constant according to target machine word ordering */ + +#ifdef WORD_HIGH_ENDIAN + +#define bc_emit_bytecode_DI_const(CST) \ +{ int opcode; \ + opcode = TREE_INT_CST_HIGH (CST); \ + bc_emit_bytecode_const ((char *) &opcode, sizeof opcode); \ + opcode = TREE_INT_CST_LOW (CST); \ + bc_emit_bytecode_const ((char *) &opcode, sizeof opcode); \ +} + +#else + +#define bc_emit_bytecode_DI_const(CST) \ +{ int opcode; \ + opcode = TREE_INT_CST_LOW (CST); \ + bc_emit_bytecode_const ((char *) &opcode, sizeof opcode); \ + opcode = TREE_INT_CST_HIGH (CST); \ + bc_emit_bytecode_const ((char *) &opcode, sizeof opcode); \ +} + +#endif + + +extern void bc_expand_expr (); +extern void bc_output_data_constructor (); +extern void bc_store_field (); +extern void bc_load_bit_field (); +extern void bc_store_bit_field (); +extern void bc_push_offset_and_size (); +extern void bc_init_mode_to_code_map (); + +/* These are just stubs, so the compiler will compile for targets + that aren't yet supported by the bytecode generator. */ + +#ifndef TARGET_SUPPORTS_BYTECODE + +#define MACHINE_SEG_ALIGN 1 +#define INT_ALIGN 1 +#define PTR_ALIGN 1 +#define NAMES_HAVE_UNDERSCORES +#define BC_NOP (0) +#define BC_GLOBALIZE_LABEL(FP, NAME) BC_NOP +#define BC_OUTPUT_COMMON(FP, NAME, SIZE, ROUNDED) BC_NOP +#define BC_OUTPUT_LOCAL(FP, NAME, SIZE, ROUNDED) BC_NOP +#define BC_OUTPUT_ALIGN(FP, ALIGN) BC_NOP +#define BC_OUTPUT_LABEL(FP, NAME) BC_NOP +#define BC_OUTPUT_SKIP(FP, SIZE) BC_NOP +#define BC_OUTPUT_LABELREF(FP, NAME) BC_NOP +#define BC_OUTPUT_FLOAT(FP, VAL) BC_NOP +#define BC_OUTPUT_DOUBLE(FP, VAL) BC_NOP +#define BC_OUTPUT_BYTE(FP, VAL) BC_NOP +#define BC_OUTPUT_FILE ASM_OUTPUT_FILE +#define BC_OUTPUT_ASCII ASM_OUTPUT_ASCII +#define BC_OUTPUT_IDENT ASM_OUTPUT_IDENT +#define BCXSTR(RTX) ((RTX)->bc_label) +#define BC_WRITE_FILE(FP) BC_NOP +#define BC_WRITE_SEGSYM(SEGSYM, FP) BC_NOP +#define BC_WRITE_RELOC_ENTRY(SEGRELOC, FP, OFFSET) BC_NOP +#define BC_START_BYTECODE_LINE(FP) BC_NOP +#define BC_WRITE_BYTECODE(SEP, VAL, FP) BC_NOP +#define BC_WRITE_RTL(R, FP) BC_NOP +#define BC_EMIT_TRAMPOLINE(TRAMPSEG, CALLINFO) BC_NOP +#define VALIDATE_STACK BC_NOP + +#endif /* !TARGET_SUPPORTS_BYTECODE */ diff --git a/gcc/modemap.def b/gcc/modemap.def new file mode 100644 index 00000000000..32576403ef9 --- /dev/null +++ b/gcc/modemap.def @@ -0,0 +1,30 @@ +/* Bytecode specific machine mode info for GNU C-compiler. + Copyright (C) 1993 Free Software Foundation, Inc. + +This file is part of GNU CC. + +GNU CC is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the 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 of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU CC; see the file COPYING. If not, write to +the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ + +/* Map mode to signed, unsigned typecodes, bytecode to push const, + to load, to store */ +DEF_MODEMAP(QImode, QIcode, QUcode, constQI, loadQI, storeQI) +DEF_MODEMAP(HImode, HIcode, HUcode, constHI, loadHI, storeHI) +DEF_MODEMAP(VOIDmode, SIcode, SUcode, constSI, loadSI, storeSI) +DEF_MODEMAP(SImode, SIcode, SUcode, constSI, loadSI, storeSI) +DEF_MODEMAP(DImode, DIcode, DUcode, constDI, loadDI, storeDI) +DEF_MODEMAP(PSImode, Pcode, Pcode, constP, loadP, storeP) +DEF_MODEMAP(BLKmode, Pcode, Pcode, constP, loadP, neverneverland) +DEF_MODEMAP(SFmode, SFcode, SFcode, constSF, loadSF, storeSF) +DEF_MODEMAP(DFmode, DFcode, DFcode, constDF, loadDF, storeDF)