pass_2.pas 17 KB

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