pmodules.pas 81 KB

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