pass_2.pas 16 KB

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