ncgopt.pas 6.2 KB

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