pmodules.pas 55 KB

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