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