1745Speter /* Copyright (c) 1979 Regents of the University of California */ 2745Speter 3*3426Speter static char sccsid[] = "@(#)call.c 1.13 04/01/81"; 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 14745Speter 15745Speter /* 16745Speter * Call generates code for calls to 17745Speter * user defined procedures and functions 18745Speter * and is called by proc and funccod. 19745Speter * P is the result of the lookup 20745Speter * of the procedure/function symbol, 21745Speter * and porf is PROC or FUNC. 22745Speter * Psbn is the block number of p. 233065Smckusic * 243065Smckusic * the idea here is that regular scalar functions are just called, 253065Smckusic * while structure functions and formal functions have their results 263065Smckusic * stored in a temporary after the call. 273065Smckusic * structure functions do this because they return pointers 283065Smckusic * to static results, so we copy the static 293065Smckusic * and return a pointer to the copy. 303065Smckusic * formal functions do this because we have to save the result 313065Smckusic * around a call to the runtime routine which restores the display, 323065Smckusic * so we can't just leave the result lying around in registers. 33*3426Speter * calls to formal parameters pass the formal as a hidden argument 34*3426Speter * to a special entry point for the formal call. 35*3426Speter * [this is somewhat dependent on the way arguments are addressed.] 363065Smckusic * so PROCs and scalar FUNCs look like 373065Smckusic * p(...args...) 383065Smckusic * structure FUNCs look like 393065Smckusic * (temp = p(...args...),&temp) 403065Smckusic * formal FPROCs look like 41*3426Speter * ( p -> entryaddr )(...args...,p),FRTN( p )) 423065Smckusic * formal scalar FFUNCs look like 43*3426Speter * (temp = ( p -> entryaddr )(...args...,p),FRTN( p ),temp) 443065Smckusic * formal structure FFUNCs look like 45*3426Speter * (temp = ( p -> entryaddr )(...args...,p),FRTN( p ),&temp) 46745Speter */ 47745Speter struct nl * 48745Speter call(p, argv, porf, psbn) 49745Speter struct nl *p; 50745Speter int *argv, porf, psbn; 51745Speter { 52745Speter register struct nl *p1, *q; 53745Speter int *r; 543065Smckusic struct nl *p_type_class = classify( p -> type ); 553297Smckusic bool chk = TRUE; 56*3426Speter long savedisp; /* temporary to hold saved display */ 57745Speter # ifdef PC 583065Smckusic long p_p2type = p2type( p ); 593065Smckusic long p_type_p2type = p2type( p -> type ); 603065Smckusic bool noarguments; 613065Smckusic long calltype; /* type of the call */ 623065Smckusic /* 633065Smckusic * these get used if temporaries and structures are used 643065Smckusic */ 653065Smckusic long tempoffset; 663065Smckusic long temptype; /* type of the temporary */ 673065Smckusic long p_type_width; 683065Smckusic long p_type_align; 693362Speter char extname[ BUFSIZ ]; 70745Speter # endif PC 71745Speter 72*3426Speter if (p->class == FFUNC || p->class == FPROC) { 73*3426Speter /* 74*3426Speter * allocate space to save the display for formal calls 75*3426Speter */ 76*3426Speter savedisp = tmpalloc( sizeof display , NIL , NOREG ); 77*3426Speter } 78745Speter # ifdef OBJ 79*3426Speter if (p->class == FFUNC || p->class == FPROC) { 80*3426Speter put(2, O_LV | cbn << 8 + INDX , (int) savedisp ); 813359Smckusic put(2, PTR_RV | psbn << 8+INDX, (int)p->value[NL_OFFS]); 82*3426Speter } 83*3426Speter if (porf == FUNC) { 84745Speter /* 85745Speter * Push some space 86745Speter * for the function return type 87745Speter */ 883063Smckusic put(2, O_PUSH, leven(-lwidth(p->type))); 89*3426Speter } 90745Speter # endif OBJ 91745Speter # ifdef PC 923065Smckusic /* 933065Smckusic * if we have to store a temporary, 943065Smckusic * temptype will be its type, 953065Smckusic * otherwise, it's P2UNDEF. 963065Smckusic */ 973065Smckusic temptype = P2UNDEF; 983065Smckusic calltype = P2INT; 99745Speter if ( porf == FUNC ) { 1003065Smckusic p_type_width = width( p -> type ); 1013065Smckusic switch( p_type_class ) { 102745Speter case TSTR: 103745Speter case TSET: 104745Speter case TREC: 105745Speter case TFILE: 106745Speter case TARY: 1073065Smckusic calltype = temptype = P2STRTY; 1083065Smckusic p_type_align = align( p -> type ); 1093065Smckusic break; 1103065Smckusic default: 1113065Smckusic if ( p -> class == FFUNC ) { 1123065Smckusic calltype = temptype = p2type( p -> type ); 113745Speter } 1143065Smckusic break; 115745Speter } 1163065Smckusic if ( temptype != P2UNDEF ) { 1173221Smckusic tempoffset = tmpalloc(p_type_width, p -> type, NOREG); 1183065Smckusic /* 1193065Smckusic * temp 1203065Smckusic * for (temp = ... 1213065Smckusic */ 1223065Smckusic putRV( 0 , cbn , tempoffset , temptype ); 1233065Smckusic } 124745Speter } 1251195Speter switch ( p -> class ) { 1261195Speter case FUNC: 1271195Speter case PROC: 1283065Smckusic /* 1293065Smckusic * ... p( ... 1303065Smckusic */ 1313372Speter sextname( extname , p -> symbol , BLOCKNO(p -> nl_block) ); 1323362Speter putleaf( P2ICON , 0 , 0 , p2type( p ) , extname ); 1331195Speter break; 1341195Speter case FFUNC: 1351195Speter case FPROC: 1361195Speter /* 137*3426Speter * ... ( p -> entryaddr )( ... 1381195Speter */ 139*3426Speter putRV( 0 , psbn , ( p -> value[NL_OFFS] ) , 140*3426Speter P2PTR | P2STRTY ); 141*3426Speter if ( FENTRYOFFSET != 0 ) { 142*3426Speter putleaf( P2ICON , FENTRYOFFSET , 0 , P2INT , 0 ); 143*3426Speter putop( P2PLUS , 144*3426Speter ADDTYPE( 145*3426Speter ADDTYPE( ADDTYPE( p2type( p ) , P2FTN ) , 146*3426Speter P2PTR ) , 147*3426Speter P2PTR ) ); 148*3426Speter } 149*3426Speter putop( P2UNARY P2MUL , 150*3426Speter ADDTYPE( ADDTYPE( p2type( p ) , P2FTN ) , P2PTR ) ); 1511195Speter break; 1521195Speter default: 1531195Speter panic("call class"); 154745Speter } 1553065Smckusic noarguments = TRUE; 156745Speter # endif PC 157745Speter /* 158745Speter * Loop and process each of 159745Speter * arguments to the proc/func. 1603065Smckusic * ... ( ... args ... ) ... 161745Speter */ 1623297Smckusic for (p1 = plist(p); p1 != NIL; p1 = p1->chain) { 1633297Smckusic if (argv == NIL) { 1643297Smckusic error("Not enough arguments to %s", p->symbol); 1653297Smckusic return (NIL); 1663297Smckusic } 1673297Smckusic switch (p1->class) { 1683297Smckusic case REF: 1693297Smckusic /* 1703297Smckusic * Var parameter 1713297Smckusic */ 1723297Smckusic r = argv[1]; 1733297Smckusic if (r != NIL && r[0] != T_VAR) { 1743297Smckusic error("Expression given (variable required) for var parameter %s of %s", p1->symbol, p->symbol); 1753361Speter chk = FALSE; 1763297Smckusic break; 1773297Smckusic } 1783372Speter q = lvalue( (int *) argv[1], MOD | ASGN , LREQ ); 1793297Smckusic if (q == NIL) { 1803297Smckusic chk = FALSE; 1813297Smckusic break; 1823297Smckusic } 1833297Smckusic if (q != p1->type) { 1843297Smckusic error("Parameter type not identical to type of var parameter %s of %s", p1->symbol, p->symbol); 1853361Speter chk = FALSE; 1863297Smckusic break; 1873297Smckusic } 1883297Smckusic break; 1893297Smckusic case VAR: 1903297Smckusic /* 1913297Smckusic * Value parameter 1923297Smckusic */ 193745Speter # ifdef OBJ 1943297Smckusic q = rvalue(argv[1], p1->type , RREQ ); 195745Speter # endif OBJ 196745Speter # ifdef PC 1973297Smckusic /* 1983297Smckusic * structure arguments require lvalues, 1993297Smckusic * scalars use rvalue. 2003297Smckusic */ 2013297Smckusic switch( classify( p1 -> type ) ) { 2023297Smckusic case TFILE: 2033297Smckusic case TARY: 2043297Smckusic case TREC: 2053297Smckusic case TSET: 2063297Smckusic case TSTR: 2073297Smckusic q = rvalue( argv[1] , p1 -> type , LREQ ); 208745Speter break; 2093297Smckusic case TINT: 2103297Smckusic case TSCAL: 2113297Smckusic case TBOOL: 2123297Smckusic case TCHAR: 2133297Smckusic precheck( p1 -> type , "_RANG4" , "_RSNG4" ); 2143297Smckusic q = rvalue( argv[1] , p1 -> type , RREQ ); 2153297Smckusic postcheck( p1 -> type ); 216745Speter break; 2173297Smckusic default: 2183297Smckusic q = rvalue( argv[1] , p1 -> type , RREQ ); 2193297Smckusic if ( isa( p1 -> type , "d" ) 2203297Smckusic && isa( q , "i" ) ) { 2213297Smckusic putop( P2SCONV , P2DOUBLE ); 2223297Smckusic } 2233297Smckusic break; 224745Speter } 2253297Smckusic # endif PC 2263297Smckusic if (q == NIL) { 2273297Smckusic chk = FALSE; 2283297Smckusic break; 2293297Smckusic } 2303297Smckusic if (incompat(q, p1->type, argv[1])) { 2313297Smckusic cerror("Expression type clashed with type of value parameter %s of %s", p1->symbol, p->symbol); 2323361Speter chk = FALSE; 2333297Smckusic break; 2343297Smckusic } 235745Speter # ifdef OBJ 2363297Smckusic if (isa(p1->type, "bcsi")) 2373297Smckusic rangechk(p1->type, q); 2383297Smckusic if (q->class != STR) 2393297Smckusic convert(q, p1->type); 240745Speter # endif OBJ 241745Speter # ifdef PC 2423297Smckusic switch( classify( p1 -> type ) ) { 2433297Smckusic case TFILE: 2443297Smckusic case TARY: 2453297Smckusic case TREC: 2463297Smckusic case TSET: 2473297Smckusic case TSTR: 2483297Smckusic putstrop( P2STARG 2493297Smckusic , p2type( p1 -> type ) 2503297Smckusic , lwidth( p1 -> type ) 2513297Smckusic , align( p1 -> type ) ); 2523297Smckusic } 2531195Speter # endif PC 2543297Smckusic break; 2553297Smckusic case FFUNC: 2561195Speter /* 2573297Smckusic * function parameter 2581195Speter */ 2593297Smckusic q = flvalue( (int *) argv[1] , p1 ); 2603297Smckusic chk = (chk && fcompat(q, p1)); 2613297Smckusic break; 2623297Smckusic case FPROC: 2631195Speter /* 2643297Smckusic * procedure parameter 2651195Speter */ 2663297Smckusic q = flvalue( (int *) argv[1] , p1 ); 2673297Smckusic chk = (chk && fcompat(q, p1)); 2683297Smckusic break; 2693297Smckusic default: 2703297Smckusic panic("call"); 2711195Speter } 2723297Smckusic # ifdef PC 2733297Smckusic /* 2743297Smckusic * if this is the nth (>1) argument, 2753297Smckusic * hang it on the left linear list of arguments 2763297Smckusic */ 2773297Smckusic if ( noarguments ) { 2783297Smckusic noarguments = FALSE; 2793297Smckusic } else { 2803297Smckusic putop( P2LISTOP , P2INT ); 2813297Smckusic } 2823297Smckusic # endif PC 2833297Smckusic argv = argv[2]; 284745Speter } 2853297Smckusic if (argv != NIL) { 2863297Smckusic error("Too many arguments to %s", p->symbol); 2873297Smckusic rvlist(argv); 2883297Smckusic return (NIL); 2893297Smckusic } 2903297Smckusic if (chk == FALSE) 2913297Smckusic return NIL; 292745Speter # ifdef OBJ 2931195Speter if ( p -> class == FFUNC || p -> class == FPROC ) { 2943359Smckusic put(2, PTR_RV | psbn << 8+INDX, (int)p->value[NL_OFFS]); 295*3426Speter put(2, O_LV | cbn << 8 + INDX , (int) savedisp ); 2963297Smckusic put(1, O_FCALL); 2973063Smckusic put(2, O_FRTN, even(width(p->type))); 2981195Speter } else { 2993063Smckusic put(2, O_CALL | psbn << 8, (long)p->entloc); 3001195Speter } 301745Speter # endif OBJ 302745Speter # ifdef PC 3033065Smckusic /* 304*3426Speter * for formal calls: add the hidden argument 305*3426Speter * which is the formal struct describing the 306*3426Speter * environment of the routine. 307*3426Speter * and the argument which is the address of the 308*3426Speter * space into which to save the display. 309*3426Speter */ 310*3426Speter if ( p -> class == FFUNC || p -> class == FPROC ) { 311*3426Speter putRV( 0 , cbn , p -> value[ NL_OFFS ] , P2PTR|P2STRTY ); 312*3426Speter if ( !noarguments ) { 313*3426Speter putop( P2LISTOP , P2INT ); 314*3426Speter } 315*3426Speter noarguments = FALSE; 316*3426Speter putLV( 0 , cbn , savedisp , P2PTR | P2STRTY ); 317*3426Speter putop( P2LISTOP , P2INT ); 318*3426Speter } 319*3426Speter /* 3203065Smckusic * do the actual call: 3213065Smckusic * either ... p( ... ) ... 322*3426Speter * or ... ( p -> entryaddr )( ... ) ... 3233065Smckusic * and maybe an assignment. 3243065Smckusic */ 325745Speter if ( porf == FUNC ) { 3263065Smckusic switch ( p_type_class ) { 327745Speter case TBOOL: 328745Speter case TCHAR: 329745Speter case TINT: 330745Speter case TSCAL: 331745Speter case TDOUBLE: 332745Speter case TPTR: 3333065Smckusic putop( ( noarguments ? P2UNARY P2CALL : P2CALL ) , 3343065Smckusic p_type_p2type ); 3353065Smckusic if ( p -> class == FFUNC ) { 3363065Smckusic putop( P2ASSIGN , p_type_p2type ); 337745Speter } 338745Speter break; 339745Speter default: 3403065Smckusic putstrop( ( noarguments ? P2UNARY P2STCALL : P2STCALL ), 3413065Smckusic ADDTYPE( p_type_p2type , P2PTR ) , 3423065Smckusic p_type_width , p_type_align ); 3433065Smckusic putstrop( P2STASG , p_type_p2type , lwidth( p -> type ) 344745Speter , align( p -> type ) ); 345745Speter break; 346745Speter } 347745Speter } else { 3483065Smckusic putop( ( noarguments ? P2UNARY P2CALL : P2CALL ) , P2INT ); 3493065Smckusic } 3503065Smckusic /* 3513065Smckusic * ... , FRTN( p ) ... 3523065Smckusic */ 3533065Smckusic if ( p -> class == FFUNC || p -> class == FPROC ) { 3543065Smckusic putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) , 3553065Smckusic "_FRTN" ); 3563359Smckusic putRV( 0 , psbn , p -> value[ NL_OFFS ] , P2PTR | P2STRTY ); 357*3426Speter putLV( 0 , cbn , savedisp , P2PTR | P2STRTY ); 358*3426Speter putop( P2LISTOP , P2INT ); 3593065Smckusic putop( P2CALL , P2INT ); 3603065Smckusic putop( P2COMOP , P2INT ); 3613065Smckusic } 3623065Smckusic /* 3633065Smckusic * if required: 3643065Smckusic * either ... , temp ) 3653065Smckusic * or ... , &temp ) 3663065Smckusic */ 3673065Smckusic if ( porf == FUNC && temptype != P2UNDEF ) { 3683065Smckusic if ( temptype != P2STRTY ) { 3693065Smckusic putRV( 0 , cbn , tempoffset , p_type_p2type ); 370745Speter } else { 3713065Smckusic putLV( 0 , cbn , tempoffset , p_type_p2type ); 372745Speter } 3733065Smckusic putop( P2COMOP , P2INT ); 3743065Smckusic } 3753065Smckusic if ( porf == PROC ) { 376745Speter putdot( filename , line ); 377745Speter } 378745Speter # endif PC 379745Speter return (p->type); 380745Speter } 381745Speter 382745Speter rvlist(al) 383745Speter register int *al; 384745Speter { 385745Speter 386745Speter for (; al != NIL; al = al[2]) 387745Speter rvalue( (int *) al[1], NLNIL , RREQ ); 388745Speter } 3893297Smckusic 3903297Smckusic /* 3913297Smckusic * check that two function/procedure namelist entries are compatible 3923297Smckusic */ 3933297Smckusic bool 3943297Smckusic fcompat( formal , actual ) 3953297Smckusic struct nl *formal; 3963297Smckusic struct nl *actual; 3973297Smckusic { 3983297Smckusic register struct nl *f_chain; 3993297Smckusic register struct nl *a_chain; 4003297Smckusic bool compat = TRUE; 4013297Smckusic 4023297Smckusic if ( formal == NIL || actual == NIL ) { 4033297Smckusic return FALSE; 4043297Smckusic } 4053297Smckusic for (a_chain = plist(actual), f_chain = plist(formal); 4063297Smckusic f_chain != NIL; 4073297Smckusic f_chain = f_chain->chain, a_chain = a_chain->chain) { 4083297Smckusic if (a_chain == NIL) { 4093297Smckusic error("%s %s declared on line %d has more arguments than", 4103297Smckusic parnam(formal->class), formal->symbol, 4113297Smckusic linenum(formal)); 4123297Smckusic cerror("%s %s declared on line %d", 4133297Smckusic parnam(actual->class), actual->symbol, 4143297Smckusic linenum(actual)); 4153297Smckusic return FALSE; 4163297Smckusic } 4173297Smckusic if ( a_chain -> class != f_chain -> class ) { 4183297Smckusic error("%s parameter %s of %s declared on line %d is not identical", 4193297Smckusic parnam(f_chain->class), f_chain->symbol, 4203297Smckusic formal->symbol, linenum(formal)); 4213297Smckusic cerror("with %s parameter %s of %s declared on line %d", 4223297Smckusic parnam(a_chain->class), a_chain->symbol, 4233297Smckusic actual->symbol, linenum(actual)); 4243297Smckusic compat = FALSE; 4253297Smckusic } else if (a_chain->class == FFUNC || a_chain->class == FPROC) { 4263297Smckusic compat = (compat && fcompat(f_chain, a_chain)); 4273297Smckusic } 4283297Smckusic if ((a_chain->class != FPROC && f_chain->class != FPROC) && 4293297Smckusic (a_chain->type != f_chain->type)) { 4303297Smckusic error("Type of %s parameter %s of %s declared on line %d is not identical", 4313297Smckusic parnam(f_chain->class), f_chain->symbol, 4323297Smckusic formal->symbol, linenum(formal)); 4333297Smckusic cerror("to type of %s parameter %s of %s declared on line %d", 4343297Smckusic parnam(a_chain->class), a_chain->symbol, 4353297Smckusic actual->symbol, linenum(actual)); 4363297Smckusic compat = FALSE; 4373297Smckusic } 4383297Smckusic } 4393297Smckusic if (a_chain != NIL) { 4403297Smckusic error("%s %s declared on line %d has fewer 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 return compat; 4493297Smckusic } 4503297Smckusic 4513297Smckusic char * 4523297Smckusic parnam(nltype) 4533297Smckusic int nltype; 4543297Smckusic { 4553297Smckusic switch(nltype) { 4563297Smckusic case REF: 4573297Smckusic return "var"; 4583297Smckusic case VAR: 4593297Smckusic return "value"; 4603297Smckusic case FUNC: 4613297Smckusic case FFUNC: 4623297Smckusic return "function"; 4633297Smckusic case PROC: 4643297Smckusic case FPROC: 4653297Smckusic return "procedure"; 4663297Smckusic default: 4673297Smckusic return "SNARK"; 4683297Smckusic } 4693297Smckusic } 4703297Smckusic 4713297Smckusic plist(p) 4723297Smckusic struct nl *p; 4733297Smckusic { 4743297Smckusic switch (p->class) { 4753297Smckusic case FFUNC: 4763297Smckusic case FPROC: 4773297Smckusic return p->ptr[ NL_FCHAIN ]; 4783297Smckusic case PROC: 4793297Smckusic case FUNC: 4803297Smckusic return p->chain; 4813297Smckusic default: 4823297Smckusic panic("plist"); 4833297Smckusic } 4843297Smckusic } 4853297Smckusic 4863297Smckusic linenum(p) 4873297Smckusic struct nl *p; 4883297Smckusic { 4893297Smckusic if (p->class == FUNC) 4903297Smckusic return p->ptr[NL_FVAR]->value[NL_LINENO]; 4913297Smckusic return p->value[NL_LINENO]; 4923297Smckusic } 493