pmodules.pas 60 KB

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