fppu.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements the first loading and searching of the modules
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit fppu;
  18. {$i fpcdefs.inc}
  19. { close ppufiles on system that are
  20. short on file handles like DOS system PM }
  21. {$ifdef GO32V2}
  22. {$define SHORT_ON_FILE_HANDLES}
  23. {$endif GO32V2}
  24. {$ifdef WATCOM}
  25. {$define SHORT_ON_FILE_HANDLES}
  26. {$endif WATCOM}
  27. interface
  28. uses
  29. cutils,cclasses,
  30. globtype,globals,finput,fmodule,
  31. symbase,ppu,symtype;
  32. type
  33. { tppumodule }
  34. tppumodule = class(tmodule)
  35. ppufile : tcompilerppufile; { the PPU file }
  36. sourcefn : pshortstring; { Source specified with "uses .. in '..'" }
  37. comments : TCmdStrList;
  38. {$ifdef Test_Double_checksum}
  39. crc_array : pointer;
  40. crc_size : longint;
  41. crc_array2 : pointer;
  42. crc_size2 : longint;
  43. {$endif def Test_Double_checksum}
  44. constructor create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  45. destructor destroy;override;
  46. procedure reset;override;
  47. function openppu:boolean;
  48. procedure getppucrc;
  49. procedure writeppu;
  50. procedure loadppu;
  51. function needrecompile:boolean;
  52. procedure setdefgeneration;
  53. procedure reload_flagged_units;
  54. private
  55. { Each time a unit's defs are (re)created, its defsgeneration is
  56. set to the value of a global counter, and the global counter is
  57. increased. We only reresolve its dependent units' defs in case
  58. they have been resolved only for an older generation, in order to
  59. avoid endless resolving loops in case of cyclic dependencies. }
  60. defsgeneration : longint;
  61. function search_unit_files(onlysource:boolean):boolean;
  62. function search_unit(onlysource,shortname:boolean):boolean;
  63. procedure load_interface;
  64. procedure load_implementation;
  65. procedure load_usedunits;
  66. procedure printcomments;
  67. procedure queuecomment(s:string;v,w:longint);
  68. procedure writesourcefiles;
  69. procedure writeusedunit(intf:boolean);
  70. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  71. procedure writederefmap;
  72. procedure writederefdata;
  73. procedure writeImportSymbols;
  74. procedure writeResources;
  75. procedure readsourcefiles;
  76. procedure readloadunit;
  77. procedure readlinkcontainer(var p:tlinkcontainer);
  78. procedure readderefmap;
  79. procedure readderefdata;
  80. procedure readImportSymbols;
  81. procedure readResources;
  82. procedure readwpofile;
  83. {$IFDEF MACRO_DIFF_HINT}
  84. procedure writeusedmacro(p:TNamedIndexItem;arg:pointer);
  85. procedure writeusedmacros;
  86. procedure readusedmacros;
  87. {$ENDIF}
  88. end;
  89. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  90. implementation
  91. uses
  92. SysUtils,
  93. cfileutl,
  94. verbose,systems,version,
  95. symtable, symsym,
  96. wpoinfo,
  97. scanner,
  98. aasmbase,ogbase,
  99. parser,
  100. comphook;
  101. var
  102. currentdefgeneration: longint;
  103. {****************************************************************************
  104. TPPUMODULE
  105. ****************************************************************************}
  106. constructor tppumodule.create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  107. begin
  108. inherited create(LoadedFrom,s,_is_unit);
  109. ppufile:=nil;
  110. sourcefn:=stringdup(fn);
  111. end;
  112. destructor tppumodule.Destroy;
  113. begin
  114. if assigned(ppufile) then
  115. ppufile.free;
  116. ppufile:=nil;
  117. comments.free;
  118. comments:=nil;
  119. stringdispose(sourcefn);
  120. inherited Destroy;
  121. end;
  122. procedure tppumodule.reset;
  123. begin
  124. inc(currentdefgeneration);
  125. if assigned(ppufile) then
  126. begin
  127. ppufile.free;
  128. ppufile:=nil;
  129. end;
  130. inherited reset;
  131. end;
  132. procedure tppumodule.queuecomment(s:string;v,w:longint);
  133. begin
  134. if comments = nil then
  135. comments := TCmdStrList.create;
  136. comments.insert(s);
  137. end;
  138. procedure tppumodule.printcomments;
  139. var
  140. comment: string;
  141. begin
  142. if comments = nil then
  143. exit;
  144. { comments are inserted in reverse order }
  145. repeat
  146. comment := comments.getlast;
  147. if length(comment) = 0 then
  148. exit;
  149. do_comment(v_normal, comment);
  150. until false;
  151. end;
  152. function tppumodule.openppu:boolean;
  153. var
  154. ppufiletime : longint;
  155. begin
  156. openppu:=false;
  157. Message1(unit_t_ppu_loading,ppufilename^,@queuecomment);
  158. { Get ppufile time (also check if the file exists) }
  159. ppufiletime:=getnamedfiletime(ppufilename^);
  160. if ppufiletime=-1 then
  161. exit;
  162. { Open the ppufile }
  163. Message1(unit_u_ppu_name,ppufilename^);
  164. ppufile:=tcompilerppufile.create(ppufilename^);
  165. if not ppufile.openfile then
  166. begin
  167. ppufile.free;
  168. ppufile:=nil;
  169. Message(unit_u_ppu_file_too_short);
  170. exit;
  171. end;
  172. { check for a valid PPU file }
  173. if not ppufile.CheckPPUId then
  174. begin
  175. ppufile.free;
  176. ppufile:=nil;
  177. Message(unit_u_ppu_invalid_header);
  178. exit;
  179. end;
  180. { check for allowed PPU versions }
  181. if not (ppufile.GetPPUVersion = CurrentPPUVersion) then
  182. begin
  183. Message1(unit_u_ppu_invalid_version,tostr(ppufile.GetPPUVersion),@queuecomment);
  184. ppufile.free;
  185. ppufile:=nil;
  186. exit;
  187. end;
  188. { check the target processor }
  189. if tsystemcpu(ppufile.header.cpu)<>target_cpu then
  190. begin
  191. ppufile.free;
  192. ppufile:=nil;
  193. Message(unit_u_ppu_invalid_processor,@queuecomment);
  194. exit;
  195. end;
  196. { check target }
  197. if tsystem(ppufile.header.target)<>target_info.system then
  198. begin
  199. ppufile.free;
  200. ppufile:=nil;
  201. Message(unit_u_ppu_invalid_target,@queuecomment);
  202. exit;
  203. end;
  204. {$ifdef cpufpemu}
  205. { check if floating point emulation is on?
  206. fpu emulation isn't unit levelwise because it affects calling convention }
  207. if ((ppufile.header.flags and uf_fpu_emulation)<>0) xor
  208. (cs_fp_emulation in current_settings.moduleswitches) then
  209. begin
  210. ppufile.free;
  211. ppufile:=nil;
  212. Message(unit_u_ppu_invalid_fpumode,@queuecomment);
  213. exit;
  214. end;
  215. {$endif cpufpemu}
  216. { Load values to be access easier }
  217. flags:=ppufile.header.flags;
  218. crc:=ppufile.header.checksum;
  219. interface_crc:=ppufile.header.interface_checksum;
  220. indirect_crc:=ppufile.header.indirect_checksum;
  221. { Show Debug info }
  222. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  223. Message1(unit_u_ppu_flags,tostr(flags));
  224. Message1(unit_u_ppu_crc,hexstr(ppufile.header.checksum,8));
  225. Message1(unit_u_ppu_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  226. Message1(unit_u_ppu_crc,hexstr(ppufile.header.indirect_checksum,8)+' (indc)');
  227. Comment(V_used,'Number of definitions: '+tostr(ppufile.header.deflistsize));
  228. Comment(V_used,'Number of symbols: '+tostr(ppufile.header.symlistsize));
  229. do_compile:=false;
  230. openppu:=true;
  231. end;
  232. function tppumodule.search_unit_files(onlysource:boolean):boolean;
  233. var
  234. found : boolean;
  235. begin
  236. found:=false;
  237. if search_unit(onlysource,false) then
  238. found:=true;
  239. if (not found) and
  240. (length(modulename^)>8) and
  241. search_unit(onlysource,true) then
  242. found:=true;
  243. search_unit_files:=found;
  244. end;
  245. function tppumodule.search_unit(onlysource,shortname:boolean):boolean;
  246. var
  247. singlepathstring,
  248. filename : TCmdStr;
  249. Function UnitExists(const ext:string;var foundfile:TCmdStr):boolean;
  250. begin
  251. if CheckVerbosity(V_Tried) then
  252. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  253. UnitExists:=FindFile(FileName+ext,Singlepathstring,true,foundfile);
  254. end;
  255. Function PPUSearchPath(const s:TCmdStr):boolean;
  256. var
  257. found : boolean;
  258. hs : TCmdStr;
  259. begin
  260. Found:=false;
  261. singlepathstring:=FixPath(s,false);
  262. { Check for PPU file }
  263. Found:=UnitExists(target_info.unitext,hs);
  264. if Found then
  265. Begin
  266. SetFileName(hs,false);
  267. Found:=OpenPPU;
  268. End;
  269. PPUSearchPath:=Found;
  270. end;
  271. Function SourceSearchPath(const s:TCmdStr):boolean;
  272. var
  273. found : boolean;
  274. hs : TCmdStr;
  275. begin
  276. Found:=false;
  277. singlepathstring:=FixPath(s,false);
  278. { Check for Sources }
  279. ppufile:=nil;
  280. do_compile:=true;
  281. recompile_reason:=rr_noppu;
  282. {Check for .pp file}
  283. Found:=UnitExists(sourceext,hs);
  284. if not Found then
  285. begin
  286. { Check for .pas }
  287. Found:=UnitExists(pasext,hs);
  288. end;
  289. if not Found and
  290. ((m_mac in current_settings.modeswitches) or
  291. (tf_p_ext_support in target_info.flags)) then
  292. begin
  293. { Check for .p, if mode is macpas}
  294. Found:=UnitExists(pext,hs);
  295. end;
  296. stringdispose(mainsource);
  297. if Found then
  298. begin
  299. sources_avail:=true;
  300. { Load Filenames when found }
  301. mainsource:=StringDup(hs);
  302. SetFileName(hs,false);
  303. end
  304. else
  305. sources_avail:=false;
  306. SourceSearchPath:=Found;
  307. end;
  308. Function SearchPath(const s:TCmdStr):boolean;
  309. var
  310. found : boolean;
  311. begin
  312. { First check for a ppu, then for the source }
  313. found:=false;
  314. if not onlysource then
  315. found:=PPUSearchPath(s);
  316. if not found then
  317. found:=SourceSearchPath(s);
  318. SearchPath:=found;
  319. end;
  320. Function SearchPathList(list:TSearchPathList):boolean;
  321. var
  322. hp : TCmdStrListItem;
  323. found : boolean;
  324. begin
  325. found:=false;
  326. hp:=TCmdStrListItem(list.First);
  327. while assigned(hp) do
  328. begin
  329. found:=SearchPath(hp.Str);
  330. if found then
  331. break;
  332. hp:=TCmdStrListItem(hp.next);
  333. end;
  334. SearchPathList:=found;
  335. end;
  336. var
  337. fnd : boolean;
  338. hs : TCmdStr;
  339. begin
  340. if shortname then
  341. filename:=FixFileName(Copy(realmodulename^,1,8))
  342. else
  343. filename:=FixFileName(realmodulename^);
  344. { try to find unit
  345. 1. look for ppu in cwd
  346. 2. look for ppu in outputpath if set, this is tp7 compatible (PFV)
  347. 3. look for ppu in maindir
  348. 4. look for the specified source file (from the uses line)
  349. 5. look for source in cwd
  350. 6. look for source in maindir
  351. 7. local unit pathlist
  352. 8. global unit pathlist }
  353. fnd:=false;
  354. if not onlysource then
  355. begin
  356. fnd:=PPUSearchPath('.');
  357. if (not fnd) and (outputpath^<>'') then
  358. fnd:=PPUSearchPath(outputpath^);
  359. if (not fnd) and Assigned(main_module) and (main_module.Path^<>'') then
  360. fnd:=PPUSearchPath(main_module.Path^);
  361. end;
  362. if (not fnd) and (sourcefn^<>'') then
  363. begin
  364. { the full filename is specified so we can't use here the
  365. searchpath (PFV) }
  366. if CheckVerbosity(V_Tried) then
  367. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,sourceext));
  368. fnd:=FindFile(ChangeFileExt(sourcefn^,sourceext),'',true,hs);
  369. if not fnd then
  370. begin
  371. if CheckVerbosity(V_Tried) then
  372. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pasext));
  373. fnd:=FindFile(ChangeFileExt(sourcefn^,pasext),'',true,hs);
  374. end;
  375. if not fnd and
  376. ((m_mac in current_settings.modeswitches) or
  377. (tf_p_ext_support in target_info.flags)) then
  378. begin
  379. if CheckVerbosity(V_Tried) then
  380. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pext));
  381. fnd:=FindFile(ChangeFileExt(sourcefn^,pext),'',true,hs);
  382. end;
  383. if fnd then
  384. begin
  385. sources_avail:=true;
  386. do_compile:=true;
  387. recompile_reason:=rr_noppu;
  388. stringdispose(mainsource);
  389. mainsource:=StringDup(hs);
  390. SetFileName(hs,false);
  391. end;
  392. end;
  393. if not fnd then
  394. fnd:=SourceSearchPath('.');
  395. if (not fnd) and Assigned(main_module) and (main_module.Path^<>'') then
  396. fnd:=SourceSearchPath(main_module.Path^);
  397. if (not fnd) and Assigned(loaded_from) then
  398. fnd:=SearchPathList(loaded_from.LocalUnitSearchPath);
  399. if not fnd then
  400. fnd:=SearchPathList(UnitSearchPath);
  401. search_unit:=fnd;
  402. end;
  403. {**********************************
  404. PPU Reading/Writing Helpers
  405. ***********************************}
  406. {$IFDEF MACRO_DIFF_HINT}
  407. var
  408. is_initial: Boolean;
  409. procedure tppumodule.writeusedmacro(p:TNamedIndexItem;arg:pointer);
  410. begin
  411. if tmacro(p).is_used or is_initial then
  412. begin
  413. ppufile.putstring(p.name);
  414. ppufile.putbyte(byte(is_initial));
  415. ppufile.putbyte(byte(tmacro(p).is_used));
  416. end;
  417. end;
  418. procedure tppumodule.writeusedmacros;
  419. begin
  420. ppufile.do_crc:=false;
  421. is_initial:= true;
  422. initialmacrosymtable.foreach(@writeusedmacro,nil);
  423. is_initial:= false;
  424. if assigned(globalmacrosymtable) then
  425. globalmacrosymtable.foreach(@writeusedmacro,nil);
  426. localmacrosymtable.foreach(@writeusedmacro,nil);
  427. ppufile.writeentry(ibusedmacros);
  428. ppufile.do_crc:=true;
  429. end;
  430. {$ENDIF}
  431. procedure tppumodule.writesourcefiles;
  432. var
  433. hp : tinputfile;
  434. i,j : longint;
  435. begin
  436. { second write the used source files }
  437. ppufile.do_crc:=false;
  438. hp:=sourcefiles.files;
  439. { write source files directly in good order }
  440. j:=0;
  441. while assigned(hp) do
  442. begin
  443. inc(j);
  444. hp:=hp.ref_next;
  445. end;
  446. while j>0 do
  447. begin
  448. hp:=sourcefiles.files;
  449. for i:=1 to j-1 do
  450. hp:=hp.ref_next;
  451. ppufile.putstring(hp.name^);
  452. ppufile.putlongint(hp.getfiletime);
  453. dec(j);
  454. end;
  455. ppufile.writeentry(ibsourcefiles);
  456. ppufile.do_crc:=true;
  457. end;
  458. procedure tppumodule.writeusedunit(intf:boolean);
  459. var
  460. hp : tused_unit;
  461. oldcrc : boolean;
  462. begin
  463. { write a reference for each used unit }
  464. hp:=tused_unit(used_units.first);
  465. while assigned(hp) do
  466. begin
  467. if hp.in_interface=intf then
  468. begin
  469. ppufile.putstring(hp.u.realmodulename^);
  470. { the checksum should not affect the crc of this unit ! (PFV) }
  471. oldcrc:=ppufile.do_crc;
  472. ppufile.do_crc:=false;
  473. ppufile.putlongint(longint(hp.checksum));
  474. ppufile.putlongint(longint(hp.interface_checksum));
  475. ppufile.putlongint(longint(hp.indirect_checksum));
  476. ppufile.do_crc:=oldcrc;
  477. { combine all indirect checksums from units used by this unit }
  478. if intf then
  479. ppufile.indirect_crc:=ppufile.indirect_crc xor hp.indirect_checksum;
  480. end;
  481. hp:=tused_unit(hp.next);
  482. end;
  483. ppufile.do_interface_crc:=true;
  484. ppufile.writeentry(ibloadunit);
  485. end;
  486. procedure tppumodule.writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  487. var
  488. hcontainer : tlinkcontainer;
  489. s : string;
  490. mask : cardinal;
  491. begin
  492. hcontainer:=TLinkContainer.Create;
  493. while not p.empty do
  494. begin
  495. s:=p.get(mask);
  496. if strippath then
  497. ppufile.putstring(ExtractFileName(s))
  498. else
  499. ppufile.putstring(s);
  500. ppufile.putlongint(mask);
  501. hcontainer.add(s,mask);
  502. end;
  503. ppufile.writeentry(id);
  504. p.Free;
  505. p:=hcontainer;
  506. end;
  507. procedure tppumodule.writederefmap;
  508. var
  509. i : longint;
  510. oldcrc : boolean;
  511. begin
  512. { This does not influence crc }
  513. oldcrc:=ppufile.do_crc;
  514. ppufile.do_crc:=false;
  515. { The unit map used for resolving }
  516. ppufile.putlongint(derefmapcnt);
  517. for i:=0 to derefmapcnt-1 do
  518. begin
  519. if not assigned(derefmap[i].u) then
  520. internalerror(2005011512);
  521. ppufile.putstring(derefmap[i].u.modulename^)
  522. end;
  523. ppufile.writeentry(ibderefmap);
  524. ppufile.do_crc:=oldcrc;
  525. end;
  526. procedure tppumodule.writederefdata;
  527. var
  528. oldcrc : boolean;
  529. len,hlen : longint;
  530. buf : array[0..1023] of byte;
  531. begin
  532. if longword(derefdataintflen)>derefdata.size then
  533. internalerror(200310223);
  534. derefdata.seek(0);
  535. { Write interface data }
  536. len:=derefdataintflen;
  537. while (len>0) do
  538. begin
  539. if len>1024 then
  540. hlen:=1024
  541. else
  542. hlen:=len;
  543. derefdata.read(buf,hlen);
  544. ppufile.putdata(buf,hlen);
  545. dec(len,hlen);
  546. end;
  547. { Write implementation data, this does not influence crc }
  548. oldcrc:=ppufile.do_crc;
  549. ppufile.do_crc:=false;
  550. len:=derefdata.size-derefdataintflen;
  551. while (len>0) do
  552. begin
  553. if len>1024 then
  554. hlen:=1024
  555. else
  556. hlen:=len;
  557. derefdata.read(buf,hlen);
  558. ppufile.putdata(buf,hlen);
  559. dec(len,hlen);
  560. end;
  561. if derefdata.pos<>derefdata.size then
  562. internalerror(200310224);
  563. ppufile.do_crc:=oldcrc;
  564. ppufile.writeentry(ibderefdata);
  565. end;
  566. procedure tppumodule.writeImportSymbols;
  567. var
  568. i,j : longint;
  569. ImportLibrary : TImportLibrary;
  570. ImportSymbol : TImportSymbol;
  571. begin
  572. for i:=0 to ImportLibraryList.Count-1 do
  573. begin
  574. ImportLibrary:=TImportLibrary(ImportLibraryList[i]);
  575. ppufile.putstring(ImportLibrary.Name);
  576. ppufile.putlongint(ImportLibrary.ImportSymbolList.Count);
  577. for j:=0 to ImportLibrary.ImportSymbolList.Count-1 do
  578. begin
  579. ImportSymbol:=TImportSymbol(ImportLibrary.ImportSymbolList[j]);
  580. ppufile.putstring(ImportSymbol.Name);
  581. ppufile.putlongint(ImportSymbol.OrdNr);
  582. ppufile.putbyte(byte(ImportSymbol.IsVar));
  583. end;
  584. end;
  585. ppufile.writeentry(ibImportSymbols);
  586. end;
  587. procedure tppumodule.writeResources;
  588. var
  589. res : TCmdStrListItem;
  590. begin
  591. res:=TCmdStrListItem(ResourceFiles.First);
  592. while res<>nil do
  593. begin
  594. ppufile.putstring(res.FPStr);
  595. res:=TCmdStrListItem(res.Next);
  596. end;
  597. ppufile.writeentry(ibresources);
  598. end;
  599. {$IFDEF MACRO_DIFF_HINT}
  600. {
  601. Define MACRO_DIFF_HINT for the whole compiler (and ppudump)
  602. to turn this facility on. Also the hint messages defined
  603. below must be commented in in the msg/errore.msg file.
  604. There is some problems with this, thats why it is shut off:
  605. At the first compilation, consider a macro which is not initially
  606. defined, but it is used (e g the check that it is undefined is true).
  607. Since it do not exist, there is no macro object where the is_used
  608. flag can be set. Later on when the macro is defined, and the ppu
  609. is opened, the check cannot detect this.
  610. Also, in which macro object should this flag be set ? It cant be set
  611. for macros in the initialmacrosymboltable since this table is shared
  612. between different files.
  613. }
  614. procedure tppumodule.readusedmacros;
  615. var
  616. hs : string;
  617. mac : tmacro;
  618. was_initial,
  619. was_used : boolean;
  620. {Reads macros which was defined or used when the module was compiled.
  621. This is done when a ppu file is open, before it possibly is parsed.}
  622. begin
  623. while not ppufile.endofentry do
  624. begin
  625. hs:=ppufile.getstring;
  626. was_initial:=boolean(ppufile.getbyte);
  627. was_used:=boolean(ppufile.getbyte);
  628. mac:=tmacro(initialmacrosymtable.Find(hs));
  629. if assigned(mac) then
  630. begin
  631. {$ifndef EXTDEBUG}
  632. { if we don't have the sources why tell }
  633. if sources_avail then
  634. {$endif ndef EXTDEBUG}
  635. if (not was_initial) and was_used then
  636. Message2(unit_h_cond_not_set_in_last_compile,hs,mainsource^);
  637. end
  638. else { not assigned }
  639. if was_initial and
  640. was_used then
  641. Message2(unit_h_cond_set_in_last_compile,hs,mainsource^);
  642. end;
  643. end;
  644. {$ENDIF}
  645. procedure tppumodule.readsourcefiles;
  646. var
  647. temp,hs : string;
  648. temp_dir : TCmdStr;
  649. main_dir : TCmdStr;
  650. found,
  651. is_main : boolean;
  652. orgfiletime,
  653. source_time : longint;
  654. hp : tinputfile;
  655. begin
  656. sources_avail:=(flags and uf_release) = 0;
  657. if not sources_avail then
  658. exit;
  659. is_main:=true;
  660. main_dir:='';
  661. while not ppufile.endofentry do
  662. begin
  663. hs:=ppufile.getstring;
  664. orgfiletime:=ppufile.getlongint;
  665. temp_dir:='';
  666. if (flags and uf_in_library)<>0 then
  667. begin
  668. sources_avail:=false;
  669. temp:=' library';
  670. end
  671. else if pos('Macro ',hs)=1 then
  672. begin
  673. { we don't want to find this file }
  674. { but there is a problem with file indexing !! }
  675. temp:='';
  676. end
  677. else
  678. begin
  679. { check the date of the source files:
  680. 1 path of ppu
  681. 2 path of main source
  682. 3 current dir
  683. 4 include/unit path }
  684. Source_Time:=GetNamedFileTime(path^+hs);
  685. found:=false;
  686. if Source_Time<>-1 then
  687. hs:=path^+hs
  688. else
  689. if not(is_main) then
  690. begin
  691. Source_Time:=GetNamedFileTime(main_dir+hs);
  692. if Source_Time<>-1 then
  693. hs:=main_dir+hs;
  694. end;
  695. if Source_Time=-1 then
  696. Source_Time:=GetNamedFileTime(hs);
  697. if (Source_Time=-1) then
  698. begin
  699. if is_main then
  700. found:=unitsearchpath.FindFile(hs,true,temp_dir)
  701. else
  702. found:=includesearchpath.FindFile(hs,true,temp_dir);
  703. if found then
  704. begin
  705. Source_Time:=GetNamedFileTime(temp_dir);
  706. if Source_Time<>-1 then
  707. hs:=temp_dir;
  708. end;
  709. end;
  710. if Source_Time<>-1 then
  711. begin
  712. if is_main then
  713. main_dir:=ExtractFilePath(hs);
  714. temp:=' time '+filetimestring(source_time);
  715. if (orgfiletime<>-1) and
  716. (source_time<>orgfiletime) then
  717. begin
  718. do_compile:=true;
  719. recompile_reason:=rr_sourcenewer;
  720. Message2(unit_u_source_modified,hs,ppufilename^,@queuecomment);
  721. temp:=temp+' *';
  722. end;
  723. end
  724. else
  725. begin
  726. sources_avail:=false;
  727. temp:=' not found';
  728. end;
  729. hp:=tdosinputfile.create(hs);
  730. { the indexing is wrong here PM }
  731. sourcefiles.register_file(hp);
  732. end;
  733. if is_main then
  734. begin
  735. stringdispose(mainsource);
  736. mainsource:=stringdup(hs);
  737. end;
  738. Message1(unit_u_ppu_source,hs+temp,@queuecomment);
  739. is_main:=false;
  740. end;
  741. { check if we want to rebuild every unit, only if the sources are
  742. available }
  743. if do_build and sources_avail then
  744. begin
  745. do_compile:=true;
  746. recompile_reason:=rr_build;
  747. end;
  748. end;
  749. procedure tppumodule.readloadunit;
  750. var
  751. hs : string;
  752. pu : tused_unit;
  753. hp : tppumodule;
  754. indchecksum,
  755. intfchecksum,
  756. checksum : cardinal;
  757. begin
  758. while not ppufile.endofentry do
  759. begin
  760. hs:=ppufile.getstring;
  761. checksum:=cardinal(ppufile.getlongint);
  762. intfchecksum:=cardinal(ppufile.getlongint);
  763. indchecksum:=cardinal(ppufile.getlongint);
  764. { set the state of this unit before registering, this is
  765. needed for a correct circular dependency check }
  766. hp:=registerunit(self,hs,'');
  767. pu:=addusedunit(hp,false,nil);
  768. pu.checksum:=checksum;
  769. pu.interface_checksum:=intfchecksum;
  770. pu.indirect_checksum:=indchecksum;
  771. end;
  772. in_interface:=false;
  773. end;
  774. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  775. var
  776. s : string;
  777. m : longint;
  778. begin
  779. while not ppufile.endofentry do
  780. begin
  781. s:=ppufile.getstring;
  782. m:=ppufile.getlongint;
  783. p.add(s,m);
  784. end;
  785. end;
  786. procedure tppumodule.readderefmap;
  787. var
  788. i : longint;
  789. begin
  790. { Load unit map used for resolving }
  791. derefmapsize:=ppufile.getlongint;
  792. derefmapcnt:=derefmapsize;
  793. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  794. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  795. for i:=0 to derefmapsize-1 do
  796. derefmap[i].modulename:=stringdup(ppufile.getstring);
  797. end;
  798. procedure tppumodule.readderefdata;
  799. var
  800. len,hlen : longint;
  801. buf : array[0..1023] of byte;
  802. begin
  803. len:=ppufile.entrysize;
  804. while (len>0) do
  805. begin
  806. if len>1024 then
  807. hlen:=1024
  808. else
  809. hlen:=len;
  810. ppufile.getdata(buf,hlen);
  811. derefdata.write(buf,hlen);
  812. dec(len,hlen);
  813. end;
  814. end;
  815. procedure tppumodule.readImportSymbols;
  816. var
  817. j,
  818. extsymcnt : longint;
  819. ImportLibrary : TImportLibrary;
  820. extsymname : string;
  821. extsymordnr : longint;
  822. extsymisvar : boolean;
  823. begin
  824. while not ppufile.endofentry do
  825. begin
  826. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  827. extsymcnt:=ppufile.getlongint;
  828. for j:=0 to extsymcnt-1 do
  829. begin
  830. extsymname:=ppufile.getstring;
  831. extsymordnr:=ppufile.getlongint;
  832. extsymisvar:=(ppufile.getbyte<>0);
  833. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,extsymordnr,extsymisvar);
  834. end;
  835. end;
  836. end;
  837. procedure tppumodule.readResources;
  838. begin
  839. while not ppufile.endofentry do
  840. resourcefiles.Insert(ppufile.getstring);
  841. end;
  842. procedure tppumodule.readwpofile;
  843. var
  844. orgwpofilename: string;
  845. orgwpofiletime: longint;
  846. begin
  847. { check whether we are using the same wpo feedback input file as when
  848. this unit was compiled (same file name and file date)
  849. }
  850. orgwpofilename:=ppufile.getstring;
  851. orgwpofiletime:=ppufile.getlongint;
  852. if (extractfilename(orgwpofilename)<>extractfilename(wpofeedbackinput)) or
  853. (orgwpofiletime<>GetNamedFileTime(orgwpofilename)) then
  854. { make sure we don't throw away a precompiled unit if the user simply
  855. forgot to specify the right wpo feedback file
  856. }
  857. message3(unit_e_different_wpo_file,ppufilename^,orgwpofilename,filetimestring(orgwpofiletime));
  858. end;
  859. procedure tppumodule.load_interface;
  860. var
  861. b : byte;
  862. newmodulename : string;
  863. begin
  864. { read interface part }
  865. repeat
  866. b:=ppufile.readentry;
  867. case b of
  868. ibmodulename :
  869. begin
  870. newmodulename:=ppufile.getstring;
  871. if (cs_check_unit_name in current_settings.globalswitches) and
  872. (upper(newmodulename)<>modulename^) then
  873. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  874. stringdispose(modulename);
  875. stringdispose(realmodulename);
  876. modulename:=stringdup(upper(newmodulename));
  877. realmodulename:=stringdup(newmodulename);
  878. end;
  879. ibmoduleoptions:
  880. begin
  881. ppufile.getsmallset(moduleoptions);
  882. if mo_has_deprecated_msg in moduleoptions then
  883. begin
  884. stringdispose(deprecatedmsg);
  885. deprecatedmsg:=stringdup(ppufile.getstring);
  886. end;
  887. end;
  888. ibsourcefiles :
  889. readsourcefiles;
  890. {$IFDEF MACRO_DIFF_HINT}
  891. ibusedmacros :
  892. readusedmacros;
  893. {$ENDIF}
  894. ibloadunit :
  895. readloadunit;
  896. iblinkunitofiles :
  897. readlinkcontainer(LinkUnitOFiles);
  898. iblinkunitstaticlibs :
  899. readlinkcontainer(LinkUnitStaticLibs);
  900. iblinkunitsharedlibs :
  901. readlinkcontainer(LinkUnitSharedLibs);
  902. iblinkotherofiles :
  903. readlinkcontainer(LinkotherOFiles);
  904. iblinkotherstaticlibs :
  905. readlinkcontainer(LinkotherStaticLibs);
  906. iblinkothersharedlibs :
  907. readlinkcontainer(LinkotherSharedLibs);
  908. iblinkotherframeworks :
  909. readlinkcontainer(LinkOtherFrameworks);
  910. ibmainname:
  911. begin
  912. mainname:=stringdup(ppufile.getstring);
  913. if (mainaliasname<>defaultmainaliasname) then
  914. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  915. mainaliasname:=mainname^;
  916. end;
  917. ibImportSymbols :
  918. readImportSymbols;
  919. ibderefmap :
  920. readderefmap;
  921. ibderefdata :
  922. readderefdata;
  923. ibresources:
  924. readResources;
  925. ibwpofile:
  926. readwpofile;
  927. ibendinterface :
  928. break;
  929. else
  930. Message1(unit_f_ppu_invalid_entry,tostr(b));
  931. end;
  932. { we can already stop when we know that we must recompile }
  933. if do_compile then
  934. exit;
  935. until false;
  936. end;
  937. procedure tppumodule.load_implementation;
  938. var
  939. b : byte;
  940. begin
  941. { read implementation part }
  942. repeat
  943. b:=ppufile.readentry;
  944. case b of
  945. ibloadunit :
  946. readloadunit;
  947. ibasmsymbols :
  948. { TODO: Remove ibasmsymbols}
  949. ;
  950. ibendimplementation :
  951. break;
  952. else
  953. Message1(unit_f_ppu_invalid_entry,tostr(b));
  954. end;
  955. until false;
  956. end;
  957. procedure tppumodule.writeppu;
  958. begin
  959. Message1(unit_u_ppu_write,realmodulename^);
  960. { create unit flags }
  961. if do_release then
  962. flags:=flags or uf_release;
  963. if assigned(localsymtable) then
  964. flags:=flags or uf_local_symtable;
  965. {$ifdef cpufpemu}
  966. if (cs_fp_emulation in current_settings.moduleswitches) then
  967. flags:=flags or uf_fpu_emulation;
  968. {$endif cpufpemu}
  969. {$ifdef Test_Double_checksum_write}
  970. Assign(CRCFile,s+'.IMP');
  971. Rewrite(CRCFile);
  972. {$endif def Test_Double_checksum_write}
  973. { create new ppufile }
  974. ppufile:=tcompilerppufile.create(ppufilename^);
  975. if not ppufile.createfile then
  976. Message(unit_f_ppu_cannot_write);
  977. { first the unitname }
  978. ppufile.putstring(realmodulename^);
  979. ppufile.writeentry(ibmodulename);
  980. ppufile.putsmallset(moduleoptions);
  981. if mo_has_deprecated_msg in moduleoptions then
  982. ppufile.putstring(deprecatedmsg^);
  983. ppufile.writeentry(ibmoduleoptions);
  984. { write the alternate main procedure name if any }
  985. if assigned(mainname) then
  986. begin
  987. ppufile.putstring(mainname^);
  988. ppufile.writeentry(ibmainname);
  989. end;
  990. writesourcefiles;
  991. {$IFDEF MACRO_DIFF_HINT}
  992. writeusedmacros;
  993. {$ENDIF}
  994. { write interface uses }
  995. writeusedunit(true);
  996. { write the objectfiles and libraries that come for this unit,
  997. preserve the containers because they are still needed to load
  998. the link.res.
  999. All doesn't depend on the crc! It doesn't matter
  1000. if a unit is in a .o or .a file }
  1001. ppufile.do_crc:=false;
  1002. { write after source files, so that we know whether or not the compiler
  1003. will recompile the unit when checking whether the correct wpo file is
  1004. used (if it will recompile the unit anyway, it doesn't matter)
  1005. }
  1006. if (wpofeedbackinput<>'') then
  1007. begin
  1008. ppufile.putstring(wpofeedbackinput);
  1009. ppufile.putlongint(getnamedfiletime(wpofeedbackinput));
  1010. ppufile.writeentry(ibwpofile);
  1011. end;
  1012. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  1013. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  1014. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  1015. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  1016. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  1017. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  1018. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  1019. writeImportSymbols;
  1020. writeResources;
  1021. ppufile.do_crc:=true;
  1022. { generate implementation deref data, the interface deref data is
  1023. already generated when calculating the interface crc }
  1024. if (cs_compilesystem in current_settings.moduleswitches) then
  1025. begin
  1026. tstoredsymtable(globalsymtable).buildderef;
  1027. derefdataintflen:=derefdata.size;
  1028. end
  1029. else
  1030. { the unit may have been re-resolved, in which case the current
  1031. position in derefdata is not necessarily at the end }
  1032. derefdata.seek(derefdata.size);
  1033. tstoredsymtable(globalsymtable).buildderefimpl;
  1034. if (flags and uf_local_symtable)<>0 then
  1035. begin
  1036. tstoredsymtable(localsymtable).buildderef;
  1037. tstoredsymtable(localsymtable).buildderefimpl;
  1038. end;
  1039. tunitwpoinfo(wpoinfo).buildderef;
  1040. tunitwpoinfo(wpoinfo).buildderefimpl;
  1041. writederefmap;
  1042. writederefdata;
  1043. ppufile.writeentry(ibendinterface);
  1044. { write the symtable entries }
  1045. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1046. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1047. begin
  1048. ppufile.putbyte(byte(true));
  1049. ppufile.writeentry(ibexportedmacros);
  1050. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1051. end
  1052. else
  1053. begin
  1054. ppufile.putbyte(byte(false));
  1055. ppufile.writeentry(ibexportedmacros);
  1056. end;
  1057. { everything after this doesn't affect the crc }
  1058. ppufile.do_crc:=false;
  1059. { write implementation uses }
  1060. writeusedunit(false);
  1061. { end of implementation }
  1062. ppufile.writeentry(ibendimplementation);
  1063. { write static symtable
  1064. needed for local debugging of unit functions }
  1065. if (flags and uf_local_symtable)<>0 then
  1066. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1067. { write whole program optimisation-related information }
  1068. tunitwpoinfo(wpoinfo).ppuwrite(ppufile);
  1069. { the last entry ibend is written automatically }
  1070. { flush to be sure }
  1071. ppufile.flush;
  1072. { create and write header }
  1073. ppufile.header.size:=ppufile.size;
  1074. ppufile.header.checksum:=ppufile.crc;
  1075. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1076. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1077. ppufile.header.compiler:=wordversion;
  1078. ppufile.header.cpu:=word(target_cpu);
  1079. ppufile.header.target:=word(target_info.system);
  1080. ppufile.header.flags:=flags;
  1081. ppufile.header.deflistsize:=current_module.deflist.count;
  1082. ppufile.header.symlistsize:=current_module.symlist.count;
  1083. ppufile.writeheader;
  1084. { save crc in current module also }
  1085. crc:=ppufile.crc;
  1086. interface_crc:=ppufile.interface_crc;
  1087. indirect_crc:=ppufile.indirect_crc;
  1088. {$ifdef Test_Double_checksum_write}
  1089. close(CRCFile);
  1090. {$endif Test_Double_checksum_write}
  1091. ppufile.closefile;
  1092. ppufile.free;
  1093. ppufile:=nil;
  1094. end;
  1095. procedure tppumodule.getppucrc;
  1096. begin
  1097. {$ifdef Test_Double_checksum_write}
  1098. Assign(CRCFile,s+'.INT')
  1099. Rewrite(CRCFile);
  1100. {$endif def Test_Double_checksum_write}
  1101. { create new ppufile }
  1102. ppufile:=tcompilerppufile.create(ppufilename^);
  1103. ppufile.crc_only:=true;
  1104. if not ppufile.createfile then
  1105. Message(unit_f_ppu_cannot_write);
  1106. { first the unitname }
  1107. ppufile.putstring(realmodulename^);
  1108. ppufile.writeentry(ibmodulename);
  1109. ppufile.putsmallset(moduleoptions);
  1110. if mo_has_deprecated_msg in moduleoptions then
  1111. ppufile.putstring(deprecatedmsg^);
  1112. ppufile.writeentry(ibmoduleoptions);
  1113. { the interface units affect the crc }
  1114. writeusedunit(true);
  1115. { deref data of interface that affect the crc }
  1116. derefdata.reset;
  1117. tstoredsymtable(globalsymtable).buildderef;
  1118. derefdataintflen:=derefdata.size;
  1119. writederefmap;
  1120. writederefdata;
  1121. ppufile.writeentry(ibendinterface);
  1122. { write the symtable entries }
  1123. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1124. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1125. begin
  1126. ppufile.putbyte(byte(true));
  1127. ppufile.writeentry(ibexportedmacros);
  1128. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1129. end
  1130. else
  1131. begin
  1132. ppufile.putbyte(byte(false));
  1133. ppufile.writeentry(ibexportedmacros);
  1134. end;
  1135. { save crc }
  1136. crc:=ppufile.crc;
  1137. interface_crc:=ppufile.interface_crc;
  1138. indirect_crc:=ppufile.indirect_crc;
  1139. { end of implementation, to generate a correct ppufile
  1140. for ppudump when using INTFPPU define }
  1141. ppufile.writeentry(ibendimplementation);
  1142. {$ifdef Test_Double_checksum}
  1143. crc_array:=ppufile.crc_test;
  1144. ppufile.crc_test:=nil;
  1145. crc_size:=ppufile.crc_index2;
  1146. crc_array2:=ppufile.crc_test2;
  1147. ppufile.crc_test2:=nil;
  1148. crc_size2:=ppufile.crc_index2;
  1149. {$endif Test_Double_checksum}
  1150. {$ifdef Test_Double_checksum_write}
  1151. close(CRCFile);
  1152. {$endif Test_Double_checksum_write}
  1153. { create and write header, this will only be used
  1154. for debugging purposes }
  1155. ppufile.header.size:=ppufile.size;
  1156. ppufile.header.checksum:=ppufile.crc;
  1157. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1158. ppufile.header.indirect_checksum:=ppufile.indirect_crc;
  1159. ppufile.header.compiler:=wordversion;
  1160. ppufile.header.cpu:=word(target_cpu);
  1161. ppufile.header.target:=word(target_info.system);
  1162. ppufile.header.flags:=flags;
  1163. ppufile.writeheader;
  1164. ppufile.closefile;
  1165. ppufile.free;
  1166. ppufile:=nil;
  1167. end;
  1168. procedure tppumodule.load_usedunits;
  1169. var
  1170. pu : tused_unit;
  1171. begin
  1172. if current_module<>self then
  1173. internalerror(200212284);
  1174. { load the used units from interface }
  1175. in_interface:=true;
  1176. pu:=tused_unit(used_units.first);
  1177. while assigned(pu) do
  1178. begin
  1179. if pu.in_interface then
  1180. begin
  1181. tppumodule(pu.u).loadppu;
  1182. { if this unit is compiled we can stop }
  1183. if state=ms_compiled then
  1184. exit;
  1185. { add this unit to the dependencies }
  1186. pu.u.adddependency(self);
  1187. { need to recompile the current unit, check the interface
  1188. crc. And when not compiled with -Ur then check the complete
  1189. crc }
  1190. if (pu.u.interface_crc<>pu.interface_checksum) or
  1191. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1192. (
  1193. ((ppufile.header.flags and uf_release)=0) and
  1194. (pu.u.crc<>pu.checksum)
  1195. ) then
  1196. begin
  1197. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^,@queuecomment);
  1198. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1199. if (pu.u.interface_crc<>pu.interface_checksum) then
  1200. writeln(' intfcrc change: ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1201. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1202. writeln(' indcrc change: ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8))
  1203. else
  1204. writeln(' implcrc change: ',hexstr(pu.u.crc,8),' <> ',hexstr(pu.checksum,8));
  1205. {$endif DEBUG_UNIT_CRC_CHANGES}
  1206. recompile_reason:=rr_crcchanged;
  1207. do_compile:=true;
  1208. exit;
  1209. end;
  1210. end;
  1211. pu:=tused_unit(pu.next);
  1212. end;
  1213. { ok, now load the interface of this unit }
  1214. if current_module<>self then
  1215. internalerror(200208187);
  1216. deflist.count:=ppufile.header.deflistsize;
  1217. symlist.count:=ppufile.header.symlistsize;
  1218. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1219. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1220. if ppufile.readentry<>ibexportedmacros then
  1221. Message(unit_f_ppu_read_error);
  1222. if boolean(ppufile.getbyte) then
  1223. begin
  1224. globalmacrosymtable:=tmacrosymtable.Create(true);
  1225. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1226. end;
  1227. interface_compiled:=true;
  1228. { read the implementation part, containing
  1229. the implementation uses and ObjData }
  1230. in_interface:=false;
  1231. load_implementation;
  1232. { now only read the implementation uses }
  1233. pu:=tused_unit(used_units.first);
  1234. while assigned(pu) do
  1235. begin
  1236. if (not pu.in_interface) then
  1237. begin
  1238. tppumodule(pu.u).loadppu;
  1239. { if this unit is compiled we can stop }
  1240. if state=ms_compiled then
  1241. exit;
  1242. { add this unit to the dependencies }
  1243. pu.u.adddependency(self);
  1244. { need to recompile the current unit ? }
  1245. if (pu.u.interface_crc<>pu.interface_checksum) or
  1246. (pu.u.indirect_crc<>pu.indirect_checksum) then
  1247. begin
  1248. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}',@queuecomment);
  1249. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1250. if (pu.u.interface_crc<>pu.interface_checksum) then
  1251. writeln(' intfcrc change (2): ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1252. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1253. writeln(' indcrc change (2): ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8));
  1254. {$endif DEBUG_UNIT_CRC_CHANGES}
  1255. recompile_reason:=rr_crcchanged;
  1256. do_compile:=true;
  1257. exit;
  1258. end;
  1259. end;
  1260. pu:=tused_unit(pu.next);
  1261. end;
  1262. { load implementation symtable }
  1263. if (flags and uf_local_symtable)<>0 then
  1264. begin
  1265. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1266. tstaticsymtable(localsymtable).ppuload(ppufile);
  1267. end;
  1268. { we can now derefence all pointers to the implementation parts }
  1269. tstoredsymtable(globalsymtable).derefimpl;
  1270. if assigned(localsymtable) then
  1271. tstoredsymtable(localsymtable).derefimpl;
  1272. { read whole program optimisation-related information }
  1273. wpoinfo:=tunitwpoinfo.ppuload(ppufile);
  1274. tunitwpoinfo(wpoinfo).deref;
  1275. tunitwpoinfo(wpoinfo).derefimpl;
  1276. end;
  1277. function tppumodule.needrecompile:boolean;
  1278. var
  1279. pu : tused_unit;
  1280. begin
  1281. result:=false;
  1282. pu:=tused_unit(used_units.first);
  1283. while assigned(pu) do
  1284. begin
  1285. { need to recompile the current unit, check the interface
  1286. crc. And when not compiled with -Ur then check the complete
  1287. crc }
  1288. if (pu.u.interface_crc<>pu.interface_checksum) or
  1289. (pu.u.indirect_crc<>pu.indirect_checksum) or
  1290. (
  1291. (pu.in_interface) and
  1292. (pu.u.crc<>pu.checksum)
  1293. ) then
  1294. begin
  1295. {$ifdef DEBUG_UNIT_CRC_CHANGES}
  1296. if (pu.u.interface_crc<>pu.interface_checksum) then
  1297. writeln(' intfcrc change (3): ',hexstr(pu.u.interface_crc,8),' <> ',hexstr(pu.interface_checksum,8))
  1298. else if (pu.u.indirect_crc<>pu.indirect_checksum) then
  1299. writeln(' indcrc change (3): ',hexstr(pu.u.indirect_crc,8),' <> ',hexstr(pu.indirect_checksum,8))
  1300. else
  1301. writeln(' implcrc change (3): ',hexstr(pu.u.crc,8),' <> ',hexstr(pu.checksum,8));
  1302. {$endif DEBUG_UNIT_CRC_CHANGES}
  1303. result:=true;
  1304. exit;
  1305. end;
  1306. pu:=tused_unit(pu.next);
  1307. end;
  1308. end;
  1309. procedure tppumodule.setdefgeneration;
  1310. begin
  1311. defsgeneration:=currentdefgeneration;
  1312. inc(currentdefgeneration);
  1313. end;
  1314. procedure tppumodule.reload_flagged_units;
  1315. var
  1316. hp : tppumodule;
  1317. begin
  1318. { now reload all dependent units with outdated defs }
  1319. hp:=tppumodule(loaded_units.first);
  1320. while assigned(hp) do
  1321. begin
  1322. if hp.do_reload and
  1323. (hp.defsgeneration<defsgeneration) then
  1324. begin
  1325. hp.defsgeneration:=defsgeneration;
  1326. hp.loadppu
  1327. end
  1328. else
  1329. hp.do_reload:=false;
  1330. hp:=tppumodule(hp.next);
  1331. end;
  1332. end;
  1333. procedure tppumodule.loadppu;
  1334. const
  1335. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1336. var
  1337. do_load,
  1338. second_time : boolean;
  1339. old_current_module : tmodule;
  1340. begin
  1341. old_current_module:=current_module;
  1342. Message3(unit_u_load_unit,old_current_module.modulename^,
  1343. ImplIntf[old_current_module.in_interface],
  1344. modulename^);
  1345. { Update loaded_from to detect cycles }
  1346. loaded_from:=old_current_module;
  1347. { check if the globalsymtable is already available, but
  1348. we must reload when the do_reload flag is set }
  1349. if (not do_reload) and
  1350. assigned(globalsymtable) then
  1351. exit;
  1352. { reset }
  1353. do_load:=true;
  1354. second_time:=false;
  1355. set_current_module(self);
  1356. { A force reload }
  1357. if do_reload then
  1358. begin
  1359. Message(unit_u_forced_reload);
  1360. do_reload:=false;
  1361. { When the unit is already loaded or being loaded
  1362. we can maybe skip a complete reload/recompile }
  1363. if assigned(globalsymtable) and
  1364. (not needrecompile) then
  1365. begin
  1366. { When we don't have any data stored yet there
  1367. is nothing to resolve }
  1368. if interface_compiled then
  1369. begin
  1370. Message1(unit_u_reresolving_unit,modulename^);
  1371. tstoredsymtable(globalsymtable).deref;
  1372. tstoredsymtable(globalsymtable).derefimpl;
  1373. if assigned(localsymtable) then
  1374. begin
  1375. tstoredsymtable(localsymtable).deref;
  1376. tstoredsymtable(localsymtable).derefimpl;
  1377. end;
  1378. if assigned(wpoinfo) then
  1379. begin
  1380. tunitwpoinfo(wpoinfo).deref;
  1381. tunitwpoinfo(wpoinfo).derefimpl;
  1382. end;
  1383. { We have to flag the units that depend on this unit even
  1384. though it didn't change, because they might also
  1385. indirectly depend on the unit that did change (e.g.,
  1386. in case rgobj, rgx86 and rgcpu have been compiled
  1387. already, and then rgobj is recompiled for some reason
  1388. -> rgx86 is re-reresolved, but the vmtentries of trgcpu
  1389. must also be re-resolved, because they will also contain
  1390. pointers to procdefs in the old trgobj (in case of a
  1391. recompile, all old defs are freed) }
  1392. flagdependent(old_current_module);
  1393. reload_flagged_units;
  1394. end
  1395. else
  1396. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1397. do_load:=false;
  1398. end;
  1399. end;
  1400. if do_load then
  1401. begin
  1402. { loading the unit for a second time? }
  1403. if state=ms_registered then
  1404. state:=ms_load
  1405. else
  1406. begin
  1407. { try to load the unit a second time first }
  1408. Message1(unit_u_second_load_unit,modulename^);
  1409. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1410. { Flag modules to reload }
  1411. flagdependent(old_current_module);
  1412. { Reset the module }
  1413. reset;
  1414. if state in [ms_compile,ms_second_compile] then
  1415. begin
  1416. Message1(unit_u_second_compile_unit,modulename^);
  1417. state:=ms_second_compile;
  1418. do_compile:=true;
  1419. end
  1420. else
  1421. state:=ms_second_load;
  1422. second_time:=true;
  1423. end;
  1424. { close old_current_ppu on system that are
  1425. short on file handles like DOS PM }
  1426. {$ifdef SHORT_ON_FILE_HANDLES}
  1427. if old_current_module.is_unit and
  1428. assigned(tppumodule(old_current_module).ppufile) then
  1429. tppumodule(old_current_module).ppufile.tempclose;
  1430. {$endif SHORT_ON_FILE_HANDLES}
  1431. { try to opening ppu, skip this when we already
  1432. know that we need to compile the unit }
  1433. if not do_compile then
  1434. begin
  1435. Message1(unit_u_loading_unit,modulename^);
  1436. search_unit_files(false);
  1437. if not do_compile then
  1438. begin
  1439. load_interface;
  1440. setdefgeneration;
  1441. if not do_compile then
  1442. begin
  1443. load_usedunits;
  1444. if not do_compile then
  1445. Message1(unit_u_finished_loading_unit,modulename^);
  1446. end;
  1447. end;
  1448. { PPU is not needed anymore }
  1449. if assigned(ppufile) then
  1450. begin
  1451. ppufile.closefile;
  1452. ppufile.free;
  1453. ppufile:=nil;
  1454. end;
  1455. end;
  1456. { Do we need to recompile the unit }
  1457. if do_compile then
  1458. begin
  1459. { recompile the unit or give a fatal error if sources not available }
  1460. if not(sources_avail) then
  1461. begin
  1462. search_unit_files(true);
  1463. if not(sources_avail) then
  1464. begin
  1465. printcomments;
  1466. if recompile_reason=rr_noppu then
  1467. Message2(unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^)
  1468. else
  1469. Message1(unit_f_cant_compile_unit,realmodulename^);
  1470. end;
  1471. end;
  1472. { we found the sources, we do not need the verbose messages anymore }
  1473. if comments <> nil then
  1474. begin
  1475. comments.free;
  1476. comments:=nil;
  1477. end;
  1478. { Flag modules to reload }
  1479. flagdependent(old_current_module);
  1480. { Reset the module }
  1481. reset;
  1482. { compile this module }
  1483. if not(state in [ms_compile,ms_second_compile]) then
  1484. state:=ms_compile;
  1485. compile(mainsource^);
  1486. setdefgeneration;
  1487. end
  1488. else
  1489. state:=ms_compiled;
  1490. if current_module<>self then
  1491. internalerror(200212282);
  1492. if in_interface then
  1493. internalerror(200212283);
  1494. { for a second_time recompile reload all dependent units,
  1495. for a first time compile register the unit _once_ }
  1496. if second_time then
  1497. reload_flagged_units
  1498. else
  1499. usedunits.concat(tused_unit.create(self,true,false,nil));
  1500. { reopen the old module }
  1501. {$ifdef SHORT_ON_FILE_HANDLES}
  1502. if old_current_module.is_unit and
  1503. assigned(tppumodule(old_current_module).ppufile) then
  1504. tppumodule(old_current_module).ppufile.tempopen;
  1505. {$endif SHORT_ON_FILE_HANDLES}
  1506. end;
  1507. { we are back, restore current_module }
  1508. set_current_module(old_current_module);
  1509. end;
  1510. {*****************************************************************************
  1511. RegisterUnit
  1512. *****************************************************************************}
  1513. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  1514. var
  1515. ups : TIDString;
  1516. hp : tppumodule;
  1517. hp2 : tmodule;
  1518. begin
  1519. { Info }
  1520. ups:=upper(s);
  1521. { search all loaded units }
  1522. hp:=tppumodule(loaded_units.first);
  1523. while assigned(hp) do
  1524. begin
  1525. if hp.modulename^=ups then
  1526. begin
  1527. { only check for units. The main program is also
  1528. as a unit in the loaded_units list. We simply need
  1529. to ignore this entry (PFV) }
  1530. if hp.is_unit then
  1531. begin
  1532. { both units in interface ? }
  1533. if callermodule.in_interface and
  1534. hp.in_interface then
  1535. begin
  1536. { check for a cycle }
  1537. hp2:=callermodule.loaded_from;
  1538. while assigned(hp2) and (hp2<>hp) do
  1539. begin
  1540. if hp2.in_interface then
  1541. hp2:=hp2.loaded_from
  1542. else
  1543. hp2:=nil;
  1544. end;
  1545. if assigned(hp2) then
  1546. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1547. end;
  1548. break;
  1549. end;
  1550. end;
  1551. { the next unit }
  1552. hp:=tppumodule(hp.next);
  1553. end;
  1554. { the unit is not in the loaded units,
  1555. we create an entry and register the unit }
  1556. if not assigned(hp) then
  1557. begin
  1558. Message1(unit_u_registering_new_unit,Upper(s));
  1559. hp:=tppumodule.create(callermodule,s,fn,true);
  1560. hp.loaded_from:=callermodule;
  1561. addloadedunit(hp);
  1562. end;
  1563. { return }
  1564. registerunit:=hp;
  1565. end;
  1566. end.