ncgopt.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. {
  2. $Id$
  3. Copyright (c) 1998-2003 by Jonas Maebe
  4. This unit implements the generic 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 ncgopt;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses node, nopt;
  22. type
  23. tcgaddsstringcharoptnode = class(taddsstringcharoptnode)
  24. function det_resulttype: tnode; override;
  25. function pass_1: tnode; override;
  26. procedure pass_2; override;
  27. end;
  28. tcgaddsstringcsstringoptnode = class(taddsstringcsstringoptnode)
  29. { must be duplicated from ti386addnode :( }
  30. procedure pass_2; override;
  31. end;
  32. implementation
  33. uses
  34. globtype,
  35. pass_1,defutil,htypechk,
  36. symdef,paramgr,
  37. aasmbase,aasmtai,
  38. ncnv, ncon, pass_2,
  39. cginfo, cgbase, cpubase,
  40. tgobj, rgobj, cgobj, ncgutil;
  41. {*****************************************************************************
  42. TCGADDOPTNODE
  43. *****************************************************************************}
  44. function tcgaddsstringcharoptnode.det_resulttype: tnode;
  45. begin
  46. det_resulttype := nil;
  47. resulttypepass(left);
  48. resulttypepass(right);
  49. if codegenerror then
  50. exit;
  51. { update the curmaxlen field (before converting to a string!) }
  52. updatecurmaxlen;
  53. if not is_shortstring(left.resulttype.def) then
  54. inserttypeconv(left,cshortstringtype);
  55. resulttype:=left.resulttype;
  56. end;
  57. function tcgaddsstringcharoptnode.pass_1: tnode;
  58. begin
  59. pass_1 := nil;
  60. firstpass(left);
  61. firstpass(right);
  62. if codegenerror then
  63. exit;
  64. expectloc:=LOC_REFERENCE;
  65. if not is_constcharnode(right) then
  66. { it's not sure we need the register, but we can't know it here yet }
  67. calcregisters(self,2,0,0)
  68. else
  69. calcregisters(self,1,0,0);
  70. end;
  71. procedure tcgaddsstringcharoptnode.pass_2;
  72. var
  73. l: tasmlabel;
  74. href,href2 : treference;
  75. hreg, lengthreg: tregister;
  76. checklength: boolean;
  77. len : integer;
  78. begin
  79. { first, we have to more or less replicate some code from }
  80. { ti386addnode.pass_2 }
  81. secondpass(left);
  82. if not(tg.istemp(left.location.reference) and
  83. (tg.sizeoftemp(exprasmlist,left.location.reference) = 256)) and
  84. not(nf_use_strconcat in flags) then
  85. begin
  86. tg.Gettemp(exprasmlist,256,tt_normal,href);
  87. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  88. { location is released by copyshortstring }
  89. location_freetemp(exprasmlist,left.location);
  90. { return temp reference }
  91. location_reset(left.location,LOC_REFERENCE,def_cgsize(resulttype.def));
  92. left.location.reference:=href;
  93. end;
  94. secondpass(right);
  95. { special case for string := string + char (JM) }
  96. hreg.enum:=R_INTREGISTER;
  97. hreg.number:=NR_NO;
  98. { we have to load the char before checking the length, because we }
  99. { may need registers from the reference }
  100. { is it a constant char? }
  101. if not is_constcharnode(right) then
  102. { no, make sure it is in a register }
  103. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  104. begin
  105. { free the registers of right }
  106. reference_release(exprasmlist,right.location.reference);
  107. { get register for the char }
  108. hreg := rg.getregisterint(exprasmlist,OS_8);
  109. cg.a_load_ref_reg(exprasmlist,OS_8,right.location.reference,hreg);
  110. { I don't think a temp char exists, but it won't hurt (JM) }
  111. tg.ungetiftemp(exprasmlist,right.location.reference);
  112. end
  113. else hreg := right.location.register;
  114. { load the current string length }
  115. lengthreg := rg.getregisterint(exprasmlist,OS_INT);
  116. cg.a_load_ref_reg(exprasmlist,OS_8,left.location.reference,lengthreg);
  117. { do we have to check the length ? }
  118. if tg.istemp(left.location.reference) then
  119. checklength := curmaxlen = 255
  120. else
  121. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  122. if checklength then
  123. begin
  124. { is it already maximal? }
  125. objectlibrary.getlabel(l);
  126. if tg.istemp(left.location.reference) then
  127. len:=255
  128. else
  129. len:=tstringdef(left.resulttype.def).len;
  130. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,len,lengthreg,l)
  131. end;
  132. { no, so increase the length and add the new character }
  133. href2 := left.location.reference;
  134. { we need a new reference to store the character }
  135. { at the end of the string. Check if the base or }
  136. { index register is still free }
  137. if (href2.base.number <> NR_NO) and
  138. (href2.index.number <> NR_NO) then
  139. begin
  140. { they're not free, so add the base reg to }
  141. { the string length (since the index can }
  142. { have a scalefactor) and use lengthreg as base }
  143. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,href2.base,lengthreg);
  144. href2.base := lengthreg;
  145. end
  146. else
  147. { at least one is still free, so put EDI there }
  148. if href2.base.number = NR_NO then
  149. href2.base := lengthreg
  150. else
  151. begin
  152. href2.index := lengthreg;
  153. {$ifdef i386}
  154. href2.scalefactor := 1;
  155. {$endif i386}
  156. end;
  157. { we need to be one position after the last char }
  158. inc(href2.offset);
  159. { store the character at the end of the string }
  160. if (right.nodetype <> ordconstn) then
  161. begin
  162. { no new_reference(href2) because it's only }
  163. { used once (JM) }
  164. cg.a_load_reg_ref(exprasmlist,OS_8,hreg,href2);
  165. rg.ungetregisterint(exprasmlist,hreg);
  166. end
  167. else
  168. cg.a_load_const_ref(exprasmlist,OS_8,tordconstnode(right).value,href2);
  169. lengthreg.number:=(lengthreg.number and not $ff) or R_SUBL;
  170. { increase the string length }
  171. cg.a_op_const_reg(exprasmlist,OP_ADD,1,lengthreg);
  172. cg.a_load_reg_ref(exprasmlist,OS_8,lengthreg,left.location.reference);
  173. rg.ungetregisterint(exprasmlist,lengthreg);
  174. if checklength then
  175. cg.a_label(exprasmlist,l);
  176. location_copy(location,left.location);
  177. end;
  178. procedure tcgaddsstringcsstringoptnode.pass_2;
  179. var
  180. href: treference;
  181. pushedregs: tpushedsavedint;
  182. regstopush: tsupregset;
  183. begin
  184. { first, we have to more or less replicate some code from }
  185. { ti386addnode.pass_2 }
  186. secondpass(left);
  187. if not(tg.istemp(left.location.reference) and
  188. (tg.sizeoftemp(exprasmlist,left.location.reference) = 256)) and
  189. not(nf_use_strconcat in flags) then
  190. begin
  191. tg.GetTemp(exprasmlist,256,tt_normal,href);
  192. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  193. { release the registers }
  194. location_freetemp(exprasmlist,left.location);
  195. { return temp reference }
  196. location_reset(left.location,LOC_REFERENCE,def_cgsize(resulttype.def));
  197. left.location.reference:=href;
  198. end;
  199. secondpass(right);
  200. { on the right we do not need the register anymore too }
  201. { Instead of releasing them already, simply do not }
  202. { push them (so the release is in the right place, }
  203. { because emitpushreferenceaddr doesn't need extra }
  204. { registers) (JM) }
  205. regstopush := all_intregisters;
  206. remove_non_regvars_from_loc(right.location,regstopush);
  207. rg.saveusedintregisters(exprasmlist,pushedregs,regstopush);
  208. { push the maximum possible length of the result }
  209. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paramanager.getintparaloc(2));
  210. { the optimizer can more easily put the }
  211. { deallocations in the right place if it happens }
  212. { too early than when it happens too late (if }
  213. { the pushref needs a "lea (..),edi; push edi") }
  214. reference_release(exprasmlist,right.location.reference);
  215. cg.a_paramaddr_ref(exprasmlist,right.location.reference,paramanager.getintparaloc(1));
  216. rg.saveintregvars(exprasmlist,regstopush);
  217. cg.a_call_name(exprasmlist,'FPC_SHORTSTR_CONCAT');
  218. tg.ungetiftemp(exprasmlist,right.location.reference);
  219. rg.restoreusedintregisters(exprasmlist,pushedregs);
  220. location_copy(location,left.location);
  221. end;
  222. begin
  223. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  224. caddsstringcsstringoptnode := tcgaddsstringcsstringoptnode
  225. end.
  226. {
  227. $Log$
  228. Revision 1.2 2003-04-26 09:12:55 peter
  229. * add string returns in LOC_REFERENCE
  230. Revision 1.1 2003/04/24 11:20:06 florian
  231. + created from n386opt
  232. }