|
@@ -38,8 +38,6 @@ Type
|
|
|
procedure DebugMsg(const s: string; p: tai);
|
|
procedure DebugMsg(const s: string; p: tai);
|
|
|
|
|
|
|
|
Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
|
|
Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
|
|
|
- function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
|
|
|
|
|
- function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
|
|
|
|
|
|
|
|
|
|
{ uses the same constructor as TAopObj }
|
|
{ uses the same constructor as TAopObj }
|
|
|
function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
|
|
function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
|
|
@@ -153,287 +151,6 @@ Implementation
|
|
|
(is_calljmp(taicpu(Next).opcode));
|
|
(is_calljmp(taicpu(Next).opcode));
|
|
|
end;
|
|
end;
|
|
|
|
|
|
|
|
-
|
|
|
|
|
- function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
|
|
|
|
|
- //var
|
|
|
|
|
- // p: taicpu;
|
|
|
|
|
- begin
|
|
|
|
|
- inherited;
|
|
|
|
|
- //if not assigned(hp) or
|
|
|
|
|
- // (hp.typ <> ait_instruction) then
|
|
|
|
|
- // begin
|
|
|
|
|
- // Result := false;
|
|
|
|
|
- // exit;
|
|
|
|
|
- // end;
|
|
|
|
|
- //p := taicpu(hp);
|
|
|
|
|
- //if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) and (reg<>NR_AF) then
|
|
|
|
|
- // begin
|
|
|
|
|
- // case p.opcode of
|
|
|
|
|
- // A_PUSH,A_POP,A_EX,A_EXX,A_NOP,A_HALT,A_DI,A_EI,A_IM,A_SET,A_RES,A_JP,A_JR,A_JRJP,A_DJNZ,A_CALL,A_RET,A_RETI,A_RETN,A_RST,A_OUT:
|
|
|
|
|
- // result:=false;
|
|
|
|
|
- // A_LD:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051112);
|
|
|
|
|
- // { LD A,I or LD A,R ? }
|
|
|
|
|
- // if (p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
|
|
|
|
|
- // (p.oper[1]^.typ=top_reg) and ((p.oper[1]^.reg=NR_I) or (p.oper[1]^.reg=NR_R)) then
|
|
|
|
|
- // result:=(reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_PARITYOVERFLOWFLAG) or
|
|
|
|
|
- // (reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ZEROFLAG) or
|
|
|
|
|
- // (reg=NR_SIGNFLAG)
|
|
|
|
|
- // else
|
|
|
|
|
- // result:=false;
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_LDI,A_LDIR,A_LDD,A_LDDR:
|
|
|
|
|
- // result:=(reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_PARITYOVERFLOWFLAG) or
|
|
|
|
|
- // (reg=NR_HALFCARRYFLAG);
|
|
|
|
|
- // A_INC,A_DEC:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>1 then
|
|
|
|
|
- // internalerror(2020051602);
|
|
|
|
|
- // if (p.oper[0]^.typ=top_reg) and ((p.oper[0]^.reg=NR_BC) or
|
|
|
|
|
- // (p.oper[0]^.reg=NR_DE) or
|
|
|
|
|
- // (p.oper[0]^.reg=NR_HL) or
|
|
|
|
|
- // (p.oper[0]^.reg=NR_SP) or
|
|
|
|
|
- // (p.oper[0]^.reg=NR_IX) or
|
|
|
|
|
- // (p.oper[0]^.reg=NR_IY)) then
|
|
|
|
|
- // result:=false
|
|
|
|
|
- // else
|
|
|
|
|
- // result:=(reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_PARITYOVERFLOWFLAG) or
|
|
|
|
|
- // (reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ZEROFLAG) or
|
|
|
|
|
- // (reg=NR_SIGNFLAG);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_CPI,A_CPIR,A_CPD,A_CPDR,A_RLD,A_RRD,A_BIT,A_INI,A_INIR,A_IND,A_INDR,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
|
|
|
|
|
- // result:=(reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_PARITYOVERFLOWFLAG) or
|
|
|
|
|
- // (reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ZEROFLAG) or
|
|
|
|
|
- // (reg=NR_SIGNFLAG);
|
|
|
|
|
- // A_ADD:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051601);
|
|
|
|
|
- // if (p.oper[0]^.typ=top_reg) and ((p.oper[0]^.reg=NR_HL) or (p.oper[0]^.reg=NR_IX) or (p.oper[0]^.reg=NR_IY)) then
|
|
|
|
|
- // result:=(reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_CARRYFLAG)
|
|
|
|
|
- // else
|
|
|
|
|
- // result:=true;
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_ADC,A_SUB,A_SBC,A_AND,A_OR,A_XOR,A_CP,A_NEG,A_RLC,A_RL,A_RRC,A_RR,A_SLA,A_SRA,A_SRL:
|
|
|
|
|
- // result:=true;
|
|
|
|
|
- // A_DAA:
|
|
|
|
|
- // result:=(reg=NR_PARITYOVERFLOWFLAG) or
|
|
|
|
|
- // (reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ZEROFLAG) or
|
|
|
|
|
- // (reg=NR_SIGNFLAG) or
|
|
|
|
|
- // (reg=NR_CARRYFLAG);
|
|
|
|
|
- // A_CPL:
|
|
|
|
|
- // result:=(reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ADDSUBTRACTFLAG);
|
|
|
|
|
- // A_CCF,A_SCF,A_RLCA,A_RLA,A_RRCA,A_RRA:
|
|
|
|
|
- // result:=(reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_CARRYFLAG);
|
|
|
|
|
- // A_IN:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051612);
|
|
|
|
|
- // if (p.oper[1]^.typ=top_ref) and ((p.oper[1]^.ref^.base=NR_C) or (p.oper[1]^.ref^.index=NR_C)) then
|
|
|
|
|
- // result:=(reg=NR_ADDSUBTRACTFLAG) or
|
|
|
|
|
- // (reg=NR_PARITYOVERFLOWFLAG) or
|
|
|
|
|
- // (reg=NR_HALFCARRYFLAG) or
|
|
|
|
|
- // (reg=NR_ZEROFLAG) or
|
|
|
|
|
- // (reg=NR_SIGNFLAG)
|
|
|
|
|
- // else
|
|
|
|
|
- // result:=false;
|
|
|
|
|
- // end;
|
|
|
|
|
- // else
|
|
|
|
|
- // internalerror(2020051111);
|
|
|
|
|
- // end;
|
|
|
|
|
- // end
|
|
|
|
|
- //else
|
|
|
|
|
- // case p.opcode of
|
|
|
|
|
- // A_LD:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051114);
|
|
|
|
|
- // result:=(p.oper[0]^.typ = top_reg) and
|
|
|
|
|
- // (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) and
|
|
|
|
|
- // ((p.oper[1]^.typ = top_const) or
|
|
|
|
|
- // ((p.oper[1]^.typ = top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
|
|
|
|
|
- // ((p.oper[1]^.typ = top_ref) and not RegInRef(reg,p.oper[1]^.ref^)));
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_PUSH,A_EX,A_EXX,A_LDI,A_LDIR,A_LDD,A_LDDR,A_CPI,A_CPIR,A_CPD,A_CPDR,
|
|
|
|
|
- // A_ADD,A_ADC,A_SBC,A_CP,A_INC,A_DEC,A_DAA,A_CPL,A_NEG,A_CCF,A_SCF,
|
|
|
|
|
- // A_NOP,A_HALT,A_DI,A_EI,A_IM,A_RLCA,A_RLA,A_RRCA,A_RRA,A_RLC,A_RL,
|
|
|
|
|
- // A_RRC,A_RR,A_SLA,A_SRA,A_SRL,A_RLD,A_RRD,A_BIT,A_SET,A_RES,A_JP,A_JR,A_JRJP,
|
|
|
|
|
- // A_DJNZ,A_CALL,A_RET,A_RETI,A_RETN,A_RST,A_INI,A_INIR,A_IND,A_INDR,
|
|
|
|
|
- // A_OUT,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
|
|
|
|
|
- // result:=false;
|
|
|
|
|
- // A_POP:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>1 then
|
|
|
|
|
- // internalerror(2020051603);
|
|
|
|
|
- // if p.oper[0]^.typ<>top_reg then
|
|
|
|
|
- // internalerror(2020051604);
|
|
|
|
|
- // result:=Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_SUB,A_XOR:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051605);
|
|
|
|
|
- // result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
|
|
|
|
|
- // (p.oper[1]^.typ=top_reg) and (p.oper[1]^.reg=NR_A) and
|
|
|
|
|
- // Reg1WriteOverwritesReg2Entirely(NR_A,reg);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_AND:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051606);
|
|
|
|
|
- // result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
|
|
|
|
|
- // (p.oper[1]^.typ=top_const) and (p.oper[1]^.val=0) and
|
|
|
|
|
- // Reg1WriteOverwritesReg2Entirely(NR_A,reg);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_OR:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051607);
|
|
|
|
|
- // result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
|
|
|
|
|
- // (p.oper[1]^.typ=top_const) and (byte(p.oper[1]^.val)=255) and
|
|
|
|
|
- // Reg1WriteOverwritesReg2Entirely(NR_A,reg);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_IN:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051608);
|
|
|
|
|
- // if p.oper[0]^.typ<>top_reg then
|
|
|
|
|
- // internalerror(2020051609);
|
|
|
|
|
- // if p.oper[1]^.typ<>top_ref then
|
|
|
|
|
- // internalerror(2020051610);
|
|
|
|
|
- // result:=Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg) and
|
|
|
|
|
- // (((p.oper[1]^.ref^.base<>NR_C) and (p.oper[1]^.ref^.index<>NR_C)) or
|
|
|
|
|
- // not(Reg1ReadDependsOnReg2(NR_BC,reg)));
|
|
|
|
|
- // end;
|
|
|
|
|
- // else
|
|
|
|
|
- // internalerror(2020051108);
|
|
|
|
|
- // end;
|
|
|
|
|
- end;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
|
|
|
|
|
- //var
|
|
|
|
|
- // p: taicpu;
|
|
|
|
|
- begin
|
|
|
|
|
- inherited;
|
|
|
|
|
- //Result := false;
|
|
|
|
|
- //if not (assigned(hp) and (hp.typ = ait_instruction)) then
|
|
|
|
|
- // exit;
|
|
|
|
|
- //p:=taicpu(hp);
|
|
|
|
|
- //
|
|
|
|
|
- //case p.opcode of
|
|
|
|
|
- // A_LD,A_BIT,A_SET,A_RES:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051102);
|
|
|
|
|
- // result:=((p.oper[0]^.typ=top_ref) and RegInRef(reg,p.oper[0]^.ref^)) or
|
|
|
|
|
- // RegInOp(reg,p.oper[1]^);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_PUSH,A_INC,A_DEC,A_RLC,A_RRC,A_SLA,A_SRA,A_SRL:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>1 then
|
|
|
|
|
- // internalerror(2020051103);
|
|
|
|
|
- // result:=RegInOp(reg,p.oper[0]^);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_POP:
|
|
|
|
|
- // result:=(reg=NR_SP);
|
|
|
|
|
- // A_EX,A_ADD,A_SUB,A_AND,A_OR,A_XOR,A_CP:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051104);
|
|
|
|
|
- // result:=RegInOp(reg,p.oper[0]^) or
|
|
|
|
|
- // RegInOp(reg,p.oper[1]^);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_EXX:
|
|
|
|
|
- // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL) or
|
|
|
|
|
- // SuperRegistersEqual(reg,NR_BC_) or SuperRegistersEqual(reg,NR_DE_) or SuperRegistersEqual(reg,NR_HL_);
|
|
|
|
|
- // A_LDI,A_LDIR,A_LDD,A_LDDR:
|
|
|
|
|
- // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL);
|
|
|
|
|
- // A_CPI,A_CPIR,A_CPD,A_CPDR:
|
|
|
|
|
- // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL) or RegistersInterfere(reg,NR_A);
|
|
|
|
|
- // A_ADC,A_SBC:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051105);
|
|
|
|
|
- // result:=RegInOp(reg,p.oper[0]^) or
|
|
|
|
|
- // RegInOp(reg,p.oper[1]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_DAA:
|
|
|
|
|
- // result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_HALFCARRYFLAG) or (reg=NR_ADDSUBTRACTFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // A_CPL,A_NEG,A_RLCA,A_RRCA:
|
|
|
|
|
- // result:=RegistersInterfere(reg,NR_A);
|
|
|
|
|
- // A_CCF:
|
|
|
|
|
- // result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // A_SCF,A_NOP,A_HALT,A_DI,A_EI,A_IM:
|
|
|
|
|
- // result:=false;
|
|
|
|
|
- // A_RLA,A_RRA:
|
|
|
|
|
- // result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // A_RL,A_RR:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>1 then
|
|
|
|
|
- // internalerror(2020051106);
|
|
|
|
|
- // result:=RegInOp(reg,p.oper[0]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_RLD,A_RRD:
|
|
|
|
|
- // result:=RegistersInterfere(reg,NR_A) or RegistersInterfere(reg,NR_HL);
|
|
|
|
|
- // A_JP,A_JR,A_JRJP:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>1 then
|
|
|
|
|
- // internalerror(2020051107);
|
|
|
|
|
- // if RegInOp(reg,p.oper[0]^) then
|
|
|
|
|
- // result:=true
|
|
|
|
|
- // else
|
|
|
|
|
- // case p.condition of
|
|
|
|
|
- // C_None:
|
|
|
|
|
- // result:=false;
|
|
|
|
|
- // C_NZ,C_Z:
|
|
|
|
|
- // result:=(reg=NR_ZEROFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // C_NC,C_C:
|
|
|
|
|
- // result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // C_PO,C_PE:
|
|
|
|
|
- // result:=(reg=NR_PARITYOVERFLOWFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // C_P,C_M:
|
|
|
|
|
- // result:=(reg=NR_SIGNFLAG) or (reg=NR_DEFAULTFLAGS);
|
|
|
|
|
- // end;
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_DJNZ:
|
|
|
|
|
- // result:=RegistersInterfere(reg,NR_B);
|
|
|
|
|
- // A_CALL,A_RET,A_RETI,A_RETN,A_RST:
|
|
|
|
|
- // result:=true;
|
|
|
|
|
- // A_IN:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051109);
|
|
|
|
|
- // result:=(p.oper[1]^.typ=top_ref) and (p.oper[1]^.ref^.base=NR_C) and RegistersInterfere(reg,NR_BC);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_OUT:
|
|
|
|
|
- // begin
|
|
|
|
|
- // if p.ops<>2 then
|
|
|
|
|
- // internalerror(2020051110);
|
|
|
|
|
- // result:=RegInOp(reg,p.oper[1]^) or (p.oper[0]^.typ=top_ref) and (p.oper[0]^.ref^.base=NR_C) and RegistersInterfere(reg,NR_BC);
|
|
|
|
|
- // end;
|
|
|
|
|
- // A_INI,A_INIR,A_IND,A_INDR,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
|
|
|
|
|
- // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL);
|
|
|
|
|
- // else
|
|
|
|
|
- // internalerror(2020051101);
|
|
|
|
|
- //end;
|
|
|
|
|
- end;
|
|
|
|
|
-
|
|
|
|
|
function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
|
|
function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
|
|
|
var
|
|
var
|
|
|
hp1,hp2,hp3,hp4,hp5: tai;
|
|
hp1,hp2,hp3,hp4,hp5: tai;
|