added the ability for the compiler to directly reference another address in a default argument (for arrays only)

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%401050
This commit is contained in:
David Anderson 2007-07-07 15:36:54 +00:00
parent cce60de20f
commit d8d13f1920
2 changed files with 68 additions and 39 deletions

View File

@ -2516,15 +2516,19 @@ static int base;
* *
* Global references: litidx (altered) * Global references: litidx (altered)
*/ */
static void initials(int ident,int tag,cell *size,int dim[],int numdim, static void initials2(int ident,int tag,cell *size,int dim[],int numdim,
constvalue *enumroot) constvalue *enumroot, int eq_match_override)
{ {
int ctag; int ctag;
cell tablesize; cell tablesize;
int curlit=litidx; int curlit=litidx;
int err=0; int err=0;
if (!matchtoken('=')) { if (eq_match_override == -1) {
eq_match_override = matchtoken('=');
}
if (!eq_match_override) {
assert(ident!=iARRAY || numdim>0); assert(ident!=iARRAY || numdim>0);
if (ident==iARRAY && dim[numdim-1]==0) { if (ident==iARRAY && dim[numdim-1]==0) {
/* declared as "myvar[];" which is senseless (note: this *does* make /* declared as "myvar[];" which is senseless (note: this *does* make
@ -2626,6 +2630,12 @@ static void initials(int ident,int tag,cell *size,int dim[],int numdim,
*size=litidx-curlit; /* number of elements defined */ *size=litidx-curlit; /* number of elements defined */
} }
static void initials(int ident,int tag,cell *size,int dim[],int numdim,
constvalue *enumroot)
{
initials2(ident, tag, size, dim, numdim, enumroot, -1);
}
static cell initarray(int ident,int tag,int dim[],int numdim,int cur, static cell initarray(int ident,int tag,int dim[],int numdim,int cur,
int startlit,int counteddim[],constvalue *lastdim, int startlit,int counteddim[],constvalue *lastdim,
constvalue *enumroot,int *errorfound) constvalue *enumroot,int *errorfound)
@ -4386,27 +4396,46 @@ static void doarg(char *name,int ident,int offset,int tags[],int numtags,
arg->dim[arg->numdim - 1] = (size + sizeof(cell) - 1) / sizeof(cell); arg->dim[arg->numdim - 1] = (size + sizeof(cell) - 1) / sizeof(cell);
} }
if (matchtoken('=')) { if (matchtoken('=')) {
lexpush(); /* initials() needs the "=" token again */
assert(litidx==0); /* at the start of a function, this is reset */ assert(litidx==0); /* at the start of a function, this is reset */
assert(numtags>0); assert(numtags>0);
initials(ident,tags[0],&size,arg->dim,arg->numdim,enumroot); /* Check if there is a symbol */
assert(size>=litidx); if (matchtoken(tSYMBOL)) {
/* allocate memory to hold the initial values */ symbol *sym;
arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell)); char *name;
if (arg->defvalue.array.data!=NULL) { cell val;
int i; tokeninfo(&val,&name);
memcpy(arg->defvalue.array.data,litq,litidx*sizeof(cell)); if ((sym=findglb(name, sGLOBAL)) == NULL) {
arg->hasdefault=TRUE; /* argument has default value */ error(17, name); /* undefined symbol */
arg->defvalue.array.size=litidx; } else {
arg->defvalue.array.addr=-1; arg->hasdefault=TRUE; /* argument as a default value */
/* calulate size to reserve on the heap */ memset(&arg->defvalue, 0, sizeof(arg->defvalue));
arg->defvalue.array.arraysize=1; arg->defvalue.array.data=NULL;
for (i=0; i<arg->numdim; i++) arg->defvalue.array.addr=sym->addr;
arg->defvalue.array.arraysize*=arg->dim[i]; arg->defvalue_tag=sym->tag;
if (arg->defvalue.array.arraysize < arg->defvalue.array.size) if (sc_status==statWRITE && (sym->usage & uREAD)==0) {
arg->defvalue.array.arraysize = arg->defvalue.array.size; markusage(sym, uREAD);
} /* if */ }
litidx=0; /* reset */ }
} else {
initials2(ident,tags[0],&size,arg->dim,arg->numdim,enumroot, 1);
assert(size>=litidx);
/* allocate memory to hold the initial values */
arg->defvalue.array.data=(cell *)malloc(litidx*sizeof(cell));
if (arg->defvalue.array.data!=NULL) {
int i;
memcpy(arg->defvalue.array.data,litq,litidx*sizeof(cell));
arg->hasdefault=TRUE; /* argument has default value */
arg->defvalue.array.size=litidx;
arg->defvalue.array.addr=-1;
/* calulate size to reserve on the heap */
arg->defvalue.array.arraysize=1;
for (i=0; i<arg->numdim; i++)
arg->defvalue.array.arraysize*=arg->dim[i];
if (arg->defvalue.array.arraysize < arg->defvalue.array.size)
arg->defvalue.array.arraysize = arg->defvalue.array.size;
} /* if */
litidx=0; /* reset */
}
} /* if */ } /* if */
} else { } else {
if (matchtoken('=')) { if (matchtoken('=')) {

View File

@ -2174,8 +2174,6 @@ static void setdefarray(cell *string,cell size,cell array_sz,cell *dataaddr,int
* the default array data is "dumped" into the data segment only once (on the * the default array data is "dumped" into the data segment only once (on the
* first use). * first use).
*/ */
assert(string!=NULL);
assert(size>0);
/* check whether to dump the default array */ /* check whether to dump the default array */
assert(dataaddr!=NULL); assert(dataaddr!=NULL);
if (sc_status==statWRITE && *dataaddr<0) { if (sc_status==statWRITE && *dataaddr<0) {
@ -2189,7 +2187,7 @@ static void setdefarray(cell *string,cell size,cell array_sz,cell *dataaddr,int
* does not modify the default value), directly pass the address of the * does not modify the default value), directly pass the address of the
* array in the data segment. * array in the data segment.
*/ */
if (fconst) { if (fconst || !string) {
ldconst(*dataaddr,sPRI); ldconst(*dataaddr,sPRI);
} else { } else {
/* Generate the code: /* Generate the code:
@ -2577,20 +2575,22 @@ static int nesting=0;
arg[argidx].defvalue.array.arraysize, arg[argidx].defvalue.array.arraysize,
&arg[argidx].defvalue.array.addr, &arg[argidx].defvalue.array.addr,
(arg[argidx].usage & uCONST)!=0); (arg[argidx].usage & uCONST)!=0);
if ((arg[argidx].usage & uCONST)==0) { if (arg[argidx].defvalue.array.data != NULL) {
heapalloc+=arg[argidx].defvalue.array.arraysize; if ((arg[argidx].usage & uCONST)==0) {
nest_stkusage+=arg[argidx].defvalue.array.arraysize; heapalloc+=arg[argidx].defvalue.array.arraysize;
} /* if */ nest_stkusage+=arg[argidx].defvalue.array.arraysize;
/* keep the lengths of all dimensions of a multi-dimensional default array */ } /* if */
assert(arg[argidx].numdim>0); /* keep the lengths of all dimensions of a multi-dimensional default array */
if (arg[argidx].numdim==1) { assert(arg[argidx].numdim>0);
append_constval(&arrayszlst,arg[argidx].name,arg[argidx].defvalue.array.arraysize,0); if (arg[argidx].numdim==1) {
} else { append_constval(&arrayszlst,arg[argidx].name,arg[argidx].defvalue.array.arraysize,0);
for (level=0; level<arg[argidx].numdim; level++) { } else {
assert(level<sDIMEN_MAX); for (level=0; level<arg[argidx].numdim; level++) {
append_constval(&arrayszlst,arg[argidx].name,arg[argidx].dim[level],level); assert(level<sDIMEN_MAX);
} /* for */ append_constval(&arrayszlst,arg[argidx].name,arg[argidx].dim[level],level);
} /* if */ } /* for */
} /* if */
}
} else if (arg[argidx].ident==iREFERENCE) { } else if (arg[argidx].ident==iREFERENCE) {
setheap(arg[argidx].defvalue.val); setheap(arg[argidx].defvalue.val);
/* address of the value on the heap in PRI */ /* address of the value on the heap in PRI */