Commit 1d372730 by Andrew Dahl

ref #14 Implemented global stack pointer -- Function's don't work

parent 4ecd8588
......@@ -44,8 +44,8 @@ PUSH dword [tempf]\n";
const string INSTRUCTION_POP_STACK_TO_FPU=
"FLD QWORD [ESP]\n\
POP DWORD EBP\n\
POP DWORD EBP\n";
POP DWORD EBX\n\
POP DWORD EBX\n";
const string INSTRUCTION_CONVERT_INT_TO_FPU=
"POP DWORD [tempi]\n\
......
......@@ -51,6 +51,8 @@ class Symbol {
enum Type getReturnType() const;
int getArguments(bool isFunction=false);
bool operator==(const Symbol *rhs) const;
void setStackAddress(int address);
int getStackAddress() const;
string toString() const;
......@@ -60,6 +62,7 @@ private:
Type valueType;
Type returnType;
Symbol* f; // These will be variables for the function
int stack_address;
};
#endif /* SYMBOL_H_ */
......@@ -31,8 +31,9 @@ public:
Symbol* FindConstant(const float value);
Symbol* Find(const string lexeme);
bool ExistsInCurrentLevel(const string lexeme);
void setParentTable(int parent_table_num, SymbolTable* parent_table);
int getParentTable() const;
void addStackAddress(int size);
int getStackAddress() const;
void Print();
......@@ -42,6 +43,7 @@ private:
map<string,Symbol*> table;
SymbolTable* parent_table;
int parent_table_num;
int stack_address;
};
#endif /* SYMBOLTABLE_H_ */
......@@ -155,7 +155,7 @@ string InstructionList::toString()
* -Andrew
*
* TODO: This needs to be made into a function now... primarily so that we can handle the stack
* variables at the start of functions... so, when we run into a functio or a block, we'll need
* variables at the start of functions... so, when we run into a function or a block, we'll need
* to call this function so that it allocates all the required stuff...
*/
for(it = this->instructions.begin(); it != this->instructions.end(); it++)
......@@ -164,6 +164,7 @@ string InstructionList::toString()
if(it->second[0]->getOpcode() != Instruction::FUNCTION_START)
{
oss << "SUB ESP, " << varAddrSpace << endl;
oss << "MOV EBP, ESP" << endl;
isFunction = false;
}
else
......@@ -260,7 +261,7 @@ string InstructionList::toString()
oss << "pint\n"
<< "POP EBX\n";
else
oss << "FLD qword [esp]\n"
oss << "FLD qword [ESP]\n"
<< "pfloat\n"
<< "POP EBX\n"
<< "POP EBX\n";
......@@ -280,10 +281,10 @@ string InstructionList::toString()
break;
case Symbol::VARIABLE:
if(temp->getArg1().sym->getReturnType() == Symbol::FLOAT) {
oss << "PUSH dword [esp+" << (this->getStackAddress(temp->getArg1().sym) + 4) << "]\n";
oss << "PUSH dword [esp+" << (this->getStackAddress(temp->getArg1().sym) +4) << "]\n";
oss << "PUSH DWORD [EBP+" << (temp->getArg1().sym->getStackAddress() + 4) << "]\n";
oss << "PUSH DWORD [EBP+" << (temp->getArg1().sym->getStackAddress()) << "]\n";
} else
oss << "PUSH dword [esp+" << this->getStackAddress(temp->getArg1().sym) << "]\n";
oss << "PUSH DWORD [EBP+" << temp->getArg1().sym->getStackAddress() << "]\n";
break;
default:
oss << "NOT IMPLEMENTED\n";
......@@ -319,13 +320,13 @@ string InstructionList::toString()
<< INSTRUCTION_POP_FPU_TO_STACK
<< "POP EAX\n"
<< "POP EBX\n"
<< "MOV [esp+" << (this->getStackAddress(temp->getArg1().sym) - 4) << "], EAX\n"
<< "MOV [esp+" << this->getStackAddress(temp->getArg1().sym) << "], EBX\n"
<< "MOV [EBP+" << temp->getArg1().sym->getStackAddress() << "], EAX\n"
<< "MOV [EBP+" << (temp->getArg1().sym->getStackAddress() + 4) << "], EBX\n"
<< "PUSH EDX\n";
break;
default:
oss << "POP EAX\n"
<< "MOV [esp+" << (this->getStackAddress(temp->getArg1().sym) - 4) << "], EAX\n"
<< "MOV [EBP+" << (temp->getArg1().sym->getStackAddress()) << "], EAX\n"
<< "PUSH EAX\n";
break;
}
......@@ -338,14 +339,14 @@ string InstructionList::toString()
oss << "FLD QWORD [ESP]\n"
<< INSTRUCTION_CONVERT_FLOAT_TO_INT
<< "POP EAX\n"
<< "MOV [esp+" << this->getStackAddress(temp->getArg1().sym) << "], EAX\n";
<< "MOV [EBP+" << temp->getArg1().sym->getStackAddress() << "], EAX\n";
break;
default:
//Move
oss << "POP EAX\n"
<< "POP EBX\n"
<< "MOV [esp+" << (this->getStackAddress(temp->getArg1().sym) - 8) << "], EAX\n"
<< "MOV [esp+" << (this->getStackAddress(temp->getArg1().sym) - 4) << "], EBX\n"
<< "MOV [EBP+" << (temp->getArg1().sym->getStackAddress()) << "], EAX\n"
<< "MOV [EBP+" << (temp->getArg1().sym->getStackAddress() + 4) << "], EBX\n"
<< "PUSH EBX\n"
<< "PUSH EAX\n";
break;
......@@ -360,7 +361,7 @@ string InstructionList::toString()
case Instruction::CALL:
//oss << "CALL "<< temp->getArg1().sym->getLexeme() << "\n";
//this->stackPop(temp->getArg1().sym->getArguments(true));
oss << FunctionToString(temp, this->stackPushFunctionParam(temp->getArg2().sym->getFunctionValue()));
////oss << FunctionToString(temp, this->stackPushFunctionParam(temp->getArg2().sym->getFunctionValue()));
/*if(temp->getArg1().sym->getReturnType() == Symbol::FLOAT)
{
this->stackPush(new Symbol("temp", Symbol::FLOAT, Symbol::FLOAT));
......@@ -404,7 +405,7 @@ string InstructionList::toString()
//TODO: Get rid of this... it's just a temporary fix for functions
string InstructionList::FunctionToString(Instruction* inst, int varAddrSpace) {
/*string InstructionList::FunctionToString(Instruction* inst, int varAddrSpace) {
stringstream oss;
Instruction* temp;
map<int, map<int, Instruction*> >::iterator it;
......@@ -634,7 +635,7 @@ string InstructionList::FunctionToString(Instruction* inst, int varAddrSpace) {
}
return oss.str();
}
}*/
void InstructionList::Print()
{
#ifdef COMPILER_VERBOSE
......
......@@ -199,6 +199,16 @@ bool Symbol::operator==(const Symbol *rhs) const
return false;
}
void Symbol::setStackAddress(int address)
{
this->stack_address = address;
}
int Symbol::getStackAddress() const
{
return this->stack_address;
}
string Symbol::toString() const
{
stringstream oss;
......@@ -215,6 +225,7 @@ string Symbol::toString() const
oss << this->value.s->toString();
if(this->f != NULL)
oss << this->f->toString();
oss << " " << this->getStackAddress();
break;
case FLOAT:
oss << "Float\t" << this->value.f;
......@@ -237,7 +248,7 @@ string Symbol::toString() const
default:
break;
}
oss << "\n";
switch(this->returnType)
{
case INTEGER:
......
......@@ -16,6 +16,7 @@ SymbolTable::SymbolTable(int parent_table_num, SymbolTable* parent_table)
{
this->parent_table = parent_table;
this->parent_table_num = parent_table_num;
this->stack_address = 0;
}
int SymbolTable::Count() const
......@@ -30,7 +31,17 @@ Symbol* SymbolTable::Insert(Symbol *entry)
it = this->table.find(entry->getLexeme());
if(it == this->table.end())
{
this->table.insert( std::pair<string,Symbol*>(entry->getLexeme(),entry));
if(entry->getType() == Symbol::VARIABLE)
{
entry->setStackAddress(this->getStackAddress());
if(entry->getReturnType() == Symbol::FLOAT)
this->addStackAddress(8);
else
this->addStackAddress(4);
}
}
else
entry = it->second;
......@@ -83,15 +94,19 @@ bool SymbolTable::ExistsInCurrentLevel(const string lexeme)
return true;
}
void SymbolTable::setParentTable(int parent_table_num, SymbolTable* parent_table)
int SymbolTable::getParentTable() const
{
this->parent_table_num = parent_table_num;
this->parent_table = parent_table;
return this->parent_table_num;
}
int SymbolTable::getParentTable() const
void SymbolTable::addStackAddress(int size)
{
return this->parent_table_num;
this->stack_address += size;
}
int SymbolTable::getStackAddress() const
{
return this->stack_address;
}
void SymbolTable::Print()
......@@ -108,7 +123,7 @@ void SymbolTable::Print()
{
case Symbol::INTEGER:
case Symbol::VARIABLE:
std::cerr << it->second->getLexeme() << " " << it->second->getValue().i;
std::cerr << it->second->getLexeme() << " " << it->second->getValue().i << " " << it->second->getStackAddress();
break;
case Symbol::FLOAT:
std::cerr << it->second->getLexeme() << " " << it->second->getValue().f;
......
a=readInput
b=readInput + 1.0
c=readInput + 2.5
function func(a,b,c,d)=a+b+c
function func2()=10
function func1(a,b)=func(a,b,1,readInput) - func2()
function outFunc(a,b,c)
{
int a=readInput
float b=readInput + 1.0
int c=readInput + 2.49999
output readInput
output a
output b
output c
return a+b+c
}
output readInput + func(func1(readInput,a),2,func2(),0)
d=outFunc(1,2,3)
output a
output b
output c
output d
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