pmodules.pas 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Handles the parsing and loading of the modules (ppufiles)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit pmodules;
  19. {$i defines.inc}
  20. {$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,symdef,symsym,symtable,aasm,
  30. cgbase,
  31. cga,
  32. link,assemble,import,export,gendef,ppu,comprsrc,
  33. cresstr,cpubase,cpuasm,
  34. {$ifdef GDB}
  35. gdb,
  36. {$endif GDB}
  37. scanner,pbase,pexpr,psystem,psub;
  38. procedure create_objectfile;
  39. var
  40. DLLScanner : TDLLScanner;
  41. s : string;
  42. KeepShared : TStringList;
  43. begin
  44. { try to create import entries from system dlls }
  45. if target_info.DllScanSupported and
  46. (not current_module.linkOtherSharedLibs.Empty) then
  47. begin
  48. { Init DLLScanner }
  49. if assigned(CDLLScanner[target_info.target]) then
  50. DLLScanner:=CDLLScanner[target_info.target].Create
  51. else
  52. internalerror(200104121);
  53. KeepShared:=TStringList.Create;
  54. { Walk all shared libs }
  55. While not current_module.linkOtherSharedLibs.Empty do
  56. begin
  57. S:=current_module.linkOtherSharedLibs.Getusemask(link_allways);
  58. if not DLLScanner.scan(s) then
  59. KeepShared.Concat(s);
  60. end;
  61. DLLscanner.Free;
  62. { Recreate import section }
  63. if (target_info.target in [target_i386_win32,target_i386_wdosx]) then
  64. begin
  65. if assigned(importssection)then
  66. importssection.clear
  67. else
  68. importssection:=taasmoutput.Create;
  69. importlib.generatelib;
  70. end;
  71. { Readd the not processed files }
  72. while not KeepShared.Empty do
  73. begin
  74. s:=KeepShared.GetFirst;
  75. current_module.linkOtherSharedLibs.add(s,link_allways);
  76. end;
  77. KeepShared.Free;
  78. end;
  79. { create the .s file and assemble it }
  80. GenerateAsm(false);
  81. { Also create a smartlinked version ? }
  82. if (cs_create_smart in aktmoduleswitches) then
  83. begin
  84. { regenerate the importssection for win32 }
  85. if assigned(importssection) and
  86. (target_info.target in [target_i386_win32,target_i386_wdosx]) then
  87. begin
  88. importsSection.clear;
  89. importlib.generatesmartlib;
  90. end;
  91. GenerateAsm(true);
  92. if target_asm.needar then
  93. Linker.MakeStaticLibrary;
  94. end;
  95. { resource files }
  96. CompileResourceFiles;
  97. end;
  98. procedure insertobjectfile;
  99. { Insert the used object file for this unit in the used list for this unit }
  100. begin
  101. current_module.linkunitofiles.add(current_module.objfilename^,link_static);
  102. current_module.flags:=current_module.flags or uf_static_linked;
  103. if (cs_create_smart in aktmoduleswitches) then
  104. begin
  105. current_module.linkunitstaticlibs.add(current_module.staticlibfilename^,link_smart);
  106. current_module.flags:=current_module.flags or uf_smart_linked;
  107. end;
  108. end;
  109. procedure insertsegment;
  110. procedure fixseg(p:TAAsmoutput;sec:tsection);
  111. begin
  112. p.insert(Tai_section.Create(sec));
  113. if (cs_create_smart in aktmoduleswitches) then
  114. p.insert(Tai_cut.Create);
  115. p.concat(Tai_section.Create(sec_none));
  116. end;
  117. begin
  118. { Insert Ident of the compiler }
  119. if (not (cs_create_smart in aktmoduleswitches))
  120. {$ifndef EXTDEBUG}
  121. and (not current_module.is_unit)
  122. {$endif}
  123. then
  124. begin
  125. dataSegment.insert(Tai_align.Create(4));
  126. dataSegment.insert(Tai_string.Create('FPC '+full_version_string+
  127. ' ['+date_string+'] for '+target_cpu_string+' - '+target_info.shortname));
  128. end;
  129. { finish codesegment }
  130. {$ifdef i386}
  131. codeSegment.concat(Tai_align.Create(16));
  132. {$else}
  133. if cs_littlesize in aktglobalswitches then
  134. codesegment.concat(tai_align.create(2))
  135. else
  136. codesegment.concat(tai_align.create(4));
  137. {$endif}
  138. { Insert start and end of sections }
  139. fixseg(codesegment,sec_code);
  140. fixseg(datasegment,sec_data);
  141. fixseg(bsssegment,sec_bss);
  142. { we should use .rdata section for these two no ? }
  143. { .rdata is a read only data section (PM) }
  144. fixseg(rttilist,sec_data);
  145. fixseg(consts,sec_data);
  146. if assigned(resourcestringlist) then
  147. fixseg(resourcestringlist,sec_data);
  148. {$ifdef GDB}
  149. if assigned(debuglist) then
  150. begin
  151. debugList.insert(Tai_symbol.Createname('gcc2_compiled',0));
  152. debugList.insert(Tai_symbol.Createname('fpc_compiled',0));
  153. fixseg(debuglist,sec_code);
  154. end;
  155. {$endif GDB}
  156. end;
  157. Procedure InsertResourceTablesTable;
  158. var
  159. hp : tused_unit;
  160. ResourceStringTables : taasmoutput;
  161. count : longint;
  162. begin
  163. ResourceStringTables:=TAAsmOutput.Create;
  164. count:=0;
  165. hp:=tused_unit(usedunits.first);
  166. while assigned(hp) do
  167. begin
  168. If (hp.u.flags and uf_has_resources)=uf_has_resources then
  169. begin
  170. ResourceStringTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_RESOURCESTRINGLIST'));
  171. inc(count);
  172. end;
  173. hp:=tused_unit(hp.next);
  174. end;
  175. { Add program resources, if any }
  176. If ResourceStringList<>Nil then
  177. begin
  178. ResourceStringTables.concat(Tai_const_symbol.Createname(current_module.modulename^+'_RESOURCESTRINGLIST'));
  179. Inc(Count);
  180. end;
  181. { TableCount }
  182. ResourceStringTables.insert(Tai_const.Create_32bit(count));
  183. ResourceStringTables.insert(Tai_symbol.Createdataname_global('FPC_RESOURCESTRINGTABLES',0));
  184. ResourceStringTables.concat(Tai_symbol_end.Createname('FPC_RESOURCESTRINGTABLES'));
  185. { insert in data segment }
  186. if (cs_create_smart in aktmoduleswitches) then
  187. dataSegment.concat(Tai_cut.Create);
  188. dataSegment.concatlist(ResourceStringTables);
  189. ResourceStringTables.free;
  190. end;
  191. procedure InsertInitFinalTable;
  192. var
  193. hp : tused_unit;
  194. unitinits : taasmoutput;
  195. count : longint;
  196. begin
  197. unitinits:=TAAsmOutput.Create;
  198. count:=0;
  199. hp:=tused_unit(usedunits.first);
  200. while assigned(hp) do
  201. begin
  202. { call the unit init code and make it external }
  203. if (hp.u.flags and (uf_init or uf_finalize))<>0 then
  204. begin
  205. if (hp.u.flags and uf_init)<>0 then
  206. unitinits.concat(Tai_const_symbol.Createname('INIT$$'+hp.u.modulename^))
  207. else
  208. unitinits.concat(Tai_const.Create_32bit(0));
  209. if (hp.u.flags and uf_finalize)<>0 then
  210. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+hp.u.modulename^))
  211. else
  212. unitinits.concat(Tai_const.Create_32bit(0));
  213. inc(count);
  214. end;
  215. hp:=tused_unit(hp.next);
  216. end;
  217. if current_module.islibrary then
  218. if (current_module.flags and uf_finalize)<>0 then
  219. begin
  220. { INIT code is done by PASCALMAIN calling }
  221. unitinits.concat(Tai_const.Create_32bit(0));
  222. unitinits.concat(Tai_const_symbol.Createname('FINALIZE$$'+current_module.modulename^));
  223. inc(count);
  224. end;
  225. { TableCount,InitCount }
  226. unitinits.insert(Tai_const.Create_32bit(0));
  227. unitinits.insert(Tai_const.Create_32bit(count));
  228. unitinits.insert(Tai_symbol.Createdataname_global('INITFINAL',0));
  229. unitinits.concat(Tai_symbol_end.Createname('INITFINAL'));
  230. { insert in data segment }
  231. if (cs_create_smart in aktmoduleswitches) then
  232. dataSegment.concat(Tai_cut.Create);
  233. dataSegment.concatlist(unitinits);
  234. unitinits.free;
  235. end;
  236. procedure insertheap;
  237. begin
  238. if (cs_create_smart in aktmoduleswitches) then
  239. begin
  240. bssSegment.concat(Tai_cut.Create);
  241. dataSegment.concat(Tai_cut.Create);
  242. end;
  243. { On the Macintosh Classic M68k Architecture
  244. The Heap variable is simply a POINTER to the
  245. real HEAP. The HEAP must be set up by the RTL
  246. and must store the pointer in this value.
  247. On OS/2 the heap is also intialized by the RTL. We do
  248. not output a pointer }
  249. case target_info.target of
  250. {$ifdef i386}
  251. target_i386_OS2:
  252. ;
  253. {$endif i386}
  254. {$ifdef alpha}
  255. target_alpha_linux:
  256. ;
  257. {$endif alpha}
  258. {$ifdef powerpc}
  259. target_powerpc_linux:
  260. ;
  261. {$endif powerpc}
  262. {$ifdef m68k}
  263. target_m68k_Mac:
  264. bssSegment.concat(Tai_datablock.Create_global('HEAP',4));
  265. target_m68k_PalmOS:
  266. ;
  267. {$endif m68k}
  268. else
  269. bssSegment.concat(Tai_datablock.Create_global('HEAP',heapsize));
  270. end;
  271. {$ifdef m68k}
  272. if target_info.target<>target_m68k_PalmOS then
  273. begin
  274. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  275. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  276. end;
  277. {$else m68k}
  278. dataSegment.concat(Tai_symbol.Createdataname_global('HEAPSIZE',4));
  279. dataSegment.concat(Tai_const.Create_32bit(heapsize));
  280. {$endif m68k}
  281. end;
  282. procedure insertstacklength;
  283. begin
  284. { stacksize can be specified and is now simulated }
  285. dataSegment.concat(Tai_symbol.Createdataname_global('__stklen',4));
  286. dataSegment.concat(Tai_const.Create_32bit(stacksize));
  287. end;
  288. procedure loaddefaultunits;
  289. var
  290. hp : tmodule;
  291. unitsym : tunitsym;
  292. begin
  293. { are we compiling the system unit? }
  294. if (cs_compilesystem in aktmoduleswitches) then
  295. begin
  296. { create system defines }
  297. createconstdefs;
  298. { we don't need to reset anything, it's already done in parser.pas }
  299. exit;
  300. end;
  301. { insert the system unit, it is allways the first }
  302. hp:=loadunit('System','');
  303. systemunit:=tglobalsymtable(hp.globalsymtable);
  304. { it's always the first unit }
  305. systemunit.next:=nil;
  306. symtablestack:=systemunit;
  307. { add to the used units }
  308. current_module.used_units.concat(tused_unit.create(hp,true));
  309. unitsym:=tunitsym.create('System',systemunit);
  310. inc(unitsym.refs);
  311. refsymtable.insert(unitsym);
  312. { read default constant definitions }
  313. make_ref:=false;
  314. readconstdefs;
  315. make_ref:=true;
  316. { Objpas unit? }
  317. if m_objpas in aktmodeswitches then
  318. begin
  319. hp:=loadunit('ObjPas','');
  320. tsymtable(hp.globalsymtable).next:=symtablestack;
  321. symtablestack:=hp.globalsymtable;
  322. { add to the used units }
  323. current_module.used_units.concat(tused_unit.create(hp,true));
  324. unitsym:=tunitsym.create('ObjPas',hp.globalsymtable);
  325. inc(unitsym.refs);
  326. refsymtable.insert(unitsym);
  327. end;
  328. { Profile unit? Needed for go32v2 only }
  329. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  330. begin
  331. hp:=loadunit('Profile','');
  332. tsymtable(hp.globalsymtable).next:=symtablestack;
  333. symtablestack:=hp.globalsymtable;
  334. { add to the used units }
  335. current_module.used_units.concat(tused_unit.create(hp,true));
  336. unitsym:=tunitsym.create('Profile',hp.globalsymtable);
  337. inc(unitsym.refs);
  338. refsymtable.insert(unitsym);
  339. end;
  340. { Units only required for main module }
  341. if not(current_module.is_unit) then
  342. begin
  343. { Heaptrc unit }
  344. if (cs_gdb_heaptrc in aktglobalswitches) then
  345. begin
  346. hp:=loadunit('HeapTrc','');
  347. tsymtable(hp.globalsymtable).next:=symtablestack;
  348. symtablestack:=hp.globalsymtable;
  349. { add to the used units }
  350. current_module.used_units.concat(tused_unit.create(hp,true));
  351. unitsym:=tunitsym.create('HeapTrc',hp.globalsymtable);
  352. inc(unitsym.refs);
  353. refsymtable.insert(unitsym);
  354. end;
  355. { Lineinfo unit }
  356. if (cs_gdb_lineinfo in aktglobalswitches) then
  357. begin
  358. hp:=loadunit('LineInfo','');
  359. tsymtable(hp.globalsymtable).next:=symtablestack;
  360. symtablestack:=hp.globalsymtable;
  361. { add to the used units }
  362. current_module.used_units.concat(tused_unit.create(hp,true));
  363. unitsym:=tunitsym.create('LineInfo',hp.globalsymtable);
  364. inc(unitsym.refs);
  365. refsymtable.insert(unitsym);
  366. end;
  367. end;
  368. { save default symtablestack }
  369. defaultsymtablestack:=symtablestack;
  370. end;
  371. procedure loadunits;
  372. var
  373. s,sorg : stringid;
  374. fn : string;
  375. pu,
  376. hp : tused_unit;
  377. hp2 : tmodule;
  378. hp3 : tsymtable;
  379. oldprocsym:tprocsym;
  380. oldprocdef:tprocdef;
  381. unitsym : tunitsym;
  382. begin
  383. oldprocsym:=aktprocsym;
  384. oldprocdef:=aktprocdef;
  385. consume(_USES);
  386. {$ifdef DEBUG}
  387. test_symtablestack;
  388. {$endif DEBUG}
  389. repeat
  390. s:=pattern;
  391. sorg:=orgpattern;
  392. consume(_ID);
  393. { support "<unit> in '<file>'" construct, but not for tp7 }
  394. if not(m_tp7 in aktmodeswitches) then
  395. begin
  396. if try_to_consume(_OP_IN) then
  397. fn:=get_stringconst
  398. else
  399. fn:='';
  400. end;
  401. { Give a warning if objpas is loaded }
  402. if s='OBJPAS' then
  403. Message(parser_w_no_objpas_use_mode);
  404. { check if the unit is already used }
  405. pu:=tused_unit(current_module.used_units.first);
  406. while assigned(pu) do
  407. begin
  408. if (pu.name^=s) then
  409. break;
  410. pu:=tused_unit(pu.next);
  411. end;
  412. { avoid uses of itself }
  413. if not assigned(pu) and (s<>current_module.modulename^) then
  414. begin
  415. { load the unit }
  416. hp2:=loadunit(sorg,fn);
  417. { the current module uses the unit hp2 }
  418. current_module.used_units.concat(tused_unit.create(hp2,not current_module.in_implementation));
  419. tused_unit(current_module.used_units.last).in_uses:=true;
  420. if current_module.compiled then
  421. exit;
  422. unitsym:=tunitsym.create(sorg,hp2.globalsymtable);
  423. { never claim about unused unit if
  424. there is init or finalize code PM }
  425. if (hp2.flags and (uf_init or uf_finalize))<>0 then
  426. inc(unitsym.refs);
  427. refsymtable.insert(unitsym);
  428. end
  429. else
  430. Message1(sym_e_duplicate_id,s);
  431. if token=_COMMA then
  432. begin
  433. pattern:='';
  434. consume(_COMMA);
  435. end
  436. else
  437. break;
  438. until false;
  439. consume(_SEMICOLON);
  440. { set the symtable to systemunit so it gets reorderd correctly }
  441. symtablestack:=defaultsymtablestack;
  442. { now insert the units in the symtablestack }
  443. hp:=tused_unit(current_module.used_units.first);
  444. while assigned(hp) do
  445. begin
  446. {$IfDef GDB}
  447. if (cs_debuginfo in aktmoduleswitches) and
  448. (cs_gdb_dbx in aktglobalswitches) and
  449. not hp.is_stab_written then
  450. begin
  451. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  452. hp.is_stab_written:=true;
  453. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  454. end;
  455. {$EndIf GDB}
  456. if hp.in_uses then
  457. begin
  458. hp3:=symtablestack;
  459. while assigned(hp3) do
  460. begin
  461. { insert units only once ! }
  462. if hp.u.globalsymtable=hp3 then
  463. break;
  464. hp3:=hp3.next;
  465. { unit isn't inserted }
  466. if hp3=nil then
  467. begin
  468. tsymtable(hp.u.globalsymtable).next:=symtablestack;
  469. symtablestack:=tsymtable(hp.u.globalsymtable);
  470. {$ifdef DEBUG}
  471. test_symtablestack;
  472. {$endif DEBUG}
  473. end;
  474. end;
  475. end;
  476. hp:=tused_unit(hp.next);
  477. end;
  478. aktprocsym:=oldprocsym;
  479. aktprocdef:=oldprocdef;
  480. end;
  481. procedure write_gdb_info;
  482. {$IfDef GDB}
  483. var
  484. hp : tused_unit;
  485. begin
  486. if not (cs_debuginfo in aktmoduleswitches) then
  487. exit;
  488. if (cs_gdb_dbx in aktglobalswitches) then
  489. begin
  490. debugList.concat(Tai_asm_comment.Create(strpnew('EINCL of global '+
  491. tglobalsymtable(current_module.globalsymtable).name^+' has index '+
  492. tostr(tglobalsymtable(current_module.globalsymtable).unitid))));
  493. debugList.concat(Tai_stabs.Create(strpnew('"'+
  494. tglobalsymtable(current_module.globalsymtable).name^+'",'+
  495. tostr(N_EINCL)+',0,0,0')));
  496. tglobalsymtable(current_module.globalsymtable).dbx_count_ok:={true}false;
  497. dbx_counter:=tglobalsymtable(current_module.globalsymtable).prev_dbx_counter;
  498. do_count_dbx:=false;
  499. end;
  500. { now insert the units in the symtablestack }
  501. hp:=tused_unit(current_module.used_units.first);
  502. while assigned(hp) do
  503. begin
  504. if (cs_debuginfo in aktmoduleswitches) and
  505. not hp.is_stab_written then
  506. begin
  507. tglobalsymtable(hp.u.globalsymtable).concattypestabto(debuglist);
  508. hp.is_stab_written:=true;
  509. hp.unitid:=tsymtable(hp.u.globalsymtable).unitid;
  510. end;
  511. hp:=tused_unit(hp.next);
  512. end;
  513. if current_module.in_implementation and
  514. assigned(current_module.localsymtable) then
  515. begin
  516. { all types }
  517. tstaticsymtable(current_module.localsymtable).concattypestabto(debuglist);
  518. { and all local symbols}
  519. tstaticsymtable(current_module.localsymtable).concatstabto(debuglist);
  520. end
  521. else if assigned(current_module.globalsymtable) then
  522. begin
  523. { all types }
  524. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  525. { and all local symbols}
  526. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  527. end;
  528. end;
  529. {$Else GDB}
  530. begin
  531. end;
  532. {$EndIf GDB}
  533. procedure parse_implementation_uses(symt:tsymtable);
  534. begin
  535. if token=_USES then
  536. begin
  537. loadunits;
  538. {$ifdef DEBUG}
  539. test_symtablestack;
  540. {$endif DEBUG}
  541. end;
  542. end;
  543. procedure setupglobalswitches;
  544. begin
  545. { can't have local browser when no global browser }
  546. if (cs_local_browser in aktmoduleswitches) and
  547. not(cs_browser in aktmoduleswitches) then
  548. exclude(aktmoduleswitches,cs_local_browser);
  549. { define a symbol in delphi,objfpc,tp,gpc mode }
  550. if (m_delphi in aktmodeswitches) then
  551. current_scanner.def_macro('FPC_DELPHI')
  552. else
  553. if (m_tp7 in aktmodeswitches) then
  554. current_scanner.def_macro('FPC_TP')
  555. else
  556. if (m_objfpc in aktmodeswitches) then
  557. current_scanner.def_macro('FPC_OBJFPC')
  558. else
  559. if (m_gpc in aktmodeswitches) then
  560. current_scanner.def_macro('FPC_GPC');
  561. end;
  562. procedure gen_main_procsym(const name:string;options:tproctypeoption;st:tsymtable);
  563. var
  564. stt : tsymtable;
  565. procdefs : pprocdeflist;
  566. begin
  567. {Generate a procsym for main}
  568. make_ref:=false;
  569. aktprocsym:=tprocsym.create('$'+name);
  570. { main are allways used }
  571. inc(aktprocsym.refs);
  572. {Try to insert in in static symtable ! }
  573. stt:=symtablestack;
  574. symtablestack:=st;
  575. aktprocdef:=tprocdef.create;
  576. new(procdefs);
  577. procdefs^.def:=aktprocdef;
  578. procdefs^.next:=aktprocsym.defs;
  579. aktprocsym.defs:=procdefs;
  580. aktprocdef.procsym:=aktprocsym;
  581. symtablestack:=stt;
  582. aktprocdef.proctypeoption:=options;
  583. aktprocdef.setmangledname(target_info.cprefix+name);
  584. aktprocdef.forwarddef:=false;
  585. make_ref:=true;
  586. { The localst is a local symtable. Change it into the static
  587. symtable }
  588. aktprocdef.localst.free;
  589. aktprocdef.localst:=st;
  590. { and insert the procsym in symtable }
  591. st.insert(aktprocsym);
  592. { set some informations about the main program }
  593. with procinfo^ do
  594. begin
  595. _class:=nil;
  596. para_offset:=8;
  597. framepointer:=frame_pointer;
  598. flags:=0;
  599. procdef:=aktprocdef;
  600. end;
  601. end;
  602. procedure insertLocalThreadvarsTablesTable;
  603. var
  604. hp : tused_unit;
  605. ltvTables : taasmoutput;
  606. count : longint;
  607. begin
  608. ltvTables:=TAAsmOutput.Create;
  609. count:=0;
  610. hp:=tused_unit(usedunits.first);
  611. while assigned(hp) do
  612. begin
  613. If (hp.u.flags and uf_local_threadvars)=uf_local_threadvars then
  614. begin
  615. ltvTables.concat(Tai_const_symbol.Createname(hp.u.modulename^+'_$LOCALTHREADVARLIST'));
  616. inc(count);
  617. end;
  618. hp:=tused_unit(hp.next);
  619. end;
  620. { TableCount }
  621. ltvTables.insert(Tai_const.Create_32bit(count));
  622. ltvTables.insert(Tai_symbol.Createdataname_global('FPC_LOCALTHREADVARTABLES',0));
  623. ltvTables.concat(Tai_symbol_end.Createname('FPC_LOCALTHREADVARTABLES'));
  624. { insert in data segment }
  625. if (cs_create_smart in aktmoduleswitches) then
  626. dataSegment.concat(Tai_cut.Create);
  627. dataSegment.concatlist(ltvTables);
  628. ltvTables.free;
  629. if count > 0 then
  630. have_local_threadvars := true;
  631. end;
  632. var ltvTable : taasmoutput;
  633. procedure addToLocalThreadvarTab(p:tnamedindexitem);
  634. var
  635. asym : tasmsymbol;
  636. begin
  637. with tvarsym(p) do
  638. begin
  639. if (typ=varsym) and (vo_is_thread_var IN varoptions) then
  640. begin
  641. if ltvTable = nil then
  642. begin { first threadvar }
  643. ltvTable := TAAsmOutput.Create;
  644. ltvTable.insert(tai_symbol.createdataname_global(current_module.modulename^+'_$LOCALTHREADVARLIST',0));
  645. end;
  646. asym := getasmsymbol(mangledname);
  647. if asym <> nil then
  648. begin
  649. ltvTable.concat(tai_const_symbol.create(asym)); { address of threadvar }
  650. ltvTable.concat(tai_const.create_32bit(getsize)); { size of threadvar }
  651. end;
  652. end;
  653. end;
  654. end;
  655. procedure proc_unit;
  656. function is_assembler_generated:boolean;
  657. begin
  658. is_assembler_generated:=(Errorcount=0) and
  659. not(
  660. codeSegment.empty and
  661. dataSegment.empty and
  662. bssSegment.empty and
  663. ((importssection=nil) or importsSection.empty) and
  664. ((resourcesection=nil) or resourceSection.empty) and
  665. ((resourcestringlist=nil) or resourcestringList.empty)
  666. );
  667. end;
  668. var
  669. main_file: tinputfile;
  670. st : tsymtable;
  671. unitst : tglobalsymtable;
  672. {$ifdef GDB}
  673. pu : tused_unit;
  674. {$endif GDB}
  675. store_crc,store_interface_crc : cardinal;
  676. s2 : ^string; {Saves stack space}
  677. force_init_final : boolean;
  678. begin
  679. consume(_UNIT);
  680. if compile_level=1 then
  681. Status.IsExe:=false;
  682. if token=_ID then
  683. begin
  684. { create filenames and unit name }
  685. main_file := current_scanner.inputfile;
  686. while assigned(main_file.next) do
  687. main_file := main_file.next;
  688. current_module.SetFileName(main_file.path^+main_file.name^,true);
  689. stringdispose(current_module.modulename);
  690. stringdispose(current_module.realmodulename);
  691. current_module.modulename:=stringdup(pattern);
  692. current_module.realmodulename:=stringdup(orgpattern);
  693. { check for system unit }
  694. new(s2);
  695. s2^:=upper(SplitName(main_file.name^));
  696. if (cs_check_unit_name in aktglobalswitches) and
  697. not((current_module.modulename^=s2^) or
  698. ((length(current_module.modulename^)>8) and
  699. (copy(current_module.modulename^,1,8)=s2^))) then
  700. Message1(unit_e_illegal_unit_name,current_module.realmodulename^);
  701. if (current_module.modulename^='SYSTEM') then
  702. include(aktmoduleswitches,cs_compilesystem);
  703. dispose(s2);
  704. end;
  705. consume(_ID);
  706. consume(_SEMICOLON);
  707. consume(_INTERFACE);
  708. { global switches are read, so further changes aren't allowed }
  709. current_module.in_global:=false;
  710. { handle the global switches }
  711. setupglobalswitches;
  712. Message1(unit_u_start_parse_interface,current_module.realmodulename^);
  713. { update status }
  714. status.currentmodule:=current_module.realmodulename^;
  715. { maybe turn off m_objpas if we are compiling objpas }
  716. if (current_module.modulename^='OBJPAS') then
  717. exclude(aktmodeswitches,m_objpas);
  718. { this should be placed after uses !!}
  719. {$ifndef UseNiceNames}
  720. procprefix:='_'+current_module.modulename^+'$$';
  721. {$else UseNiceNames}
  722. procprefix:='_'+tostr(length(current_module.modulename^))+lowercase(current_module.modulename^)+'_';
  723. {$endif UseNiceNames}
  724. parse_only:=true;
  725. { generate now the global symboltable }
  726. st:=tglobalsymtable.create(current_module.modulename^);
  727. refsymtable:=st;
  728. unitst:=tglobalsymtable(st);
  729. { define first as local to overcome dependency conflicts }
  730. current_module.localsymtable:=st;
  731. { the unit name must be usable as a unit specifier }
  732. { inside the unit itself (PM) }
  733. { this also forbids to have another symbol }
  734. { with the same name as the unit }
  735. refsymtable.insert(tunitsym.create(current_module.realmodulename^,unitst));
  736. { load default units, like the system unit }
  737. loaddefaultunits;
  738. { reset }
  739. make_ref:=true;
  740. lexlevel:=0;
  741. { insert qualifier for the system unit (allows system.writeln) }
  742. if not(cs_compilesystem in aktmoduleswitches) then
  743. begin
  744. if token=_USES then
  745. begin
  746. loadunits;
  747. { has it been compiled at a higher level ?}
  748. if current_module.compiled then
  749. begin
  750. { this unit symtable is obsolete }
  751. { dispose(unitst,done);
  752. disposed as localsymtable !! }
  753. RestoreUnitSyms;
  754. exit;
  755. end;
  756. end;
  757. { ... but insert the symbol table later }
  758. st.next:=symtablestack;
  759. symtablestack:=st;
  760. end
  761. else
  762. { while compiling a system unit, some types are directly inserted }
  763. begin
  764. st.next:=symtablestack;
  765. symtablestack:=st;
  766. insert_intern_types(st);
  767. end;
  768. { now we know the place to insert the constants }
  769. constsymtable:=symtablestack;
  770. { move the global symtab from the temporary local to global }
  771. current_module.globalsymtable:=current_module.localsymtable;
  772. current_module.localsymtable:=nil;
  773. reset_global_defs;
  774. { number all units, so we know if a unit is used by this unit or
  775. needs to be added implicitly }
  776. current_module.numberunits;
  777. { ... parse the declarations }
  778. Message1(parser_u_parsing_interface,current_module.realmodulename^);
  779. read_interface_declarations;
  780. { leave when we got an error }
  781. if (Errorcount>0) and not status.skip_error then
  782. begin
  783. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  784. status.skip_error:=true;
  785. exit;
  786. end;
  787. {else in inteface its somatimes necessary even if unused
  788. st^.allunitsused; }
  789. {$ifdef New_GDB}
  790. write_gdb_info;
  791. {$endIf Def New_GDB}
  792. if not(cs_compilesystem in aktmoduleswitches) then
  793. if (Errorcount=0) then
  794. tppumodule(current_module).getppucrc;
  795. { Parse the implementation section }
  796. consume(_IMPLEMENTATION);
  797. current_module.in_implementation:=true;
  798. Message1(unit_u_start_parse_implementation,current_module.modulename^);
  799. parse_only:=false;
  800. { generates static symbol table }
  801. st:=tstaticsymtable.create(current_module.modulename^);
  802. current_module.localsymtable:=st;
  803. { remove the globalsymtable from the symtable stack }
  804. { to reinsert it after loading the implementation units }
  805. symtablestack:=unitst.next;
  806. { we don't want implementation units symbols in unitsymtable !! PM }
  807. refsymtable:=st;
  808. { Read the implementation units }
  809. parse_implementation_uses(unitst);
  810. if current_module.compiled then
  811. begin
  812. RestoreUnitSyms;
  813. exit;
  814. end;
  815. { reset ranges/stabs in exported definitions }
  816. reset_global_defs;
  817. { All units are read, now give them a number }
  818. current_module.numberunits;
  819. { now we can change refsymtable }
  820. refsymtable:=st;
  821. { but reinsert the global symtable as lasts }
  822. unitst.next:=symtablestack;
  823. symtablestack:=unitst;
  824. tstoredsymtable(symtablestack).chainoperators;
  825. {$ifdef DEBUG}
  826. test_symtablestack;
  827. {$endif DEBUG}
  828. constsymtable:=symtablestack;
  829. {$ifdef Splitheap}
  830. if testsplit then
  831. begin
  832. Split_Heap;
  833. allow_special:=true;
  834. Switch_to_temp_heap;
  835. end;
  836. { it will report all crossings }
  837. allow_special:=false;
  838. {$endif Splitheap}
  839. Message1(parser_u_parsing_implementation,current_module.realmodulename^);
  840. { Compile the unit }
  841. codegen_newprocedure;
  842. gen_main_procsym(current_module.modulename^+'_init',potype_unitinit,st);
  843. aktprocdef.aliasnames.insert('INIT$$'+current_module.modulename^);
  844. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_init');
  845. compile_proc_body(true,false);
  846. codegen_doneprocedure;
  847. { avoid self recursive destructor call !! PM }
  848. aktprocdef.localst:=nil;
  849. { if the unit contains ansi/widestrings, initialization and
  850. finalization code must be forced }
  851. force_init_final:=tglobalsymtable(current_module.globalsymtable).needs_init_final or
  852. tstaticsymtable(current_module.localsymtable).needs_init_final;
  853. { should we force unit initialization? }
  854. { this is a hack, but how can it be done better ? }
  855. if force_init_final and ((current_module.flags and uf_init)=0) then
  856. begin
  857. current_module.flags:=current_module.flags or uf_init;
  858. { now we can insert a cut }
  859. if (cs_create_smart in aktmoduleswitches) then
  860. codeSegment.concat(Tai_cut.Create);
  861. genimplicitunitinit(codesegment);
  862. end;
  863. { finalize? }
  864. if token=_FINALIZATION then
  865. begin
  866. { set module options }
  867. current_module.flags:=current_module.flags or uf_finalize;
  868. { Compile the finalize }
  869. codegen_newprocedure;
  870. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  871. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  872. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  873. compile_proc_body(true,false);
  874. codegen_doneprocedure;
  875. end
  876. else if force_init_final then
  877. begin
  878. current_module.flags:=current_module.flags or uf_finalize;
  879. { now we can insert a cut }
  880. if (cs_create_smart in aktmoduleswitches) then
  881. codeSegment.concat(Tai_cut.Create);
  882. genimplicitunitfinal(codesegment);
  883. end;
  884. { generate a list of local threadvars }
  885. ltvTable := nil;
  886. st.foreach_static (@addToLocalThreadvarTab);
  887. if ltvTable <> nil then
  888. begin
  889. ltvTable.concat(tai_const.create_32bit(0)); { end of list marker }
  890. ltvTable.concat(tai_symbol_end.createname(current_module.modulename^+'_$LOCALTHREADVARLIST'));
  891. if (cs_create_smart in aktmoduleswitches) then
  892. dataSegment.concat(Tai_cut.Create);
  893. dataSegment.concatlist(ltvTable);
  894. ltvTable.Free;
  895. current_module.flags:=current_module.flags or uf_local_threadvars;
  896. end;
  897. { the last char should always be a point }
  898. consume(_POINT);
  899. If ResourceStrings.ResStrCount>0 then
  900. begin
  901. ResourceStrings.CreateResourceStringList;
  902. current_module.flags:=current_module.flags or uf_has_resources;
  903. { only write if no errors found }
  904. if (Errorcount=0) then
  905. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  906. end;
  907. { avoid self recursive destructor call !! PM }
  908. aktprocdef.localst:=nil;
  909. { absence does not matter here !! }
  910. aktprocdef.forwarddef:=false;
  911. { test static symtable }
  912. if (Errorcount=0) then
  913. begin
  914. tstoredsymtable(st).allsymbolsused;
  915. tstoredsymtable(st).allunitsused;
  916. tstoredsymtable(st).allprivatesused;
  917. end;
  918. { size of the static data }
  919. datasize:=st.datasize;
  920. {$ifdef GDB}
  921. { add all used definitions even for implementation}
  922. if (cs_debuginfo in aktmoduleswitches) then
  923. begin
  924. {$IfnDef New_GDB}
  925. if assigned(current_module.globalsymtable) then
  926. begin
  927. { all types }
  928. tglobalsymtable(current_module.globalsymtable).concattypestabto(debuglist);
  929. { and all local symbols}
  930. tglobalsymtable(current_module.globalsymtable).concatstabto(debuglist);
  931. end;
  932. { all local types }
  933. tglobalsymtable(st)^.concattypestabto(debuglist);
  934. { and all local symbols}
  935. st^.concatstabto(debuglist);
  936. {$else New_GDB}
  937. write_gdb_info;
  938. {$endIf Def New_GDB}
  939. end;
  940. {$endif GDB}
  941. reset_global_defs;
  942. if (Errorcount=0) then
  943. begin
  944. { tests, if all (interface) forwards are resolved }
  945. tstoredsymtable(symtablestack).check_forwards;
  946. { check if all private fields are used }
  947. tstoredsymtable(symtablestack).allprivatesused;
  948. { remove cross unit overloads }
  949. tstoredsymtable(symtablestack).unchain_overloaded;
  950. end;
  951. current_module.in_implementation:=false;
  952. {$ifdef GDB}
  953. tglobalsymtable(symtablestack).is_stab_written:=false;
  954. {$endif GDB}
  955. { leave when we got an error }
  956. if (Errorcount>0) and not status.skip_error then
  957. begin
  958. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  959. status.skip_error:=true;
  960. exit;
  961. end;
  962. { generate imports }
  963. if current_module.uses_imports then
  964. importlib.generatelib;
  965. { insert own objectfile, or say that it's in a library
  966. (no check for an .o when loading) }
  967. if is_assembler_generated then
  968. insertobjectfile
  969. else
  970. current_module.flags:=current_module.flags or uf_no_link;
  971. if cs_local_browser in aktmoduleswitches then
  972. current_module.localsymtable:=refsymtable;
  973. {$ifdef GDB}
  974. pu:=tused_unit(usedunits.first);
  975. while assigned(pu) do
  976. begin
  977. if assigned(pu.u.globalsymtable) then
  978. tglobalsymtable(pu.u.globalsymtable).is_stab_written:=false;
  979. pu:=tused_unit(pu.next);
  980. end;
  981. {$endif GDB}
  982. if is_assembler_generated then
  983. begin
  984. { finish asmlist by adding segment starts }
  985. insertsegment;
  986. { assemble }
  987. create_objectfile;
  988. end;
  989. { Write out the ppufile after the object file has been created }
  990. store_interface_crc:=current_module.interface_crc;
  991. store_crc:=current_module.crc;
  992. if (Errorcount=0) then
  993. tppumodule(current_module).writeppu;
  994. if not(cs_compilesystem in aktmoduleswitches) then
  995. if store_interface_crc<>current_module.interface_crc then
  996. Comment(V_Warning,current_module.ppufilename^+' Interface CRC changed '+
  997. hexstr(store_crc,8)+'<>'+hexstr(current_module.interface_crc,8));
  998. {$ifdef EXTDEBUG}
  999. if not(cs_compilesystem in aktmoduleswitches) then
  1000. if (store_crc<>current_module.crc) and simplify_ppu then
  1001. Comment(V_Warning,current_module.ppufilename^+' implementation CRC changed '+
  1002. hexstr(store_crc,8)+'<>'+hexstr(current_module.crc,8));
  1003. {$endif EXTDEBUG}
  1004. { remove static symtable (=refsymtable) here to save some mem }
  1005. if not (cs_local_browser in aktmoduleswitches) then
  1006. begin
  1007. st.free;
  1008. current_module.localsymtable:=nil;
  1009. end;
  1010. RestoreUnitSyms;
  1011. { leave when we got an error }
  1012. if (Errorcount>0) and not status.skip_error then
  1013. begin
  1014. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1015. status.skip_error:=true;
  1016. exit;
  1017. end;
  1018. end;
  1019. procedure proc_program(islibrary : boolean);
  1020. var
  1021. main_file: tinputfile;
  1022. st : tsymtable;
  1023. hp : tmodule;
  1024. begin
  1025. DLLsource:=islibrary;
  1026. Status.IsLibrary:=IsLibrary;
  1027. Status.IsExe:=true;
  1028. parse_only:=false;
  1029. { relocation works only without stabs under win32 !! PM }
  1030. { internal assembler uses rva for stabs info
  1031. so it should work with relocated DLLs }
  1032. if RelocSection and
  1033. (target_info.target in [target_i386_win32,target_i386_wdosx]) and
  1034. (target_info.assem<>as_i386_pecoff) then
  1035. begin
  1036. include(aktglobalswitches,cs_link_strip);
  1037. { Warning stabs info does not work with reloc section !! }
  1038. if cs_debuginfo in aktmoduleswitches then
  1039. begin
  1040. Message1(parser_w_parser_reloc_no_debug,current_module.mainsource^);
  1041. Message(parser_w_parser_win32_debug_needs_WN);
  1042. exclude(aktmoduleswitches,cs_debuginfo);
  1043. end;
  1044. end;
  1045. { get correct output names }
  1046. main_file := current_scanner.inputfile;
  1047. while assigned(main_file.next) do
  1048. main_file := main_file.next;
  1049. current_module.SetFileName(main_file.path^+main_file.name^,true);
  1050. if islibrary then
  1051. begin
  1052. consume(_LIBRARY);
  1053. stringdispose(current_module.modulename);
  1054. current_module.modulename:=stringdup(pattern);
  1055. current_module.islibrary:=true;
  1056. exportlib.preparelib(pattern);
  1057. consume(_ID);
  1058. consume(_SEMICOLON);
  1059. end
  1060. else
  1061. { is there an program head ? }
  1062. if token=_PROGRAM then
  1063. begin
  1064. consume(_PROGRAM);
  1065. stringdispose(current_module.modulename);
  1066. stringdispose(current_module.realmodulename);
  1067. current_module.modulename:=stringdup(pattern);
  1068. current_module.realmodulename:=stringdup(orgpattern);
  1069. if (target_info.target in [target_i386_WIN32,target_i386_wdosx]) then
  1070. exportlib.preparelib(pattern);
  1071. consume(_ID);
  1072. if token=_LKLAMMER then
  1073. begin
  1074. consume(_LKLAMMER);
  1075. consume_idlist;
  1076. consume(_RKLAMMER);
  1077. end;
  1078. consume(_SEMICOLON);
  1079. end
  1080. else if (target_info.target in [target_i386_WIN32,target_i386_wdosx]) then
  1081. exportlib.preparelib(current_module.modulename^);
  1082. { global switches are read, so further changes aren't allowed }
  1083. current_module.in_global:=false;
  1084. { setup things using the global switches }
  1085. setupglobalswitches;
  1086. { set implementation flag }
  1087. current_module.in_implementation:=true;
  1088. { insert after the unit symbol tables the static symbol table }
  1089. { of the program }
  1090. st:=tstaticsymtable.create(current_module.modulename^);;
  1091. current_module.localsymtable:=st;
  1092. refsymtable:=st;
  1093. { load standard units (system,objpas,profile unit) }
  1094. loaddefaultunits;
  1095. { reset }
  1096. lexlevel:=0;
  1097. {Load the units used by the program we compile.}
  1098. if token=_USES then
  1099. loadunits;
  1100. tstoredsymtable(symtablestack).chainoperators;
  1101. { reset ranges/stabs in exported definitions }
  1102. reset_global_defs;
  1103. { All units are read, now give them a number }
  1104. current_module.numberunits;
  1105. {Insert the name of the main program into the symbol table.}
  1106. if current_module.realmodulename^<>'' then
  1107. st.insert(tunitsym.create(current_module.realmodulename^,tglobalsymtable(st)));
  1108. { ...is also constsymtable, this is the symtable where }
  1109. { the elements of enumeration types are inserted }
  1110. constsymtable:=st;
  1111. Message1(parser_u_parsing_implementation,current_module.mainsource^);
  1112. { reset }
  1113. procprefix:='';
  1114. {The program intialization needs an alias, so it can be called
  1115. from the bootstrap code.}
  1116. codegen_newprocedure;
  1117. if islibrary then
  1118. begin
  1119. gen_main_procsym(current_module.modulename^+'_main',potype_proginit,st);
  1120. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_main');
  1121. aktprocdef.aliasnames.insert('PASCALMAIN');
  1122. { this code is called from C so we need to save some
  1123. registers }
  1124. include(aktprocdef.procoptions,po_savestdregs);
  1125. end
  1126. else
  1127. begin
  1128. gen_main_procsym('main',potype_proginit,st);
  1129. aktprocdef.aliasnames.insert('program_init');
  1130. aktprocdef.aliasnames.insert('PASCALMAIN');
  1131. aktprocdef.aliasnames.insert(target_info.cprefix+'main');
  1132. end;
  1133. insertLocalThreadvarsTablesTable;
  1134. compile_proc_body(true,false);
  1135. { Add symbol to the exports section for win32 so smartlinking a
  1136. DLL will include the edata section }
  1137. if assigned(exportlib) and
  1138. (target_info.target in [target_i386_win32,target_i386_wdosx]) and
  1139. assigned(current_module._exports.first) then
  1140. codesegment.concat(tai_const_symbol.create(exportlib.edatalabel));
  1141. { avoid self recursive destructor call !! PM }
  1142. aktprocdef.localst:=nil;
  1143. { consider these symbols as global ones for browser
  1144. but the typecasting of the globalsymtable with tglobalsymtable
  1145. can then lead to problems (PFV)
  1146. current_module.globalsymtable:=current_module.localsymtable;
  1147. current_module.localsymtable:=nil;}
  1148. If ResourceStrings.ResStrCount>0 then
  1149. begin
  1150. ResourceStrings.CreateResourceStringList;
  1151. { only write if no errors found }
  1152. if (Errorcount=0) then
  1153. ResourceStrings.WriteResourceFile(ForceExtension(current_module.ppufilename^,'.rst'));
  1154. end;
  1155. codegen_doneprocedure;
  1156. { finalize? }
  1157. if token=_FINALIZATION then
  1158. begin
  1159. { set module options }
  1160. current_module.flags:=current_module.flags or uf_finalize;
  1161. { Compile the finalize }
  1162. codegen_newprocedure;
  1163. gen_main_procsym(current_module.modulename^+'_finalize',potype_unitfinalize,st);
  1164. aktprocdef.aliasnames.insert('FINALIZE$$'+current_module.modulename^);
  1165. aktprocdef.aliasnames.insert(target_info.cprefix+current_module.modulename^+'_finalize');
  1166. compile_proc_body(true,false);
  1167. codegen_doneprocedure;
  1168. end;
  1169. { consume the last point }
  1170. consume(_POINT);
  1171. {$ifdef New_GDB}
  1172. write_gdb_info;
  1173. {$endIf Def New_GDB}
  1174. { leave when we got an error }
  1175. if (Errorcount>0) and not status.skip_error then
  1176. begin
  1177. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1178. status.skip_error:=true;
  1179. exit;
  1180. end;
  1181. { test static symtable }
  1182. if (Errorcount=0) then
  1183. begin
  1184. tstoredsymtable(st).allsymbolsused;
  1185. tstoredsymtable(st).allunitsused;
  1186. tstoredsymtable(st).allprivatesused;
  1187. end;
  1188. { generate imports }
  1189. if current_module.uses_imports then
  1190. importlib.generatelib;
  1191. if islibrary or
  1192. (target_info.target in [target_i386_WIN32,target_i386_wdosx]) or
  1193. (target_info.target=target_i386_NETWARE) then
  1194. exportlib.generatelib;
  1195. { insert heap }
  1196. insertResourceTablesTable;
  1197. insertinitfinaltable;
  1198. insertheap;
  1199. insertstacklength;
  1200. datasize:=symtablestack.datasize;
  1201. { finish asmlist by adding segment starts }
  1202. insertsegment;
  1203. { insert own objectfile }
  1204. insertobjectfile;
  1205. { assemble and link }
  1206. create_objectfile;
  1207. { leave when we got an error }
  1208. if (Errorcount>0) and not status.skip_error then
  1209. begin
  1210. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1211. status.skip_error:=true;
  1212. exit;
  1213. end;
  1214. { create the executable when we are at level 1 }
  1215. if (compile_level=1) then
  1216. begin
  1217. { insert all .o files from all loaded units }
  1218. hp:=tmodule(loaded_units.first);
  1219. while assigned(hp) do
  1220. begin
  1221. linker.AddModuleFiles(hp);
  1222. hp:=tmodule(hp.next);
  1223. end;
  1224. { write .def file }
  1225. if (cs_link_deffile in aktglobalswitches) then
  1226. deffile.writefile;
  1227. { finally we can create a executable }
  1228. if (not current_module.is_unit) then
  1229. begin
  1230. if DLLSource then
  1231. linker.MakeSharedLibrary
  1232. else
  1233. linker.MakeExecutable;
  1234. end;
  1235. end;
  1236. end;
  1237. end.
  1238. {
  1239. $Log$
  1240. Revision 1.59 2002-04-07 17:58:38 carl
  1241. + generic stack checking
  1242. Revision 1.58 2002/04/04 19:06:03 peter
  1243. * removed unused units
  1244. * use tlocation.size in cg.a_*loc*() routines
  1245. Revision 1.57 2002/04/04 18:42:49 carl
  1246. + added wdosx support (patch from Pavel)
  1247. Revision 1.56 2002/04/02 17:11:29 peter
  1248. * tlocation,treference update
  1249. * LOC_CONSTANT added for better constant handling
  1250. * secondadd splitted in multiple routines
  1251. * location_force_reg added for loading a location to a register
  1252. of a specified size
  1253. * secondassignment parses now first the right and then the left node
  1254. (this is compatible with Kylix). This saves a lot of push/pop especially
  1255. with string operations
  1256. * adapted some routines to use the new cg methods
  1257. Revision 1.55 2002/04/01 13:43:32 armin
  1258. addToLocalThreadvarList used '_'+name instead of mangledname to find asm symbol
  1259. Revision 1.54 2002/03/29 17:19:50 armin
  1260. + allow exports for netware
  1261. Revision 1.53 2002/03/29 09:00:56 armin
  1262. + forgot to delete a debug writeln
  1263. Revision 1.52 2002/03/28 16:07:52 armin
  1264. + initialize threadvars defined local in units
  1265. Revision 1.51 2002/01/24 18:25:49 peter
  1266. * implicit result variable generation for assembler routines
  1267. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1268. Revision 1.50 2001/12/09 03:34:58 carl
  1269. + Stack checking for solaris
  1270. Revision 1.49 2001/11/02 23:16:51 peter
  1271. * removed obsolete chainprocsym and test_procsym code
  1272. Revision 1.48 2001/11/02 22:58:05 peter
  1273. * procsym definition rewrite
  1274. Revision 1.47 2001/09/18 11:30:48 michael
  1275. * Fixes win32 linking problems with import libraries
  1276. * LINKLIB Libraries are now looked for using C file extensions
  1277. * get_exepath fix
  1278. Revision 1.46 2001/09/13 14:47:47 michael
  1279. + Committed patch from peter
  1280. Revision 1.45 2001/08/26 13:36:46 florian
  1281. * some cg reorganisation
  1282. * some PPC updates
  1283. Revision 1.44 2001/08/19 11:22:23 peter
  1284. * palmos support from v10 merged
  1285. Revision 1.43 2001/08/12 19:59:49 peter
  1286. * typo in extdebug msg
  1287. Revision 1.42 2001/08/06 21:40:47 peter
  1288. * funcret moved from tprocinfo to tprocdef
  1289. Revision 1.41 2001/08/05 12:26:52 peter
  1290. * beos fix (merged)
  1291. Revision 1.40 2001/08/04 10:23:54 peter
  1292. * updates so it works with the ide
  1293. Revision 1.38 2001/07/30 20:59:27 peter
  1294. * m68k updates from v10 merged
  1295. Revision 1.37 2001/06/18 20:36:25 peter
  1296. * -Ur switch (merged)
  1297. * masm fixes (merged)
  1298. * quoted filenames for go32v2 and win32
  1299. Revision 1.36 2001/06/06 21:58:16 peter
  1300. * Win32 fixes for Makefile so it doesn't require sh.exe
  1301. Revision 1.35 2001/06/03 21:57:36 peter
  1302. + hint directive parsing support
  1303. Revision 1.34 2001/06/03 15:15:31 peter
  1304. * dllprt0 stub for linux shared libs
  1305. * pass -init and -fini for linux shared libs
  1306. * libprefix splitted into staticlibprefix and sharedlibprefix
  1307. Revision 1.33 2001/05/19 23:05:19 peter
  1308. * support uses <unit> in <file> construction
  1309. Revision 1.32 2001/05/18 22:26:36 peter
  1310. * merged alignment for non-i386
  1311. Revision 1.31 2001/05/09 14:11:10 jonas
  1312. * range check error fixes from Peter
  1313. Revision 1.30 2001/05/06 14:49:17 peter
  1314. * ppu object to class rewrite
  1315. * move ppu read and write stuff to fppu
  1316. Revision 1.29 2001/04/18 22:01:57 peter
  1317. * registration of targets and assemblers
  1318. Revision 1.28 2001/04/13 18:08:37 peter
  1319. * scanner object to class
  1320. Revision 1.27 2001/04/13 01:22:12 peter
  1321. * symtable change to classes
  1322. * range check generation and errors fixed, make cycle DEBUG=1 works
  1323. * memory leaks fixed
  1324. Revision 1.26 2001/04/02 21:20:33 peter
  1325. * resulttype rewrite
  1326. Revision 1.25 2001/03/13 18:45:07 peter
  1327. * fixed some memory leaks
  1328. Revision 1.24 2001/03/06 18:28:02 peter
  1329. * patch from Pavel with a new and much faster DLL Scanner for
  1330. automatic importing so $linklib works for DLLs. Thanks Pavel!
  1331. Revision 1.23 2001/02/24 10:44:56 peter
  1332. * generate .rst from ppufilename instead of modulename
  1333. Revision 1.22 2001/02/21 19:37:19 peter
  1334. * moved deref to be done after loading of implementation units. prederef
  1335. is still done directly after loading of symbols and definitions.
  1336. Revision 1.21 2001/01/14 22:13:52 peter
  1337. * fixed crash with program name as a important unit name
  1338. Revision 1.20 2000/12/25 00:07:27 peter
  1339. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1340. tlinkedlist objects)
  1341. Revision 1.19 2000/11/29 00:30:36 florian
  1342. * unused units removed from uses clause
  1343. * some changes for widestrings
  1344. Revision 1.18 2000/11/01 23:04:37 peter
  1345. * tprocdef.fullprocname added for better casesensitve writing of
  1346. procedures
  1347. Revision 1.17 2000/10/31 22:02:50 peter
  1348. * symtable splitted, no real code changes
  1349. Revision 1.16 2000/10/21 14:36:26 peter
  1350. * merged pierres fixes
  1351. Revision 1.15 2000/10/15 09:08:58 peter
  1352. * use System for the systemunit instead of target dependent
  1353. Revision 1.14 2000/10/15 07:47:51 peter
  1354. * unit names and procedure names are stored mixed case
  1355. Revision 1.13 2000/10/04 14:51:08 pierre
  1356. * IsExe restored
  1357. Revision 1.12 2000/09/30 16:07:40 peter
  1358. * filepos when unit not found (merged)
  1359. Revision 1.11 2000/09/24 21:33:47 peter
  1360. * message updates merges
  1361. Revision 1.10 2000/09/24 15:06:22 peter
  1362. * use defines.inc
  1363. Revision 1.9 2000/08/31 07:53:02 michael
  1364. + Applied patch from Peter
  1365. Revision 1.8 2000/08/29 19:00:01 peter
  1366. * _init and _finalize procsyms also need a $ prefix
  1367. Revision 1.7 2000/08/27 20:19:39 peter
  1368. * store strings with case in ppu, when an internal symbol is created
  1369. a '$' is prefixed so it's not automatic uppercased
  1370. Revision 1.6 2000/08/27 16:11:52 peter
  1371. * moved some util functions from globals,cobjects to cutils
  1372. * splitted files into finput,fmodule
  1373. Revision 1.5 2000/08/25 08:48:22 jonas
  1374. * fixed bug with include files at the very beginning of .pp/.pas files
  1375. (wrong name used for generating exe/checking unit name) (merged from
  1376. fixes branch)
  1377. Revision 1.4 2000/08/21 11:27:44 pierre
  1378. * fix the stabs problems
  1379. Revision 1.3 2000/07/13 12:08:26 michael
  1380. + patched to 1.1.0 with former 1.09patch from peter
  1381. Revision 1.2 2000/07/13 11:32:45 michael
  1382. + removed logs
  1383. }