ncgopt.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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,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:=NR_NO;
  93. { we have to load the char before checking the length, because we }
  94. { may need registers from the reference }
  95. { is it a constant char? }
  96. if not is_constcharnode(right) then
  97. { no, make sure it is in a register }
  98. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  99. begin
  100. { free the registers of right }
  101. reference_release(exprasmlist,right.location.reference);
  102. { get register for the char }
  103. hreg := cg.getintregister(exprasmlist,OS_8);
  104. cg.a_load_ref_reg(exprasmlist,OS_8,OS_8,right.location.reference,hreg);
  105. { I don't think a temp char exists, but it won't hurt (JM) }
  106. tg.ungetiftemp(exprasmlist,right.location.reference);
  107. end
  108. else hreg := right.location.register;
  109. { load the current string length }
  110. lengthreg := cg.getintregister(exprasmlist,OS_INT);
  111. cg.a_load_ref_reg(exprasmlist,OS_8,OS_INT,left.location.reference,lengthreg);
  112. { do we have to check the length ? }
  113. if tg.istemp(left.location.reference) then
  114. checklength := curmaxlen = 255
  115. else
  116. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  117. if checklength then
  118. begin
  119. { is it already maximal? }
  120. objectlibrary.getlabel(l);
  121. if tg.istemp(left.location.reference) then
  122. len:=255
  123. else
  124. len:=tstringdef(left.resulttype.def).len;
  125. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,len,lengthreg,l)
  126. end;
  127. { no, so increase the length and add the new character }
  128. href2 := left.location.reference;
  129. { we need a new reference to store the character }
  130. { at the end of the string. Check if the base or }
  131. { index register is still free }
  132. if (href2.base <> NR_NO) and
  133. (href2.index <> NR_NO) then
  134. begin
  135. { they're not free, so add the base reg to }
  136. { the string length (since the index can }
  137. { have a scalefactor) and use lengthreg as base }
  138. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,href2.base,lengthreg);
  139. href2.base := lengthreg;
  140. end
  141. else
  142. { at least one is still free, so put EDI there }
  143. if href2.base = NR_NO then
  144. href2.base := lengthreg
  145. else
  146. begin
  147. href2.index := lengthreg;
  148. {$ifdef i386}
  149. href2.scalefactor := 1;
  150. {$endif i386}
  151. end;
  152. { we need to be one position after the last char }
  153. inc(href2.offset);
  154. { store the character at the end of the string }
  155. if (right.nodetype <> ordconstn) then
  156. begin
  157. { no new_reference(href2) because it's only }
  158. { used once (JM) }
  159. cg.a_load_reg_ref(exprasmlist,OS_8,OS_8,hreg,href2);
  160. cg.ungetregister(exprasmlist,hreg);
  161. end
  162. else
  163. cg.a_load_const_ref(exprasmlist,OS_8,tordconstnode(right).value,href2);
  164. lengthreg:=cg.makeregsize(lengthreg,OS_8);
  165. { increase the string length }
  166. cg.a_op_const_reg(exprasmlist,OP_ADD,OS_8,1,lengthreg);
  167. cg.a_load_reg_ref(exprasmlist,OS_8,OS_8,lengthreg,left.location.reference);
  168. cg.ungetregister(exprasmlist,lengthreg);
  169. if checklength then
  170. cg.a_label(exprasmlist,l);
  171. location_copy(location,left.location);
  172. end;
  173. begin
  174. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  175. end.
  176. {
  177. $Log$
  178. Revision 1.11 2003-10-10 17:48:13 peter
  179. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  180. * tregisteralloctor renamed to trgobj
  181. * removed rgobj from a lot of units
  182. * moved location_* and reference_* to cgobj
  183. * first things for mmx register allocation
  184. Revision 1.10 2003/10/09 21:31:37 daniel
  185. * Register allocator splitted, ans abstract now
  186. Revision 1.9 2003/10/01 20:34:48 peter
  187. * procinfo unit contains tprocinfo
  188. * cginfo renamed to cgbase
  189. * moved cgmessage to verbose
  190. * fixed ppc and sparc compiles
  191. Revision 1.8 2003/09/07 22:09:35 peter
  192. * preparations for different default calling conventions
  193. * various RA fixes
  194. Revision 1.7 2003/09/03 15:55:00 peter
  195. * NEWRA branch merged
  196. Revision 1.6.2.1 2003/08/29 17:28:59 peter
  197. * next batch of updates
  198. Revision 1.6 2003/06/03 21:11:09 peter
  199. * cg.a_load_* get a from and to size specifier
  200. * makeregsize only accepts newregister
  201. * i386 uses generic tcgnotnode,tcgunaryminus
  202. Revision 1.5 2003/06/03 13:01:59 daniel
  203. * Register allocator finished
  204. Revision 1.4 2003/06/01 21:38:06 peter
  205. * getregisterfpu size parameter added
  206. * op_const_reg size parameter added
  207. * sparc updates
  208. Revision 1.3 2003/05/26 21:15:18 peter
  209. * disable string node optimizations for the moment
  210. Revision 1.2 2003/04/26 09:12:55 peter
  211. * add string returns in LOC_REFERENCE
  212. Revision 1.1 2003/04/24 11:20:06 florian
  213. + created from n386opt
  214. }