fppu.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. private
  53. function search_unit(onlysource,shortname:boolean):boolean;
  54. procedure load_interface;
  55. procedure load_implementation;
  56. procedure load_usedunits;
  57. procedure printcomments;
  58. procedure queuecomment(s:string;v,w:longint);
  59. procedure writesourcefiles;
  60. procedure writeusedunit(intf:boolean);
  61. procedure writelinkcontainer(var p:tlinkcontainer;id:byte;strippath:boolean);
  62. procedure writederefmap;
  63. procedure writederefdata;
  64. procedure writeImportSymbols;
  65. procedure writeResources;
  66. procedure readsourcefiles;
  67. procedure readloadunit;
  68. procedure readlinkcontainer(var p:tlinkcontainer);
  69. procedure readderefmap;
  70. procedure readderefdata;
  71. procedure readImportSymbols;
  72. procedure readResources;
  73. procedure readwpofile;
  74. {$IFDEF MACRO_DIFF_HINT}
  75. procedure writeusedmacro(p:TNamedIndexItem;arg:pointer);
  76. procedure writeusedmacros;
  77. procedure readusedmacros;
  78. {$ENDIF}
  79. end;
  80. procedure reload_flagged_units;
  81. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  82. implementation
  83. uses
  84. SysUtils,
  85. cfileutl,
  86. verbose,systems,version,
  87. symtable, symsym,
  88. wpoinfo,
  89. scanner,
  90. aasmbase,ogbase,
  91. parser,
  92. comphook;
  93. {****************************************************************************
  94. Helpers
  95. ****************************************************************************}
  96. procedure reload_flagged_units;
  97. var
  98. hp : tmodule;
  99. begin
  100. { now reload all dependent units }
  101. hp:=tmodule(loaded_units.first);
  102. while assigned(hp) do
  103. begin
  104. if hp.do_reload then
  105. tppumodule(hp).loadppu;
  106. hp:=tmodule(hp.next);
  107. end;
  108. end;
  109. {****************************************************************************
  110. TPPUMODULE
  111. ****************************************************************************}
  112. constructor tppumodule.create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  113. begin
  114. inherited create(LoadedFrom,s,_is_unit);
  115. ppufile:=nil;
  116. sourcefn:=stringdup(fn);
  117. end;
  118. destructor tppumodule.Destroy;
  119. begin
  120. if assigned(ppufile) then
  121. ppufile.free;
  122. ppufile:=nil;
  123. comments.free;
  124. comments:=nil;
  125. stringdispose(sourcefn);
  126. inherited Destroy;
  127. end;
  128. procedure tppumodule.reset;
  129. begin
  130. if assigned(ppufile) then
  131. begin
  132. ppufile.free;
  133. ppufile:=nil;
  134. end;
  135. inherited reset;
  136. end;
  137. procedure tppumodule.queuecomment(s:string;v,w:longint);
  138. begin
  139. if comments = nil then
  140. comments := TCmdStrList.create;
  141. comments.insert(s);
  142. end;
  143. procedure tppumodule.printcomments;
  144. var
  145. comment: string;
  146. begin
  147. if comments = nil then
  148. exit;
  149. { comments are inserted in reverse order }
  150. repeat
  151. comment := comments.getlast;
  152. if length(comment) = 0 then
  153. exit;
  154. do_comment(v_normal, comment);
  155. until false;
  156. end;
  157. function tppumodule.openppu:boolean;
  158. var
  159. ppufiletime : longint;
  160. begin
  161. openppu:=false;
  162. Message1(unit_t_ppu_loading,ppufilename^,@queuecomment);
  163. { Get ppufile time (also check if the file exists) }
  164. ppufiletime:=getnamedfiletime(ppufilename^);
  165. if ppufiletime=-1 then
  166. exit;
  167. { Open the ppufile }
  168. Message1(unit_u_ppu_name,ppufilename^);
  169. ppufile:=tcompilerppufile.create(ppufilename^);
  170. if not ppufile.openfile then
  171. begin
  172. ppufile.free;
  173. ppufile:=nil;
  174. Message(unit_u_ppu_file_too_short);
  175. exit;
  176. end;
  177. { check for a valid PPU file }
  178. if not ppufile.CheckPPUId then
  179. begin
  180. ppufile.free;
  181. ppufile:=nil;
  182. Message(unit_u_ppu_invalid_header);
  183. exit;
  184. end;
  185. { check for allowed PPU versions }
  186. if not (ppufile.GetPPUVersion = CurrentPPUVersion) then
  187. begin
  188. Message1(unit_u_ppu_invalid_version,tostr(ppufile.GetPPUVersion),@queuecomment);
  189. ppufile.free;
  190. ppufile:=nil;
  191. exit;
  192. end;
  193. { check the target processor }
  194. if tsystemcpu(ppufile.header.cpu)<>target_cpu then
  195. begin
  196. ppufile.free;
  197. ppufile:=nil;
  198. Message(unit_u_ppu_invalid_processor,@queuecomment);
  199. exit;
  200. end;
  201. { check target }
  202. if tsystem(ppufile.header.target)<>target_info.system then
  203. begin
  204. ppufile.free;
  205. ppufile:=nil;
  206. Message(unit_u_ppu_invalid_target,@queuecomment);
  207. exit;
  208. end;
  209. {$ifdef cpufpemu}
  210. { check if floating point emulation is on?
  211. fpu emulation isn't unit levelwise because it affects calling convention }
  212. if ((ppufile.header.flags and uf_fpu_emulation)<>0) xor
  213. (cs_fp_emulation in current_settings.moduleswitches) then
  214. begin
  215. ppufile.free;
  216. ppufile:=nil;
  217. Message(unit_u_ppu_invalid_fpumode,@queuecomment);
  218. exit;
  219. end;
  220. {$endif cpufpemu}
  221. { Load values to be access easier }
  222. flags:=ppufile.header.flags;
  223. crc:=ppufile.header.checksum;
  224. interface_crc:=ppufile.header.interface_checksum;
  225. { Show Debug info }
  226. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  227. Message1(unit_u_ppu_flags,tostr(flags));
  228. Message1(unit_u_ppu_crc,hexstr(ppufile.header.checksum,8));
  229. Message1(unit_u_ppu_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  230. Comment(V_used,'Number of definitions: '+tostr(ppufile.header.deflistsize));
  231. Comment(V_used,'Number of symbols: '+tostr(ppufile.header.symlistsize));
  232. do_compile:=false;
  233. openppu:=true;
  234. end;
  235. function tppumodule.search_unit(onlysource,shortname:boolean):boolean;
  236. var
  237. singlepathstring,
  238. filename : TCmdStr;
  239. Function UnitExists(const ext:string;var foundfile:TCmdStr):boolean;
  240. begin
  241. if CheckVerbosity(V_Tried) then
  242. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  243. UnitExists:=FindFile(FileName+ext,Singlepathstring,true,foundfile);
  244. end;
  245. Function PPUSearchPath(const s:TCmdStr):boolean;
  246. var
  247. found : boolean;
  248. hs : TCmdStr;
  249. begin
  250. Found:=false;
  251. singlepathstring:=FixPath(s,false);
  252. { Check for PPU file }
  253. Found:=UnitExists(target_info.unitext,hs);
  254. if Found then
  255. Begin
  256. SetFileName(hs,false);
  257. Found:=OpenPPU;
  258. End;
  259. PPUSearchPath:=Found;
  260. end;
  261. Function SourceSearchPath(const s:TCmdStr):boolean;
  262. var
  263. found : boolean;
  264. hs : TCmdStr;
  265. begin
  266. Found:=false;
  267. singlepathstring:=FixPath(s,false);
  268. { Check for Sources }
  269. ppufile:=nil;
  270. do_compile:=true;
  271. recompile_reason:=rr_noppu;
  272. {Check for .pp file}
  273. Found:=UnitExists(sourceext,hs);
  274. if not Found then
  275. begin
  276. { Check for .pas }
  277. Found:=UnitExists(pasext,hs);
  278. end;
  279. if not Found and (m_mac in current_settings.modeswitches) then
  280. begin
  281. { Check for .p, if mode is macpas}
  282. Found:=UnitExists(pext,hs);
  283. end;
  284. stringdispose(mainsource);
  285. if Found then
  286. begin
  287. sources_avail:=true;
  288. { Load Filenames when found }
  289. mainsource:=StringDup(hs);
  290. SetFileName(hs,false);
  291. end
  292. else
  293. sources_avail:=false;
  294. SourceSearchPath:=Found;
  295. end;
  296. Function SearchPath(const s:TCmdStr):boolean;
  297. var
  298. found : boolean;
  299. begin
  300. { First check for a ppu, then for the source }
  301. found:=false;
  302. if not onlysource then
  303. found:=PPUSearchPath(s);
  304. if not found then
  305. found:=SourceSearchPath(s);
  306. SearchPath:=found;
  307. end;
  308. Function SearchPathList(list:TSearchPathList):boolean;
  309. var
  310. hp : TCmdStrListItem;
  311. found : boolean;
  312. begin
  313. found:=false;
  314. hp:=TCmdStrListItem(list.First);
  315. while assigned(hp) do
  316. begin
  317. found:=SearchPath(hp.Str);
  318. if found then
  319. break;
  320. hp:=TCmdStrListItem(hp.next);
  321. end;
  322. SearchPathList:=found;
  323. end;
  324. var
  325. fnd : boolean;
  326. hs : TCmdStr;
  327. begin
  328. if shortname then
  329. filename:=FixFileName(Copy(realmodulename^,1,8))
  330. else
  331. filename:=FixFileName(realmodulename^);
  332. { try to find unit
  333. 1. look for ppu in cwd
  334. 2. look for ppu in outputpath if set, this is tp7 compatible (PFV)
  335. 3. look for ppu in maindir
  336. 4. look for the specified source file (from the uses line)
  337. 5. look for source in cwd
  338. 6. look for source in maindir
  339. 7. local unit pathlist
  340. 8. global unit pathlist }
  341. fnd:=false;
  342. if not onlysource then
  343. begin
  344. fnd:=PPUSearchPath('.');
  345. if (not fnd) and (outputpath^<>'') then
  346. fnd:=PPUSearchPath(outputpath^);
  347. if (not fnd) and Assigned(main_module) and (main_module.Path^<>'') then
  348. fnd:=PPUSearchPath(main_module.Path^);
  349. end;
  350. if (not fnd) and (sourcefn^<>'') then
  351. begin
  352. { the full filename is specified so we can't use here the
  353. searchpath (PFV) }
  354. if CheckVerbosity(V_Tried) then
  355. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,sourceext));
  356. fnd:=FindFile(ChangeFileExt(sourcefn^,sourceext),'',true,hs);
  357. if not fnd then
  358. begin
  359. if CheckVerbosity(V_Tried) then
  360. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pasext));
  361. fnd:=FindFile(ChangeFileExt(sourcefn^,pasext),'',true,hs);
  362. end;
  363. if not fnd and ((m_mac in current_settings.modeswitches) or (tf_p_ext_support in target_info.flags)) then
  364. begin
  365. if CheckVerbosity(V_Tried) then
  366. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pext));
  367. fnd:=FindFile(ChangeFileExt(sourcefn^,pext),'',true,hs);
  368. end;
  369. if fnd then
  370. begin
  371. sources_avail:=true;
  372. do_compile:=true;
  373. recompile_reason:=rr_noppu;
  374. stringdispose(mainsource);
  375. mainsource:=StringDup(hs);
  376. SetFileName(hs,false);
  377. end;
  378. end;
  379. if not fnd then
  380. fnd:=SourceSearchPath('.');
  381. if (not fnd) and Assigned(main_module) and (main_module.Path^<>'') then
  382. fnd:=SourceSearchPath(main_module.Path^);
  383. if (not fnd) and Assigned(loaded_from) then
  384. fnd:=SearchPathList(loaded_from.LocalUnitSearchPath);
  385. if not fnd then
  386. fnd:=SearchPathList(UnitSearchPath);
  387. { try to find a file with the first 8 chars of the modulename, like
  388. dos }
  389. if (not fnd) and (length(filename)>8) then
  390. begin
  391. filename:=copy(filename,1,8);
  392. fnd:=SearchPath('.');
  393. if (not fnd) then
  394. fnd:=SearchPathList(LocalUnitSearchPath);
  395. if not fnd then
  396. fnd:=SearchPathList(UnitSearchPath);
  397. end;
  398. search_unit:=fnd;
  399. end;
  400. {**********************************
  401. PPU Reading/Writing Helpers
  402. ***********************************}
  403. {$IFDEF MACRO_DIFF_HINT}
  404. var
  405. is_initial: Boolean;
  406. procedure tppumodule.writeusedmacro(p:TNamedIndexItem;arg:pointer);
  407. begin
  408. if tmacro(p).is_used or is_initial then
  409. begin
  410. ppufile.putstring(p.name);
  411. ppufile.putbyte(byte(is_initial));
  412. ppufile.putbyte(byte(tmacro(p).is_used));
  413. end;
  414. end;
  415. procedure tppumodule.writeusedmacros;
  416. begin
  417. ppufile.do_crc:=false;
  418. is_initial:= true;
  419. initialmacrosymtable.foreach(@writeusedmacro,nil);
  420. is_initial:= false;
  421. if assigned(globalmacrosymtable) then
  422. globalmacrosymtable.foreach(@writeusedmacro,nil);
  423. localmacrosymtable.foreach(@writeusedmacro,nil);
  424. ppufile.writeentry(ibusedmacros);
  425. ppufile.do_crc:=true;
  426. end;
  427. {$ENDIF}
  428. procedure tppumodule.writesourcefiles;
  429. var
  430. hp : tinputfile;
  431. i,j : longint;
  432. begin
  433. { second write the used source files }
  434. ppufile.do_crc:=false;
  435. hp:=sourcefiles.files;
  436. { write source files directly in good order }
  437. j:=0;
  438. while assigned(hp) do
  439. begin
  440. inc(j);
  441. hp:=hp.ref_next;
  442. end;
  443. while j>0 do
  444. begin
  445. hp:=sourcefiles.files;
  446. for i:=1 to j-1 do
  447. hp:=hp.ref_next;
  448. ppufile.putstring(hp.name^);
  449. ppufile.putlongint(hp.getfiletime);
  450. dec(j);
  451. end;
  452. ppufile.writeentry(ibsourcefiles);
  453. ppufile.do_crc:=true;
  454. end;
  455. procedure tppumodule.writeusedunit(intf:boolean);
  456. var
  457. hp : tused_unit;
  458. oldcrc : boolean;
  459. begin
  460. { write a reference for each used unit }
  461. hp:=tused_unit(used_units.first);
  462. while assigned(hp) do
  463. begin
  464. if hp.in_interface=intf then
  465. begin
  466. ppufile.putstring(hp.u.realmodulename^);
  467. { the checksum should not affect the crc of this unit ! (PFV) }
  468. oldcrc:=ppufile.do_crc;
  469. ppufile.do_crc:=false;
  470. ppufile.putlongint(longint(hp.checksum));
  471. ppufile.putlongint(longint(hp.interface_checksum));
  472. ppufile.do_crc:=oldcrc;
  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. intfchecksum,
  748. checksum : cardinal;
  749. begin
  750. while not ppufile.endofentry do
  751. begin
  752. hs:=ppufile.getstring;
  753. checksum:=cardinal(ppufile.getlongint);
  754. intfchecksum:=cardinal(ppufile.getlongint);
  755. { set the state of this unit before registering, this is
  756. needed for a correct circular dependency check }
  757. hp:=registerunit(self,hs,'');
  758. pu:=addusedunit(hp,false,nil);
  759. pu.checksum:=checksum;
  760. pu.interface_checksum:=intfchecksum;
  761. end;
  762. in_interface:=false;
  763. end;
  764. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  765. var
  766. s : string;
  767. m : longint;
  768. begin
  769. while not ppufile.endofentry do
  770. begin
  771. s:=ppufile.getstring;
  772. m:=ppufile.getlongint;
  773. p.add(s,m);
  774. end;
  775. end;
  776. procedure tppumodule.readderefmap;
  777. var
  778. i : longint;
  779. begin
  780. { Load unit map used for resolving }
  781. derefmapsize:=ppufile.getlongint;
  782. derefmapcnt:=derefmapsize;
  783. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  784. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  785. for i:=0 to derefmapsize-1 do
  786. derefmap[i].modulename:=stringdup(ppufile.getstring);
  787. end;
  788. procedure tppumodule.readderefdata;
  789. var
  790. len,hlen : longint;
  791. buf : array[0..1023] of byte;
  792. begin
  793. len:=ppufile.entrysize;
  794. while (len>0) do
  795. begin
  796. if len>1024 then
  797. hlen:=1024
  798. else
  799. hlen:=len;
  800. ppufile.getdata(buf,hlen);
  801. derefdata.write(buf,hlen);
  802. dec(len,hlen);
  803. end;
  804. end;
  805. procedure tppumodule.readImportSymbols;
  806. var
  807. j,
  808. extsymcnt : longint;
  809. ImportLibrary : TImportLibrary;
  810. extsymname : string;
  811. extsymordnr : longint;
  812. extsymisvar : boolean;
  813. begin
  814. while not ppufile.endofentry do
  815. begin
  816. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  817. extsymcnt:=ppufile.getlongint;
  818. for j:=0 to extsymcnt-1 do
  819. begin
  820. extsymname:=ppufile.getstring;
  821. extsymordnr:=ppufile.getlongint;
  822. extsymisvar:=(ppufile.getbyte<>0);
  823. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,extsymordnr,extsymisvar);
  824. end;
  825. end;
  826. end;
  827. procedure tppumodule.readResources;
  828. begin
  829. while not ppufile.endofentry do
  830. resourcefiles.Insert(ppufile.getstring);
  831. end;
  832. procedure tppumodule.readwpofile;
  833. var
  834. orgwpofilename: string;
  835. orgwpofiletime: longint;
  836. begin
  837. { check whether we are using the same wpo feedback input file as when
  838. this unit was compiled (same file name and file date)
  839. }
  840. orgwpofilename:=ppufile.getstring;
  841. orgwpofiletime:=ppufile.getlongint;
  842. if (extractfilename(orgwpofilename)<>extractfilename(wpofeedbackinput)) or
  843. (orgwpofiletime<>GetNamedFileTime(orgwpofilename)) then
  844. { make sure we don't throw away a precompiled unit if the user simply
  845. forgot to specify the right wpo feedback file
  846. }
  847. message3(unit_e_different_wpo_file,ppufilename^,orgwpofilename,filetimestring(orgwpofiletime));
  848. end;
  849. procedure tppumodule.load_interface;
  850. var
  851. b : byte;
  852. newmodulename : string;
  853. begin
  854. { read interface part }
  855. repeat
  856. b:=ppufile.readentry;
  857. case b of
  858. ibmodulename :
  859. begin
  860. newmodulename:=ppufile.getstring;
  861. if (cs_check_unit_name in current_settings.globalswitches) and
  862. (upper(newmodulename)<>modulename^) then
  863. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  864. stringdispose(modulename);
  865. stringdispose(realmodulename);
  866. modulename:=stringdup(upper(newmodulename));
  867. realmodulename:=stringdup(newmodulename);
  868. end;
  869. ibsourcefiles :
  870. readsourcefiles;
  871. {$IFDEF MACRO_DIFF_HINT}
  872. ibusedmacros :
  873. readusedmacros;
  874. {$ENDIF}
  875. ibloadunit :
  876. readloadunit;
  877. iblinkunitofiles :
  878. readlinkcontainer(LinkUnitOFiles);
  879. iblinkunitstaticlibs :
  880. readlinkcontainer(LinkUnitStaticLibs);
  881. iblinkunitsharedlibs :
  882. readlinkcontainer(LinkUnitSharedLibs);
  883. iblinkotherofiles :
  884. readlinkcontainer(LinkotherOFiles);
  885. iblinkotherstaticlibs :
  886. readlinkcontainer(LinkotherStaticLibs);
  887. iblinkothersharedlibs :
  888. readlinkcontainer(LinkotherSharedLibs);
  889. iblinkotherframeworks :
  890. readlinkcontainer(LinkOtherFrameworks);
  891. ibmainname:
  892. begin
  893. mainname:=stringdup(ppufile.getstring);
  894. if (mainaliasname<>defaultmainaliasname) then
  895. Message1(scan_w_multiple_main_name_overrides,mainaliasname);
  896. mainaliasname:=mainname^;
  897. end;
  898. ibImportSymbols :
  899. readImportSymbols;
  900. ibderefmap :
  901. readderefmap;
  902. ibderefdata :
  903. readderefdata;
  904. ibresources:
  905. readResources;
  906. ibwpofile:
  907. readwpofile;
  908. ibendinterface :
  909. break;
  910. else
  911. Message1(unit_f_ppu_invalid_entry,tostr(b));
  912. end;
  913. { we can already stop when we know that we must recompile }
  914. if do_compile then
  915. exit;
  916. until false;
  917. end;
  918. procedure tppumodule.load_implementation;
  919. var
  920. b : byte;
  921. begin
  922. { read implementation part }
  923. repeat
  924. b:=ppufile.readentry;
  925. case b of
  926. ibloadunit :
  927. readloadunit;
  928. ibasmsymbols :
  929. { TODO: Remove ibasmsymbols}
  930. ;
  931. ibendimplementation :
  932. break;
  933. else
  934. Message1(unit_f_ppu_invalid_entry,tostr(b));
  935. end;
  936. until false;
  937. end;
  938. procedure tppumodule.writeppu;
  939. begin
  940. Message1(unit_u_ppu_write,realmodulename^);
  941. { create unit flags }
  942. if do_release then
  943. flags:=flags or uf_release;
  944. if assigned(localsymtable) then
  945. flags:=flags or uf_local_symtable;
  946. {$ifdef cpufpemu}
  947. if (cs_fp_emulation in current_settings.moduleswitches) then
  948. flags:=flags or uf_fpu_emulation;
  949. {$endif cpufpemu}
  950. {$ifdef Test_Double_checksum_write}
  951. Assign(CRCFile,s+'.IMP');
  952. Rewrite(CRCFile);
  953. {$endif def Test_Double_checksum_write}
  954. { create new ppufile }
  955. ppufile:=tcompilerppufile.create(ppufilename^);
  956. if not ppufile.createfile then
  957. Message(unit_f_ppu_cannot_write);
  958. { first the unitname }
  959. ppufile.putstring(realmodulename^);
  960. ppufile.writeentry(ibmodulename);
  961. { write the alternate main procedure name if any }
  962. if assigned(mainname) then
  963. begin
  964. ppufile.putstring(mainname^);
  965. ppufile.writeentry(ibmainname);
  966. end;
  967. writesourcefiles;
  968. {$IFDEF MACRO_DIFF_HINT}
  969. writeusedmacros;
  970. {$ENDIF}
  971. { write interface uses }
  972. writeusedunit(true);
  973. { write the objectfiles and libraries that come for this unit,
  974. preserve the containers becuase they are still needed to load
  975. the link.res.
  976. All doesn't depend on the crc! It doesn't matter
  977. if a unit is in a .o or .a file }
  978. ppufile.do_crc:=false;
  979. { write after source files, so that we know whether or not the compiler
  980. will recompile the unit when checking whether the correct wpo file is
  981. used (if it will recompile the unit anyway, it doesn't matter)
  982. }
  983. if (wpofeedbackinput<>'') then
  984. begin
  985. ppufile.putstring(wpofeedbackinput);
  986. ppufile.putlongint(getnamedfiletime(wpofeedbackinput));
  987. ppufile.writeentry(ibwpofile);
  988. end;
  989. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  990. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  991. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  992. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  993. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  994. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  995. writelinkcontainer(linkotherframeworks,iblinkotherframeworks,true);
  996. writeImportSymbols;
  997. writeResources;
  998. ppufile.do_crc:=true;
  999. { generate implementation deref data, the interface deref data is
  1000. already generated when calculating the interface crc }
  1001. if (cs_compilesystem in current_settings.moduleswitches) then
  1002. begin
  1003. tstoredsymtable(globalsymtable).buildderef;
  1004. derefdataintflen:=derefdata.size;
  1005. end
  1006. else
  1007. { the unit may have been re-resolved, in which case the current
  1008. position in derefdata is not necessarily at the end }
  1009. derefdata.seek(derefdata.size);
  1010. tstoredsymtable(globalsymtable).buildderefimpl;
  1011. if (flags and uf_local_symtable)<>0 then
  1012. begin
  1013. tstoredsymtable(localsymtable).buildderef;
  1014. tstoredsymtable(localsymtable).buildderefimpl;
  1015. end;
  1016. tunitwpoinfo(wpoinfo).buildderef;
  1017. tunitwpoinfo(wpoinfo).buildderefimpl;
  1018. writederefmap;
  1019. writederefdata;
  1020. ppufile.writeentry(ibendinterface);
  1021. { write the symtable entries }
  1022. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1023. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1024. begin
  1025. ppufile.putbyte(byte(true));
  1026. ppufile.writeentry(ibexportedmacros);
  1027. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1028. end
  1029. else
  1030. begin
  1031. ppufile.putbyte(byte(false));
  1032. ppufile.writeentry(ibexportedmacros);
  1033. end;
  1034. { everything after this doesn't affect the crc }
  1035. ppufile.do_crc:=false;
  1036. { write implementation uses }
  1037. writeusedunit(false);
  1038. { end of implementation }
  1039. ppufile.writeentry(ibendimplementation);
  1040. { write static symtable
  1041. needed for local debugging of unit functions }
  1042. if (flags and uf_local_symtable)<>0 then
  1043. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  1044. { write whole program optimisation-related information }
  1045. tunitwpoinfo(wpoinfo).ppuwrite(ppufile);
  1046. { the last entry ibend is written automaticly }
  1047. { flush to be sure }
  1048. ppufile.flush;
  1049. { create and write header }
  1050. ppufile.header.size:=ppufile.size;
  1051. ppufile.header.checksum:=ppufile.crc;
  1052. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1053. ppufile.header.compiler:=wordversion;
  1054. ppufile.header.cpu:=word(target_cpu);
  1055. ppufile.header.target:=word(target_info.system);
  1056. ppufile.header.flags:=flags;
  1057. ppufile.header.deflistsize:=current_module.deflist.count;
  1058. ppufile.header.symlistsize:=current_module.symlist.count;
  1059. ppufile.writeheader;
  1060. { save crc in current module also }
  1061. crc:=ppufile.crc;
  1062. interface_crc:=ppufile.interface_crc;
  1063. {$ifdef Test_Double_checksum_write}
  1064. close(CRCFile);
  1065. {$endif Test_Double_checksum_write}
  1066. ppufile.closefile;
  1067. ppufile.free;
  1068. ppufile:=nil;
  1069. end;
  1070. procedure tppumodule.getppucrc;
  1071. begin
  1072. {$ifdef Test_Double_checksum_write}
  1073. Assign(CRCFile,s+'.INT')
  1074. Rewrite(CRCFile);
  1075. {$endif def Test_Double_checksum_write}
  1076. { create new ppufile }
  1077. ppufile:=tcompilerppufile.create(ppufilename^);
  1078. ppufile.crc_only:=true;
  1079. if not ppufile.createfile then
  1080. Message(unit_f_ppu_cannot_write);
  1081. { first the unitname }
  1082. ppufile.putstring(realmodulename^);
  1083. ppufile.writeentry(ibmodulename);
  1084. { the interface units affect the crc }
  1085. writeusedunit(true);
  1086. { deref data of interface that affect the crc }
  1087. derefdata.reset;
  1088. tstoredsymtable(globalsymtable).buildderef;
  1089. derefdataintflen:=derefdata.size;
  1090. writederefmap;
  1091. writederefdata;
  1092. ppufile.writeentry(ibendinterface);
  1093. { write the symtable entries }
  1094. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1095. if assigned(globalmacrosymtable) and (globalmacrosymtable.SymList.count > 0) then
  1096. begin
  1097. ppufile.putbyte(byte(true));
  1098. ppufile.writeentry(ibexportedmacros);
  1099. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1100. end
  1101. else
  1102. begin
  1103. ppufile.putbyte(byte(false));
  1104. ppufile.writeentry(ibexportedmacros);
  1105. end;
  1106. { save crc }
  1107. crc:=ppufile.crc;
  1108. interface_crc:=ppufile.interface_crc;
  1109. { end of implementation, to generate a correct ppufile
  1110. for ppudump when using INTFPPU define }
  1111. ppufile.writeentry(ibendimplementation);
  1112. {$ifdef Test_Double_checksum}
  1113. crc_array:=ppufile.crc_test;
  1114. ppufile.crc_test:=nil;
  1115. crc_size:=ppufile.crc_index2;
  1116. crc_array2:=ppufile.crc_test2;
  1117. ppufile.crc_test2:=nil;
  1118. crc_size2:=ppufile.crc_index2;
  1119. {$endif Test_Double_checksum}
  1120. {$ifdef Test_Double_checksum_write}
  1121. close(CRCFile);
  1122. {$endif Test_Double_checksum_write}
  1123. { create and write header, this will only be used
  1124. for debugging purposes }
  1125. ppufile.header.size:=ppufile.size;
  1126. ppufile.header.checksum:=ppufile.crc;
  1127. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1128. ppufile.header.compiler:=wordversion;
  1129. ppufile.header.cpu:=word(target_cpu);
  1130. ppufile.header.target:=word(target_info.system);
  1131. ppufile.header.flags:=flags;
  1132. ppufile.writeheader;
  1133. ppufile.closefile;
  1134. ppufile.free;
  1135. ppufile:=nil;
  1136. end;
  1137. procedure tppumodule.load_usedunits;
  1138. var
  1139. pu : tused_unit;
  1140. begin
  1141. if current_module<>self then
  1142. internalerror(200212284);
  1143. { load the used units from interface }
  1144. in_interface:=true;
  1145. pu:=tused_unit(used_units.first);
  1146. while assigned(pu) do
  1147. begin
  1148. if pu.in_interface then
  1149. begin
  1150. tppumodule(pu.u).loadppu;
  1151. { if this unit is compiled we can stop }
  1152. if state=ms_compiled then
  1153. exit;
  1154. { add this unit to the dependencies }
  1155. pu.u.adddependency(self);
  1156. { need to recompile the current unit, check the interface
  1157. crc. And when not compiled with -Ur then check the complete
  1158. crc }
  1159. if (pu.u.interface_crc<>pu.interface_checksum) or
  1160. (
  1161. ((ppufile.header.flags and uf_release)=0) and
  1162. (pu.u.crc<>pu.checksum)
  1163. ) then
  1164. begin
  1165. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^,@queuecomment);
  1166. recompile_reason:=rr_crcchanged;
  1167. do_compile:=true;
  1168. exit;
  1169. end;
  1170. end;
  1171. pu:=tused_unit(pu.next);
  1172. end;
  1173. { ok, now load the interface of this unit }
  1174. if current_module<>self then
  1175. internalerror(200208187);
  1176. deflist.count:=ppufile.header.deflistsize;
  1177. symlist.count:=ppufile.header.symlistsize;
  1178. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1179. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1180. if ppufile.readentry<>ibexportedmacros then
  1181. Message(unit_f_ppu_read_error);
  1182. if boolean(ppufile.getbyte) then
  1183. begin
  1184. globalmacrosymtable:=tmacrosymtable.Create(true);
  1185. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1186. end;
  1187. interface_compiled:=true;
  1188. { read the implementation part, containing
  1189. the implementation uses and ObjData }
  1190. in_interface:=false;
  1191. load_implementation;
  1192. { now only read the implementation uses }
  1193. pu:=tused_unit(used_units.first);
  1194. while assigned(pu) do
  1195. begin
  1196. if (not pu.in_interface) then
  1197. begin
  1198. tppumodule(pu.u).loadppu;
  1199. { if this unit is compiled we can stop }
  1200. if state=ms_compiled then
  1201. exit;
  1202. { add this unit to the dependencies }
  1203. pu.u.adddependency(self);
  1204. { need to recompile the current unit ? }
  1205. if (pu.u.interface_crc<>pu.interface_checksum) then
  1206. begin
  1207. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}',@queuecomment);
  1208. recompile_reason:=rr_crcchanged;
  1209. do_compile:=true;
  1210. exit;
  1211. end;
  1212. end;
  1213. pu:=tused_unit(pu.next);
  1214. end;
  1215. { load implementation symtable }
  1216. if (flags and uf_local_symtable)<>0 then
  1217. begin
  1218. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1219. tstaticsymtable(localsymtable).ppuload(ppufile);
  1220. end;
  1221. { we can now derefence all pointers to the implementation parts }
  1222. tstoredsymtable(globalsymtable).derefimpl;
  1223. if assigned(localsymtable) then
  1224. tstoredsymtable(localsymtable).derefimpl;
  1225. { read whole program optimisation-related information }
  1226. wpoinfo:=tunitwpoinfo.ppuload(ppufile);
  1227. tunitwpoinfo(wpoinfo).deref;
  1228. tunitwpoinfo(wpoinfo).derefimpl;
  1229. end;
  1230. function tppumodule.needrecompile:boolean;
  1231. var
  1232. pu : tused_unit;
  1233. begin
  1234. result:=false;
  1235. pu:=tused_unit(used_units.first);
  1236. while assigned(pu) do
  1237. begin
  1238. { need to recompile the current unit, check the interface
  1239. crc. And when not compiled with -Ur then check the complete
  1240. crc }
  1241. if (pu.u.interface_crc<>pu.interface_checksum) or
  1242. (
  1243. (pu.in_interface) and
  1244. (pu.u.crc<>pu.checksum)
  1245. ) then
  1246. begin
  1247. result:=true;
  1248. exit;
  1249. end;
  1250. pu:=tused_unit(pu.next);
  1251. end;
  1252. end;
  1253. procedure tppumodule.loadppu;
  1254. const
  1255. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1256. var
  1257. do_load,
  1258. second_time : boolean;
  1259. old_current_module : tmodule;
  1260. begin
  1261. old_current_module:=current_module;
  1262. Message3(unit_u_load_unit,old_current_module.modulename^,
  1263. ImplIntf[old_current_module.in_interface],
  1264. modulename^);
  1265. { Update loaded_from to detect cycles }
  1266. loaded_from:=old_current_module;
  1267. { check if the globalsymtable is already available, but
  1268. we must reload when the do_reload flag is set }
  1269. if (not do_reload) and
  1270. assigned(globalsymtable) then
  1271. exit;
  1272. { reset }
  1273. do_load:=true;
  1274. second_time:=false;
  1275. set_current_module(self);
  1276. { A force reload }
  1277. if do_reload then
  1278. begin
  1279. Message(unit_u_forced_reload);
  1280. do_reload:=false;
  1281. { When the unit is already loaded or being loaded
  1282. we can maybe skip a complete reload/recompile }
  1283. if assigned(globalsymtable) and
  1284. (not needrecompile) then
  1285. begin
  1286. { When we don't have any data stored yet there
  1287. is nothing to resolve }
  1288. if interface_compiled then
  1289. begin
  1290. Message1(unit_u_reresolving_unit,modulename^);
  1291. tstoredsymtable(globalsymtable).deref;
  1292. tstoredsymtable(globalsymtable).derefimpl;
  1293. if assigned(localsymtable) then
  1294. begin
  1295. tstoredsymtable(localsymtable).deref;
  1296. tstoredsymtable(localsymtable).derefimpl;
  1297. end;
  1298. tunitwpoinfo(wpoinfo).deref;
  1299. tunitwpoinfo(wpoinfo).derefimpl;
  1300. end
  1301. else
  1302. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1303. do_load:=false;
  1304. end;
  1305. end;
  1306. if do_load then
  1307. begin
  1308. { loading the unit for a second time? }
  1309. if state=ms_registered then
  1310. state:=ms_load
  1311. else
  1312. begin
  1313. { try to load the unit a second time first }
  1314. Message1(unit_u_second_load_unit,modulename^);
  1315. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1316. { Flag modules to reload }
  1317. flagdependent(old_current_module);
  1318. { Reset the module }
  1319. reset;
  1320. if state in [ms_compile,ms_second_compile] then
  1321. begin
  1322. Message1(unit_u_second_compile_unit,modulename^);
  1323. state:=ms_second_compile;
  1324. do_compile:=true;
  1325. end
  1326. else
  1327. state:=ms_second_load;
  1328. second_time:=true;
  1329. end;
  1330. { close old_current_ppu on system that are
  1331. short on file handles like DOS PM }
  1332. {$ifdef SHORT_ON_FILE_HANDLES}
  1333. if old_current_module.is_unit and
  1334. assigned(tppumodule(old_current_module).ppufile) then
  1335. tppumodule(old_current_module).ppufile.tempclose;
  1336. {$endif SHORT_ON_FILE_HANDLES}
  1337. { try to opening ppu, skip this when we already
  1338. know that we need to compile the unit }
  1339. if not do_compile then
  1340. begin
  1341. Message1(unit_u_loading_unit,modulename^);
  1342. search_unit(false,false);
  1343. if not do_compile then
  1344. begin
  1345. load_interface;
  1346. if not do_compile then
  1347. begin
  1348. load_usedunits;
  1349. if not do_compile then
  1350. Message1(unit_u_finished_loading_unit,modulename^);
  1351. end;
  1352. end;
  1353. { PPU is not needed anymore }
  1354. if assigned(ppufile) then
  1355. begin
  1356. ppufile.closefile;
  1357. ppufile.free;
  1358. ppufile:=nil;
  1359. end;
  1360. end;
  1361. { Do we need to recompile the unit }
  1362. if do_compile then
  1363. begin
  1364. { recompile the unit or give a fatal error if sources not available }
  1365. if not(sources_avail) then
  1366. begin
  1367. if (not search_unit(true,false)) and
  1368. (length(modulename^)>8) then
  1369. search_unit(true,true);
  1370. if not(sources_avail) then
  1371. begin
  1372. printcomments;
  1373. if recompile_reason=rr_noppu then
  1374. Message2(unit_f_cant_find_ppu,realmodulename^,loaded_from.realmodulename^)
  1375. else
  1376. Message1(unit_f_cant_compile_unit,realmodulename^);
  1377. end;
  1378. end;
  1379. { we found the sources, we do not need the verbose messages anymore }
  1380. if comments <> nil then
  1381. begin
  1382. comments.free;
  1383. comments:=nil;
  1384. end;
  1385. { Flag modules to reload }
  1386. flagdependent(old_current_module);
  1387. { Reset the module }
  1388. reset;
  1389. { compile this module }
  1390. if not(state in [ms_compile,ms_second_compile]) then
  1391. state:=ms_compile;
  1392. compile(mainsource^);
  1393. end
  1394. else
  1395. state:=ms_compiled;
  1396. if current_module<>self then
  1397. internalerror(200212282);
  1398. if in_interface then
  1399. internalerror(200212283);
  1400. { for a second_time recompile reload all dependent units,
  1401. for a first time compile register the unit _once_ }
  1402. if second_time then
  1403. reload_flagged_units
  1404. else
  1405. usedunits.concat(tused_unit.create(self,true,false,nil));
  1406. { reopen the old module }
  1407. {$ifdef SHORT_ON_FILE_HANDLES}
  1408. if old_current_module.is_unit and
  1409. assigned(tppumodule(old_current_module).ppufile) then
  1410. tppumodule(old_current_module).ppufile.tempopen;
  1411. {$endif SHORT_ON_FILE_HANDLES}
  1412. end;
  1413. { we are back, restore current_module }
  1414. set_current_module(old_current_module);
  1415. end;
  1416. {*****************************************************************************
  1417. RegisterUnit
  1418. *****************************************************************************}
  1419. function registerunit(callermodule:tmodule;const s : TIDString;const fn:string) : tppumodule;
  1420. var
  1421. ups : TIDString;
  1422. hp : tppumodule;
  1423. hp2 : tmodule;
  1424. begin
  1425. { Info }
  1426. ups:=upper(s);
  1427. { search all loaded units }
  1428. hp:=tppumodule(loaded_units.first);
  1429. while assigned(hp) do
  1430. begin
  1431. if hp.modulename^=ups then
  1432. begin
  1433. { only check for units. The main program is also
  1434. as a unit in the loaded_units list. We simply need
  1435. to ignore this entry (PFV) }
  1436. if hp.is_unit then
  1437. begin
  1438. { both units in interface ? }
  1439. if callermodule.in_interface and
  1440. hp.in_interface then
  1441. begin
  1442. { check for a cycle }
  1443. hp2:=callermodule.loaded_from;
  1444. while assigned(hp2) and (hp2<>hp) do
  1445. begin
  1446. if hp2.in_interface then
  1447. hp2:=hp2.loaded_from
  1448. else
  1449. hp2:=nil;
  1450. end;
  1451. if assigned(hp2) then
  1452. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1453. end;
  1454. break;
  1455. end;
  1456. end;
  1457. { the next unit }
  1458. hp:=tppumodule(hp.next);
  1459. end;
  1460. { the unit is not in the loaded units,
  1461. we create an entry and register the unit }
  1462. if not assigned(hp) then
  1463. begin
  1464. Message1(unit_u_registering_new_unit,Upper(s));
  1465. hp:=tppumodule.create(callermodule,s,fn,true);
  1466. hp.loaded_from:=callermodule;
  1467. addloadedunit(hp);
  1468. end;
  1469. { return }
  1470. registerunit:=hp;
  1471. end;
  1472. end.