pmodules.pas 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {$i defines.inc}
  20. { close old_current_ppu on system that are
  21. short on file handles like DOS system PM }
  22. {$ifdef GO32V1}
  23. {$define SHORT_ON_FILE_HANDLES}
  24. {$endif GO32V1}
  25. {$ifdef GO32V2}
  26. {$define SHORT_ON_FILE_HANDLES}
  27. {$endif GO32V2}
  28. {$define New_GDB}
  29. interface
  30. procedure proc_unit;
  31. procedure proc_program(islibrary : boolean);
  32. implementation
  33. uses
  34. globtype,version,systems,tokens,
  35. cutils,cobjects,comphook,
  36. globals,verbose,fmodule,finput,
  37. symconst,symbase,symppu,symdef,symsym,symtable,aasm,types,
  38. {$ifdef newcg}
  39. cgbase,
  40. {$else newcg}
  41. hcodegen,
  42. {$ifdef i386}
  43. cgai386,
  44. {$ifndef NOTARGETWIN32}
  45. t_win32,
  46. {$endif}
  47. {$endif i386}
  48. {$endif newcg}
  49. link,assemble,import,export,gendef,ppu,comprsrc,
  50. cresstr,cpubase,cpuasm,
  51. {$ifdef GDB}
  52. gdb,
  53. {$endif GDB}
  54. scanner,pbase,psystem,psub,parser;
  55. procedure create_objectfile;
  56. var
  57. DLLScanner : TDLLScanner;
  58. s : string;
  59. begin
  60. { try to create import entries from system dlls }
  61. if target_info.DllScanSupported and
  62. (not current_module.linkOtherSharedLibs.Empty) then
  63. begin
  64. { Init DLLScanner }
  65. case target_info.target of
  66. {$ifdef i386}
  67. {$ifndef NOTARGETWIN32}
  68. target_i386_win32 :
  69. DLLScanner:=tDLLscannerWin32.create;
  70. {$endif}
  71. {$endif}
  72. else
  73. internalerror(769795413);
  74. end;
  75. { Walk all shared libs }
  76. While not current_module.linkOtherSharedLibs.Empty do
  77. begin
  78. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  79. DLLScanner.scan(s)
  80. end;
  81. DLLscanner.Free;
  82. { Recreate import section }
  83. if (target_info.target=target_i386_win32) then
  84. begin
  85. if assigned(importssection)then
  86. importssection.clear
  87. else
  88. importssection:=taasmoutput.Create;
  89. importlib.generatelib;
  90. end;
  91. end;
  92. { create the .s file and assemble it }
  93. GenerateAsm(false);
  94. { Also create a smartlinked version ? }
  95. if (cs_create_smart in aktmoduleswitches) then
  96. begin
  97. { regenerate the importssection for win32 }
  98. if assigned(importssection) and
  99. (target_info.target=target_i386_win32) then
  100. begin
  101. importsSection.clear;
  102. importlib.generatesmartlib;
  103. end;
  104. GenerateAsm(true);
  105. if target_asm.needar then
  106. Linker.MakeStaticLibrary;
  107. end;
  108. { resource files }
  109. CompileResourceFiles;
  110. end;
  111. procedure insertobjectfile;
  112. { Insert the used object file for this unit in the used list for this unit }
  113. begin
  114. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  115. current_module.flags:=current_module.flags or uf_static_linked;
  116. if (cs_create_smart in aktmoduleswitches) then
  117. begin
  118. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  119. current_module.flags:=current_module.flags or uf_smart_linked;
  120. end;
  121. end;
  122. procedure insertsegment;
  123. procedure fixseg(p:TAAsmoutput;sec:tsection);
  124. begin
  125. p.insert(Tai_section.Create(sec));
  126. if (cs_create_smart in aktmoduleswitches) then
  127. p.insert(Tai_cut.Create);
  128. p.concat(Tai_section.Create(sec_none));
  129. end;
  130. begin
  131. { Insert Ident of the compiler }
  132. if (not (cs_create_smart in aktmoduleswitches))
  133. {$ifndef EXTDEBUG}
  134. and (not current_module.is_unit)
  135. {$endif}
  136. then
  137. begin
  138. dataSegment.insert(Tai_align.Create(4));
  139. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  140. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.short_name));
  141. end;
  142. { finish codesegment }
  143. codeSegment.concat(Tai_align.Create(16));
  144. { Insert start and end of sections }
  145. fixseg(codesegment,sec_code);
  146. fixseg(datasegment,sec_data);
  147. fixseg(bsssegment,sec_bss);
  148. { we should use .rdata section for these two no ? }
  149. { .rdata is a read only data section (PM) }
  150. fixseg(rttilist,sec_data);
  151. fixseg(consts,sec_data);
  152. if assigned(resourcestringlist) then
  153. fixseg(resourcestringlist,sec_data);
  154. {$ifdef GDB}
  155. if assigned(debuglist) then
  156. begin
  157. debugList.insert(Tai_symbol.Createname('gcc2_compiled',0));
  158. debugList.insert(Tai_symbol.Createname('fpc_compiled',0));
  159. fixseg(debuglist,sec_code);
  160. end;
  161. {$endif GDB}
  162. end;
  163. Procedure InsertResourceTablesTable;
  164. var
  165. hp : tused_unit;
  166. ResourceStringTables : taasmoutput;
  167. count : longint;
  168. begin
  169. ResourceStringTables:=TAAsmOutput.Create;
  170. count:=0;
  171. hp:=tused_unit(usedunits.first);
  172. while assigned(hp) do
  173. begin
  174. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  175. begin
  176. ResourceStringTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  177. inc(count);
  178. end;
  179. hp:=tused_unit(hp.next);
  180. end;
  181. { Add program resources, if any }
  182. If ResourceStringList<>Nil then
  183. begin
  184. ResourceStringTables.concat(Tai_const_symbol.Createname(current_module.modulename^+'_RESOURCESTRINGLIST'));
  185. Inc(Count);
  186. end;
  187. { TableCount }
  188. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  189. ResourceStringTables.insert(Tai_symbol.Createdataname_global('FPC_RESOURCESTRINGTABLES',0));
  190. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  191. { insert in data segment }
  192. if (cs_create_smart in aktmoduleswitches) then
  193. dataSegment.concat(Tai_cut.Create);
  194. dataSegment.concatlist(ResourceStringTables);
  195. ResourceStringTables.free;
  196. end;
  197. procedure InsertInitFinalTable;
  198. var
  199. hp : tused_unit;
  200. unitinits : taasmoutput;
  201. count : longint;
  202. begin
  203. unitinits:=TAAsmOutput.Create;
  204. count:=0;
  205. hp:=tused_unit(usedunits.first);
  206. while assigned(hp) do
  207. begin
  208. { call the unit init code and make it external }
  209. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  210. begin
  211. if (hp.u.flags and uf_init)<>0 then
  212. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+hp.u.modulename^))
  213. else
  214. unitinits.concat(Tai_const.Create_32bit(0));
  215. if (hp.u.flags and uf_finalize)<>0 then
  216. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+hp.u.modulename^))
  217. else
  218. unitinits.concat(Tai_const.Create_32bit(0));
  219. inc(count);
  220. end;
  221. hp:=tused_unit(hp.next);
  222. end;
  223. if current_module.islibrary then
  224. if (current_module.flags and uf_finalize)<>0 then
  225. begin
  226. { INIT code is done by PASCALMAIN calling }
  227. unitinits.concat(Tai_const.Create_32bit(0));
  228. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+current_module.modulename^));
  229. inc(count);
  230. end;
  231. { TableCount,InitCount }
  232. unitinits.insert(Tai_const.Create_32bit(0));
  233. unitinits.insert(Tai_const.Create_32bit(count));
  234. unitinits.insert(Tai_symbol.Createdataname_global('INITFINAL',0));
  235. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  236. { insert in data segment }
  237. if (cs_create_smart in aktmoduleswitches) then
  238. dataSegment.concat(Tai_cut.Create);
  239. dataSegment.concatlist(unitinits);
  240. unitinits.free;
  241. end;
  242. procedure insertheap;
  243. begin
  244. if (cs_create_smart in aktmoduleswitches) then
  245. begin
  246. bssSegment.concat(Tai_cut.Create);
  247. dataSegment.concat(Tai_cut.Create);
  248. end;
  249. { On the Macintosh Classic M68k Architecture
  250. The Heap variable is simply a POINTER to the
  251. real HEAP. The HEAP must be set up by the RTL
  252. and must store the pointer in this value.
  253. On OS/2 the heap is also intialized by the RTL. We do
  254. not output a pointer }
  255. case target_info.target of
  256. {$ifdef i386}
  257. target_i386_OS2:
  258. ;
  259. {$endif i386}
  260. {$ifdef alpha}
  261. target_alpha_linux:
  262. ;
  263. {$endif alpha}
  264. {$ifdef powerpc}
  265. target_powerpc_linux:
  266. ;
  267. {$endif powerpc}
  268. {$ifdef m68k}
  269. target_m68k_Mac:
  270. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  271. target_m68k_PalmOS:
  272. ;
  273. {$endif m68k}
  274. else
  275. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  276. end;
  277. {$ifdef m68k}
  278. if target_info.target<>target_m68k_PalmOS then
  279. begin
  280. dataSegment.concat(Tai_symbol.Createdataname_global('HEAP_SIZE',0));
  281. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  282. end;
  283. {$else m68k}
  284. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  285. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  286. {$endif m68k}
  287. end;
  288. procedure inserttargetspecific;
  289. begin
  290. case target_info.target of
  291. {$ifdef alpha}
  292. target_alpha_linux:
  293. ;
  294. {$endif alpha}
  295. {$ifdef powerpc}
  296. target_powerpc_linux:
  297. ;
  298. {$endif powerpc}
  299. {$ifdef i386}
  300. target_i386_GO32V2 :
  301. begin
  302. { stacksize can be specified }
  303. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  304. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  305. end;
  306. {$endif i386}
  307. {$ifdef m68k}
  308. target_m68k_Atari :
  309. begin
  310. { stacksize can be specified }
  311. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  312. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  313. end;
  314. {$endif m68k}
  315. end;
  316. end;
  317. function loadunit(const s : string;compile_system:boolean) : tmodule;forward;
  318. procedure load_usedunits(compile_system:boolean);
  319. var
  320. pu : tused_unit;
  321. loaded_unit : tmodule;
  322. load_refs : boolean;
  323. nextmapentry : longint;
  324. begin
  325. load_refs:=true;
  326. { init the map }
  327. new(current_module.map);
  328. fillchar(current_module.map^,sizeof(tunitmap),#0);
  329. {$ifdef NEWMAP}
  330. current_module.map^[0]:=current_module;
  331. {$endif NEWMAP}
  332. nextmapentry:=1;
  333. { load the used units from interface }
  334. current_module.in_implementation:=false;
  335. pu:=tused_unit(current_module.used_units.first);
  336. while assigned(pu) do
  337. begin
  338. if (not pu.loaded) and (pu.in_interface) then
  339. begin
  340. loaded_unit:=loadunit(pu.name^,false);
  341. if current_module.compiled then
  342. exit;
  343. { register unit in used units }
  344. pu.u:=loaded_unit;
  345. pu.loaded:=true;
  346. { doubles are not important for that list PM }
  347. pu.u.dependent_units.concat(tdependent_unit.create(current_module));
  348. { need to recompile the current unit ? }
  349. if loaded_unit.crc<>pu.checksum then
  350. begin
  351. Message2(unit_u_recompile_crc_change,current_module.modulename^,pu.name^);
  352. current_module.recompile_reason:=rr_crcchanged;
  353. current_module.do_compile:=true;
  354. dispose(current_module.map);
  355. current_module.map:=nil;
  356. exit;
  357. end;
  358. { setup the map entry for deref }
  359. {$ifndef NEWMAP}
  360. current_module.map^[nextmapentry]:=loaded_unit.globalsymtable;
  361. {$else NEWMAP}
  362. current_module.map^[nextmapentry]:=loaded_unit;
  363. {$endif NEWMAP}
  364. inc(nextmapentry);
  365. if nextmapentry>maxunits then
  366. Message(unit_f_too_much_units);
  367. end;
  368. pu:=tused_unit(pu.next);
  369. end;
  370. { ok, now load the unit }
  371. current_module.globalsymtable:=new(punitsymtable,loadasunit);
  372. { now only read the implementation part }
  373. current_module.in_implementation:=true;
  374. { load the used units from implementation }
  375. pu:=tused_unit(current_module.used_units.first);
  376. while assigned(pu) do
  377. begin
  378. if (not pu.loaded) and (not pu.in_interface) then
  379. begin
  380. loaded_unit:=loadunit(pu.name^,false);
  381. if current_module.compiled then
  382. exit;
  383. { register unit in used units }
  384. pu.u:=loaded_unit;
  385. pu.loaded:=true;
  386. { need to recompile the current unit ? }
  387. if (loaded_unit.interface_crc<>pu.interface_checksum) {and
  388. not(current_module.in_second_compile) } then
  389. begin
  390. Message2(unit_u_recompile_crc_change,current_module.modulename^,pu.name^+' {impl}');
  391. current_module.recompile_reason:=rr_crcchanged;
  392. current_module.do_compile:=true;
  393. dispose(current_module.map);
  394. current_module.map:=nil;
  395. exit;
  396. end;
  397. { setup the map entry for deref }
  398. {$ifndef NEWMAP}
  399. current_module.map^[nextmapentry]:=loaded_unit.globalsymtable;
  400. {$else NEWMAP}
  401. current_module.map^[nextmapentry]:=loaded_unit;
  402. {$endif NEWMAP}
  403. inc(nextmapentry);
  404. if nextmapentry>maxunits then
  405. Message(unit_f_too_much_units);
  406. end;
  407. pu:=tused_unit(pu.next);
  408. end;
  409. { load browser info if stored }
  410. if ((current_module.flags and uf_has_browser)<>0) and load_refs then
  411. punitsymtable(current_module.globalsymtable)^.load_symtable_refs;
  412. { remove the map, it's not needed anymore }
  413. dispose(current_module.map);
  414. current_module.map:=nil;
  415. end;
  416. function loadunit(const s : string;compile_system:boolean) : tmodule;
  417. const
  418. ImplIntf : array[boolean] of string[15]=('interface','implementation');
  419. var
  420. st : punitsymtable;
  421. second_time : boolean;
  422. old_current_ppu : pppufile;
  423. old_current_module,hp,hp2 : tmodule;
  424. name : string;{ necessary because current_module.mainsource^ is reset in compile !! }
  425. scanner : pscannerfile;
  426. procedure loadppufile;
  427. begin
  428. { load interface section }
  429. if not current_module.do_compile then
  430. load_interface;
  431. { only load units when we don't recompile }
  432. if not current_module.do_compile then
  433. load_usedunits(compile_system);
  434. { recompile if set }
  435. if current_module.do_compile then
  436. begin
  437. { we don't need the ppufile anymore }
  438. if assigned(current_module.ppufile) then
  439. begin
  440. dispose(current_module.ppufile,done);
  441. current_module.ppufile:=nil;
  442. current_ppu:=nil;
  443. end;
  444. { recompile the unit or give a fatal error if sources not available }
  445. if not(current_module.sources_avail) and
  446. not(current_module.sources_checked) then
  447. if (not current_module.search_unit(current_module.modulename^,true))
  448. and (length(current_module.modulename^)>8) then
  449. current_module.search_unit(copy(current_module.modulename^,1,8),true);
  450. if not(current_module.sources_avail) then
  451. begin
  452. hp:=current_module;
  453. current_module:=old_current_module;
  454. if hp.recompile_reason=rr_noppu then
  455. Message1(unit_f_cant_find_ppu,hp.modulename^)
  456. else
  457. Message1(unit_f_cant_compile_unit,hp.modulename^);
  458. current_module:=hp;
  459. end
  460. else
  461. begin
  462. if current_module.in_compile then
  463. begin
  464. current_module.in_second_compile:=true;
  465. Message1(parser_d_compiling_second_time,current_module.modulename^);
  466. end;
  467. current_scanner^.tempcloseinputfile;
  468. name:=current_module.mainsource^;
  469. if assigned(scanner) then
  470. scanner^.invalid:=true;
  471. compile(name,compile_system);
  472. current_module.in_second_compile:=false;
  473. if (not current_scanner^.invalid) then
  474. current_scanner^.tempopeninputfile;
  475. end;
  476. end
  477. else
  478. begin
  479. { only reassemble ? }
  480. if (current_module.do_assemble) then
  481. OnlyAsm;
  482. end;
  483. if assigned(current_module.ppufile) then
  484. begin
  485. dispose(current_module.ppufile,done);
  486. current_module.ppufile:=nil;
  487. current_ppu:=nil;
  488. end;
  489. end;
  490. var
  491. dummy : tmodule;
  492. begin
  493. old_current_module:=current_module;
  494. old_current_ppu:=current_ppu;
  495. { Info }
  496. Message3(unit_u_load_unit,current_module.modulename^,ImplIntf[current_module.in_implementation],s);
  497. { unit not found }
  498. st:=nil;
  499. dummy:=nil;
  500. { search all loaded units }
  501. hp:=tmodule(loaded_units.first);
  502. while assigned(hp) do
  503. begin
  504. if hp.modulename^=s then
  505. begin
  506. { forced to reload ? }
  507. if hp.do_reload then
  508. begin
  509. hp.do_reload:=false;
  510. break;
  511. end;
  512. { the unit is already registered }
  513. { and this means that the unit }
  514. { is already compiled }
  515. { else there is a cyclic unit use }
  516. if assigned(hp.globalsymtable) then
  517. st:=punitsymtable(hp.globalsymtable)
  518. else
  519. begin
  520. { both units in interface ? }
  521. if (not current_module.in_implementation) and (not hp.in_implementation) then
  522. begin
  523. { check for a cycle }
  524. hp2:=current_module.loaded_from;
  525. while assigned(hp2) and (hp2<>hp) do
  526. begin
  527. if hp2.in_implementation then
  528. hp2:=nil
  529. else
  530. hp2:=hp2.loaded_from;
  531. end;
  532. if assigned(hp2) then
  533. Message2(unit_f_circular_unit_reference,current_module.modulename^,hp.modulename^);
  534. end;
  535. end;
  536. break;
  537. end
  538. else if copy(hp.modulename^,1,8)=s then
  539. dummy:=hp;
  540. { the next unit }
  541. hp:=tmodule(hp.next);
  542. end;
  543. if assigned(dummy) and not assigned(hp) then
  544. Message2(unit_w_unit_name_error,s,dummy.modulename^);
  545. { the unit is not in the symtable stack }
  546. if (not assigned(st)) then
  547. begin
  548. if assigned(hp) then
  549. begin
  550. { remove the old unit }
  551. loaded_units.remove(hp);
  552. scanner:=hp.scanner;
  553. hp.reset;
  554. hp.scanner:=scanner;
  555. { try to reopen ppu }
  556. hp.search_unit(s,false);
  557. { try to load the unit a second time first }
  558. current_module:=hp;
  559. current_module.in_second_load:=true;
  560. Message1(unit_u_second_load_unit,current_module.modulename^);
  561. second_time:=true;
  562. end
  563. else
  564. { generates a new unit info record }
  565. begin
  566. current_module:=tmodule.create(s,true);
  567. scanner:=nil;
  568. second_time:=false;
  569. end;
  570. current_ppu:=current_module.ppufile;
  571. { close old_current_ppu on system that are
  572. short on file handles like DOS PM }
  573. {$ifdef SHORT_ON_FILE_HANDLES}
  574. if assigned(old_current_ppu) then
  575. old_current_ppu^.tempclose;
  576. {$endif SHORT_ON_FILE_HANDLES}
  577. { now we can register the unit }
  578. current_module.loaded_from:=old_current_module;
  579. loaded_units.insert(current_module);
  580. { now realy load the ppu }
  581. loadppufile;
  582. { set compiled flag }
  583. current_module.compiled:=true;
  584. { load return pointer }
  585. hp:=current_module;
  586. { for a second_time recompile reload all dependent units,
  587. for a first time compile register the unit _once_ }
  588. if second_time then
  589. begin
  590. { now reload all dependent units }
  591. hp2:=tmodule(loaded_units.first);
  592. while assigned(hp2) do
  593. begin
  594. if hp2.do_reload then
  595. dummy:=loadunit(hp2.modulename^,false);
  596. hp2:=tmodule(hp2.next);
  597. end;
  598. end
  599. else
  600. usedunits.concat(tused_unit.create(current_module,true));
  601. end;
  602. { set the old module }
  603. {$ifdef SHORT_ON_FILE_HANDLES}
  604. if assigned(old_current_ppu) then
  605. old_current_ppu^.tempopen;
  606. {$endif SHORT_ON_FILE_HANDLES}
  607. current_ppu:=old_current_ppu;
  608. current_module:=old_current_module;
  609. { we are back }
  610. SetCompileModule(current_module);
  611. loadunit:=hp;
  612. end;
  613. procedure loaddefaultunits;
  614. var
  615. hp : tmodule;
  616. unitsym : punitsym;
  617. begin
  618. { are we compiling the system unit? }
  619. if (cs_compilesystem in aktmoduleswitches) then
  620. begin
  621. { create system defines }
  622. createconstdefs;
  623. { we don't need to reset anything, it's already done in parser.pas }
  624. exit;
  625. end;
  626. { insert the system unit, it is allways the first }
  627. hp:=loadunit('SYSTEM',true);
  628. systemunit:=hp.globalsymtable;
  629. { it's always the first unit }
  630. systemunit^.next:=nil;
  631. symtablestack:=systemunit;
  632. { add to the used units }
  633. current_module.used_units.concat(tused_unit.create(hp,true));
  634. unitsym:=new(punitsym,init('System',systemunit));
  635. inc(unitsym^.refs);
  636. refsymtable^.insert(unitsym);
  637. { read default constant definitions }
  638. make_ref:=false;
  639. readconstdefs;
  640. { if POWER is defined in the RTL then use it for starstar overloading }
  641. {$ifdef DONOTCHAINOPERATORS}
  642. getsym('POWER',false);
  643. {$endif DONOTCHAINOPERATORS}
  644. make_ref:=true;
  645. {$ifdef DONOTCHAINOPERATORS}
  646. { Code now in chainoperators PM }
  647. if assigned(srsym) and (srsym^.typ=procsym) and (overloaded_operators[_STARSTAR]=nil) then
  648. overloaded_operators[_STARSTAR]:=pprocsym(srsym);
  649. {$endif DONOTCHAINOPERATORS}
  650. { Objpas unit? }
  651. if m_objpas in aktmodeswitches then
  652. begin
  653. hp:=loadunit('ObjPas',false);
  654. psymtable(hp.globalsymtable)^.next:=symtablestack;
  655. symtablestack:=hp.globalsymtable;
  656. { add to the used units }
  657. current_module.used_units.concat(tused_unit.create(hp,true));
  658. unitsym:=new(punitsym,init('ObjPas',hp.globalsymtable));
  659. inc(unitsym^.refs);
  660. refsymtable^.insert(unitsym);
  661. end;
  662. { Profile unit? Needed for go32v2 only }
  663. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  664. begin
  665. hp:=loadunit('Profile',false);
  666. psymtable(hp.globalsymtable)^.next:=symtablestack;
  667. symtablestack:=hp.globalsymtable;
  668. { add to the used units }
  669. current_module.used_units.concat(tused_unit.create(hp,true));
  670. unitsym:=new(punitsym,init('Profile',hp.globalsymtable));
  671. inc(unitsym^.refs);
  672. refsymtable^.insert(unitsym);
  673. end;
  674. { Units only required for main module }
  675. if not(current_module.is_unit) then
  676. begin
  677. { Heaptrc unit }
  678. if (cs_gdb_heaptrc in aktglobalswitches) then
  679. begin
  680. hp:=loadunit('HeapTrc',false);
  681. psymtable(hp.globalsymtable)^.next:=symtablestack;
  682. symtablestack:=hp.globalsymtable;
  683. { add to the used units }
  684. current_module.used_units.concat(tused_unit.create(hp,true));
  685. unitsym:=new(punitsym,init('HeapTrc',hp.globalsymtable));
  686. inc(unitsym^.refs);
  687. refsymtable^.insert(unitsym);
  688. end;
  689. { Lineinfo unit }
  690. if (cs_gdb_lineinfo in aktglobalswitches) then
  691. begin
  692. hp:=loadunit('LineInfo',false);
  693. psymtable(hp.globalsymtable)^.next:=symtablestack;
  694. symtablestack:=hp.globalsymtable;
  695. { add to the used units }
  696. current_module.used_units.concat(tused_unit.create(hp,true));
  697. unitsym:=new(punitsym,init('LineInfo',hp.globalsymtable));
  698. inc(unitsym^.refs);
  699. refsymtable^.insert(unitsym);
  700. end;
  701. end;
  702. { save default symtablestack }
  703. defaultsymtablestack:=symtablestack;
  704. end;
  705. procedure loadunits;
  706. var
  707. s,sorg : stringid;
  708. pu,
  709. hp : tused_unit;
  710. hp2 : tmodule;
  711. hp3 : psymtable;
  712. oldprocsym:Pprocsym;
  713. unitsym : punitsym;
  714. begin
  715. oldprocsym:=aktprocsym;
  716. consume(_USES);
  717. {$ifdef DEBUG}
  718. test_symtablestack;
  719. {$endif DEBUG}
  720. repeat
  721. s:=pattern;
  722. sorg:=orgpattern;
  723. consume(_ID);
  724. { Give a warning if objpas is loaded }
  725. if s='OBJPAS' then
  726. Message(parser_w_no_objpas_use_mode);
  727. { check if the unit is already used }
  728. pu:=tused_unit(current_module.used_units.first);
  729. while assigned(pu) do
  730. begin
  731. if (pu.name^=s) then
  732. break;
  733. pu:=tused_unit(pu.next);
  734. end;
  735. { avoid uses of itself }
  736. if not assigned(pu) and (s<>current_module.modulename^) then
  737. begin
  738. { load the unit }
  739. hp2:=loadunit(s,false);
  740. { the current module uses the unit hp2 }
  741. current_module.used_units.concat(tused_unit.create(hp2,not current_module.in_implementation));
  742. tused_unit(current_module.used_units.last).in_uses:=true;
  743. if current_module.compiled then
  744. exit;
  745. unitsym:=new(punitsym,init(sorg,hp2.globalsymtable));
  746. { never claim about unused unit if
  747. there is init or finalize code PM }
  748. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  749. inc(unitsym^.refs);
  750. refsymtable^.insert(unitsym);
  751. end
  752. else
  753. Message1(sym_e_duplicate_id,s);
  754. if token=_COMMA then
  755. begin
  756. pattern:='';
  757. consume(_COMMA);
  758. end
  759. else
  760. break;
  761. until false;
  762. consume(_SEMICOLON);
  763. { set the symtable to systemunit so it gets reorderd correctly }
  764. symtablestack:=defaultsymtablestack;
  765. { now insert the units in the symtablestack }
  766. hp:=tused_unit(current_module.used_units.first);
  767. while assigned(hp) do
  768. begin
  769. {$IfDef GDB}
  770. if (cs_debuginfo in aktmoduleswitches) and
  771. (cs_gdb_dbx in aktglobalswitches) and
  772. not hp.is_stab_written then
  773. begin
  774. punitsymtable(hp.u.globalsymtable)^.concattypestabto(debuglist);
  775. hp.is_stab_written:=true;
  776. hp.unitid:=psymtable(hp.u.globalsymtable)^.unitid;
  777. end;
  778. {$EndIf GDB}
  779. if hp.in_uses then
  780. begin
  781. hp3:=symtablestack;
  782. while assigned(hp3) do
  783. begin
  784. { insert units only once ! }
  785. if hp.u.globalsymtable=hp3 then
  786. break;
  787. hp3:=hp3^.next;
  788. { unit isn't inserted }
  789. if hp3=nil then
  790. begin
  791. psymtable(hp.u.globalsymtable)^.next:=symtablestack;
  792. symtablestack:=psymtable(hp.u.globalsymtable);
  793. {$ifdef CHAINPROCSYMS}
  794. symtablestack^.chainprocsyms;
  795. {$endif CHAINPROCSYMS}
  796. {$ifdef DEBUG}
  797. test_symtablestack;
  798. {$endif DEBUG}
  799. end;
  800. end;
  801. end;
  802. hp:=tused_unit(hp.next);
  803. end;
  804. aktprocsym:=oldprocsym;
  805. end;
  806. procedure write_gdb_info;
  807. {$IfDef GDB}
  808. var
  809. hp : tused_unit;
  810. begin
  811. if not (cs_debuginfo in aktmoduleswitches) then
  812. exit;
  813. if (cs_gdb_dbx in aktglobalswitches) then
  814. begin
  815. debugList.concat(Tai_asm_comment.Create(strpnew('EINCL of global '+
  816. punitsymtable(current_module.globalsymtable)^.name^+' has index '+
  817. tostr(punitsymtable(current_module.globalsymtable)^.unitid))));
  818. debugList.concat(Tai_stabs.Create(strpnew('"'+
  819. punitsymtable(current_module.globalsymtable)^.name^+'",'+
  820. tostr(N_EINCL)+',0,0,0')));
  821. punitsymtable(current_module.globalsymtable)^.dbx_count_ok:={true}false;
  822. dbx_counter:=punitsymtable(current_module.globalsymtable)^.prev_dbx_counter;
  823. do_count_dbx:=false;
  824. end;
  825. { now insert the units in the symtablestack }
  826. hp:=tused_unit(current_module.used_units.first);
  827. while assigned(hp) do
  828. begin
  829. if (cs_debuginfo in aktmoduleswitches) and
  830. not hp.is_stab_written then
  831. begin
  832. punitsymtable(hp.u.globalsymtable)^.concattypestabto(debuglist);
  833. hp.is_stab_written:=true;
  834. hp.unitid:=psymtable(hp.u.globalsymtable)^.unitid;
  835. end;
  836. hp:=tused_unit(hp.next);
  837. end;
  838. if current_module.in_implementation and
  839. assigned(current_module.localsymtable) then
  840. begin
  841. { all types }
  842. punitsymtable(current_module.localsymtable)^.concattypestabto(debuglist);
  843. { and all local symbols}
  844. punitsymtable(current_module.localsymtable)^.concatstabto(debuglist);
  845. end
  846. else if assigned(current_module.globalsymtable) then
  847. begin
  848. { all types }
  849. punitsymtable(current_module.globalsymtable)^.concattypestabto(debuglist);
  850. { and all local symbols}
  851. punitsymtable(current_module.globalsymtable)^.concatstabto(debuglist);
  852. end;
  853. end;
  854. {$Else GDB}
  855. begin
  856. end;
  857. {$EndIf GDB}
  858. procedure parse_implementation_uses(symt:Psymtable);
  859. begin
  860. if token=_USES then
  861. begin
  862. symt^.symtabletype:=unitsymtable;
  863. loadunits;
  864. symt^.symtabletype:=globalsymtable;
  865. {$ifdef DEBUG}
  866. test_symtablestack;
  867. {$endif DEBUG}
  868. end;
  869. end;
  870. procedure setupglobalswitches;
  871. begin
  872. { can't have local browser when no global browser }
  873. if (cs_local_browser in aktmoduleswitches) and
  874. not(cs_browser in aktmoduleswitches) then
  875. exclude(aktmoduleswitches,cs_local_browser);
  876. { define a symbol in delphi,objfpc,tp,gpc mode }
  877. if (m_delphi in aktmodeswitches) then
  878. current_scanner^.def_macro('FPC_DELPHI')
  879. else
  880. if (m_tp in aktmodeswitches) then
  881. current_scanner^.def_macro('FPC_TP')
  882. else
  883. if (m_objfpc in aktmodeswitches) then
  884. current_scanner^.def_macro('FPC_OBJFPC')
  885. else
  886. if (m_gpc in aktmodeswitches) then
  887. current_scanner^.def_macro('FPC_GPC');
  888. end;
  889. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:psymtable);
  890. var
  891. stt : psymtable;
  892. begin
  893. {Generate a procsym for main}
  894. make_ref:=false;
  895. aktprocsym:=new(Pprocsym,init('$'+name));
  896. { main are allways used }
  897. inc(aktprocsym^.refs);
  898. {Try to insert in in static symtable ! }
  899. stt:=symtablestack;
  900. symtablestack:=st;
  901. aktprocsym^.definition:=new(Pprocdef,init);
  902. symtablestack:=stt;
  903. aktprocsym^.definition^.proctypeoption:=options;
  904. aktprocsym^.definition^.setmangledname(target_os.cprefix+name);
  905. aktprocsym^.definition^.forwarddef:=false;
  906. make_ref:=true;
  907. { The localst is a local symtable. Change it into the static
  908. symtable }
  909. dispose(aktprocsym^.definition^.localst,done);
  910. aktprocsym^.definition^.localst:=st;
  911. { and insert the procsym in symtable }
  912. st^.insert(aktprocsym);
  913. { set some informations about the main program }
  914. with procinfo^ do
  915. begin
  916. returntype:=voidtype;
  917. _class:=nil;
  918. para_offset:=8;
  919. framepointer:=frame_pointer;
  920. flags:=0;
  921. end;
  922. end;
  923. procedure proc_unit;
  924. function is_assembler_generated:boolean;
  925. begin
  926. is_assembler_generated:=(Errorcount=0) and
  927. not(
  928. codeSegment.empty and
  929. dataSegment.empty and
  930. bssSegment.empty and
  931. ((importssection=nil) or importsSection.empty) and
  932. ((resourcesection=nil) or resourceSection.empty) and
  933. ((resourcestringlist=nil) or resourcestringList.empty)
  934. );
  935. end;
  936. var
  937. main_file: tinputfile;
  938. st : psymtable;
  939. unitst : punitsymtable;
  940. {$ifdef GDB}
  941. pu : tused_unit;
  942. {$endif GDB}
  943. store_crc,store_interface_crc : longint;
  944. s2 : ^string; {Saves stack space}
  945. force_init_final : boolean;
  946. begin
  947. consume(_UNIT);
  948. if token=_ID then
  949. begin
  950. { create filenames and unit name }
  951. main_file := current_scanner^.inputfile;
  952. while assigned(main_file.next) do
  953. main_file := main_file.next;
  954. current_module.SetFileName(main_file.path^+main_file.name^,true);
  955. stringdispose(current_module.modulename);
  956. stringdispose(current_module.realmodulename);
  957. current_module.modulename:=stringdup(pattern);
  958. current_module.realmodulename:=stringdup(orgpattern);
  959. { check for system unit }
  960. new(s2);
  961. s2^:=upper(SplitName(main_file.name^));
  962. if (cs_compilesystem in aktmoduleswitches) then
  963. begin
  964. if ((length(current_module.modulename^)>8) or
  965. (current_module.modulename^<>'SYSTEM') or
  966. (current_module.modulename^<>s2^)) then
  967. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  968. end
  969. else
  970. begin
  971. if (cs_check_unit_name in aktglobalswitches) and
  972. not((current_module.modulename^=s2^) or
  973. ((length(current_module.modulename^)>8) and
  974. (copy(current_module.modulename^,1,8)=s2^))) then
  975. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  976. if (current_module.modulename^='SYSTEM') then
  977. Message(unit_w_switch_us_missed);
  978. end;
  979. dispose(s2);
  980. end;
  981. consume(_ID);
  982. consume(_SEMICOLON);
  983. consume(_INTERFACE);
  984. { global switches are read, so further changes aren't allowed }
  985. current_module.in_global:=false;
  986. { handle the global switches }
  987. setupglobalswitches;
  988. Message1(unit_u_start_parse_interface,current_module.realmodulename^);
  989. { update status }
  990. status.currentmodule:=current_module.realmodulename^;
  991. { maybe turn off m_objpas if we are compiling objpas }
  992. if (current_module.modulename^='OBJPAS') then
  993. aktmodeswitches:=aktmodeswitches-[m_objpas];
  994. { this should be placed after uses !!}
  995. {$ifndef UseNiceNames}
  996. procprefix:='_'+current_module.modulename^+'$$';
  997. {$else UseNiceNames}
  998. procprefix:='_'+tostr(length(current_module.modulename^))+lowercase(current_module.modulename^)+'_';
  999. {$endif UseNiceNames}
  1000. parse_only:=true;
  1001. { generate now the global symboltable }
  1002. st:=new(punitsymtable,init(globalsymtable,current_module.modulename^));
  1003. refsymtable:=st;
  1004. unitst:=punitsymtable(st);
  1005. { define first as local to overcome dependency conflicts }
  1006. current_module.localsymtable:=st;
  1007. { the unit name must be usable as a unit specifier }
  1008. { inside the unit itself (PM) }
  1009. { this also forbids to have another symbol }
  1010. { with the same name as the unit }
  1011. refsymtable^.insert(new(punitsym,init(current_module.realmodulename^,unitst)));
  1012. { a unit compiled at command line must be inside the loaded_unit list }
  1013. if (compile_level=1) then
  1014. loaded_units.insert(current_module);
  1015. { load default units, like the system unit }
  1016. loaddefaultunits;
  1017. { reset }
  1018. make_ref:=true;
  1019. lexlevel:=0;
  1020. { insert qualifier for the system unit (allows system.writeln) }
  1021. if not(cs_compilesystem in aktmoduleswitches) then
  1022. begin
  1023. if token=_USES then
  1024. begin
  1025. unitst^.symtabletype:=unitsymtable;
  1026. loadunits;
  1027. { has it been compiled at a higher level ?}
  1028. if current_module.compiled then
  1029. begin
  1030. { this unit symtable is obsolete }
  1031. { dispose(unitst,done);
  1032. disposed as localsymtable !! }
  1033. RestoreUnitSyms;
  1034. exit;
  1035. end;
  1036. unitst^.symtabletype:=globalsymtable;
  1037. end;
  1038. { ... but insert the symbol table later }
  1039. st^.next:=symtablestack;
  1040. symtablestack:=st;
  1041. end
  1042. else
  1043. { while compiling a system unit, some types are directly inserted }
  1044. begin
  1045. st^.next:=symtablestack;
  1046. symtablestack:=st;
  1047. insert_intern_types(st);
  1048. end;
  1049. { now we know the place to insert the constants }
  1050. constsymtable:=symtablestack;
  1051. { move the global symtab from the temporary local to global }
  1052. current_module.globalsymtable:=current_module.localsymtable;
  1053. current_module.localsymtable:=nil;
  1054. reset_global_defs;
  1055. { number all units, so we know if a unit is used by this unit or
  1056. needs to be added implicitly }
  1057. numberunits;
  1058. { ... parse the declarations }
  1059. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  1060. read_interface_declarations;
  1061. { leave when we got an error }
  1062. if (Errorcount>0) and not status.skip_error then
  1063. begin
  1064. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1065. status.skip_error:=true;
  1066. exit;
  1067. end;
  1068. {else in inteface its somatimes necessary even if unused
  1069. st^.allunitsused; }
  1070. {$ifdef New_GDB}
  1071. write_gdb_info;
  1072. {$endIf Def New_GDB}
  1073. if not(cs_compilesystem in aktmoduleswitches) then
  1074. if (Errorcount=0) then
  1075. writeunitas(current_module.ppufilename^,punitsymtable(symtablestack),true);
  1076. { Parse the implementation section }
  1077. consume(_IMPLEMENTATION);
  1078. current_module.in_implementation:=true;
  1079. Message1(unit_u_start_parse_implementation,current_module.modulename^);
  1080. parse_only:=false;
  1081. { generates static symbol table }
  1082. st:=new(punitsymtable,init(staticsymtable,current_module.modulename^));
  1083. current_module.localsymtable:=st;
  1084. { remove the globalsymtable from the symtable stack }
  1085. { to reinsert it after loading the implementation units }
  1086. symtablestack:=unitst^.next;
  1087. { we don't want implementation units symbols in unitsymtable !! PM }
  1088. refsymtable:=st;
  1089. { Read the implementation units }
  1090. parse_implementation_uses(unitst);
  1091. if current_module.compiled then
  1092. begin
  1093. RestoreUnitSyms;
  1094. exit;
  1095. end;
  1096. { reset ranges/stabs in exported definitions }
  1097. reset_global_defs;
  1098. { All units are read, now give them a number }
  1099. numberunits;
  1100. { now we can change refsymtable }
  1101. refsymtable:=st;
  1102. { but reinsert the global symtable as lasts }
  1103. unitst^.next:=symtablestack;
  1104. symtablestack:=unitst;
  1105. {$ifndef DONOTCHAINOPERATORS}
  1106. pstoredsymtable(symtablestack)^.chainoperators;
  1107. {$endif DONOTCHAINOPERATORS}
  1108. {$ifdef DEBUG}
  1109. test_symtablestack;
  1110. {$endif DEBUG}
  1111. constsymtable:=symtablestack;
  1112. {$ifdef Splitheap}
  1113. if testsplit then
  1114. begin
  1115. Split_Heap;
  1116. allow_special:=true;
  1117. Switch_to_temp_heap;
  1118. end;
  1119. { it will report all crossings }
  1120. allow_special:=false;
  1121. {$endif Splitheap}
  1122. Message1(parser_u_parsing_implementation,current_module.realmodulename^);
  1123. { Compile the unit }
  1124. codegen_newprocedure;
  1125. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  1126. aktprocsym^.definition^.aliasnames.insert('INIT$$'+current_module.modulename^);
  1127. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+current_module.modulename^+'_init');
  1128. compile_proc_body(true,false);
  1129. codegen_doneprocedure;
  1130. { avoid self recursive destructor call !! PM }
  1131. aktprocsym^.definition^.localst:=nil;
  1132. { if the unit contains ansi/widestrings, initialization and
  1133. finalization code must be forced }
  1134. force_init_final:=needs_init_final(current_module.globalsymtable)
  1135. or needs_init_final(current_module.localsymtable);
  1136. { should we force unit initialization? }
  1137. { this is a hack, but how can it be done better ? }
  1138. if force_init_final and ((current_module.flags and uf_init)=0) then
  1139. begin
  1140. current_module.flags:=current_module.flags or uf_init;
  1141. { now we can insert a cut }
  1142. if (cs_create_smart in aktmoduleswitches) then
  1143. codeSegment.concat(Tai_cut.Create);
  1144. genimplicitunitinit(codesegment);
  1145. end;
  1146. { finalize? }
  1147. if token=_FINALIZATION then
  1148. begin
  1149. { set module options }
  1150. current_module.flags:=current_module.flags or uf_finalize;
  1151. { Compile the finalize }
  1152. codegen_newprocedure;
  1153. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1154. aktprocsym^.definition^.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1155. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+current_module.modulename^+'_finalize');
  1156. compile_proc_body(true,false);
  1157. codegen_doneprocedure;
  1158. end
  1159. else if force_init_final then
  1160. begin
  1161. current_module.flags:=current_module.flags or uf_finalize;
  1162. { now we can insert a cut }
  1163. if (cs_create_smart in aktmoduleswitches) then
  1164. codeSegment.concat(Tai_cut.Create);
  1165. genimplicitunitfinal(codesegment);
  1166. end;
  1167. { the last char should always be a point }
  1168. consume(_POINT);
  1169. If ResourceStrings.ResStrCount>0 then
  1170. begin
  1171. ResourceStrings.CreateResourceStringList;
  1172. current_module.flags:=current_module.flags or uf_has_resources;
  1173. { only write if no errors found }
  1174. if (Errorcount=0) then
  1175. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1176. end;
  1177. { avoid self recursive destructor call !! PM }
  1178. aktprocsym^.definition^.localst:=nil;
  1179. { absence does not matter here !! }
  1180. aktprocsym^.definition^.forwarddef:=false;
  1181. { test static symtable }
  1182. if (Errorcount=0) then
  1183. begin
  1184. pstoredsymtable(st)^.allsymbolsused;
  1185. pstoredsymtable(st)^.allunitsused;
  1186. pstoredsymtable(st)^.allprivatesused;
  1187. end;
  1188. { size of the static data }
  1189. datasize:=st^.datasize;
  1190. {$ifdef GDB}
  1191. { add all used definitions even for implementation}
  1192. if (cs_debuginfo in aktmoduleswitches) then
  1193. begin
  1194. {$IfnDef New_GDB}
  1195. if assigned(current_module.globalsymtable) then
  1196. begin
  1197. { all types }
  1198. punitsymtable(current_module.globalsymtable)^.concattypestabto(debuglist);
  1199. { and all local symbols}
  1200. punitsymtable(current_module.globalsymtable)^.concatstabto(debuglist);
  1201. end;
  1202. { all local types }
  1203. punitsymtable(st)^.concattypestabto(debuglist);
  1204. { and all local symbols}
  1205. st^.concatstabto(debuglist);
  1206. {$else New_GDB}
  1207. write_gdb_info;
  1208. {$endIf Def New_GDB}
  1209. end;
  1210. {$endif GDB}
  1211. reset_global_defs;
  1212. { tests, if all (interface) forwards are resolved }
  1213. if (Errorcount=0) then
  1214. begin
  1215. pstoredsymtable(symtablestack)^.check_forwards;
  1216. pstoredsymtable(symtablestack)^.allprivatesused;
  1217. end;
  1218. { now we have a correct unit, change the symtable type }
  1219. current_module.in_implementation:=false;
  1220. symtablestack^.symtabletype:=unitsymtable;
  1221. {$ifdef GDB}
  1222. punitsymtable(symtablestack)^.is_stab_written:=false;
  1223. {$endif GDB}
  1224. { leave when we got an error }
  1225. if (Errorcount>0) and not status.skip_error then
  1226. begin
  1227. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1228. status.skip_error:=true;
  1229. closecurrentppu;
  1230. exit;
  1231. end;
  1232. { generate imports }
  1233. if current_module.uses_imports then
  1234. importlib.generatelib;
  1235. { insert own objectfile, or say that it's in a library
  1236. (no check for an .o when loading) }
  1237. if is_assembler_generated then
  1238. insertobjectfile
  1239. else
  1240. current_module.flags:=current_module.flags or uf_no_link;
  1241. if cs_local_browser in aktmoduleswitches then
  1242. current_module.localsymtable:=refsymtable;
  1243. { Write out the ppufile }
  1244. store_interface_crc:=current_module.interface_crc;
  1245. store_crc:=current_module.crc;
  1246. if (Errorcount=0) then
  1247. writeunitas(current_module.ppufilename^,punitsymtable(symtablestack),false);
  1248. if not(cs_compilesystem in aktmoduleswitches) then
  1249. if store_interface_crc<>current_module.interface_crc then
  1250. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1251. tostr(store_crc)+'<>'+tostr(current_module.interface_crc));
  1252. {$ifdef EXTDEBUG}
  1253. if not(cs_compilesystem in aktmoduleswitches) then
  1254. if (store_crc<>current_module.crc) and simplify_ppu then
  1255. Comment(V_Warning,current_module.ppufilename^+' implementation CRC changed '+
  1256. tostr(store_crc)+'<>'+tostr(current_module.interface_crc));
  1257. {$endif EXTDEBUG}
  1258. { must be done only after local symtable ref stores !! }
  1259. closecurrentppu;
  1260. {$ifdef GDB}
  1261. pu:=tused_unit(usedunits.first);
  1262. while assigned(pu) do
  1263. begin
  1264. if assigned(pu.u.globalsymtable) then
  1265. punitsymtable(pu.u.globalsymtable)^.is_stab_written:=false;
  1266. pu:=tused_unit(pu.next);
  1267. end;
  1268. {$endif GDB}
  1269. { remove static symtable (=refsymtable) here to save some mem }
  1270. if not (cs_local_browser in aktmoduleswitches) then
  1271. begin
  1272. dispose(st,done);
  1273. current_module.localsymtable:=nil;
  1274. end;
  1275. RestoreUnitSyms;
  1276. if is_assembler_generated then
  1277. begin
  1278. { finish asmlist by adding segment starts }
  1279. insertsegment;
  1280. { assemble }
  1281. create_objectfile;
  1282. end;
  1283. { leave when we got an error }
  1284. if (Errorcount>0) and not status.skip_error then
  1285. begin
  1286. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1287. status.skip_error:=true;
  1288. exit;
  1289. end;
  1290. end;
  1291. procedure proc_program(islibrary : boolean);
  1292. var
  1293. main_file: tinputfile;
  1294. st : psymtable;
  1295. hp : tmodule;
  1296. begin
  1297. DLLsource:=islibrary;
  1298. IsExe:=true;
  1299. parse_only:=false;
  1300. { relocation works only without stabs under win32 !! PM }
  1301. { internal assembler uses rva for stabs info
  1302. so it should work with relocated DLLs }
  1303. if RelocSection and
  1304. (target_info.target=target_i386_win32) and
  1305. (target_info.assem<>as_i386_pecoff) then
  1306. begin
  1307. aktglobalswitches:=aktglobalswitches+[cs_link_strip];
  1308. { Warning stabs info does not work with reloc section !! }
  1309. if cs_debuginfo in aktmoduleswitches then
  1310. begin
  1311. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1312. Message(parser_w_parser_win32_debug_needs_WN);
  1313. aktmoduleswitches:=aktmoduleswitches-[cs_debuginfo];
  1314. end;
  1315. end;
  1316. { get correct output names }
  1317. main_file := current_scanner^.inputfile;
  1318. while assigned(main_file.next) do
  1319. main_file := main_file.next;
  1320. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1321. if islibrary then
  1322. begin
  1323. consume(_LIBRARY);
  1324. stringdispose(current_module.modulename);
  1325. current_module.modulename:=stringdup(pattern);
  1326. current_module.islibrary:=true;
  1327. exportlib.preparelib(pattern);
  1328. consume(_ID);
  1329. consume(_SEMICOLON);
  1330. end
  1331. else
  1332. { is there an program head ? }
  1333. if token=_PROGRAM then
  1334. begin
  1335. consume(_PROGRAM);
  1336. stringdispose(current_module.modulename);
  1337. stringdispose(current_module.realmodulename);
  1338. current_module.modulename:=stringdup(pattern);
  1339. current_module.realmodulename:=stringdup(orgpattern);
  1340. if (target_info.target=target_i386_WIN32) then
  1341. exportlib.preparelib(pattern);
  1342. consume(_ID);
  1343. if token=_LKLAMMER then
  1344. begin
  1345. consume(_LKLAMMER);
  1346. idlist;
  1347. consume(_RKLAMMER);
  1348. end;
  1349. consume(_SEMICOLON);
  1350. end
  1351. else if (target_info.target=target_i386_WIN32) then
  1352. exportlib.preparelib(current_module.modulename^);
  1353. { global switches are read, so further changes aren't allowed }
  1354. current_module.in_global:=false;
  1355. { setup things using the global switches }
  1356. setupglobalswitches;
  1357. { set implementation flag }
  1358. current_module.in_implementation:=true;
  1359. { insert after the unit symbol tables the static symbol table }
  1360. { of the program }
  1361. st:=new(punitsymtable,init(staticsymtable,current_module.modulename^));
  1362. current_module.localsymtable:=st;
  1363. refsymtable:=st;
  1364. { a unit compiled at command line must be inside the loaded_unit list }
  1365. if (compile_level=1) then
  1366. loaded_units.insert(current_module);
  1367. { load standard units (system,objpas,profile unit) }
  1368. loaddefaultunits;
  1369. { reset }
  1370. lexlevel:=0;
  1371. {Load the units used by the program we compile.}
  1372. if token=_USES then
  1373. loadunits;
  1374. {$ifndef DONOTCHAINOPERATORS}
  1375. pstoredsymtable(symtablestack)^.chainoperators;
  1376. {$endif DONOTCHAINOPERATORS}
  1377. { reset ranges/stabs in exported definitions }
  1378. reset_global_defs;
  1379. { All units are read, now give them a number }
  1380. numberunits;
  1381. {Insert the name of the main program into the symbol table.}
  1382. if current_module.realmodulename^<>'' then
  1383. st^.insert(new(punitsym,init(current_module.realmodulename^,punitsymtable(st))));
  1384. { ...is also constsymtable, this is the symtable where }
  1385. { the elements of enumeration types are inserted }
  1386. constsymtable:=st;
  1387. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1388. { reset }
  1389. procprefix:='';
  1390. {The program intialization needs an alias, so it can be called
  1391. from the bootstrap code.}
  1392. codegen_newprocedure;
  1393. gen_main_procsym('main',potype_proginit,st);
  1394. aktprocsym^.definition^.aliasnames.insert('program_init');
  1395. aktprocsym^.definition^.aliasnames.insert('PASCALMAIN');
  1396. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+'main');
  1397. {$ifdef m68k}
  1398. if target_info.target=target_m68k_PalmOS then
  1399. aktprocsym^.definition^.aliasnames.insert('PilotMain');
  1400. {$endif m68k}
  1401. compile_proc_body(true,false);
  1402. { avoid self recursive destructor call !! PM }
  1403. aktprocsym^.definition^.localst:=nil;
  1404. { consider these symbols as global ones }
  1405. { for browser }
  1406. current_module.globalsymtable:=current_module.localsymtable;
  1407. current_module.localsymtable:=nil;
  1408. If ResourceStrings.ResStrCount>0 then
  1409. begin
  1410. ResourceStrings.CreateResourceStringList;
  1411. { only write if no errors found }
  1412. if (Errorcount=0) then
  1413. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1414. end;
  1415. codegen_doneprocedure;
  1416. { finalize? }
  1417. if token=_FINALIZATION then
  1418. begin
  1419. { set module options }
  1420. current_module.flags:=current_module.flags or uf_finalize;
  1421. { Compile the finalize }
  1422. codegen_newprocedure;
  1423. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1424. aktprocsym^.definition^.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1425. aktprocsym^.definition^.aliasnames.insert(target_os.cprefix+current_module.modulename^+'_finalize');
  1426. compile_proc_body(true,false);
  1427. codegen_doneprocedure;
  1428. end;
  1429. { consume the last point }
  1430. consume(_POINT);
  1431. {$ifdef New_GDB}
  1432. write_gdb_info;
  1433. {$endIf Def New_GDB}
  1434. { leave when we got an error }
  1435. if (Errorcount>0) and not status.skip_error then
  1436. begin
  1437. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1438. status.skip_error:=true;
  1439. exit;
  1440. end;
  1441. { test static symtable }
  1442. if (Errorcount=0) then
  1443. begin
  1444. pstoredsymtable(st)^.allsymbolsused;
  1445. pstoredsymtable(st)^.allunitsused;
  1446. pstoredsymtable(st)^.allprivatesused;
  1447. end;
  1448. { generate imports }
  1449. if current_module.uses_imports then
  1450. importlib.generatelib;
  1451. if islibrary or
  1452. (target_info.target=target_i386_WIN32) then
  1453. exportlib.generatelib;
  1454. { insert heap }
  1455. insertResourceTablesTable;
  1456. insertinitfinaltable;
  1457. insertheap;
  1458. inserttargetspecific;
  1459. datasize:=symtablestack^.datasize;
  1460. { finish asmlist by adding segment starts }
  1461. insertsegment;
  1462. { insert own objectfile }
  1463. insertobjectfile;
  1464. { assemble and link }
  1465. create_objectfile;
  1466. { leave when we got an error }
  1467. if (Errorcount>0) and not status.skip_error then
  1468. begin
  1469. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1470. status.skip_error:=true;
  1471. exit;
  1472. end;
  1473. { create the executable when we are at level 1 }
  1474. if (compile_level=1) then
  1475. begin
  1476. { insert all .o files from all loaded units }
  1477. hp:=tmodule(loaded_units.first);
  1478. while assigned(hp) do
  1479. begin
  1480. linker.AddModuleFiles(hp);
  1481. hp:=tmodule(hp.next);
  1482. end;
  1483. { write .def file }
  1484. if (cs_link_deffile in aktglobalswitches) then
  1485. deffile.writefile;
  1486. { finally we can create a executable }
  1487. if (not current_module.is_unit) then
  1488. begin
  1489. if DLLSource then
  1490. linker.MakeSharedLibrary
  1491. else
  1492. linker.MakeExecutable;
  1493. end;
  1494. end;
  1495. end;
  1496. end.
  1497. {
  1498. $Log$
  1499. Revision 1.26 2001-04-02 21:20:33 peter
  1500. * resulttype rewrite
  1501. Revision 1.25 2001/03/13 18:45:07 peter
  1502. * fixed some memory leaks
  1503. Revision 1.24 2001/03/06 18:28:02 peter
  1504. * patch from Pavel with a new and much faster DLL Scanner for
  1505. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1506. Revision 1.23 2001/02/24 10:44:56 peter
  1507. * generate .rst from ppufilename instead of modulename
  1508. Revision 1.22 2001/02/21 19:37:19 peter
  1509. * moved deref to be done after loading of implementation units. prederef
  1510. is still done directly after loading of symbols and definitions.
  1511. Revision 1.21 2001/01/14 22:13:52 peter
  1512. * fixed crash with program name as a important unit name
  1513. Revision 1.20 2000/12/25 00:07:27 peter
  1514. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1515. tlinkedlist objects)
  1516. Revision 1.19 2000/11/29 00:30:36 florian
  1517. * unused units removed from uses clause
  1518. * some changes for widestrings
  1519. Revision 1.18 2000/11/01 23:04:37 peter
  1520. * tprocdef.fullprocname added for better casesensitve writing of
  1521. procedures
  1522. Revision 1.17 2000/10/31 22:02:50 peter
  1523. * symtable splitted, no real code changes
  1524. Revision 1.16 2000/10/21 14:36:26 peter
  1525. * merged pierres fixes
  1526. Revision 1.15 2000/10/15 09:08:58 peter
  1527. * use System for the systemunit instead of target dependent
  1528. Revision 1.14 2000/10/15 07:47:51 peter
  1529. * unit names and procedure names are stored mixed case
  1530. Revision 1.13 2000/10/04 14:51:08 pierre
  1531. * IsExe restored
  1532. Revision 1.12 2000/09/30 16:07:40 peter
  1533. * filepos when unit not found (merged)
  1534. Revision 1.11 2000/09/24 21:33:47 peter
  1535. * message updates merges
  1536. Revision 1.10 2000/09/24 15:06:22 peter
  1537. * use defines.inc
  1538. Revision 1.9 2000/08/31 07:53:02 michael
  1539. + Applied patch from Peter
  1540. Revision 1.8 2000/08/29 19:00:01 peter
  1541. * _init and _finalize procsyms also need a $ prefix
  1542. Revision 1.7 2000/08/27 20:19:39 peter
  1543. * store strings with case in ppu, when an internal symbol is created
  1544. a '$' is prefixed so it's not automatic uppercased
  1545. Revision 1.6 2000/08/27 16:11:52 peter
  1546. * moved some util functions from globals,cobjects to cutils
  1547. * splitted files into finput,fmodule
  1548. Revision 1.5 2000/08/25 08:48:22 jonas
  1549. * fixed bug with include files at the very beginning of .pp/.pas files
  1550. (wrong name used for generating exe/checking unit name) (merged from
  1551. fixes branch)
  1552. Revision 1.4 2000/08/21 11:27:44 pierre
  1553. * fix the stabs problems
  1554. Revision 1.3 2000/07/13 12:08:26 michael
  1555. + patched to 1.1.0 with former 1.09patch from peter
  1556. Revision 1.2 2000/07/13 11:32:45 michael
  1557. + removed logs
  1558. }