n386opt.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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
  34. pass_1,defutil,htypechk,
  35. symdef,paramgr,
  36. aasmbase,aasmtai,aasmcpu,
  37. ncnv, ncon, pass_2,
  38. cginfo, cgbase, cpubase,
  39. tgobj, rgobj, cgobj, ncgutil;
  40. {*****************************************************************************
  41. TI386ADDOPTNODE
  42. *****************************************************************************}
  43. function ti386addsstringcharoptnode.det_resulttype: tnode;
  44. begin
  45. det_resulttype := nil;
  46. resulttypepass(left);
  47. resulttypepass(right);
  48. if codegenerror then
  49. exit;
  50. { update the curmaxlen field (before converting to a string!) }
  51. updatecurmaxlen;
  52. if not is_shortstring(left.resulttype.def) then
  53. inserttypeconv(left,cshortstringtype);
  54. resulttype:=left.resulttype;
  55. end;
  56. function ti386addsstringcharoptnode.pass_1: tnode;
  57. begin
  58. pass_1 := nil;
  59. firstpass(left);
  60. firstpass(right);
  61. if codegenerror then
  62. exit;
  63. location.loc := LOC_CREFERENCE;
  64. if not is_constcharnode(right) then
  65. { it's not sure we need the register, but we can't know it here yet }
  66. calcregisters(self,2,0,0)
  67. else
  68. calcregisters(self,1,0,0);
  69. end;
  70. procedure ti386addsstringcharoptnode.pass_2;
  71. var
  72. l: tasmlabel;
  73. href,href2 : treference;
  74. hreg, lengthreg: tregister;
  75. checklength: boolean;
  76. len : integer;
  77. begin
  78. { first, we have to more or less replicate some code from }
  79. { ti386addnode.pass_2 }
  80. secondpass(left);
  81. if not(tg.istemp(left.location.reference) and
  82. (tg.sizeoftemp(exprasmlist,left.location.reference) = 256)) and
  83. not(nf_use_strconcat in flags) then
  84. begin
  85. tg.Gettemp(exprasmlist,256,tt_normal,href);
  86. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  87. { location is released by copyshortstring }
  88. location_freetemp(exprasmlist,left.location);
  89. { return temp reference }
  90. location_reset(left.location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  91. left.location.reference:=href;
  92. end;
  93. secondpass(right);
  94. { special case for string := string + char (JM) }
  95. hreg.enum:=R_INTREGISTER;
  96. hreg.number:=NR_NO;
  97. { we have to load the char before checking the length, because we }
  98. { may need registers from the reference }
  99. { is it a constant char? }
  100. if not is_constcharnode(right) then
  101. { no, make sure it is in a register }
  102. if right.location.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  103. begin
  104. { free the registers of right }
  105. reference_release(exprasmlist,right.location.reference);
  106. { get register for the char }
  107. hreg := rg.getregisterint(exprasmlist,OS_8);
  108. cg.a_load_ref_reg(exprasmlist,OS_8,right.location.reference,hreg);
  109. { I don't think a temp char exists, but it won't hurt (JM) }
  110. tg.ungetiftemp(exprasmlist,right.location.reference);
  111. end
  112. else hreg := right.location.register;
  113. { load the current string length }
  114. lengthreg := rg.getregisterint(exprasmlist,OS_INT);
  115. cg.a_load_ref_reg(exprasmlist,OS_8,left.location.reference,lengthreg);
  116. { do we have to check the length ? }
  117. if tg.istemp(left.location.reference) then
  118. checklength := curmaxlen = 255
  119. else
  120. checklength := curmaxlen >= tstringdef(left.resulttype.def).len;
  121. if checklength then
  122. begin
  123. { is it already maximal? }
  124. objectlibrary.getlabel(l);
  125. if tg.istemp(left.location.reference) then
  126. len:=255
  127. else
  128. len:=tstringdef(left.resulttype.def).len;
  129. cg.a_cmp_const_reg_label(exprasmlist,OS_INT,OC_EQ,len,lengthreg,l)
  130. end;
  131. { no, so increase the length and add the new character }
  132. href2 := left.location.reference;
  133. { we need a new reference to store the character }
  134. { at the end of the string. Check if the base or }
  135. { index register is still free }
  136. if (href2.base.number <> NR_NO) and
  137. (href2.index.number <> NR_NO) then
  138. begin
  139. { they're not free, so add the base reg to }
  140. { the string length (since the index can }
  141. { have a scalefactor) and use lengthreg as base }
  142. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,href2.base,lengthreg);
  143. href2.base := lengthreg;
  144. end
  145. else
  146. { at least one is still free, so put EDI there }
  147. if href2.base.number = NR_NO then
  148. href2.base := lengthreg
  149. else
  150. begin
  151. href2.index := lengthreg;
  152. href2.scalefactor := 1;
  153. end;
  154. { we need to be one position after the last char }
  155. inc(href2.offset);
  156. { store the character at the end of the string }
  157. if (right.nodetype <> ordconstn) then
  158. begin
  159. { no new_reference(href2) because it's only }
  160. { used once (JM) }
  161. cg.a_load_reg_ref(exprasmlist,OS_8,hreg,href2);
  162. rg.ungetregisterint(exprasmlist,hreg);
  163. end
  164. else
  165. cg.a_load_const_ref(exprasmlist,OS_8,tordconstnode(right).value,href2);
  166. lengthreg.number:=(lengthreg.number and not $ff) or R_SUBL;
  167. { increase the string length }
  168. cg.a_op_const_reg(exprasmlist,OP_ADD,1,lengthreg);
  169. cg.a_load_reg_ref(exprasmlist,OS_8,lengthreg,left.location.reference);
  170. rg.ungetregisterint(exprasmlist,lengthreg);
  171. if checklength then
  172. cg.a_label(exprasmlist,l);
  173. location_copy(location,left.location);
  174. end;
  175. procedure ti386addsstringcsstringoptnode.pass_2;
  176. var
  177. href: treference;
  178. pushedregs: tpushedsavedint;
  179. regstopush: tsupregset;
  180. begin
  181. { first, we have to more or less replicate some code from }
  182. { ti386addnode.pass_2 }
  183. secondpass(left);
  184. if not(tg.istemp(left.location.reference) and
  185. (tg.sizeoftemp(exprasmlist,left.location.reference) = 256)) and
  186. not(nf_use_strconcat in flags) then
  187. begin
  188. tg.GetTemp(exprasmlist,256,tt_normal,href);
  189. cg.g_copyshortstring(exprasmlist,left.location.reference,href,255,true,false);
  190. { release the registers }
  191. location_freetemp(exprasmlist,left.location);
  192. { return temp reference }
  193. location_reset(left.location,LOC_CREFERENCE,def_cgsize(resulttype.def));
  194. left.location.reference:=href;
  195. end;
  196. secondpass(right);
  197. { on the right we do not need the register anymore too }
  198. { Instead of releasing them already, simply do not }
  199. { push them (so the release is in the right place, }
  200. { because emitpushreferenceaddr doesn't need extra }
  201. { registers) (JM) }
  202. regstopush := all_intregisters;
  203. remove_non_regvars_from_loc(right.location,regstopush);
  204. rg.saveusedintregisters(exprasmlist,pushedregs,regstopush);
  205. { push the maximum possible length of the result }
  206. cg.a_paramaddr_ref(exprasmlist,left.location.reference,paramanager.getintparaloc(2));
  207. { the optimizer can more easily put the }
  208. { deallocations in the right place if it happens }
  209. { too early than when it happens too late (if }
  210. { the pushref needs a "lea (..),edi; push edi") }
  211. reference_release(exprasmlist,right.location.reference);
  212. cg.a_paramaddr_ref(exprasmlist,right.location.reference,paramanager.getintparaloc(1));
  213. rg.saveintregvars(exprasmlist,regstopush);
  214. cg.a_call_name(exprasmlist,'FPC_SHORTSTR_CONCAT');
  215. tg.ungetiftemp(exprasmlist,right.location.reference);
  216. rg.restoreusedintregisters(exprasmlist,pushedregs);
  217. location_copy(location,left.location);
  218. end;
  219. begin
  220. caddsstringcharoptnode := ti386addsstringcharoptnode;
  221. caddsstringcsstringoptnode := ti386addsstringcsstringoptnode
  222. end.
  223. {
  224. $Log$
  225. Revision 1.29 2003-03-28 19:16:57 peter
  226. * generic constructor working for i386
  227. * remove fixed self register
  228. * esi added as address register for i386
  229. Revision 1.28 2003/02/19 22:00:15 daniel
  230. * Code generator converted to new register notation
  231. - Horribily outdated todo.txt removed
  232. Revision 1.27 2003/01/08 18:43:57 daniel
  233. * Tregister changed into a record
  234. Revision 1.26 2002/11/25 17:43:27 peter
  235. * splitted defbase in defutil,symutil,defcmp
  236. * merged isconvertable and is_equal into compare_defs(_ext)
  237. * made operator search faster by walking the list only once
  238. Revision 1.25 2002/11/15 01:58:57 peter
  239. * merged changes from 1.0.7 up to 04-11
  240. - -V option for generating bug report tracing
  241. - more tracing for option parsing
  242. - errors for cdecl and high()
  243. - win32 import stabs
  244. - win32 records<=8 are returned in eax:edx (turned off by default)
  245. - heaptrc update
  246. - more info for temp management in .s file with EXTDEBUG
  247. Revision 1.24 2002/08/23 16:14:49 peter
  248. * tempgen cleanup
  249. * tt_noreuse temp type added that will be used in genentrycode
  250. Revision 1.23 2002/08/11 14:32:30 peter
  251. * renamed current_library to objectlibrary
  252. Revision 1.22 2002/08/11 13:24:17 peter
  253. * saving of asmsymbols in ppu supported
  254. * asmsymbollist global is removed and moved into a new class
  255. tasmlibrarydata that will hold the info of a .a file which
  256. corresponds with a single module. Added librarydata to tmodule
  257. to keep the library info stored for the module. In the future the
  258. objectfiles will also be stored to the tasmlibrarydata class
  259. * all getlabel/newasmsymbol and friends are moved to the new class
  260. Revision 1.21 2002/07/20 11:58:04 florian
  261. * types.pas renamed to defbase.pas because D6 contains a types
  262. unit so this would conflicts if D6 programms are compiled
  263. + Willamette/SSE2 instructions to assembler added
  264. Revision 1.20 2002/07/11 14:41:34 florian
  265. * start of the new generic parameter handling
  266. Revision 1.19 2002/07/07 09:52:34 florian
  267. * powerpc target fixed, very simple units can be compiled
  268. * some basic stuff for better callparanode handling, far from being finished
  269. Revision 1.18 2002/07/01 18:46:33 peter
  270. * internal linker
  271. * reorganized aasm layer
  272. Revision 1.17 2002/05/18 13:34:25 peter
  273. * readded missing revisions
  274. Revision 1.16 2002/05/16 19:46:52 carl
  275. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  276. + try to fix temp allocation (still in ifdef)
  277. + generic constructor calls
  278. + start of tassembler / tmodulebase class cleanup
  279. Revision 1.14 2002/05/13 19:54:38 peter
  280. * removed n386ld and n386util units
  281. * maybe_save/maybe_restore added instead of the old maybe_push
  282. Revision 1.13 2002/05/12 16:53:17 peter
  283. * moved entry and exitcode to ncgutil and cgobj
  284. * foreach gets extra argument for passing local data to the
  285. iterator function
  286. * -CR checks also class typecasts at runtime by changing them
  287. into as
  288. * fixed compiler to cycle with the -CR option
  289. * fixed stabs with elf writer, finally the global variables can
  290. be watched
  291. * removed a lot of routines from cga unit and replaced them by
  292. calls to cgobj
  293. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  294. u32bit then the other is typecasted also to u32bit without giving
  295. a rangecheck warning/error.
  296. * fixed pascal calling method with reversing also the high tree in
  297. the parast, detected by tcalcst3 test
  298. Revision 1.12 2002/04/25 20:16:40 peter
  299. * moved more routines from cga/n386util
  300. Revision 1.11 2002/04/21 15:36:40 carl
  301. * changeregsize -> rg.makeregsize
  302. Revision 1.10 2002/04/15 19:44:21 peter
  303. * fixed stackcheck that would be called recursively when a stack
  304. error was found
  305. * generic changeregsize(reg,size) for i386 register resizing
  306. * removed some more routines from cga unit
  307. * fixed returnvalue handling
  308. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  309. Revision 1.9 2002/04/04 19:06:12 peter
  310. * removed unused units
  311. * use tlocation.size in cg.a_*loc*() routines
  312. Revision 1.8 2002/04/02 17:11:36 peter
  313. * tlocation,treference update
  314. * LOC_CONSTANT added for better constant handling
  315. * secondadd splitted in multiple routines
  316. * location_force_reg added for loading a location to a register
  317. of a specified size
  318. * secondassignment parses now first the right and then the left node
  319. (this is compatible with Kylix). This saves a lot of push/pop especially
  320. with string operations
  321. * adapted some routines to use the new cg methods
  322. Revision 1.7 2002/03/31 20:26:39 jonas
  323. + a_loadfpu_* and a_loadmm_* methods in tcg
  324. * register allocation is now handled by a class and is mostly processor
  325. independent (+rgobj.pas and i386/rgcpu.pas)
  326. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  327. * some small improvements and fixes to the optimizer
  328. * some register allocation fixes
  329. * some fpuvaroffset fixes in the unary minus node
  330. * push/popusedregisters is now called rg.save/restoreusedregisters and
  331. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  332. also better optimizable)
  333. * fixed and optimized register saving/restoring for new/dispose nodes
  334. * LOC_FPU locations now also require their "register" field to be set to
  335. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  336. - list field removed of the tnode class because it's not used currently
  337. and can cause hard-to-find bugs
  338. }