pmodules.pas 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. {
  2. Copyright (c) 1998-2008 by Florian Klaempfl
  3. Handles the parsing and loading of the modules (ppufiles)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit pmodules;
  18. {$i fpcdefs.inc}
  19. interface
  20. procedure proc_unit;
  21. procedure proc_package;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. SysUtils,
  26. globtype,version,systems,tokens,
  27. cutils,cfileutl,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,
  29. symconst,symbase,symtype,symdef,symsym,symtable,
  30. wpoinfo,
  31. aasmtai,aasmdata,aasmcpu,aasmbase,
  32. cgbase,cgobj,
  33. nbas,ncgutil,
  34. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  35. cresstr,procinfo,
  36. pexports,
  37. 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. {$IFDEF CPU32}
  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. if (paratargetdbg = dbg_stabs) then
  620. AddUnit('lineinfo')
  621. else
  622. AddUnit('lnfodwrf');
  623. end;
  624. { Valgrind requires c memory manager }
  625. if (cs_gdb_valgrind in current_settings.globalswitches) then
  626. AddUnit('cmem');
  627. {$ifdef cpufpemu}
  628. { Floating point emulation unit?
  629. softfpu must be in the system unit anyways (FK)
  630. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  631. AddUnit('softfpu');
  632. }
  633. {$endif cpufpemu}
  634. { Which kind of resource support?
  635. Note: if resources aren't used this unit will be removed later,
  636. otherwise we need it here since it must be loaded quite early }
  637. if (tf_has_winlike_resources in target_info.flags) then
  638. if target_res.id=res_ext then
  639. AddUnit('fpextres')
  640. else
  641. AddUnit('fpintres');
  642. end;
  643. { Objpas unit? }
  644. if m_objpas in current_settings.modeswitches then
  645. AddUnit('objpas');
  646. { Macpas unit? }
  647. if m_mac in current_settings.modeswitches then
  648. AddUnit('macpas');
  649. if m_iso in current_settings.modeswitches then
  650. AddUnit('iso7185');
  651. { Objective-C support unit? }
  652. if (m_objectivec1 in current_settings.modeswitches) then
  653. begin
  654. { interface to Objective-C run time }
  655. AddUnit('objc');
  656. loadobjctypes;
  657. { NSObject }
  658. if not(current_module.is_unit) or
  659. (current_module.modulename^<>'OBJCBASE') then
  660. AddUnit('objcbase');
  661. end;
  662. { Profile unit? Needed for go32v2 only }
  663. if (cs_profile in current_settings.moduleswitches) and
  664. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  665. AddUnit('profile');
  666. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  667. begin
  668. AddUnit('fpcylix');
  669. AddUnit('dynlibs');
  670. end;
  671. { CPU targets with microcontroller support can add a controller specific unit }
  672. {$if defined(ARM) or defined(AVR)}
  673. if (target_info.system in systems_embedded) and (current_settings.controllertype<>ct_none) and
  674. (embedded_controllers[current_settings.controllertype].controllerunitstr<>'') then
  675. AddUnit(embedded_controllers[current_settings.controllertype].controllerunitstr);
  676. {$endif ARM}
  677. end;
  678. procedure loadautounits;
  679. var
  680. hs,s : string;
  681. begin
  682. hs:=autoloadunits;
  683. repeat
  684. s:=GetToken(hs,',');
  685. if s='' then
  686. break;
  687. AddUnit(s);
  688. until false;
  689. end;
  690. procedure loadunits;
  691. var
  692. s,sorg : ansistring;
  693. fn : string;
  694. pu : tused_unit;
  695. hp2 : tmodule;
  696. unitsym : tunitsym;
  697. begin
  698. consume(_USES);
  699. repeat
  700. s:=pattern;
  701. sorg:=orgpattern;
  702. consume(_ID);
  703. while token=_POINT do
  704. begin
  705. consume(_POINT);
  706. s:=s+'.'+pattern;
  707. sorg:=sorg+'.'+orgpattern;
  708. consume(_ID);
  709. end;
  710. { support "<unit> in '<file>'" construct, but not for tp7 }
  711. fn:='';
  712. if not(m_tp7 in current_settings.modeswitches) and
  713. try_to_consume(_OP_IN) then
  714. fn:=FixFileName(get_stringconst);
  715. { Give a warning if lineinfo is loaded }
  716. if s='LINEINFO' then begin
  717. Message(parser_w_no_lineinfo_use_switch);
  718. if (paratargetdbg in [dbg_dwarf2, dbg_dwarf3]) then
  719. s := 'LNFODWRF';
  720. sorg := s;
  721. end;
  722. { Give a warning if objpas is loaded }
  723. if s='OBJPAS' then
  724. Message(parser_w_no_objpas_use_mode);
  725. { Using the unit itself is not possible }
  726. if (s<>current_module.modulename^) then
  727. begin
  728. { check if the unit is already used }
  729. hp2:=nil;
  730. pu:=tused_unit(current_module.used_units.first);
  731. while assigned(pu) do
  732. begin
  733. if (pu.u.modulename^=s) then
  734. begin
  735. hp2:=pu.u;
  736. break;
  737. end;
  738. pu:=tused_unit(pu.next);
  739. end;
  740. if not assigned(hp2) then
  741. hp2:=registerunit(current_module,sorg,fn)
  742. else
  743. Message1(sym_e_duplicate_id,s);
  744. { Create unitsym, we need to use the name as specified, we
  745. can not use the modulename because that can be different
  746. when -Un is used }
  747. unitsym:=tunitsym.create(sorg,nil);
  748. tabstractunitsymtable(current_module.localsymtable).insertunit(unitsym);
  749. { the current module uses the unit hp2 }
  750. current_module.addusedunit(hp2,true,unitsym);
  751. end
  752. else
  753. Message1(sym_e_duplicate_id,s);
  754. if token=_COMMA then
  755. begin
  756. pattern:='';
  757. consume(_COMMA);
  758. end
  759. else
  760. break;
  761. until false;
  762. { Load the units }
  763. pu:=tused_unit(current_module.used_units.first);
  764. while assigned(pu) do
  765. begin
  766. { Only load the units that are in the current
  767. (interface/implementation) uses clause }
  768. if pu.in_uses and
  769. (pu.in_interface=current_module.in_interface) then
  770. begin
  771. tppumodule(pu.u).loadppu;
  772. { is our module compiled? then we can stop }
  773. if current_module.state=ms_compiled then
  774. exit;
  775. { add this unit to the dependencies }
  776. pu.u.adddependency(current_module);
  777. { save crc values }
  778. pu.checksum:=pu.u.crc;
  779. pu.interface_checksum:=pu.u.interface_crc;
  780. pu.indirect_checksum:=pu.u.indirect_crc;
  781. { connect unitsym to the module }
  782. pu.unitsym.module:=pu.u;
  783. { add to symtable stack }
  784. symtablestack.push(pu.u.globalsymtable);
  785. if (m_mac in current_settings.modeswitches) and
  786. assigned(pu.u.globalmacrosymtable) then
  787. macrosymtablestack.push(pu.u.globalmacrosymtable);
  788. { check hints }
  789. pu.u.check_hints;
  790. end;
  791. pu:=tused_unit(pu.next);
  792. end;
  793. consume(_SEMICOLON);
  794. end;
  795. procedure reset_all_defs;
  796. begin
  797. if assigned(current_module.wpoinfo) then
  798. current_module.wpoinfo.resetdefs;
  799. end;
  800. procedure free_localsymtables(st:TSymtable);
  801. var
  802. i : longint;
  803. def : tstoreddef;
  804. pd : tprocdef;
  805. begin
  806. for i:=0 to st.DefList.Count-1 do
  807. begin
  808. def:=tstoreddef(st.DefList[i]);
  809. if def.typ=procdef then
  810. begin
  811. pd:=tprocdef(def);
  812. if assigned(pd.localst) and
  813. (pd.localst.symtabletype<>staticsymtable) and
  814. not(po_inline in pd.procoptions) then
  815. begin
  816. free_localsymtables(pd.localst);
  817. pd.localst.free;
  818. pd.localst:=nil;
  819. end;
  820. end;
  821. end;
  822. end;
  823. procedure parse_implementation_uses;
  824. begin
  825. if token=_USES then
  826. loadunits;
  827. end;
  828. procedure setupglobalswitches;
  829. begin
  830. if (cs_create_pic in current_settings.moduleswitches) then
  831. begin
  832. def_system_macro('FPC_PIC');
  833. def_system_macro('PIC');
  834. end;
  835. end;
  836. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  837. var
  838. ps : tprocsym;
  839. pd : tprocdef;
  840. begin
  841. { there should be no current_procinfo available }
  842. if assigned(current_procinfo) then
  843. internalerror(200304275);
  844. {Generate a procsym for main}
  845. ps:=tprocsym.create('$'+name);
  846. { main are allways used }
  847. inc(ps.refs);
  848. st.insert(ps);
  849. pd:=tprocdef.create(main_program_level);
  850. include(pd.procoptions,po_global);
  851. pd.procsym:=ps;
  852. ps.ProcdefList.Add(pd);
  853. { set procdef options }
  854. pd.proctypeoption:=potype;
  855. pd.proccalloption:=pocall_default;
  856. include(pd.procoptions,po_hascallingconvention);
  857. pd.forwarddef:=false;
  858. pd.setmangledname(target_info.cprefix+name);
  859. pd.aliasnames.insert(pd.mangledname);
  860. handle_calling_convention(pd);
  861. { We don't need is a local symtable. Change it into the static
  862. symtable }
  863. pd.localst.free;
  864. pd.localst:=st;
  865. { set procinfo and current_procinfo.procdef }
  866. result:=tcgprocinfo(cprocinfo.create(nil));
  867. result.procdef:=pd;
  868. { main proc does always a call e.g. to init system unit }
  869. include(result.flags,pi_do_call);
  870. end;
  871. procedure release_main_proc(pi:tcgprocinfo);
  872. begin
  873. { remove localst as it was replaced by staticsymtable }
  874. pi.procdef.localst:=nil;
  875. { remove procinfo }
  876. current_module.procinfo:=nil;
  877. pi.free;
  878. pi:=nil;
  879. end;
  880. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  881. procedure maybe_load_got;
  882. {$ifdef i386}
  883. var
  884. gotvarsym : tstaticvarsym;
  885. {$endif i386}
  886. begin
  887. {$ifdef i386}
  888. if (cs_create_pic in current_settings.moduleswitches) and
  889. (tf_pic_uses_got in target_info.flags) then
  890. begin
  891. { insert symbol for got access in assembler code}
  892. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',
  893. vs_value,voidpointertype,[vo_is_external]);
  894. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  895. current_module.localsymtable.insert(gotvarsym);
  896. { avoid unnecessary warnings }
  897. gotvarsym.varstate:=vs_read;
  898. gotvarsym.refs:=1;
  899. end;
  900. {$endif i386}
  901. end;
  902. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  903. begin
  904. { update module flags }
  905. current_module.flags:=current_module.flags or flag;
  906. { create procdef }
  907. case flag of
  908. uf_init :
  909. begin
  910. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  911. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  912. end;
  913. uf_finalize :
  914. begin
  915. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  916. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  917. if (not current_module.is_unit) then
  918. result.procdef.aliasnames.insert('PASCALFINALIZE');
  919. end;
  920. else
  921. internalerror(200304253);
  922. end;
  923. result.code:=cnothingnode.create;
  924. end;
  925. procedure copy_macro(p:TObject; arg:pointer);
  926. begin
  927. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  928. end;
  929. function try_consume_hintdirective(var moduleopt:tmoduleoptions; var deprecatedmsg:pshortstring):boolean;
  930. var
  931. last_is_deprecated:boolean;
  932. begin
  933. try_consume_hintdirective:=false;
  934. repeat
  935. last_is_deprecated:=false;
  936. case idtoken of
  937. _LIBRARY :
  938. begin
  939. include(moduleopt,mo_hint_library);
  940. try_consume_hintdirective:=true;
  941. end;
  942. _DEPRECATED :
  943. begin
  944. include(moduleopt,mo_hint_deprecated);
  945. try_consume_hintdirective:=true;
  946. last_is_deprecated:=true;
  947. end;
  948. _EXPERIMENTAL :
  949. begin
  950. include(moduleopt,mo_hint_experimental);
  951. try_consume_hintdirective:=true;
  952. end;
  953. _PLATFORM :
  954. begin
  955. include(moduleopt,mo_hint_platform);
  956. try_consume_hintdirective:=true;
  957. end;
  958. _UNIMPLEMENTED :
  959. begin
  960. include(moduleopt,mo_hint_unimplemented);
  961. try_consume_hintdirective:=true;
  962. end;
  963. else
  964. break;
  965. end;
  966. consume(Token);
  967. { handle deprecated message }
  968. if ((token=_CSTRING) or (token=_CCHAR)) and last_is_deprecated then
  969. begin
  970. if deprecatedmsg<>nil then
  971. internalerror(201001221);
  972. if token=_CSTRING then
  973. deprecatedmsg:=stringdup(cstringpattern)
  974. else
  975. deprecatedmsg:=stringdup(pattern);
  976. consume(token);
  977. include(moduleopt,mo_has_deprecated_msg);
  978. end;
  979. until false;
  980. end;
  981. procedure proc_unit;
  982. function is_assembler_generated:boolean;
  983. var
  984. hal : tasmlisttype;
  985. begin
  986. result:=false;
  987. if Errorcount=0 then
  988. begin
  989. for hal:=low(TasmlistType) to high(TasmlistType) do
  990. if not current_asmdata.asmlists[hal].empty then
  991. begin
  992. result:=true;
  993. exit;
  994. end;
  995. end;
  996. end;
  997. var
  998. main_file: tinputfile;
  999. {$ifdef EXTDEBUG}
  1000. store_crc,
  1001. {$endif EXTDEBUG}
  1002. store_interface_crc,
  1003. store_indirect_crc: cardinal;
  1004. s1,s2 : ^string; {Saves stack space}
  1005. force_init_final : boolean;
  1006. init_procinfo,
  1007. finalize_procinfo : tcgprocinfo;
  1008. unitname : ansistring;
  1009. unitname8 : string[8];
  1010. ag: boolean;
  1011. {$ifdef debug_devirt}
  1012. i: longint;
  1013. {$endif debug_devirt}
  1014. begin
  1015. init_procinfo:=nil;
  1016. finalize_procinfo:=nil;
  1017. if m_mac in current_settings.modeswitches then
  1018. current_module.mode_switch_allowed:= false;
  1019. consume(_UNIT);
  1020. if compile_level=1 then
  1021. Status.IsExe:=false;
  1022. unitname:=orgpattern;
  1023. consume(_ID);
  1024. while token=_POINT do
  1025. begin
  1026. consume(_POINT);
  1027. unitname:=unitname+'.'+orgpattern;
  1028. consume(_ID);
  1029. end;
  1030. { create filenames and unit name }
  1031. main_file := current_scanner.inputfile;
  1032. while assigned(main_file.next) do
  1033. main_file := main_file.next;
  1034. new(s1);
  1035. s1^:=current_module.modulename^;
  1036. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1037. current_module.SetModuleName(unitname);
  1038. { check for system unit }
  1039. new(s2);
  1040. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  1041. unitname8:=copy(current_module.modulename^,1,8);
  1042. if (cs_check_unit_name in current_settings.globalswitches) and
  1043. (
  1044. not(
  1045. (current_module.modulename^=s2^) or
  1046. (
  1047. (length(current_module.modulename^)>8) and
  1048. (unitname8=s2^)
  1049. )
  1050. )
  1051. or
  1052. (
  1053. (length(s1^)>8) and
  1054. (s1^<>current_module.modulename^)
  1055. )
  1056. ) then
  1057. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  1058. if (current_module.modulename^='SYSTEM') then
  1059. include(current_settings.moduleswitches,cs_compilesystem);
  1060. dispose(s2);
  1061. dispose(s1);
  1062. if (target_info.system in systems_unit_program_exports) then
  1063. exportlib.preparelib(current_module.realmodulename^);
  1064. { parse hint directives }
  1065. try_consume_hintdirective(current_module.moduleoptions, current_module.deprecatedmsg);
  1066. consume(_SEMICOLON);
  1067. consume(_INTERFACE);
  1068. { global switches are read, so further changes aren't allowed }
  1069. current_module.in_global:=false;
  1070. { handle the global switches }
  1071. setupglobalswitches;
  1072. message1(unit_u_loading_interface_units,current_module.modulename^);
  1073. { update status }
  1074. status.currentmodule:=current_module.realmodulename^;
  1075. { maybe turn off m_objpas if we are compiling objpas }
  1076. if (current_module.modulename^='OBJPAS') then
  1077. exclude(current_settings.modeswitches,m_objpas);
  1078. { maybe turn off m_mac if we are compiling macpas }
  1079. if (current_module.modulename^='MACPAS') then
  1080. exclude(current_settings.modeswitches,m_mac);
  1081. parse_only:=true;
  1082. { generate now the global symboltable,
  1083. define first as local to overcome dependency conflicts }
  1084. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  1085. { insert unitsym of this unit to prevent other units having
  1086. the same name }
  1087. tabstractunitsymtable(current_module.localsymtable).insertunit(tunitsym.create(current_module.realmodulename^,current_module));
  1088. { load default units, like the system unit }
  1089. loaddefaultunits;
  1090. { insert qualifier for the system unit (allows system.writeln) }
  1091. if not(cs_compilesystem in current_settings.moduleswitches) and
  1092. (token=_USES) then
  1093. begin
  1094. loadunits;
  1095. { has it been compiled at a higher level ?}
  1096. if current_module.state=ms_compiled then
  1097. exit;
  1098. end;
  1099. { move the global symtable from the temporary local to global }
  1100. current_module.globalsymtable:=current_module.localsymtable;
  1101. current_module.localsymtable:=nil;
  1102. { number all units, so we know if a unit is used by this unit or
  1103. needs to be added implicitly }
  1104. current_module.updatemaps;
  1105. { create whole program optimisation information (may already be
  1106. updated in the interface, e.g., in case of classrefdef typed
  1107. constants }
  1108. current_module.wpoinfo:=tunitwpoinfo.create;
  1109. { ... parse the declarations }
  1110. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1111. symtablestack.push(current_module.globalsymtable);
  1112. read_interface_declarations;
  1113. symtablestack.pop(current_module.globalsymtable);
  1114. { Export macros defined in the interface for macpas. The macros
  1115. are put in the globalmacrosymtable that will only be used by other
  1116. units. The current unit continues to use the localmacrosymtable }
  1117. if (m_mac in current_settings.modeswitches) then
  1118. begin
  1119. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  1120. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  1121. end;
  1122. { leave when we got an error }
  1123. if (Errorcount>0) and not status.skip_error then
  1124. begin
  1125. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1126. status.skip_error:=true;
  1127. exit;
  1128. end;
  1129. { Our interface is compiled, generate CRC and switch to implementation }
  1130. if not(cs_compilesystem in current_settings.moduleswitches) and
  1131. (Errorcount=0) then
  1132. tppumodule(current_module).getppucrc;
  1133. current_module.in_interface:=false;
  1134. current_module.interface_compiled:=true;
  1135. { First reload all units depending on our interface, we need to do this
  1136. in the implementation part to prevent erroneous circular references }
  1137. tppumodule(current_module).setdefgeneration;
  1138. tppumodule(current_module).reload_flagged_units;
  1139. { Parse the implementation section }
  1140. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  1141. current_module.interface_only:=true
  1142. else
  1143. current_module.interface_only:=false;
  1144. parse_only:=false;
  1145. { create static symbol table }
  1146. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1147. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1148. maybe_load_got;
  1149. if not current_module.interface_only then
  1150. begin
  1151. consume(_IMPLEMENTATION);
  1152. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  1153. { Read the implementation units }
  1154. parse_implementation_uses;
  1155. end;
  1156. if current_module.state=ms_compiled then
  1157. exit;
  1158. { All units are read, now give them a number }
  1159. current_module.updatemaps;
  1160. symtablestack.push(current_module.globalsymtable);
  1161. symtablestack.push(current_module.localsymtable);
  1162. if not current_module.interface_only then
  1163. begin
  1164. Message1(parser_u_parsing_implementation,current_module.modulename^);
  1165. if current_module.in_interface then
  1166. internalerror(200212285);
  1167. { Compile the unit }
  1168. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  1169. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  1170. init_procinfo.parse_body;
  1171. { save file pos for debuginfo }
  1172. current_module.mainfilepos:=init_procinfo.entrypos;
  1173. end;
  1174. { Generate specializations of objectdefs methods }
  1175. generate_specialization_procs;
  1176. { if the unit contains ansi/widestrings, initialization and
  1177. finalization code must be forced }
  1178. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  1179. tstaticsymtable(current_module.localsymtable).needs_init_final;
  1180. { should we force unit initialization? }
  1181. { this is a hack, but how can it be done better ? }
  1182. if force_init_final and ((current_module.flags and uf_init)=0) then
  1183. begin
  1184. { first release the not used init procinfo }
  1185. if assigned(init_procinfo) then
  1186. release_main_proc(init_procinfo);
  1187. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1188. end;
  1189. { finalize? }
  1190. if not current_module.interface_only and (token=_FINALIZATION) then
  1191. begin
  1192. { the uf_finalize flag is only set after we checked that it
  1193. wasn't empty }
  1194. { Compile the finalize }
  1195. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1196. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1197. finalize_procinfo.parse_body;
  1198. end
  1199. else if force_init_final then
  1200. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1201. { Now both init and finalize bodies are read and it is known
  1202. which variables are used in both init and finalize we can now
  1203. generate the code. This is required to prevent putting a variable in
  1204. a register that is also used in the finalize body (PFV) }
  1205. if assigned(init_procinfo) then
  1206. begin
  1207. init_procinfo.generate_code;
  1208. init_procinfo.resetprocdef;
  1209. release_main_proc(init_procinfo);
  1210. end;
  1211. if assigned(finalize_procinfo) then
  1212. begin
  1213. finalize_procinfo.generate_code;
  1214. finalize_procinfo.resetprocdef;
  1215. release_main_proc(finalize_procinfo);
  1216. end;
  1217. symtablestack.pop(current_module.localsymtable);
  1218. symtablestack.pop(current_module.globalsymtable);
  1219. { the last char should always be a point }
  1220. consume(_POINT);
  1221. { reset wpo flags for all defs }
  1222. reset_all_defs;
  1223. if (Errorcount=0) then
  1224. begin
  1225. { tests, if all (interface) forwards are resolved }
  1226. tstoredsymtable(current_module.globalsymtable).check_forwards;
  1227. { check if all private fields are used }
  1228. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  1229. { test static symtable }
  1230. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1231. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1232. tstoredsymtable(current_module.localsymtable).check_forwards;
  1233. tstoredsymtable(current_module.localsymtable).checklabels;
  1234. { used units }
  1235. current_module.allunitsused;
  1236. end;
  1237. { leave when we got an error }
  1238. if (Errorcount>0) and not status.skip_error then
  1239. begin
  1240. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1241. status.skip_error:=true;
  1242. exit;
  1243. end;
  1244. { if an Objective-C module, generate rtti and module info }
  1245. MaybeGenerateObjectiveCImageInfo(current_module.globalsymtable,current_module.localsymtable);
  1246. { do we need to add the variants unit? }
  1247. maybeloadvariantsunit;
  1248. { generate wrappers for interfaces }
  1249. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable,false);
  1250. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  1251. { generate rtti/init tables }
  1252. write_persistent_type_info(current_module.globalsymtable);
  1253. write_persistent_type_info(current_module.localsymtable);
  1254. { Tables }
  1255. InsertThreadvars;
  1256. { Resource strings }
  1257. GenerateResourceStrings;
  1258. { Widestring typed constants }
  1259. InsertWideInits;
  1260. { Resourcestring references }
  1261. InsertResStrInits;
  1262. { generate debuginfo }
  1263. if (cs_debuginfo in current_settings.moduleswitches) then
  1264. current_debuginfo.inserttypeinfo;
  1265. { generate imports }
  1266. if current_module.ImportLibraryList.Count>0 then
  1267. importlib.generatelib;
  1268. { insert own objectfile, or say that it's in a library
  1269. (no check for an .o when loading) }
  1270. ag:=is_assembler_generated;
  1271. if ag then
  1272. insertobjectfile
  1273. else
  1274. begin
  1275. current_module.flags:=current_module.flags or uf_no_link;
  1276. current_module.flags:=current_module.flags and not (uf_has_stabs_debuginfo or uf_has_dwarf_debuginfo);
  1277. end;
  1278. if ag then
  1279. begin
  1280. { create callframe info }
  1281. create_dwarf_frame;
  1282. { assemble }
  1283. create_objectfile;
  1284. end;
  1285. { Write out the ppufile after the object file has been created }
  1286. store_interface_crc:=current_module.interface_crc;
  1287. store_indirect_crc:=current_module.indirect_crc;
  1288. {$ifdef EXTDEBUG}
  1289. store_crc:=current_module.crc;
  1290. {$endif EXTDEBUG}
  1291. if (Errorcount=0) then
  1292. tppumodule(current_module).writeppu;
  1293. if not(cs_compilesystem in current_settings.moduleswitches) then
  1294. begin
  1295. if store_interface_crc<>current_module.interface_crc then
  1296. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1297. if store_indirect_crc<>current_module.indirect_crc then
  1298. Message1(unit_u_indirect_crc_changed,current_module.ppufilename^);
  1299. end;
  1300. {$ifdef EXTDEBUG}
  1301. if not(cs_compilesystem in current_settings.moduleswitches) then
  1302. if (store_crc<>current_module.crc) and simplify_ppu then
  1303. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1304. {$endif EXTDEBUG}
  1305. { release local symtables that are not needed anymore }
  1306. free_localsymtables(current_module.globalsymtable);
  1307. free_localsymtables(current_module.localsymtable);
  1308. { leave when we got an error }
  1309. if (Errorcount>0) and not status.skip_error then
  1310. begin
  1311. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1312. status.skip_error:=true;
  1313. exit;
  1314. end;
  1315. {$ifdef debug_devirt}
  1316. { print out all instantiated class/object types }
  1317. writeln('constructed object/class/classreftypes in ',current_module.realmodulename^);
  1318. for i := 0 to current_module.wpoinfo.createdobjtypes.count-1 do
  1319. begin
  1320. write(' ',tdef(current_module.wpoinfo.createdobjtypes[i]).GetTypeName);
  1321. case tdef(current_module.wpoinfo.createdobjtypes[i]).typ of
  1322. objectdef:
  1323. case tobjectdef(current_module.wpoinfo.createdobjtypes[i]).objecttype of
  1324. odt_object:
  1325. writeln(' (object)');
  1326. odt_class:
  1327. writeln(' (class)');
  1328. else
  1329. internalerror(2008101103);
  1330. end;
  1331. else
  1332. internalerror(2008101104);
  1333. end;
  1334. end;
  1335. for i := 0 to current_module.wpoinfo.createdclassrefobjtypes.count-1 do
  1336. begin
  1337. write(' Class Of ',tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).GetTypeName);
  1338. case tdef(current_module.wpoinfo.createdclassrefobjtypes[i]).typ of
  1339. objectdef:
  1340. case tobjectdef(current_module.wpoinfo.createdclassrefobjtypes[i]).objecttype of
  1341. odt_class:
  1342. writeln(' (classrefdef)');
  1343. else
  1344. internalerror(2008101105);
  1345. end
  1346. else
  1347. internalerror(2008101102);
  1348. end;
  1349. end;
  1350. {$endif debug_devirt}
  1351. Message1(unit_u_finished_compiling,current_module.modulename^);
  1352. end;
  1353. procedure procexport(const s : string);
  1354. var
  1355. hp : texported_item;
  1356. begin
  1357. hp:=texported_item.create;
  1358. hp.name:=stringdup(s);
  1359. hp.options:=hp.options or eo_name;
  1360. exportlib.exportprocedure(hp);
  1361. end;
  1362. procedure varexport(const s : string);
  1363. var
  1364. hp : texported_item;
  1365. begin
  1366. hp:=texported_item.create;
  1367. hp.name:=stringdup(s);
  1368. hp.options:=hp.options or eo_name;
  1369. exportlib.exportvar(hp);
  1370. end;
  1371. procedure insert_export(sym : TObject;arg:pointer);
  1372. var
  1373. i : longint;
  1374. item : TCmdStrListItem;
  1375. begin
  1376. case TSym(sym).typ of
  1377. { ignore: }
  1378. unitsym,
  1379. syssym,
  1380. constsym,
  1381. enumsym,
  1382. typesym:
  1383. ;
  1384. procsym:
  1385. begin
  1386. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1387. begin
  1388. if not(tprocdef(tprocsym(sym).ProcdefList[i]).proccalloption in [pocall_internproc]) and
  1389. ((tprocdef(tprocsym(sym).ProcdefList[i]).procoptions*[po_external])=[]) and
  1390. ((tsymtable(arg).symtabletype=globalsymtable) or
  1391. ((tsymtable(arg).symtabletype=staticsymtable) and (po_public in tprocdef(tprocsym(sym).ProcdefList[i]).procoptions))
  1392. ) then
  1393. begin
  1394. procexport(tprocdef(tprocsym(sym).ProcdefList[i]).mangledname);
  1395. { walk through all aliases }
  1396. item:=TCmdStrListItem(tprocdef(tprocsym(sym).ProcdefList[i]).aliasnames.first);
  1397. while assigned(item) do
  1398. begin
  1399. { avoid duplicate entries, sometimes aliasnames contains the mangledname }
  1400. if item.str<>tprocdef(tprocsym(sym).ProcdefList[i]).mangledname then
  1401. procexport(item.str);
  1402. item:=TCmdStrListItem(item.next);
  1403. end;
  1404. end;
  1405. end;
  1406. end;
  1407. staticvarsym:
  1408. begin
  1409. varexport(tsym(sym).mangledname);
  1410. end;
  1411. else
  1412. begin
  1413. writeln('unknown: ',ord(TSym(sym).typ));
  1414. end;
  1415. end;
  1416. end;
  1417. Function RewritePPU(const PPUFn,PPLFn:String):Boolean;
  1418. Var
  1419. MakeStatic : Boolean;
  1420. Var
  1421. buffer : array[0..$1fff] of byte;
  1422. inppu,
  1423. outppu : tppufile;
  1424. b,
  1425. untilb : byte;
  1426. l,m : longint;
  1427. f : file;
  1428. ext,
  1429. s : string;
  1430. ppuversion : dword;
  1431. begin
  1432. Result:=false;
  1433. MakeStatic:=False;
  1434. inppu:=tppufile.create(PPUFn);
  1435. if not inppu.openfile then
  1436. begin
  1437. inppu.free;
  1438. Comment(V_Error,'Could not open : '+PPUFn);
  1439. Exit;
  1440. end;
  1441. { Check the ppufile }
  1442. if not inppu.CheckPPUId then
  1443. begin
  1444. inppu.free;
  1445. Comment(V_Error,'Not a PPU File : '+PPUFn);
  1446. Exit;
  1447. end;
  1448. ppuversion:=inppu.GetPPUVersion;
  1449. if ppuversion<CurrentPPUVersion then
  1450. begin
  1451. inppu.free;
  1452. Comment(V_Error,'Wrong PPU Version '+tostr(ppuversion)+' in '+PPUFn);
  1453. Exit;
  1454. end;
  1455. { No .o file generated for this ppu, just skip }
  1456. if (inppu.header.flags and uf_no_link)<>0 then
  1457. begin
  1458. inppu.free;
  1459. Result:=true;
  1460. Exit;
  1461. end;
  1462. { Already a lib? }
  1463. if (inppu.header.flags and uf_in_library)<>0 then
  1464. begin
  1465. inppu.free;
  1466. Comment(V_Error,'PPU is already in a library : '+PPUFn);
  1467. Exit;
  1468. end;
  1469. { We need a static linked unit }
  1470. if (inppu.header.flags and uf_static_linked)=0 then
  1471. begin
  1472. inppu.free;
  1473. Comment(V_Error,'PPU is not static linked : '+PPUFn);
  1474. Exit;
  1475. end;
  1476. { Check if shared is allowed }
  1477. if tsystem(inppu.header.target) in [system_i386_go32v2] then
  1478. begin
  1479. Comment(V_Error,'Shared library not supported for ppu target, switching to static library');
  1480. MakeStatic:=true;
  1481. end;
  1482. { Create the new ppu }
  1483. if PPUFn=PPLFn then
  1484. outppu:=tppufile.create('ppumove.$$$')
  1485. else
  1486. outppu:=tppufile.create(PPLFn);
  1487. outppu.createfile;
  1488. { Create new header, with the new flags }
  1489. outppu.header:=inppu.header;
  1490. outppu.header.flags:=outppu.header.flags or uf_in_library;
  1491. if MakeStatic then
  1492. outppu.header.flags:=outppu.header.flags or uf_static_linked
  1493. else
  1494. outppu.header.flags:=outppu.header.flags or uf_shared_linked;
  1495. { read until the object files are found }
  1496. untilb:=iblinkunitofiles;
  1497. repeat
  1498. b:=inppu.readentry;
  1499. if b in [ibendinterface,ibend] then
  1500. begin
  1501. inppu.free;
  1502. outppu.free;
  1503. Comment(V_Error,'No files to be linked found : '+PPUFn);
  1504. Exit;
  1505. end;
  1506. if b<>untilb then
  1507. begin
  1508. repeat
  1509. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1510. outppu.putdata(buffer,l);
  1511. until l<sizeof(buffer);
  1512. outppu.writeentry(b);
  1513. end;
  1514. until (b=untilb);
  1515. { we have now reached the section for the files which need to be added,
  1516. now add them to the list }
  1517. case b of
  1518. iblinkunitofiles :
  1519. begin
  1520. { add all o files, and save the entry when not creating a static
  1521. library to keep staticlinking possible }
  1522. while not inppu.endofentry do
  1523. begin
  1524. s:=inppu.getstring;
  1525. m:=inppu.getlongint;
  1526. if not MakeStatic then
  1527. begin
  1528. outppu.putstring(s);
  1529. outppu.putlongint(m);
  1530. end;
  1531. current_module.linkotherofiles.add(s,link_always);;
  1532. end;
  1533. if not MakeStatic then
  1534. outppu.writeentry(b);
  1535. end;
  1536. { iblinkunitstaticlibs :
  1537. begin
  1538. AddToLinkFiles(ExtractLib(inppu.getstring));
  1539. if not inppu.endofentry then
  1540. begin
  1541. repeat
  1542. inppu.getdatabuf(buffer^,bufsize,l);
  1543. outppu.putdata(buffer^,l);
  1544. until l<bufsize;
  1545. outppu.writeentry(b);
  1546. end;
  1547. end; }
  1548. end;
  1549. { just add a new entry with the new lib }
  1550. if MakeStatic then
  1551. begin
  1552. outppu.putstring('imp'+current_module.realmodulename^);
  1553. outppu.putlongint(link_static);
  1554. outppu.writeentry(iblinkunitstaticlibs)
  1555. end
  1556. else
  1557. begin
  1558. outppu.putstring('imp'+current_module.realmodulename^);
  1559. outppu.putlongint(link_shared);
  1560. outppu.writeentry(iblinkunitsharedlibs);
  1561. end;
  1562. { read all entries until the end and write them also to the new ppu }
  1563. repeat
  1564. b:=inppu.readentry;
  1565. { don't write ibend, that's written automatically }
  1566. if b<>ibend then
  1567. begin
  1568. if b=iblinkothersharedlibs then
  1569. begin
  1570. while not inppu.endofentry do
  1571. begin
  1572. s:=inppu.getstring;
  1573. m:=inppu.getlongint;
  1574. outppu.putstring(s);
  1575. outppu.putlongint(m);
  1576. { strip lib prefix }
  1577. if copy(s,1,3)='lib' then
  1578. delete(s,1,3);
  1579. ext:=ExtractFileExt(s);
  1580. if ext<>'' then
  1581. delete(s,length(s)-length(ext)+1,length(ext));
  1582. current_module.linkOtherSharedLibs.add(s,link_always);
  1583. end;
  1584. end
  1585. else
  1586. repeat
  1587. inppu.getdatabuf(buffer,sizeof(buffer),l);
  1588. outppu.putdata(buffer,l);
  1589. until l<sizeof(buffer);
  1590. outppu.writeentry(b);
  1591. end;
  1592. until b=ibend;
  1593. { write the last stuff and close }
  1594. outppu.flush;
  1595. outppu.writeheader;
  1596. outppu.free;
  1597. inppu.free;
  1598. { rename }
  1599. if PPUFn=PPLFn then
  1600. begin
  1601. {$I-}
  1602. assign(f,PPUFn);
  1603. erase(f);
  1604. assign(f,'ppumove.$$$');
  1605. rename(f,PPUFn);
  1606. {$I+}
  1607. if ioresult<>0 then;
  1608. end;
  1609. Result:=True;
  1610. end;
  1611. procedure createimportlibfromexports;
  1612. var
  1613. hp : texported_item;
  1614. begin
  1615. hp:=texported_item(current_module._exports.first);
  1616. while assigned(hp) do
  1617. begin
  1618. current_module.AddExternalImport(current_module.realmodulename^,hp.name^,hp.name^,hp.index,hp.is_var,false);
  1619. hp:=texported_item(hp.next);
  1620. end;
  1621. end;
  1622. procedure proc_package;
  1623. var
  1624. main_file : tinputfile;
  1625. hp,hp2 : tmodule;
  1626. {finalize_procinfo,
  1627. init_procinfo,
  1628. main_procinfo : tcgprocinfo;}
  1629. force_init_final : boolean;
  1630. uu : tused_unit;
  1631. module_name: ansistring;
  1632. begin
  1633. Status.IsPackage:=true;
  1634. Status.IsExe:=true;
  1635. parse_only:=false;
  1636. {main_procinfo:=nil;
  1637. init_procinfo:=nil;
  1638. finalize_procinfo:=nil;}
  1639. if not RelocSectionSetExplicitly then
  1640. RelocSection:=true;
  1641. { Relocation works only without stabs under Windows when }
  1642. { external linker (LD) is used. LD generates relocs for }
  1643. { stab sections which is not loaded in memory. It causes }
  1644. { AV error when DLL is loaded and relocation is needed. }
  1645. { Internal linker does not have this problem. }
  1646. if RelocSection and
  1647. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1648. (cs_link_extern in current_settings.globalswitches) then
  1649. begin
  1650. include(current_settings.globalswitches,cs_link_strip);
  1651. { Warning stabs info does not work with reloc section !! }
  1652. if (cs_debuginfo in current_settings.moduleswitches) and
  1653. (target_dbg.id=dbg_stabs) then
  1654. begin
  1655. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1656. Message(parser_w_parser_win32_debug_needs_WN);
  1657. exclude(current_settings.moduleswitches,cs_debuginfo);
  1658. end;
  1659. end;
  1660. { get correct output names }
  1661. main_file := current_scanner.inputfile;
  1662. while assigned(main_file.next) do
  1663. main_file := main_file.next;
  1664. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1665. { consume _PACKAGE word }
  1666. consume(_ID);
  1667. module_name:=orgpattern;
  1668. consume(_ID);
  1669. while token=_POINT do
  1670. begin
  1671. consume(_POINT);
  1672. module_name:=module_name+'.'+orgpattern;
  1673. consume(_ID);
  1674. end;
  1675. current_module.setmodulename(module_name);
  1676. current_module.ispackage:=true;
  1677. exportlib.preparelib(module_name);
  1678. if tf_library_needs_pic in target_info.flags then
  1679. include(current_settings.moduleswitches,cs_create_pic);
  1680. consume(_SEMICOLON);
  1681. { global switches are read, so further changes aren't allowed }
  1682. current_module.in_global:=false;
  1683. { setup things using the switches }
  1684. setupglobalswitches;
  1685. { set implementation flag }
  1686. current_module.in_interface:=false;
  1687. current_module.interface_compiled:=true;
  1688. { insert after the unit symbol tables the static symbol table }
  1689. { of the program }
  1690. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1691. {Load the units used by the program we compile.}
  1692. if token=_REQUIRES then
  1693. begin
  1694. end;
  1695. {Load the units used by the program we compile.}
  1696. if (token=_ID) and (idtoken=_CONTAINS) then
  1697. begin
  1698. { consume _CONTAINS word }
  1699. consume(_ID);
  1700. while true do
  1701. begin
  1702. if token=_ID then
  1703. begin
  1704. module_name:=pattern;
  1705. consume(_ID);
  1706. while token=_POINT do
  1707. begin
  1708. consume(_POINT);
  1709. module_name:=module_name+'.'+orgpattern;
  1710. consume(_ID);
  1711. end;
  1712. AddUnit(module_name);
  1713. end
  1714. else
  1715. consume(_ID);
  1716. if token=_COMMA then
  1717. consume(_COMMA)
  1718. else break;
  1719. end;
  1720. consume(_SEMICOLON);
  1721. end;
  1722. { All units are read, now give them a number }
  1723. current_module.updatemaps;
  1724. {Insert the name of the main program into the symbol table.}
  1725. if current_module.realmodulename^<>'' then
  1726. tabstractunitsymtable(current_module.localsymtable).insertunit(tunitsym.create(current_module.realmodulename^,current_module));
  1727. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1728. symtablestack.push(current_module.localsymtable);
  1729. { create whole program optimisation information }
  1730. current_module.wpoinfo:=tunitwpoinfo.create;
  1731. { should we force unit initialization? }
  1732. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1733. if force_init_final then
  1734. {init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable)};
  1735. { Add symbol to the exports section for win32 so smartlinking a
  1736. DLL will include the edata section }
  1737. if assigned(exportlib) and
  1738. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1739. ((current_module.flags and uf_has_exports)<>0) then
  1740. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1741. { all labels must be defined before generating code }
  1742. if Errorcount=0 then
  1743. tstoredsymtable(current_module.localsymtable).checklabels;
  1744. symtablestack.pop(current_module.localsymtable);
  1745. { consume the last point }
  1746. consume(_END);
  1747. consume(_POINT);
  1748. if (Errorcount=0) then
  1749. begin
  1750. { test static symtable }
  1751. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1752. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1753. tstoredsymtable(current_module.localsymtable).check_forwards;
  1754. current_module.allunitsused;
  1755. end;
  1756. new_section(current_asmdata.asmlists[al_globals],sec_data,'_FPCDummy',4);
  1757. current_asmdata.asmlists[al_globals].concat(tai_symbol.createname_global('_FPCDummy',AT_DATA,0));
  1758. current_asmdata.asmlists[al_globals].concat(tai_const.create_32bit(0));
  1759. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  1760. current_asmdata.asmlists[al_procedures].concat(tai_symbol.createname_global('_DLLMainCRTStartup',AT_FUNCTION,0));
  1761. gen_fpc_dummy(current_asmdata.asmlists[al_procedures]);
  1762. current_asmdata.asmlists[al_procedures].concat(tai_const.createname('_FPCDummy',0));
  1763. { leave when we got an error }
  1764. if (Errorcount>0) and not status.skip_error then
  1765. begin
  1766. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1767. status.skip_error:=true;
  1768. exit;
  1769. end;
  1770. { remove all unused units, this happends when units are removed
  1771. from the uses clause in the source and the ppu was already being loaded }
  1772. hp:=tmodule(loaded_units.first);
  1773. while assigned(hp) do
  1774. begin
  1775. hp2:=hp;
  1776. hp:=tmodule(hp.next);
  1777. if hp2.is_unit and
  1778. not assigned(hp2.globalsymtable) then
  1779. loaded_units.remove(hp2);
  1780. end;
  1781. { force exports }
  1782. uu:=tused_unit(usedunits.first);
  1783. while assigned(uu) do
  1784. begin
  1785. uu.u.globalsymtable.symlist.ForEachCall(@insert_export,uu.u.globalsymtable);
  1786. { check localsymtable for exports too to get public symbols }
  1787. uu.u.localsymtable.symlist.ForEachCall(@insert_export,uu.u.localsymtable);
  1788. { create special exports }
  1789. if (uu.u.flags and uf_init)<>0 then
  1790. procexport(make_mangledname('INIT$',uu.u.globalsymtable,''));
  1791. if (uu.u.flags and uf_finalize)<>0 then
  1792. procexport(make_mangledname('FINALIZE$',uu.u.globalsymtable,''));
  1793. if (uu.u.flags and uf_threadvars)=uf_threadvars then
  1794. varexport(make_mangledname('THREADVARLIST',uu.u.globalsymtable,''));
  1795. uu:=tused_unit(uu.next);
  1796. end;
  1797. {$ifdef arm}
  1798. { Insert .pdata section for arm-wince.
  1799. It is needed for exception handling. }
  1800. if target_info.system in [system_arm_wince] then
  1801. InsertPData;
  1802. {$endif arm}
  1803. { generate debuginfo }
  1804. if (cs_debuginfo in current_settings.moduleswitches) then
  1805. current_debuginfo.inserttypeinfo;
  1806. exportlib.generatelib;
  1807. { write all our exports to the import library,
  1808. needs to be done after exportlib.generatelib; }
  1809. createimportlibfromexports;
  1810. { generate imports }
  1811. if current_module.ImportLibraryList.Count>0 then
  1812. importlib.generatelib;
  1813. { Reference all DEBUGINFO sections from the main .fpc section }
  1814. if (cs_debuginfo in current_settings.moduleswitches) then
  1815. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1816. { insert own objectfile }
  1817. insertobjectfile;
  1818. { assemble and link }
  1819. create_objectfile;
  1820. { We might need the symbols info if not using
  1821. the default do_extractsymbolinfo
  1822. which is a dummy function PM }
  1823. needsymbolinfo:=do_extractsymbolinfo<>@def_extractsymbolinfo;
  1824. { release all local symtables that are not needed anymore }
  1825. if (not needsymbolinfo) then
  1826. free_localsymtables(current_module.localsymtable);
  1827. { leave when we got an error }
  1828. if (Errorcount>0) and not status.skip_error then
  1829. begin
  1830. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1831. status.skip_error:=true;
  1832. exit;
  1833. end;
  1834. if (not current_module.is_unit) then
  1835. begin
  1836. { finally rewrite all units included into the package }
  1837. uu:=tused_unit(usedunits.first);
  1838. while assigned(uu) do
  1839. begin
  1840. RewritePPU(uu.u.ppufilename^,uu.u.ppufilename^);
  1841. uu:=tused_unit(uu.next);
  1842. end;
  1843. { create the executable when we are at level 1 }
  1844. if (compile_level=1) then
  1845. begin
  1846. { create global resource file by collecting all resource files }
  1847. CollectResourceFiles;
  1848. { write .def file }
  1849. if (cs_link_deffile in current_settings.globalswitches) then
  1850. deffile.writefile;
  1851. { insert all .o files from all loaded units and
  1852. unload the units, we don't need them anymore.
  1853. Keep the current_module because that is still needed }
  1854. hp:=tmodule(loaded_units.first);
  1855. while assigned(hp) do
  1856. begin
  1857. { the package itself contains no code so far }
  1858. linker.AddModuleFiles(hp);
  1859. hp2:=tmodule(hp.next);
  1860. if (hp<>current_module) and
  1861. (not needsymbolinfo) then
  1862. begin
  1863. loaded_units.remove(hp);
  1864. hp.free;
  1865. end;
  1866. hp:=hp2;
  1867. end;
  1868. linker.MakeSharedLibrary
  1869. end;
  1870. { Give Fatal with error count for linker errors }
  1871. if (Errorcount>0) and not status.skip_error then
  1872. begin
  1873. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1874. status.skip_error:=true;
  1875. end;
  1876. end;
  1877. end;
  1878. procedure proc_program(islibrary : boolean);
  1879. var
  1880. main_file : tinputfile;
  1881. hp,hp2 : tmodule;
  1882. finalize_procinfo,
  1883. init_procinfo,
  1884. main_procinfo : tcgprocinfo;
  1885. force_init_final : boolean;
  1886. resources_used : boolean;
  1887. program_name : ansistring;
  1888. begin
  1889. DLLsource:=islibrary;
  1890. Status.IsLibrary:=IsLibrary;
  1891. Status.IsPackage:=false;
  1892. Status.IsExe:=true;
  1893. parse_only:=false;
  1894. main_procinfo:=nil;
  1895. init_procinfo:=nil;
  1896. finalize_procinfo:=nil;
  1897. resources_used:=false;
  1898. { DLL defaults to create reloc info }
  1899. if islibrary then
  1900. begin
  1901. if not RelocSectionSetExplicitly then
  1902. RelocSection:=true;
  1903. end;
  1904. { Relocation works only without stabs under Windows when }
  1905. { external linker (LD) is used. LD generates relocs for }
  1906. { stab sections which is not loaded in memory. It causes }
  1907. { AV error when DLL is loaded and relocation is needed. }
  1908. { Internal linker does not have this problem. }
  1909. if RelocSection and
  1910. (target_info.system in systems_all_windows+[system_i386_wdosx]) and
  1911. (cs_link_extern in current_settings.globalswitches) then
  1912. begin
  1913. include(current_settings.globalswitches,cs_link_strip);
  1914. { Warning stabs info does not work with reloc section !! }
  1915. if (cs_debuginfo in current_settings.moduleswitches) and
  1916. (target_dbg.id=dbg_stabs) then
  1917. begin
  1918. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1919. Message(parser_w_parser_win32_debug_needs_WN);
  1920. exclude(current_settings.moduleswitches,cs_debuginfo);
  1921. end;
  1922. end;
  1923. { get correct output names }
  1924. main_file := current_scanner.inputfile;
  1925. while assigned(main_file.next) do
  1926. main_file := main_file.next;
  1927. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1928. if islibrary then
  1929. begin
  1930. consume(_LIBRARY);
  1931. program_name:=orgpattern;
  1932. consume(_ID);
  1933. while token=_POINT do
  1934. begin
  1935. consume(_POINT);
  1936. program_name:=program_name+'.'+orgpattern;
  1937. consume(_ID);
  1938. end;
  1939. current_module.setmodulename(program_name);
  1940. current_module.islibrary:=true;
  1941. exportlib.preparelib(program_name);
  1942. if tf_library_needs_pic in target_info.flags then
  1943. include(current_settings.moduleswitches,cs_create_pic);
  1944. consume(_SEMICOLON);
  1945. end
  1946. else
  1947. { is there an program head ? }
  1948. if token=_PROGRAM then
  1949. begin
  1950. consume(_PROGRAM);
  1951. program_name:=orgpattern;
  1952. consume(_ID);
  1953. while token=_POINT do
  1954. begin
  1955. consume(_POINT);
  1956. program_name:=program_name+'.'+orgpattern;
  1957. consume(_ID);
  1958. end;
  1959. current_module.setmodulename(program_name);
  1960. if (target_info.system in systems_unit_program_exports) then
  1961. exportlib.preparelib(program_name);
  1962. if token=_LKLAMMER then
  1963. begin
  1964. consume(_LKLAMMER);
  1965. repeat
  1966. consume(_ID);
  1967. until not try_to_consume(_COMMA);
  1968. consume(_RKLAMMER);
  1969. end;
  1970. consume(_SEMICOLON);
  1971. end
  1972. else if (target_info.system in systems_unit_program_exports) then
  1973. exportlib.preparelib(current_module.realmodulename^);
  1974. { global switches are read, so further changes aren't allowed }
  1975. current_module.in_global:=false;
  1976. { setup things using the switches }
  1977. setupglobalswitches;
  1978. { set implementation flag }
  1979. current_module.in_interface:=false;
  1980. current_module.interface_compiled:=true;
  1981. { insert after the unit symbol tables the static symbol table }
  1982. { of the program }
  1983. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1984. { load standard units (system,objpas,profile unit) }
  1985. loaddefaultunits;
  1986. { Load units provided on the command line }
  1987. loadautounits;
  1988. {Load the units used by the program we compile.}
  1989. if token=_USES then
  1990. loadunits;
  1991. { All units are read, now give them a number }
  1992. current_module.updatemaps;
  1993. {Insert the name of the main program into the symbol table.}
  1994. if current_module.realmodulename^<>'' then
  1995. tabstractunitsymtable(current_module.localsymtable).insertunit(tunitsym.create(current_module.realmodulename^,current_module));
  1996. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1997. symtablestack.push(current_module.localsymtable);
  1998. { Insert _GLOBAL_OFFSET_TABLE_ symbol if system uses it }
  1999. maybe_load_got;
  2000. { create whole program optimisation information }
  2001. current_module.wpoinfo:=tunitwpoinfo.create;
  2002. { The program intialization needs an alias, so it can be called
  2003. from the bootstrap code.}
  2004. if islibrary then
  2005. begin
  2006. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  2007. { Win32 startup code needs a single name }
  2008. if not(target_info.system in systems_darwin) then
  2009. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  2010. else
  2011. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  2012. end
  2013. else if (target_info.system in ([system_i386_netware,system_i386_netwlibc,system_powerpc_macos]+systems_darwin)) then
  2014. begin
  2015. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  2016. end
  2017. else
  2018. begin
  2019. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  2020. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  2021. end;
  2022. main_procinfo.parse_body;
  2023. { save file pos for debuginfo }
  2024. current_module.mainfilepos:=main_procinfo.entrypos;
  2025. { Generate specializations of objectdefs methods }
  2026. generate_specialization_procs;
  2027. { should we force unit initialization? }
  2028. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  2029. if force_init_final then
  2030. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  2031. { Add symbol to the exports section for win32 so smartlinking a
  2032. DLL will include the edata section }
  2033. if assigned(exportlib) and
  2034. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  2035. ((current_module.flags and uf_has_exports)<>0) then
  2036. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  2037. { finalize? }
  2038. if token=_FINALIZATION then
  2039. begin
  2040. { the uf_finalize flag is only set after we checked that it
  2041. wasn't empty }
  2042. { Parse the finalize }
  2043. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  2044. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  2045. finalize_procinfo.procdef.aliasnames.insert('PASCALFINALIZE');
  2046. finalize_procinfo.parse_body;
  2047. end
  2048. else
  2049. if force_init_final then
  2050. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  2051. { the finalization routine of libraries is generic (and all libraries need to }
  2052. { be finalized, so they can finalize any units they use }
  2053. if (islibrary) then
  2054. exportlib.setfininame(current_asmdata.asmlists[al_procedures],'FPC_LIB_EXIT');
  2055. { all labels must be defined before generating code }
  2056. if Errorcount=0 then
  2057. tstoredsymtable(current_module.localsymtable).checklabels;
  2058. { See remark in unit init/final }
  2059. main_procinfo.generate_code;
  2060. main_procinfo.resetprocdef;
  2061. release_main_proc(main_procinfo);
  2062. if assigned(init_procinfo) then
  2063. begin
  2064. init_procinfo.generate_code;
  2065. init_procinfo.resetprocdef;
  2066. release_main_proc(init_procinfo);
  2067. end;
  2068. if assigned(finalize_procinfo) then
  2069. begin
  2070. finalize_procinfo.generate_code;
  2071. finalize_procinfo.resetprocdef;
  2072. release_main_proc(finalize_procinfo);
  2073. end;
  2074. symtablestack.pop(current_module.localsymtable);
  2075. { consume the last point }
  2076. consume(_POINT);
  2077. { reset wpo flags for all defs }
  2078. reset_all_defs;
  2079. if (Errorcount=0) then
  2080. begin
  2081. { test static symtable }
  2082. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  2083. tstoredsymtable(current_module.localsymtable).allprivatesused;
  2084. tstoredsymtable(current_module.localsymtable).check_forwards;
  2085. current_module.allunitsused;
  2086. end;
  2087. { leave when we got an error }
  2088. if (Errorcount>0) and not status.skip_error then
  2089. begin
  2090. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2091. status.skip_error:=true;
  2092. exit;
  2093. end;
  2094. { remove all unused units, this happens when units are removed
  2095. from the uses clause in the source and the ppu was already being loaded }
  2096. hp:=tmodule(loaded_units.first);
  2097. while assigned(hp) do
  2098. begin
  2099. hp2:=hp;
  2100. hp:=tmodule(hp.next);
  2101. if hp2.is_unit and
  2102. not assigned(hp2.globalsymtable) then
  2103. begin
  2104. loaded_units.remove(hp2);
  2105. unloaded_units.concat(hp2);
  2106. end;
  2107. end;
  2108. { do we need to add the variants unit? }
  2109. maybeloadvariantsunit;
  2110. { Now that everything has been compiled we know if we need resource
  2111. support. If not, remove the unit. }
  2112. resources_used:=MaybeRemoveResUnit;
  2113. linker.initsysinitunitname;
  2114. if target_info.system in systems_internal_sysinit then
  2115. begin
  2116. { add start/halt unit }
  2117. AddUnit(linker.sysinitunit);
  2118. end;
  2119. {$ifdef arm}
  2120. { Insert .pdata section for arm-wince.
  2121. It is needed for exception handling. }
  2122. if target_info.system in [system_arm_wince] then
  2123. InsertPData;
  2124. {$endif arm}
  2125. InsertThreadvars;
  2126. { generate rtti/init tables }
  2127. write_persistent_type_info(current_module.localsymtable);
  2128. { if an Objective-C module, generate rtti and module info }
  2129. MaybeGenerateObjectiveCImageInfo(nil,current_module.localsymtable);
  2130. { generate wrappers for interfaces }
  2131. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable,false);
  2132. { generate imports }
  2133. if current_module.ImportLibraryList.Count>0 then
  2134. importlib.generatelib;
  2135. { generate debuginfo }
  2136. if (cs_debuginfo in current_settings.moduleswitches) then
  2137. current_debuginfo.inserttypeinfo;
  2138. if islibrary or (target_info.system in systems_unit_program_exports) then
  2139. exportlib.generatelib;
  2140. { Reference all DEBUGINFO sections from the main .fpc section }
  2141. if (cs_debuginfo in current_settings.moduleswitches) then
  2142. current_debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  2143. { Resource strings }
  2144. GenerateResourceStrings;
  2145. { Windows widestring needing initialization }
  2146. InsertWideInits;
  2147. { Resourcestring references (const foo:string=someresourcestring) }
  2148. InsertResStrInits;
  2149. { insert Tables and StackLength }
  2150. InsertInitFinalTable;
  2151. InsertThreadvarTablesTable;
  2152. InsertResourceTablesTable;
  2153. InsertWideInitsTablesTable;
  2154. InsertResStrTablesTable;
  2155. InsertMemorySizes;
  2156. {$ifdef FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  2157. if target_info.system in systems_interrupt_table then
  2158. InsertInterruptTable;
  2159. {$endif FPC_HAS_SYSTEMS_INTERRUPT_TABLE}
  2160. { Insert symbol to resource info }
  2161. InsertResourceInfo(resources_used);
  2162. { create callframe info }
  2163. create_dwarf_frame;
  2164. { insert own objectfile }
  2165. insertobjectfile;
  2166. { assemble and link }
  2167. create_objectfile;
  2168. { We might need the symbols info if not using
  2169. the default do_extractsymbolinfo
  2170. which is a dummy function PM }
  2171. needsymbolinfo:=
  2172. (do_extractsymbolinfo<>@def_extractsymbolinfo) or
  2173. ((current_settings.genwpoptimizerswitches*WPOptimizationsNeedingAllUnitInfo)<>[]);
  2174. { release all local symtables that are not needed anymore }
  2175. if (not needsymbolinfo) then
  2176. free_localsymtables(current_module.localsymtable);
  2177. { leave when we got an error }
  2178. if (Errorcount>0) and not status.skip_error then
  2179. begin
  2180. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2181. status.skip_error:=true;
  2182. exit;
  2183. end;
  2184. if (not current_module.is_unit) then
  2185. begin
  2186. { create the executable when we are at level 1 }
  2187. if (compile_level=1) then
  2188. begin
  2189. { create global resource file by collecting all resource files }
  2190. CollectResourceFiles;
  2191. { write .def file }
  2192. if (cs_link_deffile in current_settings.globalswitches) then
  2193. deffile.writefile;
  2194. { insert all .o files from all loaded units and
  2195. unload the units, we don't need them anymore.
  2196. Keep the current_module because that is still needed }
  2197. hp:=tmodule(loaded_units.first);
  2198. while assigned(hp) do
  2199. begin
  2200. linker.AddModuleFiles(hp);
  2201. hp2:=tmodule(hp.next);
  2202. if (hp<>current_module) and
  2203. (not needsymbolinfo) then
  2204. begin
  2205. loaded_units.remove(hp);
  2206. hp.free;
  2207. end;
  2208. hp:=hp2;
  2209. end;
  2210. { free also unneeded units we didn't free before }
  2211. if not needsymbolinfo then
  2212. unloaded_units.Clear;
  2213. { finally we can create a executable }
  2214. if DLLSource then
  2215. linker.MakeSharedLibrary
  2216. else
  2217. linker.MakeExecutable;
  2218. { collect all necessary information for whole-program optimization }
  2219. wpoinfomanager.extractwpoinfofromprogram;
  2220. end;
  2221. { Give Fatal with error count for linker errors }
  2222. if (Errorcount>0) and not status.skip_error then
  2223. begin
  2224. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  2225. status.skip_error:=true;
  2226. end;
  2227. end;
  2228. end;
  2229. end.