n386opt.pas 8.5 KB

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