ncgopt.pas 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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,globals,
  31. pass_1,defutil,htypechk,
  32. symdef,paramgr,
  33. aasmbase,aasmtai,
  34. ncnv, ncon, pass_2,
  35. cgbase, cpubase,
  36. tgobj, 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);
  84. location_freetemp(exprasmlist,left.location);
  85. { return temp reference }
  86. location_reset(left.location,LOC_REFERENCE,def_cgsize(resulttype.def));
  87. left.location.reference:=href;
  88. end;
  89. secondpass(right);
  90. { special case for string := string + char (JM) }
  91. hreg:=NR_NO;
  92. { we have to load the char before checking the length, because we }
  93. { may need registers from the reference }
  94. { is it a constant char? }
  95. if not is_constcharnode(right) then
  96. { no, make sure it is in a register }
  97. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  98. begin
  99. { get register for the char }
  100. hreg := cg.getintregister(exprasmlist,OS_8);
  101. cg.a_load_ref_reg(exprasmlist,OS_8,OS_8,right.location.reference,hreg);
  102. { I don't think a temp char exists, but it won't hurt (JM) }
  103. tg.ungetiftemp(exprasmlist,right.location.reference);
  104. end
  105. else hreg := right.location.register;
  106. { load the current string length }
  107. lengthreg := cg.getintregister(exprasmlist,OS_INT);
  108. cg.a_load_ref_reg(exprasmlist,OS_8,OS_INT,left.location.reference,lengthreg);
  109. { do we have to check the length ? }
  110. if tg.istemp(left.location.reference) then
  111. checklength := curmaxlen = 255
  112. else
  113. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  114. if checklength then
  115. begin
  116. { is it already maximal? }
  117. objectlibrary.getlabel(l);
  118. if tg.istemp(left.location.reference) then
  119. len:=255
  120. else
  121. len:=tstringdef(left.resulttype.def).len;
  122. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,len,lengthreg,l)
  123. end;
  124. { no, so increase the length and add the new character }
  125. href2 := left.location.reference;
  126. { we need a new reference to store the character }
  127. { at the end of the string. Check if the base or }
  128. { index register is still free }
  129. if (href2.base <> NR_NO) and
  130. (href2.index <> NR_NO) then
  131. begin
  132. { they're not free, so add the base reg to }
  133. { the string length (since the index can }
  134. { have a scalefactor) and use lengthreg as base }
  135. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,href2.base,lengthreg);
  136. href2.base := lengthreg;
  137. end
  138. else
  139. { at least one is still free, so put EDI there }
  140. if href2.base = NR_NO then
  141. href2.base := lengthreg
  142. else
  143. begin
  144. href2.index := lengthreg;
  145. {$ifdef x86}
  146. href2.scalefactor := 1;
  147. {$endif x86}
  148. end;
  149. { we need to be one position after the last char }
  150. inc(href2.offset);
  151. { store the character at the end of the string }
  152. if (right.nodetype <> ordconstn) then
  153. begin
  154. { no new_reference(href2) because it's only }
  155. { used once (JM) }
  156. cg.a_load_reg_ref(exprasmlist,OS_8,OS_8,hreg,href2);
  157. end
  158. else
  159. cg.a_load_const_ref(exprasmlist,OS_8,tordconstnode(right).value,href2);
  160. lengthreg:=cg.makeregsize(exprasmlist,lengthreg,OS_8);
  161. { increase the string length }
  162. cg.a_op_const_reg(exprasmlist,OP_ADD,OS_8,1,lengthreg);
  163. cg.a_load_reg_ref(exprasmlist,OS_8,OS_8,lengthreg,left.location.reference);
  164. if checklength then
  165. cg.a_label(exprasmlist,l);
  166. location_copy(location,left.location);
  167. end;
  168. begin
  169. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  170. end.
  171. {
  172. $Log$
  173. Revision 1.16 2004-10-24 11:44:28 peter
  174. * small regvar fixes
  175. * loadref parameter removed from concatcopy,incrrefcount,etc
  176. Revision 1.15 2004/09/25 14:23:54 peter
  177. * ungetregister is now only used for cpuregisters, renamed to
  178. ungetcpuregister
  179. * renamed (get|unget)explicitregister(s) to ..cpuregister
  180. * removed location-release/reference_release
  181. Revision 1.14 2004/06/20 08:55:29 florian
  182. * logs truncated
  183. Revision 1.13 2004/05/22 23:34:28 peter
  184. tai_regalloc.allocation changed to ratype to notify rgobj of register size changes
  185. Revision 1.12 2004/01/31 17:45:17 peter
  186. * Change several $ifdef i386 to x86
  187. * Change several OS_32 to OS_INT/OS_ADDR
  188. }