pmodules.pas 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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. { remove cross unit overloads }
  1036. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1037. { test static symtable }
  1038. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1039. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1040. tstoredsymtable(current_module.localsymtable).check_forwards;
  1041. tstoredsymtable(current_module.localsymtable).checklabels;
  1042. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1043. { used units }
  1044. current_module.allunitsused;
  1045. end;
  1046. { leave when we got an error }
  1047. if (Errorcount>0) and not status.skip_error then
  1048. begin
  1049. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1050. status.skip_error:=true;
  1051. exit;
  1052. end;
  1053. { do we need to add the variants unit? }
  1054. maybeloadvariantsunit;
  1055. { generate wrappers for interfaces }
  1056. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  1057. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1058. { generate pic helpers to load eip if necessary }
  1059. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1060. { generate rtti/init tables }
  1061. write_persistent_type_info(current_module.globalsymtable);
  1062. write_persistent_type_info(current_module.localsymtable);
  1063. { Tables }
  1064. insertThreadVars;
  1065. { Resource strings }
  1066. GenerateResourceStrings;
  1067. { generate debuginfo }
  1068. if (cs_debuginfo in current_settings.moduleswitches) then
  1069. current_debuginfo.inserttypeinfo;
  1070. { generate imports }
  1071. if current_module.ImportLibraryList.Count>0 then
  1072. importlib.generatelib;
  1073. { insert own objectfile, or say that it's in a library
  1074. (no check for an .o when loading) }
  1075. ag:=is_assembler_generated;
  1076. if ag then
  1077. insertobjectfile
  1078. else
  1079. begin
  1080. current_module.flags:=current_module.flags or uf_no_link;
  1081. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1082. end;
  1083. if ag then
  1084. begin
  1085. { create dwarf debuginfo }
  1086. create_dwarf;
  1087. { assemble }
  1088. create_objectfile;
  1089. end;
  1090. { Write out the ppufile after the object file has been created }
  1091. store_interface_crc:=current_module.interface_crc;
  1092. {$ifdef EXTDEBUG}
  1093. store_crc:=current_module.crc;
  1094. {$endif EXTDEBUG}
  1095. if (Errorcount=0) then
  1096. tppumodule(current_module).writeppu;
  1097. if not(cs_compilesystem in current_settings.moduleswitches) then
  1098. if store_interface_crc<>current_module.interface_crc then
  1099. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1100. {$ifdef EXTDEBUG}
  1101. if not(cs_compilesystem in current_settings.moduleswitches) then
  1102. if (store_crc<>current_module.crc) and simplify_ppu then
  1103. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1104. {$endif EXTDEBUG}
  1105. { release all overload references and local symtables that
  1106. are not needed anymore }
  1107. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1108. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1109. free_localsymtables(current_module.globalsymtable);
  1110. free_localsymtables(current_module.localsymtable);
  1111. { leave when we got an error }
  1112. if (Errorcount>0) and not status.skip_error then
  1113. begin
  1114. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1115. status.skip_error:=true;
  1116. exit;
  1117. end;
  1118. {$ifdef debug_devirt}
  1119. { print out all instantiated class/object types }
  1120. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1121. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1122. begin
  1123. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1124. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1125. objectdef:
  1126. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1127. odt_object:
  1128. writeln(' (object)');
  1129. odt_class:
  1130. writeln(' (class)');
  1131. else
  1132. internalerror(2008092101);
  1133. end;
  1134. classrefdef:
  1135. writeln(' (classrefdef)');
  1136. else
  1137. internalerror(2008092102);
  1138. end;
  1139. end;
  1140. {$endif debug_devirt}
  1141. Message1(unit_u_finished_compiling,current_module.modulename^);
  1142. end;
  1143. procedure procexport(const s : string);
  1144. var
  1145. hp : texported_item;
  1146. begin
  1147. hp:=texported_item.create;
  1148. hp.name:=stringdup(s);
  1149. hp.options:=hp.options or eo_name;
  1150. exportlib.exportprocedure(hp);
  1151. end;
  1152. procedure varexport(const s : string);
  1153. var
  1154. hp : texported_item;
  1155. begin
  1156. hp:=texported_item.create;
  1157. hp.name:=stringdup(s);
  1158. hp.options:=hp.options or eo_name;
  1159. exportlib.exportvar(hp);
  1160. end;
  1161. procedure insert_export(sym : TObject;arg:pointer);
  1162. var
  1163. i : longint;
  1164. item : TCmdStrListItem;
  1165. begin
  1166. case TSym(sym).typ of
  1167. { ignore: }
  1168. unitsym,
  1169. syssym,
  1170. constsym,
  1171. enumsym,
  1172. typesym:
  1173. ;
  1174. procsym:
  1175. begin
  1176. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1177. begin
  1178. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1179. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1180. ((tsymtable(arg).symtabletype=globalsymtable) or
  1181. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1182. ) then
  1183. begin
  1184. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1185. { walk through all aliases }
  1186. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1187. while assigned(item) do
  1188. begin
  1189. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1190. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1191. procexport(item.str);
  1192. item:=TCmdStrListItem(item.next);
  1193. end;
  1194. end;
  1195. end;
  1196. end;
  1197. staticvarsym:
  1198. begin
  1199. varexport(tsym(sym).mangledname);
  1200. end;
  1201. else
  1202. begin
  1203. writeln('unknown: ',ord(TSym(sym).typ));
  1204. end;
  1205. end;
  1206. end;
  1207. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1208. Var
  1209. MakeStatic : Boolean;
  1210. Var
  1211. buffer : array[0..$1fff] of byte;
  1212. inppu,
  1213. outppu : tppufile;
  1214. b,
  1215. untilb : byte;
  1216. l,m : longint;
  1217. f : file;
  1218. ext,
  1219. s : string;
  1220. ppuversion : dword;
  1221. begin
  1222. Result:=false;
  1223. MakeStatic:=False;
  1224. inppu:=tppufile.create(PPUFn);
  1225. if not inppu.openfile then
  1226. begin
  1227. inppu.free;
  1228. Comment(V_Error,'Could not open : '+PPUFn);
  1229. Exit;
  1230. end;
  1231. { Check the ppufile }
  1232. if not inppu.CheckPPUId then
  1233. begin
  1234. inppu.free;
  1235. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1236. Exit;
  1237. end;
  1238. ppuversion:=inppu.GetPPUVersion;
  1239. if ppuversion<CurrentPPUVersion then
  1240. begin
  1241. inppu.free;
  1242. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1243. Exit;
  1244. end;
  1245. { No .o file generated for this ppu, just skip }
  1246. if (inppu.header.flags and uf_no_link)<>0 then
  1247. begin
  1248. inppu.free;
  1249. Result:=true;
  1250. Exit;
  1251. end;
  1252. { Already a lib? }
  1253. if (inppu.header.flags and uf_in_library)<>0 then
  1254. begin
  1255. inppu.free;
  1256. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1257. Exit;
  1258. end;
  1259. { We need a static linked unit }
  1260. if (inppu.header.flags and uf_static_linked)=0 then
  1261. begin
  1262. inppu.free;
  1263. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1264. Exit;
  1265. end;
  1266. { Check if shared is allowed }
  1267. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1268. begin
  1269. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1270. MakeStatic:=true;
  1271. end;
  1272. { Create the new ppu }
  1273. if PPUFn=PPLFn then
  1274. outppu:=tppufile.create('ppumove.$$$')
  1275. else
  1276. outppu:=tppufile.create(PPLFn);
  1277. outppu.createfile;
  1278. { Create new header, with the new flags }
  1279. outppu.header:=inppu.header;
  1280. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1281. if MakeStatic then
  1282. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1283. else
  1284. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1285. { read until the object files are found }
  1286. untilb:=iblinkunitofiles;
  1287. repeat
  1288. b:=inppu.readentry;
  1289. if b in [ibendinterface,ibend] then
  1290. begin
  1291. inppu.free;
  1292. outppu.free;
  1293. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1294. Exit;
  1295. end;
  1296. if b<>untilb then
  1297. begin
  1298. repeat
  1299. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1300. outppu.putdata(buffer,l);
  1301. until l<sizeof(buffer);
  1302. outppu.writeentry(b);
  1303. end;
  1304. until (b=untilb);
  1305. { we have now reached the section for the files which need to be added,
  1306. now add them to the list }
  1307. case b of
  1308. iblinkunitofiles :
  1309. begin
  1310. { add all o files, and save the entry when not creating a static
  1311. library to keep staticlinking possible }
  1312. while not inppu.endofentry do
  1313. begin
  1314. s:=inppu.getstring;
  1315. m:=inppu.getlongint;
  1316. if not MakeStatic then
  1317. begin
  1318. outppu.putstring(s);
  1319. outppu.putlongint(m);
  1320. end;
  1321. current_module.linkotherofiles.add(s,link_always);;
  1322. end;
  1323. if not MakeStatic then
  1324. outppu.writeentry(b);
  1325. end;
  1326. { iblinkunitstaticlibs :
  1327. begin
  1328. AddToLinkFiles(ExtractLib(inppu.getstring));
  1329. if not inppu.endofentry then
  1330. begin
  1331. repeat
  1332. inppu.getdatabuf(buffer^,bufsize,l);
  1333. outppu.putdata(buffer^,l);
  1334. until l<bufsize;
  1335. outppu.writeentry(b);
  1336. end;
  1337. end; }
  1338. end;
  1339. { just add a new entry with the new lib }
  1340. if MakeStatic then
  1341. begin
  1342. outppu.putstring('imp'+current_module.realmodulename^);
  1343. outppu.putlongint(link_static);
  1344. outppu.writeentry(iblinkunitstaticlibs)
  1345. end
  1346. else
  1347. begin
  1348. outppu.putstring('imp'+current_module.realmodulename^);
  1349. outppu.putlongint(link_shared);
  1350. outppu.writeentry(iblinkunitsharedlibs);
  1351. end;
  1352. { read all entries until the end and write them also to the new ppu }
  1353. repeat
  1354. b:=inppu.readentry;
  1355. { don't write ibend, that's written automaticly }
  1356. if b<>ibend then
  1357. begin
  1358. if b=iblinkothersharedlibs then
  1359. begin
  1360. while not inppu.endofentry do
  1361. begin
  1362. s:=inppu.getstring;
  1363. m:=inppu.getlongint;
  1364. outppu.putstring(s);
  1365. outppu.putlongint(m);
  1366. { strip lib prefix }
  1367. if copy(s,1,3)='lib' then
  1368. delete(s,1,3);
  1369. ext:=ExtractFileExt(s);
  1370. if ext<>'' then
  1371. delete(s,length(s)-length(ext)+1,length(ext));
  1372. current_module.linkOtherSharedLibs.add(s,link_always);
  1373. end;
  1374. end
  1375. else
  1376. repeat
  1377. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1378. outppu.putdata(buffer,l);
  1379. until l<sizeof(buffer);
  1380. outppu.writeentry(b);
  1381. end;
  1382. until b=ibend;
  1383. { write the last stuff and close }
  1384. outppu.flush;
  1385. outppu.writeheader;
  1386. outppu.free;
  1387. inppu.free;
  1388. { rename }
  1389. if PPUFn=PPLFn then
  1390. begin
  1391. {$I-}
  1392. assign(f,PPUFn);
  1393. erase(f);
  1394. assign(f,'ppumove.$$$');
  1395. rename(f,PPUFn);
  1396. {$I+}
  1397. if ioresult<>0 then;
  1398. end;
  1399. Result:=True;
  1400. end;
  1401. procedure createimportlibfromexports;
  1402. var
  1403. hp : texported_item;
  1404. begin
  1405. hp:=texported_item(current_module._exports.first);
  1406. while assigned(hp) do
  1407. begin
  1408. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.index,hp.is_var,false);
  1409. hp:=texported_item(hp.next);
  1410. end;
  1411. end;
  1412. procedure proc_package;
  1413. var
  1414. main_file : tinputfile;
  1415. hp,hp2 : tmodule;
  1416. {finalize_procinfo,
  1417. init_procinfo,
  1418. main_procinfo : tcgprocinfo;}
  1419. force_init_final : boolean;
  1420. uu : tused_unit;
  1421. begin
  1422. Status.IsPackage:=true;
  1423. Status.IsExe:=true;
  1424. parse_only:=false;
  1425. {main_procinfo:=nil;
  1426. init_procinfo:=nil;
  1427. finalize_procinfo:=nil;}
  1428. if not RelocSectionSetExplicitly then
  1429. RelocSection:=true;
  1430. { Relocation works only without stabs under Windows when }
  1431. { external linker (LD) is used. LD generates relocs for }
  1432. { stab sections which is not loaded in memory. It causes }
  1433. { AV error when DLL is loaded and relocation is needed. }
  1434. { Internal linker does not have this problem. }
  1435. if RelocSection and
  1436. (target_info.system in system_all_windows+[system_i386_wdosx]) and
  1437. (cs_link_extern in current_settings.globalswitches) then
  1438. begin
  1439. include(current_settings.globalswitches,cs_link_strip);
  1440. { Warning stabs info does not work with reloc section !! }
  1441. if cs_debuginfo in current_settings.moduleswitches then
  1442. begin
  1443. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1444. Message(parser_w_parser_win32_debug_needs_WN);
  1445. exclude(current_settings.moduleswitches,cs_debuginfo);
  1446. end;
  1447. end;
  1448. { get correct output names }
  1449. main_file := current_scanner.inputfile;
  1450. while assigned(main_file.next) do
  1451. main_file := main_file.next;
  1452. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1453. consume(_ID);
  1454. current_module.setmodulename(orgpattern);
  1455. current_module.ispackage:=true;
  1456. exportlib.preparelib(orgpattern);
  1457. if tf_library_needs_pic in target_info.flags then
  1458. include(current_settings.moduleswitches,cs_create_pic);
  1459. consume(_ID);
  1460. consume(_SEMICOLON);
  1461. { global switches are read, so further changes aren't allowed }
  1462. current_module.in_global:=false;
  1463. { setup things using the switches }
  1464. setupglobalswitches;
  1465. { set implementation flag }
  1466. current_module.in_interface:=false;
  1467. current_module.interface_compiled:=true;
  1468. { insert after the unit symbol tables the static symbol table }
  1469. { of the program }
  1470. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1471. {Load the units used by the program we compile.}
  1472. if token=_REQUIRES then
  1473. begin
  1474. end;
  1475. {Load the units used by the program we compile.}
  1476. if (token=_ID) and (idtoken=_CONTAINS) then
  1477. begin
  1478. consume(_ID);
  1479. while true do
  1480. begin
  1481. if token=_ID then
  1482. AddUnit(pattern);
  1483. consume(_ID);
  1484. if token=_COMMA then
  1485. consume(_COMMA)
  1486. else break;
  1487. end;
  1488. consume(_SEMICOLON);
  1489. end;
  1490. { reset ranges/stabs in exported definitions }
  1491. reset_all_defs;
  1492. { All units are read, now give them a number }
  1493. current_module.updatemaps;
  1494. {Insert the name of the main program into the symbol table.}
  1495. if current_module.realmodulename^<>'' then
  1496. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1497. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1498. symtablestack.push(current_module.localsymtable);
  1499. { create whole program optimisation information }
  1500. current_module.wpoinfo:=tunitwpoinfo.create;
  1501. { should we force unit initialization? }
  1502. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1503. if force_init_final then
  1504. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1505. { Add symbol to the exports section for win32 so smartlinking a
  1506. DLL will include the edata section }
  1507. if assigned(exportlib) and
  1508. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1509. ((current_module.flags and uf_has_exports)<>0) then
  1510. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1511. { all labels must be defined before generating code }
  1512. if Errorcount=0 then
  1513. tstoredsymtable(current_module.localsymtable).checklabels;
  1514. symtablestack.pop(current_module.localsymtable);
  1515. { consume the last point }
  1516. consume(_END);
  1517. consume(_POINT);
  1518. if (Errorcount=0) then
  1519. begin
  1520. { test static symtable }
  1521. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1522. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1523. tstoredsymtable(current_module.localsymtable).check_forwards;
  1524. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1525. current_module.allunitsused;
  1526. end;
  1527. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1528. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1529. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1530. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1531. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1532. {$ifdef i386}
  1533. { fix me! }
  1534. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const_reg(A_MOV,S_L,1,NR_EAX));
  1535. current_asmdata.asmlists[al_procedures].concat(Taicpu.Op_const(A_RET,S_W,12));
  1536. {$endif i386}
  1537. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1538. { leave when we got an error }
  1539. if (Errorcount>0) and not status.skip_error then
  1540. begin
  1541. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1542. status.skip_error:=true;
  1543. exit;
  1544. end;
  1545. { remove all unused units, this happends when units are removed
  1546. from the uses clause in the source and the ppu was already being loaded }
  1547. hp:=tmodule(loaded_units.first);
  1548. while assigned(hp) do
  1549. begin
  1550. hp2:=hp;
  1551. hp:=tmodule(hp.next);
  1552. if hp2.is_unit and
  1553. not assigned(hp2.globalsymtable) then
  1554. loaded_units.remove(hp2);
  1555. end;
  1556. { force exports }
  1557. uu:=tused_unit(usedunits.first);
  1558. while assigned(uu) do
  1559. begin
  1560. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1561. { check localsymtable for exports too to get public symbols }
  1562. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1563. { create special exports }
  1564. if (uu.u.flags and uf_init)<>0 then
  1565. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1566. if (uu.u.flags and uf_finalize)<>0 then
  1567. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1568. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1569. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1570. uu:=tused_unit(uu.next);
  1571. end;
  1572. {$ifdef arm}
  1573. { Insert .pdata section for arm-wince.
  1574. It is needed for exception handling. }
  1575. if target_info.system in [system_arm_wince] then
  1576. InsertPData;
  1577. {$endif arm}
  1578. { generate debuginfo }
  1579. if (cs_debuginfo in current_settings.moduleswitches) then
  1580. current_debuginfo.inserttypeinfo;
  1581. exportlib.generatelib;
  1582. { write all our exports to the import library,
  1583. needs to be done after exportlib.generatelib; }
  1584. createimportlibfromexports;
  1585. { generate imports }
  1586. if current_module.ImportLibraryList.Count>0 then
  1587. importlib.generatelib;
  1588. { Reference all DEBUGINFO sections from the main .fpc section }
  1589. if (cs_debuginfo in current_settings.moduleswitches) then
  1590. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1591. { insert own objectfile }
  1592. insertobjectfile;
  1593. { assemble and link }
  1594. create_objectfile;
  1595. { We might need the symbols info if not using
  1596. the default do_extractsymbolinfo
  1597. which is a dummy function PM }
  1598. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1599. { release all local symtables that are not needed anymore }
  1600. if (not needsymbolinfo) then
  1601. free_localsymtables(current_module.localsymtable);
  1602. { leave when we got an error }
  1603. if (Errorcount>0) and not status.skip_error then
  1604. begin
  1605. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1606. status.skip_error:=true;
  1607. exit;
  1608. end;
  1609. if (not current_module.is_unit) then
  1610. begin
  1611. { finally rewrite all units included into the package }
  1612. uu:=tused_unit(usedunits.first);
  1613. while assigned(uu) do
  1614. begin
  1615. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1616. uu:=tused_unit(uu.next);
  1617. end;
  1618. { create the executable when we are at level 1 }
  1619. if (compile_level=1) then
  1620. begin
  1621. { create global resource file by collecting all resource files }
  1622. CollectResourceFiles;
  1623. { write .def file }
  1624. if (cs_link_deffile in current_settings.globalswitches) then
  1625. deffile.writefile;
  1626. { insert all .o files from all loaded units and
  1627. unload the units, we don't need them anymore.
  1628. Keep the current_module because that is still needed }
  1629. hp:=tmodule(loaded_units.first);
  1630. while assigned(hp) do
  1631. begin
  1632. { the package itself contains no code so far }
  1633. linker.AddModuleFiles(hp);
  1634. hp2:=tmodule(hp.next);
  1635. if (hp<>current_module) and
  1636. (not needsymbolinfo) then
  1637. begin
  1638. loaded_units.remove(hp);
  1639. hp.free;
  1640. end;
  1641. hp:=hp2;
  1642. end;
  1643. linker.MakeSharedLibrary
  1644. end;
  1645. { Give Fatal with error count for linker errors }
  1646. if (Errorcount>0) and not status.skip_error then
  1647. begin
  1648. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1649. status.skip_error:=true;
  1650. end;
  1651. end;
  1652. end;
  1653. procedure proc_program(islibrary : boolean);
  1654. var
  1655. main_file : tinputfile;
  1656. hp,hp2 : tmodule;
  1657. finalize_procinfo,
  1658. init_procinfo,
  1659. main_procinfo : tcgprocinfo;
  1660. force_init_final : boolean;
  1661. resources_used : boolean;
  1662. begin
  1663. DLLsource:=islibrary;
  1664. Status.IsLibrary:=IsLibrary;
  1665. Status.IsPackage:=false;
  1666. Status.IsExe:=true;
  1667. parse_only:=false;
  1668. main_procinfo:=nil;
  1669. init_procinfo:=nil;
  1670. finalize_procinfo:=nil;
  1671. resources_used:=false;
  1672. { DLL defaults to create reloc info }
  1673. if islibrary then
  1674. begin
  1675. if not RelocSectionSetExplicitly then
  1676. RelocSection:=true;
  1677. end;
  1678. { Relocation works only without stabs under Windows when }
  1679. { external linker (LD) is used. LD generates relocs for }
  1680. { stab sections which is not loaded in memory. It causes }
  1681. { AV error when DLL is loaded and relocation is needed. }
  1682. { Internal linker does not have this problem. }
  1683. if RelocSection and
  1684. (target_info.system in system_all_windows+[system_i386_wdosx]) and
  1685. (cs_link_extern in current_settings.globalswitches) then
  1686. begin
  1687. include(current_settings.globalswitches,cs_link_strip);
  1688. { Warning stabs info does not work with reloc section !! }
  1689. if cs_debuginfo in current_settings.moduleswitches then
  1690. begin
  1691. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1692. Message(parser_w_parser_win32_debug_needs_WN);
  1693. exclude(current_settings.moduleswitches,cs_debuginfo);
  1694. end;
  1695. end;
  1696. { get correct output names }
  1697. main_file := current_scanner.inputfile;
  1698. while assigned(main_file.next) do
  1699. main_file := main_file.next;
  1700. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1701. if islibrary then
  1702. begin
  1703. consume(_LIBRARY);
  1704. current_module.setmodulename(orgpattern);
  1705. current_module.islibrary:=true;
  1706. exportlib.preparelib(orgpattern);
  1707. if tf_library_needs_pic in target_info.flags then
  1708. include(current_settings.moduleswitches,cs_create_pic);
  1709. consume(_ID);
  1710. consume(_SEMICOLON);
  1711. end
  1712. else
  1713. { is there an program head ? }
  1714. if token=_PROGRAM then
  1715. begin
  1716. consume(_PROGRAM);
  1717. current_module.setmodulename(orgpattern);
  1718. if (target_info.system in system_unit_program_exports) then
  1719. exportlib.preparelib(orgpattern);
  1720. consume(_ID);
  1721. if token=_LKLAMMER then
  1722. begin
  1723. consume(_LKLAMMER);
  1724. repeat
  1725. consume(_ID);
  1726. until not try_to_consume(_COMMA);
  1727. consume(_RKLAMMER);
  1728. end;
  1729. consume(_SEMICOLON);
  1730. end
  1731. else if (target_info.system in system_unit_program_exports) then
  1732. exportlib.preparelib(current_module.realmodulename^);
  1733. { global switches are read, so further changes aren't allowed }
  1734. current_module.in_global:=false;
  1735. { setup things using the switches }
  1736. setupglobalswitches;
  1737. { set implementation flag }
  1738. current_module.in_interface:=false;
  1739. current_module.interface_compiled:=true;
  1740. { insert after the unit symbol tables the static symbol table }
  1741. { of the program }
  1742. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1743. { load standard units (system,objpas,profile unit) }
  1744. loaddefaultunits;
  1745. { Load units provided on the command line }
  1746. loadautounits;
  1747. {Load the units used by the program we compile.}
  1748. if token=_USES then
  1749. loadunits;
  1750. { reset ranges/stabs in exported definitions }
  1751. reset_all_defs;
  1752. { All units are read, now give them a number }
  1753. current_module.updatemaps;
  1754. {Insert the name of the main program into the symbol table.}
  1755. if current_module.realmodulename^<>'' then
  1756. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1757. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1758. symtablestack.push(current_module.localsymtable);
  1759. { create whole program optimisation information }
  1760. current_module.wpoinfo:=tunitwpoinfo.create;
  1761. { The program intialization needs an alias, so it can be called
  1762. from the bootstrap code.}
  1763. if islibrary then
  1764. begin
  1765. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1766. { Win32 startup code needs a single name }
  1767. if not(target_info.system in systems_darwin) then
  1768. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1769. else
  1770. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1771. end
  1772. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  1773. begin
  1774. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1775. end
  1776. else
  1777. begin
  1778. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1779. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1780. end;
  1781. main_procinfo.parse_body;
  1782. { save file pos for debuginfo }
  1783. current_module.mainfilepos:=main_procinfo.entrypos;
  1784. { Generate specializations of objectdefs methods }
  1785. generate_specialization_procs;
  1786. { should we force unit initialization? }
  1787. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1788. if force_init_final then
  1789. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1790. { Add symbol to the exports section for win32 so smartlinking a
  1791. DLL will include the edata section }
  1792. if assigned(exportlib) and
  1793. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1794. ((current_module.flags and uf_has_exports)<>0) then
  1795. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1796. { finalize? }
  1797. if token=_FINALIZATION then
  1798. begin
  1799. { set module options }
  1800. current_module.flags:=current_module.flags or uf_finalize;
  1801. { Parse the finalize }
  1802. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1803. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1804. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  1805. finalize_procinfo.parse_body;
  1806. end
  1807. else
  1808. if force_init_final then
  1809. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1810. { the finalization routine of libraries is generic (and all libraries need to }
  1811. { be finalized, so they can finalize any units they use }
  1812. if (islibrary) then
  1813. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  1814. { all labels must be defined before generating code }
  1815. if Errorcount=0 then
  1816. tstoredsymtable(current_module.localsymtable).checklabels;
  1817. { See remark in unit init/final }
  1818. main_procinfo.generate_code;
  1819. main_procinfo.resetprocdef;
  1820. release_main_proc(main_procinfo);
  1821. if assigned(init_procinfo) then
  1822. begin
  1823. init_procinfo.generate_code;
  1824. init_procinfo.resetprocdef;
  1825. release_main_proc(init_procinfo);
  1826. end;
  1827. if assigned(finalize_procinfo) then
  1828. begin
  1829. finalize_procinfo.generate_code;
  1830. finalize_procinfo.resetprocdef;
  1831. release_main_proc(finalize_procinfo);
  1832. end;
  1833. symtablestack.pop(current_module.localsymtable);
  1834. { consume the last point }
  1835. consume(_POINT);
  1836. if (Errorcount=0) then
  1837. begin
  1838. { test static symtable }
  1839. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1840. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1841. tstoredsymtable(current_module.localsymtable).check_forwards;
  1842. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1843. current_module.allunitsused;
  1844. end;
  1845. { leave when we got an error }
  1846. if (Errorcount>0) and not status.skip_error then
  1847. begin
  1848. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1849. status.skip_error:=true;
  1850. exit;
  1851. end;
  1852. { remove all unused units, this happens when units are removed
  1853. from the uses clause in the source and the ppu was already being loaded }
  1854. hp:=tmodule(loaded_units.first);
  1855. while assigned(hp) do
  1856. begin
  1857. hp2:=hp;
  1858. hp:=tmodule(hp.next);
  1859. if hp2.is_unit and
  1860. not assigned(hp2.globalsymtable) then
  1861. begin
  1862. loaded_units.remove(hp2);
  1863. unloaded_units.concat(hp2);
  1864. end;
  1865. end;
  1866. { do we need to add the variants unit? }
  1867. maybeloadvariantsunit;
  1868. { Now that everything has been compiled we know if we need resource
  1869. support. If not, remove the unit. }
  1870. resources_used:=MaybeRemoveResUnit;
  1871. linker.initsysinitunitname;
  1872. if target_info.system in system_internal_sysinit then
  1873. begin
  1874. { add start/halt unit }
  1875. AddUnit(linker.sysinitunit);
  1876. end;
  1877. {$ifdef arm}
  1878. { Insert .pdata section for arm-wince.
  1879. It is needed for exception handling. }
  1880. if target_info.system in [system_arm_wince] then
  1881. InsertPData;
  1882. {$endif arm}
  1883. InsertThreadvars;
  1884. { generate pic helpers to load eip if necessary }
  1885. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1886. { generate rtti/init tables }
  1887. write_persistent_type_info(current_module.localsymtable);
  1888. { generate wrappers for interfaces }
  1889. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1890. { generate imports }
  1891. if current_module.ImportLibraryList.Count>0 then
  1892. importlib.generatelib;
  1893. { generate debuginfo }
  1894. if (cs_debuginfo in current_settings.moduleswitches) then
  1895. current_debuginfo.inserttypeinfo;
  1896. if islibrary or (target_info.system in system_unit_program_exports) then
  1897. exportlib.generatelib;
  1898. { Reference all DEBUGINFO sections from the main .fpc section }
  1899. if (cs_debuginfo in current_settings.moduleswitches) then
  1900. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1901. { Resource strings }
  1902. GenerateResourceStrings;
  1903. { insert Tables and StackLength }
  1904. insertinitfinaltable;
  1905. InsertThreadvarTablesTable;
  1906. InsertResourceTablesTable;
  1907. insertmemorysizes;
  1908. { Insert symbol to resource info }
  1909. InsertResourceInfo(resources_used);
  1910. { create dwarf debuginfo }
  1911. create_dwarf;
  1912. { insert own objectfile }
  1913. insertobjectfile;
  1914. { assemble and link }
  1915. create_objectfile;
  1916. { We might need the symbols info if not using
  1917. the default do_extractsymbolinfo
  1918. which is a dummy function PM }
  1919. needsymbolinfo:=
  1920. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  1921. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  1922. { release all local symtables that are not needed anymore }
  1923. if (not needsymbolinfo) then
  1924. free_localsymtables(current_module.localsymtable);
  1925. { leave when we got an error }
  1926. if (Errorcount>0) and not status.skip_error then
  1927. begin
  1928. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1929. status.skip_error:=true;
  1930. exit;
  1931. end;
  1932. if (not current_module.is_unit) then
  1933. begin
  1934. { create the executable when we are at level 1 }
  1935. if (compile_level=1) then
  1936. begin
  1937. { create global resource file by collecting all resource files }
  1938. CollectResourceFiles;
  1939. { write .def file }
  1940. if (cs_link_deffile in current_settings.globalswitches) then
  1941. deffile.writefile;
  1942. { insert all .o files from all loaded units and
  1943. unload the units, we don't need them anymore.
  1944. Keep the current_module because that is still needed }
  1945. hp:=tmodule(loaded_units.first);
  1946. while assigned(hp) do
  1947. begin
  1948. linker.AddModuleFiles(hp);
  1949. hp2:=tmodule(hp.next);
  1950. if (hp<>current_module) and
  1951. (not needsymbolinfo) then
  1952. begin
  1953. loaded_units.remove(hp);
  1954. hp.free;
  1955. end;
  1956. hp:=hp2;
  1957. end;
  1958. { free also unneeded units we didn't free before }
  1959. if not needsymbolinfo then
  1960. unloaded_units.Clear;
  1961. { finally we can create a executable }
  1962. if DLLSource then
  1963. linker.MakeSharedLibrary
  1964. else
  1965. linker.MakeExecutable;
  1966. { collect all necessary information for whole-program optimization }
  1967. wpoinfomanager.extractwpoinfofromprogram;
  1968. end;
  1969. { Give Fatal with error count for linker errors }
  1970. if (Errorcount>0) and not status.skip_error then
  1971. begin
  1972. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1973. status.skip_error:=true;
  1974. end;
  1975. end;
  1976. end;
  1977. end.