ncgopt.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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,
  31. symdef,
  32. aasmbase,aasmdata,
  33. ncnv, ncon, pass_2,
  34. cgbase, cpubase,
  35. tgobj, cgobj, hlcgobj, cgutils;
  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. end;
  61. procedure tcgaddsstringcharoptnode.pass_generate_code;
  62. var
  63. l: tasmlabel;
  64. href,href2 : treference;
  65. hreg, lengthreg: tregister;
  66. checklength: boolean;
  67. len : integer;
  68. begin
  69. l:=nil;
  70. { first, we have to more or less replicate some code from }
  71. { ti386addnode.pass_generate_code }
  72. secondpass(left);
  73. if not(tg.istemp(left.location.reference) and
  74. (tg.sizeoftemp(current_asmdata.CurrAsmList,left.location.reference) = 256)) then
  75. begin
  76. tg.gethltemp(current_asmdata.CurrAsmList,cshortstringtype,256,tt_normal,href);
  77. hlcg.g_copyshortstring(current_asmdata.CurrAsmList,left.location.reference,href,tstringdef(cshortstringtype));
  78. location_freetemp(current_asmdata.CurrAsmList,left.location);
  79. { return temp reference }
  80. location_reset_ref(left.location,LOC_REFERENCE,def_cgsize(resultdef),1,[]);
  81. left.location.reference:=href;
  82. end;
  83. secondpass(right);
  84. { special case for string := string + char (JM) }
  85. hreg:=NR_NO;
  86. { we have to load the char before checking the length, because we }
  87. { may need registers from the reference }
  88. { is it a constant char? }
  89. if not is_constcharnode(right) then
  90. { no, make sure it is in a register }
  91. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  92. begin
  93. { get register for the char }
  94. hreg := cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  95. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_8,right.location.reference,hreg);
  96. { I don't think a temp char exists, but it won't hurt (JM) }
  97. tg.ungetiftemp(current_asmdata.CurrAsmList,right.location.reference);
  98. end
  99. else hreg := right.location.register;
  100. { load the current string length }
  101. lengthreg := cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  102. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_8,OS_INT,left.location.reference,lengthreg);
  103. { do we have to check the length ? }
  104. if tg.istemp(left.location.reference) then
  105. checklength := curmaxlen = 255
  106. else
  107. checklength := curmaxlen >= tstringdef(left.resultdef).len;
  108. if checklength then
  109. begin
  110. { is it already maximal? }
  111. current_asmdata.getjumplabel(l);
  112. if tg.istemp(left.location.reference) then
  113. len:=255
  114. else
  115. len:=tstringdef(left.resultdef).len;
  116. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,OC_EQ,len,lengthreg,l)
  117. end;
  118. { no, so increase the length and add the new character }
  119. href2 := left.location.reference;
  120. { we need a new reference to store the character }
  121. { at the end of the string. Check if the base or }
  122. { index register is still free }
  123. if (href2.base <> NR_NO) and
  124. (href2.index <> NR_NO) then
  125. begin
  126. { they're not free, so add the base reg to }
  127. { the string length (since the index can }
  128. { have a scalefactor) and use lengthreg as base }
  129. cg.a_op_reg_reg(current_asmdata.CurrAsmList,OP_ADD,OS_INT,href2.base,lengthreg);
  130. href2.base := lengthreg;
  131. end
  132. else
  133. { at least one is still free, so put EDI there }
  134. if href2.base = NR_NO then
  135. href2.base := lengthreg
  136. else
  137. begin
  138. href2.index := lengthreg;
  139. {$ifdef x86}
  140. href2.scalefactor := 1;
  141. {$endif x86}
  142. end;
  143. { we need to be one position after the last char }
  144. inc(href2.offset);
  145. { store the character at the end of the string }
  146. if (right.nodetype <> ordconstn) then
  147. begin
  148. { no new_reference(href2) because it's only }
  149. { used once (JM) }
  150. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,hreg,href2);
  151. end
  152. else
  153. cg.a_load_const_ref(current_asmdata.CurrAsmList,OS_8,tordconstnode(right).value.svalue,href2);
  154. lengthreg:=cg.makeregsize(current_asmdata.CurrAsmList,lengthreg,OS_8);
  155. { increase the string length }
  156. cg.a_op_const_reg(current_asmdata.CurrAsmList,OP_ADD,OS_8,1,lengthreg);
  157. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_8,OS_8,lengthreg,left.location.reference);
  158. if checklength then
  159. cg.a_label(current_asmdata.CurrAsmList,l);
  160. location_copy(location,left.location);
  161. end;
  162. begin
  163. caddsstringcharoptnode := tcgaddsstringcharoptnode;
  164. end.