pass_2.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit handles the codegeneration pass
  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 pass_2;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. type
  24. tenumflowcontrol = (fc_exit,fc_break,fc_continue);
  25. tflowcontrol = set of tenumflowcontrol;
  26. var
  27. allow_multi_pass2 : boolean;
  28. flowcontrol : tflowcontrol;
  29. { produces the actual code }
  30. function do_secondpass(var p : tnode) : boolean;
  31. procedure secondpass(var p : tnode);
  32. implementation
  33. uses
  34. {$ifdef EXTDEBUG}
  35. cutils,
  36. {$endif}
  37. globtype,systems,verbose,
  38. globals,
  39. paramgr,
  40. aasmtai,
  41. cgbase,
  42. nflw,cgobj;
  43. {*****************************************************************************
  44. SecondPass
  45. *****************************************************************************}
  46. {$ifdef EXTDEBUG}
  47. procedure logsecond(ht:tnodetype; entry: boolean);
  48. const
  49. secondnames: array[tnodetype] of string[13] =
  50. ('<emptynode>',
  51. 'add-addn', {addn}
  52. 'add-muln', {muln}
  53. 'add-subn', {subn}
  54. 'moddiv-divn', {divn}
  55. 'add-symdifn', {symdifn}
  56. 'moddiv-modn', {modn}
  57. 'assignment', {assignn}
  58. 'load', {loadn}
  59. 'nothing-range', {range}
  60. 'add-ltn', {ltn}
  61. 'add-lten', {lten}
  62. 'add-gtn', {gtn}
  63. 'add-gten', {gten}
  64. 'add-equaln', {equaln}
  65. 'add-unequaln', {unequaln}
  66. 'in', {inn}
  67. 'add-orn', {orn}
  68. 'add-xorn', {xorn}
  69. 'shlshr-shrn', {shrn}
  70. 'shlshr-shln', {shln}
  71. 'add-slashn', {slashn}
  72. 'add-andn', {andn}
  73. 'subscriptn', {subscriptn}
  74. 'deref', {derefn}
  75. 'addr', {addrn}
  76. 'ordconst', {ordconstn}
  77. 'typeconv', {typeconvn}
  78. 'calln', {calln}
  79. 'noth-callpar',{callparan}
  80. 'realconst', {realconstn}
  81. 'unaryminus', {unaryminusn}
  82. 'asm', {asmn}
  83. 'vecn', {vecn}
  84. 'pointerconst',{pointerconstn}
  85. 'stringconst', {stringconstn}
  86. 'not', {notn}
  87. 'inline', {inlinen}
  88. 'niln', {niln}
  89. 'error', {errorn}
  90. 'nothing-typen', {typen}
  91. 'setelement', {setelementn}
  92. 'setconst', {setconstn}
  93. 'blockn', {blockn}
  94. 'statement', {statementn}
  95. 'ifn', {ifn}
  96. 'breakn', {breakn}
  97. 'continuen', {continuen}
  98. 'while_repeat', {whilerepeatn}
  99. 'for', {forn}
  100. 'exitn', {exitn}
  101. 'with', {withn}
  102. 'case', {casen}
  103. 'label', {labeln}
  104. 'goto', {goton}
  105. 'tryexcept', {tryexceptn}
  106. 'raise', {raisen}
  107. 'tryfinally', {tryfinallyn}
  108. 'on', {onn}
  109. 'is', {isn}
  110. 'as', {asn}
  111. 'error-caret', {caretn}
  112. 'add-starstar', {starstarn}
  113. 'arrayconstruc', {arrayconstructn}
  114. 'noth-arrcnstr', {arrayconstructrangen}
  115. 'tempcreaten',
  116. 'temprefn',
  117. 'tempdeleten',
  118. 'addoptn',
  119. 'nothing-nothg', {nothingn}
  120. 'loadvmt', {loadvmtn}
  121. 'guidconstn',
  122. 'rttin',
  123. 'loadparentfpn'
  124. );
  125. var
  126. p: pchar;
  127. begin
  128. if entry then
  129. p := strpnew('second '+secondnames[ht]+' (entry)')
  130. else
  131. p := strpnew('second '+secondnames[ht]+' (exit)');
  132. exprasmlist.concat(tai_comment.create(p));
  133. end;
  134. {$endif EXTDEBUG}
  135. procedure secondpass(var p : tnode);
  136. var
  137. oldcodegenerror : boolean;
  138. oldlocalswitches : tlocalswitches;
  139. oldpos : tfileposinfo;
  140. begin
  141. if not assigned(p) then
  142. internalerror(200208221);
  143. if not(nf_error in p.flags) then
  144. begin
  145. oldcodegenerror:=codegenerror;
  146. oldlocalswitches:=aktlocalswitches;
  147. oldpos:=aktfilepos;
  148. if not inlining_procedure then
  149. aktfilepos:=p.fileinfo;
  150. aktlocalswitches:=p.localswitches;
  151. codegenerror:=false;
  152. {$ifdef EXTDEBUG}
  153. if (p.expectloc=LOC_INVALID) then
  154. Comment(V_Warning,'ExpectLoc is not set before secondpass: '+nodetype2str[p.nodetype]);
  155. if (not allow_multi_pass2) and
  156. (p.location.loc<>LOC_INVALID) then
  157. Comment(V_Warning,'Location.Loc is already set before secondpass: '+nodetype2str[p.nodetype]);
  158. if (cs_asm_nodes in aktglobalswitches) then
  159. logsecond(p.nodetype,true);
  160. {$endif EXTDEBUG}
  161. p.pass_2;
  162. {$ifdef EXTDEBUG}
  163. if (cs_asm_nodes in aktglobalswitches) then
  164. logsecond(p.nodetype,false);
  165. if (not codegenerror) then
  166. begin
  167. if (p.location.loc=LOC_INVALID) then
  168. Comment(V_Warning,'Location not set in secondpass: '+nodetype2str[p.nodetype])
  169. else if (p.location.loc<>p.expectloc) then
  170. Comment(V_Warning,'Location is different in secondpass: '+nodetype2str[p.nodetype]);
  171. end;
  172. {$endif EXTDEBUG}
  173. if codegenerror then
  174. include(p.flags,nf_error);
  175. codegenerror:=codegenerror or oldcodegenerror;
  176. aktlocalswitches:=oldlocalswitches;
  177. aktfilepos:=oldpos;
  178. end
  179. else
  180. codegenerror:=true;
  181. end;
  182. function do_secondpass(var p : tnode) : boolean;
  183. begin
  184. codegenerror:=false;
  185. if not(nf_error in p.flags) then
  186. secondpass(p);
  187. do_secondpass:=codegenerror;
  188. end;
  189. end.
  190. {
  191. $Log$
  192. Revision 1.74 2003-11-10 22:02:52 peter
  193. * cross unit inlining fixed
  194. Revision 1.73 2003/10/30 16:22:40 peter
  195. * call firstpass before allocation and codegeneration is started
  196. * move leftover code from pass_2.generatecode() to psub
  197. Revision 1.72 2003/10/19 01:34:30 florian
  198. * some ppc stuff fixed
  199. * memory leak fixed
  200. Revision 1.71 2003/10/18 15:41:26 peter
  201. * made worklists dynamic in size
  202. Revision 1.70 2003/10/17 15:08:34 peter
  203. * commented out more obsolete constants
  204. Revision 1.69 2003/10/10 17:48:13 peter
  205. * old trgobj moved to x86/rgcpu and renamed to trgx86fpu
  206. * tregisteralloctor renamed to trgobj
  207. * removed rgobj from a lot of units
  208. * moved location_* and reference_* to cgobj
  209. * first things for mmx register allocation
  210. Revision 1.68 2003/10/09 21:31:37 daniel
  211. * Register allocator splitted, ans abstract now
  212. Revision 1.67 2003/10/01 20:34:49 peter
  213. * procinfo unit contains tprocinfo
  214. * cginfo renamed to cgbase
  215. * moved cgmessage to verbose
  216. * fixed ppc and sparc compiles
  217. Revision 1.66 2003/09/28 21:45:52 peter
  218. * fix register leak in with debug
  219. Revision 1.65 2003/09/28 17:55:04 peter
  220. * parent framepointer changed to hidden parameter
  221. * tloadparentfpnode added
  222. Revision 1.64 2003/09/03 15:55:01 peter
  223. * NEWRA branch merged
  224. Revision 1.63.2.3 2003/08/31 15:46:26 peter
  225. * more updates for tregister
  226. Revision 1.63.2.2 2003/08/31 13:50:16 daniel
  227. * Remove sorting and use pregenerated indexes
  228. * Some work on making things compile
  229. Revision 1.63.2.1 2003/08/29 17:28:59 peter
  230. * next batch of updates
  231. Revision 1.63 2003/08/11 21:18:20 peter
  232. * start of sparc support for newra
  233. Revision 1.62 2003/08/10 17:25:23 peter
  234. * fixed some reported bugs
  235. Revision 1.61 2003/07/06 17:58:22 peter
  236. * framepointer fixes for sparc
  237. * parent framepointer code more generic
  238. Revision 1.60 2003/07/06 15:31:20 daniel
  239. * Fixed register allocator. *Lots* of fixes.
  240. Revision 1.59 2003/07/06 10:18:47 jonas
  241. * also generate the caller paraloc info of a procedure if it doesn't exist
  242. yet at the start of pass_2
  243. Revision 1.58 2003/07/05 20:13:03 jonas
  244. * create_paraloc_info() is now called separately for the caller and
  245. callee info
  246. * fixed ppc cycle
  247. Revision 1.57 2003/06/13 21:19:30 peter
  248. * current_procdef removed, use current_procinfo.procdef instead
  249. Revision 1.56 2003/06/12 16:43:07 peter
  250. * newra compiles for sparc
  251. Revision 1.55 2003/06/09 12:23:30 peter
  252. * init/final of procedure data splitted from genentrycode
  253. * use asmnode getposition to insert final at the correct position
  254. als for the implicit try...finally
  255. Revision 1.54 2003/06/03 13:01:59 daniel
  256. * Register allocator finished
  257. Revision 1.53 2003/05/26 21:17:17 peter
  258. * procinlinenode removed
  259. * aktexit2label removed, fast exit removed
  260. + tcallnode.inlined_pass_2 added
  261. Revision 1.52 2003/05/15 18:58:53 peter
  262. * removed selfpointer_offset, vmtpointer_offset
  263. * tvarsym.adjusted_address
  264. * address in localsymtable is now in the real direction
  265. * removed some obsolete globals
  266. Revision 1.51 2003/05/13 19:14:41 peter
  267. * failn removed
  268. * inherited result code check moven to pexpr
  269. Revision 1.50 2003/05/09 17:47:02 peter
  270. * self moved to hidden parameter
  271. * removed hdisposen,hnewn,selfn
  272. Revision 1.49 2003/04/27 11:21:33 peter
  273. * aktprocdef renamed to current_procinfo.procdef
  274. * procinfo renamed to current_procinfo
  275. * procinfo will now be stored in current_module so it can be
  276. cleaned up properly
  277. * gen_main_procsym changed to create_main_proc and release_main_proc
  278. to also generate a tprocinfo structure
  279. * fixed unit implicit initfinal
  280. Revision 1.48 2003/04/27 07:29:50 peter
  281. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  282. a new procdef declaration
  283. * aktprocsym removed
  284. * lexlevel removed, use symtable.symtablelevel instead
  285. * implicit init/final code uses the normal genentry/genexit
  286. * funcret state checking updated for new funcret handling
  287. Revision 1.47 2003/04/25 20:59:33 peter
  288. * removed funcretn,funcretsym, function result is now in varsym
  289. and aliases for result and function name are added using absolutesym
  290. * vs_hidden parameter for funcret passed in parameter
  291. * vs_hidden fixes
  292. * writenode changed to printnode and released from extdebug
  293. * -vp option added to generate a tree.log with the nodetree
  294. * nicer printnode for statements, callnode
  295. Revision 1.46 2003/04/23 10:12:14 peter
  296. * allow multi pass2 changed to global boolean instead of node flag
  297. Revision 1.45 2003/04/22 23:50:23 peter
  298. * firstpass uses expectloc
  299. * checks if there are differences between the expectloc and
  300. location.loc from secondpass in EXTDEBUG
  301. Revision 1.44 2003/04/22 12:45:58 florian
  302. * fixed generic in operator code
  303. + added debug code to check if all scratch registers are released
  304. Revision 1.43 2003/01/09 20:40:59 daniel
  305. * Converted some code in cgx86.pas to new register numbering
  306. Revision 1.42 2003/01/09 15:49:56 daniel
  307. * Added register conversion
  308. Revision 1.41 2002/12/22 14:35:39 peter
  309. * removed Writeln
  310. Revision 1.40 2002/12/21 23:21:47 mazen
  311. + added support for the shift nodes
  312. + added debug output on screen with -an command line option
  313. Revision 1.39 2002/08/23 16:14:49 peter
  314. * tempgen cleanup
  315. * tt_noreuse temp type added that will be used in genentrycode
  316. Revision 1.38 2002/08/20 16:55:38 peter
  317. * don't write (stabs)line info when inlining a procedure
  318. Revision 1.37 2002/08/19 19:36:44 peter
  319. * More fixes for cross unit inlining, all tnodes are now implemented
  320. * Moved pocall_internconst to po_internconst because it is not a
  321. calling type at all and it conflicted when inlining of these small
  322. functions was requested
  323. Revision 1.36 2002/08/18 20:06:24 peter
  324. * inlining is now also allowed in interface
  325. * renamed write/load to ppuwrite/ppuload
  326. * tnode storing in ppu
  327. * nld,ncon,nbas are already updated for storing in ppu
  328. Revision 1.35 2002/08/17 09:23:38 florian
  329. * first part of procinfo rewrite
  330. Revision 1.34 2002/08/15 19:10:35 peter
  331. * first things tai,tnode storing in ppu
  332. Revision 1.33 2002/07/30 20:50:44 florian
  333. * the code generator knows now if parameters are in registers
  334. Revision 1.32 2002/07/19 11:41:36 daniel
  335. * State tracker work
  336. * The whilen and repeatn are now completely unified into whilerepeatn. This
  337. allows the state tracker to change while nodes automatically into
  338. repeat nodes.
  339. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  340. 'not(a>b)' is optimized into 'a<=b'.
  341. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  342. by removing the notn and later switchting the true and falselabels. The
  343. same is done with 'repeat until not a'.
  344. Revision 1.31 2002/07/01 18:46:25 peter
  345. * internal linker
  346. * reorganized aasm layer
  347. Revision 1.30 2002/05/18 13:34:11 peter
  348. * readded missing revisions
  349. Revision 1.29 2002/05/16 19:46:42 carl
  350. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  351. + try to fix temp allocation (still in ifdef)
  352. + generic constructor calls
  353. + start of tassembler / tmodulebase class cleanup
  354. Revision 1.27 2002/05/12 16:53:08 peter
  355. * moved entry and exitcode to ncgutil and cgobj
  356. * foreach gets extra argument for passing local data to the
  357. iterator function
  358. * -CR checks also class typecasts at runtime by changing them
  359. into as
  360. * fixed compiler to cycle with the -CR option
  361. * fixed stabs with elf writer, finally the global variables can
  362. be watched
  363. * removed a lot of routines from cga unit and replaced them by
  364. calls to cgobj
  365. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  366. u32bit then the other is typecasted also to u32bit without giving
  367. a rangecheck warning/error.
  368. * fixed pascal calling method with reversing also the high tree in
  369. the parast, detected by tcalcst3 test
  370. Revision 1.26 2002/04/21 19:02:04 peter
  371. * removed newn and disposen nodes, the code is now directly
  372. inlined from pexpr
  373. * -an option that will write the secondpass nodes to the .s file, this
  374. requires EXTDEBUG define to actually write the info
  375. * fixed various internal errors and crashes due recent code changes
  376. Revision 1.25 2002/04/20 21:32:24 carl
  377. + generic FPC_CHECKPOINTER
  378. + first parameter offset in stack now portable
  379. * rename some constants
  380. + move some cpu stuff to other units
  381. - remove unused constents
  382. * fix stacksize for some targets
  383. * fix generic size problems which depend now on EXTEND_SIZE constant
  384. Revision 1.24 2002/04/07 13:30:13 carl
  385. - removed unused variable
  386. Revision 1.23 2002/04/02 17:11:29 peter
  387. * tlocation,treference update
  388. * LOC_CONSTANT added for better constant handling
  389. * secondadd splitted in multiple routines
  390. * location_force_reg added for loading a location to a register
  391. of a specified size
  392. * secondassignment parses now first the right and then the left node
  393. (this is compatible with Kylix). This saves a lot of push/pop especially
  394. with string operations
  395. * adapted some routines to use the new cg methods
  396. Revision 1.22 2002/03/31 20:26:35 jonas
  397. + a_loadfpu_* and a_loadmm_* methods in tcg
  398. * register allocation is now handled by a class and is mostly processor
  399. independent (+rgobj.pas and i386/rgcpu.pas)
  400. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  401. * some small improvements and fixes to the optimizer
  402. * some register allocation fixes
  403. * some fpuvaroffset fixes in the unary minus node
  404. * push/popusedregisters is now called rg.save/restoreusedregisters and
  405. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  406. also better optimizable)
  407. * fixed and optimized register saving/restoring for new/dispose nodes
  408. * LOC_FPU locations now also require their "register" field to be set to
  409. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  410. - list field removed of the tnode class because it's not used currently
  411. and can cause hard-to-find bugs
  412. }