예제 #1
0
파일: conf.c 프로젝트: uoaerg/wavemon
static void write_cf(void)
{
	char tmp[0x100], rv[0x40];
	struct conf_item *ci = NULL;
	char *lp, *cp;
	int add, i;
	char *cfname = get_confname();
	int cfld = ll_create();
	FILE *fd = fopen(cfname, "w");

	if (fd == NULL)
		err_sys("failed to open configuration file '%s'", cfname);

	for (ll_reset(conf_items); (ci = ll_getall(conf_items)); ) {
		if (ci->type != t_sep && ci->type != t_func &&
		    (!ci->dep || (ci->dep && *ci->dep))) {
			switch (ci->type) {
			case t_int:
				sprintf(rv, "%d", *ci->v.i);
				break;
			case t_list:
				if (!argv_count(ci->list))
					continue;
				sprintf(rv, "%s", ci->list[*ci->v.i]);
				str_tolower(rv);
				break;
			case t_sep:
			case t_func:
				break;
			}

			add = 1;

			for (i = 0; i < ll_size(cfld); i++) {
				lp = ll_get(cfld, i);
				cp = lp += strspn(lp, " ");
				if (!strncasecmp(cp, ci->cfname, strcspn(cp, " ="))
				    && strlen(ci->cfname) == strcspn(cp, " =")) {
					add = 0;
					cp += strcspn(cp, "=") + 1;
					cp += strspn(cp, " ");
					strncpy(tmp, cp, strcspn(cp, " #\n"));
					if (strcasecmp(tmp, rv)) {
						strncpy(tmp, lp, strcspn(lp, " ="));
						tmp[strcspn(lp, " =")] = '\0';
						strcat(tmp, " = ");
						strcat(tmp, rv);
						strcat(tmp, "\n");
						ll_replace(cfld, i, "s", tmp);
					}
				}
			}

			if (add) {
				strcpy(tmp, ci->cfname);
				strcat(tmp, " = ");
				strcat(tmp, rv);
				strcat(tmp, "\n");
				ll_push(cfld, "s", tmp);
			}
		}
	}

	for (ll_reset(cfld); (lp = ll_getall(cfld)); )
		fputs(lp, fd);
	fclose(fd);

	ll_destroy(cfld);
	free(cfname);
}
예제 #2
0
파일: socketthread.c 프로젝트: brunsa2/lab5
void *socket_thread(void *thread) {
    int server_fd = ((thread_data *) thread)->socket;
    struct sockaddr_storage client_addr;
    int client_addr_size;
    int client_fd;
    thread_id send_thread_id, receive_thread_id;
    int bytes_handled;
    char buffer[BUFFER_SIZE];
    time_t current_time;
    size_t time_buffer_bytes;
    struct tm *local_time;

    while(true) {
        client_addr_size = sizeof(client_addr);
        lock(&accept_lock);
        client_fd = accept(server_fd, (struct sockaddr *) &client_addr,
                           &client_addr_size);
        if(client_fd == -1) {
            close(client_fd);
            continue;
        }
        unlock(&accept_lock);

        ((thread_data *) thread)->socket = client_fd;

        start_joinable_thread(&receive_thread_id, receive_thread, thread);

        ll_clear(((thread_data *) thread)->message_queue);

        for(;;) {
            sleep(1);
            if(ll_size(((thread_data *) thread)->message_queue)) {
                message *sending_message = ll_get(((thread_data *) thread)->message_queue, 0);

                current_time = time(NULL);
                local_time = localtime(&current_time);
                if(local_time != NULL) {
                    time_buffer_bytes = strftime(buffer, BUFFER_SIZE,
                                                 "%a, %d %b %Y %T %Z", local_time);
                    if(time_buffer_bytes != 0) {
                        sending_message->headers[sending_message->header_size] =
                            (char *) malloc(strlen("Echoed-at: ") +
                                            strlen(buffer) + strlen("\r\r"));
                        strcpy(sending_message->headers[sending_message->header_size],
                               "Echoed-at: ");
                        strcat(sending_message->headers[sending_message->header_size],
                               buffer);
                        strcat(sending_message->headers[sending_message->header_size],
                               "\r\n");
                        sending_message->header_size++;
                    }
                }

                int line_index;
                for(line_index = 0; line_index < sending_message->header_size; line_index++) {
                    if(write(client_fd, sending_message->headers[line_index], strlen(sending_message->headers[line_index])) != strlen(sending_message->headers[line_index])) {
                        syslog(LOG_INFO, "Error writing");
                    }
                    free(sending_message->headers[line_index]);
                }
                write(client_fd, "\r\n", strlen("\r\n"));
                syslog(LOG_INFO, "Message");
                for(line_index = 0; line_index < sending_message->message_size; line_index++) {
                    if(write(client_fd, sending_message->message[line_index], strlen(sending_message->message[line_index])) != strlen(sending_message->message[line_index])) {
                        syslog(LOG_INFO, "Error writing");
                    }
                    free(sending_message->message[line_index]);
                }
                write(client_fd, "\r\n", strlen("\r\n"));
                free(sending_message->headers);
                free(sending_message->message);
                free(sending_message);
                ll_remove(((thread_data *) thread)->message_queue, 0);
            }

            if(((thread_data *) thread)->should_shutdown == 1) {
                break;
            }
        }

        ll_clear(((thread_data *) thread)->message_queue);

        join_thread(&receive_thread_id);

        close(client_fd);

    }

    return NULL;
}
예제 #3
0
int ll_check(){
    printf("Testing Linked Lists Functions\n");
    LinkList *l;

    counter = 0;

    l = ll_init(); assert_i(ll_size(l), 0, "Check LL Size after init");
    //ll_print(l);

    ll_addFront(l, 1, -1); assert_i(ll_size(l), 1, "Check LL Size after addFront");
    ll_addFront(l, 2, -2); assert_i(ll_size(l), 2, "Check LL Size after addFront");
    ll_addFront(l, 3, -3); assert_i(ll_size(l), 3, "Check LL Size after addFront");
    //ll_print(l);

    ll_addBack(l, 4, -4); assert_i(ll_size(l), 4, "Check LL Size after addBack");
    ll_addBack(l, 5, -5); assert_i(ll_size(l), 5, "Check LL Size after addBack");
    ll_addBack(l, 6, -6); assert_i(ll_size(l), 6, "Check LL Size after addBack");
    //ll_print(l);

    int x, y;
    ll_getFront(l, &x, &y); 
        assert_p(x, y, 3, -3, "Check LL getFront");
        assert_i(ll_size(l), 5, "Check LL Size after getFront");
    ll_getFront(l, &x, &y);
        assert_p(x, y, 2, -2, "Check LL getFront");
        assert_i(ll_size(l), 4, "Check LL Size after getFront");
    ll_getFront(l, &x, &y);
        assert_p(x, y, 1, -1, "Check LL getFront");
        assert_i(ll_size(l), 3, "Check LL Size after getFront");
    ll_getFront(l, &x, &y);
        assert_p(x, y, 4, -4, "Check LL getFront");
        assert_i(ll_size(l), 2, "Check LL Size after getFront");
    ll_getFront(l, &x, &y);
        assert_p(x, y, 5, -5, "Check LL getFront");
        assert_i(ll_size(l), 1, "Check LL Size after getFront");
    ll_getFront(l, &x, &y);
        assert_p(x, y, 6, -6, "Check LL getFront");
        assert_i(ll_size(l), 0, "Check LL Size after getFront");

    ll_addBack(l, 2, 3); assert_i(ll_size(l), 1, "Check LL Size after addBack on Empty List");
    ll_getFront(l, &x, &y);
        assert_p(x, y, 2, 3, "Check LL getFront");
        assert_i(ll_size(l), 0, "Check LL Size after getFront");


    ll_free(l);
}
예제 #4
0
int asm_sous_opcode(FILE* f,SousOpcode* s,Regle* r,linkedlist* arguments,linkedlist* lockeds)
{
  switch(s->letype)
    {
      case ARG:
      {
	    int reg=registre_libre(f);
	    cellule* p=ll_find(arguments,s->operandes.nom,compare_char_arg);
	    if(p!=NULL)
	    {
	      int pos=ll_indice(arguments,p);
	      fprintf(f,"\tmov %s,[esi+%d+8] ; %s\n",registres_noms[reg],(ll_size(arguments)-1-pos)*4,s->operandes.nom);
	      s->argtype=((Argument*)p->valeur)->type;
	      return reg;
	    }
	    p=ll_find(lockeds,s->operandes.nom,compare_lockeds);
	    if(p!=NULL)
	    {
	      fprintf(f,"\tmov %s,[ebp+locked_%s]\n",registres_noms[reg],s->operandes.nom);
	      return reg;
	    }
	    fprintf(stderr,"[FATAL] Identificateur inconnu : %s\n",s->operandes.nom);
      }
      case UREG:
      case REG:
      {
	int reg=registre_libre(f);
	fprintf(f,"\tmov %s,%s\n",registres_noms[reg],asm_registres[s->operandes.valeur]);
	return reg;
      }
      case VAREXT:
      {
	int reg=registre_libre(f);
	fprintf(f,"\tmov %s,%s\n",registres_noms[reg],s->operandes.nom);
	return reg;
      }
     
      case RNDI:
      {
	int reg;
	if(r->randint_used[s->operandes.valeur]>1)
	{
	    reg=registre_libre(f);
	    fprintf(f,"\tmov %s,[esi-%d-4] ; RNDINT%d\n",registres_noms[reg],position_rnd(s->operandes.valeur,r->randint_used)*4,s->operandes.valeur);
	}
	else
	{
	  reg=registre_libre_different(f,REAX);
	  registre_force(f,REAX);
	  fprintf(f,"\txor eax,eax\n\tdec eax\n");
	  fprintf(f,"\tcall poly_rand_int\n");
	  fprintf(f,"\tmov %s,%s ; RNDINT%d\n",registres_noms[reg],registres_noms[REAX],s->operandes.valeur);
	  libere_registre(f,REAX);
	}
	return reg;
      }
      case RNDR:
      {
	int reg;
	if(r->randreg_used[s->operandes.valeur]>0)
	{
	  reg=registre_libre(f);
	  fprintf(f,"\tmov %s,[esi-%d-4] ; FREEREG%d\n",registres_noms[reg],(r->nb_randint+position_rnd(s->operandes.valeur,r->randreg_used))*4,s->operandes.valeur);
	}
	else
	{
	  reg=registre_libre_different(f,REAX);
	  registre_force(f,REAX);
	  fprintf(f,"\tcall choix_registre\n");
	  fprintf(f,"\tmov %s,%s ; FREEREG%d\n",registres_noms[reg],registres_noms[REAX],s->operandes.valeur);
	  libere_registre(f,REAX);
	}
	return reg;
      }
      case URNDR:
      {
	int reg;
	if(r->urandreg_used[s->operandes.valeur]>0)
	{
	  reg=registre_libre(f);
	  fprintf(f,"\tmov %s,[esi-%d-4] ; RNDREG%d\n",registres_noms[reg],(r->nb_randint+r->nb_randreg+r->nb_randmem+r->nb_lab+position_rnd(s->operandes.valeur,r->urandreg_used))*4,s->operandes.valeur);
	}
	else
	{
	  reg=registre_libre_different(f,REAX);
	  registre_force(f,REAX);
	  fprintf(f,"\tcall choix_uregistre\n");
	  fprintf(f,"\tmov %s,%s ; RNDREG%d\n",registres_noms[reg],registres_noms[REAX],s->operandes.valeur);
	  libere_registre(f,REAX);
	}
	return reg;
      }
      case RNDM:
      {
	int reg;
	if(r->randmem_used[s->operandes.valeur]>0)
	{
	  reg=registre_libre(f);
	  fprintf(f,"\tmov %s,[esi-%d-4] ; FREEMEM%d\n",registres_noms[reg],(r->nb_randint+r->nb_randreg+position_rnd(s->operandes.valeur,r->randmem_used))*4,s->operandes.valeur);
	}
	else
	{
	  reg=registre_libre_different(f,REBX);
	  registre_force(f,REBX);
	  fprintf(f,"\tcall choix_memoire\n");
	  fprintf(f,"\tmov %s,%s ; FREEMEM%d\n",registres_noms[reg],registres_noms[REAX],s->operandes.valeur);
	  libere_registre(f,REBX);
	}
	return reg;
      }
      case LAB:
      {
	      int reg=registre_libre(f);
	      fprintf(f,"\tmov %s,[esi-%d-4] ; LABEL%d\n",registres_noms[reg],(r->nb_randint+r->nb_randreg+r->nb_randmem+position_rnd(s->operandes.valeur,r->lab_used))*4,s->operandes.valeur);
	      fprintf(f,"\tmov %s,[%s]\n",registres_noms[reg],registres_noms[reg]);
	      return reg;
      }
      case CONSTANTE:
      {
	int reg=registre_libre(f);
	fprintf(f,"\tmov %s,%d\n",registres_noms[reg],s->operandes.valeur);
	return reg;
      }
      case ET:
      case OU:
      case OUEX:
      case SOUSTRACTION:
      case ADDITION:
      {
	char op[4];
	switch(s->letype)
	{
	  case ET:
	    strcpy(op,"and");
	    break;
	  case OU:
	    strcpy(op,"or");
	    break;
	  case OUEX:
	    strcpy(op,"xor");
	    break;
	  case ADDITION:
	    strcpy(op,"add");
	    break;
	  case SOUSTRACTION:
	    strcpy(op,"sub");
	    break;
	}
	SousOpcode* s1=s->operandes.couple.gauche;
	SousOpcode* s2=s->operandes.couple.droit;
	int r1,r2;
	if(s1->letype==CONSTANTE && s->letype!=SOUSTRACTION)
	{
	  r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	  fprintf(f,"\t%s %s,%d\n",op,registres_noms[r2],s1->operandes.valeur);
	  return r2;
	}
	else if(s2->letype==CONSTANTE)
	{
	  r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	  fprintf(f,"\t%s %s,%d\n",op,registres_noms[r1],s2->operandes.valeur);
	  return r1;
	}
	else
	{
	  r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	  r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	  fprintf(f,"\t%s %s,%s\n",op,registres_noms[r1],registres_noms[r2]);
	  libere_registre(f,r2);
	  return r1;
	}
      }
      case MULTIPLICATION:
      {
	SousOpcode* s1=s->operandes.couple.gauche;
	SousOpcode* s2=s->operandes.couple.droit;
	int r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	int r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	fprintf(f,"\timul %s,%s\n",registres_noms[r1],registres_noms[r2]);
	libere_registre(f,r2);
	return r1;
      }
      case DIVISION:
      {
	SousOpcode* s1=s->operandes.couple.gauche;
	SousOpcode* s2=s->operandes.couple.droit;
	int r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	int r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	fprintf(f,"\tidiv %s,%s\n",registres_noms[r1],registres_noms[r2]);
	libere_registre(f,r2);
	return r1;
      }
      case DECDROIT:
      case DECGAUCHE:
      {
	char op[4];
	int r1,r2;
	if(s->letype==DECDROIT)
	  strcpy(op,"shr");
	else
	  strcpy(op,"shl");
	SousOpcode* s1=s->operandes.couple.gauche;
	SousOpcode* s2=s->operandes.couple.droit;
	if(s2->letype==CONSTANTE)
	{
	  r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	  fprintf(f,"\t%s %s,%d\n",op,registres_noms[r1],s2->operandes.valeur);
	  return r1;
	}
	else
	{
	  int reg=registre_libre_different(f,RECX);
	  registre_force(f,RECX);
	  r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	  fprintf(f,"\tmov %s,%s\n",registres_noms[reg],registres_noms[r1]);
	  r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	  fprintf(f,"\tmov ecx,%s\n",registres_noms[r2]);
	  
	  fprintf(f,"\tshl %s,cl\n",registres_noms[reg],registres_noms[r1]);
	  libere_registre(f,r2);
	  libere_registre(f,r1);
	  libere_registre(f,RECX);
	  return reg;
	}
      }
      case INDIRECTION:
      {
	SousOpcode* s1=s->operandes.sousopcode;
	int r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	if(r1!=REBX)
	{
	  registre_force(f,REBX);
	  fprintf(f,"\tmov ebx,%s\n",registres_noms[r1]);
	}
	fprintf(f,"\tcall contenu_memoire\n");
	if(r1!=REBX)
	{
	  fprintf(f,"\tmov %s,ebx\n");
	  libere_registre(f,REBX);
	}
	return r1;
      }
      case MOD:
      {
	SousOpcode* s1=s->operandes.couple.gauche;
	SousOpcode* s2=s->operandes.couple.droit;
	int r2=registre_libre_different2(f,REAX,REDX);
	registre_force(f,REAX);
	registre_force(f,REDX);
	int r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	if(r1!=REAX)
	  fprintf(f,"\tmov eax,%s\n",registres_noms[r1]);
	fprintf(f,"\txor edx,edx\n");
	libere_registre(f,r1);
	r1=asm_sous_opcode(f,s2,r,arguments,lockeds);
	fprintf(f,"\tdiv %s\n",registres_noms[r1]);
	if(r2!=REDX)
	  fprintf(f,"\tmov %s,edx\n",registres_noms[r2]);
	libere_registre(f,r1);
	libere_registre(f,REDX);
	libere_registre(f,REAX);
	return r2;
	}
      case NEGATION:
      {
	SousOpcode* s1=s->operandes.sousopcode;
	int r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	fprintf(f,"\tnot %s\n",registres_noms[r1]);
	return r1;
      }
      case JA:
      case JB:
      case JAE:
      case JBE:
      case JE:
      case JNE:
      {
	SousOpcode* s1=s->operandes.couple.gauche;
	SousOpcode* s2=s->operandes.couple.droit;
	int r1,r2;
	int l1=get_label(),l2=get_label();
	if(s1->letype==CONSTANTE || s1->letype==UREG)
	{
	  r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	  if(s1->letype==CONSTANTE)
	    fprintf(f,"\tcmp %s,%d\n",registres_noms[r2],s1->operandes.valeur);
	  else
	    fprintf(f,"\tcmp %s,%s\n",registres_noms[r2],asm_registres[s1->operandes.valeur]);
	  r1=r2;
	}
	else if(s2->letype==CONSTANTE || s2->letype==UREG)
	{
	   r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	  if(s2->letype==CONSTANTE)
	    fprintf(f,"\tcmp %s,%d\n",registres_noms[r1],s2->operandes.valeur);
	  else
	    fprintf(f,"\tcmp %s,%s\n",registres_noms[r1],asm_registres[s2->operandes.valeur]);

	}
	else
	{
	  r1=asm_sous_opcode(f,s1,r,arguments,lockeds);
	  r2=asm_sous_opcode(f,s2,r,arguments,lockeds);
	  fprintf(f,"\tcmp %s,%s\n",registres_noms[r1],registres_noms[r2]);
	  libere_registre(f,r2);
	}
	switch(s->letype)
	{
	  case JA:
	    fprintf(f,"\tjg  goto_%d\n",l1);
	    break;
	  case JAE:
	    fprintf(f,"\tjge  goto_%d\n",l1);
	    break;
	  case JB:
	    fprintf(f,"\tjl  goto_%d\n",l1);
	    break;
	  case JBE:
	    fprintf(f,"\tjle  goto_%d\n",l1);
	    break;
   	  case JE:
	    fprintf(f,"\tje  goto_%d\n",l1);
	    break;
 	  case JNE:
	    fprintf(f,"\tjne  goto_%d\n",l1);
	    break;
	}
	fprintf(f,"\txor %s,%s\n",registres_noms[r1],registres_noms[r1]);
	fprintf(f,"\tjmp  goto_%d\n",l2);
	fprintf(f,"goto_%d:\n",l1);
	fprintf(f,"\txor %s,%s\n",registres_noms[r1],registres_noms[r1]);
	fprintf(f,"\tinc %s\n",registres_noms[r1]);
	fprintf(f,"goto_%d:\n",l2);
	return r1;
      }
    }
  return registre_libre(f);
}
예제 #5
0
void asm_opcode(FILE* f,Opcode* o,Regle* r,linkedlist* arguments,linkedlist* lockeds)
{
  switch(o->letype)
    {
    case ASMOPCODE:
      {
          int l=get_label();
          fprintf(f,"\tcmp byte ptr [ebp+poly_passe],NB_PASSES/2\n");
          fprintf(f,"\tjbe goto_%d  ; pas d'asm a la première passe car labels faux\n",l);
      
        	fprintf(f,"; ASM code from line %d\n",o->ligne);
        	/* A changer : on remplace le nom des arguments par leur position dans la pile */
        	char* txt=o->operandes.opasm.asmtxt;
        	int indice=0;
        	cellule* a=ll_front(arguments);
        	while(a!=NULL)
        	{
        	  char rplc[20];
        	  Argument* arg=(Argument*)a->valeur;
        	  snprintf(rplc,25,"[esi+%d+8]",(ll_size(arguments)-1-indice)*4);
        	  txt=strmyreplace(txt,(const char*)arg->nom,(const char*) rplc);
        	  a=ll_next(arguments,a);
        	  indice++;
        	}
        	indice=0;
        	a=ll_front(lockeds);
        	while(a!=NULL)
        	{
        	  char rplc[250];
        	  Locked* lo=(Locked*) a->valeur;
        	  snprintf(rplc,250,"[ebp+locked_%s]",lo->id);
        	  txt=strmyreplace(txt,(const char*)lo->id,(const char*) rplc);
        	  a=ll_next(lockeds,a);
        	  indice++;
        	}
        
        	o->operandes.opasm.asmtxt=txt;
        	fprintf(f,"%s",txt);
        	fprintf(f,"goto_%d:   ; END of asm code\n",l);
        	break;
      }
    case CALLOPCODE:
      {
          if(nb_calls_restants(r,o)>0)
          {
              fprintf(f,"\tmov eax,%d\n",nb_calls_restants(r,o));
	      fprintf(f,"\tcall optimise_taille_code_genere\n");
              fprintf(f,"\tpush eax\n");
          }    
    if(o->operandes.opcall.transfo->regledefaut->minimum_taille>0)      
	    fprintf(f,"\tadd edx,%d ; taille minimale de la transformation %s\n",o->operandes.opcall.transfo->regledefaut->minimum_taille,o->operandes.opcall.transfo->nom);
	cellule* cs=ll_front(&o->operandes.opcall.arguments);
	while(cs!=NULL)
	  {
	    SousOpcode* s=(SousOpcode*)cs->valeur;
	    int reg=asm_sous_opcode(f,s,r,arguments,lockeds);
	    if(libere_registre(f,reg))
	      {
		fprintf(f,"\txchg %s,dword ptr [esp]\n",registres_noms[reg]);
	      }
	    else
	      {
		fprintf(f,"\tpush %s\n",registres_noms[reg]);
	      }
	    cs=ll_next(&o->operandes.opcall.arguments,cs);
	  }
	fprintf(f,"\tcall asm_%s\n",o->operandes.opcall.transfo->nom);
	if(nb_calls_restants(r,o)>0)
    {	
       fprintf(f,"\tpop eax\n");
	   fprintf(f,"\tadd edx,eax\n");
    }	   
	break;
      }
      case LABELOPCODE:
      {
        fprintf(f,"\tmov eax,edi\n");
        fprintf(f,"\tsub eax,[ebp+poly_va_buffer]\n");
        fprintf(f,"\tadd eax,[ebp+poly_va_code]\n");
        fprintf(f,"\tmov ebx,[esi-%d-4] ; LABEL%d\n",(r->nb_randint+r->nb_randreg+r->nb_randmem+position_rnd(o->operandes.oplabel.num,r->lab_used))*4,o->operandes.oplabel.num); 
        fprintf(f,"\tmov [ebx],eax\n");
        break;
      }
      
      
      
      case LOCKOPCODE:
      {
	int reg=asm_sous_opcode(f,o->operandes.oplock.ss,r,arguments,lockeds );
	if(o->operandes.oplock.ss->letype==REG || o->operandes.oplock.ss->letype==RNDR 
        || o->operandes.oplock.ss->letype==ARG && o->operandes.oplock.ss->argtype==ATYPE_REGISTRE)
	{
	  fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	  fprintf(f,"\tcall lock_registre\n");
	}
	else if(o->operandes.oplock.ss->letype==RNDM
         || o->operandes.oplock.ss->letype==ARG && o->operandes.oplock.ss->argtype==ATYPE_ADRESSE)
	{
	  fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	  fprintf(f,"\tcall lock_memoire\n");
	}
	else if(o->operandes.oplock.ss->letype==UREG || o->operandes.oplock.ss->letype==URNDR )
	{
	  fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	  fprintf(f,"\tcall lock_uregistre\n");
	}
	if(o->operandes.oplock.id!=NULL)
	{
	  fprintf(f,"\tmov [ebp+locked_%s],%s    ; LOCKED\n",o->operandes.oplock.id,registres_noms[reg]);
	}
	libere_registre(f,reg);
	break;
      }
      
      
            
      case FREEOPCODE:
      {
	if(o->operandes.oplock.id!=NULL)
	{
	  cellule* p=ll_find(lockeds,o->operandes.oplock.id,compare_lockeds);
	  if(p!=NULL)
	  {
	    Locked* l=(Locked*)p->valeur;
	    if(l->ss->letype==RNDR  ||l->ss->letype==REG
             || l->ss->letype==ARG && l->ss->argtype==ATYPE_REGISTRE)
	    {
	      fprintf(f,"\tmov eax,[ebp+locked_%s] ; FREEDREG %s\n",l->id,l->id);
	      fprintf(f,"\tcall free_registre\n");
	    }
	    else if(l->ss->letype==RNDM
                  || l->ss->letype==ARG && l->ss->argtype==ATYPE_ADRESSE)
	    {
	      fprintf(f,"\tmov eax,[ebp+locked_%s] ; FREEDMEM %s\n",l->id,l->id);
	      fprintf(f,"\tcall free_memoire\n");
	    }
	    else if(l->ss->letype==URNDR||l->ss->letype==UREG)
	    {
	      fprintf(f,"\tmov eax,[ebp+locked_%s] ; FREEDUREG %s\n",l->id,l->id);
	      fprintf(f,"\tcall free_uregistre\n");
	    }
	  }
	}
	else
	{
	  int reg=asm_sous_opcode(f,o->operandes.oplock.ss,r,arguments,lockeds );
	  if(o->operandes.oplock.ss->letype==REG || o->operandes.oplock.ss->letype==RNDR )
	  {
	    fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	    fprintf(f,"\tcall free_registre\n");
	  }
	  else if(o->operandes.oplock.ss->letype==RNDM)
	  {
	    fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	    fprintf(f,"\tcall free_memoire\n");
	  }
	  else if(o->operandes.oplock.ss->letype==UREG || o->operandes.oplock.ss->letype==URNDR )
	  {
	    fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	    fprintf(f,"\tcall free_uregistre\n");
	  }
	  libere_registre(f,reg);
	}
	break;
      }
   
      case INITMEMOPCODE:
      {
	registre_force(f,REBX);
	int reg=asm_sous_opcode(f,o->operandes.opegal.valeur,r,arguments,lockeds);
	fprintf(f,"\tmov ebx,%s\n",registres_noms[reg]);
	libere_registre(f,reg);
	
	registre_force(f,REAX);
	reg=asm_sous_opcode(f,o->operandes.opegal.dest,r,arguments,lockeds);
	fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	libere_registre(f,reg);	
	fprintf(f,"\tcall set_memoire\n");
	libere_registre(f,REAX);	
	libere_registre(f,REBX);	
	break;
      }
      case CHANGEMEMOPCODE:
      {
	registre_force(f,REBX);
	int reg=asm_sous_opcode(f,o->operandes.opegal.valeur,r,arguments,lockeds);
	fprintf(f,"\tmov ebx,%s\n",registres_noms[reg]);
	libere_registre(f,reg);
	
	registre_force(f,REAX);
	reg=asm_sous_opcode(f,o->operandes.opegal.dest,r,arguments,lockeds);
	fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	libere_registre(f,reg);	
	fprintf(f,"\tcall change_memoire\n");
	libere_registre(f,REAX);	
	libere_registre(f,REBX);	
	break;
      }
      case WRITE:
      {
	if(o->operandes.opwrite.valeur->letype==CONSTANTE)
	{
	  fprintf(f,"\tmov eax,%d\n",o->operandes.opwrite.valeur->operandes.valeur);
	}
	else
	{
	  int reg=asm_sous_opcode(f,o->operandes.opwrite.valeur,r,arguments,lockeds);
	  if(reg!=REG_EAX)
	    fprintf(f,"\tmov eax,%s\n",registres_noms[reg]);
	  libere_registre(f,reg);
	}
	switch(o->operandes.opwrite.nboctets)
	{
	  case 1:
	    fprintf(f,"\tstosb\n");
	    break;
	  case 2:
	    fprintf(f,"\tstosw\n");
	    break;
	  case 4:
	    fprintf(f,"\tstosd\n");
	    break;
	}
	break;
      }
      case CONDITIONIF:
      {
	int lfin=get_label();
	int taille_if=minimum_taille(&o->operandes.opif.codeif,50000,0);
	fprintf(f,"\tadd edx,%d\n",taille_if);
	int reg=asm_sous_opcode(f,o->operandes.opif.condition,r,arguments,lockeds);	
	fprintf(f,"\ttest %s,%s\n",registres_noms[reg],registres_noms[reg]);
	libere_registre(f,reg);
	fprintf(f,"\tjz  goto_%d\n",lfin);
	fprintf(f,"\tsub edx,%d\n",taille_if);
	cellule* co=ll_front(&o->operandes.opif.codeif);
	while(co!=NULL)
	{
	  Opcode* o2=(Opcode*)co->valeur;
	  asm_opcode(f,o2,r,arguments,lockeds);
	  co=ll_next(&o->operandes.opif.codeif,co);
	}
	fprintf(f,"goto_%d:\n",lfin);
	break;
      }
    case CONDITIONIFELSE:
      {
	int lelse=get_label(),lfin=get_label();
	int taille_if=minimum_taille(&o->operandes.opif.codeif,50000,0);
	int taille_else=minimum_taille(&o->operandes.opif.codeelse,50000,0);
	int taille_max=taille_if>taille_else?taille_if:taille_else;
	int reg=asm_sous_opcode(f,o->operandes.opif.condition,r,arguments,lockeds);
	fprintf(f,"\ttest %s,%s\n",registres_noms[reg],registres_noms[reg]);
	libere_registre(f,reg);
	fprintf(f,"\tjz  goto_%d\n",lelse);
	if(taille_if<taille_max)
	   fprintf(f,"\tadd edx,%d\n",taille_max-taille_if);
	cellule* co=ll_front(&o->operandes.opif.codeif);
	while(co!=NULL)
	{
	  Opcode* o2=(Opcode*)co->valeur;
	  asm_opcode(f,o2,r,arguments,lockeds);
	  co=ll_next(&o->operandes.opif.codeif,co);
	}
	fprintf(f,"\tjmp  goto_%d\n",lfin);
	fprintf(f,"goto_%d:\n",lelse);
	if(taille_else<taille_max)
	   fprintf(f,"\tadd edx,%d\n",taille_max-taille_else);	
	co=ll_front(&o->operandes.opif.codeelse);
	while(co!=NULL)
	{
	  Opcode* o2=(Opcode*)co->valeur;
	  asm_opcode(f,o2,r,arguments,lockeds);
	  co=ll_next(&o->operandes.opif.codeelse,co);
	}
	fprintf(f,"goto_%d:\n",lfin);
	break;
      }
    }
}
예제 #6
0
/*
 * Assemble une regle définie par l'utilisateur. Ici, le côté "difficile"
 * est de gérer les différents RNDINTx RNDMEMx RNDREGx utilisés dans la regle.
 * En effet, si disons RNDINT1 est utilisé plusieurs fois dans la regle, il
 * nous faut sauvegarder sa valeur sur la pile.
 * Enfin, il nous faut également gérer les gardes de la règle, i.e si une regle
 * utilise 3 RNDREG !=, il ne faut pas rentrer dedans si il n'y a pas 3 registres
 * dispos a cet instant là.
 * De même si la taille minimum du code généré par la regle est superieur a
 * la place dispo, on en choisra une autre.
 */
void asm_regle(FILE*f,Transformation* t,Regle* r,int indice,int somme_probas,linkedlist* arguments,linkedlist* lockeds)
{
  int i;
  if(!r->is_defaut) //gardes
    {
      fprintf(f,"%s_regle_%d:\n",t->nom,indice);
      fprintf(f,"\tmov eax,%d ; somme des probas des règles de cette transformation\n",somme_probas);
      fprintf(f,"\tcall poly_rand_int\n");
      fprintf(f,"\tcmp eax,%d ; probabilité d'occurence de la règle\n",r->proba);
      fprintf(f,"\tjae  %s_regle_%d\n",t->nom,indice+1);
      if(r->minimum_taille>0)
      {
	fprintf(f,"\tcmp edx,%d ; taille minimale du code généré par cette règle\n",r->minimum_taille);
	fprintf(f,"\tjb  %s_regle_%d\n",t->nom,indice+1);
      }
      if(r->nb_randreg+r->nb_reg>0)
      {
	fprintf(f,"\tcall nb_registres_libres\n");
	fprintf(f,"\tcmp eax,%d\n",r->nb_randreg+r->nb_reg);
	fprintf(f,"\tjb  %s_regle_%d\n",t->nom,indice+1);
      }
      if(r->nb_randmem>0)
      {
	fprintf(f,"\tcall nb_memoire_libre\n");
	fprintf(f,"\tcmp eax,%d\n",r->nb_randmem);
	fprintf(f,"\tjb  %s_regle_%d\n",t->nom,indice+1);
      }
      for(i=0;i<10;i++)
      {
	if(r->reg_used[i]>0)
	{
	  fprintf(f,"\tmov eax,%s\n",asm_registres[i]);
	  fprintf(f,"\tcall est_registre_libre\n");
	  fprintf(f,"\tjc  %s_regle_%d\n",t->nom,indice+1);
	}
      }
    }
  else
    {
      fprintf(f,"%s_defaut:\n",t->nom);
      fprintf(f,"%s_regle_%d:\n",t->nom,ll_size(&t->regles)-1);
    }
  if(r->nb_randint+r->nb_randmem+r->nb_randreg+r->nb_urandreg+r->nb_lab>0)
    {
      fprintf(f,"\tsub esp,%d\n",(r->nb_randint+r->nb_randmem+r->nb_randreg+r->nb_lab+r->nb_urandreg)*4);
    }
  if(r->nb_lab>0)
  {
      fprintf(f,"\tsub [ebp+poly_ebp],dword ptr %d\n",4*r->nb_lab);
      fprintf(f,"\tmov eax,dword ptr [ebp+poly_ebp]\n");
      int i;
      for(i=0;i<r->nb_lab;i++)
      {
         fprintf(f,"\tmov [esi-%d-4],eax ; LABEL%d\n",(r->nb_randint+r->nb_randreg+r->nb_randmem+i)*4,i);
         fprintf(f,"\tadd eax,4\n");
      }   
  }    

  if(r->nb_urandreg>0)
  {
    fprintf(f,"\tcall raz_choix_uregistres\n");
  }
  cellule* a=ll_front(arguments);
  while(a!=NULL && !r->is_defaut && r->nb_randreg+r->nb_urandreg>0)
  {
    Argument* arg=(Argument*)a->valeur;
    if(arg->type==ATYPE_REGISTRE)
    {
      int pos=ll_indice(arguments,a);
      fprintf(f,"\tmov eax,[esi+%d+8]\n",(ll_size(arguments)-1-pos)*4);
      fprintf(f,"\tinc byte ptr [ebp+choix_uregistres+eax]\n");
    }
    a=ll_next(arguments,a);
  }
  for(i=0;i<10;i++)
  {
    if(r->reg_used[i]>0)
    {
      fprintf(f,"\tmov eax,%s\n",asm_registres[i]);
      fprintf(f,"\tcall reserve_registre\n");
    }
  }
  for(i=0;i<10;i++)
  {
    if(r->ureg_used[i]>0)
    {
      fprintf(f,"\tmov eax,%s\n",asm_registres[i]);
      fprintf(f,"\tcall reserve_uregistre\n");
    }
  }
  for(i=0;i<10;i++)
    {
      if(r->randint_used[i]>1)
	{
	  fprintf(f,"\txor eax,eax\n\tdec eax\n");
	  fprintf(f,"\tcall poly_rand_int\n");
	  fprintf(f,"\tmov [esi-%d-4],eax ; RNDINT%i\n",position_rnd(i,r->randint_used)*4,i);
	}
      if(r->randreg_used[i]>0)
	{
	  fprintf(f,"\tcall choix_registre\n");
	  fprintf(f,"\tmov [esi-%d-4],eax ; FREEREG%d\n",(r->nb_randint+position_rnd(i,r->randreg_used))*4,i);
	}
      if(r->randmem_used[i]>0)
	{
	  fprintf(f,"\tcall choix_memoire\n");
	  fprintf(f,"\tmov [esi-%d-4],eax ; FREEMEM%d\n",(r->nb_randint+r->nb_randreg+position_rnd(i,r->randmem_used))*4,i);
	}
    }
  for(i=0;i<10;i++)
    {
      if(r->urandreg_used[i]>0)
	{
	  fprintf(f,"\tcall choix_uregistre\n");
	  fprintf(f,"\tmov [esi-%d-4],eax ; RNDREG%d\n",(r->nb_randint+r->nb_randreg+r->nb_randmem+r->nb_lab+position_rnd(i,r->urandreg_used))*4,i);
	}
    }
  if(r->minimum_taille!=0)
    {
      fprintf(f,"\tsub edx,%d\n",r->minimum_taille);
    }
  cellule* co=ll_front(&r->opcodes);
  while(co!=NULL)
    {
      Opcode* o=(Opcode*)co->valeur;
      if(o!=NULL)
      {
	asm_opcode(f,o,r,arguments,lockeds);
	co=ll_next(&r->opcodes,co);
      }
    }
 
  for(i=0;i<10;i++)
  {
    if(r->reg_used[i]>0 )
    {
      fprintf(f,"\tmov eax,%s\n",asm_registres[i]);
      fprintf(f,"\tcall libere_registre\n");
    }
    if(r->ureg_used[i]>0 )
    {
      fprintf(f,"\tmov eax,%s\n",asm_registres[i]);
      fprintf(f,"\tcall libere_uregistre\n");
    }
    if(r->randreg_used[i]>0 )
    {
      fprintf(f,"\tmov eax,[esi-%d-4] ; FREEREG%d\n",(r->nb_randint+position_rnd(i,r->randreg_used))*4,i);
      fprintf(f,"\tcall libere_registre\n");
    }
    if(r->urandreg_used[i]>0 )
    {
      fprintf(f,"\tmov eax,[esi-%d-4] ; RNDREG%d\n",(r->nb_randint+position_rnd(i,r->urandreg_used))*4,i);
      fprintf(f,"\tcall libere_uregistre\n");
    }
    if(r->randmem_used[i]>0 )
    {
      fprintf(f,"\tmov eax,[esi-%d-4] ; FREEMEM%d\n",(r->nb_randint+r->nb_randreg+position_rnd(i,r->randmem_used))*4,i);
      fprintf(f,"\tcall libere_memoire\n");
    }
  }
  if(r->nb_randint+r->nb_randmem+r->nb_randreg+r->nb_urandreg+r->nb_lab>0)
    {
      fprintf(f,"\tadd esp,%d\n",(r->nb_randint+r->nb_randmem+r->nb_randreg+r->nb_urandreg+r->nb_lab)*4);
    }
  fprintf(f,"\tjmp %s_fin\n",t->nom);    
}