pmodules.pas 80 KB

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