pmodules.pas 51 KB

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