pmodules.pas 54 KB

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