1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)p2put.c 1.8 04/21/81"; 4 5 /* 6 * functions to help pi put out 7 * polish postfix binary portable c compiler intermediate code 8 * thereby becoming the portable pascal compiler 9 */ 10 11 #include "whoami.h" 12 #ifdef PC 13 #include "0.h" 14 #include "pcops.h" 15 #include "pc.h" 16 17 /* 18 * mash into f77's format 19 * lovely, isn't it? 20 */ 21 #define TOF77( fop,val,rest ) ( ( ( (rest) & 0177777 ) << 16 ) \ 22 | ( ( (val) & 0377 ) << 8 ) \ 23 | ( (fop) & 0377 ) ) 24 25 /* 26 * emits an ftext operator and a string to the pcstream 27 */ 28 puttext( string ) 29 char *string; 30 { 31 int length = str4len( string ); 32 33 if ( !CGENNING ) 34 return; 35 p2word( TOF77( P2FTEXT , length , 0 ) ); 36 # ifdef DEBUG 37 if ( opt( 'k' ) ) { 38 fprintf( stdout , "P2FTEXT | %3d | 0 " , length ); 39 } 40 # endif 41 p2string( string ); 42 } 43 44 int 45 str4len( string ) 46 char *string; 47 { 48 49 return ( ( strlen( string ) + 3 ) / 4 ); 50 } 51 52 /* 53 * put formatted text into a buffer for printing to the pcstream. 54 * a call to putpflush actually puts out the text. 55 * none of arg1 .. arg5 need be present. 56 * and you can add more if you need them. 57 */ 58 /* VARARGS */ 59 putprintf( format , incomplete , arg1 , arg2 , arg3 , arg4 , arg5 ) 60 char *format; 61 int incomplete; 62 { 63 static char ppbuffer[ BUFSIZ ]; 64 static char *ppbufp = ppbuffer; 65 66 if ( !CGENNING ) 67 return; 68 sprintf( ppbufp , format , arg1 , arg2 , arg3 , arg4 , arg5 ); 69 ppbufp = &( ppbuffer[ strlen( ppbuffer ) ] ); 70 if ( ppbufp >= &( ppbuffer[ BUFSIZ ] ) ) 71 panic( "putprintf" ); 72 if ( ! incomplete ) { 73 puttext( ppbuffer ); 74 ppbufp = ppbuffer; 75 } 76 } 77 78 /* 79 * emit a left bracket operator to pcstream 80 * with function number, the maximum temp register, and total local bytes 81 * until i figure out how to use them, regs 0 .. 11 are free. 82 * one idea for one reg is to save the display pointer on block entry 83 */ 84 putlbracket( ftnno , localbytes ) 85 int ftnno; 86 int localbytes; 87 { 88 # define MAXTP2REG 11 89 90 p2word( TOF77( P2FLBRAC , MAXTP2REG , ftnno ) ); 91 p2word( BITSPERBYTE * localbytes ); 92 # ifdef DEBUG 93 if ( opt( 'k' ) ) { 94 fprintf( stdout 95 , "P2FLBRAC | %3d | %d " , MAXTP2REG , ftnno ); 96 fprintf( stdout , "%d\n" 97 , BITSPERBYTE * localbytes ); 98 } 99 # endif 100 } 101 102 /* 103 * emit a right bracket operator 104 * which for the binary (fortran) interface 105 * forces the stack allocate and register mask 106 */ 107 putrbracket( ftnno ) 108 int ftnno; 109 { 110 111 p2word( TOF77( P2FRBRAC , 0 , ftnno ) ); 112 # ifdef DEBUG 113 if ( opt( 'k' ) ) { 114 fprintf( stdout , "P2FRBRAC | 0 | %d\n" , ftnno ); 115 } 116 # endif 117 } 118 119 /* 120 * emit an eof operator 121 */ 122 puteof() 123 { 124 125 p2word( P2FEOF ); 126 # ifdef DEBUG 127 if ( opt( 'k' ) ) { 128 fprintf( stdout , "P2FEOF\n" ); 129 } 130 # endif 131 } 132 133 /* 134 * emit a dot operator, 135 * with a source file line number and name 136 * if line is negative, there was an error on that line, but who cares? 137 */ 138 putdot( filename , line ) 139 char *filename; 140 int line; 141 { 142 int length = str4len( filename ); 143 144 if ( line < 0 ) { 145 line = -line; 146 } 147 p2word( TOF77( P2FEXPR , length , line ) ); 148 # ifdef DEBUG 149 if ( opt( 'k' ) ) { 150 fprintf( stdout , "P2FEXPR | %3d | %d " , length , line ); 151 } 152 # endif 153 p2string( filename ); 154 } 155 156 /* 157 * put out a leaf node 158 */ 159 putleaf( op , lval , rval , type , name ) 160 int op; 161 int lval; 162 int rval; 163 int type; 164 char *name; 165 { 166 if ( !CGENNING ) 167 return; 168 switch ( op ) { 169 default: 170 panic( "[putleaf]" ); 171 case P2ICON: 172 p2word( TOF77( P2ICON , name != NIL , type ) ); 173 p2word( lval ); 174 # ifdef DEBUG 175 if ( opt( 'k' ) ) { 176 fprintf( stdout , "P2ICON | %3d | 0x%x " 177 , name != NIL , type ); 178 fprintf( stdout , "%d\n" , lval ); 179 } 180 # endif 181 if ( name ) 182 p2name( name ); 183 break; 184 case P2NAME: 185 p2word( TOF77( P2NAME , lval != 0 , type ) ); 186 if ( lval ) 187 p2word( lval ); 188 # ifdef DEBUG 189 if ( opt( 'k' ) ) { 190 fprintf( stdout , "P2NAME | %3d | 0x%x " 191 , lval != 0 , type ); 192 if ( lval ) 193 fprintf( stdout , "%d " , lval ); 194 } 195 # endif 196 p2name( name ); 197 break; 198 case P2REG: 199 p2word( TOF77( P2REG , rval , type ) ); 200 # ifdef DEBUG 201 if ( opt( 'k' ) ) { 202 fprintf( stdout , "P2REG | %3d | 0x%x\n" , 203 rval , type ); 204 } 205 # endif 206 break; 207 } 208 } 209 210 /* 211 * rvalues are just lvalues with indirection, except 212 * special case for named globals, whose names are their rvalues 213 */ 214 putRV( name , level , offset , type ) 215 char *name; 216 int level; 217 int offset; 218 int type; 219 { 220 char extname[ BUFSIZ ]; 221 char *printname; 222 int regnumber; 223 224 if ( !CGENNING ) 225 return; 226 if (whereis(offset) == REGVAR) { 227 regnumber = ( - offset ) >> 1; 228 if ( ( regnumber < 0 ) || ( regnumber > P2FP ) ) { 229 panic( "putRV regnumber" ); 230 } 231 putleaf( P2REG , 0 , regnumber , type , 0 ); 232 return; 233 } 234 if ( ( level <= 1 ) && ( name != 0 ) ) { 235 if ( name[0] != '_' ) { 236 sprintf( extname , EXTFORMAT , name ); 237 printname = extname; 238 } else { 239 printname = name; 240 } 241 putleaf( P2NAME , offset , 0 , type , printname ); 242 return; 243 } 244 putLV( name , level , offset , type ); 245 putop( P2UNARY P2MUL , type ); 246 } 247 248 /* 249 * put out an lvalue 250 * given a level and offset 251 * special case for 252 * named globals, whose lvalues are just their names as constants. 253 * negative offsets, that are offsets from the frame pointer. 254 * odd negative numbers are register variables. 255 * positive offsets, that are offsets from argument pointer. 256 */ 257 putLV( name , level , offset , type ) 258 char *name; 259 int level; 260 int offset; 261 int type; 262 { 263 char extname[ BUFSIZ ]; 264 char *printname; 265 266 if ( !CGENNING ) 267 return; 268 if ( ( level <= 1 ) && ( name != 0 ) ) { 269 if ( name[0] != '_' ) { 270 sprintf( extname , EXTFORMAT , name ); 271 printname = extname; 272 } else { 273 printname = name; 274 } 275 putleaf( P2ICON , offset , 0 , ADDTYPE( type , P2PTR ) 276 , printname ); 277 return; 278 } 279 switch(whereis(offset)) { 280 case PARAMVAR: 281 if ( level == cbn ) { 282 putleaf( P2REG , 0 , P2AP , ADDTYPE( type , P2PTR ) , 0 ); 283 } else { 284 putleaf( P2NAME , (level * sizeof(struct dispsave)) + AP_OFFSET 285 , 0 , P2PTR | P2CHAR , DISPLAYNAME ); 286 } 287 putleaf( P2ICON , offset , 0 , P2INT , 0 ); 288 putop( P2PLUS , P2PTR | P2CHAR ); 289 break; 290 case LOCALVAR: 291 if ( level == cbn ) { 292 putleaf( P2REG , 0 , P2FP , ADDTYPE( type , P2PTR ) , 0 ); 293 } else { 294 putleaf( P2NAME , (level * sizeof(struct dispsave)) + FP_OFFSET 295 , 0 , P2PTR | P2CHAR , DISPLAYNAME ); 296 } 297 putleaf( P2ICON , -offset , 0 , P2INT , 0 ); 298 putop( P2MINUS , P2PTR | P2CHAR ); 299 break; 300 case REGVAR: 301 panic("putLV regvar"); 302 } 303 return; 304 } 305 306 /* 307 * put out a floating point constant leaf node 308 * the constant is declared in aligned data space 309 * and a P2NAME leaf put out for it 310 */ 311 putCON8( value ) 312 double value; 313 { 314 int label; 315 char name[ BUFSIZ ]; 316 317 if ( !CGENNING ) 318 return; 319 putprintf( " .data" , 0 ); 320 putprintf( " .align 2" , 0 ); 321 label = getlab(); 322 putlab( label ); 323 putprintf( " .double 0d%.20e" , 0 , value ); 324 putprintf( " .text" , 0 ); 325 sprintf( name , PREFIXFORMAT , LABELPREFIX , label ); 326 putleaf( P2NAME , 0 , 0 , P2DOUBLE , name ); 327 } 328 329 /* 330 * put out either an lvalue or an rvalue for a constant string. 331 * an lvalue (for assignment rhs's) is the name as a constant, 332 * an rvalue (for parameters) is just the name. 333 */ 334 putCONG( string , length , required ) 335 char *string; 336 int length; 337 int required; 338 { 339 char name[ BUFSIZ ]; 340 int label; 341 char *cp; 342 int pad; 343 int others; 344 345 if ( !CGENNING ) 346 return; 347 putprintf( " .data" , 0 ); 348 label = getlab(); 349 putlab( label ); 350 cp = string; 351 while ( *cp ) { 352 putprintf( " .byte 0%o" , 1 , *cp ++ ); 353 for ( others = 2 ; ( others <= 8 ) && *cp ; others ++ ) { 354 putprintf( ",0%o" , 1 , *cp++ ); 355 } 356 putprintf( "" , 0 ); 357 } 358 pad = length - strlen( string ); 359 while ( pad-- > 0 ) { 360 putprintf( " .byte 0%o" , 1 , ' ' ); 361 for ( others = 2 ; ( others <= 8 ) && ( pad-- > 0 ) ; others++ ) { 362 putprintf( ",0%o" , 1 , ' ' ); 363 } 364 putprintf( "" , 0 ); 365 } 366 putprintf( " .byte 0" , 0 ); 367 putprintf( " .text" , 0 ); 368 sprintf( name , PREFIXFORMAT , LABELPREFIX , label ); 369 if ( required == RREQ ) { 370 putleaf( P2NAME , 0 , 0 , P2ARY | P2CHAR , name ); 371 } else { 372 putleaf( P2ICON , 0 , 0 , P2PTR | P2CHAR , name ); 373 } 374 } 375 376 /* 377 * map a pascal type to a c type 378 * this would be tail recursive, but i unfolded it into a for (;;). 379 * this is sort of like isa and lwidth 380 * a note on the types used by the portable c compiler: 381 * they are divided into a basic type (char, short, int, long, etc.) 382 * and qualifications on those basic types (pointer, function, array). 383 * the basic type is kept in the low 4 bits of the type descriptor, 384 * and the qualifications are arranged in two bit chunks, with the 385 * most significant on the right, 386 * and the least significant on the left 387 * e.g. int *foo(); 388 * (a function returning a pointer to an integer) 389 * is stored as 390 * <ptr><ftn><int> 391 * so, we build types recursively 392 * also, we know that /lib/f1 can only deal with 6 qualifications 393 * so we stop the recursion there. this stops infinite type recursion 394 * through mutually recursive pointer types. 395 */ 396 #define MAXQUALS 6 397 int 398 p2type( np ) 399 { 400 401 return typerecur( np , 0 ); 402 } 403 typerecur( np , quals ) 404 struct nl *np; 405 int quals; 406 { 407 408 if ( np == NIL || quals > MAXQUALS ) { 409 return P2UNDEF; 410 } 411 switch ( np -> class ) { 412 case SCAL : 413 case RANGE : 414 if ( np -> type == ( nl + TDOUBLE ) ) { 415 return P2DOUBLE; 416 } 417 switch ( bytes( np -> range[0] , np -> range[1] ) ) { 418 case 1: 419 return P2CHAR; 420 case 2: 421 return P2SHORT; 422 case 4: 423 return P2INT; 424 default: 425 panic( "p2type int" ); 426 } 427 case STR : 428 return ( P2ARY | P2CHAR ); 429 case RECORD : 430 case SET : 431 return P2STRTY; 432 case FILET : 433 return ( P2PTR | P2STRTY ); 434 case CONST : 435 case VAR : 436 case FIELD : 437 return p2type( np -> type ); 438 case TYPE : 439 switch ( nloff( np ) ) { 440 case TNIL : 441 return ( P2PTR | P2UNDEF ); 442 case TSTR : 443 return ( P2ARY | P2CHAR ); 444 case TSET : 445 return P2STRTY; 446 default : 447 return ( p2type( np -> type ) ); 448 } 449 case REF: 450 case WITHPTR: 451 case PTR : 452 return ADDTYPE( typerecur( np -> type , quals + 1 ) , P2PTR ); 453 case ARRAY : 454 return ADDTYPE( typerecur( np -> type , quals + 1 ) , P2ARY ); 455 case FUNC : 456 /* 457 * functions are really pointers to functions 458 * which return their underlying type. 459 */ 460 return ADDTYPE( ADDTYPE( typerecur( np -> type , quals + 2 ) , 461 P2FTN ) , P2PTR ); 462 case PROC : 463 /* 464 * procedures are pointers to functions 465 * which return integers (whether you look at them or not) 466 */ 467 return ADDTYPE( ADDTYPE( P2INT , P2FTN ) , P2PTR ); 468 case FFUNC : 469 case FPROC : 470 /* 471 * formal procedures and functions are pointers 472 * to structures which describe their environment. 473 */ 474 return ( P2PTR | P2STRTY ); 475 default : 476 panic( "p2type" ); 477 } 478 } 479 480 /* 481 * add a most significant type modifier to a type 482 */ 483 long 484 addtype( underlying , mtype ) 485 long underlying; 486 long mtype; 487 { 488 return ( ( ( underlying & ~P2BASETYPE ) << P2TYPESHIFT ) 489 | mtype 490 | ( underlying & P2BASETYPE ) ); 491 } 492 493 /* 494 * put a typed operator to the pcstream 495 */ 496 putop( op , type ) 497 int op; 498 int type; 499 { 500 extern char *p2opnames[]; 501 502 if ( !CGENNING ) 503 return; 504 p2word( TOF77( op , 0 , type ) ); 505 # ifdef DEBUG 506 if ( opt( 'k' ) ) { 507 fprintf( stdout , "%s (%d) | 0 | 0x%x\n" 508 , p2opnames[ op ] , op , type ); 509 } 510 # endif 511 } 512 513 /* 514 * put out a structure operator (STASG, STARG, STCALL, UNARY STCALL ) 515 * which looks just like a regular operator, only the size and 516 * alignment go in the next consecutive words 517 */ 518 putstrop( op , type , size , alignment ) 519 int op; 520 int type; 521 int size; 522 int alignment; 523 { 524 extern char *p2opnames[]; 525 526 if ( !CGENNING ) 527 return; 528 p2word( TOF77( op , 0 , type ) ); 529 p2word( size ); 530 p2word( alignment ); 531 # ifdef DEBUG 532 if ( opt( 'k' ) ) { 533 fprintf( stdout , "%s (%d) | 0 | 0x%x %d %d\n" 534 , p2opnames[ op ] , op , type , size , alignment ); 535 } 536 # endif 537 } 538 539 /* 540 * the string names of p2ops 541 */ 542 char *p2opnames[] = { 543 "", 544 "P2UNDEFINED", /* 1 */ 545 "P2NAME", /* 2 */ 546 "P2STRING", /* 3 */ 547 "P2ICON", /* 4 */ 548 "P2FCON", /* 5 */ 549 "P2PLUS", /* 6 */ 550 "", 551 "P2MINUS", /* 8 also unary == P2NEG */ 552 "", 553 "P2NEG", 554 "P2MUL", /* 11 also unary == P2INDIRECT */ 555 "", 556 "P2INDIRECT", 557 "P2AND", /* 14 also unary == P2ADDROF */ 558 "", 559 "P2ADDROF", 560 "P2OR", /* 17 */ 561 "", 562 "P2ER", /* 19 */ 563 "", 564 "P2QUEST", /* 21 */ 565 "P2COLON", /* 22 */ 566 "P2ANDAND", /* 23 */ 567 "P2OROR", /* 24 */ 568 "", /* 25 */ 569 "", /* 26 */ 570 "", /* 27 */ 571 "", /* 28 */ 572 "", /* 29 */ 573 "", /* 30 */ 574 "", /* 31 */ 575 "", /* 32 */ 576 "", /* 33 */ 577 "", /* 34 */ 578 "", /* 35 */ 579 "", /* 36 */ 580 "", /* 37 */ 581 "", /* 38 */ 582 "", /* 39 */ 583 "", /* 40 */ 584 "", /* 41 */ 585 "", /* 42 */ 586 "", /* 43 */ 587 "", /* 44 */ 588 "", /* 45 */ 589 "", /* 46 */ 590 "", /* 47 */ 591 "", /* 48 */ 592 "", /* 49 */ 593 "", /* 50 */ 594 "", /* 51 */ 595 "", /* 52 */ 596 "", /* 53 */ 597 "", /* 54 */ 598 "", /* 55 */ 599 "P2LISTOP", /* 56 */ 600 "", 601 "P2ASSIGN", /* 58 */ 602 "P2COMOP", /* 59 */ 603 "P2DIV", /* 60 */ 604 "", 605 "P2MOD", /* 62 */ 606 "", 607 "P2LS", /* 64 */ 608 "", 609 "P2RS", /* 66 */ 610 "", 611 "P2DOT", /* 68 */ 612 "P2STREF", /* 69 */ 613 "P2CALL", /* 70 also unary */ 614 "", 615 "P2UNARYCALL", 616 "P2FORTCALL", /* 73 also unary */ 617 "", 618 "P2UNARYFORTCALL", 619 "P2NOT", /* 76 */ 620 "P2COMPL", /* 77 */ 621 "P2INCR", /* 78 */ 622 "P2DECR", /* 79 */ 623 "P2EQ", /* 80 */ 624 "P2NE", /* 81 */ 625 "P2LE", /* 82 */ 626 "P2LT", /* 83 */ 627 "P2GE", /* 84 */ 628 "P2GT", /* 85 */ 629 "P2ULE", /* 86 */ 630 "P2ULT", /* 87 */ 631 "P2UGE", /* 88 */ 632 "P2UGT", /* 89 */ 633 "P2SETBIT", /* 90 */ 634 "P2TESTBIT", /* 91 */ 635 "P2RESETBIT", /* 92 */ 636 "P2ARS", /* 93 */ 637 "P2REG", /* 94 */ 638 "P2OREG", /* 95 */ 639 "P2CCODES", /* 96 */ 640 "P2FREE", /* 97 */ 641 "P2STASG", /* 98 */ 642 "P2STARG", /* 99 */ 643 "P2STCALL", /* 100 also unary */ 644 "", 645 "P2UNARYSTCALL", 646 "P2FLD", /* 103 */ 647 "P2SCONV", /* 104 */ 648 "P2PCONV", /* 105 */ 649 "P2PMCONV", /* 106 */ 650 "P2PVCONV", /* 107 */ 651 "P2FORCE", /* 108 */ 652 "P2CBRANCH", /* 109 */ 653 "P2INIT", /* 110 */ 654 "P2CAST", /* 111 */ 655 }; 656 657 /* 658 * low level routines 659 */ 660 661 /* 662 * puts a long word on the pcstream 663 */ 664 p2word( word ) 665 long word; 666 { 667 668 putw( word , pcstream ); 669 } 670 671 /* 672 * put a length 0 mod 4 null padded string onto the pcstream 673 */ 674 p2string( string ) 675 char *string; 676 { 677 int slen = strlen( string ); 678 int wlen = ( slen + 3 ) / 4; 679 int plen = ( wlen * 4 ) - slen; 680 char *cp; 681 int p; 682 683 for ( cp = string ; *cp ; cp++ ) 684 putc( *cp , pcstream ); 685 for ( p = 1 ; p <= plen ; p++ ) 686 putc( '\0' , pcstream ); 687 # ifdef DEBUG 688 if ( opt( 'k' ) ) { 689 fprintf( stdout , "\"%s" , string ); 690 for ( p = 1 ; p <= plen ; p++ ) 691 fprintf( stdout , "\\0" ); 692 fprintf( stdout , "\"\n" ); 693 } 694 # endif 695 } 696 697 /* 698 * puts a name on the pcstream 699 */ 700 p2name( name ) 701 char *name; 702 { 703 int pad; 704 705 fprintf( pcstream , NAMEFORMAT , name ); 706 pad = strlen( name ) % sizeof (long); 707 for ( ; pad < sizeof (long) ; pad++ ) { 708 putc( '\0' , pcstream ); 709 } 710 # ifdef DEBUG 711 if ( opt( 'k' ) ) { 712 fprintf( stdout , NAMEFORMAT , name ); 713 pad = strlen( name ) % sizeof (long); 714 for ( ; pad < sizeof (long) ; pad++ ) { 715 fprintf( stdout , "\\0" ); 716 } 717 fprintf( stdout , "\n" ); 718 } 719 # endif 720 } 721 722 /* 723 * put out a jump to a label 724 */ 725 putjbr( label ) 726 long label; 727 { 728 729 printjbr( LABELPREFIX , label ); 730 } 731 732 /* 733 * put out a jump to any kind of label 734 */ 735 printjbr( prefix , label ) 736 char *prefix; 737 long label; 738 { 739 740 putprintf( " jbr " , 1 ); 741 putprintf( PREFIXFORMAT , 0 , prefix , label ); 742 } 743 744 /* 745 * another version of put to catch calls to put 746 */ 747 put( arg1 , arg2 ) 748 { 749 750 putprintf( "# PUT CALLED!: arg1 = %d arg2 = 0%o" , 0 , arg1 , arg2 ); 751 } 752 753 #endif PC 754