1 /* Copyright (c) 1979 Regents of the University of California */ 2 3 static char sccsid[] = "@(#)put.c 1.19 01/12/83"; 4 5 #include "whoami.h" 6 #include "opcode.h" 7 #include "0.h" 8 #include "objfmt.h" 9 #ifdef PC 10 # include "pc.h" 11 #endif PC 12 13 short *obufp = obuf; 14 15 /* 16 * If DEBUG is defined, include the table 17 * of the printing opcode names. 18 */ 19 #ifdef DEBUG 20 #include "OPnames.h" 21 #endif 22 23 #ifdef OBJ 24 /* 25 * Put is responsible for the interpreter equivalent of code 26 * generation. Since the interpreter is specifically designed 27 * for Pascal, little work is required here. 28 */ 29 put(a) 30 { 31 register int *p, i; 32 register char *cp; 33 register short *sp; 34 register long *lp; 35 int n, subop, suboppr, op, oldlc, w; 36 char *string; 37 static int casewrd; 38 39 /* 40 * It would be nice to do some more 41 * optimizations here. The work 42 * done to collapse offsets in lval 43 * should be done here, the IFEQ etc 44 * relational operators could be used 45 * etc. 46 */ 47 oldlc = lc; 48 if ( !CGENNING ) 49 /* 50 * code disabled - do nothing 51 */ 52 return (oldlc); 53 p = &a; 54 n = *p++; 55 suboppr = subop = (*p >> 8) & 0377; 56 op = *p & 0377; 57 string = 0; 58 #ifdef DEBUG 59 if ((cp = otext[op]) == NIL) { 60 printf("op= %o\n", op); 61 panic("put"); 62 } 63 #endif 64 switch (op) { 65 case O_ABORT: 66 cp = "*"; 67 break; 68 case O_AS: 69 switch(p[1]) { 70 case 0: 71 break; 72 case 2: 73 op = O_AS2; 74 n = 1; 75 break; 76 case 4: 77 op = O_AS4; 78 n = 1; 79 break; 80 case 8: 81 op = O_AS8; 82 n = 1; 83 break; 84 default: 85 goto pack; 86 } 87 # ifdef DEBUG 88 cp = otext[op]; 89 # endif DEBUG 90 break; 91 case O_CONG: 92 case O_LVCON: 93 case O_CON: 94 case O_LINO: 95 case O_NEW: 96 case O_DISPOSE: 97 case O_DFDISP: 98 case O_IND: 99 case O_OFF: 100 case O_INX2: 101 case O_INX4: 102 case O_CARD: 103 case O_ADDT: 104 case O_SUBT: 105 case O_MULT: 106 case O_IN: 107 case O_CASE1OP: 108 case O_CASE2OP: 109 case O_CASE4OP: 110 case O_FRTN: 111 case O_WRITES: 112 case O_WRITEC: 113 case O_WRITEF: 114 case O_MAX: 115 case O_MIN: 116 case O_ARGV: 117 case O_CTTOT: 118 case O_INCT: 119 case O_RANG2: 120 case O_RSNG2: 121 case O_RANG42: 122 case O_RSNG42: 123 case O_SUCC2: 124 case O_SUCC24: 125 case O_PRED2: 126 case O_PRED24: 127 if (p[1] == 0) 128 break; 129 case O_CON2: 130 case O_CON24: 131 pack: 132 if (p[1] < 128 && p[1] >= -128) { 133 suboppr = subop = p[1]; 134 p++; 135 n--; 136 if (op == O_CON2) { 137 op = O_CON1; 138 # ifdef DEBUG 139 cp = otext[O_CON1]; 140 # endif DEBUG 141 } 142 if (op == O_CON24) { 143 op = O_CON14; 144 # ifdef DEBUG 145 cp = otext[O_CON14]; 146 # endif DEBUG 147 } 148 } 149 break; 150 case O_CON8: 151 { 152 short *sp = &p[1]; 153 154 #ifdef DEBUG 155 if ( opt( 'k' ) ) 156 printf ( "%5d\tCON8\t%22.14e\n" , 157 lc - HEADER_BYTES , 158 * ( ( double * ) &p[1] ) ); 159 #endif 160 # ifdef DEC11 161 word(op); 162 # else 163 word(op << 8); 164 # endif DEC11 165 for ( i = 1 ; i <= 4 ; i ++ ) 166 word ( *sp ++ ); 167 return ( oldlc ); 168 } 169 default: 170 if (op >= O_REL2 && op <= O_REL84) { 171 if ((i = (subop >> INDX) * 5 ) >= 30) 172 i -= 30; 173 else 174 i += 2; 175 #ifdef DEBUG 176 string = &"IFEQ\0IFNE\0IFLT\0IFGT\0IFLE\0IFGE"[i]; 177 #endif 178 suboppr = 0; 179 } 180 break; 181 case O_IF: 182 case O_TRA: 183 /***** 184 codeline = 0; 185 *****/ 186 /* relative addressing */ 187 p[1] -= ( unsigned ) lc + sizeof(short); 188 break; 189 case O_FOR1U: 190 case O_FOR2U: 191 case O_FOR1D: 192 case O_FOR2D: 193 /* sub opcode optimization */ 194 if (p[1] < 128 && p[1] >= -128 && p[1] != 0) { 195 suboppr = subop = p[1]; 196 p++; 197 n--; 198 } 199 /* relative addressing */ 200 p[n - 1] -= ( unsigned ) lc + (n - 1) * sizeof(short); 201 break; 202 case O_CONC: 203 #ifdef DEBUG 204 (string = "'x'")[1] = p[1]; 205 #endif 206 suboppr = 0; 207 op = O_CON1; 208 # ifdef DEBUG 209 cp = otext[O_CON1]; 210 # endif DEBUG 211 subop = p[1]; 212 goto around; 213 case O_CONC4: 214 #ifdef DEBUG 215 (string = "'x'")[1] = p[1]; 216 #endif 217 suboppr = 0; 218 op = O_CON14; 219 subop = p[1]; 220 goto around; 221 case O_CON1: 222 case O_CON14: 223 suboppr = subop = p[1]; 224 around: 225 n--; 226 break; 227 case O_CASEBEG: 228 casewrd = 0; 229 return (oldlc); 230 case O_CASEEND: 231 if ((unsigned) lc & 1) { 232 lc--; 233 word(casewrd); 234 } 235 return (oldlc); 236 case O_CASE1: 237 #ifdef DEBUG 238 if (opt('k')) 239 printf("%5d\tCASE1\t%d\n" 240 , lc - HEADER_BYTES, p[1]); 241 #endif 242 /* 243 * this to build a byte size case table 244 * saving bytes across calls in casewrd 245 * so they can be put out by word() 246 */ 247 lc++; 248 if ((unsigned) lc & 1) 249 # ifdef DEC11 250 casewrd = p[1] & 0377; 251 # else 252 casewrd = (p[1] & 0377) << 8; 253 # endif DEC11 254 else { 255 lc -= 2; 256 # ifdef DEC11 257 word(((p[1] & 0377) << 8) | casewrd); 258 # else 259 word((p[1] & 0377) | casewrd); 260 # endif DEC11 261 } 262 return (oldlc); 263 case O_CASE2: 264 #ifdef DEBUG 265 if (opt('k')) 266 printf("%5d\tCASE2\t%d\n" 267 , lc - HEADER_BYTES , p[1]); 268 #endif 269 word(p[1]); 270 return (oldlc); 271 case O_FOR4U: 272 case O_FOR4D: 273 /* sub opcode optimization */ 274 lp = (long *)&p[1]; 275 if (*lp < 128 && *lp >= -128 && *lp != 0) { 276 suboppr = subop = *lp; 277 p += (sizeof(long) / sizeof(int)); 278 n--; 279 } 280 /* relative addressing */ 281 p[1 + (n - 2) * (sizeof(long) / sizeof(int))] -= 282 (unsigned)lc + (sizeof(short) + 283 (n - 2) * sizeof(long)); 284 goto longgen; 285 case O_PUSH: 286 lp = (long *)&p[1]; 287 if (*lp == 0) 288 return (oldlc); 289 /* and fall through */ 290 case O_RANG4: 291 case O_RANG24: 292 case O_RSNG4: 293 case O_RSNG24: 294 case O_SUCC4: 295 case O_PRED4: 296 /* sub opcode optimization */ 297 lp = (long *)&p[1]; 298 if (*lp < 128 && *lp >= -128 && *lp != 0) { 299 suboppr = subop = *lp; 300 p += (sizeof(long) / sizeof(int)); 301 n--; 302 } 303 goto longgen; 304 case O_TRA4: 305 case O_CALL: 306 case O_FSAV: 307 case O_GOTO: 308 case O_NAM: 309 case O_READE: 310 /* absolute long addressing */ 311 lp = (long *)&p[1]; 312 *lp -= HEADER_BYTES; 313 goto longgen; 314 case O_RV1: 315 case O_RV14: 316 case O_RV2: 317 case O_RV24: 318 case O_RV4: 319 case O_RV8: 320 case O_RV: 321 case O_LV: 322 /* 323 * positive offsets represent arguments 324 * and must use "ap" display entry rather 325 * than the "fp" entry 326 */ 327 if (p[1] >= 0) { 328 subop++; 329 suboppr++; 330 } 331 # ifdef PDP11 332 break; 333 # else 334 /* 335 * offsets out of range of word addressing 336 * must use long offset opcodes 337 */ 338 if (p[1] < SHORTADDR && p[1] >= -SHORTADDR) 339 break; 340 else { 341 op += O_LRV - O_RV; 342 # ifdef DEBUG 343 cp = otext[op]; 344 # endif DEBUG 345 } 346 /* and fall through */ 347 # endif PDP11 348 case O_BEG: 349 case O_NODUMP: 350 case O_CON4: 351 case O_CASE4: 352 longgen: 353 n = (n << 1) - 1; 354 if ( op == O_LRV || op == O_FOR4U || op == O_FOR4D) 355 n--; 356 #ifdef DEBUG 357 if (opt('k')) { 358 printf("%5d\t%s", lc - HEADER_BYTES, cp+1); 359 if (suboppr) 360 printf(":%d", suboppr); 361 for ( i = 2, lp = (long *)&p[1]; i < n 362 ; i += sizeof ( long )/sizeof ( short ) ) 363 printf( "\t%D " , *lp ++ ); 364 if (i == n) { 365 sp = (short *)lp; 366 printf( "\t%d ", *sp ); 367 } 368 pchr ( '\n' ); 369 } 370 #endif 371 if ( op != O_CASE4 ) 372 # ifdef DEC11 373 word((op & 0377) | subop << 8); 374 # else 375 word(op << 8 | (subop & 0377)); 376 # endif DEC11 377 for ( i = 1, sp = (short *)&p[1]; i < n; i++) 378 word ( *sp ++ ); 379 return ( oldlc ); 380 } 381 #ifdef DEBUG 382 if (opt('k')) { 383 printf("%5d\t%s", lc - HEADER_BYTES, cp+1); 384 if (suboppr) 385 printf(":%d", suboppr); 386 if (string) 387 printf("\t%s",string); 388 if (n > 1) 389 pchr('\t'); 390 for (i=1; i<n; i++) 391 printf("%d ", p[i]); 392 pchr('\n'); 393 } 394 #endif 395 if (op != NIL) 396 # ifdef DEC11 397 word((op & 0377) | subop << 8); 398 # else 399 word(op << 8 | (subop & 0377)); 400 # endif DEC11 401 for (i=1; i<n; i++) 402 word(p[i]); 403 return (oldlc); 404 } 405 #endif OBJ 406 407 /* 408 * listnames outputs a list of enumerated type names which 409 * can then be selected from to output a TSCAL 410 * a pointer to the address in the code of the namelist 411 * is kept in value[ NL_ELABEL ]. 412 */ 413 listnames(ap) 414 415 register struct nl *ap; 416 { 417 struct nl *next; 418 register int oldlc, len; 419 register unsigned w; 420 register char *strptr; 421 422 if ( !CGENNING ) 423 /* code is off - do nothing */ 424 return(NIL); 425 if (ap->class != TYPE) 426 ap = ap->type; 427 if (ap->value[ NL_ELABEL ] != 0) { 428 /* the list already exists */ 429 return( ap -> value[ NL_ELABEL ] ); 430 } 431 # ifdef OBJ 432 oldlc = lc; 433 put(2, O_TRA, lc); 434 ap->value[ NL_ELABEL ] = lc; 435 # endif OBJ 436 # ifdef PC 437 # ifdef vax 438 putprintf(" .data", 0); 439 putprintf(" .align 1", 0); 440 # endif vax 441 # ifdef mc68000 442 putprintf(" .data", 0); 443 putprintf(" .even", 0); 444 # endif mc68000 445 ap -> value[ NL_ELABEL ] = getlab(); 446 putlab( ap -> value[ NL_ELABEL ] ); 447 # endif PC 448 /* number of scalars */ 449 next = ap->type; 450 len = next->range[1]-next->range[0]+1; 451 # ifdef OBJ 452 put(2, O_CASE2, len); 453 # endif OBJ 454 # ifdef PC 455 putprintf( " .word %d" , 0 , len ); 456 # endif PC 457 /* offsets of each scalar name */ 458 len = (len+1)*sizeof(short); 459 # ifdef OBJ 460 put(2, O_CASE2, len); 461 # endif OBJ 462 # ifdef PC 463 putprintf( " .word %d" , 0 , len ); 464 # endif PC 465 next = ap->chain; 466 do { 467 for(strptr = next->symbol; *strptr++; len++) 468 continue; 469 len++; 470 # ifdef OBJ 471 put(2, O_CASE2, len); 472 # endif OBJ 473 # ifdef PC 474 putprintf( " .word %d" , 0 , len ); 475 # endif PC 476 } while (next = next->chain); 477 /* list of scalar names */ 478 strptr = getnext(ap, &next); 479 # ifdef OBJ 480 do { 481 # ifdef DEC11 482 w = (unsigned) *strptr; 483 # else 484 w = *strptr << 8; 485 # endif DEC11 486 if (!*strptr++) 487 strptr = getnext(next, &next); 488 # ifdef DEC11 489 w |= *strptr << 8; 490 # else 491 w |= (unsigned) *strptr; 492 # endif DEC11 493 if (!*strptr++) 494 strptr = getnext(next, &next); 495 word(w); 496 } while (next); 497 /* jump over the mess */ 498 patch(oldlc); 499 # endif OBJ 500 # ifdef PC 501 while ( next ) { 502 while ( *strptr ) { 503 putprintf( " .byte 0%o" , 1 , *strptr++ ); 504 for ( w = 2 ; ( w <= 8 ) && *strptr ; w ++ ) { 505 putprintf( ",0%o" , 1 , *strptr++ ); 506 } 507 putprintf( "" , 0 ); 508 } 509 putprintf( " .byte 0" , 0 ); 510 strptr = getnext( next , &next ); 511 } 512 putprintf( " .text" , 0 ); 513 # endif PC 514 return( ap -> value[ NL_ELABEL ] ); 515 } 516 517 getnext(next, new) 518 519 struct nl *next, **new; 520 { 521 if (next != NIL) { 522 next = next->chain; 523 *new = next; 524 } 525 if (next == NIL) 526 return(""); 527 #ifdef OBJ 528 if (opt('k') && CGENNING ) 529 printf("%5d\t\t\"%s\"\n", lc-HEADER_BYTES, next->symbol); 530 #endif OBJ 531 return(next->symbol); 532 } 533 534 #ifdef OBJ 535 /* 536 * Putspace puts out a table 537 * of nothing to leave space 538 * for the case branch table e.g. 539 */ 540 putspace(n) 541 int n; 542 { 543 register i; 544 545 if ( !CGENNING ) 546 /* 547 * code disabled - do nothing 548 */ 549 return(lc); 550 #ifdef DEBUG 551 if (opt('k')) 552 printf("%5d\t.=.+%d\n", lc - HEADER_BYTES, n); 553 #endif 554 for (i = even(n); i > 0; i -= 2) 555 word(0); 556 } 557 558 putstr(sptr, padding) 559 560 char *sptr; 561 int padding; 562 { 563 register unsigned short w; 564 register char *strptr = sptr; 565 register int pad = padding; 566 567 if ( !CGENNING ) 568 /* 569 * code disabled - do nothing 570 */ 571 return(lc); 572 #ifdef DEBUG 573 if (opt('k')) 574 printf("%5d\t\t\"%s\"\n", lc-HEADER_BYTES, strptr); 575 #endif 576 if (pad == 0) { 577 do { 578 # ifdef DEC11 579 w = (unsigned short) * strptr; 580 # else 581 w = (unsigned short)*strptr<<8; 582 # endif DEC11 583 if (w) 584 # ifdef DEC11 585 w |= *++strptr << 8; 586 # else 587 w |= *++strptr; 588 # endif DEC11 589 word(w); 590 } while (*strptr++); 591 } else { 592 # ifdef DEC11 593 do { 594 w = (unsigned short) * strptr; 595 if (w) { 596 if (*++strptr) 597 w |= *strptr << 8; 598 else { 599 w |= ' \0'; 600 pad--; 601 } 602 word(w); 603 } 604 } while (*strptr++); 605 # else 606 do { 607 w = (unsigned short)*strptr<<8; 608 if (w) { 609 if (*++strptr) 610 w |= *strptr; 611 else { 612 w |= ' '; 613 pad--; 614 } 615 word(w); 616 } 617 } while (*strptr++); 618 # endif DEC11 619 while (pad > 1) { 620 word(' '); 621 pad -= 2; 622 } 623 if (pad == 1) 624 # ifdef DEC11 625 word(' '); 626 # else 627 word(' \0'); 628 # endif DEC11 629 else 630 word(0); 631 } 632 } 633 #endif OBJ 634 635 lenstr(sptr, padding) 636 637 char *sptr; 638 int padding; 639 640 { 641 register int cnt; 642 register char *strptr = sptr; 643 644 cnt = padding; 645 do { 646 cnt++; 647 } while (*strptr++); 648 return((++cnt) & ~1); 649 } 650 651 /* 652 * Patch repairs the branch 653 * at location loc to come 654 * to the current location. 655 * for PC, this puts down the label 656 * and the branch just references that label. 657 * lets here it for two pass assemblers. 658 */ 659 patch(loc) 660 { 661 662 # ifdef OBJ 663 patchfil(loc, (long)(lc-loc-2), 1); 664 # endif OBJ 665 # ifdef PC 666 putlab( loc ); 667 # endif PC 668 } 669 670 #ifdef OBJ 671 patch4(loc) 672 { 673 patchfil(loc, (long)(lc - HEADER_BYTES), 2); 674 } 675 676 /* 677 * Patchfil makes loc+2 have jmploc 678 * as its contents. 679 */ 680 patchfil(loc, jmploc, words) 681 PTR_DCL loc; 682 long jmploc; 683 int words; 684 { 685 register i; 686 int val; 687 688 if ( !CGENNING ) 689 return; 690 if (loc > (unsigned) lc) 691 panic("patchfil"); 692 #ifdef DEBUG 693 if (opt('k')) 694 printf("\tpatch %u %D\n", loc - HEADER_BYTES, jmploc); 695 #endif 696 val = jmploc; 697 do { 698 # ifndef DEC11 699 if (words > 1) 700 val = jmploc >> 16; 701 else 702 val = jmploc; 703 # endif DEC11 704 i = ((unsigned) loc + 2 - ((unsigned) lc & ~01777))/2; 705 if (i >= 0 && i < 1024) 706 obuf[i] = val; 707 else { 708 lseek(ofil, (long) loc+2, 0); 709 write(ofil, &val, 2); 710 lseek(ofil, (long) 0, 2); 711 } 712 loc += 2; 713 # ifdef DEC11 714 val = jmploc >> 16; 715 # endif DEC11 716 } while (--words); 717 } 718 719 /* 720 * Put the word o into the code 721 */ 722 word(o) 723 int o; 724 { 725 726 *obufp = o; 727 obufp++; 728 lc += 2; 729 if (obufp >= obuf+512) 730 pflush(); 731 } 732 733 extern char *obj; 734 /* 735 * Flush the code buffer 736 */ 737 pflush() 738 { 739 register i; 740 741 i = (obufp - ( ( short * ) obuf ) ) * 2; 742 if (i != 0 && write(ofil, obuf, i) != i) 743 perror(obj), pexit(DIED); 744 obufp = obuf; 745 } 746 #endif OBJ 747 748 /* 749 * Getlab - returns the location counter. 750 * included here for the eventual code generator. 751 * for PC, thank you! 752 */ 753 getlab() 754 { 755 # ifdef OBJ 756 757 return (lc); 758 # endif OBJ 759 # ifdef PC 760 static long lastlabel; 761 762 return ( ++lastlabel ); 763 # endif PC 764 } 765 766 /* 767 * Putlab - lay down a label. 768 * for PC, just print the label name with a colon after it. 769 */ 770 putlab(l) 771 int l; 772 { 773 774 # ifdef PC 775 putprintf( PREFIXFORMAT , 1 , LABELPREFIX , l ); 776 putprintf( ":" , 0 ); 777 # endif PC 778 return (l); 779 } 780