ncgopt.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 pass_typecheck: tnode; override;
  24. function pass_1: tnode; override;
  25. procedure pass_generate_code; 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.pass_typecheck: tnode;
  40. begin
  41. pass_typecheck := nil;
  42. typecheckpass(left);
  43. typecheckpass(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.resultdef) then
  49. inserttypeconv(left,cshortstringtype);
  50. resultdef:=left.resultdef;
  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_generate_code;
  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_generate_code }
  76. secondpass(left);
  77. if not(tg.istemp(left.location.reference) and
  78. (tg.sizeoftemp(current_asmdata.CurrAsmList,left.location.reference) = 256)) then
  79. begin
  80. tg.Gettemp(current_asmdata.CurrAsmList,256,tt_normal,href);
  81. cg.g_copyshortstring(current_asmdata.CurrAsmList,left.location.reference,href,255);
  82. location_freetemp(current_asmdata.CurrAsmList,left.location);
  83. { return temp reference }
  84. location_reset(left.location,LOC_REFERENCE,def_cgsize(resultdef));
  85. left.location.reference:=href;
  86. end;
  87. secondpass(right);
  88. { special case for string := string + char (JM) }
  89. hreg:=NR_NO;
  90. { we have to load the char before checking the length, because we }
  91. { may need registers from the reference }
  92. { is it a constant char? }
  93. if not is_constcharnode(right) then
  94. { no, make sure it is in a register }
  95. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  96. begin
  97. { get register for the char }
  98. hreg := cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  99. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,hreg);
  100. { I don't think a temp char exists, but it won't hurt (JM) }
  101. tg.ungetiftemp(current_asmdata.CurrAsmList,right.location.reference);
  102. end
  103. else hreg := right.location.register;
  104. { load the current string length }
  105. lengthreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  106. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_INT,left.location.reference,lengthreg);
  107. { do we have to check the length ? }
  108. if tg.istemp(left.location.reference) then
  109. checklength := curmaxlen = 255
  110. else
  111. checklength := curmaxlen >= tstringdef(left.resultdef).len;
  112. if checklength then
  113. begin
  114. { is it already maximal? }
  115. current_asmdata.getjumplabel(l);
  116. if tg.istemp(left.location.reference) then
  117. len:=255
  118. else
  119. len:=tstringdef(left.resultdef).len;
  120. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,len,lengthreg,l)
  121. end;
  122. { no, so increase the length and add the new character }
  123. href2 := left.location.reference;
  124. { we need a new reference to store the character }
  125. { at the end of the string. Check if the base or }
  126. { index register is still free }
  127. if (href2.base <> NR_NO) and
  128. (href2.index <> NR_NO) then
  129. begin
  130. { they're not free, so add the base reg to }
  131. { the string length (since the index can }
  132. { have a scalefactor) and use lengthreg as base }
  133. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,href2.base,lengthreg);
  134. href2.base := lengthreg;
  135. end
  136. else
  137. { at least one is still free, so put EDI there }
  138. if href2.base = NR_NO then
  139. href2.base := lengthreg
  140. else
  141. begin
  142. href2.index := lengthreg;
  143. {$ifdef x86}
  144. href2.scalefactor := 1;
  145. {$endif x86}
  146. end;
  147. { we need to be one position after the last char }
  148. inc(href2.offset);
  149. { store the character at the end of the string }
  150. if (right.nodetype <> ordconstn) then
  151. begin
  152. { no new_reference(href2) because it's only }
  153. { used once (JM) }
  154. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,hreg,href2);
  155. end
  156. else
  157. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,tordconstnode(right).value,href2);
  158. lengthreg:=cg.makeregsize(current_asmdata.CurrAsmList,lengthreg,OS_8);
  159. { increase the string length }
  160. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_8,1,lengthreg);
  161. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,lengthreg,left.location.reference);
  162. if checklength then
  163. cg.a_label(current_asmdata.CurrAsmList,l);
  164. location_copy(location,left.location);
  165. end;
  166. begin
  167. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  168. end.