pmodules.pas 62 KB

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