Exemplo n.º 1
0
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);
        } 
	}
}
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 3
0
//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);
        }
    }
}
Exemplo n.º 4
0
 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;
 }
Exemplo n.º 5
0
    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
    }
Exemplo n.º 6
0
/*  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";
}
Exemplo n.º 7
0
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;
}
Exemplo n.º 8
0
    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
    }
Exemplo n.º 9
0
  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);
  }
Exemplo n.º 10
0
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());
				}
			}
		}
	}
}
Exemplo n.º 11
0
    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
    }
Exemplo n.º 12
0
void Statements::evaluate(SymTab &symTab, std::unique_ptr<FuncTab> &funcTab) {
	for (auto &s: _statements) {
		if(symTab.isDefinedGlobal(RETURN))
			break;
		s->evaluate(symTab, funcTab);
	}
}
Exemplo n.º 13
0
  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

  }
Exemplo n.º 14
0
  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));
  }
Exemplo n.º 15
0
  void visitBoolLit(BoolLit * p)
  {
      p -> m_attribute.m_scope = m_st->get_scope();
      p -> visit_children(this);
      p -> m_attribute.m_basetype = bt_boolean;

  }
Exemplo n.º 16
0
  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);	
  }
Exemplo n.º 17
0
 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;
 }
Exemplo n.º 18
0
 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);
 }
Exemplo n.º 19
0
 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()) );    
 }
Exemplo n.º 20
0
 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;
 }
Exemplo n.º 21
0
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_);
}
Exemplo n.º 22
0
  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);

  }
Exemplo n.º 23
0
  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);

  }
Exemplo n.º 24
0
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;
}
Exemplo n.º 25
0
  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);

  }
Exemplo n.º 26
0
  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)); 
  }
Exemplo n.º 27
0
 // 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");
 }
Exemplo n.º 28
0
  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;

  }
Exemplo n.º 29
0
 // 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);
 }
Exemplo n.º 30
0
 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);
 }