fppu.pas 51 KB

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