n386opt.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. cg.g_maybe_loadself(exprasmlist);
  217. rg.restoreusedintregisters(exprasmlist,pushedregs);
  218. location_copy(location,left.location);
  219. end;
  220. begin
  221. caddsstringcharoptnode := ti386addsstringcharoptnode;
  222. caddsstringcsstringoptnode := ti386addsstringcsstringoptnode
  223. end.
  224. {
  225. $Log$
  226. Revision 1.28 2003-02-19 22:00:15 daniel
  227. * Code generator converted to new register notation
  228. - Horribily outdated todo.txt removed
  229. Revision 1.27 2003/01/08 18:43:57 daniel
  230. * Tregister changed into a record
  231. Revision 1.26 2002/11/25 17:43:27 peter
  232. * splitted defbase in defutil,symutil,defcmp
  233. * merged isconvertable and is_equal into compare_defs(_ext)
  234. * made operator search faster by walking the list only once
  235. Revision 1.25 2002/11/15 01:58:57 peter
  236. * merged changes from 1.0.7 up to 04-11
  237. - -V option for generating bug report tracing
  238. - more tracing for option parsing
  239. - errors for cdecl and high()
  240. - win32 import stabs
  241. - win32 records<=8 are returned in eax:edx (turned off by default)
  242. - heaptrc update
  243. - more info for temp management in .s file with EXTDEBUG
  244. Revision 1.24 2002/08/23 16:14:49 peter
  245. * tempgen cleanup
  246. * tt_noreuse temp type added that will be used in genentrycode
  247. Revision 1.23 2002/08/11 14:32:30 peter
  248. * renamed current_library to objectlibrary
  249. Revision 1.22 2002/08/11 13:24:17 peter
  250. * saving of asmsymbols in ppu supported
  251. * asmsymbollist global is removed and moved into a new class
  252. tasmlibrarydata that will hold the info of a .a file which
  253. corresponds with a single module. Added librarydata to tmodule
  254. to keep the library info stored for the module. In the future the
  255. objectfiles will also be stored to the tasmlibrarydata class
  256. * all getlabel/newasmsymbol and friends are moved to the new class
  257. Revision 1.21 2002/07/20 11:58:04 florian
  258. * types.pas renamed to defbase.pas because D6 contains a types
  259. unit so this would conflicts if D6 programms are compiled
  260. + Willamette/SSE2 instructions to assembler added
  261. Revision 1.20 2002/07/11 14:41:34 florian
  262. * start of the new generic parameter handling
  263. Revision 1.19 2002/07/07 09:52:34 florian
  264. * powerpc target fixed, very simple units can be compiled
  265. * some basic stuff for better callparanode handling, far from being finished
  266. Revision 1.18 2002/07/01 18:46:33 peter
  267. * internal linker
  268. * reorganized aasm layer
  269. Revision 1.17 2002/05/18 13:34:25 peter
  270. * readded missing revisions
  271. Revision 1.16 2002/05/16 19:46:52 carl
  272. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  273. + try to fix temp allocation (still in ifdef)
  274. + generic constructor calls
  275. + start of tassembler / tmodulebase class cleanup
  276. Revision 1.14 2002/05/13 19:54:38 peter
  277. * removed n386ld and n386util units
  278. * maybe_save/maybe_restore added instead of the old maybe_push
  279. Revision 1.13 2002/05/12 16:53:17 peter
  280. * moved entry and exitcode to ncgutil and cgobj
  281. * foreach gets extra argument for passing local data to the
  282. iterator function
  283. * -CR checks also class typecasts at runtime by changing them
  284. into as
  285. * fixed compiler to cycle with the -CR option
  286. * fixed stabs with elf writer, finally the global variables can
  287. be watched
  288. * removed a lot of routines from cga unit and replaced them by
  289. calls to cgobj
  290. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  291. u32bit then the other is typecasted also to u32bit without giving
  292. a rangecheck warning/error.
  293. * fixed pascal calling method with reversing also the high tree in
  294. the parast, detected by tcalcst3 test
  295. Revision 1.12 2002/04/25 20:16:40 peter
  296. * moved more routines from cga/n386util
  297. Revision 1.11 2002/04/21 15:36:40 carl
  298. * changeregsize -> rg.makeregsize
  299. Revision 1.10 2002/04/15 19:44:21 peter
  300. * fixed stackcheck that would be called recursively when a stack
  301. error was found
  302. * generic changeregsize(reg,size) for i386 register resizing
  303. * removed some more routines from cga unit
  304. * fixed returnvalue handling
  305. * fixed default stacksize of linux and go32v2, 8kb was a bit small :-)
  306. Revision 1.9 2002/04/04 19:06:12 peter
  307. * removed unused units
  308. * use tlocation.size in cg.a_*loc*() routines
  309. Revision 1.8 2002/04/02 17:11:36 peter
  310. * tlocation,treference update
  311. * LOC_CONSTANT added for better constant handling
  312. * secondadd splitted in multiple routines
  313. * location_force_reg added for loading a location to a register
  314. of a specified size
  315. * secondassignment parses now first the right and then the left node
  316. (this is compatible with Kylix). This saves a lot of push/pop especially
  317. with string operations
  318. * adapted some routines to use the new cg methods
  319. Revision 1.7 2002/03/31 20:26:39 jonas
  320. + a_loadfpu_* and a_loadmm_* methods in tcg
  321. * register allocation is now handled by a class and is mostly processor
  322. independent (+rgobj.pas and i386/rgcpu.pas)
  323. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  324. * some small improvements and fixes to the optimizer
  325. * some register allocation fixes
  326. * some fpuvaroffset fixes in the unary minus node
  327. * push/popusedregisters is now called rg.save/restoreusedregisters and
  328. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  329. also better optimizable)
  330. * fixed and optimized register saving/restoring for new/dispose nodes
  331. * LOC_FPU locations now also require their "register" field to be set to
  332. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  333. - list field removed of the tnode class because it's not used currently
  334. and can cause hard-to-find bugs
  335. }