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