pmodules.pas 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {$i fpcdefs.inc}
  20. interface
  21. procedure proc_unit;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. globtype,version,systems,tokens,
  26. cutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmcpu,aasmbase,
  30. cgbase,cgobj,
  31. nbas,
  32. link,assemble,import,export,gendef,ppu,comprsrc,
  33. cresstr,procinfo,
  34. dwarf,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub,pdecsub;
  39. procedure fixseg(p:TAAsmoutput; sec:TAsmSectionType; secname: string);
  40. begin
  41. maybe_new_object_file(p);
  42. if target_info.system <> system_powerpc_macos then
  43. p.insert(Tai_section.Create(sec,'',0))
  44. else
  45. p.insert(Tai_section.Create(sec,secname,0));
  46. end;
  47. procedure create_objectfile;
  48. var
  49. DLLScanner : TDLLScanner;
  50. s : string;
  51. KeepShared : TStringList;
  52. begin
  53. { try to create import entries from system dlls }
  54. if target_info.DllScanSupported and
  55. (not current_module.linkOtherSharedLibs.Empty) then
  56. begin
  57. { Init DLLScanner }
  58. if assigned(CDLLScanner[target_info.system]) then
  59. DLLScanner:=CDLLScanner[target_info.system].Create
  60. else
  61. internalerror(200104121);
  62. KeepShared:=TStringList.Create;
  63. { Walk all shared libs }
  64. While not current_module.linkOtherSharedLibs.Empty do
  65. begin
  66. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  67. if not DLLScanner.scan(s) then
  68. KeepShared.Concat(s);
  69. end;
  70. DLLscanner.Free;
  71. { Recreate import section }
  72. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  73. begin
  74. if assigned(importssection)then
  75. importssection.clear
  76. else
  77. importssection:=taasmoutput.Create;
  78. importlib.generatelib;
  79. end;
  80. { Readd the not processed files }
  81. while not KeepShared.Empty do
  82. begin
  83. s:=KeepShared.GetFirst;
  84. current_module.linkOtherSharedLibs.add(s,link_allways);
  85. end;
  86. KeepShared.Free;
  87. end;
  88. { create the .s file and assemble it }
  89. GenerateAsm(false);
  90. { Also create a smartlinked version ? }
  91. if (cs_create_smart in aktmoduleswitches) then
  92. begin
  93. { regenerate the importssection for win32 }
  94. if assigned(importssection) and
  95. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  96. begin
  97. importsSection.clear;
  98. importlib.generatesmartlib;
  99. end;
  100. GenerateAsm(true);
  101. if (af_needar in target_asm.flags) then
  102. Linker.MakeStaticLibrary;
  103. end;
  104. { resource files }
  105. CompileResourceFiles;
  106. end;
  107. procedure insertobjectfile;
  108. { Insert the used object file for this unit in the used list for this unit }
  109. begin
  110. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  111. current_module.flags:=current_module.flags or uf_static_linked;
  112. if (cs_create_smart in aktmoduleswitches) then
  113. begin
  114. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  115. current_module.flags:=current_module.flags or uf_smart_linked;
  116. end;
  117. end;
  118. procedure create_dwarf;
  119. begin
  120. dwarflist:=taasmoutput.create;
  121. { Call frame information }
  122. if (tf_needs_dwarf_cfi in target_info.flags) and
  123. (af_supports_dwarf in target_asm.flags) then
  124. dwarfcfi.generate_code(dwarflist);
  125. end;
  126. procedure insertsegment;
  127. var
  128. oldaktfilepos : tfileposinfo;
  129. {Note: Sections get names in macos only.}
  130. begin
  131. { Insert Ident of the compiler }
  132. if (not (cs_create_smart in aktmoduleswitches))
  133. {$ifndef EXTDEBUG}
  134. and (not current_module.is_unit)
  135. {$endif}
  136. then
  137. begin
  138. { align the first data }
  139. dataSegment.insert(Tai_align.Create(const_align(32)));
  140. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  141. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  142. end;
  143. { align code segment }
  144. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  145. { Insert start and end of sections }
  146. fixseg(codesegment,sec_code,'____seg_code');
  147. fixseg(datasegment,sec_data,'____seg_data');
  148. fixseg(bsssegment,sec_bss,'____seg_bss');
  149. { we should use .rdata section for these two no ?
  150. .rdata is a read only data section (PM) }
  151. fixseg(rttilist,sec_data,'____seg_rtti');
  152. fixseg(consts,sec_data,'____seg_consts');
  153. fixseg(picdata,sec_data,'____seg_picdata');
  154. if assigned(resourcestringlist) then
  155. fixseg(resourcestringlist,sec_data,'____seg_resstrings');
  156. {$ifdef GDB}
  157. if assigned(debuglist) then
  158. begin
  159. oldaktfilepos:=aktfilepos;
  160. aktfilepos.line:=0;
  161. debugList.insert(Tai_symbol.Createname('gcc2_compiled',AT_DATA,0));
  162. debugList.insert(Tai_symbol.Createname('fpc_compiled',AT_DATA,0));
  163. fixseg(debuglist,sec_code,'____seg_debug');
  164. aktfilepos:=oldaktfilepos;
  165. end;
  166. {$endif GDB}
  167. end;
  168. procedure InsertThreadvarTablesTable;
  169. var
  170. hp : tused_unit;
  171. ltvTables : taasmoutput;
  172. count : longint;
  173. begin
  174. ltvTables:=TAAsmOutput.Create;
  175. count:=0;
  176. hp:=tused_unit(usedunits.first);
  177. while assigned(hp) do
  178. begin
  179. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  180. begin
  181. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),AT_DATA,0));
  182. inc(count);
  183. end;
  184. hp:=tused_unit(hp.next);
  185. end;
  186. { Add program threadvars, if any }
  187. If (current_module.flags and uf_threadvars)=uf_threadvars then
  188. begin
  189. ltvTables.concat(Tai_const.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),AT_DATA,0));
  190. inc(count);
  191. end;
  192. { TableCount }
  193. ltvTables.insert(Tai_const.Create_32bit(count));
  194. ltvTables.insert(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  195. ltvTables.insert(Tai_align.Create(const_align(sizeof(aint))));
  196. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  197. { insert in data segment }
  198. maybe_new_object_file(dataSegment);
  199. dataSegment.concatlist(ltvTables);
  200. ltvTables.free;
  201. end;
  202. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  203. var
  204. ltvTable : taasmoutput;
  205. begin
  206. ltvTable:=taasmoutput(arg);
  207. if (tsym(p).typ=globalvarsym) and
  208. (vo_is_thread_var in tglobalvarsym(p).varoptions) then
  209. begin
  210. { address of threadvar }
  211. ltvTable.concat(tai_const.Createname(tglobalvarsym(p).mangledname,AT_DATA,0));
  212. { size of threadvar }
  213. ltvTable.concat(tai_const.create_32bit(tglobalvarsym(p).getsize));
  214. end;
  215. end;
  216. procedure InsertThreadvars;
  217. var
  218. s : string;
  219. ltvTable : TAAsmoutput;
  220. begin
  221. ltvTable:=TAAsmoutput.create;
  222. if assigned(current_module.globalsymtable) then
  223. current_module.globalsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  224. current_module.localsymtable.foreach_static(@AddToThreadvarList,ltvTable);
  225. if ltvTable.first<>nil then
  226. begin
  227. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  228. { add begin and end of the list }
  229. ltvTable.insert(tai_symbol.Createname_global(s,AT_DATA,0));
  230. ltvTable.insert(Tai_align.Create(const_align(32)));
  231. ltvTable.concat(tai_const.create_sym(nil)); { end of list marker }
  232. ltvTable.concat(tai_symbol_end.createname(s));
  233. maybe_new_object_file(dataSegment);
  234. dataSegment.concatlist(ltvTable);
  235. current_module.flags:=current_module.flags or uf_threadvars;
  236. end;
  237. ltvTable.Free;
  238. end;
  239. Procedure InsertResourceTablesTable;
  240. var
  241. hp : tused_unit;
  242. ResourceStringTables : taasmoutput;
  243. count : longint;
  244. begin
  245. ResourceStringTables:=TAAsmOutput.Create;
  246. count:=0;
  247. hp:=tused_unit(usedunits.first);
  248. while assigned(hp) do
  249. begin
  250. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  251. begin
  252. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,''),AT_DATA,0));
  253. inc(count);
  254. end;
  255. hp:=tused_unit(hp.next);
  256. end;
  257. { Add program resources, if any }
  258. If ResourceStringList<>Nil then
  259. begin
  260. ResourceStringTables.concat(Tai_const.Createname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  261. Inc(Count);
  262. end;
  263. { TableCount }
  264. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  265. ResourceStringTables.insert(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  266. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  267. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  268. { insert in data segment }
  269. maybe_new_object_file(dataSegment);
  270. dataSegment.concatlist(ResourceStringTables);
  271. ResourceStringTables.free;
  272. end;
  273. procedure InsertInitFinalTable;
  274. var
  275. hp : tused_unit;
  276. unitinits : taasmoutput;
  277. count : longint;
  278. begin
  279. unitinits:=TAAsmOutput.Create;
  280. count:=0;
  281. hp:=tused_unit(usedunits.first);
  282. while assigned(hp) do
  283. begin
  284. { call the unit init code and make it external }
  285. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  286. begin
  287. if (hp.u.flags and uf_init)<>0 then
  288. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  289. else
  290. unitinits.concat(Tai_const.Create_sym(nil));
  291. if (hp.u.flags and uf_finalize)<>0 then
  292. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  293. else
  294. unitinits.concat(Tai_const.Create_sym(nil));
  295. inc(count);
  296. end;
  297. hp:=tused_unit(hp.next);
  298. end;
  299. { Insert initialization/finalization of the program }
  300. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  301. begin
  302. if (current_module.flags and uf_init)<>0 then
  303. unitinits.concat(Tai_const.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0))
  304. else
  305. unitinits.concat(Tai_const.Create_sym(nil));
  306. if (current_module.flags and uf_finalize)<>0 then
  307. unitinits.concat(Tai_const.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0))
  308. else
  309. unitinits.concat(Tai_const.Create_sym(nil));
  310. inc(count);
  311. end;
  312. { TableCount,InitCount }
  313. unitinits.insert(Tai_const.Create_32bit(0));
  314. unitinits.insert(Tai_const.Create_32bit(count));
  315. unitinits.insert(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  316. unitinits.insert(Tai_align.Create(const_align(4)));
  317. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  318. { insert in data segment }
  319. maybe_new_object_file(dataSegment);
  320. dataSegment.concatlist(unitinits);
  321. unitinits.free;
  322. end;
  323. procedure insertmemorysizes;
  324. begin
  325. { stacksize can be specified and is now simulated }
  326. dataSegment.concat(Tai_align.Create(const_align(4)));
  327. dataSegment.concat(Tai_symbol.Createname_global('__stklen',AT_DATA,4));
  328. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  329. dataSegment.concat(Tai_symbol.Createname_global('__heapsize',AT_DATA,4));
  330. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  331. end;
  332. procedure loaddefaultunits;
  333. procedure AddUnit(const s:string);
  334. var
  335. hp : tppumodule;
  336. unitsym : tunitsym;
  337. begin
  338. { load unit }
  339. hp:=registerunit(current_module,s,'');
  340. hp.loadppu;
  341. hp.adddependency(current_module);
  342. { add to symtable stack }
  343. tsymtable(hp.globalsymtable).next:=symtablestack;
  344. symtablestack:=hp.globalsymtable;
  345. { insert unitsym }
  346. unitsym:=tunitsym.create(s,hp.globalsymtable);
  347. inc(unitsym.refs);
  348. refsymtable.insert(unitsym);
  349. { add to used units }
  350. current_module.addusedunit(hp,false,unitsym);
  351. end;
  352. begin
  353. { are we compiling the system unit? }
  354. if (cs_compilesystem in aktmoduleswitches) then
  355. begin
  356. { create system defines }
  357. createconstdefs;
  358. { we don't need to reset anything, it's already done in parser.pas }
  359. exit;
  360. end;
  361. { insert the system unit, it is allways the first }
  362. Symtablestack:=nil;
  363. AddUnit('System');
  364. SystemUnit:=TGlobalSymtable(Symtablestack);
  365. { read default constant definitions }
  366. make_ref:=false;
  367. readconstdefs;
  368. make_ref:=true;
  369. { Set the owner of errorsym and errortype to symtable to
  370. prevent crashes when accessing .owner }
  371. generrorsym.owner:=systemunit;
  372. generrortype.def.owner:=systemunit;
  373. {$ifdef cpufpemu}
  374. { Floating point emulation unit? }
  375. if (cs_fp_emulation in aktmoduleswitches) then
  376. AddUnit('SoftFpu');
  377. {$endif cpufpemu}
  378. { Thread support unit? }
  379. if (cs_threading in aktmoduleswitches) then
  380. AddUnit('SysThrds');
  381. { Objpas unit? }
  382. if m_objpas in aktmodeswitches then
  383. AddUnit('ObjPas');
  384. { Macpas unit? }
  385. if m_mac in aktmodeswitches then
  386. AddUnit('MacPas');
  387. { Profile unit? Needed for go32v2 only }
  388. if (cs_profile in aktmoduleswitches) and
  389. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  390. AddUnit('Profile');
  391. { Units only required for main module }
  392. if not(current_module.is_unit) then
  393. begin
  394. { Heaptrc unit }
  395. if (cs_gdb_heaptrc in aktglobalswitches) then
  396. AddUnit('HeapTrc');
  397. { Lineinfo unit }
  398. if (cs_gdb_lineinfo in aktglobalswitches) then
  399. AddUnit('LineInfo');
  400. { Lineinfo unit }
  401. if (cs_gdb_valgrind in aktglobalswitches) then
  402. AddUnit('CMem');
  403. end;
  404. { save default symtablestack }
  405. defaultsymtablestack:=symtablestack;
  406. end;
  407. procedure loadunits;
  408. var
  409. s,sorg : stringid;
  410. fn : string;
  411. pu : tused_unit;
  412. hp2 : tmodule;
  413. hp3 : tsymtable;
  414. unitsym : tunitsym;
  415. begin
  416. consume(_USES);
  417. {$ifdef DEBUG}
  418. test_symtablestack;
  419. {$endif DEBUG}
  420. repeat
  421. s:=pattern;
  422. sorg:=orgpattern;
  423. consume(_ID);
  424. { support "<unit> in '<file>'" construct, but not for tp7 }
  425. if not(m_tp7 in aktmodeswitches) then
  426. begin
  427. if try_to_consume(_OP_IN) then
  428. fn:=FixFileName(get_stringconst)
  429. else
  430. fn:='';
  431. end;
  432. { Give a warning if objpas is loaded }
  433. if s='OBJPAS' then
  434. Message(parser_w_no_objpas_use_mode);
  435. { Using the unit itself is not possible }
  436. if (s<>current_module.modulename^) then
  437. begin
  438. { check if the unit is already used }
  439. hp2:=nil;
  440. pu:=tused_unit(current_module.used_units.first);
  441. while assigned(pu) do
  442. begin
  443. if (pu.u.modulename^=s) then
  444. begin
  445. hp2:=pu.u;
  446. break;
  447. end;
  448. pu:=tused_unit(pu.next);
  449. end;
  450. if not assigned(hp2) then
  451. hp2:=registerunit(current_module,sorg,fn)
  452. else
  453. Message1(sym_e_duplicate_id,s);
  454. { Create unitsym, we need to use the name as specified, we
  455. can not use the modulename because that can be different
  456. when -Un is used }
  457. unitsym:=tunitsym.create(sorg,nil);
  458. refsymtable.insert(unitsym);
  459. { the current module uses the unit hp2 }
  460. current_module.addusedunit(hp2,true,unitsym);
  461. end
  462. else
  463. Message1(sym_e_duplicate_id,s);
  464. if token=_COMMA then
  465. begin
  466. pattern:='';
  467. consume(_COMMA);
  468. end
  469. else
  470. break;
  471. until false;
  472. consume(_SEMICOLON);
  473. { Load the units }
  474. pu:=tused_unit(current_module.used_units.first);
  475. while assigned(pu) do
  476. begin
  477. { Only load the units that are in the current
  478. (interface/implementation) uses clause }
  479. if pu.in_uses and
  480. (pu.in_interface=current_module.in_interface) then
  481. begin
  482. tppumodule(pu.u).loadppu;
  483. { is our module compiled? then we can stop }
  484. if current_module.state=ms_compiled then
  485. exit;
  486. { add this unit to the dependencies }
  487. pu.u.adddependency(current_module);
  488. { save crc values }
  489. pu.checksum:=pu.u.crc;
  490. pu.interface_checksum:=pu.u.interface_crc;
  491. { connect unitsym to the globalsymtable of the unit }
  492. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  493. { increase refs of the unitsym when the unit contains
  494. initialization/finalization code so it doesn't trigger
  495. the unit not used hint }
  496. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  497. inc(pu.unitsym.refs);
  498. end;
  499. pu:=tused_unit(pu.next);
  500. end;
  501. { set the symtable to systemunit so it gets reorderd correctly,
  502. then insert the units in the symtablestack }
  503. pu:=tused_unit(current_module.used_units.first);
  504. symtablestack:=defaultsymtablestack;
  505. while assigned(pu) do
  506. begin
  507. if pu.in_uses then
  508. begin
  509. { Reinsert in symtablestack }
  510. hp3:=symtablestack;
  511. while assigned(hp3) do
  512. begin
  513. { insert units only once ! }
  514. if pu.u.globalsymtable=hp3 then
  515. break;
  516. hp3:=hp3.next;
  517. { unit isn't inserted }
  518. if hp3=nil then
  519. begin
  520. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  521. symtablestack:=tsymtable(pu.u.globalsymtable);
  522. {$ifdef DEBUG}
  523. test_symtablestack;
  524. {$endif DEBUG}
  525. end;
  526. end;
  527. end;
  528. pu:=tused_unit(pu.next);
  529. end;
  530. end;
  531. {$IfDef GDB}
  532. procedure write_gdb_info;
  533. procedure reset_unit_type_info;
  534. var
  535. hp : tmodule;
  536. begin
  537. hp:=tmodule(loaded_units.first);
  538. while assigned(hp) do
  539. begin
  540. hp.is_stab_written:=false;
  541. hp:=tmodule(hp.next);
  542. end;
  543. end;
  544. procedure write_used_unit_type_info(hp:tmodule);
  545. var
  546. pu : tused_unit;
  547. begin
  548. pu:=tused_unit(hp.used_units.first);
  549. while assigned(pu) do
  550. begin
  551. if not pu.u.is_stab_written then
  552. begin
  553. { prevent infinte loop for circular dependencies }
  554. pu.u.is_stab_written:=true;
  555. { write type info from used units, use a depth first
  556. strategy to reduce the recursion in writing all
  557. dependent stabs }
  558. write_used_unit_type_info(pu.u);
  559. if assigned(pu.u.globalsymtable) then
  560. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  561. end;
  562. pu:=tused_unit(pu.next);
  563. end;
  564. end;
  565. var
  566. vardebuglist : taasmoutput;
  567. storefilepos : tfileposinfo;
  568. begin
  569. if not (cs_debuginfo in aktmoduleswitches) then
  570. exit;
  571. storefilepos:=aktfilepos;
  572. aktfilepos:=current_module.mainfilepos;
  573. { include symbol that will be referenced from the program to be sure to
  574. include this debuginfo .o file }
  575. if current_module.is_unit then
  576. begin
  577. current_module.flags:=current_module.flags or uf_has_debuginfo;
  578. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.globalsymtable,''),AT_DATA,0));
  579. end
  580. else
  581. debugList.concat(tai_symbol.Createname_global(make_mangledname('DEBUGINFO',current_module.localsymtable,''),AT_DATA,0));
  582. { first write all global/local symbols again to a temp list. This will flag
  583. all required tdefs. After that the temp list can be removed since the debuginfo is already
  584. written to the stabs when the variables/consts were written }
  585. {$warning Hack to get all needed types}
  586. vardebuglist:=taasmoutput.create;
  587. new_section(vardebuglist,sec_data,'',0);
  588. if assigned(current_module.globalsymtable) then
  589. tglobalsymtable(current_module.globalsymtable).concatstabto(vardebuglist);
  590. if assigned(current_module.localsymtable) then
  591. tstaticsymtable(current_module.localsymtable).concatstabto(vardebuglist);
  592. vardebuglist.free;
  593. { reset unit type info flag }
  594. reset_unit_type_info;
  595. { write used types from the used units }
  596. write_used_unit_type_info(current_module);
  597. { last write the types from this unit }
  598. if assigned(current_module.globalsymtable) then
  599. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  600. if assigned(current_module.localsymtable) then
  601. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  602. { include files }
  603. if (cs_gdb_dbx in aktglobalswitches) then
  604. begin
  605. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  606. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  607. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  608. debugList.concat(Tai_stabs.Create(strpnew('"'+
  609. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  610. tostr(N_EINCL)+',0,0,0')));
  611. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  612. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  613. do_count_dbx:=false;
  614. end;
  615. aktfilepos:=storefilepos;
  616. end;
  617. {$EndIf GDB}
  618. procedure reset_all_defs;
  619. procedure reset_used_unit_defs(hp:tmodule);
  620. var
  621. pu : tused_unit;
  622. begin
  623. pu:=tused_unit(hp.used_units.first);
  624. while assigned(pu) do
  625. begin
  626. if not pu.u.is_reset then
  627. begin
  628. { prevent infinte loop for circular dependencies }
  629. pu.u.is_reset:=true;
  630. if assigned(pu.u.globalsymtable) then
  631. begin
  632. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  633. reset_used_unit_defs(pu.u);
  634. end;
  635. end;
  636. pu:=tused_unit(pu.next);
  637. end;
  638. end;
  639. var
  640. hp2 : tmodule;
  641. begin
  642. hp2:=tmodule(loaded_units.first);
  643. while assigned(hp2) do
  644. begin
  645. hp2.is_reset:=false;
  646. hp2:=tmodule(hp2.next);
  647. end;
  648. reset_used_unit_defs(current_module);
  649. end;
  650. procedure free_localsymtables(st:tsymtable);
  651. var
  652. def : tstoreddef;
  653. pd : tprocdef;
  654. begin
  655. def:=tstoreddef(st.defindex.first);
  656. while assigned(def) do
  657. begin
  658. if def.deftype=procdef then
  659. begin
  660. pd:=tprocdef(def);
  661. if assigned(pd.localst) and
  662. (pd.localst.symtabletype<>staticsymtable) and
  663. not((pd.proccalloption=pocall_inline) or
  664. ((current_module.flags and uf_local_browser)<>0)) then
  665. begin
  666. free_localsymtables(pd.localst);
  667. pd.localst.free;
  668. pd.localst:=nil;
  669. end;
  670. end;
  671. def:=tstoreddef(def.indexnext);
  672. end;
  673. end;
  674. procedure parse_implementation_uses;
  675. begin
  676. if token=_USES then
  677. begin
  678. loadunits;
  679. {$ifdef DEBUG}
  680. test_symtablestack;
  681. {$endif DEBUG}
  682. end;
  683. end;
  684. procedure setupglobalswitches;
  685. begin
  686. { can't have local browser when no global browser }
  687. if (cs_local_browser in aktmoduleswitches) and
  688. not(cs_browser in aktmoduleswitches) then
  689. exclude(aktmoduleswitches,cs_local_browser);
  690. { define a symbol in delphi,objfpc,tp,gpc mode }
  691. if (m_delphi in aktmodeswitches) then
  692. current_scanner.def_macro('FPC_DELPHI')
  693. else
  694. if (m_tp7 in aktmodeswitches) then
  695. current_scanner.def_macro('FPC_TP')
  696. else
  697. if (m_objfpc in aktmodeswitches) then
  698. current_scanner.def_macro('FPC_OBJFPC')
  699. else
  700. if (m_gpc in aktmodeswitches) then
  701. current_scanner.def_macro('FPC_GPC')
  702. else
  703. if (m_mac in aktmodeswitches) then
  704. current_scanner.def_macro('FPC_MACPAS');
  705. end;
  706. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  707. var
  708. stt : tsymtable;
  709. ps : tprocsym;
  710. pd : tprocdef;
  711. begin
  712. { there should be no current_procinfo available }
  713. if assigned(current_procinfo) then
  714. internalerror(200304275);
  715. {Generate a procsym for main}
  716. make_ref:=false;
  717. { try to insert in in static symtable ! }
  718. stt:=symtablestack;
  719. symtablestack:=st;
  720. { generate procsym }
  721. ps:=tprocsym.create('$'+name);
  722. { main are allways used }
  723. inc(ps.refs);
  724. symtablestack.insert(ps);
  725. pd:=tprocdef.create(main_program_level);
  726. include(pd.procoptions,po_global);
  727. pd.procsym:=ps;
  728. ps.addprocdef(pd);
  729. { restore symtable }
  730. make_ref:=true;
  731. symtablestack:=stt;
  732. { set procdef options }
  733. pd.proctypeoption:=potype;
  734. pd.proccalloption:=pocall_default;
  735. pd.forwarddef:=false;
  736. pd.setmangledname(target_info.cprefix+name);
  737. pd.aliasnames.insert(pd.mangledname);
  738. handle_calling_convention(pd);
  739. { We don't need is a local symtable. Change it into the static
  740. symtable }
  741. pd.localst.free;
  742. pd.localst:=st;
  743. { set procinfo and current_procinfo.procdef }
  744. current_procinfo:=cprocinfo.create(nil);
  745. current_module.procinfo:=current_procinfo;
  746. current_procinfo.procdef:=pd;
  747. { return procdef }
  748. create_main_proc:=pd;
  749. { main proc does always a call e.g. to init system unit }
  750. include(current_procinfo.flags,pi_do_call);
  751. end;
  752. procedure release_main_proc(pd:tprocdef);
  753. begin
  754. { this is a main proc, so there should be no parent }
  755. if not(assigned(current_procinfo)) or
  756. assigned(current_procinfo.parent) or
  757. not(current_procinfo.procdef=pd) then
  758. internalerror(200304276);
  759. { remove procinfo }
  760. current_module.procinfo:=nil;
  761. current_procinfo.free;
  762. current_procinfo:=nil;
  763. { remove localst as it was replaced by staticsymtable }
  764. pd.localst:=nil;
  765. end;
  766. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  767. var
  768. pd : tprocdef;
  769. begin
  770. { update module flags }
  771. current_module.flags:=current_module.flags or flag;
  772. { create procdef }
  773. case flag of
  774. uf_init :
  775. begin
  776. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  777. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  778. end;
  779. uf_finalize :
  780. begin
  781. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  782. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  783. end;
  784. else
  785. internalerror(200304253);
  786. end;
  787. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  788. tcgprocinfo(current_procinfo).generate_code;
  789. release_main_proc(pd);
  790. end;
  791. procedure proc_unit;
  792. function is_assembler_generated:boolean;
  793. begin
  794. is_assembler_generated:=(Errorcount=0) and
  795. not(
  796. codeSegment.empty and
  797. dataSegment.empty and
  798. bssSegment.empty and
  799. ((importssection=nil) or importsSection.empty) and
  800. ((resourcesection=nil) or resourceSection.empty) and
  801. ((resourcestringlist=nil) or resourcestringList.empty)
  802. );
  803. end;
  804. var
  805. main_file: tinputfile;
  806. st : tsymtable;
  807. unitst : tglobalsymtable;
  808. store_crc,store_interface_crc : cardinal;
  809. s1,s2 : ^string; {Saves stack space}
  810. force_init_final : boolean;
  811. pd : tprocdef;
  812. unitname8 : string[8];
  813. has_impl: boolean;
  814. begin
  815. consume(_UNIT);
  816. if compile_level=1 then
  817. Status.IsExe:=false;
  818. if token=_ID then
  819. begin
  820. { create filenames and unit name }
  821. main_file := current_scanner.inputfile;
  822. while assigned(main_file.next) do
  823. main_file := main_file.next;
  824. new(s1);
  825. s1^:=current_module.modulename^;
  826. current_module.SetFileName(main_file.path^+main_file.name^,true);
  827. current_module.SetModuleName(orgpattern);
  828. { check for system unit }
  829. new(s2);
  830. s2^:=upper(SplitName(main_file.name^));
  831. unitname8:=copy(current_module.modulename^,1,8);
  832. if (cs_check_unit_name in aktglobalswitches) and
  833. (
  834. not(
  835. (current_module.modulename^=s2^) or
  836. (
  837. (length(current_module.modulename^)>8) and
  838. (unitname8=s2^)
  839. )
  840. )
  841. or
  842. (
  843. (length(s1^)>8) and
  844. (s1^<>current_module.modulename^)
  845. )
  846. ) then
  847. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  848. if (current_module.modulename^='SYSTEM') then
  849. include(aktmoduleswitches,cs_compilesystem);
  850. dispose(s2);
  851. dispose(s1);
  852. end;
  853. consume(_ID);
  854. consume(_SEMICOLON);
  855. consume(_INTERFACE);
  856. { global switches are read, so further changes aren't allowed }
  857. current_module.in_global:=false;
  858. { handle the global switches }
  859. setupglobalswitches;
  860. message1(unit_u_loading_interface_units,current_module.modulename^);
  861. { update status }
  862. status.currentmodule:=current_module.realmodulename^;
  863. { maybe turn off m_objpas if we are compiling objpas }
  864. if (current_module.modulename^='OBJPAS') then
  865. exclude(aktmodeswitches,m_objpas);
  866. { maybe turn off m_mac if we are compiling macpas }
  867. if (current_module.modulename^='MACPAS') then
  868. exclude(aktmodeswitches,m_mac);
  869. parse_only:=true;
  870. { generate now the global symboltable }
  871. st:=tglobalsymtable.create(current_module.modulename^);
  872. refsymtable:=st;
  873. unitst:=tglobalsymtable(st);
  874. { define first as local to overcome dependency conflicts }
  875. current_module.localsymtable:=st;
  876. { the unit name must be usable as a unit specifier }
  877. { inside the unit itself (PM) }
  878. { this also forbids to have another symbol }
  879. { with the same name as the unit }
  880. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  881. { load default units, like the system unit }
  882. loaddefaultunits;
  883. { reset }
  884. make_ref:=true;
  885. { insert qualifier for the system unit (allows system.writeln) }
  886. if not(cs_compilesystem in aktmoduleswitches) then
  887. begin
  888. if token=_USES then
  889. begin
  890. loadunits;
  891. { has it been compiled at a higher level ?}
  892. if current_module.state=ms_compiled then
  893. exit;
  894. end;
  895. { ... but insert the symbol table later }
  896. st.next:=symtablestack;
  897. symtablestack:=st;
  898. end
  899. else
  900. { while compiling a system unit, some types are directly inserted }
  901. begin
  902. st.next:=symtablestack;
  903. symtablestack:=st;
  904. insert_intern_types(st);
  905. end;
  906. { now we know the place to insert the constants }
  907. constsymtable:=symtablestack;
  908. { move the global symtab from the temporary local to global }
  909. current_module.globalsymtable:=current_module.localsymtable;
  910. current_module.localsymtable:=nil;
  911. reset_all_defs;
  912. { number all units, so we know if a unit is used by this unit or
  913. needs to be added implicitly }
  914. current_module.numberunits;
  915. { ... parse the declarations }
  916. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  917. read_interface_declarations;
  918. { leave when we got an error }
  919. if (Errorcount>0) and not status.skip_error then
  920. begin
  921. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  922. status.skip_error:=true;
  923. exit;
  924. end;
  925. { Our interface is compiled, generate CRC and switch to implementation }
  926. if not(cs_compilesystem in aktmoduleswitches) and
  927. (Errorcount=0) then
  928. tppumodule(current_module).getppucrc;
  929. current_module.in_interface:=false;
  930. current_module.interface_compiled:=true;
  931. { First reload all units depending on our interface, we need to do this
  932. in the implementation part to prevent errorneous circular references }
  933. reload_flagged_units;
  934. { Parse the implementation section }
  935. if (m_mac in aktmodeswitches) and try_to_consume(_END) then
  936. has_impl:= false
  937. else
  938. begin
  939. consume(_IMPLEMENTATION);
  940. has_impl:= true;
  941. end;
  942. if has_impl then
  943. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  944. parse_only:=false;
  945. { generates static symbol table }
  946. st:=tstaticsymtable.create(current_module.modulename^);
  947. current_module.localsymtable:=st;
  948. { remove the globalsymtable from the symtable stack }
  949. { to reinsert it after loading the implementation units }
  950. symtablestack:=unitst.next;
  951. { we don't want implementation units symbols in unitsymtable !! PM }
  952. refsymtable:=st;
  953. { Read the implementation units }
  954. if has_impl then
  955. parse_implementation_uses;
  956. if current_module.state=ms_compiled then
  957. exit;
  958. { reset ranges/stabs in exported definitions }
  959. reset_all_defs;
  960. { All units are read, now give them a number }
  961. current_module.numberunits;
  962. { now we can change refsymtable }
  963. refsymtable:=st;
  964. { but reinsert the global symtable as lasts }
  965. unitst.next:=symtablestack;
  966. symtablestack:=unitst;
  967. {$ifdef DEBUG}
  968. test_symtablestack;
  969. {$endif DEBUG}
  970. constsymtable:=symtablestack;
  971. if has_impl then
  972. begin
  973. Message1(parser_u_parsing_implementation,current_module.modulename^);
  974. if current_module.in_interface then
  975. internalerror(200212285);
  976. { Compile the unit }
  977. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  978. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  979. tcgprocinfo(current_procinfo).parse_body;
  980. tcgprocinfo(current_procinfo).generate_code;
  981. tcgprocinfo(current_procinfo).resetprocdef;
  982. { save file pos for debuginfo }
  983. current_module.mainfilepos:=current_procinfo.entrypos;
  984. release_main_proc(pd);
  985. end;
  986. { if the unit contains ansi/widestrings, initialization and
  987. finalization code must be forced }
  988. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  989. tstaticsymtable(current_module.localsymtable).needs_init_final;
  990. { should we force unit initialization? }
  991. { this is a hack, but how can it be done better ? }
  992. if force_init_final and ((current_module.flags and uf_init)=0) then
  993. gen_implicit_initfinal(uf_init,st);
  994. { finalize? }
  995. if has_impl and (token=_FINALIZATION) then
  996. begin
  997. { set module options }
  998. current_module.flags:=current_module.flags or uf_finalize;
  999. { Compile the finalize }
  1000. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1001. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1002. tcgprocinfo(current_procinfo).parse_body;
  1003. tcgprocinfo(current_procinfo).generate_code;
  1004. tcgprocinfo(current_procinfo).resetprocdef;
  1005. release_main_proc(pd);
  1006. end
  1007. else if force_init_final then
  1008. gen_implicit_initfinal(uf_finalize,st);
  1009. { the last char should always be a point }
  1010. consume(_POINT);
  1011. { Generate resoucestrings }
  1012. If ResourceStrings.ResStrCount>0 then
  1013. begin
  1014. ResourceStrings.CreateResourceStringList;
  1015. current_module.flags:=current_module.flags or uf_has_resources;
  1016. { only write if no errors found }
  1017. if (Errorcount=0) then
  1018. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1019. end;
  1020. if (Errorcount=0) then
  1021. begin
  1022. { tests, if all (interface) forwards are resolved }
  1023. tstoredsymtable(symtablestack).check_forwards;
  1024. { check if all private fields are used }
  1025. tstoredsymtable(symtablestack).allprivatesused;
  1026. { remove cross unit overloads }
  1027. tstoredsymtable(symtablestack).unchain_overloaded;
  1028. { test static symtable }
  1029. tstoredsymtable(st).allsymbolsused;
  1030. tstoredsymtable(st).allprivatesused;
  1031. tstoredsymtable(st).check_forwards;
  1032. tstoredsymtable(st).checklabels;
  1033. tstoredsymtable(st).unchain_overloaded;
  1034. { used units }
  1035. current_module.allunitsused;
  1036. end;
  1037. { leave when we got an error }
  1038. if (Errorcount>0) and not status.skip_error then
  1039. begin
  1040. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1041. status.skip_error:=true;
  1042. exit;
  1043. end;
  1044. { generate debuginfo }
  1045. {$ifdef GDB}
  1046. write_gdb_info;
  1047. {$endif GDB}
  1048. { generate a list of threadvars }
  1049. InsertThreadvars;
  1050. { generate imports }
  1051. if current_module.uses_imports then
  1052. importlib.generatelib;
  1053. { insert own objectfile, or say that it's in a library
  1054. (no check for an .o when loading) }
  1055. if is_assembler_generated then
  1056. insertobjectfile
  1057. else
  1058. current_module.flags:=current_module.flags or uf_no_link;
  1059. if cs_local_browser in aktmoduleswitches then
  1060. current_module.localsymtable:=refsymtable;
  1061. if is_assembler_generated then
  1062. begin
  1063. { create dwarf debuginfo }
  1064. create_dwarf;
  1065. { finish asmlist by adding segment starts }
  1066. insertsegment;
  1067. { assemble }
  1068. create_objectfile;
  1069. end;
  1070. { Write out the ppufile after the object file has been created }
  1071. store_interface_crc:=current_module.interface_crc;
  1072. store_crc:=current_module.crc;
  1073. if (Errorcount=0) then
  1074. tppumodule(current_module).writeppu;
  1075. if not(cs_compilesystem in aktmoduleswitches) then
  1076. if store_interface_crc<>current_module.interface_crc then
  1077. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1078. {$ifdef EXTDEBUG}
  1079. if not(cs_compilesystem in aktmoduleswitches) then
  1080. if (store_crc<>current_module.crc) and simplify_ppu then
  1081. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1082. {$endif EXTDEBUG}
  1083. { release all local symtables that are not needed anymore }
  1084. free_localsymtables(current_module.globalsymtable);
  1085. free_localsymtables(current_module.localsymtable);
  1086. { remove static symtable (=refsymtable) here to save some mem }
  1087. if not (cs_local_browser in aktmoduleswitches) then
  1088. begin
  1089. st.free;
  1090. current_module.localsymtable:=nil;
  1091. end;
  1092. { leave when we got an error }
  1093. if (Errorcount>0) and not status.skip_error then
  1094. begin
  1095. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1096. status.skip_error:=true;
  1097. exit;
  1098. end;
  1099. Message1(unit_u_finished_compiling,current_module.modulename^);
  1100. end;
  1101. procedure proc_program(islibrary : boolean);
  1102. var
  1103. main_file : tinputfile;
  1104. st : tsymtable;
  1105. hp,hp2 : tmodule;
  1106. pd : tprocdef;
  1107. begin
  1108. DLLsource:=islibrary;
  1109. Status.IsLibrary:=IsLibrary;
  1110. Status.IsExe:=true;
  1111. parse_only:=false;
  1112. { DLL defaults to create reloc info }
  1113. if islibrary then
  1114. begin
  1115. if not RelocSectionSetExplicitly then
  1116. RelocSection:=true;
  1117. end;
  1118. { relocation works only without stabs under win32 !! PM }
  1119. { internal assembler uses rva for stabs info
  1120. so it should work with relocated DLLs }
  1121. if RelocSection and
  1122. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1123. (target_info.assem<>as_i386_pecoff) then
  1124. begin
  1125. include(aktglobalswitches,cs_link_strip);
  1126. { Warning stabs info does not work with reloc section !! }
  1127. if cs_debuginfo in aktmoduleswitches then
  1128. begin
  1129. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1130. Message(parser_w_parser_win32_debug_needs_WN);
  1131. exclude(aktmoduleswitches,cs_debuginfo);
  1132. end;
  1133. end;
  1134. { get correct output names }
  1135. main_file := current_scanner.inputfile;
  1136. while assigned(main_file.next) do
  1137. main_file := main_file.next;
  1138. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1139. if islibrary then
  1140. begin
  1141. consume(_LIBRARY);
  1142. stringdispose(current_module.modulename);
  1143. stringdispose(current_module.realmodulename);
  1144. current_module.modulename:=stringdup(pattern);
  1145. current_module.realmodulename:=stringdup(orgpattern);
  1146. current_module.islibrary:=true;
  1147. exportlib.preparelib(orgpattern);
  1148. consume(_ID);
  1149. consume(_SEMICOLON);
  1150. end
  1151. else
  1152. { is there an program head ? }
  1153. if token=_PROGRAM then
  1154. begin
  1155. consume(_PROGRAM);
  1156. stringdispose(current_module.modulename);
  1157. stringdispose(current_module.realmodulename);
  1158. current_module.modulename:=stringdup(pattern);
  1159. current_module.realmodulename:=stringdup(orgpattern);
  1160. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1161. exportlib.preparelib(orgpattern);
  1162. consume(_ID);
  1163. if token=_LKLAMMER then
  1164. begin
  1165. consume(_LKLAMMER);
  1166. repeat
  1167. consume(_ID);
  1168. until not try_to_consume(_COMMA);
  1169. consume(_RKLAMMER);
  1170. end;
  1171. consume(_SEMICOLON);
  1172. end
  1173. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1174. exportlib.preparelib(current_module.realmodulename^);
  1175. { global switches are read, so further changes aren't allowed }
  1176. current_module.in_global:=false;
  1177. { setup things using the global switches }
  1178. setupglobalswitches;
  1179. { set implementation flag }
  1180. current_module.in_interface:=false;
  1181. current_module.interface_compiled:=true;
  1182. { insert after the unit symbol tables the static symbol table }
  1183. { of the program }
  1184. st:=tstaticsymtable.create(current_module.modulename^);;
  1185. current_module.localsymtable:=st;
  1186. refsymtable:=st;
  1187. { load standard units (system,objpas,profile unit) }
  1188. loaddefaultunits;
  1189. {Load the units used by the program we compile.}
  1190. if token=_USES then
  1191. loadunits;
  1192. { reset ranges/stabs in exported definitions }
  1193. reset_all_defs;
  1194. { All units are read, now give them a number }
  1195. current_module.numberunits;
  1196. {Insert the name of the main program into the symbol table.}
  1197. if current_module.realmodulename^<>'' then
  1198. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1199. { ...is also constsymtable, this is the symtable where }
  1200. { the elements of enumeration types are inserted }
  1201. constsymtable:=st;
  1202. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1203. { The program intialization needs an alias, so it can be called
  1204. from the bootstrap code.}
  1205. if islibrary or
  1206. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1207. begin
  1208. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1209. { Win32 startup code needs a single name }
  1210. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1211. pd.aliasnames.insert('PASCALMAIN');
  1212. end
  1213. else
  1214. begin
  1215. if (target_info.system = system_i386_netware) or
  1216. (target_info.system = system_i386_netwlibc) then
  1217. begin
  1218. pd:=create_main_proc('PASCALMAIN',potype_proginit,st); { main is need by the netware rtl }
  1219. end else
  1220. begin
  1221. pd:=create_main_proc('main',potype_proginit,st);
  1222. pd.aliasnames.insert('PASCALMAIN');
  1223. end;
  1224. end;
  1225. tcgprocinfo(current_procinfo).parse_body;
  1226. tcgprocinfo(current_procinfo).generate_code;
  1227. tcgprocinfo(current_procinfo).resetprocdef;
  1228. { save file pos for debuginfo }
  1229. current_module.mainfilepos:=current_procinfo.entrypos;
  1230. release_main_proc(pd);
  1231. { should we force unit initialization? }
  1232. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1233. begin
  1234. { initialize section }
  1235. gen_implicit_initfinal(uf_init,st);
  1236. { finalize section }
  1237. gen_implicit_initfinal(uf_finalize,st);
  1238. end;
  1239. { Add symbol to the exports section for win32 so smartlinking a
  1240. DLL will include the edata section }
  1241. if assigned(exportlib) and
  1242. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1243. assigned(current_module._exports.first) then
  1244. codesegment.concat(tai_const.create_sym(exportlib.edatalabel));
  1245. If ResourceStrings.ResStrCount>0 then
  1246. begin
  1247. ResourceStrings.CreateResourceStringList;
  1248. { only write if no errors found }
  1249. if (Errorcount=0) then
  1250. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1251. end;
  1252. { finalize? }
  1253. if token=_FINALIZATION then
  1254. begin
  1255. { set module options }
  1256. current_module.flags:=current_module.flags or uf_finalize;
  1257. { Compile the finalize }
  1258. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1259. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1260. tcgprocinfo(current_procinfo).parse_body;
  1261. tcgprocinfo(current_procinfo).generate_code;
  1262. tcgprocinfo(current_procinfo).resetprocdef;
  1263. release_main_proc(pd);
  1264. end;
  1265. { consume the last point }
  1266. consume(_POINT);
  1267. if (Errorcount=0) then
  1268. begin
  1269. { test static symtable }
  1270. tstoredsymtable(st).allsymbolsused;
  1271. tstoredsymtable(st).allprivatesused;
  1272. tstoredsymtable(st).check_forwards;
  1273. tstoredsymtable(st).checklabels;
  1274. tstoredsymtable(st).unchain_overloaded;
  1275. current_module.allunitsused;
  1276. end;
  1277. { leave when we got an error }
  1278. if (Errorcount>0) and not status.skip_error then
  1279. begin
  1280. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1281. status.skip_error:=true;
  1282. exit;
  1283. end;
  1284. { generate debuginfo }
  1285. {$ifdef GDB}
  1286. write_gdb_info;
  1287. {$endif GDB}
  1288. { generate a list of threadvars }
  1289. InsertThreadvars;
  1290. { generate imports }
  1291. if current_module.uses_imports then
  1292. importlib.generatelib;
  1293. if islibrary or
  1294. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1295. (target_info.system=system_i386_NETWARE) then
  1296. exportlib.generatelib;
  1297. { insert Tables and StackLength }
  1298. insertThreadVarTablesTable;
  1299. insertResourceTablesTable;
  1300. insertinitfinaltable;
  1301. insertmemorysizes;
  1302. { create dwarf debuginfo }
  1303. create_dwarf;
  1304. { finish asmlist by adding segment starts }
  1305. insertsegment;
  1306. { insert own objectfile }
  1307. insertobjectfile;
  1308. { assemble and link }
  1309. create_objectfile;
  1310. { release all local symtables that are not needed anymore }
  1311. free_localsymtables(current_module.localsymtable);
  1312. { leave when we got an error }
  1313. if (Errorcount>0) and not status.skip_error then
  1314. begin
  1315. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1316. status.skip_error:=true;
  1317. exit;
  1318. end;
  1319. { create the executable when we are at level 1 }
  1320. if (compile_level=1) then
  1321. begin
  1322. { remove all unused units, this happends when units are removed
  1323. from the uses clause in the source and the ppu was already being loaded }
  1324. hp:=tmodule(loaded_units.first);
  1325. while assigned(hp) do
  1326. begin
  1327. hp2:=hp;
  1328. hp:=tmodule(hp.next);
  1329. if hp2.is_unit and
  1330. not assigned(hp2.globalsymtable) then
  1331. loaded_units.remove(hp2);
  1332. end;
  1333. { insert all .o files from all loaded units }
  1334. hp:=tmodule(loaded_units.first);
  1335. while assigned(hp) do
  1336. begin
  1337. linker.AddModuleFiles(hp);
  1338. hp:=tmodule(hp.next);
  1339. end;
  1340. { write .def file }
  1341. if (cs_link_deffile in aktglobalswitches) then
  1342. deffile.writefile;
  1343. { finally we can create a executable }
  1344. if (not current_module.is_unit) then
  1345. begin
  1346. if DLLSource then
  1347. linker.MakeSharedLibrary
  1348. else
  1349. linker.MakeExecutable;
  1350. end;
  1351. end;
  1352. end;
  1353. end.
  1354. {
  1355. $Log$
  1356. Revision 1.177 2004-11-29 18:50:15 peter
  1357. * os2 fixes for import
  1358. * asmsymtype support for intel reader
  1359. Revision 1.176 2004/11/19 08:17:02 michael
  1360. * Split po_public into po_public and po_global (Peter)
  1361. Revision 1.175 2004/11/16 20:32:40 peter
  1362. * fixes for win32 mangledname
  1363. Revision 1.174 2004/11/15 23:35:31 peter
  1364. * tparaitem removed, use tparavarsym instead
  1365. * parameter order is now calculated from paranr value in tparavarsym
  1366. Revision 1.173 2004/11/08 22:09:59 peter
  1367. * tvarsym splitted
  1368. Revision 1.172 2004/11/05 20:04:49 florian
  1369. * THREADVARLIST is now aligned
  1370. Revision 1.171 2004/11/04 23:59:13 peter
  1371. use filepos of main when generating the module stabs
  1372. Revision 1.170 2004/11/04 17:09:54 peter
  1373. fixed debuginfo for variables in staticsymtable
  1374. Revision 1.169 2004/10/31 15:29:39 olle
  1375. + All sections get names in macos
  1376. Revision 1.168 2004/10/26 15:11:01 peter
  1377. * -Ch for heapsize added again
  1378. * __heapsize contains the heapsize
  1379. Revision 1.167 2004/10/25 15:38:41 peter
  1380. * heap and heapsize removed
  1381. * checkpointer fixes
  1382. Revision 1.166 2004/10/15 09:14:17 mazen
  1383. - remove $IFDEF DELPHI and related code
  1384. - remove $IFDEF FPCPROCVAR and related code
  1385. Revision 1.165 2004/10/04 18:26:51 peter
  1386. * debuginfo fixes
  1387. Revision 1.164 2004/09/14 16:33:46 peter
  1388. * release localsymtables when module is compiled
  1389. Revision 1.163 2004/09/04 21:18:47 armin
  1390. * target netwlibc added (libc is preferred for newer netware versions)
  1391. Revision 1.162 2004/09/03 16:12:32 armin
  1392. * dont create main for netware (only PASCALMAIN)
  1393. Revision 1.161 2004/08/16 22:52:35 olle
  1394. + Added automatic use of unit macpas under mode macpas
  1395. Revision 1.160 2004/07/06 20:23:25 peter
  1396. * remove unused and not loaded units before linking
  1397. Revision 1.159 2004/06/29 21:00:08 peter
  1398. * only enable dwarf for supported platforms
  1399. Revision 1.158 2004/06/20 08:55:30 florian
  1400. * logs truncated
  1401. Revision 1.157 2004/06/18 15:16:27 peter
  1402. * fixed debuginfo symbol
  1403. Revision 1.156 2004/06/16 20:07:09 florian
  1404. * dwarf branch merged
  1405. Revision 1.155 2004/05/23 20:56:42 peter
  1406. * initialize errorsym/errortype.def.owner to prevent crashes
  1407. Revision 1.154 2004/05/23 15:06:21 peter
  1408. * implicit_finally flag must be set in pass1
  1409. * add check whether the implicit frame is generated when expected
  1410. Revision 1.153 2004/05/19 21:16:13 peter
  1411. * add DEBUGINFO symbol to reference the .o file that includes the
  1412. stabs info for types and global/static variables
  1413. * debuginfo flag added to ppu to indicate whether debuginfo is
  1414. generated or not
  1415. }