pmodules.pas 83 KB

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