pass_1.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. {
  2. $Id$
  3. Copyright (c) 1996-98 by Florian Klaempfl
  4. This unit implements the first pass of the code generator
  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. {$ifdef tp}
  19. {$F+}
  20. {$endif tp}
  21. unit pass_1;
  22. interface
  23. uses
  24. tree;
  25. procedure firstpass(var p : ptree);
  26. function do_firstpass(var p : ptree) : boolean;
  27. implementation
  28. uses
  29. globtype,systems,
  30. cobjects,verbose,globals,
  31. aasm,symtable,types,
  32. hcodegen,htypechk,
  33. tcadd,tccal,tccnv,tccon,tcflw,
  34. tcinl,tcld,tcmat,tcmem,tcset
  35. {$ifdef i386}
  36. ,i386,tgeni386
  37. {$endif}
  38. {$ifdef m68k}
  39. ,m68k,tgen68k
  40. {$endif}
  41. ;
  42. {*****************************************************************************
  43. FirstPass
  44. *****************************************************************************}
  45. type
  46. firstpassproc = procedure(var p : ptree);
  47. procedure firstnothing(var p : ptree);
  48. begin
  49. p^.resulttype:=voiddef;
  50. end;
  51. procedure firsterror(var p : ptree);
  52. begin
  53. p^.error:=true;
  54. codegenerror:=true;
  55. p^.resulttype:=generrordef;
  56. end;
  57. procedure firststatement(var p : ptree);
  58. begin
  59. { left is the next statement in the list }
  60. p^.resulttype:=voiddef;
  61. { no temps over several statements }
  62. cleartempgen;
  63. { right is the statement itself calln assignn or a complex one }
  64. firstpass(p^.right);
  65. if (not (cs_extsyntax in aktmoduleswitches)) and
  66. assigned(p^.right^.resulttype) and
  67. (p^.right^.resulttype<>pdef(voiddef)) then
  68. CGMessage(cg_e_illegal_expression);
  69. if codegenerror then
  70. exit;
  71. p^.registers32:=p^.right^.registers32;
  72. p^.registersfpu:=p^.right^.registersfpu;
  73. {$ifdef SUPPORT_MMX}
  74. p^.registersmmx:=p^.right^.registersmmx;
  75. {$endif SUPPORT_MMX}
  76. { left is the next in the list }
  77. firstpass(p^.left);
  78. if codegenerror then
  79. exit;
  80. if p^.right^.registers32>p^.registers32 then
  81. p^.registers32:=p^.right^.registers32;
  82. if p^.right^.registersfpu>p^.registersfpu then
  83. p^.registersfpu:=p^.right^.registersfpu;
  84. {$ifdef SUPPORT_MMX}
  85. if p^.right^.registersmmx>p^.registersmmx then
  86. p^.registersmmx:=p^.right^.registersmmx;
  87. {$endif}
  88. end;
  89. procedure firstblock(var p : ptree);
  90. var
  91. hp : ptree;
  92. count : longint;
  93. begin
  94. count:=0;
  95. hp:=p^.left;
  96. while assigned(hp) do
  97. begin
  98. if cs_regalloc in aktglobalswitches then
  99. begin
  100. { Codeumstellungen }
  101. { Funktionsresultate an exit anh„ngen }
  102. { this is wrong for string or other complex
  103. result types !!! }
  104. if ret_in_acc(procinfo.retdef) and
  105. assigned(hp^.left) and
  106. (hp^.left^.right^.treetype=exitn) and
  107. (hp^.right^.treetype=assignn) and
  108. (hp^.right^.left^.treetype=funcretn) then
  109. begin
  110. if assigned(hp^.left^.right^.left) then
  111. CGMessage(cg_n_inefficient_code)
  112. else
  113. begin
  114. hp^.left^.right^.left:=getcopy(hp^.right^.right);
  115. disposetree(hp^.right);
  116. hp^.right:=nil;
  117. end;
  118. end
  119. { warning if unreachable code occurs and elimate this }
  120. else if (hp^.right^.treetype in
  121. [exitn,breakn,continuen,goton]) and
  122. assigned(hp^.left) and
  123. (hp^.left^.treetype<>labeln) then
  124. begin
  125. { use correct line number }
  126. aktfilepos:=hp^.left^.fileinfo;
  127. disposetree(hp^.left);
  128. hp^.left:=nil;
  129. CGMessage(cg_w_unreachable_code);
  130. { old lines }
  131. aktfilepos:=hp^.right^.fileinfo;
  132. end;
  133. end;
  134. if assigned(hp^.right) then
  135. begin
  136. cleartempgen;
  137. firstpass(hp^.right);
  138. if (not (cs_extsyntax in aktmoduleswitches)) and
  139. assigned(hp^.right^.resulttype) and
  140. (hp^.right^.resulttype<>pdef(voiddef)) then
  141. CGMessage(cg_e_illegal_expression);
  142. if codegenerror then
  143. exit;
  144. hp^.registers32:=hp^.right^.registers32;
  145. hp^.registersfpu:=hp^.right^.registersfpu;
  146. {$ifdef SUPPORT_MMX}
  147. hp^.registersmmx:=hp^.right^.registersmmx;
  148. {$endif SUPPORT_MMX}
  149. end
  150. else
  151. hp^.registers32:=0;
  152. if hp^.registers32>p^.registers32 then
  153. p^.registers32:=hp^.registers32;
  154. if hp^.registersfpu>p^.registersfpu then
  155. p^.registersfpu:=hp^.registersfpu;
  156. {$ifdef SUPPORT_MMX}
  157. if hp^.registersmmx>p^.registersmmx then
  158. p^.registersmmx:=hp^.registersmmx;
  159. {$endif}
  160. inc(count);
  161. hp:=hp^.left;
  162. end;
  163. end;
  164. procedure firstasm(var p : ptree);
  165. begin
  166. procinfo.flags:=procinfo.flags or pi_uses_asm;
  167. end;
  168. procedure firstpass(var p : ptree);
  169. const
  170. procedures : array[ttreetyp] of firstpassproc =
  171. (firstadd, {addn}
  172. firstadd, {muln}
  173. firstadd, {subn}
  174. firstmoddiv, {divn}
  175. firstadd, {symdifn}
  176. firstmoddiv, {modn}
  177. firstassignment, {assignn}
  178. firstload, {loadn}
  179. firstrange, {range}
  180. firstadd, {ltn}
  181. firstadd, {lten}
  182. firstadd, {gtn}
  183. firstadd, {gten}
  184. firstadd, {equaln}
  185. firstadd, {unequaln}
  186. firstin, {inn}
  187. firstadd, {orn}
  188. firstadd, {xorn}
  189. firstshlshr, {shrn}
  190. firstshlshr, {shln}
  191. firstadd, {slashn}
  192. firstadd, {andn}
  193. firstsubscript, {subscriptn}
  194. firstderef, {derefn}
  195. firstaddr, {addrn}
  196. firstdoubleaddr, {doubleaddrn}
  197. firstordconst, {ordconstn}
  198. firsttypeconv, {typeconvn}
  199. firstcalln, {calln}
  200. firstnothing, {callparan}
  201. firstrealconst, {realconstn}
  202. firstfixconst, {fixconstn}
  203. firstumminus, {umminusn}
  204. firstasm, {asmn}
  205. firstvec, {vecn}
  206. firststringconst, {stringconstn}
  207. firstfuncret, {funcretn}
  208. firstself, {selfn}
  209. firstnot, {notn}
  210. firstinline, {inlinen}
  211. firstniln, {niln}
  212. firsterror, {errorn}
  213. firsttype, {typen}
  214. firsthnew, {hnewn}
  215. firsthdispose, {hdisposen}
  216. firstnew, {newn}
  217. firstsimplenewdispose, {simpledisposen}
  218. firstsetelement, {setelementn}
  219. firstsetconst, {setconstn}
  220. firstblock, {blockn}
  221. firststatement, {statementn}
  222. firstnothing, {loopn}
  223. firstif, {ifn}
  224. firstnothing, {breakn}
  225. firstnothing, {continuen}
  226. first_while_repeat, {repeatn}
  227. first_while_repeat, {whilen}
  228. firstfor, {forn}
  229. firstexit, {exitn}
  230. firstwith, {withn}
  231. firstcase, {casen}
  232. firstlabel, {labeln}
  233. firstgoto, {goton}
  234. firstsimplenewdispose, {simplenewn}
  235. firsttryexcept, {tryexceptn}
  236. firstraise, {raisen}
  237. firstnothing, {switchesn}
  238. firsttryfinally, {tryfinallyn}
  239. firston, {onn}
  240. firstis, {isn}
  241. firstas, {asn}
  242. firsterror, {caretn}
  243. firstnothing, {failn}
  244. firstadd, {starstarn}
  245. firstprocinline, {procinlinen}
  246. firstarrayconstruct, {arrayconstructn}
  247. firstarrayconstructrange, {arrayconstructrangen}
  248. firstnothing, {nothingn}
  249. firstloadvmt {loadvmtn}
  250. );
  251. var
  252. oldcodegenerror : boolean;
  253. oldlocalswitches : tlocalswitches;
  254. oldpos : tfileposinfo;
  255. {$ifdef extdebug}
  256. str1,str2 : string;
  257. oldp : ptree;
  258. not_first : boolean;
  259. {$endif extdebug}
  260. begin
  261. {$ifdef extdebug}
  262. inc(total_of_firstpass);
  263. if (p^.firstpasscount>0) and only_one_pass then
  264. exit;
  265. {$endif extdebug}
  266. oldcodegenerror:=codegenerror;
  267. oldpos:=aktfilepos;
  268. oldlocalswitches:=aktlocalswitches;
  269. {$ifdef extdebug}
  270. if p^.firstpasscount>0 then
  271. begin
  272. move(p^,str1[1],sizeof(ttree));
  273. {$ifndef TP}
  274. {$ifopt H+}
  275. SetLength(str1,sizeof(ttree));
  276. {$else}
  277. str1[0]:=char(sizeof(ttree));
  278. {$endif}
  279. {$else}
  280. str1[0]:=char(sizeof(ttree));
  281. {$endif}
  282. new(oldp);
  283. oldp^:=p^;
  284. not_first:=true;
  285. inc(firstpass_several);
  286. end
  287. else
  288. not_first:=false;
  289. {$endif extdebug}
  290. if not p^.error then
  291. begin
  292. codegenerror:=false;
  293. aktfilepos:=p^.fileinfo;
  294. aktlocalswitches:=p^.localswitches;
  295. procedures[p^.treetype](p);
  296. aktlocalswitches:=oldlocalswitches;
  297. aktfilepos:=oldpos;
  298. p^.error:=codegenerror;
  299. codegenerror:=codegenerror or oldcodegenerror;
  300. end
  301. else
  302. codegenerror:=true;
  303. {$ifdef extdebug}
  304. if not_first then
  305. begin
  306. { dirty trick to compare two ttree's (PM) }
  307. move(p^,str2[1],sizeof(ttree));
  308. {$ifndef TP}
  309. {$ifopt H+}
  310. SetLength(str2,sizeof(ttree));
  311. {$else}
  312. str2[0]:=char(sizeof(ttree));
  313. {$endif}
  314. {$else}
  315. str2[0]:=char(sizeof(ttree));
  316. {$endif}
  317. if str1<>str2 then
  318. begin
  319. comment(v_debug,'tree changed after first counting pass '
  320. +tostr(longint(p^.treetype)));
  321. compare_trees(oldp,p);
  322. end;
  323. dispose(oldp);
  324. end;
  325. if count_ref then
  326. inc(p^.firstpasscount);
  327. {$endif extdebug}
  328. end;
  329. function do_firstpass(var p : ptree) : boolean;
  330. begin
  331. codegenerror:=false;
  332. firstpass(p);
  333. do_firstpass:=codegenerror;
  334. end;
  335. end.
  336. {
  337. $Log$
  338. Revision 1.99 1998-12-11 00:03:27 peter
  339. + globtype,tokens,version unit splitted from globals
  340. Revision 1.98 1998/11/23 17:49:03 pierre
  341. * ansistring support in extdebug code
  342. Revision 1.97 1998/11/05 14:26:47 peter
  343. * fixed variant warning with was sometimes said with sets
  344. Revision 1.96 1998/10/06 20:49:07 peter
  345. * m68k compiler compiles again
  346. Revision 1.95 1998/09/24 15:13:44 peter
  347. * fixed type node which was always set to void :(
  348. Revision 1.94 1998/09/23 20:42:22 peter
  349. * splitted pass_1
  350. }