xref: /original-bsd/usr.bin/pascal/src/put.c (revision f0fd5f8a)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)put.c 1.18 08/29/82";
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 	    putprintf( "	.data" , 0 );
438 	    putprintf( "	.align 1" , 0 );
439 	    ap -> value[ NL_ELABEL ] = getlab();
440 	    putlab( ap -> value[ NL_ELABEL ] );
441 #	endif PC
442 	/* number of scalars */
443 	next = ap->type;
444 	len = next->range[1]-next->range[0]+1;
445 #	ifdef OBJ
446 	    put(2, O_CASE2, len);
447 #	endif OBJ
448 #	ifdef PC
449 	    putprintf( "	.word %d" , 0 , len );
450 #	endif PC
451 	/* offsets of each scalar name */
452 	len = (len+1)*sizeof(short);
453 #	ifdef OBJ
454 	    put(2, O_CASE2, len);
455 #	endif OBJ
456 #	ifdef PC
457 	    putprintf( "	.word %d" , 0 , len );
458 #	endif PC
459 	next = ap->chain;
460 	do	{
461 		for(strptr = next->symbol;  *strptr++;  len++)
462 			continue;
463 		len++;
464 #		ifdef OBJ
465 		    put(2, O_CASE2, len);
466 #		endif OBJ
467 #		ifdef PC
468 		    putprintf( "	.word %d" , 0 , len );
469 #		endif PC
470 	} while (next = next->chain);
471 	/* list of scalar names */
472 	strptr = getnext(ap, &next);
473 #	ifdef OBJ
474 	    do	{
475 #		    ifdef DEC11
476 			w = (unsigned) *strptr;
477 #		    else
478 			w = *strptr << 8;
479 #		    endif DEC11
480 		    if (!*strptr++)
481 			    strptr = getnext(next, &next);
482 #		    ifdef DEC11
483 			w |= *strptr << 8;
484 #		    else
485 			w |= (unsigned) *strptr;
486 #		    endif DEC11
487 		    if (!*strptr++)
488 			    strptr = getnext(next, &next);
489 		    word(w);
490 	    } while (next);
491 	    /* jump over the mess */
492 	    patch(oldlc);
493 #	endif OBJ
494 #	ifdef PC
495 	    while ( next ) {
496 		while ( *strptr ) {
497 		    putprintf( "	.byte	0%o" , 1 , *strptr++ );
498 		    for ( w = 2 ; ( w <= 8 ) && *strptr ; w ++ ) {
499 			putprintf( ",0%o" , 1 , *strptr++ );
500 		    }
501 		    putprintf( "" , 0 );
502 		}
503 		putprintf( "	.byte	0" , 0 );
504 		strptr = getnext( next , &next );
505 	    }
506 	    putprintf( "	.text" , 0 );
507 #	endif PC
508 	return( ap -> value[ NL_ELABEL ] );
509 }
510 
511 getnext(next, new)
512 
513 	struct nl *next, **new;
514 {
515 	if (next != NIL) {
516 		next = next->chain;
517 		*new = next;
518 	}
519 	if (next == NIL)
520 		return("");
521 #ifdef OBJ
522 	if (opt('k') && CGENNING )
523 		printf("%5d\t\t\"%s\"\n", lc-HEADER_BYTES, next->symbol);
524 #endif OBJ
525 	return(next->symbol);
526 }
527 
528 #ifdef OBJ
529 /*
530  * Putspace puts out a table
531  * of nothing to leave space
532  * for the case branch table e.g.
533  */
534 putspace(n)
535 	int n;
536 {
537 	register i;
538 
539 	if ( !CGENNING )
540 		/*
541 		 * code disabled - do nothing
542 		 */
543 		return(lc);
544 #ifdef DEBUG
545 	if (opt('k'))
546 		printf("%5d\t.=.+%d\n", lc - HEADER_BYTES, n);
547 #endif
548 	for (i = even(n); i > 0; i -= 2)
549 		word(0);
550 }
551 
552 putstr(sptr, padding)
553 
554 	char *sptr;
555 	int padding;
556 {
557 	register unsigned short w;
558 	register char *strptr = sptr;
559 	register int pad = padding;
560 
561 	if ( !CGENNING )
562 		/*
563 		 * code disabled - do nothing
564 		 */
565 		return(lc);
566 #ifdef DEBUG
567 	if (opt('k'))
568 		printf("%5d\t\t\"%s\"\n", lc-HEADER_BYTES, strptr);
569 #endif
570 	if (pad == 0) {
571 		do	{
572 #			ifdef DEC11
573 			    w = (unsigned short) * strptr;
574 #			else
575 			    w = (unsigned short)*strptr<<8;
576 #			endif DEC11
577 			if (w)
578 #				ifdef DEC11
579 				    w |= *++strptr << 8;
580 #				else
581 				    w |= *++strptr;
582 #				endif DEC11
583 			word(w);
584 		} while (*strptr++);
585 	} else {
586 #		ifdef DEC11
587 		    do 	{
588 			    w = (unsigned short) * strptr;
589 			    if (w) {
590 				    if (*++strptr)
591 					    w |= *strptr << 8;
592 				    else {
593 					    w |= ' \0';
594 					    pad--;
595 				    }
596 				    word(w);
597 			    }
598 		    } while (*strptr++);
599 #		else
600 		    do 	{
601 			    w = (unsigned short)*strptr<<8;
602 			    if (w) {
603 				    if (*++strptr)
604 					    w |= *strptr;
605 				    else {
606 					    w |= ' ';
607 					    pad--;
608 				    }
609 				    word(w);
610 			    }
611 		    } while (*strptr++);
612 #		endif DEC11
613 		while (pad > 1) {
614 			word('  ');
615 			pad -= 2;
616 		}
617 		if (pad == 1)
618 #			ifdef DEC11
619 			    word(' ');
620 #			else
621 			    word(' \0');
622 #			endif DEC11
623 		else
624 			word(0);
625 	}
626 }
627 #endif OBJ
628 
629 lenstr(sptr, padding)
630 
631 	char *sptr;
632 	int padding;
633 
634 {
635 	register int cnt;
636 	register char *strptr = sptr;
637 
638 	cnt = padding;
639 	do	{
640 		cnt++;
641 	} while (*strptr++);
642 	return((++cnt) & ~1);
643 }
644 
645 /*
646  * Patch repairs the branch
647  * at location loc to come
648  * to the current location.
649  *	for PC, this puts down the label
650  *	and the branch just references that label.
651  *	lets here it for two pass assemblers.
652  */
653 patch(loc)
654 {
655 
656 #	ifdef OBJ
657 	    patchfil(loc, (long)(lc-loc-2), 1);
658 #	endif OBJ
659 #	ifdef PC
660 	    putlab( loc );
661 #	endif PC
662 }
663 
664 #ifdef OBJ
665 patch4(loc)
666 {
667 	patchfil(loc, (long)(lc - HEADER_BYTES), 2);
668 }
669 
670 /*
671  * Patchfil makes loc+2 have jmploc
672  * as its contents.
673  */
674 patchfil(loc, jmploc, words)
675 	PTR_DCL loc;
676 	long jmploc;
677 	int words;
678 {
679 	register i;
680 	int val;
681 
682 	if ( !CGENNING )
683 		return;
684 	if (loc > (unsigned) lc)
685 		panic("patchfil");
686 #ifdef DEBUG
687 	if (opt('k'))
688 		printf("\tpatch %u %D\n", loc - HEADER_BYTES, jmploc);
689 #endif
690 	val = jmploc;
691 	do {
692 #		ifndef DEC11
693 		    if (words > 1)
694 			    val = jmploc >> 16;
695 		    else
696 			    val = jmploc;
697 #		endif DEC11
698 		i = ((unsigned) loc + 2 - ((unsigned) lc & ~01777))/2;
699 		if (i >= 0 && i < 1024)
700 			obuf[i] = val;
701 		else {
702 			lseek(ofil, (long) loc+2, 0);
703 			write(ofil, &val, 2);
704 			lseek(ofil, (long) 0, 2);
705 		}
706 		loc += 2;
707 #		ifdef DEC11
708 		    val = jmploc >> 16;
709 #		endif DEC11
710 	} while (--words);
711 }
712 
713 /*
714  * Put the word o into the code
715  */
716 word(o)
717 	int o;
718 {
719 
720 	*obufp = o;
721 	obufp++;
722 	lc += 2;
723 	if (obufp >= obuf+512)
724 		pflush();
725 }
726 
727 extern char	*obj;
728 /*
729  * Flush the code buffer
730  */
731 pflush()
732 {
733 	register i;
734 
735 	i = (obufp - ( ( short * ) obuf ) ) * 2;
736 	if (i != 0 && write(ofil, obuf, i) != i)
737 		perror(obj), pexit(DIED);
738 	obufp = obuf;
739 }
740 #endif OBJ
741 
742 /*
743  * Getlab - returns the location counter.
744  * included here for the eventual code generator.
745  *	for PC, thank you!
746  */
747 getlab()
748 {
749 #	ifdef OBJ
750 
751 	    return (lc);
752 #	endif OBJ
753 #	ifdef PC
754 	    static long	lastlabel;
755 
756 	    return ( ++lastlabel );
757 #	endif PC
758 }
759 
760 /*
761  * Putlab - lay down a label.
762  *	for PC, just print the label name with a colon after it.
763  */
764 putlab(l)
765 	int l;
766 {
767 
768 #	ifdef PC
769 	    putprintf( PREFIXFORMAT , 1 , LABELPREFIX , l );
770 	    putprintf( ":" , 0 );
771 #	endif PC
772 	return (l);
773 }
774