1745Speter /* Copyright (c) 1979 Regents of the University of California */ 2745Speter 3*11855Speter static char sccsid[] = "@(#)call.c 1.23 04/06/83"; 4745Speter 5745Speter #include "whoami.h" 6745Speter #include "0.h" 7745Speter #include "tree.h" 8745Speter #include "opcode.h" 9745Speter #include "objfmt.h" 10745Speter #ifdef PC 11745Speter # include "pc.h" 12745Speter # include "pcops.h" 13745Speter #endif PC 1411331Speter #include "tmps.h" 15745Speter 16745Speter /* 17745Speter * Call generates code for calls to 18745Speter * user defined procedures and functions 19745Speter * and is called by proc and funccod. 20745Speter * P is the result of the lookup 21745Speter * of the procedure/function symbol, 22745Speter * and porf is PROC or FUNC. 23745Speter * Psbn is the block number of p. 243065Smckusic * 253065Smckusic * the idea here is that regular scalar functions are just called, 263065Smckusic * while structure functions and formal functions have their results 273065Smckusic * stored in a temporary after the call. 283065Smckusic * structure functions do this because they return pointers 293065Smckusic * to static results, so we copy the static 303065Smckusic * and return a pointer to the copy. 313065Smckusic * formal functions do this because we have to save the result 323065Smckusic * around a call to the runtime routine which restores the display, 333065Smckusic * so we can't just leave the result lying around in registers. 343886Speter * formal calls save the address of the descriptor in a local 353886Speter * temporary, so it can be addressed for the call which restores 363886Speter * the display (FRTN). 373426Speter * calls to formal parameters pass the formal as a hidden argument 383426Speter * to a special entry point for the formal call. 393426Speter * [this is somewhat dependent on the way arguments are addressed.] 403065Smckusic * so PROCs and scalar FUNCs look like 413065Smckusic * p(...args...) 423065Smckusic * structure FUNCs look like 433065Smckusic * (temp = p(...args...),&temp) 443065Smckusic * formal FPROCs look like 454014Smckusic * ( t=p,( t -> entryaddr )(...args...,t,s),FRTN(t,s)) 463065Smckusic * formal scalar FFUNCs look like 474014Smckusic * ( t=p,temp=( t -> entryaddr )(...args...,t,s),FRTN(t,s),temp) 483065Smckusic * formal structure FFUNCs look like 494014Smckusic * (t=p,temp = ( t -> entryaddr )(...args...,t,s),FRTN(t,s),&temp) 50745Speter */ 51745Speter struct nl * 52745Speter call(p, argv, porf, psbn) 53745Speter struct nl *p; 54745Speter int *argv, porf, psbn; 55745Speter { 56745Speter register struct nl *p1, *q; 57745Speter int *r; 583065Smckusic struct nl *p_type_class = classify( p -> type ); 593297Smckusic bool chk = TRUE; 604014Smckusic struct nl *savedispnp; /* temporary to hold saved display */ 61745Speter # ifdef PC 623065Smckusic long p_p2type = p2type( p ); 633065Smckusic long p_type_p2type = p2type( p -> type ); 643065Smckusic bool noarguments; 653065Smckusic long calltype; /* type of the call */ 663065Smckusic /* 673065Smckusic * these get used if temporaries and structures are used 683065Smckusic */ 693824Speter struct nl *tempnlp; 703065Smckusic long temptype; /* type of the temporary */ 713065Smckusic long p_type_width; 723065Smckusic long p_type_align; 733362Speter char extname[ BUFSIZ ]; 743886Speter struct nl *tempdescrp; 75745Speter # endif PC 76745Speter 774014Smckusic if (p->class == FFUNC || p->class == FPROC) { 784014Smckusic /* 794014Smckusic * allocate space to save the display for formal calls 804014Smckusic */ 814014Smckusic savedispnp = tmpalloc( sizeof display , NIL , NOREG ); 824014Smckusic } 83745Speter # ifdef OBJ 843426Speter if (p->class == FFUNC || p->class == FPROC) { 854014Smckusic put(2, O_LV | cbn << 8 + INDX , 864014Smckusic (int) savedispnp -> value[ NL_OFFS ] ); 873359Smckusic put(2, PTR_RV | psbn << 8+INDX, (int)p->value[NL_OFFS]); 883426Speter } 893426Speter if (porf == FUNC) { 90745Speter /* 91745Speter * Push some space 92745Speter * for the function return type 93745Speter */ 943063Smckusic put(2, O_PUSH, leven(-lwidth(p->type))); 953426Speter } 96745Speter # endif OBJ 97745Speter # ifdef PC 983065Smckusic /* 993886Speter * if this is a formal call, 1003886Speter * stash the address of the descriptor 1013886Speter * in a temporary so we can find it 1023886Speter * after the FCALL for the call to FRTN 1033886Speter */ 1043886Speter if ( p -> class == FFUNC || p -> class == FPROC ) { 1053886Speter tempdescrp = tmpalloc(sizeof( struct formalrtn *) , NIL , 1063886Speter REGOK ); 1073886Speter putRV( 0 , cbn , tempdescrp -> value[ NL_OFFS ] , 1083886Speter tempdescrp -> extra_flags , P2PTR|P2STRTY ); 1093886Speter putRV( 0 , psbn , p -> value[ NL_OFFS ] , 1103886Speter p -> extra_flags , P2PTR|P2STRTY ); 1113886Speter putop( P2ASSIGN , P2PTR | P2STRTY ); 1123886Speter } 1133886Speter /* 1143065Smckusic * if we have to store a temporary, 1153065Smckusic * temptype will be its type, 1163065Smckusic * otherwise, it's P2UNDEF. 1173065Smckusic */ 1183065Smckusic temptype = P2UNDEF; 1193065Smckusic calltype = P2INT; 120745Speter if ( porf == FUNC ) { 1213065Smckusic p_type_width = width( p -> type ); 1223065Smckusic switch( p_type_class ) { 123745Speter case TSTR: 124745Speter case TSET: 125745Speter case TREC: 126745Speter case TFILE: 127745Speter case TARY: 1283065Smckusic calltype = temptype = P2STRTY; 1293065Smckusic p_type_align = align( p -> type ); 1303065Smckusic break; 1313065Smckusic default: 1323065Smckusic if ( p -> class == FFUNC ) { 1333065Smckusic calltype = temptype = p2type( p -> type ); 134745Speter } 1353065Smckusic break; 136745Speter } 1373065Smckusic if ( temptype != P2UNDEF ) { 1383824Speter tempnlp = tmpalloc(p_type_width, p -> type, NOREG); 1393065Smckusic /* 1403065Smckusic * temp 1413065Smckusic * for (temp = ... 1423065Smckusic */ 1433824Speter putRV( 0 , cbn , tempnlp -> value[ NL_OFFS ] , 1443824Speter tempnlp -> extra_flags , temptype ); 1453065Smckusic } 146745Speter } 1471195Speter switch ( p -> class ) { 1481195Speter case FUNC: 1491195Speter case PROC: 1503065Smckusic /* 1513065Smckusic * ... p( ... 1523065Smckusic */ 1533372Speter sextname( extname , p -> symbol , BLOCKNO(p -> nl_block) ); 1543362Speter putleaf( P2ICON , 0 , 0 , p2type( p ) , extname ); 1551195Speter break; 1561195Speter case FFUNC: 1571195Speter case FPROC: 1583886Speter 1591195Speter /* 1603886Speter * ... ( t -> entryaddr )( ... 1611195Speter */ 1623886Speter putRV( 0 , cbn , tempdescrp -> value[ NL_OFFS ] , 1633886Speter tempdescrp -> extra_flags , P2PTR | P2STRTY ); 1643426Speter if ( FENTRYOFFSET != 0 ) { 1653426Speter putleaf( P2ICON , FENTRYOFFSET , 0 , P2INT , 0 ); 1663426Speter putop( P2PLUS , 1673426Speter ADDTYPE( 1683426Speter ADDTYPE( ADDTYPE( p2type( p ) , P2FTN ) , 1693426Speter P2PTR ) , 1703426Speter P2PTR ) ); 1713426Speter } 1723426Speter putop( P2UNARY P2MUL , 1733426Speter ADDTYPE( ADDTYPE( p2type( p ) , P2FTN ) , P2PTR ) ); 1741195Speter break; 1751195Speter default: 1761195Speter panic("call class"); 177745Speter } 1783065Smckusic noarguments = TRUE; 179745Speter # endif PC 180745Speter /* 181745Speter * Loop and process each of 182745Speter * arguments to the proc/func. 1833065Smckusic * ... ( ... args ... ) ... 184745Speter */ 1853297Smckusic for (p1 = plist(p); p1 != NIL; p1 = p1->chain) { 1863297Smckusic if (argv == NIL) { 1873297Smckusic error("Not enough arguments to %s", p->symbol); 1883297Smckusic return (NIL); 1893297Smckusic } 1903297Smckusic switch (p1->class) { 1913297Smckusic case REF: 1923297Smckusic /* 1933297Smckusic * Var parameter 1943297Smckusic */ 1953297Smckusic r = argv[1]; 1963297Smckusic if (r != NIL && r[0] != T_VAR) { 1973297Smckusic error("Expression given (variable required) for var parameter %s of %s", p1->symbol, p->symbol); 1983361Speter chk = FALSE; 1993297Smckusic break; 2003297Smckusic } 2013372Speter q = lvalue( (int *) argv[1], MOD | ASGN , LREQ ); 2023297Smckusic if (q == NIL) { 2033297Smckusic chk = FALSE; 2043297Smckusic break; 2053297Smckusic } 2063297Smckusic if (q != p1->type) { 2073297Smckusic error("Parameter type not identical to type of var parameter %s of %s", p1->symbol, p->symbol); 2083361Speter chk = FALSE; 2093297Smckusic break; 2103297Smckusic } 2113297Smckusic break; 2123297Smckusic case VAR: 2133297Smckusic /* 2143297Smckusic * Value parameter 2153297Smckusic */ 216745Speter # ifdef OBJ 2173297Smckusic q = rvalue(argv[1], p1->type , RREQ ); 218745Speter # endif OBJ 219745Speter # ifdef PC 2203297Smckusic /* 2213297Smckusic * structure arguments require lvalues, 2223297Smckusic * scalars use rvalue. 2233297Smckusic */ 2243297Smckusic switch( classify( p1 -> type ) ) { 2253297Smckusic case TFILE: 2263297Smckusic case TARY: 2273297Smckusic case TREC: 2283297Smckusic case TSET: 2293297Smckusic case TSTR: 23010365Smckusick q = stkrval( argv[1] , p1 -> type , LREQ ); 231745Speter break; 2323297Smckusic case TINT: 2333297Smckusic case TSCAL: 2343297Smckusic case TBOOL: 2353297Smckusic case TCHAR: 2363297Smckusic precheck( p1 -> type , "_RANG4" , "_RSNG4" ); 23710365Smckusick q = stkrval( argv[1] , p1 -> type , RREQ ); 23810667Speter postcheck(p1 -> type, nl+T4INT); 239745Speter break; 24010365Smckusick case TDOUBLE: 24110365Smckusick q = stkrval( argv[1] , p1 -> type , RREQ ); 24210365Smckusick sconv(p2type(q), P2DOUBLE); 24310365Smckusick break; 2443297Smckusic default: 2453297Smckusic q = rvalue( argv[1] , p1 -> type , RREQ ); 2463297Smckusic break; 247745Speter } 2483297Smckusic # endif PC 2493297Smckusic if (q == NIL) { 2503297Smckusic chk = FALSE; 2513297Smckusic break; 2523297Smckusic } 2533297Smckusic if (incompat(q, p1->type, argv[1])) { 2543297Smckusic cerror("Expression type clashed with type of value parameter %s of %s", p1->symbol, p->symbol); 2553361Speter chk = FALSE; 2563297Smckusic break; 2573297Smckusic } 258745Speter # ifdef OBJ 2593297Smckusic if (isa(p1->type, "bcsi")) 2603297Smckusic rangechk(p1->type, q); 2613297Smckusic if (q->class != STR) 2623297Smckusic convert(q, p1->type); 263745Speter # endif OBJ 264745Speter # ifdef PC 2653297Smckusic switch( classify( p1 -> type ) ) { 2663297Smckusic case TFILE: 2673297Smckusic case TARY: 2683297Smckusic case TREC: 2693297Smckusic case TSET: 2703297Smckusic case TSTR: 2713297Smckusic putstrop( P2STARG 2723297Smckusic , p2type( p1 -> type ) 2733297Smckusic , lwidth( p1 -> type ) 2743297Smckusic , align( p1 -> type ) ); 2753297Smckusic } 2761195Speter # endif PC 2773297Smckusic break; 2783297Smckusic case FFUNC: 2791195Speter /* 2803297Smckusic * function parameter 2811195Speter */ 2823297Smckusic q = flvalue( (int *) argv[1] , p1 ); 2833297Smckusic chk = (chk && fcompat(q, p1)); 2843297Smckusic break; 2853297Smckusic case FPROC: 2861195Speter /* 2873297Smckusic * procedure parameter 2881195Speter */ 2893297Smckusic q = flvalue( (int *) argv[1] , p1 ); 2903297Smckusic chk = (chk && fcompat(q, p1)); 2913297Smckusic break; 2923297Smckusic default: 2933297Smckusic panic("call"); 2941195Speter } 2953297Smckusic # ifdef PC 2963297Smckusic /* 2973297Smckusic * if this is the nth (>1) argument, 2983297Smckusic * hang it on the left linear list of arguments 2993297Smckusic */ 3003297Smckusic if ( noarguments ) { 3013297Smckusic noarguments = FALSE; 3023297Smckusic } else { 3033297Smckusic putop( P2LISTOP , P2INT ); 3043297Smckusic } 3053297Smckusic # endif PC 3063297Smckusic argv = argv[2]; 307745Speter } 3083297Smckusic if (argv != NIL) { 3093297Smckusic error("Too many arguments to %s", p->symbol); 3103297Smckusic rvlist(argv); 3113297Smckusic return (NIL); 3123297Smckusic } 3133297Smckusic if (chk == FALSE) 3143297Smckusic return NIL; 315745Speter # ifdef OBJ 3161195Speter if ( p -> class == FFUNC || p -> class == FPROC ) { 3173359Smckusic put(2, PTR_RV | psbn << 8+INDX, (int)p->value[NL_OFFS]); 3184014Smckusic put(2, O_LV | cbn << 8 + INDX , 3194014Smckusic (int) savedispnp -> value[ NL_OFFS ] ); 3203297Smckusic put(1, O_FCALL); 3213063Smckusic put(2, O_FRTN, even(width(p->type))); 3221195Speter } else { 3237916Smckusick put(2, O_CALL | psbn << 8, (long)p->value[NL_ENTLOC]); 3241195Speter } 325745Speter # endif OBJ 326745Speter # ifdef PC 3273065Smckusic /* 3283426Speter * for formal calls: add the hidden argument 3293426Speter * which is the formal struct describing the 3303426Speter * environment of the routine. 3313426Speter * and the argument which is the address of the 3323426Speter * space into which to save the display. 3333426Speter */ 3343426Speter if ( p -> class == FFUNC || p -> class == FPROC ) { 3353886Speter putRV( 0 , cbn , tempdescrp -> value[ NL_OFFS ] , 3363886Speter tempdescrp -> extra_flags , P2PTR|P2STRTY ); 3373426Speter if ( !noarguments ) { 3383426Speter putop( P2LISTOP , P2INT ); 3393426Speter } 3403426Speter noarguments = FALSE; 3414014Smckusic putLV( 0 , cbn , savedispnp -> value[ NL_OFFS ] , 3424014Smckusic savedispnp -> extra_flags , P2PTR | P2STRTY ); 3434014Smckusic putop( P2LISTOP , P2INT ); 3443426Speter } 3453426Speter /* 3463065Smckusic * do the actual call: 3473065Smckusic * either ... p( ... ) ... 3483886Speter * or ... ( t -> entryaddr )( ... ) ... 3493065Smckusic * and maybe an assignment. 3503065Smckusic */ 351745Speter if ( porf == FUNC ) { 3523065Smckusic switch ( p_type_class ) { 353745Speter case TBOOL: 354745Speter case TCHAR: 355745Speter case TINT: 356745Speter case TSCAL: 357745Speter case TDOUBLE: 358745Speter case TPTR: 3593065Smckusic putop( ( noarguments ? P2UNARY P2CALL : P2CALL ) , 3603065Smckusic p_type_p2type ); 3613065Smckusic if ( p -> class == FFUNC ) { 3623065Smckusic putop( P2ASSIGN , p_type_p2type ); 363745Speter } 364745Speter break; 365745Speter default: 3663065Smckusic putstrop( ( noarguments ? P2UNARY P2STCALL : P2STCALL ), 3673065Smckusic ADDTYPE( p_type_p2type , P2PTR ) , 3683065Smckusic p_type_width , p_type_align ); 369*11855Speter putstrop(P2STASG, ADDTYPE(p_type_p2type, P2PTR), 370*11855Speter lwidth(p -> type), align(p -> type)); 371745Speter break; 372745Speter } 373745Speter } else { 3743065Smckusic putop( ( noarguments ? P2UNARY P2CALL : P2CALL ) , P2INT ); 3753065Smckusic } 3763065Smckusic /* 3773886Speter * ( t=p , ... , FRTN( t ) ... 3783065Smckusic */ 3793065Smckusic if ( p -> class == FFUNC || p -> class == FPROC ) { 3803886Speter putop( P2COMOP , P2INT ); 3813065Smckusic putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) , 3823065Smckusic "_FRTN" ); 3833886Speter putRV( 0 , cbn , tempdescrp -> value[ NL_OFFS ] , 3843886Speter tempdescrp -> extra_flags , P2PTR | P2STRTY ); 3854014Smckusic putLV( 0 , cbn , savedispnp -> value[ NL_OFFS ] , 3864014Smckusic savedispnp -> extra_flags , P2PTR | P2STRTY ); 3874014Smckusic putop( P2LISTOP , P2INT ); 3883065Smckusic putop( P2CALL , P2INT ); 3893065Smckusic putop( P2COMOP , P2INT ); 3903065Smckusic } 3913065Smckusic /* 3923065Smckusic * if required: 3933065Smckusic * either ... , temp ) 3943065Smckusic * or ... , &temp ) 3953065Smckusic */ 3963065Smckusic if ( porf == FUNC && temptype != P2UNDEF ) { 3973065Smckusic if ( temptype != P2STRTY ) { 3983824Speter putRV( 0 , cbn , tempnlp -> value[ NL_OFFS ] , 3993824Speter tempnlp -> extra_flags , p_type_p2type ); 400745Speter } else { 4013824Speter putLV( 0 , cbn , tempnlp -> value[ NL_OFFS ] , 4023824Speter tempnlp -> extra_flags , p_type_p2type ); 403745Speter } 4043065Smckusic putop( P2COMOP , P2INT ); 4053065Smckusic } 4063065Smckusic if ( porf == PROC ) { 407745Speter putdot( filename , line ); 408745Speter } 409745Speter # endif PC 410745Speter return (p->type); 411745Speter } 412745Speter 413745Speter rvlist(al) 414745Speter register int *al; 415745Speter { 416745Speter 417745Speter for (; al != NIL; al = al[2]) 418745Speter rvalue( (int *) al[1], NLNIL , RREQ ); 419745Speter } 4203297Smckusic 4213297Smckusic /* 4223297Smckusic * check that two function/procedure namelist entries are compatible 4233297Smckusic */ 4243297Smckusic bool 4253297Smckusic fcompat( formal , actual ) 4263297Smckusic struct nl *formal; 4273297Smckusic struct nl *actual; 4283297Smckusic { 4293297Smckusic register struct nl *f_chain; 4303297Smckusic register struct nl *a_chain; 4313297Smckusic bool compat = TRUE; 4323297Smckusic 4333297Smckusic if ( formal == NIL || actual == NIL ) { 4343297Smckusic return FALSE; 4353297Smckusic } 4363297Smckusic for (a_chain = plist(actual), f_chain = plist(formal); 4373297Smckusic f_chain != NIL; 4383297Smckusic f_chain = f_chain->chain, a_chain = a_chain->chain) { 4393297Smckusic if (a_chain == NIL) { 4403297Smckusic error("%s %s declared on line %d has more arguments than", 4413297Smckusic parnam(formal->class), formal->symbol, 4423297Smckusic linenum(formal)); 4433297Smckusic cerror("%s %s declared on line %d", 4443297Smckusic parnam(actual->class), actual->symbol, 4453297Smckusic linenum(actual)); 4463297Smckusic return FALSE; 4473297Smckusic } 4483297Smckusic if ( a_chain -> class != f_chain -> class ) { 4493297Smckusic error("%s parameter %s of %s declared on line %d is not identical", 4503297Smckusic parnam(f_chain->class), f_chain->symbol, 4513297Smckusic formal->symbol, linenum(formal)); 4523297Smckusic cerror("with %s parameter %s of %s declared on line %d", 4533297Smckusic parnam(a_chain->class), a_chain->symbol, 4543297Smckusic actual->symbol, linenum(actual)); 4553297Smckusic compat = FALSE; 4563297Smckusic } else if (a_chain->class == FFUNC || a_chain->class == FPROC) { 4573297Smckusic compat = (compat && fcompat(f_chain, a_chain)); 4583297Smckusic } 4593297Smckusic if ((a_chain->class != FPROC && f_chain->class != FPROC) && 4603297Smckusic (a_chain->type != f_chain->type)) { 4613297Smckusic error("Type of %s parameter %s of %s declared on line %d is not identical", 4623297Smckusic parnam(f_chain->class), f_chain->symbol, 4633297Smckusic formal->symbol, linenum(formal)); 4643297Smckusic cerror("to type of %s parameter %s of %s declared on line %d", 4653297Smckusic parnam(a_chain->class), a_chain->symbol, 4663297Smckusic actual->symbol, linenum(actual)); 4673297Smckusic compat = FALSE; 4683297Smckusic } 4693297Smckusic } 4703297Smckusic if (a_chain != NIL) { 4713297Smckusic error("%s %s declared on line %d has fewer arguments than", 4723297Smckusic parnam(formal->class), formal->symbol, 4733297Smckusic linenum(formal)); 4743297Smckusic cerror("%s %s declared on line %d", 4753297Smckusic parnam(actual->class), actual->symbol, 4763297Smckusic linenum(actual)); 4773297Smckusic return FALSE; 4783297Smckusic } 4793297Smckusic return compat; 4803297Smckusic } 4813297Smckusic 4823297Smckusic char * 4833297Smckusic parnam(nltype) 4843297Smckusic int nltype; 4853297Smckusic { 4863297Smckusic switch(nltype) { 4873297Smckusic case REF: 4883297Smckusic return "var"; 4893297Smckusic case VAR: 4903297Smckusic return "value"; 4913297Smckusic case FUNC: 4923297Smckusic case FFUNC: 4933297Smckusic return "function"; 4943297Smckusic case PROC: 4953297Smckusic case FPROC: 4963297Smckusic return "procedure"; 4973297Smckusic default: 4983297Smckusic return "SNARK"; 4993297Smckusic } 5003297Smckusic } 5013297Smckusic 5023297Smckusic plist(p) 5033297Smckusic struct nl *p; 5043297Smckusic { 5053297Smckusic switch (p->class) { 5063297Smckusic case FFUNC: 5073297Smckusic case FPROC: 5083297Smckusic return p->ptr[ NL_FCHAIN ]; 5093297Smckusic case PROC: 5103297Smckusic case FUNC: 5113297Smckusic return p->chain; 5123297Smckusic default: 5133297Smckusic panic("plist"); 5143297Smckusic } 5153297Smckusic } 5163297Smckusic 5173297Smckusic linenum(p) 5183297Smckusic struct nl *p; 5193297Smckusic { 5203297Smckusic if (p->class == FUNC) 5213297Smckusic return p->ptr[NL_FVAR]->value[NL_LINENO]; 5223297Smckusic return p->value[NL_LINENO]; 5233297Smckusic } 524