fppu.pas 52 KB

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