ir.c
// Renumber all labels so they are consecutive. Uses label_count global.
Renumber all labels so they are consecutive.
Renumber all labels so they are consecutive .
void renumber_labels(Function *function) {
    int max_label_count = 0;
    for (Tac *tac = function->ir; tac; tac = tac->next) {
        if (tac->src1 && tac->src1->label && tac->src1->label > max_label_count) max_label_count = tac->src1->label;
        if (tac->src2 && tac->src2->label && tac->src2->label > max_label_count) max_label_count = tac->src2->label;
        if (tac->label && tac->label > max_label_count) max_label_count = tac->label;
    }
    if (!max_label_count) return;
    int *mapping = malloc(sizeof(int) * (max_label_count + 1));
    memset(mapping, 0, sizeof(int) * (max_label_count + 1));
    for (Tac *tac = function->ir; tac; tac = tac->next) {
        if (tac->src1 && tac->src1->label && !mapping[tac->src1->label]) mapping[tac->src1->label] = ++label_count;
        if (tac->src2 && tac->src2->label && !mapping[tac->src2->label]) mapping[tac->src2->label] = ++label_count;
        if (tac->label && !mapping[tac->label]) mapping[tac->label] = ++label_count;
    }
    for (Tac *tac = function->ir; tac; tac = tac->next) {
        if (tac->src1 && tac->src1->label && !tac->src1->int_value) { tac->src1->label = mapping[tac->src1->label]; tac->src1->int_value = 1; }
        if (tac->src2 && tac->src2->label && !tac->src2->int_value) { tac->src2->label = mapping[tac->src2->label]; tac->src2->int_value = 1; }
        if (tac->label) tac->label = mapping[tac->label];
    }
    for (Tac *tac = function->ir; tac; tac = tac->next) {
        if (tac->src1 && tac->src1->label) tac->src1->int_value = 0;
        if (tac->src2 && tac->src2->label) tac->src2->int_value = 0;
    }
    free(mapping);
}
void renumber_labels ( Function * function ) { int max_label_count = 0 ; for ( Tac * tac = function -> ir ; tac ; tac = tac -> next ) { if ( tac -> src1 && tac -> src1 -> label && tac -> src1 -> label > max_label_count ) max_label_count = tac -> src1 -> label ; if ( tac -> src2 && tac -> src2 -> label && tac -> src2 -> label > max_label_count ) max_label_count = tac -> src2 -> label ; if ( tac -> label && tac -> label > max_label_count ) max_label_count = tac -> label ; } if ( ! max_label_count ) return ; int * mapping = malloc ( sizeof ( int ) * ( max_label_count + 1 ) ) ; memset ( mapping , 0 , sizeof ( int ) * ( max_label_count + 1 ) ) ; for ( Tac * tac = function -> ir ; tac ; tac = tac -> next ) { if ( tac -> src1 && tac -> src1 -> label && ! mapping [ tac -> src1 -> label ] ) mapping [ tac -> src1 -> label ] = ++ label_count ; if ( tac -> src2 && tac -> src2 -> label && ! mapping [ tac -> src2 -> label ] ) mapping [ tac -> src2 -> label ] = ++ label_count ; if ( tac -> label && ! mapping [ tac -> label ] ) mapping [ tac -> label ] = ++ label_count ; } for ( Tac * tac = function -> ir ; tac ; tac = tac -> next ) { if ( tac -> src1 && tac -> src1 -> label && ! tac -> src1 -> int_value ) { tac -> src1 -> label = mapping [ tac -> src1 -> label ] ; tac -> src1 -> int_value = 1 ; } if ( tac -> src2 && tac -> src2 -> label && ! tac -> src2 -> int_value ) { tac -> src2 -> label = mapping [ tac -> src2 -> label ] ; tac -> src2 -> int_value = 1 ; } if ( tac -> label ) tac -> label = mapping [ tac -> label ] ; } for ( Tac * tac = function -> ir ; tac ; tac = tac -> next ) { if ( tac -> src1 && tac -> src1 -> label ) tac -> src1 -> int_value = 0 ; if ( tac -> src2 && tac -> src2 -> label ) tac -> src2 -> int_value = 0 ; } free ( mapping ) ; }
Renumber all labels so they are consecutive.
Renumber all labels so they are consecutive .
// Find highest label number in the function // Make mapping of old to new label indexes // Set int_value to one for renamed labels. This is needed since some Value objects // are shared, and should not be renamed twice.