fppu.pas 59 KB

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