pass_2.pas 19 KB

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