pmodules.pas 61 KB

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