target/whirl.c
static void generate_code_segments(WhirlCodeSegment *last_segment, RingState *state) {
    WhirlCodeSegment *first_segment = last_segment;

    while (first_segment != NULL) {
        last_segment = first_segment;
        if (is_jump(first_segment->inst->op)) {
            generate_segment(first_segment, state);
        }
        else {
            // We want to minimize the amount of times we reset the ring states,
            // so we try to generate the longest string of instructions at once
            // that we can. We can only generate the same instructions at once if
            // they have same PC and they don't have any jump instructions
            while (first_segment->prev != NULL &&
                   !is_jump(first_segment->prev->inst->op) &&
                   first_segment->prev->inst->pc == first_segment->inst->pc)
            {
                first_segment = first_segment->prev;
            }

            WhirlCodeSegment *segment = first_segment;
            while (segment != last_segment->next) {
                generate_segment(segment, state);
                segment = segment->next;
            }
            reset_to_after_jump(last_segment, state);
        }

        // Update the total code length field for the newly-added code segments
        for (WhirlCodeSegment *segment = last_segment; segment != first_segment->prev; segment = segment->prev) {
            if (segment->next == NULL) {
                segment->total_code_len = segment->len;
            }
            else {
                segment->total_code_len = segment->len + segment->next->total_code_len;
            }
        }

        first_segment = first_segment->prev;
    }
}
// Goes through the code segments in reverse order, generating the Whirl code to
// implement each ELVM instruction. This must be done in reverse order, because
// jump instructions need to know how far forward they have to jump, so all of the
// later instructions must be generated already. 
Goes through the code segments in reverse order, generating the Whirl code to
implement each ELVM instruction. This must be done in reverse order, because
jump instructions need to know how far forward they have to jump, so all of the
later instructions must be generated already.
Goes through the code segments in reverse order generating the Whirl code to implement each ELVM instruction . This must be done in reverse order because jump instructions need to know how far forward they have to jump so all of the later instructions must be generated already .
static void generate_code_segments(WhirlCodeSegment *last_segment, RingState *state) {
    WhirlCodeSegment *first_segment = last_segment;
    while (first_segment != NULL) {
        last_segment = first_segment;
        if (is_jump(first_segment->inst->op)) {
            generate_segment(first_segment, state);
        }
        else {
            while (first_segment->prev != NULL &&
                   !is_jump(first_segment->prev->inst->op) &&
                   first_segment->prev->inst->pc == first_segment->inst->pc)
            {
                first_segment = first_segment->prev;
            }
            WhirlCodeSegment *segment = first_segment;
            while (segment != last_segment->next) {
                generate_segment(segment, state);
                segment = segment->next;
            }
            reset_to_after_jump(last_segment, state);
        }
        for (WhirlCodeSegment *segment = last_segment; segment != first_segment->prev; segment = segment->prev) {
            if (segment->next == NULL) {
                segment->total_code_len = segment->len;
            }
            else {
                segment->total_code_len = segment->len + segment->next->total_code_len;
            }
        }
        first_segment = first_segment->prev;
    }
}
static void generate_code_segments ( WhirlCodeSegment * last_segment , RingState * state ) { WhirlCodeSegment * first_segment = last_segment ; while ( first_segment != NULL ) { last_segment = first_segment ; if ( is_jump ( first_segment -> inst -> op ) ) { generate_segment ( first_segment , state ) ; } else { while ( first_segment -> prev != NULL && ! is_jump ( first_segment -> prev -> inst -> op ) && first_segment -> prev -> inst -> pc == first_segment -> inst -> pc ) { first_segment = first_segment -> prev ; } WhirlCodeSegment * segment = first_segment ; while ( segment != last_segment -> next ) { generate_segment ( segment , state ) ; segment = segment -> next ; } reset_to_after_jump ( last_segment , state ) ; } for ( WhirlCodeSegment * segment = last_segment ; segment != first_segment -> prev ; segment = segment -> prev ) { if ( segment -> next == NULL ) { segment -> total_code_len = segment -> len ; } else { segment -> total_code_len = segment -> len + segment -> next -> total_code_len ; } } first_segment = first_segment -> prev ; } }
Goes through the code segments in reverse order, generating the Whirl code to
implement each ELVM instruction.
Goes through the code segments in reverse order generating the Whirl code to implement each ELVM instruction .
// We want to minimize the amount of times we reset the ring states, // so we try to generate the longest string of instructions at once // that we can. We can only generate the same instructions at once if // they have same PC and they don't have any jump instructions // Update the total code length field for the newly-added code segments