pmodules.pas 61 KB

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