2
0

pmodules.pas 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.inc}
  20. interface
  21. procedure proc_unit;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. globtype,version,systems,tokens,
  26. cutils,cclasses,comphook,
  27. globals,verbose,fmodule,finput,fppu,
  28. symconst,symbase,symtype,symdef,symsym,symtable,
  29. aasmtai,aasmcpu,aasmbase,
  30. cgbase,cpuinfo,cgobj,
  31. nbas,
  32. link,assemble,import,export,gendef,ppu,comprsrc,
  33. cresstr,procinfo,
  34. {$ifdef GDB}
  35. gdb,
  36. {$endif GDB}
  37. scanner,pbase,pexpr,psystem,psub;
  38. procedure fixseg(p:TAAsmoutput;sec:TSection);
  39. begin
  40. p.insert(Tai_section.Create(sec));
  41. if (cs_create_smart in aktmoduleswitches) then
  42. p.insert(Tai_cut.Create);
  43. p.concat(Tai_section.Create(sec_none));
  44. end;
  45. procedure create_objectfile;
  46. var
  47. DLLScanner : TDLLScanner;
  48. s : string;
  49. KeepShared : TStringList;
  50. begin
  51. { try to create import entries from system dlls }
  52. if target_info.DllScanSupported and
  53. (not current_module.linkOtherSharedLibs.Empty) then
  54. begin
  55. { Init DLLScanner }
  56. if assigned(CDLLScanner[target_info.system]) then
  57. DLLScanner:=CDLLScanner[target_info.system].Create
  58. else
  59. internalerror(200104121);
  60. KeepShared:=TStringList.Create;
  61. { Walk all shared libs }
  62. While not current_module.linkOtherSharedLibs.Empty do
  63. begin
  64. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  65. if not DLLScanner.scan(s) then
  66. KeepShared.Concat(s);
  67. end;
  68. DLLscanner.Free;
  69. { Recreate import section }
  70. if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  71. begin
  72. if assigned(importssection)then
  73. importssection.clear
  74. else
  75. importssection:=taasmoutput.Create;
  76. importlib.generatelib;
  77. end;
  78. { Readd the not processed files }
  79. while not KeepShared.Empty do
  80. begin
  81. s:=KeepShared.GetFirst;
  82. current_module.linkOtherSharedLibs.add(s,link_allways);
  83. end;
  84. KeepShared.Free;
  85. end;
  86. { create the .s file and assemble it }
  87. GenerateAsm(false);
  88. { Also create a smartlinked version ? }
  89. if (cs_create_smart in aktmoduleswitches) then
  90. begin
  91. { regenerate the importssection for win32 }
  92. if assigned(importssection) and
  93. (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  94. begin
  95. importsSection.clear;
  96. importlib.generatesmartlib;
  97. end;
  98. GenerateAsm(true);
  99. if target_asm.needar then
  100. Linker.MakeStaticLibrary;
  101. end;
  102. { resource files }
  103. CompileResourceFiles;
  104. end;
  105. procedure insertobjectfile;
  106. { Insert the used object file for this unit in the used list for this unit }
  107. begin
  108. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  109. current_module.flags:=current_module.flags or uf_static_linked;
  110. if (cs_create_smart in aktmoduleswitches) then
  111. begin
  112. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  113. current_module.flags:=current_module.flags or uf_smart_linked;
  114. end;
  115. end;
  116. procedure insertsegment;
  117. begin
  118. { Insert Ident of the compiler }
  119. if (not (cs_create_smart in aktmoduleswitches))
  120. {$ifndef EXTDEBUG}
  121. and (not current_module.is_unit)
  122. {$endif}
  123. then
  124. begin
  125. { align the first data }
  126. dataSegment.insert(Tai_align.Create(const_align(32)));
  127. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  128. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  129. end;
  130. { align code segment }
  131. codeSegment.concat(Tai_align.Create(aktalignment.procalign));
  132. { Insert start and end of sections }
  133. fixseg(codesegment,sec_code);
  134. fixseg(datasegment,sec_data);
  135. fixseg(bsssegment,sec_bss);
  136. { we should use .rdata section for these two no ?
  137. .rdata is a read only data section (PM) }
  138. fixseg(rttilist,sec_data);
  139. fixseg(consts,sec_data);
  140. fixseg(picdata,sec_data);
  141. if assigned(resourcestringlist) then
  142. fixseg(resourcestringlist,sec_data);
  143. {$ifdef GDB}
  144. if assigned(debuglist) then
  145. begin
  146. debugList.insert(Tai_symbol.Createname('gcc2_compiled',AT_FUNCTION,0));
  147. debugList.insert(Tai_symbol.Createname('fpc_compiled',AT_FUNCTION,0));
  148. fixseg(debuglist,sec_code);
  149. end;
  150. {$endif GDB}
  151. end;
  152. procedure InsertThreadvarTablesTable;
  153. var
  154. hp : tused_unit;
  155. ltvTables : taasmoutput;
  156. count : longint;
  157. begin
  158. ltvTables:=TAAsmOutput.Create;
  159. count:=0;
  160. hp:=tused_unit(usedunits.first);
  161. while assigned(hp) do
  162. begin
  163. If (hp.u.flags and uf_threadvars)=uf_threadvars then
  164. begin
  165. ltvTables.concat(Tai_const_symbol.Createname(make_mangledname('THREADVARLIST',hp.u.globalsymtable,''),AT_DATA,0));
  166. inc(count);
  167. end;
  168. hp:=tused_unit(hp.next);
  169. end;
  170. { Add program threadvars, if any }
  171. If (current_module.flags and uf_threadvars)=uf_threadvars then
  172. begin
  173. ltvTables.concat(Tai_const_symbol.Createname(make_mangledname('THREADVARLIST',current_module.localsymtable,''),AT_DATA,0));
  174. inc(count);
  175. end;
  176. { TableCount }
  177. ltvTables.insert(Tai_const.Create_32bit(count));
  178. ltvTables.insert(Tai_symbol.Createname_global('FPC_THREADVARTABLES',AT_DATA,0));
  179. ltvTables.insert(Tai_align.Create(const_align(pointer_size)));
  180. ltvTables.concat(Tai_symbol_end.Createname('FPC_THREADVARTABLES'));
  181. { insert in data segment }
  182. if (cs_create_smart in aktmoduleswitches) then
  183. dataSegment.concat(Tai_cut.Create);
  184. dataSegment.concatlist(ltvTables);
  185. ltvTables.free;
  186. end;
  187. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  188. var
  189. ltvTable : taasmoutput;
  190. begin
  191. ltvTable:=taasmoutput(arg);
  192. if (tsym(p).typ=varsym) and
  193. (vo_is_thread_var in tvarsym(p).varoptions) then
  194. begin
  195. { address of threadvar }
  196. ltvTable.concat(tai_const_symbol.Createname(tvarsym(p).mangledname,AT_DATA,0));
  197. { size of threadvar }
  198. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  199. end;
  200. end;
  201. procedure InsertThreadvars;
  202. var
  203. s : string;
  204. ltvTable : TAAsmoutput;
  205. begin
  206. ltvTable:=TAAsmoutput.create;
  207. if assigned(current_module.globalsymtable) then
  208. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  209. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  210. if ltvTable.first<>nil then
  211. begin
  212. s:=make_mangledname('THREADVARLIST',current_module.localsymtable,'');
  213. { add begin and end of the list }
  214. ltvTable.insert(tai_symbol.Createname_global(s,AT_DATA,0));
  215. ltvTable.concat(tai_const.create_ptr(0)); { end of list marker }
  216. ltvTable.concat(tai_symbol_end.createname(s));
  217. if (cs_create_smart in aktmoduleswitches) then
  218. dataSegment.concat(Tai_cut.Create);
  219. dataSegment.concatlist(ltvTable);
  220. current_module.flags:=current_module.flags or uf_threadvars;
  221. end;
  222. ltvTable.Free;
  223. end;
  224. Procedure InsertResourceTablesTable;
  225. var
  226. hp : tused_unit;
  227. ResourceStringTables : taasmoutput;
  228. count : longint;
  229. begin
  230. ResourceStringTables:=TAAsmOutput.Create;
  231. count:=0;
  232. hp:=tused_unit(usedunits.first);
  233. while assigned(hp) do
  234. begin
  235. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  236. begin
  237. ResourceStringTables.concat(Tai_const_symbol.Createname(make_mangledname('RESOURCESTRINGLIST',hp.u.globalsymtable,''),AT_DATA,0));
  238. inc(count);
  239. end;
  240. hp:=tused_unit(hp.next);
  241. end;
  242. { Add program resources, if any }
  243. If ResourceStringList<>Nil then
  244. begin
  245. ResourceStringTables.concat(Tai_const_symbol.Createname(make_mangledname('RESOURCESTRINGLIST',current_module.localsymtable,''),AT_DATA,0));
  246. Inc(Count);
  247. end;
  248. { TableCount }
  249. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  250. ResourceStringTables.insert(Tai_symbol.Createname_global('FPC_RESOURCESTRINGTABLES',AT_DATA,0));
  251. ResourceStringTables.insert(Tai_align.Create(const_align(4)));
  252. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  253. { insert in data segment }
  254. if (cs_create_smart in aktmoduleswitches) then
  255. dataSegment.concat(Tai_cut.Create);
  256. dataSegment.concatlist(ResourceStringTables);
  257. ResourceStringTables.free;
  258. end;
  259. procedure InsertInitFinalTable;
  260. var
  261. hp : tused_unit;
  262. unitinits : taasmoutput;
  263. count : longint;
  264. begin
  265. unitinits:=TAAsmOutput.Create;
  266. count:=0;
  267. hp:=tused_unit(usedunits.first);
  268. while assigned(hp) do
  269. begin
  270. { call the unit init code and make it external }
  271. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  272. begin
  273. if (hp.u.flags and uf_init)<>0 then
  274. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('INIT$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  275. else
  276. unitinits.concat(Tai_const.Create_ptr(0));
  277. if (hp.u.flags and uf_finalize)<>0 then
  278. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('FINALIZE$',hp.u.globalsymtable,''),AT_FUNCTION,0))
  279. else
  280. unitinits.concat(Tai_const.Create_ptr(0));
  281. inc(count);
  282. end;
  283. hp:=tused_unit(hp.next);
  284. end;
  285. { Insert initialization/finalization of the program }
  286. if (current_module.flags and (uf_init or uf_finalize))<>0 then
  287. begin
  288. if (current_module.flags and uf_init)<>0 then
  289. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('INIT$',current_module.localsymtable,''),AT_FUNCTION,0))
  290. else
  291. unitinits.concat(Tai_const.Create_ptr(0));
  292. if (current_module.flags and uf_finalize)<>0 then
  293. unitinits.concat(Tai_const_symbol.Createname(make_mangledname('FINALIZE$',current_module.localsymtable,''),AT_FUNCTION,0))
  294. else
  295. unitinits.concat(Tai_const.Create_ptr(0));
  296. inc(count);
  297. end;
  298. { TableCount,InitCount }
  299. unitinits.insert(Tai_const.Create_32bit(0));
  300. unitinits.insert(Tai_const.Create_32bit(count));
  301. unitinits.insert(Tai_symbol.Createname_global('INITFINAL',AT_DATA,0));
  302. unitinits.insert(Tai_align.Create(const_align(4)));
  303. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  304. { insert in data segment }
  305. if (cs_create_smart in aktmoduleswitches) then
  306. dataSegment.concat(Tai_cut.Create);
  307. dataSegment.concatlist(unitinits);
  308. unitinits.free;
  309. end;
  310. procedure insertheap;
  311. begin
  312. if (cs_create_smart in aktmoduleswitches) then
  313. begin
  314. bssSegment.concat(Tai_cut.Create);
  315. dataSegment.concat(Tai_cut.Create);
  316. end;
  317. { On the Macintosh Classic M68k Architecture
  318. The Heap variable is simply a POINTER to the
  319. real HEAP. The HEAP must be set up by the RTL
  320. and must store the pointer in this value.
  321. On OS/2 the heap is also intialized by the RTL. We do
  322. not output a pointer }
  323. case target_info.system of
  324. system_i386_OS2,system_i386_EMX:
  325. ;
  326. system_powerpc_macos:
  327. ;
  328. system_i386_watcom:
  329. ;
  330. system_alpha_linux:
  331. ;
  332. system_m68k_Mac:
  333. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  334. system_m68k_PalmOS:
  335. ;
  336. else
  337. begin
  338. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  339. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  340. end;
  341. end;
  342. {$ifdef m68k}
  343. if target_info.system<>system_m68k_PalmOS then
  344. begin
  345. dataSegment.concat(Tai_align.Create(const_align(4)));
  346. dataSegment.concat(Tai_symbol.Createname_global('HEAPSIZE',AT_DATA,4));
  347. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  348. end;
  349. {$else m68k}
  350. dataSegment.concat(Tai_align.Create(const_align(4)));
  351. dataSegment.concat(Tai_symbol.Createname_global('HEAPSIZE',AT_DATA,4));
  352. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  353. {$endif m68k}
  354. end;
  355. procedure insertstacklength;
  356. begin
  357. { stacksize can be specified and is now simulated }
  358. dataSegment.concat(Tai_align.Create(const_align(4)));
  359. dataSegment.concat(Tai_symbol.Createname_global('__stklen',AT_DATA,4));
  360. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  361. end;
  362. procedure loaddefaultunits;
  363. procedure AddUnit(const s:string);
  364. var
  365. hp : tppumodule;
  366. unitsym : tunitsym;
  367. begin
  368. { load unit }
  369. hp:=registerunit(current_module,s,'');
  370. hp.loadppu;
  371. hp.adddependency(current_module);
  372. { add to symtable stack }
  373. tsymtable(hp.globalsymtable).next:=symtablestack;
  374. symtablestack:=hp.globalsymtable;
  375. { insert unitsym }
  376. unitsym:=tunitsym.create(s,hp.globalsymtable);
  377. inc(unitsym.refs);
  378. refsymtable.insert(unitsym);
  379. { add to used units }
  380. current_module.addusedunit(hp,false,unitsym);
  381. end;
  382. begin
  383. { are we compiling the system unit? }
  384. if (cs_compilesystem in aktmoduleswitches) then
  385. begin
  386. { create system defines }
  387. createconstdefs;
  388. { we don't need to reset anything, it's already done in parser.pas }
  389. exit;
  390. end;
  391. { insert the system unit, it is allways the first }
  392. Symtablestack:=nil;
  393. AddUnit('System');
  394. SystemUnit:=TGlobalSymtable(Symtablestack);
  395. { read default constant definitions }
  396. make_ref:=false;
  397. readconstdefs;
  398. make_ref:=true;
  399. {$ifdef cpufpemu}
  400. { Floating point emulation unit? }
  401. if (cs_fp_emulation in aktmoduleswitches) then
  402. AddUnit('SoftFpu');
  403. {$endif cpufpemu}
  404. { Thread support unit? }
  405. if (cs_threading in aktmoduleswitches) then
  406. AddUnit('SysThrds');
  407. { Objpas unit? }
  408. if m_objpas in aktmodeswitches then
  409. AddUnit('ObjPas');
  410. { Profile unit? Needed for go32v2 only }
  411. if (cs_profile in aktmoduleswitches) and
  412. (target_info.system in [system_i386_go32v2,system_i386_watcom]) then
  413. AddUnit('Profile');
  414. { Units only required for main module }
  415. if not(current_module.is_unit) then
  416. begin
  417. { Heaptrc unit }
  418. if (cs_gdb_heaptrc in aktglobalswitches) then
  419. AddUnit('HeapTrc');
  420. { Lineinfo unit }
  421. if (cs_gdb_lineinfo in aktglobalswitches) then
  422. AddUnit('LineInfo');
  423. end;
  424. { save default symtablestack }
  425. defaultsymtablestack:=symtablestack;
  426. end;
  427. procedure loadunits;
  428. var
  429. s,sorg : stringid;
  430. fn : string;
  431. pu : tused_unit;
  432. hp2 : tmodule;
  433. hp3 : tsymtable;
  434. unitsym : tunitsym;
  435. begin
  436. consume(_USES);
  437. {$ifdef DEBUG}
  438. test_symtablestack;
  439. {$endif DEBUG}
  440. repeat
  441. s:=pattern;
  442. sorg:=orgpattern;
  443. consume(_ID);
  444. { support "<unit> in '<file>'" construct, but not for tp7 }
  445. if not(m_tp7 in aktmodeswitches) then
  446. begin
  447. if try_to_consume(_OP_IN) then
  448. fn:=FixFileName(get_stringconst)
  449. else
  450. fn:='';
  451. end;
  452. { Give a warning if objpas is loaded }
  453. if s='OBJPAS' then
  454. Message(parser_w_no_objpas_use_mode);
  455. { Using the unit itself is not possible }
  456. if (s<>current_module.modulename^) then
  457. begin
  458. { check if the unit is already used }
  459. hp2:=nil;
  460. pu:=tused_unit(current_module.used_units.first);
  461. while assigned(pu) do
  462. begin
  463. if (pu.u.modulename^=s) then
  464. begin
  465. hp2:=pu.u;
  466. break;
  467. end;
  468. pu:=tused_unit(pu.next);
  469. end;
  470. if not assigned(hp2) then
  471. hp2:=registerunit(current_module,sorg,fn)
  472. else
  473. Message1(sym_e_duplicate_id,s);
  474. { Create unitsym, we need to use the name as specified, we
  475. can not use the modulename because that can be different
  476. when -Un is used }
  477. unitsym:=tunitsym.create(sorg,nil);
  478. refsymtable.insert(unitsym);
  479. { the current module uses the unit hp2 }
  480. current_module.addusedunit(hp2,true,unitsym);
  481. end
  482. else
  483. Message1(sym_e_duplicate_id,s);
  484. if token=_COMMA then
  485. begin
  486. pattern:='';
  487. consume(_COMMA);
  488. end
  489. else
  490. break;
  491. until false;
  492. consume(_SEMICOLON);
  493. { Load the units }
  494. pu:=tused_unit(current_module.used_units.first);
  495. while assigned(pu) do
  496. begin
  497. { Only load the units that are in the current
  498. (interface/implementation) uses clause }
  499. if pu.in_uses and
  500. (pu.in_interface=current_module.in_interface) then
  501. begin
  502. tppumodule(pu.u).loadppu;
  503. { is our module compiled? then we can stop }
  504. if current_module.state=ms_compiled then
  505. exit;
  506. { add this unit to the dependencies }
  507. pu.u.adddependency(current_module);
  508. { save crc values }
  509. pu.checksum:=pu.u.crc;
  510. pu.interface_checksum:=pu.u.interface_crc;
  511. { connect unitsym to the globalsymtable of the unit }
  512. pu.unitsym.unitsymtable:=pu.u.globalsymtable;
  513. { increase refs of the unitsym when the unit contains
  514. initialization/finalization code so it doesn't trigger
  515. the unit not used hint }
  516. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  517. inc(pu.unitsym.refs);
  518. end;
  519. pu:=tused_unit(pu.next);
  520. end;
  521. { set the symtable to systemunit so it gets reorderd correctly,
  522. then insert the units in the symtablestack }
  523. pu:=tused_unit(current_module.used_units.first);
  524. symtablestack:=defaultsymtablestack;
  525. while assigned(pu) do
  526. begin
  527. if pu.in_uses then
  528. begin
  529. { Reinsert in symtablestack }
  530. hp3:=symtablestack;
  531. while assigned(hp3) do
  532. begin
  533. { insert units only once ! }
  534. if pu.u.globalsymtable=hp3 then
  535. break;
  536. hp3:=hp3.next;
  537. { unit isn't inserted }
  538. if hp3=nil then
  539. begin
  540. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  541. symtablestack:=tsymtable(pu.u.globalsymtable);
  542. {$ifdef DEBUG}
  543. test_symtablestack;
  544. {$endif DEBUG}
  545. end;
  546. end;
  547. end;
  548. pu:=tused_unit(pu.next);
  549. end;
  550. end;
  551. {$IfDef GDB}
  552. procedure write_gdb_info;
  553. procedure reset_unit_type_info;
  554. var
  555. hp : tmodule;
  556. begin
  557. hp:=tmodule(loaded_units.first);
  558. while assigned(hp) do
  559. begin
  560. hp.is_stab_written:=false;
  561. hp:=tmodule(hp.next);
  562. end;
  563. end;
  564. procedure write_used_unit_type_info(hp:tmodule);
  565. var
  566. pu : tused_unit;
  567. begin
  568. pu:=tused_unit(hp.used_units.first);
  569. while assigned(pu) do
  570. begin
  571. if not pu.u.is_stab_written then
  572. begin
  573. { prevent infinte loop for circular dependencies }
  574. pu.u.is_stab_written:=true;
  575. { write type info from used units, use a depth first
  576. strategy to reduce the recursion in writing all
  577. dependent stabs }
  578. write_used_unit_type_info(pu.u);
  579. if assigned(pu.u.globalsymtable) then
  580. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  581. end;
  582. pu:=tused_unit(pu.next);
  583. end;
  584. end;
  585. begin
  586. if not (cs_debuginfo in aktmoduleswitches) then
  587. exit;
  588. { reset unit type info flag }
  589. reset_unit_type_info;
  590. { write used types from the used units }
  591. write_used_unit_type_info(current_module);
  592. { first write the types from this unit }
  593. if assigned(current_module.globalsymtable) then
  594. begin
  595. { all types }
  596. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  597. { and all local symbols}
  598. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  599. end;
  600. if assigned(current_module.localsymtable) then
  601. begin
  602. { all types }
  603. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  604. { and all local symbols}
  605. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  606. end;
  607. if (cs_gdb_dbx in aktglobalswitches) then
  608. begin
  609. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  610. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  611. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  612. debugList.concat(Tai_stabs.Create(strpnew('"'+
  613. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  614. tostr(N_EINCL)+',0,0,0')));
  615. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  616. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  617. do_count_dbx:=false;
  618. end;
  619. end;
  620. {$EndIf GDB}
  621. procedure reset_all_defs;
  622. procedure reset_used_unit_defs(hp:tmodule);
  623. var
  624. pu : tused_unit;
  625. begin
  626. pu:=tused_unit(hp.used_units.first);
  627. while assigned(pu) do
  628. begin
  629. if not pu.u.is_reset then
  630. begin
  631. { prevent infinte loop for circular dependencies }
  632. pu.u.is_reset:=true;
  633. if assigned(pu.u.globalsymtable) then
  634. begin
  635. tglobalsymtable(pu.u.globalsymtable).reset_all_defs;
  636. reset_used_unit_defs(pu.u);
  637. end;
  638. end;
  639. pu:=tused_unit(pu.next);
  640. end;
  641. end;
  642. var
  643. hp2 : tmodule;
  644. begin
  645. hp2:=tmodule(loaded_units.first);
  646. while assigned(hp2) do
  647. begin
  648. hp2.is_reset:=false;
  649. hp2:=tmodule(hp2.next);
  650. end;
  651. reset_used_unit_defs(current_module);
  652. end;
  653. procedure parse_implementation_uses;
  654. begin
  655. if token=_USES then
  656. begin
  657. loadunits;
  658. {$ifdef DEBUG}
  659. test_symtablestack;
  660. {$endif DEBUG}
  661. end;
  662. end;
  663. procedure setupglobalswitches;
  664. begin
  665. { can't have local browser when no global browser }
  666. if (cs_local_browser in aktmoduleswitches) and
  667. not(cs_browser in aktmoduleswitches) then
  668. exclude(aktmoduleswitches,cs_local_browser);
  669. { define a symbol in delphi,objfpc,tp,gpc mode }
  670. if (m_delphi in aktmodeswitches) then
  671. current_scanner.def_macro('FPC_DELPHI')
  672. else
  673. if (m_tp7 in aktmodeswitches) then
  674. current_scanner.def_macro('FPC_TP')
  675. else
  676. if (m_objfpc in aktmodeswitches) then
  677. current_scanner.def_macro('FPC_OBJFPC')
  678. else
  679. if (m_gpc in aktmodeswitches) then
  680. current_scanner.def_macro('FPC_GPC');
  681. end;
  682. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  683. var
  684. stt : tsymtable;
  685. ps : tprocsym;
  686. pd : tprocdef;
  687. begin
  688. { there should be no current_procinfo available }
  689. if assigned(current_procinfo) then
  690. internalerror(200304275);
  691. {Generate a procsym for main}
  692. make_ref:=false;
  693. { try to insert in in static symtable ! }
  694. stt:=symtablestack;
  695. symtablestack:=st;
  696. { generate procsym }
  697. ps:=tprocsym.create('$'+name);
  698. { main are allways used }
  699. inc(ps.refs);
  700. symtablestack.insert(ps);
  701. pd:=tprocdef.create(main_program_level);
  702. include(pd.procoptions,po_public);
  703. pd.procsym:=ps;
  704. ps.addprocdef(pd);
  705. { restore symtable }
  706. make_ref:=true;
  707. symtablestack:=stt;
  708. { set procdef options }
  709. pd.proctypeoption:=potype;
  710. pd.proccalloption:=pocall_default;
  711. pd.forwarddef:=false;
  712. pd.setmangledname(target_info.cprefix+name);
  713. pd.aliasnames.insert(pd.mangledname);
  714. { We don't need is a local symtable. Change it into the static
  715. symtable }
  716. pd.localst.free;
  717. pd.localst:=st;
  718. { set procinfo and current_procinfo.procdef }
  719. current_procinfo:=cprocinfo.create(nil);
  720. current_module.procinfo:=current_procinfo;
  721. current_procinfo.procdef:=pd;
  722. { return procdef }
  723. create_main_proc:=pd;
  724. { main proc does always a call e.g. to init system unit }
  725. include(current_procinfo.flags,pi_do_call);
  726. end;
  727. procedure release_main_proc(pd:tprocdef);
  728. begin
  729. { this is a main proc, so there should be no parent }
  730. if not(assigned(current_procinfo)) or
  731. assigned(current_procinfo.parent) or
  732. not(current_procinfo.procdef=pd) then
  733. internalerror(200304276);
  734. { remove procinfo }
  735. current_module.procinfo:=nil;
  736. current_procinfo.free;
  737. current_procinfo:=nil;
  738. { remove localst as it was replaced by staticsymtable }
  739. pd.localst:=nil;
  740. end;
  741. procedure gen_implicit_initfinal(flag:word;st:tsymtable);
  742. var
  743. pd : tprocdef;
  744. begin
  745. { update module flags }
  746. current_module.flags:=current_module.flags or flag;
  747. { create procdef }
  748. case flag of
  749. uf_init :
  750. begin
  751. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init_implicit'),potype_unitinit,st);
  752. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  753. end;
  754. uf_finalize :
  755. begin
  756. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize_implicit'),potype_unitfinalize,st);
  757. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  758. end;
  759. else
  760. internalerror(200304253);
  761. end;
  762. tcgprocinfo(current_procinfo).code:=cnothingnode.create;
  763. tcgprocinfo(current_procinfo).add_entry_exit_code;
  764. tcgprocinfo(current_procinfo).generate_code;
  765. release_main_proc(pd);
  766. end;
  767. procedure proc_unit;
  768. function is_assembler_generated:boolean;
  769. begin
  770. is_assembler_generated:=(Errorcount=0) and
  771. not(
  772. codeSegment.empty and
  773. dataSegment.empty and
  774. bssSegment.empty and
  775. ((importssection=nil) or importsSection.empty) and
  776. ((resourcesection=nil) or resourceSection.empty) and
  777. ((resourcestringlist=nil) or resourcestringList.empty)
  778. );
  779. end;
  780. var
  781. main_file: tinputfile;
  782. st : tsymtable;
  783. unitst : tglobalsymtable;
  784. store_crc,store_interface_crc : cardinal;
  785. s1,s2 : ^string; {Saves stack space}
  786. force_init_final : boolean;
  787. pd : tprocdef;
  788. unitname8 : string[8];
  789. begin
  790. consume(_UNIT);
  791. if compile_level=1 then
  792. Status.IsExe:=false;
  793. if token=_ID then
  794. begin
  795. { create filenames and unit name }
  796. main_file := current_scanner.inputfile;
  797. while assigned(main_file.next) do
  798. main_file := main_file.next;
  799. new(s1);
  800. s1^:=current_module.modulename^;
  801. current_module.SetFileName(main_file.path^+main_file.name^,true);
  802. current_module.SetModuleName(orgpattern);
  803. { check for system unit }
  804. new(s2);
  805. s2^:=upper(SplitName(main_file.name^));
  806. unitname8:=copy(current_module.modulename^,1,8);
  807. if (cs_check_unit_name in aktglobalswitches) and
  808. (
  809. not(
  810. (current_module.modulename^=s2^) or
  811. (
  812. (length(current_module.modulename^)>8) and
  813. (unitname8=s2^)
  814. )
  815. )
  816. or
  817. (
  818. (length(s1^)>8) and
  819. (s1^<>current_module.modulename^)
  820. )
  821. ) then
  822. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  823. if (current_module.modulename^='SYSTEM') then
  824. include(aktmoduleswitches,cs_compilesystem);
  825. dispose(s2);
  826. dispose(s1);
  827. end;
  828. consume(_ID);
  829. consume(_SEMICOLON);
  830. consume(_INTERFACE);
  831. { global switches are read, so further changes aren't allowed }
  832. current_module.in_global:=false;
  833. { handle the global switches }
  834. setupglobalswitches;
  835. message1(unit_u_loading_interface_units,current_module.modulename^);
  836. { update status }
  837. status.currentmodule:=current_module.realmodulename^;
  838. { maybe turn off m_objpas if we are compiling objpas }
  839. if (current_module.modulename^='OBJPAS') then
  840. exclude(aktmodeswitches,m_objpas);
  841. parse_only:=true;
  842. { generate now the global symboltable }
  843. st:=tglobalsymtable.create(current_module.modulename^);
  844. refsymtable:=st;
  845. unitst:=tglobalsymtable(st);
  846. { define first as local to overcome dependency conflicts }
  847. current_module.localsymtable:=st;
  848. { the unit name must be usable as a unit specifier }
  849. { inside the unit itself (PM) }
  850. { this also forbids to have another symbol }
  851. { with the same name as the unit }
  852. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  853. { load default units, like the system unit }
  854. loaddefaultunits;
  855. { reset }
  856. make_ref:=true;
  857. { insert qualifier for the system unit (allows system.writeln) }
  858. if not(cs_compilesystem in aktmoduleswitches) then
  859. begin
  860. if token=_USES then
  861. begin
  862. loadunits;
  863. { has it been compiled at a higher level ?}
  864. if current_module.state=ms_compiled then
  865. exit;
  866. end;
  867. { ... but insert the symbol table later }
  868. st.next:=symtablestack;
  869. symtablestack:=st;
  870. end
  871. else
  872. { while compiling a system unit, some types are directly inserted }
  873. begin
  874. st.next:=symtablestack;
  875. symtablestack:=st;
  876. insert_intern_types(st);
  877. end;
  878. { now we know the place to insert the constants }
  879. constsymtable:=symtablestack;
  880. { move the global symtab from the temporary local to global }
  881. current_module.globalsymtable:=current_module.localsymtable;
  882. current_module.localsymtable:=nil;
  883. reset_all_defs;
  884. { number all units, so we know if a unit is used by this unit or
  885. needs to be added implicitly }
  886. current_module.numberunits;
  887. { ... parse the declarations }
  888. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  889. read_interface_declarations;
  890. { leave when we got an error }
  891. if (Errorcount>0) and not status.skip_error then
  892. begin
  893. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  894. status.skip_error:=true;
  895. exit;
  896. end;
  897. { Our interface is compiled, generate CRC and switch to implementation }
  898. if not(cs_compilesystem in aktmoduleswitches) and
  899. (Errorcount=0) then
  900. tppumodule(current_module).getppucrc;
  901. current_module.in_interface:=false;
  902. current_module.interface_compiled:=true;
  903. { First reload all units depending on our interface, we need to do this
  904. in the implementation part to prevent errorneous circular references }
  905. reload_flagged_units;
  906. { Parse the implementation section }
  907. consume(_IMPLEMENTATION);
  908. Message1(unit_u_loading_implementation_units,current_module.modulename^);
  909. parse_only:=false;
  910. { generates static symbol table }
  911. st:=tstaticsymtable.create(current_module.modulename^);
  912. current_module.localsymtable:=st;
  913. { remove the globalsymtable from the symtable stack }
  914. { to reinsert it after loading the implementation units }
  915. symtablestack:=unitst.next;
  916. { we don't want implementation units symbols in unitsymtable !! PM }
  917. refsymtable:=st;
  918. { Read the implementation units }
  919. parse_implementation_uses;
  920. if current_module.state=ms_compiled then
  921. exit;
  922. { reset ranges/stabs in exported definitions }
  923. reset_all_defs;
  924. { All units are read, now give them a number }
  925. current_module.numberunits;
  926. { now we can change refsymtable }
  927. refsymtable:=st;
  928. { but reinsert the global symtable as lasts }
  929. unitst.next:=symtablestack;
  930. symtablestack:=unitst;
  931. {$ifdef DEBUG}
  932. test_symtablestack;
  933. {$endif DEBUG}
  934. constsymtable:=symtablestack;
  935. {$ifdef Splitheap}
  936. if testsplit then
  937. begin
  938. Split_Heap;
  939. allow_special:=true;
  940. Switch_to_temp_heap;
  941. end;
  942. { it will report all crossings }
  943. allow_special:=false;
  944. {$endif Splitheap}
  945. Message1(parser_u_parsing_implementation,current_module.modulename^);
  946. if current_module.in_interface then
  947. internalerror(200212285);
  948. { Compile the unit }
  949. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'init'),potype_unitinit,st);
  950. pd.aliasnames.insert(make_mangledname('INIT$',current_module.localsymtable,''));
  951. tcgprocinfo(current_procinfo).parse_body;
  952. tcgprocinfo(current_procinfo).generate_code;
  953. tcgprocinfo(current_procinfo).resetprocdef;
  954. release_main_proc(pd);
  955. { if the unit contains ansi/widestrings, initialization and
  956. finalization code must be forced }
  957. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  958. tstaticsymtable(current_module.localsymtable).needs_init_final;
  959. { should we force unit initialization? }
  960. { this is a hack, but how can it be done better ? }
  961. if force_init_final and ((current_module.flags and uf_init)=0) then
  962. gen_implicit_initfinal(uf_init,st);
  963. { finalize? }
  964. if token=_FINALIZATION then
  965. begin
  966. { set module options }
  967. current_module.flags:=current_module.flags or uf_finalize;
  968. { Compile the finalize }
  969. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  970. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  971. tcgprocinfo(current_procinfo).parse_body;
  972. tcgprocinfo(current_procinfo).generate_code;
  973. tcgprocinfo(current_procinfo).resetprocdef;
  974. release_main_proc(pd);
  975. end
  976. else if force_init_final then
  977. gen_implicit_initfinal(uf_finalize,st);
  978. { the last char should always be a point }
  979. consume(_POINT);
  980. { generate a list of threadvars }
  981. InsertThreadvars;
  982. { Generate resoucestrings }
  983. If ResourceStrings.ResStrCount>0 then
  984. begin
  985. ResourceStrings.CreateResourceStringList;
  986. current_module.flags:=current_module.flags or uf_has_resources;
  987. { only write if no errors found }
  988. if (Errorcount=0) then
  989. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  990. end;
  991. if (Errorcount=0) then
  992. begin
  993. { test static symtable }
  994. tstoredsymtable(st).allsymbolsused;
  995. tstoredsymtable(st).allprivatesused;
  996. current_module.allunitsused;
  997. end;
  998. {$ifdef GDB}
  999. write_gdb_info;
  1000. {$endif GDB}
  1001. if (Errorcount=0) then
  1002. begin
  1003. { tests, if all (interface) forwards are resolved }
  1004. tstoredsymtable(symtablestack).check_forwards;
  1005. { check if all private fields are used }
  1006. tstoredsymtable(symtablestack).allprivatesused;
  1007. { remove cross unit overloads }
  1008. tstoredsymtable(symtablestack).unchain_overloaded;
  1009. end;
  1010. { leave when we got an error }
  1011. if (Errorcount>0) and not status.skip_error then
  1012. begin
  1013. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1014. status.skip_error:=true;
  1015. exit;
  1016. end;
  1017. { generate imports }
  1018. if current_module.uses_imports then
  1019. importlib.generatelib;
  1020. { insert own objectfile, or say that it's in a library
  1021. (no check for an .o when loading) }
  1022. if is_assembler_generated then
  1023. insertobjectfile
  1024. else
  1025. current_module.flags:=current_module.flags or uf_no_link;
  1026. if cs_local_browser in aktmoduleswitches then
  1027. current_module.localsymtable:=refsymtable;
  1028. if is_assembler_generated then
  1029. begin
  1030. { finish asmlist by adding segment starts }
  1031. insertsegment;
  1032. { assemble }
  1033. create_objectfile;
  1034. end;
  1035. { Write out the ppufile after the object file has been created }
  1036. store_interface_crc:=current_module.interface_crc;
  1037. store_crc:=current_module.crc;
  1038. if (Errorcount=0) then
  1039. tppumodule(current_module).writeppu;
  1040. if not(cs_compilesystem in aktmoduleswitches) then
  1041. if store_interface_crc<>current_module.interface_crc then
  1042. Message1(unit_u_interface_crc_changed,current_module.ppufilename^);
  1043. {$ifdef EXTDEBUG}
  1044. if not(cs_compilesystem in aktmoduleswitches) then
  1045. if (store_crc<>current_module.crc) and simplify_ppu then
  1046. Message1(unit_u_implementation_crc_changed,current_module.ppufilename^);
  1047. {$endif EXTDEBUG}
  1048. { remove static symtable (=refsymtable) here to save some mem }
  1049. if not (cs_local_browser in aktmoduleswitches) then
  1050. begin
  1051. st.free;
  1052. current_module.localsymtable:=nil;
  1053. end;
  1054. { leave when we got an error }
  1055. if (Errorcount>0) and not status.skip_error then
  1056. begin
  1057. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1058. status.skip_error:=true;
  1059. exit;
  1060. end;
  1061. Message1(unit_u_finished_compiling,current_module.modulename^);
  1062. end;
  1063. procedure proc_program(islibrary : boolean);
  1064. var
  1065. main_file: tinputfile;
  1066. st : tsymtable;
  1067. hp : tmodule;
  1068. pd : tprocdef;
  1069. begin
  1070. DLLsource:=islibrary;
  1071. Status.IsLibrary:=IsLibrary;
  1072. Status.IsExe:=true;
  1073. parse_only:=false;
  1074. { relocation works only without stabs under win32 !! PM }
  1075. { internal assembler uses rva for stabs info
  1076. so it should work with relocated DLLs }
  1077. if RelocSection and
  1078. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1079. (target_info.assem<>as_i386_pecoff) then
  1080. begin
  1081. include(aktglobalswitches,cs_link_strip);
  1082. { Warning stabs info does not work with reloc section !! }
  1083. if cs_debuginfo in aktmoduleswitches then
  1084. begin
  1085. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1086. Message(parser_w_parser_win32_debug_needs_WN);
  1087. exclude(aktmoduleswitches,cs_debuginfo);
  1088. end;
  1089. end;
  1090. { get correct output names }
  1091. main_file := current_scanner.inputfile;
  1092. while assigned(main_file.next) do
  1093. main_file := main_file.next;
  1094. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1095. if islibrary then
  1096. begin
  1097. consume(_LIBRARY);
  1098. stringdispose(current_module.modulename);
  1099. current_module.modulename:=stringdup(pattern);
  1100. current_module.islibrary:=true;
  1101. exportlib.preparelib(pattern);
  1102. consume(_ID);
  1103. consume(_SEMICOLON);
  1104. end
  1105. else
  1106. { is there an program head ? }
  1107. if token=_PROGRAM then
  1108. begin
  1109. consume(_PROGRAM);
  1110. stringdispose(current_module.modulename);
  1111. stringdispose(current_module.realmodulename);
  1112. current_module.modulename:=stringdup(pattern);
  1113. current_module.realmodulename:=stringdup(orgpattern);
  1114. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1115. exportlib.preparelib(pattern);
  1116. consume(_ID);
  1117. if token=_LKLAMMER then
  1118. begin
  1119. consume(_LKLAMMER);
  1120. repeat
  1121. consume(_ID);
  1122. until not try_to_consume(_COMMA);
  1123. consume(_RKLAMMER);
  1124. end;
  1125. consume(_SEMICOLON);
  1126. end
  1127. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1128. exportlib.preparelib(current_module.modulename^);
  1129. { global switches are read, so further changes aren't allowed }
  1130. current_module.in_global:=false;
  1131. { setup things using the global switches }
  1132. setupglobalswitches;
  1133. { set implementation flag }
  1134. current_module.in_interface:=false;
  1135. current_module.interface_compiled:=true;
  1136. { insert after the unit symbol tables the static symbol table }
  1137. { of the program }
  1138. st:=tstaticsymtable.create(current_module.modulename^);;
  1139. current_module.localsymtable:=st;
  1140. refsymtable:=st;
  1141. { load standard units (system,objpas,profile unit) }
  1142. loaddefaultunits;
  1143. {Load the units used by the program we compile.}
  1144. if token=_USES then
  1145. loadunits;
  1146. { reset ranges/stabs in exported definitions }
  1147. reset_all_defs;
  1148. { All units are read, now give them a number }
  1149. current_module.numberunits;
  1150. {Insert the name of the main program into the symbol table.}
  1151. if current_module.realmodulename^<>'' then
  1152. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1153. { ...is also constsymtable, this is the symtable where }
  1154. { the elements of enumeration types are inserted }
  1155. constsymtable:=st;
  1156. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1157. { The program intialization needs an alias, so it can be called
  1158. from the bootstrap code.}
  1159. if islibrary or
  1160. (target_info.system in [system_powerpc_macos,system_powerpc_darwin]) then
  1161. begin
  1162. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'main'),potype_proginit,st);
  1163. { Win32 startup code needs a single name }
  1164. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1165. pd.aliasnames.insert('PASCALMAIN');
  1166. end
  1167. else
  1168. begin
  1169. pd:=create_main_proc('main',potype_proginit,st);
  1170. pd.aliasnames.insert('PASCALMAIN');
  1171. end;
  1172. tcgprocinfo(current_procinfo).parse_body;
  1173. tcgprocinfo(current_procinfo).generate_code;
  1174. tcgprocinfo(current_procinfo).resetprocdef;
  1175. release_main_proc(pd);
  1176. { should we force unit initialization? }
  1177. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1178. begin
  1179. { initialize section }
  1180. gen_implicit_initfinal(uf_init,st);
  1181. { finalize section }
  1182. gen_implicit_initfinal(uf_finalize,st);
  1183. end;
  1184. { Add symbol to the exports section for win32 so smartlinking a
  1185. DLL will include the edata section }
  1186. if assigned(exportlib) and
  1187. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1188. assigned(current_module._exports.first) then
  1189. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1190. If ResourceStrings.ResStrCount>0 then
  1191. begin
  1192. ResourceStrings.CreateResourceStringList;
  1193. { only write if no errors found }
  1194. if (Errorcount=0) then
  1195. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1196. end;
  1197. { finalize? }
  1198. if token=_FINALIZATION then
  1199. begin
  1200. { set module options }
  1201. current_module.flags:=current_module.flags or uf_finalize;
  1202. { Compile the finalize }
  1203. pd:=create_main_proc(make_mangledname('',current_module.localsymtable,'finalize'),potype_unitfinalize,st);
  1204. pd.aliasnames.insert(make_mangledname('FINALIZE$',current_module.localsymtable,''));
  1205. tcgprocinfo(current_procinfo).parse_body;
  1206. tcgprocinfo(current_procinfo).generate_code;
  1207. tcgprocinfo(current_procinfo).resetprocdef;
  1208. release_main_proc(pd);
  1209. end;
  1210. { consume the last point }
  1211. consume(_POINT);
  1212. {$ifdef GDB}
  1213. write_gdb_info;
  1214. {$endif GDB}
  1215. { leave when we got an error }
  1216. if (Errorcount>0) and not status.skip_error then
  1217. begin
  1218. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1219. status.skip_error:=true;
  1220. exit;
  1221. end;
  1222. if (Errorcount=0) then
  1223. begin
  1224. { test static symtable }
  1225. tstoredsymtable(st).allsymbolsused;
  1226. tstoredsymtable(st).allprivatesused;
  1227. current_module.allunitsused;
  1228. end;
  1229. { generate a list of threadvars }
  1230. InsertThreadvars;
  1231. { generate imports }
  1232. if current_module.uses_imports then
  1233. importlib.generatelib;
  1234. if islibrary or
  1235. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1236. (target_info.system=system_i386_NETWARE) then
  1237. exportlib.generatelib;
  1238. { insert Tables and Heap }
  1239. insertThreadVarTablesTable;
  1240. insertResourceTablesTable;
  1241. insertinitfinaltable;
  1242. insertheap;
  1243. insertstacklength;
  1244. // datasize:=symtablestack.datasize;
  1245. { finish asmlist by adding segment starts }
  1246. insertsegment;
  1247. { insert own objectfile }
  1248. insertobjectfile;
  1249. { assemble and link }
  1250. create_objectfile;
  1251. { leave when we got an error }
  1252. if (Errorcount>0) and not status.skip_error then
  1253. begin
  1254. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1255. status.skip_error:=true;
  1256. exit;
  1257. end;
  1258. { create the executable when we are at level 1 }
  1259. if (compile_level=1) then
  1260. begin
  1261. { insert all .o files from all loaded units }
  1262. hp:=tmodule(loaded_units.first);
  1263. while assigned(hp) do
  1264. begin
  1265. linker.AddModuleFiles(hp);
  1266. hp:=tmodule(hp.next);
  1267. end;
  1268. { write .def file }
  1269. if (cs_link_deffile in aktglobalswitches) then
  1270. deffile.writefile;
  1271. { finally we can create a executable }
  1272. if (not current_module.is_unit) then
  1273. begin
  1274. if DLLSource then
  1275. linker.MakeSharedLibrary
  1276. else
  1277. linker.MakeExecutable;
  1278. end;
  1279. end;
  1280. end;
  1281. end.
  1282. {
  1283. $Log$
  1284. Revision 1.144 2004-03-09 20:45:04 peter
  1285. * more stabs updates
  1286. Revision 1.143 2004/03/08 22:07:47 peter
  1287. * stabs updates to write stabs for def for all implictly used
  1288. units
  1289. Revision 1.142 2004/03/02 17:32:12 florian
  1290. * make cycle fixed
  1291. + pic support for darwin
  1292. + support of importing vars from shared libs on darwin implemented
  1293. Revision 1.141 2004/03/02 00:36:33 olle
  1294. * big transformation of Tai_[const_]Symbol.Create[data]name*
  1295. Revision 1.140 2004/02/26 16:16:38 peter
  1296. * tai_const.create_ptr added
  1297. Revision 1.139 2004/02/06 22:37:00 daniel
  1298. * Removed not very usefull nextglobal & previousglobal fields from
  1299. Tstoreddef, saving 78 kb of memory
  1300. Revision 1.138 2004/02/04 22:15:15 daniel
  1301. * Rtti generation moved to ncgutil
  1302. * Assmtai usage of symsym removed
  1303. * operator overloading cleanup up
  1304. Revision 1.137 2004/01/28 16:48:24 peter
  1305. use local string of 8 chars
  1306. Revision 1.136 2004/01/04 21:08:09 jonas
  1307. * Never generate a "main" symbol for PASCALMAIN for Darwin and classic
  1308. Mac OS, they use a C-main in their system unit
  1309. Revision 1.135 2003/12/12 19:42:21 peter
  1310. * check unit name when expected unitname > 8 chars
  1311. Revision 1.134 2003/12/08 22:33:43 peter
  1312. * don't allow duplicate uses
  1313. * fix wrong circular dependency
  1314. Revision 1.133 2003/11/29 20:13:25 florian
  1315. * fixed several pi_do_call problems
  1316. Revision 1.132 2003/10/29 19:48:51 peter
  1317. * renamed mangeldname_prefix to make_mangledname and made it more
  1318. generic
  1319. * make_mangledname is now also used for internal threadvar/resstring
  1320. lists
  1321. * Add P$ in front of program modulename to prevent duplicated symbols
  1322. at assembler level, because the main program can have the same name
  1323. as a unit, see webtbs/tw1251b
  1324. Revision 1.131 2003/10/24 17:40:23 peter
  1325. * cleanup of the entry and exit code insertion
  1326. Revision 1.130 2003/10/22 15:22:33 peter
  1327. * fixed unitsym-globalsymtable relation so the uses of a unit
  1328. is counted correctly
  1329. Revision 1.129 2003/10/21 15:14:33 peter
  1330. * fixed memleak for initfinalcode
  1331. * exit from generatecode when there are already errors
  1332. Revision 1.128 2003/10/01 20:34:49 peter
  1333. * procinfo unit contains tprocinfo
  1334. * cginfo renamed to cgbase
  1335. * moved cgmessage to verbose
  1336. * fixed ppc and sparc compiles
  1337. Revision 1.127 2003/09/30 08:39:50 michael
  1338. + Patch from Wiktor Sywula for watcom support
  1339. Revision 1.126 2003/09/23 18:03:08 peter
  1340. * add missing release of main_proc
  1341. Revision 1.125 2003/09/23 17:56:05 peter
  1342. * locals and paras are allocated in the code generation
  1343. * tvarsym.localloc contains the location of para/local when
  1344. generating code for the current procedure
  1345. Revision 1.124 2003/09/09 20:59:27 daniel
  1346. * Adding register allocation order
  1347. Revision 1.123 2003/09/09 15:55:44 peter
  1348. * use register with least interferences in spillregister
  1349. Revision 1.122 2003/09/07 22:09:35 peter
  1350. * preparations for different default calling conventions
  1351. * various RA fixes
  1352. Revision 1.121 2003/09/05 17:41:12 florian
  1353. * merged Wiktor's Watcom patches in 1.1
  1354. Revision 1.120 2003/08/23 22:29:24 peter
  1355. * reload flagged units when interface is loaded
  1356. Revision 1.119 2003/08/21 14:47:41 peter
  1357. * remove convert_registers
  1358. Revision 1.118 2003/08/20 17:48:49 peter
  1359. * fixed stackalloc to not allocate localst.datasize twice
  1360. * order of stackalloc code fixed for implicit init/final
  1361. Revision 1.117 2003/08/20 09:07:00 daniel
  1362. * New register coding now mandatory, some more convert_registers calls
  1363. removed.
  1364. Revision 1.116 2003/07/23 11:04:15 jonas
  1365. * split en_exit_code into a part that may allocate a register and a part
  1366. that doesn't, so the former can be done before the register colouring
  1367. has been performed
  1368. Revision 1.115 2003/07/06 17:58:22 peter
  1369. * framepointer fixes for sparc
  1370. * parent framepointer code more generic
  1371. Revision 1.114 2003/06/13 21:19:31 peter
  1372. * current_procdef removed, use current_procinfo.procdef instead
  1373. Revision 1.113 2003/06/09 12:23:30 peter
  1374. * init/final of procedure data splitted from genentrycode
  1375. * use asmnode getposition to insert final at the correct position
  1376. als for the implicit try...finally
  1377. Revision 1.112 2003/06/07 20:26:32 peter
  1378. * re-resolving added instead of reloading from ppu
  1379. * tderef object added to store deref info for resolving
  1380. Revision 1.111 2003/06/03 20:21:45 mazen
  1381. - removed unneeded ifdefs
  1382. - removed unneeded cases for sparc and x86_64
  1383. Revision 1.110 2003/06/03 13:01:59 daniel
  1384. * Register allocator finished
  1385. Revision 1.109 2003/05/26 21:17:17 peter
  1386. * procinlinenode removed
  1387. * aktexit2label removed, fast exit removed
  1388. + tcallnode.inlined_pass_2 added
  1389. Revision 1.108 2003/05/25 10:27:12 peter
  1390. * moved Comment calls to messge file
  1391. Revision 1.107 2003/05/22 21:31:35 peter
  1392. * defer codegeneration for nested procedures
  1393. Revision 1.106 2003/05/15 18:58:53 peter
  1394. * removed selfpointer_offset, vmtpointer_offset
  1395. * tvarsym.adjusted_address
  1396. * address in localsymtable is now in the real direction
  1397. * removed some obsolete globals
  1398. Revision 1.105 2003/05/11 19:31:28 florian
  1399. * fixed implicit init/final code for units, stack frame was wrong for ppc
  1400. Revision 1.104 2003/04/28 21:19:02 peter
  1401. * fix stabs generation for implicit initfinal
  1402. Revision 1.103 2003/04/27 11:21:34 peter
  1403. * aktprocdef renamed to current_procinfo.procdef
  1404. * procinfo renamed to current_procinfo
  1405. * procinfo will now be stored in current_module so it can be
  1406. cleaned up properly
  1407. * gen_main_procsym changed to create_main_proc and release_main_proc
  1408. to also generate a tprocinfo structure
  1409. * fixed unit implicit initfinal
  1410. Revision 1.102 2003/04/27 07:29:50 peter
  1411. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  1412. a new procdef declaration
  1413. * aktprocsym removed
  1414. * lexlevel removed, use symtable.symtablelevel instead
  1415. * implicit init/final code uses the normal genentry/genexit
  1416. * funcret state checking updated for new funcret handling
  1417. Revision 1.101 2003/04/23 12:35:34 florian
  1418. * fixed several issues with powerpc
  1419. + applied a patch from Jonas for nested function calls (PowerPC only)
  1420. * ...
  1421. Revision 1.100 2003/04/12 15:13:03 peter
  1422. * Use the original unitname when defining a unitsym
  1423. Revision 1.99 2003/03/23 23:21:42 hajny
  1424. + emx target added
  1425. Revision 1.98 2003/03/17 22:20:08 peter
  1426. *** empty log message ***
  1427. Revision 1.97 2003/03/17 13:36:39 peter
  1428. * fix import linking under linux
  1429. Revision 1.96 2003/02/19 22:00:14 daniel
  1430. * Code generator converted to new register notation
  1431. - Horribily outdated todo.txt removed
  1432. Revision 1.95 2003/02/06 22:36:55 mazen
  1433. * fixing bug related to errornous program main entry stack frame
  1434. Revision 1.94 2003/01/30 21:46:20 peter
  1435. * tai_const_symbol.createdataname added
  1436. Revision 1.93 2003/01/11 11:19:54 hajny
  1437. * correction from rev. 1.88 put back
  1438. Revision 1.92 2003/01/08 18:43:56 daniel
  1439. * Tregister changed into a record
  1440. Revision 1.91 2003/01/05 13:36:53 florian
  1441. * x86-64 compiles
  1442. + very basic support for float128 type (x86-64 only)
  1443. Revision 1.90 2002/12/29 18:17:23 peter
  1444. * insert unitsym with the name as specified in the uses list
  1445. Revision 1.89 2002/12/29 14:57:50 peter
  1446. * unit loading changed to first register units and load them
  1447. afterwards. This is needed to support uses xxx in yyy correctly
  1448. * unit dependency check fixed
  1449. Revision 1.88 2002/12/27 19:09:33 hajny
  1450. * another (hopefully final ;-) ) fix for not linked import libraries for units with no code
  1451. Revision 1.87 2002/12/24 23:32:56 peter
  1452. * Use FixFilename for specified unit sourcefile in uses
  1453. Revision 1.86 2002/12/06 16:56:58 peter
  1454. * only compile cs_fp_emulation support when cpufpuemu is defined
  1455. * define cpufpuemu for m68k only
  1456. Revision 1.85 2002/11/30 21:32:24 carl
  1457. + Add loading of softfpu in emulation mode
  1458. + Correct routine call for softfpu
  1459. * Extended type must also be defined even with softfpu
  1460. Revision 1.84 2002/11/15 01:58:53 peter
  1461. * merged changes from 1.0.7 up to 04-11
  1462. - -V option for generating bug report tracing
  1463. - more tracing for option parsing
  1464. - errors for cdecl and high()
  1465. - win32 import stabs
  1466. - win32 records<=8 are returned in eax:edx (turned off by default)
  1467. - heaptrc update
  1468. - more info for temp management in .s file with EXTDEBUG
  1469. Revision 1.83 2002/11/09 15:33:26 carl
  1470. * major alignment updates
  1471. Revision 1.82 2002/10/16 06:32:52 michael
  1472. + Renamed thread unit to systhrds
  1473. Revision 1.81 2002/10/14 19:42:34 peter
  1474. * only use init tables for threadvars
  1475. Revision 1.80 2002/10/06 19:41:30 peter
  1476. * Add finalization of typed consts
  1477. * Finalization of globals in the main program
  1478. Revision 1.79 2002/09/09 17:34:15 peter
  1479. * tdicationary.replace added to replace and item in a dictionary. This
  1480. is only allowed for the same name
  1481. * varsyms are inserted in symtable before the types are parsed. This
  1482. fixes the long standing "var longint : longint" bug
  1483. - consume_idlist and idstringlist removed. The loops are inserted
  1484. at the callers place and uses the symtable for duplicate id checking
  1485. Revision 1.78 2002/09/07 15:25:07 peter
  1486. * old logs removed and tabs fixed
  1487. Revision 1.77 2002/09/03 16:26:27 daniel
  1488. * Make Tprocdef.defs protected
  1489. Revision 1.76 2002/09/02 18:46:26 peter
  1490. * insert PASCALMAIN in library for Win32 only
  1491. Revision 1.75 2002/08/31 15:59:30 florian
  1492. + HEAP* stuff must be generated for Linux/PPC as well
  1493. + direct assembler reader searches now global and static symtables as well
  1494. Revision 1.74 2002/08/25 19:25:20 peter
  1495. * sym.insert_in_data removed
  1496. * symtable.insertvardata/insertconstdata added
  1497. * removed insert_in_data call from symtable.insert, it needs to be
  1498. called separatly. This allows to deref the address calculation
  1499. * procedures now calculate the parast addresses after the procedure
  1500. directives are parsed. This fixes the cdecl parast problem
  1501. * push_addr_param has an extra argument that specifies if cdecl is used
  1502. or not
  1503. Revision 1.73 2002/08/18 20:06:25 peter
  1504. * inlining is now also allowed in interface
  1505. * renamed write/load to ppuwrite/ppuload
  1506. * tnode storing in ppu
  1507. * nld,ncon,nbas are already updated for storing in ppu
  1508. Revision 1.72 2002/08/17 09:23:39 florian
  1509. * first part of procinfo rewrite
  1510. Revision 1.71 2002/08/11 13:24:12 peter
  1511. * saving of asmsymbols in ppu supported
  1512. * asmsymbollist global is removed and moved into a new class
  1513. tasmlibrarydata that will hold the info of a .a file which
  1514. corresponds with a single module. Added librarydata to tmodule
  1515. to keep the library info stored for the module. In the future the
  1516. objectfiles will also be stored to the tasmlibrarydata class
  1517. * all getlabel/newasmsymbol and friends are moved to the new class
  1518. Revision 1.70 2002/08/10 14:46:29 carl
  1519. + moved target_cpu_string to cpuinfo
  1520. * renamed asmmode enum.
  1521. * assembler reader has now less ifdef's
  1522. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1523. Revision 1.69 2002/07/26 21:15:41 florian
  1524. * rewrote the system handling
  1525. Revision 1.68 2002/07/04 20:43:01 florian
  1526. * first x86-64 patches
  1527. Revision 1.67 2002/07/01 18:46:25 peter
  1528. * internal linker
  1529. * reorganized aasm layer
  1530. Revision 1.66 2002/05/16 19:46:43 carl
  1531. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1532. + try to fix temp allocation (still in ifdef)
  1533. + generic constructor calls
  1534. + start of tassembler / tmodulebase class cleanup
  1535. Revision 1.65 2002/05/14 19:34:49 peter
  1536. * removed old logs and updated copyright year
  1537. Revision 1.64 2002/05/12 16:53:09 peter
  1538. * moved entry and exitcode to ncgutil and cgobj
  1539. * foreach gets extra argument for passing local data to the
  1540. iterator function
  1541. * -CR checks also class typecasts at runtime by changing them
  1542. into as
  1543. * fixed compiler to cycle with the -CR option
  1544. * fixed stabs with elf writer, finally the global variables can
  1545. be watched
  1546. * removed a lot of routines from cga unit and replaced them by
  1547. calls to cgobj
  1548. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1549. u32bit then the other is typecasted also to u32bit without giving
  1550. a rangecheck warning/error.
  1551. * fixed pascal calling method with reversing also the high tree in
  1552. the parast, detected by tcalcst3 test
  1553. Revision 1.63 2002/05/06 19:54:50 carl
  1554. + added more patches from Mazen for SPARC port
  1555. Revision 1.62 2002/04/20 21:32:24 carl
  1556. + generic FPC_CHECKPOINTER
  1557. + first parameter offset in stack now portable
  1558. * rename some constants
  1559. + move some cpu stuff to other units
  1560. - remove unused constents
  1561. * fix stacksize for some targets
  1562. * fix generic size problems which depend now on EXTEND_SIZE constant
  1563. Revision 1.61 2002/04/19 15:46:02 peter
  1564. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1565. in most cases and not written to the ppu
  1566. * add mangeledname_prefix() routine to generate the prefix of
  1567. manglednames depending on the current procedure, object and module
  1568. * removed static procprefix since the mangledname is now build only
  1569. on demand from tprocdef.mangledname
  1570. Revision 1.60 2002/04/14 16:53:10 carl
  1571. + align code section and data section according to alignment rules
  1572. Revision 1.59 2002/04/07 17:58:38 carl
  1573. + generic stack checking
  1574. Revision 1.58 2002/04/04 19:06:03 peter
  1575. * removed unused units
  1576. * use tlocation.size in cg.a_*loc*() routines
  1577. }