pmodules.pas 84 KB

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