pass_2.pas 17 KB

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