Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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.º 5
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.º 6
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.º 7
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.º 8
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.º 9
0
	void visitArrayAccess(ArrayAccess * p)
	{
		p->visit_children(this);
		fprintf( m_outputfile, "popl %%eax\n");
		fprintf( m_outputfile, "imul $4, %%eax\n");
		fprintf( m_outputfile, "movl %%ebp, %%ebx\n");
		fprintf( m_outputfile, "subl $%d, %%ebp\n", (m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling()))->get_size() + (m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling()))->get_size());//here
		fprintf( m_outputfile, "addl %%eax, %%ebp\n");
		fprintf( m_outputfile, "pushl 0(%%ebp)\n");
		fprintf( m_outputfile, "movl %%ebx, %%ebp\n");
	}
Exemplo n.º 10
0
  // check that intarray index is integer and that variable is actually intarray
  void check_array_access(ArrayAccess *p)
  {  
      Symbol *s;
      s = m_st->lookup(p->m_symname->spelling());
      if(!s)
          this->t_error(var_undef, p->m_attribute);
      if(p->m_attribute.m_basetype != 4)
          this->t_error(incompat_assign, p->m_attribute);
      if(p->m_expr->m_attribute.m_basetype != 1)
          this->t_error(incompat_assign, p->m_attribute);

  }
Exemplo n.º 11
0
    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;
      
    }
Exemplo n.º 12
0
 void visitArrayAccess(ArrayAccess * p)
 {
   fprintf(m_outputfile, "\t ## ArrayAccess ##\n");
   p->visit_children(this);
   fprintf(m_outputfile, "\tpopl\t%%ebx\n");
   Symbol *s = m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling());
   fprintf(m_outputfile, "\timull\t$4, %%ebx\n");
   fprintf(m_outputfile, "\taddl\t$%d, %%ebx\n", s->get_offset());
   fprintf(m_outputfile, "\taddl\t$%d, %%ebx\n", 4);
   fprintf(m_outputfile, "\tneg\t%%ebx\n");
   fprintf(m_outputfile, "\taddl\t%%ebp, %%ebx\n");
   fprintf(m_outputfile, "\tpushl\t(%%ebx)\n");
   fprintf(m_outputfile, "\t ## END ArrayAccess ##\n");
 }
Exemplo n.º 13
0
  // create a symbol for the function and check there is none already existing
  // make sure all the expressions used as arguments are valid
  // check if the argument list matches the declared one (length and types)
  // make sure the variable assigned to matches the return type of the function
  void check_call(Call *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);
      
      Symbol *s2;
      
      s2 = m_st->lookup( p->m_symname_1->spelling());
      
      if(!s2)
          this->t_error(var_undef, p->m_attribute);
      
      if(s->m_return_type != s2->m_basetype )
          this->t_error(incompat_assign, p->m_attribute);
      
      if(s->m_arg_type.size() != p->m_expr_list->size())
          this->t_error(call_narg_mismatch, p->m_attribute);
      
      list<Expr_ptr>::iterator iter;
      iter = p->m_expr_list->begin();
      
      for(int i =0; i < s->m_arg_type.size(); i++){
          if(s->m_arg_type[i] != (*iter)->m_attribute.m_basetype)
              this->t_error(call_args_mismatch, (*iter)->m_attribute);
          iter++;
      }

  }
Exemplo n.º 14
0
 void visitCall(Call * p)
 {
   int num_arguments = 0;
   list<Expr_ptr>::reverse_iterator m_expr_list_iter;
   for(m_expr_list_iter = p->m_expr_list->rbegin();
     m_expr_list_iter != p->m_expr_list->rend();
     ++m_expr_list_iter)
   {
     num_arguments++;
     (*m_expr_list_iter)->accept( this );
   }
   fprintf(m_outputfile, "\tcall\t%s\n", p->m_symname_2->spelling());
   Symbol *s = m_st->lookup(p->m_attribute.m_scope, p->m_symname_1->spelling());
   fprintf(m_outputfile, "\tmovl\t%%eax, %d(%%ebx)\n", -(4+s->get_offset()) );    
 }
Exemplo n.º 15
0
    void visitAssignment(Assignment *p) {
     p->visit_children(this);

     string str1, str2, temp1, temp2;
     Symbol * symp;
     // Symbol * symp;
     SymScope *sync;
     ClassNode * parent;
     char* parentName;
     bool oneLevel = false;
      VariableIDImpl * VarIdP = dynamic_cast<VariableIDImpl*>(p->m_variableid);
      char * key = strdup(VarIdP->m_symname->spelling());
      symp = m_symboltable->lookup((const char *)"xxx");


      symp = InScope(key);
      if(symp != NULL){ // or i could change this to if symp ==NULL
        if(p->m_expression->m_attribute.m_type.baseType == 8){
          if(p->m_expression->m_attribute.m_type.baseType == 8 && symp->baseType == 8){
          // std::string temp1(p->m_expression->m_attribute.m_type.classType.classID);
          //std::string temp2(symp->classType.classID);
          if(temp1!= temp2){
            t_error(incompat_assign, p->m_attribute);
            }
          if(p->m_expression->m_attribute.m_type.methodType.returnType.baseType != symp->baseType){
            t_error(incompat_assign, p->m_attribute);
            }
           } 
        }
        if(p->m_expression->m_attribute.m_type.baseType != 8 && p->m_expression->m_attribute.m_type.baseType != symp->baseType){

          t_error(incompat_assign, p->m_attribute);
        }
      }     
      else{
         t_error(sym_name_undef, p->m_attribute);
        }
      //WRITE ME
    }
Exemplo n.º 16
0
 void visitArrayCall(ArrayCall *p)
 {
   fprintf(m_outputfile, "\t ## ArrayCall ##\n");
   int num_arguments = 0;
   list<Expr_ptr>::reverse_iterator m_expr_list_iter;
   for(m_expr_list_iter = p->m_expr_list_2->rbegin();
     m_expr_list_iter != p->m_expr_list_2->rend();
     ++m_expr_list_iter)
   {
     num_arguments++;
     (*m_expr_list_iter)->accept( this );
   }
   fprintf(m_outputfile, "\tcall\t%s\n", p->m_symname_2->spelling());
   p->m_expr_1->accept(this);
   fprintf(m_outputfile, "\tpopl\t%%ebx\n");
   fprintf(m_outputfile, "\timull\t$4, %%ebx\n");
   Symbol *s = m_st->lookup(p->m_attribute.m_scope, p->m_symname_1->spelling());
   fprintf(m_outputfile, "\taddl\t$%d, %%ebx\n", s->get_offset());
   fprintf(m_outputfile, "\taddl\t$%d, %%ebx\n", 4);
   fprintf(m_outputfile, "\tneg\t%%ebx\n");
   fprintf(m_outputfile, "\taddl\t%%ebp, %%ebx\n");
   fprintf(m_outputfile, "\tmovl\t%%eax, (%%ebx)\n");
   fprintf(m_outputfile, "\t ## END ArrayCall ##\n");
 }
Exemplo n.º 17
0
    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
    }
Exemplo n.º 18
0
	// variable and constant access
	void visitIdent(Ident * p)
	{
		p->visit_children(this);
		p->m_symname->accept( this );
		fprintf( m_outputfile, "pushl -%d(%%ebp)\n", (m_st->lookup(p->m_attribute.m_scope, p->m_symname->spelling()))->get_offset() + 4);
  	}