fppu.pas 81 KB

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