pmodules.pas 89 KB

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