nopt.pas 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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 defines.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 pass_1: tnode; override;
  43. function getcopy: 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;
  51. end;
  52. { add a constant string to a short string }
  53. taddsstringcsstringoptnode = class(taddsstringoptnode)
  54. constructor create(l,r : tnode); virtual;
  55. end;
  56. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  57. function genaddsstringcharoptnode(p: taddnode): tnode;
  58. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  59. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  60. function is_addsstringoptnode(p: tnode): boolean;
  61. var
  62. { these are never used directly
  63. caddoptnode: class of taddoptnode; }
  64. caddsstringcharoptnode: class of taddsstringcharoptnode;
  65. caddsstringcsstringoptnode: class of taddsstringcsstringoptnode;
  66. implementation
  67. uses cutils, htypechk, types, globtype, globals, cpubase, pass_1, ncnv, ncon,
  68. verbose, symdef, hcodegen;
  69. {*****************************************************************************
  70. TADDOPTNODE
  71. *****************************************************************************}
  72. constructor taddoptnode.create(ts: tsubnodetype; l,r : tnode);
  73. begin
  74. { we need to keep the addn nodetype, otherwise taddnode.pass_2 will be }
  75. { confused. Comparison for equal nodetypes therefore has to be }
  76. { implemented using the classtype() method (JM) }
  77. inherited create(addn,l,r);
  78. subnodetype := ts;
  79. end;
  80. function taddoptnode.getcopy: tnode;
  81. var
  82. hp: taddoptnode;
  83. begin
  84. hp := taddoptnode(inherited getcopy);
  85. hp.subnodetype := subnodetype;
  86. getcopy := hp;
  87. end;
  88. function taddoptnode.docompare(p: tnode): boolean;
  89. begin
  90. docompare :=
  91. inherited docompare(p) and
  92. (subnodetype = taddoptnode(p).subnodetype);
  93. end;
  94. {*****************************************************************************
  95. TADDSSTRINGOPTNODE
  96. *****************************************************************************}
  97. function taddsstringoptnode.pass_1: tnode;
  98. begin
  99. pass_1 := nil;
  100. updatecurmaxlen;
  101. { left and right are already firstpass'ed by taddnode.pass_1 }
  102. if not is_shortstring(left.resulttype) then
  103. begin
  104. left := gentypeconvnode(left,cshortstringdef);
  105. firstpass(left);
  106. end;
  107. if not is_shortstring(right.resulttype) then
  108. begin
  109. right := gentypeconvnode(right,cshortstringdef);
  110. firstpass(right);
  111. end;
  112. location.loc := LOC_MEM;
  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. resulttype := left.resulttype;
  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) 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^.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. {*****************************************************************************
  189. HELPERS
  190. *****************************************************************************}
  191. function canbeaddsstringcharoptnode(p: taddnode): boolean;
  192. begin
  193. canbeaddsstringcharoptnode :=
  194. (cs_optimize in aktglobalswitches) and
  195. { the shortstring will be gotten through conversion if necessary (JM)
  196. is_shortstring(p.left.resulttype) and }
  197. ((p.nodetype = addn) and
  198. is_char(p.right.resulttype));
  199. end;
  200. function genaddsstringcharoptnode(p: taddnode): tnode;
  201. var
  202. hp: tnode;
  203. begin
  204. hp := caddsstringcharoptnode.create(p.left.getcopy,p.right.getcopy);
  205. hp.flags := p.flags;
  206. genaddsstringcharoptnode := hp;
  207. end;
  208. function canbeaddsstringcsstringoptnode(p: taddnode): boolean;
  209. begin
  210. canbeaddsstringcsstringoptnode :=
  211. (cs_optimize in aktglobalswitches) and
  212. { the shortstring will be gotten through conversion if necessary (JM)
  213. is_shortstring(p.left.resulttype) and }
  214. ((p.nodetype = addn) and
  215. (p.right.nodetype = stringconstn));
  216. end;
  217. function genaddsstringcsstringoptnode(p: taddnode): tnode;
  218. var
  219. hp: tnode;
  220. begin
  221. hp := caddsstringcsstringoptnode.create(p.left.getcopy,p.right.getcopy);
  222. hp.flags := p.flags;
  223. genaddsstringcsstringoptnode := hp;
  224. end;
  225. begin
  226. caddsstringcharoptnode := taddsstringcharoptnode;
  227. caddsstringcsstringoptnode := taddsstringcsstringoptnode;
  228. end.
  229. {
  230. $Log$
  231. Revision 1.1 2001-01-04 11:24:19 jonas
  232. + initial implementation (still needs to be made more modular)
  233. }