emit.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 814 行 · 第 1/2 页

C
814
字号
#ifndef lintstatic	char	*sccsid = "@(#)emit.c	4.1	(ULTRIX)	7/17/90";#endif lint/************************************************************************ *									* *			Copyright (c) 1986 by				* *		Digital Equipment Corporation, Maynard, MA		* *			All rights reserved.				* *									* *   This software is furnished under a license and may be used and	* *   copied  only  in accordance with the terms of such license and	* *   with the  inclusion  of  the  above  copyright  notice.   This	* *   software  or  any  other copies thereof may not be provided or	* *   otherwise made available to any other person.  No title to and	* *   ownership of the software is hereby transferred.			* *									* *   The information in this software is subject to change  without	* *   notice  and should not be construed as a commitment by Digital	* *   Equipment Corporation.						* *									* *   Digital assumes no responsibility for the use  or  reliability	* *   of its software on equipment which is not supplied by Digital.	* *									* ************************************************************************//* * File: 	emit.c * * Pascal to C translator - Emit C code *//*	Modification History: emit.c * * 21-July-87 afd *	Added "case STRINGTY" to output of const values. * */#include <stdio.h>#include "ptoc.h"#ifdef EMITDEBUGint emitdebug = 1;#define printd if (emitdebug) fprintf#define printd10 if (emitdebug >= 10) fprintf#endifextern struct treenode *procindex[MAXLEV];extern int lexlev;/* * Print spaces on the current line. */indent(lexlev)    int lexlev;{    int i;    for (i=0; i < lexlev; i++)	printf("    ");}/*  * Emit C code. */emitcode(tn)    struct treenode *tn;{    register struct stentry *st;    register struct cmtinfo *cptr;    /* switch on treenode type */    if (tn == NULL)	return;    switch (tn->type)	{	case PROGNODE:	    emitcode(tn->firstc);	    break;	case ASSIGNNODE:	    indent(lexlev);	    printf("%s = %s;\n", tn->storewhere, tn->storewhat);	    emitcode(tn->next);	    break;	case BEGINNODE:	    emitbegin(tn);	    break;	case CASENODE:	    emitcase(tn);	    break;	case COMMENTNODE:	    indent(lexlev);	    printf("/*");	    for (cptr = tn->blkcmt; cptr != NULL; cptr = cptr->next)		{		if (cptr != tn->blkcmt)		    printf("\n");		printf("%s", cptr->cmt);		}	    printf("*/\n");	    emitcode(tn->next);	    break;	case FDECLNODE:	    emitproc(tn,1);	    break;	case FORNODE:	    emitfor(tn);	    break;	case GOTONODE:	    emitgoto(tn);	    break;	case IFNODE:	    emitif(tn);	    break;	case LABELNODE:	    printf("L%s:\n", tn->expression);	/* must start w/ a letter */	    emitcode(tn->next);	    break;	case PCALLNODE:	    indent(lexlev);	    printf("%s (%s);\n", tn->stdecl->st_name, tn->expression);	    emitcode(tn->next);	    break;	case PDECLNODE:	    emitproc(tn,0);	    break;	case READNODE:	    indent(lexlev);	    printf("%s\n", tn->expression);	    emitcode(tn->next);	    break;	case REPEATNODE:	    emitrepeat(tn,0);	    break;	case SEMINODE:	    indent(lexlev);	    printf(";\n");	    emitcode(tn->next);	    break;	case WHILENODE:	    emitwhile(tn);	    break;	case WITHNODE:	    emitcode(tn->firstc);	    emitcode(tn->next);	    break;	case WRITENODE:	    indent(lexlev);	    printf("%s\n", tn->expression);	    emitcode(tn->next);	    break;	default:	    emitcode(tn->firstc);	    emitcode(tn->next);	}}emitbegin(tn)    struct treenode *tn;{    struct stentry *st;		/* st entry to emit code for */    struct stentry *stptr;	/* temp pointer */    int i,j;    struct cmtinfo *cptr;    switch (tn->blktype)	{	case DECLBLOCK:	    if (tn->parent->type != PROGNODE)		printf("{\n");	    for (st = tn->firstsym; st != NULL;  st = st->st_link)		{		/*		 * Skip over st entries from include file		 */		if (st->st_emit == 0)		    if (st == tn->lastsym)			break;		    else			continue;		switch (st->st_class)		    {		    case COMTC:			printf("\n/*");			for (cptr = st->st_cmt; cptr != NULL; cptr = cptr->next)			    {			    if (cptr != st->st_cmt)				printf("\n");			    printf("%s", cptr->cmt);			    }			printf("*/\n\n");			break;		    case INCLUDEC:			if (strcmp(st->st_name, "<stdio.h>"))			    printf("#include \"%s\"", st->st_name);			else			    printf("#include %s", st->st_name);			if (st->st_cmt != NULL)			    prstcmt(st->st_cmt);			printf("\n");			break;		    case CONSTC:			emitconst(st);			break;		    case TYPEC:		    case VARC:			emittype(st,0);			/*			 * Skip over enumeration constants and			 * entries of duplicate vars w/ same type			 */			if (st->st_dstruct == UDEFS && st->st_tipe == ENUMTY)			    for (i = 0, j = st->st_numdims; i < j; i++)				{				if (i == 0)				    st = st->st_uptr;				else				    st = st->st_link;				}			for (; st->st_dupvar != NULL; )			    st = st->st_dupvar;			/*			 * If there are any more symbols to print,			 * skip over any union "defines" 			 */			if (st != tn->lastsym)			    for (stptr = st->st_link; stptr != NULL; stptr = stptr->st_link)				{				if (stptr->st_class == DEFINEC)				    st = stptr;				else				    break;				}			break;		    case PROCC:		/* forward stmts */		    case FUNCC:			prtipe(st);			printf("%s();\n",st->st_name);			break;		    default:			fprintf(stderr, "emitcode: class #%d not implemented yet\n",			     st->st_class);		    }	/* end switch on "class" */		if (st == tn->lastsym)		    break;	/* out of for loop (so we don't fall into procs) */		}	    break;	case ELSEBLOCK:	    emitcode(tn->firstc);	    break;	case THENBLOCK:	case SEMIBLOCK:	    indent(lexlev);	    printf("{\n");	    emitcode(tn->firstc);	    indent(lexlev);	    printf("}\n");	    break;	case SUBRBLOCK:	    emitcode(tn->firstc);	    if (tn->parent->stdecl->st_class == FUNCC &&		tn->parent->stdecl->st_funcpar == 0)		printf("    return(v___%s);\n", tn->parent->stdecl->st_name);	    printf("}\n\n");	    break;	case PARAMBLOCK:	    emitparam(tn);	    break;	case NOBLOCK:		/* proc/funct block */	case WITHBLOCK:	    emitcode(tn->firstc);	    break;	}    /* end switch on block type */    emitcode(tn->next);}/* * Emit case stmt.  Switch in C. */emitcase(tn)    struct treenode *tn;{    struct treenode *listtn, *labeltn;    indent(lexlev);    printf("switch (%s)\n", tn->expression);    lexlev++;    indent(lexlev);    printf("{\n");    /*     * loop thru listhead nodes (for case labels)     */    for (listtn = tn->firstc; listtn != NULL; listtn = listtn->next)	{	/* loop thru individual labels for this case section. */	for (labeltn = listtn->firstc; labeltn != NULL; labeltn = labeltn->next)	    {	    indent(lexlev);	    if (strcmp(labeltn->expression,"default"))		printf("case %s:\n", labeltn->expression);	    else		printf("default:\n");	    }	/*	 * Emit stmts hung off last label node.	 */	lexlev++;	emitcode(listtn->lastc->firstc);	indent(lexlev);	printf("break;\n");	lexlev--;	}    indent(lexlev);    printf("}\n");    lexlev--;    emitcode(tn->next);}/* * Emit for stmt. */emitfor(tn)    struct treenode *tn;{    indent(lexlev);    printf("for (%s = %s; %s %s %s; %s%s)\n", tn->variable, tn->initvalue,	tn->variable, (tn->to) ? "<=" : ">=", tn->finalvalue, tn->variable,	(tn->to) ? "++" : "--");    /*     * Do body of for loop     */    lexlev++;    emitcode(tn->firstc);    lexlev--;    emitcode(tn->next);}/* * Emit goto stmt. * Labels must start with a letter, so we stuff in the letter 'L' before * each label. */emitgoto(tn)    struct treenode *tn;{    indent(lexlev);    printf("goto L%s;\n", tn->expression);    emitcode(tn->next);}/* * Emit if stmt. */emitif(tn)    struct treenode *tn;{    indent(lexlev);    printf("if (%s)\n", tn->expression);    /*     * Do 'then' part (true part)     */    lexlev++;    emitcode(tn->firstc);    /*     * See if there is an 'else' part     */    if (tn->lastc != NULL)	{	indent(lexlev - 1);	printf("else\n");	emitcode(tn->lastc);	}    lexlev--;    emitcode(tn->next);}/* * emit paramaters.  First the list of parameters, then their type * declarations. */emitparam(tn)    struct treenode *tn;{    struct stentry *st;    printf("(");    for (st=tn->firstsym; st != NULL; st = st->st_link)	{	printf("%s", st->st_name);	if (st == tn->lastsym)	    break;	else	    printf(", ");

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?