nopt.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. {
  2. Copyright (c) 1998-2002 by Jonas Maebe
  3. This unit implements optimized nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit nopt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses node,nbas,nadd;
  21. type
  22. tsubnodetype = (
  23. addsstringcharoptn, { shorstring + char }
  24. addsstringcsstringoptn { shortstring + constant shortstring }
  25. );
  26. taddoptnode = class(taddnode)
  27. subnodetype: tsubnodetype;
  28. constructor create(ts: tsubnodetype; l,r : tnode); virtual; reintroduce;
  29. { pass_1 will be overridden by the separate subclasses }
  30. { By default, pass_generate_code is the same as for addnode }
  31. { Only if there's a processor specific implementation, it }
  32. { will be overridden. }
  33. function dogetcopy: tnode; override;
  34. function docompare(p: tnode): boolean; override;
  35. end;
  36. taddsstringoptnode = class(taddoptnode)
  37. { maximum length of the string until now, allows us to skip a compare }
  38. { sometimes (it's initialized/updated by calling updatecurmaxlen) }
  39. curmaxlen: byte;
  40. { pass_1 must be overridden, otherwise we get an endless loop }
  41. function pass_typecheck: tnode; override;
  42. function pass_1: tnode; override;
  43. function dogetcopy: tnode; override;
  44. function docompare(p: tnode): boolean; override;
  45. protected
  46. procedure updatecurmaxlen;
  47. end;
  48. { add a char to a shortstring }
  49. taddsstringcharoptnode = class(taddsstringoptnode)
  50. constructor create(l,r : tnode); virtual; reintroduce;
  51. end;
  52. taddsstringcharoptnodeclass = class of taddsstringcharoptnode;
  53. { add a constant string to a short string }
  54. taddsstringcsstringoptnode = class(taddsstringoptnode)
  55. constructor create(l,r : tnode); virtual; reintroduce;
  56. function pass_1: tnode; override;
  57. end;
  58. taddsstringcsstringoptnodeclass = class of taddsstringcsstringoptnode;
  59. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  60. function genaddsstringcharoptnode(p: taddnode): tnode;
  61. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  62. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  63. function canbemultistringadd(p: taddnode): boolean;
  64. function genmultistringadd(p: taddnode): tnode;
  65. function is_addsstringoptnode(p: tnode): boolean;
  66. var
  67. caddsstringcharoptnode: taddsstringcharoptnodeclass;
  68. caddsstringcsstringoptnode: taddsstringcsstringoptnodeclass;
  69. implementation
  70. uses cutils, htypechk, defutil, defcmp, globtype, globals, cpubase, ncnv, ncon,ncal,nld,nmem,
  71. verbose, symconst,symdef, cgbase, procinfo;
  72. {*****************************************************************************
  73. TADDOPTNODE
  74. *****************************************************************************}
  75. constructor taddoptnode.create(ts: tsubnodetype; l,r : tnode);
  76. begin
  77. { we need to keep the addn nodetype, otherwise taddnode.pass_generate_code will be }
  78. { confused. Comparison for equal nodetypes therefore has to be }
  79. { implemented using the classtype() method (JM) }
  80. inherited create(addn,l,r);
  81. subnodetype := ts;
  82. end;
  83. function taddoptnode.dogetcopy: tnode;
  84. var
  85. hp: taddoptnode;
  86. begin
  87. hp := taddoptnode(inherited dogetcopy);
  88. hp.subnodetype := subnodetype;
  89. dogetcopy := hp;
  90. end;
  91. function taddoptnode.docompare(p: tnode): boolean;
  92. begin
  93. docompare :=
  94. inherited docompare(p) and
  95. (subnodetype = taddoptnode(p).subnodetype);
  96. end;
  97. {*****************************************************************************
  98. TADDSSTRINGOPTNODE
  99. *****************************************************************************}
  100. function taddsstringoptnode.pass_typecheck: tnode;
  101. begin
  102. result := nil;
  103. updatecurmaxlen;
  104. { left and right are already firstpass'ed by taddnode.pass_1 }
  105. if not is_shortstring(left.resultdef) then
  106. inserttypeconv(left,cshortstringtype);
  107. if not is_shortstring(right.resultdef) then
  108. inserttypeconv(right,cshortstringtype);
  109. resultdef := left.resultdef;
  110. end;
  111. function taddsstringoptnode.pass_1: tnode;
  112. begin
  113. pass_1 := nil;
  114. expectloc:= LOC_REFERENCE;
  115. calcregisters(self,0,0,0);
  116. { here we call STRCONCAT or STRCMP or STRCOPY }
  117. include(current_procinfo.flags,pi_do_call);
  118. end;
  119. function taddsstringoptnode.dogetcopy: tnode;
  120. var
  121. hp: taddsstringoptnode;
  122. begin
  123. hp := taddsstringoptnode(inherited dogetcopy);
  124. hp.curmaxlen := curmaxlen;
  125. dogetcopy := hp;
  126. end;
  127. function taddsstringoptnode.docompare(p: tnode): boolean;
  128. begin
  129. docompare :=
  130. inherited docompare(p) and
  131. (curmaxlen = taddsstringcharoptnode(p).curmaxlen);
  132. end;
  133. function is_addsstringoptnode(p: tnode): boolean;
  134. begin
  135. is_addsstringoptnode :=
  136. p.inheritsfrom(taddsstringoptnode);
  137. end;
  138. procedure taddsstringoptnode.updatecurmaxlen;
  139. begin
  140. if is_addsstringoptnode(left) then
  141. begin
  142. { made it a separate block so no other if's are processed (would be a }
  143. { simple waste of time) (JM) }
  144. if (taddsstringoptnode(left).curmaxlen < 255) then
  145. case subnodetype of
  146. addsstringcharoptn:
  147. curmaxlen := succ(taddsstringoptnode(left).curmaxlen);
  148. addsstringcsstringoptn:
  149. curmaxlen := min(taddsstringoptnode(left).curmaxlen +
  150. tstringconstnode(right).len,255)
  151. else
  152. internalerror(291220001);
  153. end
  154. else curmaxlen := 255;
  155. end
  156. else if (left.nodetype = stringconstn) then
  157. curmaxlen := min(tstringconstnode(left).len,255)
  158. else if is_char(left.resultdef) then
  159. curmaxlen := 1
  160. else if (left.nodetype = typeconvn) then
  161. begin
  162. case ttypeconvnode(left).convtype of
  163. tc_char_2_string:
  164. curmaxlen := 1;
  165. { doesn't work yet, don't know why (JM)
  166. tc_chararray_2_string:
  167. curmaxlen :=
  168. min(ttypeconvnode(left).left.resultdef.size,255); }
  169. else curmaxlen := 255;
  170. end;
  171. end
  172. else
  173. curmaxlen := 255;
  174. end;
  175. {*****************************************************************************
  176. TADDSSTRINGCHAROPTNODE
  177. *****************************************************************************}
  178. constructor taddsstringcharoptnode.create(l,r : tnode);
  179. begin
  180. inherited create(addsstringcharoptn,l,r);
  181. end;
  182. {*****************************************************************************
  183. TADDSSTRINGCSSTRINGOPTNODE
  184. *****************************************************************************}
  185. constructor taddsstringcsstringoptnode.create(l,r : tnode);
  186. begin
  187. inherited create(addsstringcsstringoptn,l,r);
  188. end;
  189. function taddsstringcsstringoptnode.pass_1: tnode;
  190. begin
  191. { create the call to the concat routine both strings as arguments }
  192. result := ccallnode.createintern('fpc_shortstr_append_shortstr',
  193. ccallparanode.create(left,ccallparanode.create(right,nil)));
  194. left:=nil;
  195. right:=nil;
  196. end;
  197. {*****************************************************************************
  198. HELPERS
  199. *****************************************************************************}
  200. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  201. begin
  202. canbeaddsstringcharoptnode :=
  203. (cs_opt_level1 in current_settings.optimizerswitches) and
  204. { the shortstring will be gotten through conversion if necessary (JM)
  205. is_shortstring(p.left.resultdef) and }
  206. ((p.nodetype = addn) and
  207. is_char(p.right.resultdef));
  208. end;
  209. function genaddsstringcharoptnode(p: taddnode): tnode;
  210. var
  211. hp: tnode;
  212. begin
  213. hp := caddsstringcharoptnode.create(p.left.getcopy,p.right.getcopy);
  214. hp.flags := p.flags;
  215. genaddsstringcharoptnode := hp;
  216. end;
  217. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  218. begin
  219. canbeaddsstringcsstringoptnode :=
  220. (cs_opt_level1 in current_settings.optimizerswitches) and
  221. { the shortstring will be gotten through conversion if necessary (JM)
  222. is_shortstring(p.left.resultdef) and }
  223. ((p.nodetype = addn) and
  224. (p.right.nodetype = stringconstn));
  225. end;
  226. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  227. var
  228. hp: tnode;
  229. begin
  230. hp := caddsstringcsstringoptnode.create(p.left.getcopy,p.right.getcopy);
  231. hp.flags := p.flags;
  232. genaddsstringcsstringoptnode := hp;
  233. end;
  234. function canbemultistringadd(p: taddnode): boolean;
  235. var
  236. hp : tnode;
  237. i : longint;
  238. begin
  239. result:=false;
  240. if p.resultdef.typ<>stringdef then
  241. exit;
  242. i:=0;
  243. hp:=p;
  244. while assigned(hp) and (hp.nodetype=addn) do
  245. begin
  246. inc(i);
  247. hp:=taddnode(hp).left;
  248. end;
  249. result:=(i>1);
  250. end;
  251. function genmultistringadd(p: taddnode): tnode;
  252. var
  253. hp,sn : tnode;
  254. arrp : tarrayconstructornode;
  255. newstatement : tstatementnode;
  256. tempnode : ttempcreatenode;
  257. is_shortstr : boolean;
  258. begin
  259. arrp:=nil;
  260. hp:=p;
  261. is_shortstr:=is_shortstring(p.resultdef);
  262. while assigned(hp) and (hp.nodetype=addn) do
  263. begin
  264. sn:=taddnode(hp).right.getcopy;
  265. inserttypeconv(sn,p.resultdef);
  266. if is_shortstr then
  267. begin
  268. sn:=caddrnode.create(sn);
  269. include(sn.flags,nf_internal);
  270. end;
  271. arrp:=carrayconstructornode.create(sn,arrp);
  272. hp:=taddnode(hp).left;
  273. end;
  274. sn:=hp.getcopy;
  275. inserttypeconv(sn,p.resultdef);
  276. if is_shortstr then
  277. begin
  278. sn:=caddrnode.create(sn);
  279. include(sn.flags,nf_internal);
  280. end;
  281. arrp:=carrayconstructornode.create(sn,arrp);
  282. if assigned(aktassignmentnode) and
  283. (aktassignmentnode.right=p) and
  284. (aktassignmentnode.left.resultdef=p.resultdef) and
  285. valid_for_var(aktassignmentnode.left,false) then
  286. begin
  287. result:=ccallnode.createintern('fpc_'+
  288. tstringdef(p.resultdef).stringtypname+'_concat_multi',
  289. ccallparanode.create(arrp,
  290. ccallparanode.create(aktassignmentnode.left.getcopy,nil)));
  291. include(aktassignmentnode.flags,nf_assign_done_in_right);
  292. end
  293. else
  294. begin
  295. result:=internalstatements(newstatement);
  296. tempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent ,true);
  297. addstatement(newstatement,tempnode);
  298. addstatement(newstatement,ccallnode.createintern('fpc_'+
  299. tstringdef(p.resultdef).stringtypname+'_concat_multi',
  300. ccallparanode.create(arrp,
  301. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  302. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  303. addstatement(newstatement,ctemprefnode.create(tempnode));
  304. end;
  305. end;
  306. begin
  307. caddsstringcharoptnode := taddsstringcharoptnode;
  308. caddsstringcsstringoptnode := taddsstringcsstringoptnode;
  309. end.