void visitSelfCall(SelfCall *p) { fprintf( m_outputfile, "#### SELF CALL\n"); int args; Symbol * symbP; SymScope * sync; MethodIDImpl * MethIdP = dynamic_cast<MethodIDImpl*>(p->m_methodid); char * funcName = strdup(MethIdP->m_symname->spelling()); sync = m_symboltable->get_current_scope(); symbP = sync->lookup((const char *)"xxx"); p->visit_children(this); args = p->m_expression_list->size(); args = args * wordsize; // cout<<"the number of params: "<<args<<endl; char * className = strdup(symbP->classType.classID); strcat(className,"_"); strcat(className,funcName); fprintf( m_outputfile, "call %s\n",className); fprintf( m_outputfile, "addl $%d , %%esp\n",args); // WRITEME }
Symbol * InScope(char* key){// before calling this function make sure there is a parent scope Symbol * symbP; SymScope *sync; ClassNode * parent; ClassNode * clasp; char* parentName; bool found; string pName; int i =0; // cout<<" the key is "<<key<<endl; symbP = m_symboltable->lookup(key); if(!m_symboltable->exist(key)){ symbP = m_symboltable->lookup((const char *)"xxx"); char * Cname = strdup(symbP->classType.classID); parent = m_classtable->lookup(Cname); // cout<<"parent: "<<parent->name->spelling()<<endl; sync = parent->scope; symbP = sync->lookup(key); found = sync->exist(key); while(found == false){ ClassName * classGreat = new ClassName(Cname); ClassNode* pNode = m_classtable->getParentOf(classGreat); Cname = strdup(pNode->name->spelling()); std::string pName(pNode->name->spelling()); // cout<<"the parent name: "<<Cname<<endl; if( pName == "TopClass"){ return NULL; } sync = pNode->scope; found = sync->exist(key); symbP = sync->lookup(key); i++; } } return symbP; }
Symbol* SymScope::lookup( const char * name ) { //first check the current table; ScopeTableType::const_iterator i; i = m_scopetable.find( name ); if ( i != m_scopetable.end() ) { return i->second; } //failing that, check all the parents; if ( m_parent != NULL ) { return m_parent->lookup( name ); } else { //if this has no parents, then it cannot be found return NULL; } }
void visitProgramImpl(ProgramImpl *p) { p->visit_children(this); string x = "Program"; char* a = strdup("Program"); ClassName * cName = new ClassName(a); if(!m_classtable->exist(cName)){ t_error(no_program, p->m_attribute); } ClassNode* classnode = m_classtable->lookup(cName); SymScope * sync = classnode->scope; char * s = strdup("start"); // char * methodName = (s); Symbol * f= sync->lookup(s); if((f == NULL) || (f->methodType.returnType.baseType != 16)){ t_error(no_start, p->m_attribute); } if(f->methodType.argsType.size() != 0){ t_error(start_args_err, p->m_attribute); } printSymTab(); //WRITE ME }
void visitMethodCall(MethodCall *p) { p->visit_children(this); Symbol* symp; ClassNode * clasp; Basetype bargs; int i =0; Symbol * test; VariableIDImpl * VarIdP = dynamic_cast<VariableIDImpl*>(p->m_variableid); MethodIDImpl * MethIdP = dynamic_cast<MethodIDImpl*>(p->m_methodid); char * meth = strdup(MethIdP->m_symname->spelling()); test = m_symboltable->lookup(meth); // Basetype bargs; char * var = strdup(VarIdP->m_symname->spelling()); // cout<<"the var : "<<var<<endl; if( !m_symboltable->exist(var)){//if symbol not defined within class t_error(sym_name_undef, p->m_attribute); } symp = InScope(var) ;//~!~!~!~!~!~!~!~ place here if(symp->baseType != 8){ t_error(sym_type_mismatch,p->m_attribute); } char *name = strdup(symp->classType.classID); clasp = m_classtable->lookup(name); // cout<<name<<endl; if(!m_classtable->exist(name)){ t_error(sym_name_undef,p->m_attribute); } // SymScope *sync = clasp->scope; char * className= strdup(symp->classType.classID); ClassNode* classnode = m_classtable->lookup(className); SymScope * sync = classnode->scope; symp= sync->lookup(meth); //symp = InScope(meth);//~!~!~!~!~!~!~!~ place here if(symp == NULL){ t_error(no_class_method, p->m_attribute); } if(p->m_expression_list->size()!= symp->methodType.argsType.size() ) t_error(call_args_mismatch,p->m_attribute); typename std::list<Expression_ptr>::iterator it = p->m_expression_list->begin(); for( ; it != p->m_expression_list->end(); ++it) { Expression * Expoin = dynamic_cast<Expression *> (*it); bargs = symp->methodType.argsType[i].baseType; if(bargs != Expoin->m_attribute.m_type.baseType) t_error(call_args_mismatch, p->m_attribute); i++; } p->m_attribute.m_type.baseType = symp->methodType.returnType.baseType; p->m_attribute.m_type.methodType.returnType.baseType = symp->methodType.returnType.baseType; //WRITE ME }
void visitMethodImpl(MethodImpl *p) { fprintf( m_outputfile, "#### METHOD IMPLEMENTATION\n"); int localSpace, args, mem; int j=0; int methMem = 0; CompoundType info; currMethodOffset = new OffsetTable(); // cout<<"before my childen"<<endl; //this is to make the label name Symbol * symbP; SymScope * sync; MethodIDImpl * MethIdP = dynamic_cast<MethodIDImpl*>(p->m_methodid); char * funcName = strdup(MethIdP->m_symname->spelling()); sync = m_symboltable->get_current_scope(); symbP = sync->lookup((const char *)"xxx"); char * classMethName = strdup(symbP->classType.classID); strcat(classMethName,"_"); strcat(classMethName,funcName); fprintf( m_outputfile, "_%s:\n",classMethName); //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ fprintf( m_outputfile, " pushl %%ebp\n"); fprintf( m_outputfile, " movl %%esp , %%ebp\n"); MethodBodyImpl * MethBodP = dynamic_cast<MethodBodyImpl*>(p->m_methodbody); localSpace = (p->m_parameter_list->size() + MethBodP->m_declaration_list->size()); localSpace = localSpace * wordsize; // currMethodOffset->insert(classMethName, localSpace, 4,symbP->classType); currMethodOffset->setTotalSize(localSpace); currMethodOffset->setParamSize(p->m_parameter_list->size() * wordsize); //### inserting paramaters into the offset table ########### for (std::list<Parameter_ptr>::iterator it = p->m_parameter_list->begin() ; it != p->m_parameter_list->end(); ++it){ ParameterImpl * param = dynamic_cast<ParameterImpl*>(*it); VariableIDImpl * VarId = dynamic_cast<VariableIDImpl*>(param->m_variableid); info.baseType = param->m_type->m_attribute.m_type.baseType; if(info.baseType == 8){ info.classID = param->m_type->m_attribute.m_type.classType.classID; } else{ info.classID = "NO CLASS"; } methMem -= 4; // cout<<"Offset-> symname: "<<VarId->m_symname->spelling()<<" offset: "<<methMem<<" class type: " <<info.baseType<<endl; currMethodOffset->insert(VarId->m_symname->spelling(), methMem, 4,info); } //################################ //<><>Diving into Declaration <><><><>><><><><><><><><><><><>><><><><><>< typename std::list<Declaration_ptr>::iterator it = MethBodP->m_declaration_list->begin(); for( ; it != MethBodP->m_declaration_list->end(); ++it) { DeclarationImpl * DeclaP = dynamic_cast<DeclarationImpl *> (*it); typename std::list<VariableID_ptr>::iterator it = DeclaP->m_variableid_list->begin(); for( ; it != DeclaP->m_variableid_list->end(); ++it) { methMem -= 4; // need to move to the next offset VariableIDImpl * VarIdP = dynamic_cast<VariableIDImpl*>(*it); char * var = strdup(VarIdP->m_symname->spelling()); // cout<<"Offset-> symname: "<<var<<" Offset: "<<methMem<<" Class type: " <<endl; info.baseType = DeclaP->m_type->m_attribute.m_type.baseType; if(info.baseType == 8){ info.classID = DeclaP->m_type->m_attribute.m_type.classType.classID; } else{ info.classID = "NO CLASS"; } currMethodOffset->insert(var, methMem, 4,info); } } //<><><><><><><><><><><><><><><><><>><><><><><>< //~~~~ allocating space on the stack and moves parameters into local ~~~~~~ // cout<<"param size: "<<currMethodOffset->getParamSize()<<endl; // cout<<" LocalSpace: "<< -(methMem)<<endl; fprintf( m_outputfile, " subl $%d, %%esp\n",-(methMem)); mem = -4; for(int i = currMethodOffset->getParamSize() + 4; i>= 8; i = i-4){ fprintf( m_outputfile, " movl %d(%%ebp) , %%eax\n",i); fprintf( m_outputfile, " movl %%eax , %d(%%ebp)\n",mem); mem -= 4; // symbP->methodType.argsType[j].baseType; } //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ p->visit_children(this); // cout<<"after the children"<<endl; fprintf( m_outputfile, " leave\n"); fprintf( m_outputfile, " ret\n"); // WRITEME }