pmodules.pas 61 KB

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