n386opt.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Jonas Maebe
  4. This unit implements the 80x86 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 n386opt;
  19. {$i defines.inc}
  20. interface
  21. uses node, nopt;
  22. type
  23. ti386addsstringcharoptnode = class(taddsstringcharoptnode)
  24. function det_resulttype: tnode; override;
  25. function pass_1: tnode; override;
  26. procedure pass_2; override;
  27. end;
  28. ti386addsstringcsstringoptnode = class(taddsstringcsstringoptnode)
  29. { must be duplicated from ti386addnode :( }
  30. procedure pass_2; override;
  31. end;
  32. implementation
  33. uses pass_1, types, htypechk, cginfo, cgbase, cpubase, cga,
  34. tgobj, aasm, ncnv, ncon, pass_2, symdef, rgobj, cgobj;
  35. {*****************************************************************************
  36. TI386ADDOPTNODE
  37. *****************************************************************************}
  38. function ti386addsstringcharoptnode.det_resulttype: tnode;
  39. begin
  40. det_resulttype := nil;
  41. resulttypepass(left);
  42. resulttypepass(right);
  43. if codegenerror then
  44. exit;
  45. { update the curmaxlen field (before converting to a string!) }
  46. updatecurmaxlen;
  47. if not is_shortstring(left.resulttype.def) then
  48. inserttypeconv(left,cshortstringtype);
  49. resulttype:=left.resulttype;
  50. end;
  51. function ti386addsstringcharoptnode.pass_1: tnode;
  52. begin
  53. pass_1 := nil;
  54. firstpass(left);
  55. firstpass(right);
  56. if codegenerror then
  57. exit;
  58. location.loc := LOC_CREFERENCE;
  59. if not is_constcharnode(right) then
  60. { it's not sure we need the register, but we can't know it here yet }
  61. calcregisters(self,2,0,0)
  62. else
  63. calcregisters(self,1,0,0);
  64. end;
  65. procedure ti386addsstringcharoptnode.pass_2;
  66. var
  67. l: tasmlabel;
  68. href,href2 : treference;
  69. hreg, lengthreg: tregister;
  70. checklength: boolean;
  71. begin
  72. { first, we have to more or less replicate some code from }
  73. { ti386addnode.pass_2 }
  74. secondpass(left);
  75. if not(tg.istemp(left.location.reference) and
  76. (tg.getsizeoftemp(left.location.reference) = 256)) and
  77. not(nf_use_strconcat in flags) then
  78. begin
  79. tg.gettempofsizereference(exprasmlist,256,href);
  80. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  81. { location is released by copyshortstring }
  82. location_freetemp(exprasmlist,left.location);
  83. { return temp reference }
  84. location_reset(left.location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  85. left.location.reference:=href;
  86. end;
  87. secondpass(right);
  88. { special case for string := string + char (JM) }
  89. hreg := R_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. { free the registers of right }
  98. reference_release(exprasmlist,right.location.reference);
  99. { get register for the char }
  100. hreg := rg.makeregsize(rg.getregisterint(exprasmlist),OS_8);
  101. emit_ref_reg(A_MOV,S_B,right.location.reference,hreg);
  102. { I don't think a temp char exists, but it won't hurt (JM) }
  103. tg.ungetiftemp(exprasmlist,right.location.reference);
  104. end
  105. else hreg := right.location.register;
  106. { load the current string length }
  107. lengthreg := rg.getregisterint(exprasmlist);
  108. emit_ref_reg(A_MOVZX,S_BL,left.location.reference,lengthreg);
  109. { do we have to check the length ? }
  110. if tg.istemp(left.location.reference) then
  111. checklength := curmaxlen = 255
  112. else
  113. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  114. if checklength then
  115. begin
  116. { is it already maximal? }
  117. getlabel(l);
  118. if tg.istemp(left.location.reference) then
  119. emit_const_reg(A_CMP,S_L,255,lengthreg)
  120. else
  121. emit_const_reg(A_CMP,S_L,tstringdef(left.resulttype.def).len,lengthreg);
  122. emitjmp(C_E,l);
  123. end;
  124. { no, so increase the length and add the new character }
  125. href2 := left.location.reference;
  126. { we need a new reference to store the character }
  127. { at the end of the string. Check if the base or }
  128. { index register is still free }
  129. if (href2.base <> R_NO) and
  130. (href2.index <> R_NO) then
  131. begin
  132. { they're not free, so add the base reg to }
  133. { the string length (since the index can }
  134. { have a scalefactor) and use lengthreg as base }
  135. emit_reg_reg(A_ADD,S_L,href2.base,lengthreg);
  136. href2.base := lengthreg;
  137. end
  138. else
  139. { at least one is still free, so put EDI there }
  140. if href2.base = R_NO then
  141. href2.base := lengthreg
  142. else
  143. begin
  144. href2.index := lengthreg;
  145. href2.scalefactor := 1;
  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. emit_reg_ref(A_MOV,S_B,hreg,href2);
  155. rg.ungetregister(exprasmlist,hreg);
  156. end
  157. else
  158. emit_const_ref(A_MOV,S_B,tordconstnode(right).value,href2);
  159. { increase the string length }
  160. emit_reg(A_INC,S_B,rg.makeregsize(lengthreg,OS_8));
  161. emit_reg_ref(A_MOV,S_B,rg.makeregsize(lengthreg,OS_8),left.location.reference);
  162. rg.ungetregisterint(exprasmlist,lengthreg);
  163. if checklength then
  164. emitlab(l);
  165. location_copy(location,left.location);
  166. end;
  167. procedure ti386addsstringcsstringoptnode.pass_2;
  168. var
  169. href: treference;
  170. pushedregs: tpushedsaved;
  171. regstopush: tregisterset;
  172. begin
  173. { first, we have to more or less replicate some code from }
  174. { ti386addnode.pass_2 }
  175. secondpass(left);
  176. if not(tg.istemp(left.location.reference) and
  177. (tg.getsizeoftemp(left.location.reference) = 256)) and
  178. not(nf_use_strconcat in flags) then
  179. begin
  180. tg.gettempofsizereference(exprasmlist,256,href);
  181. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  182. { release the registers }
  183. location_freetemp(exprasmlist,left.location);
  184. { return temp reference }
  185. location_reset(left.location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  186. left.location.reference:=href;
  187. end;
  188. secondpass(right);
  189. { on the right we do not need the register anymore too }
  190. { Instead of releasing them already, simply do not }
  191. { push them (so the release is in the right place, }
  192. { because emitpushreferenceaddr doesn't need extra }
  193. { registers) (JM) }
  194. regstopush := all_registers;
  195. remove_non_regvars_from_loc(right.location,regstopush);
  196. rg.saveusedregisters(exprasmlist,pushedregs,regstopush);
  197. { push the maximum possible length of the result }
  198. emitpushreferenceaddr(left.location.reference);
  199. { the optimizer can more easily put the }
  200. { deallocations in the right place if it happens }
  201. { too early than when it happens too late (if }
  202. { the pushref needs a "lea (..),edi; push edi") }
  203. reference_release(exprasmlist,right.location.reference);
  204. emitpushreferenceaddr(right.location.reference);
  205. rg.saveregvars(exprasmlist,regstopush);
  206. emitcall('FPC_SHORTSTR_CONCAT');
  207. tg.ungetiftemp(exprasmlist,right.location.reference);
  208. maybe_loadself;
  209. rg.restoreusedregisters(exprasmlist,pushedregs);
  210. location_copy(location,left.location);
  211. end;
  212. begin
  213. caddsstringcharoptnode := ti386addsstringcharoptnode;
  214. caddsstringcsstringoptnode := ti386addsstringcsstringoptnode
  215. end.
  216. {
  217. $Log$
  218. Revision 1.12 2002-04-25 20:16:40 peter
  219. * moved more routines from cga/n386util
  220. Revision 1.11 2002/04/21 15:36:40 carl
  221. * changeregsize -> rg.makeregsize
  222. Revision 1.10 2002/04/15 19:44:21 peter
  223. * fixed stackcheck that would be called recursively when a stack
  224. error was found
  225. * generic changeregsize(reg,size) for i386 register resizing
  226. * removed some more routines from cga unit
  227. * fixed returnvalue handling
  228. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  229. Revision 1.9 2002/04/04 19:06:12 peter
  230. * removed unused units
  231. * use tlocation.size in cg.a_*loc*() routines
  232. Revision 1.8 2002/04/02 17:11:36 peter
  233. * tlocation,treference update
  234. * LOC_CONSTANT added for better constant handling
  235. * secondadd splitted in multiple routines
  236. * location_force_reg added for loading a location to a register
  237. of a specified size
  238. * secondassignment parses now first the right and then the left node
  239. (this is compatible with Kylix). This saves a lot of push/pop especially
  240. with string operations
  241. * adapted some routines to use the new cg methods
  242. Revision 1.7 2002/03/31 20:26:39 jonas
  243. + a_loadfpu_* and a_loadmm_* methods in tcg
  244. * register allocation is now handled by a class and is mostly processor
  245. independent (+rgobj.pas and i386/rgcpu.pas)
  246. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  247. * some small improvements and fixes to the optimizer
  248. * some register allocation fixes
  249. * some fpuvaroffset fixes in the unary minus node
  250. * push/popusedregisters is now called rg.save/restoreusedregisters and
  251. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  252. also better optimizable)
  253. * fixed and optimized register saving/restoring for new/dispose nodes
  254. * LOC_FPU locations now also require their "register" field to be set to
  255. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  256. - list field removed of the tnode class because it's not used currently
  257. and can cause hard-to-find bugs
  258. Revision 1.6 2001/12/31 09:53:15 jonas
  259. * changed remaining "getregister32" calls to ":=rg.getregisterint(exprasmlist);"
  260. Revision 1.5 2001/08/26 13:37:00 florian
  261. * some cg reorganisation
  262. * some PPC updates
  263. Revision 1.4 2001/04/13 01:22:19 peter
  264. * symtable change to classes
  265. * range check generation and errors fixed, make cycle DEBUG=1 works
  266. * memory leaks fixed
  267. Revision 1.3 2001/04/02 21:20:38 peter
  268. * resulttype rewrite
  269. Revision 1.2 2001/01/06 19:12:31 jonas
  270. * fixed IE 10 (but code is less efficient now :( )
  271. Revision 1.1 2001/01/04 11:24:19 jonas
  272. + initial implementation (still needs to be made more modular)
  273. }