pmodules.pas 89 KB

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