## instructions-rewrite-pwrpc32-g.pkg
# Compiled by:
#
src/lib/compiler/back/low/pwrpc32/backend-pwrpc32.lib# We get invoked from:
#
#
src/lib/compiler/back/low/main/pwrpc32/backend-lowhalf-pwrpc32.pkg#
src/lib/compiler/back/low/pwrpc32/regor/spill-instructions-pwrpc32-g.pkgstipulate
package lem = lowhalf_error_message; # lowhalf_error_message is from
src/lib/compiler/back/low/control/lowhalf-error-message.pkg package rkj = registerkinds_junk; # registerkinds_junk is from
src/lib/compiler/back/low/code/registerkinds-junk.pkgherein
generic package instructions_rewrite_pwrpc32_g (
# ==============================
#
mcf: Machcode_Pwrpc32 # Machcode_Pwrpc32 is from
src/lib/compiler/back/low/pwrpc32/code/machcode-pwrpc32.codemade.api )
{
# Export to client packages:
#
package mcf = mcf; # "mcf" == "machcode_form" (abstract machine code).
stipulate
package rgk = mcf::rgk; # "rgk" == "registerkinds".
package cls = rkj::cls; # "cls" == "codetemplists".
herein
fun error msg
=
lem::error ("instructions_rewrite_pwrpc32_g", msg);
fun ea (e as THE (mcf::DIRECT r), rs, rt)
=>
if (rkj::codetemps_are_same_color (r, rs)) THE (mcf::DIRECT rt);
else e;
fi;
ea (e as THE (mcf::FDIRECT r), rs, rt)
=>
if (rkj::codetemps_are_same_color (r, rs)) THE (mcf::FDIRECT rt);
else e;
fi;
ea (e as THE (mcf::DISPLACE { base, disp, ramregion } ), rs, rt)
=>
if (rkj::codetemps_are_same_color (base, rs)) THE (mcf::DISPLACE { base=>rt, disp, ramregion } );
else e;
fi;
ea (NULL, _, _)
=>
NULL;
end;
fun rewrite_use (instruction, rs, rt)
=
{
fun rplac r
=
if (rkj::codetemps_are_same_color (r, rs)) rt;
else r;
fi;
fun rw_operand (operand as mcf::REG_OP r)
=>
if (rkj::codetemps_are_same_color (r, rs)) mcf::REG_OP rt;
else operand;
fi;
rw_operand operand => operand;
end;
fun ea (THE (mcf::DISPLACE { base, disp, ramregion } ))
=>
THE (mcf::DISPLACE { base=>rplac base, disp, ramregion } );
ea x => x;
end;
fun use_pwrpc32 instruction
=
case instruction
#
mcf::LL { ld, rt, ra, d, ramregion }
=>
mcf::LL { ld, rt, ra=>rplac ra, d=>rw_operand d, ramregion };
mcf::LF { ld, ft, ra, d, ramregion }
=>
mcf::LF { ld, ft, ra=>rplac ra, d=>rw_operand d, ramregion };
mcf::ST { st, rs, ra, d, ramregion }
=>
mcf::ST { st, rs=>rplac rs, ra=>rplac ra, d=>rw_operand d, ramregion };
mcf::STF { st, fs, ra, d, ramregion }
=>
mcf::STF { st, fs, ra=>rplac ra, d=>rw_operand d, ramregion };
mcf::UNARY { oper, rt, ra, rc, oe }
=>
mcf::UNARY { oper, rt, ra=>rplac ra, rc, oe };
mcf::ARITH { oper, rt, ra, rb, rc, oe }
=>
mcf::ARITH { oper, rt, ra=>rplac ra, rb=>rplac rb, rc, oe };
mcf::ARITHI { oper, rt, ra, im }
=>
mcf::ARITHI { oper, rt, ra=>rplac ra, im=>rw_operand im };
mcf::ROTATE { oper, ra, rs, sh, mb, me }
=>
mcf::ROTATE { oper, ra, rs=>rplac rs, sh=>rplac sh, mb, me };
mcf::ROTATEI { oper, ra, rs, sh, mb, me }
=>
mcf::ROTATEI { oper, ra, rs=>rplac rs, sh=>rw_operand sh, mb, me };
mcf::COMPARE { cmp, bf, l, ra, rb }
=>
mcf::COMPARE { cmp, bf, l, ra=>rplac ra, rb=>rw_operand rb };
mcf::MTSPR { rs, spr } => mcf::MTSPR { rs=>rplac rs, spr };
mcf::TW { to, ra, si } => mcf::TW { to, ra=>rplac ra, si=>rw_operand si };
mcf::TD { to, ra, si } => mcf::TD { to, ra=>rplac ra, si=>rw_operand si };
mcf::CALL { def, uses, cuts_to, ramregion }
=>
mcf::CALL { def, cuts_to, ramregion,
uses => cls::replace_this_by_that_in_codetemplists { this=>rs, that=>rt } uses
};
mcf::LWARX { rt, ra, rb }
=>
mcf::LWARX { rt, ra=>rplac ra, rb=>rplac rb };
mcf::STWCX { rs, ra, rb }
=>
mcf::STWCX { rs=>rplac rs, ra=>rplac ra, rb=>rplac rb };
_ => instruction;
esac;
case instruction
#
mcf::NOTE { op, ... }
=>
rewrite_use (op, rs, rt);
mcf::BASE_OP instruction
=>
mcf::BASE_OP (use_pwrpc32 instruction);
mcf::COPY { kind, size_in_bits, dst, src, tmp }
=>
mcf::COPY { kind, size_in_bits, dst, tmp=> ea tmp,
src=>case kind rkj::INT_REGISTER => map rplac src; _ => src; esac
};
mcf::LIVE { regs, spilled }
=>
mcf::LIVE { regs=>rgk::add_codetemp_info_to_appropriate_kindlist (rt, rgk::drop_codetemp_info_from_codetemplists (rs, regs)), spilled };
_ => error "rewrite_use";
esac;
};
fun rewrite_def (instruction, rs, rt)
=
{ fun rplac r
=
if (rkj::codetemps_are_same_color (r, rs)) rt;
else r;
fi;
fun ea (THE (mcf::DIRECT r)) => THE (mcf::DIRECT (rplac r));
ea x => x;
end;
fun def_pwrpc32 instruction
=
case instruction
#
mcf::LL { ld, rt, ra, d, ramregion }
=>
mcf::LL { ld, rt=>rplac rt, ra, d, ramregion };
mcf::UNARY { oper, rt, ra, rc, oe }
=>
mcf::UNARY { oper, rt=>rplac rt, ra, rc, oe };
mcf::ARITH { oper, rt, ra, rb, rc, oe }
=>
mcf::ARITH { oper, rt=>rplac rt, ra, rb, rc, oe };
mcf::ARITHI { oper, rt, ra, im }
=>
mcf::ARITHI { oper, rt=>rplac rt, ra, im };
mcf::ROTATE { oper, ra, rs, sh, mb, me }
=>
mcf::ROTATE { oper, ra=>rplac ra, rs, sh, mb, me };
mcf::ROTATEI { oper, ra, rs, sh, mb, me }
=>
mcf::ROTATEI { oper, ra=>rplac ra, rs, sh, mb, me };
mcf::MFSPR { rt, spr }
=>
mcf::MFSPR { rt=>rplac rt, spr };
mcf::CALL { def, uses, cuts_to, ramregion }
=>
mcf::CALL { def=>cls::replace_this_by_that_in_codetemplists { this=>rs, that=>rt } def, uses, cuts_to, ramregion };
mcf::LWARX { rt, ra, rb }
=>
mcf::LWARX { rt=>rplac rt, ra, rb };
_ =>
instruction;
esac;
case instruction
#
mcf::NOTE { op, ... }
=>
rewrite_def (op, rs, rt);
mcf::BASE_OP i
=>
mcf::BASE_OP (def_pwrpc32 i);
mcf::DEAD { regs, spilled }
=>
mcf::DEAD { regs=>rgk::add_codetemp_info_to_appropriate_kindlist (rt, rgk::drop_codetemp_info_from_codetemplists (rs, regs)), spilled };
mcf::COPY { kind, size_in_bits, dst, src, tmp }
=>
mcf::COPY { kind, size_in_bits, src, tmp=>ea tmp,
dst => case kind
rkj::INT_REGISTER => map rplac dst;
_ => dst;
esac
};
_ => error "rewriteDef";
esac;
};
fun frewrite_use (instruction, fs, ft)
=
{ fun rplac r
=
if (rkj::codetemps_are_same_color (r, fs) ) ft; else r;fi;
fun use_pwrpc32 instruction
=
case instruction
#
mcf::STF { st, fs, ra, d, ramregion }
=>
mcf::STF { st, fs=>rplac fs, ra, d, ramregion };
mcf::CALL { def, uses, cuts_to, ramregion }
=>
mcf::CALL { def, uses=>cls::replace_this_by_that_in_codetemplists { this=>fs, that=>ft } uses, cuts_to, ramregion };
mcf::FCOMPARE { cmp, bf, fa, fb }
=>
mcf::FCOMPARE { cmp, bf, fa=>rplac fa, fb=>rplac fb };
mcf::FUNARY { oper, ft, fb, rc }
=>
mcf::FUNARY { oper, ft, fb=>rplac fb, rc };
mcf::FARITH { oper, ft, fa, fb, rc }
=>
mcf::FARITH { oper, ft, fa=>rplac fa, fb=>rplac fb, rc };
mcf::FARITH3 { oper, ft, fa, fb, fc, rc }
=>
mcf::FARITH3 { oper, ft, fa=>rplac fa, fb=>rplac fb, fc=>rplac fc, rc };
_ => instruction;
esac;
case instruction
#
mcf::NOTE { op, ... }
=>
frewrite_use (op, fs, ft);
mcf::BASE_OP i
=>
mcf::BASE_OP (use_pwrpc32 i);
mcf::LIVE { regs, spilled }
=>
mcf::LIVE { regs=>rgk::add_codetemp_info_to_appropriate_kindlist (ft, rgk::drop_codetemp_info_from_codetemplists (fs, regs)), spilled };
mcf::COPY { kind as rkj::FLOAT_REGISTER, size_in_bits, dst, src, tmp }
=>
mcf::COPY { kind, size_in_bits, dst, src=>map rplac src, tmp };
_ => error "frewriteUse";
esac;
};
fun frewrite_def (instruction, fs, ft)
=
{ fun rplac r
=
if (rkj::codetemps_are_same_color (r, fs) ) ft; else r;fi;
fun rplac_ea (THE (mcf::FDIRECT f))
=>
THE (mcf::FDIRECT (rplac f));
rplac_ea ea
=>
ea;
end;
fun def_pwrpc32 instruction
=
case instruction
#
mcf::LF { ld, ft, ra, d, ramregion }
=>
mcf::LF { ld, ft=>rplac ft, ra, d, ramregion };
mcf::FUNARY { oper, ft, fb, rc }
=>
mcf::FUNARY { oper, ft=>rplac ft, fb, rc };
mcf::FARITH { oper, ft, fa, fb, rc }
=>
mcf::FARITH { oper, ft=>rplac ft, fa, fb, rc };
mcf::FARITH3 { oper, ft, fa, fb, fc, rc }
=>
mcf::FARITH3 { oper, ft=>rplac ft, fa, fb, fc, rc };
# CALL = BCLR { bo=ALWAYS, bf=0, bit=0, LK=TRUE, labels=[]
#
mcf::CALL { def, uses, cuts_to, ramregion }
=>
mcf::CALL { def=>cls::replace_this_by_that_in_codetemplists { this=>fs, that=>ft } def, uses, cuts_to, ramregion };
_ => instruction;
esac;
case instruction
#
mcf::NOTE { op, ... }
=>
frewrite_def (op, fs, ft);
mcf::BASE_OP i
=>
mcf::BASE_OP (def_pwrpc32 i);
mcf::DEAD { regs, spilled }
=>
mcf::DEAD { regs=>rgk::add_codetemp_info_to_appropriate_kindlist (ft, rgk::drop_codetemp_info_from_codetemplists (fs, regs)), spilled };
mcf::COPY { kind as rkj::FLOAT_REGISTER, size_in_bits, dst, src, tmp }
=>
mcf::COPY { kind, size_in_bits, dst=>map rplac dst, src, tmp=>rplac_ea tmp };
_ => error "frewriteDef";
esac;
};
end;
};
end;