pmodules.pas 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  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. {$define New_GDB}
  21. interface
  22. procedure proc_unit;
  23. procedure proc_program(islibrary : boolean);
  24. implementation
  25. uses
  26. globtype,version,systems,tokens,
  27. cutils,cclasses,comphook,
  28. globals,verbose,fmodule,finput,fppu,
  29. symconst,symbase,symtype,symdef,symsym,symtable,
  30. aasmbase,aasmtai,aasmcpu,
  31. cgbase,cpuinfo,
  32. ncgutil,
  33. link,assemble,import,export,gendef,ppu,comprsrc,
  34. cresstr,cpubase,
  35. {$ifdef GDB}
  36. gdb,
  37. {$endif GDB}
  38. scanner,pbase,pexpr,psystem,psub;
  39. procedure create_objectfile;
  40. var
  41. DLLScanner : TDLLScanner;
  42. s : string;
  43. KeepShared : TStringList;
  44. begin
  45. { try to create import entries from system dlls }
  46. if target_info.DllScanSupported and
  47. (not current_module.linkOtherSharedLibs.Empty) then
  48. begin
  49. { Init DLLScanner }
  50. if assigned(CDLLScanner[target_info.system]) then
  51. DLLScanner:=CDLLScanner[target_info.system].Create
  52. else
  53. internalerror(200104121);
  54. KeepShared:=TStringList.Create;
  55. { Walk all shared libs }
  56. While not current_module.linkOtherSharedLibs.Empty do
  57. begin
  58. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  59. if not DLLScanner.scan(s) then
  60. KeepShared.Concat(s);
  61. end;
  62. DLLscanner.Free;
  63. { Recreate import section }
  64. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  65. begin
  66. if assigned(importssection)then
  67. importssection.clear
  68. else
  69. importssection:=taasmoutput.Create;
  70. importlib.generatelib;
  71. end;
  72. { Readd the not processed files }
  73. while not KeepShared.Empty do
  74. begin
  75. s:=KeepShared.GetFirst;
  76. current_module.linkOtherSharedLibs.add(s,link_allways);
  77. end;
  78. KeepShared.Free;
  79. end;
  80. { create the .s file and assemble it }
  81. GenerateAsm(false);
  82. { Also create a smartlinked version ? }
  83. if (cs_create_smart in aktmoduleswitches) then
  84. begin
  85. { regenerate the importssection for win32 }
  86. if assigned(importssection) and
  87. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  88. begin
  89. importsSection.clear;
  90. importlib.generatesmartlib;
  91. end;
  92. GenerateAsm(true);
  93. if target_asm.needar then
  94. Linker.MakeStaticLibrary;
  95. end;
  96. { resource files }
  97. CompileResourceFiles;
  98. end;
  99. procedure insertobjectfile;
  100. { Insert the used object file for this unit in the used list for this unit }
  101. begin
  102. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  103. current_module.flags:=current_module.flags or uf_static_linked;
  104. if (cs_create_smart in aktmoduleswitches) then
  105. begin
  106. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  107. current_module.flags:=current_module.flags or uf_smart_linked;
  108. end;
  109. end;
  110. procedure insertsegment;
  111. procedure fixseg(p:TAAsmoutput;sec:TSection);
  112. begin
  113. p.insert(Tai_section.Create(sec));
  114. if (cs_create_smart in aktmoduleswitches) then
  115. p.insert(Tai_cut.Create);
  116. p.concat(Tai_section.Create(sec_none));
  117. end;
  118. begin
  119. { Insert Ident of the compiler }
  120. if (not (cs_create_smart in aktmoduleswitches))
  121. {$ifndef EXTDEBUG}
  122. and (not current_module.is_unit)
  123. {$endif}
  124. then
  125. begin
  126. { align the first data }
  127. dataSegment.insert(Tai_align.Create(used_align(32,
  128. aktalignment.constalignmin,aktalignment.constalignmax)));
  129. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  130. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  131. end;
  132. { align code segment }
  133. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  134. { Insert start and end of sections }
  135. fixseg(codesegment,sec_code);
  136. fixseg(datasegment,sec_data);
  137. fixseg(bsssegment,sec_bss);
  138. { we should use .rdata section for these two no ? }
  139. { .rdata is a read only data section (PM) }
  140. fixseg(rttilist,sec_data);
  141. fixseg(consts,sec_data);
  142. if assigned(resourcestringlist) then
  143. fixseg(resourcestringlist,sec_data);
  144. {$ifdef GDB}
  145. if assigned(debuglist) then
  146. begin
  147. debugList.insert(Tai_symbol.Createname('gcc2_compiled',0));
  148. debugList.insert(Tai_symbol.Createname('fpc_compiled',0));
  149. fixseg(debuglist,sec_code);
  150. end;
  151. {$endif GDB}
  152. end;
  153. Procedure InsertResourceTablesTable;
  154. var
  155. hp : tused_unit;
  156. ResourceStringTables : taasmoutput;
  157. count : longint;
  158. begin
  159. ResourceStringTables:=TAAsmOutput.Create;
  160. count:=0;
  161. hp:=tused_unit(usedunits.first);
  162. while assigned(hp) do
  163. begin
  164. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  165. begin
  166. ResourceStringTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  167. inc(count);
  168. end;
  169. hp:=tused_unit(hp.next);
  170. end;
  171. { Add program resources, if any }
  172. If ResourceStringList<>Nil then
  173. begin
  174. ResourceStringTables.concat(Tai_const_symbol.Createname(current_module.modulename^+'_RESOURCESTRINGLIST'));
  175. Inc(Count);
  176. end;
  177. { TableCount }
  178. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  179. ResourceStringTables.insert(Tai_symbol.Createdataname_global('FPC_RESOURCESTRINGTABLES',0));
  180. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  181. { insert in data segment }
  182. if (cs_create_smart in aktmoduleswitches) then
  183. dataSegment.concat(Tai_cut.Create);
  184. dataSegment.concatlist(ResourceStringTables);
  185. ResourceStringTables.free;
  186. end;
  187. procedure InsertInitFinalTable;
  188. var
  189. hp : tused_unit;
  190. unitinits : taasmoutput;
  191. count : longint;
  192. begin
  193. unitinits:=TAAsmOutput.Create;
  194. count:=0;
  195. hp:=tused_unit(usedunits.first);
  196. while assigned(hp) do
  197. begin
  198. { call the unit init code and make it external }
  199. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  200. begin
  201. if (hp.u.flags and uf_init)<>0 then
  202. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+hp.u.modulename^))
  203. else
  204. unitinits.concat(Tai_const.Create_32bit(0));
  205. if (hp.u.flags and uf_finalize)<>0 then
  206. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+hp.u.modulename^))
  207. else
  208. unitinits.concat(Tai_const.Create_32bit(0));
  209. inc(count);
  210. end;
  211. hp:=tused_unit(hp.next);
  212. end;
  213. { Insert initialization/finalization of the program }
  214. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  215. begin
  216. if (current_module.flags and uf_init)<>0 then
  217. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+current_module.modulename^))
  218. else
  219. unitinits.concat(Tai_const.Create_32bit(0));
  220. if (current_module.flags and uf_finalize)<>0 then
  221. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+current_module.modulename^))
  222. else
  223. unitinits.concat(Tai_const.Create_32bit(0));
  224. inc(count);
  225. end;
  226. { TableCount,InitCount }
  227. unitinits.insert(Tai_const.Create_32bit(0));
  228. unitinits.insert(Tai_const.Create_32bit(count));
  229. unitinits.insert(Tai_symbol.Createdataname_global('INITFINAL',0));
  230. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  231. { insert in data segment }
  232. if (cs_create_smart in aktmoduleswitches) then
  233. dataSegment.concat(Tai_cut.Create);
  234. dataSegment.concatlist(unitinits);
  235. unitinits.free;
  236. end;
  237. procedure insertheap;
  238. begin
  239. if (cs_create_smart in aktmoduleswitches) then
  240. begin
  241. bssSegment.concat(Tai_cut.Create);
  242. dataSegment.concat(Tai_cut.Create);
  243. end;
  244. { On the Macintosh Classic M68k Architecture
  245. The Heap variable is simply a POINTER to the
  246. real HEAP. The HEAP must be set up by the RTL
  247. and must store the pointer in this value.
  248. On OS/2 the heap is also intialized by the RTL. We do
  249. not output a pointer }
  250. case target_info.system of
  251. {$ifdef x86_64}
  252. target_x86_64_linux:
  253. ;
  254. {$endif x86_64}
  255. {$ifdef i386}
  256. system_i386_OS2:
  257. ;
  258. {$endif i386}
  259. {$ifdef powerpc}
  260. system_powerpc_macos:
  261. ;
  262. {$endif powerpc}
  263. {$ifdef alpha}
  264. system_alpha_linux:
  265. ;
  266. {$endif alpha}
  267. {$ifdef m68k}
  268. system_m68k_Mac:
  269. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  270. system_m68k_PalmOS:
  271. ;
  272. {$endif m68k}
  273. {$IFDEF SPARC}
  274. system_SPARC_Linux:
  275. ;
  276. {$ENDIF SPARC}
  277. else
  278. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  279. end;
  280. {$ifdef m68k}
  281. if target_info.system<>system_m68k_PalmOS then
  282. begin
  283. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  284. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  285. end;
  286. {$else m68k}
  287. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  288. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  289. {$endif m68k}
  290. end;
  291. procedure insertstacklength;
  292. begin
  293. { stacksize can be specified and is now simulated }
  294. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  295. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  296. end;
  297. procedure loaddefaultunits;
  298. var
  299. hp : tmodule;
  300. unitsym : tunitsym;
  301. begin
  302. { are we compiling the system unit? }
  303. if (cs_compilesystem in aktmoduleswitches) then
  304. begin
  305. { create system defines }
  306. createconstdefs;
  307. { we don't need to reset anything, it's already done in parser.pas }
  308. exit;
  309. end;
  310. { insert the system unit, it is allways the first }
  311. hp:=loadunit('System','');
  312. systemunit:=tglobalsymtable(hp.globalsymtable);
  313. { it's always the first unit }
  314. systemunit.next:=nil;
  315. symtablestack:=systemunit;
  316. { add to the used units }
  317. current_module.used_units.concat(tused_unit.create(hp,true));
  318. unitsym:=tunitsym.create('System',systemunit);
  319. inc(unitsym.refs);
  320. refsymtable.insert(unitsym);
  321. { read default constant definitions }
  322. make_ref:=false;
  323. readconstdefs;
  324. make_ref:=true;
  325. { Objpas unit? }
  326. if m_objpas in aktmodeswitches then
  327. begin
  328. hp:=loadunit('ObjPas','');
  329. tsymtable(hp.globalsymtable).next:=symtablestack;
  330. symtablestack:=hp.globalsymtable;
  331. { add to the used units }
  332. current_module.used_units.concat(tused_unit.create(hp,true));
  333. unitsym:=tunitsym.create('ObjPas',hp.globalsymtable);
  334. inc(unitsym.refs);
  335. refsymtable.insert(unitsym);
  336. end;
  337. { Profile unit? Needed for go32v2 only }
  338. if (cs_profile in aktmoduleswitches) and (target_info.system=system_i386_go32v2) then
  339. begin
  340. hp:=loadunit('Profile','');
  341. tsymtable(hp.globalsymtable).next:=symtablestack;
  342. symtablestack:=hp.globalsymtable;
  343. { add to the used units }
  344. current_module.used_units.concat(tused_unit.create(hp,true));
  345. unitsym:=tunitsym.create('Profile',hp.globalsymtable);
  346. inc(unitsym.refs);
  347. refsymtable.insert(unitsym);
  348. end;
  349. { Units only required for main module }
  350. if not(current_module.is_unit) then
  351. begin
  352. { Heaptrc unit }
  353. if (cs_gdb_heaptrc in aktglobalswitches) then
  354. begin
  355. hp:=loadunit('HeapTrc','');
  356. tsymtable(hp.globalsymtable).next:=symtablestack;
  357. symtablestack:=hp.globalsymtable;
  358. { add to the used units }
  359. current_module.used_units.concat(tused_unit.create(hp,true));
  360. unitsym:=tunitsym.create('HeapTrc',hp.globalsymtable);
  361. inc(unitsym.refs);
  362. refsymtable.insert(unitsym);
  363. end;
  364. { Lineinfo unit }
  365. if (cs_gdb_lineinfo in aktglobalswitches) then
  366. begin
  367. hp:=loadunit('LineInfo','');
  368. tsymtable(hp.globalsymtable).next:=symtablestack;
  369. symtablestack:=hp.globalsymtable;
  370. { add to the used units }
  371. current_module.used_units.concat(tused_unit.create(hp,true));
  372. unitsym:=tunitsym.create('LineInfo',hp.globalsymtable);
  373. inc(unitsym.refs);
  374. refsymtable.insert(unitsym);
  375. end;
  376. end;
  377. { save default symtablestack }
  378. defaultsymtablestack:=symtablestack;
  379. end;
  380. procedure loadunits;
  381. var
  382. s,sorg : stringid;
  383. fn : string;
  384. pu,
  385. hp : tused_unit;
  386. hp2 : tmodule;
  387. hp3 : tsymtable;
  388. oldprocsym:tprocsym;
  389. oldprocdef:tprocdef;
  390. unitsym : tunitsym;
  391. begin
  392. oldprocsym:=aktprocsym;
  393. oldprocdef:=aktprocdef;
  394. consume(_USES);
  395. {$ifdef DEBUG}
  396. test_symtablestack;
  397. {$endif DEBUG}
  398. repeat
  399. s:=pattern;
  400. sorg:=orgpattern;
  401. consume(_ID);
  402. { support "<unit> in '<file>'" construct, but not for tp7 }
  403. if not(m_tp7 in aktmodeswitches) then
  404. begin
  405. if try_to_consume(_OP_IN) then
  406. fn:=get_stringconst
  407. else
  408. fn:='';
  409. end;
  410. { Give a warning if objpas is loaded }
  411. if s='OBJPAS' then
  412. Message(parser_w_no_objpas_use_mode);
  413. { check if the unit is already used }
  414. pu:=tused_unit(current_module.used_units.first);
  415. while assigned(pu) do
  416. begin
  417. if (pu.name^=s) then
  418. break;
  419. pu:=tused_unit(pu.next);
  420. end;
  421. { avoid uses of itself }
  422. if not assigned(pu) and (s<>current_module.modulename^) then
  423. begin
  424. { load the unit }
  425. hp2:=loadunit(sorg,fn);
  426. { the current module uses the unit hp2 }
  427. current_module.used_units.concat(tused_unit.create(hp2,not current_module.in_implementation));
  428. tused_unit(current_module.used_units.last).in_uses:=true;
  429. if current_module.compiled then
  430. exit;
  431. unitsym:=tunitsym.create(sorg,hp2.globalsymtable);
  432. { never claim about unused unit if
  433. there is init or finalize code PM }
  434. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  435. inc(unitsym.refs);
  436. refsymtable.insert(unitsym);
  437. end
  438. else
  439. Message1(sym_e_duplicate_id,s);
  440. if token=_COMMA then
  441. begin
  442. pattern:='';
  443. consume(_COMMA);
  444. end
  445. else
  446. break;
  447. until false;
  448. consume(_SEMICOLON);
  449. { set the symtable to systemunit so it gets reorderd correctly }
  450. symtablestack:=defaultsymtablestack;
  451. { now insert the units in the symtablestack }
  452. hp:=tused_unit(current_module.used_units.first);
  453. while assigned(hp) do
  454. begin
  455. {$IfDef GDB}
  456. if (cs_debuginfo in aktmoduleswitches) and
  457. (cs_gdb_dbx in aktglobalswitches) and
  458. not hp.is_stab_written then
  459. begin
  460. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  461. hp.is_stab_written:=true;
  462. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  463. end;
  464. {$EndIf GDB}
  465. if hp.in_uses then
  466. begin
  467. hp3:=symtablestack;
  468. while assigned(hp3) do
  469. begin
  470. { insert units only once ! }
  471. if hp.u.globalsymtable=hp3 then
  472. break;
  473. hp3:=hp3.next;
  474. { unit isn't inserted }
  475. if hp3=nil then
  476. begin
  477. tsymtable(hp.u.globalsymtable).next:=symtablestack;
  478. symtablestack:=tsymtable(hp.u.globalsymtable);
  479. {$ifdef DEBUG}
  480. test_symtablestack;
  481. {$endif DEBUG}
  482. end;
  483. end;
  484. end;
  485. hp:=tused_unit(hp.next);
  486. end;
  487. aktprocsym:=oldprocsym;
  488. aktprocdef:=oldprocdef;
  489. end;
  490. procedure write_gdb_info;
  491. {$IfDef GDB}
  492. var
  493. hp : tused_unit;
  494. begin
  495. if not (cs_debuginfo in aktmoduleswitches) then
  496. exit;
  497. if (cs_gdb_dbx in aktglobalswitches) then
  498. begin
  499. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  500. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  501. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  502. debugList.concat(Tai_stabs.Create(strpnew('"'+
  503. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  504. tostr(N_EINCL)+',0,0,0')));
  505. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  506. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  507. do_count_dbx:=false;
  508. end;
  509. { now insert the units in the symtablestack }
  510. hp:=tused_unit(current_module.used_units.first);
  511. while assigned(hp) do
  512. begin
  513. if (cs_debuginfo in aktmoduleswitches) and
  514. not hp.is_stab_written then
  515. begin
  516. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  517. hp.is_stab_written:=true;
  518. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  519. end;
  520. hp:=tused_unit(hp.next);
  521. end;
  522. if current_module.in_implementation and
  523. assigned(current_module.localsymtable) then
  524. begin
  525. { all types }
  526. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  527. { and all local symbols}
  528. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  529. end
  530. else if assigned(current_module.globalsymtable) then
  531. begin
  532. { all types }
  533. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  534. { and all local symbols}
  535. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  536. end;
  537. end;
  538. {$Else GDB}
  539. begin
  540. end;
  541. {$EndIf GDB}
  542. procedure parse_implementation_uses(symt:tsymtable);
  543. begin
  544. if token=_USES then
  545. begin
  546. loadunits;
  547. {$ifdef DEBUG}
  548. test_symtablestack;
  549. {$endif DEBUG}
  550. end;
  551. end;
  552. procedure setupglobalswitches;
  553. begin
  554. { can't have local browser when no global browser }
  555. if (cs_local_browser in aktmoduleswitches) and
  556. not(cs_browser in aktmoduleswitches) then
  557. exclude(aktmoduleswitches,cs_local_browser);
  558. { define a symbol in delphi,objfpc,tp,gpc mode }
  559. if (m_delphi in aktmodeswitches) then
  560. current_scanner.def_macro('FPC_DELPHI')
  561. else
  562. if (m_tp7 in aktmodeswitches) then
  563. current_scanner.def_macro('FPC_TP')
  564. else
  565. if (m_objfpc in aktmodeswitches) then
  566. current_scanner.def_macro('FPC_OBJFPC')
  567. else
  568. if (m_gpc in aktmodeswitches) then
  569. current_scanner.def_macro('FPC_GPC');
  570. end;
  571. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:tsymtable);
  572. var
  573. stt : tsymtable;
  574. begin
  575. {Generate a procsym for main}
  576. make_ref:=false;
  577. aktprocsym:=tprocsym.create('$'+name);
  578. { main are allways used }
  579. inc(aktprocsym.refs);
  580. {Try to insert in in static symtable ! }
  581. stt:=symtablestack;
  582. symtablestack:=st;
  583. aktprocdef:=tprocdef.create;
  584. aktprocsym.addprocdef(aktprocdef);
  585. aktprocdef.procsym:=aktprocsym;
  586. symtablestack:=stt;
  587. aktprocdef.proctypeoption:=options;
  588. aktprocdef.setmangledname(target_info.cprefix+name);
  589. aktprocdef.forwarddef:=false;
  590. make_ref:=true;
  591. { The localst is a local symtable. Change it into the static
  592. symtable }
  593. aktprocdef.localst.free;
  594. aktprocdef.localst:=st;
  595. { and insert the procsym in symtable }
  596. st.insert(aktprocsym);
  597. { set some informations about the main program }
  598. with procinfo do
  599. begin
  600. _class:=nil;
  601. para_offset:=target_info.first_parm_offset;
  602. framepointer:=FRAME_POINTER_REG;
  603. flags:=0;
  604. procdef:=aktprocdef;
  605. end;
  606. end;
  607. procedure insertLocalThreadvarsTablesTable;
  608. var
  609. hp : tused_unit;
  610. ltvTables : taasmoutput;
  611. count : longint;
  612. begin
  613. ltvTables:=TAAsmOutput.Create;
  614. count:=0;
  615. hp:=tused_unit(usedunits.first);
  616. while assigned(hp) do
  617. begin
  618. If (hp.u.flags and uf_local_threadvars)=uf_local_threadvars then
  619. begin
  620. ltvTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_$LOCALTHREADVARLIST'));
  621. inc(count);
  622. end;
  623. hp:=tused_unit(hp.next);
  624. end;
  625. { TableCount }
  626. ltvTables.insert(Tai_const.Create_32bit(count));
  627. ltvTables.insert(Tai_symbol.Createdataname_global('FPC_LOCALTHREADVARTABLES',0));
  628. ltvTables.concat(Tai_symbol_end.Createname('FPC_LOCALTHREADVARTABLES'));
  629. { insert in data segment }
  630. if (cs_create_smart in aktmoduleswitches) then
  631. dataSegment.concat(Tai_cut.Create);
  632. dataSegment.concatlist(ltvTables);
  633. ltvTables.free;
  634. if count > 0 then
  635. have_local_threadvars := true;
  636. end;
  637. procedure addToLocalThreadvarTab(p:tnamedindexitem;arg:pointer);
  638. var
  639. ltvTable : taasmoutput;
  640. begin
  641. ltvTable:=taasmoutput(arg);
  642. if (tsym(p).typ=varsym) and
  643. (vo_is_thread_var in tvarsym(p).varoptions) then
  644. begin
  645. { address of threadvar }
  646. ltvTable.concat(tai_const_symbol.createname(tvarsym(p).mangledname));
  647. { size of threadvar }
  648. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  649. end;
  650. end;
  651. procedure proc_unit;
  652. function is_assembler_generated:boolean;
  653. begin
  654. is_assembler_generated:=(Errorcount=0) and
  655. not(
  656. codeSegment.empty and
  657. dataSegment.empty and
  658. bssSegment.empty and
  659. ((importssection=nil) or importsSection.empty) and
  660. ((resourcesection=nil) or resourceSection.empty) and
  661. ((resourcestringlist=nil) or resourcestringList.empty)
  662. );
  663. end;
  664. var
  665. main_file: tinputfile;
  666. st : tsymtable;
  667. unitst : tglobalsymtable;
  668. {$ifdef GDB}
  669. pu : tused_unit;
  670. {$endif GDB}
  671. store_crc,store_interface_crc : cardinal;
  672. s2 : ^string; {Saves stack space}
  673. force_init_final : boolean;
  674. ltvTable : taasmoutput;
  675. begin
  676. consume(_UNIT);
  677. if compile_level=1 then
  678. Status.IsExe:=false;
  679. if token=_ID then
  680. begin
  681. { create filenames and unit name }
  682. main_file := current_scanner.inputfile;
  683. while assigned(main_file.next) do
  684. main_file := main_file.next;
  685. current_module.SetFileName(main_file.path^+main_file.name^,true);
  686. stringdispose(current_module.modulename);
  687. stringdispose(current_module.realmodulename);
  688. current_module.modulename:=stringdup(pattern);
  689. current_module.realmodulename:=stringdup(orgpattern);
  690. { check for system unit }
  691. new(s2);
  692. s2^:=upper(SplitName(main_file.name^));
  693. if (cs_check_unit_name in aktglobalswitches) and
  694. not((current_module.modulename^=s2^) or
  695. ((length(current_module.modulename^)>8) and
  696. (copy(current_module.modulename^,1,8)=s2^))) then
  697. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  698. if (current_module.modulename^='SYSTEM') then
  699. include(aktmoduleswitches,cs_compilesystem);
  700. dispose(s2);
  701. end;
  702. consume(_ID);
  703. consume(_SEMICOLON);
  704. consume(_INTERFACE);
  705. { global switches are read, so further changes aren't allowed }
  706. current_module.in_global:=false;
  707. { handle the global switches }
  708. setupglobalswitches;
  709. Message1(unit_u_start_parse_interface,current_module.realmodulename^);
  710. { update status }
  711. status.currentmodule:=current_module.realmodulename^;
  712. { maybe turn off m_objpas if we are compiling objpas }
  713. if (current_module.modulename^='OBJPAS') then
  714. exclude(aktmodeswitches,m_objpas);
  715. parse_only:=true;
  716. { generate now the global symboltable }
  717. st:=tglobalsymtable.create(current_module.modulename^);
  718. refsymtable:=st;
  719. unitst:=tglobalsymtable(st);
  720. { define first as local to overcome dependency conflicts }
  721. current_module.localsymtable:=st;
  722. { the unit name must be usable as a unit specifier }
  723. { inside the unit itself (PM) }
  724. { this also forbids to have another symbol }
  725. { with the same name as the unit }
  726. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  727. { load default units, like the system unit }
  728. loaddefaultunits;
  729. { reset }
  730. make_ref:=true;
  731. lexlevel:=0;
  732. { insert qualifier for the system unit (allows system.writeln) }
  733. if not(cs_compilesystem in aktmoduleswitches) then
  734. begin
  735. if token=_USES then
  736. begin
  737. loadunits;
  738. { has it been compiled at a higher level ?}
  739. if current_module.compiled then
  740. begin
  741. { this unit symtable is obsolete }
  742. { dispose(unitst,done);
  743. disposed as localsymtable !! }
  744. RestoreUnitSyms;
  745. exit;
  746. end;
  747. end;
  748. { ... but insert the symbol table later }
  749. st.next:=symtablestack;
  750. symtablestack:=st;
  751. end
  752. else
  753. { while compiling a system unit, some types are directly inserted }
  754. begin
  755. st.next:=symtablestack;
  756. symtablestack:=st;
  757. insert_intern_types(st);
  758. end;
  759. { now we know the place to insert the constants }
  760. constsymtable:=symtablestack;
  761. { move the global symtab from the temporary local to global }
  762. current_module.globalsymtable:=current_module.localsymtable;
  763. current_module.localsymtable:=nil;
  764. reset_global_defs;
  765. { number all units, so we know if a unit is used by this unit or
  766. needs to be added implicitly }
  767. current_module.numberunits;
  768. { ... parse the declarations }
  769. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  770. read_interface_declarations;
  771. { leave when we got an error }
  772. if (Errorcount>0) and not status.skip_error then
  773. begin
  774. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  775. status.skip_error:=true;
  776. exit;
  777. end;
  778. {else in inteface its somatimes necessary even if unused
  779. st^.allunitsused; }
  780. {$ifdef New_GDB}
  781. write_gdb_info;
  782. {$endIf Def New_GDB}
  783. if not(cs_compilesystem in aktmoduleswitches) then
  784. if (Errorcount=0) then
  785. tppumodule(current_module).getppucrc;
  786. { Parse the implementation section }
  787. consume(_IMPLEMENTATION);
  788. current_module.in_implementation:=true;
  789. Message1(unit_u_start_parse_implementation,current_module.modulename^);
  790. parse_only:=false;
  791. { generates static symbol table }
  792. st:=tstaticsymtable.create(current_module.modulename^);
  793. current_module.localsymtable:=st;
  794. { remove the globalsymtable from the symtable stack }
  795. { to reinsert it after loading the implementation units }
  796. symtablestack:=unitst.next;
  797. { we don't want implementation units symbols in unitsymtable !! PM }
  798. refsymtable:=st;
  799. { Read the implementation units }
  800. parse_implementation_uses(unitst);
  801. if current_module.compiled then
  802. begin
  803. RestoreUnitSyms;
  804. exit;
  805. end;
  806. { reset ranges/stabs in exported definitions }
  807. reset_global_defs;
  808. { All units are read, now give them a number }
  809. current_module.numberunits;
  810. { now we can change refsymtable }
  811. refsymtable:=st;
  812. { but reinsert the global symtable as lasts }
  813. unitst.next:=symtablestack;
  814. symtablestack:=unitst;
  815. tstoredsymtable(symtablestack).chainoperators;
  816. {$ifdef DEBUG}
  817. test_symtablestack;
  818. {$endif DEBUG}
  819. constsymtable:=symtablestack;
  820. {$ifdef Splitheap}
  821. if testsplit then
  822. begin
  823. Split_Heap;
  824. allow_special:=true;
  825. Switch_to_temp_heap;
  826. end;
  827. { it will report all crossings }
  828. allow_special:=false;
  829. {$endif Splitheap}
  830. Message1(parser_u_parsing_implementation,current_module.realmodulename^);
  831. { Compile the unit }
  832. codegen_newprocedure;
  833. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  834. aktprocdef.aliasnames.insert('INIT$$'+current_module.modulename^);
  835. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_init');
  836. compile_proc_body(true,false);
  837. codegen_doneprocedure;
  838. { avoid self recursive destructor call !! PM }
  839. aktprocdef.localst:=nil;
  840. { if the unit contains ansi/widestrings, initialization and
  841. finalization code must be forced }
  842. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  843. tstaticsymtable(current_module.localsymtable).needs_init_final;
  844. { should we force unit initialization? }
  845. { this is a hack, but how can it be done better ? }
  846. if force_init_final and ((current_module.flags and uf_init)=0) then
  847. begin
  848. current_module.flags:=current_module.flags or uf_init;
  849. { now we can insert a cut }
  850. if (cs_create_smart in aktmoduleswitches) then
  851. codeSegment.concat(Tai_cut.Create);
  852. genimplicitunitinit(codesegment);
  853. end;
  854. { finalize? }
  855. if token=_FINALIZATION then
  856. begin
  857. { set module options }
  858. current_module.flags:=current_module.flags or uf_finalize;
  859. { Compile the finalize }
  860. codegen_newprocedure;
  861. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  862. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  863. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  864. compile_proc_body(true,false);
  865. codegen_doneprocedure;
  866. end
  867. else if force_init_final then
  868. begin
  869. current_module.flags:=current_module.flags or uf_finalize;
  870. { now we can insert a cut }
  871. if (cs_create_smart in aktmoduleswitches) then
  872. codeSegment.concat(Tai_cut.Create);
  873. genimplicitunitfinal(codesegment);
  874. end;
  875. { generate a list of local threadvars }
  876. ltvTable:=TAAsmoutput.create;
  877. st.foreach_static({$ifdef FPCPROCVAR}@{$endif}addToLocalThreadvarTab,ltvTable);
  878. if ltvTable.first<>nil then
  879. begin
  880. { add begin and end of the list }
  881. ltvTable.insert(tai_symbol.createdataname_global(current_module.modulename^+'_$LOCALTHREADVARLIST',0));
  882. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  883. ltvTable.concat(tai_symbol_end.createname(current_module.modulename^+'_$LOCALTHREADVARLIST'));
  884. if (cs_create_smart in aktmoduleswitches) then
  885. dataSegment.concat(Tai_cut.Create);
  886. dataSegment.concatlist(ltvTable);
  887. current_module.flags:=current_module.flags or uf_local_threadvars;
  888. end;
  889. ltvTable.Free;
  890. { the last char should always be a point }
  891. consume(_POINT);
  892. If ResourceStrings.ResStrCount>0 then
  893. begin
  894. ResourceStrings.CreateResourceStringList;
  895. current_module.flags:=current_module.flags or uf_has_resources;
  896. { only write if no errors found }
  897. if (Errorcount=0) then
  898. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  899. end;
  900. { avoid self recursive destructor call !! PM }
  901. aktprocdef.localst:=nil;
  902. { absence does not matter here !! }
  903. aktprocdef.forwarddef:=false;
  904. { test static symtable }
  905. if (Errorcount=0) then
  906. begin
  907. tstoredsymtable(st).allsymbolsused;
  908. tstoredsymtable(st).allunitsused;
  909. tstoredsymtable(st).allprivatesused;
  910. end;
  911. { size of the static data }
  912. datasize:=st.datasize;
  913. {$ifdef GDB}
  914. { add all used definitions even for implementation}
  915. if (cs_debuginfo in aktmoduleswitches) then
  916. begin
  917. {$IfnDef New_GDB}
  918. if assigned(current_module.globalsymtable) then
  919. begin
  920. { all types }
  921. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  922. { and all local symbols}
  923. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  924. end;
  925. { all local types }
  926. tglobalsymtable(st)^.concattypestabto(debuglist);
  927. { and all local symbols}
  928. st^.concatstabto(debuglist);
  929. {$else New_GDB}
  930. write_gdb_info;
  931. {$endIf Def New_GDB}
  932. end;
  933. {$endif GDB}
  934. reset_global_defs;
  935. if (Errorcount=0) then
  936. begin
  937. { tests, if all (interface) forwards are resolved }
  938. tstoredsymtable(symtablestack).check_forwards;
  939. { check if all private fields are used }
  940. tstoredsymtable(symtablestack).allprivatesused;
  941. { remove cross unit overloads }
  942. tstoredsymtable(symtablestack).unchain_overloaded;
  943. end;
  944. current_module.in_implementation:=false;
  945. {$ifdef GDB}
  946. tglobalsymtable(symtablestack).is_stab_written:=false;
  947. {$endif GDB}
  948. { leave when we got an error }
  949. if (Errorcount>0) and not status.skip_error then
  950. begin
  951. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  952. status.skip_error:=true;
  953. exit;
  954. end;
  955. { generate imports }
  956. if current_module.uses_imports then
  957. importlib.generatelib;
  958. { insert own objectfile, or say that it's in a library
  959. (no check for an .o when loading) }
  960. if is_assembler_generated then
  961. insertobjectfile
  962. else
  963. current_module.flags:=current_module.flags or uf_no_link;
  964. if cs_local_browser in aktmoduleswitches then
  965. current_module.localsymtable:=refsymtable;
  966. {$ifdef GDB}
  967. pu:=tused_unit(usedunits.first);
  968. while assigned(pu) do
  969. begin
  970. if assigned(pu.u.globalsymtable) then
  971. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  972. pu:=tused_unit(pu.next);
  973. end;
  974. {$endif GDB}
  975. if is_assembler_generated then
  976. begin
  977. { finish asmlist by adding segment starts }
  978. insertsegment;
  979. { assemble }
  980. create_objectfile;
  981. end;
  982. { Write out the ppufile after the object file has been created }
  983. store_interface_crc:=current_module.interface_crc;
  984. store_crc:=current_module.crc;
  985. if (Errorcount=0) then
  986. tppumodule(current_module).writeppu;
  987. if not(cs_compilesystem in aktmoduleswitches) then
  988. if store_interface_crc<>current_module.interface_crc then
  989. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  990. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  991. {$ifdef EXTDEBUG}
  992. if not(cs_compilesystem in aktmoduleswitches) then
  993. if (store_crc<>current_module.crc) and simplify_ppu then
  994. Comment(V_Warning,current_module.ppufilename^+' implementation CRC changed '+
  995. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  996. {$endif EXTDEBUG}
  997. { remove static symtable (=refsymtable) here to save some mem }
  998. if not (cs_local_browser in aktmoduleswitches) then
  999. begin
  1000. st.free;
  1001. current_module.localsymtable:=nil;
  1002. end;
  1003. RestoreUnitSyms;
  1004. { leave when we got an error }
  1005. if (Errorcount>0) and not status.skip_error then
  1006. begin
  1007. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1008. status.skip_error:=true;
  1009. exit;
  1010. end;
  1011. end;
  1012. procedure proc_program(islibrary : boolean);
  1013. var
  1014. main_file: tinputfile;
  1015. st : tsymtable;
  1016. hp : tmodule;
  1017. begin
  1018. DLLsource:=islibrary;
  1019. Status.IsLibrary:=IsLibrary;
  1020. Status.IsExe:=true;
  1021. parse_only:=false;
  1022. { relocation works only without stabs under win32 !! PM }
  1023. { internal assembler uses rva for stabs info
  1024. so it should work with relocated DLLs }
  1025. if RelocSection and
  1026. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1027. (target_info.assem<>as_i386_pecoff) then
  1028. begin
  1029. include(aktglobalswitches,cs_link_strip);
  1030. { Warning stabs info does not work with reloc section !! }
  1031. if cs_debuginfo in aktmoduleswitches then
  1032. begin
  1033. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1034. Message(parser_w_parser_win32_debug_needs_WN);
  1035. exclude(aktmoduleswitches,cs_debuginfo);
  1036. end;
  1037. end;
  1038. { get correct output names }
  1039. main_file := current_scanner.inputfile;
  1040. while assigned(main_file.next) do
  1041. main_file := main_file.next;
  1042. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1043. if islibrary then
  1044. begin
  1045. consume(_LIBRARY);
  1046. stringdispose(current_module.modulename);
  1047. current_module.modulename:=stringdup(pattern);
  1048. current_module.islibrary:=true;
  1049. exportlib.preparelib(pattern);
  1050. consume(_ID);
  1051. consume(_SEMICOLON);
  1052. end
  1053. else
  1054. { is there an program head ? }
  1055. if token=_PROGRAM then
  1056. begin
  1057. consume(_PROGRAM);
  1058. stringdispose(current_module.modulename);
  1059. stringdispose(current_module.realmodulename);
  1060. current_module.modulename:=stringdup(pattern);
  1061. current_module.realmodulename:=stringdup(orgpattern);
  1062. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1063. exportlib.preparelib(pattern);
  1064. consume(_ID);
  1065. if token=_LKLAMMER then
  1066. begin
  1067. consume(_LKLAMMER);
  1068. repeat
  1069. consume(_ID);
  1070. until not try_to_consume(_COMMA);
  1071. consume(_RKLAMMER);
  1072. end;
  1073. consume(_SEMICOLON);
  1074. end
  1075. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1076. exportlib.preparelib(current_module.modulename^);
  1077. { global switches are read, so further changes aren't allowed }
  1078. current_module.in_global:=false;
  1079. { setup things using the global switches }
  1080. setupglobalswitches;
  1081. { set implementation flag }
  1082. current_module.in_implementation:=true;
  1083. { insert after the unit symbol tables the static symbol table }
  1084. { of the program }
  1085. st:=tstaticsymtable.create(current_module.modulename^);;
  1086. current_module.localsymtable:=st;
  1087. refsymtable:=st;
  1088. { load standard units (system,objpas,profile unit) }
  1089. loaddefaultunits;
  1090. { reset }
  1091. lexlevel:=0;
  1092. {Load the units used by the program we compile.}
  1093. if token=_USES then
  1094. loadunits;
  1095. tstoredsymtable(symtablestack).chainoperators;
  1096. { reset ranges/stabs in exported definitions }
  1097. reset_global_defs;
  1098. { All units are read, now give them a number }
  1099. current_module.numberunits;
  1100. {Insert the name of the main program into the symbol table.}
  1101. if current_module.realmodulename^<>'' then
  1102. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1103. { ...is also constsymtable, this is the symtable where }
  1104. { the elements of enumeration types are inserted }
  1105. constsymtable:=st;
  1106. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1107. {The program intialization needs an alias, so it can be called
  1108. from the bootstrap code.}
  1109. codegen_newprocedure;
  1110. if islibrary then
  1111. begin
  1112. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1113. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1114. { Win32 startup code needs a single name }
  1115. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1116. aktprocdef.aliasnames.insert('PASCALMAIN');
  1117. { this code is called from C so we need to save some
  1118. registers }
  1119. include(aktprocdef.procoptions,po_savestdregs);
  1120. end
  1121. else
  1122. begin
  1123. gen_main_procsym('main',potype_proginit,st);
  1124. aktprocdef.aliasnames.insert('program_init');
  1125. aktprocdef.aliasnames.insert('PASCALMAIN');
  1126. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1127. end;
  1128. insertLocalThreadvarsTablesTable;
  1129. compile_proc_body(true,false);
  1130. { should we force unit initialization? }
  1131. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1132. begin
  1133. current_module.flags:=current_module.flags or (uf_init or uf_finalize);
  1134. { Add initialize section }
  1135. if (cs_create_smart in aktmoduleswitches) then
  1136. codeSegment.concat(Tai_cut.Create);
  1137. genimplicitunitinit(codesegment);
  1138. { Add finalize section }
  1139. if (cs_create_smart in aktmoduleswitches) then
  1140. codeSegment.concat(Tai_cut.Create);
  1141. genimplicitunitfinal(codesegment);
  1142. end;
  1143. { Add symbol to the exports section for win32 so smartlinking a
  1144. DLL will include the edata section }
  1145. if assigned(exportlib) and
  1146. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1147. assigned(current_module._exports.first) then
  1148. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1149. { avoid self recursive destructor call !! PM }
  1150. aktprocdef.localst:=nil;
  1151. { consider these symbols as global ones for browser
  1152. but the typecasting of the globalsymtable with tglobalsymtable
  1153. can then lead to problems (PFV)
  1154. current_module.globalsymtable:=current_module.localsymtable;
  1155. current_module.localsymtable:=nil;}
  1156. If ResourceStrings.ResStrCount>0 then
  1157. begin
  1158. ResourceStrings.CreateResourceStringList;
  1159. { only write if no errors found }
  1160. if (Errorcount=0) then
  1161. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1162. end;
  1163. codegen_doneprocedure;
  1164. { finalize? }
  1165. if token=_FINALIZATION then
  1166. begin
  1167. { set module options }
  1168. current_module.flags:=current_module.flags or uf_finalize;
  1169. { Compile the finalize }
  1170. codegen_newprocedure;
  1171. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1172. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1173. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1174. compile_proc_body(true,false);
  1175. codegen_doneprocedure;
  1176. end;
  1177. { consume the last point }
  1178. consume(_POINT);
  1179. {$ifdef New_GDB}
  1180. write_gdb_info;
  1181. {$endIf Def New_GDB}
  1182. { leave when we got an error }
  1183. if (Errorcount>0) and not status.skip_error then
  1184. begin
  1185. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1186. status.skip_error:=true;
  1187. exit;
  1188. end;
  1189. { test static symtable }
  1190. if (Errorcount=0) then
  1191. begin
  1192. tstoredsymtable(st).allsymbolsused;
  1193. tstoredsymtable(st).allunitsused;
  1194. tstoredsymtable(st).allprivatesused;
  1195. end;
  1196. { generate imports }
  1197. if current_module.uses_imports then
  1198. importlib.generatelib;
  1199. if islibrary or
  1200. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1201. (target_info.system=system_i386_NETWARE) then
  1202. exportlib.generatelib;
  1203. { insert heap }
  1204. insertResourceTablesTable;
  1205. insertinitfinaltable;
  1206. insertheap;
  1207. insertstacklength;
  1208. datasize:=symtablestack.datasize;
  1209. { finish asmlist by adding segment starts }
  1210. insertsegment;
  1211. { insert own objectfile }
  1212. insertobjectfile;
  1213. { assemble and link }
  1214. create_objectfile;
  1215. { leave when we got an error }
  1216. if (Errorcount>0) and not status.skip_error then
  1217. begin
  1218. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1219. status.skip_error:=true;
  1220. exit;
  1221. end;
  1222. { create the executable when we are at level 1 }
  1223. if (compile_level=1) then
  1224. begin
  1225. { insert all .o files from all loaded units }
  1226. hp:=tmodule(loaded_units.first);
  1227. while assigned(hp) do
  1228. begin
  1229. linker.AddModuleFiles(hp);
  1230. hp:=tmodule(hp.next);
  1231. end;
  1232. { write .def file }
  1233. if (cs_link_deffile in aktglobalswitches) then
  1234. deffile.writefile;
  1235. { finally we can create a executable }
  1236. if (not current_module.is_unit) then
  1237. begin
  1238. if DLLSource then
  1239. linker.MakeSharedLibrary
  1240. else
  1241. linker.MakeExecutable;
  1242. end;
  1243. end;
  1244. end;
  1245. end.
  1246. {
  1247. $Log$
  1248. Revision 1.80 2002-10-06 19:41:30 peter
  1249. * Add finalization of typed consts
  1250. * Finalization of globals in the main program
  1251. Revision 1.79 2002/09/09 17:34:15 peter
  1252. * tdicationary.replace added to replace and item in a dictionary. This
  1253. is only allowed for the same name
  1254. * varsyms are inserted in symtable before the types are parsed. This
  1255. fixes the long standing "var longint : longint" bug
  1256. - consume_idlist and idstringlist removed. The loops are inserted
  1257. at the callers place and uses the symtable for duplicate id checking
  1258. Revision 1.78 2002/09/07 15:25:07 peter
  1259. * old logs removed and tabs fixed
  1260. Revision 1.77 2002/09/03 16:26:27 daniel
  1261. * Make Tprocdef.defs protected
  1262. Revision 1.76 2002/09/02 18:46:26 peter
  1263. * insert PASCALMAIN in library for Win32 only
  1264. Revision 1.75 2002/08/31 15:59:30 florian
  1265. + HEAP* stuff must be generated for Linux/PPC as well
  1266. + direct assembler reader searches now global and static symtables as well
  1267. Revision 1.74 2002/08/25 19:25:20 peter
  1268. * sym.insert_in_data removed
  1269. * symtable.insertvardata/insertconstdata added
  1270. * removed insert_in_data call from symtable.insert, it needs to be
  1271. called separatly. This allows to deref the address calculation
  1272. * procedures now calculate the parast addresses after the procedure
  1273. directives are parsed. This fixes the cdecl parast problem
  1274. * push_addr_param has an extra argument that specifies if cdecl is used
  1275. or not
  1276. Revision 1.73 2002/08/18 20:06:25 peter
  1277. * inlining is now also allowed in interface
  1278. * renamed write/load to ppuwrite/ppuload
  1279. * tnode storing in ppu
  1280. * nld,ncon,nbas are already updated for storing in ppu
  1281. Revision 1.72 2002/08/17 09:23:39 florian
  1282. * first part of procinfo rewrite
  1283. Revision 1.71 2002/08/11 13:24:12 peter
  1284. * saving of asmsymbols in ppu supported
  1285. * asmsymbollist global is removed and moved into a new class
  1286. tasmlibrarydata that will hold the info of a .a file which
  1287. corresponds with a single module. Added librarydata to tmodule
  1288. to keep the library info stored for the module. In the future the
  1289. objectfiles will also be stored to the tasmlibrarydata class
  1290. * all getlabel/newasmsymbol and friends are moved to the new class
  1291. Revision 1.70 2002/08/10 14:46:29 carl
  1292. + moved target_cpu_string to cpuinfo
  1293. * renamed asmmode enum.
  1294. * assembler reader has now less ifdef's
  1295. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1296. Revision 1.69 2002/07/26 21:15:41 florian
  1297. * rewrote the system handling
  1298. Revision 1.68 2002/07/04 20:43:01 florian
  1299. * first x86-64 patches
  1300. Revision 1.67 2002/07/01 18:46:25 peter
  1301. * internal linker
  1302. * reorganized aasm layer
  1303. Revision 1.66 2002/05/16 19:46:43 carl
  1304. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1305. + try to fix temp allocation (still in ifdef)
  1306. + generic constructor calls
  1307. + start of tassembler / tmodulebase class cleanup
  1308. Revision 1.65 2002/05/14 19:34:49 peter
  1309. * removed old logs and updated copyright year
  1310. Revision 1.64 2002/05/12 16:53:09 peter
  1311. * moved entry and exitcode to ncgutil and cgobj
  1312. * foreach gets extra argument for passing local data to the
  1313. iterator function
  1314. * -CR checks also class typecasts at runtime by changing them
  1315. into as
  1316. * fixed compiler to cycle with the -CR option
  1317. * fixed stabs with elf writer, finally the global variables can
  1318. be watched
  1319. * removed a lot of routines from cga unit and replaced them by
  1320. calls to cgobj
  1321. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1322. u32bit then the other is typecasted also to u32bit without giving
  1323. a rangecheck warning/error.
  1324. * fixed pascal calling method with reversing also the high tree in
  1325. the parast, detected by tcalcst3 test
  1326. Revision 1.63 2002/05/06 19:54:50 carl
  1327. + added more patches from Mazen for SPARC port
  1328. Revision 1.62 2002/04/20 21:32:24 carl
  1329. + generic FPC_CHECKPOINTER
  1330. + first parameter offset in stack now portable
  1331. * rename some constants
  1332. + move some cpu stuff to other units
  1333. - remove unused constents
  1334. * fix stacksize for some targets
  1335. * fix generic size problems which depend now on EXTEND_SIZE constant
  1336. Revision 1.61 2002/04/19 15:46:02 peter
  1337. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1338. in most cases and not written to the ppu
  1339. * add mangeledname_prefix() routine to generate the prefix of
  1340. manglednames depending on the current procedure, object and module
  1341. * removed static procprefix since the mangledname is now build only
  1342. on demand from tprocdef.mangledname
  1343. Revision 1.60 2002/04/14 16:53:10 carl
  1344. + align code section and data section according to alignment rules
  1345. Revision 1.59 2002/04/07 17:58:38 carl
  1346. + generic stack checking
  1347. Revision 1.58 2002/04/04 19:06:03 peter
  1348. * removed unused units
  1349. * use tlocation.size in cg.a_*loc*() routines
  1350. }