fppu.pas 53 KB

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