BSD 4 release
[unix-history] / usr / src / cmd / pi / flvalue.c
CommitLineData
3bcee7da
PK
1/* Copyright (c) 1980 Regents of the University of California */
2
31cef89c 3static char sccsid[] = "@(#)flvalue.c 1.2 10/2/80";
3bcee7da
PK
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
c4e911b6
PK
14#ifdef OBJ
15/*
16 * define the display structure for purposes of allocating
17 * a temporary
18 */
19struct dispsave {
20 char *ptr;
21};
22#endif OBJ
3bcee7da
PK
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 */
29struct nl *
c4e911b6
PK
30flvalue( r , formalp )
31 int *r;
32 struct nl *formalp;
3bcee7da
PK
33 {
34 struct nl *p;
35 long tempoff;
c4e911b6 36 char *typename;
3bcee7da
PK
37
38 if ( r == NIL ) {
39 return NIL;
40 }
c4e911b6
PK
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 }
3bcee7da
PK
47 p = lookup(r[2]);
48 if (p == NIL) {
c4e911b6 49 return NIL;
3bcee7da 50 }
c4e911b6
PK
51 switch ( p -> class ) {
52 case FFUNC:
53 case FPROC:
3bcee7da 54 if ( r[3] != NIL ) {
c4e911b6
PK
55 error("Formal %s %s cannot be qualified" ,
56 typename , p -> symbol );
3bcee7da
PK
57 return NIL;
58 }
3bcee7da
PK
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;
c4e911b6
PK
67 case FUNC:
68 case PROC:
3bcee7da 69 if ( r[3] != NIL ) {
c4e911b6
PK
70 error("%s %s cannot be qualified" , typename ,
71 p -> symbol );
3bcee7da
PK
72 return NIL;
73 }
c4e911b6
PK
74 if (bn == 0) {
75 error("Built-in %s %s cannot be passed as a parameter" ,
76 typename , p -> symbol );
3bcee7da
PK
77 return NIL;
78 }
3bcee7da
PK
79 /*
80 * formal routine structure:
81 *
82 * struct formalrtn {
83 * long (*entryaddr)();
84 * long cbn;
85 * struct dispsave disp[2*MAXLVL];
86 * };
87 */
c4e911b6 88 sizes[ cbn ].om_off -= sizeof (long (*)())
3bcee7da
PK
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
c4e911b6 96 put( 2 , O_LV | cbn << 8 + INDX , tempoff );
3bcee7da
PK
97 put( 2 , O_FSAV | bn << 8 + INDX , p -> entloc );
98# endif OBJ
99# ifdef PC
100 putlbracket( ftnno , -tempoff );
101 putleaf( P2ICON , 0 , 0 ,
c4e911b6 102 ADDTYPE( P2PTR , ADDTYPE( P2FTN , P2PTR|P2STRTY ) ) ,
3bcee7da
PK
103 "_FSAV" );
104 {
105 char extname[ BUFSIZ ];
106 char *starthere;
107 int i;
108
109 starthere = &extname[0];
110 for ( i = 1 ; i < bn ; i++ ) {
111 sprintf( starthere , EXTFORMAT , enclosing[ i ] );
112 starthere += strlen( enclosing[ i ] ) + 1;
113 }
114 sprintf( starthere , EXTFORMAT , p -> symbol );
115 starthere += strlen( p -> symbol ) + 1;
116 if ( starthere >= &extname[ BUFSIZ ] ) {
117 panic( "flvalue namelength" );
118 }
119 putleaf( P2ICON , 0 , 0 , p2type( p ) , extname );
120 }
121 putleaf( P2ICON , bn , 0 , P2INT , 0 );
122 putop( P2LISTOP , P2INT );
c4e911b6 123 putLV( 0 , cbn , tempoff , P2STRTY );
3bcee7da
PK
124 putop( P2LISTOP , P2INT );
125 putop( P2CALL , P2PTR | P2STRTY );
126# endif PC
127 return p -> type;
128 default:
c4e911b6
PK
129 error("Variable given, %s required for %s parameter %s" ,
130 typename , typename , formalp -> symbol );
131 return NIL;
3bcee7da
PK
132 }
133 }