pmodules.pas 75 KB

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