xref: /csrg-svn/usr.bin/pascal/src/p2put.c (revision 762)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static	char sccsid[] = "@(#)p2put.c 1.1 08/27/80";
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 ( cgenflg )
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 ( cgenflg )
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 ( cgenflg )
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 | %d	"
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 | %d	"
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 | %d\n" , rval , type );
203 		    }
204 #		endif
205 		break;
206 	}
207     }
208 
209     /*
210      *	rvalues are just lvalues with indirection, except
211      *	special case for named globals, whose names are their rvalues
212      */
213 putRV( name , level , offset , type )
214     char	*name;
215     int		level;
216     int		offset;
217     int		type;
218     {
219 	char	extname[ BUFSIZ ];
220 	char	*printname;
221 
222 	if ( cgenflg )
223 	    return;
224 	if ( ( level <= 1 ) && ( name != 0 ) ) {
225 	    if ( name[0] != '_' ) {
226 		    sprintf( extname , EXTFORMAT , name );
227 		    printname = extname;
228 	    } else {
229 		    printname = name;
230 	    }
231 	    putleaf( P2NAME , offset , 0 , type , printname );
232 	    return;
233 	}
234 	putLV( name , level , offset , type );
235 	putop( P2UNARY P2MUL , type );
236     }
237 
238     /*
239      *	put out an lvalue
240      *	given a level and offset
241      *	special case for
242      *	    named globals, whose lvalues are just their names as constants.
243      *	    negative offsets, that are offsets from the frame pointer.
244      *	    positive offsets, that are offsets from argument pointer.
245      */
246 putLV( name , level , offset , type )
247     char	*name;
248     int		level;
249     int		offset;
250     int		type;
251     {
252 	char		extname[ BUFSIZ ];
253 	char		*printname;
254 
255 	if ( cgenflg )
256 	    return;
257 	if ( ( level <= 1 ) && ( name != 0 ) ) {
258 	    if ( name[0] != '_' ) {
259 		    sprintf( extname , EXTFORMAT , name );
260 		    printname = extname;
261 	    } else {
262 		    printname = name;
263 	    }
264 	    putleaf( P2ICON , offset , 0 , ADDTYPE( type , P2PTR )
265 		    , printname );
266 	    return;
267 	}
268 	if ( level == cbn ) {
269 		if ( offset < 0 ) {
270 		    putleaf( P2REG , 0 , P2FP , ADDTYPE( type , P2PTR ) , 0 );
271 		} else {
272 		    putleaf( P2REG , 0 , P2AP , ADDTYPE( type , P2PTR ) , 0 );
273 		}
274 	} else {
275 		if ( offset < 0 ) {
276 			putleaf( P2NAME
277 			    , ( level * sizeof(struct dispsave) ) + FP_OFFSET
278 			    , 0 , P2PTR | P2CHAR , DISPLAYNAME );
279 		} else {
280 			putleaf( P2NAME
281 			    , ( level * sizeof(struct dispsave) ) + AP_OFFSET
282 			    , 0 , P2PTR | P2CHAR , DISPLAYNAME );
283 		}
284 	}
285 	if ( offset < 0 ) {
286 		putleaf( P2ICON , -offset , 0 , P2INT , 0 );
287 		putop( P2MINUS , P2PTR | P2CHAR );
288 	} else {
289 		putleaf( P2ICON , offset , 0 , P2INT , 0 );
290 		putop( P2PLUS , P2PTR | P2CHAR );
291 	}
292 	return;
293     }
294 
295     /*
296      *	put out a floating point constant leaf node
297      *	the constant is declared in aligned data space
298      *	and a P2NAME leaf put out for it
299      */
300 putCON8( value )
301     double	value;
302     {
303 	int	label;
304 	char	name[ BUFSIZ ];
305 
306 	if ( cgenflg )
307 	    return;
308 	putprintf( "	.data" , 0 );
309 	putprintf( "	.align 2" , 0 );
310 	label = getlab();
311 	putlab( label );
312 	putprintf( "	.double 0d%.20e" , 0 , value );
313 	putprintf( "	.text" , 0 );
314 	sprintf( name , PREFIXFORMAT , LABELPREFIX , label );
315 	putleaf( P2NAME , 0 , 0 , P2DOUBLE , name );
316     }
317 
318 	/*
319 	 * put out either an lvalue or an rvalue for a constant string.
320 	 * an lvalue (for assignment rhs's) is the name as a constant,
321 	 * an rvalue (for parameters) is just the name.
322 	 */
323 putCONG( string , length , required )
324     char	*string;
325     int		length;
326     int		required;
327     {
328 	char	name[ BUFSIZ ];
329 	int	label;
330 	char	*cp;
331 	int	pad;
332 	int	others;
333 
334 	if ( cgenflg )
335 	    return;
336 	putprintf( "	.data" , 0 );
337 	label = getlab();
338 	putlab( label );
339 	cp = string;
340 	while ( *cp ) {
341 	    putprintf( "	.byte	0%o" , 1 , *cp ++ );
342 	    for ( others = 2 ; ( others <= 8 ) && *cp ; others ++ ) {
343 		putprintf( ",0%o" , 1 , *cp++ );
344 	    }
345 	    putprintf( "" , 0 );
346 	}
347 	pad = length - strlen( string );
348 	while ( pad-- > 0 ) {
349 	    putprintf( "	.byte	0%o" , 1 , ' ' );
350 	    for ( others = 2 ; ( others <= 8 ) && ( pad-- > 0 ) ; others++ ) {
351 		putprintf( ",0%o" , 1 , ' ' );
352 	    }
353 	    putprintf( "" , 0 );
354 	}
355 	putprintf( "	.byte	0" , 0 );
356 	putprintf( "	.text"  , 0 );
357 	sprintf( name , PREFIXFORMAT , LABELPREFIX , label );
358 	if ( required == RREQ ) {
359 	    putleaf( P2NAME , 0 , 0 , P2ARY | P2CHAR , name );
360 	} else {
361 	    putleaf( P2ICON , 0 , 0 , P2PTR | P2CHAR , name );
362 	}
363     }
364 
365     /*
366      *	map a pascal type to a c type
367      *	this would be tail recursive, but i unfolded it into a for (;;).
368      *	this is sort of like isa and lwidth
369      *	a note on the types used by the portable c compiler:
370      *	    they are divided into a basic type (char, short, int, long, etc.)
371      *	    and qualifications on those basic types (pointer, function, array).
372      *	    the basic type is kept in the low 4 bits of the type descriptor,
373      *	    and the qualifications are arranged in two bit chunks, with the
374      *	    most significant on the right,
375      *	    and the least significant on the left
376      *		e.g. int *foo();
377      *			(a function returning a pointer to an integer)
378      *		is stored as
379      *		    <ptr><ftn><int>
380      *	so, we build types recursively
381      */
382 int
383 p2type( np )
384     struct nl *np;
385     {
386 
387 	if ( np == NIL )
388 	    return P2UNDEFINED;
389 	switch ( np -> class ) {
390 	    case SCAL :
391 	    case RANGE :
392 		if ( np -> type == ( nl + TDOUBLE ) ) {
393 		    return P2DOUBLE;
394 		}
395 		switch ( bytes( np -> range[0] , np -> range[1] ) ) {
396 		    case 1:
397 			return P2CHAR;
398 		    case 2:
399 			return P2SHORT;
400 		    case 4:
401 			return P2INT;
402 		    default:
403 			panic( "p2type int" );
404 		}
405 	    case STR :
406 		return ( P2ARY | P2CHAR );
407 		/*
408 		return P2STRTY;
409 		*/
410 	    case RECORD :
411 	    case SET :
412 		return P2STRTY;
413 	    case FILET :
414 		return ( P2PTR | P2STRTY );
415 	    case CONST :
416 	    case VAR :
417 	    case FIELD :
418 		return p2type( np -> type );
419 	    case TYPE :
420 		switch ( nloff( np ) ) {
421 		    case TNIL :
422 			return ( P2PTR | P2UNDEFINED );
423 		    case TSTR :
424 			return ( P2ARY | P2CHAR );
425 			/*
426 			return P2STRTY;
427 			*/
428 		    case TSET :
429 			return P2STRTY;
430 		    default :
431 			return ( p2type( np -> type ) );
432 		}
433 	    case REF:
434 	    case WITHPTR:
435 	    case PTR :
436 		return ADDTYPE( p2type( np -> type ) , P2PTR );
437 	    case ARRAY :
438 		return ADDTYPE( p2type( np -> type ) , P2ARY );
439 		/*
440 		return P2STRTY;
441 		*/
442 	    case FUNC :
443 		    /*
444 		     * functions are really pointers to functions
445 		     * which return their underlying type.
446 		     */
447 		return ADDTYPE( ADDTYPE( p2type( np -> type ) , P2FTN )
448 				, P2PTR );
449 	    case PROC :
450 		    /*
451 		     * procedures are pointers to functions
452 		     * which return integers (whether you look at them or not)
453 		     */
454 		return ADDTYPE( ADDTYPE( P2INT , P2FTN ) , P2PTR );
455 	    default :
456 		fprintf( stderr , "[p2type] np -> class %d\n" , np -> class );
457 		panic( "p2type" );
458 	}
459     }
460 
461     /*
462      *	add a most significant type modifier to a type
463      */
464 long
465 addtype( underlying , mtype )
466     long	underlying;
467     long	mtype;
468     {
469 	return ( ( ( underlying & ~P2BASETYPE ) << P2TYPESHIFT )
470 	       | mtype
471 	       | ( underlying & P2BASETYPE ) );
472     }
473 
474     /*
475      *	put a typed operator to the pcstream
476      */
477 putop( op , type )
478     int		op;
479     int		type;
480     {
481 	extern char	*p2opnames[];
482 
483 	if ( cgenflg )
484 	    return;
485 	p2word( TOF77( op , 0 , type ) );
486 #	ifdef DEBUG
487 	    if ( opt( 'k' ) ) {
488 		fprintf( stdout , "%s (%d) |   0 | %d\n"
489 			, p2opnames[ op ] , op , type );
490 	    }
491 #	endif
492     }
493 
494     /*
495      *	put out a structure operator (STASG, STARG, STCALL, UNARY STCALL )
496      *	which looks just like a regular operator, only the size and
497      *	alignment go in the next consecutive words
498      */
499 putstrop( op , type , size , alignment )
500     int	op;
501     int	type;
502     int	size;
503     int	alignment;
504     {
505 	extern char	*p2opnames[];
506 
507 	if ( cgenflg )
508 	    return;
509 	p2word( TOF77( op , 0 , type ) );
510 	p2word( size );
511 	p2word( alignment );
512 #	ifdef DEBUG
513 	    if ( opt( 'k' ) ) {
514 		fprintf( stdout , "%s (%d) |   0 | %d	%d %d\n"
515 			, p2opnames[ op ] , op , type , size , alignment );
516 	    }
517 #	endif
518     }
519 
520     /*
521      *	the string names of p2ops
522      */
523 char	*p2opnames[] = {
524 	"",
525 	"P2UNDEFINED",		/* 1 */
526 	"P2NAME",		/* 2 */
527 	"P2STRING",		/* 3 */
528 	"P2ICON",		/* 4 */
529 	"P2FCON",		/* 5 */
530 	"P2PLUS",		/* 6 */
531 	"",
532 	"P2MINUS",		/* 8		also unary == P2NEG */
533 	"",
534 	"P2NEG",
535 	"P2MUL",		/* 11		also unary == P2INDIRECT */
536 	"",
537 	"P2INDIRECT",
538 	"P2AND",		/* 14		also unary == P2ADDROF */
539 	"",
540 	"P2ADDROF",
541 	"P2OR",			/* 17 */
542 	"",
543 	"P2ER",			/* 19 */
544 	"",
545 	"P2QUEST",		/* 21 */
546 	"P2COLON",		/* 22 */
547 	"P2ANDAND",		/* 23 */
548 	"P2OROR",		/* 24 */
549 	"",			/* 25 */
550 	"",			/* 26 */
551 	"",			/* 27 */
552 	"",			/* 28 */
553 	"",			/* 29 */
554 	"",			/* 30 */
555 	"",			/* 31 */
556 	"",			/* 32 */
557 	"",			/* 33 */
558 	"",			/* 34 */
559 	"",			/* 35 */
560 	"",			/* 36 */
561 	"",			/* 37 */
562 	"",			/* 38 */
563 	"",			/* 39 */
564 	"",			/* 40 */
565 	"",			/* 41 */
566 	"",			/* 42 */
567 	"",			/* 43 */
568 	"",			/* 44 */
569 	"",			/* 45 */
570 	"",			/* 46 */
571 	"",			/* 47 */
572 	"",			/* 48 */
573 	"",			/* 49 */
574 	"",			/* 50 */
575 	"",			/* 51 */
576 	"",			/* 52 */
577 	"",			/* 53 */
578 	"",			/* 54 */
579 	"",			/* 55 */
580 	"P2LISTOP",		/* 56 */
581 	"",
582 	"P2ASSIGN",		/* 58 */
583 	"P2COMOP",		/* 59 */
584 	"P2DIV",		/* 60 */
585 	"",
586 	"P2MOD",		/* 62 */
587 	"",
588 	"P2LS",			/* 64 */
589 	"",
590 	"P2RS",			/* 66 */
591 	"",
592 	"P2DOT",		/* 68 */
593 	"P2STREF",		/* 69 */
594 	"P2CALL",		/* 70		also unary */
595 	"",
596 	"P2UNARYCALL",
597 	"P2FORTCALL",		/* 73		also unary */
598 	"",
599 	"P2UNARYFORTCALL",
600 	"P2NOT",		/* 76 */
601 	"P2COMPL",		/* 77 */
602 	"P2INCR",		/* 78 */
603 	"P2DECR",		/* 79 */
604 	"P2EQ",			/* 80 */
605 	"P2NE",			/* 81 */
606 	"P2LE",			/* 82 */
607 	"P2LT",			/* 83 */
608 	"P2GE",			/* 84 */
609 	"P2GT",			/* 85 */
610 	"P2ULE",		/* 86 */
611 	"P2ULT",		/* 87 */
612 	"P2UGE",		/* 88 */
613 	"P2UGT",		/* 89 */
614 	"P2SETBIT",		/* 90 */
615 	"P2TESTBIT",		/* 91 */
616 	"P2RESETBIT",		/* 92 */
617 	"P2ARS",		/* 93 */
618 	"P2REG",		/* 94 */
619 	"P2OREG",		/* 95 */
620 	"P2CCODES",		/* 96 */
621 	"P2FREE",		/* 97 */
622 	"P2STASG",		/* 98 */
623 	"P2STARG",		/* 99 */
624 	"P2STCALL",		/* 100		also unary */
625 	"",
626 	"P2UNARYSTCALL",
627 	"P2FLD",		/* 103 */
628 	"P2SCONV",		/* 104 */
629 	"P2PCONV",		/* 105 */
630 	"P2PMCONV",		/* 106 */
631 	"P2PVCONV",		/* 107 */
632 	"P2FORCE",		/* 108 */
633 	"P2CBRANCH",		/* 109 */
634 	"P2INIT",		/* 110 */
635 	"P2CAST",		/* 111 */
636     };
637 
638     /*
639      *	low level routines
640      */
641 
642     /*
643      *	puts a long word on the pcstream
644      */
645 p2word( word )
646     long	word;
647     {
648 
649 	putw( word , pcstream );
650     }
651 
652     /*
653      *	put a length 0 mod 4 null padded string onto the pcstream
654      */
655 p2string( string )
656     char	*string;
657     {
658 	int	slen = strlen( string );
659 	int	wlen = ( slen + 3 ) / 4;
660 	int	plen = ( wlen * 4 ) - slen;
661 	char	*cp;
662 	int	p;
663 
664 	for ( cp = string ; *cp ; cp++ )
665 	    putc( *cp , pcstream );
666 	for ( p = 1 ; p <= plen ; p++ )
667 	    putc( '\0' , pcstream );
668 #	ifdef DEBUG
669 	    if ( opt( 'k' ) ) {
670 		fprintf( stdout , "\"%s" , string );
671 		for ( p = 1 ; p <= plen ; p++ )
672 		    fprintf( stdout , "\\0" );
673 		fprintf( stdout , "\"\n" );
674 	    }
675 #	endif
676     }
677 
678     /*
679      *	puts a name on the pcstream
680      */
681 p2name( name )
682     char	*name;
683     {
684 	int	pad;
685 
686 	fprintf( pcstream , NAMEFORMAT , name );
687 	pad = strlen( name ) % sizeof (long);
688 	for ( ; pad < sizeof (long) ; pad++ ) {
689 	    putc( '\0' , pcstream );
690 	}
691 #	ifdef DEBUG
692 	    if ( opt( 'k' ) ) {
693 		fprintf( stdout , NAMEFORMAT , name );
694 		pad = strlen( name ) % sizeof (long);
695 		for ( ; pad < sizeof (long) ; pad++ ) {
696 		    fprintf( stdout , "\\0" );
697 		}
698 		fprintf( stdout , "\n" );
699 	    }
700 #	endif
701     }
702 
703     /*
704      *	put out a jump to a label
705      */
706 putjbr( label )
707     long	label;
708     {
709 
710 	printjbr( LABELPREFIX , label );
711     }
712 
713     /*
714      *	put out a jump to any kind of label
715      */
716 printjbr( prefix , label )
717     char	*prefix;
718     long	label;
719     {
720 
721 	putprintf( "	jbr	" , 1 );
722 	putprintf( PREFIXFORMAT , 0 , prefix , label );
723     }
724 
725     /*
726      *	another version of put to catch calls to put
727      */
728 put( arg1 , arg2 )
729     {
730 
731 	putprintf( "#	PUT CALLED!: arg1 = %d arg2 = 0%o" , 0 , arg1 , arg2 );
732     }
733 
734 #endif PC
735