aoptcpu.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the Z80 optimizer object
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit aoptcpu;
  19. {$i fpcdefs.inc}
  20. {$define DEBUG_AOPTCPU}
  21. Interface
  22. uses cpubase, cgbase, aasmtai, aopt,AoptObj, aoptcpub;
  23. Type
  24. TCpuAsmOptimizer = class(TAsmOptimizer)
  25. { outputs a debug message into the assembler file }
  26. procedure DebugMsg(const s: string; p: tai);
  27. { checks whether loading a new value in reg1 overwrites the entirety of reg2 }
  28. function Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  29. { checks whether reading the value in reg1 depends on the value of reg2. This
  30. is very similar to SuperRegisterEquals, except it takes into account that
  31. R_SUBH and R_SUBL are independendent (e.g. reading from AL does not
  32. depend on the value in AH). }
  33. function Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  34. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  35. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  36. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  37. { uses the same constructor as TAopObj }
  38. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  39. procedure PeepHoleOptPass2;override;
  40. End;
  41. Implementation
  42. uses
  43. cutils,
  44. verbose,
  45. cpuinfo,
  46. aasmbase,aasmcpu,aasmdata,
  47. globals,globtype,
  48. cgutils;
  49. type
  50. TAsmOpSet = set of TAsmOp;
  51. function CanBeCond(p : tai) : boolean;
  52. begin
  53. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  54. end;
  55. function RefsEqual(const r1, r2: treference): boolean;
  56. begin
  57. refsequal :=
  58. (r1.offset = r2.offset) and
  59. (r1.base = r2.base) and
  60. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  61. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  62. (r1.relsymbol = r2.relsymbol);
  63. end;
  64. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  65. begin
  66. result:=oper1.typ=oper2.typ;
  67. if result then
  68. case oper1.typ of
  69. top_const:
  70. Result:=oper1.val = oper2.val;
  71. top_reg:
  72. Result:=oper1.reg = oper2.reg;
  73. top_ref:
  74. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  75. else Result:=false;
  76. end
  77. end;
  78. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  79. begin
  80. result := (oper.typ = top_reg) and (oper.reg = reg);
  81. end;
  82. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  83. begin
  84. result :=
  85. (instr.typ = ait_instruction) and
  86. (taicpu(instr).opcode = op);
  87. end;
  88. function MatchInstruction(const instr: tai; const ops: TAsmOpSet): boolean;
  89. begin
  90. result :=
  91. (instr.typ = ait_instruction) and
  92. (taicpu(instr).opcode in ops);
  93. end;
  94. function MatchInstruction(const instr: tai; const ops: TAsmOpSet;opcount : byte): boolean;
  95. begin
  96. result :=
  97. (instr.typ = ait_instruction) and
  98. (taicpu(instr).opcode in ops) and
  99. (taicpu(instr).ops=opcount);
  100. end;
  101. function MatchOpType(const instr : tai;ot0,ot1 : toptype) : Boolean;
  102. begin
  103. Result:=(taicpu(instr).ops=2) and
  104. (taicpu(instr).oper[0]^.typ=ot0) and
  105. (taicpu(instr).oper[1]^.typ=ot1);
  106. end;
  107. {$ifdef DEBUG_AOPTCPU}
  108. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  109. begin
  110. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  111. end;
  112. {$else DEBUG_AOPTCPU}
  113. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  114. begin
  115. end;
  116. {$endif DEBUG_AOPTCPU}
  117. function TCpuAsmOptimizer.Reg1WriteOverwritesReg2Entirely(reg1, reg2: tregister): boolean;
  118. begin
  119. case reg1 of
  120. NR_F:
  121. result:=SuperRegistersEqual(reg2,NR_DEFAULTFLAGS);
  122. NR_AF:
  123. result:=(reg2=NR_A) or (reg2=NR_AF) or SuperRegistersEqual(reg2,NR_DEFAULTFLAGS);
  124. NR_BC:
  125. result:=(reg2=NR_B) or (reg2=NR_C) or (reg2=NR_BC);
  126. NR_DE:
  127. result:=(reg2=NR_D) or (reg2=NR_E) or (reg2=NR_DE);
  128. NR_HL:
  129. result:=(reg2=NR_H) or (reg2=NR_L) or (reg2=NR_HL);
  130. NR_F_:
  131. result:=SuperRegistersEqual(reg2,NR_F_);
  132. NR_AF_:
  133. result:=(reg2=NR_A_) or (reg2=NR_AF_) or SuperRegistersEqual(reg2,NR_F_);
  134. NR_BC_:
  135. result:=(reg2=NR_B_) or (reg2=NR_C_) or (reg2=NR_BC_);
  136. NR_DE_:
  137. result:=(reg2=NR_D_) or (reg2=NR_E_) or (reg2=NR_DE_);
  138. NR_HL_:
  139. result:=(reg2=NR_H_) or (reg2=NR_L_) or (reg2=NR_HL_);
  140. else
  141. result:=reg1=reg2;
  142. end;
  143. end;
  144. function TCpuAsmOptimizer.Reg1ReadDependsOnReg2(reg1, reg2: tregister): boolean;
  145. begin
  146. case reg1 of
  147. NR_AF:
  148. result:=(reg2=NR_A) or (reg2=NR_AF) or SuperRegistersEqual(reg2,NR_DEFAULTFLAGS);
  149. NR_A:
  150. result:=(reg2=NR_A) or (reg2=NR_AF);
  151. NR_F:
  152. result:=SuperRegistersEqual(reg2,NR_DEFAULTFLAGS);
  153. NR_BC:
  154. result:=(reg2=NR_B) or (reg2=NR_C) or (reg2=NR_BC);
  155. NR_B:
  156. result:=(reg2=NR_B) or (reg2=NR_BC);
  157. NR_C:
  158. result:=(reg2=NR_C) or (reg2=NR_BC);
  159. NR_DE:
  160. result:=(reg2=NR_D) or (reg2=NR_E) or (reg2=NR_DE);
  161. NR_D:
  162. result:=(reg2=NR_D) or (reg2=NR_DE);
  163. NR_E:
  164. result:=(reg2=NR_E) or (reg2=NR_DE);
  165. NR_HL:
  166. result:=(reg2=NR_H) or (reg2=NR_L) or (reg2=NR_HL);
  167. NR_H:
  168. result:=(reg2=NR_H) or (reg2=NR_HL);
  169. NR_L:
  170. result:=(reg2=NR_L) or (reg2=NR_HL);
  171. NR_AF_:
  172. result:=(reg2=NR_A_) or (reg2=NR_AF_) or SuperRegistersEqual(reg2,NR_F_);
  173. NR_A_:
  174. result:=(reg2=NR_A_) or (reg2=NR_AF_);
  175. NR_F_:
  176. result:=SuperRegistersEqual(reg2,NR_F_);
  177. NR_BC_:
  178. result:=(reg2=NR_B_) or (reg2=NR_C_) or (reg2=NR_BC_);
  179. NR_B_:
  180. result:=(reg2=NR_B_) or (reg2=NR_BC_);
  181. NR_C_:
  182. result:=(reg2=NR_C_) or (reg2=NR_BC_);
  183. NR_DE_:
  184. result:=(reg2=NR_D_) or (reg2=NR_E_) or (reg2=NR_DE_);
  185. NR_D_:
  186. result:=(reg2=NR_D_) or (reg2=NR_DE_);
  187. NR_E_:
  188. result:=(reg2=NR_E_) or (reg2=NR_DE_);
  189. NR_HL_:
  190. result:=(reg2=NR_H_) or (reg2=NR_L_) or (reg2=NR_HL_);
  191. NR_H_:
  192. result:=(reg2=NR_H_) or (reg2=NR_HL_);
  193. NR_L_:
  194. result:=(reg2=NR_L_) or (reg2=NR_HL_);
  195. else
  196. result:=reg1=reg2;
  197. end;
  198. end;
  199. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  200. var Next: tai; reg: TRegister): Boolean;
  201. begin
  202. Next:=Current;
  203. repeat
  204. Result:=GetNextInstruction(Next,Next);
  205. until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  206. (is_calljmp(taicpu(Next).opcode));
  207. end;
  208. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  209. var
  210. p: taicpu;
  211. begin
  212. if not assigned(hp) or
  213. (hp.typ <> ait_instruction) then
  214. begin
  215. Result := false;
  216. exit;
  217. end;
  218. p := taicpu(hp);
  219. if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) and (reg<>NR_AF) then
  220. begin
  221. case p.opcode of
  222. 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_DJNZ,A_CALL,A_RET,A_RETI,A_RETN,A_RST,A_OUT:
  223. result:=false;
  224. A_LD:
  225. begin
  226. if p.ops<>2 then
  227. internalerror(2020051112);
  228. { LD A,I or LD A,R ? }
  229. if (p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  230. (p.oper[1]^.typ=top_reg) and ((p.oper[1]^.reg=NR_I) or (p.oper[1]^.reg=NR_R)) then
  231. result:=(reg=NR_ADDSUBTRACTFLAG) or
  232. (reg=NR_PARITYOVERFLOWFLAG) or
  233. (reg=NR_HALFCARRYFLAG) or
  234. (reg=NR_ZEROFLAG) or
  235. (reg=NR_SIGNFLAG)
  236. else
  237. result:=false;
  238. end;
  239. A_LDI,A_LDIR,A_LDD,A_LDDR:
  240. result:=(reg=NR_ADDSUBTRACTFLAG) or
  241. (reg=NR_PARITYOVERFLOWFLAG) or
  242. (reg=NR_HALFCARRYFLAG);
  243. A_INC,A_DEC:
  244. begin
  245. if p.ops<>1 then
  246. internalerror(2020051602);
  247. if (p.oper[0]^.typ=top_reg) and ((p.oper[0]^.reg=NR_BC) or
  248. (p.oper[0]^.reg=NR_DE) or
  249. (p.oper[0]^.reg=NR_HL) or
  250. (p.oper[0]^.reg=NR_SP) or
  251. (p.oper[0]^.reg=NR_IX) or
  252. (p.oper[0]^.reg=NR_IY)) then
  253. result:=false
  254. else
  255. result:=(reg=NR_ADDSUBTRACTFLAG) or
  256. (reg=NR_PARITYOVERFLOWFLAG) or
  257. (reg=NR_HALFCARRYFLAG) or
  258. (reg=NR_ZEROFLAG) or
  259. (reg=NR_SIGNFLAG);
  260. end;
  261. 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:
  262. result:=(reg=NR_ADDSUBTRACTFLAG) or
  263. (reg=NR_PARITYOVERFLOWFLAG) or
  264. (reg=NR_HALFCARRYFLAG) or
  265. (reg=NR_ZEROFLAG) or
  266. (reg=NR_SIGNFLAG);
  267. A_ADD:
  268. begin
  269. if p.ops<>2 then
  270. internalerror(2020051601);
  271. 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
  272. result:=(reg=NR_HALFCARRYFLAG) or
  273. (reg=NR_ADDSUBTRACTFLAG) or
  274. (reg=NR_CARRYFLAG)
  275. else
  276. result:=true;
  277. end;
  278. 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:
  279. result:=true;
  280. A_DAA:
  281. result:=(reg=NR_PARITYOVERFLOWFLAG) or
  282. (reg=NR_HALFCARRYFLAG) or
  283. (reg=NR_ZEROFLAG) or
  284. (reg=NR_SIGNFLAG) or
  285. (reg=NR_CARRYFLAG);
  286. A_CPL:
  287. result:=(reg=NR_HALFCARRYFLAG) or
  288. (reg=NR_ADDSUBTRACTFLAG);
  289. A_CCF,A_SCF,A_RLCA,A_RLA,A_RRCA,A_RRA:
  290. result:=(reg=NR_HALFCARRYFLAG) or
  291. (reg=NR_ADDSUBTRACTFLAG) or
  292. (reg=NR_CARRYFLAG);
  293. A_IN:
  294. begin
  295. if p.ops<>2 then
  296. internalerror(2020051602);
  297. if (p.oper[1]^.typ=top_ref) and ((p.oper[1]^.ref^.base=NR_C) or (p.oper[1]^.ref^.index=NR_C)) then
  298. result:=(reg=NR_ADDSUBTRACTFLAG) or
  299. (reg=NR_PARITYOVERFLOWFLAG) or
  300. (reg=NR_HALFCARRYFLAG) or
  301. (reg=NR_ZEROFLAG) or
  302. (reg=NR_SIGNFLAG)
  303. else
  304. result:=false;
  305. end;
  306. else
  307. internalerror(2020051111);
  308. end;
  309. end
  310. else
  311. case p.opcode of
  312. A_LD:
  313. begin
  314. if p.ops<>2 then
  315. internalerror(2020051112);
  316. result:=(p.oper[0]^.typ = top_reg) and
  317. (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) and
  318. ((p.oper[1]^.typ = top_const) or
  319. ((p.oper[1]^.typ = top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  320. ((p.oper[1]^.typ = top_ref) and not RegInRef(reg,p.oper[1]^.ref^)));
  321. end;
  322. A_PUSH,A_EX,A_EXX,A_LDI,A_LDIR,A_LDD,A_LDDR,A_CPI,A_CPIR,A_CPD,A_CPDR,
  323. A_ADD,A_ADC,A_SBC,A_CP,A_INC,A_DEC,A_DAA,A_CPL,A_NEG,A_CCF,A_SCF,
  324. A_NOP,A_HALT,A_DI,A_EI,A_IM,A_RLCA,A_RLA,A_RRCA,A_RRA,A_RLC,A_RL,
  325. A_RRC,A_RR,A_SLA,A_SRA,A_SRL,A_RLD,A_RRD,A_BIT,A_SET,A_RES,A_JP,A_JR,
  326. A_DJNZ,A_CALL,A_RET,A_RETI,A_RETN,A_RST,A_INI,A_INIR,A_IND,A_INDR,
  327. A_OUT,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
  328. result:=false;
  329. A_POP:
  330. begin
  331. if p.ops<>1 then
  332. internalerror(2020051603);
  333. if p.oper[0]^.typ<>top_reg then
  334. internalerror(2020051604);
  335. result:=Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg);
  336. end;
  337. A_SUB,A_XOR:
  338. begin
  339. if p.ops<>2 then
  340. internalerror(2020051605);
  341. result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  342. (p.oper[1]^.typ=top_reg) and (p.oper[1]^.reg=NR_A) and
  343. Reg1WriteOverwritesReg2Entirely(NR_A,reg);
  344. end;
  345. A_AND:
  346. begin
  347. if p.ops<>2 then
  348. internalerror(2020051606);
  349. result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  350. (p.oper[1]^.typ=top_const) and (p.oper[1]^.val=0) and
  351. Reg1WriteOverwritesReg2Entirely(NR_A,reg);
  352. end;
  353. A_OR:
  354. begin
  355. if p.ops<>2 then
  356. internalerror(2020051607);
  357. result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  358. (p.oper[1]^.typ=top_const) and (byte(p.oper[1]^.val)=255) and
  359. Reg1WriteOverwritesReg2Entirely(NR_A,reg);
  360. end;
  361. A_IN:
  362. begin
  363. if p.ops<>2 then
  364. internalerror(2020051608);
  365. if p.oper[0]^.typ<>top_reg then
  366. internalerror(2020051609);
  367. if p.oper[1]^.typ<>top_ref then
  368. internalerror(2020051610);
  369. result:=Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg) and
  370. (((p.oper[1]^.ref^.base<>NR_C) and (p.oper[1]^.ref^.index<>NR_C)) or
  371. not(Reg1ReadDependsOnReg2(NR_BC,reg)));
  372. end;
  373. else
  374. internalerror(2020051111);
  375. end;
  376. end;
  377. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  378. var
  379. p: taicpu;
  380. begin
  381. Result := false;
  382. if not (assigned(hp) and (hp.typ = ait_instruction)) then
  383. exit;
  384. p:=taicpu(hp);
  385. case p.opcode of
  386. A_LD,A_BIT,A_SET,A_RES:
  387. begin
  388. if p.ops<>2 then
  389. internalerror(2020051102);
  390. result:=((p.oper[0]^.typ=top_ref) and RegInRef(reg,p.oper[0]^.ref^)) or
  391. RegInOp(reg,p.oper[1]^);
  392. end;
  393. A_PUSH,A_INC,A_DEC,A_RLC,A_RRC,A_SLA,A_SRA,A_SRL:
  394. begin
  395. if p.ops<>1 then
  396. internalerror(2020051103);
  397. result:=RegInOp(reg,p.oper[0]^);
  398. end;
  399. A_POP:
  400. result:=(reg=NR_SP);
  401. A_EX,A_ADD,A_SUB,A_AND,A_OR,A_XOR,A_CP:
  402. begin
  403. if p.ops<>2 then
  404. internalerror(2020051104);
  405. result:=RegInOp(reg,p.oper[0]^) or
  406. RegInOp(reg,p.oper[1]^);
  407. end;
  408. A_EXX:
  409. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL) or
  410. SuperRegistersEqual(reg,NR_BC_) or SuperRegistersEqual(reg,NR_DE_) or SuperRegistersEqual(reg,NR_HL_);
  411. A_LDI,A_LDIR,A_LDD,A_LDDR:
  412. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL);
  413. A_CPI,A_CPIR,A_CPD,A_CPDR:
  414. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL) or RegistersInterfere(reg,NR_A);
  415. A_ADC,A_SBC:
  416. begin
  417. if p.ops<>2 then
  418. internalerror(2020051105);
  419. result:=RegInOp(reg,p.oper[0]^) or
  420. RegInOp(reg,p.oper[1]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  421. end;
  422. A_DAA:
  423. result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_HALFCARRYFLAG) or (reg=NR_ADDSUBTRACTFLAG) or (reg=NR_DEFAULTFLAGS);
  424. A_CPL,A_NEG,A_RLCA,A_RRCA:
  425. result:=RegistersInterfere(reg,NR_A);
  426. A_CCF:
  427. result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  428. A_SCF,A_NOP,A_HALT,A_DI,A_EI,A_IM:
  429. result:=false;
  430. A_RLA,A_RRA:
  431. result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  432. A_RL,A_RR:
  433. begin
  434. if p.ops<>1 then
  435. internalerror(2020051106);
  436. result:=RegInOp(reg,p.oper[0]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  437. end;
  438. A_RLD,A_RRD:
  439. result:=RegistersInterfere(reg,NR_A) or RegistersInterfere(reg,NR_HL);
  440. A_JP,A_JR:
  441. begin
  442. if p.ops<>1 then
  443. internalerror(2020051107);
  444. if RegInOp(reg,p.oper[0]^) then
  445. result:=true
  446. else
  447. case p.condition of
  448. C_None:
  449. result:=false;
  450. C_NZ,C_Z:
  451. result:=(reg=NR_ZEROFLAG) or (reg=NR_DEFAULTFLAGS);
  452. C_NC,C_C:
  453. result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  454. C_PO,C_PE:
  455. result:=(reg=NR_PARITYOVERFLOWFLAG) or (reg=NR_DEFAULTFLAGS);
  456. C_P,C_M:
  457. result:=(reg=NR_SIGNFLAG) or (reg=NR_DEFAULTFLAGS);
  458. end;
  459. end;
  460. A_DJNZ:
  461. result:=RegistersInterfere(reg,NR_B);
  462. A_CALL,A_RET,A_RETI,A_RETN,A_RST:
  463. result:=true;
  464. A_IN:
  465. begin
  466. if p.ops<>2 then
  467. internalerror(2020051109);
  468. result:=(p.oper[1]^.typ=top_ref) and (p.oper[1]^.ref^.base=NR_C) and RegistersInterfere(reg,NR_BC);
  469. end;
  470. A_OUT:
  471. begin
  472. if p.ops<>2 then
  473. internalerror(2020051110);
  474. 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);
  475. end;
  476. A_INI,A_INIR,A_IND,A_INDR,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
  477. result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL);
  478. else
  479. internalerror(2020051101);
  480. end;
  481. end;
  482. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  483. var
  484. hp1,hp2,hp3,hp4,hp5: tai;
  485. alloc, dealloc: tai_regalloc;
  486. i: integer;
  487. l: TAsmLabel;
  488. //TmpUsedRegs : TAllUsedRegs;
  489. begin
  490. result := false;
  491. //case p.typ of
  492. // ait_instruction:
  493. // begin
  494. // end;
  495. //end;
  496. end;
  497. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  498. begin
  499. end;
  500. begin
  501. casmoptimizer:=TCpuAsmOptimizer;
  502. End.