pmodules.pas 60 KB

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