pmodules.pas 82 KB

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