ScopLib::ScopLib(Scop *S) : PollyScop(S) { scoplib = scoplib_scop_malloc(); initializeArrays(); initializeParameters(); initializeScattering(); initializeStatements(); }
/** * scoplib_scop_dup function: * This function returns a fresh identical (non shadow) copy of the * input scop. * \param scop The scop whose information have to be printed. ** */ scoplib_scop_p scoplib_scop_dup(scoplib_scop_p scop) { int i; scoplib_statement_p stm; scoplib_statement_p tmp = NULL; scoplib_symbol_p tmpsymbol = NULL; scoplib_symbol_p symbol; scoplib_scop_p ret = scoplib_scop_malloc(); ret->context = scoplib_matrix_copy(scop->context); ret->nb_parameters = scop->nb_parameters; ret->parameters = (char**) malloc(sizeof(char*) * ret->nb_parameters); for (i = 0; i < ret->nb_parameters; ++i) ret->parameters[i] = strdup(scop->parameters[i]); ret->nb_arrays = scop->nb_arrays; ret->arrays = (char**) malloc(sizeof(char*) * ret->nb_arrays); for (i = 0; i < ret->nb_arrays; ++i) ret->arrays[i] = strdup(scop->arrays[i]); for (stm = scop->statement; stm; stm = stm->next) { scoplib_statement_p newstm = scoplib_statement_malloc(); newstm->domain = scoplib_matrix_list_malloc(); newstm->domain->elt = scoplib_matrix_copy(stm->domain->elt); newstm->schedule = scoplib_matrix_copy(stm->schedule); newstm->read = scoplib_matrix_copy(stm->read); newstm->write = scoplib_matrix_copy(stm->write); newstm->nb_iterators = stm->nb_iterators; newstm->iterators = (char**) malloc(sizeof(char*) * newstm->nb_iterators); for (i = 0; i < newstm->nb_iterators; ++i) newstm->iterators[i] = strdup(stm->iterators[i]); newstm->body = strdup (stm->body); if (ret->statement == NULL) ret->statement = tmp = newstm; else { tmp->next = newstm; tmp = tmp->next; } } for(symbol = scop->symbol_table;symbol;symbol = symbol->next) { scoplib_symbol_p newsymbol = scoplib_symbol_malloc(); newsymbol->identifier = strdup(symbol->identifier); newsymbol->type = symbol->type; if(ret->symbol_table == NULL ) ret->symbol_table = tmpsymbol = newsymbol; else { tmpsymbol->next = newsymbol; tmpsymbol = tmpsymbol->next; } } if (scop->optiontags) ret->optiontags = strdup(scop->optiontags); ret->usr = scop->usr; return ret; }
/** * scoplib_scop_read function: * This function reads a scoplib_scop_t structure from an input stream * (possibly stdin) corresponding to a scoplib SCoP dump. * \param file The input stream */ scoplib_scop_p scoplib_scop_read(FILE* file) { char tmpbuff[SCOPLIB_MAX_STRING]; scoplib_scop_p scop = NULL; scoplib_statement_p stmt = NULL; scoplib_statement_p prev = NULL; int nb_statements; char** tmp; int i; char* content; if (file == NULL) return NULL; scop = scoplib_scop_malloc(); /* Backup the arrays of the program. Buffer is reajustable. */ int nb_arr = SCOPLIB_MAX_STRING; char** arrays = (char**) malloc (sizeof(char*) * nb_arr); for (i = 0; i < nb_arr; ++i) arrays[i] = NULL; /* Ensure the file is a .scop. */ tmp = scoplib_scop_read_strings(file, 1); if (strcmp(*tmp, "SCoP")) { fprintf(stderr, "[Scoplib] Error. The file is not a .scop\n"); exit (1); } free(*tmp); free(tmp); /* Read the language. */ char** language = scoplib_scop_read_strings(file, 1); if (strcmp(*language, "C") && strcmp(*language, "JAVA") && strcmp(*language, "C#")) { fprintf(stderr, "[Scoplib] Error. The language is not recognized\n"); exit (1); } /* language is not used so far. */ free(*language); free(language); /* Read the context. */ scop->context = scoplib_matrix_read (file); scop->nb_parameters = scop->context->NbColumns - 2; /* Read the parameter names, if any. */ if (scoplib_scop_read_int(file, NULL) > 0) scop->parameters = scoplib_scop_read_strings (file, scop->nb_parameters); else scop->parameters = scoplib_scop_generate_names("M", scop->nb_parameters); /* Read the number of statements. */ nb_statements = scoplib_scop_read_int (file, NULL); for (i = 0; i < nb_statements; ++i) { /* Read each statement. */ stmt = scoplib_statement_read (file, scop->nb_parameters, &arrays, &nb_arr); if (scop->statement == NULL) scop->statement = stmt; else prev->next = stmt; prev = stmt; } /* Read the remainder of the file, and store it in the optiontags field. */ /* Skip blank lines. */ while (! feof(file) && (fgets(tmpbuff, SCOPLIB_MAX_STRING, file) == 0 || tmpbuff[0] == '#' || isspace(tmpbuff[0]) || tmpbuff[0] != '<')) ; /* Store the remainder of the file, if any. */ if (tmpbuff[0]) { int count = strlen(tmpbuff); int pos = 0; int bufs = SCOPLIB_MAX_STRING; scop->optiontags = (char*) malloc(bufs * sizeof(char)); do { scop->optiontags = (char*) realloc (scop->optiontags, (bufs += count) * sizeof(char)); for (i = 0; i < count; ++i) scop->optiontags[pos++] = tmpbuff[i]; } while ((count = fread(tmpbuff, sizeof(char), SCOPLIB_MAX_STRING, file)) > 0); } /* Count the number of referenced arrays/variables. */ scop->nb_arrays = 0; for (stmt = scop->statement; stmt; stmt = stmt->next) { if (stmt->read) for (i = 0; i < stmt->read->NbRows; ++i) if (scop->nb_arrays < SCOPVAL_get_si(stmt->read->p[i][0])) scop->nb_arrays = SCOPVAL_get_si(stmt->read->p[i][0]); if (stmt->write) for (i = 0; i < stmt->write->NbRows; ++i) if (scop->nb_arrays < SCOPVAL_get_si(stmt->write->p[i][0])) scop->nb_arrays = SCOPVAL_get_si(stmt->write->p[i][0]); } /* Allocate the array names array. */ scop->arrays = (char**) malloc(sizeof(char*) * (scop->nb_arrays + 1)); for (i = 0; i < scop->nb_arrays; ++i) scop->arrays[i] = NULL; /* Populate the array list with referenced in the <array> tag, if any. */ if ((content = scoplib_scop_tag_content(scop, "<arrays>", "</arrays>"))) { char* start = content; int n_arr = scoplib_scop_read_int(NULL, &content); char buff2[SCOPLIB_MAX_STRING]; int idx_array; i = 0; while (n_arr--) { /* Skip blank or commented lines. */ while (*content == '#' || *content == '\n') { for (; *content != '\n'; ++content) ; ++content; } /* Get the variable id. */ for (i = 0; *content && ! isspace(*content); ++i, ++content) buff2[i] = *content; buff2[i] = '\0'; sscanf (buff2, "%d", &idx_array); /* Get the variable name. */ while (*content && isspace(*content)) ++content; for (i = 0; *content && ! isspace(*content); ++i, ++content) buff2[i] = *content; buff2[i] = '\0'; /* array is in 0-basis. */ if (arrays[idx_array - 1]) free(arrays[idx_array - 1]); arrays[idx_array - 1] = strdup(buff2); /* Go to the end of line. */ while (*content && *content != '\n') ++content; } content = start; } /* Fill the array of array names. */ char** tmparrays = scoplib_scop_generate_names("var", scop->nb_arrays); for (i = 0; i < scop->nb_arrays; ++i) { if (arrays[i] == NULL || arrays[i][0] == '\0') { /* Use a generated name in case no array name was parsed. */ scop->arrays[i] = tmparrays[i]; if (arrays[i]) free(arrays[i]); } else { /* Use the parsed array name. */ scop->arrays[i] = arrays[i]; free(tmparrays[i]); } } scop->arrays[i] = NULL; free(arrays); free(tmparrays); return scop; }