fppu.pas 62 KB

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