pmodules.pas 54 KB

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