1 /* Copyright (c) 1980 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)flvalue.c 1.7 03/18/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, (int)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; 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 tempoff = tmpalloc(sizeof (long (*)()) + sizeof (long) 89 + 2*bn*sizeof (struct dispsave), nl+TSTR, NOREG); 90 # ifdef OBJ 91 put(2 , O_LV | cbn << 8 + INDX , (int)tempoff ); 92 put(2, O_FSAV | bn << 8, (long)p->entloc); 93 # endif OBJ 94 # ifdef PC 95 putleaf( P2ICON , 0 , 0 , 96 ADDTYPE( P2PTR , ADDTYPE( P2FTN , P2PTR|P2STRTY ) ) , 97 "_FSAV" ); 98 { 99 char extname[ BUFSIZ ]; 100 char *starthere; 101 int i; 102 103 starthere = &extname[0]; 104 for ( i = 1 ; i < bn ; i++ ) { 105 sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 106 starthere += strlen( enclosing[ i ] ) + 1; 107 } 108 sprintf( starthere , EXTFORMAT , p -> symbol ); 109 starthere += strlen( p -> symbol ) + 1; 110 if ( starthere >= &extname[ BUFSIZ ] ) { 111 panic( "flvalue namelength" ); 112 } 113 putleaf( P2ICON , 0 , 0 , p2type( p ) , extname ); 114 } 115 putleaf( P2ICON , bn , 0 , P2INT , 0 ); 116 putop( P2LISTOP , P2INT ); 117 putLV( 0 , cbn , tempoff , P2STRTY ); 118 putop( P2LISTOP , P2INT ); 119 putop( P2CALL , P2PTR | P2STRTY ); 120 # endif PC 121 return p; 122 default: 123 error("Variable given, %s required for %s parameter %s" , 124 typename , typename , formalp -> symbol ); 125 return NIL; 126 } 127 } 128