pass_2.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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,
  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. ('add-addn', {addn}
  52. 'add-muln', {muln}
  53. 'add-subn', {subn}
  54. 'moddiv-divn', {divn}
  55. 'add-symdifn', {symdifn}
  56. 'moddiv-modn', {modn}
  57. 'assignment', {assignn}
  58. 'load', {loadn}
  59. 'nothing-range', {range}
  60. 'add-ltn', {ltn}
  61. 'add-lten', {lten}
  62. 'add-gtn', {gtn}
  63. 'add-gten', {gten}
  64. 'add-equaln', {equaln}
  65. 'add-unequaln', {unequaln}
  66. 'in', {inn}
  67. 'add-orn', {orn}
  68. 'add-xorn', {xorn}
  69. 'shlshr-shrn', {shrn}
  70. 'shlshr-shln', {shln}
  71. 'add-slashn', {slashn}
  72. 'add-andn', {andn}
  73. 'subscriptn', {subscriptn}
  74. 'dderef', {derefn}
  75. 'addr', {addrn}
  76. 'doubleaddr', {doubleaddrn}
  77. 'ordconst', {ordconstn}
  78. 'typeconv', {typeconvn}
  79. 'calln', {calln}
  80. 'noth-callpar', {callparan}
  81. 'realconst', {realconstn}
  82. 'unaryminus', {unaryminusn}
  83. 'asm', {asmn}
  84. 'vecn', {vecn}
  85. 'pointerconst', {pointerconstn}
  86. 'stringconst', {stringconstn}
  87. 'funcret', {funcretn}
  88. 'selfn', {selfn}
  89. 'not', {notn}
  90. 'inline', {inlinen}
  91. 'niln', {niln}
  92. 'error', {errorn}
  93. 'nothing-typen', {typen}
  94. 'hnewn', {hnewn}
  95. 'hdisposen', {hdisposen}
  96. 'setelement', {setelementn}
  97. 'setconst', {setconstn}
  98. 'blockn', {blockn}
  99. 'statement', {statementn}
  100. 'nothing-loopn', {loopn}
  101. 'ifn', {ifn}
  102. 'breakn', {breakn}
  103. 'continuen', {continuen}
  104. (* '_while_REPEAT', {repeatn}
  105. '_WHILE_repeat', {whilen}*)
  106. 'while_repeat', {whilerepeatn}
  107. 'for', {forn}
  108. 'exitn', {exitn}
  109. 'with', {withn}
  110. 'case', {casen}
  111. 'label', {labeln}
  112. 'goto', {goton}
  113. 'tryexcept', {tryexceptn}
  114. 'raise', {raisen}
  115. 'tryfinally', {tryfinallyn}
  116. 'on', {onn}
  117. 'is', {isn}
  118. 'as', {asn}
  119. 'error-caret', {caretn}
  120. 'fail', {failn}
  121. 'add-starstar', {starstarn}
  122. 'procinline', {procinlinen}
  123. 'arrayconstruc', {arrayconstructn}
  124. 'noth-arrcnstr', {arrayconstructrangen}
  125. 'tempn',
  126. 'temprefn',
  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_asm_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. oldloc : tloc;
  153. {$endif EXTDEBUG}
  154. begin
  155. if not(nf_error in p.flags) then
  156. begin
  157. oldcodegenerror:=codegenerror;
  158. oldlocalswitches:=aktlocalswitches;
  159. oldpos:=aktfilepos;
  160. {$ifdef TEMPREGDEBUG}
  161. testregisters32;
  162. prevp:=curptree;
  163. curptree:=@p;
  164. p^.usableregs:=usablereg32;
  165. {$endif TEMPREGDEBUG}
  166. aktfilepos:=p.fileinfo;
  167. aktlocalswitches:=p.localswitches;
  168. codegenerror:=false;
  169. {$ifdef EXTDEBUG}
  170. oldloc:=p.location.loc;
  171. p.location.loc:=LOC_INVALID;
  172. if (cs_asm_nodes in aktglobalswitches) then
  173. logsecond(p.nodetype,true);
  174. {$endif EXTDEBUG}
  175. p.pass_2;
  176. {$ifdef EXTDEBUG}
  177. if (cs_asm_nodes in aktglobalswitches) then
  178. logsecond(p.nodetype,false);
  179. if (not codegenerror) and
  180. (oldloc<>LOC_INVALID) and
  181. (p.location.loc=LOC_INVALID) then
  182. Comment(V_Fatal,'Location not set in secondpass: '+nodetype2str[p.nodetype]);
  183. {$endif EXTDEBUG}
  184. if codegenerror then
  185. include(p.flags,nf_error);
  186. codegenerror:=codegenerror or oldcodegenerror;
  187. aktlocalswitches:=oldlocalswitches;
  188. aktfilepos:=oldpos;
  189. {$ifdef TEMPREGDEBUG}
  190. curptree:=prevp;
  191. {$endif TEMPREGDEBUG}
  192. {$ifdef EXTTEMPREGDEBUG}
  193. if p.usableregs-usablereg32>p.reallyusedregs then
  194. p.reallyusedregs:=p.usableregs-usablereg32;
  195. if p.reallyusedregs<p.registers32 then
  196. Comment(V_Debug,'registers32 overestimated '+tostr(p^.registers32)+
  197. '>'+tostr(p^.reallyusedregs));
  198. {$endif EXTTEMPREGDEBUG}
  199. end
  200. else
  201. codegenerror:=true;
  202. end;
  203. function do_secondpass(var p : tnode) : boolean;
  204. begin
  205. codegenerror:=false;
  206. if not(nf_error in p.flags) then
  207. secondpass(p);
  208. do_secondpass:=codegenerror;
  209. end;
  210. procedure clearrefs(p : tnamedindexitem;arg:pointer);
  211. begin
  212. if (tsym(p).typ=varsym) then
  213. if tvarsym(p).refs>1 then
  214. tvarsym(p).refs:=1;
  215. end;
  216. procedure generatecode(var p : tnode);
  217. begin
  218. rg.cleartempgen;
  219. flowcontrol:=[];
  220. { when size optimization only count occurrence }
  221. if cs_littlesize in aktglobalswitches then
  222. rg.t_times:=1
  223. else
  224. { reference for repetition is 100 }
  225. rg.t_times:=100;
  226. { clear register count }
  227. rg.clearregistercount;
  228. use_esp_stackframe:=false;
  229. symtablestack.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  230. symtablestack.next.foreach_static({$ifdef FPCPROCVAR}@{$endif}clearrefs,nil);
  231. { firstpass everything }
  232. do_firstpass(p);
  233. { only do secondpass if there are no errors }
  234. if ErrorCount=0 then
  235. begin
  236. {$ifdef OMITSTACKFRAME}
  237. if (cs_regalloc in aktglobalswitches) and
  238. ((procinfo^.flags and (pi_uses_asm or pi_uses_exceptions))=0) then
  239. begin
  240. { can we omit the stack frame ? }
  241. { conditions:
  242. 1. procedure (not main block)
  243. 2. no constructor or destructor
  244. 3. no call to other procedures
  245. 4. no interrupt handler
  246. }
  247. {!!!!!! this doesn work yet, because of problems with
  248. with linux and windows
  249. }
  250. (*
  251. if assigned(aktprocsym) then
  252. begin
  253. if not(assigned(procinfo^._class)) and
  254. not(aktprocdef.proctypeoption in [potype_constructor,potype_destructor]) and
  255. not(po_interrupt in aktprocdef.procoptions) and
  256. ((procinfo^.flags and pi_do_call)=0) and
  257. (lexlevel>=normal_function_level) then
  258. begin
  259. { use ESP as frame pointer }
  260. procinfo^.framepointer:=STACK_POINTER_REG;
  261. use_esp_stackframe:=true;
  262. { calc parameter distance new }
  263. dec(procinfo^.framepointer_offset,4);
  264. dec(procinfo^.selfpointer_offset,4);
  265. { is this correct ???}
  266. { retoffset can be negativ for results in eax !! }
  267. { the value should be decreased only if positive }
  268. if procinfo^.retoffset>=0 then
  269. dec(procinfo^.retoffset,4);
  270. dec(procinfo^.para_offset,4);
  271. aktprocdef.parast.address_fixup:=procinfo^.para_offset;
  272. end;
  273. end;
  274. *)
  275. end;
  276. {$endif OMITSTACKFRAME}
  277. { process register variable stuff (JM) }
  278. assign_regvars(p);
  279. load_regvars(procinfo^.aktentrycode,p);
  280. { for the i386 it must be done in genexitcode because it has }
  281. { to add 'fstp' instructions when using fpu regvars and those }
  282. { must come after the "exitlabel" (JM) }
  283. {$ifndef i386}
  284. cleanup_regvars(procinfo^.aktexitcode);
  285. {$endif i386}
  286. do_secondpass(p);
  287. if assigned(procinfo^.procdef) then
  288. procinfo^.procdef.fpu_used:=p.registersfpu;
  289. end;
  290. procinfo^.aktproccode.concatlist(exprasmlist);
  291. end;
  292. end.
  293. {
  294. $Log$
  295. Revision 1.32 2002-07-19 11:41:36 daniel
  296. * State tracker work
  297. * The whilen and repeatn are now completely unified into whilerepeatn. This
  298. allows the state tracker to change while nodes automatically into
  299. repeat nodes.
  300. * Resulttypepass improvements to the notn. 'not not a' is optimized away and
  301. 'not(a>b)' is optimized into 'a<=b'.
  302. * Resulttypepass improvements to the whilerepeatn. 'while not a' is optimized
  303. by removing the notn and later switchting the true and falselabels. The
  304. same is done with 'repeat until not a'.
  305. Revision 1.31 2002/07/01 18:46:25 peter
  306. * internal linker
  307. * reorganized aasm layer
  308. Revision 1.30 2002/05/18 13:34:11 peter
  309. * readded missing revisions
  310. Revision 1.29 2002/05/16 19:46:42 carl
  311. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  312. + try to fix temp allocation (still in ifdef)
  313. + generic constructor calls
  314. + start of tassembler / tmodulebase class cleanup
  315. Revision 1.27 2002/05/12 16:53:08 peter
  316. * moved entry and exitcode to ncgutil and cgobj
  317. * foreach gets extra argument for passing local data to the
  318. iterator function
  319. * -CR checks also class typecasts at runtime by changing them
  320. into as
  321. * fixed compiler to cycle with the -CR option
  322. * fixed stabs with elf writer, finally the global variables can
  323. be watched
  324. * removed a lot of routines from cga unit and replaced them by
  325. calls to cgobj
  326. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  327. u32bit then the other is typecasted also to u32bit without giving
  328. a rangecheck warning/error.
  329. * fixed pascal calling method with reversing also the high tree in
  330. the parast, detected by tcalcst3 test
  331. Revision 1.26 2002/04/21 19:02:04 peter
  332. * removed newn and disposen nodes, the code is now directly
  333. inlined from pexpr
  334. * -an option that will write the secondpass nodes to the .s file, this
  335. requires EXTDEBUG define to actually write the info
  336. * fixed various internal errors and crashes due recent code changes
  337. Revision 1.25 2002/04/20 21:32:24 carl
  338. + generic FPC_CHECKPOINTER
  339. + first parameter offset in stack now portable
  340. * rename some constants
  341. + move some cpu stuff to other units
  342. - remove unused constents
  343. * fix stacksize for some targets
  344. * fix generic size problems which depend now on EXTEND_SIZE constant
  345. Revision 1.24 2002/04/07 13:30:13 carl
  346. - removed unused variable
  347. Revision 1.23 2002/04/02 17:11:29 peter
  348. * tlocation,treference update
  349. * LOC_CONSTANT added for better constant handling
  350. * secondadd splitted in multiple routines
  351. * location_force_reg added for loading a location to a register
  352. of a specified size
  353. * secondassignment parses now first the right and then the left node
  354. (this is compatible with Kylix). This saves a lot of push/pop especially
  355. with string operations
  356. * adapted some routines to use the new cg methods
  357. Revision 1.22 2002/03/31 20:26:35 jonas
  358. + a_loadfpu_* and a_loadmm_* methods in tcg
  359. * register allocation is now handled by a class and is mostly processor
  360. independent (+rgobj.pas and i386/rgcpu.pas)
  361. * temp allocation is now handled by a class (+tgobj.pas, -i386\tgcpu.pas)
  362. * some small improvements and fixes to the optimizer
  363. * some register allocation fixes
  364. * some fpuvaroffset fixes in the unary minus node
  365. * push/popusedregisters is now called rg.save/restoreusedregisters and
  366. (for i386) uses temps instead of push/pop's when using -Op3 (that code is
  367. also better optimizable)
  368. * fixed and optimized register saving/restoring for new/dispose nodes
  369. * LOC_FPU locations now also require their "register" field to be set to
  370. R_ST, not R_ST0 (the latter is used for LOC_CFPUREGISTER locations only)
  371. - list field removed of the tnode class because it's not used currently
  372. and can cause hard-to-find bugs
  373. }