pmodules.pas 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  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 pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. procedure proc_unit;
  21. procedure proc_package;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. SysUtils,
  26. globtype,version,systems,tokens,
  27. cutils,cfileutl,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,
  29. symconst,symbase,symtype,symdef,symsym,symtable,
  30. wpoinfo,
  31. aasmtai,aasmdata,aasmcpu,aasmbase,
  32. cgbase,cgobj,
  33. nbas,ncgutil,
  34. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  35. cresstr,procinfo,
  36. pexports,
  37. objcgutl,
  38. wpobase,
  39. scanner,pbase,pexpr,psystem,psub,pdecsub,ptype
  40. ,cpuinfo
  41. {$ifdef i386}
  42. { fix me! }
  43. ,cpubase
  44. {$endif i386}
  45. ;
  46. procedure create_objectfile;
  47. var
  48. DLLScanner : TDLLScanner;
  49. s : string;
  50. KeepShared : TCmdStrList;
  51. begin
  52. { try to create import entries from system dlls }
  53. if (tf_has_dllscanner in target_info.flags) and
  54. (not current_module.linkOtherSharedLibs.Empty) then
  55. begin
  56. { Init DLLScanner }
  57. if assigned(CDLLScanner[target_info.system]) then
  58. DLLScanner:=CDLLScanner[target_info.system].Create
  59. else
  60. internalerror(200104121);
  61. KeepShared:=TCmdStrList.Create;
  62. { Walk all shared libs }
  63. While not current_module.linkOtherSharedLibs.Empty do
  64. begin
  65. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  66. if not DLLScanner.scan(s) then
  67. KeepShared.Concat(s);
  68. end;
  69. DLLscanner.Free;
  70. { Recreate import section }
  71. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  72. begin
  73. if assigned(current_asmdata.asmlists[al_imports]) then
  74. current_asmdata.asmlists[al_imports].clear
  75. else
  76. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  77. importlib.generatelib;
  78. end;
  79. { Readd the not processed files }
  80. while not KeepShared.Empty do
  81. begin
  82. s:=KeepShared.GetFirst;
  83. current_module.linkOtherSharedLibs.add(s,link_always);
  84. end;
  85. KeepShared.Free;
  86. end;
  87. { Start and end module debuginfo, at least required for stabs
  88. to insert n_sourcefile lines }
  89. if (cs_debuginfo in current_settings.moduleswitches) or
  90. (cs_use_lineinfo in current_settings.globalswitches) then
  91. current_debuginfo.insertmoduleinfo;
  92. { create the .s file and assemble it }
  93. GenerateAsm(false);
  94. { Also create a smartlinked version ? }
  95. if create_smartlink_library then
  96. begin
  97. GenerateAsm(true);
  98. if (af_needar in target_asm.flags) then
  99. Linker.MakeStaticLibrary;
  100. end;
  101. { resource files }
  102. CompileResourceFiles;
  103. end;
  104. procedure insertobjectfile;
  105. { Insert the used object file for this unit in the used list for this unit }
  106. begin
  107. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  108. current_module.flags:=current_module.flags or uf_static_linked;
  109. if create_smartlink_library then
  110. begin
  111. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  112. current_module.flags:=current_module.flags or uf_smart_linked;
  113. end;
  114. end;
  115. procedure create_dwarf_frame;
  116. begin
  117. { Dwarf conflicts with smartlinking in separate .a files }
  118. if create_smartlink_library then
  119. exit;
  120. { Call frame information }
  121. { MWE: we write our own info, so dwarf asm support is not really needed }
  122. { if (af_supports_dwarf in target_asm.flags) and }
  123. { CFI is currently broken for Darwin }
  124. if not(target_info.system in systems_darwin) and
  125. (
  126. (tf_needs_dwarf_cfi in target_info.flags) or
  127. (paratargetdbg in [dbg_dwarf2, dbg_dwarf3])
  128. ) then
  129. begin
  130. current_asmdata.asmlists[al_dwarf_frame].Free;
  131. current_asmdata.asmlists[al_dwarf_frame] := TAsmList.create;
  132. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf_frame]);
  133. end;
  134. end;
  135. procedure InsertThreadvarTablesTable;
  136. var
  137. hp : tused_unit;
  138. ltvTables : TAsmList;
  139. count : longint;
  140. begin
  141. if (tf_section_threadvars in target_info.flags) then
  142. exit;
  143. ltvTables:=TAsmList.Create;
  144. count:=0;
  145. hp:=tused_unit(usedunits.first);
  146. while assigned(hp) do
  147. begin
  148. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  149. begin
  150. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  151. inc(count);
  152. end;
  153. hp:=tused_unit(hp.next);
  154. end;
  155. { Add program threadvars, if any }
  156. If (current_module.flags and uf_threadvars)=uf_threadvars then
  157. begin
  158. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  159. inc(count);
  160. end;
  161. { Insert TableCount at start }
  162. ltvTables.insert(Tai_const.Create_32bit(count));
  163. { insert in data segment }
  164. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  165. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(pint));
  166. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  167. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  168. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  169. ltvTables.free;
  170. end;
  171. procedure AddToThreadvarList(p:TObject;arg:pointer);
  172. var
  173. ltvTable : TAsmList;
  174. begin
  175. ltvTable:=TAsmList(arg);
  176. if (tsym(p).typ=staticvarsym) and
  177. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  178. begin
  179. { address of threadvar }
  180. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  181. { size of threadvar }
  182. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  183. end;
  184. end;
  185. procedure InsertThreadvars;
  186. var
  187. s : string;
  188. ltvTable : TAsmList;
  189. begin
  190. if (tf_section_threadvars in target_info.flags) then
  191. exit;
  192. ltvTable:=TAsmList.create;
  193. if assigned(current_module.globalsymtable) then
  194. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  195. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  196. if ltvTable.first<>nil then
  197. begin
  198. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  199. { end of the list marker }
  200. ltvTable.concat(tai_const.create_sym(nil));
  201. { add to datasegment }
  202. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  203. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  204. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  205. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  206. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  207. current_module.flags:=current_module.flags or uf_threadvars;
  208. end;
  209. ltvTable.Free;
  210. end;
  211. procedure InsertWideInits;
  212. var
  213. s: string;
  214. item: TTCInitItem;
  215. begin
  216. item:=TTCInitItem(current_asmdata.WideInits.First);
  217. if item=nil then
  218. exit;
  219. s:=make_mangledname('WIDEINITS',current_module.localsymtable,'');
  220. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  221. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(pint));
  222. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  223. repeat
  224. { address to initialize }
  225. current_asmdata.asmlists[al_globals].concat(Tai_const.createname(item.sym.mangledname, item.offset));
  226. { value with which to initialize }
  227. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(item.datalabel));
  228. item:=TTCInitItem(item.Next);
  229. until item=nil;
  230. { end-of-list marker }
  231. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_sym(nil));
  232. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  233. current_module.flags:=current_module.flags or uf_wideinits;
  234. end;
  235. procedure InsertWideInitsTablesTable;
  236. var
  237. hp: tused_unit;
  238. lwiTables: TAsmList;
  239. count: longint;
  240. begin
  241. lwiTables:=TAsmList.Create;
  242. count:=0;
  243. hp:=tused_unit(usedunits.first);
  244. while assigned(hp) do
  245. begin
  246. if (hp.u.flags and uf_wideinits)=uf_wideinits then
  247. begin
  248. lwiTables.concat(Tai_const.Createname(make_mangledname('WIDEINITS',hp.u.globalsymtable,''),0));
  249. inc(count);
  250. end;
  251. hp:=tused_unit(hp.next);
  252. end;
  253. { Add program widestring consts, if any }
  254. if (current_module.flags and uf_wideinits)=uf_wideinits then
  255. begin
  256. lwiTables.concat(Tai_const.Createname(make_mangledname('WIDEINITS',current_module.localsymtable,''),0));
  257. inc(count);
  258. end;
  259. { Insert TableCount at start }
  260. lwiTables.insert(Tai_const.Create_32bit(count));
  261. { insert in data segment }
  262. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  263. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_WIDEINITTABLES',sizeof(pint));
  264. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_WIDEINITTABLES',AT_DATA,0));
  265. current_asmdata.asmlists[al_globals].concatlist(lwiTables);
  266. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_WIDEINITTABLES'));
  267. lwiTables.free;
  268. end;
  269. Function CheckResourcesUsed : boolean;
  270. var
  271. hp : tused_unit;
  272. found : Boolean;
  273. begin
  274. CheckResourcesUsed:=tf_has_winlike_resources in target_info.flags;
  275. if not CheckResourcesUsed then exit;
  276. hp:=tused_unit(usedunits.first);
  277. found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  278. If not found then
  279. While Assigned(hp) and not found do
  280. begin
  281. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  282. hp:=tused_unit(hp.next);
  283. end;
  284. CheckResourcesUsed:=found;
  285. end;
  286. Procedure InsertResourceInfo(ResourcesUsed : boolean);
  287. var
  288. ResourceInfo : TAsmList;
  289. begin
  290. if (target_res.id in [res_elf,res_macho]) then
  291. begin
  292. ResourceInfo:=TAsmList.Create;
  293. maybe_new_object_file(ResourceInfo);
  294. new_section(ResourceInfo,sec_data,'FPC_RESLOCATION',sizeof(aint));
  295. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  296. if ResourcesUsed then
  297. { Valid pointer to resource information }
  298. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0))
  299. else
  300. { Nil pointer to resource information }
  301. {$IFDEF CPU32}
  302. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  303. {$ELSE}
  304. ResourceInfo.Concat(Tai_const.Create_64bit(0));
  305. {$ENDIF}
  306. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  307. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  308. ResourceInfo.free;
  309. end;
  310. end;
  311. Procedure InsertResourceTablesTable;
  312. var
  313. hp : tmodule;
  314. ResourceStringTables : tasmlist;
  315. count : longint;
  316. begin
  317. ResourceStringTables:=tasmlist.Create;
  318. count:=0;
  319. hp:=tmodule(loaded_units.first);
  320. while assigned(hp) do
  321. begin
  322. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  323. begin
  324. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  325. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  326. inc(count);
  327. end;
  328. hp:=tmodule(hp.next);
  329. end;
  330. { Insert TableCount at start }
  331. ResourceStringTables.insert(Tai_const.Create_pint(count));
  332. { Add to data segment }
  333. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  334. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(pint));
  335. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  336. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  337. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  338. ResourceStringTables.free;
  339. end;
  340. procedure AddToClasInits(p:TObject;arg:pointer);
  341. var
  342. ClassList: TFPList absolute arg;
  343. begin
  344. if (tdef(p).typ=objectdef) and
  345. ([oo_has_class_constructor,oo_has_class_destructor] * tobjectdef(p).objectoptions <> []) then
  346. ClassList.Add(p);
  347. end;
  348. procedure InsertInitFinalTable;
  349. var
  350. hp : tused_unit;
  351. unitinits : TAsmList;
  352. count : longint;
  353. procedure write_class_inits(u: tmodule);
  354. var
  355. i: integer;
  356. classlist: TFPList;
  357. pd: tprocdef;
  358. begin
  359. classlist := TFPList.Create;
  360. if assigned(u.globalsymtable) then
  361. u.globalsymtable.DefList.ForEachCall(@AddToClasInits,classlist);
  362. u.localsymtable.DefList.ForEachCall(@AddToClasInits,classlist);
  363. { write classes }
  364. for i := 0 to classlist.Count - 1 do
  365. begin
  366. pd := tobjectdef(classlist[i]).find_procdef_bytype(potype_class_constructor);
  367. if assigned(pd) then
  368. unitinits.concat(Tai_const.Createname(pd.mangledname,0))
  369. else
  370. unitinits.concat(Tai_const.Create_pint(0));
  371. pd := tobjectdef(classlist[i]).find_procdef_bytype(potype_class_destructor);
  372. if assigned(pd) then
  373. unitinits.concat(Tai_const.Createname(pd.mangledname,0))
  374. else
  375. unitinits.concat(Tai_const.Create_pint(0));
  376. inc(count);
  377. end;
  378. classlist.free;
  379. end;
  380. begin
  381. unitinits:=TAsmList.Create;
  382. count:=0;
  383. hp:=tused_unit(usedunits.first);
  384. while assigned(hp) do
  385. begin
  386. { insert class constructors/destructors of the unit }
  387. if (hp.u.flags and uf_classinits) <> 0 then
  388. write_class_inits(hp.u);
  389. { call the unit init code and make it external }
  390. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  391. begin
  392. if (hp.u.flags and uf_init)<>0 then
  393. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  394. else
  395. unitinits.concat(Tai_const.Create_sym(nil));
  396. if (hp.u.flags and uf_finalize)<>0 then
  397. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  398. else
  399. unitinits.concat(Tai_const.Create_sym(nil));
  400. inc(count);
  401. end;
  402. hp:=tused_unit(hp.next);
  403. end;
  404. { insert class constructors/destructor of the program }
  405. if (current_module.flags and uf_classinits) <> 0 then
  406. write_class_inits(current_module);
  407. { Insert initialization/finalization of the program }
  408. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  409. begin
  410. if (current_module.flags and uf_init)<>0 then
  411. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  412. else
  413. unitinits.concat(Tai_const.Create_sym(nil));
  414. if (current_module.flags and uf_finalize)<>0 then
  415. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  416. else
  417. unitinits.concat(Tai_const.Create_sym(nil));
  418. inc(count);
  419. end;
  420. { Insert TableCount,InitCount at start }
  421. unitinits.insert(Tai_const.Create_32bit(0));
  422. unitinits.insert(Tai_const.Create_32bit(count));
  423. { Add to data segment }
  424. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  425. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(pint));
  426. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  427. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  428. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  429. unitinits.free;
  430. end;
  431. procedure InsertMemorySizes;
  432. {$IFDEF POWERPC}
  433. var
  434. stkcookie: string;
  435. {$ENDIF POWERPC}
  436. begin
  437. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  438. { Insert Ident of the compiler in the .fpc.version section }
  439. new_section(current_asmdata.asmlists[al_globals],sec_fpc,'version',const_align(32));
  440. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  441. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  442. if not(tf_no_generic_stackcheck in target_info.flags) then
  443. begin
  444. { stacksize can be specified and is now simulated }
  445. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(pint));
  446. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(pint)));
  447. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(stacksize));
  448. end;
  449. {$IFDEF POWERPC}
  450. { AmigaOS4 "stack cookie" support }
  451. if ( target_info.system = system_powerpc_amiga ) then
  452. begin
  453. { this symbol is needed to ignite powerpc amigaos' }
  454. { stack allocation magic for us with the given stack size. }
  455. { note: won't work for m68k amigaos or morphos. (KB) }
  456. str(stacksize,stkcookie);
  457. stkcookie:='$STACK: '+stkcookie+#0;
  458. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  459. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  460. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  461. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  462. end;
  463. {$ENDIF POWERPC}
  464. { Initial heapsize }
  465. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  466. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(pint));
  467. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(pint)));
  468. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_pint(heapsize));
  469. { Initial heapsize }
  470. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  471. new_section(current_asmdata.asmlists[al_globals],sec_data,'__fpc_valgrind',sizeof(boolean));
  472. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__fpc_valgrind',AT_DATA,sizeof(boolean)));
  473. current_asmdata.asmlists[al_globals].concat(Tai_const.create_8bit(byte(cs_gdb_valgrind in current_settings.globalswitches)));
  474. end;
  475. procedure AddUnit(const s:string);
  476. var
  477. hp : tppumodule;
  478. unitsym : tunitsym;
  479. begin
  480. { load unit }
  481. hp:=registerunit(current_module,s,'');
  482. hp.loadppu;
  483. hp.adddependency(current_module);
  484. { add to symtable stack }
  485. symtablestack.push(hp.globalsymtable);
  486. if (m_mac in current_settings.modeswitches) and
  487. assigned(hp.globalmacrosymtable) then
  488. macrosymtablestack.push(hp.globalmacrosymtable);
  489. { insert unitsym }
  490. unitsym:=tunitsym.create(s,hp);
  491. inc(unitsym.refs);
  492. current_module.localsymtable.insert(unitsym);
  493. { add to used units }
  494. current_module.addusedunit(hp,false,unitsym);
  495. end;
  496. procedure maybeloadvariantsunit;
  497. var
  498. hp : tmodule;
  499. begin
  500. { Do we need the variants unit? Skip this
  501. for VarUtils unit for bootstrapping }
  502. if (current_module.flags and uf_uses_variants=0) or
  503. (current_module.modulename^='VARUTILS') then
  504. exit;
  505. { Variants unit already loaded? }
  506. hp:=tmodule(loaded_units.first);
  507. while assigned(hp) do
  508. begin
  509. if hp.modulename^='VARIANTS' then
  510. exit;
  511. hp:=tmodule(hp.next);
  512. end;
  513. { Variants unit is not loaded yet, load it now }
  514. Message(parser_w_implicit_uses_of_variants_unit);
  515. AddUnit('variants');
  516. end;
  517. function MaybeRemoveResUnit : boolean;
  518. var
  519. resources_used : boolean;
  520. hp : tmodule;
  521. uu : tused_unit;
  522. unitname : shortstring;
  523. begin
  524. { We simply remove the unit from:
  525. - usedunit list, so that things like init/finalization table won't
  526. contain references to this unit
  527. - loaded_units list, so that the unit object file doesn't get linked
  528. with the executable. }
  529. { Note: on windows we always need resources! }
  530. resources_used:=(target_info.system in systems_all_windows)
  531. or CheckResourcesUsed;
  532. if (not resources_used) and (tf_has_winlike_resources in target_info.flags) then
  533. begin
  534. { resources aren't used, so we don't need this unit }
  535. if target_res.id=res_ext then
  536. unitname:='FPEXTRES'
  537. else
  538. unitname:='FPINTRES';
  539. Message1(unit_u_unload_resunit,unitname);
  540. { find the module }
  541. hp:=tmodule(loaded_units.first);
  542. while assigned(hp) do
  543. begin
  544. if hp.is_unit and (hp.modulename^=unitname) then break;
  545. hp:=tmodule(hp.next);
  546. end;
  547. if not assigned(hp) then
  548. internalerror(200801071);
  549. { find its tused_unit in the global list }
  550. uu:=tused_unit(usedunits.first);
  551. while assigned(uu) do
  552. begin
  553. if uu.u=hp then break;
  554. uu:=tused_unit(uu.next);
  555. end;
  556. if not assigned(uu) then
  557. internalerror(200801072);
  558. { remove the tused_unit }
  559. usedunits.Remove(uu);
  560. uu.Free;
  561. { remove the module }
  562. loaded_units.Remove(hp);
  563. unloaded_units.Concat(hp);
  564. end;
  565. MaybeRemoveResUnit:=resources_used;
  566. end;
  567. procedure loaddefaultunits;
  568. begin
  569. { we are going to rebuild the symtablestack, clear it first }
  570. symtablestack.clear;
  571. macrosymtablestack.clear;
  572. { macro symtable }
  573. macrosymtablestack.push(initialmacrosymtable);
  574. { are we compiling the system unit? }
  575. if (cs_compilesystem in current_settings.moduleswitches) then
  576. begin
  577. systemunit:=tglobalsymtable(current_module.localsymtable);
  578. { create system defines }
  579. create_intern_symbols;
  580. create_intern_types;
  581. { Set the owner of errorsym and errortype to symtable to
  582. prevent crashes when accessing .owner }
  583. generrorsym.owner:=systemunit;
  584. generrordef.owner:=systemunit;
  585. exit;
  586. end;
  587. { insert the system unit, it is allways the first. Load also the
  588. internal types from the system unit }
  589. AddUnit('system');
  590. systemunit:=tglobalsymtable(symtablestack.top);
  591. load_intern_types;
  592. { Set the owner of errorsym and errortype to symtable to
  593. prevent crashes when accessing .owner }
  594. generrorsym.owner:=systemunit;
  595. generrordef.owner:=systemunit;
  596. { Units only required for main module }
  597. if not(current_module.is_unit) then
  598. begin
  599. { Heaptrc unit, load heaptrace before any other units especially objpas }
  600. if (cs_use_heaptrc in current_settings.globalswitches) then
  601. AddUnit('heaptrc');
  602. { Lineinfo unit }
  603. if (cs_use_lineinfo in current_settings.globalswitches) then begin
  604. if (paratargetdbg = dbg_stabs) then
  605. AddUnit('lineinfo')
  606. else
  607. AddUnit('lnfodwrf');
  608. end;
  609. { Valgrind requires c memory manager }
  610. if (cs_gdb_valgrind in current_settings.globalswitches) then
  611. AddUnit('cmem');
  612. {$ifdef cpufpemu}
  613. { Floating point emulation unit?
  614. softfpu must be in the system unit anyways (FK)
  615. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  616. AddUnit('softfpu');
  617. }
  618. {$endif cpufpemu}
  619. { Which kind of resource support?
  620. Note: if resources aren't used this unit will be removed later,
  621. otherwise we need it here since it must be loaded quite early }
  622. if (tf_has_winlike_resources in target_info.flags) then
  623. if target_res.id=res_ext then
  624. AddUnit('fpextres')
  625. else
  626. AddUnit('fpintres');
  627. end;
  628. { Objpas unit? }
  629. if m_objpas in current_settings.modeswitches then
  630. AddUnit('objpas');
  631. { Macpas unit? }
  632. if m_mac in current_settings.modeswitches then
  633. AddUnit('macpas');
  634. { Objective-C support unit? }
  635. if (m_objectivec1 in current_settings.modeswitches) then
  636. begin
  637. { interface to Objective-C run time }
  638. AddUnit('objc');
  639. loadobjctypes;
  640. { NSObject }
  641. if not(current_module.is_unit) or
  642. (current_module.modulename^<>'OBJCBASE') then
  643. AddUnit('objcbase');
  644. end;
  645. { Profile unit? Needed for go32v2 only }
  646. if (cs_profile in current_settings.moduleswitches) and
  647. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  648. AddUnit('profile');
  649. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  650. begin
  651. AddUnit('fpcylix');
  652. AddUnit('dynlibs');
  653. end;
  654. { CPU targets with microcontroller support can add a controller specific unit }
  655. {$if defined(ARM)}
  656. if (target_info.system in systems_embedded) and (current_settings.controllertype<>ct_none) then
  657. AddUnit(controllerunitstr[current_settings.controllertype]);
  658. {$endif ARM}
  659. end;
  660. procedure loadautounits;
  661. var
  662. hs,s : string;
  663. begin
  664. hs:=autoloadunits;
  665. repeat
  666. s:=GetToken(hs,',');
  667. if s='' then
  668. break;
  669. AddUnit(s);
  670. until false;
  671. end;
  672. procedure loadunits;
  673. var
  674. s,sorg : TIDString;
  675. fn : string;
  676. pu : tused_unit;
  677. hp2 : tmodule;
  678. unitsym : tunitsym;
  679. begin
  680. consume(_USES);
  681. repeat
  682. s:=pattern;
  683. sorg:=orgpattern;
  684. consume(_ID);
  685. { support "<unit> in '<file>'" construct, but not for tp7 }
  686. fn:='';
  687. if not(m_tp7 in current_settings.modeswitches) and
  688. try_to_consume(_OP_IN) then
  689. fn:=FixFileName(get_stringconst);
  690. { Give a warning if lineinfo is loaded }
  691. if s='LINEINFO' then begin
  692. Message(parser_w_no_lineinfo_use_switch);
  693. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  694. s := 'LNFODWRF';
  695. sorg := s;
  696. end;
  697. { Give a warning if objpas is loaded }
  698. if s='OBJPAS' then
  699. Message(parser_w_no_objpas_use_mode);
  700. { Using the unit itself is not possible }
  701. if (s<>current_module.modulename^) then
  702. begin
  703. { check if the unit is already used }
  704. hp2:=nil;
  705. pu:=tused_unit(current_module.used_units.first);
  706. while assigned(pu) do
  707. begin
  708. if (pu.u.modulename^=s) then
  709. begin
  710. hp2:=pu.u;
  711. break;
  712. end;
  713. pu:=tused_unit(pu.next);
  714. end;
  715. if not assigned(hp2) then
  716. hp2:=registerunit(current_module,sorg,fn)
  717. else
  718. Message1(sym_e_duplicate_id,s);
  719. { Create unitsym, we need to use the name as specified, we
  720. can not use the modulename because that can be different
  721. when -Un is used }
  722. unitsym:=tunitsym.create(sorg,nil);
  723. current_module.localsymtable.insert(unitsym);
  724. { the current module uses the unit hp2 }
  725. current_module.addusedunit(hp2,true,unitsym);
  726. end
  727. else
  728. Message1(sym_e_duplicate_id,s);
  729. if token=_COMMA then
  730. begin
  731. pattern:='';
  732. consume(_COMMA);
  733. end
  734. else
  735. break;
  736. until false;
  737. { Load the units }
  738. pu:=tused_unit(current_module.used_units.first);
  739. while assigned(pu) do
  740. begin
  741. { Only load the units that are in the current
  742. (interface/implementation) uses clause }
  743. if pu.in_uses and
  744. (pu.in_interface=current_module.in_interface) then
  745. begin
  746. tppumodule(pu.u).loadppu;
  747. { is our module compiled? then we can stop }
  748. if current_module.state=ms_compiled then
  749. exit;
  750. { add this unit to the dependencies }
  751. pu.u.adddependency(current_module);
  752. { save crc values }
  753. pu.checksum:=pu.u.crc;
  754. pu.interface_checksum:=pu.u.interface_crc;
  755. pu.indirect_checksum:=pu.u.indirect_crc;
  756. { connect unitsym to the module }
  757. pu.unitsym.module:=pu.u;
  758. { add to symtable stack }
  759. symtablestack.push(pu.u.globalsymtable);
  760. if (m_mac in current_settings.modeswitches) and
  761. assigned(pu.u.globalmacrosymtable) then
  762. macrosymtablestack.push(pu.u.globalmacrosymtable);
  763. { check hints }
  764. pu.u.check_hints;
  765. end;
  766. pu:=tused_unit(pu.next);
  767. end;
  768. consume(_SEMICOLON);
  769. end;
  770. procedure reset_all_defs;
  771. begin
  772. if assigned(current_module.wpoinfo) then
  773. current_module.wpoinfo.resetdefs;
  774. end;
  775. procedure free_localsymtables(st:TSymtable);
  776. var
  777. i : longint;
  778. def : tstoreddef;
  779. pd : tprocdef;
  780. begin
  781. for i:=0 to st.DefList.Count-1 do
  782. begin
  783. def:=tstoreddef(st.DefList[i]);
  784. if def.typ=procdef then
  785. begin
  786. pd:=tprocdef(def);
  787. if assigned(pd.localst) and
  788. (pd.localst.symtabletype<>staticsymtable) and
  789. not(po_inline in pd.procoptions) then
  790. begin
  791. free_localsymtables(pd.localst);
  792. pd.localst.free;
  793. pd.localst:=nil;
  794. end;
  795. end;
  796. end;
  797. end;
  798. procedure parse_implementation_uses;
  799. begin
  800. if token=_USES then
  801. loadunits;
  802. end;
  803. procedure setupglobalswitches;
  804. begin
  805. if (cs_create_pic in current_settings.moduleswitches) then
  806. begin
  807. def_system_macro('FPC_PIC');
  808. def_system_macro('PIC');
  809. end;
  810. end;
  811. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  812. var
  813. ps : tprocsym;
  814. pd : tprocdef;
  815. begin
  816. { there should be no current_procinfo available }
  817. if assigned(current_procinfo) then
  818. internalerror(200304275);
  819. {Generate a procsym for main}
  820. ps:=tprocsym.create('$'+name);
  821. { main are allways used }
  822. inc(ps.refs);
  823. st.insert(ps);
  824. pd:=tprocdef.create(main_program_level);
  825. include(pd.procoptions,po_global);
  826. pd.procsym:=ps;
  827. ps.ProcdefList.Add(pd);
  828. { set procdef options }
  829. pd.proctypeoption:=potype;
  830. pd.proccalloption:=pocall_default;
  831. include(pd.procoptions,po_hascallingconvention);
  832. pd.forwarddef:=false;
  833. pd.setmangledname(target_info.cprefix+name);
  834. pd.aliasnames.insert(pd.mangledname);
  835. handle_calling_convention(pd);
  836. { We don't need is a local symtable. Change it into the static
  837. symtable }
  838. pd.localst.free;
  839. pd.localst:=st;
  840. { set procinfo and current_procinfo.procdef }
  841. result:=tcgprocinfo(cprocinfo.create(nil));
  842. result.procdef:=pd;
  843. { main proc does always a call e.g. to init system unit }
  844. include(result.flags,pi_do_call);
  845. end;
  846. procedure release_main_proc(pi:tcgprocinfo);
  847. begin
  848. { remove localst as it was replaced by staticsymtable }
  849. pi.procdef.localst:=nil;
  850. { remove procinfo }
  851. current_module.procinfo:=nil;
  852. pi.free;
  853. pi:=nil;
  854. end;
  855. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  856. begin
  857. { update module flags }
  858. current_module.flags:=current_module.flags or flag;
  859. { create procdef }
  860. case flag of
  861. uf_init :
  862. begin
  863. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  864. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  865. end;
  866. uf_finalize :
  867. begin
  868. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  869. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  870. if (not current_module.is_unit) then
  871. result.procdef.aliasnames.insert('PASCALFINALIZE');
  872. end;
  873. else
  874. internalerror(200304253);
  875. end;
  876. result.code:=cnothingnode.create;
  877. end;
  878. procedure copy_macro(p:TObject; arg:pointer);
  879. begin
  880. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  881. end;
  882. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  883. var
  884. last_is_deprecated:boolean;
  885. begin
  886. try_consume_hintdirective:=false;
  887. repeat
  888. last_is_deprecated:=false;
  889. case idtoken of
  890. _LIBRARY :
  891. begin
  892. include(moduleopt,mo_hint_library);
  893. try_consume_hintdirective:=true;
  894. end;
  895. _DEPRECATED :
  896. begin
  897. include(moduleopt,mo_hint_deprecated);
  898. try_consume_hintdirective:=true;
  899. last_is_deprecated:=true;
  900. end;
  901. _EXPERIMENTAL :
  902. begin
  903. include(moduleopt,mo_hint_experimental);
  904. try_consume_hintdirective:=true;
  905. end;
  906. _PLATFORM :
  907. begin
  908. include(moduleopt,mo_hint_platform);
  909. try_consume_hintdirective:=true;
  910. end;
  911. _UNIMPLEMENTED :
  912. begin
  913. include(moduleopt,mo_hint_unimplemented);
  914. try_consume_hintdirective:=true;
  915. end;
  916. else
  917. break;
  918. end;
  919. consume(Token);
  920. { handle deprecated message }
  921. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  922. begin
  923. if deprecatedmsg<>nil then
  924. internalerror(201001221);
  925. if token=_CSTRING then
  926. deprecatedmsg:=stringdup(cstringpattern)
  927. else
  928. deprecatedmsg:=stringdup(pattern);
  929. consume(token);
  930. include(moduleopt,mo_has_deprecated_msg);
  931. end;
  932. until false;
  933. end;
  934. procedure proc_unit;
  935. function is_assembler_generated:boolean;
  936. var
  937. hal : tasmlisttype;
  938. begin
  939. result:=false;
  940. if Errorcount=0 then
  941. begin
  942. for hal:=low(TasmlistType) to high(TasmlistType) do
  943. if not current_asmdata.asmlists[hal].empty then
  944. begin
  945. result:=true;
  946. exit;
  947. end;
  948. end;
  949. end;
  950. var
  951. main_file: tinputfile;
  952. {$ifdef EXTDEBUG}
  953. store_crc,
  954. {$endif EXTDEBUG}
  955. store_interface_crc,
  956. store_indirect_crc: cardinal;
  957. s1,s2 : ^string; {Saves stack space}
  958. force_init_final : boolean;
  959. init_procinfo,
  960. finalize_procinfo : tcgprocinfo;
  961. unitname8 : string[8];
  962. ag: boolean;
  963. {$ifdef i386}
  964. gotvarsym : tstaticvarsym;
  965. {$endif i386}
  966. {$ifdef debug_devirt}
  967. i: longint;
  968. {$endif debug_devirt}
  969. begin
  970. init_procinfo:=nil;
  971. finalize_procinfo:=nil;
  972. if m_mac in current_settings.modeswitches then
  973. current_module.mode_switch_allowed:= false;
  974. consume(_UNIT);
  975. if compile_level=1 then
  976. Status.IsExe:=false;
  977. if token=_ID then
  978. begin
  979. { create filenames and unit name }
  980. main_file := current_scanner.inputfile;
  981. while assigned(main_file.next) do
  982. main_file := main_file.next;
  983. new(s1);
  984. s1^:=current_module.modulename^;
  985. current_module.SetFileName(main_file.path^+main_file.name^,true);
  986. current_module.SetModuleName(orgpattern);
  987. { check for system unit }
  988. new(s2);
  989. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  990. unitname8:=copy(current_module.modulename^,1,8);
  991. if (cs_check_unit_name in current_settings.globalswitches) and
  992. (
  993. not(
  994. (current_module.modulename^=s2^) or
  995. (
  996. (length(current_module.modulename^)>8) and
  997. (unitname8=s2^)
  998. )
  999. )
  1000. or
  1001. (
  1002. (length(s1^)>8) and
  1003. (s1^<>current_module.modulename^)
  1004. )
  1005. ) then
  1006. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  1007. if (current_module.modulename^='SYSTEM') then
  1008. include(current_settings.moduleswitches,cs_compilesystem);
  1009. dispose(s2);
  1010. dispose(s1);
  1011. end;
  1012. if (target_info.system in systems_unit_program_exports) then
  1013. exportlib.preparelib(current_module.realmodulename^);
  1014. consume(_ID);
  1015. { parse hint directives }
  1016. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  1017. consume(_SEMICOLON);
  1018. consume(_INTERFACE);
  1019. { global switches are read, so further changes aren't allowed }
  1020. current_module.in_global:=false;
  1021. { handle the global switches }
  1022. setupglobalswitches;
  1023. message1(unit_u_loading_interface_units,current_module.modulename^);
  1024. { update status }
  1025. status.currentmodule:=current_module.realmodulename^;
  1026. { maybe turn off m_objpas if we are compiling objpas }
  1027. if (current_module.modulename^='OBJPAS') then
  1028. exclude(current_settings.modeswitches,m_objpas);
  1029. { maybe turn off m_mac if we are compiling macpas }
  1030. if (current_module.modulename^='MACPAS') then
  1031. exclude(current_settings.modeswitches,m_mac);
  1032. parse_only:=true;
  1033. { generate now the global symboltable,
  1034. define first as local to overcome dependency conflicts }
  1035. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  1036. { insert unitsym of this unit to prevent other units having
  1037. the same name }
  1038. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1039. { load default units, like the system unit }
  1040. loaddefaultunits;
  1041. { insert qualifier for the system unit (allows system.writeln) }
  1042. if not(cs_compilesystem in current_settings.moduleswitches) and
  1043. (token=_USES) then
  1044. begin
  1045. loadunits;
  1046. { has it been compiled at a higher level ?}
  1047. if current_module.state=ms_compiled then
  1048. exit;
  1049. end;
  1050. { move the global symtable from the temporary local to global }
  1051. current_module.globalsymtable:=current_module.localsymtable;
  1052. current_module.localsymtable:=nil;
  1053. { number all units, so we know if a unit is used by this unit or
  1054. needs to be added implicitly }
  1055. current_module.updatemaps;
  1056. { create whole program optimisation information (may already be
  1057. updated in the interface, e.g., in case of classrefdef typed
  1058. constants }
  1059. current_module.wpoinfo:=tunitwpoinfo.create;
  1060. { ... parse the declarations }
  1061. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1062. symtablestack.push(current_module.globalsymtable);
  1063. read_interface_declarations;
  1064. symtablestack.pop(current_module.globalsymtable);
  1065. { Export macros defined in the interface for macpas. The macros
  1066. are put in the globalmacrosymtable that will only be used by other
  1067. units. The current unit continues to use the localmacrosymtable }
  1068. if (m_mac in current_settings.modeswitches) then
  1069. begin
  1070. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  1071. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  1072. end;
  1073. { leave when we got an error }
  1074. if (Errorcount>0) and not status.skip_error then
  1075. begin
  1076. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1077. status.skip_error:=true;
  1078. exit;
  1079. end;
  1080. { Our interface is compiled, generate CRC and switch to implementation }
  1081. if not(cs_compilesystem in current_settings.moduleswitches) and
  1082. (Errorcount=0) then
  1083. tppumodule(current_module).getppucrc;
  1084. current_module.in_interface:=false;
  1085. current_module.interface_compiled:=true;
  1086. { First reload all units depending on our interface, we need to do this
  1087. in the implementation part to prevent erroneous circular references }
  1088. tppumodule(current_module).setdefgeneration;
  1089. tppumodule(current_module).reload_flagged_units;
  1090. { Parse the implementation section }
  1091. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1092. current_module.interface_only:=true
  1093. else
  1094. current_module.interface_only:=false;
  1095. parse_only:=false;
  1096. { create static symbol table }
  1097. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1098. {$ifdef i386}
  1099. if cs_create_pic in current_settings.moduleswitches then
  1100. begin
  1101. { insert symbol for got access in assembler code}
  1102. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  1103. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  1104. current_module.localsymtable.insert(gotvarsym);
  1105. { avoid unnecessary warnings }
  1106. gotvarsym.varstate:=vs_read;
  1107. gotvarsym.refs:=1;
  1108. end;
  1109. {$endif i386}
  1110. if not current_module.interface_only then
  1111. begin
  1112. consume(_IMPLEMENTATION);
  1113. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  1114. { Read the implementation units }
  1115. parse_implementation_uses;
  1116. end;
  1117. if current_module.state=ms_compiled then
  1118. exit;
  1119. { All units are read, now give them a number }
  1120. current_module.updatemaps;
  1121. symtablestack.push(current_module.globalsymtable);
  1122. symtablestack.push(current_module.localsymtable);
  1123. if not current_module.interface_only then
  1124. begin
  1125. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1126. if current_module.in_interface then
  1127. internalerror(200212285);
  1128. { Compile the unit }
  1129. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  1130. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1131. init_procinfo.parse_body;
  1132. { save file pos for debuginfo }
  1133. current_module.mainfilepos:=init_procinfo.entrypos;
  1134. end;
  1135. { Generate specializations of objectdefs methods }
  1136. generate_specialization_procs;
  1137. { if the unit contains ansi/widestrings, initialization and
  1138. finalization code must be forced }
  1139. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1140. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1141. { should we force unit initialization? }
  1142. { this is a hack, but how can it be done better ? }
  1143. if force_init_final and ((current_module.flags and uf_init)=0) then
  1144. begin
  1145. { first release the not used init procinfo }
  1146. if assigned(init_procinfo) then
  1147. release_main_proc(init_procinfo);
  1148. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1149. end;
  1150. { finalize? }
  1151. if not current_module.interface_only and (token=_FINALIZATION) then
  1152. begin
  1153. { the uf_finalize flag is only set after we checked that it
  1154. wasn't empty }
  1155. { Compile the finalize }
  1156. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1157. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1158. finalize_procinfo.parse_body;
  1159. end
  1160. else if force_init_final then
  1161. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1162. { Now both init and finalize bodies are read and it is known
  1163. which variables are used in both init and finalize we can now
  1164. generate the code. This is required to prevent putting a variable in
  1165. a register that is also used in the finalize body (PFV) }
  1166. if assigned(init_procinfo) then
  1167. begin
  1168. init_procinfo.generate_code;
  1169. init_procinfo.resetprocdef;
  1170. release_main_proc(init_procinfo);
  1171. end;
  1172. if assigned(finalize_procinfo) then
  1173. begin
  1174. finalize_procinfo.generate_code;
  1175. finalize_procinfo.resetprocdef;
  1176. release_main_proc(finalize_procinfo);
  1177. end;
  1178. symtablestack.pop(current_module.localsymtable);
  1179. symtablestack.pop(current_module.globalsymtable);
  1180. { the last char should always be a point }
  1181. consume(_POINT);
  1182. { reset wpo flags for all defs }
  1183. reset_all_defs;
  1184. if (Errorcount=0) then
  1185. begin
  1186. { tests, if all (interface) forwards are resolved }
  1187. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1188. { check if all private fields are used }
  1189. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1190. { test static symtable }
  1191. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1192. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1193. tstoredsymtable(current_module.localsymtable).check_forwards;
  1194. tstoredsymtable(current_module.localsymtable).checklabels;
  1195. { used units }
  1196. current_module.allunitsused;
  1197. end;
  1198. { leave when we got an error }
  1199. if (Errorcount>0) and not status.skip_error then
  1200. begin
  1201. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1202. status.skip_error:=true;
  1203. exit;
  1204. end;
  1205. { if an Objective-C module, generate rtti and module info }
  1206. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1207. { do we need to add the variants unit? }
  1208. maybeloadvariantsunit;
  1209. { generate wrappers for interfaces }
  1210. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  1211. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1212. { generate pic helpers to load eip if necessary }
  1213. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1214. { generate rtti/init tables }
  1215. write_persistent_type_info(current_module.globalsymtable);
  1216. write_persistent_type_info(current_module.localsymtable);
  1217. { Tables }
  1218. InsertThreadvars;
  1219. { Resource strings }
  1220. GenerateResourceStrings;
  1221. { Widestring typed constants }
  1222. InsertWideInits;
  1223. { generate debuginfo }
  1224. if (cs_debuginfo in current_settings.moduleswitches) then
  1225. current_debuginfo.inserttypeinfo;
  1226. { generate imports }
  1227. if current_module.ImportLibraryList.Count>0 then
  1228. importlib.generatelib;
  1229. { insert own objectfile, or say that it's in a library
  1230. (no check for an .o when loading) }
  1231. ag:=is_assembler_generated;
  1232. if ag then
  1233. insertobjectfile
  1234. else
  1235. begin
  1236. current_module.flags:=current_module.flags or uf_no_link;
  1237. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1238. end;
  1239. if ag then
  1240. begin
  1241. { create callframe info }
  1242. create_dwarf_frame;
  1243. { assemble }
  1244. create_objectfile;
  1245. end;
  1246. { Write out the ppufile after the object file has been created }
  1247. store_interface_crc:=current_module.interface_crc;
  1248. store_indirect_crc:=current_module.indirect_crc;
  1249. {$ifdef EXTDEBUG}
  1250. store_crc:=current_module.crc;
  1251. {$endif EXTDEBUG}
  1252. if (Errorcount=0) then
  1253. tppumodule(current_module).writeppu;
  1254. if not(cs_compilesystem in current_settings.moduleswitches) then
  1255. begin
  1256. if store_interface_crc<>current_module.interface_crc then
  1257. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1258. if store_indirect_crc<>current_module.indirect_crc then
  1259. Message1(unit_u_indirect_crc_changed,current_module.ppufilename^);
  1260. end;
  1261. {$ifdef EXTDEBUG}
  1262. if not(cs_compilesystem in current_settings.moduleswitches) then
  1263. if (store_crc<>current_module.crc) and simplify_ppu then
  1264. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1265. {$endif EXTDEBUG}
  1266. { release local symtables that are not needed anymore }
  1267. free_localsymtables(current_module.globalsymtable);
  1268. free_localsymtables(current_module.localsymtable);
  1269. { leave when we got an error }
  1270. if (Errorcount>0) and not status.skip_error then
  1271. begin
  1272. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1273. status.skip_error:=true;
  1274. exit;
  1275. end;
  1276. {$ifdef debug_devirt}
  1277. { print out all instantiated class/object types }
  1278. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1279. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1280. begin
  1281. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1282. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1283. objectdef:
  1284. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1285. odt_object:
  1286. writeln(' (object)');
  1287. odt_class:
  1288. writeln(' (class)');
  1289. else
  1290. internalerror(2008101103);
  1291. end;
  1292. else
  1293. internalerror(2008101104);
  1294. end;
  1295. end;
  1296. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1297. begin
  1298. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1299. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1300. objectdef:
  1301. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1302. odt_class:
  1303. writeln(' (classrefdef)');
  1304. else
  1305. internalerror(2008101105);
  1306. end
  1307. else
  1308. internalerror(2008101102);
  1309. end;
  1310. end;
  1311. {$endif debug_devirt}
  1312. Message1(unit_u_finished_compiling,current_module.modulename^);
  1313. end;
  1314. procedure procexport(const s : string);
  1315. var
  1316. hp : texported_item;
  1317. begin
  1318. hp:=texported_item.create;
  1319. hp.name:=stringdup(s);
  1320. hp.options:=hp.options or eo_name;
  1321. exportlib.exportprocedure(hp);
  1322. end;
  1323. procedure varexport(const s : string);
  1324. var
  1325. hp : texported_item;
  1326. begin
  1327. hp:=texported_item.create;
  1328. hp.name:=stringdup(s);
  1329. hp.options:=hp.options or eo_name;
  1330. exportlib.exportvar(hp);
  1331. end;
  1332. procedure insert_export(sym : TObject;arg:pointer);
  1333. var
  1334. i : longint;
  1335. item : TCmdStrListItem;
  1336. begin
  1337. case TSym(sym).typ of
  1338. { ignore: }
  1339. unitsym,
  1340. syssym,
  1341. constsym,
  1342. enumsym,
  1343. typesym:
  1344. ;
  1345. procsym:
  1346. begin
  1347. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1348. begin
  1349. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1350. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1351. ((tsymtable(arg).symtabletype=globalsymtable) or
  1352. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1353. ) then
  1354. begin
  1355. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1356. { walk through all aliases }
  1357. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1358. while assigned(item) do
  1359. begin
  1360. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1361. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1362. procexport(item.str);
  1363. item:=TCmdStrListItem(item.next);
  1364. end;
  1365. end;
  1366. end;
  1367. end;
  1368. staticvarsym:
  1369. begin
  1370. varexport(tsym(sym).mangledname);
  1371. end;
  1372. else
  1373. begin
  1374. writeln('unknown: ',ord(TSym(sym).typ));
  1375. end;
  1376. end;
  1377. end;
  1378. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1379. Var
  1380. MakeStatic : Boolean;
  1381. Var
  1382. buffer : array[0..$1fff] of byte;
  1383. inppu,
  1384. outppu : tppufile;
  1385. b,
  1386. untilb : byte;
  1387. l,m : longint;
  1388. f : file;
  1389. ext,
  1390. s : string;
  1391. ppuversion : dword;
  1392. begin
  1393. Result:=false;
  1394. MakeStatic:=False;
  1395. inppu:=tppufile.create(PPUFn);
  1396. if not inppu.openfile then
  1397. begin
  1398. inppu.free;
  1399. Comment(V_Error,'Could not open : '+PPUFn);
  1400. Exit;
  1401. end;
  1402. { Check the ppufile }
  1403. if not inppu.CheckPPUId then
  1404. begin
  1405. inppu.free;
  1406. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1407. Exit;
  1408. end;
  1409. ppuversion:=inppu.GetPPUVersion;
  1410. if ppuversion<CurrentPPUVersion then
  1411. begin
  1412. inppu.free;
  1413. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1414. Exit;
  1415. end;
  1416. { No .o file generated for this ppu, just skip }
  1417. if (inppu.header.flags and uf_no_link)<>0 then
  1418. begin
  1419. inppu.free;
  1420. Result:=true;
  1421. Exit;
  1422. end;
  1423. { Already a lib? }
  1424. if (inppu.header.flags and uf_in_library)<>0 then
  1425. begin
  1426. inppu.free;
  1427. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1428. Exit;
  1429. end;
  1430. { We need a static linked unit }
  1431. if (inppu.header.flags and uf_static_linked)=0 then
  1432. begin
  1433. inppu.free;
  1434. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1435. Exit;
  1436. end;
  1437. { Check if shared is allowed }
  1438. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1439. begin
  1440. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1441. MakeStatic:=true;
  1442. end;
  1443. { Create the new ppu }
  1444. if PPUFn=PPLFn then
  1445. outppu:=tppufile.create('ppumove.$$$')
  1446. else
  1447. outppu:=tppufile.create(PPLFn);
  1448. outppu.createfile;
  1449. { Create new header, with the new flags }
  1450. outppu.header:=inppu.header;
  1451. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1452. if MakeStatic then
  1453. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1454. else
  1455. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1456. { read until the object files are found }
  1457. untilb:=iblinkunitofiles;
  1458. repeat
  1459. b:=inppu.readentry;
  1460. if b in [ibendinterface,ibend] then
  1461. begin
  1462. inppu.free;
  1463. outppu.free;
  1464. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1465. Exit;
  1466. end;
  1467. if b<>untilb then
  1468. begin
  1469. repeat
  1470. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1471. outppu.putdata(buffer,l);
  1472. until l<sizeof(buffer);
  1473. outppu.writeentry(b);
  1474. end;
  1475. until (b=untilb);
  1476. { we have now reached the section for the files which need to be added,
  1477. now add them to the list }
  1478. case b of
  1479. iblinkunitofiles :
  1480. begin
  1481. { add all o files, and save the entry when not creating a static
  1482. library to keep staticlinking possible }
  1483. while not inppu.endofentry do
  1484. begin
  1485. s:=inppu.getstring;
  1486. m:=inppu.getlongint;
  1487. if not MakeStatic then
  1488. begin
  1489. outppu.putstring(s);
  1490. outppu.putlongint(m);
  1491. end;
  1492. current_module.linkotherofiles.add(s,link_always);;
  1493. end;
  1494. if not MakeStatic then
  1495. outppu.writeentry(b);
  1496. end;
  1497. { iblinkunitstaticlibs :
  1498. begin
  1499. AddToLinkFiles(ExtractLib(inppu.getstring));
  1500. if not inppu.endofentry then
  1501. begin
  1502. repeat
  1503. inppu.getdatabuf(buffer^,bufsize,l);
  1504. outppu.putdata(buffer^,l);
  1505. until l<bufsize;
  1506. outppu.writeentry(b);
  1507. end;
  1508. end; }
  1509. end;
  1510. { just add a new entry with the new lib }
  1511. if MakeStatic then
  1512. begin
  1513. outppu.putstring('imp'+current_module.realmodulename^);
  1514. outppu.putlongint(link_static);
  1515. outppu.writeentry(iblinkunitstaticlibs)
  1516. end
  1517. else
  1518. begin
  1519. outppu.putstring('imp'+current_module.realmodulename^);
  1520. outppu.putlongint(link_shared);
  1521. outppu.writeentry(iblinkunitsharedlibs);
  1522. end;
  1523. { read all entries until the end and write them also to the new ppu }
  1524. repeat
  1525. b:=inppu.readentry;
  1526. { don't write ibend, that's written automaticly }
  1527. if b<>ibend then
  1528. begin
  1529. if b=iblinkothersharedlibs then
  1530. begin
  1531. while not inppu.endofentry do
  1532. begin
  1533. s:=inppu.getstring;
  1534. m:=inppu.getlongint;
  1535. outppu.putstring(s);
  1536. outppu.putlongint(m);
  1537. { strip lib prefix }
  1538. if copy(s,1,3)='lib' then
  1539. delete(s,1,3);
  1540. ext:=ExtractFileExt(s);
  1541. if ext<>'' then
  1542. delete(s,length(s)-length(ext)+1,length(ext));
  1543. current_module.linkOtherSharedLibs.add(s,link_always);
  1544. end;
  1545. end
  1546. else
  1547. repeat
  1548. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1549. outppu.putdata(buffer,l);
  1550. until l<sizeof(buffer);
  1551. outppu.writeentry(b);
  1552. end;
  1553. until b=ibend;
  1554. { write the last stuff and close }
  1555. outppu.flush;
  1556. outppu.writeheader;
  1557. outppu.free;
  1558. inppu.free;
  1559. { rename }
  1560. if PPUFn=PPLFn then
  1561. begin
  1562. {$I-}
  1563. assign(f,PPUFn);
  1564. erase(f);
  1565. assign(f,'ppumove.$$$');
  1566. rename(f,PPUFn);
  1567. {$I+}
  1568. if ioresult<>0 then;
  1569. end;
  1570. Result:=True;
  1571. end;
  1572. procedure createimportlibfromexports;
  1573. var
  1574. hp : texported_item;
  1575. begin
  1576. hp:=texported_item(current_module._exports.first);
  1577. while assigned(hp) do
  1578. begin
  1579. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.index,hp.is_var,false);
  1580. hp:=texported_item(hp.next);
  1581. end;
  1582. end;
  1583. procedure proc_package;
  1584. var
  1585. main_file : tinputfile;
  1586. hp,hp2 : tmodule;
  1587. {finalize_procinfo,
  1588. init_procinfo,
  1589. main_procinfo : tcgprocinfo;}
  1590. force_init_final : boolean;
  1591. uu : tused_unit;
  1592. begin
  1593. Status.IsPackage:=true;
  1594. Status.IsExe:=true;
  1595. parse_only:=false;
  1596. {main_procinfo:=nil;
  1597. init_procinfo:=nil;
  1598. finalize_procinfo:=nil;}
  1599. if not RelocSectionSetExplicitly then
  1600. RelocSection:=true;
  1601. { Relocation works only without stabs under Windows when }
  1602. { external linker (LD) is used. LD generates relocs for }
  1603. { stab sections which is not loaded in memory. It causes }
  1604. { AV error when DLL is loaded and relocation is needed. }
  1605. { Internal linker does not have this problem. }
  1606. if RelocSection and
  1607. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1608. (cs_link_extern in current_settings.globalswitches) then
  1609. begin
  1610. include(current_settings.globalswitches,cs_link_strip);
  1611. { Warning stabs info does not work with reloc section !! }
  1612. if (cs_debuginfo in current_settings.moduleswitches) and
  1613. (target_dbg.id=dbg_stabs) then
  1614. begin
  1615. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1616. Message(parser_w_parser_win32_debug_needs_WN);
  1617. exclude(current_settings.moduleswitches,cs_debuginfo);
  1618. end;
  1619. end;
  1620. { get correct output names }
  1621. main_file := current_scanner.inputfile;
  1622. while assigned(main_file.next) do
  1623. main_file := main_file.next;
  1624. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1625. consume(_ID);
  1626. current_module.setmodulename(orgpattern);
  1627. current_module.ispackage:=true;
  1628. exportlib.preparelib(orgpattern);
  1629. if tf_library_needs_pic in target_info.flags then
  1630. include(current_settings.moduleswitches,cs_create_pic);
  1631. consume(_ID);
  1632. consume(_SEMICOLON);
  1633. { global switches are read, so further changes aren't allowed }
  1634. current_module.in_global:=false;
  1635. { setup things using the switches }
  1636. setupglobalswitches;
  1637. { set implementation flag }
  1638. current_module.in_interface:=false;
  1639. current_module.interface_compiled:=true;
  1640. { insert after the unit symbol tables the static symbol table }
  1641. { of the program }
  1642. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1643. {Load the units used by the program we compile.}
  1644. if token=_REQUIRES then
  1645. begin
  1646. end;
  1647. {Load the units used by the program we compile.}
  1648. if (token=_ID) and (idtoken=_CONTAINS) then
  1649. begin
  1650. consume(_ID);
  1651. while true do
  1652. begin
  1653. if token=_ID then
  1654. AddUnit(pattern);
  1655. consume(_ID);
  1656. if token=_COMMA then
  1657. consume(_COMMA)
  1658. else break;
  1659. end;
  1660. consume(_SEMICOLON);
  1661. end;
  1662. { All units are read, now give them a number }
  1663. current_module.updatemaps;
  1664. {Insert the name of the main program into the symbol table.}
  1665. if current_module.realmodulename^<>'' then
  1666. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1667. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1668. symtablestack.push(current_module.localsymtable);
  1669. { create whole program optimisation information }
  1670. current_module.wpoinfo:=tunitwpoinfo.create;
  1671. { should we force unit initialization? }
  1672. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1673. if force_init_final then
  1674. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1675. { Add symbol to the exports section for win32 so smartlinking a
  1676. DLL will include the edata section }
  1677. if assigned(exportlib) and
  1678. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1679. ((current_module.flags and uf_has_exports)<>0) then
  1680. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1681. { all labels must be defined before generating code }
  1682. if Errorcount=0 then
  1683. tstoredsymtable(current_module.localsymtable).checklabels;
  1684. symtablestack.pop(current_module.localsymtable);
  1685. { consume the last point }
  1686. consume(_END);
  1687. consume(_POINT);
  1688. if (Errorcount=0) then
  1689. begin
  1690. { test static symtable }
  1691. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1692. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1693. tstoredsymtable(current_module.localsymtable).check_forwards;
  1694. current_module.allunitsused;
  1695. end;
  1696. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1697. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1698. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1699. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1700. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1701. {$ifdef i386}
  1702. { fix me! }
  1703. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1704. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const(A_RET,S_W,12));
  1705. {$endif i386}
  1706. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1707. { leave when we got an error }
  1708. if (Errorcount>0) and not status.skip_error then
  1709. begin
  1710. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1711. status.skip_error:=true;
  1712. exit;
  1713. end;
  1714. { remove all unused units, this happends when units are removed
  1715. from the uses clause in the source and the ppu was already being loaded }
  1716. hp:=tmodule(loaded_units.first);
  1717. while assigned(hp) do
  1718. begin
  1719. hp2:=hp;
  1720. hp:=tmodule(hp.next);
  1721. if hp2.is_unit and
  1722. not assigned(hp2.globalsymtable) then
  1723. loaded_units.remove(hp2);
  1724. end;
  1725. { force exports }
  1726. uu:=tused_unit(usedunits.first);
  1727. while assigned(uu) do
  1728. begin
  1729. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1730. { check localsymtable for exports too to get public symbols }
  1731. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1732. { create special exports }
  1733. if (uu.u.flags and uf_init)<>0 then
  1734. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1735. if (uu.u.flags and uf_finalize)<>0 then
  1736. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1737. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1738. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1739. uu:=tused_unit(uu.next);
  1740. end;
  1741. {$ifdef arm}
  1742. { Insert .pdata section for arm-wince.
  1743. It is needed for exception handling. }
  1744. if target_info.system in [system_arm_wince] then
  1745. InsertPData;
  1746. {$endif arm}
  1747. { generate debuginfo }
  1748. if (cs_debuginfo in current_settings.moduleswitches) then
  1749. current_debuginfo.inserttypeinfo;
  1750. exportlib.generatelib;
  1751. { write all our exports to the import library,
  1752. needs to be done after exportlib.generatelib; }
  1753. createimportlibfromexports;
  1754. { generate imports }
  1755. if current_module.ImportLibraryList.Count>0 then
  1756. importlib.generatelib;
  1757. { Reference all DEBUGINFO sections from the main .fpc section }
  1758. if (cs_debuginfo in current_settings.moduleswitches) then
  1759. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1760. { insert own objectfile }
  1761. insertobjectfile;
  1762. { assemble and link }
  1763. create_objectfile;
  1764. { We might need the symbols info if not using
  1765. the default do_extractsymbolinfo
  1766. which is a dummy function PM }
  1767. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1768. { release all local symtables that are not needed anymore }
  1769. if (not needsymbolinfo) then
  1770. free_localsymtables(current_module.localsymtable);
  1771. { leave when we got an error }
  1772. if (Errorcount>0) and not status.skip_error then
  1773. begin
  1774. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1775. status.skip_error:=true;
  1776. exit;
  1777. end;
  1778. if (not current_module.is_unit) then
  1779. begin
  1780. { finally rewrite all units included into the package }
  1781. uu:=tused_unit(usedunits.first);
  1782. while assigned(uu) do
  1783. begin
  1784. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1785. uu:=tused_unit(uu.next);
  1786. end;
  1787. { create the executable when we are at level 1 }
  1788. if (compile_level=1) then
  1789. begin
  1790. { create global resource file by collecting all resource files }
  1791. CollectResourceFiles;
  1792. { write .def file }
  1793. if (cs_link_deffile in current_settings.globalswitches) then
  1794. deffile.writefile;
  1795. { insert all .o files from all loaded units and
  1796. unload the units, we don't need them anymore.
  1797. Keep the current_module because that is still needed }
  1798. hp:=tmodule(loaded_units.first);
  1799. while assigned(hp) do
  1800. begin
  1801. { the package itself contains no code so far }
  1802. linker.AddModuleFiles(hp);
  1803. hp2:=tmodule(hp.next);
  1804. if (hp<>current_module) and
  1805. (not needsymbolinfo) then
  1806. begin
  1807. loaded_units.remove(hp);
  1808. hp.free;
  1809. end;
  1810. hp:=hp2;
  1811. end;
  1812. linker.MakeSharedLibrary
  1813. end;
  1814. { Give Fatal with error count for linker errors }
  1815. if (Errorcount>0) and not status.skip_error then
  1816. begin
  1817. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1818. status.skip_error:=true;
  1819. end;
  1820. end;
  1821. end;
  1822. procedure proc_program(islibrary : boolean);
  1823. var
  1824. main_file : tinputfile;
  1825. hp,hp2 : tmodule;
  1826. finalize_procinfo,
  1827. init_procinfo,
  1828. main_procinfo : tcgprocinfo;
  1829. force_init_final : boolean;
  1830. resources_used : boolean;
  1831. begin
  1832. DLLsource:=islibrary;
  1833. Status.IsLibrary:=IsLibrary;
  1834. Status.IsPackage:=false;
  1835. Status.IsExe:=true;
  1836. parse_only:=false;
  1837. main_procinfo:=nil;
  1838. init_procinfo:=nil;
  1839. finalize_procinfo:=nil;
  1840. resources_used:=false;
  1841. { DLL defaults to create reloc info }
  1842. if islibrary then
  1843. begin
  1844. if not RelocSectionSetExplicitly then
  1845. RelocSection:=true;
  1846. end;
  1847. { Relocation works only without stabs under Windows when }
  1848. { external linker (LD) is used. LD generates relocs for }
  1849. { stab sections which is not loaded in memory. It causes }
  1850. { AV error when DLL is loaded and relocation is needed. }
  1851. { Internal linker does not have this problem. }
  1852. if RelocSection and
  1853. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1854. (cs_link_extern in current_settings.globalswitches) then
  1855. begin
  1856. include(current_settings.globalswitches,cs_link_strip);
  1857. { Warning stabs info does not work with reloc section !! }
  1858. if (cs_debuginfo in current_settings.moduleswitches) and
  1859. (target_dbg.id=dbg_stabs) then
  1860. begin
  1861. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1862. Message(parser_w_parser_win32_debug_needs_WN);
  1863. exclude(current_settings.moduleswitches,cs_debuginfo);
  1864. end;
  1865. end;
  1866. { get correct output names }
  1867. main_file := current_scanner.inputfile;
  1868. while assigned(main_file.next) do
  1869. main_file := main_file.next;
  1870. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1871. if islibrary then
  1872. begin
  1873. consume(_LIBRARY);
  1874. current_module.setmodulename(orgpattern);
  1875. current_module.islibrary:=true;
  1876. exportlib.preparelib(orgpattern);
  1877. if tf_library_needs_pic in target_info.flags then
  1878. include(current_settings.moduleswitches,cs_create_pic);
  1879. consume(_ID);
  1880. consume(_SEMICOLON);
  1881. end
  1882. else
  1883. { is there an program head ? }
  1884. if token=_PROGRAM then
  1885. begin
  1886. consume(_PROGRAM);
  1887. current_module.setmodulename(orgpattern);
  1888. if (target_info.system in systems_unit_program_exports) then
  1889. exportlib.preparelib(orgpattern);
  1890. consume(_ID);
  1891. if token=_LKLAMMER then
  1892. begin
  1893. consume(_LKLAMMER);
  1894. repeat
  1895. consume(_ID);
  1896. until not try_to_consume(_COMMA);
  1897. consume(_RKLAMMER);
  1898. end;
  1899. consume(_SEMICOLON);
  1900. end
  1901. else if (target_info.system in systems_unit_program_exports) then
  1902. exportlib.preparelib(current_module.realmodulename^);
  1903. { global switches are read, so further changes aren't allowed }
  1904. current_module.in_global:=false;
  1905. { setup things using the switches }
  1906. setupglobalswitches;
  1907. { set implementation flag }
  1908. current_module.in_interface:=false;
  1909. current_module.interface_compiled:=true;
  1910. { insert after the unit symbol tables the static symbol table }
  1911. { of the program }
  1912. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1913. { load standard units (system,objpas,profile unit) }
  1914. loaddefaultunits;
  1915. { Load units provided on the command line }
  1916. loadautounits;
  1917. {Load the units used by the program we compile.}
  1918. if token=_USES then
  1919. loadunits;
  1920. { All units are read, now give them a number }
  1921. current_module.updatemaps;
  1922. {Insert the name of the main program into the symbol table.}
  1923. if current_module.realmodulename^<>'' then
  1924. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1925. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1926. symtablestack.push(current_module.localsymtable);
  1927. { create whole program optimisation information }
  1928. current_module.wpoinfo:=tunitwpoinfo.create;
  1929. { The program intialization needs an alias, so it can be called
  1930. from the bootstrap code.}
  1931. if islibrary then
  1932. begin
  1933. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1934. { Win32 startup code needs a single name }
  1935. if not(target_info.system in systems_darwin) then
  1936. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1937. else
  1938. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1939. end
  1940. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1941. begin
  1942. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1943. end
  1944. else
  1945. begin
  1946. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1947. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1948. end;
  1949. main_procinfo.parse_body;
  1950. { save file pos for debuginfo }
  1951. current_module.mainfilepos:=main_procinfo.entrypos;
  1952. { Generate specializations of objectdefs methods }
  1953. generate_specialization_procs;
  1954. { should we force unit initialization? }
  1955. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1956. if force_init_final then
  1957. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1958. { Add symbol to the exports section for win32 so smartlinking a
  1959. DLL will include the edata section }
  1960. if assigned(exportlib) and
  1961. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1962. ((current_module.flags and uf_has_exports)<>0) then
  1963. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1964. { finalize? }
  1965. if token=_FINALIZATION then
  1966. begin
  1967. { the uf_finalize flag is only set after we checked that it
  1968. wasn't empty }
  1969. { Parse the finalize }
  1970. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1971. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1972. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1973. finalize_procinfo.parse_body;
  1974. end
  1975. else
  1976. if force_init_final then
  1977. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1978. { the finalization routine of libraries is generic (and all libraries need to }
  1979. { be finalized, so they can finalize any units they use }
  1980. if (islibrary) then
  1981. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1982. { all labels must be defined before generating code }
  1983. if Errorcount=0 then
  1984. tstoredsymtable(current_module.localsymtable).checklabels;
  1985. { See remark in unit init/final }
  1986. main_procinfo.generate_code;
  1987. main_procinfo.resetprocdef;
  1988. release_main_proc(main_procinfo);
  1989. if assigned(init_procinfo) then
  1990. begin
  1991. init_procinfo.generate_code;
  1992. init_procinfo.resetprocdef;
  1993. release_main_proc(init_procinfo);
  1994. end;
  1995. if assigned(finalize_procinfo) then
  1996. begin
  1997. finalize_procinfo.generate_code;
  1998. finalize_procinfo.resetprocdef;
  1999. release_main_proc(finalize_procinfo);
  2000. end;
  2001. symtablestack.pop(current_module.localsymtable);
  2002. { consume the last point }
  2003. consume(_POINT);
  2004. { reset wpo flags for all defs }
  2005. reset_all_defs;
  2006. if (Errorcount=0) then
  2007. begin
  2008. { test static symtable }
  2009. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2010. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2011. tstoredsymtable(current_module.localsymtable).check_forwards;
  2012. current_module.allunitsused;
  2013. end;
  2014. { leave when we got an error }
  2015. if (Errorcount>0) and not status.skip_error then
  2016. begin
  2017. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2018. status.skip_error:=true;
  2019. exit;
  2020. end;
  2021. { remove all unused units, this happens when units are removed
  2022. from the uses clause in the source and the ppu was already being loaded }
  2023. hp:=tmodule(loaded_units.first);
  2024. while assigned(hp) do
  2025. begin
  2026. hp2:=hp;
  2027. hp:=tmodule(hp.next);
  2028. if hp2.is_unit and
  2029. not assigned(hp2.globalsymtable) then
  2030. begin
  2031. loaded_units.remove(hp2);
  2032. unloaded_units.concat(hp2);
  2033. end;
  2034. end;
  2035. { do we need to add the variants unit? }
  2036. maybeloadvariantsunit;
  2037. { Now that everything has been compiled we know if we need resource
  2038. support. If not, remove the unit. }
  2039. resources_used:=MaybeRemoveResUnit;
  2040. linker.initsysinitunitname;
  2041. if target_info.system in systems_internal_sysinit then
  2042. begin
  2043. { add start/halt unit }
  2044. AddUnit(linker.sysinitunit);
  2045. end;
  2046. {$ifdef arm}
  2047. { Insert .pdata section for arm-wince.
  2048. It is needed for exception handling. }
  2049. if target_info.system in [system_arm_wince] then
  2050. InsertPData;
  2051. {$endif arm}
  2052. InsertThreadvars;
  2053. { generate pic helpers to load eip if necessary }
  2054. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  2055. { generate rtti/init tables }
  2056. write_persistent_type_info(current_module.localsymtable);
  2057. { if an Objective-C module, generate rtti and module info }
  2058. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2059. { generate wrappers for interfaces }
  2060. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  2061. { generate imports }
  2062. if current_module.ImportLibraryList.Count>0 then
  2063. importlib.generatelib;
  2064. { generate debuginfo }
  2065. if (cs_debuginfo in current_settings.moduleswitches) then
  2066. current_debuginfo.inserttypeinfo;
  2067. if islibrary or (target_info.system in systems_unit_program_exports) then
  2068. exportlib.generatelib;
  2069. { Reference all DEBUGINFO sections from the main .fpc section }
  2070. if (cs_debuginfo in current_settings.moduleswitches) then
  2071. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2072. { Resource strings }
  2073. GenerateResourceStrings;
  2074. { Windows widestring needing initialization }
  2075. InsertWideInits;
  2076. { insert Tables and StackLength }
  2077. InsertInitFinalTable;
  2078. InsertThreadvarTablesTable;
  2079. InsertResourceTablesTable;
  2080. InsertWideInitsTablesTable;
  2081. InsertMemorySizes;
  2082. { Insert symbol to resource info }
  2083. InsertResourceInfo(resources_used);
  2084. { create callframe info }
  2085. create_dwarf_frame;
  2086. { insert own objectfile }
  2087. insertobjectfile;
  2088. { assemble and link }
  2089. create_objectfile;
  2090. { We might need the symbols info if not using
  2091. the default do_extractsymbolinfo
  2092. which is a dummy function PM }
  2093. needsymbolinfo:=
  2094. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2095. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2096. { release all local symtables that are not needed anymore }
  2097. if (not needsymbolinfo) then
  2098. free_localsymtables(current_module.localsymtable);
  2099. { leave when we got an error }
  2100. if (Errorcount>0) and not status.skip_error then
  2101. begin
  2102. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2103. status.skip_error:=true;
  2104. exit;
  2105. end;
  2106. if (not current_module.is_unit) then
  2107. begin
  2108. { create the executable when we are at level 1 }
  2109. if (compile_level=1) then
  2110. begin
  2111. { create global resource file by collecting all resource files }
  2112. CollectResourceFiles;
  2113. { write .def file }
  2114. if (cs_link_deffile in current_settings.globalswitches) then
  2115. deffile.writefile;
  2116. { insert all .o files from all loaded units and
  2117. unload the units, we don't need them anymore.
  2118. Keep the current_module because that is still needed }
  2119. hp:=tmodule(loaded_units.first);
  2120. while assigned(hp) do
  2121. begin
  2122. linker.AddModuleFiles(hp);
  2123. hp2:=tmodule(hp.next);
  2124. if (hp<>current_module) and
  2125. (not needsymbolinfo) then
  2126. begin
  2127. loaded_units.remove(hp);
  2128. hp.free;
  2129. end;
  2130. hp:=hp2;
  2131. end;
  2132. { free also unneeded units we didn't free before }
  2133. if not needsymbolinfo then
  2134. unloaded_units.Clear;
  2135. { finally we can create a executable }
  2136. if DLLSource then
  2137. linker.MakeSharedLibrary
  2138. else
  2139. linker.MakeExecutable;
  2140. { collect all necessary information for whole-program optimization }
  2141. wpoinfomanager.extractwpoinfofromprogram;
  2142. end;
  2143. { Give Fatal with error count for linker errors }
  2144. if (Errorcount>0) and not status.skip_error then
  2145. begin
  2146. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2147. status.skip_error:=true;
  2148. end;
  2149. end;
  2150. end;
  2151. end.