pmodules.pas 64 KB

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