pass_2.pas 14 KB

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