ncgopt.pas 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. {
  2. $Id$
  3. Copyright (c) 1998-2003 by Jonas Maebe
  4. This unit implements the generic implementation of 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 ncgopt;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses node, nopt;
  22. type
  23. tcgaddsstringcharoptnode = class(taddsstringcharoptnode)
  24. function det_resulttype: tnode; override;
  25. function pass_1: tnode; override;
  26. procedure pass_2; override;
  27. end;
  28. implementation
  29. uses
  30. globtype,
  31. pass_1,defutil,htypechk,
  32. symdef,paramgr,
  33. aasmbase,aasmtai,
  34. ncnv, ncon, pass_2,
  35. cginfo, cgbase, cpubase,
  36. tgobj, rgobj, cgobj, ncgutil;
  37. {*****************************************************************************
  38. TCGADDOPTNODE
  39. *****************************************************************************}
  40. function tcgaddsstringcharoptnode.det_resulttype: tnode;
  41. begin
  42. det_resulttype := nil;
  43. resulttypepass(left);
  44. resulttypepass(right);
  45. if codegenerror then
  46. exit;
  47. { update the curmaxlen field (before converting to a string!) }
  48. updatecurmaxlen;
  49. if not is_shortstring(left.resulttype.def) then
  50. inserttypeconv(left,cshortstringtype);
  51. resulttype:=left.resulttype;
  52. end;
  53. function tcgaddsstringcharoptnode.pass_1: tnode;
  54. begin
  55. pass_1 := nil;
  56. firstpass(left);
  57. firstpass(right);
  58. if codegenerror then
  59. exit;
  60. expectloc:=LOC_REFERENCE;
  61. if not is_constcharnode(right) then
  62. { it's not sure we need the register, but we can't know it here yet }
  63. calcregisters(self,2,0,0)
  64. else
  65. calcregisters(self,1,0,0);
  66. end;
  67. procedure tcgaddsstringcharoptnode.pass_2;
  68. var
  69. l: tasmlabel;
  70. href,href2 : treference;
  71. hreg, lengthreg: tregister;
  72. checklength: boolean;
  73. len : integer;
  74. begin
  75. { first, we have to more or less replicate some code from }
  76. { ti386addnode.pass_2 }
  77. secondpass(left);
  78. if not(tg.istemp(left.location.reference) and
  79. (tg.sizeoftemp(exprasmlist,left.location.reference) = 256)) and
  80. not(nf_use_strconcat in flags) then
  81. begin
  82. tg.Gettemp(exprasmlist,256,tt_normal,href);
  83. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  84. { location is released by copyshortstring }
  85. location_freetemp(exprasmlist,left.location);
  86. { return temp reference }
  87. location_reset(left.location,LOC_REFERENCE,def_cgsize(resulttype.def));
  88. left.location.reference:=href;
  89. end;
  90. secondpass(right);
  91. { special case for string := string + char (JM) }
  92. hreg.enum:=R_INTREGISTER;
  93. hreg.number:=NR_NO;
  94. { we have to load the char before checking the length, because we }
  95. { may need registers from the reference }
  96. { is it a constant char? }
  97. if not is_constcharnode(right) then
  98. { no, make sure it is in a register }
  99. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  100. begin
  101. { free the registers of right }
  102. reference_release(exprasmlist,right.location.reference);
  103. { get register for the char }
  104. hreg := rg.getregisterint(exprasmlist,OS_8);
  105. cg.a_load_ref_reg(exprasmlist,OS_8,right.location.reference,hreg);
  106. { I don't think a temp char exists, but it won't hurt (JM) }
  107. tg.ungetiftemp(exprasmlist,right.location.reference);
  108. end
  109. else hreg := right.location.register;
  110. { load the current string length }
  111. lengthreg := rg.getregisterint(exprasmlist,OS_INT);
  112. cg.a_load_ref_reg(exprasmlist,OS_8,left.location.reference,lengthreg);
  113. { do we have to check the length ? }
  114. if tg.istemp(left.location.reference) then
  115. checklength := curmaxlen = 255
  116. else
  117. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  118. if checklength then
  119. begin
  120. { is it already maximal? }
  121. objectlibrary.getlabel(l);
  122. if tg.istemp(left.location.reference) then
  123. len:=255
  124. else
  125. len:=tstringdef(left.resulttype.def).len;
  126. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,len,lengthreg,l)
  127. end;
  128. { no, so increase the length and add the new character }
  129. href2 := left.location.reference;
  130. { we need a new reference to store the character }
  131. { at the end of the string. Check if the base or }
  132. { index register is still free }
  133. if (href2.base.number <> NR_NO) and
  134. (href2.index.number <> NR_NO) then
  135. begin
  136. { they're not free, so add the base reg to }
  137. { the string length (since the index can }
  138. { have a scalefactor) and use lengthreg as base }
  139. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,href2.base,lengthreg);
  140. href2.base := lengthreg;
  141. end
  142. else
  143. { at least one is still free, so put EDI there }
  144. if href2.base.number = NR_NO then
  145. href2.base := lengthreg
  146. else
  147. begin
  148. href2.index := lengthreg;
  149. {$ifdef i386}
  150. href2.scalefactor := 1;
  151. {$endif i386}
  152. end;
  153. { we need to be one position after the last char }
  154. inc(href2.offset);
  155. { store the character at the end of the string }
  156. if (right.nodetype <> ordconstn) then
  157. begin
  158. { no new_reference(href2) because it's only }
  159. { used once (JM) }
  160. cg.a_load_reg_ref(exprasmlist,OS_8,hreg,href2);
  161. rg.ungetregisterint(exprasmlist,hreg);
  162. end
  163. else
  164. cg.a_load_const_ref(exprasmlist,OS_8,tordconstnode(right).value,href2);
  165. lengthreg.number:=(lengthreg.number and not $ff) or R_SUBL;
  166. { increase the string length }
  167. cg.a_op_const_reg(exprasmlist,OP_ADD,1,lengthreg);
  168. cg.a_load_reg_ref(exprasmlist,OS_8,lengthreg,left.location.reference);
  169. rg.ungetregisterint(exprasmlist,lengthreg);
  170. if checklength then
  171. cg.a_label(exprasmlist,l);
  172. location_copy(location,left.location);
  173. end;
  174. begin
  175. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  176. end.
  177. {
  178. $Log$
  179. Revision 1.3 2003-05-26 21:15:18 peter
  180. * disable string node optimizations for the moment
  181. Revision 1.2 2003/04/26 09:12:55 peter
  182. * add string returns in LOC_REFERENCE
  183. Revision 1.1 2003/04/24 11:20:06 florian
  184. + created from n386opt
  185. }