pmodules.pas 80 KB

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