pmodules.pas 77 KB

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