pmodules.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. {
  2. Copyright (c) 1998-2002 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_program(islibrary : boolean);
  22. implementation
  23. uses
  24. SysUtils,
  25. globtype,version,systems,tokens,
  26. cutils,cfileutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmdata,aasmcpu,aasmbase,
  30. cgbase,cgobj,
  31. nbas,ncgutil,
  32. link,assemble,import,export,gendef,ppu,comprsrc,dbgbase,
  33. cresstr,procinfo,
  34. pexports,
  35. scanner,pbase,pexpr,psystem,psub,pdecsub;
  36. procedure create_objectfile;
  37. var
  38. DLLScanner : TDLLScanner;
  39. s : string;
  40. KeepShared : TStringList;
  41. begin
  42. { try to create import entries from system dlls }
  43. if (tf_has_dllscanner in target_info.flags) and
  44. (not current_module.linkOtherSharedLibs.Empty) then
  45. begin
  46. { Init DLLScanner }
  47. if assigned(CDLLScanner[target_info.system]) then
  48. DLLScanner:=CDLLScanner[target_info.system].Create
  49. else
  50. internalerror(200104121);
  51. KeepShared:=TStringList.Create;
  52. { Walk all shared libs }
  53. While not current_module.linkOtherSharedLibs.Empty do
  54. begin
  55. S:=current_module.linkOtherSharedLibs.Getusemask(link_always);
  56. if not DLLScanner.scan(s) then
  57. KeepShared.Concat(s);
  58. end;
  59. DLLscanner.Free;
  60. { Recreate import section }
  61. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  62. begin
  63. if assigned(current_asmdata.asmlists[al_imports]) then
  64. current_asmdata.asmlists[al_imports].clear
  65. else
  66. current_asmdata.asmlists[al_imports]:=TAsmList.Create;
  67. importlib.generatelib;
  68. end;
  69. { Readd the not processed files }
  70. while not KeepShared.Empty do
  71. begin
  72. s:=KeepShared.GetFirst;
  73. current_module.linkOtherSharedLibs.add(s,link_always);
  74. end;
  75. KeepShared.Free;
  76. end;
  77. { Start and end module debuginfo, at least required for stabs
  78. to insert n_sourcefile lines }
  79. if (cs_debuginfo in current_settings.moduleswitches) or
  80. (cs_use_lineinfo in current_settings.globalswitches) then
  81. debuginfo.insertmoduleinfo;
  82. { create the .s file and assemble it }
  83. GenerateAsm(false);
  84. { Also create a smartlinked version ? }
  85. if (cs_create_smart in current_settings.moduleswitches) and
  86. (not use_smartlink_section) then
  87. begin
  88. GenerateAsm(true);
  89. if (af_needar in target_asm.flags) then
  90. Linker.MakeStaticLibrary;
  91. end;
  92. { resource files }
  93. CompileResourceFiles;
  94. end;
  95. procedure insertobjectfile;
  96. { Insert the used object file for this unit in the used list for this unit }
  97. begin
  98. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  99. current_module.flags:=current_module.flags or uf_static_linked;
  100. if (cs_create_smart in current_settings.moduleswitches) and
  101. not use_smartlink_section then
  102. begin
  103. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  104. current_module.flags:=current_module.flags or uf_smart_linked;
  105. end;
  106. end;
  107. procedure create_dwarf;
  108. begin
  109. { Dwarf conflicts with smartlinking in separate .a files }
  110. if (cs_create_smart in current_settings.moduleswitches) and
  111. not use_smartlink_section then
  112. exit;
  113. { Call frame information }
  114. if (tf_needs_dwarf_cfi in target_info.flags) and
  115. (af_supports_dwarf in target_asm.flags) then
  116. begin
  117. current_asmdata.asmlists[al_dwarf]:=TAsmList.create;
  118. current_asmdata.asmcfi.generate_code(current_asmdata.asmlists[al_dwarf]);
  119. end;
  120. end;
  121. procedure InsertThreadvarTablesTable;
  122. var
  123. hp : tused_unit;
  124. ltvTables : TAsmList;
  125. count : longint;
  126. begin
  127. if (tf_section_threadvars in target_info.flags) then
  128. exit;
  129. ltvTables:=TAsmList.Create;
  130. count:=0;
  131. hp:=tused_unit(usedunits.first);
  132. while assigned(hp) do
  133. begin
  134. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  135. begin
  136. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),0));
  137. inc(count);
  138. end;
  139. hp:=tused_unit(hp.next);
  140. end;
  141. { Add program threadvars, if any }
  142. If (current_module.flags and uf_threadvars)=uf_threadvars then
  143. begin
  144. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),0));
  145. inc(count);
  146. end;
  147. { Insert TableCount at start }
  148. ltvTables.insert(Tai_const.Create_32bit(count));
  149. { insert in data segment }
  150. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  151. new_section(current_asmdata.asmlists[al_globals],sec_data,'FPC_THREADVARTABLES',sizeof(aint));
  152. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  153. current_asmdata.asmlists[al_globals].concatlist(ltvTables);
  154. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  155. ltvTables.free;
  156. end;
  157. procedure AddToThreadvarList(p:TObject;arg:pointer);
  158. var
  159. ltvTable : TAsmList;
  160. begin
  161. ltvTable:=TAsmList(arg);
  162. if (tsym(p).typ=staticvarsym) and
  163. (vo_is_thread_var in tstaticvarsym(p).varoptions) then
  164. begin
  165. { address of threadvar }
  166. ltvTable.concat(tai_const.Createname(tstaticvarsym(p).mangledname,0));
  167. { size of threadvar }
  168. ltvTable.concat(tai_const.create_32bit(tstaticvarsym(p).getsize));
  169. end;
  170. end;
  171. procedure InsertThreadvars;
  172. var
  173. s : string;
  174. ltvTable : TAsmList;
  175. begin
  176. if (tf_section_threadvars in target_info.flags) then
  177. exit;
  178. ltvTable:=TAsmList.create;
  179. if assigned(current_module.globalsymtable) then
  180. current_module.globalsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  181. current_module.localsymtable.SymList.ForEachCall(@AddToThreadvarList,ltvTable);
  182. if ltvTable.first<>nil then
  183. begin
  184. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  185. { end of the list marker }
  186. ltvTable.concat(tai_const.create_sym(nil));
  187. { add to datasegment }
  188. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  189. new_section(current_asmdata.asmlists[al_globals],sec_data,s,sizeof(aint));
  190. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global(s,AT_DATA,0));
  191. current_asmdata.asmlists[al_globals].concatlist(ltvTable);
  192. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname(s));
  193. current_module.flags:=current_module.flags or uf_threadvars;
  194. end;
  195. ltvTable.Free;
  196. end;
  197. {$ifdef arm}
  198. procedure InsertPData;
  199. var
  200. prolog: TAsmList;
  201. begin
  202. prolog:=TAsmList.create;
  203. new_section(prolog,sec_code,'FPC_EH_PROLOG',sizeof(aint),secorder_begin);
  204. prolog.concat(Tai_const.Createname('_ARM_ExceptionHandler', 0));
  205. prolog.concat(Tai_const.Create_32bit(0));
  206. prolog.concat(Tai_symbol.Createname_global('FPC_EH_CODE_START',AT_DATA,0));
  207. current_asmdata.asmlists[al_start].insertList(prolog);
  208. prolog.Free;
  209. new_section(current_asmdata.asmlists[al_end],sec_pdata,'',sizeof(aint));
  210. current_asmdata.asmlists[al_end].concat(Tai_const.Createname('FPC_EH_CODE_START', 0));
  211. current_asmdata.asmlists[al_end].concat(Tai_const.Create_32bit($cfffff02));
  212. end;
  213. {$endif arm}
  214. Procedure InsertResourceInfo;
  215. var
  216. hp : tused_unit;
  217. found : Boolean;
  218. I : Integer;
  219. ResourceInfo : TAsmList;
  220. begin
  221. if target_res.id=res_elf then
  222. begin
  223. hp:=tused_unit(usedunits.first);
  224. found:=false;
  225. Found:=((current_module.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  226. If not found then
  227. While Assigned(hp) and not Found do
  228. begin
  229. Found:=((hp.u.flags and uf_has_resourcefiles)=uf_has_resourcefiles);
  230. hp:=tused_unit(hp.next);
  231. end;
  232. ResourceInfo:=TAsmList.Create;
  233. if found then
  234. begin
  235. { Valid pointer to resource information }
  236. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  237. ResourceInfo.concat(Tai_const.Createname('FPC_RESSYMBOL',0));
  238. {$ifdef EXTERNALRESPTRS}
  239. current_module.linkotherofiles.add('resptrs.o',link_always);
  240. {$else EXTERNALRESPTRS}
  241. new_section(ResourceInfo,sec_fpc,'resptrs',4);
  242. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESSYMBOL',AT_DATA,0));
  243. For I:=1 to 32 do
  244. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  245. {$endif EXTERNALRESPTRS}
  246. end
  247. else
  248. begin
  249. { Nil pointer to resource information }
  250. ResourceInfo.concat(Tai_symbol.Createname_global('FPC_RESLOCATION',AT_DATA,0));
  251. ResourceInfo.Concat(Tai_const.Create_32bit(0));
  252. end;
  253. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  254. current_asmdata.asmlists[al_globals].concatlist(ResourceInfo);
  255. ResourceInfo.free;
  256. end;
  257. end;
  258. Procedure InsertResourceTablesTable;
  259. var
  260. hp : tmodule;
  261. ResourceStringTables : tasmlist;
  262. count : longint;
  263. begin
  264. ResourceStringTables:=tasmlist.Create;
  265. count:=0;
  266. hp:=tmodule(loaded_units.first);
  267. while assigned(hp) do
  268. begin
  269. If (hp.flags and uf_has_resourcestrings)=uf_has_resourcestrings then
  270. begin
  271. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'START'),0));
  272. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESSTR',hp.localsymtable,'END'),0));
  273. inc(count);
  274. end;
  275. hp:=tmodule(hp.next);
  276. end;
  277. { Insert TableCount at start }
  278. ResourceStringTables.insert(Tai_const.Create_aint(count));
  279. { Add to data segment }
  280. maybe_new_object_file(current_asmdata.AsmLists[al_globals]);
  281. new_section(current_asmdata.AsmLists[al_globals],sec_data,'FPC_RESOURCESTRINGTABLES',sizeof(aint));
  282. current_asmdata.AsmLists[al_globals].concat(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  283. current_asmdata.AsmLists[al_globals].concatlist(ResourceStringTables);
  284. current_asmdata.AsmLists[al_globals].concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  285. ResourceStringTables.free;
  286. end;
  287. procedure InsertInitFinalTable;
  288. var
  289. hp : tused_unit;
  290. unitinits : TAsmList;
  291. count : longint;
  292. begin
  293. unitinits:=TAsmList.Create;
  294. count:=0;
  295. hp:=tused_unit(usedunits.first);
  296. while assigned(hp) do
  297. begin
  298. { call the unit init code and make it external }
  299. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  300. begin
  301. if (hp.u.flags and uf_init)<>0 then
  302. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),0))
  303. else
  304. unitinits.concat(Tai_const.Create_sym(nil));
  305. if (hp.u.flags and uf_finalize)<>0 then
  306. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),0))
  307. else
  308. unitinits.concat(Tai_const.Create_sym(nil));
  309. inc(count);
  310. end;
  311. hp:=tused_unit(hp.next);
  312. end;
  313. { Insert initialization/finalization of the program }
  314. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  315. begin
  316. if (current_module.flags and uf_init)<>0 then
  317. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),0))
  318. else
  319. unitinits.concat(Tai_const.Create_sym(nil));
  320. if (current_module.flags and uf_finalize)<>0 then
  321. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),0))
  322. else
  323. unitinits.concat(Tai_const.Create_sym(nil));
  324. inc(count);
  325. end;
  326. { Insert TableCount,InitCount at start }
  327. unitinits.insert(Tai_const.Create_32bit(0));
  328. unitinits.insert(Tai_const.Create_32bit(count));
  329. { Add to data segment }
  330. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  331. new_section(current_asmdata.asmlists[al_globals],sec_data,'INITFINAL',sizeof(aint));
  332. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  333. current_asmdata.asmlists[al_globals].concatlist(unitinits);
  334. current_asmdata.asmlists[al_globals].concat(Tai_symbol_end.Createname('INITFINAL'));
  335. unitinits.free;
  336. end;
  337. procedure insertmemorysizes;
  338. {$IFDEF POWERPC}
  339. var
  340. stkcookie: string;
  341. {$ENDIF POWERPC}
  342. begin
  343. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  344. { Insert Ident of the compiler in the .fpc.version section }
  345. current_asmdata.asmlists[al_globals].concat(Tai_section.create(sec_fpc,'version',0));
  346. current_asmdata.asmlists[al_globals].concat(Tai_align.Create(const_align(32)));
  347. current_asmdata.asmlists[al_globals].concat(Tai_string.Create('FPC '+full_version_string+
  348. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  349. { stacksize can be specified and is now simulated }
  350. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stklen', sizeof(aint));
  351. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stklen',AT_DATA,sizeof(aint)));
  352. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(stacksize));
  353. {$IFDEF POWERPC}
  354. { AmigaOS4 "stack cookie" support }
  355. if ( target_info.system = system_powerpc_amiga ) then
  356. begin
  357. { this symbol is needed to ignite powerpc amigaos' }
  358. { stack allocation magic for us with the given stack size. }
  359. { note: won't work for m68k amigaos or morphos. (KB) }
  360. str(stacksize,stkcookie);
  361. stkcookie:='$STACK: '+stkcookie+#0;
  362. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  363. new_section(current_asmdata.asmlists[al_globals],sec_data,'__stack_cookie',length(stkcookie));
  364. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__stack_cookie',AT_DATA,length(stkcookie)));
  365. current_asmdata.asmlists[al_globals].concat(Tai_string.Create(stkcookie));
  366. end;
  367. {$ENDIF POWERPC}
  368. { Initial heapsize }
  369. maybe_new_object_file(current_asmdata.asmlists[al_globals]);
  370. new_section(current_asmdata.asmlists[al_globals],sec_data,'__heapsize',sizeof(aint));
  371. current_asmdata.asmlists[al_globals].concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,sizeof(aint)));
  372. current_asmdata.asmlists[al_globals].concat(Tai_const.Create_aint(heapsize));
  373. end;
  374. procedure AddUnit(const s:string);
  375. var
  376. hp : tppumodule;
  377. unitsym : tunitsym;
  378. begin
  379. { load unit }
  380. hp:=registerunit(current_module,s,'');
  381. hp.loadppu;
  382. hp.adddependency(current_module);
  383. { add to symtable stack }
  384. symtablestack.push(hp.globalsymtable);
  385. if (m_mac in current_settings.modeswitches) and
  386. assigned(hp.globalmacrosymtable) then
  387. macrosymtablestack.push(hp.globalmacrosymtable);
  388. { insert unitsym }
  389. unitsym:=tunitsym.create(s,hp);
  390. inc(unitsym.refs);
  391. current_module.localsymtable.insert(unitsym);
  392. { add to used units }
  393. current_module.addusedunit(hp,false,unitsym);
  394. end;
  395. procedure maybeloadvariantsunit;
  396. var
  397. hp : tmodule;
  398. begin
  399. { Do we need the variants unit? Skip this
  400. for VarUtils unit for bootstrapping }
  401. if (current_module.flags and uf_uses_variants=0) or
  402. (current_module.modulename^='VARUTILS') then
  403. exit;
  404. { Variants unit already loaded? }
  405. hp:=tmodule(loaded_units.first);
  406. while assigned(hp) do
  407. begin
  408. if hp.modulename^='VARIANTS' then
  409. exit;
  410. hp:=tmodule(hp.next);
  411. end;
  412. { Variants unit is not loaded yet, load it now }
  413. Message(parser_w_implicit_uses_of_variants_unit);
  414. AddUnit('Variants');
  415. end;
  416. procedure loaddefaultunits;
  417. begin
  418. { we are going to rebuild the symtablestack, clear it first }
  419. symtablestack.clear;
  420. macrosymtablestack.clear;
  421. { macro symtable }
  422. macrosymtablestack.push(initialmacrosymtable);
  423. { are we compiling the system unit? }
  424. if (cs_compilesystem in current_settings.moduleswitches) then
  425. begin
  426. systemunit:=tglobalsymtable(current_module.localsymtable);
  427. { create system defines }
  428. create_intern_symbols;
  429. create_intern_types;
  430. { Set the owner of errorsym and errortype to symtable to
  431. prevent crashes when accessing .owner }
  432. generrorsym.owner:=systemunit;
  433. generrordef.owner:=systemunit;
  434. exit;
  435. end;
  436. { insert the system unit, it is allways the first. Load also the
  437. internal types from the system unit }
  438. AddUnit('System');
  439. systemunit:=tglobalsymtable(symtablestack.top);
  440. load_intern_types;
  441. { Set the owner of errorsym and errortype to symtable to
  442. prevent crashes when accessing .owner }
  443. generrorsym.owner:=systemunit;
  444. generrordef.owner:=systemunit;
  445. { Units only required for main module }
  446. if not(current_module.is_unit) then
  447. begin
  448. { Heaptrc unit, load heaptrace before any other units especially objpas }
  449. if (cs_use_heaptrc in current_settings.globalswitches) then
  450. AddUnit('HeapTrc');
  451. { Lineinfo unit }
  452. if (cs_use_lineinfo in current_settings.globalswitches) then
  453. AddUnit('LineInfo');
  454. { Lineinfo unit }
  455. if (cs_gdb_valgrind in current_settings.globalswitches) then
  456. AddUnit('CMem');
  457. {$ifdef cpufpemu}
  458. { Floating point emulation unit?
  459. softfpu must be in the system unit anyways (FK)
  460. if (cs_fp_emulation in current_settings.moduleswitches) and not(target_info.system in system_wince) then
  461. AddUnit('SoftFpu');
  462. }
  463. {$endif cpufpemu}
  464. end;
  465. { Objpas unit? }
  466. if m_objpas in current_settings.modeswitches then
  467. AddUnit('ObjPas');
  468. { Macpas unit? }
  469. if m_mac in current_settings.modeswitches then
  470. AddUnit('MacPas');
  471. { Profile unit? Needed for go32v2 only }
  472. if (cs_profile in current_settings.moduleswitches) and
  473. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  474. AddUnit('Profile');
  475. if (cs_load_fpcylix_unit in current_settings.globalswitches) then
  476. begin
  477. AddUnit('FPCylix');
  478. AddUnit('DynLibs');
  479. end;
  480. end;
  481. procedure loadautounits;
  482. var
  483. hs,s : string;
  484. begin
  485. hs:=autoloadunits;
  486. repeat
  487. s:=GetToken(hs,',');
  488. if s='' then
  489. break;
  490. AddUnit(s);
  491. until false;
  492. end;
  493. procedure loadunits;
  494. var
  495. s,sorg : TIDString;
  496. fn : string;
  497. pu : tused_unit;
  498. hp2 : tmodule;
  499. unitsym : tunitsym;
  500. begin
  501. consume(_USES);
  502. repeat
  503. s:=pattern;
  504. sorg:=orgpattern;
  505. consume(_ID);
  506. { support "<unit> in '<file>'" construct, but not for tp7 }
  507. fn:='';
  508. if not(m_tp7 in current_settings.modeswitches) and
  509. try_to_consume(_OP_IN) then
  510. fn:=FixFileName(get_stringconst);
  511. { Give a warning if objpas is loaded }
  512. if s='OBJPAS' then
  513. Message(parser_w_no_objpas_use_mode);
  514. { Using the unit itself is not possible }
  515. if (s<>current_module.modulename^) then
  516. begin
  517. { check if the unit is already used }
  518. hp2:=nil;
  519. pu:=tused_unit(current_module.used_units.first);
  520. while assigned(pu) do
  521. begin
  522. if (pu.u.modulename^=s) then
  523. begin
  524. hp2:=pu.u;
  525. break;
  526. end;
  527. pu:=tused_unit(pu.next);
  528. end;
  529. if not assigned(hp2) then
  530. hp2:=registerunit(current_module,sorg,fn)
  531. else
  532. Message1(sym_e_duplicate_id,s);
  533. { Create unitsym, we need to use the name as specified, we
  534. can not use the modulename because that can be different
  535. when -Un is used }
  536. unitsym:=tunitsym.create(sorg,nil);
  537. current_module.localsymtable.insert(unitsym);
  538. { the current module uses the unit hp2 }
  539. current_module.addusedunit(hp2,true,unitsym);
  540. end
  541. else
  542. Message1(sym_e_duplicate_id,s);
  543. if token=_COMMA then
  544. begin
  545. pattern:='';
  546. consume(_COMMA);
  547. end
  548. else
  549. break;
  550. until false;
  551. { Load the units }
  552. pu:=tused_unit(current_module.used_units.first);
  553. while assigned(pu) do
  554. begin
  555. { Only load the units that are in the current
  556. (interface/implementation) uses clause }
  557. if pu.in_uses and
  558. (pu.in_interface=current_module.in_interface) then
  559. begin
  560. tppumodule(pu.u).loadppu;
  561. { is our module compiled? then we can stop }
  562. if current_module.state=ms_compiled then
  563. exit;
  564. { add this unit to the dependencies }
  565. pu.u.adddependency(current_module);
  566. { save crc values }
  567. pu.checksum:=pu.u.crc;
  568. pu.interface_checksum:=pu.u.interface_crc;
  569. { connect unitsym to the module }
  570. pu.unitsym.module:=pu.u;
  571. { add to symtable stack }
  572. symtablestack.push(pu.u.globalsymtable);
  573. if (m_mac in current_settings.modeswitches) and
  574. assigned(pu.u.globalmacrosymtable) then
  575. macrosymtablestack.push(pu.u.globalmacrosymtable);
  576. end;
  577. pu:=tused_unit(pu.next);
  578. end;
  579. consume(_SEMICOLON);
  580. end;
  581. procedure reset_all_defs;
  582. procedure reset_used_unit_defs(hp:tmodule);
  583. var
  584. pu : tused_unit;
  585. begin
  586. pu:=tused_unit(hp.used_units.first);
  587. while assigned(pu) do
  588. begin
  589. if not pu.u.is_reset then
  590. begin
  591. { prevent infinte loop for circular dependencies }
  592. pu.u.is_reset:=true;
  593. if assigned(pu.u.globalsymtable) then
  594. begin
  595. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  596. reset_used_unit_defs(pu.u);
  597. end;
  598. end;
  599. pu:=tused_unit(pu.next);
  600. end;
  601. end;
  602. var
  603. hp2 : tmodule;
  604. begin
  605. hp2:=tmodule(loaded_units.first);
  606. while assigned(hp2) do
  607. begin
  608. hp2.is_reset:=false;
  609. hp2:=tmodule(hp2.next);
  610. end;
  611. reset_used_unit_defs(current_module);
  612. end;
  613. procedure free_localsymtables(st:TSymtable);
  614. var
  615. i : longint;
  616. def : tstoreddef;
  617. pd : tprocdef;
  618. begin
  619. for i:=0 to st.DefList.Count-1 do
  620. begin
  621. def:=tstoreddef(st.DefList[i]);
  622. if def.typ=procdef then
  623. begin
  624. pd:=tprocdef(def);
  625. if assigned(pd.localst) and
  626. (pd.localst.symtabletype<>staticsymtable) and
  627. not(po_inline in pd.procoptions) then
  628. begin
  629. free_localsymtables(pd.localst);
  630. pd.localst.free;
  631. pd.localst:=nil;
  632. end;
  633. end;
  634. end;
  635. end;
  636. procedure parse_implementation_uses;
  637. begin
  638. if token=_USES then
  639. loadunits;
  640. end;
  641. procedure setupglobalswitches;
  642. begin
  643. if (cs_create_pic in current_settings.moduleswitches) then
  644. begin
  645. def_system_macro('FPC_PIC');
  646. def_system_macro('PIC');
  647. end;
  648. end;
  649. function create_main_proc(const name:string;potype:tproctypeoption;st:TSymtable):tcgprocinfo;
  650. var
  651. ps : tprocsym;
  652. pd : tprocdef;
  653. begin
  654. { there should be no current_procinfo available }
  655. if assigned(current_procinfo) then
  656. internalerror(200304275);
  657. {Generate a procsym for main}
  658. ps:=tprocsym.create('$'+name);
  659. { main are allways used }
  660. inc(ps.refs);
  661. st.insert(ps);
  662. pd:=tprocdef.create(main_program_level);
  663. include(pd.procoptions,po_global);
  664. pd.procsym:=ps;
  665. ps.ProcdefList.Add(pd);
  666. { set procdef options }
  667. pd.proctypeoption:=potype;
  668. pd.proccalloption:=pocall_default;
  669. pd.forwarddef:=false;
  670. pd.setmangledname(target_info.cprefix+name);
  671. pd.aliasnames.insert(pd.mangledname);
  672. handle_calling_convention(pd);
  673. { We don't need is a local symtable. Change it into the static
  674. symtable }
  675. pd.localst.free;
  676. pd.localst:=st;
  677. { set procinfo and current_procinfo.procdef }
  678. result:=tcgprocinfo(cprocinfo.create(nil));
  679. result.procdef:=pd;
  680. { main proc does always a call e.g. to init system unit }
  681. include(result.flags,pi_do_call);
  682. end;
  683. procedure release_main_proc(pi:tcgprocinfo);
  684. begin
  685. { remove localst as it was replaced by staticsymtable }
  686. pi.procdef.localst:=nil;
  687. { remove procinfo }
  688. current_module.procinfo:=nil;
  689. pi.free;
  690. pi:=nil;
  691. end;
  692. function gen_implicit_initfinal(flag:word;st:TSymtable):tcgprocinfo;
  693. begin
  694. { update module flags }
  695. current_module.flags:=current_module.flags or flag;
  696. { create procdef }
  697. case flag of
  698. uf_init :
  699. begin
  700. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  701. result.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  702. end;
  703. uf_finalize :
  704. begin
  705. result:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  706. result.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  707. end;
  708. else
  709. internalerror(200304253);
  710. end;
  711. result.code:=cnothingnode.create;
  712. end;
  713. procedure copy_macro(p:TObject; arg:pointer);
  714. begin
  715. current_module.globalmacrosymtable.insert(tmacro(p).getcopy);
  716. end;
  717. procedure proc_unit;
  718. function is_assembler_generated:boolean;
  719. var
  720. hal : tasmlisttype;
  721. begin
  722. result:=false;
  723. if Errorcount=0 then
  724. begin
  725. for hal:=low(TasmlistType) to high(TasmlistType) do
  726. if not current_asmdata.asmlists[hal].empty then
  727. begin
  728. result:=true;
  729. exit;
  730. end;
  731. end;
  732. end;
  733. var
  734. main_file: tinputfile;
  735. {$ifdef EXTDEBUG}
  736. store_crc,
  737. {$endif EXTDEBUG}
  738. store_interface_crc : cardinal;
  739. s1,s2 : ^string; {Saves stack space}
  740. force_init_final : boolean;
  741. init_procinfo,
  742. finalize_procinfo : tcgprocinfo;
  743. unitname8 : string[8];
  744. has_impl,ag: boolean;
  745. {$ifdef i386}
  746. gotvarsym : tstaticvarsym;
  747. {$endif i386}
  748. begin
  749. init_procinfo:=nil;
  750. finalize_procinfo:=nil;
  751. if m_mac in current_settings.modeswitches then
  752. current_module.mode_switch_allowed:= false;
  753. consume(_UNIT);
  754. if compile_level=1 then
  755. Status.IsExe:=false;
  756. if token=_ID then
  757. begin
  758. { create filenames and unit name }
  759. main_file := current_scanner.inputfile;
  760. while assigned(main_file.next) do
  761. main_file := main_file.next;
  762. new(s1);
  763. s1^:=current_module.modulename^;
  764. current_module.SetFileName(main_file.path^+main_file.name^,true);
  765. current_module.SetModuleName(orgpattern);
  766. { check for system unit }
  767. new(s2);
  768. s2^:=upper(ChangeFileExt(ExtractFileName(main_file.name^),''));
  769. unitname8:=copy(current_module.modulename^,1,8);
  770. if (cs_check_unit_name in current_settings.globalswitches) and
  771. (
  772. not(
  773. (current_module.modulename^=s2^) or
  774. (
  775. (length(current_module.modulename^)>8) and
  776. (unitname8=s2^)
  777. )
  778. )
  779. or
  780. (
  781. (length(s1^)>8) and
  782. (s1^<>current_module.modulename^)
  783. )
  784. ) then
  785. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  786. if (current_module.modulename^='SYSTEM') then
  787. include(current_settings.moduleswitches,cs_compilesystem);
  788. dispose(s2);
  789. dispose(s1);
  790. end;
  791. if (target_info.system in system_unit_program_exports) then
  792. exportlib.preparelib(current_module.realmodulename^);
  793. consume(_ID);
  794. consume(_SEMICOLON);
  795. consume(_INTERFACE);
  796. { global switches are read, so further changes aren't allowed }
  797. current_module.in_global:=false;
  798. { handle the global switches }
  799. setupglobalswitches;
  800. message1(unit_u_loading_interface_units,current_module.modulename^);
  801. { update status }
  802. status.currentmodule:=current_module.realmodulename^;
  803. { maybe turn off m_objpas if we are compiling objpas }
  804. if (current_module.modulename^='OBJPAS') then
  805. exclude(current_settings.modeswitches,m_objpas);
  806. { maybe turn off m_mac if we are compiling macpas }
  807. if (current_module.modulename^='MACPAS') then
  808. exclude(current_settings.modeswitches,m_mac);
  809. parse_only:=true;
  810. { generate now the global symboltable,
  811. define first as local to overcome dependency conflicts }
  812. current_module.localsymtable:=tglobalsymtable.create(current_module.modulename^,current_module.moduleid);
  813. { insert unitsym of this unit to prevent other units having
  814. the same name }
  815. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  816. { load default units, like the system unit }
  817. loaddefaultunits;
  818. { insert qualifier for the system unit (allows system.writeln) }
  819. if not(cs_compilesystem in current_settings.moduleswitches) and
  820. (token=_USES) then
  821. begin
  822. loadunits;
  823. { has it been compiled at a higher level ?}
  824. if current_module.state=ms_compiled then
  825. exit;
  826. end;
  827. { move the global symtable from the temporary local to global }
  828. current_module.globalsymtable:=current_module.localsymtable;
  829. current_module.localsymtable:=nil;
  830. reset_all_defs;
  831. { number all units, so we know if a unit is used by this unit or
  832. needs to be added implicitly }
  833. current_module.updatemaps;
  834. { ... parse the declarations }
  835. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  836. symtablestack.push(current_module.globalsymtable);
  837. read_interface_declarations;
  838. symtablestack.pop(current_module.globalsymtable);
  839. { Export macros defined in the interface for macpas. The macros
  840. are put in the globalmacrosymtable that will only be used by other
  841. units. The current unit continues to use the localmacrosymtable }
  842. if (m_mac in current_settings.modeswitches) then
  843. begin
  844. current_module.globalmacrosymtable:=tmacrosymtable.create(true);
  845. current_module.localmacrosymtable.SymList.ForEachCall(@copy_macro,nil);
  846. end;
  847. { leave when we got an error }
  848. if (Errorcount>0) and not status.skip_error then
  849. begin
  850. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  851. status.skip_error:=true;
  852. exit;
  853. end;
  854. { Our interface is compiled, generate CRC and switch to implementation }
  855. if not(cs_compilesystem in current_settings.moduleswitches) and
  856. (Errorcount=0) then
  857. tppumodule(current_module).getppucrc;
  858. current_module.in_interface:=false;
  859. current_module.interface_compiled:=true;
  860. { First reload all units depending on our interface, we need to do this
  861. in the implementation part to prevent errorneous circular references }
  862. reload_flagged_units;
  863. { Parse the implementation section }
  864. if (m_mac in current_settings.modeswitches) and try_to_consume(_END) then
  865. has_impl:= false
  866. else
  867. has_impl:= true;
  868. parse_only:=false;
  869. { generates static symbol table }
  870. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  871. {$ifdef i386}
  872. if cs_create_pic in current_settings.moduleswitches then
  873. begin
  874. { insert symbol for got access in assembler code}
  875. gotvarsym:=tstaticvarsym.create('_GLOBAL_OFFSET_TABLE_',vs_value,voidpointertype,[vo_is_external]);
  876. gotvarsym.set_mangledname('_GLOBAL_OFFSET_TABLE_');
  877. current_module.localsymtable.insert(gotvarsym);
  878. { avoid unnecessary warnings }
  879. gotvarsym.varstate:=vs_read;
  880. gotvarsym.refs:=1;
  881. end;
  882. {$endif i386}
  883. if has_impl then
  884. begin
  885. consume(_IMPLEMENTATION);
  886. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  887. { Read the implementation units }
  888. parse_implementation_uses;
  889. end;
  890. if current_module.state=ms_compiled then
  891. exit;
  892. { reset ranges/stabs in exported definitions }
  893. reset_all_defs;
  894. { All units are read, now give them a number }
  895. current_module.updatemaps;
  896. symtablestack.push(current_module.globalsymtable);
  897. symtablestack.push(current_module.localsymtable);
  898. if has_impl then
  899. begin
  900. Message1(parser_u_parsing_implementation,current_module.modulename^);
  901. if current_module.in_interface then
  902. internalerror(200212285);
  903. { Compile the unit }
  904. init_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,current_module.localsymtable);
  905. init_procinfo.procdef.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  906. init_procinfo.parse_body;
  907. { save file pos for debuginfo }
  908. current_module.mainfilepos:=init_procinfo.entrypos;
  909. end;
  910. { Generate specializations of objectdefs methods }
  911. generate_specialization_procs;
  912. { if the unit contains ansi/widestrings, initialization and
  913. finalization code must be forced }
  914. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  915. tstaticsymtable(current_module.localsymtable).needs_init_final;
  916. { should we force unit initialization? }
  917. { this is a hack, but how can it be done better ? }
  918. if force_init_final and ((current_module.flags and uf_init)=0) then
  919. begin
  920. { first release the not used init procinfo }
  921. if assigned(init_procinfo) then
  922. release_main_proc(init_procinfo);
  923. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  924. end;
  925. { finalize? }
  926. if has_impl and (token=_FINALIZATION) then
  927. begin
  928. { set module options }
  929. current_module.flags:=current_module.flags or uf_finalize;
  930. { Compile the finalize }
  931. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  932. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  933. finalize_procinfo.parse_body;
  934. end
  935. else if force_init_final then
  936. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  937. { Now both init and finalize bodies are read and it is known
  938. which variables are used in both init and finalize we can now
  939. generate the code. This is required to prevent putting a variable in
  940. a register that is also used in the finalize body (PFV) }
  941. if assigned(init_procinfo) then
  942. begin
  943. init_procinfo.generate_code;
  944. init_procinfo.resetprocdef;
  945. release_main_proc(init_procinfo);
  946. end;
  947. if assigned(finalize_procinfo) then
  948. begin
  949. finalize_procinfo.generate_code;
  950. finalize_procinfo.resetprocdef;
  951. release_main_proc(finalize_procinfo);
  952. end;
  953. symtablestack.pop(current_module.localsymtable);
  954. symtablestack.pop(current_module.globalsymtable);
  955. { the last char should always be a point }
  956. consume(_POINT);
  957. if (Errorcount=0) then
  958. begin
  959. { tests, if all (interface) forwards are resolved }
  960. tstoredsymtable(current_module.globalsymtable).check_forwards;
  961. { check if all private fields are used }
  962. tstoredsymtable(current_module.globalsymtable).allprivatesused;
  963. { remove cross unit overloads }
  964. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  965. { test static symtable }
  966. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  967. tstoredsymtable(current_module.localsymtable).allprivatesused;
  968. tstoredsymtable(current_module.localsymtable).check_forwards;
  969. tstoredsymtable(current_module.localsymtable).checklabels;
  970. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  971. { used units }
  972. current_module.allunitsused;
  973. end;
  974. { leave when we got an error }
  975. if (Errorcount>0) and not status.skip_error then
  976. begin
  977. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  978. status.skip_error:=true;
  979. exit;
  980. end;
  981. { do we need to add the variants unit? }
  982. maybeloadvariantsunit;
  983. { generate debuginfo }
  984. if (cs_debuginfo in current_settings.moduleswitches) then
  985. debuginfo.inserttypeinfo;
  986. { generate wrappers for interfaces }
  987. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.globalsymtable);
  988. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  989. { generate pic helpers to load eip if necessary }
  990. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  991. { Tables }
  992. insertThreadVars;
  993. { Resource strings }
  994. GenerateResourceStrings;
  995. { generate imports }
  996. if current_module.ImportLibraryList.Count>0 then
  997. importlib.generatelib;
  998. { insert own objectfile, or say that it's in a library
  999. (no check for an .o when loading) }
  1000. ag:=is_assembler_generated;
  1001. if ag then
  1002. insertobjectfile
  1003. else
  1004. begin
  1005. current_module.flags:=current_module.flags or uf_no_link;
  1006. current_module.flags:=current_module.flags and not uf_has_debuginfo;
  1007. end;
  1008. if ag then
  1009. begin
  1010. { create dwarf debuginfo }
  1011. create_dwarf;
  1012. { assemble }
  1013. create_objectfile;
  1014. end;
  1015. { Write out the ppufile after the object file has been created }
  1016. store_interface_crc:=current_module.interface_crc;
  1017. {$ifdef EXTDEBUG}
  1018. store_crc:=current_module.crc;
  1019. {$endif EXTDEBUG}
  1020. if (Errorcount=0) then
  1021. tppumodule(current_module).writeppu;
  1022. if not(cs_compilesystem in current_settings.moduleswitches) then
  1023. if store_interface_crc<>current_module.interface_crc then
  1024. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1025. {$ifdef EXTDEBUG}
  1026. if not(cs_compilesystem in current_settings.moduleswitches) then
  1027. if (store_crc<>current_module.crc) and simplify_ppu then
  1028. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1029. {$endif EXTDEBUG}
  1030. { release all overload references and local symtables that
  1031. are not needed anymore }
  1032. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1033. tstoredsymtable(current_module.globalsymtable).unchain_overloaded;
  1034. free_localsymtables(current_module.globalsymtable);
  1035. free_localsymtables(current_module.localsymtable);
  1036. { leave when we got an error }
  1037. if (Errorcount>0) and not status.skip_error then
  1038. begin
  1039. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1040. status.skip_error:=true;
  1041. exit;
  1042. end;
  1043. Message1(unit_u_finished_compiling,current_module.modulename^);
  1044. end;
  1045. procedure proc_program(islibrary : boolean);
  1046. var
  1047. main_file : tinputfile;
  1048. hp,hp2 : tmodule;
  1049. finalize_procinfo,
  1050. init_procinfo,
  1051. main_procinfo : tcgprocinfo;
  1052. force_init_final : boolean;
  1053. begin
  1054. DLLsource:=islibrary;
  1055. Status.IsLibrary:=IsLibrary;
  1056. Status.IsExe:=true;
  1057. parse_only:=false;
  1058. main_procinfo:=nil;
  1059. init_procinfo:=nil;
  1060. finalize_procinfo:=nil;
  1061. { DLL defaults to create reloc info }
  1062. if islibrary then
  1063. begin
  1064. if not RelocSectionSetExplicitly then
  1065. RelocSection:=true;
  1066. end;
  1067. { relocation works only without stabs under win32 !! PM }
  1068. { internal assembler uses rva for stabs info
  1069. so it should work with relocated DLLs }
  1070. if RelocSection and
  1071. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1072. (target_info.assem<>as_i386_pecoff) then
  1073. begin
  1074. include(current_settings.globalswitches,cs_link_strip);
  1075. { Warning stabs info does not work with reloc section !! }
  1076. if cs_debuginfo in current_settings.moduleswitches then
  1077. begin
  1078. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1079. Message(parser_w_parser_win32_debug_needs_WN);
  1080. exclude(current_settings.moduleswitches,cs_debuginfo);
  1081. end;
  1082. end;
  1083. { get correct output names }
  1084. main_file := current_scanner.inputfile;
  1085. while assigned(main_file.next) do
  1086. main_file := main_file.next;
  1087. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1088. if islibrary then
  1089. begin
  1090. consume(_LIBRARY);
  1091. stringdispose(current_module.modulename);
  1092. stringdispose(current_module.realmodulename);
  1093. current_module.modulename:=stringdup(pattern);
  1094. current_module.realmodulename:=stringdup(orgpattern);
  1095. current_module.islibrary:=true;
  1096. exportlib.preparelib(orgpattern);
  1097. if tf_library_needs_pic in target_info.flags then
  1098. include(current_settings.moduleswitches,cs_create_pic);
  1099. consume(_ID);
  1100. consume(_SEMICOLON);
  1101. end
  1102. else
  1103. { is there an program head ? }
  1104. if token=_PROGRAM then
  1105. begin
  1106. consume(_PROGRAM);
  1107. stringdispose(current_module.modulename);
  1108. stringdispose(current_module.realmodulename);
  1109. current_module.modulename:=stringdup(pattern);
  1110. current_module.realmodulename:=stringdup(orgpattern);
  1111. if (target_info.system in system_unit_program_exports) then
  1112. exportlib.preparelib(orgpattern);
  1113. consume(_ID);
  1114. if token=_LKLAMMER then
  1115. begin
  1116. consume(_LKLAMMER);
  1117. repeat
  1118. consume(_ID);
  1119. until not try_to_consume(_COMMA);
  1120. consume(_RKLAMMER);
  1121. end;
  1122. consume(_SEMICOLON);
  1123. end
  1124. else if (target_info.system in system_unit_program_exports) then
  1125. exportlib.preparelib(current_module.realmodulename^);
  1126. { global switches are read, so further changes aren't allowed }
  1127. current_module.in_global:=false;
  1128. { setup things using the switches }
  1129. setupglobalswitches;
  1130. { set implementation flag }
  1131. current_module.in_interface:=false;
  1132. current_module.interface_compiled:=true;
  1133. { insert after the unit symbol tables the static symbol table }
  1134. { of the program }
  1135. current_module.localsymtable:=tstaticsymtable.create(current_module.modulename^,current_module.moduleid);
  1136. { load standard units (system,objpas,profile unit) }
  1137. loaddefaultunits;
  1138. { Load units provided on the command line }
  1139. loadautounits;
  1140. {Load the units used by the program we compile.}
  1141. if token=_USES then
  1142. loadunits;
  1143. { reset ranges/stabs in exported definitions }
  1144. reset_all_defs;
  1145. { All units are read, now give them a number }
  1146. current_module.updatemaps;
  1147. {Insert the name of the main program into the symbol table.}
  1148. if current_module.realmodulename^<>'' then
  1149. current_module.localsymtable.insert(tunitsym.create(current_module.realmodulename^,current_module));
  1150. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1151. symtablestack.push(current_module.localsymtable);
  1152. { The program intialization needs an alias, so it can be called
  1153. from the bootstrap code.}
  1154. if islibrary then
  1155. begin
  1156. main_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,mainaliasname),potype_proginit,current_module.localsymtable);
  1157. { Win32 startup code needs a single name }
  1158. if not(target_info.system in [system_powerpc_darwin,system_i386_darwin]) then
  1159. main_procinfo.procdef.aliasnames.insert('PASCALMAIN')
  1160. else
  1161. main_procinfo.procdef.aliasnames.insert(target_info.Cprefix+'PASCALMAIN')
  1162. end
  1163. else if (target_info.system in [system_i386_netware,system_i386_netwlibc,system_powerpc_macos,system_powerpc_darwin,system_i386_darwin]) then
  1164. begin
  1165. main_procinfo:=create_main_proc('PASCALMAIN',potype_proginit,current_module.localsymtable);
  1166. end
  1167. else
  1168. begin
  1169. main_procinfo:=create_main_proc(mainaliasname,potype_proginit,current_module.localsymtable);
  1170. main_procinfo.procdef.aliasnames.insert('PASCALMAIN');
  1171. end;
  1172. main_procinfo.parse_body;
  1173. { save file pos for debuginfo }
  1174. current_module.mainfilepos:=main_procinfo.entrypos;
  1175. { Generate specializations of objectdefs methods }
  1176. generate_specialization_procs;
  1177. { should we force unit initialization? }
  1178. force_init_final:=tstaticsymtable(current_module.localsymtable).needs_init_final;
  1179. if force_init_final then
  1180. init_procinfo:=gen_implicit_initfinal(uf_init,current_module.localsymtable);
  1181. { Add symbol to the exports section for win32 so smartlinking a
  1182. DLL will include the edata section }
  1183. if assigned(exportlib) and
  1184. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1185. ((current_module.flags and uf_has_exports)<>0) then
  1186. current_asmdata.asmlists[al_procedures].concat(tai_const.createname(make_mangledname('EDATA',current_module.localsymtable,''),0));
  1187. { finalize? }
  1188. if token=_FINALIZATION then
  1189. begin
  1190. { set module options }
  1191. current_module.flags:=current_module.flags or uf_finalize;
  1192. { Parse the finalize }
  1193. finalize_procinfo:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,current_module.localsymtable);
  1194. finalize_procinfo.procdef.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1195. finalize_procinfo.parse_body;
  1196. end
  1197. else
  1198. if force_init_final then
  1199. finalize_procinfo:=gen_implicit_initfinal(uf_finalize,current_module.localsymtable);
  1200. { See remark in unit init/final }
  1201. main_procinfo.generate_code;
  1202. main_procinfo.resetprocdef;
  1203. release_main_proc(main_procinfo);
  1204. if assigned(init_procinfo) then
  1205. begin
  1206. init_procinfo.generate_code;
  1207. init_procinfo.resetprocdef;
  1208. release_main_proc(init_procinfo);
  1209. end;
  1210. if assigned(finalize_procinfo) then
  1211. begin
  1212. finalize_procinfo.generate_code;
  1213. finalize_procinfo.resetprocdef;
  1214. release_main_proc(finalize_procinfo);
  1215. end;
  1216. symtablestack.pop(current_module.localsymtable);
  1217. { consume the last point }
  1218. consume(_POINT);
  1219. if (Errorcount=0) then
  1220. begin
  1221. { test static symtable }
  1222. tstoredsymtable(current_module.localsymtable).allsymbolsused;
  1223. tstoredsymtable(current_module.localsymtable).allprivatesused;
  1224. tstoredsymtable(current_module.localsymtable).check_forwards;
  1225. tstoredsymtable(current_module.localsymtable).checklabels;
  1226. tstoredsymtable(current_module.localsymtable).unchain_overloaded;
  1227. current_module.allunitsused;
  1228. end;
  1229. { leave when we got an error }
  1230. if (Errorcount>0) and not status.skip_error then
  1231. begin
  1232. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1233. status.skip_error:=true;
  1234. exit;
  1235. end;
  1236. { remove all unused units, this happends when units are removed
  1237. from the uses clause in the source and the ppu was already being loaded }
  1238. hp:=tmodule(loaded_units.first);
  1239. while assigned(hp) do
  1240. begin
  1241. hp2:=hp;
  1242. hp:=tmodule(hp.next);
  1243. if hp2.is_unit and
  1244. not assigned(hp2.globalsymtable) then
  1245. loaded_units.remove(hp2);
  1246. end;
  1247. { do we need to add the variants unit? }
  1248. maybeloadvariantsunit;
  1249. linker.initsysinitunitname;
  1250. if target_info.system in systems_internal_sysinit then
  1251. begin
  1252. { add start/halt unit }
  1253. AddUnit(linker.sysinitunit);
  1254. end;
  1255. {$ifdef arm}
  1256. { Insert .pdata section for arm-wince.
  1257. It is needed for exception handling. }
  1258. if target_info.system in [system_arm_wince] then
  1259. InsertPData;
  1260. {$endif arm}
  1261. { generate debuginfo }
  1262. if (cs_debuginfo in current_settings.moduleswitches) then
  1263. debuginfo.inserttypeinfo;
  1264. InsertThreadvars;
  1265. { generate wrappers for interfaces }
  1266. gen_intf_wrappers(current_asmdata.asmlists[al_procedures],current_module.localsymtable);
  1267. { generate pic helpers to load eip if necessary }
  1268. gen_pic_helpers(current_asmdata.asmlists[al_procedures]);
  1269. { generate imports }
  1270. if current_module.ImportLibraryList.Count>0 then
  1271. importlib.generatelib;
  1272. if islibrary or (target_info.system in system_unit_program_exports) then
  1273. exportlib.generatelib;
  1274. { Reference all DEBUGINFO sections from the main .fpc section }
  1275. if (cs_debuginfo in current_settings.moduleswitches) then
  1276. debuginfo.referencesections(current_asmdata.asmlists[al_procedures]);
  1277. { Resource strings }
  1278. GenerateResourceStrings;
  1279. { insert Tables and StackLength }
  1280. insertinitfinaltable;
  1281. InsertThreadvarTablesTable;
  1282. InsertResourceTablesTable;
  1283. insertmemorysizes;
  1284. { Insert symbol to resource info }
  1285. InsertResourceInfo;
  1286. { create dwarf debuginfo }
  1287. create_dwarf;
  1288. { insert own objectfile }
  1289. insertobjectfile;
  1290. { assemble and link }
  1291. create_objectfile;
  1292. { release all local symtables that are not needed anymore }
  1293. free_localsymtables(current_module.localsymtable);
  1294. { leave when we got an error }
  1295. if (Errorcount>0) and not status.skip_error then
  1296. begin
  1297. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1298. status.skip_error:=true;
  1299. exit;
  1300. end;
  1301. if (not current_module.is_unit) then
  1302. begin
  1303. { create the executable when we are at level 1 }
  1304. if (compile_level=1) then
  1305. begin
  1306. { write .def file }
  1307. if (cs_link_deffile in current_settings.globalswitches) then
  1308. deffile.writefile;
  1309. { insert all .o files from all loaded units and
  1310. unload the units, we don't need them anymore.
  1311. Keep the current_module because that is still needed }
  1312. hp:=tmodule(loaded_units.first);
  1313. while assigned(hp) do
  1314. begin
  1315. linker.AddModuleFiles(hp);
  1316. hp2:=tmodule(hp.next);
  1317. if hp<>current_module then
  1318. begin
  1319. loaded_units.remove(hp);
  1320. hp.free;
  1321. end;
  1322. hp:=hp2;
  1323. end;
  1324. { finally we can create a executable }
  1325. if DLLSource then
  1326. linker.MakeSharedLibrary
  1327. else
  1328. linker.MakeExecutable;
  1329. end;
  1330. { Give Fatal with error count for linker errors }
  1331. if (Errorcount>0) and not status.skip_error then
  1332. begin
  1333. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1334. status.skip_error:=true;
  1335. end;
  1336. end;
  1337. end;
  1338. end.