pmodules.pas 62 KB

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