fppu.pas 49 KB

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