nopt.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. 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 is_addsstringoptnode(p: tnode): boolean;
  64. var
  65. caddsstringcharoptnode: taddsstringcharoptnodeclass;
  66. caddsstringcsstringoptnode: taddsstringcsstringoptnodeclass;
  67. implementation
  68. uses cutils, htypechk, defbase, globtype, globals, cpubase, ncnv, ncon,
  69. verbose, symdef, cgbase;
  70. {*****************************************************************************
  71. TADDOPTNODE
  72. *****************************************************************************}
  73. constructor taddoptnode.create(ts: tsubnodetype; l,r : tnode);
  74. begin
  75. { we need to keep the addn nodetype, otherwise taddnode.pass_2 will be }
  76. { confused. Comparison for equal nodetypes therefore has to be }
  77. { implemented using the classtype() method (JM) }
  78. inherited create(addn,l,r);
  79. subnodetype := ts;
  80. end;
  81. function taddoptnode.getcopy: tnode;
  82. var
  83. hp: taddoptnode;
  84. begin
  85. hp := taddoptnode(inherited getcopy);
  86. hp.subnodetype := subnodetype;
  87. getcopy := hp;
  88. end;
  89. function taddoptnode.docompare(p: tnode): boolean;
  90. begin
  91. docompare :=
  92. inherited docompare(p) and
  93. (subnodetype = taddoptnode(p).subnodetype);
  94. end;
  95. {*****************************************************************************
  96. TADDSSTRINGOPTNODE
  97. *****************************************************************************}
  98. function taddsstringoptnode.det_resulttype: tnode;
  99. begin
  100. result := nil;
  101. updatecurmaxlen;
  102. { left and right are already firstpass'ed by taddnode.pass_1 }
  103. if not is_shortstring(left.resulttype.def) then
  104. inserttypeconv(left,cshortstringtype);
  105. if not is_shortstring(right.resulttype.def) then
  106. inserttypeconv(right,cshortstringtype);
  107. resulttype := left.resulttype;
  108. end;
  109. function taddsstringoptnode.pass_1: tnode;
  110. begin
  111. pass_1 := nil;
  112. location.loc := LOC_CREFERENCE;
  113. calcregisters(self,0,0,0);
  114. { here we call STRCONCAT or STRCMP or STRCOPY }
  115. procinfo.flags:=procinfo.flags or pi_do_call;
  116. end;
  117. function taddsstringoptnode.getcopy: tnode;
  118. var
  119. hp: taddsstringoptnode;
  120. begin
  121. hp := taddsstringoptnode(inherited getcopy);
  122. hp.curmaxlen := curmaxlen;
  123. getcopy := hp;
  124. end;
  125. function taddsstringoptnode.docompare(p: tnode): boolean;
  126. begin
  127. docompare :=
  128. inherited docompare(p) and
  129. (curmaxlen = taddsstringcharoptnode(p).curmaxlen);
  130. end;
  131. function is_addsstringoptnode(p: tnode): boolean;
  132. begin
  133. is_addsstringoptnode :=
  134. p.inheritsfrom(taddsstringoptnode);
  135. end;
  136. procedure taddsstringoptnode.updatecurmaxlen;
  137. begin
  138. if is_addsstringoptnode(left) then
  139. begin
  140. { made it a separate block so no other if's are processed (would be a }
  141. { simple waste of time) (JM) }
  142. if (taddsstringoptnode(left).curmaxlen < 255) then
  143. case subnodetype of
  144. addsstringcharoptn:
  145. curmaxlen := succ(taddsstringoptnode(left).curmaxlen);
  146. addsstringcsstringoptn:
  147. curmaxlen := min(taddsstringoptnode(left).curmaxlen +
  148. tstringconstnode(right).len,255)
  149. else
  150. internalerror(291220001);
  151. end
  152. else curmaxlen := 255;
  153. end
  154. else if (left.nodetype = stringconstn) then
  155. curmaxlen := min(tstringconstnode(left).len,255)
  156. else if is_char(left.resulttype.def) then
  157. curmaxlen := 1
  158. else if (left.nodetype = typeconvn) then
  159. begin
  160. case ttypeconvnode(left).convtype of
  161. tc_char_2_string:
  162. curmaxlen := 1;
  163. { doesn't work yet, don't know why (JM)
  164. tc_chararray_2_string:
  165. curmaxlen :=
  166. min(ttypeconvnode(left).left.resulttype.def.size,255); }
  167. else curmaxlen := 255;
  168. end;
  169. end
  170. else
  171. curmaxlen := 255;
  172. end;
  173. {*****************************************************************************
  174. TADDSSTRINGCHAROPTNODE
  175. *****************************************************************************}
  176. constructor taddsstringcharoptnode.create(l,r : tnode);
  177. begin
  178. inherited create(addsstringcharoptn,l,r);
  179. end;
  180. {*****************************************************************************
  181. TADDSSTRINGCSSTRINGOPTNODE
  182. *****************************************************************************}
  183. constructor taddsstringcsstringoptnode.create(l,r : tnode);
  184. begin
  185. inherited create(addsstringcsstringoptn,l,r);
  186. end;
  187. {*****************************************************************************
  188. HELPERS
  189. *****************************************************************************}
  190. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  191. begin
  192. canbeaddsstringcharoptnode :=
  193. (cs_optimize in aktglobalswitches) and
  194. { the shortstring will be gotten through conversion if necessary (JM)
  195. is_shortstring(p.left.resulttype.def) and }
  196. ((p.nodetype = addn) and
  197. is_char(p.right.resulttype.def));
  198. end;
  199. function genaddsstringcharoptnode(p: taddnode): tnode;
  200. var
  201. hp: tnode;
  202. begin
  203. hp := caddsstringcharoptnode.create(p.left.getcopy,p.right.getcopy);
  204. hp.flags := p.flags;
  205. genaddsstringcharoptnode := hp;
  206. end;
  207. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  208. begin
  209. canbeaddsstringcsstringoptnode :=
  210. (cs_optimize in aktglobalswitches) and
  211. { the shortstring will be gotten through conversion if necessary (JM)
  212. is_shortstring(p.left.resulttype.def) and }
  213. ((p.nodetype = addn) and
  214. (p.right.nodetype = stringconstn));
  215. end;
  216. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  217. var
  218. hp: tnode;
  219. begin
  220. hp := caddsstringcsstringoptnode.create(p.left.getcopy,p.right.getcopy);
  221. hp.flags := p.flags;
  222. genaddsstringcsstringoptnode := hp;
  223. end;
  224. begin
  225. caddsstringcharoptnode := taddsstringcharoptnode;
  226. caddsstringcsstringoptnode := taddsstringcsstringoptnode;
  227. end.
  228. {
  229. $Log$
  230. Revision 1.11 2002-08-17 09:23:37 florian
  231. * first part of procinfo rewrite
  232. Revision 1.10 2002/07/20 11:57:55 florian
  233. * types.pas renamed to defbase.pas because D6 contains a types
  234. unit so this would conflicts if D6 programms are compiled
  235. + Willamette/SSE2 instructions to assembler added
  236. Revision 1.9 2002/05/18 13:34:10 peter
  237. * readded missing revisions
  238. Revision 1.8 2002/05/16 19:46:39 carl
  239. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  240. + try to fix temp allocation (still in ifdef)
  241. + generic constructor calls
  242. + start of tassembler / tmodulebase class cleanup
  243. Revision 1.6 2002/04/02 17:11:29 peter
  244. * tlocation,treference update
  245. * LOC_CONSTANT added for better constant handling
  246. * secondadd splitted in multiple routines
  247. * location_force_reg added for loading a location to a register
  248. of a specified size
  249. * secondassignment parses now first the right and then the left node
  250. (this is compatible with Kylix). This saves a lot of push/pop especially
  251. with string operations
  252. * adapted some routines to use the new cg methods
  253. }