pmodules.pas 90 KB

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