xref: /original-bsd/usr.bin/pascal/src/flvalue.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)flvalue.c	8.1 (Berkeley) 06/06/93";
10 #endif /* not lint */
11 
12 #include "whoami.h"
13 #include "0.h"
14 #include "tree.h"
15 #include "opcode.h"
16 #include "objfmt.h"
17 #include "tree_ty.h"
18 #ifdef PC
19 #   include "pc.h"
20 #   include <pcc.h>
21 #endif PC
22 #include "tmps.h"
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     struct tnode *r; 	/* T_VAR */
32     struct nl	*formalp;
33     {
34 	struct nl	*p;
35 	struct nl	*tempnlp;
36 	char		*typename;
37 #ifdef PC
38 	char		extname[ BUFSIZ ];
39 #endif PC
40 
41 	if ( r == TR_NIL ) {
42 	    return NLNIL;
43 	}
44 	typename = formalp -> class == FFUNC ? "function":"procedure";
45 	if ( r->tag != T_VAR ) {
46 	    error("Expression given, %s required for %s parameter %s" ,
47 		    typename , typename , formalp -> symbol );
48 	    return NLNIL;
49 	}
50 	p = lookup(r->var_node.cptr);
51 	if (p == NLNIL) {
52 	    return NLNIL;
53 	}
54 	switch ( p -> class ) {
55 	    case FFUNC:
56 	    case FPROC:
57 		    if ( r->var_node.qual != TR_NIL ) {
58 			error("Formal %s %s cannot be qualified" ,
59 				typename , p -> symbol );
60 			return NLNIL;
61 		    }
62 #		    ifdef OBJ
63 			(void) put(2, PTR_RV | bn << 8+INDX, (int)p->value[NL_OFFS]);
64 #		    endif OBJ
65 #		    ifdef PC
66 			putRV( p -> symbol , bn , p -> value[ NL_OFFS ] ,
67 				p -> extra_flags ,
68 				p2type( p ) );
69 #		    endif PC
70 		    return p;
71 	    case FUNC:
72 	    case PROC:
73 		    if ( r->var_node.qual != TR_NIL ) {
74 			error("%s %s cannot be qualified" , typename ,
75 				p -> symbol );
76 			return NLNIL;
77 		    }
78 		    if (bn == 0) {
79 			error("Built-in %s %s cannot be passed as a parameter" ,
80 				typename , p -> symbol );
81 			return NLNIL;
82 		    }
83 			/*
84 			 *	allocate space for the thunk
85 			 */
86 		    tempnlp = tmpalloc((long) (sizeof(struct formalrtn)), NLNIL, NOREG);
87 #		    ifdef OBJ
88 			(void) put(2 , O_LV | cbn << 8 + INDX ,
89 				(int)tempnlp -> value[ NL_OFFS ] );
90 			(void) put(2, O_FSAV | bn << 8, (long)p->value[NL_ENTLOC]);
91 #		    endif OBJ
92 #		    ifdef PC
93 			putleaf( PCC_ICON , 0 , 0 ,
94 			    PCCM_ADDTYPE( PCCTM_PTR , PCCM_ADDTYPE( PCCTM_FTN , PCCTM_PTR|PCCT_STRTY ) ) ,
95 			    "_FSAV" );
96 			sprintf( extname , "%s" , FORMALPREFIX );
97 			sextname( &extname[ strlen( extname ) ] ,
98 				    p -> symbol , bn );
99 			putleaf( PCC_ICON , 0 , 0 , p2type( p ) , extname );
100 			putleaf( PCC_ICON , bn , 0 , PCCT_INT , (char *) 0 );
101 			putop( PCC_CM , PCCT_INT );
102 			putLV( (char *) 0 , cbn , tempnlp -> value[NL_OFFS] ,
103 				tempnlp -> extra_flags , PCCT_STRTY );
104 			putop( PCC_CM , PCCT_INT );
105 			putop( PCC_CALL , PCCTM_PTR | PCCT_STRTY );
106 #		    endif PC
107 		    return p;
108 	    default:
109 		    error("Variable given, %s required for %s parameter %s" ,
110 			    typename , typename , formalp -> symbol );
111 		    return NLNIL;
112 	}
113     }
114