File:  [local] / rpl / src / optimisation.c
Revision 1.50: download - view: text, annotated - select for diffs - revision graph
Sat Mar 23 16:14:39 2013 UTC (11 years, 1 month ago) by bertrand
Branches: MAIN
CVS tags: HEAD
Correction d'un problème de mutex dans la gestion des variables partagées.
Correction d'un segfault dans les routines de transformation des arbres des
variables en tableau.

    1: /*
    2: ================================================================================
    3:   RPL/2 (R) version 4.1.13
    4:   Copyright (C) 1989-2013 Dr. BERTRAND Joël
    5: 
    6:   This file is part of RPL/2.
    7: 
    8:   RPL/2 is free software; you can redistribute it and/or modify it
    9:   under the terms of the CeCILL V2 License as published by the french
   10:   CEA, CNRS and INRIA.
   11:  
   12:   RPL/2 is distributed in the hope that it will be useful, but WITHOUT
   13:   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   14:   FITNESS FOR A PARTICULAR PURPOSE.  See the CeCILL V2 License
   15:   for more details.
   16:  
   17:   You should have received a copy of the CeCILL License
   18:   along with RPL/2. If not, write to info@cecill.info.
   19: ================================================================================
   20: */
   21: 
   22: 
   23: #include "rpl-conv.h"
   24: 
   25: 
   26: /*
   27: ================================================================================
   28:   Boucle principale optimisée de l'interprète RPL/2
   29: ================================================================================
   30:   Entrées : structure sur l'état du processus
   31: --------------------------------------------------------------------------------
   32:   Sorties : Néant
   33: --------------------------------------------------------------------------------
   34:   Effets de bord : néant
   35: ================================================================================
   36: */
   37: 
   38: logical1
   39: sequenceur_optimise(struct_processus *s_etat_processus)
   40: {
   41:     integer8                    adresse_point_entree;
   42:     integer8                    i;
   43:     integer8                    j;
   44:     integer8                    nb_variables;
   45:     integer8                    point_entree;
   46: 
   47:     logical1                    erreur;
   48: 
   49:     struct_objet                *programme_principal;
   50: 
   51:     struct_tableau_variables    *tableau;
   52: 
   53:     unsigned char               *message;
   54:     unsigned char               registre;
   55: 
   56:     if ((*s_etat_processus).debug == d_vrai)
   57:         if (((*s_etat_processus).type_debug &
   58:                 d_debug_analyse) != 0)
   59:     {
   60:         if ((*s_etat_processus).langue == 'F')
   61:         {
   62:             printf("+++Compilation [%d]\n", (int) getpid());
   63:         }
   64:         else
   65:         {
   66:             printf("+++Compilation [%d]\n", (int) getpid());
   67:         }
   68: 
   69:         printf("\n");
   70:         fflush(stdout);
   71:     }
   72: 
   73:     point_entree = -1;
   74:     adresse_point_entree = 0;
   75:     programme_principal = NULL;
   76: 
   77:     empilement_pile_systeme(s_etat_processus);
   78:     (*(*s_etat_processus).l_base_pile_systeme).retour_definition = 'Y';
   79:     (*s_etat_processus).autorisation_empilement_programme = 'Y';
   80:     (*s_etat_processus).mode_execution_programme = 'N';
   81: 
   82:     if (pthread_mutex_lock(&mutex_liste_variables_partagees) != 0)
   83:     {
   84:         (*s_etat_processus).erreur_systeme = d_es_processus;
   85:         return(d_erreur);
   86:     }
   87: 
   88:     nb_variables = nombre_variables(s_etat_processus);
   89: 
   90:     if ((tableau = malloc(((size_t) nb_variables) *
   91:             sizeof(struct_tableau_variables))) == NULL)
   92:     {
   93:         if ((*s_etat_processus).langue == 'F')
   94:         {
   95:             printf("+++Système : Mémoire insuffisante\n");
   96:         }
   97:         else
   98:         {
   99:             printf("+++System : Not enough memory\n");
  100:         }
  101: 
  102:         liberation_mutexes_arbre_variables_partagees(s_etat_processus,
  103:                 (*(*s_etat_processus).s_arbre_variables_partagees));
  104:         pthread_mutex_unlock(&mutex_liste_variables_partagees);
  105:         return(d_erreur);
  106:     }
  107: 
  108:     nb_variables = liste_variables(s_etat_processus, tableau);
  109: 
  110:     if (pthread_mutex_unlock(&mutex_liste_variables_partagees) != 0)
  111:     {
  112:         (*s_etat_processus).erreur_systeme = d_es_processus;
  113:         return(d_erreur);
  114:     }
  115: 
  116:     for(i = 0; i < nb_variables; i++)
  117:     {
  118:         if (tableau[i].niveau == 0)
  119:         {
  120:             // Variables qui contiennent les points d'entrée des définitions.
  121: 
  122:             (*s_etat_processus).position_courante = (*((integer8 *)
  123:                     (*(tableau[i].objet)).objet));
  124: 
  125:             if (point_entree == -1)
  126:             {
  127:                 adresse_point_entree = (*s_etat_processus).position_courante;
  128:                 point_entree = i;
  129:             }
  130:             else
  131:             {
  132:                 if ((*s_etat_processus).position_courante <
  133:                         adresse_point_entree)
  134:                 {
  135:                     adresse_point_entree = (*s_etat_processus)
  136:                             .position_courante;
  137:                     point_entree = i;
  138:                 }
  139:             }
  140: 
  141:             if ((erreur = recherche_instruction_suivante(s_etat_processus))
  142:                     == d_erreur)
  143:             {
  144:                 if ((*s_etat_processus).langue == 'F')
  145:                 {
  146:                     printf("+++Fatal : Compilation impossible\n");
  147:                 }
  148:                 else
  149:                 {
  150:                     printf("+++Fatal : Compilation failed\n");
  151:                 }
  152: 
  153:                 for(j = 0; j < nb_variables; j++)
  154:                 {
  155:                     if (tableau[j].mutex != NULL)
  156:                     {
  157:                         pthread_mutex_unlock(tableau[j].mutex);
  158:                     }
  159:                 }
  160: 
  161:                 free(tableau);
  162:                 return(d_erreur);
  163:             }
  164: 
  165:             // Il faut désactiver la vérification des variables
  166:             // implicites car aucune variable de niveau strictement
  167:             // positif étant créée, la fonction recherche_type() pourrait
  168:             // échouer.
  169: 
  170:             registre = (*s_etat_processus).autorisation_nom_implicite;
  171:             (*s_etat_processus).autorisation_nom_implicite = 'Y';
  172:             recherche_type(s_etat_processus);
  173:             (*s_etat_processus).autorisation_nom_implicite = registre;
  174: 
  175:             if (((*s_etat_processus).erreur_execution != d_ex) ||
  176:                     ((*s_etat_processus).erreur_systeme != d_es))
  177:             {
  178:                 if ((*s_etat_processus).core == d_vrai)
  179:                 {
  180:                     if ((*s_etat_processus).langue == 'F')
  181:                     {
  182:                         printf("+++Information : "
  183:                                 "Génération du fichier rpl-core "
  184:                                 "[%d]\n", (int) getpid());
  185:                     }
  186:                     else
  187:                     {
  188:                         printf("+++Information : "
  189:                                 "Writing rpl-core file [%d]\n",
  190:                                 (int) getpid());
  191:                     }
  192: 
  193:                     rplcore(s_etat_processus);
  194: 
  195:                     if ((*s_etat_processus).langue == 'F')
  196:                     {
  197:                         printf("+++Information : "
  198:                                 "Processus tracé [%d]\n",
  199:                                 (int) getpid());
  200:                     }
  201:                     else
  202:                     {
  203:                         printf("+++Information : Done [%d]\n",
  204:                                 (int) getpid());
  205:                     }
  206: 
  207:                     fflush(stdout);
  208:                 }
  209: 
  210:                 if ((*s_etat_processus).langue == 'F')
  211:                 {
  212:                     printf("+++Fatal : Compilation impossible\n");
  213:                 }
  214:                 else
  215:                 {
  216:                     printf("+++Fatal : Compilation failed\n");
  217:                 }
  218: 
  219:                 for(j = 0; j < nb_variables; j++)
  220:                 {
  221:                     if (tableau[j].mutex != NULL)
  222:                     {
  223:                         pthread_mutex_unlock(tableau[j].mutex);
  224:                     }
  225:                 }
  226: 
  227:                 free(tableau);
  228:                 return(d_erreur);
  229:             }
  230: 
  231:             // Modification de la variable. Il n'existe à cet instant
  232:             // que des variables de niveau 0.
  233: 
  234:             if (recherche_variable(s_etat_processus, tableau[i].nom) ==
  235:                     d_faux)
  236:             {
  237:                 if ((*s_etat_processus).langue == 'F')
  238:                 {
  239:                     printf("+++Fatal : Compilation impossible\n");
  240:                 }
  241:                 else
  242:                 {
  243:                     printf("+++Fatal : Compilation failed\n");
  244:                 }
  245: 
  246:                 for(j = 0; j < nb_variables; j++)
  247:                 {
  248:                     if (tableau[j].mutex != NULL)
  249:                     {
  250:                         pthread_mutex_unlock(tableau[j].mutex);
  251:                     }
  252:                 }
  253: 
  254:                 free(tableau);
  255:                 return(d_erreur);
  256:             }
  257: 
  258:             liberation(s_etat_processus, tableau[i].objet);
  259: 
  260:             if (depilement(s_etat_processus, &((*s_etat_processus).l_base_pile),
  261:                     &((*(*s_etat_processus).pointeur_variable_courante).objet))
  262:                     == d_erreur)
  263:             {
  264:                 if ((*s_etat_processus).langue == 'F')
  265:                 {
  266:                     printf("+++Fatal : Compilation impossible\n");
  267:                 }
  268:                 else
  269:                 {
  270:                     printf("+++Fatal : Compilation failed\n");
  271:                 }
  272: 
  273:                 for(j = 0; j < nb_variables; j++)
  274:                 {
  275:                     if (tableau[j].mutex != NULL)
  276:                     {
  277:                         pthread_mutex_unlock(tableau[j].mutex);
  278:                     }
  279:                 }
  280: 
  281:                 free(tableau);
  282:                 return(d_erreur);
  283:             }
  284: 
  285:             (*(*s_etat_processus).pointeur_variable_courante).origine = 'E';
  286:             free((*s_etat_processus).instruction_courante);
  287: 
  288:             if (point_entree == i)
  289:             {
  290:                 programme_principal = (*(*s_etat_processus)
  291:                         .pointeur_variable_courante).objet;
  292:             }
  293:         }
  294:     }
  295: 
  296:     if (point_entree == -1)
  297:     {
  298:         if ((*s_etat_processus).langue == 'F')
  299:         {
  300:             printf("+++Fatal : Compilation impossible\n");
  301:         }
  302:         else
  303:         {
  304:             printf("+++Fatal : Compilation failed\n");
  305:         }
  306: 
  307:         for(j = 0; j < nb_variables; j++)
  308:         {
  309:             if (tableau[j].mutex != NULL)
  310:             {
  311:                 pthread_mutex_unlock(tableau[j].mutex);
  312:             }
  313:         }
  314: 
  315:         free(tableau);
  316:         return(d_erreur);
  317:     }
  318: 
  319:     if ((*s_etat_processus).debug == d_vrai)
  320:     {
  321:         if (((*s_etat_processus).type_debug &
  322:                 d_debug_analyse) != 0)
  323:         {
  324:             printf("\n");
  325: 
  326:             if ((*s_etat_processus).langue == 'F')
  327:             {
  328:                 printf("[%d] Compilation achevée\n", (int) getpid());
  329:             }
  330:             else
  331:             {
  332:                 printf("[%d] Compilation done\n", (int) getpid());
  333:             }
  334: 
  335:             printf("\n");
  336:             fflush(stdout);
  337:         }
  338:     }
  339: 
  340:     (*s_etat_processus).retour_routine_evaluation = 'Y';
  341: 
  342:     free((*s_etat_processus).definitions_chainees);
  343: 
  344:     if (((*s_etat_processus).definitions_chainees =
  345:             malloc(sizeof(unsigned char))) == NULL)
  346:     {
  347:         if ((*s_etat_processus).langue == 'F')
  348:         {
  349:             printf("+++Fatal : Compilation impossible\n");
  350:         }
  351:         else
  352:         {
  353:             printf("+++Fatal : Compilation failed\n");
  354:         }
  355: 
  356:         for(j = 0; j < nb_variables; j++)
  357:         {
  358:             if (tableau[j].mutex != NULL)
  359:             {
  360:                 pthread_mutex_unlock(tableau[j].mutex);
  361:             }
  362:         }
  363: 
  364:         free(tableau);
  365:         return(d_erreur);
  366:     }
  367: 
  368:     (*s_etat_processus).definitions_chainees[0] = d_code_fin_chaine;
  369:     (*s_etat_processus).longueur_definitions_chainees = 0;
  370:     (*s_etat_processus).evaluation_expression_compilee = 'Y';
  371: 
  372:     if ((*s_etat_processus).profilage == d_vrai)
  373:     {
  374:         profilage(s_etat_processus, tableau[point_entree].nom);
  375:     }
  376: 
  377:     if ((*s_etat_processus).erreur_systeme != d_es)
  378:     {
  379:         if ((*s_etat_processus).langue == 'F')
  380:         {
  381:             printf("+++Système : Mémoire insuffisante\n");
  382:         }
  383:         else
  384:         {
  385:             printf("+++System : Not enough memory\n");
  386:         }
  387: 
  388:         for(j = 0; j < nb_variables; j++)
  389:         {
  390:             if (tableau[j].mutex != NULL)
  391:             {
  392:                 pthread_mutex_unlock(tableau[j].mutex);
  393:             }
  394:         }
  395: 
  396:         free(tableau);
  397:         return(d_erreur);
  398:     }
  399: 
  400:     for(j = 0; j < nb_variables; j++)
  401:     {
  402:         if (tableau[j].mutex != NULL)
  403:         {
  404:             pthread_mutex_unlock(tableau[j].mutex);
  405:         }
  406:     }
  407: 
  408:     free(tableau);
  409: 
  410:     erreur = evaluation(s_etat_processus, programme_principal, 'E');
  411: 
  412:     if ((*s_etat_processus).profilage == d_vrai)
  413:     {
  414:         profilage(s_etat_processus, NULL);
  415:     }
  416: 
  417:     (*s_etat_processus).mode_execution_programme = 'N';
  418: 
  419:     if (((*s_etat_processus).erreur_execution != d_ex) ||
  420:             ((*s_etat_processus).exception != d_ep) ||
  421:             ((*s_etat_processus).erreur_systeme != d_es))
  422:     {
  423:         printf("%s [%d]\n", message = messages(s_etat_processus),
  424:                 (int) getpid());
  425: 
  426:         if (test_cfsf(s_etat_processus, 51) == d_faux)
  427:         {
  428:             printf("%s", ds_beep);
  429:         }
  430: 
  431:         free(message);
  432:     }
  433: 
  434:     return(erreur);
  435: }
  436: 
  437: // vim: ts=4

CVSweb interface <joel.bertrand@systella.fr>