Ejemplo n.º 1
0
Archivo: sc7.c Proyecto: z80/mapper
static void stgopt(char *start,char *end,int (*outputfunc)(char *str))
{
    char symbols[MAX_OPT_VARS+1][MAX_ALIAS+1];
    int seq,match_length,repl_length;
    int matches;
    char *debut=start;  /* save original start of the buffer */

    assert(sequences!=NULL);
    /* do not match anything if debug-level is maximum */
    if (pc_optimize>sOPTIMIZE_NONE && sc_status==statWRITE) {
        do {
            matches=0;
            start=debut;
            while (start<end) {
                seq=0;
                while (sequences[seq].find!=NULL) {
                    assert(seq>=0);
                    if (*sequences[seq].find<sOPTIMIZE_NUMBER) {
                        if (*sequences[seq].find>pc_optimize)
                            break;    /* don't look further */
                        seq++;    /* continue with next string */
                        continue;
                    } /* if */
                    if (matchsequence(start,end,sequences[seq].find,symbols,&match_length)) {
                        char *replace=replacesequence(sequences[seq].replace,symbols,&repl_length);
                        /* If the replacement is bigger than the original section, we may need
                         * to "grow" the staging buffer. This is quite complex, due to the
                         * re-ordering of expressions that can also happen in the staging
                         * buffer. In addition, it should not happen: the peephole optimizer
                         * must replace sequences with *shorter* sequences, not longer ones.
                         * So, I simply forbid sequences that are longer than the ones they
                         * are meant to replace.
                         */
                        assert(match_length>=repl_length);
                        if (match_length>=repl_length) {
                            strreplace(start,replace,match_length,repl_length,(int)(end-start));
                            end-=match_length-repl_length;
                            free(replace);
                            code_idx-=opcodes(sequences[seq].opc)+opargs(sequences[seq].arg);
                            seq=0;                      /* restart search for matches */
                            matches++;
                        } else {
                            /* actually, we should never get here (match_length<repl_length) */
                            assert(0);
                            free(replace);
                            seq++;
                        } /* if */
                    } else {
                        seq++;
                    } /* if */
                } /* while */
                assert(sequences[seq].find==NULL || *sequences[seq].find<sOPTIMIZE_NUMBER);
                start += strlen(start) + 1;       /* to next string */
            } /* while (start<end) */
        } while (matches>0);
    } /* if (pc_optimize>sOPTIMIZE_NONE && sc_status==statWRITE) */

    for (start=debut; start<end; start+=strlen(start)+1)
        outputfunc(start);
}
static void stgopt(char *start, char *end) {
    char symbols[_maxoptvars][_aliasmax + 1];
    int seq, match_length, repl_length;

    assert(sequences != NULL);
    while (start < end) {
        if ((sc_debug & sNOOPTIMIZE) != 0 || sc_status != statWRITE) {
            /* do not match anything if debug-level is maximum */
            filewrite(start);
        } else {
            seq = 0;
            while (sequences[seq].find) {
                assert(seq >= 0);
                if (matchsequence(start, end, sequences[seq].find, symbols,
                                  &match_length)) {
                    char *replace = replacesequence(sequences[seq].replace,
                                                    symbols, &repl_length);
                    /* If the replacement is bigger than the original section, we may need
                     * to "grow" the staging buffer. This is quite complex, due to the
                     * re-ordering of expressions that can also happen in the staging
                     * buffer. In addition, it should not happen: the peephole optimizer
                     * must replace sequences with *shorter* sequences, not longer ones.
                     * So, I simply forbid sequences that are longer than the ones they
                     * are meant to replace.
                     */
                    assert(match_length >= repl_length);
                    if (match_length >= repl_length) {
                        strreplace(start, replace, match_length, repl_length,
                                   (int) (end - start));
                        end -= match_length - repl_length;
                        free(replace);
                        code_idx -= sequences[seq].savesize;
                        seq = 0; /* restart search for matches */
                    } else {
                        /* actually, we should never get here (match_length<repl_length) */
                        assert(0);
                        seq++;
                    } /* if */
                } else {
                    seq++;
                } /* if */
            } /* while */
            assert(sequences[seq].find == NULL);
            filewrite(start);
        } /* if */
        assert(start < end);
        start += strlen(start) + 1; /* to next string */
    } /* while (start<end) */
}