fppu.pas 57 KB

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