pmodules.pas 88 KB

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