pmodules.pas 93 KB

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