fppu.pas 50 KB

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