13192Smckusick /* Copyright (c) 1979 Regents of the University of California */ 23192Smckusick 3*10714Speter static char sccsid[] = "@(#)fend.c 1.19 02/03/83"; 43192Smckusick 53192Smckusick #include "whoami.h" 63192Smckusick #include "0.h" 73192Smckusick #include "tree.h" 83192Smckusick #include "opcode.h" 93192Smckusick #include "objfmt.h" 103192Smckusick #include "align.h" 113192Smckusick 123192Smckusick /* 133192Smckusick * this array keeps the pxp counters associated with 143192Smckusick * functions and procedures, so that they can be output 153192Smckusick * when their bodies are encountered 163192Smckusick */ 173192Smckusick int bodycnts[ DSPLYSZ ]; 183192Smckusick 193192Smckusick #ifdef PC 203192Smckusick # include "pc.h" 213192Smckusick # include "pcops.h" 223192Smckusick #endif PC 233192Smckusick 243192Smckusick #ifdef OBJ 253192Smckusick int cntpatch; 263192Smckusick int nfppatch; 273192Smckusick #endif OBJ 283192Smckusick 293192Smckusick struct nl *Fp; 303192Smckusick int pnumcnt; 313192Smckusick /* 323192Smckusick * Funcend is called to 333192Smckusick * finish a block by generating 343192Smckusick * the code for the statements. 353192Smckusick * It then looks for unresolved declarations 363192Smckusick * of labels, procedures and functions, 373192Smckusick * and cleans up the name list. 383192Smckusick * For the program, it checks the 393192Smckusick * semantics of the program 403192Smckusick * statement (yuchh). 413192Smckusick */ 423192Smckusick funcend(fp, bundle, endline) 433192Smckusick struct nl *fp; 443192Smckusick int *bundle; 453192Smckusick int endline; 463192Smckusick { 473192Smckusick register struct nl *p; 483192Smckusick register int i, b; 493192Smckusick int var, inp, out, *blk; 503192Smckusick bool chkref; 513192Smckusick struct nl *iop; 523192Smckusick char *cp; 533192Smckusick extern int cntstat; 543192Smckusick # ifdef PC 5510671Speter struct entry_exit_cookie eecookie; 563192Smckusick # endif PC 573192Smckusick 583192Smckusick cntstat = 0; 593192Smckusick /* 603192Smckusick * yyoutline(); 613192Smckusick */ 623192Smckusick if (program != NIL) 633192Smckusick line = program->value[3]; 643192Smckusick blk = bundle[2]; 653192Smckusick if (fp == NIL) { 663192Smckusick cbn--; 673192Smckusick # ifdef PTREE 683192Smckusick nesting--; 693192Smckusick # endif PTREE 703192Smckusick return; 713192Smckusick } 723192Smckusick #ifdef OBJ 733192Smckusick /* 743192Smckusick * Patch the branch to the 753192Smckusick * entry point of the function 763192Smckusick */ 777917Smckusick patch4(fp->value[NL_ENTLOC]); 783192Smckusick /* 793192Smckusick * Put out the block entrance code and the block name. 803192Smckusick * HDRSZE is the number of bytes of info in the static 813192Smckusick * BEG data area exclusive of the proc name. It is 823192Smckusick * currently defined as: 833192Smckusick /* struct hdr { 843192Smckusick /* long framesze; /* number of bytes of local vars */ 853192Smckusick /* long nargs; /* number of bytes of arguments */ 863192Smckusick /* bool tests; /* TRUE => perform runtime tests */ 873192Smckusick /* short offset; /* offset of procedure in source file */ 883192Smckusick /* char name[1]; /* name of active procedure */ 893192Smckusick /* }; 903192Smckusick */ 913192Smckusick # define HDRSZE (2 * sizeof(long) + sizeof(short) + sizeof(bool)) 923192Smckusick var = put(2, ((lenstr(fp->symbol,0) + HDRSZE) << 8) 933192Smckusick | (cbn == 1 && opt('p') == 0 ? O_NODUMP: O_BEG), (long)0); 943192Smckusick /* 953192Smckusick * output the number of bytes of arguments 963192Smckusick * this is only checked on formal calls. 973192Smckusick */ 983192Smckusick put(2, O_CASE4, cbn == 1 ? (long)0 : (long)(fp->value[NL_OFFS]-DPOFF2)); 993192Smckusick /* 1003192Smckusick * Output the runtime test mode for the routine 1013192Smckusick */ 1023192Smckusick put(2, sizeof(bool) == 2 ? O_CASE2 : O_CASE4, opt('t') ? TRUE : FALSE); 1033192Smckusick /* 1043192Smckusick * Output line number and routine name 1053192Smckusick */ 1063192Smckusick put(2, O_CASE2, bundle[1]); 1073192Smckusick putstr(fp->symbol, 0); 1083192Smckusick #endif OBJ 1093192Smckusick #ifdef PC 1103192Smckusick /* 1113192Smckusick * put out the procedure entry code 1123192Smckusick */ 11310671Speter eecookie.nlp = fp; 1143192Smckusick if ( fp -> class == PROG ) { 1159129Smckusick /* 1169129Smckusick * If there is a label declaration in the main routine 1179129Smckusick * then there may be a non-local goto to it that does 1189129Smckusick * not appear in this module. We have to assume that 1199129Smckusick * such a reference may occur and generate code to 1209129Smckusick * prepare for it. 1219129Smckusick */ 1229129Smckusick if ( parts[ cbn ] & LPRT ) { 1239129Smckusick parts[ cbn ] |= ( NONLOCALVAR | NONLOCALGOTO ); 1249129Smckusick } 12510671Speter codeformain(); 1267917Smckusick ftnno = fp -> value[NL_ENTLOC]; 12710671Speter prog_prologue(&eecookie); 1283192Smckusick stabfunc( "program" , fp -> class , bundle[1] , 0 ); 1293192Smckusick } else { 1307917Smckusick ftnno = fp -> value[NL_ENTLOC]; 13110671Speter fp_prologue(&eecookie); 1323192Smckusick stabfunc( fp -> symbol , fp -> class , bundle[1] , cbn - 1 ); 1333192Smckusick for ( p = fp -> chain ; p != NIL ; p = p -> chain ) { 1343192Smckusick stabparam( p -> symbol , p2type( p -> type ) 1353192Smckusick , p -> value[ NL_OFFS ] , lwidth( p -> type ) ); 1363192Smckusick } 1373192Smckusick if ( fp -> class == FUNC ) { 1383192Smckusick /* 1393192Smckusick * stab the function variable 1403192Smckusick */ 1413192Smckusick p = fp -> ptr[ NL_FVAR ]; 1423192Smckusick stablvar( p -> symbol , p2type( p -> type ) , cbn 1433192Smckusick , p -> value[ NL_OFFS ] , lwidth( p -> type ) ); 1443192Smckusick } 1453192Smckusick /* 1463192Smckusick * stab local variables 1473192Smckusick * rummage down hash chain links. 1483192Smckusick */ 1493192Smckusick for ( i = 0 ; i <= 077 ; i++ ) { 1503192Smckusick for ( p = disptab[ i ] ; p != NIL ; p = p->nl_next) { 1513192Smckusick if ( ( p -> nl_block & 037 ) != cbn ) { 1523192Smckusick break; 1533192Smckusick } 1543192Smckusick /* 1553192Smckusick * stab local variables 1563192Smckusick * that's named variables, but not params 1573192Smckusick */ 1583192Smckusick if ( ( p -> symbol != NIL ) 1593192Smckusick && ( p -> class == VAR ) 1603192Smckusick && ( p -> value[ NL_OFFS ] < 0 ) ) { 1613192Smckusick stablvar( p -> symbol , p2type( p -> type ) , cbn 1623192Smckusick , p -> value[ NL_OFFS ] , lwidth( p -> type ) ); 1633192Smckusick } 1643192Smckusick } 1653192Smckusick } 1663192Smckusick } 1673192Smckusick stablbrac( cbn ); 1683192Smckusick /* 1693192Smckusick * ask second pass to allocate known locals 1703192Smckusick */ 1713192Smckusick putlbracket( ftnno , -sizes[ cbn ].om_max ); 17210671Speter fp_entrycode(&eecookie); 1733192Smckusick #endif PC 1743192Smckusick if ( monflg ) { 1753192Smckusick if ( fp -> value[ NL_CNTR ] != 0 ) { 1763192Smckusick inccnt( fp -> value [ NL_CNTR ] ); 1773192Smckusick } 1783192Smckusick inccnt( bodycnts[ fp -> nl_block & 037 ] ); 1793192Smckusick } 1803192Smckusick if (fp->class == PROG) { 1813192Smckusick /* 1823192Smckusick * The glorious buffers option. 1833192Smckusick * 0 = don't buffer output 1843192Smckusick * 1 = line buffer output 1853192Smckusick * 2 = 512 byte buffer output 1863192Smckusick */ 1873192Smckusick # ifdef OBJ 1883192Smckusick if (opt('b') != 1) 1893192Smckusick put(1, O_BUFF | opt('b') << 8); 1903192Smckusick # endif OBJ 1913192Smckusick # ifdef PC 1923192Smckusick if ( opt( 'b' ) != 1 ) { 1933192Smckusick putleaf( P2ICON , 0 , 0 1943192Smckusick , ADDTYPE( P2FTN | P2INT , P2PTR ) , "_BUFF" ); 1953192Smckusick putleaf( P2ICON , opt( 'b' ) , 0 , P2INT , 0 ); 1963192Smckusick putop( P2CALL , P2INT ); 1973192Smckusick putdot( filename , line ); 1983192Smckusick } 1993192Smckusick # endif PC 2007953Speter inp = 0; 2013192Smckusick out = 0; 2023192Smckusick for (p = fp->chain; p != NIL; p = p->chain) { 2037953Speter if (strcmp(p->symbol, input->symbol) == 0) { 2043192Smckusick inp++; 2053192Smckusick continue; 2063192Smckusick } 2077953Speter if (strcmp(p->symbol, output->symbol) == 0) { 2083192Smckusick out++; 2093192Smckusick continue; 2103192Smckusick } 2113192Smckusick iop = lookup1(p->symbol); 2123192Smckusick if (iop == NIL || bn != cbn) { 2133192Smckusick error("File %s listed in program statement but not declared", p->symbol); 2143192Smckusick continue; 2153192Smckusick } 2163192Smckusick if (iop->class != VAR) { 2173192Smckusick error("File %s listed in program statement but declared as a %s", p->symbol, classes[iop->class]); 2183192Smckusick continue; 2193192Smckusick } 2203192Smckusick if (iop->type == NIL) 2213192Smckusick continue; 2223192Smckusick if (iop->type->class != FILET) { 2233192Smckusick error("File %s listed in program statement but defined as %s", 2243192Smckusick p->symbol, nameof(iop->type)); 2253192Smckusick continue; 2263192Smckusick } 2273192Smckusick # ifdef OBJ 2283192Smckusick put(2, O_CON24, text(iop->type) ? 0 : width(iop->type->type)); 2293192Smckusick i = lenstr(p->symbol,0); 2303192Smckusick put(2, O_CON24, i); 2313192Smckusick put(2, O_LVCON, i); 2323192Smckusick putstr(p->symbol, 0); 2333192Smckusick put(2, O_LV | bn<<8+INDX, (int)iop->value[NL_OFFS]); 2343192Smckusick put(1, O_DEFNAME); 2353192Smckusick # endif OBJ 2363192Smckusick # ifdef PC 2373192Smckusick putleaf( P2ICON , 0 , 0 2383192Smckusick , ADDTYPE( P2FTN | P2INT , P2PTR ) 2393192Smckusick , "_DEFNAME" ); 2403837Speter putLV( p -> symbol , bn , iop -> value[NL_OFFS] , 2413837Speter iop -> extra_flags , p2type( iop ) ); 2423192Smckusick putCONG( p -> symbol , strlen( p -> symbol ) 2433192Smckusick , LREQ ); 2443192Smckusick putop( P2LISTOP , P2INT ); 2453192Smckusick putleaf( P2ICON , strlen( p -> symbol ) 2463192Smckusick , 0 , P2INT , 0 ); 2473192Smckusick putop( P2LISTOP , P2INT ); 2483192Smckusick putleaf( P2ICON 2493192Smckusick , text(iop->type) ? 0 : width(iop->type->type) 2503192Smckusick , 0 , P2INT , 0 ); 2513192Smckusick putop( P2LISTOP , P2INT ); 2523192Smckusick putop( P2CALL , P2INT ); 2533192Smckusick putdot( filename , line ); 2543192Smckusick # endif PC 2553192Smckusick } 2563192Smckusick } 2573192Smckusick /* 2583192Smckusick * Process the prog/proc/func body 2593192Smckusick */ 2603192Smckusick noreach = 0; 2613192Smckusick line = bundle[1]; 2623192Smckusick statlist(blk); 2633192Smckusick # ifdef PTREE 2643192Smckusick { 2653192Smckusick pPointer Body = tCopy( blk ); 2663192Smckusick 2673192Smckusick pDEF( PorFHeader[ nesting -- ] ).PorFBody = Body; 2683192Smckusick } 2693192Smckusick # endif PTREE 2703192Smckusick # ifdef OBJ 2713192Smckusick if (cbn== 1 && monflg != 0) { 2723192Smckusick patchfil(cntpatch - 2, (long)cnts, 2); 2733192Smckusick patchfil(nfppatch - 2, (long)pfcnt, 2); 2743192Smckusick } 2753192Smckusick # endif OBJ 2763192Smckusick # ifdef PC 2773192Smckusick if ( fp -> class == PROG && monflg ) { 2783192Smckusick putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 2793192Smckusick , "_PMFLUSH" ); 2803192Smckusick putleaf( P2ICON , cnts , 0 , P2INT , 0 ); 2813192Smckusick putleaf( P2ICON , pfcnt , 0 , P2INT , 0 ); 2823192Smckusick putop( P2LISTOP , P2INT ); 2833837Speter putLV( PCPCOUNT , 0 , 0 , NGLOBAL , P2INT ); 2843192Smckusick putop( P2LISTOP , P2INT ); 2853192Smckusick putop( P2CALL , P2INT ); 2863192Smckusick putdot( filename , line ); 2873192Smckusick } 2883192Smckusick # endif PC 2897953Speter /* 2907953Speter * Clean up the symbol table displays and check for unresolves 2917953Speter */ 2927953Speter line = endline; 2933192Smckusick if (fp->class == PROG && inp == 0 && (input->nl_flags & (NUSED|NMOD)) != 0) { 2943192Smckusick recovered(); 2953192Smckusick error("Input is used but not defined in the program statement"); 2963192Smckusick } 2977953Speter if (fp->class == PROG && out == 0 && (output->nl_flags & (NUSED|NMOD)) != 0) { 2987953Speter recovered(); 2997953Speter error("Output is used but not defined in the program statement"); 3007953Speter } 3013192Smckusick b = cbn; 3023192Smckusick Fp = fp; 3033192Smckusick chkref = syneflg == errcnt[cbn] && opt('w') == 0; 3043192Smckusick for (i = 0; i <= 077; i++) { 3053192Smckusick for (p = disptab[i]; p != NIL && (p->nl_block & 037) == b; p = p->nl_next) { 3063192Smckusick /* 3073192Smckusick * Check for variables defined 3083192Smckusick * but not referenced 3093192Smckusick */ 3103192Smckusick if (chkref && p->symbol != NIL) 3113192Smckusick switch (p->class) { 3123192Smckusick case FIELD: 3133192Smckusick /* 3143192Smckusick * If the corresponding record is 3153192Smckusick * unused, we shouldn't complain about 3163192Smckusick * the fields. 3173192Smckusick */ 3183192Smckusick default: 3193192Smckusick if ((p->nl_flags & (NUSED|NMOD)) == 0) { 3203192Smckusick warning(); 3213192Smckusick nerror("%s %s is neither used nor set", classes[p->class], p->symbol); 3223192Smckusick break; 3233192Smckusick } 3243192Smckusick /* 3253192Smckusick * If a var parameter is either 3263192Smckusick * modified or used that is enough. 3273192Smckusick */ 3283192Smckusick if (p->class == REF) 3293192Smckusick continue; 3303192Smckusick # ifdef OBJ 3313192Smckusick if ((p->nl_flags & NUSED) == 0) { 3323192Smckusick warning(); 3333192Smckusick nerror("%s %s is never used", classes[p->class], p->symbol); 3343192Smckusick break; 3353192Smckusick } 3363192Smckusick # endif OBJ 3373192Smckusick # ifdef PC 3383837Speter if (((p->nl_flags & NUSED) == 0) && ((p->extra_flags & NEXTERN) == 0)) { 3393192Smckusick warning(); 3403192Smckusick nerror("%s %s is never used", classes[p->class], p->symbol); 3413192Smckusick break; 3423192Smckusick } 3433192Smckusick # endif PC 3443192Smckusick if ((p->nl_flags & NMOD) == 0) { 3453192Smckusick warning(); 3463192Smckusick nerror("%s %s is used but never set", classes[p->class], p->symbol); 3473192Smckusick break; 3483192Smckusick } 3493192Smckusick case LABEL: 3503192Smckusick case FVAR: 3513192Smckusick case BADUSE: 3523192Smckusick break; 3533192Smckusick } 3543192Smckusick switch (p->class) { 3553192Smckusick case BADUSE: 3563192Smckusick cp = "s"; 3573192Smckusick if (p->chain->ud_next == NIL) 3583192Smckusick cp++; 3593192Smckusick eholdnl(); 3603192Smckusick if (p->value[NL_KINDS] & ISUNDEF) 3613192Smckusick nerror("%s undefined on line%s", p->symbol, cp); 3623192Smckusick else 3633192Smckusick nerror("%s improperly used on line%s", p->symbol, cp); 3643192Smckusick pnumcnt = 10; 3653192Smckusick pnums(p->chain); 3663192Smckusick pchr('\n'); 3673192Smckusick break; 3683192Smckusick 3693192Smckusick case FUNC: 3703192Smckusick case PROC: 3713192Smckusick # ifdef OBJ 3723192Smckusick if ((p->nl_flags & NFORWD)) 3733192Smckusick nerror("Unresolved forward declaration of %s %s", classes[p->class], p->symbol); 3743192Smckusick # endif OBJ 3753192Smckusick # ifdef PC 3763837Speter if ((p->nl_flags & NFORWD) && ((p->extra_flags & NEXTERN) == 0)) 3773192Smckusick nerror("Unresolved forward declaration of %s %s", classes[p->class], p->symbol); 3783192Smckusick # endif PC 3793192Smckusick break; 3803192Smckusick 3813192Smckusick case LABEL: 3823192Smckusick if (p->nl_flags & NFORWD) 3833192Smckusick nerror("label %s was declared but not defined", p->symbol); 3843192Smckusick break; 3853192Smckusick case FVAR: 3863192Smckusick if ((p->nl_flags & NMOD) == 0) 3873192Smckusick nerror("No assignment to the function variable"); 3883192Smckusick break; 3893192Smckusick } 3903192Smckusick } 3913192Smckusick /* 3923192Smckusick * Pop this symbol 3933192Smckusick * table slot 3943192Smckusick */ 3953192Smckusick disptab[i] = p; 3963192Smckusick } 3973192Smckusick 3983192Smckusick # ifdef OBJ 3993192Smckusick put(1, O_END); 4003192Smckusick # endif OBJ 4013192Smckusick # ifdef PC 40210671Speter fp_exitcode(&eecookie); 40310671Speter stabrbrac(cbn); 40410671Speter putrbracket(ftnno); 40510671Speter fp_epilogue(&eecookie); 40610671Speter if (fp -> class != PROG) { 40710671Speter fp_formalentry(&eecookie); 4083192Smckusick } 4093192Smckusick /* 4103192Smckusick * declare pcp counters, if any 4113192Smckusick */ 4123192Smckusick if ( monflg && fp -> class == PROG ) { 4133192Smckusick putprintf( " .data" , 0 ); 41410671Speter aligndot(P2INT); 4153192Smckusick putprintf( " .comm " , 1 ); 4163192Smckusick putprintf( PCPCOUNT , 1 ); 4173192Smckusick putprintf( ",%d" , 0 , ( cnts + 1 ) * sizeof (long) ); 4183192Smckusick putprintf( " .text" , 0 ); 4193192Smckusick } 4203192Smckusick # endif PC 4213192Smckusick #ifdef DEBUG 4223192Smckusick dumpnl(fp->ptr[2], fp->symbol); 4233192Smckusick #endif 4245654Slinton 4255654Slinton #ifdef OBJ 4263192Smckusick /* 4275654Slinton * save the namelist for the debugger pdx 4285654Slinton */ 4295654Slinton 4305654Slinton savenl(fp->ptr[2], fp->symbol); 4315654Slinton #endif 4325654Slinton 4335654Slinton /* 4343192Smckusick * Restore the 4353192Smckusick * (virtual) name list 4363192Smckusick * position 4373192Smckusick */ 4383192Smckusick nlfree(fp->ptr[2]); 4393192Smckusick /* 4403192Smckusick * Proc/func has been 4413192Smckusick * resolved 4423192Smckusick */ 4433192Smckusick fp->nl_flags &= ~NFORWD; 4443192Smckusick /* 4453192Smckusick * Patch the beg 4463192Smckusick * of the proc/func to 4473192Smckusick * the proper variable size 4483192Smckusick */ 4493192Smckusick if (Fp == NIL) 4503192Smckusick elineon(); 4513192Smckusick # ifdef OBJ 45210671Speter patchfil(var, leven(-sizes[cbn].om_max), 2); 4533192Smckusick # endif OBJ 4543192Smckusick cbn--; 4553192Smckusick if (inpflist(fp->symbol)) { 4563192Smckusick opop('l'); 4573192Smckusick } 4583192Smckusick } 4593363Speter 4603363Speter #ifdef PC 4613363Speter /* 4623363Speter * construct the long name of a function based on it's static nesting. 4633363Speter * into a caller-supplied buffer (that should be about BUFSIZ big). 4643363Speter */ 4653363Speter sextname( buffer , name , level ) 4663363Speter char buffer[]; 4673363Speter char *name; 4683363Speter int level; 4693363Speter { 4703363Speter char *starthere; 4713363Speter int i; 4723363Speter 4733363Speter starthere = &buffer[0]; 4743363Speter for ( i = 1 ; i < level ; i++ ) { 4753363Speter sprintf( starthere , EXTFORMAT , enclosing[ i ] ); 4763363Speter starthere += strlen( enclosing[ i ] ) + 1; 4773363Speter } 4783367Speter sprintf( starthere , EXTFORMAT , name ); 4793367Speter starthere += strlen( name ) + 1; 4803363Speter if ( starthere >= &buffer[ BUFSIZ ] ) { 4813363Speter panic( "sextname" ); 4823363Speter } 4833363Speter } 48410671Speter 48510671Speter /* 48610671Speter * code for main() 48710671Speter */ 48810671Speter #ifdef vax 48910671Speter 49010671Speter codeformain() 49110671Speter { 49210671Speter putprintf(" .text" , 0 ); 49310671Speter putprintf(" .align 1" , 0 ); 49410671Speter putprintf(" .globl _main" , 0 ); 49510671Speter putprintf("_main:" , 0 ); 49610671Speter putprintf(" .word 0" , 0 ); 49710671Speter if ( opt ( 't' ) ) { 49810671Speter putprintf(" pushl $1" , 0 ); 49910671Speter } else { 50010671Speter putprintf(" pushl $0" , 0 ); 50110671Speter } 50210671Speter putprintf(" calls $1,_PCSTART" , 0 ); 50310671Speter putprintf(" movl 4(ap),__argc" , 0 ); 50410671Speter putprintf(" movl 8(ap),__argv" , 0 ); 50510671Speter putprintf(" calls $0,_program" , 0 ); 50610671Speter putprintf(" pushl $0" , 0 ); 50710671Speter putprintf(" calls $1,_PCEXIT" , 0 ); 50810671Speter } 50910671Speter 51010671Speter /* 51110671Speter * prologue for the program. 51210671Speter * different because it 51310671Speter * doesn't have formal entry point 51410671Speter */ 51510671Speter prog_prologue(eecookiep) 51610671Speter struct entry_exit_cookie *eecookiep; 51710671Speter { 51810671Speter putprintf(" .text" , 0 ); 51910671Speter putprintf(" .align 1" , 0 ); 52010671Speter putprintf(" .globl _program" , 0 ); 52110671Speter putprintf("_program:" , 0 ); 52210671Speter } 52310671Speter 52410671Speter fp_prologue(eecookiep) 52510671Speter struct entry_exit_cookie *eecookiep; 52610671Speter { 52710671Speter int ftnno = eecookiep -> nlp -> value[NL_ENTLOC]; 52810671Speter 52910671Speter sextname( eecookiep -> extname, eecookiep -> nlp -> symbol , cbn - 1 ); 53010671Speter putprintf( " .text" , 0 ); 53110671Speter putprintf( " .align 1" , 0 ); 53210671Speter putprintf( " .globl %s%s", 0, FORMALPREFIX, eecookiep -> extname ); 53310671Speter putprintf( " .globl %s" , 0 , eecookiep -> extname ); 53410671Speter putprintf( "%s:" , 0 , eecookiep -> extname ); 53510671Speter /* 53610671Speter * register save mask 53710671Speter */ 53810671Speter eecookiep -> savlabel = getlab(); 53910671Speter putprintf(" .word %s%d", 0, SAVE_MASK_LABEL , eecookiep -> savlabel ); 54010671Speter } 54110671Speter 54210671Speter /* 54310671Speter * code before any user code. 54410671Speter * or code that is machine dependent. 54510671Speter */ 54610671Speter fp_entrycode(eecookiep) 54710671Speter struct entry_exit_cookie *eecookiep; 54810671Speter { 549*10714Speter int ftnno = eecookiep -> nlp -> value[NL_ENTLOC]; 55010671Speter int proflabel = getlab(); 55110671Speter int setjmp0 = getlab(); 55210671Speter 55310671Speter /* 55410671Speter * top of code; destination of jump from formal entry code. 55510671Speter */ 55610671Speter eecookiep -> toplabel = getlab(); 55710671Speter putlab( eecookiep -> toplabel ); 55810671Speter putprintf(" subl2 $%s%d,sp" , 0 , FRAME_SIZE_LABEL, ftnno ); 55910671Speter if ( profflag ) { 56010671Speter /* 56110671Speter * call mcount for profiling 56210671Speter */ 56310671Speter putprintf( " moval " , 1 ); 56410671Speter putprintf( PREFIXFORMAT , 1 , LABELPREFIX , proflabel ); 56510671Speter putprintf( ",r0" , 0 ); 56610671Speter putprintf( " jsb mcount" , 0 ); 56710671Speter putprintf( " .data" , 0 ); 56810671Speter putprintf( " .align 2" , 0 ); 56910671Speter putlab( proflabel ); 57010671Speter putprintf( " .long 0" , 0 ); 57110671Speter putprintf( " .text" , 0 ); 57210671Speter } 57310671Speter /* 57410671Speter * if there are nested procedures that access our variables 57510671Speter * we must save the display. 57610671Speter */ 57710671Speter if ( parts[ cbn ] & NONLOCALVAR ) { 57810671Speter /* 57910671Speter * save old display 58010671Speter */ 58110671Speter putprintf( " movq %s+%d,%d(%s)" , 0 58210671Speter , DISPLAYNAME , cbn * sizeof(struct dispsave) 58310671Speter , DSAVEOFFSET , P2FPNAME ); 58410671Speter /* 58510671Speter * set up new display by saving AP and FP in appropriate 58610671Speter * slot in display structure. 58710671Speter */ 58810671Speter putprintf( " movq %s,%s+%d" , 0 58910671Speter , P2APNAME , DISPLAYNAME , cbn * sizeof(struct dispsave) ); 59010671Speter } 59110671Speter /* 59210671Speter * set underflow checking if runtime tests 59310671Speter */ 59410671Speter if ( opt( 't' ) ) { 59510671Speter putprintf( " bispsw $0xe0" , 0 ); 59610671Speter } 59710671Speter /* 59810671Speter * zero local variables if checking is on 59910671Speter * by calling blkclr( bytes of locals , starting local address ); 60010671Speter */ 60110671Speter if ( opt( 't' ) && ( -sizes[ cbn ].om_max ) > DPOFF1 ) { 60210671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 60310671Speter , "_blkclr" ); 60410671Speter putLV( 0 , cbn , sizes[ cbn ].om_max , NLOCAL , P2CHAR ); 60510671Speter putleaf( P2ICON , ( -sizes[ cbn ].om_max ) - DPOFF1 60610671Speter , 0 , P2INT , 0 ); 60710671Speter putop( P2LISTOP , P2INT ); 60810671Speter putop( P2CALL , P2INT ); 60910671Speter putdot( filename , line ); 61010671Speter } 61110671Speter /* 61210671Speter * set up goto vector if non-local goto to this frame 61310671Speter */ 61410671Speter if ( parts[ cbn ] & NONLOCALGOTO ) { 61510671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 61610671Speter , "_setjmp" ); 61710671Speter putLV( 0 , cbn , GOTOENVOFFSET , NLOCAL , P2PTR|P2STRTY ); 61810671Speter putop( P2CALL , P2INT ); 61910671Speter putleaf( P2ICON , 0 , 0 , P2INT , 0 ); 62010671Speter putop( P2NE , P2INT ); 62110671Speter putleaf( P2ICON , setjmp0 , 0 , P2INT , 0 ); 62210671Speter putop( P2CBRANCH , P2INT ); 62310671Speter putdot( filename , line ); 62410671Speter /* 62510671Speter * on non-local goto, setjmp returns with address to 62610671Speter * be branched to. 62710671Speter */ 62810671Speter putprintf( " jmp (r0)" , 0 ); 62910671Speter putlab(setjmp0); 63010671Speter } 63110671Speter } 63210671Speter 63310671Speter fp_exitcode(eecookiep) 63410671Speter struct entry_exit_cookie *eecookiep; 63510671Speter { 63610671Speter /* 63710671Speter * if there were file variables declared at this level 63810671Speter * call PCLOSE( ap ) to clean them up. 63910671Speter */ 64010671Speter if ( dfiles[ cbn ] ) { 64110671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 64210671Speter , "_PCLOSE" ); 64310671Speter putleaf( P2REG , 0 , P2AP , ADDTYPE( P2CHAR , P2PTR ) , 0 ); 64410671Speter putop( P2CALL , P2INT ); 64510671Speter putdot( filename , line ); 64610671Speter } 64710671Speter /* 64810671Speter * if this is a function, 64910671Speter * the function variable is the return value. 65010671Speter * if it's a scalar valued function, return scalar, 65110671Speter * else, return a pointer to the structure value. 65210671Speter */ 65310671Speter if ( eecookiep-> nlp -> class == FUNC ) { 65410671Speter struct nl *fvar = eecookiep-> nlp -> ptr[ NL_FVAR ]; 65510671Speter long fvartype = p2type( fvar -> type ); 65610671Speter long label; 65710671Speter char labelname[ BUFSIZ ]; 65810671Speter 65910671Speter switch ( classify( fvar -> type ) ) { 66010671Speter case TBOOL: 66110671Speter case TCHAR: 66210671Speter case TINT: 66310671Speter case TSCAL: 66410671Speter case TDOUBLE: 66510671Speter case TPTR: 66610671Speter putRV( fvar -> symbol , ( fvar -> nl_block ) & 037 , 66710671Speter fvar -> value[ NL_OFFS ] , 66810671Speter fvar -> extra_flags , 66910671Speter fvartype ); 67010671Speter break; 67110671Speter default: 67210671Speter label = getlab(); 67310671Speter sprintf( labelname , PREFIXFORMAT , LABELPREFIX , label ); 67410671Speter putprintf( " .data" , 0 ); 67510671Speter aligndot(A_STRUCT); 67610671Speter putprintf( " .lcomm %s,%d" , 0 , 67710671Speter labelname , lwidth( fvar -> type ) ); 67810671Speter putprintf( " .text" , 0 ); 67910671Speter putleaf( P2NAME , 0 , 0 , fvartype , labelname ); 68010671Speter putLV( fvar -> symbol , ( fvar -> nl_block ) & 037 , 68110671Speter fvar -> value[ NL_OFFS ] , 68210671Speter fvar -> extra_flags , 68310671Speter fvartype ); 68410671Speter putstrop( P2STASG , fvartype , lwidth( fvar -> type ) , 68510671Speter align( fvar -> type ) ); 68610671Speter putdot( filename , line ); 68710671Speter putleaf( P2ICON , 0 , 0 , fvartype , labelname ); 68810671Speter break; 68910671Speter } 69010671Speter putop( P2FORCE , fvartype ); 69110671Speter putdot( filename , line ); 69210671Speter } 69310671Speter /* 69410671Speter * if there are nested procedures we must save the display. 69510671Speter */ 69610671Speter if ( parts[ cbn ] & NONLOCALVAR ) { 69710671Speter /* 69810671Speter * restore old display entry from save area 69910671Speter */ 70010671Speter putprintf( " movq %d(%s),%s+%d" , 0 70110671Speter , DSAVEOFFSET , P2FPNAME 70210671Speter , DISPLAYNAME , cbn * sizeof(struct dispsave) ); 70310671Speter } 70410671Speter } 70510671Speter 70610671Speter fp_epilogue(eecookiep) 70710671Speter struct entry_exit_cookie *eecookiep; 70810671Speter { 70910671Speter putprintf(" ret" , 0 ); 71010671Speter /* 71110671Speter * set the register save mask. 71210671Speter */ 71310671Speter putprintf(" .set %s%d,0x%x", 0, 71410671Speter SAVE_MASK_LABEL, eecookiep -> savlabel, savmask()); 71510671Speter } 71610671Speter 71710671Speter fp_formalentry(eecookiep) 71810671Speter struct entry_exit_cookie *eecookiep; 71910671Speter { 72010671Speter 72110671Speter putprintf("%s%s:" , 0 , FORMALPREFIX , eecookiep -> extname ); 72210671Speter putprintf(" .word %s%d", 0, SAVE_MASK_LABEL, eecookiep -> savlabel ); 72310671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) , "_FCALL" ); 72410671Speter putRV( 0 , cbn , 72510671Speter eecookiep -> nlp -> value[ NL_OFFS ] + sizeof( struct formalrtn * ) , 72610671Speter NPARAM , P2PTR | P2STRTY ); 72710671Speter putRV(0, cbn, eecookiep -> nlp -> value[NL_OFFS], NPARAM, P2PTR|P2STRTY); 72810671Speter putop( P2LISTOP , P2INT ); 72910671Speter putop( P2CALL , P2INT ); 73010671Speter putdot( filename , line ); 73110671Speter putjbr( eecookiep -> toplabel ); 73210671Speter } 73310671Speter #endif vax 73410671Speter 73510671Speter #ifdef mc68000 73610671Speter 73710671Speter codeformain() 73810671Speter { 73910671Speter putprintf(" .text", 0); 74010671Speter putprintf(" .globl _main", 0); 74110671Speter putprintf("_main:", 0); 74210671Speter putprintf(" link %s,#0", 0, P2FPNAME); 74310671Speter if (opt('t')) { 74410671Speter putprintf(" pea 1", 0); 74510671Speter } else { 74610671Speter putprintf(" pea 0", 0); 74710671Speter } 74810671Speter putprintf(" jbsr _PCSTART", 0); 74910671Speter putprintf(" addql #4,sp", 0); 75010671Speter putprintf(" movl %s@(8),__argc", 0, P2FPNAME); 75110671Speter putprintf(" movl %s@(12),__argv", 0, P2FPNAME); 75210671Speter putprintf(" jbsr _program", 0); 75310671Speter putprintf(" pea 0", 0); 75410671Speter putprintf(" jbsr _PCEXIT", 0); 75510671Speter } 75610671Speter 75710671Speter prog_prologue(eecookiep) 75810671Speter struct entry_exit_cookie *eecookiep; 75910671Speter { 76010671Speter int ftnno = eecookiep -> nlp -> value[NL_ENTLOC]; 76110671Speter 76210671Speter putprintf(" .text", 0); 76310671Speter putprintf(" .globl _program", 0); 76410671Speter putprintf("_program:", 0); 76510671Speter putprintf(" link %s,#0", 0, P2FPNAME); 76610671Speter putprintf(" addl #-%s%d,sp", 0, FRAME_SIZE_LABEL, ftnno); 76710671Speter /* touch new end of stack, to break more stack space */ 76810671Speter putprintf(" tstb sp@(-%s%d)", 0, PAGE_BREAK_LABEL, ftnno); 76910671Speter putprintf(" moveml #%s%d,sp@", 0, SAVE_MASK_LABEL, ftnno); 77010671Speter } 77110671Speter 77210671Speter fp_prologue(eecookiep) 77310671Speter struct entry_exit_cookie *eecookiep; 77410671Speter { 77510671Speter int ftnno = eecookiep -> nlp -> value[NL_ENTLOC]; 77610671Speter 77710671Speter sextname(eecookiep -> extname, eecookiep -> nlp -> symbol, cbn - 1); 77810671Speter putprintf(" .text", 0); 77910671Speter putprintf(" .globl %s%s", 0, FORMALPREFIX, eecookiep -> extname); 78010671Speter putprintf(" .globl %s", 0, eecookiep -> extname); 78110671Speter putprintf("%s:", 0, eecookiep -> extname); 78210671Speter putprintf(" link %s,#0", 0, P2FPNAME); 78310671Speter putprintf(" addl #-%s%d,sp", 0, FRAME_SIZE_LABEL, ftnno); 78410671Speter /* touch new end of stack, to break more stack space */ 78510671Speter putprintf(" tstb sp@(-%s%d)", 0, PAGE_BREAK_LABEL, ftnno); 78610671Speter putprintf(" moveml #%s%d,sp@", 0, SAVE_MASK_LABEL, ftnno); 78710671Speter } 78810671Speter 78910671Speter fp_entrycode(eecookiep) 79010671Speter struct entry_exit_cookie *eecookiep; 79110671Speter { 79210671Speter int proflabel = getlab(); 79310671Speter int setjmp0 = getlab(); 79410671Speter 79510671Speter /* 79610671Speter * fill in the label cookie 79710671Speter */ 79810671Speter eecookiep -> toplabel = getlab(); 79910671Speter putlab(eecookiep -> toplabel); 80010671Speter /* 80110671Speter * call mcount if we are profiling. 80210671Speter */ 80310671Speter if ( profflag ) { 80410671Speter putprintf(" movl #%s%d,a0", 0, LABELPREFIX, proflabel); 80510671Speter putprintf(" jsr mcount", 0); 80610671Speter putprintf(" .data", 0); 80710671Speter putprintf(" .even", 0); 80810671Speter putlab(proflabel); 80910671Speter putprintf(" .long 0", 0); 81010671Speter putprintf(" .text", 0); 81110671Speter } 81210671Speter /* 81310671Speter * if there are nested procedures that access our variables 81410671Speter * we must save the display 81510671Speter */ 81610671Speter if (parts[cbn] & NONLOCALVAR) { 81710671Speter /* 81810671Speter * save the old display 81910671Speter */ 82010671Speter putprintf(" movl %s+%d,%s@(%d)", 0, 82110671Speter DISPLAYNAME, cbn * sizeof(struct dispsave), 82210671Speter P2FPNAME, DSAVEOFFSET); 82310671Speter /* 82410671Speter * set up the new display by saving the framepointer 82510671Speter * in the display structure. 82610671Speter */ 82710671Speter putprintf(" movl %s,%s+%d", 0, 82810671Speter P2FPNAME, DISPLAYNAME, cbn * sizeof(struct dispsave)); 82910671Speter } 83010671Speter /* 83110671Speter * zero local variables if checking is on 83210671Speter * by calling blkclr( bytes of locals , starting local address ); 83310671Speter */ 83410671Speter if ( opt( 't' ) && ( -sizes[ cbn ].om_max ) > DPOFF1 ) { 83510671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 83610671Speter , "_blkclr" ); 83710671Speter putLV( 0 , cbn , sizes[ cbn ].om_max , NLOCAL , P2CHAR ); 83810671Speter putleaf( P2ICON , ( -sizes[ cbn ].om_max ) - DPOFF1 83910671Speter , 0 , P2INT , 0 ); 84010671Speter putop( P2LISTOP , P2INT ); 84110671Speter putop( P2CALL , P2INT ); 84210671Speter putdot( filename , line ); 84310671Speter } 84410671Speter /* 84510671Speter * set up goto vector if non-local goto to this frame 84610671Speter */ 84710671Speter if ( parts[ cbn ] & NONLOCALGOTO ) { 84810671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 84910671Speter , "_setjmp" ); 85010671Speter putLV( 0 , cbn , GOTOENVOFFSET , NLOCAL , P2PTR|P2STRTY ); 85110671Speter putop( P2CALL , P2INT ); 85210671Speter putleaf( P2ICON , 0 , 0 , P2INT , 0 ); 85310671Speter putop( P2NE , P2INT ); 85410671Speter putleaf( P2ICON , setjmp0 , 0 , P2INT , 0 ); 85510671Speter putop( P2CBRANCH , P2INT ); 85610671Speter putdot( filename , line ); 85710671Speter /* 85810671Speter * on non-local goto, setjmp returns with address to 85910671Speter * be branched to. 86010671Speter */ 86110671Speter putprintf(" movl d0,a0", 0); 86210671Speter putprintf(" jmp a0@", 0); 86310671Speter putlab(setjmp0); 86410671Speter } 86510671Speter } 86610671Speter 86710671Speter fp_exitcode(eecookiep) 86810671Speter struct entry_exit_cookie *eecookiep; 86910671Speter { 87010671Speter /* 87110671Speter * if there were file variables declared at this level 87210671Speter * call PCLOSE( ap ) to clean them up. 87310671Speter */ 87410671Speter if ( dfiles[ cbn ] ) { 87510671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) 87610671Speter , "_PCLOSE" ); 87710671Speter putleaf( P2REG , 0 , P2AP , ADDTYPE( P2CHAR , P2PTR ) , 0 ); 87810671Speter putop( P2CALL , P2INT ); 87910671Speter putdot( filename , line ); 88010671Speter } 88110671Speter /* 88210671Speter * if this is a function, 88310671Speter * the function variable is the return value. 88410671Speter * if it's a scalar valued function, return scalar, 88510671Speter * else, return a pointer to the structure value. 88610671Speter */ 88710671Speter if ( eecookiep -> nlp -> class == FUNC ) { 88810671Speter struct nl *fvar = eecookiep -> nlp -> ptr[ NL_FVAR ]; 88910671Speter long fvartype = p2type( fvar -> type ); 89010671Speter long label; 89110671Speter char labelname[ BUFSIZ ]; 89210671Speter 89310671Speter switch ( classify( fvar -> type ) ) { 89410671Speter case TBOOL: 89510671Speter case TCHAR: 89610671Speter case TINT: 89710671Speter case TSCAL: 89810671Speter case TDOUBLE: 89910671Speter case TPTR: 90010671Speter putRV( fvar -> symbol , ( fvar -> nl_block ) & 037 , 90110671Speter fvar -> value[ NL_OFFS ] , 90210671Speter fvar -> extra_flags , 90310671Speter fvartype ); 90410671Speter break; 90510671Speter default: 90610671Speter label = getlab(); 90710671Speter sprintf( labelname , PREFIXFORMAT , LABELPREFIX , label ); 90810671Speter putprintf(" .lcomm %s,%d", 0, 90910671Speter labelname, lwidth(fvar -> type)); 91010671Speter putleaf( P2NAME , 0 , 0 , fvartype , labelname ); 91110671Speter putLV( fvar -> symbol , ( fvar -> nl_block ) & 037 , 91210671Speter fvar -> value[ NL_OFFS ] , 91310671Speter fvar -> extra_flags , 91410671Speter fvartype ); 91510671Speter putstrop( P2STASG , fvartype , lwidth( fvar -> type ) , 91610671Speter align( fvar -> type ) ); 91710671Speter putdot( filename , line ); 91810671Speter putleaf( P2ICON , 0 , 0 , fvartype , labelname ); 91910671Speter break; 92010671Speter } 92110671Speter putop( P2FORCE , fvartype ); 92210671Speter putdot( filename , line ); 92310671Speter } 92410671Speter /* 92510671Speter * if we saved a display, we must restore it. 92610671Speter */ 92710671Speter if ( parts[ cbn ] & NONLOCALVAR ) { 92810671Speter /* 92910671Speter * restore old display entry from save area 93010671Speter */ 93110671Speter putprintf(" movl %s@(%d),%s+%d", 0, 93210671Speter P2FPNAME, DSAVEOFFSET, 93310671Speter DISPLAYNAME, cbn * sizeof(struct dispsave)); 93410671Speter } 93510671Speter } 93610671Speter 93710671Speter fp_epilogue(eecookiep) 93810671Speter struct entry_exit_cookie *eecookiep; 93910671Speter { 94010671Speter /* 94110671Speter * all done by the second pass. 94210671Speter */ 94310671Speter } 94410671Speter 94510671Speter fp_formalentry(eecookiep) 94610671Speter struct entry_exit_cookie *eecookiep; 94710671Speter { 94810671Speter putprintf( "%s%s:" , 0 , FORMALPREFIX , eecookiep -> extname ); 94910671Speter putprintf(" link %s,#0", 0, P2FPNAME); 95010671Speter putprintf(" addl #-%s%d,sp", 0, FRAME_SIZE_LABEL, ftnno); 95110671Speter /* touch new end of stack, to break more stack space */ 95210671Speter putprintf(" tstb sp@(-%s%d)", 0, PAGE_BREAK_LABEL, ftnno); 95310671Speter putprintf(" moveml #%s%d,sp@", 0, SAVE_MASK_LABEL, ftnno); 95410671Speter putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) , "_FCALL" ); 95510671Speter putRV( 0 , cbn , 95610671Speter eecookiep -> nlp -> value[ NL_OFFS ] + sizeof( struct formalrtn * ) , 95710671Speter NPARAM , P2PTR | P2STRTY ); 95810671Speter putRV(0, cbn, eecookiep -> nlp -> value[NL_OFFS], NPARAM, P2PTR|P2STRTY); 95910671Speter putop( P2LISTOP , P2INT ); 96010671Speter putop( P2CALL , P2INT ); 96110671Speter putdot( filename , line ); 96210671Speter putjbr( eecookiep -> toplabel ); 96310671Speter } 96410671Speter #endif mc68000 9653363Speter #endif PC 966