1987-03-10 17:51:10 +00:00
|
|
|
/*
|
|
|
|
* (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
|
|
|
|
* See the copyright notice in the ACK home directory, in the file "Copyright".
|
|
|
|
*/
|
1986-03-10 13:07:55 +00:00
|
|
|
/* $Header$ */
|
|
|
|
/* CODE FOR THE INITIALISATION OF GLOBAL VARIABLES */
|
|
|
|
|
1986-09-24 13:53:16 +00:00
|
|
|
#include "nofloat.h"
|
1986-03-25 16:40:43 +00:00
|
|
|
#include <em.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "debug.h"
|
1987-02-23 13:08:54 +00:00
|
|
|
#include <alloc.h>
|
1986-03-10 13:07:55 +00:00
|
|
|
#include "nobitfield.h"
|
|
|
|
#include "arith.h"
|
|
|
|
#include "align.h"
|
|
|
|
#include "label.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "type.h"
|
|
|
|
#include "struct.h"
|
|
|
|
#include "field.h"
|
|
|
|
#include "assert.h"
|
|
|
|
#include "Lpars.h"
|
|
|
|
#include "class.h"
|
|
|
|
#include "sizes.h"
|
|
|
|
#include "idf.h"
|
|
|
|
#include "level.h"
|
|
|
|
#include "def.h"
|
|
|
|
|
1986-03-26 16:58:43 +00:00
|
|
|
#define con_nullbyte() C_con_ucon("0", (arith)1)
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-03-26 16:58:43 +00:00
|
|
|
char *symbol2str();
|
|
|
|
char *long2str();
|
1987-03-20 08:30:31 +00:00
|
|
|
char *strncpy();
|
1986-03-10 13:07:55 +00:00
|
|
|
struct expr *do_array(), *do_struct(), *IVAL();
|
1986-12-10 12:00:55 +00:00
|
|
|
extern char options[];
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
/* do_ival() performs the initialisation of a global variable
|
|
|
|
of type tp with the initialisation expression expr by calling IVAL().
|
|
|
|
Guided by type tp, the expression is evaluated.
|
|
|
|
*/
|
1986-04-03 11:33:32 +00:00
|
|
|
do_ival(tpp, ex)
|
1986-03-10 13:07:55 +00:00
|
|
|
struct type **tpp;
|
1986-04-03 11:33:32 +00:00
|
|
|
struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-04-03 11:33:32 +00:00
|
|
|
if (IVAL(tpp, ex) != 0)
|
|
|
|
too_many_initialisers(ex);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* IVAL() recursively guides the initialisation expression through the
|
|
|
|
different routines for the different types of initialisation:
|
|
|
|
- array initialisation
|
|
|
|
- struct initialisation
|
|
|
|
- fundamental type initialisation
|
|
|
|
Upto now, the initialisation of a union is not allowed!
|
|
|
|
An initialisation expression tree consists of normal expressions
|
|
|
|
which can be joined together by ',' nodes, which operator acts
|
|
|
|
like the lisp function "cons" to build lists.
|
|
|
|
IVAL() returns a pointer to the remaining expression tree.
|
|
|
|
*/
|
|
|
|
struct expr *
|
1986-04-03 11:33:32 +00:00
|
|
|
IVAL(tpp, ex)
|
1986-03-10 13:07:55 +00:00
|
|
|
struct type **tpp; /* type of global variable */
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct expr *ex; /* initialiser expression */
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
register struct type *tp = *tpp;
|
|
|
|
|
|
|
|
switch (tp->tp_fund) {
|
1986-09-29 14:01:34 +00:00
|
|
|
case ARRAY: /* array initialisation */
|
1986-03-10 13:07:55 +00:00
|
|
|
if (valid_type(tp->tp_up, "array element") == 0)
|
|
|
|
return 0;
|
1986-04-03 11:33:32 +00:00
|
|
|
if (ISCOMMA(ex)) /* list of initialisation expressions */
|
|
|
|
return do_array(ex, tpp);
|
|
|
|
if (tp->tp_up->tp_fund == CHAR && ex->ex_class == String)
|
1986-09-29 14:01:34 +00:00
|
|
|
/* initialisation like char s[] = "I am a string" */
|
1986-08-25 08:47:07 +00:00
|
|
|
ch_array(tpp, ex);
|
1986-03-25 16:40:43 +00:00
|
|
|
else /* " int i[24] = 12;" */
|
1986-04-03 11:33:32 +00:00
|
|
|
check_and_pad(ex, tpp);
|
1986-04-02 08:37:17 +00:00
|
|
|
break;
|
1986-09-29 14:01:34 +00:00
|
|
|
case STRUCT: /* struct initialisation */
|
1986-03-10 13:07:55 +00:00
|
|
|
if (valid_type(tp, "struct") == 0)
|
|
|
|
return 0;
|
1986-04-03 11:33:32 +00:00
|
|
|
if (ISCOMMA(ex)) /* list of initialisation expressions */
|
|
|
|
return do_struct(ex, tp);
|
1986-09-29 14:01:34 +00:00
|
|
|
check_and_pad(ex, tpp); /* "struct foo f = 12;" */
|
1986-04-02 08:37:17 +00:00
|
|
|
break;
|
1986-03-14 16:15:16 +00:00
|
|
|
case UNION:
|
1986-03-10 13:07:55 +00:00
|
|
|
error("union initialisation not allowed");
|
1986-04-02 08:37:17 +00:00
|
|
|
break;
|
1986-03-10 13:07:55 +00:00
|
|
|
case ERRONEOUS:
|
1986-04-02 08:37:17 +00:00
|
|
|
break;
|
1986-03-25 16:40:43 +00:00
|
|
|
default: /* fundamental type */
|
1986-09-29 14:01:34 +00:00
|
|
|
if (ISCOMMA(ex)) { /* " int i = {12};" */
|
1986-04-03 11:33:32 +00:00
|
|
|
if (IVAL(tpp, ex->OP_LEFT) != 0)
|
|
|
|
too_many_initialisers(ex);
|
1986-03-10 13:07:55 +00:00
|
|
|
/* return remainings of the list for the
|
|
|
|
other members of the aggregate, if this
|
|
|
|
item belongs to an aggregate.
|
|
|
|
*/
|
1986-04-03 11:33:32 +00:00
|
|
|
return ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
check_ival(ex, tp); /* "int i = 12;" */
|
1986-04-02 08:37:17 +00:00
|
|
|
break;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
1986-04-02 08:37:17 +00:00
|
|
|
return 0;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do_array() initialises the members of an array described
|
|
|
|
by type tp with the expressions in expr.
|
|
|
|
Two important cases:
|
|
|
|
- the number of members is known
|
|
|
|
- the number of members is not known
|
|
|
|
In the latter case, do_array() digests the whole expression
|
|
|
|
tree it is given.
|
|
|
|
In the former case, do_array() eats as many members from
|
|
|
|
the expression tree as are needed for the array.
|
|
|
|
If there are not sufficient members for the array, the remaining
|
|
|
|
members are padded with zeroes
|
|
|
|
*/
|
|
|
|
struct expr *
|
1986-04-03 11:33:32 +00:00
|
|
|
do_array(ex, tpp)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
struct type **tpp;
|
|
|
|
{
|
|
|
|
register struct type *tp = *tpp;
|
|
|
|
register arith elem_count;
|
|
|
|
|
1986-04-03 11:33:32 +00:00
|
|
|
ASSERT(tp->tp_fund == ARRAY && ISCOMMA(ex));
|
1986-03-10 13:07:55 +00:00
|
|
|
/* the following test catches initialisations like
|
|
|
|
char c[] = {"just a string"};
|
|
|
|
or
|
1986-03-14 16:15:16 +00:00
|
|
|
char d[] = {{"just another string"}};
|
1986-03-10 13:07:55 +00:00
|
|
|
The use of the brackets causes this problem.
|
|
|
|
Note: although the implementation of such initialisations
|
|
|
|
is completely foolish, we did it!! (no applause, thank you)
|
|
|
|
*/
|
|
|
|
if (tp->tp_up->tp_fund == CHAR) {
|
1986-09-29 14:01:34 +00:00
|
|
|
register struct expr *f = ex->OP_LEFT, *g = NILEXPR;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
while (ISCOMMA(f)) { /* eat the brackets!!! */
|
|
|
|
g = f;
|
|
|
|
f = f->OP_LEFT;
|
|
|
|
}
|
|
|
|
if (f->ex_class == String) { /* hallelujah, it's a string! */
|
1986-08-25 08:47:07 +00:00
|
|
|
ch_array(tpp, f);
|
1986-04-03 11:33:32 +00:00
|
|
|
return g ? g->OP_RIGHT : ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
/* else: just go on with the next part of this function */
|
|
|
|
if (g != 0)
|
1986-04-03 11:33:32 +00:00
|
|
|
ex = g;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
if (tp->tp_size == (arith)-1) {
|
|
|
|
/* declared with unknown size: [] */
|
1986-04-03 11:33:32 +00:00
|
|
|
for (elem_count = 0; ex; elem_count++) {
|
1986-03-10 13:07:55 +00:00
|
|
|
/* eat whole initialisation expression */
|
1986-09-29 14:01:34 +00:00
|
|
|
if (ISCOMMA(ex->OP_LEFT)) { /* embraced member */
|
1986-04-03 11:33:32 +00:00
|
|
|
if (IVAL(&(tp->tp_up), ex->OP_LEFT) != 0)
|
|
|
|
too_many_initialisers(ex);
|
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (aggregate_type(tp->tp_up))
|
1986-04-03 11:33:32 +00:00
|
|
|
ex = IVAL(&(tp->tp_up), ex);
|
1986-03-10 13:07:55 +00:00
|
|
|
else {
|
1986-04-03 11:33:32 +00:00
|
|
|
check_ival(ex->OP_LEFT, tp->tp_up);
|
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* set the proper size */
|
|
|
|
*tpp = construct_type(ARRAY, tp->tp_up, elem_count);
|
|
|
|
}
|
|
|
|
else { /* the number of members is already known */
|
|
|
|
arith dim = tp->tp_size / tp->tp_up->tp_size;
|
|
|
|
|
1986-04-03 11:33:32 +00:00
|
|
|
for (elem_count = 0; elem_count < dim && ex; elem_count++) {
|
1986-09-29 14:01:34 +00:00
|
|
|
if (ISCOMMA(ex->OP_LEFT)) { /* embraced member */
|
1986-04-03 11:33:32 +00:00
|
|
|
if (IVAL(&(tp->tp_up), ex->OP_LEFT) != 0)
|
|
|
|
too_many_initialisers(ex);
|
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (aggregate_type(tp->tp_up))
|
1986-04-03 11:33:32 +00:00
|
|
|
ex = IVAL(&(tp->tp_up), ex);
|
1986-03-10 13:07:55 +00:00
|
|
|
else {
|
1986-04-03 11:33:32 +00:00
|
|
|
check_ival(ex->OP_LEFT, tp->tp_up);
|
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1986-04-03 11:33:32 +00:00
|
|
|
if (ex && elem_count == dim)
|
1986-03-10 13:07:55 +00:00
|
|
|
/* all the members are initialised but there
|
|
|
|
remains a part of the expression tree which
|
|
|
|
is returned
|
|
|
|
*/
|
1986-04-03 11:33:32 +00:00
|
|
|
return ex;
|
1986-09-29 14:01:34 +00:00
|
|
|
if ((ex == 0) && elem_count < dim)
|
1986-03-10 13:07:55 +00:00
|
|
|
/* the expression tree is completely absorbed
|
|
|
|
but there are still members which must be
|
|
|
|
initialised with zeroes
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
pad(tp->tp_up);
|
|
|
|
while (++elem_count < dim);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* do_struct() initialises a struct of type tp with the expression expr.
|
|
|
|
The main loop is just controlled by the definition of the selectors
|
|
|
|
during which alignment is taken care of.
|
|
|
|
*/
|
|
|
|
struct expr *
|
1986-04-03 11:33:32 +00:00
|
|
|
do_struct(ex, tp)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct expr *ex;
|
|
|
|
register struct type *tp;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct sdef *sd = tp->tp_sdef;
|
1986-03-10 13:07:55 +00:00
|
|
|
arith bytes_upto_here = (arith)0;
|
|
|
|
arith last_offset = (arith)-1;
|
1986-03-14 16:15:16 +00:00
|
|
|
|
1986-04-03 11:33:32 +00:00
|
|
|
ASSERT(tp->tp_fund == STRUCT && ISCOMMA(ex));
|
1986-03-10 13:07:55 +00:00
|
|
|
/* as long as there are selectors and there is an initialiser.. */
|
1986-04-03 11:33:32 +00:00
|
|
|
while (sd && ex) {
|
|
|
|
if (ISCOMMA(ex->OP_LEFT)) { /* embraced expression */
|
|
|
|
if (IVAL(&(sd->sd_type), ex->OP_LEFT) != 0)
|
|
|
|
too_many_initialisers(ex);
|
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (aggregate_type(sd->sd_type))
|
|
|
|
/* selector is an aggregate itself */
|
1986-04-03 11:33:32 +00:00
|
|
|
ex = IVAL(&(sd->sd_type), ex);
|
1986-03-10 13:07:55 +00:00
|
|
|
else {
|
|
|
|
#ifdef NOBITFIELD
|
|
|
|
/* fundamental type, not embraced */
|
1986-04-03 11:33:32 +00:00
|
|
|
check_ival(ex->OP_LEFT, sd->sd_type);
|
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
#else
|
|
|
|
if (is_anon_idf(sd->sd_idf))
|
|
|
|
/* a hole in the struct due to
|
|
|
|
the use of ";:n;" in a struct
|
|
|
|
definition.
|
|
|
|
*/
|
|
|
|
put_bf(sd->sd_type, (arith)0);
|
1986-09-29 14:01:34 +00:00
|
|
|
else { /* fundamental type, not embraced */
|
1986-09-24 13:53:16 +00:00
|
|
|
check_ival(ex->OP_LEFT, sd->sd_type);
|
1986-04-03 11:33:32 +00:00
|
|
|
ex = ex->OP_RIGHT;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
#endif NOBITFIELD
|
|
|
|
}
|
|
|
|
}
|
1986-09-29 14:01:34 +00:00
|
|
|
if (sd->sd_sdef) /* align upto the next selector boundary */
|
1986-03-10 13:07:55 +00:00
|
|
|
bytes_upto_here += zero_bytes(sd);
|
|
|
|
if (last_offset != sd->sd_offset) {
|
|
|
|
/* don't take the field-width more than once */
|
1986-03-14 16:15:16 +00:00
|
|
|
bytes_upto_here +=
|
|
|
|
size_of_type(sd->sd_type, "selector");
|
1986-03-10 13:07:55 +00:00
|
|
|
last_offset = sd->sd_offset;
|
|
|
|
}
|
|
|
|
sd = sd->sd_sdef;
|
|
|
|
}
|
1986-04-03 11:33:32 +00:00
|
|
|
/* perfect fit if (ex && (sd == 0)) holds */
|
|
|
|
if ((ex == 0) && (sd != 0)) {
|
1986-09-29 14:01:34 +00:00
|
|
|
/* there are selectors left which must be padded with zeroes */
|
1986-03-10 13:07:55 +00:00
|
|
|
do {
|
|
|
|
pad(sd->sd_type);
|
|
|
|
/* take care of the alignment restrictions */
|
|
|
|
if (sd->sd_sdef)
|
|
|
|
bytes_upto_here += zero_bytes(sd);
|
|
|
|
/* no field thrown-outs here */
|
1986-03-14 16:15:16 +00:00
|
|
|
bytes_upto_here +=
|
|
|
|
size_of_type(sd->sd_type, "selector");
|
1986-03-10 13:07:55 +00:00
|
|
|
} while (sd = sd->sd_sdef);
|
|
|
|
}
|
|
|
|
/* keep on aligning... */
|
|
|
|
while (bytes_upto_here++ < tp->tp_size)
|
1986-03-26 16:58:43 +00:00
|
|
|
con_nullbyte();
|
1986-04-03 11:33:32 +00:00
|
|
|
return ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* check_and_pad() is given a simple initialisation expression
|
|
|
|
where the type can be either a simple or an aggregate type.
|
|
|
|
In the latter case, only the first member is initialised and
|
|
|
|
the rest is zeroed.
|
|
|
|
*/
|
1986-04-03 11:33:32 +00:00
|
|
|
check_and_pad(ex, tpp)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
struct type **tpp;
|
|
|
|
{
|
1986-04-03 11:33:32 +00:00
|
|
|
/* ex is of a fundamental type */
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct type *tp = *tpp;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
if (tp->tp_fund == ARRAY) {
|
|
|
|
if (valid_type(tp->tp_up, "array element") == 0)
|
|
|
|
return;
|
1986-04-03 11:33:32 +00:00
|
|
|
check_and_pad(ex, &(tp->tp_up)); /* first member */
|
1986-03-10 13:07:55 +00:00
|
|
|
if (tp->tp_size == (arith)-1)
|
|
|
|
/* no size specified upto here: just
|
|
|
|
set it to the size of one member.
|
|
|
|
*/
|
1986-03-13 13:27:44 +00:00
|
|
|
tp = *tpp = construct_type(ARRAY, tp->tp_up, (arith)1);
|
1986-03-10 13:07:55 +00:00
|
|
|
else {
|
1986-09-29 14:01:34 +00:00
|
|
|
register int dim = tp->tp_size / tp->tp_up->tp_size;
|
1986-03-10 13:07:55 +00:00
|
|
|
/* pad remaining members with zeroes */
|
|
|
|
while (--dim > 0)
|
|
|
|
pad(tp->tp_up);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (tp->tp_fund == STRUCT) {
|
|
|
|
register struct sdef *sd = tp->tp_sdef;
|
|
|
|
|
|
|
|
if (valid_type(tp, "struct") == 0)
|
|
|
|
return;
|
1986-04-03 11:33:32 +00:00
|
|
|
check_and_pad(ex, &(sd->sd_type));
|
1986-09-29 14:01:34 +00:00
|
|
|
/* next selector is aligned by adding extra zeroes */
|
1986-03-10 13:07:55 +00:00
|
|
|
if (sd->sd_sdef)
|
|
|
|
zero_bytes(sd);
|
|
|
|
while (sd = sd->sd_sdef) { /* pad remaining selectors */
|
|
|
|
pad(sd->sd_type);
|
|
|
|
if (sd->sd_sdef)
|
|
|
|
zero_bytes(sd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else /* simple type */
|
1986-04-03 11:33:32 +00:00
|
|
|
check_ival(ex, tp);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* pad() fills an element of type tp with zeroes.
|
|
|
|
If the element is an aggregate, pad() is called recursively.
|
|
|
|
*/
|
|
|
|
pad(tp)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct type *tp;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-12-10 12:00:55 +00:00
|
|
|
register arith sz = tp->tp_size;
|
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
switch (tp->tp_fund) {
|
|
|
|
case ARRAY:
|
|
|
|
if (valid_type(tp->tp_up, "array element") == 0)
|
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case STRUCT:
|
|
|
|
if (valid_type(tp, "struct") == 0)
|
|
|
|
return;
|
|
|
|
break;
|
1986-12-10 12:00:55 +00:00
|
|
|
case UNION:
|
|
|
|
if (valid_type(tp, "union") == 0)
|
|
|
|
return;
|
|
|
|
if (options['R']) {
|
|
|
|
warning("initialisation of unions not allowed");
|
|
|
|
}
|
|
|
|
break;
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOBITFIELD
|
|
|
|
case FIELD:
|
|
|
|
put_bf(tp, (arith)0);
|
1986-12-10 12:00:55 +00:00
|
|
|
return;
|
1986-03-10 13:07:55 +00:00
|
|
|
#endif NOBITFIELD
|
|
|
|
case ERRONEOUS:
|
1986-12-10 12:00:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sz >= word_size) {
|
|
|
|
C_con_cst((arith) 0);
|
|
|
|
sz -= word_size;
|
|
|
|
}
|
|
|
|
while (sz) {
|
|
|
|
C_con_icon("0", (arith) 1);
|
|
|
|
sz--;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check_ival() checks whether the initialisation of an element
|
|
|
|
of a fundamental type is legal and, if so, performs the initialisation
|
|
|
|
by directly generating the necessary code.
|
|
|
|
No further comment is needed to explain the internal structure
|
|
|
|
of this straightforward function.
|
|
|
|
*/
|
1986-12-10 12:00:55 +00:00
|
|
|
check_ival(expr, tp)
|
|
|
|
register struct expr *expr;
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct type *tp;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
/* The philosophy here is that ch7cast puts an explicit
|
|
|
|
conversion node in front of the expression if the types
|
1986-03-14 16:15:16 +00:00
|
|
|
are not compatible. In this case, the initialisation
|
|
|
|
expression is no longer a constant.
|
1986-03-10 13:07:55 +00:00
|
|
|
*/
|
1986-12-10 12:00:55 +00:00
|
|
|
struct expr *ex = expr;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-04-03 11:33:32 +00:00
|
|
|
switch (tp->tp_fund) {
|
1986-03-10 13:07:55 +00:00
|
|
|
case CHAR:
|
|
|
|
case SHORT:
|
|
|
|
case INT:
|
|
|
|
case LONG:
|
1986-03-14 16:15:16 +00:00
|
|
|
case ENUM:
|
1986-04-03 11:33:32 +00:00
|
|
|
case POINTER:
|
|
|
|
ch7cast(&ex, '=', tp);
|
1986-12-10 12:00:55 +00:00
|
|
|
expr = ex;
|
1986-04-03 11:33:32 +00:00
|
|
|
#ifdef DEBUG
|
1986-12-10 12:00:55 +00:00
|
|
|
print_expr("init-expr after cast", expr);
|
1986-04-03 11:33:32 +00:00
|
|
|
#endif DEBUG
|
1986-12-10 12:00:55 +00:00
|
|
|
if (!is_ld_cst(expr))
|
|
|
|
illegal_init_cst(expr);
|
1986-04-02 08:37:17 +00:00
|
|
|
else
|
1986-12-10 12:00:55 +00:00
|
|
|
if (expr->VL_CLASS == Const)
|
|
|
|
con_int(expr);
|
1986-04-02 08:37:17 +00:00
|
|
|
else
|
1986-12-10 12:00:55 +00:00
|
|
|
if (expr->VL_CLASS == Name) {
|
|
|
|
register struct idf *idf = expr->VL_IDF;
|
1986-04-03 11:33:32 +00:00
|
|
|
|
1986-12-10 12:00:55 +00:00
|
|
|
if (idf->id_def->df_level >= L_LOCAL)
|
|
|
|
illegal_init_cst(expr);
|
1986-04-03 11:33:32 +00:00
|
|
|
else /* e.g., int f(); int p = f; */
|
1986-12-10 12:00:55 +00:00
|
|
|
if (idf->id_def->df_type->tp_fund == FUNCTION)
|
|
|
|
C_con_pnam(idf->id_text);
|
1986-04-03 11:33:32 +00:00
|
|
|
else /* e.g., int a; int *p = &a; */
|
1986-12-10 12:00:55 +00:00
|
|
|
C_con_dnam(idf->id_text, expr->VL_VALUE);
|
1986-04-03 11:33:32 +00:00
|
|
|
}
|
|
|
|
else {
|
1986-12-10 12:00:55 +00:00
|
|
|
ASSERT(expr->VL_CLASS == Label);
|
|
|
|
C_con_dlb(expr->VL_LBL, expr->VL_VALUE);
|
1986-04-03 11:33:32 +00:00
|
|
|
}
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
1986-09-12 09:16:07 +00:00
|
|
|
#ifndef NOFLOAT
|
1986-03-10 13:07:55 +00:00
|
|
|
case FLOAT:
|
|
|
|
case DOUBLE:
|
1986-04-03 11:33:32 +00:00
|
|
|
ch7cast(&ex, '=', tp);
|
1986-12-10 12:00:55 +00:00
|
|
|
expr = ex;
|
1986-04-03 11:33:32 +00:00
|
|
|
#ifdef DEBUG
|
1986-12-10 12:00:55 +00:00
|
|
|
print_expr("init-expr after cast", expr);
|
1986-04-03 11:33:32 +00:00
|
|
|
#endif DEBUG
|
1986-12-10 12:00:55 +00:00
|
|
|
if (expr->ex_class == Float)
|
|
|
|
C_con_fcon(expr->FL_VALUE, expr->ex_type->tp_size);
|
1986-03-10 13:07:55 +00:00
|
|
|
else
|
1986-12-10 12:00:55 +00:00
|
|
|
if (expr->ex_class == Oper && expr->OP_OPER == INT2FLOAT) {
|
1986-04-03 11:33:32 +00:00
|
|
|
/* float f = 1; */
|
1986-12-10 12:00:55 +00:00
|
|
|
expr = expr->OP_RIGHT;
|
|
|
|
if (is_cp_cst(expr))
|
|
|
|
C_con_fcon(long2str((long)expr->VL_VALUE, 10),
|
1986-09-29 14:01:34 +00:00
|
|
|
tp->tp_size);
|
1986-04-02 08:37:17 +00:00
|
|
|
else
|
1986-12-10 12:00:55 +00:00
|
|
|
illegal_init_cst(expr);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
else
|
1986-12-10 12:00:55 +00:00
|
|
|
illegal_init_cst(expr);
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
1986-09-12 09:16:07 +00:00
|
|
|
#endif NOFLOAT
|
1986-04-02 08:37:17 +00:00
|
|
|
|
1986-04-03 11:33:32 +00:00
|
|
|
#ifndef NOBITFIELD
|
|
|
|
case FIELD:
|
|
|
|
ch7cast(&ex, '=', tp->tp_up);
|
1986-12-10 12:00:55 +00:00
|
|
|
expr = ex;
|
1986-04-03 11:33:32 +00:00
|
|
|
#ifdef DEBUG
|
1986-12-10 12:00:55 +00:00
|
|
|
print_expr("init-expr after cast", expr);
|
1986-04-03 11:33:32 +00:00
|
|
|
#endif DEBUG
|
1986-12-10 12:00:55 +00:00
|
|
|
if (is_cp_cst(expr))
|
|
|
|
put_bf(tp, expr->VL_VALUE);
|
1986-04-03 11:33:32 +00:00
|
|
|
else
|
1986-12-10 12:00:55 +00:00
|
|
|
illegal_init_cst(expr);
|
1986-03-10 13:07:55 +00:00
|
|
|
break;
|
1986-04-03 11:33:32 +00:00
|
|
|
#endif NOBITFIELD
|
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
case ERRONEOUS:
|
|
|
|
break;
|
|
|
|
default:
|
1986-04-03 11:33:32 +00:00
|
|
|
crash("check_ival");
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1986-08-25 08:47:07 +00:00
|
|
|
/* ch_array() initialises an array of characters when given
|
1986-03-10 13:07:55 +00:00
|
|
|
a string constant.
|
|
|
|
Alignment is taken care of.
|
|
|
|
*/
|
1986-08-25 08:47:07 +00:00
|
|
|
ch_array(tpp, ex)
|
1986-03-10 13:07:55 +00:00
|
|
|
struct type **tpp; /* type tp = array of characters */
|
1986-04-03 11:33:32 +00:00
|
|
|
struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
register struct type *tp = *tpp;
|
1987-02-23 13:08:54 +00:00
|
|
|
register arith length = ex->SG_LEN;
|
|
|
|
char *s;
|
|
|
|
arith ntopad;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-04-03 11:33:32 +00:00
|
|
|
ASSERT(ex->ex_class == String);
|
1986-09-29 14:01:34 +00:00
|
|
|
if (tp->tp_size == (arith)-1) {
|
1986-03-10 13:07:55 +00:00
|
|
|
/* set the dimension */
|
|
|
|
tp = *tpp = construct_type(ARRAY, tp->tp_up, length);
|
1987-03-20 08:30:31 +00:00
|
|
|
ntopad = align(tp->tp_size, word_size) - tp->tp_size;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
arith dim = tp->tp_size / tp->tp_up->tp_size;
|
1986-04-04 09:54:50 +00:00
|
|
|
extern char options[];
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-04-04 09:54:50 +00:00
|
|
|
if (length > dim) {
|
|
|
|
if (options['R'])
|
|
|
|
too_many_initialisers(ex);
|
|
|
|
else { /* don't take the null byte into account */
|
|
|
|
if (length > dim + 1)
|
|
|
|
expr_warning(ex,
|
|
|
|
"too many initialisers");
|
|
|
|
length = dim;
|
|
|
|
}
|
|
|
|
}
|
1987-03-20 08:30:31 +00:00
|
|
|
ntopad = align(dim, word_size) - length;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
/* throw out the characters of the already prepared string */
|
1987-03-20 08:30:31 +00:00
|
|
|
s = Malloc((unsigned) (length + ntopad));
|
1987-02-23 13:08:54 +00:00
|
|
|
clear(s, (int) (length + ntopad));
|
|
|
|
strncpy(s, ex->SG_VALUE, (int) length);
|
1987-03-20 08:30:31 +00:00
|
|
|
free(ex->SG_VALUE);
|
1987-02-23 13:08:54 +00:00
|
|
|
str_cst(s, (int) (length + ntopad));
|
|
|
|
free(s);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
1987-02-23 13:08:54 +00:00
|
|
|
/* As long as some parts of the pipeline cannot handle very long string
|
|
|
|
constants, string constants are written out in chunks
|
|
|
|
*/
|
1986-08-25 08:47:07 +00:00
|
|
|
str_cst(str, len)
|
|
|
|
register char *str;
|
|
|
|
register int len;
|
|
|
|
{
|
1987-02-23 13:08:54 +00:00
|
|
|
arith chunksize = ((127 + word_size) / word_size) * word_size;
|
|
|
|
|
|
|
|
while (len > chunksize) {
|
|
|
|
C_con_scon(str, chunksize);
|
|
|
|
len -= chunksize;
|
|
|
|
str += chunksize;
|
|
|
|
}
|
|
|
|
C_con_scon(str, (arith) len);
|
1986-08-25 08:47:07 +00:00
|
|
|
}
|
|
|
|
|
1986-03-10 13:07:55 +00:00
|
|
|
#ifndef NOBITFIELD
|
|
|
|
/* put_bf() takes care of the initialisation of (bit-)field
|
|
|
|
selectors of a struct: each time such an initialisation takes place,
|
|
|
|
put_bf() is called instead of the normal code generating routines.
|
|
|
|
Put_bf() stores the given integral value into "field" and
|
|
|
|
"throws" the result of "field" out if the current selector
|
|
|
|
is the last of this number of fields stored at the same address.
|
|
|
|
*/
|
|
|
|
put_bf(tp, val)
|
|
|
|
struct type *tp;
|
|
|
|
arith val;
|
|
|
|
{
|
|
|
|
static long field = (arith)0;
|
|
|
|
static arith offset = (arith)-1;
|
|
|
|
register struct field *fd = tp->tp_field;
|
|
|
|
register struct sdef *sd = fd->fd_sdef;
|
1986-04-02 08:37:17 +00:00
|
|
|
static struct expr exp;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
ASSERT(sd);
|
|
|
|
if (offset == (arith)-1) {
|
|
|
|
/* first bitfield in this field */
|
|
|
|
offset = sd->sd_offset;
|
1986-04-02 08:37:17 +00:00
|
|
|
exp.ex_type = tp->tp_up;
|
|
|
|
exp.ex_class = Value;
|
|
|
|
exp.VL_CLASS = Const;
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
if (val != 0) /* insert the value into "field" */
|
|
|
|
field |= (val & fd->fd_mask) << fd->fd_shift;
|
|
|
|
if (sd->sd_sdef == 0 || sd->sd_sdef->sd_offset != offset) {
|
|
|
|
/* the selector was the last stored at this address */
|
1986-04-02 08:37:17 +00:00
|
|
|
exp.VL_VALUE = field;
|
|
|
|
con_int(&exp);
|
1986-03-10 13:07:55 +00:00
|
|
|
field = (arith)0;
|
|
|
|
offset = (arith)-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif NOBITFIELD
|
|
|
|
|
|
|
|
int
|
|
|
|
zero_bytes(sd)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct sdef *sd;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
/* fills the space between a selector of a struct
|
|
|
|
and the next selector of that struct with zero-bytes.
|
|
|
|
*/
|
1986-09-29 14:01:34 +00:00
|
|
|
register int n = sd->sd_sdef->sd_offset - sd->sd_offset -
|
1986-03-10 13:07:55 +00:00
|
|
|
size_of_type(sd->sd_type, "struct member");
|
1986-12-10 12:00:55 +00:00
|
|
|
register int count = n;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
|
|
|
while (n-- > 0)
|
1986-03-26 16:58:43 +00:00
|
|
|
con_nullbyte();
|
1986-03-10 13:07:55 +00:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
valid_type(tp, str)
|
|
|
|
struct type *tp;
|
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
if (tp->tp_size < 0) {
|
|
|
|
error("size of %s unknown", str);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1986-04-02 08:37:17 +00:00
|
|
|
con_int(ex)
|
|
|
|
register struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-04-02 08:37:17 +00:00
|
|
|
register struct type *tp = ex->ex_type;
|
1986-03-10 13:07:55 +00:00
|
|
|
|
1986-04-02 08:37:17 +00:00
|
|
|
ASSERT(is_cp_cst(ex));
|
1986-03-10 13:07:55 +00:00
|
|
|
if (tp->tp_unsigned)
|
1986-04-02 08:37:17 +00:00
|
|
|
C_con_ucon(long2str((long)ex->VL_VALUE, -10), tp->tp_size);
|
1986-03-10 13:07:55 +00:00
|
|
|
else
|
1986-04-02 08:37:17 +00:00
|
|
|
C_con_icon(long2str((long)ex->VL_VALUE, 10), tp->tp_size);
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
1986-04-02 08:37:17 +00:00
|
|
|
illegal_init_cst(ex)
|
|
|
|
struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-04-02 08:37:17 +00:00
|
|
|
expr_error(ex, "illegal initialisation constant");
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
1986-04-02 08:37:17 +00:00
|
|
|
too_many_initialisers(ex)
|
|
|
|
struct expr *ex;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
1986-04-02 08:37:17 +00:00
|
|
|
expr_error(ex, "too many initialisers");
|
1986-03-10 13:07:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aggregate_type(tp)
|
1986-09-24 13:53:16 +00:00
|
|
|
register struct type *tp;
|
1986-03-10 13:07:55 +00:00
|
|
|
{
|
|
|
|
return tp->tp_fund == ARRAY || tp->tp_fund == STRUCT;
|
|
|
|
}
|