pmodules.pas 61 KB

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