pmodules.pas 53 KB

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