fppu.pas 85 KB

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