ncgopt.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. {
  2. Copyright (c) 1998-2003 by Jonas Maebe
  3. This unit implements the generic implementation of optimized nodes
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ncgopt;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses node, nopt;
  21. type
  22. tcgaddsstringcharoptnode = class(taddsstringcharoptnode)
  23. function det_resulttype: tnode; override;
  24. function pass_1: tnode; override;
  25. procedure pass_2; override;
  26. end;
  27. implementation
  28. uses
  29. globtype,globals,
  30. pass_1,defutil,htypechk,
  31. symdef,paramgr,
  32. aasmbase,aasmtai,aasmdata,
  33. ncnv, ncon, pass_2,
  34. cgbase, cpubase,
  35. tgobj, cgobj, cgutils,ncgutil;
  36. {*****************************************************************************
  37. TCGADDOPTNODE
  38. *****************************************************************************}
  39. function tcgaddsstringcharoptnode.det_resulttype: tnode;
  40. begin
  41. det_resulttype := nil;
  42. resulttypepass(left);
  43. resulttypepass(right);
  44. if codegenerror then
  45. exit;
  46. { update the curmaxlen field (before converting to a string!) }
  47. updatecurmaxlen;
  48. if not is_shortstring(left.resulttype.def) then
  49. inserttypeconv(left,cshortstringtype);
  50. resulttype:=left.resulttype;
  51. end;
  52. function tcgaddsstringcharoptnode.pass_1: tnode;
  53. begin
  54. pass_1 := nil;
  55. firstpass(left);
  56. firstpass(right);
  57. if codegenerror then
  58. exit;
  59. expectloc:=LOC_REFERENCE;
  60. if not is_constcharnode(right) then
  61. { it's not sure we need the register, but we can't know it here yet }
  62. calcregisters(self,2,0,0)
  63. else
  64. calcregisters(self,1,0,0);
  65. end;
  66. procedure tcgaddsstringcharoptnode.pass_2;
  67. var
  68. l: tasmlabel;
  69. href,href2 : treference;
  70. hreg, lengthreg: tregister;
  71. checklength: boolean;
  72. len : integer;
  73. begin
  74. { first, we have to more or less replicate some code from }
  75. { ti386addnode.pass_2 }
  76. secondpass(left);
  77. if not(tg.istemp(left.location.reference) and
  78. (tg.sizeoftemp(current_asmdata.CurrAsmList,left.location.reference) = 256)) and
  79. not(nf_use_strconcat in flags) then
  80. begin
  81. tg.Gettemp(current_asmdata.CurrAsmList,256,tt_normal,href);
  82. cg.g_copyshortstring(current_asmdata.CurrAsmList,left.location.reference,href,255);
  83. location_freetemp(current_asmdata.CurrAsmList,left.location);
  84. { return temp reference }
  85. location_reset(left.location,LOC_REFERENCE,def_cgsize(resulttype.def));
  86. left.location.reference:=href;
  87. end;
  88. secondpass(right);
  89. { special case for string := string + char (JM) }
  90. hreg:=NR_NO;
  91. { we have to load the char before checking the length, because we }
  92. { may need registers from the reference }
  93. { is it a constant char? }
  94. if not is_constcharnode(right) then
  95. { no, make sure it is in a register }
  96. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  97. begin
  98. { get register for the char }
  99. hreg := cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  100. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,hreg);
  101. { I don't think a temp char exists, but it won't hurt (JM) }
  102. tg.ungetiftemp(current_asmdata.CurrAsmList,right.location.reference);
  103. end
  104. else hreg := right.location.register;
  105. { load the current string length }
  106. lengthreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  107. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_INT,left.location.reference,lengthreg);
  108. { do we have to check the length ? }
  109. if tg.istemp(left.location.reference) then
  110. checklength := curmaxlen = 255
  111. else
  112. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  113. if checklength then
  114. begin
  115. { is it already maximal? }
  116. current_asmdata.getjumplabel(l);
  117. if tg.istemp(left.location.reference) then
  118. len:=255
  119. else
  120. len:=tstringdef(left.resulttype.def).len;
  121. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,len,lengthreg,l)
  122. end;
  123. { no, so increase the length and add the new character }
  124. href2 := left.location.reference;
  125. { we need a new reference to store the character }
  126. { at the end of the string. Check if the base or }
  127. { index register is still free }
  128. if (href2.base <> NR_NO) and
  129. (href2.index <> NR_NO) then
  130. begin
  131. { they're not free, so add the base reg to }
  132. { the string length (since the index can }
  133. { have a scalefactor) and use lengthreg as base }
  134. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,href2.base,lengthreg);
  135. href2.base := lengthreg;
  136. end
  137. else
  138. { at least one is still free, so put EDI there }
  139. if href2.base = NR_NO then
  140. href2.base := lengthreg
  141. else
  142. begin
  143. href2.index := lengthreg;
  144. {$ifdef x86}
  145. href2.scalefactor := 1;
  146. {$endif x86}
  147. end;
  148. { we need to be one position after the last char }
  149. inc(href2.offset);
  150. { store the character at the end of the string }
  151. if (right.nodetype <> ordconstn) then
  152. begin
  153. { no new_reference(href2) because it's only }
  154. { used once (JM) }
  155. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,hreg,href2);
  156. end
  157. else
  158. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,tordconstnode(right).value,href2);
  159. lengthreg:=cg.makeregsize(current_asmdata.CurrAsmList,lengthreg,OS_8);
  160. { increase the string length }
  161. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_8,1,lengthreg);
  162. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,lengthreg,left.location.reference);
  163. if checklength then
  164. cg.a_label(current_asmdata.CurrAsmList,l);
  165. location_copy(location,left.location);
  166. end;
  167. begin
  168. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  169. end.