nopt.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Jonas Maebe
  4. This unit implements 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 nopt;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses node, nadd;
  22. type
  23. tsubnodetype = (
  24. addsstringcharoptn, { shorstring + char }
  25. addsstringcsstringoptn { shortstring + constant shortstring }
  26. );
  27. taddoptnode = class(taddnode)
  28. subnodetype: tsubnodetype;
  29. constructor create(ts: tsubnodetype; l,r : tnode); virtual;
  30. { pass_1 will be overridden by the separate subclasses }
  31. { By default, pass_2 is the same as for addnode }
  32. { Only if there's a processor specific implementation, it }
  33. { will be overridden. }
  34. function getcopy: tnode; override;
  35. function docompare(p: tnode): boolean; override;
  36. end;
  37. taddsstringoptnode = class(taddoptnode)
  38. { maximum length of the string until now, allows us to skip a compare }
  39. { sometimes (it's initialized/updated by calling updatecurmaxlen) }
  40. curmaxlen: byte;
  41. { pass_1 must be overridden, otherwise we get an endless loop }
  42. function det_resulttype: tnode; override;
  43. function pass_1: tnode; override;
  44. function getcopy: tnode; override;
  45. function docompare(p: tnode): boolean; override;
  46. protected
  47. procedure updatecurmaxlen;
  48. end;
  49. { add a char to a shortstring }
  50. taddsstringcharoptnode = class(taddsstringoptnode)
  51. constructor create(l,r : tnode); virtual;
  52. end;
  53. taddsstringcharoptnodeclass = class of taddsstringcharoptnode;
  54. { add a constant string to a short string }
  55. taddsstringcsstringoptnode = class(taddsstringoptnode)
  56. constructor create(l,r : tnode); virtual;
  57. function pass_1: tnode; override;
  58. end;
  59. taddsstringcsstringoptnodeclass = class of taddsstringcsstringoptnode;
  60. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  61. function genaddsstringcharoptnode(p: taddnode): tnode;
  62. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  63. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  64. function is_addsstringoptnode(p: tnode): boolean;
  65. var
  66. caddsstringcharoptnode: taddsstringcharoptnodeclass;
  67. caddsstringcsstringoptnode: taddsstringcsstringoptnodeclass;
  68. implementation
  69. uses cutils, htypechk, defutil, defcmp, globtype, globals, cpubase, ncnv, ncon,ncal,
  70. verbose, symdef, cgbase, procinfo;
  71. {*****************************************************************************
  72. TADDOPTNODE
  73. *****************************************************************************}
  74. constructor taddoptnode.create(ts: tsubnodetype; l,r : tnode);
  75. begin
  76. { we need to keep the addn nodetype, otherwise taddnode.pass_2 will be }
  77. { confused. Comparison for equal nodetypes therefore has to be }
  78. { implemented using the classtype() method (JM) }
  79. inherited create(addn,l,r);
  80. subnodetype := ts;
  81. end;
  82. function taddoptnode.getcopy: tnode;
  83. var
  84. hp: taddoptnode;
  85. begin
  86. hp := taddoptnode(inherited getcopy);
  87. hp.subnodetype := subnodetype;
  88. getcopy := hp;
  89. end;
  90. function taddoptnode.docompare(p: tnode): boolean;
  91. begin
  92. docompare :=
  93. inherited docompare(p) and
  94. (subnodetype = taddoptnode(p).subnodetype);
  95. end;
  96. {*****************************************************************************
  97. TADDSSTRINGOPTNODE
  98. *****************************************************************************}
  99. function taddsstringoptnode.det_resulttype: tnode;
  100. begin
  101. result := nil;
  102. updatecurmaxlen;
  103. { left and right are already firstpass'ed by taddnode.pass_1 }
  104. if not is_shortstring(left.resulttype.def) then
  105. inserttypeconv(left,cshortstringtype);
  106. if not is_shortstring(right.resulttype.def) then
  107. inserttypeconv(right,cshortstringtype);
  108. resulttype := left.resulttype;
  109. end;
  110. function taddsstringoptnode.pass_1: tnode;
  111. begin
  112. pass_1 := nil;
  113. expectloc:= LOC_REFERENCE;
  114. calcregisters(self,0,0,0);
  115. { here we call STRCONCAT or STRCMP or STRCOPY }
  116. include(current_procinfo.flags,pi_do_call);
  117. end;
  118. function taddsstringoptnode.getcopy: tnode;
  119. var
  120. hp: taddsstringoptnode;
  121. begin
  122. hp := taddsstringoptnode(inherited getcopy);
  123. hp.curmaxlen := curmaxlen;
  124. getcopy := 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.resulttype.def) 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.resulttype.def.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_optimize in aktglobalswitches) and
  203. { the shortstring will be gotten through conversion if necessary (JM)
  204. is_shortstring(p.left.resulttype.def) and }
  205. ((p.nodetype = addn) and
  206. is_char(p.right.resulttype.def));
  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_optimize in aktglobalswitches) and
  220. { the shortstring will be gotten through conversion if necessary (JM)
  221. is_shortstring(p.left.resulttype.def) 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. begin
  234. caddsstringcharoptnode := taddsstringcharoptnode;
  235. caddsstringcsstringoptnode := taddsstringcsstringoptnode;
  236. end.
  237. {
  238. $Log$
  239. Revision 1.17 2003-10-01 20:34:49 peter
  240. * procinfo unit contains tprocinfo
  241. * cginfo renamed to cgbase
  242. * moved cgmessage to verbose
  243. * fixed ppc and sparc compiles
  244. Revision 1.16 2003/05/26 21:15:18 peter
  245. * disable string node optimizations for the moment
  246. Revision 1.15 2003/04/27 11:21:33 peter
  247. * aktprocdef renamed to current_procdef
  248. * procinfo renamed to current_procinfo
  249. * procinfo will now be stored in current_module so it can be
  250. cleaned up properly
  251. * gen_main_procsym changed to create_main_proc and release_main_proc
  252. to also generate a tprocinfo structure
  253. * fixed unit implicit initfinal
  254. Revision 1.14 2003/04/26 09:12:55 peter
  255. * add string returns in LOC_REFERENCE
  256. Revision 1.13 2003/04/22 23:50:23 peter
  257. * firstpass uses expectloc
  258. * checks if there are differences between the expectloc and
  259. location.loc from secondpass in EXTDEBUG
  260. Revision 1.12 2002/11/25 17:43:20 peter
  261. * splitted defbase in defutil,symutil,defcmp
  262. * merged isconvertable and is_equal into compare_defs(_ext)
  263. * made operator search faster by walking the list only once
  264. Revision 1.11 2002/08/17 09:23:37 florian
  265. * first part of current_procinfo rewrite
  266. Revision 1.10 2002/07/20 11:57:55 florian
  267. * types.pas renamed to defbase.pas because D6 contains a types
  268. unit so this would conflicts if D6 programms are compiled
  269. + Willamette/SSE2 instructions to assembler added
  270. Revision 1.9 2002/05/18 13:34:10 peter
  271. * readded missing revisions
  272. Revision 1.8 2002/05/16 19:46:39 carl
  273. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  274. + try to fix temp allocation (still in ifdef)
  275. + generic constructor calls
  276. + start of tassembler / tmodulebase class cleanup
  277. Revision 1.6 2002/04/02 17:11:29 peter
  278. * tlocation,treference update
  279. * LOC_CONSTANT added for better constant handling
  280. * secondadd splitted in multiple routines
  281. * location_force_reg added for loading a location to a register
  282. of a specified size
  283. * secondassignment parses now first the right and then the left node
  284. (this is compatible with Kylix). This saves a lot of push/pop especially
  285. with string operations
  286. * adapted some routines to use the new cg methods
  287. }