/****************************************************************
Copyright 1990, 1993 by AT&T Bell Laboratories and Bellcore.
Permission to use, copy, modify, and distribute this software
and its documentation for any purpose and without fee is hereby
granted, provided that the above copyright notice appear in all
copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the names of AT&T Bell Laboratories or
Bellcore or any of their entities not be used in advertising or
publicity pertaining to distribution of the software without
specific, written prior permission.
AT&T and Bellcore disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall AT&T or Bellcore be liable for
any special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
****************************************************************/
LOCAL
void exar2(), popctl(), pushctl();
putif(p
, 0); /* 0 => if, not elseif */
if (ctlstack
->ctltype
== CTLIF
|| ctlstack
->ctltype
== CTLIFX
)
putif(p
, 1); /* 1 ==> elseif */
execerr("elseif out of place", CNULL
);
register struct Ctlframe
*c
;
for(c
= ctlstack
; c
->ctltype
== CTLIFX
; --c
);
if(c
->ctltype
== CTLIF
) {
execerr("else out of place", CNULL
);
while(ctlstack
->ctltype
== CTLIFX
) {
if(ctlstack
->ctltype
== CTLIF
) {
else if(ctlstack
->ctltype
== CTLELSE
) {
execerr("endif out of place", CNULL
);
if (ctlstack
->ctltype
== CTLIF
|| ctlstack
->ctltype
== CTLIFX
)
/* pushctl -- Start a new control construct, initialize the labels (to
if(++ctlstack
>= lastctl
)
many("loops or if-then-elses", 'c', maxctl
);
ctlstack
->ctltype
= code
;
ctlstack
->ctlabels
[i
] = 0;
Fatal("control stack empty");
/* poplab -- update the flags in labeltab */
register struct Labelblock
*lp
;
for(lp
= labeltab
; lp
< highlabtab
; ++lp
)
/* mark all labels in inner blocks unreachable */
if(lp
->blklevel
> blklevel
)
else if(lp
->blklevel
> blklevel
)
/* move all labels referred to in inner blocks out a level */
p1_goto (lab
-> stateno
);
register struct Primblock
*lp
;
err("assignment to a non-variable");
else if(lp
->namep
->vclass
!=CLVAR
&& lp
->argsp
)
err("statement function amid executables");
new_lp
= mklhs (lp
, keepsubs
);
/* Make Statement Function */
long laststfcn
= -1, thisstno
;
register struct Primblock
*p
;
if(np
->vclass
== CLUNKNOWN
)
dclerr("redeclaration of statement function", np
);
np
->vprocclass
= PSTFUNCT
;
/* Set the type of the function */
if (np
->vtype
== TYCHAR
&& !np
->vleng
)
err("character statement function with length (*)");
args
= (lp
->argsp
? lp
->argsp
->listp
: CHNULL
);
np
->varxptr
.vstfdesc
= mkchain((char *)args
, (chainp
)rp
);
for(doing_stmtfcn
= 1 ; args
; args
= args
->nextp
)
/* It is an error for the formal parameters to have arguments or
if( ((tagptr
)(args
->datap
))->tag
!=TPRIM
||
(p
= (struct Primblock
*)(args
->datap
) )->argsp
||
err("non-variable argument in statement function definition");
/* Replace the name on the left-hand side */
args
->datap
= (char *)p
->namep
;
sprintf(buf
, "%s function %.90s invoked as subroutine",
ftn_types
[np
->vtype
], np
->fvarname
);
excall(name
, args
, nstars
, labels
)
struct Labelblock
*labels
[ ];
if (name
->vtype
!= TYSUBR
) {
if (name
->vinfproc
&& !name
->vcalled
) {
else if (!name
->vimpltype
&& name
->vtype
!= TYUNKNOWN
)
settype(name
, TYSUBR
, (ftnint
)0);
p
= mkfunct( mkprim(name
, args
, CHNULL
) );
/* Subroutines and their identifiers acquire the type INT */
p
->exprblock
.vtype
= p
->exprblock
.leftp
->headblock
.vtype
= TYINT
;
/* Handle the alternate return mechanism */
putcmgo(putx(fixtype(p
)), nstars
, labels
);
execerr("pause/stop argument must be constant", CNULL
);
else if( ISINT(p
->constblock
.vtype
) )
str
= convic(p
->constblock
.Const
.ci
);
p
->constblock
.Const
.ccp
= copyn(n
, str
);
p
->constblock
.Const
.ccp1
.blanks
= 0;
p
->constblock
.vtype
= TYCHAR
;
p
->constblock
.vleng
= (expptr
) ICON(n
);
p
= (expptr
) mkstrcon(0, CNULL
);
else if(p
->constblock
.vtype
!= TYCHAR
)
execerr("pause/stop argument must be integer or string", CNULL
);
p
= (expptr
) mkstrcon(0, CNULL
);
else p
= (expptr
) mkstrcon(0, CNULL
);
subr_call
= call1(TYSUBR
, (stop
? "s_stop" : "s_paus"), p
);
/* Macros for ctlstack -> dostepsign */
/* exdo -- generate DO loop code. In the case of a variable increment,
positive increment tests are placed above the body, negative increment
tests are placed below (see enddo() ) */
exdo(range
, loopname
, spec
)
int range
; /* end label */
chainp spec
; /* input spec must have at least 2 exprs */
chainp cp
; /* loops over the fields in spec */
int dotype
; /* type of the index variable */
int incsign
; /* sign of the increment, if it's constant
Addrp dovarp
; /* loop index variable */
expptr doinit
; /* constant or register for init param */
expptr par
[3]; /* local specification parameters */
expptr init
, test
, inc
; /* Expressions in the resulting FOR loop */
dorange
= ctlstack
->dolabel
= range
;
ctlstack
->loopname
= loopname
;
/* Declare the loop index */
ctlstack
->donamep
= NULL
;
if (!np
) { /* do while */
if (loopname
->vtype
== TYUNKNOWN
) {
loopname
->vclass
= CLLABEL
;
loopname
->vprocclass
= PLABEL
;
loopname
->vtype
= TYLABEL
;
if (loopname
->vtype
== TYLABEL
)
dclerr("already in use as a loop name",
dclerr("already declared; cannot be a loop name",
putwhile((expptr
)spec
->nextp
);
errstr("nested loops with variable %s", np
->fvarname
);
ctlstack
->donamep
= NULL
;
/* Create a memory-resident version of the index variable */
if( ! ONEOF(dovarp
->vtype
, MSKINT
|MSKREAL
) )
err("bad type on do variable");
/* Now dovarp points to the index to be used within the loop, dostgp
points to the one which may need to be stored */
/* Count the input specifications and type-check each one independently;
this just eliminates non-numeric values from the specification */
for(i
=0 , cp
= spec
->nextp
; cp
!=NULL
&& i
<3 ; cp
= cp
->nextp
)
p
= par
[i
++] = fixtype((tagptr
)cp
->datap
);
if( ! ONEOF(p
->headblock
.vtype
, MSKINT
|MSKREAL
) )
err("bad type on DO parameter");
err("too few DO parameters");
err("too many DO parameters");
DOINCR
= (expptr
) ICON(1);
/* Now all of the local specification fields are set, but their types are
/* Declare the loop initialization value, casting it properly and declaring a
if (ISCONST (DOINIT
) || !onetripflag
)
/* putx added 6-29-89 (mwm), not sure if fixtype is required, but I doubt it
since mkconv is called just before */
doinit
= putx (mkconv (dotype
, DOINIT
));
doinit
= (expptr
) mktmp(dotype
, ENULL
);
puteq (cpexpr (doinit
), DOINIT
);
/* Declare the loop ending value, casting it to the type of the index
ctlstack
->domax
= mkconv(dotype
, DOLIMIT
);
ctlstack
->domax
= (expptr
) mktmp0(dotype
, ENULL
);
puteq (cpexpr (ctlstack
-> domax
), DOLIMIT
);
/* Declare the loop increment value, casting it to the type of the index
ctlstack
->dostep
= mkconv(dotype
, DOINCR
);
if( (incsign
= conssgn(ctlstack
->dostep
)) == 0)
err("zero DO increment");
ctlstack
->dostepsign
= (incsign
> 0 ? POSSTEP
: NEGSTEP
);
ctlstack
->dostep
= (expptr
) mktmp0(dotype
, ENULL
);
ctlstack
->dostepsign
= VARSTEP
;
puteq (cpexpr (ctlstack
-> dostep
), DOINCR
);
/* All data is now properly typed and in the ctlstack, except for the
initial value. Assignments of temps have been generated already */
switch (ctlstack
-> dostepsign
) {
test
= mkexpr (OPQUEST
, mkexpr (OPLT
,
cpexpr (ctlstack
-> dostep
), ICON(0)),
mkexpr (OPGE
, cpexpr((expptr
)dovarp
),
cpexpr (ctlstack
-> domax
)),
mkexpr (OPLE
, cpexpr((expptr
)dovarp
),
cpexpr (ctlstack
-> domax
))));
test
= mkexpr (OPLE
, cpexpr((expptr
)dovarp
),
cpexpr (ctlstack
-> domax
));
test
= mkexpr (OPGE
, cpexpr((expptr
)dovarp
),
cpexpr (ctlstack
-> domax
));
erri ("exdo: bad dostepsign '%d'", ctlstack
-> dostepsign
);
} /* switch (ctlstack -> dostepsign) */
test
= mkexpr (OPOR
, test
,
mkexpr (OPEQ
, cpexpr((expptr
)dovarp
), cpexpr (doinit
)));
init
= mkexpr (OPASSIGN
, cpexpr((expptr
)dovarp
), doinit
);
inc
= mkexpr (OPPLUSEQ
, (expptr
)dovarp
, cpexpr (ctlstack
-> dostep
));
if (!onetripflag
&& ISCONST (ctlstack
-> domax
) && ISCONST (doinit
)
&& ctlstack
-> dostepsign
!= VARSTEP
) {
tester
= mkexpr (OPMINUS
, cpexpr (doinit
),
cpexpr (ctlstack
-> domax
));
if (incsign
== conssgn (tester
))
warn ("DO range never executed");
} /* if !onetripflag && */
p1_for (init
, test
, inc
);
here
= ctlstack
->dolabel
;
if (ctlstack
->ctltype
!= CTLDO
|| here
>= 0 && (!thislabel
|| thislabel
->labelno
!= here
)) {
if (np
!= ctlstack
->loopname
) {
if (np1
= ctlstack
->loopname
)
errstr("expected \"enddo %s\"", np1
->fvarname
);
err("expected unnamed ENDDO");
for(cf
= ctls
; cf
< ctlstack
; cf
++)
if (cf
->ctltype
== CTLDO
&& cf
->loopname
== np
) {
register struct Ctlframe
*q
;
Namep np
; /* name of the current DO index */
/* Many DO's can end at the same statement, so keep looping over all
if(np
= ctlstack
->donamep
)
/* Now we're done with all of the tests, and the loop has terminated.
Store the index value back in long-term memory */
puteq((expptr
)ap
, (expptr
)mkplace(np
));
ctlstack
->ctlabels
[i
] = 0;
deregister(ctlstack
->donamep
);
ctlstack
->donamep
->vdovar
= NO
;
if (e
->tag
== TADDR
&& e
->addrblock
.istemp
)
if (e
->tag
== TADDR
&& e
->addrblock
.istemp
)
else if (ctlstack
->dowhile
)
/* Set dorange to the closing label of the next most enclosing DO loop
for(q
= ctlstack
; q
>=ctls
; --q
)
exassign(vname
, labelval
)
struct Labelblock
*labelval
;
register chainp cp
, cpprev
;
if( ! ONEOF(p
->vtype
, MSKINT
|MSKADDR
) ) {
err("noninteger assign variable");
/* If the label hasn't been defined, then we do things twice:
* once for an executable stmt label, once for a format
/* code for executable label... */
/* Now store the assigned value in a list associated with this variable.
This will be used later to generate a switch() statement in the C output */
fs
= labelval
->fmtstring
;
if (!labelval
->labdefined
|| !fs
) {
if (vname
-> vis_assigned
== 0) {
vname
-> varxptr
.assigned_values
= CHNULL
;
vname
-> vis_assigned
= 1;
/* don't duplicate labels... */
stno
= labelval
->stateno
;
for(k
= 0, cp
= vname
->varxptr
.assigned_values
;
cp
; cpprev
= cp
, cp
= cp
->nextp
, k
++)
if ((ftnint
)cp
->datap
== stno
)
cp
= mkchain((char *)stno
, CHNULL
);
vname
->varxptr
.assigned_values
= cp
;
putout(mkexpr(OPASSIGN
, (expptr
)p
, mkintcon(k
)));
/* Code for FORMAT label... */
if (!labelval
->labdefined
|| fs
) {
labelval
->fmtlabused
= 1;
q
->uname_tag
= UNAM_IDENT
;
sprintf(q
->user
.ident
, "fmt_%ld", labelval
->stateno
);
putout(mkexpr(OPASSIGN
, (expptr
)p
, (expptr
)q
));
exarif(expr
, neglab
, zerlab
, poslab
)
struct Labelblock
*neglab
, *zerlab
, *poslab
;
if( ! ONEOF(expr
->headblock
.vtype
, MSKINT
|MSKREAL
) )
err("invalid type of arithmetic if expression");
if (lm
== lz
&& lz
== lp
)
exar2(OPLE
, expr
, neglab
, poslab
);
exar2(OPNE
, expr
, neglab
, zerlab
);
exar2(OPGE
, expr
, zerlab
, neglab
);
if (!addressable (expr
)) {
t
= (expptr
) mktmp(expr
-> headblock
.vtype
, ENULL
);
expr
= mkexpr (OPASSIGN
, cpexpr (t
), expr
);
t
= (expptr
) cpexpr (expr
);
p1_if(putx(fixtype(mkexpr (OPLT
, expr
, ICON (0)))));
p1_elif (mkexpr (OPEQ
, t
, ICON (0)));
/* exar2 -- Do arithmetic IF for only 2 distinct labels; if !(e.op.0)
goto l2 else goto l1. If this seems backwards, that's because it is,
in order to make the 1 pass algorithm work. */
struct Labelblock
*l1
, *l2
;
comp
= mkexpr (op
, e
, ICON (0));
p1_if(putx(fixtype(comp
)));
/* exreturn -- return the value in p from a SUBROUTINE call -- used to
implement the alternate return mechanism */
warn("RETURN statement in main or block data");
if(p
&& (proctype
!=TYSUBR
|| procclass
!=CLPROC
) )
err("alternate return in nonsubroutine");
if (p
|| proctype
== TYSUBR
) {
if (p
== ENULL
) p
= ICON (0);
p
= mkconv (TYLONG
, fixtype (p
));
} /* if p || proctype == TYSUBR */
p1_subr_ret((expptr
)retslot
);
err("assigned goto variable must be integer");