fppu.pas 49 KB

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