fppu.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  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 = class(tmodule)
  34. ppufile : tcompilerppufile; { the PPU file }
  35. sourcefn : pshortstring; { Source specified with "uses .. in '..'" }
  36. comments : tstringlist;
  37. {$ifdef Test_Double_checksum}
  38. crc_array : pointer;
  39. crc_size : longint;
  40. crc_array2 : pointer;
  41. crc_size2 : longint;
  42. {$endif def Test_Double_checksum}
  43. constructor create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  44. destructor destroy;override;
  45. procedure reset;override;
  46. function openppu:boolean;
  47. procedure getppucrc;
  48. procedure writeppu;
  49. procedure loadppu;
  50. function needrecompile:boolean;
  51. private
  52. function search_unit(onlysource,shortname:boolean):boolean;
  53. procedure load_interface;
  54. procedure load_implementation;
  55. procedure load_symtable_refs;
  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 readsourcefiles;
  66. procedure readloadunit;
  67. procedure readlinkcontainer(var p:tlinkcontainer);
  68. procedure readderefmap;
  69. procedure readderefdata;
  70. procedure readImportSymbols;
  71. {$IFDEF MACRO_DIFF_HINT}
  72. procedure writeusedmacro(p:TNamedIndexItem;arg:pointer);
  73. procedure writeusedmacros;
  74. procedure readusedmacros;
  75. {$ENDIF}
  76. end;
  77. procedure reload_flagged_units;
  78. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  79. implementation
  80. uses
  81. SysUtils,
  82. cfileutils,
  83. verbose,systems,version,
  84. symtable, symsym,
  85. scanner,
  86. aasmbase,ogbase,
  87. parser,
  88. comphook;
  89. {****************************************************************************
  90. Helpers
  91. ****************************************************************************}
  92. procedure reload_flagged_units;
  93. var
  94. hp : tmodule;
  95. begin
  96. { now reload all dependent units }
  97. hp:=tmodule(loaded_units.first);
  98. while assigned(hp) do
  99. begin
  100. if hp.do_reload then
  101. tppumodule(hp).loadppu;
  102. hp:=tmodule(hp.next);
  103. end;
  104. end;
  105. {****************************************************************************
  106. TPPUMODULE
  107. ****************************************************************************}
  108. constructor tppumodule.create(LoadedFrom:TModule;const s:string;const fn:string;_is_unit:boolean);
  109. begin
  110. inherited create(LoadedFrom,s,_is_unit);
  111. ppufile:=nil;
  112. sourcefn:=stringdup(fn);
  113. end;
  114. destructor tppumodule.Destroy;
  115. begin
  116. if assigned(ppufile) then
  117. ppufile.free;
  118. ppufile:=nil;
  119. comments.free;
  120. comments:=nil;
  121. stringdispose(sourcefn);
  122. inherited Destroy;
  123. end;
  124. procedure tppumodule.reset;
  125. begin
  126. if assigned(ppufile) then
  127. begin
  128. ppufile.free;
  129. ppufile:=nil;
  130. end;
  131. inherited reset;
  132. end;
  133. procedure tppumodule.queuecomment(s:string;v,w:longint);
  134. begin
  135. if comments = nil then
  136. comments := tstringlist.create;
  137. comments.insert(s);
  138. end;
  139. procedure tppumodule.printcomments;
  140. var
  141. comment: string;
  142. begin
  143. if comments = nil then
  144. exit;
  145. { comments are inserted in reverse order }
  146. repeat
  147. comment := comments.getlast;
  148. if length(comment) = 0 then
  149. exit;
  150. do_comment(v_normal, comment);
  151. until false;
  152. end;
  153. function tppumodule.openppu:boolean;
  154. var
  155. ppufiletime : longint;
  156. begin
  157. openppu:=false;
  158. Message1(unit_t_ppu_loading,ppufilename^,@queuecomment);
  159. { Get ppufile time (also check if the file exists) }
  160. ppufiletime:=getnamedfiletime(ppufilename^);
  161. if ppufiletime=-1 then
  162. exit;
  163. { Open the ppufile }
  164. Message1(unit_u_ppu_name,ppufilename^);
  165. ppufile:=tcompilerppufile.create(ppufilename^);
  166. if not ppufile.openfile then
  167. begin
  168. ppufile.free;
  169. ppufile:=nil;
  170. Message(unit_u_ppu_file_too_short);
  171. exit;
  172. end;
  173. { check for a valid PPU file }
  174. if not ppufile.CheckPPUId then
  175. begin
  176. ppufile.free;
  177. ppufile:=nil;
  178. Message(unit_u_ppu_invalid_header);
  179. exit;
  180. end;
  181. { check for allowed PPU versions }
  182. if not (ppufile.GetPPUVersion = CurrentPPUVersion) then
  183. begin
  184. Message1(unit_u_ppu_invalid_version,tostr(ppufile.GetPPUVersion),@queuecomment);
  185. ppufile.free;
  186. ppufile:=nil;
  187. exit;
  188. end;
  189. { check the target processor }
  190. if tsystemcpu(ppufile.header.cpu)<>target_cpu then
  191. begin
  192. ppufile.free;
  193. ppufile:=nil;
  194. Message(unit_u_ppu_invalid_processor,@queuecomment);
  195. exit;
  196. end;
  197. { check target }
  198. if tsystem(ppufile.header.target)<>target_info.system then
  199. begin
  200. ppufile.free;
  201. ppufile:=nil;
  202. Message(unit_u_ppu_invalid_target,@queuecomment);
  203. exit;
  204. end;
  205. {$ifdef cpufpemu}
  206. { check if floating point emulation is on?}
  207. { fpu emulation isn't unit levelwise
  208. if ((ppufile.header.flags and uf_fpu_emulation)<>0) and
  209. (cs_fp_emulation in current_settings.moduleswitches) then
  210. begin
  211. ppufile.free;
  212. ppufile:=nil;
  213. Message(unit_u_ppu_invalid_fpumode);
  214. exit;
  215. end;
  216. }
  217. {$endif cpufpemu}
  218. { Load values to be access easier }
  219. flags:=ppufile.header.flags;
  220. crc:=ppufile.header.checksum;
  221. interface_crc:=ppufile.header.interface_checksum;
  222. { Show Debug info }
  223. Message1(unit_u_ppu_time,filetimestring(ppufiletime));
  224. Message1(unit_u_ppu_flags,tostr(flags));
  225. Message1(unit_u_ppu_crc,hexstr(ppufile.header.checksum,8));
  226. Message1(unit_u_ppu_crc,hexstr(ppufile.header.interface_checksum,8)+' (intfc)');
  227. Comment(V_used,'Number of definitions: '+tostr(ppufile.header.deflistsize));
  228. Comment(V_used,'Number of symbols: '+tostr(ppufile.header.symlistsize));
  229. do_compile:=false;
  230. openppu:=true;
  231. end;
  232. function tppumodule.search_unit(onlysource,shortname:boolean):boolean;
  233. var
  234. singlepathstring,
  235. filename : string;
  236. Function UnitExists(const ext:string;var foundfile:string):boolean;
  237. begin
  238. Message1(unit_t_unitsearch,Singlepathstring+filename+ext);
  239. UnitExists:=FindFile(FileName+ext,Singlepathstring,true,foundfile);
  240. end;
  241. Function PPUSearchPath(const s:string):boolean;
  242. var
  243. found : boolean;
  244. hs : string;
  245. begin
  246. Found:=false;
  247. singlepathstring:=FixPath(s,false);
  248. { Check for PPU file }
  249. Found:=UnitExists(target_info.unitext,hs);
  250. if Found then
  251. Begin
  252. SetFileName(hs,false);
  253. Found:=OpenPPU;
  254. End;
  255. PPUSearchPath:=Found;
  256. end;
  257. Function SourceSearchPath(const s:string):boolean;
  258. var
  259. found : boolean;
  260. hs : string;
  261. begin
  262. Found:=false;
  263. singlepathstring:=FixPath(s,false);
  264. { Check for Sources }
  265. ppufile:=nil;
  266. do_compile:=true;
  267. recompile_reason:=rr_noppu;
  268. {Check for .pp file}
  269. Found:=UnitExists(sourceext,hs);
  270. if not Found then
  271. begin
  272. { Check for .pas }
  273. Found:=UnitExists(pasext,hs);
  274. end;
  275. if not Found and (m_mac in current_settings.modeswitches) then
  276. begin
  277. { Check for .p, if mode is macpas}
  278. Found:=UnitExists(pext,hs);
  279. end;
  280. stringdispose(mainsource);
  281. if Found then
  282. begin
  283. sources_avail:=true;
  284. { Load Filenames when found }
  285. mainsource:=StringDup(hs);
  286. SetFileName(hs,false);
  287. end
  288. else
  289. sources_avail:=false;
  290. SourceSearchPath:=Found;
  291. end;
  292. Function SearchPath(const s:string):boolean;
  293. var
  294. found : boolean;
  295. begin
  296. { First check for a ppu, then for the source }
  297. found:=false;
  298. if not onlysource then
  299. found:=PPUSearchPath(s);
  300. if not found then
  301. found:=SourceSearchPath(s);
  302. SearchPath:=found;
  303. end;
  304. Function SearchPathList(list:TSearchPathList):boolean;
  305. var
  306. hp : TStringListItem;
  307. found : boolean;
  308. begin
  309. found:=false;
  310. hp:=TStringListItem(list.First);
  311. while assigned(hp) do
  312. begin
  313. found:=SearchPath(hp.Str);
  314. if found then
  315. break;
  316. hp:=TStringListItem(hp.next);
  317. end;
  318. SearchPathList:=found;
  319. end;
  320. var
  321. fnd : boolean;
  322. hs : string;
  323. begin
  324. if shortname then
  325. filename:=FixFileName(Copy(realmodulename^,1,8))
  326. else
  327. filename:=FixFileName(realmodulename^);
  328. { try to find unit
  329. 1. look for ppu in cwd
  330. 2. look for ppu in outputpath if set, this is tp7 compatible (PFV)
  331. 3. look for the specified source file (from the uses line)
  332. 4. look for source in cwd
  333. 5. look in same path as local unit
  334. 6. local unit pathlist
  335. 7. global unit pathlist }
  336. fnd:=false;
  337. if not onlysource then
  338. begin
  339. fnd:=PPUSearchPath('.');
  340. if (not fnd) and (outputpath^<>'') then
  341. fnd:=PPUSearchPath(outputpath^);
  342. end;
  343. if (not fnd) and (sourcefn^<>'') then
  344. begin
  345. { the full filename is specified so we can't use here the
  346. searchpath (PFV) }
  347. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,sourceext));
  348. fnd:=FindFile(ChangeFileExt(sourcefn^,sourceext),'',true,hs);
  349. if not fnd then
  350. begin
  351. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pasext));
  352. fnd:=FindFile(ChangeFileExt(sourcefn^,pasext),'',true,hs);
  353. end;
  354. if not fnd and ((m_mac in current_settings.modeswitches) or (tf_p_ext_support in target_info.flags)) then
  355. begin
  356. Message1(unit_t_unitsearch,ChangeFileExt(sourcefn^,pext));
  357. fnd:=FindFile(ChangeFileExt(sourcefn^,pext),'',true,hs);
  358. end;
  359. if fnd then
  360. begin
  361. sources_avail:=true;
  362. do_compile:=true;
  363. recompile_reason:=rr_noppu;
  364. stringdispose(mainsource);
  365. mainsource:=StringDup(hs);
  366. SetFileName(hs,false);
  367. end;
  368. end;
  369. if not fnd then
  370. fnd:=SourceSearchPath('.');
  371. if (not fnd) and Assigned(Loaded_From) then
  372. begin
  373. fnd:=PPUSearchPath(Loaded_From.Path^);
  374. if not fnd then
  375. fnd:=SourceSearchPath(Loaded_From.Path^);
  376. if not fnd then
  377. fnd:=SearchPathList(Loaded_From.LocalUnitSearchPath);
  378. end;
  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 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. {$IFDEF MACRO_DIFF_HINT}
  575. {
  576. Define MACRO_DIFF_HINT for the whole compiler (and ppudump)
  577. to turn this facility on. Also the hint messages defined
  578. below must be commented in in the msg/errore.msg file.
  579. There is some problems with this, thats why it is shut off:
  580. At the first compilation, consider a macro which is not initially
  581. defined, but it is used (e g the check that it is undefined is true).
  582. Since it do not exist, there is no macro object where the is_used
  583. flag can be set. Later on when the macro is defined, and the ppu
  584. is opened, the check cannot detect this.
  585. Also, in which macro object should this flag be set ? It cant be set
  586. for macros in the initialmacrosymboltable since this table is shared
  587. between different files.
  588. }
  589. procedure tppumodule.readusedmacros;
  590. var
  591. hs : string;
  592. mac : tmacro;
  593. was_initial,
  594. was_used : boolean;
  595. {Reads macros which was defined or used when the module was compiled.
  596. This is done when a ppu file is open, before it possibly is parsed.}
  597. begin
  598. while not ppufile.endofentry do
  599. begin
  600. hs:=ppufile.getstring;
  601. was_initial:=boolean(ppufile.getbyte);
  602. was_used:=boolean(ppufile.getbyte);
  603. mac:=tmacro(initialmacrosymtable.search(hs));
  604. if assigned(mac) then
  605. begin
  606. {$ifndef EXTDEBUG}
  607. { if we don't have the sources why tell }
  608. if sources_avail then
  609. {$endif ndef EXTDEBUG}
  610. if (not was_initial) and was_used then
  611. Message2(unit_h_cond_not_set_in_last_compile,hs,mainsource^);
  612. end
  613. else { not assigned }
  614. if was_initial and
  615. was_used then
  616. Message2(unit_h_cond_set_in_last_compile,hs,mainsource^);
  617. end;
  618. end;
  619. {$ENDIF}
  620. procedure tppumodule.readsourcefiles;
  621. var
  622. temp,hs : string;
  623. temp_dir : string;
  624. main_dir : string;
  625. found,
  626. is_main : boolean;
  627. orgfiletime,
  628. source_time : longint;
  629. hp : tinputfile;
  630. begin
  631. sources_avail:=(flags and uf_release) = 0;
  632. if not sources_avail then
  633. exit;
  634. is_main:=true;
  635. main_dir:='';
  636. while not ppufile.endofentry do
  637. begin
  638. hs:=ppufile.getstring;
  639. orgfiletime:=ppufile.getlongint;
  640. temp_dir:='';
  641. if (flags and uf_in_library)<>0 then
  642. begin
  643. sources_avail:=false;
  644. temp:=' library';
  645. end
  646. else if pos('Macro ',hs)=1 then
  647. begin
  648. { we don't want to find this file }
  649. { but there is a problem with file indexing !! }
  650. temp:='';
  651. end
  652. else
  653. begin
  654. { check the date of the source files:
  655. 1 path of ppu
  656. 2 path of main source
  657. 3 current dir
  658. 4 include/unit path }
  659. Source_Time:=GetNamedFileTime(path^+hs);
  660. found:=false;
  661. if Source_Time<>-1 then
  662. hs:=path^+hs
  663. else
  664. if not(is_main) then
  665. begin
  666. Source_Time:=GetNamedFileTime(main_dir+hs);
  667. if Source_Time<>-1 then
  668. hs:=main_dir+hs;
  669. end;
  670. if Source_Time=-1 then
  671. Source_Time:=GetNamedFileTime(hs);
  672. if (Source_Time=-1) then
  673. begin
  674. if is_main then
  675. found:=unitsearchpath.FindFile(hs,true,temp_dir)
  676. else
  677. found:=includesearchpath.FindFile(hs,true,temp_dir);
  678. if found then
  679. begin
  680. Source_Time:=GetNamedFileTime(temp_dir);
  681. if Source_Time<>-1 then
  682. hs:=temp_dir;
  683. end;
  684. end;
  685. if Source_Time<>-1 then
  686. begin
  687. if is_main then
  688. main_dir:=ExtractFilePath(hs);
  689. temp:=' time '+filetimestring(source_time);
  690. if (orgfiletime<>-1) and
  691. (source_time<>orgfiletime) then
  692. begin
  693. do_compile:=true;
  694. recompile_reason:=rr_sourcenewer;
  695. Message2(unit_u_source_modified,hs,ppufilename^);
  696. temp:=temp+' *';
  697. end;
  698. end
  699. else
  700. begin
  701. sources_avail:=false;
  702. temp:=' not found';
  703. end;
  704. hp:=tinputfile.create(hs);
  705. { the indexing is wrong here PM }
  706. sourcefiles.register_file(hp);
  707. end;
  708. if is_main then
  709. begin
  710. stringdispose(mainsource);
  711. mainsource:=stringdup(hs);
  712. end;
  713. Message1(unit_u_ppu_source,hs+temp);
  714. is_main:=false;
  715. end;
  716. { check if we want to rebuild every unit, only if the sources are
  717. available }
  718. if do_build and sources_avail then
  719. begin
  720. do_compile:=true;
  721. recompile_reason:=rr_build;
  722. end;
  723. end;
  724. procedure tppumodule.readloadunit;
  725. var
  726. hs : string;
  727. pu : tused_unit;
  728. hp : tppumodule;
  729. intfchecksum,
  730. checksum : cardinal;
  731. begin
  732. while not ppufile.endofentry do
  733. begin
  734. hs:=ppufile.getstring;
  735. checksum:=cardinal(ppufile.getlongint);
  736. intfchecksum:=cardinal(ppufile.getlongint);
  737. { set the state of this unit before registering, this is
  738. needed for a correct circular dependency check }
  739. hp:=registerunit(self,hs,'');
  740. pu:=addusedunit(hp,false,nil);
  741. pu.checksum:=checksum;
  742. pu.interface_checksum:=intfchecksum;
  743. end;
  744. in_interface:=false;
  745. end;
  746. procedure tppumodule.readlinkcontainer(var p:tlinkcontainer);
  747. var
  748. s : string;
  749. m : longint;
  750. begin
  751. while not ppufile.endofentry do
  752. begin
  753. s:=ppufile.getstring;
  754. m:=ppufile.getlongint;
  755. p.add(s,m);
  756. end;
  757. end;
  758. procedure tppumodule.readderefmap;
  759. var
  760. i : longint;
  761. begin
  762. { Load unit map used for resolving }
  763. derefmapsize:=ppufile.getlongint;
  764. derefmapcnt:=derefmapsize;
  765. getmem(derefmap,derefmapsize*sizeof(tderefmaprec));
  766. fillchar(derefmap^,derefmapsize*sizeof(tderefmaprec),0);
  767. for i:=0 to derefmapsize-1 do
  768. derefmap[i].modulename:=stringdup(ppufile.getstring);
  769. end;
  770. procedure tppumodule.readderefdata;
  771. var
  772. len,hlen : longint;
  773. buf : array[0..1023] of byte;
  774. begin
  775. len:=ppufile.entrysize;
  776. while (len>0) do
  777. begin
  778. if len>1024 then
  779. hlen:=1024
  780. else
  781. hlen:=len;
  782. ppufile.getdata(buf,hlen);
  783. derefdata.write(buf,hlen);
  784. dec(len,hlen);
  785. end;
  786. end;
  787. procedure tppumodule.readImportSymbols;
  788. var
  789. j,
  790. extsymcnt : longint;
  791. ImportLibrary : TImportLibrary;
  792. extsymname : string;
  793. extsymordnr : longint;
  794. extsymisvar : boolean;
  795. begin
  796. while not ppufile.endofentry do
  797. begin
  798. ImportLibrary:=TImportLibrary.Create(ImportLibraryList,ppufile.getstring);
  799. extsymcnt:=ppufile.getlongint;
  800. for j:=0 to extsymcnt-1 do
  801. begin
  802. extsymname:=ppufile.getstring;
  803. extsymordnr:=ppufile.getlongint;
  804. extsymisvar:=(ppufile.getbyte<>0);
  805. TImportSymbol.Create(ImportLibrary.ImportSymbolList,extsymname,extsymordnr,extsymisvar);
  806. end;
  807. end;
  808. end;
  809. procedure tppumodule.load_interface;
  810. var
  811. b : byte;
  812. newmodulename : string;
  813. begin
  814. { read interface part }
  815. repeat
  816. b:=ppufile.readentry;
  817. case b of
  818. ibmodulename :
  819. begin
  820. newmodulename:=ppufile.getstring;
  821. if (cs_check_unit_name in current_settings.globalswitches) and
  822. (upper(newmodulename)<>modulename^) then
  823. Message2(unit_f_unit_name_error,realmodulename^,newmodulename);
  824. stringdispose(modulename);
  825. stringdispose(realmodulename);
  826. modulename:=stringdup(upper(newmodulename));
  827. realmodulename:=stringdup(newmodulename);
  828. end;
  829. ibsourcefiles :
  830. readsourcefiles;
  831. {$IFDEF MACRO_DIFF_HINT}
  832. ibusedmacros :
  833. readusedmacros;
  834. {$ENDIF}
  835. ibloadunit :
  836. readloadunit;
  837. iblinkunitofiles :
  838. readlinkcontainer(LinkUnitOFiles);
  839. iblinkunitstaticlibs :
  840. readlinkcontainer(LinkUnitStaticLibs);
  841. iblinkunitsharedlibs :
  842. readlinkcontainer(LinkUnitSharedLibs);
  843. iblinkotherofiles :
  844. readlinkcontainer(LinkotherOFiles);
  845. iblinkotherstaticlibs :
  846. readlinkcontainer(LinkotherStaticLibs);
  847. iblinkothersharedlibs :
  848. readlinkcontainer(LinkotherSharedLibs);
  849. ibImportSymbols :
  850. readImportSymbols;
  851. ibderefmap :
  852. readderefmap;
  853. ibderefdata :
  854. readderefdata;
  855. ibendinterface :
  856. break;
  857. else
  858. Message1(unit_f_ppu_invalid_entry,tostr(b));
  859. end;
  860. { we can already stop when we know that we must recompile }
  861. if do_compile then
  862. exit;
  863. until false;
  864. end;
  865. procedure tppumodule.load_implementation;
  866. var
  867. b : byte;
  868. begin
  869. { read implementation part }
  870. repeat
  871. b:=ppufile.readentry;
  872. case b of
  873. ibloadunit :
  874. readloadunit;
  875. ibasmsymbols :
  876. {$warning TODO Remove ibasmsymbols}
  877. ;
  878. ibendimplementation :
  879. break;
  880. else
  881. Message1(unit_f_ppu_invalid_entry,tostr(b));
  882. end;
  883. until false;
  884. end;
  885. procedure tppumodule.load_symtable_refs;
  886. var
  887. b : byte;
  888. i : longint;
  889. begin
  890. if (flags and uf_has_browser)<>0 then
  891. begin
  892. tstoredsymtable(globalsymtable).load_references(ppufile,true);
  893. for i:=0 to unitmapsize-1 do
  894. tstoredsymtable(globalsymtable).load_references(ppufile,false);
  895. b:=ppufile.readentry;
  896. if b<>ibendbrowser then
  897. Message1(unit_f_ppu_invalid_entry,tostr(b));
  898. end;
  899. if ((flags and uf_local_browser)<>0) then
  900. tstaticsymtable(localsymtable).load_references(ppufile,true);
  901. end;
  902. procedure tppumodule.writeppu;
  903. var
  904. pu : tused_unit;
  905. begin
  906. Message1(unit_u_ppu_write,realmodulename^);
  907. { create unit flags }
  908. if cs_browser in current_settings.moduleswitches then
  909. flags:=flags or uf_has_browser;
  910. if cs_local_browser in current_settings.moduleswitches then
  911. flags:=flags or uf_local_browser;
  912. if do_release then
  913. flags:=flags or uf_release;
  914. if assigned(localsymtable) then
  915. flags:=flags or uf_local_symtable;
  916. {$ifdef cpufpemu}
  917. if (cs_fp_emulation in current_settings.moduleswitches) then
  918. flags:=flags or uf_fpu_emulation;
  919. {$endif cpufpemu}
  920. {$ifdef Test_Double_checksum_write}
  921. Assign(CRCFile,s+'.IMP');
  922. Rewrite(CRCFile);
  923. {$endif def Test_Double_checksum_write}
  924. { create new ppufile }
  925. ppufile:=tcompilerppufile.create(ppufilename^);
  926. if not ppufile.createfile then
  927. Message(unit_f_ppu_cannot_write);
  928. { first the unitname }
  929. ppufile.putstring(realmodulename^);
  930. ppufile.writeentry(ibmodulename);
  931. writesourcefiles;
  932. {$IFDEF MACRO_DIFF_HINT}
  933. writeusedmacros;
  934. {$ENDIF}
  935. { write interface uses }
  936. writeusedunit(true);
  937. { write the objectfiles and libraries that come for this unit,
  938. preserve the containers becuase they are still needed to load
  939. the link.res. All doesn't depend on the crc! It doesn't matter
  940. if a unit is in a .o or .a file }
  941. ppufile.do_crc:=false;
  942. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  943. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  944. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  945. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  946. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  947. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  948. writeImportSymbols;
  949. ppufile.do_crc:=true;
  950. { generate implementation deref data, the interface deref data is
  951. already generated when calculating the interface crc }
  952. if (cs_compilesystem in current_settings.moduleswitches) then
  953. begin
  954. tstoredsymtable(globalsymtable).buildderef;
  955. derefdataintflen:=derefdata.size;
  956. end;
  957. tstoredsymtable(globalsymtable).buildderefimpl;
  958. if (flags and uf_local_symtable)<>0 then
  959. begin
  960. tstoredsymtable(localsymtable).buildderef;
  961. tstoredsymtable(localsymtable).buildderefimpl;
  962. end;
  963. writederefmap;
  964. writederefdata;
  965. ppufile.writeentry(ibendinterface);
  966. { write the symtable entries }
  967. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  968. if assigned(globalmacrosymtable) and (globalmacrosymtable.symindex.count > 0) then
  969. begin
  970. ppufile.putbyte(byte(true));
  971. ppufile.writeentry(ibexportedmacros);
  972. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  973. end
  974. else
  975. begin
  976. ppufile.putbyte(byte(false));
  977. ppufile.writeentry(ibexportedmacros);
  978. end;
  979. { everything after this doesn't affect the crc }
  980. ppufile.do_crc:=false;
  981. { write implementation uses }
  982. writeusedunit(false);
  983. { end of implementation }
  984. ppufile.writeentry(ibendimplementation);
  985. { write static symtable
  986. needed for local debugging of unit functions }
  987. if (flags and uf_local_symtable)<>0 then
  988. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  989. { write all browser section }
  990. if (flags and uf_has_browser)<>0 then
  991. begin
  992. tstoredsymtable(globalsymtable).write_references(ppufile,true);
  993. pu:=tused_unit(used_units.first);
  994. while assigned(pu) do
  995. begin
  996. tstoredsymtable(pu.u.globalsymtable).write_references(ppufile,false);
  997. pu:=tused_unit(pu.next);
  998. end;
  999. ppufile.writeentry(ibendbrowser);
  1000. end;
  1001. if ((flags and uf_local_browser)<>0) then
  1002. begin
  1003. if not assigned(localsymtable) then
  1004. internalerror(200408271);
  1005. tstaticsymtable(localsymtable).write_references(ppufile,true);
  1006. end;
  1007. { the last entry ibend is written automaticly }
  1008. { flush to be sure }
  1009. ppufile.flush;
  1010. { create and write header }
  1011. ppufile.header.size:=ppufile.size;
  1012. ppufile.header.checksum:=ppufile.crc;
  1013. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1014. ppufile.header.compiler:=wordversion;
  1015. ppufile.header.cpu:=word(target_cpu);
  1016. ppufile.header.target:=word(target_info.system);
  1017. ppufile.header.flags:=flags;
  1018. ppufile.header.deflistsize:=current_module.deflist.count;
  1019. ppufile.header.symlistsize:=current_module.symlist.count;
  1020. ppufile.writeheader;
  1021. { save crc in current module also }
  1022. crc:=ppufile.crc;
  1023. interface_crc:=ppufile.interface_crc;
  1024. {$ifdef Test_Double_checksum_write}
  1025. close(CRCFile);
  1026. {$endif Test_Double_checksum_write}
  1027. ppufile.closefile;
  1028. ppufile.free;
  1029. ppufile:=nil;
  1030. end;
  1031. procedure tppumodule.getppucrc;
  1032. begin
  1033. {$ifdef Test_Double_checksum_write}
  1034. Assign(CRCFile,s+'.INT')
  1035. Rewrite(CRCFile);
  1036. {$endif def Test_Double_checksum_write}
  1037. { create new ppufile }
  1038. ppufile:=tcompilerppufile.create(ppufilename^);
  1039. ppufile.crc_only:=true;
  1040. if not ppufile.createfile then
  1041. Message(unit_f_ppu_cannot_write);
  1042. { first the unitname }
  1043. ppufile.putstring(realmodulename^);
  1044. ppufile.writeentry(ibmodulename);
  1045. { the interface units affect the crc }
  1046. writeusedunit(true);
  1047. { deref data of interface that affect the crc }
  1048. derefdata.reset;
  1049. tstoredsymtable(globalsymtable).buildderef;
  1050. derefdataintflen:=derefdata.size;
  1051. writederefmap;
  1052. writederefdata;
  1053. ppufile.writeentry(ibendinterface);
  1054. { write the symtable entries }
  1055. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  1056. if assigned(globalmacrosymtable) and (globalmacrosymtable.symindex.count > 0) then
  1057. begin
  1058. ppufile.putbyte(byte(true));
  1059. ppufile.writeentry(ibexportedmacros);
  1060. tstoredsymtable(globalmacrosymtable).ppuwrite(ppufile);
  1061. end
  1062. else
  1063. begin
  1064. ppufile.putbyte(byte(false));
  1065. ppufile.writeentry(ibexportedmacros);
  1066. end;
  1067. { save crc }
  1068. crc:=ppufile.crc;
  1069. interface_crc:=ppufile.interface_crc;
  1070. { end of implementation, to generate a correct ppufile
  1071. for ppudump when using INTFPPU define }
  1072. ppufile.writeentry(ibendimplementation);
  1073. {$ifdef Test_Double_checksum}
  1074. crc_array:=ppufile.crc_test;
  1075. ppufile.crc_test:=nil;
  1076. crc_size:=ppufile.crc_index2;
  1077. crc_array2:=ppufile.crc_test2;
  1078. ppufile.crc_test2:=nil;
  1079. crc_size2:=ppufile.crc_index2;
  1080. {$endif Test_Double_checksum}
  1081. {$ifdef Test_Double_checksum_write}
  1082. close(CRCFile);
  1083. {$endif Test_Double_checksum_write}
  1084. { create and write header, this will only be used
  1085. for debugging purposes }
  1086. ppufile.header.size:=ppufile.size;
  1087. ppufile.header.checksum:=ppufile.crc;
  1088. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1089. ppufile.header.compiler:=wordversion;
  1090. ppufile.header.cpu:=word(target_cpu);
  1091. ppufile.header.target:=word(target_info.system);
  1092. ppufile.header.flags:=flags;
  1093. ppufile.writeheader;
  1094. ppufile.closefile;
  1095. ppufile.free;
  1096. ppufile:=nil;
  1097. end;
  1098. procedure tppumodule.load_usedunits;
  1099. var
  1100. pu : tused_unit;
  1101. load_refs : boolean;
  1102. begin
  1103. if current_module<>self then
  1104. internalerror(200212284);
  1105. load_refs:=true;
  1106. { load the used units from interface }
  1107. in_interface:=true;
  1108. pu:=tused_unit(used_units.first);
  1109. while assigned(pu) do
  1110. begin
  1111. if pu.in_interface then
  1112. begin
  1113. tppumodule(pu.u).loadppu;
  1114. { if this unit is compiled we can stop }
  1115. if state=ms_compiled then
  1116. exit;
  1117. { add this unit to the dependencies }
  1118. pu.u.adddependency(self);
  1119. { need to recompile the current unit, check the interface
  1120. crc. And when not compiled with -Ur then check the complete
  1121. crc }
  1122. if (pu.u.interface_crc<>pu.interface_checksum) or
  1123. (
  1124. ((ppufile.header.flags and uf_release)=0) and
  1125. (pu.u.crc<>pu.checksum)
  1126. ) then
  1127. begin
  1128. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^,@queuecomment);
  1129. recompile_reason:=rr_crcchanged;
  1130. do_compile:=true;
  1131. exit;
  1132. end;
  1133. end;
  1134. pu:=tused_unit(pu.next);
  1135. end;
  1136. { ok, now load the interface of this unit }
  1137. if current_module<>self then
  1138. internalerror(200208187);
  1139. deflist.count:=ppufile.header.deflistsize;
  1140. symlist.count:=ppufile.header.symlistsize;
  1141. globalsymtable:=tglobalsymtable.create(modulename^,moduleid);
  1142. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1143. if ppufile.readentry<>ibexportedmacros then
  1144. Message(unit_f_ppu_read_error);
  1145. if boolean(ppufile.getbyte) then
  1146. begin
  1147. globalmacrosymtable:=tmacrosymtable.Create(true);
  1148. tstoredsymtable(globalmacrosymtable).ppuload(ppufile)
  1149. end;
  1150. interface_compiled:=true;
  1151. { read the implementation part, containing
  1152. the implementation uses and ObjData }
  1153. in_interface:=false;
  1154. load_implementation;
  1155. { now only read the implementation uses }
  1156. pu:=tused_unit(used_units.first);
  1157. while assigned(pu) do
  1158. begin
  1159. if (not pu.in_interface) then
  1160. begin
  1161. tppumodule(pu.u).loadppu;
  1162. { if this unit is compiled we can stop }
  1163. if state=ms_compiled then
  1164. exit;
  1165. { add this unit to the dependencies }
  1166. pu.u.adddependency(self);
  1167. { need to recompile the current unit ? }
  1168. if (pu.u.interface_crc<>pu.interface_checksum) then
  1169. begin
  1170. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}',@queuecomment);
  1171. recompile_reason:=rr_crcchanged;
  1172. do_compile:=true;
  1173. exit;
  1174. end;
  1175. end;
  1176. pu:=tused_unit(pu.next);
  1177. end;
  1178. { load implementation symtable }
  1179. if (flags and uf_local_symtable)<>0 then
  1180. begin
  1181. localsymtable:=tstaticsymtable.create(modulename^,moduleid);
  1182. tstaticsymtable(localsymtable).ppuload(ppufile);
  1183. end;
  1184. { we can now derefence all pointers to the implementation parts }
  1185. tstoredsymtable(globalsymtable).derefimpl;
  1186. if assigned(localsymtable) then
  1187. tstoredsymtable(localsymtable).derefimpl;
  1188. { load browser info if stored }
  1189. if ((flags and uf_has_browser)<>0) and load_refs then
  1190. begin
  1191. if current_module<>self then
  1192. internalerror(200208188);
  1193. load_symtable_refs;
  1194. end;
  1195. end;
  1196. function tppumodule.needrecompile:boolean;
  1197. var
  1198. pu : tused_unit;
  1199. begin
  1200. result:=false;
  1201. pu:=tused_unit(used_units.first);
  1202. while assigned(pu) do
  1203. begin
  1204. { need to recompile the current unit, check the interface
  1205. crc. And when not compiled with -Ur then check the complete
  1206. crc }
  1207. if (pu.u.interface_crc<>pu.interface_checksum) or
  1208. (
  1209. (pu.in_interface) and
  1210. (pu.u.crc<>pu.checksum)
  1211. ) then
  1212. begin
  1213. result:=true;
  1214. exit;
  1215. end;
  1216. pu:=tused_unit(pu.next);
  1217. end;
  1218. end;
  1219. procedure tppumodule.loadppu;
  1220. const
  1221. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1222. var
  1223. do_load,
  1224. second_time : boolean;
  1225. old_current_module : tmodule;
  1226. begin
  1227. old_current_module:=current_module;
  1228. Message3(unit_u_load_unit,old_current_module.modulename^,
  1229. ImplIntf[old_current_module.in_interface],
  1230. modulename^);
  1231. { Update loaded_from to detect cycles }
  1232. loaded_from:=old_current_module;
  1233. { check if the globalsymtable is already available, but
  1234. we must reload when the do_reload flag is set }
  1235. if (not do_reload) and
  1236. assigned(globalsymtable) then
  1237. exit;
  1238. { reset }
  1239. do_load:=true;
  1240. second_time:=false;
  1241. current_module:=self;
  1242. SetCompileModule(current_module);
  1243. Fillchar(current_filepos,0,sizeof(current_filepos));
  1244. { A force reload }
  1245. if do_reload then
  1246. begin
  1247. Message(unit_u_forced_reload);
  1248. do_reload:=false;
  1249. { When the unit is already loaded or being loaded
  1250. we can maybe skip a complete reload/recompile }
  1251. if assigned(globalsymtable) and
  1252. (not needrecompile) then
  1253. begin
  1254. { When we don't have any data stored yet there
  1255. is nothing to resolve }
  1256. if interface_compiled then
  1257. begin
  1258. Message1(unit_u_reresolving_unit,modulename^);
  1259. tstoredsymtable(globalsymtable).deref;
  1260. tstoredsymtable(globalsymtable).derefimpl;
  1261. if assigned(localsymtable) then
  1262. begin
  1263. tstoredsymtable(localsymtable).deref;
  1264. tstoredsymtable(localsymtable).derefimpl;
  1265. end;
  1266. end
  1267. else
  1268. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1269. do_load:=false;
  1270. end;
  1271. end;
  1272. if do_load then
  1273. begin
  1274. { we are loading a new module, save the state of the scanner
  1275. and reset scanner+module }
  1276. if assigned(current_scanner) then
  1277. current_scanner.tempcloseinputfile;
  1278. current_scanner:=nil;
  1279. { loading the unit for a second time? }
  1280. if state=ms_registered then
  1281. state:=ms_load
  1282. else
  1283. begin
  1284. { try to load the unit a second time first }
  1285. Message1(unit_u_second_load_unit,modulename^);
  1286. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1287. { Flag modules to reload }
  1288. flagdependent(old_current_module);
  1289. { Reset the module }
  1290. reset;
  1291. if state in [ms_compile,ms_second_compile] then
  1292. begin
  1293. Message1(unit_u_second_compile_unit,modulename^);
  1294. state:=ms_second_compile;
  1295. do_compile:=true;
  1296. end
  1297. else
  1298. state:=ms_second_load;
  1299. second_time:=true;
  1300. end;
  1301. { close old_current_ppu on system that are
  1302. short on file handles like DOS PM }
  1303. {$ifdef SHORT_ON_FILE_HANDLES}
  1304. if old_current_module.is_unit and
  1305. assigned(tppumodule(old_current_module).ppufile) then
  1306. tppumodule(old_current_module).ppufile.tempclose;
  1307. {$endif SHORT_ON_FILE_HANDLES}
  1308. { try to opening ppu, skip this when we already
  1309. know that we need to compile the unit }
  1310. if not do_compile then
  1311. begin
  1312. Message1(unit_u_loading_unit,modulename^);
  1313. search_unit(false,false);
  1314. if not do_compile then
  1315. begin
  1316. load_interface;
  1317. if not do_compile then
  1318. begin
  1319. load_usedunits;
  1320. if not do_compile then
  1321. Message1(unit_u_finished_loading_unit,modulename^);
  1322. end;
  1323. end;
  1324. { PPU is not needed anymore }
  1325. if assigned(ppufile) then
  1326. begin
  1327. ppufile.closefile;
  1328. ppufile.free;
  1329. ppufile:=nil;
  1330. end;
  1331. end;
  1332. { Do we need to recompile the unit }
  1333. if do_compile then
  1334. begin
  1335. { recompile the unit or give a fatal error if sources not available }
  1336. if not(sources_avail) then
  1337. begin
  1338. if (not search_unit(true,false)) and
  1339. (length(modulename^)>8) then
  1340. search_unit(true,true);
  1341. if not(sources_avail) then
  1342. begin
  1343. printcomments;
  1344. if recompile_reason=rr_noppu then
  1345. Message1(unit_f_cant_find_ppu,realmodulename^)
  1346. else
  1347. Message1(unit_f_cant_compile_unit,realmodulename^);
  1348. end;
  1349. end;
  1350. { we found the sources, we do not need the verbose messages anymore }
  1351. if comments <> nil then
  1352. begin
  1353. comments.free;
  1354. comments:=nil;
  1355. end;
  1356. { Flag modules to reload }
  1357. flagdependent(old_current_module);
  1358. { Reset the module }
  1359. reset;
  1360. { compile this module }
  1361. if not(state in [ms_compile,ms_second_compile]) then
  1362. state:=ms_compile;
  1363. compile(mainsource^);
  1364. end
  1365. else
  1366. state:=ms_compiled;
  1367. if current_module<>self then
  1368. internalerror(200212282);
  1369. if in_interface then
  1370. internalerror(200212283);
  1371. { for a second_time recompile reload all dependent units,
  1372. for a first time compile register the unit _once_ }
  1373. if second_time then
  1374. reload_flagged_units
  1375. else
  1376. usedunits.concat(tused_unit.create(self,true,false,nil));
  1377. { reopen the old module }
  1378. {$ifdef SHORT_ON_FILE_HANDLES}
  1379. if old_current_module.is_unit and
  1380. assigned(tppumodule(old_current_module).ppufile) then
  1381. tppumodule(old_current_module).ppufile.tempopen;
  1382. {$endif SHORT_ON_FILE_HANDLES}
  1383. { reload old scanner }
  1384. current_scanner:=tscannerfile(old_current_module.scanner);
  1385. if assigned(current_scanner) then
  1386. begin
  1387. current_scanner.tempopeninputfile;
  1388. current_scanner.gettokenpos
  1389. end
  1390. else
  1391. fillchar(current_filepos,sizeof(current_filepos),0);
  1392. end;
  1393. { we are back, restore current_module }
  1394. current_module:=old_current_module;
  1395. SetCompileModule(current_module);
  1396. end;
  1397. {*****************************************************************************
  1398. RegisterUnit
  1399. *****************************************************************************}
  1400. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  1401. var
  1402. ups : stringid;
  1403. hp : tppumodule;
  1404. hp2 : tmodule;
  1405. begin
  1406. { Info }
  1407. ups:=upper(s);
  1408. { search all loaded units }
  1409. hp:=tppumodule(loaded_units.first);
  1410. while assigned(hp) do
  1411. begin
  1412. if hp.modulename^=ups then
  1413. begin
  1414. { only check for units. The main program is also
  1415. as a unit in the loaded_units list. We simply need
  1416. to ignore this entry (PFV) }
  1417. if hp.is_unit then
  1418. begin
  1419. { both units in interface ? }
  1420. if callermodule.in_interface and
  1421. hp.in_interface then
  1422. begin
  1423. { check for a cycle }
  1424. hp2:=callermodule.loaded_from;
  1425. while assigned(hp2) and (hp2<>hp) do
  1426. begin
  1427. if hp2.in_interface then
  1428. hp2:=hp2.loaded_from
  1429. else
  1430. hp2:=nil;
  1431. end;
  1432. if assigned(hp2) then
  1433. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1434. end;
  1435. break;
  1436. end;
  1437. end;
  1438. { the next unit }
  1439. hp:=tppumodule(hp.next);
  1440. end;
  1441. { the unit is not in the loaded units,
  1442. we create an entry and register the unit }
  1443. if not assigned(hp) then
  1444. begin
  1445. Message1(unit_u_registering_new_unit,Upper(s));
  1446. hp:=tppumodule.create(callermodule,s,fn,true);
  1447. hp.loaded_from:=callermodule;
  1448. addloadedunit(hp);
  1449. end;
  1450. { return }
  1451. registerunit:=hp;
  1452. end;
  1453. end.