pmodules.pas 53 KB

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