n386opt.pas 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe
  4. This unit implements the 80x86 implementation of optimized nodes
  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 n386opt;
  19. {$i defines.inc}
  20. interface
  21. uses node, nopt;
  22. type
  23. ti386addsstringcharoptnode = class(taddsstringcharoptnode)
  24. function pass_1: tnode; override;
  25. procedure pass_2; override;
  26. end;
  27. ti386addsstringcsstringoptnode = class(taddsstringcsstringoptnode)
  28. { must be duplicated from ti386addnode :( }
  29. procedure pass_2; override;
  30. end;
  31. implementation
  32. uses pass_1, types, htypechk, temp_gen, cpubase, cpuasm, cgai386, verbose,
  33. tgcpu, aasm, ncnv, ncon, pass_2, symdef;
  34. {*****************************************************************************
  35. TI386ADDOPTNODE
  36. *****************************************************************************}
  37. function ti386addsstringcharoptnode.pass_1: tnode;
  38. begin
  39. pass_1 := nil;
  40. { already done before it's created (JM)
  41. firstpass(left);
  42. firstpass(right);
  43. if codegenerror then
  44. exit; }
  45. { update the curmaxlen field (before converting to a string!) }
  46. updatecurmaxlen;
  47. if not is_shortstring(left.resulttype) then
  48. begin
  49. left := gentypeconvnode(left,cshortstringdef);
  50. firstpass(left);
  51. end;
  52. location.loc := LOC_MEM;
  53. if not is_constcharnode(right) then
  54. { it's not sure we need the register, but we can't know it here yet }
  55. calcregisters(self,2,0,0)
  56. else
  57. calcregisters(self,1,0,0);
  58. resulttype := left.resulttype;
  59. end;
  60. procedure ti386addsstringcharoptnode.pass_2;
  61. var
  62. l: pasmlabel;
  63. href2: preference;
  64. href: treference;
  65. hreg, lengthreg: tregister;
  66. checklength: boolean;
  67. begin
  68. { first, we have to more or less replicate some code from }
  69. { ti386addnode.pass_2 }
  70. secondpass(left);
  71. if not(istemp(left.location.reference) and
  72. (getsizeoftemp(left.location.reference) = 256)) and
  73. not(nf_use_strconcat in flags) then
  74. begin
  75. gettempofsizereference(256,href);
  76. copyshortstring(href,left.location.reference,255,false,true);
  77. { release the registers }
  78. ungetiftemp(left.location.reference);
  79. { does not hurt: }
  80. clear_location(left.location);
  81. left.location.loc:=LOC_MEM;
  82. left.location.reference:=href;
  83. end;
  84. secondpass(right);
  85. { special case for string := string + char (JM) }
  86. hreg := R_NO;
  87. { we have to load the char before checking the length, because we }
  88. { may need registers from the reference }
  89. { is it a constant char? }
  90. if not is_constcharnode(right) then
  91. { no, make sure it is in a register }
  92. if right.location.loc in [LOC_REFERENCE,LOC_MEM] then
  93. begin
  94. { free the registers of right }
  95. del_reference(right.location.reference);
  96. { get register for the char }
  97. hreg := reg32toreg8(getregister32);
  98. emit_ref_reg(A_MOV,S_B,
  99. newreference(right.location.reference),hreg);
  100. { I don't think a temp char exists, but it won't hurt (JM) }
  101. ungetiftemp(right.location.reference);
  102. end
  103. else hreg := right.location.register;
  104. { load the current string length }
  105. lengthreg := getregister32;
  106. emit_ref_reg(A_MOVZX,S_BL,newreference(left.location.reference),lengthreg);
  107. { do we have to check the length ? }
  108. if istemp(left.location.reference) then
  109. checklength := curmaxlen = 255
  110. else
  111. checklength := curmaxlen >= pstringdef(left.resulttype)^.len;
  112. if checklength then
  113. begin
  114. { is it already maximal? }
  115. getlabel(l);
  116. if istemp(left.location.reference) then
  117. emit_const_reg(A_CMP,S_L,255,lengthreg)
  118. else
  119. emit_const_reg(A_CMP,S_L,pstringdef(left.resulttype)^.len,lengthreg);
  120. emitjmp(C_E,l);
  121. end;
  122. { no, so increase the length and add the new character }
  123. href2 := newreference(left.location.reference);
  124. { we need a new reference to store the character }
  125. { at the end of the string. Check if the base or }
  126. { index register is still free }
  127. if (href2^.base <> R_NO) and
  128. (href2^.index <> R_NO) then
  129. begin
  130. { they're not free, so add the base reg to }
  131. { the string length (since the index can }
  132. { have a scalefactor) and use lengthreg as base }
  133. emit_reg_reg(A_ADD,S_L,href2^.base,lengthreg);
  134. href2^.base := lengthreg;
  135. end
  136. else
  137. { at least one is still free, so put EDI there }
  138. if href2^.base = R_NO then
  139. href2^.base := lengthreg
  140. else
  141. begin
  142. href2^.index := lengthreg;
  143. href2^.scalefactor := 1;
  144. end;
  145. { we need to be one position after the last char }
  146. inc(href2^.offset);
  147. { store the character at the end of the string }
  148. if (right.nodetype <> ordconstn) then
  149. begin
  150. { no new_reference(href2) because it's only }
  151. { used once (JM) }
  152. emit_reg_ref(A_MOV,S_B,hreg,href2);
  153. ungetregister(hreg);
  154. end
  155. else
  156. emit_const_ref(A_MOV,S_B,tordconstnode(right).value,href2);
  157. { increase the string length }
  158. emit_reg(A_INC,S_B,reg32toreg8(lengthreg));
  159. emit_reg_ref(A_MOV,S_B,reg32toreg8(lengthreg),
  160. newreference(left.location.reference));
  161. ungetregister32(lengthreg);
  162. if checklength then
  163. emitlab(l);
  164. set_location(location,left.location);
  165. end;
  166. procedure ti386addsstringcsstringoptnode.pass_2;
  167. var
  168. href: treference;
  169. pushedregs: tpushed;
  170. regstopush: byte;
  171. begin
  172. { first, we have to more or less replicate some code from }
  173. { ti386addnode.pass_2 }
  174. secondpass(left);
  175. if not(istemp(left.location.reference) and
  176. (getsizeoftemp(left.location.reference) = 256)) and
  177. not(nf_use_strconcat in flags) then
  178. begin
  179. gettempofsizereference(256,href);
  180. copyshortstring(href,left.location.reference,255,false,true);
  181. { release the registers }
  182. ungetiftemp(left.location.reference);
  183. { does not hurt: }
  184. clear_location(left.location);
  185. left.location.loc:=LOC_MEM;
  186. left.location.reference:=href;
  187. end;
  188. secondpass(right);
  189. { on the right we do not need the register anymore too }
  190. { Instead of releasing them already, simply do not }
  191. { push them (so the release is in the right place, }
  192. { because emitpushreferenceaddr doesn't need extra }
  193. { registers) (JM) }
  194. regstopush := $ff;
  195. remove_non_regvars_from_loc(right.location,
  196. regstopush);
  197. pushusedregisters(pushedregs,regstopush);
  198. { push the maximum possible length of the result }
  199. emitpushreferenceaddr(left.location.reference);
  200. { the optimizer can more easily put the }
  201. { deallocations in the right place if it happens }
  202. { too early than when it happens too late (if }
  203. { the pushref needs a "lea (..),edi; push edi") }
  204. del_reference(right.location.reference);
  205. emitpushreferenceaddr(right.location.reference);
  206. saveregvars(regstopush);
  207. emitcall('FPC_SHORTSTR_CONCAT');
  208. ungetiftemp(right.location.reference);
  209. maybe_loadesi;
  210. popusedregisters(pushedregs);
  211. set_location(location,left.location);
  212. end;
  213. begin
  214. caddsstringcharoptnode := ti386addsstringcharoptnode;
  215. caddsstringcsstringoptnode := ti386addsstringcsstringoptnode
  216. end.
  217. {
  218. $Log$
  219. Revision 1.2 2001-01-06 19:12:31 jonas
  220. * fixed IE 10 (but code is less efficient now :( )
  221. Revision 1.1 2001/01/04 11:24:19 jonas
  222. + initial implementation (still needs to be made more modular)
  223. }