Commit 0736eb76 by Andrew Dahl

Functions work without arguments

parent f0539f17
Showing with 46 additions and 11 deletions
......@@ -89,6 +89,7 @@ string InstructionList::toString()
int varAddrSpace;
map<int, map<int, Instruction*> >::iterator it;
map<int, Instruction*>::iterator itr;
bool isFunction = false;
// Note: The following is a pointer in case we need to pass it later... Maybe okay to change?
vector<Symbol*>::iterator itvar;
......@@ -121,7 +122,13 @@ string InstructionList::toString()
for(it = this->instructions.begin(); it != this->instructions.end(); it++)
{
varAddrSpace = this->findVarStackSpace(it->second);
oss << "SUB ESP, " << varAddrSpace << endl;
if(it->second[0]->getOpcode() != Instruction::FUNCTION_START)
{
oss << "SUB ESP, " << varAddrSpace << endl;
isFunction = false;
}
else
isFunction = true;
for(itr = it->second.begin(); itr != it->second.end(); itr++)
{
temp = itr->second;
......@@ -134,7 +141,8 @@ string InstructionList::toString()
if(this->getTopTwoTypes() == Symbol::FLOAT)
{
oss << "FADD ST1\n" //Add ST0 to top of FPU Stack
<< INSTRUCTION_POP_FPU_TO_STACK;
<< INSTRUCTION_POP_FPU_TO_STACK
<< "FSTP QWORD [tempf] ; Clearing FPU\n";
this->stackPush(Symbol::FLOAT);
}
else
......@@ -151,7 +159,8 @@ string InstructionList::toString()
if(this->getTopTwoTypes() == Symbol::FLOAT)
{
oss << "FSUB ST1\n" //Sub ST0 and top of FPU Stack
<< INSTRUCTION_POP_FPU_TO_STACK;
<< INSTRUCTION_POP_FPU_TO_STACK
<< "FSTP QWORD [tempf] ; Clearing FPU\n";
this->stackPush(Symbol::FLOAT);
}
else
......@@ -168,7 +177,8 @@ string InstructionList::toString()
if(this->getTopTwoTypes() == Symbol::FLOAT)
{
oss << "FDIV ST1\n" //Sub ST0 and top of FPU Stack
<< INSTRUCTION_POP_FPU_TO_STACK;
<< INSTRUCTION_POP_FPU_TO_STACK
<< "FSTP QWORD [tempf] ; Clearing FPU\n";
this->stackPush(Symbol::FLOAT);
}
else
......@@ -186,7 +196,8 @@ string InstructionList::toString()
if(this->getTopTwoTypes() == Symbol::FLOAT)
{
oss << "FMUL ST1\n" //Sub ST0 and top of FPU Stack
<< INSTRUCTION_POP_FPU_TO_STACK;
<< INSTRUCTION_POP_FPU_TO_STACK
<< "FSTP QWORD [tempf] ; Clearing FPU\n";
this->stackPush(Symbol::FLOAT);
}
else
......@@ -306,21 +317,43 @@ string InstructionList::toString()
break;
case Instruction::CALL:
oss << "CALL "<< temp->getArg1().sym->getLexeme() << "\n";
if(temp->getArg1().sym->getReturnType() == Symbol::FLOAT)
{
this->stackPush(new Symbol("temp", Symbol::FLOAT, Symbol::FLOAT));
oss << INSTRUCTION_POP_FPU_TO_STACK;
}
else
{
this->stackPush(new Symbol("temp", Symbol::INTEGER, Symbol::INTEGER));
oss << "PUSH EBX\n";
}
break;
case Instruction::FUNCTION_START:
// Do we care?
oss << "NOT IMPLEMENTED\n";
// Do we care? Yes.
oss << endl << temp->getArg1().sym->getLexeme() << ":\n";
break;
case Instruction::VARDECL:
// This is just a placeholder... the only purpose for VARDECL is for determining space needed on the stack
break;
}
}
if(!isFunction)
oss << "ADD ESP, " << varAddrSpace << endl
<< "MOV EAX, 0\n"
<< "RET\n";
else
{
if(this->getTopType() == Symbol::FLOAT)
oss << INSTRUCTION_POP_STACK_TO_FPU;
else
oss << "POP EBX\n";
oss << "ADD ESP, " << varAddrSpace << endl;
}
oss << "ADD ESP, " << varAddrSpace << endl;
}
oss << "MOV EAX, 0\n"
<< "RET\n";
}
return oss.str();
}
......
......@@ -57,11 +57,13 @@ function_define: FUNCTION ID LPAREN argument_exp_list RPAREN EQUALS {
symbol_table.push_back(new SymbolTable(level, symbol_table[level]));
level = symbol_table.size()-1;
symbol_table[level]->Insert(temp);
instructions->levelPush();
instructions->addInstruction(new Instruction(Instruction::FUNCTION_START, temp));
} additive_exp {
Symbol* temp = symbol_table[level]->Find($2);
level = symbol_table[level]->getParentTable();
temp->setReturnType($8->getReturnType());
instructions->levelPop();
$$ = temp;
}
;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment