pmodules.pas 55 KB

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