/* some special actions, used in finding the type of nodes */
NAME: rval>0 is stab index for external
rval<0 is -inlabel number
rval has the STAB index, or - label number,
if a name whose address is in the constant
rval = NONAME means no name
REG: rval is reg. identification cookie
buildtree( o
, l
, r
) register NODE
*l
, *r
; {
register struct symtab
*sp
;
if( bdebug
) printf( "buildtree( %s, %o, %o )\n", opst
[o
], l
, r
);
/* check for constants */
if( opty
== UTYPE
&& l
->op
== ICON
){
if( hflag
) werror( "constant argument to NOT" );
if( conval( l
, o
, l
) ) return(l
);
else if( o
==UNARY MINUS
&& l
->op
==FCON
){
else if( o
==QUEST
&& l
->op
==ICON
) {
else if( (o
==ANDAND
|| o
==OROR
) && (l
->op
==ICON
||r
->op
==ICON
) ) goto ccwarn
;
else if( opty
== BITYPE
&& l
->op
== ICON
&& r
->op
== ICON
){
if( hflag
) werror( "constant in conditional context" );
if( conval( l
, o
, r
) ) {
else if( opty
== BITYPE
&& (l
->op
==FCON
||l
->op
==ICON
) &&
(r
->op
==FCON
||r
->op
==ICON
) ){
l
->type
= l
->csiz
= DOUBLE
;
if( r
->dval
== 0 ) uerror( "division by 0." );
/* its real; we must make a new node */
p
= block( o
, l
, r
, INT
, 0, INT
);
if( actions
&LVAL
){ /* check left descendent */
uerror( "lvalue required" );
p
->left
= pconvert( p
->left
);
else if( !(actions
& NCVT
) ){
p
->right
= pconvert( p
->right
);
p
->left
= pconvert( p
->left
);
if( (actions
&PUN
) && (o
!=CAST
||cflag
) ){
if( actions
& (TYPL
|TYPR
) ){
q
= (actions
&TYPL
) ? p
->left
: p
->right
;
if( actions
& CVTL
) p
= convert( p
, CVTL
);
if( actions
& CVTR
) p
= convert( p
, CVTR
);
if( actions
& TYMATCH
) p
= tymatch(p
);
if( actions
& PTMATCH
) p
= ptmatch(p
);
if( sp
->stype
== UNDEF
){
uerror( "%.8s undefined", sp
->sname
);
/* make p look reasonable */
p
->type
= p
->cdim
= p
->csiz
= INT
;
/* special case: MOETY is really an ICON... */
/* p->x turned into *(p+offset) */
/* rhs must be a name; check correctness */
if( i
<0 || ((sp
= &stab
[i
])->sclass
!= MOS
&& sp
->sclass
!= MOU
&& !(sp
->sclass
&FIELD
)) ){
uerror( "member of structure or union required" );
if( l
->type
!= PTR
+STRTY
&& l
->type
!= PTR
+UNIONTY
){
werror( "struct/union or struct/union pointer required" );
else if( (j
=l
->csiz
+1)<0 ) cerror( "undefined structure or union" );
else if( !chkstr( i
, dimtab
[j
], DECREF(l
->type
) ) ){
werror( "illegal member use: %.8s", stab
[i
].sname
);
if( l
->op
== UNARY AND
){
if( !ISPTR(l
->type
))uerror("illegal indirection");
p
->type
= DECREF(l
->type
);
p
->type
= INCREF( l
->type
);
lr
= buildtree( UNARY AND
, l
->right
, NIL
);
p
= buildtree( COMOP
, l
->left
, lr
);
lr
= buildtree( UNARY AND
, l
->right
->right
, NIL
);
ll
= buildtree( UNARY AND
, l
->right
->left
, NIL
);
p
->op
= l
->op
= l
->right
->op
= FREE
;
p
= buildtree( QUEST
, l
->left
, buildtree( COLON
, ll
, lr
) );
uerror( "unacceptable operand of &" );
if(tsize(p
->right
->type
, p
->right
->cdim
, p
->right
->csiz
) > SZINT
)
p
->right
= makety(p
->right
, INT
, 0, INT
);
/* structure assignment */
/* take the addresses of the two sides; then make an
/* operator using STASG and
/* the addresses of left and right */
if( l
->csiz
!= r
->csiz
) uerror( "assignment of different structures" );
r
= buildtree( UNARY AND
, r
, NIL
);
l
= block( STASG
, l
, r
, t
, d
, s
);
if( l
->csiz
!= r
->csiz
) uerror( "type clash in conditional" );
p
->right
= r
= strargs( p
->right
);
if( !ISPTR(l
->type
)) uerror("illegal function");
p
->type
= DECREF(l
->type
);
if( !ISFTN(p
->type
)) uerror("illegal function");
p
->type
= DECREF( p
->type
);
if( l
->op
== UNARY AND
&& l
->left
->op
== NAME
&&
l
->left
->rval
>= 0 && l
->left
->rval
!= NONAME
&&
( (i
=stab
[l
->left
->rval
].sclass
) == FORTRAN
|| i
==UFORTRAN
) ){
p
->op
+= (FORTCALL
-CALL
);
if( p
->type
== STRTY
|| p
->type
== UNIONTY
){
/* function returning structure */
/* make function really return ptr to str., with * */
p
->type
= INCREF( p
->type
);
p
= buildtree( UNARY MUL
, p
, NIL
);
cerror( "other code %d", o
);
if( actions
& CVTO
) p
= oconvert(p
);
if( bdebug
) fwalk( p
, eprint
, 0 );
strargs( p
) register NODE
*p
; { /* rewrite structure flavored arguments */
p
->left
= strargs( p
->left
);
p
->right
= strargs( p
->right
);
if( p
->type
== STRTY
|| p
->type
== UNIONTY
){
p
= block( STARG
, p
, NIL
, p
->type
, p
->cdim
, p
->csiz
);
p
->left
= buildtree( UNARY AND
, p
->left
, NIL
);
chkstr( i
, j
, type
) TWORD type
; {
/* is the MOS or MOU at stab[i] OK for strict reference by a ptr */
/* i has been checked to contain a MOS or MOU */
/* j is the index in dimtab of the members... */
if( ddebug
> 1 ) printf( "chkstr( %.8s(%d), %d )\n", stab
[i
].sname
, i
, j
);
if( (k
= j
) < 0 ) uerror( "undefined structure or union" );
for( ; (kk
= dimtab
[k
] ) >= 0; ++k
){
cerror( "gummy structure" );
if( kk
== i
) return( 1 );
switch( stab
[kk
].stype
){
if( type
== STRTY
) continue; /* no recursive looking for strs */
if( chkstr( i
, dimtab
[stab
[kk
].sizoff
+1], stab
[kk
].stype
) ) return(1);
conval( p
, o
, q
) register NODE
*p
, *q
; {
/* apply the op o to the lval part of p; if binary, rhs is val */
u
= ISUNSIGNED(p
->type
) || ISUNSIGNED(q
->type
);
if( u
&& (o
==LE
||o
==LT
||o
==GE
||o
==GT
)) o
+= (UGE
-GE
);
if( p
->rval
!= NONAME
&& q
->rval
!= NONAME
) return(0);
if( q
->rval
!= NONAME
&& o
!=PLUS
) return(0);
if( p
->rval
!= NONAME
&& o
!=PLUS
&& o
!=MINUS
) return(0);
if( val
== 0 ) uerror( "division by 0" );
if( val
== 0 ) uerror( "division by 0" );
p
->lval
= p
->lval
<= val
;
p
->lval
= p
->lval
>= val
;
p
->lval
= (p
->lval
-val
)<0;
p
->lval
= (p
->lval
-val
)<=0;
p
->lval
= (p
->lval
-val
)>=0;
p
->lval
= (p
->lval
-val
)>0;
p
->lval
= p
->lval
== val
;
p
->lval
= p
->lval
!= val
;
chkpun(p
) register NODE
*p
; {
/* checks p for the existance of a pun */
/* this is called when the op of p is ASSIGN, RETURN, CAST, COLON, or relational */
/* one case is when enumerations are used: this applies only to lint */
/* in the other case, one operand is a pointer, the other integer type */
/* we check that this integer is in fact a constant zero... */
/* in the case of ASSIGN, any assignment of pointer to integer is illegal */
/* this falls out, because the LHS is never 0 */
if( t1
==ENUMTY
|| t2
==ENUMTY
) { /* check for enumerations */
if( logop( p
->op
) && p
->op
!= EQ
&& p
->op
!= NE
) {
uerror( "illegal comparison of enums" );
if( t1
==ENUMTY
&& t2
==ENUMTY
&& p
->left
->csiz
==p
->right
->csiz
) return;
werror( "enumeration type clash, operator %s", opst
[p
->op
] );
if( ISPTR(t1
) || ISARY(t1
) ) q
= p
->right
;
if( !ISPTR(q
->type
) && !ISARY(q
->type
) ){
if( q
->op
!= ICON
|| q
->lval
!= 0 ){
werror( "illegal combination of pointer and integer");
if( p
->left
->csiz
!= p
->right
->csiz
) {
werror( "illegal structure pointer combination" );
if( ISARY(t1
) || ISPTR(t1
) ){
if( !ISARY(t2
) && !ISPTR(t2
) ) break;
if( ISARY(t1
) && ISARY(t2
) && dimtab
[d1
] != dimtab
[d2
] ){
werror( "illegal array size combination" );
werror( "illegal pointer combination" );
stref( p
) register NODE
*p
; {
register struct symtab
*q
;
/* this is also used to reference automatic variables */
q
= &stab
[p
->right
->rval
];
/* make p look like ptr to x */
p
= makety( p
, t
, d
, s
);
/* compute the offset to be added */
if( dsc
& FIELD
){ /* make fields look like ints */
if( off
!= 0 ) p
= clocal( block( PLUS
, p
, offcon( off
, t
, d
, s
), t
, d
, s
) );
p
= buildtree( UNARY MUL
, p
, NIL
);
/* if field, build field info */
p
= block( FLD
, p
, NIL
, q
->stype
, 0, q
->sizoff
);
p
->rval
= PKFIELD( dsc
&FLDSIZ
, q
->offset
%ALINT
);
notlval(p
) register NODE
*p
; {
/* return 0 if p an lvalue, 1 otherwise */
if( ISARY(p
->type
) || ISFTN(p
->type
) ) return(1);
bcon( i
){ /* make a constant node with value i */
p
= block( ICON
, NIL
, NIL
, INT
, 0, INT
);
bpsize(p
) register NODE
*p
; {
return( offcon( psize(p
), p
->type
, p
->cdim
, p
->csiz
) );
/* p is a node of type pointer; psize returns the
size of the thing pointed to */
uerror( "pointer required");
/* note: no pointers to fields */
return( tsize( DECREF(p
->type
), p
->cdim
, p
->csiz
) );
convert( p
, f
) register NODE
*p
; {
/* convert an operand of p
operand has type int, and is converted by the size of the other side
q
= (f
==CVTL
)?p
->left
:p
->right
;
q
, bpsize(f
==CVTL
?p
->right
:p
->left
), INT
, 0, INT
);
econvert( p
) register NODE
*p
; {
/* change enums to ints, or appropriate types */
if( (ty
=BTYPE(p
->type
)) == ENUMTY
|| ty
== MOETY
) {
if( dimtab
[ p
->csiz
] == SZCHAR
) ty
= CHAR
;
else if( dimtab
[ p
->csiz
] == SZINT
) ty
= INT
;
else if( dimtab
[ p
->csiz
] == SZSHORT
) ty
= SHORT
;
if( p
->op
== ICON
&& ty
!= LONG
) p
->type
= p
->csiz
= INT
;
pconvert( p
) register NODE
*p
; {
/* if p should be changed into a pointer, do so */
p
->type
= DECREF( p
->type
);
return( buildtree( UNARY AND
, p
, NIL
) );
return( buildtree( UNARY AND
, p
, NIL
) );
oconvert(p
) register NODE
*p
; {
/* convert the result itself: used for pointer and unsigned */
if( ISUNSIGNED(p
->left
->type
) || ISUNSIGNED(p
->right
->type
) ) p
->op
+= (ULE
-LE
);
return( clocal( block( PVCONV
,
p
, bpsize(p
->left
), INT
, 0, INT
) ) );
cerror( "illegal oconvert: %d", p
->op
);
ptmatch(p
) register NODE
*p
; {
/* makes the operands of p agree; they are
either pointers or integers, by this time */
/* with MINUS, the sizes must be the same */
/* with COLON, the types must be the same */
{ if( psize(p
->left
) != psize(p
->right
) ){
uerror( "illegal pointer subtraction");
{ if( t1
!= t2
) uerror( "illegal types in :");
default: /* must work harder: relationals or comparisons */
if( talign(t2
,s2
) < talign(t
,s
) ){
p
->left
= makety( p
->left
, t
, d
, s
);
p
->right
= makety( p
->right
, t
, d
, s
);
if( o
!=MINUS
&& !logop(o
) ){
tymatch(p
) register NODE
*p
; {
/* satisfy the types of various arithmetic binary ops */
if assignment, op, type of LHS
if any float or doubles, make double
if either operand is unsigned, the result is...
register TWORD t1
, t2
, t
, tu
;
if( ( t1
== CHAR
|| t1
== SHORT
) && o
!= RETURN
) t1
= INT
;
if( t2
== CHAR
|| t2
== SHORT
) t2
= INT
;
if( t1
==DOUBLE
|| t1
==FLOAT
|| t2
==DOUBLE
|| t2
==FLOAT
) t
= DOUBLE
;
else if( t1
==LONG
|| t2
==LONG
) t
= LONG
;
tu
= (u
&& UNSIGNABLE(t
))?ENUNSIGN(t
):t
;
/* because expressions have values that are at least as wide
as INT or UNSIGNED, the only conversions needed
are those involving FLOAT/DOUBLE, and those
from LONG to INT and ULONG to UNSIGNED */
if( t
!= t1
) p
->left
= makety( p
->left
, tu
, 0, (int)tu
);
if( t
!= t2
|| o
==CAST
) p
->right
= makety( p
->right
, tu
, 0, (int)tu
);
if( tdebug
) printf( "tymatch(%o): %o %s %o => %o\n",p
,t1
,opst
[o
],t2
,tu
);
makety( p
, t
, d
, s
) register NODE
*p
; TWORD t
; {
/* make p into type t by inserting a conversion */
if( p
->type
== ENUMTY
&& p
->op
== ICON
) econvert(p
);
return( block( PCONV
, p
, NIL
, t
, d
, s
) );
if( t
==DOUBLE
||t
==FLOAT
){
if( ISUNSIGNED(p
->type
) ){
p
->dval
= /* (unsigned CONSZ) */ p
->lval
;
return( block( SCONV
, p
, NIL
, t
, d
, s
) );
block( o
, l
, r
, t
, d
, s
) register NODE
*l
, *r
; TWORD t
; {
icons(p
) register NODE
*p
; {
/* if p is an integer constant, return its value */
uerror( "constant expected");
if( val
!= p
->lval
) uerror( "constant too big for cross-compiler" );
/* the intent of this table is to examine the
operators, and to check them for
The table is searched for the op and the
modified type (where this is one of the
types INT (includes char and short), LONG,
DOUBLE (includes FLOAT), and POINTER
The default action is to make the node type integer
The actions taken include:
CVTL convert the left operand
CVTR convert the right operand
TYPL the type is determined by the left operand
TYPR the type is determined by the right operand
TYMATCH force type of left and right to match, by inserting conversions
PTMATCH like TYMATCH, but for pointers
LVAL left operand must be lval
NCVT do not convert the operands
NCVTR convert the left operand, not the right...
# define MINT 01 /* integer */
# define MDBI 02 /* integer or double */
# define MSTR 04 /* structure */
# define MPTR 010 /* pointer */
# define MPTI 020 /* pointer or integer */
# define MENU 040 /* enumeration variable or member */
register mt12
, mt1
, mt2
, o
;
switch( optype(o
=p
->op
) ){
mt12
=mt2
= moditype( p
->right
->type
);
mt12
&= (mt1
= moditype( p
->left
->type
));
if( mt1
& MDBI
) return( TYPL
);
if( mt1
& MINT
) return( TYPL
);
{ return( NCVT
+OTHER
); }
if( mt12
& MDBI
) return( TYMATCH
);
if( mt12
& MINT
) return( TYMATCH
);
if( mt12
& MINT
) return( TYPL
+OTHER
);
if( (mt1
&MENU
)||(mt2
&MENU
) ) return( PTMATCH
+PUN
+NCVT
);
if( mt12
& MDBI
) return( TYMATCH
+CVTO
);
else if( mt12
& MPTR
) return( PTMATCH
+PUN
);
else if( mt12
& MPTI
) return( PTMATCH
+PUN
);
if( mt2
&MENU
) return( TYPR
+NCVTR
);
if( mt12
& MENU
) return( NCVT
+PUN
+PTMATCH
);
else if( mt12
& MDBI
) return( TYMATCH
);
else if( mt12
& MPTR
) return( TYPL
+PTMATCH
+PUN
);
else if( (mt1
&MINT
) && (mt2
&MPTR
) ) return( TYPR
+PUN
);
else if( (mt1
&MPTR
) && (mt2
&MINT
) ) return( TYPL
+PUN
);
else if( mt12
& MSTR
) return( NCVT
+TYPL
+OTHER
);
if( mt12
& MSTR
) return( LVAL
+NCVT
+TYPL
+OTHER
);
if( mt12
& MDBI
) return( TYPL
+LVAL
+TYMATCH
);
else if( (mt1
&MENU
)||(mt2
&MENU
) ) return( LVAL
+NCVT
+TYPL
+PTMATCH
+PUN
);
else if( mt1
& MPTR
) return( LVAL
+PTMATCH
+PUN
);
else if( mt12
& MPTI
) return( TYPL
+LVAL
+TYMATCH
+PUN
);
if( mt12
& MINT
) return( TYPL
+LVAL
+OTHER
);
if( mt12
& MDBI
) return( LVAL
+TYMATCH
);
if( mt12
& MINT
) return( LVAL
+TYMATCH
);
if( mt12
& MDBI
) return( TYMATCH
+LVAL
);
else if( (mt1
&MPTR
) && (mt2
&MINT
) ) return( TYPL
+LVAL
+CVTR
);
if( mt12
& MPTR
) return( CVTO
+PTMATCH
+PUN
);
if( mt12
& MDBI
) return( TYMATCH
);
else if( (mt1
&MPTR
) && (mt2
&MINT
) ) return( TYPL
+CVTR
);
else if( (mt1
&MINT
) && (mt2
&MPTR
) ) return( TYPR
+CVTL
);
uerror( "operands of %s have incompatible types", opst
[o
] );
moditype( ty
) TWORD ty
; {
return( MINT
|MDBI
|MPTI
);
doszof( p
) register NODE
*p
; {
/* whatever is the meaning of this if it is a bitfield? */
i
= tsize( p
->type
, p
->cdim
, p
->csiz
)/SZCHAR
;
if( i
<= 0 ) werror( "sizeof returns 0" );
eprint( p
, down
, a
, b
) register NODE
*p
; int *a
, *b
; {
if( down
) printf( " " );
printf("%o) %s, ", p
, opst
[p
->op
] );
printf( CONFMT
, p
->lval
);
printf( ", %d, ", p
->rval
);
printf( ", %d, %d\n", p
->cdim
, p
->csiz
);
prtdcon( p
) register NODE
*p
; {
fincode( p
->dval
, SZDOUBLE
);
ecomp( p
) register NODE
*p
; {
if( edebug
) fwalk( p
, eprint
, 0 );
werror( "statement not reached" );
prtree(p
) register NODE
*p
; {
register struct symtab
*q
;
MYPRTREE(p
); /* local action can be taken here; then return... */
printf( CONFMT
, p
->lval
);
if( p
->op
== NAME
|| p
->op
== ICON
) printf( "0\t" );
else printf( "%d\t", p
->rval
);
printf( "%o\t", p
->type
);
/* handle special cases */
/* print external name */
if( p
->rval
== NONAME
) printf( "\n" );
printf( "%s\n", exname(q
->sname
) );
printf( LABFMT
, -p
->rval
);
/* use lhs size, in order to avoid hassles with the structure `.' operator */
/* note: p->left not a field... */
printf( CONFMT
, (CONSZ
) tsize( STRTY
, p
->left
->cdim
, p
->left
->csiz
) );
printf( "\t%d\t\n", talign( STRTY
, p
->left
->csiz
) );
if( ty
!= LTYPE
) prtree( p
->left
);
if( ty
== BITYPE
) prtree( p
->right
);
p2tree(p
) register NODE
*p
; {
MYP2TREE(p
); /* local action can be taken here; then return... */
if( p
->rval
== NONAME
) p
->name
[0] = '\0';
else if( p
->rval
>= 0 ){ /* copy name from exname */
cp
= exname( stab
[p
->rval
].sname
);
for( i
=0; i
<NCHNAM
; ++i
) p
->name
[i
] = *cp
++;
else sprintf( p
->name
, LABFMT
, -p
->rval
);
/* set up size parameters */
p
->stsize
= (tsize(STRTY
,p
->left
->cdim
,p
->left
->csiz
)+SZCHAR
-1)/SZCHAR
;
p
->stalign
= talign(STRTY
,p
->left
->csiz
)/SZCHAR
;
rbusy( p
->rval
, p
->type
);
if( ty
!= LTYPE
) p2tree( p
->left
);
if( ty
== BITYPE
) p2tree( p
->right
);