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