fppu.pas 84 KB

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