pmodules.pas 80 KB

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