From 2a032b6bf77be054d131ff838282526e8764a4bb Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sat, 15 Jul 2006 01:24:25 +0000 Subject: [PATCH] finished decl implementation --HG-- branch : faluco extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/branches/faluco%4016 --- compiler-init/sc1.c | 59 +++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/compiler-init/sc1.c b/compiler-init/sc1.c index 59a2efdb..33f3f7c5 100644 --- a/compiler-init/sc1.c +++ b/compiler-init/sc1.c @@ -132,6 +132,7 @@ static void delwhile(void); static int *readwhile(void); static int norun = 0; /* the compiler never ran */ +static int autozero = 0; /* if 1 will zero out the variable, if 0 omit the zeroing */ static int lastst = 0; /* last executed statement type */ static int nestlevel = 0; /* number of active (open) compound statements */ static int rettype = 0; /* the type that a "return" expression should have */ @@ -1958,19 +1959,24 @@ static int declloc(int fstatic) int ctag = tag; /* set to "tag" by default */ int explicit_init=FALSE;/* is the variable explicitly initialized? */ if (matchtoken('=')) { + if (!autozero) + error(10); doexpr(FALSE,FALSE,FALSE,FALSE,&ctag,NULL,TRUE); explicit_init=TRUE; } else { - ldconst(0,sPRI); /* uninitialized variable, set to zero */ + if (autozero) + ldconst(0,sPRI); /* uninitialized variable, set to zero */ } /* if */ - /* now try to save the value (still in PRI) in the variable */ - lval.sym=sym; - lval.ident=iVARIABLE; - lval.constval=0; - lval.tag=tag; - check_userop(NULL,ctag,lval.tag,2,NULL,&ctag); - store(&lval); - markexpr(sEXPR,NULL,0); /* full expression ends after the store */ + if (autozero) { + /* now try to save the value (still in PRI) in the variable */ + lval.sym=sym; + lval.ident=iVARIABLE; + lval.constval=0; + lval.tag=tag; + check_userop(NULL,ctag,lval.tag,2,NULL,&ctag); + store(&lval); + markexpr(sEXPR,NULL,0); /* full expression ends after the store */ + } assert(staging); /* end staging phase (optimize expression) */ stgout(staging_start); stgset(FALSE); @@ -1985,15 +1991,17 @@ static int declloc(int fstatic) assert(cur_lit>=0 && cur_lit<=litidx && litidx<=litmax); assert(size>0 && size>=sym->dim.array.length); assert(numdim>1 || size==sym->dim.array.length); - /* final literal values that are zero make no sense to put in the literal - * pool, because values get zero-initialized anyway; we check for this, - * because users often explicitly initialize strings to "" - */ - while (litidx>cur_lit && litq[litidx-1]==0) - litidx--; - /* if the array is not completely filled, set all values to zero first */ - if (litidx-cur_litcur_lit && litq[litidx-1]==0) + litidx--; + /* if the array is not completely filled, set all values to zero first */ + if (litidx-cur_litnext : NULL; do { int fieldlit=litidx; @@ -2312,6 +2320,8 @@ static cell initvector(int ident,int tag,cell size,int fillzero, } while (matchtoken(',')); /* do */ needtoken('}'); } else { + if (hadtoken && !autozero) + error(10); init(ident,&ctag,errorfound); if (!matchtag(tag,ctag,TRUE)) error(213); /* tagname mismatch */ @@ -4585,6 +4595,7 @@ static void statement(int *lastindent,int allow_decl) break; case tNEW: if (allow_decl) { + autozero=1; declloc(FALSE); lastst=tNEW; } else { @@ -4593,9 +4604,10 @@ static void statement(int *lastindent,int allow_decl) break; case tDECL: if (allow_decl) { - declloc(FALSE); - lastst=tDECL; - } else { + autozero=0; + declloc(FALSE); + lastst=tDECL; + } else { error(3); /* declaration only valid in a block */ } /* if */ break; @@ -4974,6 +4986,7 @@ static void dofor(void) * 'compound statement' level of it own. */ nestlevel++; + autozero=1; declloc(FALSE); /* declare local variable */ } else { doexpr(TRUE,TRUE,TRUE,TRUE,NULL,NULL,FALSE); /* expression 1 */