fppu.pas 81 KB

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