2
0

nopt.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. { here we call STRCONCAT or STRCMP or STRCOPY }
  116. include(current_procinfo.flags,pi_do_call);
  117. end;
  118. function taddsstringoptnode.dogetcopy: tnode;
  119. var
  120. hp: taddsstringoptnode;
  121. begin
  122. hp := taddsstringoptnode(inherited dogetcopy);
  123. hp.curmaxlen := curmaxlen;
  124. dogetcopy := hp;
  125. end;
  126. function taddsstringoptnode.docompare(p: tnode): boolean;
  127. begin
  128. docompare :=
  129. inherited docompare(p) and
  130. (curmaxlen = taddsstringcharoptnode(p).curmaxlen);
  131. end;
  132. function is_addsstringoptnode(p: tnode): boolean;
  133. begin
  134. is_addsstringoptnode :=
  135. p.inheritsfrom(taddsstringoptnode);
  136. end;
  137. procedure taddsstringoptnode.updatecurmaxlen;
  138. begin
  139. if is_addsstringoptnode(left) then
  140. begin
  141. { made it a separate block so no other if's are processed (would be a }
  142. { simple waste of time) (JM) }
  143. if (taddsstringoptnode(left).curmaxlen < 255) then
  144. case subnodetype of
  145. addsstringcharoptn:
  146. curmaxlen := succ(taddsstringoptnode(left).curmaxlen);
  147. addsstringcsstringoptn:
  148. curmaxlen := min(taddsstringoptnode(left).curmaxlen +
  149. tstringconstnode(right).len,255)
  150. else
  151. internalerror(291220001);
  152. end
  153. else curmaxlen := 255;
  154. end
  155. else if (left.nodetype = stringconstn) then
  156. curmaxlen := min(tstringconstnode(left).len,255)
  157. else if is_char(left.resultdef) then
  158. curmaxlen := 1
  159. else if (left.nodetype = typeconvn) then
  160. begin
  161. case ttypeconvnode(left).convtype of
  162. tc_char_2_string:
  163. curmaxlen := 1;
  164. { doesn't work yet, don't know why (JM)
  165. tc_chararray_2_string:
  166. curmaxlen :=
  167. min(ttypeconvnode(left).left.resultdef.size,255); }
  168. else curmaxlen := 255;
  169. end;
  170. end
  171. else
  172. curmaxlen := 255;
  173. end;
  174. {*****************************************************************************
  175. TADDSSTRINGCHAROPTNODE
  176. *****************************************************************************}
  177. constructor taddsstringcharoptnode.create(l,r : tnode);
  178. begin
  179. inherited create(addsstringcharoptn,l,r);
  180. end;
  181. {*****************************************************************************
  182. TADDSSTRINGCSSTRINGOPTNODE
  183. *****************************************************************************}
  184. constructor taddsstringcsstringoptnode.create(l,r : tnode);
  185. begin
  186. inherited create(addsstringcsstringoptn,l,r);
  187. end;
  188. function taddsstringcsstringoptnode.pass_1: tnode;
  189. begin
  190. { create the call to the concat routine both strings as arguments }
  191. result := ccallnode.createintern('fpc_shortstr_append_shortstr',
  192. ccallparanode.create(left,ccallparanode.create(right,nil)));
  193. left:=nil;
  194. right:=nil;
  195. end;
  196. {*****************************************************************************
  197. HELPERS
  198. *****************************************************************************}
  199. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  200. begin
  201. canbeaddsstringcharoptnode :=
  202. (cs_opt_level1 in current_settings.optimizerswitches) and
  203. { the shortstring will be gotten through conversion if necessary (JM)
  204. is_shortstring(p.left.resultdef) and }
  205. ((p.nodetype = addn) and
  206. is_char(p.right.resultdef));
  207. end;
  208. function genaddsstringcharoptnode(p: taddnode): tnode;
  209. var
  210. hp: tnode;
  211. begin
  212. hp := caddsstringcharoptnode.create(p.left.getcopy,p.right.getcopy);
  213. hp.flags := p.flags;
  214. genaddsstringcharoptnode := hp;
  215. end;
  216. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  217. begin
  218. canbeaddsstringcsstringoptnode :=
  219. (cs_opt_level1 in current_settings.optimizerswitches) and
  220. { the shortstring will be gotten through conversion if necessary (JM)
  221. is_shortstring(p.left.resultdef) and }
  222. ((p.nodetype = addn) and
  223. (p.right.nodetype = stringconstn));
  224. end;
  225. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  226. var
  227. hp: tnode;
  228. begin
  229. hp := caddsstringcsstringoptnode.create(p.left.getcopy,p.right.getcopy);
  230. hp.flags := p.flags;
  231. genaddsstringcsstringoptnode := hp;
  232. end;
  233. function canbemultistringadd(p: taddnode): boolean;
  234. var
  235. hp : tnode;
  236. i : longint;
  237. begin
  238. result:=false;
  239. if p.resultdef.typ<>stringdef then
  240. exit;
  241. i:=0;
  242. hp:=p;
  243. while assigned(hp) and (hp.nodetype=addn) do
  244. begin
  245. inc(i);
  246. hp:=taddnode(hp).left;
  247. end;
  248. result:=(i>1);
  249. end;
  250. function genmultistringadd(p: taddnode): tnode;
  251. var
  252. hp,sn : tnode;
  253. arrp : tarrayconstructornode;
  254. newstatement : tstatementnode;
  255. tempnode : ttempcreatenode;
  256. is_shortstr : boolean;
  257. begin
  258. arrp:=nil;
  259. hp:=p;
  260. is_shortstr:=is_shortstring(p.resultdef);
  261. while assigned(hp) and (hp.nodetype=addn) do
  262. begin
  263. sn:=taddnode(hp).right.getcopy;
  264. inserttypeconv(sn,p.resultdef);
  265. if is_shortstr then
  266. begin
  267. sn:=caddrnode.create(sn);
  268. include(sn.flags,nf_internal);
  269. end;
  270. arrp:=carrayconstructornode.create(sn,arrp);
  271. hp:=taddnode(hp).left;
  272. end;
  273. sn:=hp.getcopy;
  274. inserttypeconv(sn,p.resultdef);
  275. if is_shortstr then
  276. begin
  277. sn:=caddrnode.create(sn);
  278. include(sn.flags,nf_internal);
  279. end;
  280. arrp:=carrayconstructornode.create(sn,arrp);
  281. if assigned(aktassignmentnode) and
  282. (aktassignmentnode.right=p) and
  283. (aktassignmentnode.left.resultdef=p.resultdef) and
  284. valid_for_var(aktassignmentnode.left,false) then
  285. begin
  286. result:=ccallnode.createintern('fpc_'+
  287. tstringdef(p.resultdef).stringtypname+'_concat_multi',
  288. ccallparanode.create(arrp,
  289. ccallparanode.create(aktassignmentnode.left.getcopy,nil)));
  290. include(aktassignmentnode.flags,nf_assign_done_in_right);
  291. end
  292. else
  293. begin
  294. result:=internalstatements(newstatement);
  295. tempnode:=ctempcreatenode.create(p.resultdef,p.resultdef.size,tt_persistent ,true);
  296. addstatement(newstatement,tempnode);
  297. addstatement(newstatement,ccallnode.createintern('fpc_'+
  298. tstringdef(p.resultdef).stringtypname+'_concat_multi',
  299. ccallparanode.create(arrp,
  300. ccallparanode.create(ctemprefnode.create(tempnode),nil))));
  301. addstatement(newstatement,ctempdeletenode.create_normal_temp(tempnode));
  302. addstatement(newstatement,ctemprefnode.create(tempnode));
  303. end;
  304. end;
  305. begin
  306. caddsstringcharoptnode := taddsstringcharoptnode;
  307. caddsstringcsstringoptnode := taddsstringcsstringoptnode;
  308. end.