pmodules.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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,initdataname_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,initdataname_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,initdataname_global('HEAP_SIZE',0)));
  249. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  250. end;
  251. {$else m68k}
  252. datasegment^.concat(new(pai_symbol,initdataname_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,initdataname_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,initdataname_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) and
  414. not(current_module^.sources_checked) then
  415. if (not current_module^.search_unit(current_module^.modulename^,true))
  416. and (length(current_module^.modulename^)>8) then
  417. current_module^.search_unit(copy(current_module^.modulename^,1,8),true);
  418. if not(current_module^.sources_avail) then
  419. begin
  420. if current_module^.recompile_reason=rr_noppu then
  421. Message1(unit_f_cant_find_ppu,current_module^.modulename^)
  422. else
  423. Message1(unit_f_cant_compile_unit,current_module^.modulename^);
  424. end
  425. else
  426. begin
  427. if current_module^.in_compile then
  428. begin
  429. current_module^.in_second_compile:=true;
  430. Message1(parser_d_compiling_second_time,current_module^.modulename^);
  431. end;
  432. current_scanner^.tempcloseinputfile;
  433. name:=current_module^.mainsource^;
  434. if assigned(scanner) then
  435. scanner^.invalid:=true;
  436. compile(name,compile_system);
  437. current_module^.in_second_compile:=false;
  438. if (not current_scanner^.invalid) then
  439. current_scanner^.tempopeninputfile;
  440. end;
  441. end
  442. else
  443. begin
  444. { only reassemble ? }
  445. if (current_module^.do_assemble) then
  446. OnlyAsm;
  447. end;
  448. if assigned(current_module^.ppufile) then
  449. begin
  450. dispose(current_module^.ppufile,done);
  451. current_module^.ppufile:=nil;
  452. current_ppu:=nil;
  453. end;
  454. end;
  455. var
  456. dummy : pmodule;
  457. begin
  458. old_current_module:=current_module;
  459. old_current_ppu:=current_ppu;
  460. { Info }
  461. Message3(unit_u_load_unit,current_module^.modulename^,ImplIntf[current_module^.in_implementation],s);
  462. { unit not found }
  463. st:=nil;
  464. dummy:=nil;
  465. { search all loaded units }
  466. hp:=pmodule(loaded_units.first);
  467. while assigned(hp) do
  468. begin
  469. if hp^.modulename^=s then
  470. begin
  471. { forced to reload ? }
  472. if hp^.do_reload then
  473. begin
  474. hp^.do_reload:=false;
  475. break;
  476. end;
  477. { the unit is already registered }
  478. { and this means that the unit }
  479. { is already compiled }
  480. { else there is a cyclic unit use }
  481. if assigned(hp^.globalsymtable) then
  482. st:=punitsymtable(hp^.globalsymtable)
  483. else
  484. begin
  485. { both units in interface ? }
  486. if (not current_module^.in_implementation) and (not hp^.in_implementation) then
  487. begin
  488. { check for a cycle }
  489. hp2:=current_module^.loaded_from;
  490. while assigned(hp2) and (hp2<>hp) do
  491. begin
  492. if hp2^.in_implementation then
  493. hp2:=nil
  494. else
  495. hp2:=hp2^.loaded_from;
  496. end;
  497. if assigned(hp2) then
  498. Message2(unit_f_circular_unit_reference,current_module^.modulename^,hp^.modulename^);
  499. end;
  500. end;
  501. break;
  502. end
  503. else if copy(hp^.modulename^,1,8)=s then
  504. dummy:=hp;
  505. { the next unit }
  506. hp:=pmodule(hp^.next);
  507. end;
  508. if assigned(dummy) and not assigned(hp) then
  509. Message2(unit_w_unit_name_error,s,dummy^.modulename^);
  510. { the unit is not in the symtable stack }
  511. if (not assigned(st)) then
  512. begin
  513. if assigned(hp) then
  514. begin
  515. { remove the old unit }
  516. loaded_units.remove(hp);
  517. scanner:=hp^.scanner;
  518. hp^.reset;
  519. hp^.scanner:=scanner;
  520. { try to reopen ppu }
  521. hp^.search_unit(s,false);
  522. { try to load the unit a second time first }
  523. current_module:=hp;
  524. current_module^.in_second_load:=true;
  525. Message1(unit_u_second_load_unit,current_module^.modulename^);
  526. second_time:=true;
  527. end
  528. else
  529. { generates a new unit info record }
  530. begin
  531. current_module:=new(pmodule,init(s,true));
  532. scanner:=nil;
  533. second_time:=false;
  534. end;
  535. current_ppu:=current_module^.ppufile;
  536. { close old_current_ppu on system that are
  537. short on file handles like DOS PM }
  538. {$ifdef SHORT_ON_FILE_HANDLES}
  539. if assigned(old_current_ppu) then
  540. old_current_ppu^.tempclose;
  541. {$endif SHORT_ON_FILE_HANDLES}
  542. { now we can register the unit }
  543. current_module^.loaded_from:=old_current_module;
  544. loaded_units.insert(current_module);
  545. { now realy load the ppu }
  546. loadppufile;
  547. { set compiled flag }
  548. current_module^.compiled:=true;
  549. { load return pointer }
  550. hp:=current_module;
  551. { for a second_time recompile reload all dependent units,
  552. for a first time compile register the unit _once_ }
  553. if second_time then
  554. begin
  555. { now reload all dependent units }
  556. hp2:=pmodule(loaded_units.first);
  557. while assigned(hp2) do
  558. begin
  559. if hp2^.do_reload then
  560. dummy:=loadunit(hp2^.modulename^,false);
  561. hp2:=pmodule(hp2^.next);
  562. end;
  563. end
  564. else
  565. usedunits.concat(new(pused_unit,init(current_module,true)));
  566. end;
  567. { set the old module }
  568. {$ifdef SHORT_ON_FILE_HANDLES}
  569. if assigned(old_current_ppu) then
  570. old_current_ppu^.tempopen;
  571. {$endif SHORT_ON_FILE_HANDLES}
  572. current_ppu:=old_current_ppu;
  573. current_module:=old_current_module;
  574. loadunit:=hp;
  575. end;
  576. procedure loaddefaultunits;
  577. var
  578. hp : pmodule;
  579. unitsym : punitsym;
  580. begin
  581. { are we compiling the system unit? }
  582. if (cs_compilesystem in aktmoduleswitches) then
  583. begin
  584. { create system defines }
  585. createconstdefs;
  586. { we don't need to reset anything, it's already done in parser.pas }
  587. exit;
  588. end;
  589. { insert the system unit, it is allways the first }
  590. hp:=loadunit(upper(target_info.system_unit),true);
  591. systemunit:=hp^.globalsymtable;
  592. { it's always the first unit }
  593. systemunit^.next:=nil;
  594. symtablestack:=systemunit;
  595. { add to the used units }
  596. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  597. unitsym:=new(punitsym,init('SYSTEM',systemunit));
  598. inc(unitsym^.refs);
  599. refsymtable^.insert(unitsym);
  600. { read default constant definitions }
  601. make_ref:=false;
  602. readconstdefs;
  603. { if POWER is defined in the RTL then use it for starstar overloading }
  604. {$ifdef DONOTCHAINOPERATORS}
  605. getsym('POWER',false);
  606. {$endif DONOTCHAINOPERATORS}
  607. make_ref:=true;
  608. {$ifdef DONOTCHAINOPERATORS}
  609. { Code now in chainoperators PM }
  610. if assigned(srsym) and (srsym^.typ=procsym) and (overloaded_operators[_STARSTAR]=nil) then
  611. overloaded_operators[_STARSTAR]:=pprocsym(srsym);
  612. {$endif DONOTCHAINOPERATORS}
  613. { Objpas unit? }
  614. if m_objpas in aktmodeswitches then
  615. begin
  616. hp:=loadunit('OBJPAS',false);
  617. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  618. symtablestack:=hp^.globalsymtable;
  619. { add to the used units }
  620. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  621. unitsym:=new(punitsym,init('OBJPAS',hp^.globalsymtable));
  622. inc(unitsym^.refs);
  623. refsymtable^.insert(unitsym);
  624. end;
  625. { Profile unit? Needed for go32v2 only }
  626. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  627. begin
  628. hp:=loadunit('PROFILE',false);
  629. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  630. symtablestack:=hp^.globalsymtable;
  631. { add to the used units }
  632. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  633. unitsym:=new(punitsym,init('PROFILE',hp^.globalsymtable));
  634. inc(unitsym^.refs);
  635. refsymtable^.insert(unitsym);
  636. end;
  637. { Units only required for main module }
  638. if not(current_module^.is_unit) then
  639. begin
  640. { Heaptrc unit }
  641. if (cs_gdb_heaptrc in aktglobalswitches) then
  642. begin
  643. hp:=loadunit('HEAPTRC',false);
  644. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  645. symtablestack:=hp^.globalsymtable;
  646. { add to the used units }
  647. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  648. unitsym:=new(punitsym,init('HEAPTRC',hp^.globalsymtable));
  649. inc(unitsym^.refs);
  650. refsymtable^.insert(unitsym);
  651. end;
  652. { Lineinfo unit }
  653. if (cs_gdb_lineinfo in aktglobalswitches) then
  654. begin
  655. hp:=loadunit('LINEINFO',false);
  656. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  657. symtablestack:=hp^.globalsymtable;
  658. { add to the used units }
  659. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  660. unitsym:=new(punitsym,init('LINEINFO',hp^.globalsymtable));
  661. inc(unitsym^.refs);
  662. refsymtable^.insert(unitsym);
  663. end;
  664. end;
  665. { save default symtablestack }
  666. defaultsymtablestack:=symtablestack;
  667. end;
  668. procedure loadunits;
  669. var
  670. s : stringid;
  671. pu,
  672. hp : pused_unit;
  673. hp2 : pmodule;
  674. hp3 : psymtable;
  675. oldprocsym:Pprocsym;
  676. unitsym : punitsym;
  677. begin
  678. oldprocsym:=aktprocsym;
  679. consume(_USES);
  680. {$ifdef DEBUG}
  681. test_symtablestack;
  682. {$endif DEBUG}
  683. repeat
  684. s:=pattern;
  685. consume(_ID);
  686. { Give a warning if objpas is loaded }
  687. if s='OBJPAS' then
  688. Message(parser_w_no_objpas_use_mode);
  689. { check if the unit is already used }
  690. pu:=pused_unit(current_module^.used_units.first);
  691. while assigned(pu) do
  692. begin
  693. if (pu^.name^=s) then
  694. break;
  695. pu:=pused_unit(pu^.next);
  696. end;
  697. { avoid uses of itself }
  698. if not assigned(pu) and (s<>current_module^.modulename^) then
  699. begin
  700. { load the unit }
  701. hp2:=loadunit(s,false);
  702. { the current module uses the unit hp2 }
  703. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  704. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  705. if current_module^.compiled then
  706. exit;
  707. unitsym:=new(punitsym,init(s,hp2^.globalsymtable));
  708. { never claim about unused unit if
  709. there is init or finalize code PM }
  710. if (hp2^.flags and (uf_init or uf_finalize))<>0 then
  711. inc(unitsym^.refs);
  712. refsymtable^.insert(unitsym);
  713. end
  714. else
  715. Message1(sym_e_duplicate_id,s);
  716. if token=_COMMA then
  717. begin
  718. pattern:='';
  719. consume(_COMMA);
  720. end
  721. else
  722. break;
  723. until false;
  724. consume(_SEMICOLON);
  725. { set the symtable to systemunit so it gets reorderd correctly }
  726. symtablestack:=defaultsymtablestack;
  727. { now insert the units in the symtablestack }
  728. hp:=pused_unit(current_module^.used_units.first);
  729. while assigned(hp) do
  730. begin
  731. {$IfDef GDB}
  732. if (cs_debuginfo in aktmoduleswitches) and
  733. (cs_gdb_dbx in aktglobalswitches) and
  734. not hp^.is_stab_written then
  735. begin
  736. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  737. hp^.is_stab_written:=true;
  738. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  739. end;
  740. {$EndIf GDB}
  741. if hp^.in_uses then
  742. begin
  743. hp3:=symtablestack;
  744. while assigned(hp3) do
  745. begin
  746. { insert units only once ! }
  747. if hp^.u^.globalsymtable=hp3 then
  748. break;
  749. hp3:=hp3^.next;
  750. { unit isn't inserted }
  751. if hp3=nil then
  752. begin
  753. psymtable(hp^.u^.globalsymtable)^.next:=symtablestack;
  754. symtablestack:=psymtable(hp^.u^.globalsymtable);
  755. {$ifdef CHAINPROCSYMS}
  756. symtablestack^.chainprocsyms;
  757. {$endif CHAINPROCSYMS}
  758. {$ifdef DEBUG}
  759. test_symtablestack;
  760. {$endif DEBUG}
  761. end;
  762. end;
  763. end;
  764. hp:=pused_unit(hp^.next);
  765. end;
  766. aktprocsym:=oldprocsym;
  767. end;
  768. procedure write_gdb_info;
  769. {$IfDef GDB}
  770. var
  771. hp : pused_unit;
  772. begin
  773. if not (cs_debuginfo in aktmoduleswitches) then
  774. exit;
  775. if (cs_gdb_dbx in aktglobalswitches) then
  776. begin
  777. debuglist^.concat(new(pai_asm_comment,init(strpnew('EINCL of global '+
  778. punitsymtable(current_module^.globalsymtable)^.name^+' has index '+
  779. tostr(punitsymtable(current_module^.globalsymtable)^.unitid)))));
  780. debuglist^.concat(new(pai_stabs,init(strpnew('"'+
  781. punitsymtable(current_module^.globalsymtable)^.name^+'",'+
  782. tostr(N_EINCL)+',0,0,0'))));
  783. punitsymtable(current_module^.globalsymtable)^.dbx_count_ok:={true}false;
  784. dbx_counter:=punitsymtable(current_module^.globalsymtable)^.prev_dbx_counter;
  785. do_count_dbx:=false;
  786. end;
  787. { now insert the units in the symtablestack }
  788. hp:=pused_unit(current_module^.used_units.first);
  789. while assigned(hp) do
  790. begin
  791. if (cs_debuginfo in aktmoduleswitches) and
  792. not hp^.is_stab_written then
  793. begin
  794. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  795. hp^.is_stab_written:=true;
  796. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  797. end;
  798. hp:=pused_unit(hp^.next);
  799. end;
  800. if current_module^.in_implementation and
  801. assigned(current_module^.localsymtable) then
  802. begin
  803. { all types }
  804. punitsymtable(current_module^.localsymtable)^.concattypestabto(debuglist);
  805. { and all local symbols}
  806. punitsymtable(current_module^.localsymtable)^.concatstabto(debuglist);
  807. end
  808. else if assigned(current_module^.globalsymtable) then
  809. begin
  810. { all types }
  811. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  812. { and all local symbols}
  813. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  814. end;
  815. end;
  816. {$Else GDB}
  817. begin
  818. end;
  819. {$EndIf GDB}
  820. procedure parse_implementation_uses(symt:Psymtable);
  821. begin
  822. if token=_USES then
  823. begin
  824. symt^.symtabletype:=unitsymtable;
  825. loadunits;
  826. symt^.symtabletype:=globalsymtable;
  827. {$ifdef DEBUG}
  828. test_symtablestack;
  829. {$endif DEBUG}
  830. end;
  831. end;
  832. procedure setupglobalswitches;
  833. procedure def_symbol(const s:string);
  834. var
  835. mac : pmacrosym;
  836. begin
  837. mac:=new(pmacrosym,init(s));
  838. mac^.defined:=true;
  839. Message1(parser_m_macro_defined,mac^.name);
  840. macros^.insert(mac);
  841. end;
  842. begin
  843. { can't have local browser when no global browser }
  844. if (cs_local_browser in aktmoduleswitches) and
  845. not(cs_browser in aktmoduleswitches) then
  846. aktmoduleswitches:=aktmoduleswitches-[cs_local_browser];
  847. { define a symbol in delphi,objfpc,tp,gpc mode }
  848. if (m_delphi in aktmodeswitches) then
  849. def_symbol('FPC_DELPHI')
  850. else
  851. if (m_tp in aktmodeswitches) then
  852. def_symbol('FPC_TP')
  853. else
  854. if (m_objfpc in aktmodeswitches) then
  855. def_symbol('FPC_OBJFPC')
  856. else
  857. if (m_gpc in aktmodeswitches) then
  858. def_symbol('FPC_GPC');
  859. { turn ansistrings on by default ? }
  860. if (m_default_ansistring in aktmodeswitches) then
  861. aktlocalswitches:=aktlocalswitches+[cs_ansistrings];
  862. end;
  863. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:psymtable);
  864. var
  865. stt : psymtable;
  866. begin
  867. {Generate a procsym for main}
  868. make_ref:=false;
  869. aktprocsym:=new(Pprocsym,init(name));
  870. { main are allways used }
  871. inc(aktprocsym^.refs);
  872. {Try to insert in in static symtable ! }
  873. stt:=symtablestack;
  874. symtablestack:=st;
  875. aktprocsym^.definition:=new(Pprocdef,init);
  876. symtablestack:=stt;
  877. aktprocsym^.definition^.proctypeoption:=options;
  878. aktprocsym^.definition^.setmangledname(target_os.cprefix+name);
  879. aktprocsym^.definition^.forwarddef:=false;
  880. make_ref:=true;
  881. { The localst is a local symtable. Change it into the static
  882. symtable }
  883. dispose(aktprocsym^.definition^.localst,done);
  884. aktprocsym^.definition^.localst:=st;
  885. { and insert the procsym in symtable }
  886. st^.insert(aktprocsym);
  887. { set some informations about the main program }
  888. with procinfo^ do
  889. begin
  890. returntype.setdef(voiddef);
  891. _class:=nil;
  892. para_offset:=8;
  893. framepointer:=frame_pointer;
  894. flags:=0;
  895. end;
  896. end;
  897. procedure proc_unit;
  898. function is_assembler_generated:boolean;
  899. begin
  900. is_assembler_generated:=(Errorcount=0) and
  901. not(
  902. codesegment^.empty and
  903. datasegment^.empty and
  904. bsssegment^.empty and
  905. ((importssection=nil) or importssection^.empty) and
  906. ((resourcesection=nil) or resourcesection^.empty) and
  907. ((resourcestringlist=nil) or resourcestringlist^.empty)
  908. );
  909. end;
  910. var
  911. {$ifdef fixLeaksOnError}
  912. names : Pstringcontainer;
  913. {$else fixLeaksOnError}
  914. names : Tstringcontainer;
  915. {$endif fixLeaksOnError}
  916. st : psymtable;
  917. unitst : punitsymtable;
  918. {$ifdef GDB}
  919. pu : pused_unit;
  920. {$endif GDB}
  921. {$ifndef Dont_use_double_checksum}
  922. store_crc,store_interface_crc : longint;
  923. {$endif}
  924. s1,s2 : ^string; {Saves stack space}
  925. force_init_final : boolean;
  926. begin
  927. consume(_UNIT);
  928. if Compile_Level=1 then
  929. IsExe:=false;
  930. if token=_ID then
  931. begin
  932. { create filenames and unit name }
  933. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  934. stringdispose(current_module^.modulename);
  935. current_module^.modulename:=stringdup(upper(pattern));
  936. { check for system unit }
  937. new(s1);
  938. new(s2);
  939. s1^:=upper(target_info.system_unit);
  940. s2^:=upper(SplitName(current_scanner^.inputfile^.name^));
  941. if (cs_compilesystem in aktmoduleswitches) then
  942. begin
  943. if ((length(current_module^.modulename^)>8) or
  944. (current_module^.modulename^<>s1^) or
  945. (current_module^.modulename^<>s2^)) then
  946. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  947. end
  948. else
  949. begin
  950. if (cs_check_unit_name in aktglobalswitches) and
  951. not((current_module^.modulename^=s2^) or
  952. ((length(current_module^.modulename^)>8) and
  953. (copy(current_module^.modulename^,1,8)=s2^))) then
  954. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  955. if (current_module^.modulename^=s1^) then
  956. Message(unit_w_switch_us_missed);
  957. end;
  958. dispose(s2);
  959. dispose(s1);
  960. end;
  961. consume(_ID);
  962. consume(_SEMICOLON);
  963. consume(_INTERFACE);
  964. { global switches are read, so further changes aren't allowed }
  965. current_module^.in_global:=false;
  966. { handle the global switches }
  967. setupglobalswitches;
  968. Message1(unit_u_start_parse_interface,current_module^.modulename^);
  969. { update status }
  970. status.currentmodule:=current_module^.modulename^;
  971. { maybe turn off m_objpas if we are compiling objpas }
  972. if (current_module^.modulename^='OBJPAS') then
  973. aktmodeswitches:=aktmodeswitches-[m_objpas];
  974. { this should be placed after uses !!}
  975. {$ifndef UseNiceNames}
  976. procprefix:='_'+current_module^.modulename^+'$$';
  977. {$else UseNiceNames}
  978. procprefix:='_'+tostr(length(current_module^.modulename^))+lowercase(current_module^.modulename^)+'_';
  979. {$endif UseNiceNames}
  980. parse_only:=true;
  981. { generate now the global symboltable }
  982. st:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  983. refsymtable:=st;
  984. unitst:=punitsymtable(st);
  985. { define first as local to overcome dependency conflicts }
  986. current_module^.localsymtable:=st;
  987. { the unit name must be usable as a unit specifier }
  988. { inside the unit itself (PM) }
  989. { this also forbids to have another symbol }
  990. { with the same name as the unit }
  991. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  992. { a unit compiled at command line must be inside the loaded_unit list }
  993. if (compile_level=1) then
  994. loaded_units.insert(current_module);
  995. { load default units, like the system unit }
  996. loaddefaultunits;
  997. { reset }
  998. make_ref:=true;
  999. lexlevel:=0;
  1000. { insert qualifier for the system unit (allows system.writeln) }
  1001. if not(cs_compilesystem in aktmoduleswitches) then
  1002. begin
  1003. if token=_USES then
  1004. begin
  1005. unitst^.symtabletype:=unitsymtable;
  1006. loadunits;
  1007. { has it been compiled at a higher level ?}
  1008. if current_module^.compiled then
  1009. begin
  1010. { this unit symtable is obsolete }
  1011. { dispose(unitst,done);
  1012. disposed as localsymtable !! }
  1013. RestoreUnitSyms;
  1014. exit;
  1015. end;
  1016. unitst^.symtabletype:=globalsymtable;
  1017. end;
  1018. { ... but insert the symbol table later }
  1019. st^.next:=symtablestack;
  1020. symtablestack:=st;
  1021. end
  1022. else
  1023. { while compiling a system unit, some types are directly inserted }
  1024. begin
  1025. st^.next:=symtablestack;
  1026. symtablestack:=st;
  1027. insert_intern_types(st);
  1028. end;
  1029. { now we know the place to insert the constants }
  1030. constsymtable:=symtablestack;
  1031. { move the global symtab from the temporary local to global }
  1032. current_module^.globalsymtable:=current_module^.localsymtable;
  1033. current_module^.localsymtable:=nil;
  1034. reset_global_defs;
  1035. { number all units, so we know if a unit is used by this unit or
  1036. needs to be added implicitly }
  1037. numberunits;
  1038. { ... parse the declarations }
  1039. Message1(parser_u_parsing_interface,current_module^.modulename^);
  1040. read_interface_declarations;
  1041. { leave when we got an error }
  1042. if (Errorcount>0) and not status.skip_error then
  1043. begin
  1044. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1045. status.skip_error:=true;
  1046. exit;
  1047. end;
  1048. {else in inteface its somatimes necessary even if unused
  1049. st^.allunitsused; }
  1050. {$ifdef New_GDB}
  1051. write_gdb_info;
  1052. {$endIf Def New_GDB}
  1053. {$ifndef Dont_use_double_checksum}
  1054. if not(cs_compilesystem in aktmoduleswitches) then
  1055. if (Errorcount=0) then
  1056. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),true);
  1057. {$endif Test_Double_checksum}
  1058. { Parse the implementation section }
  1059. consume(_IMPLEMENTATION);
  1060. current_module^.in_implementation:=true;
  1061. Message1(unit_u_start_parse_implementation,current_module^.modulename^);
  1062. parse_only:=false;
  1063. { generates static symbol table }
  1064. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1065. current_module^.localsymtable:=st;
  1066. { remove the globalsymtable from the symtable stack }
  1067. { to reinsert it after loading the implementation units }
  1068. symtablestack:=unitst^.next;
  1069. { we don't want implementation units symbols in unitsymtable !! PM }
  1070. refsymtable:=st;
  1071. { Read the implementation units }
  1072. parse_implementation_uses(unitst);
  1073. if current_module^.compiled then
  1074. begin
  1075. RestoreUnitSyms;
  1076. exit;
  1077. end;
  1078. { reset ranges/stabs in exported definitions }
  1079. reset_global_defs;
  1080. { All units are read, now give them a number }
  1081. numberunits;
  1082. { now we can change refsymtable }
  1083. refsymtable:=st;
  1084. { but reinsert the global symtable as lasts }
  1085. unitst^.next:=symtablestack;
  1086. symtablestack:=unitst;
  1087. {$ifndef DONOTCHAINOPERATORS}
  1088. symtablestack^.chainoperators;
  1089. {$endif DONOTCHAINOPERATORS}
  1090. {$ifdef DEBUG}
  1091. test_symtablestack;
  1092. {$endif DEBUG}
  1093. constsymtable:=symtablestack;
  1094. {$ifdef Splitheap}
  1095. if testsplit then
  1096. begin
  1097. Split_Heap;
  1098. allow_special:=true;
  1099. Switch_to_temp_heap;
  1100. end;
  1101. { it will report all crossings }
  1102. allow_special:=false;
  1103. {$endif Splitheap}
  1104. Message1(parser_u_parsing_implementation,current_module^.modulename^);
  1105. { Compile the unit }
  1106. codegen_newprocedure;
  1107. gen_main_procsym(current_module^.modulename^+'_init',potype_unitinit,st);
  1108. {$ifdef fixLeaksOnError}
  1109. new(names,init);
  1110. strContStack.push(names);
  1111. names^.insert('INIT$$'+current_module^.modulename^);
  1112. names^.insert(target_os.cprefix+current_module^.modulename^+'_init');
  1113. compile_proc_body(names^,true,false);
  1114. if names <> PstringContainer(strContStack.pop) then
  1115. writeln('Problem with strContStack in pmodules (1)');
  1116. dispose(names,done);
  1117. {$else fixLeaksOnError}
  1118. names.init;
  1119. names.insert('INIT$$'+current_module^.modulename^);
  1120. names.insert(target_os.cprefix+current_module^.modulename^+'_init');
  1121. compile_proc_body(names,true,false);
  1122. names.done;
  1123. {$endif fixLeaksOnError}
  1124. codegen_doneprocedure;
  1125. { avoid self recursive destructor call !! PM }
  1126. aktprocsym^.definition^.localst:=nil;
  1127. { if the unit contains ansi/widestrings, initialization and
  1128. finalization code must be forced }
  1129. force_init_final:=needs_init_final(current_module^.globalsymtable)
  1130. or needs_init_final(current_module^.localsymtable);
  1131. { should we force unit initialization? }
  1132. { this is a hack, but how can it be done better ? }
  1133. if force_init_final and ((current_module^.flags and uf_init)=0) then
  1134. begin
  1135. current_module^.flags:=current_module^.flags or uf_init;
  1136. { now we can insert a cut }
  1137. if (cs_create_smart in aktmoduleswitches) then
  1138. codesegment^.concat(new(pai_cut,init));
  1139. genimplicitunitinit(codesegment);
  1140. end;
  1141. { finalize? }
  1142. if token=_FINALIZATION then
  1143. begin
  1144. { set module options }
  1145. current_module^.flags:=current_module^.flags or uf_finalize;
  1146. { Compile the finalize }
  1147. codegen_newprocedure;
  1148. gen_main_procsym(current_module^.modulename^+'_finalize',potype_unitfinalize,st);
  1149. {$ifdef fixLeaksOnError}
  1150. new(names,init);
  1151. strContStack.push(names);
  1152. names^.insert('FINALIZE$$'+current_module^.modulename^);
  1153. names^.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1154. compile_proc_body(names^,true,false);
  1155. if names <> PstringContainer(strContStack.pop) then
  1156. writeln('Problem with strContStack in pmodules (2)');
  1157. dispose(names,done);
  1158. {$else fixLeaksOnError}
  1159. names.init;
  1160. names.insert('FINALIZE$$'+current_module^.modulename^);
  1161. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1162. compile_proc_body(names,true,false);
  1163. names.done;
  1164. {$endif fixLeaksOnError}
  1165. codegen_doneprocedure;
  1166. end
  1167. else if force_init_final then
  1168. begin
  1169. current_module^.flags:=current_module^.flags or uf_finalize;
  1170. { now we can insert a cut }
  1171. if (cs_create_smart in aktmoduleswitches) then
  1172. codesegment^.concat(new(pai_cut,init));
  1173. genimplicitunitfinal(codesegment);
  1174. end;
  1175. { the last char should always be a point }
  1176. consume(_POINT);
  1177. If ResourceStrings^.ResStrCount>0 then
  1178. begin
  1179. ResourceStrings^.CreateResourceStringList;
  1180. current_module^.flags:=current_module^.flags or uf_has_resources;
  1181. { only write if no errors found }
  1182. if (Errorcount=0) then
  1183. ResourceStrings^.WriteResourceFile(Current_module^.ModuleName^);
  1184. end;
  1185. { avoid self recursive destructor call !! PM }
  1186. aktprocsym^.definition^.localst:=nil;
  1187. { absence does not matter here !! }
  1188. aktprocsym^.definition^.forwarddef:=false;
  1189. { test static symtable }
  1190. if (Errorcount=0) then
  1191. begin
  1192. st^.allsymbolsused;
  1193. st^.allunitsused;
  1194. st^.allprivatesused;
  1195. end;
  1196. { size of the static data }
  1197. datasize:=st^.datasize;
  1198. {$ifdef GDB}
  1199. { add all used definitions even for implementation}
  1200. if (cs_debuginfo in aktmoduleswitches) then
  1201. begin
  1202. {$IfnDef New_GDB}
  1203. if assigned(current_module^.globalsymtable) then
  1204. begin
  1205. { all types }
  1206. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  1207. { and all local symbols}
  1208. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  1209. end;
  1210. { all local types }
  1211. punitsymtable(st)^.concattypestabto(debuglist);
  1212. { and all local symbols}
  1213. st^.concatstabto(debuglist);
  1214. {$else New_GDB}
  1215. write_gdb_info;
  1216. {$endIf Def New_GDB}
  1217. end;
  1218. {$endif GDB}
  1219. reset_global_defs;
  1220. { tests, if all (interface) forwards are resolved }
  1221. if (Errorcount=0) then
  1222. begin
  1223. symtablestack^.check_forwards;
  1224. symtablestack^.allprivatesused;
  1225. end;
  1226. { now we have a correct unit, change the symtable type }
  1227. current_module^.in_implementation:=false;
  1228. symtablestack^.symtabletype:=unitsymtable;
  1229. {$ifdef GDB}
  1230. punitsymtable(symtablestack)^.is_stab_written:=false;
  1231. {$endif GDB}
  1232. { leave when we got an error }
  1233. if (Errorcount>0) and not status.skip_error then
  1234. begin
  1235. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1236. status.skip_error:=true;
  1237. closecurrentppu;
  1238. exit;
  1239. end;
  1240. { generate imports }
  1241. if current_module^.uses_imports then
  1242. importlib^.generatelib;
  1243. { insert own objectfile, or say that it's in a library
  1244. (no check for an .o when loading) }
  1245. if is_assembler_generated then
  1246. insertobjectfile
  1247. else
  1248. current_module^.flags:=current_module^.flags or uf_no_link;
  1249. if cs_local_browser in aktmoduleswitches then
  1250. current_module^.localsymtable:=refsymtable;
  1251. { Write out the ppufile }
  1252. {$ifndef Dont_use_double_checksum}
  1253. store_interface_crc:=current_module^.interface_crc;
  1254. store_crc:=current_module^.crc;
  1255. {$endif Test_Double_checksum}
  1256. if (Errorcount=0) then
  1257. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),false);
  1258. {$ifndef Dont_use_double_checksum}
  1259. if not(cs_compilesystem in aktmoduleswitches) then
  1260. if store_interface_crc<>current_module^.interface_crc then
  1261. Do_comment(V_Warning,current_module^.ppufilename^+' Interface CRC changed '+
  1262. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1263. {$ifdef EXTDEBUG}
  1264. if not(cs_compilesystem in aktmoduleswitches) then
  1265. if (store_crc<>current_module^.crc) and simplify_ppu then
  1266. Do_comment(V_Warning,current_module^.ppufilename^+' implementation CRC changed '+
  1267. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1268. {$endif EXTDEBUG}
  1269. {$endif ndef Dont_use_Double_checksum}
  1270. { must be done only after local symtable ref stores !! }
  1271. closecurrentppu;
  1272. {$ifdef GDB}
  1273. pu:=pused_unit(usedunits.first);
  1274. while assigned(pu) do
  1275. begin
  1276. if assigned(pu^.u^.globalsymtable) then
  1277. punitsymtable(pu^.u^.globalsymtable)^.is_stab_written:=false;
  1278. pu:=pused_unit(pu^.next);
  1279. end;
  1280. {$endif GDB}
  1281. { remove static symtable (=refsymtable) here to save some mem }
  1282. if not (cs_local_browser in aktmoduleswitches) then
  1283. begin
  1284. dispose(st,done);
  1285. current_module^.localsymtable:=nil;
  1286. end;
  1287. RestoreUnitSyms;
  1288. if is_assembler_generated then
  1289. begin
  1290. { finish asmlist by adding segment starts }
  1291. insertsegment;
  1292. { assemble }
  1293. create_objectfile;
  1294. end;
  1295. { leave when we got an error }
  1296. if (Errorcount>0) and not status.skip_error then
  1297. begin
  1298. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1299. status.skip_error:=true;
  1300. exit;
  1301. end;
  1302. end;
  1303. procedure proc_program(islibrary : boolean);
  1304. var
  1305. st : psymtable;
  1306. hp : pmodule;
  1307. {$ifdef fixLeaksOnError}
  1308. names : Pstringcontainer;
  1309. {$else fixLeaksOnError}
  1310. names : Tstringcontainer;
  1311. {$endif fixLeaksOnError}
  1312. begin
  1313. DLLsource:=islibrary;
  1314. IsExe:=true;
  1315. parse_only:=false;
  1316. { relocation works only without stabs under win32 !! PM }
  1317. { internal assembler uses rva for stabs info
  1318. so it should work with relocated DLLs }
  1319. if RelocSection and
  1320. (target_info.target=target_i386_win32) and
  1321. (target_info.assem<>as_i386_pecoff) then
  1322. begin
  1323. aktglobalswitches:=aktglobalswitches+[cs_link_strip];
  1324. { Warning stabs info does not work with reloc section !! }
  1325. if cs_debuginfo in aktmoduleswitches then
  1326. begin
  1327. Message1(parser_w_parser_reloc_no_debug,current_module^.mainsource^);
  1328. Message(parser_w_parser_win32_debug_needs_WN);
  1329. aktmoduleswitches:=aktmoduleswitches-[cs_debuginfo];
  1330. end;
  1331. end;
  1332. { get correct output names }
  1333. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  1334. if islibrary then
  1335. begin
  1336. consume(_LIBRARY);
  1337. stringdispose(current_module^.modulename);
  1338. current_module^.modulename:=stringdup(pattern);
  1339. current_module^.islibrary:=true;
  1340. exportlib^.preparelib(pattern);
  1341. consume(_ID);
  1342. consume(_SEMICOLON);
  1343. end
  1344. else
  1345. { is there an program head ? }
  1346. if token=_PROGRAM then
  1347. begin
  1348. consume(_PROGRAM);
  1349. stringdispose(current_module^.modulename);
  1350. current_module^.modulename:=stringdup(pattern);
  1351. if (target_info.target=target_i386_WIN32) then
  1352. exportlib^.preparelib(pattern);
  1353. consume(_ID);
  1354. if token=_LKLAMMER then
  1355. begin
  1356. consume(_LKLAMMER);
  1357. idlist;
  1358. consume(_RKLAMMER);
  1359. end;
  1360. consume(_SEMICOLON);
  1361. end
  1362. else if (target_info.target=target_i386_WIN32) then
  1363. exportlib^.preparelib(current_module^.modulename^);
  1364. { global switches are read, so further changes aren't allowed }
  1365. current_module^.in_global:=false;
  1366. { setup things using the global switches }
  1367. setupglobalswitches;
  1368. { set implementation flag }
  1369. current_module^.in_implementation:=true;
  1370. { insert after the unit symbol tables the static symbol table }
  1371. { of the program }
  1372. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1373. current_module^.localsymtable:=st;
  1374. symtablestack:=st;
  1375. refsymtable:=st;
  1376. { necessary for browser }
  1377. loaded_units.insert(current_module);
  1378. { load standard units (system,objpas,profile unit) }
  1379. loaddefaultunits;
  1380. { reset }
  1381. lexlevel:=0;
  1382. {Load the units used by the program we compile.}
  1383. if token=_USES then
  1384. loadunits;
  1385. {$ifndef DONOTCHAINOPERATORS}
  1386. symtablestack^.chainoperators;
  1387. {$endif DONOTCHAINOPERATORS}
  1388. { reset ranges/stabs in exported definitions }
  1389. reset_global_defs;
  1390. { All units are read, now give them a number }
  1391. numberunits;
  1392. {Insert the name of the main program into the symbol table.}
  1393. if current_module^.modulename^<>'' then
  1394. {st^.insert(new(pprogramsym,init(current_module^.modulename^)));}
  1395. st^.insert(new(punitsym,init(current_module^.modulename^,punitsymtable(st))));
  1396. { ...is also constsymtable, this is the symtable where }
  1397. { the elements of enumeration types are inserted }
  1398. constsymtable:=st;
  1399. Message1(parser_u_parsing_implementation,current_module^.mainsource^);
  1400. { reset }
  1401. procprefix:='';
  1402. {The program intialization needs an alias, so it can be called
  1403. from the bootstrap code.}
  1404. codegen_newprocedure;
  1405. gen_main_procsym('main',potype_proginit,st);
  1406. {$ifdef fixLeaksOnError}
  1407. new(names,init);
  1408. strContStack.push(names);
  1409. names^.insert('program_init');
  1410. names^.insert('PASCALMAIN');
  1411. names^.insert(target_os.cprefix+'main');
  1412. {$ifdef m68k}
  1413. if target_info.target=target_m68k_PalmOS then
  1414. names^.insert('PilotMain');
  1415. {$endif m68k}
  1416. compile_proc_body(names^,true,false);
  1417. if names <> PstringContainer(strContStack.pop) then
  1418. writeln('Problem with strContStack in pmodules (1)');
  1419. dispose(names,done);
  1420. {$else fixLeaksOnError}
  1421. names.init;
  1422. names.insert('program_init');
  1423. names.insert('PASCALMAIN');
  1424. names.insert(target_os.cprefix+'main');
  1425. {$ifdef m68k}
  1426. if target_info.target=target_m68k_PalmOS then
  1427. names.insert('PilotMain');
  1428. {$endif m68k}
  1429. compile_proc_body(names,true,false);
  1430. names.done;
  1431. {$endif fixLeaksOnError}
  1432. { avoid self recursive destructor call !! PM }
  1433. aktprocsym^.definition^.localst:=nil;
  1434. { consider these symbols as global ones }
  1435. { for browser }
  1436. current_module^.globalsymtable:=current_module^.localsymtable;
  1437. current_module^.localsymtable:=nil;
  1438. If ResourceStrings^.ResStrCount>0 then
  1439. begin
  1440. ResourceStrings^.CreateResourceStringList;
  1441. { only write if no errors found }
  1442. if (Errorcount=0) then
  1443. ResourceStrings^.WriteResourceFile(Current_module^.ModuleName^);
  1444. end;
  1445. codegen_doneprocedure;
  1446. { finalize? }
  1447. if token=_FINALIZATION then
  1448. begin
  1449. { set module options }
  1450. current_module^.flags:=current_module^.flags or uf_finalize;
  1451. { Compile the finalize }
  1452. codegen_newprocedure;
  1453. gen_main_procsym(current_module^.modulename^+'_finalize',potype_unitfinalize,st);
  1454. {$ifdef fixLeaksOnError}
  1455. new(names,init);
  1456. strContStack.push(names);
  1457. names^.insert('FINALIZE$$'+current_module^.modulename^);
  1458. names^.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1459. compile_proc_body(names^,true,false);
  1460. if names <> PstringContainer(strContStack.pop) then
  1461. writeln('Problem with strContStack in pmodules (1)');
  1462. dispose(names,done);
  1463. {$else fixLeaksOnError}
  1464. names.init;
  1465. names.insert('FINALIZE$$'+current_module^.modulename^);
  1466. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  1467. compile_proc_body(names,true,false);
  1468. names.done;
  1469. {$endif fixLeaksOnError}
  1470. codegen_doneprocedure;
  1471. end;
  1472. { consume the last point }
  1473. consume(_POINT);
  1474. {$ifdef New_GDB}
  1475. write_gdb_info;
  1476. {$endIf Def New_GDB}
  1477. { leave when we got an error }
  1478. if (Errorcount>0) and not status.skip_error then
  1479. begin
  1480. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1481. status.skip_error:=true;
  1482. exit;
  1483. end;
  1484. { test static symtable }
  1485. if (Errorcount=0) then
  1486. begin
  1487. st^.allsymbolsused;
  1488. st^.allunitsused;
  1489. st^.allprivatesused;
  1490. end;
  1491. { generate imports }
  1492. if current_module^.uses_imports then
  1493. importlib^.generatelib;
  1494. if islibrary or
  1495. (target_info.target=target_i386_WIN32) then
  1496. exportlib^.generatelib;
  1497. { insert heap }
  1498. insertResourceTablesTable;
  1499. insertinitfinaltable;
  1500. insertheap;
  1501. inserttargetspecific;
  1502. datasize:=symtablestack^.datasize;
  1503. { finish asmlist by adding segment starts }
  1504. insertsegment;
  1505. { insert own objectfile }
  1506. insertobjectfile;
  1507. { assemble and link }
  1508. create_objectfile;
  1509. { leave when we got an error }
  1510. if (Errorcount>0) and not status.skip_error then
  1511. begin
  1512. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1513. status.skip_error:=true;
  1514. exit;
  1515. end;
  1516. { create the executable when we are at level 1 }
  1517. if (compile_level=1) then
  1518. begin
  1519. { insert all .o files from all loaded units }
  1520. hp:=pmodule(loaded_units.first);
  1521. while assigned(hp) do
  1522. begin
  1523. Linker^.AddModuleFiles(hp);
  1524. hp:=pmodule(hp^.next);
  1525. end;
  1526. { write .def file }
  1527. if (cs_link_deffile in aktglobalswitches) then
  1528. deffile.writefile;
  1529. { finally we can create a executable }
  1530. if (not current_module^.is_unit) then
  1531. begin
  1532. if DLLSource then
  1533. Linker^.MakeSharedLibrary
  1534. else
  1535. Linker^.MakeExecutable;
  1536. end;
  1537. end;
  1538. end;
  1539. end.
  1540. {
  1541. $Log$
  1542. Revision 1.4 2000-08-21 11:27:44 pierre
  1543. * fix the stabs problems
  1544. Revision 1.3 2000/07/13 12:08:26 michael
  1545. + patched to 1.1.0 with former 1.09patch from peter
  1546. Revision 1.2 2000/07/13 11:32:45 michael
  1547. + removed logs
  1548. }