2
0

pmodules.pas 92 KB

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