pmodules.pas 62 KB

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