pass_2.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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,aasm,
  42. pass_1,cpubase,cgbase,regvars,nflw,rgobj;
  43. {*****************************************************************************
  44. SecondPass
  45. *****************************************************************************}
  46. {$ifdef EXTDEBUG}
  47. procedure logsecond(ht:tnodetype; entry: boolean);
  48. const
  49. secondnames: array[tnodetype] of string[13] =
  50. ('add-addn', {addn}
  51. 'add-muln', {muln}
  52. 'add-subn', {subn}
  53. 'moddiv-divn', {divn}
  54. 'add-symdifn', {symdifn}
  55. 'moddiv-modn', {modn}
  56. 'assignment', {assignn}
  57. 'load', {loadn}
  58. 'nothing-range', {range}
  59. 'add-ltn', {ltn}
  60. 'add-lten', {lten}
  61. 'add-gtn', {gtn}
  62. 'add-gten', {gten}
  63. 'add-equaln', {equaln}
  64. 'add-unequaln', {unequaln}
  65. 'in', {inn}
  66. 'add-orn', {orn}
  67. 'add-xorn', {xorn}
  68. 'shlshr-shrn', {shrn}
  69. 'shlshr-shln', {shln}
  70. 'add-slashn', {slashn}
  71. 'add-andn', {andn}
  72. 'subscriptn', {subscriptn}
  73. 'dderef', {derefn}
  74. 'addr', {addrn}
  75. 'doubleaddr', {doubleaddrn}
  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. 'funcret', {funcretn}
  87. 'selfn', {selfn}
  88. 'not', {notn}
  89. 'inline', {inlinen}
  90. 'niln', {niln}
  91. 'error', {errorn}
  92. 'nothing-typen', {typen}
  93. 'hnewn', {hnewn}
  94. 'hdisposen', {hdisposen}
  95. 'setelement', {setelementn}
  96. 'setconst', {setconstn}
  97. 'blockn', {blockn}
  98. 'statement', {statementn}
  99. 'nothing-loopn', {loopn}
  100. 'ifn', {ifn}
  101. 'breakn', {breakn}
  102. 'continuen', {continuen}
  103. '_while_REPEAT', {repeatn}
  104. '_WHILE_repeat', {whilen}
  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. 'tempn',
  124. 'temprefn',
  125. 'addoptn',
  126. 'nothing-nothg', {nothingn}
  127. 'loadvmt', {loadvmtn}
  128. 'guidconstn',
  129. 'rttin'
  130. );
  131. var
  132. p: pchar;
  133. begin
  134. if entry then
  135. p := strpnew('second'+secondnames[ht]+' (entry)')
  136. else
  137. p := strpnew('second'+secondnames[ht]+' (exit)');
  138. exprasmlist.concat(tai_asm_comment.create(p));
  139. end;
  140. {$endif EXTDEBUG}
  141. procedure secondpass(var p : tnode);
  142. var
  143. oldcodegenerror : boolean;
  144. oldlocalswitches : tlocalswitches;
  145. oldpos : tfileposinfo;
  146. {$ifdef TEMPREGDEBUG}
  147. prevp : pptree;
  148. {$endif TEMPREGDEBUG}
  149. {$ifdef EXTDEBUG}
  150. oldloc : tloc;
  151. {$endif EXTDEBUG}
  152. begin
  153. if not(nf_error in p.flags) then
  154. begin
  155. oldcodegenerror:=codegenerror;
  156. oldlocalswitches:=aktlocalswitches;
  157. oldpos:=aktfilepos;
  158. {$ifdef TEMPREGDEBUG}
  159. testregisters32;
  160. prevp:=curptree;
  161. curptree:=@p;
  162. p^.usableregs:=usablereg32;
  163. {$endif TEMPREGDEBUG}
  164. aktfilepos:=p.fileinfo;
  165. aktlocalswitches:=p.localswitches;
  166. codegenerror:=false;
  167. {$ifdef EXTDEBUG}
  168. oldloc:=p.location.loc;
  169. p.location.loc:=LOC_INVALID;
  170. if (cs_asm_nodes in aktglobalswitches) then
  171. logsecond(p.nodetype,true);
  172. {$endif EXTDEBUG}
  173. p.pass_2;
  174. {$ifdef EXTDEBUG}
  175. if (cs_asm_nodes in aktglobalswitches) then
  176. logsecond(p.nodetype,false);
  177. if (not codegenerror) and
  178. (oldloc<>LOC_INVALID) and
  179. (p.location.loc=LOC_INVALID) then
  180. Comment(V_Fatal,'Location not set in secondpass: '+nodetype2str[p.nodetype]);
  181. {$endif EXTDEBUG}
  182. if codegenerror then
  183. include(p.flags,nf_error);
  184. codegenerror:=codegenerror or oldcodegenerror;
  185. aktlocalswitches:=oldlocalswitches;
  186. aktfilepos:=oldpos;
  187. {$ifdef TEMPREGDEBUG}
  188. curptree:=prevp;
  189. {$endif TEMPREGDEBUG}
  190. {$ifdef EXTTEMPREGDEBUG}
  191. if p.usableregs-usablereg32>p.reallyusedregs then
  192. p.reallyusedregs:=p.usableregs-usablereg32;
  193. if p.reallyusedregs<p.registers32 then
  194. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  195. '>'+tostr(p^.reallyusedregs));
  196. {$endif EXTTEMPREGDEBUG}
  197. end
  198. else
  199. codegenerror:=true;
  200. end;
  201. function do_secondpass(var p : tnode) : boolean;
  202. begin
  203. codegenerror:=false;
  204. if not(nf_error in p.flags) then
  205. secondpass(p);
  206. do_secondpass:=codegenerror;
  207. end;
  208. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  209. begin
  210. if (tsym(p).typ=varsym) then
  211. if tvarsym(p).refs>1 then
  212. tvarsym(p).refs:=1;
  213. end;
  214. procedure generatecode(var p : tnode);
  215. begin
  216. rg.cleartempgen;
  217. flowcontrol:=[];
  218. { when size optimization only count occurrence }
  219. if cs_littlesize in aktglobalswitches then
  220. rg.t_times:=1
  221. else
  222. { reference for repetition is 100 }
  223. rg.t_times:=100;
  224. { clear register count }
  225. rg.clearregistercount;
  226. use_esp_stackframe:=false;
  227. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  228. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  229. { firstpass everything }
  230. do_firstpass(p);
  231. { only do secondpass if there are no errors }
  232. if ErrorCount=0 then
  233. begin
  234. {$ifdef OMITSTACKFRAME}
  235. if (cs_regalloc in aktglobalswitches) and
  236. ((procinfo^.flags and (pi_uses_asm or pi_uses_exceptions))=0) then
  237. begin
  238. { can we omit the stack frame ? }
  239. { conditions:
  240. 1. procedure (not main block)
  241. 2. no constructor or destructor
  242. 3. no call to other procedures
  243. 4. no interrupt handler
  244. }
  245. {!!!!!! this doesn work yet, because of problems with
  246. with linux and windows
  247. }
  248. (*
  249. if assigned(aktprocsym) then
  250. begin
  251. if not(assigned(procinfo^._class)) and
  252. not(aktprocdef.proctypeoption in [potype_constructor,potype_destructor]) and
  253. not(po_interrupt in aktprocdef.procoptions) and
  254. ((procinfo^.flags and pi_do_call)=0) and
  255. (lexlevel>=normal_function_level) then
  256. begin
  257. { use ESP as frame pointer }
  258. procinfo^.framepointer:=STACK_POINTER_REG;
  259. use_esp_stackframe:=true;
  260. { calc parameter distance new }
  261. dec(procinfo^.framepointer_offset,4);
  262. dec(procinfo^.selfpointer_offset,4);
  263. { is this correct ???}
  264. { retoffset can be negativ for results in eax !! }
  265. { the value should be decreased only if positive }
  266. if procinfo^.retoffset>=0 then
  267. dec(procinfo^.retoffset,4);
  268. dec(procinfo^.para_offset,4);
  269. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  270. end;
  271. end;
  272. *)
  273. end;
  274. {$endif OMITSTACKFRAME}
  275. { process register variable stuff (JM) }
  276. assign_regvars(p);
  277. load_regvars(procinfo^.aktentrycode,p);
  278. { for the i386 it must be done in genexitcode because it has }
  279. { to add 'fstp' instructions when using fpu regvars and those }
  280. { must come after the "exitlabel" (JM) }
  281. {$ifndef i386}
  282. cleanup_regvars(procinfo^.aktexitcode);
  283. {$endif i386}
  284. do_secondpass(p);
  285. if assigned(procinfo^.procdef) then
  286. procinfo^.procdef.fpu_used:=p.registersfpu;
  287. end;
  288. procinfo^.aktproccode.concatlist(exprasmlist);
  289. end;
  290. end.
  291. {
  292. $Log$
  293. Revision 1.29 2002-05-16 19:46:42 carl
  294. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  295. + try to fix temp allocation (still in ifdef)
  296. + generic constructor calls
  297. + start of tassembler / tmodulebase class cleanup
  298. Revision 1.27 2002/05/12 16:53:08 peter
  299. * moved entry and exitcode to ncgutil and cgobj
  300. * foreach gets extra argument for passing local data to the
  301. iterator function
  302. * -CR checks also class typecasts at runtime by changing them
  303. into as
  304. * fixed compiler to cycle with the -CR option
  305. * fixed stabs with elf writer, finally the global variables can
  306. be watched
  307. * removed a lot of routines from cga unit and replaced them by
  308. calls to cgobj
  309. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  310. u32bit then the other is typecasted also to u32bit without giving
  311. a rangecheck warning/error.
  312. * fixed pascal calling method with reversing also the high tree in
  313. the parast, detected by tcalcst3 test
  314. Revision 1.26 2002/04/21 19:02:04 peter
  315. * removed newn and disposen nodes, the code is now directly
  316. inlined from pexpr
  317. * -an option that will write the secondpass nodes to the .s file, this
  318. requires EXTDEBUG define to actually write the info
  319. * fixed various internal errors and crashes due recent code changes
  320. Revision 1.25 2002/04/20 21:32:24 carl
  321. + generic FPC_CHECKPOINTER
  322. + first parameter offset in stack now portable
  323. * rename some constants
  324. + move some cpu stuff to other units
  325. - remove unused constents
  326. * fix stacksize for some targets
  327. * fix generic size problems which depend now on EXTEND_SIZE constant
  328. Revision 1.24 2002/04/07 13:30:13 carl
  329. - removed unused variable
  330. Revision 1.23 2002/04/02 17:11:29 peter
  331. * tlocation,treference update
  332. * LOC_CONSTANT added for better constant handling
  333. * secondadd splitted in multiple routines
  334. * location_force_reg added for loading a location to a register
  335. of a specified size
  336. * secondassignment parses now first the right and then the left node
  337. (this is compatible with Kylix). This saves a lot of push/pop especially
  338. with string operations
  339. * adapted some routines to use the new cg methods
  340. Revision 1.22 2002/03/31 20:26:35 jonas
  341. + a_loadfpu_* and a_loadmm_* methods in tcg
  342. * register allocation is now handled by a class and is mostly processor
  343. independent (+rgobj.pas and i386/rgcpu.pas)
  344. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  345. * some small improvements and fixes to the optimizer
  346. * some register allocation fixes
  347. * some fpuvaroffset fixes in the unary minus node
  348. * push/popusedregisters is now called rg.save/restoreusedregisters and
  349. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  350. also better optimizable)
  351. * fixed and optimized register saving/restoring for new/dispose nodes
  352. * LOC_FPU locations now also require their "register" field to be set to
  353. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  354. - list field removed of the tnode class because it's not used currently
  355. and can cause hard-to-find bugs
  356. Revision 1.21 2001/11/06 16:39:02 jonas
  357. * moved call to "cleanup_regvars" to cga.pas for i386 because it has
  358. to insert "fstp %st0" instructions after the exit label
  359. Revision 1.20 2001/11/02 22:58:02 peter
  360. * procsym definition rewrite
  361. Revision 1.19 2001/10/25 21:22:35 peter
  362. * calling convention rewrite
  363. Revision 1.18 2001/08/26 13:36:44 florian
  364. * some cg reorganisation
  365. * some PPC updates
  366. Revision 1.17 2001/08/06 21:40:47 peter
  367. * funcret moved from tprocinfo to tprocdef
  368. Revision 1.16 2001/05/09 19:57:07 peter
  369. * check for errorcount after firstpass
  370. Revision 1.15 2001/04/15 09:48:30 peter
  371. * fixed crash in labelnode
  372. * easier detection of goto and label in try blocks
  373. Revision 1.14 2001/04/13 01:22:10 peter
  374. * symtable change to classes
  375. * range check generation and errors fixed, make cycle DEBUG=1 works
  376. * memory leaks fixed
  377. Revision 1.13 2001/04/02 21:20:31 peter
  378. * resulttype rewrite
  379. Revision 1.12 2000/12/25 00:07:27 peter
  380. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  381. tlinkedlist objects)
  382. Revision 1.11 2000/11/29 00:30:35 florian
  383. * unused units removed from uses clause
  384. * some changes for widestrings
  385. Revision 1.10 2000/10/31 22:02:49 peter
  386. * symtable splitted, no real code changes
  387. Revision 1.9 2000/10/14 10:14:51 peter
  388. * moehrendorf oct 2000 rewrite
  389. Revision 1.8 2000/09/24 15:06:21 peter
  390. * use defines.inc
  391. Revision 1.7 2000/08/27 16:11:51 peter
  392. * moved some util functions from globals,cobjects to cutils
  393. * splitted files into finput,fmodule
  394. Revision 1.6 2000/08/12 15:34:22 peter
  395. + usedasmsymbollist to check and reset only the used symbols (merged)
  396. Revision 1.5 2000/08/03 13:17:25 jonas
  397. + allow regvars to be used inside inlined procs, which required the
  398. following changes:
  399. + load regvars in genentrycode/free them in genexitcode (cgai386)
  400. * moved all regvar related code to new regvars unit
  401. + added pregvarinfo type to hcodegen
  402. + added regvarinfo field to tprocinfo (symdef/symdefh)
  403. * deallocate the regvars of the caller in secondprocinline before
  404. inlining the called procedure and reallocate them afterwards
  405. Revision 1.4 2000/08/03 11:15:42 jonas
  406. - disable regvars for inlined procedures (merged from fixes branch)
  407. Revision 1.3 2000/07/21 15:14:02 jonas
  408. + added is_addr field for labels, if they are only used for getting the address
  409. (e.g. for io checks) and corresponding getaddrlabel() procedure
  410. Revision 1.2 2000/07/13 11:32:44 michael
  411. + removed logs
  412. }