예제 #1
0
파일: func (2).c 프로젝트: jkingxt/compiler
struct InterCodes* translate_Args(struct TreeNode* Args, struct ArgList* arg_list){
	if(strcmp(Args->children->name, "Exp") == 0){
		printf("Args - Exp\n");
		Operand t1 = new_temp();
		struct InterCodes* code1 = translate_Exp(Args->children, t1);
		struct ArgList* temp = (struct ArgList*)malloc(sizeof(struct ArgList));
		temp->op = t1;
		temp->next = arg_list;
		arg_list = temp;
		return code1;
	}
	if(strcmp(Args->children->name, "COMMA") == 0){
		printf("Args - Exp COMMA Args\n");
		Operand t1 = new_temp();
		struct InterCodes* code1 = translate_Exp(Args->children->neighbours->neighbours, t1);
		struct ArgList* temp = (struct ArgList*)malloc(sizeof(struct ArgList));
		temp->op = t1;
		temp->next = arg_list;
		arg_list = temp;
		struct InterCodes* code2 = translate_Args(Args->children, arg_list);

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code2;
		code2->prev = p;

		code2->next = NULL;

		return code1;
	}
}
예제 #2
0
InterCodes translate_Dec(node* dec){
	/*
	*	Dec
	*	|	VarDec 
	*	|	VarDec ASSIGNOP Exp
	*/	
		
	if(dec->child->brother != NULL){
		Operand t1 = new_temp();
		InterCodes codes1 = translate_Exp(dec->child->brother->brother, t1);
		InterCodes codes2 = translate_VarDec(dec->child);
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(0);
		codes3->code->assign.left = new_operand_name(dec->child->node_value);
		codes3->code->assign.right = t1;
		if(codes1 == NULL){
			codes1 = codes2;
		}
		else{
			InterCodes_link(codes1,codes2);
		}
		InterCodes_link(codes1,codes3);
		return codes1;
	}
	else if(dec->child->brother == NULL){
		return translate_VarDec(dec->child);
	}
	return NULL;
}
예제 #3
0
파일: func (2).c 프로젝트: jkingxt/compiler
struct InterCodes* translate_Dec(struct TreeNode* Dec){
	if(strcmp(Dec->children->name, "Exp") == 0){
		printf("Dec - VarDec ASSIGNOP Exp\n");
		Operand t1 = new_temp();
		FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
		lookupTable(Dec->children->neighbours->neighbours->children->value_str,&item,0);

		struct InterCodes* code1 = translate_Exp(Dec->children, t1);
		struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code2->code.kind = ASSIGN_;
		code2->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
		code2->code.u.assign.left->kind = VARIABLE;
		strcpy(code2->code.u.assign.left->u.ID, item->inter_name);
		code2->code.u.assign.right = t1;

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code2;
		code2->prev = p;

		code2->next = NULL;

		return code1;
	}
}
예제 #4
0
InterCodes translate_Args(node* Args,Operand *arg,int* num){
	if(Args->child->brother == NULL){
		InterCodes code1 = InterCodes_init();
		/*if(strcmp(Args->child->child->name,"ID") == 0){
			FieldList p = Findname(Args->child->child->node_value);
			if(p->type->kind!=Int && p->type->kind!=Float){
				Operand op = new_temp();
				Operand re = new_operand_name(p->name);
				code1->code = new_interCode(ASSIGN);
				code1->code->assign.right = re;
				code1->code->assign.left = op;
				return code1;
			}
		}*/
		Operand op = new_temp();
		code1 = translate_Exp(Args->child, op);
		arg[(*num)++] = op; 
		return code1;
	}
	else{
		InterCodes code1 = InterCodes_init();
		if(strcmp(Args->child->child->name,"ID") == 0){
			FieldList p = Findname(Args->child->child->node_value);
			if(p->type->kind!=Int && p->type->kind!=Float){
				Operand op = new_temp();
				Operand re = new_operand_name(p->name);
				code1->code = new_interCode(ASSIGN);
				code1->code->assign.right = re;
				code1->code->assign.left = op;
				arg[(*num)++] = op;
			}
		}
		else{
			Operand op = new_temp();
			code1 = translate_Exp(Args->child, op);
			arg[(*num)++] = op;
		}
		InterCodes code2 = translate_Args(Args,arg,num);
		InterCodes_link(code1,code2);
		return code1;
	}
}
예제 #5
0
InterCodes translate_Struct(node *Exp,Operand place){
	InterCodes code1 = InterCodes_init();
	int size = 0;
	if(strcmp(Exp->child->child->name,"ID") == 0){		//ID1.ID2
		node *ID2 = Exp->child->brother->brother;
		node *ID1 = Exp->child->child;	
		//char typename[20];
		//strcpy(typename,FindStruct(ID1->node_value,ID2->node_value));
		FieldList p = Findname(ID1->node_value);
		p = p->brother;
		while(p!=NULL){
			if(strcmp(p->name,Exp->child->brother->brother->node_value) == 0)
				break;
			size = size + getSize(p);
			p = p->brother;
		}	
		Operand op1 = new_operand_name(ID1->node_value);op1->kind = ADDR_op;
		Operand op2 = new_operand(1,size);
		code1->code = new_interCode(ADDR);
		code1->code->binop.result = place;
		code1->code->binop.op1 = op1;
		code1->code->binop.op2 = op2;
		//place->kind = ADDR_op;
		return code1;
	}
	else if(strcpy(Exp->child->child->name,"Exp") == 0){
		InterCodes code2 = InterCodes_init();
		node *ID1 = Exp->child;
		Operand t1 = new_temp();
		code1 = translate_Struct(Exp->child, t1);
		FieldList p = Findname(ID1->child->node_value);	
		p = p->brother;
		while(p!=NULL){
			if(strcmp(p->name,Exp->child->brother->brother->node_value) == 0)
				break;
			size = size + getSize(p);
			p = p->brother;
		}
		Operand op1 = new_operand_name(ID1->child->node_value);op1->kind = ADDR_op;
		Operand op2 = new_operand(1,size);
		code1->code = new_interCode(ADDR);
		code2->code->binop.result = place;
		code2->code->binop.op1 = op1;
		code2->code->binop.op2 = op2;
		InterCodes_link(code1,code2);
		//place->kind = ADDR_op;
		return code1;
	}
}
예제 #6
0
void intercode_aly(node *p){		
	char name[20];
	strcpy(name,p->name);
	if (p == NULL){
		return ;
	}
	if(strcmp(name,"ExtDef")==0){
		InterCodes codes = translate_Extdef(p);
		add_to_head(codes);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;
	}
	else if(strcmp(name,"Exp")==0){
		Operand t1 = new_temp();
		InterCodes expe = translate_Exp(p,t1);
		add_to_head(expe);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;
	}
	else if(strcmp(name,"CompSt")==0){/*
		InterCodes codes =	translate_Compst(p);
		add_to_head(codes);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;*/
	}
	else if(strcmp(name,"Stmt")==0){
		InterCodes codes = translate_Stmt(p);
		add_to_head(codes);
		if(p->brother != NULL)
			intercode_aly(p->brother);
		return;
	}

	if(p->child != NULL)
		intercode_aly(p->child);
	if(p->brother != NULL)
		intercode_aly(p->brother);
	return;
}
예제 #7
0
void	CCodeList_Maker::WriteToAddress(PINSTR p)
{	//	这个函数的意思是,如果是向一个address写,则加一条 i_Writepointto
	
//	对于add [ebx+4],6,变为
//		tem_1 = i_addr(ebx,4);
//		tem_2 = i_readpointto(tem_1);
//		tem_3 = tem_2 + 6;
//		i_writepointto(tem_1, tem_3);

	
	//	当前的情况是:
	//		tem1 addr eax,ebx*4,401000
	//		tem1 = ????
	//	要改成
	//		tem1 addr eax,ebx*4,401000
	//		tem2 = ????
	//		Writepointto(tem1, tem2);
	if (p->var_w.type != v_Tem)
	{	//	实际上不会来这里
		InstrAddTail(p);
		return;
	}
	
	VAR tem1 = p->var_w;	//	sav it
	VAR tem2;

	new_temp(&tem2);

	p->var_w = tem2;
	InstrAddTail(p);	//	add this

	PINSTR pnew = new INSTR;    //new_INSTR
	pnew->type = i_Writepointto;
	pnew->var_r1 = tem1;				// the pointer
	pnew->var_r2 = tem2;					// the value
		//	对 i_Writepointto, 是var_r1是指针,var_r2是值
	InstrAddTail(pnew);
	
}
예제 #8
0
파일: quad.c 프로젝트: dolenle/compiler
qnode* gen_rvalue(node* n, qnode* target) {
	switch(n->type) {
		case IDENT_NODE: {
			qnode* q = qnode_new(Q_IDENT);
			q->name = n->u.ident.id;
			q->u.ast = n;
			q->pos = &(n->u.ident.pos);
			if(n->next->type == SCALAR_NODE || n->next->type == POINTER_NODE) {	
				if(target && target->type == Q_IDENT) {
					if(target->u.ast->next->type == SCALAR_NODE) {
						emit(O_MOV, target, q, NULL);
					}
				}
				return q;
			} else if(n->next->type == ARRAY_NODE) {
				//qnode* dest = new_temp();
				//emit(O_LEA, dest, q, NULL);
				if(!target) target = new_temp();
				emit(O_LEA, target, q, NULL);
				return target;
			} else if(n->next->type == FUNCTION_NODE) {
				return q;
			} else {
				fprintf(stderr, "RVAL Unimplemented IDENT\n");
			}
			break;
		}
		case NUMBER_NODE: {
			qnode* q = qnode_new(Q_CONSTANT);
			sprintf(q->name, "%lli", n->u.number.value);
			q->u.value = n->u.number.value;
			if(target && target->type == Q_IDENT) {
				if(target->u.ast->next->type == SCALAR_NODE) {
					emit(O_MOV, target, q, NULL);
				}
			}
			return q;
			break;
		}
		case UNOP_NODE: {
			switch(n->u.unop.type) {
				case DEREF_OP: {
					//because of the AST structure, this is a bit messy
					// if(n->u.unop.operand->type == BINOP_NODE && n->u.unop.operand->u.binop.type == PLUS_OP) {
					// 	node* l = n->u.unop.operand->u.binop.left;
					// 	if(l->type == IDENT_NODE && l->next && l->next->type == ARRAY_NODE) {
					// 		return gen_rvalue(n->u.unop.operand, target);
					// 	}
					// }
					qnode* addr = gen_rvalue(n->u.unop.operand, NULL);
					if(!target) target = new_temp();
					emit(O_LOAD, target, addr, NULL);
					return target;
				}
				case POSTINC_OP: {
					qnode* q = gen_rvalue(n->u.unop.operand, NULL);
					if(!target) target = new_temp();
					emit(O_MOV, target, q, NULL);
					emit(O_INC, NULL, q, NULL);
					return target;
				}
				case POSTDEC_OP: {
					qnode* q = gen_rvalue(n->u.unop.operand, NULL);
					if(!target) target = new_temp();
					emit(O_MOV, target, q, NULL);
					emit(O_DEC, NULL, q, NULL);
					return target;
				}
				case PREINC_OP: {
					qnode* q = gen_rvalue(n->u.unop.operand, NULL);
					emit(O_INC, NULL, q, NULL);
					return q;
				}
				case PREDEC_OP: {
					qnode* q = gen_rvalue(n->u.unop.operand, NULL);
					emit(O_DEC, NULL, q, NULL);
					return q;
				}
				case LOGNOT_OP: {					
					block* bt = bb_newBlock(functionCount, ++blockCount, currentBlock);
					block* bf = bb_newBlock(functionCount, ++blockCount, bt);
					block* bn = bb_newBlock(functionCount, ++blockCount, bf);
					if(!target) target = new_temp();
					
					qnode* zero = qnode_new(Q_CONSTANT);
					zero->name = strdup("0");
					zero->u.value = 0;
					qnode* one = qnode_new(Q_CONSTANT);
					one->name = strdup("1");
					one->u.value = 1;
										
					gen_cond(n->u.unop.operand, blockToQnode(bt), blockToQnode(bf));
					currentBlock->next = bt; //reset next block in case it has changed
					bt->prev = currentBlock;
					currentBlock = bt;
					emit(O_MOV, target, zero, NULL);
					emit(O_BR, NULL, blockToQnode(bn), NULL);
					currentBlock = bf;
					emit(O_MOV, target, one, NULL);
					emit(O_BR, NULL, blockToQnode(bn), NULL);
					currentBlock = bn;
					return target;
				}
				case ADDR_OP: {
					if(!target) target = new_temp();
					emit(O_LEA, target, gen_rvalue(n->u.unop.operand, NULL), NULL);
					return target;
				}
				case NEG_OP: {
					if(!target) target = new_temp();
					qnode* val = gen_rvalue(n->u.unop.operand, NULL);
					qnode* one = qnode_new(Q_CONSTANT);
					one->name = strdup("-1");
					one->u.value = -1;
					emit(O_MUL, target, one, val);
					return target;
				}
				case POS_OP: {
					return gen_rvalue(n->u.unop.operand, NULL); //not much to do...
				}
				case SIZEOF_OP: {
					int size = 0;
					if(n->u.unop.operand->type == IDENT_NODE) {
						if(n->u.unop.operand->next->type == ARRAY_NODE) { //compute size
							node* a = n->u.unop.operand->next;
							size = a->u.array.length;
							while(a->next && a->next->type == ARRAY_NODE) {//multidimensional
								a = a->next;
								size *= a->u.array.length;
							}
							size *= sizeof(int);
						} else { //assume its an int or ptr (same size)
							size = sizeof(int);
						}
					} else if(n->u.unop.operand->type == POINTER_NODE) {
						size = sizeof(void*);
					} else if(n->u.unop.operand->type == SCALAR_NODE) {
						size = sizeof(int); //only supporting ints
					} else {
						fprintf(stderr, "RVAL unimplemented SIZEOF (type %i)\n", n->u.unop.operand->type);
						exit(1);
					}
					qnode* q = qnode_new(Q_CONSTANT);
					sprintf(q->name, "%i", size);
					q->u.value = size;
					if(target && target->type == Q_IDENT) {
						if(target->u.ast->next->type == SCALAR_NODE) {
							emit(O_MOV, target, q, NULL);
						}
					}
					return q;
				}
				default:
					fprintf(stderr, "RVAL Unimplemented UNOP %i\n", n->u.unop.type);
			}
			break;
		}
		case BINOP_NODE: {
			//boolean expression
			if((n->u.binop.type >= LT_OP && n->u.binop.type <= NOTEQ_OP) || n->u.binop.type >= LOGOR_OP) {
				if(!target) target = new_temp();
				block* temp = currentBlock;
				block* bt = bb_newBlock(functionCount, ++blockCount, currentBlock);
				block* bf = bb_newBlock(functionCount, ++blockCount, bt);
				block* bn = bb_newBlock(functionCount, ++blockCount, bf);
				
				//create qnodes for the above blocks
				qnode* trueBlock = blockToQnode(bt);
				qnode* falseBlock = blockToQnode(bf);
				qnode* nextBlock = blockToQnode(bn);

				//qnodes for zero and one
				qnode* zero = qnode_new(Q_CONSTANT);
				zero->name = strdup("0");
				zero->u.value = 0;
				qnode* one = qnode_new(Q_CONSTANT);
				one->name = strdup("1");
				one->u.value = 1;

				gen_cond(n, trueBlock, falseBlock); //evaluate expression
				currentBlock = bt; //true
				emit(O_MOV, target, one, NULL);
				emit(O_BR, NULL, nextBlock, NULL);
				currentBlock = bf; //false
				emit(O_MOV, target, zero, NULL);
				emit(O_BR, NULL, nextBlock, NULL);
				
				currentBlock = bn;
				return target;
			} else {
				node* bLeft = n->u.binop.left;
				qnode* left = gen_rvalue(n->u.binop.left, NULL);
				qnode* right = gen_rvalue(n->u.binop.right, NULL);
				if(bLeft->type == IDENT_NODE && bLeft->next->type == ARRAY_NODE) {
					qnode* size = qnode_new(Q_CONSTANT);
					int nextType = bLeft->next->next->type;
					if(nextType == SCALAR_NODE) {
						size->u.value = (long long) sizeof(int);
					} else if(nextType == POINTER_NODE) {
						size->u.value = (long long) sizeof(int*);
					} else if(nextType == ARRAY_NODE) {
						node* arr = bLeft->next->next;
						size->u.value = 1;
						while(arr) {
							size->u.value *= (long long) sizeof(int*)*arr->u.array.length;
							if(arr->next && arr->next->type == ARRAY_NODE) {
								arr = arr->next;
							} else {
								break;
							}
						}
					} else {
						fprintf(stderr, "QUAD ERROR, unimplemented sizeof\n");
					}
					qnode* temp = new_temp();
					sprintf(size->name, "%li", size->u.value);
					emit(O_MUL, temp, right, size);
					right = temp;
				}
				if(!target) target = new_temp();
				emit(getBinop(n->u.binop.type), target, left, right);
				return target;
			}
		}
		case CALL_NODE: {
			if(n->u.call.function) {
				int argnum = n->u.call.argnum;
				qnode* num = qnode_new(Q_CONSTANT);
				num->name = malloc(64);
				sprintf(num->name, "%i", argnum);
				num->u.value = argnum;
				emit(O_ARGNUM, NULL, num, NULL);
				if(argnum) { //Generate argument quad
					node* argptr = n->u.call.args;
					while(argptr) {
						emit(O_ARGDEF, NULL, gen_rvalue(argptr->u.list.start, NULL), NULL);
						if(argptr->next) {
							argptr = argptr->next;
						} else {
							break;
						}
					}
				}
				if(!target) target = new_temp();
				emit(O_CALL, target, gen_rvalue(n->u.call.function, NULL), NULL);
				return target;
			} else {
				fprintf(stderr, "Error: Call Node RVALUE\n");
			}
			break;
		}
		case STRING_NODE: {
			qnode* str = qnode_new(Q_STRING);
			str->name = malloc(64);
			sprintf(str->name, ".S%i", stringCount++);
			str->u.ast = n;
			if(target && target->type == Q_IDENT) {
				if(target->u.ast->next->type == POINTER_NODE) {
					emit(O_MOV, target, str, NULL);
					return target;
				}
			} else {
				return str;
			}
		}
		default:
			fprintf(stderr, "Error: Cannot generate RVALUE for AST node type %i\n", n->type);
			exit(1);
	}
	return NULL;
}
예제 #9
0
InterCodes translate_Stmt(node* Stmt){
	if(strcmp(Stmt->child->name,"Exp") == 0){
		Operand t = new_temp();
		return translate_Exp(Stmt->child,t);

	}
	else if(strcmp(Stmt->child->name,"CompSt") == 0){
		return translate_Compst(Stmt->child);
	}
	else if(strcmp(Stmt->child->name,"RETURN") == 0){
		Operand t1 = new_temp();
		InterCodes codes1 =  translate_Exp(Stmt->child->brother,t1);
		
		InterCodes codes2 = InterCodes_init();
		codes2->code = new_interCode(RET);
		codes2->code->onlyop.op = t1;
		
		InterCodes_link(codes1,codes2);
		return codes1;
	}
	else if(strcmp(Stmt->child->name,"WHILE") == 0){
		Operand label1 = new_label();
		Operand label2 = new_label();
		Operand label3 = new_label();

		InterCodes codes1 = InterCodes_init();
		codes1->code = new_interCode(LAB);
		codes1->code->onlyop.op = label1;

		InterCodes codes2 = translate_Cond(Stmt->child->brother->brother, label2, label3);
		
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(LAB);
		codes3->code->onlyop.op = label2;

		InterCodes codes4 = translate_Stmt(Stmt->child->brother->brother->brother->brother);

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(GOTO);
		codes5->code->onlyop.op = label1;

		InterCodes codes6 = InterCodes_init();
		codes6->code = new_interCode(LAB);
		codes6->code->onlyop.op = label3;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);
		InterCodes_link(codes1,codes6);

		return codes1;

	}
	//IF LP Exp RP Stmt1 
	else if(Stmt->child->brother->brother->brother->brother->brother == NULL){
		Operand label1 = new_label();
		Operand label2 = new_label();
		
		InterCodes codes1 = translate_Cond(Stmt->child->brother->brother, label1, label2);
		
		InterCodes codes2 = InterCodes_init();
		codes2->code = new_interCode(LAB);
		codes2->code->onlyop.op = label1;

		InterCodes codes3 = translate_Stmt(Stmt->child->brother->brother->brother->brother);

		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(LAB);
		codes4->code->onlyop.op = label2;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);

		return codes1;
	}
	//IF LP Exp RP Stmt1 ELSE Stmt2 
	else{
		Operand	label1 = new_label();
	 	Operand	label2 = new_label();
		Operand label3 = new_label();

		InterCodes codes1 = translate_Cond(Stmt->child->brother->brother, label1, label2);

		InterCodes codes2 = InterCodes_init();
		codes2->code = new_interCode(LAB);
		codes2->code->onlyop.op = label1;

		InterCodes codes3 = translate_Stmt(Stmt->child->brother->brother->brother->brother);

		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(GOTO);
		codes4->code->onlyop.op = label3;

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(LAB);
		codes5->code->onlyop.op = label2;

		InterCodes codes6 = translate_Stmt(Stmt->child->brother->brother->brother->brother->brother->brother);

		InterCodes codes7 = InterCodes_init();
		codes7->code = new_interCode(LAB);
		codes7->code->onlyop.op = label3;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);
		InterCodes_link(codes1,codes6);
		InterCodes_link(codes1,codes7);

		return codes1;
	}
	//else if(Stmt->child->brother-)
} 
예제 #10
0
void	CCodeList_Maker::TransVar_(VAR* pvar,int no)
//SuperC_func: 只在<CCodeList_Maker::TransVar>中使用
{
	OPERITEM* op = &this->cur->xcpu.op[no];
	switch (op->mode)
	{
	case OP_Register:
		pvar->type = v_Reg;
		pvar->opsize = op->opersize;
		pvar->reg = regindex_2_regoff(op->reg.reg_index);
		return;
	case OP_Immed:
		pvar->type = v_Immed;
		pvar->opsize = op->opersize;
		pvar->d = op->immed.immed_value;
		return;
	case OP_Address:
		if (op->addr.base_reg_index == _NOREG_
			&& op->addr.off_reg_index == _NOREG_)
		{
            if (op->addr.off_value == 0 && op->addr.seg_index == _FS_)
            {//判断 fs:[0]
                pvar->type = v_Volatile;    //现在只用于 fs:0
                pvar->opsize = op->opersize;
                pvar->temno = 222;  //只要是偶数就行
                return;
            }
			pvar->type = v_Global;
			pvar->opsize = op->opersize;
			pvar->off = op->addr.off_value;
			return;
		}
		if (op->addr.base_reg_index == _ESP_
			&& op->addr.off_reg_index == _NOREG_)
		{
			pvar->opsize = op->opersize;
			signed long l = this->cur->esp_level + (signed int)op->addr.off_value;
			if (l >= 0)
			{
				pvar->par_off = l;
				pvar->type = v_Par;
			}
			else
			{
				pvar->var_off = stack2varoff(l);
				pvar->type = v_Var;
			}
			return;
		}
		if (op->addr.base_reg_index == _EBP_
			&& this->m_EBP_base != Not_EBP_based
			&& op->addr.off_reg_index == _NOREG_)
		{
			pvar->opsize = op->opersize;
			signed long l = this->m_EBP_base + (signed int)op->addr.off_value;
			if (l >= 0)
			{
				pvar->par_off = l;
				pvar->type = v_Par;
			}
			else
			{
				pvar->var_off = stack2varoff(l);
				pvar->type = v_Var;
			}
			return;
		}
		//	now, really stuff
		{
			VAR v;
			new_temp(&v);

			PINSTR p = new INSTR;   //new_INSTR
			p->type = i_Address;
			
			p->var_w = v;

			set_address(op, p);

			InstrAddTail(p);

			*pvar = v;
		}
		return;
	default:
		//warn_msg(0,"op mode unknown");
		break;
	}
}
예제 #11
0
InterCodes translate_Cond(node* exp,Operand true_place,Operand false_place){
	if(strcmp(exp->child->name,"NOT")==0){
		return translate_Cond(exp->child->brother,true_place,false_place);
	}
	else if(strcmp(exp->child->brother->name,"RELOP")==0){
		Operand t1 = new_temp();
		Operand t2 = new_temp();
		InterCodes codes1 = translate_Exp(exp->child,t1);
		InterCodes codes2 = translate_Exp(exp->child->brother->brother,t2);
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(COND);
		codes3->code->cond.op1 = t1;
		codes3->code->cond.op2 = t2;
		codes3->code->cond.op = new_operand(op,0);
		strcpy(codes3->code->cond.op->op,exp->child->brother->node_value);
		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(GOTO);
		codes4->code->onlyop.op = true_place;

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(GOTO);
		codes5->code->onlyop.op = false_place;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);

		return codes1;
	}
	else if(strcmp(exp->child->brother->name,"AND")==0){
		Operand label1 = new_label();
		InterCodes codes1 = translate_Cond(exp->child, label1, false_place);
		InterCodes codes2 = translate_Cond(exp->child->brother->brother, true_place, false_place);
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(LAB);
		codes3->code->onlyop.op = label1;
		
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes2);
		return codes1;
	}
	else if(strcmp(exp->child->brother->name,"OR")==0){
		Operand label1 = new_label();
		InterCodes codes1 = translate_Cond(exp->child, true_place, label1);
		InterCodes codes2 = translate_Cond(exp->child->brother->brother, true_place, false_place);
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(LAB);
		codes3->code->onlyop.op = label1;
		
		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		return codes1;
	}
	else{
		/*Operand t1 = new_temp()
		InterCodes codes1 = translate_Exp(Exp, sym_table, t1)
		code2 = [IF t1 != #0 GOTO label_true]
		return code1 + code2 + [GOTO label_false]
		*/
	}
}
예제 #12
0
파일: func (2).c 프로젝트: jkingxt/compiler
//符号表是全局变量
struct InterCodes* translate_Exp(struct TreeNode* Exp, Operand place){
	if (strcmp(Exp->children->name, "INT")){
		printf("Exp - INT\n");
		if(place != NULL){
			FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
			lookupTable(Exp->children->value_str,&item,0);

			place->kind = CONSTANT;
			place->u.value = Exp->children->value_int;

			struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			temp->code.kind = NONE_;
			return temp;
		}
		else{
			struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			temp->code.kind = NONE_;
			return temp;
		}
	}
	if (strcmp(Exp->children->name, "ID")){
		if(Exp->children->neighbours == NULL){
			printf("Exp - ID\n");
			if(place != NULL){
				FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
				lookupTable(Exp->children->value_str,&item,0);

				place->kind = VARIABLE;
				strcpy(place->u.ID, item->inter_name);

				struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				temp->code.kind = NONE_;
				return temp;
			}
			else{
				struct InterCodes* temp = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				temp->code.kind = NONE_;
				return temp;
			}
		}
		if(strcmp(Exp->children->neighbours->name, "DOT")){
			printf("Exp - Exp DOT ID\n");
			//Operand t1 = new_temp();
			//struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);

			FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
			int i = lookupTable(Exp->children->neighbours->neighbours->value_str,&item,0);
			switch(i){
				case 0:{
					Operand t1 = new_temp();
					struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code1->code.kind = ADD_;
					code1->code.u.binop.result = t1;
					code1->code.u.binop.op1 = (Operand)malloc(sizeof(struct Operand_));
					code1->code.u.binop.op1->kind = VARIABLE;
					strcpy(code1->code.u.binop.op1->u.ID, item->inter_name);
					code1->code.u.binop.op2 = (Operand)malloc(sizeof(struct Operand_));
					code1->code.u.binop.op2->kind = CONSTANT;
					code1->code.u.binop.op2->u.value = index_size(item, Exp->children->value_str);

					place->kind = REFERENCE;
					strcpy(place->u.ID, t1->u.ID);

					return code1;
					//break;
				}
				case 1:{
					Operand t1 = new_temp();
					Operand t2 = new_temp();

					struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code1->code.kind = ASSIGN_;
					code1->code.u.assign.left = t1;
					code1->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
					code1->code.u.assign.right->kind = ADDRESS;
					strcpy(code1->code.u.assign.right->u.ID, item->inter_name);

					struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code2->code.kind = ADD_;
					code2->code.u.binop.result = t2;
					code2->code.u.binop.op1 = t1;
					code2->code.u.binop.op2 = (Operand)malloc(sizeof(struct Operand_));
					code2->code.u.binop.op2->kind = CONSTANT;
					code2->code.u.binop.op2->u.value = index_size(item, Exp->children->value_str);

					place->kind = REFERENCE;
					strcpy(place->u.ID, t2->u.ID);

					code1->next = code2;
					code2->prev = code1;

					return code1;
				}
			}
		}
	}
	if (strcmp(Exp->children->name, "Exp")){
		if (strcmp(Exp->children->neighbours->name, "ASSIGNOP")){
			if (strcmp(Exp->children->neighbours->neighbours->children->name, "ID")){
				printf("Exp - Exp ASSIGNOP Exp\n");
				if(place != NULL){
					Operand t1 = new_temp();
					FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
					lookupTable(Exp->children->neighbours->neighbours->children->value_str,&item,0);

					struct InterCodes* code1 = translate_Exp(Exp->children, t1);

					struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code2->code.kind = ASSIGN_;
					code2->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
					code2->code.u.assign.left->kind = VARIABLE;
					strcpy(code2->code.u.assign.left->u.ID, item->inter_name);
					code2->code.u.assign.right = t1;

					/*struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code3->code.kind = ASSIGN;
					code3->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
					code3->code.u.assign.left->kind = VARIABLE;
					strcpy(code3->code.u.assign.left->u.ID, place->u.ID);
					code3->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
					code3->code.u.assign.right->kind = VARIABLE;
					strcpy(code3->code.u.assign.right->u.ID, Exp->children->neighbours->neighbours->children->inter_name);*/

					place->kind = VARIABLE;
					strcpy(place->u.ID, item->inter_name);

					struct InterCodes* p = code1;
					while(p->next != NULL)
						p = p->next;
					p->next = code2;
					code2->prev = p;
					code2->next = NULL;

					return code1;
				}
				else{
					Operand t1 = new_temp();
					FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
					lookupTable(Exp->children->neighbours->neighbours->children->value_str,&item,0);
					
					struct InterCodes* code1 = translate_Exp(Exp->children, t1);

					struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code2->code.kind = ASSIGN_;
					code2->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
					code2->code.u.assign.left->kind = VARIABLE;
					strcpy(code2->code.u.assign.left->u.ID, item->inter_name);
					code2->code.u.assign.right = t1;

					struct InterCodes* p = code1;
					while(p->next != NULL)
						p = p->next;
					p->next = code2;
					code2->prev = p;
					code2->next = NULL;

					return code1;
				}
			}
		}
		if(strcmp(Exp->children->neighbours->name, "PLUS")){
			printf("Exp - Exp PLUS Exp\n");
			if(place != NULL){
				Operand t1 = new_temp();
				Operand t2 = new_temp();
				struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
				struct InterCodes* code2 = translate_Exp(Exp->children, t2);		
				struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code3->code.kind = ADD_;
				code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
				code3->code.u.binop.result->kind = VARIABLE;
				strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
				code3->code.u.binop.op1 = t1;
				code3->code.u.binop.op2 = t2;

				struct InterCodes* p = code1;
				while(p->next != NULL)
					p = p->next;
				p->next = code2;

				code2->prev = p;
				p = code2;
				while(p->next != NULL)
					p = p->next;
				p->next = code3;
				code3->prev = p;

				code3->next = NULL;

				return code1;
			}
			else{
				Operand t1 = new_temp();
				Operand t2 = new_temp();
				struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
				struct InterCodes* code2 = translate_Exp(Exp->children, t2);

				struct InterCodes* p = code1;
				while(p->next != NULL)
					p = p->next;
				p->next = code2;
				code2->prev = p;

				code2->next = NULL;

				return code1;
			}
		}
		if(strcmp(Exp->children->neighbours->name, "MINUS")){
			printf("Exp - Exp MINUS Exp\n");
			if(place != NULL){
				if(Exp->children->neighbours->neighbours != NULL){
					Operand t1 = new_temp();
					Operand t2 = new_temp();
					struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
					struct InterCodes* code2 = translate_Exp(Exp->children, t2);
					struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code3->code.kind = SUB_;
					code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
					code3->code.u.binop.result->kind = VARIABLE;
					strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
					code3->code.u.binop.op1 = t1;
					code3->code.u.binop.op2 = t2;

					struct InterCodes* p = code1;
					while(p->next != NULL)
						p = p->next;
					p->next = code2;

					code2->prev = p;
					p = code2;
					while(p->next != NULL)
						p = p->next;
					p->next = code3;
					code3->prev = p;

					code3->next = NULL;

					return code1;
				}
				else{
					Operand t1 = new_temp();
					struct InterCodes* code1 = translate_Exp(Exp->children, t1);
					struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
					code2->code.kind = SUB_;
					code2->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
					code2->code.u.binop.result->kind = VARIABLE;
					strcpy(code2->code.u.binop.result->u.ID, place->u.ID);
					code2->code.u.binop.op1 = (Operand)malloc(sizeof(struct Operand_));
					code2->code.u.binop.op1->kind = CONSTANT;
					code2->code.u.binop.op1->u.value = 0;
					code2->code.u.binop.op2 = t1;

					struct InterCodes* p = code1;
					while(p->next != NULL)
						p = p->next;
					p->next = code2;
					code2->prev = p;

					code2->next = NULL;

					return code1;
				}
			}
			else{
				if(Exp->children->neighbours->neighbours != NULL){
					Operand t1 = new_temp();
					Operand t2 = new_temp();
					struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
					struct InterCodes* code2 = translate_Exp(Exp->children, t2);

					struct InterCodes* p = code1;
					while(p->next != NULL)
						p = p->next;
					p->next = code2;
					code2->prev = p;

					code2->next = NULL;

					return code1;
				}
				else{
					Operand t1 = new_temp();
					struct InterCodes* code1 = translate_Exp(Exp->children, t1);

					return code1;
				}
			}
		}
		if(strcmp(Exp->children->neighbours->name, "STAR")){
			printf("Exp - Exp STAR Exp\n");
			if(place != NULL){
				Operand t1 = new_temp();
				Operand t2 = new_temp();
				struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
				struct InterCodes* code2 = translate_Exp(Exp->children, t2);		
				struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code3->code.kind = MUL_;
				code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
				code3->code.u.binop.result->kind = VARIABLE;
				strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
				code3->code.u.binop.op1 = t1;
				code3->code.u.binop.op2 = t2;

				struct InterCodes* p = code1;
				while(p->next != NULL)
					p = p->next;
				p->next = code2;

				code2->prev = p;
				p = code2;
				while(p->next != NULL)
					p = p->next;
				p->next = code3;
				code3->prev = p;

				code3->next = NULL;

				return code1;
			}
			else{
				Operand t1 = new_temp();
				Operand t2 = new_temp();
				struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
				struct InterCodes* code2 = translate_Exp(Exp->children, t2);

				struct InterCodes* p = code1;
				while(p->next != NULL)
					p = p->next;
				p->next = code2;
				code2->prev = p;

				code2->next = NULL;

				return code1;
			}
		}
		if(strcmp(Exp->children->neighbours->name, "DIV")){
			printf("Exp - Exp DIV Exp\n");
			if(place != NULL){
				Operand t1 = new_temp();
				Operand t2 = new_temp();
				struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
				struct InterCodes* code2 = translate_Exp(Exp->children, t2);
				struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code3->code.kind = DIV_;
				code3->code.u.binop.result = (Operand)malloc(sizeof(struct Operand_));
				code3->code.u.binop.result->kind = VARIABLE;
				strcpy(code3->code.u.binop.result->u.ID, place->u.ID);
				code3->code.u.binop.op1 = t1;
				code3->code.u.binop.op2 = t2;

				struct InterCodes* p = code1;
				while(p->next != NULL)
					p = p->next;
				p->next = code2;

				code2->prev = p;
				p = code2;
				while(p->next != NULL)
					p = p->next;
				p->next = code3;
				code3->prev = p;

				code3->next = NULL;

				return code1;
			}
			else{
				Operand t1 = new_temp();
				Operand t2 = new_temp();
				struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
				struct InterCodes* code2 = translate_Exp(Exp->children, t2);

				struct InterCodes* p = code1;
				while(p->next != NULL)
					p = p->next;
				p->next = code2;
				code2->prev = p;

				code2->next = NULL;

				return code1;
			}
		}
		if(strcmp(Exp->children->neighbours->name, "RELOP") || strcmp(Exp->children->neighbours->name, "NOT") || strcmp(Exp->children->neighbours->name, "AND") || strcmp(Exp->children->neighbours->name, "OR")){
			printf("Exp - Exp Cond Exp\n");
			if(place != NULL){
				Operand label1 = new_label();
				Operand label2 = new_label();
				struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code1->code.kind = ASSIGN_;
				code1->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
				code1->code.u.assign.left->kind = VARIABLE;
				strcpy(code1->code.u.assign.left->u.ID, place->u.ID);
				code1->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
				code1->code.u.assign.right->kind = CONSTANT;
				code1->code.u.assign.right->u.value = 0;
				struct InterCodes* code2 = translate_Cond(Exp, label1, label2);
				struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code3->code.kind = LABEL_;
				code3->code.u.labelcode.label = label1;
				struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code4->code.kind = ASSIGN_;
				code4->code.u.assign.left = (Operand)malloc(sizeof(struct Operand_));
				code4->code.u.assign.left->kind = VARIABLE;
				strcpy(code4->code.u.assign.left->u.ID, place->u.ID);
				code4->code.u.assign.right = (Operand)malloc(sizeof(struct Operand_));
				code4->code.u.assign.right->kind = CONSTANT;
				code4->code.u.assign.right->u.value = 1;
				struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code5->code.kind = LABEL_;
				code5->code.u.labelcode.label = label2;

				code1->next = code2;
				code2->prev = code1;
				struct InterCodes* p = code2;
				while(p->next != NULL)
					p = p->next;
				p->next = code3;
				code3->prev = p;

				code3->next = code4;
				code4->prev = code3;

				code4->next = code5;
				code5->prev = code4;

				code5->next = NULL;

				return code1;
			}
		}
	}
	if (strcmp(Exp->children->neighbours->name, "LP")){
		printf("Exp - ID LP RP\n");
		if(strcmp(Exp->children->neighbours->neighbours->value_str, "read") == 0){
			struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code1->code.kind = READ_;
			code1->code.u.read.rd = place;
			return code1;
		}
		else{
			FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
			lookupTable(Exp->children->neighbours->neighbours->value_str,&item,1);//查函数
			struct InterCodes* code1 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code1->code.kind = CALL_;
			code1->code.u.call.left = (Operand)malloc(sizeof(struct Operand_));
			code1->code.u.call.left->kind = VARIABLE;
			strcpy(code1->code.u.assign.left->u.ID, place->u.ID);
			code1->code.u.call.function = (Operand)malloc(sizeof(struct Operand_));
			code1->code.u.call.function->kind = VARIABLE;
			strcpy(code1->code.u.call.function->u.ID, item->name);
			return code1;
		}
	}
	if(strcmp(Exp->children->neighbours->name, "Args")){
		printf("Exp - ID LP Args RP\n");
		struct ArgList* arg_list = NULL;
		struct InterCodes* code1 = translate_Args(Exp->children->neighbours, arg_list);
		if(strcpy(Exp->children->neighbours->neighbours->neighbours->value_str, "write") == 0){
			struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code2->code.kind = WRITE_;
			code2->code.u.write.wr = arg_list->op;

			struct InterCodes* p = code1;
			while(p->next != NULL)
				p = p->next;
			p->next = code2;
			code2->prev = p;

			code2->next = NULL;

			return code1;
		}
		else{
			FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
			lookupTable(Exp->children->neighbours->neighbours->neighbours->value_str,&item,0);
			struct ArgList* q = arg_list;
			struct InterCodes* code2 = NULL;
			while(q != NULL){
				struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
				code3->code.kind = ARG_;
				code3->code.u.arg.argument = q->op;
				if(code2 == NULL){
					code2 = code3;
				}
				else{
					struct InterCodes* p = code2;
					while(p->next != NULL)
						p = p->next;
					p->next = code3;
					code3->prev = p;
				}
				q = q->next;
			}
			struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code4->code.kind = CALL_;
			code4->code.u.call.left = (Operand)malloc(sizeof(struct Operand_));
			code4->code.u.call.left->kind = VARIABLE;
			strcpy(code4->code.u.assign.left->u.ID, place->u.ID);
			code4->code.u.call.function = (Operand)malloc(sizeof(struct Operand_));
			code4->code.u.call.function->kind = VARIABLE;
			strcpy(code4->code.u.call.function->u.ID, item->name);

			struct InterCodes* p = code1;
			while(p->next != NULL)
				p = p->next;
			p->next = code2;
			code2->prev = p;

			p = code2;
			while(p->next != NULL)
				p = p->next;
			p->next = code4;
			code4->prev = p;

			code4->next = NULL;

			return code1;
		}
	}
	if (strcmp(Exp->children->name, "RB") == 0){
		printf("Exp - Exp LB Exp RB\n");
		FieldList item = (FieldList)malloc(sizeof(struct FieldList_));
		lookupTable(Exp->children->neighbours->neighbours->neighbours->children->value_str,&item,0);

		Operand t1 = new_temp();
		Operand t2 = new_temp();
		Operand t3 = new_temp();

		struct InterCodes* code1 = translate_Exp(Exp->children->neighbours, t1);
		struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code2->code.kind = MUL_;
		code2->code.u.binop.result = t2;
		code2->code.u.binop.op1 = t1;
		code2->code.u.binop.op2 = (Operand)malloc(sizeof(struct Operand_));
		code2->code.u.binop.result->kind = CONSTANT;
		code2->code.u.binop.result->u.value = 4;
		struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code3->code.kind = ADD_;	
		code3->code.u.binop.result = t3;
		code3->code.u.binop.op1 = (Operand)malloc(sizeof(struct Operand_));
		code3->code.u.binop.op1->kind = ADDRESS;
		strcpy(code3->code.u.binop.op1->u.ID, item->inter_name);
		code3->code.u.binop.op2 = t2;
		//place = t4;
		place->kind = REFERENCE;
		strcpy(place->u.ID, t3->u.ID);

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code2;
		code2->prev = p;

		code2->next = code3;
		code3->prev = code2;

		code3->next = NULL;

		return code1;
	}
}
예제 #13
0
파일: func (2).c 프로젝트: jkingxt/compiler
struct InterCodes* translate_Stmt(struct TreeNode* Stmt){
	if (strcmp(Stmt->children->name, "SEMI") == 0){
		if(Stmt->children->neighbours->neighbours == NULL){
			printf("Stmt - Stmt SEMI\n");
			return translate_Exp(Stmt->children->neighbours, NULL);
		}
		else{
			printf("Stmt - RETURN Stmt SEMI\n");
			Operand t1 = new_temp();
			struct InterCodes* code1 = translate_Exp(Stmt->children->neighbours, t1);
			struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code2->code.kind = RETURN_;
			code2->code.u.returncode.r = t1;
			struct InterCodes* p = code1;
			while(p->next != NULL)
				p = p->next;
			p->next = code2;
			code2->prev = p;

			code2->next = NULL;

			return code1;
		}
	}
	if(strcmp(Stmt->children->name, "CompSt") == 0){
		printf("Stmt - CompSt\n");
		return translate_CompSt(Stmt->children);
	}
	if(strcmp(Stmt->children->name, "Stmt") == 0){
		if(strcmp(Stmt->children->neighbours->neighbours->neighbours->neighbours->name, "IF") == 0){
			printf("Stmt - IF LP Exp RP Stmt\n");
			Operand label1 = new_label();
			Operand label2 = new_label();
			struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label1, label2);
			struct InterCodes* code2 = translate_Stmt(Stmt->children);
			struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code3->code.kind = LABEL_;
			code3->code.u.labelcode.label = label1;
			struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code4->code.kind = LABEL_;
			code4->code.u.labelcode.label = label2;

			struct InterCodes* p = code1;
			while(p->next != NULL)
				p = p->next;
			p->next = code3;
			code3->prev = p;
			code3->next = code2;

			p = code2;
			while(p->next != NULL)
				p = p->next;
			p->next = code4;
			code4->prev = p;
			code4->next = NULL;

			return code1;
		}
		if(strcmp(Stmt->children->neighbours->neighbours->neighbours->neighbours->name, "WHILE") == 0){
			printf("Stmt - WHILE LP Exp RP Stmt\n");
			Operand label1 = new_label();
			Operand label2 = new_label();
			Operand label3 = new_label();
			struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label2, label3);
			struct InterCodes* code2 = translate_Stmt(Stmt->children->neighbours->neighbours);
			struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code3->code.kind = LABEL_;
			code3->code.u.labelcode.label = label1;
			struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code4->code.kind = LABEL_;
			code4->code.u.labelcode.label = label2;
			struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code5->code.kind = LABEL_;
			code5->code.u.labelcode.label = label3;
			struct InterCodes* code6 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code6->code.kind = GOTO_;
			code6->code.u.gotocode.label = label3;

			code3->next = code1;
			code1->prev = code3;

			struct InterCodes* p = code1;
			while(p->next != NULL)
				p = p->next;
			p->next = code4;
			code4->prev = p;
			code4->next = code2;

			p = code2;
			while(p->next != NULL)
				p = p->next;
			p->next = code6;
			code6->prev = p;
			code6->next = code5;
			code5->prev = code6;

			code5->next = NULL;

			return code3;
		}
		if(strcmp(Stmt->children->neighbours->name, "ELSE") == 0){
			printf("Stmt - IF LP Exp RP Stmt ELSE Stmt\n");
			Operand label1 = new_label();
			Operand label2 = new_label();
			Operand label3 = new_label();
			struct InterCodes* code1 = translate_Cond(Stmt->children->neighbours->neighbours, label1, label2);
			struct InterCodes* code2 = translate_Stmt(Stmt->children->neighbours->neighbours);
			struct InterCodes* code3 = translate_Stmt(Stmt->children);
			struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code4->code.kind = LABEL_;
			code4->code.u.labelcode.label = label1;
			struct InterCodes* code5 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code5->code.kind = LABEL_;
			code5->code.u.labelcode.label = label2;
			struct InterCodes* code6 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code6->code.kind = LABEL_;
			code6->code.u.labelcode.label = label3;
			struct InterCodes* code7 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
			code7->code.kind = GOTO_;
			code7->code.u.gotocode.label = label3;

			struct InterCodes* p = code1;
			while(p->next != NULL)
				p = p->next;
			p->next = code4;
			code4->prev = p;
			code4->next = code2;

			p = code2;
			while(p->next != NULL)
				p = p->next;
			p->next = code7;
			code7->prev = p;
			code7->next = code5;

			code5->prev = code7;
			code5->next = code3;

			p = code3;
			while(p->next != NULL)
				p = p->next;
			p->next = code6;
			code6->prev = p;
			code6->next = NULL;

			return code1;
		}
	}
}
예제 #14
0
InterCodes translate_Exp(node* exp,Operand place){
	//-------------------------------------------------Exp ASSIGNOP Exp
	if(exp->exp_type == 7){
		Operand t = new_temp();		
		InterCodes codes1 ;
		codes1 = translate_Exp(exp->child->brother->brother,t);

		if(strcmp(exp->child->child->name,"ID")==0){
			InterCodes codes2 = InterCodes_init();
			codes2->code = new_interCode(0);
			codes2->code->assign.left = new_operand_name(exp->child->child->node_value);
			codes2->code->assign.right = t;
			InterCodes_link(codes1,codes2);
			/*if(place != NULL){
				InterCodes codes3 = InterCodes_init();
				codes3->code = new_interCode(0);
				codes3->code->assign.left = place;
				codes3->code->assign.right = codes2->code->assign.left;
				InterCodes_link(codes1,codes3);
			}*/
		}
		else{
			Operand t2 =new_temp();
			InterCodes codes2 = translate_Exp(exp->child,t2);
			if(place != NULL){
				InterCodes codes3 = InterCodes_init();
				codes3->code = new_interCode(0);
				codes3->code->assign.left = place;
				codes3->code->assign.right = t2;
				InterCodes_link(codes1,codes3);
			}
		}
		return codes1;
	}
	//-------------------------------------------------Exp AND Exp
	else if(exp->exp_type == 8){
		Operand	label1 = new_label();
		Operand label2 = new_label();
		InterCodes codes1 = InterCodes_init();
		codes1->code = new_interCode(0);
		codes1->code->assign.left = place;
		codes1->code->assign.right = new_operand(1,0);

		InterCodes codes2 = translate_Cond(exp, label1, label2);
		
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(5);
		codes3->code->onlyop.op = label1;

		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(0);
		codes4->code->assign.left = place;
		codes4->code->assign.right = new_operand(1,1);

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(LAB);
		codes5->code->onlyop.op = label2; 

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);
		
		return codes1;
	}

	//-------------------------------------------------Exp OR Exp
	else if(exp->exp_type == 9){
		Operand	label1 = new_label();
		Operand label2 = new_label();
		InterCodes codes1 = InterCodes_init();
		codes1->code = new_interCode(0);
		codes1->code->assign.left = place;
		codes1->code->assign.right = new_operand(1,0);

		InterCodes codes2 = translate_Cond(exp, label1, label2);
		
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(5);
		codes3->code->onlyop.op = label1;

		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(0);
		codes4->code->assign.left = place;
		codes4->code->assign.right = new_operand(1,1);

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(LAB);
		codes5->code->onlyop.op = label2; 

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);
		
		return codes1;
	}

	//-------------------------------------------------Exp RELOP Exp
	else if(exp->exp_type == 10){
		Operand	label1 = new_label();
		Operand label2 = new_label();
		InterCodes codes1 = InterCodes_init();
		codes1->code = new_interCode(0);
		codes1->code->assign.left = place;
		codes1->code->assign.right = new_operand(1,0);

		InterCodes codes2 = translate_Cond(exp, label1, label2);
		
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(5);
		codes3->code->onlyop.op = label1;

		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(0);
		codes4->code->assign.left = place;
		codes4->code->assign.right = new_operand(1,1);

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(LAB);
		codes5->code->onlyop.op = label2; 

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);
		
		return codes1;
	}

	//-------------------------------------------------Exp PLUS Exp
	else if(exp->exp_type == 11){
		Operand t1 = new_temp();
		Operand t2 = new_temp();

		InterCodes codes1 = InterCodes_init();
		InterCodes codes2 = InterCodes_init();
		InterCodes codes3 = InterCodes_init();
		codes1 = translate_Exp(exp->child->brother->brother,t1);
		codes2 = translate_Exp(exp->child,t2);
		codes3->code = new_interCode(1);//plus
		codes3->code->binop.result = place;
		codes3->code->binop.op1 = t1;
		codes3->code->binop.op2 = t2;
		InterCodes_link(codes1,codes2);
		InterCodes_link(codes2,codes3);
		return codes1;
	}

	//-------------------------------------------------Exp MINUS Exp
	else if(exp->exp_type == 12){
		Operand t1 = new_temp();
		Operand t2 = new_temp();

		InterCodes codes1 = InterCodes_init();
		InterCodes codes2 = InterCodes_init();
		InterCodes codes3 = InterCodes_init();
		codes1 = translate_Exp(exp->child->brother->brother,t1);
		codes2 = translate_Exp(exp->child,t2);
		codes3->code = new_interCode(2);//sub
		codes3->code->binop.result = place;
		codes3->code->binop.op1 = t2;
		codes3->code->binop.op2 = t1;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes2,codes3);

		return codes1;
	}

	//--------------------------------------------------Exp STAR Exp
	else if(exp->exp_type == 13){
		Operand t1 = new_temp();
		Operand t2 = new_temp();

		InterCodes codes1 = InterCodes_init();
		InterCodes codes2 = InterCodes_init();
		InterCodes codes3 = InterCodes_init();
		codes1 = translate_Exp(exp->child->brother->brother,t1);
		codes2 = translate_Exp(exp->child,t2);
		codes3->code = new_interCode(3);//star
		codes3->code->binop.result = place;
		codes3->code->binop.op1 = t1;
		codes3->code->binop.op2 = t2;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes2,codes3);

		return codes1;
	}

	//--------------------------------------------------Exp DIV Exp
	else if(exp->exp_type == 14){	
		Operand t1 = new_temp();
		Operand t2 = new_temp();

		InterCodes codes1 = InterCodes_init();
		InterCodes codes2 = InterCodes_init();
		InterCodes codes3 = InterCodes_init();
		codes1 = translate_Exp(exp->child->brother->brother,t1);
		codes2 = translate_Exp(exp->child,t2);
		codes3->code = new_interCode(4);//div
		codes3->code->binop.result = place;
		codes3->code->binop.op1 = t1;
		codes3->code->binop.op2 = t2;

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes2,codes3);

		return codes1;
	}

	//--------------------------------------------------LP Exp RP
	else if(exp->exp_type == 15){
		return translate_Exp(exp->child->brother,place);
	}

	//--------------------------------------------------MINUS Exp
	else if(exp->exp_type == 16){
		Operand t1 = new_temp();
		InterCodes codes1 = InterCodes_init();
		InterCodes codes2 = InterCodes_init();
		codes1 = translate_Exp(exp->child->brother,t1);
		codes2->code = new_interCode(SUB);
		codes2->code->binop.result = place;
		t1->is_min = 1;
		codes2->code->binop.op1 = new_operand(1,0);
		codes2->code->binop.op2 = t1;

		InterCodes_link(codes1,codes2);
		return codes1;

	}

	//---------------------------------------------------NOT Exp
	else if(exp->exp_type == 17){
		Operand	label1 = new_label();
		Operand label2 = new_label();
		InterCodes codes1 = InterCodes_init();
		codes1->code = new_interCode(0);
		codes1->code->assign.left = place;
		codes1->code->assign.right = new_operand(1,0);

		InterCodes codes2 = translate_Cond(exp, label1, label2);
		
		InterCodes codes3 = InterCodes_init();
		codes3->code = new_interCode(5);
		codes3->code->onlyop.op = label1;

		InterCodes codes4 = InterCodes_init();
		codes4->code = new_interCode(0);
		codes4->code->assign.left = place;
		codes4->code->assign.right = new_operand(1,1);

		InterCodes codes5 = InterCodes_init();
		codes5->code = new_interCode(LAB);
		codes5->code->onlyop.op = label2; 

		InterCodes_link(codes1,codes2);
		InterCodes_link(codes1,codes3);
		InterCodes_link(codes1,codes4);
		InterCodes_link(codes1,codes5);
		
		return codes1;
	}

	//---------------------------------------------------ID LP Args RP
	else if(exp->exp_type == 18){
		Operand* arg_list = (Operand*)malloc(sizeof(Operand)*8);
		int arg_num = 0;
		InterCodes codes1 = translate_Args(exp->child->brother->brother,arg_list,&arg_num);
		if (strcmp(exp->child->node_value,"write")==0){
			InterCodes codes2 = InterCodes_init();
			codes2->code = new_interCode(WRITE);
			codes2->code->onlyop.op = arg_list[0];

			InterCodes_link(codes1,codes2);
			
			return codes1;
		}
		int i = 0;
   		for(;i < arg_num;i++){
   			InterCodes codes2 = InterCodes_init();
   			codes2->code = new_interCode(ARG);//codes2->code->kind = REFERENCE;	////////////////////////////////
   			codes2->code->onlyop.op = arg_list[i];

   			InterCodes_link(codes1,codes2);
   		}
   		InterCodes codes3 = InterCodes_init();
   		codes3->code = new_interCode(CALL);
   		codes3->code->assign.left = place;
   		codes3->code->assign.right = new_operand_name(exp->child->node_value);
   		InterCodes_link(codes1,codes3);

  		return codes1;
	}

	//---------------------------------------------------ID LP RP
	else if(exp->exp_type == 19){
		InterCodes codes = InterCodes_init();
		if(strcmp(exp->child->node_value,"read")==0){
			codes->code = new_interCode(READ);
			codes->code->onlyop.op = place;
		}
		/*else if(strcmp(exp->child->name,"write")==0){
			InterCodes codes = InterCodes_init();
			codes->code = new_interCode(READ);
			codes->code->onlyop.op = place;

		}*/
		else{
			codes->code = new_interCode(CALL);
			codes->code->assign.left = place;
			codes->code->assign.right = new_operand_name(exp->child->name); 
		}
		return codes;
	}

	//--------------------------------------------------Exp LB Exp RB
	else if(exp->exp_type == 20){
		return translate_Array(exp,place);
	}

	//--------------------------------------------------Exp DOT ID
	else if(exp->exp_type == 21){
		return translate_Struct(exp,place);
	}

	//---------------------------------------------------ID
	else if(exp->exp_type == 22){
		InterCodes codes = InterCodes_init();
		codes->code = new_interCode(0);
		codes->code->assign.right = new_operand_name(exp->node_value);
		codes->code->assign.left = place;
		return codes;
	}

	//--------------------------------------------------INT
	else if(exp->exp_type == 23){
		InterCodes codes = InterCodes_init();
		codes->code = new_interCode(0);
		codes->code->assign.right = new_operand(1,exp->node_int);
		codes->code->assign.left = place;
		return codes;
	}

	//--------------------------------------------------FLOAT
	else if(exp->exp_type == 24){
		
	}

} 
예제 #15
0
InterCodes translate_Array(node *Exp,Operand place){
	InterCodes code1 = NULL;			
	FieldList p = Findname(Exp->child->child->node_value);
	int size =0;
	//Operand t ;
	if(strcpy(Exp->child->child->name,"ID") == 0){	//Exp[Exp]
		if(p->type->array.elem->kind == STRUCTURE)
			size = getSize(p);
		else size = 4;
		Operand op1 = new_operand_name(Exp->child->child->node_value);
		InterCodes code2,code3,code4;
		Operand t1 = new_temp();
		code2 = translate_Exp(Exp->child->brother->brother,t1);			//翻译[]中的exp
		Operand t2 = new_temp();
		Operand c1 = new_operand(1,size);
		code3 = InterCodes_init();
		code3->code = new_interCode(MUL);
		code3->code->binop.result = t2;
		code3->code->binop.op1 = t1;
		code3->code->binop.op2 = c1;
		code4 = InterCodes_init();
		code4->code = new_interCode(ADDR);
		code4->code->binop.result = place;
		code4->code->binop.op1 = op1;
		code4->code->binop.op2 = t2;
		/*InterCodes_link(code1,code2);
		InterCodes_link(code1,code3);
		InterCodes_link(code1,code4);*/
		InterCodes_link(code2,code3);
		InterCodes_link(code2,code4);
		place->kind = ADDR_op;
		return code2;
	}
	else if(strcpy(Exp->child->child->name,"Exp") == 0)			//Exp[Exp][Exp]
	{
		if(p->type->array.elem->kind == STRUCTURE)
			size = getSize(p);
		else size = 4;
		InterCodes code = InterCodes_init();
		Operand temp = new_temp();
		code = translate_Array(Exp->child,temp);
		Operand op1 = new_operand_name(Exp->child->child->node_value);
		InterCodes code2,code3,code4;
		Operand t1 = new_temp();
		code2 = translate_Exp(Exp->child->brother->brother,t1);			//翻译[]中的exp
		Operand t2 = new_temp();
		Operand c1 = new_operand(1,size);
		code3 = InterCodes_init();
		code3->code = new_interCode(MUL);
		code3->code->binop.result = t2;
		code3->code->binop.op1 = t1;
		code3->code->binop.op2 = c1;
		code4 = InterCodes_init();
		code4->code = new_interCode(ADDR);
		code4->code->binop.result = place;
		code4->code->binop.op1 = op1;
		code4->code->binop.op2 = t2;
		/*InterCodes_link(code1,code2);
		InterCodes_link(code1,code3);
		InterCodes_link(code1,code4);
		InterCodes_link(code1,code);*/
	InterCodes_link(code2,code3);
	InterCodes_link(code2,code4);
	InterCodes_link(code2,code);
		place->kind = ADDR_op;
		return code2;
	}

}
예제 #16
0
파일: func (2).c 프로젝트: jkingxt/compiler
struct InterCodes* translate_Cond(struct TreeNode* Exp, Operand label_true, Operand label_false){
	if(strcmp(Exp->children->neighbours->name, "RELOP") == 0){
		printf("Exp - Exp RELOP Exp\n");
		Operand t1 = new_temp();
		Operand t2 = new_temp();
		struct InterCodes* code1 = translate_Exp(Exp->children->neighbours->neighbours, t1);
		struct InterCodes* code2 = translate_Exp(Exp->children, t2);
		struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code3->code.kind = IF_;
		code3->code.u.ifcode.op1 = t1;
		code3->code.u.ifcode.op2 = t2;
		code3->code.u.ifcode.label = label_true;
		strcpy(code3->code.u.ifcode.op_type, Exp->children->neighbours->value_str);
		struct InterCodes* code4 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code4->code.kind = GOTO_;
		code4->code.u.gotocode.label = label_false;

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code2;
		code2->prev = p;

		p = code2;
		while(p->next != NULL)
			p = p->next;
		p->next = code3;
		code3->prev = p;

		code3->next = code4;
		code4->prev = code3;

		code4->next = NULL;

		return code1;
	}
	if(strcmp(Exp->children->neighbours->name, "NOT") == 0){
		printf("Exp - NOT Exp\n");
		return translate_Cond(Exp->children, label_false, label_true);
	}
	if(strcmp(Exp->children->neighbours->name, "AND") == 0){
		printf("Exp - Exp AND Exp\n");
		Operand label1 = new_label();
		struct InterCodes* code1 = translate_Cond(Exp->children->neighbours->neighbours, label1, label_false);
		struct InterCodes* code2 = translate_Cond(Exp->children, label_true, label_false);
		struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code3->code.kind = LABEL_;
		code3->code.u.labelcode.label = label1;

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code3;
		code3->prev = p;

		code3->next = code2;
		code2->prev = code3;

		return code1;
	}
	if(strcmp(Exp->children->neighbours->name, "OR") == 0){
		printf("Exp - Exp OR Exp\n");
		Operand label1 = new_label();
		struct InterCodes* code1 = translate_Cond(Exp->children->neighbours->neighbours, label_true, label1);
		struct InterCodes* code2 = translate_Cond(Exp->children, label_true, label_false);
		struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code3->code.kind = LABEL_;
		code3->code.u.labelcode.label = label1;

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code3;
		code3->prev = p;

		code3->next = code2;
		code2->prev = code3;

		return code1;
	}
	else{
		printf("Exp - ELSE\n");
		Operand t1 = new_temp();
		struct InterCodes* code1 = translate_Exp(Exp, t1);
		struct InterCodes* code2 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code2->code.kind = IF_;
		code2->code.u.ifcode.op1 = t1;
		code2->code.u.ifcode.op2 = (Operand)malloc(sizeof(struct Operand_));
		code2->code.u.ifcode.op2->kind = CONSTANT;
		code2->code.u.ifcode.op2->u.value = 0;
		code2->code.u.ifcode.label = label_true;
		char op[2] = {'!', '='};
		strcpy(code2->code.u.ifcode.op_type, op);
		struct InterCodes* code3 = (struct InterCodes*)malloc(sizeof(struct InterCodes));
		code3->code.kind = GOTO_;
		code3->code.u.gotocode.label = label_false;

		struct InterCodes* p = code1;
		while(p->next != NULL)
			p = p->next;
		p->next = code2;
		code2->prev = p;

		code2->next = code3;
		code3->prev = code2;

		code3->next = NULL;

		return code1;
	}
}
예제 #17
0
int PhotoScaleMain::process_buffer(VFrame *frame,
	int64_t start_position,
	double frame_rate)
{
	load_configuration();

	if(config.use_file)
	{
		frame->get_params()->update("AUTOSCALE", 1);
		frame->get_params()->update("AUTOSCALE_W", config.width);
		frame->get_params()->update("AUTOSCALE_H", config.height);
		read_frame(frame, 
			0, 
			start_position, 
			frame_rate,
			get_use_opengl());
		return 0;
	}
	else
	{
		frame->get_params()->update("AUTOSCALE", 0);
	}
	

	read_frame(frame, 
		0, 
		start_position, 
		frame_rate,
		0);

	if(!engine) engine = new PhotoScaleEngine(this, 
		PluginClient::get_project_smp() + 1);
	engine->process_packages();

// 	printf("PhotoScaleMain::process_buffer %d %d %d %d %d\n",
// 		__LINE__,
// 		engine->top_border,
// 		engine->bottom_border,
// 		engine->left_border,
// 		engine->right_border);

	int in_width = engine->right_border - engine->left_border;
	int in_height = engine->bottom_border - engine->top_border;
	if(in_width > 0 &&
		in_height > 0 &&
		(engine->top_border > 0 ||
		engine->left_border > 0 ||
		engine->bottom_border < frame->get_h() ||
		engine->right_border < frame->get_w()))
	{
		VFrame *temp_frame = new_temp(frame->get_w(), 
			frame->get_h(),
			frame->get_color_model());
		temp_frame->copy_from(frame);
		if(!overlayer)
		{
			overlayer = new OverlayFrame(PluginClient::get_project_smp() + 1);
		}

		float scale1 = (float)config.width / in_width;
		float scale2 = (float)config.height / in_height;
		float out_x1 = 0;
		float out_y1 = 0;
		float out_x2 = 0;
		float out_y2 = 0;

// printf("PhotoScaleMain::process_buffer %d %d %d %d %d\n", 
// __LINE__, 
// engine->left_border,
// engine->top_border,
// engine->right_border,
// engine->bottom_border);
// printf("PhotoScaleMain::process_buffer %d %f %f\n", __LINE__, scale1, scale2);
		if(scale1 < scale2)
		{
			out_x1 = (float)frame->get_w() / 2 - config.width / 2;
			out_y1 = (float)frame->get_h() / 2 - in_height * scale1 / 2;
			out_x2 = (float)frame->get_w() / 2 + config.width / 2;
			out_y2 = (float)frame->get_h() / 2 + in_height * scale1 / 2;
		}
		else
		{
			out_x1 = (float)frame->get_w() / 2 - in_width * scale2 / 2;
			out_y1 = (float)frame->get_h() / 2 - config.height / 2;
			out_x2 = (float)frame->get_w() / 2 + in_width * scale2 / 2;
			out_y2 = (float)frame->get_h() / 2 + config.height / 2;
		}

// printf("PhotoScaleMain::process_buffer %d %d %d %d %d\n", 
// __LINE__, 
// (int)out_x1, 
// (int)out_y1, 
// (int)out_x2, 
// (int)out_y2);
		frame->clear_frame();
		overlayer->overlay(frame, 
			temp_frame,
			(float)engine->left_border, 
			(float)engine->top_border, 
			(float)engine->right_border, 
			(float)engine->bottom_border,
			(float)out_x1, 
			(float)out_y1, 
			(float)out_x2, 
			(float)out_y2, 
			1,
			TRANSFER_REPLACE,
			get_interpolation_type());
		
	}

	return 0;
}
예제 #18
0
int LensMain::process_buffer(VFrame *frame,
	int64_t start_position,
	double frame_rate)
{
	VFrame *input;
	load_configuration();
	
	if(get_use_opengl())
	{
		input = frame;
	}
	else
	{
		input = new_temp(frame->get_w(), frame->get_h(), frame->get_color_model());
	}
	
	read_frame(input, 
		0, 
		start_position, 
		frame_rate,
		get_use_opengl());


	if(get_use_opengl())
	{
		run_opengl();
		return 0;
	}
	else
	{
		if(!engine) engine = new LensEngine(this);
		engine->process_packages();
		if(config.draw_guides)
		{
// Draw center
#define CENTER_H 20
#define CENTER_W 20
#define DRAW_GUIDES(components, type, max) \
{ \
	type **rows = (type**)get_output()->get_rows(); \
	if(center_x >= 0 && center_x < w || \
		center_y >= 0 && center_y < h) \
	{ \
		type *hrow = rows[center_y] + components * (center_x - CENTER_W / 2); \
		for(int i = center_x - CENTER_W / 2; i <= center_x + CENTER_W / 2; i++) \
		{ \
			if(i >= 0 && i < w) \
			{ \
				hrow[0] = max - hrow[0]; \
				hrow[1] = max - hrow[1]; \
				hrow[2] = max - hrow[2]; \
				hrow += components; \
			} \
		} \
 \
		for(int i = center_y - CENTER_W / 2; i <= center_y + CENTER_W / 2; i++) \
		{ \
			if(i >= 0 && i < h) \
			{ \
				type *vrow = rows[i] + center_x * components; \
				vrow[0] = max - vrow[0]; \
				vrow[1] = max - vrow[1]; \
				vrow[2] = max - vrow[2]; \
			} \
		} \
	} \
}

			int w = get_output()->get_w();
			int h = get_output()->get_h();
			int center_x = (int)(config.center_x * w / 100);
			int center_y = (int)(config.center_y * h / 100);
			switch(get_output()->get_color_model())
			{
				case BC_RGB_FLOAT:
					DRAW_GUIDES(3, float, 1.0)
					break;
				case BC_RGBA_FLOAT:
					DRAW_GUIDES(4, float, 1.0)
					break;
				case BC_RGB888:
					DRAW_GUIDES(3, unsigned char, 0xff)
					break;
				case BC_RGBA8888:
					DRAW_GUIDES(4, unsigned char, 0xff)
					break;
				case BC_YUV888:
					DRAW_GUIDES(3, unsigned char, 0xff)
					break;
				case BC_YUVA8888:
					DRAW_GUIDES(4, unsigned char, 0xff)
					break;
			}

		}
	}
예제 #19
0
/*
 * Sequential version.
 */
int
seqsa_solver(char *stg_filename, char *ssf_filename)
{
    int malloced;
    int freed;

    int malloced_initial;
    int freed_initial;

    int malloced_select;
    int freed_select;

    struct stg *tg;
    struct ssf_status *status;
    struct ssf *schedule;

    double eps;
    unsigned seed;

    int i;
    double t0;
    double t;

    struct iss *alpha;  /* present solution */
    struct iss *beta;   /* neighbour solution of alpha */

    double cost_alpha;
    double cost_beta;

    double r;
    double bf;  /* Boltzmann Factor p(alpha -> beta) */


    printf("** Sequential Simulated Annealing Solver\n");

    /* Allocate data structures. */
    malloced = 0;
    freed = 0;
    tg = new_task_graph_from_file(stg_filename, &malloced);
    status = new_status(&malloced);
    strncpy(status->name, "YASA Sequential", 20);

    eps = 1e-3;
    seed = 0;
    t0 = 1000.0;

    alpha = NULL;
    beta = NULL;

    /* Solve. */
    srand(seed);
    print_task_graph(tg);

    malloced_initial = 0;
    freed_initial = 0;
    alpha = create_initial_solution(tg, &malloced_initial, &freed_initial);
    printf("malloced %d bytes for initial solution\n", malloced_initial);
    printf("freed %d bytes for initial solution\n", freed_initial);
    printf("difference: %d bytes\n", malloced_initial - freed_initial);

    cost_alpha = cost(tg, alpha);

    i = 0;
    t = t0;
    
    while (t > eps) {
        printf("i = %d, t = %f\n", i, t);

        malloced_select = 0;
        freed_select = 0;
        beta = select_neighbour(tg, alpha, &malloced_select, &freed_select);
        printf("malloced %d bytes for selection\n", malloced_select);
        printf("freed %d bytes for selection\n", freed_select);
        printf("difference: %d bytes\n", malloced_select - freed_select);
        cost_beta = cost(tg, beta);

        if (cost_beta <= cost_alpha) {
            /* TODO alpha := beta */
            cost_alpha = cost_beta;
        } else {
            r = get_random_r();  /* r from (0, 1) */
            bf = boltzmann_factor(t, cost_alpha, cost_beta);
            printf("r = %f, bf = %f\n", r, bf);
            if (r < bf) {
                /* TODO alpha := beta */
                cost_alpha = cost_beta;
            }
        }
        i++;
        t = new_temp(t, i);
    }
    printf("stopping at i = %d, t = %f.\n", i, t);

    /* alpha to schedule */
    //schedule = new_schedule(tg, alpha, &malloced);
    schedule = new_schedule(tg, &malloced);
    printf("malloced %d bytes\n", malloced);

    /* Display Results */
    print_schedule(schedule);
    write_schedule_to_file(ssf_filename, schedule, status);

    /* Free data structures. */
    free_schedule(schedule, &freed);
    // TODO free initial solution
    free_status(status, &freed);
    free_task_graph(tg, &freed);
    printf("freed %d bytes => ", freed);
    if (malloced == freed)
        printf("OK.\n");
    else {
        printf("Error: malloced != freed!\n");
        return (1);
    }

    return (0);
}