fppu.pas 73 KB

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