xref: /original-bsd/usr.bin/pascal/src/flvalue.c (revision bac5051c)
1 /* Copyright (c) 1980 Regents of the University of California */
2 
3 static	char sccsid[] = "@(#)flvalue.c 1.4 01/10/81";
4 
5 #include "whoami.h"
6 #include "0.h"
7 #include "tree.h"
8 #include "opcode.h"
9 #include "objfmt.h"
10 #ifdef PC
11 #   include "pc.h"
12 #   include "pcops.h"
13 #endif PC
14 #ifdef OBJ
15 /*
16  * runtime display structure
17  */
18 struct dispsave {
19 	char *locvars;		/* pointer to local variables */
20 	struct stack *stp;	/* pointer to local stack frame */
21 };
22 #endif OBJ
23 
24     /*
25      *	flvalue generates the code to either pass on a formal routine,
26      *	or construct the structure which is the environment for passing.
27      *	it tells the difference by looking at the tree it's given.
28      */
29 struct nl *
30 flvalue( r , formalp )
31     int		*r;
32     struct nl	*formalp;
33     {
34 	struct nl	*p;
35 	long		tempoff;
36 	char		*typename;
37 
38 	if ( r == NIL ) {
39 	    return NIL;
40 	}
41 	typename = formalp -> class == FFUNC ? "function":"procedure";
42 	if ( r[0] != T_VAR ) {
43 	    error("Expression given, %s required for %s parameter %s" ,
44 		    typename , typename , formalp -> symbol );
45 	    return NIL;
46 	}
47 	p = lookup(r[2]);
48 	if (p == NIL) {
49 	    return NIL;
50 	}
51 	switch ( p -> class ) {
52 	    case FFUNC:
53 	    case FPROC:
54 		    if ( r[3] != NIL ) {
55 			error("Formal %s %s cannot be qualified" ,
56 				typename , p -> symbol );
57 			return NIL;
58 		    }
59 #		    ifdef OBJ
60 			put( 2 , PTR_RV | bn << 8+INDX , p -> value[NL_OFFS] );
61 #		    endif OBJ
62 #		    ifdef PC
63 			putRV( p -> symbol , bn , p -> value[ NL_OFFS ] ,
64 				p2type( p ) );
65 #		    endif PC
66 		    return p -> type;
67 	    case FUNC:
68 	    case PROC:
69 		    if ( r[3] != NIL ) {
70 			error("%s %s cannot be qualified" , typename ,
71 				p -> symbol );
72 			return NIL;
73 		    }
74 		    if (bn == 0) {
75 			error("Built-in %s %s cannot be passed as a parameter" ,
76 				typename , p -> symbol );
77 			return NIL;
78 		    }
79 			/*
80 			 *	formal routine structure:
81 			 *
82 			 *	struct formalrtn {
83 			 *		long		(*entryaddr)();
84 			 *		long		cbn;
85 			 *		struct dispsave	disp[2*MAXLVL];
86 			 *	};
87 			 */
88 		    sizes[ cbn ].om_off -=	  sizeof (long (*)())
89 						+ sizeof (long)
90 						+ 2*bn*sizeof (struct dispsave);
91 		    tempoff = sizes[ cbn ].om_off;
92 		    if ( sizes[ cbn ].om_off < sizes[ cbn ].om_max ) {
93 			sizes[ cbn ].om_max = tempoff;
94 		    }
95 #		    ifdef OBJ
96 			put( 2 , O_LV | cbn << 8 + INDX , tempoff );
97 			/* put( 2 , O_FSAV | bn << 8 + INDX , p -> entloc ); */
98 			put( 2 , O_FSAV | bn << 8 , p -> entloc );
99 #		    endif OBJ
100 #		    ifdef PC
101 			putlbracket( ftnno , -tempoff );
102 			putleaf( P2ICON , 0 , 0 ,
103 			    ADDTYPE( P2PTR , ADDTYPE( P2FTN , P2PTR|P2STRTY ) ) ,
104 			    "_FSAV" );
105 			{
106 			    char	extname[ BUFSIZ ];
107 			    char	*starthere;
108 			    int		i;
109 
110 			    starthere = &extname[0];
111 			    for ( i = 1 ; i < bn ; i++ ) {
112 				sprintf( starthere , EXTFORMAT , enclosing[ i ] );
113 				starthere += strlen( enclosing[ i ] ) + 1;
114 			    }
115 			    sprintf( starthere , EXTFORMAT , p -> symbol );
116 			    starthere += strlen( p -> symbol ) + 1;
117 			    if ( starthere >= &extname[ BUFSIZ ] ) {
118 				panic( "flvalue namelength" );
119 			    }
120 			    putleaf( P2ICON , 0 , 0 , p2type( p ) , extname );
121 			}
122 			putleaf( P2ICON , bn , 0 , P2INT , 0 );
123 			putop( P2LISTOP , P2INT );
124 			putLV( 0 , cbn , tempoff , P2STRTY );
125 			putop( P2LISTOP , P2INT );
126 			putop( P2CALL , P2PTR | P2STRTY );
127 #		    endif PC
128 		    return p -> type;
129 	    default:
130 		    error("Variable given, %s required for %s parameter %s" ,
131 			    typename , typename , formalp -> symbol );
132 		    return NIL;
133 	}
134     }
135