target/whirl.c
static void generate_load(WhirlCodeSegment *segment, RingState *state) {
    Inst *inst = segment->inst;

    if (inst->src.type == REG) {
        // Get the jump address from the register, and convert it to the actual
        // memory position by multiplying it by two and adding the offset of the
        // data to it
        set_mem(segment, state, DATA_START);
        move_to_reg(segment, state, inst->src.reg);
        generate_math_command(segment, state, MATH_LOAD);
        generate_math_command(segment, state, MATH_ADD);
        move_back_from_reg(segment, state, inst->src.reg);
        generate_math_command(segment, state, MATH_ADD);
        generate_math_command(segment, state, MATH_STORE);
    }
    else {
        // Set up the current memory position to have the correct address for the
        // memory we're loading from
        set_mem(segment, state, inst->src.imm * 2 + DATA_START);
    }

    // Load the memory address, jump to it
    generate_op_command(segment, state, OP_LOAD);
    generate_op_command(segment, state, OP_DADD);

    // Save the memory address in the current cell, but negated, so we know
    // how far back in memory to jump to
    generate_op_command(segment, state, OP_STORE);
    generate_math_command(segment, state, MATH_LOAD);
    generate_math_command(segment, state, MATH_NEG);
    generate_math_command(segment, state, MATH_STORE);

    // Move to the temp cell to the right of the memory we want to load
    generate_op_command(segment, state, OP_ONE);
    generate_op_command(segment, state, OP_DADD);
    generate_op_command(segment, state, OP_DADD);

    // Now we set operation value to negative one so we can go backwards in
    // memory
    generate_math_command(segment, state, MATH_LOAD);
    generate_math_command(segment, state, MATH_EQUAL);
    generate_math_command(segment, state, MATH_NEG);
    generate_math_command(segment, state, MATH_STORE);
    generate_op_command(segment, state, OP_LOAD);

    // Move back to the memory we want to load, and load it into the math value
    generate_op_command(segment, state, OP_DADD);
    generate_math_command(segment, state, MATH_LOAD);

    // Now go back to the position where we saved the memory address
    generate_op_command(segment, state, OP_DADD);
    generate_op_command(segment, state, OP_LOAD);
    generate_op_command(segment, state, OP_DADD);

    // Save the math value in the destination register
    move_to_reg(segment, state, inst->dst.reg);
    generate_math_command(segment, state, MATH_STORE);
    move_back_from_reg(segment, state, inst->dst.reg);
}
// Generates whirl code implementing a elvm LOAD instruction
Generates whirl code implementing a elvm LOAD instruction
Generates whirl code implementing a elvm LOAD instruction
static void generate_load(WhirlCodeSegment *segment, RingState *state) {
    Inst *inst = segment->inst;
    if (inst->src.type == REG) {
        set_mem(segment, state, DATA_START);
        move_to_reg(segment, state, inst->src.reg);
        generate_math_command(segment, state, MATH_LOAD);
        generate_math_command(segment, state, MATH_ADD);
        move_back_from_reg(segment, state, inst->src.reg);
        generate_math_command(segment, state, MATH_ADD);
        generate_math_command(segment, state, MATH_STORE);
    }
    else {
        set_mem(segment, state, inst->src.imm * 2 + DATA_START);
    }
    generate_op_command(segment, state, OP_LOAD);
    generate_op_command(segment, state, OP_DADD);
    generate_op_command(segment, state, OP_STORE);
    generate_math_command(segment, state, MATH_LOAD);
    generate_math_command(segment, state, MATH_NEG);
    generate_math_command(segment, state, MATH_STORE);
    generate_op_command(segment, state, OP_ONE);
    generate_op_command(segment, state, OP_DADD);
    generate_op_command(segment, state, OP_DADD);
    generate_math_command(segment, state, MATH_LOAD);
    generate_math_command(segment, state, MATH_EQUAL);
    generate_math_command(segment, state, MATH_NEG);
    generate_math_command(segment, state, MATH_STORE);
    generate_op_command(segment, state, OP_LOAD);
    generate_op_command(segment, state, OP_DADD);
    generate_math_command(segment, state, MATH_LOAD);
    generate_op_command(segment, state, OP_DADD);
    generate_op_command(segment, state, OP_LOAD);
    generate_op_command(segment, state, OP_DADD);
    move_to_reg(segment, state, inst->dst.reg);
    generate_math_command(segment, state, MATH_STORE);
    move_back_from_reg(segment, state, inst->dst.reg);
}
static void generate_load ( WhirlCodeSegment * segment , RingState * state ) { Inst * inst = segment -> inst ; if ( inst -> src . type == REG ) { set_mem ( segment , state , DATA_START ) ; move_to_reg ( segment , state , inst -> src . reg ) ; generate_math_command ( segment , state , MATH_LOAD ) ; generate_math_command ( segment , state , MATH_ADD ) ; move_back_from_reg ( segment , state , inst -> src . reg ) ; generate_math_command ( segment , state , MATH_ADD ) ; generate_math_command ( segment , state , MATH_STORE ) ; } else { set_mem ( segment , state , inst -> src . imm * 2 + DATA_START ) ; } generate_op_command ( segment , state , OP_LOAD ) ; generate_op_command ( segment , state , OP_DADD ) ; generate_op_command ( segment , state , OP_STORE ) ; generate_math_command ( segment , state , MATH_LOAD ) ; generate_math_command ( segment , state , MATH_NEG ) ; generate_math_command ( segment , state , MATH_STORE ) ; generate_op_command ( segment , state , OP_ONE ) ; generate_op_command ( segment , state , OP_DADD ) ; generate_op_command ( segment , state , OP_DADD ) ; generate_math_command ( segment , state , MATH_LOAD ) ; generate_math_command ( segment , state , MATH_EQUAL ) ; generate_math_command ( segment , state , MATH_NEG ) ; generate_math_command ( segment , state , MATH_STORE ) ; generate_op_command ( segment , state , OP_LOAD ) ; generate_op_command ( segment , state , OP_DADD ) ; generate_math_command ( segment , state , MATH_LOAD ) ; generate_op_command ( segment , state , OP_DADD ) ; generate_op_command ( segment , state , OP_LOAD ) ; generate_op_command ( segment , state , OP_DADD ) ; move_to_reg ( segment , state , inst -> dst . reg ) ; generate_math_command ( segment , state , MATH_STORE ) ; move_back_from_reg ( segment , state , inst -> dst . reg ) ; }
Generates whirl code implementing a elvm LOAD instruction
Generates whirl code implementing a elvm LOAD instruction
// Get the jump address from the register, and convert it to the actual // memory position by multiplying it by two and adding the offset of the // data to it // Set up the current memory position to have the correct address for the // memory we're loading from // Load the memory address, jump to it // Save the memory address in the current cell, but negated, so we know // how far back in memory to jump to // Move to the temp cell to the right of the memory we want to load // Now we set operation value to negative one so we can go backwards in // memory // Move back to the memory we want to load, and load it into the math value // Now go back to the position where we saved the memory address // Save the math value in the destination register