void AssignmentStatement::evaluate(SymTab &symTab, std::unique_ptr<FuncTab> &funcTab) { if(_lhsExpression == nullptr) { symTab.setValueFor(lhsVariable(), rhsExpression()->evaluate(symTab, funcTab)); } else { auto lhsindex = dynamic_cast<NumberDescriptor*> (_lhsExpression->evaluate(symTab, funcTab).get()); if(lhsindex->type() != TypeDescriptor::INTEGER) { std::cout << "AssignmentStatement::evaluate error index must be"; std::cout << " an integer\n"; exit(1); } int index = lhsindex->value.intValue; auto lhs = symTab.getValueFor(lhsVariable()); auto rhs = _rhsExpression->evaluate(symTab, funcTab); if(lhs->type() == TypeDescriptor::STRINGARRAY) { if(rhs->type() != TypeDescriptor::STRING){ std::cout<<"array value not of compatible types"<<std::endl; exit(1); } auto desc = dynamic_cast<StringDescriptor*>(rhs.get()); std::string val = desc->value; dynamic_cast<StringArray*>(lhs.get())->setSubStr(index, val); } else if(lhs->type() == TypeDescriptor::NUMBERARRAY) { if(rhs->type() != TypeDescriptor::INTEGER){ std::cout<<"array value not of compatible types"<<std::endl; exit(1); } auto desc = dynamic_cast<NumberDescriptor*>(rhs.get()); int val = desc->value.intValue; dynamic_cast<NumberArray*>(lhs.get())->setSubNum(index, val); } } }
void visitFuncImpl(FuncImpl * p) { p->m_attribute.m_scope = m_st->get_scope(); p->m_type->accept(this); add_func_symbol(p); m_st->open_scope(); p->visit_children(this); m_st->close_scope(); check_return_type(p); }
//Add catch type string into symbol tabel to speed up string comparation. static void addCatchTypeName(DexRegion * rg) { Dex2IR * d2ir = rg->getDex2IR(); SymTab * symtab = rg->getRegionMgr()->getSymTab(); for (TryInfo * ti = d2ir->getTryInfo(); ti != NULL; ti = ti->next) { for (CatchInfo * ci = ti->catch_list; ci != NULL; ci = ci->next) { ASSERT0(ci->kindname); symtab->add(ci->kindname); } } }
void visitIdent(Ident * p) { p -> m_attribute.m_scope = m_st->get_scope(); p -> visit_children(this); Symbol *newSym; newSym = m_st->lookup( p->m_symname->spelling()); if(!newSym) this->t_error(var_undef, p->m_attribute); p->m_attribute.m_basetype = newSym->m_basetype; }
void visitClassImpl(ClassImpl *p) { int stop; m_symboltable->open_scope(); Symbol * symPtr = new Symbol; // = new ClassNode; Symbol * symp;// = new Symbol; ClassIDImpl * ClassIdP = dynamic_cast<ClassIDImpl*>(p->m_classid_1); ClassIDImpl * ClassIdP2 = dynamic_cast<ClassIDImpl*>(p->m_classid_2); char * key1 = strdup(ClassIdP->m_classname->spelling()); symPtr->classType.classID = ClassIdP->m_classname->spelling(); char * progFinder = strdup("Program"); ClassName * nm = new ClassName(key1); if(progger == true){ t_error(no_program, p->m_attribute); } if(std::string(key1) == std::string(progFinder)){ progger = true; } //char * key2 = strdup(ClassIdP2->m_classname->spelling()); if(m_classtable->exist(key1)){ t_error(dup_ident_name, p->m_attribute); } else{ if(ClassIdP2->m_classname != NULL){ char * key2 = strdup(ClassIdP2->m_classname->spelling()); ClassName * nm2 = new ClassName(key2); // cout<<"inserted class: " <<key1 <<" from :"<<key2 <<endl; m_classtable->insert(nm, nm2, p, m_symboltable->get_scope()); ClassNode * clasp = m_classtable->getParentOf(key2); // cout<<"Class: " <<key1 <<", Super class: "<<clasp->name->spelling()<<endl; } else{ // cout<< "instered this in class table: "<< key1 <<endl; m_classtable->insert(nm, NULL, p, m_symboltable->get_scope()); } } m_symboltable->insert((char *)"xxx", symPtr); p->visit_children(this); m_symboltable->close_scope(); //WRITE ME }
/* generate declarations for global variables used in the program */ void generateDecls( const SymTab &curSymTab ) { for ( SymTab::const_iterator curSym = curSymTab.begin( ); curSym != curSymTab.end( ); ++curSym ) { if ( curSym -> second.isReg( ) == true || curSym -> second.Type( ) == FUNC || curSym -> second.isLocal( ) ) continue; curSym -> second.declare( curSym -> first ); } cout << "\n\n"; }
Type* GlobalEntry::typeCheck(){ SymTab* st = this->symTab(); for(SymTab::iterator i = st->begin(); i != st->end(); ++i){ (*i)->typeCheck(); } //Loops over all Rules for(unsigned int x = 0; x < rules_.size();++x){ rules_[x]->typeCheck(); } return NULL; }
void visitMethodImpl(MethodImpl *p) { m_symboltable->open_scope(); if (p->m_type != NULL) { p->m_type->accept( this ); } else { this->visitNullPointer(); } // cout<<"my type: "<<p->m_type->m_attribute.m_type.baseType<<endl; Symbol *symp = new Symbol; Symbol *Msymp = new Symbol; Symbol *test; MethodIDImpl * MethIdP = dynamic_cast<MethodIDImpl*>(p->m_methodid); p->m_attribute.m_type.baseType = p->m_type->m_attribute.m_type.baseType; //this is dealing with duplicate method names char * key = strdup(MethIdP->m_symname->spelling());//~!~!~!~!~!~!~!~ place here test = InScope(key); if(test != NULL){//change this make Msymp = my function t_error(dup_ident_name, p->m_attribute); } else{ CompoundType arg; p->visit_children(this); //Adding parameters to sym table and each argument to method vector typename std::list<Parameter_ptr>::iterator it = p->m_parameter_list->begin(); for( ; it != p->m_parameter_list->end(); ++it) { ParameterImpl * Pimp = dynamic_cast<ParameterImpl *> (*it); VariableIDImpl * VarIdP = dynamic_cast<VariableIDImpl*>(Pimp->m_variableid); //char * key = strdup(VarIdP->m_symname->spelling()); arg.baseType = Pimp->m_type->m_attribute.m_type.baseType; Msymp->methodType.argsType.push_back(arg); } Msymp->methodType.returnType.baseType = p->m_type->m_attribute.m_type.baseType; Msymp->baseType = p->m_type->m_attribute.m_type.baseType; m_symboltable->insert_in_parent_scope(key, Msymp); } if(p->m_type->m_attribute.m_type.baseType != p->m_methodbody->m_attribute.m_type.baseType){ t_error(ret_type_mismatch, p->m_attribute); } m_symboltable->close_scope(); //WRITE ME }
void visitCall(Call * p) { int n = 0; list<Expr_ptr>::reverse_iterator iter; for(iter = p->m_expr_list->rbegin();iter != p->m_expr_list->rend();iter++) { n = n + wordsize; (*iter)->accept( this ); } Symbol *x = m_st->lookup(p->m_attribute.m_scope, p->m_symname_1->spelling()); fprintf(m_outputfile, "call %s\n", strdup(p->m_symname_2->spelling())); fprintf(m_outputfile, "addl $%d, %%esp\n",n); fprintf( m_outputfile, "movl %%eax, -%d(%%ebp)\n", (m_st->lookup(p->m_attribute.m_scope, p->m_symname_1->spelling()))->get_offset() + 4); }
void EventEntry::memAlloc(MemoryMgr &mm){ memory_mgr_ = MemoryMgr(); //cout << "Event memAlloc" << endl; SymTab* st = this->symTab(); if(st!=NULL){ for(SymTab::iterator i = st->begin(); i != st->end();++i){ if((*i)->kind()==SymTabEntry::Kind::VARIABLE_KIND){ VariableEntry* ve = (VariableEntry*) (*i); if(ve->varKind()==VariableEntry::VarKind::PARAM_VAR){ ve->offSet(memory_mgr_.getNextAddress()); } } } } }
void visitParameterImpl(ParameterImpl *p) { p->visit_children(this); Symbol *symp = new Symbol; Symbol * test; VariableIDImpl * VarIdP = dynamic_cast<VariableIDImpl*>(p->m_variableid); char * key = strdup(VarIdP->m_symname->spelling()); test = InScope(key); if(test!=NULL){ t_error(dup_ident_name, p->m_attribute); } else{ symp->baseType = p->m_type->m_attribute.m_type.baseType; // cout<< "param; key: "<< key <<" type : "<< symp->baseType<<endl; if(symp->baseType == 8){ symp->classType.classID = p->m_type->m_attribute.m_type.classType.classID; // cout<< "this is what i'm SEETTTING key: " << symp->classType.classID<<endl; } // cout<<"insertingthis var from param: "<<key <<endl; m_symboltable->insert(key, symp); } //WRITE ME }
void Statements::evaluate(SymTab &symTab, std::unique_ptr<FuncTab> &funcTab) { for (auto &s: _statements) { if(symTab.isDefinedGlobal(RETURN)) break; s->evaluate(symTab, funcTab); } }
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 }
void visitArrayCall(ArrayCall *p) { int n = 0; list<Expr_ptr>::reverse_iterator iter; for(iter = p->m_expr_list_2->rbegin();iter != p->m_expr_list_2->rend();iter++) { n = n + wordsize; (*iter)->accept( this ); } Symbol *x = m_st->lookup(p->m_attribute.m_scope, p->m_symname_1->spelling()); fprintf(m_outputfile, "call %s\n", strdup(p->m_symname_2->spelling())); fprintf(m_outputfile, "addl $%d, %%esp\n",n); x = m_st->lookup(p->m_attribute.m_scope, p->m_symname_2->spelling()); int xx=4+x->get_offset(); fprintf(m_outputfile, "movl %%eax, \t%d(%%ebp)\n", (-xx+(-1)*4*((IntLit*)p->m_expr_1)->m_primitive->m_data)); }
void visitBoolLit(BoolLit * p) { p -> m_attribute.m_scope = m_st->get_scope(); p -> visit_children(this); p -> m_attribute.m_basetype = bt_boolean; }
void visitAssignment(Assignment * p) { p->m_symname->accept( this ); p->m_expr->accept( this ); fprintf( m_outputfile, "popl %%eax\n"); fprintf( m_outputfile, "movl %%eax, -%d(%%ebp)\n", (m_st->lookup( p -> m_attribute.m_scope, p->m_symname->spelling()))->get_offset() + 4); }
void visitMagnitude(Magnitude * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); checkset_absolute(p, p->m_expr); p->m_attribute.m_basetype = bt_integer; }
void visitDiv(Div * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); p->m_attribute.m_basetype = bt_integer; checkset_arithexpr(p,p->m_expr_1, p->m_expr_2); }
void visitAssignment(Assignment * p) { p->visit_children(this); fprintf(m_outputfile, "\tpopl\t%%eax\n"); Symbol *s = m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling()); fprintf(m_outputfile, "\tmovl\t%%eax, %d(%%ebp)\n", -(4+s->get_offset()) ); }
void visitUminus(Uminus * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); checkset_uminus(p, p->m_expr); p->m_attribute.m_basetype = bt_integer; }
void GlobalEntry::memAlloc(MemoryMgr &mm){ memory_mgr_ = mm; parse_label_ = new Label(Label::LabelType::PARSE_START); end_label_ = new Label(Label::LabelType::END_PROGRAM); //Loops over all declarations SymTab* st = this->symTab(); for(SymTab::iterator i = st->begin(); i != st->end(); ++i){ (*i)->memAlloc(mm); } //Technically this happens between the two of them, but all registers should be free at this point in_reg_ = mm.getNextRegister(true); mm.addRegister(in_reg_); comp_reg_ = mm.getNextRegister(true); mm.addRegister(comp_reg_); mm.freeRegister(comp_reg_); //Loops over all Rules for(unsigned int x = 0; x < rules_.size();++x){ MemoryMgr mm_r; mm_r.addRegister(in_reg_); rules_[x]->memAlloc(mm_r); mm_r.freeRegister(in_reg_); if(rules_[x]->pat()->kind() == BasePatNode::PatNodeKind::PRIMITIVE){ PrimitivePatNode* pn = (PrimitivePatNode*)((PatNode*)rules_[x]->pat())->pat1(); EventEntry* ee = pn->event(); if(ee->name().length() == 1){ rule_names_.push_back(ee->name()); rule_labels_.push_back(rules_[x]->startLabel()); rule_return_labels_.push_back(rules_[x]->returnLabel()); } if(ee->name() == "any"){ any_labels_.push_back(rules_[x]->startLabel()); any_return_labels_.push_back(rules_[x]->returnLabel()); } } } mm.freeRegister(in_reg_); }
void visitLteq(Lteq * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); p->m_attribute.m_basetype = bt_boolean; checkset_relationalexpr(p,p->m_expr_1, p->m_expr_2); }
void visitCompare(Compare * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); p->m_attribute.m_basetype = bt_boolean; checkset_equalityexpr(p,p->m_expr_1, p->m_expr_2); }
Type* EventEntry::typeCheck(){ vector<Type*>* arg_types = new vector<Type*>(); SymTab* st = this->symTab(); if(st!=NULL){ for(SymTab::iterator i = st->begin(); i != st->end();++i){ if((*i)->kind()==SymTabEntry::Kind::VARIABLE_KIND){ VariableEntry* ve = (VariableEntry*) (*i); if(ve->varKind()==VariableEntry::VarKind::PARAM_VAR){ arg_types->push_back(ve->type()); } } } } type()->argTypes(arg_types); return NULL; }
void visitProgramImpl(ProgramImpl * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); check_for_one_main(p); m_st -> dump(stdout); }
void visitArrayAssignment(ArrayAssignment * p) { p->visit_children(this); fprintf(m_outputfile, "popl %%eax\n"); Symbol *x = m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling()); int xx=4+x->get_offset(); fprintf(m_outputfile, "movl %%eax, \t%d(%%ebp)\n", (-xx+(-1)*4*((IntLit*)p->m_expr_1)->m_primitive->m_data)); }
// variable and constant access void visitIdent(Ident * p) { fprintf(m_outputfile, "\t ## Ident ##\n"); p->visit_children(this); Symbol *s = m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling()); fprintf(m_outputfile, "\tpushl\t%d(%%ebp)\n", -(4+s->get_offset())); fprintf(m_outputfile, "\t ## END Ident ##\n"); }
void visitNot(Not * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); checkset_not(p, p->m_expr); p->m_attribute.m_basetype = bt_boolean; }
// check that the assigned-to variable exists and that the right-hand-side expression is valid // check that the return type of the expression matches the type of the variable void check_assignment( Assignment * p ) { Symbol *s; s = m_st->lookup(p->m_symname->spelling()); if(!s) this->t_error(var_undef, p->m_attribute); if(s->m_basetype - p->m_expr->m_attribute.m_basetype) this->t_error(incompat_assign, p->m_attribute); }
void visitForLoop(ForLoop * p) { p->m_attribute.m_scope = m_st->get_scope(); p->visit_children(this); if(!is_assignment(p->m_stat_1)) this->t_error(for_assign_err, p->m_attribute); if(!is_assignment(p->m_stat_2)) this->t_error(for_assign_err, p->m_attribute); check_for_pred(p->m_expr); }