fppu.pas 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559
  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. { we can already stop when we know that we must recompile }
  815. if do_compile then
  816. exit;
  817. until false;
  818. end;
  819. procedure tppumodule.load_implementation;
  820. var
  821. b : byte;
  822. begin
  823. { read implementation part }
  824. repeat
  825. b:=ppufile.readentry;
  826. case b of
  827. ibloadunit :
  828. readloadunit;
  829. ibasmsymbols :
  830. readasmsymbols;
  831. ibendimplementation :
  832. break;
  833. else
  834. Message1(unit_f_ppu_invalid_entry,tostr(b));
  835. end;
  836. until false;
  837. end;
  838. procedure tppumodule.load_symtable_refs;
  839. var
  840. b : byte;
  841. i : longint;
  842. begin
  843. if (flags and uf_has_browser)<>0 then
  844. begin
  845. tstoredsymtable(globalsymtable).load_references(ppufile,true);
  846. for i:=0 to mapsize-1 do
  847. tstoredsymtable(globalsymtable).load_references(ppufile,false);
  848. b:=ppufile.readentry;
  849. if b<>ibendbrowser then
  850. Message1(unit_f_ppu_invalid_entry,tostr(b));
  851. end;
  852. if ((flags and uf_local_browser)<>0) then
  853. tstaticsymtable(localsymtable).load_references(ppufile,true);
  854. end;
  855. procedure tppumodule.writeppu;
  856. var
  857. pu : tused_unit;
  858. begin
  859. Message1(unit_u_ppu_write,realmodulename^);
  860. { create unit flags }
  861. {$ifdef GDB}
  862. if cs_gdb_dbx in aktglobalswitches then
  863. flags:=flags or uf_has_dbx;
  864. {$endif GDB}
  865. if cs_browser in aktmoduleswitches then
  866. flags:=flags or uf_has_browser;
  867. if cs_local_browser in aktmoduleswitches then
  868. flags:=flags or uf_local_browser;
  869. if do_release then
  870. flags:=flags or uf_release;
  871. if assigned(localsymtable) then
  872. flags:=flags or uf_local_symtable;
  873. {$ifdef cpufpemu}
  874. if (cs_fp_emulation in aktmoduleswitches) then
  875. flags:=flags or uf_fpu_emulation;
  876. {$endif cpufpemu}
  877. {$ifdef Test_Double_checksum_write}
  878. Assign(CRCFile,s+'.IMP');
  879. Rewrite(CRCFile);
  880. {$endif def Test_Double_checksum_write}
  881. { create new ppufile }
  882. ppufile:=tcompilerppufile.create(ppufilename^);
  883. if not ppufile.createfile then
  884. Message(unit_f_ppu_cannot_write);
  885. { first the unitname }
  886. ppufile.putstring(realmodulename^);
  887. ppufile.writeentry(ibmodulename);
  888. writesourcefiles;
  889. writeusedmacros;
  890. { write interface uses }
  891. writeusedunit(true);
  892. { write the objectfiles and libraries that come for this unit,
  893. preserve the containers becuase they are still needed to load
  894. the link.res. All doesn't depend on the crc! It doesn't matter
  895. if a unit is in a .o or .a file }
  896. ppufile.do_crc:=false;
  897. writelinkcontainer(linkunitofiles,iblinkunitofiles,true);
  898. writelinkcontainer(linkunitstaticlibs,iblinkunitstaticlibs,true);
  899. writelinkcontainer(linkunitsharedlibs,iblinkunitsharedlibs,true);
  900. writelinkcontainer(linkotherofiles,iblinkotherofiles,false);
  901. writelinkcontainer(linkotherstaticlibs,iblinkotherstaticlibs,true);
  902. writelinkcontainer(linkothersharedlibs,iblinkothersharedlibs,true);
  903. ppufile.do_crc:=true;
  904. { generate implementation deref data, the interface deref data is
  905. already generated when calculating the interface crc }
  906. if (cs_compilesystem in aktmoduleswitches) then
  907. begin
  908. tstoredsymtable(globalsymtable).buildderef;
  909. derefdataintflen:=derefdata.size;
  910. end;
  911. tstoredsymtable(globalsymtable).buildderefimpl;
  912. if (flags and uf_local_symtable)<>0 then
  913. begin
  914. tstoredsymtable(localsymtable).buildderef;
  915. tstoredsymtable(localsymtable).buildderefimpl;
  916. end;
  917. writederefdata;
  918. ppufile.writeentry(ibendinterface);
  919. { write the symtable entries }
  920. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  921. { everything after this doesn't affect the crc }
  922. ppufile.do_crc:=false;
  923. { write implementation uses }
  924. writeusedunit(false);
  925. { write asmsymbols }
  926. writeasmsymbols;
  927. { end of implementation }
  928. ppufile.writeentry(ibendimplementation);
  929. { write static symtable
  930. needed for local debugging of unit functions }
  931. if (flags and uf_local_symtable)<>0 then
  932. tstoredsymtable(localsymtable).ppuwrite(ppufile);
  933. { write all browser section }
  934. if (flags and uf_has_browser)<>0 then
  935. begin
  936. tstoredsymtable(globalsymtable).write_references(ppufile,true);
  937. pu:=tused_unit(used_units.first);
  938. while assigned(pu) do
  939. begin
  940. tstoredsymtable(pu.u.globalsymtable).write_references(ppufile,false);
  941. pu:=tused_unit(pu.next);
  942. end;
  943. ppufile.writeentry(ibendbrowser);
  944. end;
  945. if ((flags and uf_local_browser)<>0) then
  946. begin
  947. if not assigned(localsymtable) then
  948. internalerror(200408271);
  949. tstaticsymtable(localsymtable).write_references(ppufile,true);
  950. end;
  951. { the last entry ibend is written automaticly }
  952. { flush to be sure }
  953. ppufile.flush;
  954. { create and write header }
  955. ppufile.header.size:=ppufile.size;
  956. ppufile.header.checksum:=ppufile.crc;
  957. ppufile.header.interface_checksum:=ppufile.interface_crc;
  958. ppufile.header.compiler:=wordversion;
  959. ppufile.header.cpu:=word(target_cpu);
  960. ppufile.header.target:=word(target_info.system);
  961. ppufile.header.flags:=flags;
  962. ppufile.writeheader;
  963. { save crc in current module also }
  964. crc:=ppufile.crc;
  965. interface_crc:=ppufile.interface_crc;
  966. {$ifdef Test_Double_checksum_write}
  967. close(CRCFile);
  968. {$endif Test_Double_checksum_write}
  969. ppufile.closefile;
  970. ppufile.free;
  971. ppufile:=nil;
  972. end;
  973. procedure tppumodule.getppucrc;
  974. begin
  975. {$ifdef Test_Double_checksum_write}
  976. Assign(CRCFile,s+'.INT')
  977. Rewrite(CRCFile);
  978. {$endif def Test_Double_checksum_write}
  979. { create new ppufile }
  980. ppufile:=tcompilerppufile.create(ppufilename^);
  981. ppufile.crc_only:=true;
  982. if not ppufile.createfile then
  983. Message(unit_f_ppu_cannot_write);
  984. { first the unitname }
  985. ppufile.putstring(realmodulename^);
  986. ppufile.writeentry(ibmodulename);
  987. { the interface units affect the crc }
  988. writeusedunit(true);
  989. { deref data of interface that affect the crc }
  990. derefdata.reset;
  991. tstoredsymtable(globalsymtable).buildderef;
  992. derefdataintflen:=derefdata.size;
  993. writederefdata;
  994. ppufile.writeentry(ibendinterface);
  995. { write the symtable entries }
  996. tstoredsymtable(globalsymtable).ppuwrite(ppufile);
  997. { save crc }
  998. crc:=ppufile.crc;
  999. interface_crc:=ppufile.interface_crc;
  1000. { end of implementation, to generate a correct ppufile
  1001. for ppudump when using INTFPPU define }
  1002. ppufile.writeentry(ibendimplementation);
  1003. {$ifdef Test_Double_checksum}
  1004. crc_array:=ppufile.crc_test;
  1005. ppufile.crc_test:=nil;
  1006. crc_size:=ppufile.crc_index2;
  1007. crc_array2:=ppufile.crc_test2;
  1008. ppufile.crc_test2:=nil;
  1009. crc_size2:=ppufile.crc_index2;
  1010. {$endif Test_Double_checksum}
  1011. {$ifdef Test_Double_checksum_write}
  1012. close(CRCFile);
  1013. {$endif Test_Double_checksum_write}
  1014. { create and write header, this will only be used
  1015. for debugging purposes }
  1016. ppufile.header.size:=ppufile.size;
  1017. ppufile.header.checksum:=ppufile.crc;
  1018. ppufile.header.interface_checksum:=ppufile.interface_crc;
  1019. ppufile.header.compiler:=wordversion;
  1020. ppufile.header.cpu:=word(target_cpu);
  1021. ppufile.header.target:=word(target_info.system);
  1022. ppufile.header.flags:=flags;
  1023. ppufile.writeheader;
  1024. ppufile.closefile;
  1025. ppufile.free;
  1026. ppufile:=nil;
  1027. end;
  1028. procedure tppumodule.load_usedunits;
  1029. var
  1030. pu : tused_unit;
  1031. load_refs : boolean;
  1032. oldobjectlibrary : tasmlibrarydata;
  1033. begin
  1034. if current_module<>self then
  1035. internalerror(200212284);
  1036. load_refs:=true;
  1037. { load the used units from interface }
  1038. in_interface:=true;
  1039. pu:=tused_unit(used_units.first);
  1040. while assigned(pu) do
  1041. begin
  1042. if pu.in_interface then
  1043. begin
  1044. tppumodule(pu.u).loadppu;
  1045. { if this unit is compiled we can stop }
  1046. if state=ms_compiled then
  1047. exit;
  1048. { add this unit to the dependencies }
  1049. pu.u.adddependency(self);
  1050. { need to recompile the current unit, check the interface
  1051. crc. And when not compiled with -Ur then check the complete
  1052. crc }
  1053. if (pu.u.interface_crc<>pu.interface_checksum) or
  1054. (
  1055. ((ppufile.header.flags and uf_release)=0) and
  1056. (pu.u.crc<>pu.checksum)
  1057. ) then
  1058. begin
  1059. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^);
  1060. recompile_reason:=rr_crcchanged;
  1061. do_compile:=true;
  1062. exit;
  1063. end;
  1064. end;
  1065. pu:=tused_unit(pu.next);
  1066. end;
  1067. numberunits;
  1068. { ok, now load the interface of this unit }
  1069. if current_module<>self then
  1070. internalerror(200208187);
  1071. globalsymtable:=tglobalsymtable.create(modulename^);
  1072. tstoredsymtable(globalsymtable).ppuload(ppufile);
  1073. interface_compiled:=true;
  1074. { read the implementation part, containing
  1075. the implementation uses and objectdata }
  1076. in_interface:=false;
  1077. load_implementation;
  1078. { now only read the implementation uses }
  1079. pu:=tused_unit(used_units.first);
  1080. while assigned(pu) do
  1081. begin
  1082. if (not pu.in_interface) then
  1083. begin
  1084. tppumodule(pu.u).loadppu;
  1085. { if this unit is compiled we can stop }
  1086. if state=ms_compiled then
  1087. exit;
  1088. { add this unit to the dependencies }
  1089. pu.u.adddependency(self);
  1090. { need to recompile the current unit ? }
  1091. if (pu.u.interface_crc<>pu.interface_checksum) then
  1092. begin
  1093. Message2(unit_u_recompile_crc_change,realmodulename^,pu.u.realmodulename^+' {impl}');
  1094. recompile_reason:=rr_crcchanged;
  1095. do_compile:=true;
  1096. exit;
  1097. end;
  1098. end;
  1099. pu:=tused_unit(pu.next);
  1100. end;
  1101. numberunits;
  1102. { load implementation symtable }
  1103. if (flags and uf_local_symtable)<>0 then
  1104. begin
  1105. localsymtable:=tstaticsymtable.create(modulename^);
  1106. tstaticsymtable(localsymtable).ppuload(ppufile);
  1107. end;
  1108. { we can now derefence all pointers to the implementation parts }
  1109. oldobjectlibrary:=objectlibrary;
  1110. objectlibrary:=librarydata;
  1111. tstoredsymtable(globalsymtable).derefimpl;
  1112. if assigned(localsymtable) then
  1113. tstoredsymtable(localsymtable).derefimpl;
  1114. objectlibrary:=oldobjectlibrary;
  1115. { load browser info if stored }
  1116. if ((flags and uf_has_browser)<>0) and load_refs then
  1117. begin
  1118. if current_module<>self then
  1119. internalerror(200208188);
  1120. load_symtable_refs;
  1121. end;
  1122. end;
  1123. function tppumodule.needrecompile:boolean;
  1124. var
  1125. pu : tused_unit;
  1126. begin
  1127. result:=false;
  1128. pu:=tused_unit(used_units.first);
  1129. while assigned(pu) do
  1130. begin
  1131. { need to recompile the current unit, check the interface
  1132. crc. And when not compiled with -Ur then check the complete
  1133. crc }
  1134. if (pu.u.interface_crc<>pu.interface_checksum) or
  1135. (
  1136. (pu.in_interface) and
  1137. (pu.u.crc<>pu.checksum)
  1138. ) then
  1139. begin
  1140. result:=true;
  1141. exit;
  1142. end;
  1143. pu:=tused_unit(pu.next);
  1144. end;
  1145. end;
  1146. procedure tppumodule.loadppu;
  1147. const
  1148. ImplIntf : array[boolean] of string[15]=('implementation','interface');
  1149. var
  1150. do_load,
  1151. second_time : boolean;
  1152. old_current_module : tmodule;
  1153. begin
  1154. old_current_module:=current_module;
  1155. Message3(unit_u_load_unit,old_current_module.modulename^,
  1156. ImplIntf[old_current_module.in_interface],
  1157. modulename^);
  1158. { Update loaded_from to detect cycles }
  1159. loaded_from:=old_current_module;
  1160. { check if the globalsymtable is already available, but
  1161. we must reload when the do_reload flag is set }
  1162. if (not do_reload) and
  1163. assigned(globalsymtable) then
  1164. exit;
  1165. { reset }
  1166. do_load:=true;
  1167. second_time:=false;
  1168. current_module:=self;
  1169. SetCompileModule(current_module);
  1170. Fillchar(aktfilepos,0,sizeof(aktfilepos));
  1171. { A force reload }
  1172. if do_reload then
  1173. begin
  1174. Message(unit_u_forced_reload);
  1175. do_reload:=false;
  1176. { When the unit is already loaded or being loaded
  1177. we can maybe skip a complete reload/recompile }
  1178. if assigned(globalsymtable) and
  1179. (not needrecompile) then
  1180. begin
  1181. { When we don't have any data stored yet there
  1182. is nothing to resolve }
  1183. if interface_compiled then
  1184. begin
  1185. Message1(unit_u_reresolving_unit,modulename^);
  1186. tstoredsymtable(globalsymtable).deref;
  1187. tstoredsymtable(globalsymtable).derefimpl;
  1188. if assigned(localsymtable) then
  1189. begin
  1190. tstoredsymtable(localsymtable).deref;
  1191. tstoredsymtable(localsymtable).derefimpl;
  1192. end;
  1193. end
  1194. else
  1195. Message1(unit_u_skipping_reresolving_unit,modulename^);
  1196. do_load:=false;
  1197. end;
  1198. end;
  1199. if do_load then
  1200. begin
  1201. { we are loading a new module, save the state of the scanner
  1202. and reset scanner+module }
  1203. if assigned(current_scanner) then
  1204. current_scanner.tempcloseinputfile;
  1205. current_scanner:=nil;
  1206. { loading the unit for a second time? }
  1207. if state=ms_registered then
  1208. state:=ms_load
  1209. else
  1210. begin
  1211. { try to load the unit a second time first }
  1212. Message1(unit_u_second_load_unit,modulename^);
  1213. Message2(unit_u_previous_state,modulename^,ModuleStateStr[state]);
  1214. { Flag modules to reload }
  1215. flagdependent(old_current_module);
  1216. { Reset the module }
  1217. reset;
  1218. if state in [ms_compile,ms_second_compile] then
  1219. begin
  1220. Message1(unit_u_second_compile_unit,modulename^);
  1221. state:=ms_second_compile;
  1222. do_compile:=true;
  1223. end
  1224. else
  1225. state:=ms_second_load;
  1226. second_time:=true;
  1227. end;
  1228. { close old_current_ppu on system that are
  1229. short on file handles like DOS PM }
  1230. {$ifdef SHORT_ON_FILE_HANDLES}
  1231. if old_current_module.is_unit and
  1232. assigned(tppumodule(old_current_module).ppufile) then
  1233. tppumodule(old_current_module).ppufile.tempclose;
  1234. {$endif SHORT_ON_FILE_HANDLES}
  1235. { try to opening ppu, skip this when we already
  1236. know that we need to compile the unit }
  1237. if not do_compile then
  1238. begin
  1239. Message1(unit_u_loading_unit,modulename^);
  1240. search_unit(false,false);
  1241. if not do_compile then
  1242. begin
  1243. load_interface;
  1244. if not do_compile then
  1245. begin
  1246. load_usedunits;
  1247. if not do_compile then
  1248. Message1(unit_u_finished_loading_unit,modulename^);
  1249. end;
  1250. end;
  1251. { PPU is not needed anymore }
  1252. if assigned(ppufile) then
  1253. begin
  1254. ppufile.closefile;
  1255. ppufile.free;
  1256. ppufile:=nil;
  1257. end;
  1258. end;
  1259. { Do we need to recompile the unit }
  1260. if do_compile then
  1261. begin
  1262. { recompile the unit or give a fatal error if sources not available }
  1263. if not(sources_avail) then
  1264. begin
  1265. if (not search_unit(true,false)) and
  1266. (length(modulename^)>8) then
  1267. search_unit(true,true);
  1268. if not(sources_avail) then
  1269. begin
  1270. if recompile_reason=rr_noppu then
  1271. Message1(unit_f_cant_find_ppu,realmodulename^)
  1272. else
  1273. Message1(unit_f_cant_compile_unit,realmodulename^);
  1274. end;
  1275. end;
  1276. { Flag modules to reload }
  1277. flagdependent(old_current_module);
  1278. { Reset the module }
  1279. reset;
  1280. { compile this module }
  1281. if not(state in [ms_compile,ms_second_compile]) then
  1282. state:=ms_compile;
  1283. compile(mainsource^);
  1284. end
  1285. else
  1286. state:=ms_compiled;
  1287. if current_module<>self then
  1288. internalerror(200212282);
  1289. if in_interface then
  1290. internalerror(200212283);
  1291. { for a second_time recompile reload all dependent units,
  1292. for a first time compile register the unit _once_ }
  1293. if second_time then
  1294. reload_flagged_units
  1295. else
  1296. usedunits.concat(tused_unit.create(self,true,false,nil));
  1297. { reopen the old module }
  1298. {$ifdef SHORT_ON_FILE_HANDLES}
  1299. if old_current_module.is_unit and
  1300. assigned(tppumodule(old_current_module).ppufile) then
  1301. tppumodule(old_current_module).ppufile.tempopen;
  1302. {$endif SHORT_ON_FILE_HANDLES}
  1303. { reload old scanner }
  1304. current_scanner:=tscannerfile(old_current_module.scanner);
  1305. if assigned(current_scanner) then
  1306. begin
  1307. current_scanner.tempopeninputfile;
  1308. current_scanner.gettokenpos
  1309. end
  1310. else
  1311. fillchar(aktfilepos,sizeof(aktfilepos),0);
  1312. end;
  1313. { we are back, restore current_module }
  1314. current_module:=old_current_module;
  1315. SetCompileModule(current_module);
  1316. end;
  1317. {*****************************************************************************
  1318. RegisterUnit
  1319. *****************************************************************************}
  1320. function registerunit(callermodule:tmodule;const s : stringid;const fn:string) : tppumodule;
  1321. var
  1322. ups : stringid;
  1323. hp : tppumodule;
  1324. hp2 : tmodule;
  1325. begin
  1326. { Info }
  1327. ups:=upper(s);
  1328. { search all loaded units }
  1329. hp:=tppumodule(loaded_units.first);
  1330. while assigned(hp) do
  1331. begin
  1332. if hp.modulename^=ups then
  1333. begin
  1334. { only check for units. The main program is also
  1335. as a unit in the loaded_units list. We simply need
  1336. to ignore this entry (PFV) }
  1337. if hp.is_unit then
  1338. begin
  1339. { both units in interface ? }
  1340. if callermodule.in_interface and
  1341. hp.in_interface then
  1342. begin
  1343. { check for a cycle }
  1344. hp2:=callermodule.loaded_from;
  1345. while assigned(hp2) and (hp2<>hp) do
  1346. begin
  1347. if hp2.in_interface then
  1348. hp2:=hp2.loaded_from
  1349. else
  1350. hp2:=nil;
  1351. end;
  1352. if assigned(hp2) then
  1353. Message2(unit_f_circular_unit_reference,callermodule.realmodulename^,hp.realmodulename^);
  1354. end;
  1355. break;
  1356. end;
  1357. end;
  1358. { the next unit }
  1359. hp:=tppumodule(hp.next);
  1360. end;
  1361. { the unit is not in the loaded units,
  1362. we create an entry and register the unit }
  1363. if not assigned(hp) then
  1364. begin
  1365. Message1(unit_u_registering_new_unit,Upper(s));
  1366. hp:=tppumodule.create(callermodule,s,fn,true);
  1367. hp.loaded_from:=callermodule;
  1368. loaded_units.insert(hp);
  1369. end;
  1370. { return }
  1371. registerunit:=hp;
  1372. end;
  1373. end.
  1374. {
  1375. $Log$
  1376. Revision 1.62 2004-09-13 20:27:27 peter
  1377. * remove check for shortunitname, it broke units with 8 and 9 chars
  1378. with the first 8 chars being equal
  1379. Revision 1.61 2004/08/28 20:01:09 peter
  1380. * print realmodulename in fatal errors
  1381. Revision 1.60 2004/08/27 21:59:26 peter
  1382. browser disabled
  1383. uf_local_symtable ppu flag when a localsymtable is stored
  1384. Revision 1.59 2004/07/09 22:17:31 peter
  1385. * revert has_localst patch
  1386. * replace aktstaticsymtable/aktglobalsymtable with current_module
  1387. Revision 1.58 2004/07/06 20:23:25 peter
  1388. * remove unused and not loaded units before linking
  1389. Revision 1.57 2004/07/06 19:52:04 peter
  1390. * fix storing of localst in ppu
  1391. Revision 1.56 2004/07/05 21:26:28 olle
  1392. + allow fileextension .p, in mode macpas
  1393. Revision 1.55 2004/06/20 08:55:29 florian
  1394. * logs truncated
  1395. Revision 1.54 2004/03/17 12:12:59 olle
  1396. * bugfix in [read|write]asmsymbols for data and address labels
  1397. Revision 1.53 2004/03/02 00:36:33 olle
  1398. * big transformation of Tai_[const_]Symbol.Create[data]name*
  1399. Revision 1.52 2004/02/03 16:14:13 peter
  1400. * fix remembering path of main source file
  1401. Revision 1.51 2004/01/26 16:12:27 daniel
  1402. * reginfo now also only allocated during register allocation
  1403. * third round of gdb cleanups: kick out most of concatstabto
  1404. Revision 1.50 2004/01/22 17:23:56 peter
  1405. * also check in the same dir as the unit we are loading from, this
  1406. makes UNITPATH working better
  1407. }