Example #1
0
/* Pushes local variable var node on top of the stack */
int varToStack(astNode* varNode, symTab* symtab) {
	char* id = varNode->leftMostChild->id;
	symbol* sym = seekExecutionLocal(id, symtab);
	if(sym == NULL) {
		sym = seekExecutionGlobal(id, symtab);
		if(sym == NULL) {
			printf("\n\nVAR DUMPED %s \n\n", id);
			return -1;
		}
		else {
			int offset = offsetVariable(varNode);
			if(offset < 0)
				return -1;
			fprintf(yyoutasm, "\tla\t$t0, %s\t# Load address of %s into $t0\n", id+1, id+1);
			fprintf(yyoutasm, "\tlw\t$t0, %d($t0)\t# Load value of %s[%d] into $t0\n", offset*4, id+1, offset);
			pushStackReg("$t0", symtab);
			return 0;
		}
	}
	int offset = offsetVariable(varNode);
	if(offset < 0)
		return -1;
	if(sym->scope == S_ARG)
		fprintf(yyoutasm, "\tlw\t$t0, %d($sp)\t# Load value of %s[%d] into $t0\n", (symtab->sp-(sym->address+offset+1))*4, id, offset);
	else
		fprintf(yyoutasm, "\tlw\t$t0, %d($sp)\t# Load value of %s[%d] into $t0\n", (symtab->sp-(sym->address+offset+1)-3)*4, id, offset);
	pushStackReg("$t0", symtab);
	return 0;
}
Example #2
0
void offsetVariable(t_tree node, int formal, int local)
{
	if (node == NULL)
		return;
	if (node->Node.Variable.VarKind == kFormal)
		offsetFormal(node, &formal);
	else
		offsetLocal(node, &local);

	offsetVariable(node->Node.Variable.Next, formal, local);
}
Example #3
0
void codeGenAffect(astNode* root, symTab* symtab) {
	astNode* child = root->leftMostChild;
	fprintf(yyoutasm, "\t## Affectation\n");
	if(child != NULL && child->rightSibling != NULL && child->rightSibling->type == EXPR) {
		short offset = offsetVariable(child);
		if(offset >= 0) {
			if(computeExpression(child->rightSibling, symtab) >= 0) { 	// Compute expression pushes result on the stack
				stackToVar(child->leftMostChild->id, offset, symtab);	// Pop stack to variable 
			}
		}
	}
	fprintf(yyoutasm, "\t## End affectation\n");
}
Example #4
0
void offsetFunction(t_tree node)
{
	if (node == NULL)
		return;

	scope = FindId(node->Node.Function.Name, scope);
	
	// go through var list and calc offsets..
	// formals start at offset 2 and locals start at offset 0
	offsetVariable(node->Node.Function.Variables, 2, 0);

	scope = scope->parent;

	offsetFunction(node->Node.Function.Next);
}