pmodules.pas 62 KB

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