⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 analyze.c

📁 本程序集是Allen I. Holub所写的《Compiler Design in C》一书的附随软件
💻 C
📖 第 1 页 / 共 2 页
字号:
                                scanexpr(block->exp,0,0);
                                break;
                        case st_while:
                        case st_do:
                                opt4(&block->exp);
                                scanexpr(block->exp,0,0);
                                scan(block->s1);
                                break;
                        case st_for:
                                opt4(&block->label);
                                scanexpr(block->label,0,0);
                                opt4(&block->exp);
                                scanexpr(block->exp,0,0);
                                scan(block->s1);
                                opt4(&block->s2);
                                scanexpr(block->s2,0,0);
                                break;
                        case st_if:
                                opt4(&block->exp);
                                scanexpr(block->exp,0,0);
                                scan(block->s1);
                                scan(block->s2);
                                break;
                        case st_switch:
                                opt4(&block->exp);
                                scanexpr(block->exp,0,0);
                                scan(block->s1);
                                break;
                        case st_case:
                                scan(block->s1);
                                break;
												case st_block:
																scan(block->exp);
																break;
												case st_asm:
																if (block->exp)
																	asm_scan(block->exp);
																break;
                        }
                block = block->next;
                }
}

void exchange(CSE **c1)
/*
 *      exchange will exchange the order of two expression entries
 *      following c1 in the linked list.
 */
{       CSE      *csp1, *csp2;
        csp1 = *c1;
        csp2 = csp1->next;
        csp1->next = csp2->next;
        csp2->next = csp1;
        *c1 = csp2;
}

int     desire(CSE *csp)
/*
 *      returns the desirability of optimization for a subexpression.
 */
{       if( csp->voidf || (isintconst(csp->exp->nodetype) &&
                        csp->exp->v.i < 16 && csp->exp->v.i >= 0))
                return 0;
        if( lvalue(csp->exp) )
                return 2 * csp->uses;
        return csp->uses;
}

int     bsort(CSE **list)
/*
 *      bsort implements a bubble sort on the expression list.
 */
{       CSE      *csp1, *csp2;
        csp1 = *list;
        if( csp1 == 0 || csp1->next == 0 )
                return 0;
        bsort( &(csp1->next));
        csp2 = csp1->next;
        if( desire(csp1) < desire(csp2) ) {
                exchange(list);
                return 1;
                }
        return 0;
}

void repexpr(ENODE *node, int size)
/*
 *      repexpr will replace all allocated references within an expression
 *      with tempref nodes.
 */
{       CSE      *csp;
        if( node == 0 )
                return;
				if (size == 0)
					size = natural_size(node);
        switch( node->nodetype ) {
                case en_rcon: case en_lrcon: case en_fcon:
                case en_icon:
								case en_lcon:
								case en_iucon:
								case en_lucon:
								case en_ccon:
                case en_nacon:
                case en_napccon:
								case en_absacon:
                case en_autocon:
								case en_autoreg:
                        if( (csp = searchnode(node)) != 0 )
                                if( csp->reg > 0 ) {
                                        node->nodetype = en_tempref;
                                        node->v.i = csp->reg | (size << 8);
                                        }
                        break;
								case en_floatref:
								case en_doubleref:
								case en_longdoubleref:
                case en_ub_ref:
                case en_uw_ref:
                case en_b_ref:
                case en_w_ref:
                case en_l_ref:
                case en_ul_ref:
                        if( (csp = searchnode(node)) != 0 ) {
                                if( csp->reg > 0 ) {
                                        node->nodetype = en_tempref;
                                        node->v.i = csp->reg | (size << 8);
                                        }
                                else
                                        repexpr(node->v.p[0],0);
                                }
                        else
                                repexpr(node->v.p[0],0);
                        break;
                case en_uminus: case en_bits:
                case en_not:    case en_compl:
                case en_ainc:   case en_adec:
                        repexpr(node->v.p[0],0);
                        break;
								case en_cb: case en_cub:
								case en_cw: case en_cuw:
								case en_cl: case en_cul:
								case en_cf: case en_cd: case en_cp: case en_cld:
                        repexpr(node->v.p[0],natural_size(node));
                        break;
                case en_add:    case en_sub:
                case en_umul:    case en_udiv: case en_umod:
                case en_mul:    case en_div:
                case en_mod:    case en_lsh:
								case en_asalsh: case en_asarsh: case en_alsh: case en_arsh:
                case en_rsh:    case en_and:
                case en_or:     case en_xor:
                case en_land:   case en_lor:
                case en_eq:     case en_ne:
                case en_lt:     case en_le:
								case en_ugt:	case en_uge: case en_ult: case en_ule:
                case en_gt:     case en_ge:
                case en_cond:   case en_void:
                case en_asadd:  case en_assub:
                case en_asmul:  case en_asdiv:
                case en_asor:   case en_asand:   case en_asxor:
                case en_asmod:  case en_aslsh:
								case en_asumod: case en_asudiv: case en_asumul: case en_pmul:
                case en_asrsh:  case en_fcall: case en_trapcall: case en_pdiv:
								case en_pfcall: case en_pfcallb:
                case en_assign: case en_intcall: case en_fcallb: case en_refassign:
                case en_moveblock: case en_stackblock: case en_callblock:
								case en_pcallblock:
                        repexpr(node->v.p[0],0);
                        repexpr(node->v.p[1],0);
                        break;
                }
}

void repcse(SNODE *block)
/*
 *      repcse will scan through a block of statements replacing the
 *      optimized expressions with their temporary references.
 */
{       while( block != 0 ) {
                switch( block->stype ) {
                        case st_return:
                        case st_expr:
                                repexpr(block->exp,0);
                                break;
                        case st_while:
                        case st_do:
                                repexpr(block->exp,0);
                                repcse(block->s1);
                                break;
                        case st_for:
                                repexpr(block->label,0);
                                repexpr(block->exp,0);
                                repcse(block->s1);
                                repexpr(block->s2,0);
                                break;
                        case st_if:
                                repexpr(block->exp,0);
                                repcse(block->s1);
                                repcse(block->s2);
                                break;
                        case st_switch:
                                repexpr(block->exp,0);
                                repcse(block->s1);
                                break;
                        case st_case:
                                repcse(block->s1);
                                break;
												case st_block:
																repcse(block->exp);
																break;
												case st_asm:
																if (block->exp)
																	asm_repcse(block->exp);
																break;
                        }
                block = block->next;
                }
}
void allocstack(void)
/*
 * This allocates space for local variables
 * we do this AFTER the register optimization so that we can
 * avoid allocating space on the stack twice
 */
{
	int lvl = 0;
	int again = TRUE;
	int oldauto, max;
	lc_maxauto = max = 0;
	while (again) {
		LIST *t = varlisthead;
		int align;
		lvl ++;
		oldauto = max;
		again = FALSE;
		while (t) {
			SYM *sp = t->data;
			while (sp && (sp->inreg || sp->funcparm || sp->storage_class == sc_static || sp->storage_class == sc_label || sp->storage_class == sc_ulabel))
				sp = sp->next;
			if (!sp) {
				t = t->link;
				continue;
			}
			if (sp->value.i >= lvl)
				again = TRUE;
			if (sp->value.i == lvl) {
				lc_maxauto = oldauto;
				while (sp) {
					if (!sp->inreg && !sp->funcparm && sp->storage_class != sc_static) {
						int val;
						align = getalign(sc_auto,sp->tp);
						val = lc_maxauto % align;
						if (val != 0)
							lc_maxauto += align - val;
						lc_maxauto += sp->tp->size;
						sp->value.i = -lc_maxauto;
					}
					sp = sp->next;
				}
			}
			if (lc_maxauto > max)
				max = lc_maxauto;
			t = t->link;
		}
	}
	lc_maxauto = max;
	lc_maxauto += stackadd;
	lc_maxauto &= stackmod;
}
void opt1(SNODE *block)
/*
 *      opt1 is the externally callable optimization routine. it will
 *      collect and allocate common subexpressions and substitute the
 *      tempref for all occurrances of the expression within the block.
 *
 *		optimizer is currently turned off...
 */
{
		int datareg = cf_freedata;
		int addreg = 16 + cf_freeaddress;
		int floatreg = 32 + cf_freefloat;
		olist = 0;
        scan(block);            /* collect expressions */
				voidall();							/* Disallow regs whose address is taken  */
				voidfloat(block);						/* disallow optimizing things which are assigned values derived from floats */
				reserveregs(&datareg, &addreg, &floatreg);		/* Allocate register vars */
        allocate(datareg, addreg,floatreg,block);             /* allocate registers  for opt*/
        repcse(block);          /* replace allocated expressions */
				loadregs();						/* Initialize allocated regs */
}

⌨️ 快捷键说明

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