2
0

aoptcpu.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe, member of the Free Pascal
  3. Development Team
  4. This unit implements the MOS Technology 6502 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. Function GetNextInstructionUsingReg(Current: tai; Var Next: tai;reg : TRegister): Boolean;
  28. function RegLoadedWithNewValue(reg : tregister; hp : tai) : boolean; override;
  29. function InstructionLoadsFromReg(const reg : TRegister; const hp : tai) : boolean; override;
  30. { uses the same constructor as TAopObj }
  31. function PeepHoleOptPass1Cpu(var p: tai): boolean; override;
  32. procedure PeepHoleOptPass2;override;
  33. End;
  34. Implementation
  35. uses
  36. cutils,
  37. verbose,
  38. cpuinfo,
  39. aasmbase,aasmcpu,aasmdata,
  40. globals,globtype,
  41. cgutils;
  42. type
  43. TAsmOpSet = set of TAsmOp;
  44. function CanBeCond(p : tai) : boolean;
  45. begin
  46. result:=(p.typ=ait_instruction) and (taicpu(p).condition=C_None);
  47. end;
  48. function RefsEqual(const r1, r2: treference): boolean;
  49. begin
  50. refsequal :=
  51. (r1.offset = r2.offset) and
  52. (r1.base = r2.base) and
  53. (r1.index = r2.index) and (r1.scalefactor = r2.scalefactor) and
  54. (r1.symbol=r2.symbol) and (r1.refaddr = r2.refaddr) and
  55. (r1.relsymbol = r2.relsymbol);
  56. end;
  57. function MatchOperand(const oper1: TOper; const oper2: TOper): boolean; inline;
  58. begin
  59. result:=oper1.typ=oper2.typ;
  60. if result then
  61. case oper1.typ of
  62. top_const:
  63. Result:=oper1.val = oper2.val;
  64. top_reg:
  65. Result:=oper1.reg = oper2.reg;
  66. top_ref:
  67. Result:=RefsEqual(oper1.ref^, oper2.ref^);
  68. else Result:=false;
  69. end
  70. end;
  71. function MatchOperand(const oper: TOper; const reg: TRegister): boolean; inline;
  72. begin
  73. result := (oper.typ = top_reg) and (oper.reg = reg);
  74. end;
  75. function MatchInstruction(const instr: tai; const op: TAsmOp): boolean;
  76. begin
  77. result :=
  78. (instr.typ = ait_instruction) and
  79. (taicpu(instr).opcode = op);
  80. end;
  81. function MatchInstruction(const instr: tai; const ops: TAsmOpSet): boolean;
  82. begin
  83. result :=
  84. (instr.typ = ait_instruction) and
  85. (taicpu(instr).opcode in ops);
  86. end;
  87. function MatchInstruction(const instr: tai; const ops: TAsmOpSet;opcount : byte): boolean;
  88. begin
  89. result :=
  90. (instr.typ = ait_instruction) and
  91. (taicpu(instr).opcode in ops) and
  92. (taicpu(instr).ops=opcount);
  93. end;
  94. function MatchOpType(const instr : tai;ot0,ot1 : toptype) : Boolean;
  95. begin
  96. Result:=(taicpu(instr).ops=2) and
  97. (taicpu(instr).oper[0]^.typ=ot0) and
  98. (taicpu(instr).oper[1]^.typ=ot1);
  99. end;
  100. {$ifdef DEBUG_AOPTCPU}
  101. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);
  102. begin
  103. asml.insertbefore(tai_comment.Create(strpnew(s)), p);
  104. end;
  105. {$else DEBUG_AOPTCPU}
  106. procedure TCpuAsmOptimizer.DebugMsg(const s: string;p : tai);inline;
  107. begin
  108. end;
  109. {$endif DEBUG_AOPTCPU}
  110. function TCpuAsmOptimizer.GetNextInstructionUsingReg(Current: tai;
  111. var Next: tai; reg: TRegister): Boolean;
  112. begin
  113. Next:=Current;
  114. repeat
  115. Result:=GetNextInstruction(Next,Next);
  116. until not(cs_opt_level3 in current_settings.optimizerswitches) or not(Result) or (Next.typ<>ait_instruction) or (RegInInstruction(reg,Next)) or
  117. (is_calljmp(taicpu(Next).opcode));
  118. end;
  119. function TCpuAsmOptimizer.RegLoadedWithNewValue(reg: tregister; hp: tai): boolean;
  120. //var
  121. // p: taicpu;
  122. begin
  123. inherited;
  124. //if not assigned(hp) or
  125. // (hp.typ <> ait_instruction) then
  126. // begin
  127. // Result := false;
  128. // exit;
  129. // end;
  130. //p := taicpu(hp);
  131. //if SuperRegistersEqual(reg,NR_DEFAULTFLAGS) and (reg<>NR_AF) then
  132. // begin
  133. // case p.opcode of
  134. // 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:
  135. // result:=false;
  136. // A_LD:
  137. // begin
  138. // if p.ops<>2 then
  139. // internalerror(2020051112);
  140. // { LD A,I or LD A,R ? }
  141. // if (p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  142. // (p.oper[1]^.typ=top_reg) and ((p.oper[1]^.reg=NR_I) or (p.oper[1]^.reg=NR_R)) then
  143. // result:=(reg=NR_ADDSUBTRACTFLAG) or
  144. // (reg=NR_PARITYOVERFLOWFLAG) or
  145. // (reg=NR_HALFCARRYFLAG) or
  146. // (reg=NR_ZEROFLAG) or
  147. // (reg=NR_SIGNFLAG)
  148. // else
  149. // result:=false;
  150. // end;
  151. // A_LDI,A_LDIR,A_LDD,A_LDDR:
  152. // result:=(reg=NR_ADDSUBTRACTFLAG) or
  153. // (reg=NR_PARITYOVERFLOWFLAG) or
  154. // (reg=NR_HALFCARRYFLAG);
  155. // A_INC,A_DEC:
  156. // begin
  157. // if p.ops<>1 then
  158. // internalerror(2020051602);
  159. // if (p.oper[0]^.typ=top_reg) and ((p.oper[0]^.reg=NR_BC) or
  160. // (p.oper[0]^.reg=NR_DE) or
  161. // (p.oper[0]^.reg=NR_HL) or
  162. // (p.oper[0]^.reg=NR_SP) or
  163. // (p.oper[0]^.reg=NR_IX) or
  164. // (p.oper[0]^.reg=NR_IY)) then
  165. // result:=false
  166. // else
  167. // result:=(reg=NR_ADDSUBTRACTFLAG) or
  168. // (reg=NR_PARITYOVERFLOWFLAG) or
  169. // (reg=NR_HALFCARRYFLAG) or
  170. // (reg=NR_ZEROFLAG) or
  171. // (reg=NR_SIGNFLAG);
  172. // end;
  173. // 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:
  174. // result:=(reg=NR_ADDSUBTRACTFLAG) or
  175. // (reg=NR_PARITYOVERFLOWFLAG) or
  176. // (reg=NR_HALFCARRYFLAG) or
  177. // (reg=NR_ZEROFLAG) or
  178. // (reg=NR_SIGNFLAG);
  179. // A_ADD:
  180. // begin
  181. // if p.ops<>2 then
  182. // internalerror(2020051601);
  183. // 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
  184. // result:=(reg=NR_HALFCARRYFLAG) or
  185. // (reg=NR_ADDSUBTRACTFLAG) or
  186. // (reg=NR_CARRYFLAG)
  187. // else
  188. // result:=true;
  189. // end;
  190. // 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:
  191. // result:=true;
  192. // A_DAA:
  193. // result:=(reg=NR_PARITYOVERFLOWFLAG) or
  194. // (reg=NR_HALFCARRYFLAG) or
  195. // (reg=NR_ZEROFLAG) or
  196. // (reg=NR_SIGNFLAG) or
  197. // (reg=NR_CARRYFLAG);
  198. // A_CPL:
  199. // result:=(reg=NR_HALFCARRYFLAG) or
  200. // (reg=NR_ADDSUBTRACTFLAG);
  201. // A_CCF,A_SCF,A_RLCA,A_RLA,A_RRCA,A_RRA:
  202. // result:=(reg=NR_HALFCARRYFLAG) or
  203. // (reg=NR_ADDSUBTRACTFLAG) or
  204. // (reg=NR_CARRYFLAG);
  205. // A_IN:
  206. // begin
  207. // if p.ops<>2 then
  208. // internalerror(2020051612);
  209. // if (p.oper[1]^.typ=top_ref) and ((p.oper[1]^.ref^.base=NR_C) or (p.oper[1]^.ref^.index=NR_C)) then
  210. // result:=(reg=NR_ADDSUBTRACTFLAG) or
  211. // (reg=NR_PARITYOVERFLOWFLAG) or
  212. // (reg=NR_HALFCARRYFLAG) or
  213. // (reg=NR_ZEROFLAG) or
  214. // (reg=NR_SIGNFLAG)
  215. // else
  216. // result:=false;
  217. // end;
  218. // else
  219. // internalerror(2020051111);
  220. // end;
  221. // end
  222. //else
  223. // case p.opcode of
  224. // A_LD:
  225. // begin
  226. // if p.ops<>2 then
  227. // internalerror(2020051114);
  228. // result:=(p.oper[0]^.typ = top_reg) and
  229. // (Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg)) and
  230. // ((p.oper[1]^.typ = top_const) or
  231. // ((p.oper[1]^.typ = top_reg) and not(Reg1ReadDependsOnReg2(p.oper[1]^.reg,reg))) or
  232. // ((p.oper[1]^.typ = top_ref) and not RegInRef(reg,p.oper[1]^.ref^)));
  233. // end;
  234. // A_PUSH,A_EX,A_EXX,A_LDI,A_LDIR,A_LDD,A_LDDR,A_CPI,A_CPIR,A_CPD,A_CPDR,
  235. // A_ADD,A_ADC,A_SBC,A_CP,A_INC,A_DEC,A_DAA,A_CPL,A_NEG,A_CCF,A_SCF,
  236. // A_NOP,A_HALT,A_DI,A_EI,A_IM,A_RLCA,A_RLA,A_RRCA,A_RRA,A_RLC,A_RL,
  237. // 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,
  238. // A_DJNZ,A_CALL,A_RET,A_RETI,A_RETN,A_RST,A_INI,A_INIR,A_IND,A_INDR,
  239. // A_OUT,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
  240. // result:=false;
  241. // A_POP:
  242. // begin
  243. // if p.ops<>1 then
  244. // internalerror(2020051603);
  245. // if p.oper[0]^.typ<>top_reg then
  246. // internalerror(2020051604);
  247. // result:=Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg);
  248. // end;
  249. // A_SUB,A_XOR:
  250. // begin
  251. // if p.ops<>2 then
  252. // internalerror(2020051605);
  253. // result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  254. // (p.oper[1]^.typ=top_reg) and (p.oper[1]^.reg=NR_A) and
  255. // Reg1WriteOverwritesReg2Entirely(NR_A,reg);
  256. // end;
  257. // A_AND:
  258. // begin
  259. // if p.ops<>2 then
  260. // internalerror(2020051606);
  261. // result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  262. // (p.oper[1]^.typ=top_const) and (p.oper[1]^.val=0) and
  263. // Reg1WriteOverwritesReg2Entirely(NR_A,reg);
  264. // end;
  265. // A_OR:
  266. // begin
  267. // if p.ops<>2 then
  268. // internalerror(2020051607);
  269. // result:=(p.oper[0]^.typ=top_reg) and (p.oper[0]^.reg=NR_A) and
  270. // (p.oper[1]^.typ=top_const) and (byte(p.oper[1]^.val)=255) and
  271. // Reg1WriteOverwritesReg2Entirely(NR_A,reg);
  272. // end;
  273. // A_IN:
  274. // begin
  275. // if p.ops<>2 then
  276. // internalerror(2020051608);
  277. // if p.oper[0]^.typ<>top_reg then
  278. // internalerror(2020051609);
  279. // if p.oper[1]^.typ<>top_ref then
  280. // internalerror(2020051610);
  281. // result:=Reg1WriteOverwritesReg2Entirely(p.oper[0]^.reg,reg) and
  282. // (((p.oper[1]^.ref^.base<>NR_C) and (p.oper[1]^.ref^.index<>NR_C)) or
  283. // not(Reg1ReadDependsOnReg2(NR_BC,reg)));
  284. // end;
  285. // else
  286. // internalerror(2020051108);
  287. // end;
  288. end;
  289. function TCpuAsmOptimizer.InstructionLoadsFromReg(const reg: TRegister; const hp: tai): boolean;
  290. //var
  291. // p: taicpu;
  292. begin
  293. inherited;
  294. //Result := false;
  295. //if not (assigned(hp) and (hp.typ = ait_instruction)) then
  296. // exit;
  297. //p:=taicpu(hp);
  298. //
  299. //case p.opcode of
  300. // A_LD,A_BIT,A_SET,A_RES:
  301. // begin
  302. // if p.ops<>2 then
  303. // internalerror(2020051102);
  304. // result:=((p.oper[0]^.typ=top_ref) and RegInRef(reg,p.oper[0]^.ref^)) or
  305. // RegInOp(reg,p.oper[1]^);
  306. // end;
  307. // A_PUSH,A_INC,A_DEC,A_RLC,A_RRC,A_SLA,A_SRA,A_SRL:
  308. // begin
  309. // if p.ops<>1 then
  310. // internalerror(2020051103);
  311. // result:=RegInOp(reg,p.oper[0]^);
  312. // end;
  313. // A_POP:
  314. // result:=(reg=NR_SP);
  315. // A_EX,A_ADD,A_SUB,A_AND,A_OR,A_XOR,A_CP:
  316. // begin
  317. // if p.ops<>2 then
  318. // internalerror(2020051104);
  319. // result:=RegInOp(reg,p.oper[0]^) or
  320. // RegInOp(reg,p.oper[1]^);
  321. // end;
  322. // A_EXX:
  323. // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL) or
  324. // SuperRegistersEqual(reg,NR_BC_) or SuperRegistersEqual(reg,NR_DE_) or SuperRegistersEqual(reg,NR_HL_);
  325. // A_LDI,A_LDIR,A_LDD,A_LDDR:
  326. // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_DE) or SuperRegistersEqual(reg,NR_HL);
  327. // A_CPI,A_CPIR,A_CPD,A_CPDR:
  328. // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL) or RegistersInterfere(reg,NR_A);
  329. // A_ADC,A_SBC:
  330. // begin
  331. // if p.ops<>2 then
  332. // internalerror(2020051105);
  333. // result:=RegInOp(reg,p.oper[0]^) or
  334. // RegInOp(reg,p.oper[1]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  335. // end;
  336. // A_DAA:
  337. // result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_HALFCARRYFLAG) or (reg=NR_ADDSUBTRACTFLAG) or (reg=NR_DEFAULTFLAGS);
  338. // A_CPL,A_NEG,A_RLCA,A_RRCA:
  339. // result:=RegistersInterfere(reg,NR_A);
  340. // A_CCF:
  341. // result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  342. // A_SCF,A_NOP,A_HALT,A_DI,A_EI,A_IM:
  343. // result:=false;
  344. // A_RLA,A_RRA:
  345. // result:=RegistersInterfere(reg,NR_A) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  346. // A_RL,A_RR:
  347. // begin
  348. // if p.ops<>1 then
  349. // internalerror(2020051106);
  350. // result:=RegInOp(reg,p.oper[0]^) or (reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  351. // end;
  352. // A_RLD,A_RRD:
  353. // result:=RegistersInterfere(reg,NR_A) or RegistersInterfere(reg,NR_HL);
  354. // A_JP,A_JR,A_JRJP:
  355. // begin
  356. // if p.ops<>1 then
  357. // internalerror(2020051107);
  358. // if RegInOp(reg,p.oper[0]^) then
  359. // result:=true
  360. // else
  361. // case p.condition of
  362. // C_None:
  363. // result:=false;
  364. // C_NZ,C_Z:
  365. // result:=(reg=NR_ZEROFLAG) or (reg=NR_DEFAULTFLAGS);
  366. // C_NC,C_C:
  367. // result:=(reg=NR_CARRYFLAG) or (reg=NR_DEFAULTFLAGS);
  368. // C_PO,C_PE:
  369. // result:=(reg=NR_PARITYOVERFLOWFLAG) or (reg=NR_DEFAULTFLAGS);
  370. // C_P,C_M:
  371. // result:=(reg=NR_SIGNFLAG) or (reg=NR_DEFAULTFLAGS);
  372. // end;
  373. // end;
  374. // A_DJNZ:
  375. // result:=RegistersInterfere(reg,NR_B);
  376. // A_CALL,A_RET,A_RETI,A_RETN,A_RST:
  377. // result:=true;
  378. // A_IN:
  379. // begin
  380. // if p.ops<>2 then
  381. // internalerror(2020051109);
  382. // result:=(p.oper[1]^.typ=top_ref) and (p.oper[1]^.ref^.base=NR_C) and RegistersInterfere(reg,NR_BC);
  383. // end;
  384. // A_OUT:
  385. // begin
  386. // if p.ops<>2 then
  387. // internalerror(2020051110);
  388. // 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);
  389. // end;
  390. // A_INI,A_INIR,A_IND,A_INDR,A_OUTI,A_OTIR,A_OUTD,A_OTDR:
  391. // result:=SuperRegistersEqual(reg,NR_BC) or SuperRegistersEqual(reg,NR_HL);
  392. // else
  393. // internalerror(2020051101);
  394. //end;
  395. end;
  396. function TCpuAsmOptimizer.PeepHoleOptPass1Cpu(var p: tai): boolean;
  397. var
  398. hp1,hp2,hp3,hp4,hp5: tai;
  399. alloc, dealloc: tai_regalloc;
  400. i: integer;
  401. l: TAsmLabel;
  402. //TmpUsedRegs : TAllUsedRegs;
  403. begin
  404. result := false;
  405. //case p.typ of
  406. // ait_instruction:
  407. // begin
  408. // end;
  409. //end;
  410. end;
  411. procedure TCpuAsmOptimizer.PeepHoleOptPass2;
  412. begin
  413. end;
  414. begin
  415. casmoptimizer:=TCpuAsmOptimizer;
  416. End.