pmodules.pas 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710
  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. aasmbase,aasmtai,aasmcpu,
  31. cgbase,cpuinfo,
  32. ncgutil,
  33. link,assemble,import,export,gendef,ppu,comprsrc,
  34. cresstr,cpubase,
  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(hp.u.modulename^+'_$THREADVARLIST'));
  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(current_module.modulename^+'_$THREADVARLIST'));
  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. if count > 0 then
  187. have_local_threadvars := true;
  188. end;
  189. procedure AddToThreadvarList(p:tnamedindexitem;arg:pointer);
  190. var
  191. ltvTable : taasmoutput;
  192. begin
  193. ltvTable:=taasmoutput(arg);
  194. if (tsym(p).typ=varsym) and
  195. (vo_is_thread_var in tvarsym(p).varoptions) then
  196. begin
  197. { address of threadvar }
  198. ltvTable.concat(tai_const_symbol.createdataname(tvarsym(p).mangledname));
  199. { size of threadvar }
  200. ltvTable.concat(tai_const.create_32bit(tvarsym(p).getsize));
  201. end;
  202. end;
  203. procedure InsertThreadvars;
  204. var
  205. ltvTable : TAAsmoutput;
  206. begin
  207. ltvTable:=TAAsmoutput.create;
  208. if assigned(current_module.globalsymtable) then
  209. current_module.globalsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  210. current_module.localsymtable.foreach_static({$ifdef FPCPROCVAR}@{$endif}AddToThreadvarList,ltvTable);
  211. if ltvTable.first<>nil then
  212. begin
  213. { add begin and end of the list }
  214. ltvTable.insert(tai_symbol.createdataname_global(current_module.modulename^+'_$THREADVARLIST',0));
  215. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  216. ltvTable.concat(tai_symbol_end.createname(current_module.modulename^+'_$THREADVARLIST'));
  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(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  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(current_module.modulename^+'_RESOURCESTRINGLIST'));
  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('INIT$$'+hp.u.modulename^))
  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('FINALIZE$$'+hp.u.modulename^))
  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('INIT$$'+current_module.modulename^))
  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('FINALIZE$$'+current_module.modulename^))
  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. {$ifdef x86_64}
  325. system_x86_64_linux:
  326. ;
  327. {$endif x86_64}
  328. {$ifdef i386}
  329. system_i386_OS2,system_i386_EMX:
  330. ;
  331. {$endif i386}
  332. {$ifdef powerpc}
  333. system_powerpc_macos:
  334. ;
  335. {$endif powerpc}
  336. {$ifdef alpha}
  337. system_alpha_linux:
  338. ;
  339. {$endif alpha}
  340. {$ifdef m68k}
  341. system_m68k_Mac:
  342. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  343. system_m68k_PalmOS:
  344. ;
  345. {$endif m68k}
  346. {$IFDEF SPARC}
  347. system_SPARC_Linux:
  348. ;
  349. {$ENDIF SPARC}
  350. else
  351. begin
  352. bssSegment.concat(Tai_align.Create(var_align(heapsize)));
  353. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  354. end;
  355. end;
  356. {$ifdef m68k}
  357. if target_info.system<>system_m68k_PalmOS then
  358. begin
  359. dataSegment.concat(Tai_align.Create(const_align(4)));
  360. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  361. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  362. end;
  363. {$else m68k}
  364. dataSegment.concat(Tai_align.Create(const_align(4)));
  365. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  366. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  367. {$endif m68k}
  368. end;
  369. procedure insertstacklength;
  370. begin
  371. { stacksize can be specified and is now simulated }
  372. dataSegment.concat(Tai_align.Create(const_align(4)));
  373. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  374. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  375. end;
  376. procedure loaddefaultunits;
  377. procedure AddUnit(const s:string);
  378. var
  379. hp : tppumodule;
  380. unitsym : tunitsym;
  381. begin
  382. { load unit }
  383. hp:=registerunit(current_module,s,'');
  384. hp.loadppu;
  385. hp.adddependency(current_module);
  386. current_module.addusedunit(hp,false);
  387. { add to symtable stack }
  388. tsymtable(hp.globalsymtable).next:=symtablestack;
  389. symtablestack:=hp.globalsymtable;
  390. { insert unitsym }
  391. unitsym:=tunitsym.create(s,hp.globalsymtable);
  392. inc(unitsym.refs);
  393. refsymtable.insert(unitsym);
  394. end;
  395. begin
  396. { are we compiling the system unit? }
  397. if (cs_compilesystem in aktmoduleswitches) then
  398. begin
  399. { create system defines }
  400. createconstdefs;
  401. { we don't need to reset anything, it's already done in parser.pas }
  402. exit;
  403. end;
  404. { insert the system unit, it is allways the first }
  405. Symtablestack:=nil;
  406. AddUnit('System');
  407. SystemUnit:=TGlobalSymtable(Symtablestack);
  408. { read default constant definitions }
  409. make_ref:=false;
  410. readconstdefs;
  411. make_ref:=true;
  412. {$ifdef cpufpemu}
  413. { Floating point emulation unit? }
  414. if (cs_fp_emulation in aktmoduleswitches) then
  415. AddUnit('SoftFpu');
  416. {$endif cpufpemu}
  417. { Thread support unit? }
  418. if (cs_threading in aktmoduleswitches) then
  419. AddUnit('SysThrds');
  420. { Objpas unit? }
  421. if m_objpas in aktmodeswitches then
  422. AddUnit('ObjPas');
  423. { Profile unit? Needed for go32v2 only }
  424. if (cs_profile in aktmoduleswitches) and
  425. (target_info.system=system_i386_go32v2) then
  426. AddUnit('Profile');
  427. { Units only required for main module }
  428. if not(current_module.is_unit) then
  429. begin
  430. { Heaptrc unit }
  431. if (cs_gdb_heaptrc in aktglobalswitches) then
  432. AddUnit('HeapTrc');
  433. { Lineinfo unit }
  434. if (cs_gdb_lineinfo in aktglobalswitches) then
  435. AddUnit('LineInfo');
  436. end;
  437. { save default symtablestack }
  438. defaultsymtablestack:=symtablestack;
  439. end;
  440. procedure loadunits;
  441. var
  442. s,sorg : stringid;
  443. fn : string;
  444. pu : tused_unit;
  445. hp2 : tmodule;
  446. hp3 : tsymtable;
  447. oldprocdef : tprocdef;
  448. unitsym : tunitsym;
  449. begin
  450. oldprocdef:=current_procdef;
  451. consume(_USES);
  452. {$ifdef DEBUG}
  453. test_symtablestack;
  454. {$endif DEBUG}
  455. repeat
  456. s:=pattern;
  457. sorg:=orgpattern;
  458. consume(_ID);
  459. { support "<unit> in '<file>'" construct, but not for tp7 }
  460. if not(m_tp7 in aktmodeswitches) then
  461. begin
  462. if try_to_consume(_OP_IN) then
  463. fn:=FixFileName(get_stringconst)
  464. else
  465. fn:='';
  466. end;
  467. { Give a warning if objpas is loaded }
  468. if s='OBJPAS' then
  469. Message(parser_w_no_objpas_use_mode);
  470. { Using the unit itself is not possible }
  471. if (s<>current_module.modulename^) then
  472. begin
  473. { check if the unit is already used }
  474. hp2:=nil;
  475. pu:=tused_unit(current_module.used_units.first);
  476. while assigned(pu) do
  477. begin
  478. if (pu.u.modulename^=s) then
  479. begin
  480. hp2:=pu.u;
  481. break;
  482. end;
  483. pu:=tused_unit(pu.next);
  484. end;
  485. { Need to register the unit? }
  486. if not assigned(hp2) then
  487. hp2:=registerunit(current_module,sorg,fn);
  488. { the current module uses the unit hp2 }
  489. current_module.addusedunit(hp2,true);
  490. end
  491. else
  492. Message1(sym_e_duplicate_id,s);
  493. if token=_COMMA then
  494. begin
  495. pattern:='';
  496. consume(_COMMA);
  497. end
  498. else
  499. break;
  500. until false;
  501. consume(_SEMICOLON);
  502. { Load the units }
  503. pu:=tused_unit(current_module.used_units.first);
  504. while assigned(pu) do
  505. begin
  506. if pu.in_uses then
  507. begin
  508. { store the original name to create the unitsym }
  509. sorg:=pu.u.realmodulename^;
  510. tppumodule(pu.u).loadppu;
  511. { is our module compiled? then we can stop }
  512. if current_module.state=ms_compiled then
  513. exit;
  514. { add this unit to the dependencies }
  515. pu.u.adddependency(current_module);
  516. pu.checksum:=pu.u.crc;
  517. pu.interface_checksum:=pu.u.interface_crc;
  518. { Create unitsym, we need to use the name as specified, we
  519. can not use the modulename because that can be different
  520. when -Un is used. But when the names are the same then
  521. force the name of the module so there will be no difference
  522. in the case of the name }
  523. if upper(sorg)=pu.u.modulename^ then
  524. sorg:=pu.u.realmodulename^;
  525. unitsym:=tunitsym.create(sorg,pu.u.globalsymtable);
  526. if (pu.u.flags and (uf_init or uf_finalize))<>0 then
  527. inc(unitsym.refs);
  528. refsymtable.insert(unitsym);
  529. end;
  530. pu:=tused_unit(pu.next);
  531. end;
  532. { set the symtable to systemunit so it gets reorderd correctly,
  533. then insert the units in the symtablestack }
  534. pu:=tused_unit(current_module.used_units.first);
  535. symtablestack:=defaultsymtablestack;
  536. while assigned(pu) do
  537. begin
  538. {$IfDef GDB}
  539. if (cs_debuginfo in aktmoduleswitches) and
  540. (cs_gdb_dbx in aktglobalswitches) and
  541. not pu.is_stab_written then
  542. begin
  543. tglobalsymtable(pu.u.globalsymtable).concattypestabto(debuglist);
  544. pu.is_stab_written:=true;
  545. pu.unitid:=tsymtable(pu.u.globalsymtable).unitid;
  546. end;
  547. {$EndIf GDB}
  548. if pu.in_uses then
  549. begin
  550. { Reinsert in symtablestack }
  551. hp3:=symtablestack;
  552. while assigned(hp3) do
  553. begin
  554. { insert units only once ! }
  555. if pu.u.globalsymtable=hp3 then
  556. break;
  557. hp3:=hp3.next;
  558. { unit isn't inserted }
  559. if hp3=nil then
  560. begin
  561. tsymtable(pu.u.globalsymtable).next:=symtablestack;
  562. symtablestack:=tsymtable(pu.u.globalsymtable);
  563. {$ifdef DEBUG}
  564. test_symtablestack;
  565. {$endif DEBUG}
  566. end;
  567. end;
  568. end;
  569. pu:=tused_unit(pu.next);
  570. end;
  571. current_procdef:=oldprocdef;
  572. end;
  573. procedure write_gdb_info;
  574. {$IfDef GDB}
  575. var
  576. hp : tused_unit;
  577. begin
  578. if not (cs_debuginfo in aktmoduleswitches) then
  579. exit;
  580. { now insert the units in the symtablestack }
  581. hp:=tused_unit(current_module.used_units.first);
  582. while assigned(hp) do
  583. begin
  584. if (cs_debuginfo in aktmoduleswitches) and
  585. not hp.is_stab_written then
  586. begin
  587. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  588. hp.is_stab_written:=true;
  589. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  590. end;
  591. hp:=tused_unit(hp.next);
  592. end;
  593. if (not current_module.in_interface) and
  594. assigned(current_module.localsymtable) then
  595. begin
  596. { all types }
  597. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  598. { and all local symbols}
  599. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  600. end
  601. else if assigned(current_module.globalsymtable) then
  602. begin
  603. { all types }
  604. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  605. { and all local symbols}
  606. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  607. end;
  608. if (cs_gdb_dbx in aktglobalswitches) then
  609. begin
  610. debugList.concat(tai_comment.Create(strpnew('EINCL of global '+
  611. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  612. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  613. debugList.concat(Tai_stabs.Create(strpnew('"'+
  614. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  615. tostr(N_EINCL)+',0,0,0')));
  616. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  617. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  618. do_count_dbx:=false;
  619. end;
  620. end;
  621. {$Else GDB}
  622. begin
  623. end;
  624. {$EndIf GDB}
  625. procedure parse_implementation_uses(symt:tsymtable);
  626. begin
  627. if token=_USES then
  628. begin
  629. loadunits;
  630. {$ifdef DEBUG}
  631. test_symtablestack;
  632. {$endif DEBUG}
  633. end;
  634. end;
  635. procedure setupglobalswitches;
  636. begin
  637. { can't have local browser when no global browser }
  638. if (cs_local_browser in aktmoduleswitches) and
  639. not(cs_browser in aktmoduleswitches) then
  640. exclude(aktmoduleswitches,cs_local_browser);
  641. { define a symbol in delphi,objfpc,tp,gpc mode }
  642. if (m_delphi in aktmodeswitches) then
  643. current_scanner.def_macro('FPC_DELPHI')
  644. else
  645. if (m_tp7 in aktmodeswitches) then
  646. current_scanner.def_macro('FPC_TP')
  647. else
  648. if (m_objfpc in aktmodeswitches) then
  649. current_scanner.def_macro('FPC_OBJFPC')
  650. else
  651. if (m_gpc in aktmodeswitches) then
  652. current_scanner.def_macro('FPC_GPC');
  653. end;
  654. function create_main_proc(const name:string;potype:tproctypeoption;st:tsymtable):tprocdef;
  655. var
  656. stt : tsymtable;
  657. ps : tprocsym;
  658. pd : tprocdef;
  659. begin
  660. { there should be no current_procinfo available }
  661. if assigned(current_procinfo) then
  662. internalerror(200304275);
  663. {Generate a procsym for main}
  664. make_ref:=false;
  665. { try to insert in in static symtable ! }
  666. stt:=symtablestack;
  667. symtablestack:=st;
  668. { generate procsym }
  669. ps:=tprocsym.create('$'+name);
  670. { main are allways used }
  671. inc(ps.refs);
  672. symtablestack.insert(ps);
  673. pd:=tprocdef.create(main_program_level);
  674. pd.procsym:=ps;
  675. ps.addprocdef(pd);
  676. { restore symtable }
  677. make_ref:=true;
  678. symtablestack:=stt;
  679. { set procdef options }
  680. pd.proctypeoption:=potype;
  681. pd.forwarddef:=false;
  682. pd.setmangledname(target_info.cprefix+name);
  683. pd.aliasnames.insert(pd.mangledname);
  684. { We don't need is a local symtable. Change it into the static
  685. symtable }
  686. pd.localst.free;
  687. pd.localst:=st;
  688. { set procinfo and current_procdef }
  689. current_procinfo:=cprocinfo.create(nil);
  690. current_module.procinfo:=current_procinfo;
  691. current_procinfo.procdef:=pd;
  692. current_procdef:=pd;
  693. { return procdef }
  694. create_main_proc:=pd;
  695. end;
  696. procedure release_main_proc(pd:tprocdef);
  697. begin
  698. { this is a main proc, so there should be no parent }
  699. if not(assigned(current_procinfo)) or
  700. assigned(current_procinfo.parent) or
  701. not(current_procinfo.procdef=pd) then
  702. internalerror(200304276);
  703. { remove procinfo }
  704. current_module.procinfo:=nil;
  705. current_procinfo.free;
  706. current_procinfo:=nil;
  707. { remove localst as it was replaced by staticsymtable }
  708. pd.localst:=nil;
  709. end;
  710. procedure gen_implicit_initfinal(list:taasmoutput;flag:word;st:tsymtable);
  711. var
  712. parasize : longint;
  713. nostackframe : boolean;
  714. pd : tprocdef;
  715. oldexitlabel,
  716. oldexit2label : tasmlabel;
  717. begin
  718. { update module flags }
  719. current_module.flags:=current_module.flags or flag;
  720. { create procdef }
  721. case flag of
  722. uf_init :
  723. begin
  724. pd:=create_main_proc(current_module.modulename^+'_init_implicit',potype_unitinit,st);
  725. pd.aliasnames.insert('INIT$$'+current_module.modulename^);
  726. end;
  727. uf_finalize :
  728. begin
  729. pd:=create_main_proc(current_module.modulename^+'_finalize_implicit',potype_unitfinalize,st);
  730. pd.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  731. end;
  732. else
  733. internalerror(200304253);
  734. end;
  735. { save labels }
  736. oldexitlabel:=aktexitlabel;
  737. oldexit2label:=aktexit2label;
  738. { generate a dummy function }
  739. parasize:=0;
  740. nostackframe:=false;
  741. objectlibrary.getlabel(aktexitlabel);
  742. objectlibrary.getlabel(aktexit2label);
  743. genentrycode(list,true,0,parasize,nostackframe,false);
  744. genexitcode(list,parasize,nostackframe,false);
  745. list.convert_registers;
  746. release_main_proc(pd);
  747. { restore }
  748. aktexitlabel:=oldexitlabel;
  749. aktexit2label:=oldexit2label;
  750. end;
  751. procedure proc_unit;
  752. function is_assembler_generated:boolean;
  753. begin
  754. is_assembler_generated:=(Errorcount=0) and
  755. not(
  756. codeSegment.empty and
  757. dataSegment.empty and
  758. bssSegment.empty and
  759. ((importssection=nil) or importsSection.empty) and
  760. ((resourcesection=nil) or resourceSection.empty) and
  761. ((resourcestringlist=nil) or resourcestringList.empty)
  762. );
  763. end;
  764. var
  765. main_file: tinputfile;
  766. st : tsymtable;
  767. unitst : tglobalsymtable;
  768. {$ifdef GDB}
  769. pu : tused_unit;
  770. {$endif GDB}
  771. store_crc,store_interface_crc : cardinal;
  772. s2 : ^string; {Saves stack space}
  773. force_init_final : boolean;
  774. initfinalcode : taasmoutput;
  775. pd : tprocdef;
  776. begin
  777. initfinalcode:=taasmoutput.create;
  778. consume(_UNIT);
  779. if compile_level=1 then
  780. Status.IsExe:=false;
  781. if token=_ID then
  782. begin
  783. { create filenames and unit name }
  784. main_file := current_scanner.inputfile;
  785. while assigned(main_file.next) do
  786. main_file := main_file.next;
  787. current_module.SetFileName(main_file.path^+main_file.name^,true);
  788. stringdispose(current_module.modulename);
  789. stringdispose(current_module.realmodulename);
  790. current_module.modulename:=stringdup(pattern);
  791. current_module.realmodulename:=stringdup(orgpattern);
  792. { check for system unit }
  793. new(s2);
  794. s2^:=upper(SplitName(main_file.name^));
  795. if (cs_check_unit_name in aktglobalswitches) and
  796. not((current_module.modulename^=s2^) or
  797. ((length(current_module.modulename^)>8) and
  798. (copy(current_module.modulename^,1,8)=s2^))) then
  799. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  800. if (current_module.modulename^='SYSTEM') then
  801. include(aktmoduleswitches,cs_compilesystem);
  802. dispose(s2);
  803. end;
  804. consume(_ID);
  805. consume(_SEMICOLON);
  806. consume(_INTERFACE);
  807. { global switches are read, so further changes aren't allowed }
  808. current_module.in_global:=false;
  809. { handle the global switches }
  810. setupglobalswitches;
  811. Comment(V_Used,'Loading interface units from '+current_module.modulename^);
  812. // Message1(unit_u_start_parse_interface,current_module.modulename^);
  813. { update status }
  814. status.currentmodule:=current_module.realmodulename^;
  815. { maybe turn off m_objpas if we are compiling objpas }
  816. if (current_module.modulename^='OBJPAS') then
  817. exclude(aktmodeswitches,m_objpas);
  818. parse_only:=true;
  819. { generate now the global symboltable }
  820. st:=tglobalsymtable.create(current_module.modulename^);
  821. refsymtable:=st;
  822. unitst:=tglobalsymtable(st);
  823. { define first as local to overcome dependency conflicts }
  824. current_module.localsymtable:=st;
  825. { the unit name must be usable as a unit specifier }
  826. { inside the unit itself (PM) }
  827. { this also forbids to have another symbol }
  828. { with the same name as the unit }
  829. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  830. { load default units, like the system unit }
  831. loaddefaultunits;
  832. { reset }
  833. make_ref:=true;
  834. { insert qualifier for the system unit (allows system.writeln) }
  835. if not(cs_compilesystem in aktmoduleswitches) then
  836. begin
  837. if token=_USES then
  838. begin
  839. loadunits;
  840. { has it been compiled at a higher level ?}
  841. if current_module.state=ms_compiled then
  842. begin
  843. { this unit symtable is obsolete }
  844. { dispose(unitst,done);
  845. disposed as localsymtable !! }
  846. RestoreUnitSyms;
  847. exit;
  848. end;
  849. end;
  850. { ... but insert the symbol table later }
  851. st.next:=symtablestack;
  852. symtablestack:=st;
  853. end
  854. else
  855. { while compiling a system unit, some types are directly inserted }
  856. begin
  857. st.next:=symtablestack;
  858. symtablestack:=st;
  859. insert_intern_types(st);
  860. end;
  861. { now we know the place to insert the constants }
  862. constsymtable:=symtablestack;
  863. { move the global symtab from the temporary local to global }
  864. current_module.globalsymtable:=current_module.localsymtable;
  865. current_module.localsymtable:=nil;
  866. reset_global_defs;
  867. { number all units, so we know if a unit is used by this unit or
  868. needs to be added implicitly }
  869. current_module.numberunits;
  870. { ... parse the declarations }
  871. Comment(V_Used,'Parsing interface of '+current_module.modulename^);
  872. // Message1(parser_u_parsing_interface,current_module.realmodulename^);
  873. read_interface_declarations;
  874. { leave when we got an error }
  875. if (Errorcount>0) and not status.skip_error then
  876. begin
  877. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  878. status.skip_error:=true;
  879. exit;
  880. end;
  881. {else in inteface its somatimes necessary even if unused
  882. st^.allunitsused; }
  883. {$ifdef New_GDB}
  884. write_gdb_info;
  885. {$endIf Def New_GDB}
  886. if not(cs_compilesystem in aktmoduleswitches) then
  887. if (Errorcount=0) then
  888. tppumodule(current_module).getppucrc;
  889. { Parse the implementation section }
  890. consume(_IMPLEMENTATION);
  891. current_module.in_interface:=false;
  892. Comment(V_Used,'Loading implementation units from '+current_module.modulename^);
  893. // Message1(unit_u_start_parse_implementation,current_module.modulename^);
  894. parse_only:=false;
  895. { generates static symbol table }
  896. st:=tstaticsymtable.create(current_module.modulename^);
  897. current_module.localsymtable:=st;
  898. { remove the globalsymtable from the symtable stack }
  899. { to reinsert it after loading the implementation units }
  900. symtablestack:=unitst.next;
  901. { we don't want implementation units symbols in unitsymtable !! PM }
  902. refsymtable:=st;
  903. { Read the implementation units }
  904. parse_implementation_uses(unitst);
  905. if current_module.state=ms_compiled then
  906. begin
  907. RestoreUnitSyms;
  908. exit;
  909. end;
  910. { reset ranges/stabs in exported definitions }
  911. reset_global_defs;
  912. { All units are read, now give them a number }
  913. current_module.numberunits;
  914. { now we can change refsymtable }
  915. refsymtable:=st;
  916. { but reinsert the global symtable as lasts }
  917. unitst.next:=symtablestack;
  918. symtablestack:=unitst;
  919. tstoredsymtable(symtablestack).chainoperators;
  920. {$ifdef DEBUG}
  921. test_symtablestack;
  922. {$endif DEBUG}
  923. constsymtable:=symtablestack;
  924. {$ifdef Splitheap}
  925. if testsplit then
  926. begin
  927. Split_Heap;
  928. allow_special:=true;
  929. Switch_to_temp_heap;
  930. end;
  931. { it will report all crossings }
  932. allow_special:=false;
  933. {$endif Splitheap}
  934. Comment(V_Used,'Parsing implementation of '+current_module.modulename^);
  935. if current_module.in_interface then
  936. internalerror(200212285);
  937. // Message1(parser_u_parsing_implementation,current_module.modulename^);
  938. { Compile the unit }
  939. pd:=create_main_proc(current_module.modulename^+'_init',potype_unitinit,st);
  940. pd.aliasnames.insert('INIT$$'+current_module.modulename^);
  941. compile_proc_body(pd,true,false);
  942. release_main_proc(pd);
  943. { if the unit contains ansi/widestrings, initialization and
  944. finalization code must be forced }
  945. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  946. tstaticsymtable(current_module.localsymtable).needs_init_final;
  947. { should we force unit initialization? }
  948. { this is a hack, but how can it be done better ? }
  949. if force_init_final and ((current_module.flags and uf_init)=0) then
  950. begin
  951. gen_implicit_initfinal(initfinalcode,uf_init,st);
  952. codesegment.concatlist(initfinalcode);
  953. end;
  954. { finalize? }
  955. if token=_FINALIZATION then
  956. begin
  957. { set module options }
  958. current_module.flags:=current_module.flags or uf_finalize;
  959. { Compile the finalize }
  960. pd:=create_main_proc(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  961. pd.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  962. compile_proc_body(pd,true,false);
  963. release_main_proc(pd);
  964. end
  965. else if force_init_final then
  966. begin
  967. gen_implicit_initfinal(initfinalcode,uf_finalize,st);
  968. codesegment.concatlist(initfinalcode);
  969. end;
  970. { the last char should always be a point }
  971. consume(_POINT);
  972. { generate a list of threadvars }
  973. InsertThreadvars;
  974. { Generate resoucestrings }
  975. If ResourceStrings.ResStrCount>0 then
  976. begin
  977. ResourceStrings.CreateResourceStringList;
  978. current_module.flags:=current_module.flags or uf_has_resources;
  979. { only write if no errors found }
  980. if (Errorcount=0) then
  981. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  982. end;
  983. { test static symtable }
  984. if (Errorcount=0) then
  985. begin
  986. tstoredsymtable(st).allsymbolsused;
  987. tstoredsymtable(st).allunitsused;
  988. tstoredsymtable(st).allprivatesused;
  989. end;
  990. { size of the static data }
  991. datasize:=st.datasize;
  992. {$ifdef GDB}
  993. { add all used definitions even for implementation}
  994. if (cs_debuginfo in aktmoduleswitches) then
  995. begin
  996. {$IfnDef New_GDB}
  997. if assigned(current_module.globalsymtable) then
  998. begin
  999. { all types }
  1000. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  1001. { and all local symbols}
  1002. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  1003. end;
  1004. { all local types }
  1005. tglobalsymtable(st)^.concattypestabto(debuglist);
  1006. { and all local symbols}
  1007. st^.concatstabto(debuglist);
  1008. {$else New_GDB}
  1009. write_gdb_info;
  1010. {$endIf Def New_GDB}
  1011. end;
  1012. {$endif GDB}
  1013. reset_global_defs;
  1014. if (Errorcount=0) then
  1015. begin
  1016. { tests, if all (interface) forwards are resolved }
  1017. tstoredsymtable(symtablestack).check_forwards;
  1018. { check if all private fields are used }
  1019. tstoredsymtable(symtablestack).allprivatesused;
  1020. { remove cross unit overloads }
  1021. tstoredsymtable(symtablestack).unchain_overloaded;
  1022. end;
  1023. {$ifdef GDB}
  1024. tglobalsymtable(symtablestack).is_stab_written:=false;
  1025. {$endif GDB}
  1026. { leave when we got an error }
  1027. if (Errorcount>0) and not status.skip_error then
  1028. begin
  1029. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1030. status.skip_error:=true;
  1031. exit;
  1032. end;
  1033. { generate imports }
  1034. if current_module.uses_imports then
  1035. importlib.generatelib;
  1036. { insert own objectfile, or say that it's in a library
  1037. (no check for an .o when loading) }
  1038. if is_assembler_generated then
  1039. insertobjectfile
  1040. else
  1041. current_module.flags:=current_module.flags or uf_no_link;
  1042. if cs_local_browser in aktmoduleswitches then
  1043. current_module.localsymtable:=refsymtable;
  1044. {$ifdef GDB}
  1045. pu:=tused_unit(usedunits.first);
  1046. while assigned(pu) do
  1047. begin
  1048. if assigned(pu.u.globalsymtable) then
  1049. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  1050. pu:=tused_unit(pu.next);
  1051. end;
  1052. {$endif GDB}
  1053. if is_assembler_generated then
  1054. begin
  1055. { finish asmlist by adding segment starts }
  1056. insertsegment;
  1057. { assemble }
  1058. create_objectfile;
  1059. end;
  1060. { Write out the ppufile after the object file has been created }
  1061. store_interface_crc:=current_module.interface_crc;
  1062. store_crc:=current_module.crc;
  1063. if (Errorcount=0) then
  1064. tppumodule(current_module).writeppu;
  1065. if not(cs_compilesystem in aktmoduleswitches) then
  1066. if store_interface_crc<>current_module.interface_crc then
  1067. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  1068. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  1069. {$ifdef EXTDEBUG}
  1070. if not(cs_compilesystem in aktmoduleswitches) then
  1071. if (store_crc<>current_module.crc) and simplify_ppu then
  1072. Comment(V_Note,current_module.ppufilename^+' implementation CRC changed '+
  1073. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  1074. {$endif EXTDEBUG}
  1075. { remove static symtable (=refsymtable) here to save some mem }
  1076. if not (cs_local_browser in aktmoduleswitches) then
  1077. begin
  1078. st.free;
  1079. current_module.localsymtable:=nil;
  1080. end;
  1081. RestoreUnitSyms;
  1082. { leave when we got an error }
  1083. if (Errorcount>0) and not status.skip_error then
  1084. begin
  1085. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1086. status.skip_error:=true;
  1087. exit;
  1088. end;
  1089. initfinalcode.free;
  1090. Comment(V_Used,'Finished compiling module '+current_module.modulename^);
  1091. end;
  1092. procedure proc_program(islibrary : boolean);
  1093. var
  1094. main_file: tinputfile;
  1095. st : tsymtable;
  1096. hp : tmodule;
  1097. initfinalcode : taasmoutput;
  1098. pd : tprocdef;
  1099. begin
  1100. initfinalcode:=taasmoutput.create;
  1101. DLLsource:=islibrary;
  1102. Status.IsLibrary:=IsLibrary;
  1103. Status.IsExe:=true;
  1104. parse_only:=false;
  1105. { relocation works only without stabs under win32 !! PM }
  1106. { internal assembler uses rva for stabs info
  1107. so it should work with relocated DLLs }
  1108. if RelocSection and
  1109. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1110. (target_info.assem<>as_i386_pecoff) then
  1111. begin
  1112. include(aktglobalswitches,cs_link_strip);
  1113. { Warning stabs info does not work with reloc section !! }
  1114. if cs_debuginfo in aktmoduleswitches then
  1115. begin
  1116. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1117. Message(parser_w_parser_win32_debug_needs_WN);
  1118. exclude(aktmoduleswitches,cs_debuginfo);
  1119. end;
  1120. end;
  1121. { get correct output names }
  1122. main_file := current_scanner.inputfile;
  1123. while assigned(main_file.next) do
  1124. main_file := main_file.next;
  1125. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1126. if islibrary then
  1127. begin
  1128. consume(_LIBRARY);
  1129. stringdispose(current_module.modulename);
  1130. current_module.modulename:=stringdup(pattern);
  1131. current_module.islibrary:=true;
  1132. exportlib.preparelib(pattern);
  1133. consume(_ID);
  1134. consume(_SEMICOLON);
  1135. end
  1136. else
  1137. { is there an program head ? }
  1138. if token=_PROGRAM then
  1139. begin
  1140. consume(_PROGRAM);
  1141. stringdispose(current_module.modulename);
  1142. stringdispose(current_module.realmodulename);
  1143. current_module.modulename:=stringdup(pattern);
  1144. current_module.realmodulename:=stringdup(orgpattern);
  1145. if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1146. exportlib.preparelib(pattern);
  1147. consume(_ID);
  1148. if token=_LKLAMMER then
  1149. begin
  1150. consume(_LKLAMMER);
  1151. repeat
  1152. consume(_ID);
  1153. until not try_to_consume(_COMMA);
  1154. consume(_RKLAMMER);
  1155. end;
  1156. consume(_SEMICOLON);
  1157. end
  1158. else if (target_info.system in [system_i386_WIN32,system_i386_wdosx]) then
  1159. exportlib.preparelib(current_module.modulename^);
  1160. { global switches are read, so further changes aren't allowed }
  1161. current_module.in_global:=false;
  1162. { setup things using the global switches }
  1163. setupglobalswitches;
  1164. { set implementation flag }
  1165. current_module.in_interface:=false;
  1166. { insert after the unit symbol tables the static symbol table }
  1167. { of the program }
  1168. st:=tstaticsymtable.create(current_module.modulename^);;
  1169. current_module.localsymtable:=st;
  1170. refsymtable:=st;
  1171. { load standard units (system,objpas,profile unit) }
  1172. loaddefaultunits;
  1173. {Load the units used by the program we compile.}
  1174. if token=_USES then
  1175. loadunits;
  1176. tstoredsymtable(symtablestack).chainoperators;
  1177. { reset ranges/stabs in exported definitions }
  1178. reset_global_defs;
  1179. { All units are read, now give them a number }
  1180. current_module.numberunits;
  1181. {Insert the name of the main program into the symbol table.}
  1182. if current_module.realmodulename^<>'' then
  1183. st.insert(tunitsym.create(current_module.realmodulename^,st));
  1184. { ...is also constsymtable, this is the symtable where }
  1185. { the elements of enumeration types are inserted }
  1186. constsymtable:=st;
  1187. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1188. { The program intialization needs an alias, so it can be called
  1189. from the bootstrap code.}
  1190. if islibrary then
  1191. begin
  1192. pd:=create_main_proc(current_module.modulename^+'_main',potype_proginit,st);
  1193. { Win32 startup code needs a single name }
  1194. // if (target_info.system in [system_i386_win32,system_i386_wdosx]) then
  1195. pd.aliasnames.insert('PASCALMAIN');
  1196. { this code is called from C so we need to save some
  1197. registers }
  1198. include(pd.procoptions,po_savestdregs);
  1199. end
  1200. else
  1201. begin
  1202. pd:=create_main_proc('main',potype_proginit,st);
  1203. pd.aliasnames.insert('PASCALMAIN');
  1204. end;
  1205. {$IFDEF SPARC}
  1206. current_procinfo.After_Header;
  1207. {main function is declared as
  1208. PROCEDURE main(ArgC:Integer;ArgV,EnvP:ARRAY OF PChar):Integer;CDECL;
  1209. So, all parameters are passerd into registers in sparc architecture.}
  1210. {$ENDIF SPARC}
  1211. compile_proc_body(pd,true,false);
  1212. release_main_proc(pd);
  1213. { should we force unit initialization? }
  1214. if tstaticsymtable(current_module.localsymtable).needs_init_final then
  1215. begin
  1216. { initialize section }
  1217. gen_implicit_initfinal(initfinalcode,uf_init,st);
  1218. codesegment.concatlist(initfinalcode);
  1219. { finalize section }
  1220. gen_implicit_initfinal(initfinalcode,uf_finalize,st);
  1221. codesegment.concatlist(initfinalcode);
  1222. end;
  1223. { Add symbol to the exports section for win32 so smartlinking a
  1224. DLL will include the edata section }
  1225. if assigned(exportlib) and
  1226. (target_info.system in [system_i386_win32,system_i386_wdosx]) and
  1227. assigned(current_module._exports.first) then
  1228. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1229. If ResourceStrings.ResStrCount>0 then
  1230. begin
  1231. ResourceStrings.CreateResourceStringList;
  1232. { only write if no errors found }
  1233. if (Errorcount=0) then
  1234. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1235. end;
  1236. { finalize? }
  1237. if token=_FINALIZATION then
  1238. begin
  1239. { set module options }
  1240. current_module.flags:=current_module.flags or uf_finalize;
  1241. { Compile the finalize }
  1242. pd:=create_main_proc(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1243. pd.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1244. compile_proc_body(pd,true,false);
  1245. release_main_proc(pd);
  1246. end;
  1247. { consume the last point }
  1248. consume(_POINT);
  1249. {$ifdef New_GDB}
  1250. write_gdb_info;
  1251. {$endIf Def New_GDB}
  1252. { leave when we got an error }
  1253. if (Errorcount>0) and not status.skip_error then
  1254. begin
  1255. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1256. status.skip_error:=true;
  1257. exit;
  1258. end;
  1259. { test static symtable }
  1260. if (Errorcount=0) then
  1261. begin
  1262. tstoredsymtable(st).allsymbolsused;
  1263. tstoredsymtable(st).allunitsused;
  1264. tstoredsymtable(st).allprivatesused;
  1265. end;
  1266. { generate a list of threadvars }
  1267. InsertThreadvars;
  1268. { generate imports }
  1269. if current_module.uses_imports then
  1270. importlib.generatelib;
  1271. if islibrary or
  1272. (target_info.system in [system_i386_WIN32,system_i386_wdosx]) or
  1273. (target_info.system=system_i386_NETWARE) then
  1274. exportlib.generatelib;
  1275. { insert Tables and Heap }
  1276. insertThreadVarTablesTable;
  1277. insertResourceTablesTable;
  1278. insertinitfinaltable;
  1279. insertheap;
  1280. insertstacklength;
  1281. datasize:=symtablestack.datasize;
  1282. { finish asmlist by adding segment starts }
  1283. insertsegment;
  1284. { insert own objectfile }
  1285. insertobjectfile;
  1286. { assemble and link }
  1287. create_objectfile;
  1288. { leave when we got an error }
  1289. if (Errorcount>0) and not status.skip_error then
  1290. begin
  1291. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1292. status.skip_error:=true;
  1293. exit;
  1294. end;
  1295. { create the executable when we are at level 1 }
  1296. if (compile_level=1) then
  1297. begin
  1298. { insert all .o files from all loaded units }
  1299. hp:=tmodule(loaded_units.first);
  1300. while assigned(hp) do
  1301. begin
  1302. linker.AddModuleFiles(hp);
  1303. hp:=tmodule(hp.next);
  1304. end;
  1305. { write .def file }
  1306. if (cs_link_deffile in aktglobalswitches) then
  1307. deffile.writefile;
  1308. { finally we can create a executable }
  1309. if (not current_module.is_unit) then
  1310. begin
  1311. if DLLSource then
  1312. linker.MakeSharedLibrary
  1313. else
  1314. linker.MakeExecutable;
  1315. end;
  1316. end;
  1317. initfinalcode.free;
  1318. end;
  1319. end.
  1320. {
  1321. $Log$
  1322. Revision 1.104 2003-04-28 21:19:02 peter
  1323. * fix stabs generation for implicit initfinal
  1324. Revision 1.103 2003/04/27 11:21:34 peter
  1325. * aktprocdef renamed to current_procdef
  1326. * procinfo renamed to current_procinfo
  1327. * procinfo will now be stored in current_module so it can be
  1328. cleaned up properly
  1329. * gen_main_procsym changed to create_main_proc and release_main_proc
  1330. to also generate a tprocinfo structure
  1331. * fixed unit implicit initfinal
  1332. Revision 1.102 2003/04/27 07:29:50 peter
  1333. * current_procdef cleanup, current_procdef is now always nil when parsing
  1334. a new procdef declaration
  1335. * aktprocsym removed
  1336. * lexlevel removed, use symtable.symtablelevel instead
  1337. * implicit init/final code uses the normal genentry/genexit
  1338. * funcret state checking updated for new funcret handling
  1339. Revision 1.101 2003/04/23 12:35:34 florian
  1340. * fixed several issues with powerpc
  1341. + applied a patch from Jonas for nested function calls (PowerPC only)
  1342. * ...
  1343. Revision 1.100 2003/04/12 15:13:03 peter
  1344. * Use the original unitname when defining a unitsym
  1345. Revision 1.99 2003/03/23 23:21:42 hajny
  1346. + emx target added
  1347. Revision 1.98 2003/03/17 22:20:08 peter
  1348. *** empty log message ***
  1349. Revision 1.97 2003/03/17 13:36:39 peter
  1350. * fix import linking under linux
  1351. Revision 1.96 2003/02/19 22:00:14 daniel
  1352. * Code generator converted to new register notation
  1353. - Horribily outdated todo.txt removed
  1354. Revision 1.95 2003/02/06 22:36:55 mazen
  1355. * fixing bug related to errornous program main entry stack frame
  1356. Revision 1.94 2003/01/30 21:46:20 peter
  1357. * tai_const_symbol.createdataname added
  1358. Revision 1.93 2003/01/11 11:19:54 hajny
  1359. * correction from rev. 1.88 put back
  1360. Revision 1.92 2003/01/08 18:43:56 daniel
  1361. * Tregister changed into a record
  1362. Revision 1.91 2003/01/05 13:36:53 florian
  1363. * x86-64 compiles
  1364. + very basic support for float128 type (x86-64 only)
  1365. Revision 1.90 2002/12/29 18:17:23 peter
  1366. * insert unitsym with the name as specified in the uses list
  1367. Revision 1.89 2002/12/29 14:57:50 peter
  1368. * unit loading changed to first register units and load them
  1369. afterwards. This is needed to support uses xxx in yyy correctly
  1370. * unit dependency check fixed
  1371. Revision 1.88 2002/12/27 19:09:33 hajny
  1372. * another (hopefully final ;-) ) fix for not linked import libraries for units with no code
  1373. Revision 1.87 2002/12/24 23:32:56 peter
  1374. * Use FixFilename for specified unit sourcefile in uses
  1375. Revision 1.86 2002/12/06 16:56:58 peter
  1376. * only compile cs_fp_emulation support when cpufpuemu is defined
  1377. * define cpufpuemu for m68k only
  1378. Revision 1.85 2002/11/30 21:32:24 carl
  1379. + Add loading of softfpu in emulation mode
  1380. + Correct routine call for softfpu
  1381. * Extended type must also be defined even with softfpu
  1382. Revision 1.84 2002/11/15 01:58:53 peter
  1383. * merged changes from 1.0.7 up to 04-11
  1384. - -V option for generating bug report tracing
  1385. - more tracing for option parsing
  1386. - errors for cdecl and high()
  1387. - win32 import stabs
  1388. - win32 records<=8 are returned in eax:edx (turned off by default)
  1389. - heaptrc update
  1390. - more info for temp management in .s file with EXTDEBUG
  1391. Revision 1.83 2002/11/09 15:33:26 carl
  1392. * major alignment updates
  1393. Revision 1.82 2002/10/16 06:32:52 michael
  1394. + Renamed thread unit to systhrds
  1395. Revision 1.81 2002/10/14 19:42:34 peter
  1396. * only use init tables for threadvars
  1397. Revision 1.80 2002/10/06 19:41:30 peter
  1398. * Add finalization of typed consts
  1399. * Finalization of globals in the main program
  1400. Revision 1.79 2002/09/09 17:34:15 peter
  1401. * tdicationary.replace added to replace and item in a dictionary. This
  1402. is only allowed for the same name
  1403. * varsyms are inserted in symtable before the types are parsed. This
  1404. fixes the long standing "var longint : longint" bug
  1405. - consume_idlist and idstringlist removed. The loops are inserted
  1406. at the callers place and uses the symtable for duplicate id checking
  1407. Revision 1.78 2002/09/07 15:25:07 peter
  1408. * old logs removed and tabs fixed
  1409. Revision 1.77 2002/09/03 16:26:27 daniel
  1410. * Make Tprocdef.defs protected
  1411. Revision 1.76 2002/09/02 18:46:26 peter
  1412. * insert PASCALMAIN in library for Win32 only
  1413. Revision 1.75 2002/08/31 15:59:30 florian
  1414. + HEAP* stuff must be generated for Linux/PPC as well
  1415. + direct assembler reader searches now global and static symtables as well
  1416. Revision 1.74 2002/08/25 19:25:20 peter
  1417. * sym.insert_in_data removed
  1418. * symtable.insertvardata/insertconstdata added
  1419. * removed insert_in_data call from symtable.insert, it needs to be
  1420. called separatly. This allows to deref the address calculation
  1421. * procedures now calculate the parast addresses after the procedure
  1422. directives are parsed. This fixes the cdecl parast problem
  1423. * push_addr_param has an extra argument that specifies if cdecl is used
  1424. or not
  1425. Revision 1.73 2002/08/18 20:06:25 peter
  1426. * inlining is now also allowed in interface
  1427. * renamed write/load to ppuwrite/ppuload
  1428. * tnode storing in ppu
  1429. * nld,ncon,nbas are already updated for storing in ppu
  1430. Revision 1.72 2002/08/17 09:23:39 florian
  1431. * first part of procinfo rewrite
  1432. Revision 1.71 2002/08/11 13:24:12 peter
  1433. * saving of asmsymbols in ppu supported
  1434. * asmsymbollist global is removed and moved into a new class
  1435. tasmlibrarydata that will hold the info of a .a file which
  1436. corresponds with a single module. Added librarydata to tmodule
  1437. to keep the library info stored for the module. In the future the
  1438. objectfiles will also be stored to the tasmlibrarydata class
  1439. * all getlabel/newasmsymbol and friends are moved to the new class
  1440. Revision 1.70 2002/08/10 14:46:29 carl
  1441. + moved target_cpu_string to cpuinfo
  1442. * renamed asmmode enum.
  1443. * assembler reader has now less ifdef's
  1444. * move from nppcmem.pas -> ncgmem.pas vec. node.
  1445. Revision 1.69 2002/07/26 21:15:41 florian
  1446. * rewrote the system handling
  1447. Revision 1.68 2002/07/04 20:43:01 florian
  1448. * first x86-64 patches
  1449. Revision 1.67 2002/07/01 18:46:25 peter
  1450. * internal linker
  1451. * reorganized aasm layer
  1452. Revision 1.66 2002/05/16 19:46:43 carl
  1453. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1454. + try to fix temp allocation (still in ifdef)
  1455. + generic constructor calls
  1456. + start of tassembler / tmodulebase class cleanup
  1457. Revision 1.65 2002/05/14 19:34:49 peter
  1458. * removed old logs and updated copyright year
  1459. Revision 1.64 2002/05/12 16:53:09 peter
  1460. * moved entry and exitcode to ncgutil and cgobj
  1461. * foreach gets extra argument for passing local data to the
  1462. iterator function
  1463. * -CR checks also class typecasts at runtime by changing them
  1464. into as
  1465. * fixed compiler to cycle with the -CR option
  1466. * fixed stabs with elf writer, finally the global variables can
  1467. be watched
  1468. * removed a lot of routines from cga unit and replaced them by
  1469. calls to cgobj
  1470. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1471. u32bit then the other is typecasted also to u32bit without giving
  1472. a rangecheck warning/error.
  1473. * fixed pascal calling method with reversing also the high tree in
  1474. the parast, detected by tcalcst3 test
  1475. Revision 1.63 2002/05/06 19:54:50 carl
  1476. + added more patches from Mazen for SPARC port
  1477. Revision 1.62 2002/04/20 21:32:24 carl
  1478. + generic FPC_CHECKPOINTER
  1479. + first parameter offset in stack now portable
  1480. * rename some constants
  1481. + move some cpu stuff to other units
  1482. - remove unused constents
  1483. * fix stacksize for some targets
  1484. * fix generic size problems which depend now on EXTEND_SIZE constant
  1485. Revision 1.61 2002/04/19 15:46:02 peter
  1486. * mangledname rewrite, tprocdef.mangledname is now created dynamicly
  1487. in most cases and not written to the ppu
  1488. * add mangeledname_prefix() routine to generate the prefix of
  1489. manglednames depending on the current procedure, object and module
  1490. * removed static procprefix since the mangledname is now build only
  1491. on demand from tprocdef.mangledname
  1492. Revision 1.60 2002/04/14 16:53:10 carl
  1493. + align code section and data section according to alignment rules
  1494. Revision 1.59 2002/04/07 17:58:38 carl
  1495. + generic stack checking
  1496. Revision 1.58 2002/04/04 19:06:03 peter
  1497. * removed unused units
  1498. * use tlocation.size in cg.a_*loc*() routines
  1499. }