pmodules.pas 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845
  1. {
  2. $Id$
  3. Copyright (c) 1998 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. {$define New_GDB}
  20. interface
  21. procedure proc_unit;
  22. procedure proc_program(islibrary : boolean);
  23. implementation
  24. uses
  25. globtype,version,systems,tokens,
  26. cobjects,comphook,globals,verbose,files,
  27. symtable,aasm,hcodegen,
  28. link,assemble,import,export,gendef,ppu,comprsrc
  29. {$ifdef i386}
  30. {$ifndef OLDASM}
  31. ,i386base,i386asm
  32. {$else} ,i386
  33. {$endif}
  34. {$endif}
  35. {$ifdef m68k}
  36. ,m68k
  37. {$endif}
  38. ,scanner,pbase,psystem,pdecl,psub,parser;
  39. procedure create_objectfile;
  40. begin
  41. { create the .s file and assemble it }
  42. GenerateAsm;
  43. { resource files }
  44. CompileResourceFiles;
  45. { When creating a library call the linker. And insert the output
  46. of the linker files }
  47. if (cs_create_sharedlib in aktmoduleswitches) then
  48. Linker.MakeSharedLibrary
  49. else
  50. if (cs_create_staticlib in aktmoduleswitches) or
  51. ((cs_smartlink in aktmoduleswitches) and target_asm.needar) then
  52. Linker.MakeStaticLibrary(SmartLinkFilesCnt);
  53. end;
  54. procedure insertobjectfile;
  55. { Insert the used object file for this unit in the used list for this unit }
  56. begin
  57. if (cs_create_sharedlib in aktmoduleswitches) then
  58. begin
  59. current_module^.linksharedlibs.insert(current_module^.sharedlibfilename^);
  60. current_module^.flags:=current_module^.flags or uf_shared_linked;
  61. end
  62. else
  63. begin
  64. if (cs_create_staticlib in aktmoduleswitches) or
  65. (cs_smartlink in aktmoduleswitches) then
  66. begin
  67. current_module^.linkstaticlibs.insert(current_module^.staticlibfilename^);
  68. current_module^.flags:=current_module^.flags or uf_static_linked;
  69. end
  70. else
  71. begin
  72. current_module^.linkunitfiles.insert(current_module^.objfilename^);
  73. current_module^.flags:=current_module^.flags or uf_obj_linked;
  74. end;
  75. end;
  76. end;
  77. procedure insertsegment;
  78. procedure fixseg(p:paasmoutput;sec:tsection);
  79. begin
  80. p^.insert(new(pai_section,init(sec)));
  81. if (cs_smartlink in aktmoduleswitches) then
  82. p^.insert(new(pai_cut,init));
  83. p^.concat(new(pai_section,init(sec_none)));
  84. end;
  85. begin
  86. {Insert Ident of the compiler}
  87. if (not (cs_smartlink in aktmoduleswitches))
  88. {$ifndef EXTDEBUG}
  89. and (not current_module^.is_unit)
  90. {$endif}
  91. then
  92. begin
  93. datasegment^.insert(new(pai_align,init(4)));
  94. datasegment^.insert(new(pai_string,init('FPC '+version_string+
  95. ' for '+target_cpu_string+' - '+target_info.short_name)));
  96. end;
  97. { Insert start and end of sections }
  98. fixseg(codesegment,sec_code);
  99. fixseg(datasegment,sec_data);
  100. fixseg(bsssegment,sec_bss);
  101. { we should use .rdata section for these two no ? }
  102. { .rdata is a read only data section (PM) }
  103. fixseg(rttilist,sec_data);
  104. fixseg(consts,sec_data);
  105. {$ifdef GDB}
  106. if assigned(debuglist) then
  107. fixseg(debuglist,sec_code);
  108. {$endif GDB}
  109. end;
  110. procedure InsertInitFinalTable;
  111. var
  112. hp : pused_unit;
  113. unitinits : taasmoutput;
  114. count : longint;
  115. begin
  116. unitinits.init;
  117. count:=0;
  118. hp:=pused_unit(usedunits.first);
  119. while assigned(hp) do
  120. begin
  121. { call the unit init code and make it external }
  122. if (hp^.u^.flags and (uf_init or uf_finalize))<>0 then
  123. begin
  124. if (hp^.u^.flags and uf_init)<>0 then
  125. begin
  126. unitinits.concat(new(pai_const_symbol,initname('INIT$$'+hp^.u^.modulename^)));
  127. {$ifndef NEWLAB}
  128. concat_external('INIT$$'+hp^.u^.modulename^,EXT_NEAR);
  129. {$endif}
  130. end
  131. else
  132. unitinits.concat(new(pai_const,init_32bit(0)));
  133. if (hp^.u^.flags and uf_finalize)<>0 then
  134. begin
  135. unitinits.concat(new(pai_const_symbol,initname('FINALIZE$$'+hp^.u^.modulename^)));
  136. {$ifndef NEWLAB}
  137. concat_external('FINALIZE$$'+hp^.u^.modulename^,EXT_NEAR);
  138. {$endif}
  139. end
  140. else
  141. unitinits.concat(new(pai_const,init_32bit(0)));
  142. inc(count);
  143. end;
  144. hp:=Pused_unit(hp^.next);
  145. end;
  146. { TableCount,InitCount }
  147. unitinits.insert(new(pai_const,init_32bit(0)));
  148. unitinits.insert(new(pai_const,init_32bit(count)));
  149. unitinits.insert(new(pai_symbol,initname_global('INITFINAL')));
  150. { insert in data segment }
  151. if (cs_smartlink in aktmoduleswitches) then
  152. datasegment^.concat(new(pai_cut,init));
  153. datasegment^.concatlist(@unitinits);
  154. unitinits.done;
  155. end;
  156. procedure insertheap;
  157. begin
  158. if (cs_smartlink in aktmoduleswitches) then
  159. begin
  160. bsssegment^.concat(new(pai_cut,init));
  161. datasegment^.concat(new(pai_cut,init));
  162. end;
  163. { On the Macintosh Classic M68k Architecture
  164. The Heap variable is simply a POINTER to the
  165. real HEAP. The HEAP must be set up by the RTL
  166. and must store the pointer in this value.
  167. On OS/2 the heap is also intialized by the RTL. We do
  168. not output a pointer }
  169. case target_info.target of
  170. {$ifdef i386}
  171. target_i386_OS2:
  172. ;
  173. {$endif i386}
  174. {$ifdef m68k}
  175. target_m68k_Mac:
  176. bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  177. target_m68k_PalmOS:
  178. ;
  179. {$endif m68k}
  180. else
  181. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  182. end;
  183. {$ifdef i386}
  184. datasegment^.concat(new(pai_symbol,initname_global('HEAPSIZE')));
  185. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  186. {$endif i386}
  187. {$ifdef m68k}
  188. if target_info.target<>target_m68k_PalmOS then
  189. begin
  190. datasegment^.concat(new(pai_symbol,init_global('HEAP_SIZE')));
  191. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  192. end;
  193. {$endif m68k}
  194. end;
  195. procedure inserttargetspecific;
  196. begin
  197. case target_info.target of
  198. {$ifdef i386}
  199. target_i386_GO32V2 :
  200. begin
  201. { stacksize can be specified }
  202. datasegment^.concat(new(pai_symbol,initname_global('__stklen')));
  203. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  204. end;
  205. target_i386_WIN32 :
  206. begin
  207. { Generate an external entry to be sure that _mainCRTStarup will be
  208. linked, can't use concat_external because those aren't written for
  209. asw (PFV) }
  210. target_link.bindcmd[1]:=target_link.bindcmd[1]+' -d '+deffile.fname;
  211. if DLLsource then
  212. target_link.binders:=2;
  213. if RelocSection then
  214. begin
  215. target_link.linkcmd:=target_link.linkcmd+' --base-file base.$$$';
  216. target_link.bindcmd[1]:=target_link.bindcmd[1]+' --base-file base.$$$';
  217. target_link.binders:=2;
  218. end;
  219. if apptype=at_gui then
  220. begin
  221. target_link.linkcmd:='--subsystem windows '+target_link.linkcmd;
  222. target_link.bindcmd[2]:='--subsystem windows '+target_link.bindcmd[2];
  223. end;
  224. if DLLsource then
  225. begin
  226. target_link.linkcmd:='--dll '+target_link.linkcmd;
  227. target_link.bindcmd[2]:='--dll '+target_link.bindcmd[2];
  228. end;
  229. end;
  230. {$endif i386}
  231. {$ifdef m68k}
  232. target_m68k_Atari :
  233. begin
  234. { stacksize can be specified }
  235. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  236. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  237. end;
  238. {$endif m68k}
  239. end;
  240. end;
  241. function loadunit(const s : string;compile_system:boolean) : pmodule;forward;
  242. procedure load_usedunits(compile_system:boolean);
  243. var
  244. pu : pused_unit;
  245. loaded_unit : pmodule;
  246. load_refs : boolean;
  247. nextmapentry : longint;
  248. begin
  249. load_refs:=true;
  250. { init the map }
  251. new(current_module^.map);
  252. fillchar(current_module^.map^,sizeof(tunitmap),#0);
  253. {$ifdef NEWMAP}
  254. current_module^.map^[0]:=current_module;
  255. {$endif NEWMAP}
  256. nextmapentry:=1;
  257. { load the used units from interface }
  258. current_module^.in_implementation:=false;
  259. pu:=pused_unit(current_module^.used_units.first);
  260. while assigned(pu) do
  261. begin
  262. if (not pu^.loaded) and (pu^.in_interface) then
  263. begin
  264. loaded_unit:=loadunit(pu^.name^,false);
  265. if current_module^.compiled then
  266. exit;
  267. { register unit in used units }
  268. pu^.u:=loaded_unit;
  269. pu^.loaded:=true;
  270. { doubles are not important for that list PM }
  271. pu^.u^.dependent_units.concat(new(pdependent_unit,init(current_module)));
  272. { need to recompile the current unit ? }
  273. if loaded_unit^.crc<>pu^.checksum then
  274. begin
  275. Message2(unit_u_recompile_crc_change,current_module^.modulename^,pu^.name^);
  276. current_module^.do_compile:=true;
  277. dispose(current_module^.map);
  278. current_module^.map:=nil;
  279. exit;
  280. end;
  281. { setup the map entry for deref }
  282. {$ifndef NEWMAP}
  283. current_module^.map^[nextmapentry]:=loaded_unit^.globalsymtable;
  284. {$else NEWMAP}
  285. current_module^.map^[nextmapentry]:=loaded_unit;
  286. {$endif NEWMAP}
  287. inc(nextmapentry);
  288. if nextmapentry>maxunits then
  289. Message(unit_f_too_much_units);
  290. end;
  291. pu:=pused_unit(pu^.next);
  292. end;
  293. { ok, now load the unit }
  294. current_module^.globalsymtable:=new(punitsymtable,loadasunit);
  295. { now only read the implementation part }
  296. current_module^.in_implementation:=true;
  297. { load the used units from implementation }
  298. pu:=pused_unit(current_module^.used_units.first);
  299. while assigned(pu) do
  300. begin
  301. if (not pu^.loaded) and (not pu^.in_interface) then
  302. begin
  303. loaded_unit:=loadunit(pu^.name^,false);
  304. if current_module^.compiled then
  305. exit;
  306. { register unit in used units }
  307. pu^.u:=loaded_unit;
  308. pu^.loaded:=true;
  309. { need to recompile the current unit ? }
  310. if loaded_unit^.crc<>pu^.checksum then
  311. begin
  312. Message2(unit_u_recompile_crc_change,current_module^.modulename^,pu^.name^);
  313. current_module^.do_compile:=true;
  314. dispose(current_module^.map);
  315. current_module^.map:=nil;
  316. exit;
  317. end;
  318. { setup the map entry for deref }
  319. {$ifndef NEWMAP}
  320. current_module^.map^[nextmapentry]:=loaded_unit^.globalsymtable;
  321. {$else NEWMAP}
  322. current_module^.map^[nextmapentry]:=loaded_unit;
  323. {$endif NEWMAP}
  324. inc(nextmapentry);
  325. if nextmapentry>maxunits then
  326. Message(unit_f_too_much_units);
  327. end;
  328. pu:=pused_unit(pu^.next);
  329. end;
  330. { load browser info if stored }
  331. if ((current_module^.flags and uf_has_browser)<>0) and load_refs then
  332. punitsymtable(current_module^.globalsymtable)^.load_symtable_refs;
  333. { remove the map, it's not needed anymore }
  334. dispose(current_module^.map);
  335. current_module^.map:=nil;
  336. end;
  337. function loadunit(const s : string;compile_system:boolean) : pmodule;
  338. const
  339. ImplIntf : array[boolean] of string[15]=('interface','implementation');
  340. var
  341. st : punitsymtable;
  342. second_time : boolean;
  343. old_current_ppu : pppufile;
  344. old_current_module,hp,hp2,hp3 : pmodule;
  345. name : string;{ necessary because
  346. current_module^.mainsource^ is reset in compile !! }
  347. scanner : pscannerfile;
  348. procedure loadppufile;
  349. begin
  350. { load interface section }
  351. if not current_module^.do_compile then
  352. load_interface;
  353. { only load units when we don't recompile }
  354. if not current_module^.do_compile then
  355. load_usedunits(compile_system);
  356. { recompile if set }
  357. if current_module^.do_compile then
  358. begin
  359. { we don't need the ppufile anymore }
  360. if assigned(current_module^.ppufile) then
  361. begin
  362. dispose(current_module^.ppufile,done);
  363. current_module^.ppufile:=nil;
  364. current_ppu:=nil;
  365. end;
  366. { recompile the unit or give a fatal error if sources not available }
  367. if not(current_module^.sources_avail) then
  368. if (not current_module^.search_unit(current_module^.modulename^,true))
  369. and (length(current_module^.modulename^)>8) then
  370. current_module^.search_unit(copy(current_module^.modulename^,1,8),true);
  371. if not(current_module^.sources_avail) then
  372. Message1(unit_f_cant_compile_unit,current_module^.modulename^)
  373. else
  374. begin
  375. if current_module^.in_second_compile then
  376. Message1(parser_d_compiling_second_time,current_module^.modulename^);
  377. current_scanner^.tempcloseinputfile;
  378. name:=current_module^.mainsource^;
  379. if assigned(scanner) then
  380. scanner^.invalid:=true;
  381. compile(name,compile_system);
  382. if (not current_scanner^.invalid) then
  383. current_scanner^.tempopeninputfile;
  384. end;
  385. end
  386. else
  387. begin
  388. { only reassemble ? }
  389. if (current_module^.do_assemble) then
  390. OnlyAsm;
  391. end;
  392. if assigned(current_module^.ppufile) then
  393. begin
  394. dispose(current_module^.ppufile,done);
  395. current_module^.ppufile:=nil;
  396. current_ppu:=nil;
  397. end;
  398. end;
  399. begin
  400. old_current_module:=current_module;
  401. old_current_ppu:=current_ppu;
  402. { Info }
  403. Message3(unit_u_load_unit,current_module^.modulename^,ImplIntf[current_module^.in_implementation],s);
  404. { unit not found }
  405. st:=nil;
  406. { search all loaded units }
  407. hp:=pmodule(loaded_units.first);
  408. while assigned(hp) do
  409. begin
  410. if hp^.modulename^=s then
  411. begin
  412. { forced to reload ? }
  413. if hp^.do_reload then
  414. break;
  415. { the unit is already registered }
  416. { and this means that the unit }
  417. { is already compiled }
  418. { else there is a cyclic unit use }
  419. if assigned(hp^.globalsymtable) then
  420. st:=punitsymtable(hp^.globalsymtable)
  421. else
  422. begin
  423. { both units in interface ? }
  424. if (not current_module^.in_implementation) and (not hp^.in_implementation) then
  425. begin
  426. { check for a cycle }
  427. hp2:=current_module^.loaded_from;
  428. while assigned(hp2) and (hp2<>hp) do
  429. begin
  430. if hp2^.in_implementation then
  431. hp2:=nil
  432. else
  433. hp2:=hp2^.loaded_from;
  434. end;
  435. if assigned(hp2) then
  436. Message2(unit_f_circular_unit_reference,current_module^.modulename^,hp^.modulename^);
  437. end;
  438. end;
  439. break;
  440. end;
  441. { the next unit }
  442. hp:=pmodule(hp^.next);
  443. end;
  444. { the unit is not in the symtable stack }
  445. if (not assigned(st)) then
  446. begin
  447. if assigned(hp) then
  448. begin
  449. { remove the old unit }
  450. loaded_units.remove(hp);
  451. scanner:=hp^.scanner;
  452. hp^.reset;
  453. { now reload all dependent units }
  454. hp2:=pmodule(loaded_units.first);
  455. while assigned(hp2) do
  456. begin
  457. if hp2^.do_reload then
  458. begin
  459. hp2^.do_reload:=false;
  460. hp3:=loadunit(hp^.modulename^,false);
  461. end;
  462. hp2:=pmodule(hp2^.next);
  463. end;
  464. hp^.scanner:=scanner;
  465. { try to reopen ppu }
  466. hp^.search_unit(s,false);
  467. { try to load the unit a second time first }
  468. current_module:=hp;
  469. current_module^.in_second_compile:=true;
  470. Message1(unit_u_second_load_unit,current_module^.modulename^);
  471. second_time:=true;
  472. end
  473. else
  474. { generates a new unit info record }
  475. begin
  476. current_module:=new(pmodule,init(s,true));
  477. scanner:=nil;
  478. second_time:=false;
  479. end;
  480. current_ppu:=current_module^.ppufile;
  481. { now we can register the unit }
  482. current_module^.loaded_from:=old_current_module;
  483. loaded_units.insert(current_module);
  484. { now realy load the ppu }
  485. loadppufile;
  486. { set compiled flag }
  487. current_module^.compiled:=true;
  488. { register the unit _once_ }
  489. { this is buggy PM }
  490. if not second_time then
  491. usedunits.concat(new(pused_unit,init(current_module,true)));
  492. { load return pointer }
  493. hp:=current_module;
  494. end;
  495. { set the old module }
  496. current_ppu:=old_current_ppu;
  497. current_module:=old_current_module;
  498. loadunit:=hp;
  499. end;
  500. procedure loaddefaultunits;
  501. var
  502. hp : pmodule;
  503. begin
  504. { are we compiling the system unit? }
  505. if (cs_compilesystem in aktmoduleswitches) then
  506. begin
  507. { create system defines }
  508. createconstdefs;
  509. { we don't need to reset anything, it's already done in parser.pas }
  510. exit;
  511. end;
  512. { insert the system unit, it is allways the first }
  513. hp:=loadunit(upper(target_info.system_unit),true);
  514. systemunit:=hp^.globalsymtable;
  515. { it's always the first unit }
  516. systemunit^.next:=nil;
  517. symtablestack:=systemunit;
  518. { add to the used units }
  519. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  520. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  521. { read default constant definitions }
  522. make_ref:=false;
  523. readconstdefs;
  524. make_ref:=true;
  525. { if POWER is defined in the RTL then use it for starstar overloading }
  526. getsym('POWER',false);
  527. if assigned(srsym) and (srsym^.typ=procsym) and (overloaded_operators[STARSTAR]=nil) then
  528. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  529. { Objpas unit? }
  530. if m_objpas in aktmodeswitches then
  531. begin
  532. hp:=loadunit('OBJPAS',false);
  533. objpasunit:=hp^.globalsymtable;
  534. { insert in stack }
  535. objpasunit^.next:=symtablestack;
  536. symtablestack:=objpasunit;
  537. { add to the used units }
  538. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  539. refsymtable^.insert(new(punitsym,init('OBJPAS',objpasunit)));
  540. end
  541. else
  542. objpasunit:=nil;
  543. { Profile unit? Needed for go32v2 only }
  544. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  545. begin
  546. hp:=loadunit('PROFILE',false);
  547. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  548. symtablestack:=hp^.globalsymtable;
  549. { add to the used units }
  550. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  551. refsymtable^.insert(new(punitsym,init('PROFILE',hp^.globalsymtable)));
  552. end;
  553. { Heaptrc unit? (not needed for units), this is here to be sure that it is really
  554. loaded as first unit }
  555. if (cs_gdb_heaptrc in aktglobalswitches) and not(current_module^.is_unit)then
  556. begin
  557. hp:=loadunit('HEAPTRC',false);
  558. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  559. symtablestack:=hp^.globalsymtable;
  560. { add to the used units }
  561. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  562. refsymtable^.insert(new(punitsym,init('HEAPTRC',hp^.globalsymtable)));
  563. end;
  564. { save default symtablestack }
  565. defaultsymtablestack:=symtablestack;
  566. end;
  567. procedure loadunits;
  568. var
  569. s : stringid;
  570. pu,
  571. hp : pused_unit;
  572. hp2 : pmodule;
  573. hp3 : psymtable;
  574. oldprocsym:Pprocsym;
  575. begin
  576. oldprocsym:=aktprocsym;
  577. consume(_USES);
  578. {$ifdef DEBUG}
  579. test_symtablestack;
  580. {$endif DEBUG}
  581. repeat
  582. s:=pattern;
  583. consume(ID);
  584. { Give a warning if objpas is loaded }
  585. if s='OBJPAS' then
  586. Message(parser_w_no_objpas_use_mode);
  587. { check if the unit is already used }
  588. pu:=pused_unit(current_module^.used_units.first);
  589. while assigned(pu) do
  590. begin
  591. if (pu^.name^=s) then
  592. break;
  593. pu:=pused_unit(pu^.next);
  594. end;
  595. { avoid uses of itself }
  596. if not assigned(pu) and (s<>current_module^.modulename^) then
  597. begin
  598. { load the unit }
  599. hp2:=loadunit(s,false);
  600. { the current module uses the unit hp2 }
  601. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  602. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  603. if current_module^.compiled then
  604. exit;
  605. refsymtable^.insert(new(punitsym,init(s,hp2^.globalsymtable)));
  606. end
  607. else
  608. Message1(sym_e_duplicate_id,s);
  609. if token=COMMA then
  610. begin
  611. pattern:='';
  612. consume(COMMA);
  613. end
  614. else
  615. break;
  616. until false;
  617. consume(SEMICOLON);
  618. { set the symtable to systemunit so it gets reorderd correctly }
  619. symtablestack:=defaultsymtablestack;
  620. { now insert the units in the symtablestack }
  621. hp:=pused_unit(current_module^.used_units.first);
  622. while assigned(hp) do
  623. begin
  624. {$IfDef GDB}
  625. {$IfnDef New_GDB}
  626. if (cs_debuginfo in aktmoduleswitches) and
  627. not hp^.is_stab_written then
  628. begin
  629. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  630. hp^.is_stab_written:=true;
  631. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  632. end;
  633. {$endIf nDef New_GDB}
  634. {$EndIf GDB}
  635. if hp^.in_uses then
  636. begin
  637. hp3:=symtablestack;
  638. while assigned(hp3) do
  639. begin
  640. { insert units only once ! }
  641. if hp^.u^.globalsymtable=hp3 then
  642. break;
  643. hp3:=hp3^.next;
  644. { unit isn't inserted }
  645. if hp3=nil then
  646. begin
  647. psymtable(hp^.u^.globalsymtable)^.next:=symtablestack;
  648. symtablestack:=psymtable(hp^.u^.globalsymtable);
  649. {$ifdef CHAINPROCSYMS}
  650. symtablestack^.chainprocsyms;
  651. {$endif CHAINPROCSYMS}
  652. {$ifdef DEBUG}
  653. test_symtablestack;
  654. {$endif DEBUG}
  655. end;
  656. end;
  657. end;
  658. hp:=pused_unit(hp^.next);
  659. end;
  660. aktprocsym:=oldprocsym;
  661. end;
  662. procedure write_gdb_info;
  663. {$IfDef GDB}
  664. var
  665. hp : pused_unit;
  666. begin
  667. if not (cs_debuginfo in aktmoduleswitches) then
  668. exit;
  669. { now insert the units in the symtablestack }
  670. hp:=pused_unit(current_module^.used_units.first);
  671. while assigned(hp) do
  672. begin
  673. if (cs_debuginfo in aktmoduleswitches) and
  674. not hp^.is_stab_written then
  675. begin
  676. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  677. hp^.is_stab_written:=true;
  678. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  679. end;
  680. hp:=pused_unit(hp^.next);
  681. end;
  682. if current_module^.in_implementation then
  683. begin
  684. if assigned(current_module^.localsymtable) then
  685. begin
  686. { all types }
  687. punitsymtable(current_module^.localsymtable)^.concattypestabto(debuglist);
  688. { and all local symbols}
  689. punitsymtable(current_module^.localsymtable)^.concatstabto(debuglist);
  690. end;
  691. end
  692. else
  693. begin
  694. if assigned(current_module^.globalsymtable) then
  695. begin
  696. { all types }
  697. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  698. { and all local symbols}
  699. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  700. end;
  701. end;
  702. end;
  703. {$Else GDB}
  704. begin
  705. end;
  706. {$EndIf GDB}
  707. procedure parse_implementation_uses(symt:Psymtable);
  708. begin
  709. if token=_USES then
  710. begin
  711. symt^.symtabletype:=unitsymtable;
  712. loadunits;
  713. symt^.symtabletype:=globalsymtable;
  714. {$ifdef DEBUG}
  715. test_symtablestack;
  716. {$endif DEBUG}
  717. end;
  718. end;
  719. procedure gen_main_procsym(const name:string;options:longint;st:psymtable);
  720. var
  721. stt : psymtable;
  722. begin
  723. {Generate a procsym for main}
  724. make_ref:=false;
  725. aktprocsym:=new(Pprocsym,init(name));
  726. {Try to insert in in static symtable ! }
  727. stt:=symtablestack;
  728. symtablestack:=st;
  729. aktprocsym^.definition:=new(Pprocdef,init);
  730. symtablestack:=stt;
  731. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or options;
  732. aktprocsym^.definition^.setmangledname(target_os.cprefix+name);
  733. aktprocsym^.definition^.forwarddef:=false;
  734. make_ref:=true;
  735. { The localst is a local symtable. Change it into the static
  736. symtable }
  737. dispose(aktprocsym^.definition^.localst,done);
  738. aktprocsym^.definition^.localst:=st;
  739. { and insert the procsym in symtable }
  740. st^.insert(aktprocsym);
  741. { set some informations about the main program }
  742. with procinfo do
  743. begin
  744. retdef:=voiddef;
  745. _class:=nil;
  746. call_offset:=8;
  747. framepointer:=frame_pointer;
  748. flags:=0;
  749. end;
  750. end;
  751. procedure proc_unit;
  752. function is_assembler_generated:boolean;
  753. begin
  754. is_assembler_generated:=(Errorcount=0) and
  755. not(
  756. codesegment^.empty and
  757. datasegment^.empty and
  758. bsssegment^.empty and
  759. ((importssection=nil) or importssection^.empty) and
  760. ((resourcesection=nil) or resourcesection^.empty)
  761. );
  762. end;
  763. var
  764. names : Tstringcontainer;
  765. st : psymtable;
  766. unitst : punitsymtable;
  767. {$ifdef GDB}
  768. pu : pused_unit;
  769. {$endif GDB}
  770. {$ifdef Test_Double_checksum}
  771. store_crc : longint;
  772. {$endif}
  773. s1,s2 : ^string; {Saves stack space}
  774. begin
  775. consume(_UNIT);
  776. if token=ID then
  777. begin
  778. { create filenames and unit name }
  779. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  780. stringdispose(current_module^.modulename);
  781. current_module^.modulename:=stringdup(upper(pattern));
  782. { check for system unit }
  783. new(s1);
  784. new(s2);
  785. s1^:=upper(target_info.system_unit);
  786. s2^:=upper(SplitName(current_scanner^.inputfile^.name^));
  787. if (cs_compilesystem in aktmoduleswitches) then
  788. begin
  789. if ((length(current_module^.modulename^)>8) or
  790. (current_module^.modulename^<>s1^) or
  791. (current_module^.modulename^<>s2^)) then
  792. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  793. end
  794. else
  795. begin
  796. if (cs_check_unit_name in aktglobalswitches) and
  797. not((current_module^.modulename^=s2^) or
  798. ((length(current_module^.modulename^)>8) and
  799. (copy(current_module^.modulename^,1,8)=s2^))) then
  800. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  801. if (current_module^.modulename^=s1^) then
  802. Message(unit_w_switch_us_missed);
  803. end;
  804. dispose(s2);
  805. dispose(s1);
  806. end;
  807. consume(ID);
  808. consume(SEMICOLON);
  809. consume(_INTERFACE);
  810. { global switches are read, so further changes aren't allowed }
  811. current_module^.in_global:=false;
  812. { can't have local browser when no global browser }
  813. if (cs_local_browser in aktmoduleswitches) and
  814. not(cs_browser in aktmoduleswitches) then
  815. aktmoduleswitches:=aktmoduleswitches-[cs_local_browser];
  816. Message1(unit_u_start_parse_interface,current_module^.modulename^);
  817. { update status }
  818. status.currentmodule:=current_module^.modulename^;
  819. { maybe turn off m_objpas if we are compiling objpas }
  820. if (current_module^.modulename^='OBJPAS') then
  821. aktmodeswitches:=aktmodeswitches-[m_objpas];
  822. { this should be placed after uses !!}
  823. {$ifndef UseNiceNames}
  824. procprefix:='_'+current_module^.modulename^+'$$';
  825. {$else UseNiceNames}
  826. procprefix:='_'+tostr(length(current_module^.modulename^))+lowercase(current_module^.modulename^)+'_';
  827. {$endif UseNiceNames}
  828. parse_only:=true;
  829. { generate now the global symboltable }
  830. st:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  831. refsymtable:=st;
  832. unitst:=punitsymtable(st);
  833. { define first as local to overcome dependency conflicts }
  834. current_module^.localsymtable:=st;
  835. { the unit name must be usable as a unit specifier }
  836. { inside the unit itself (PM) }
  837. { this also forbids to have another symbol }
  838. { with the same name as the unit }
  839. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  840. { a unit compiled at command line must be inside the loaded_unit list }
  841. if (compile_level=1) then
  842. loaded_units.insert(current_module);
  843. { load default units, like the system unit }
  844. loaddefaultunits;
  845. { reset }
  846. make_ref:=true;
  847. lexlevel:=0;
  848. { insert qualifier for the system unit (allows system.writeln) }
  849. if not(cs_compilesystem in aktmoduleswitches) then
  850. begin
  851. if token=_USES then
  852. begin
  853. unitst^.symtabletype:=unitsymtable;
  854. loadunits;
  855. { has it been compiled at a higher level ?}
  856. if current_module^.compiled then
  857. begin
  858. { this unit symtable is obsolete }
  859. { dispose(unitst,done);
  860. disposed as localsymtable !! }
  861. exit;
  862. end;
  863. unitst^.symtabletype:=globalsymtable;
  864. end;
  865. { ... but insert the symbol table later }
  866. st^.next:=symtablestack;
  867. symtablestack:=st;
  868. end
  869. else
  870. { while compiling a system unit, some types are directly inserted }
  871. begin
  872. st^.next:=symtablestack;
  873. symtablestack:=st;
  874. insert_intern_types(st);
  875. end;
  876. { now we know the place to insert the constants }
  877. constsymtable:=symtablestack;
  878. { move the global symtab from the temporary local to global }
  879. current_module^.globalsymtable:=current_module^.localsymtable;
  880. current_module^.localsymtable:=nil;
  881. reset_global_defs;
  882. { ... parse the declarations }
  883. read_interface_declarations;
  884. { leave when we got an error }
  885. if (Errorcount>0) and not status.skip_error then
  886. begin
  887. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  888. status.skip_error:=true;
  889. exit;
  890. end;
  891. {$ifdef New_GDB}
  892. write_gdb_info;
  893. {$endIf Def New_GDB}
  894. {$ifdef Test_Double_checksum}
  895. if (Errorcount=0) then
  896. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),true);
  897. {$endif Test_Double_checksum}
  898. { Parse the implementation section }
  899. consume(_IMPLEMENTATION);
  900. current_module^.in_implementation:=true;
  901. Message1(unit_u_start_parse_implementation,current_module^.modulename^);
  902. parse_only:=false;
  903. { generates static symbol table }
  904. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  905. current_module^.localsymtable:=st;
  906. { remove the globalsymtable from the symtable stack }
  907. { to reinsert it after loading the implementation units }
  908. symtablestack:=unitst^.next;
  909. { we don't want implementation units symbols in unitsymtable !! PM }
  910. refsymtable:=st;
  911. { Read the implementation units }
  912. parse_implementation_uses(unitst);
  913. if current_module^.compiled then
  914. begin
  915. exit;
  916. end;
  917. { reset ranges/stabs in exported definitions }
  918. reset_global_defs;
  919. { All units are read, now give them a number }
  920. numberunits;
  921. { now we can change refsymtable }
  922. refsymtable:=st;
  923. { but reinsert the global symtable as lasts }
  924. unitst^.next:=symtablestack;
  925. symtablestack:=unitst;
  926. {$ifdef DEBUG}
  927. test_symtablestack;
  928. {$endif DEBUG}
  929. constsymtable:=symtablestack;
  930. {$ifdef Splitheap}
  931. if testsplit then
  932. begin
  933. Split_Heap;
  934. allow_special:=true;
  935. Switch_to_temp_heap;
  936. end;
  937. { it will report all crossings }
  938. allow_special:=false;
  939. {$endif Splitheap}
  940. { Generate a procsym }
  941. gen_main_procsym(current_module^.modulename^+'_init',pounitinit,st);
  942. { Compile the unit }
  943. codegen_newprocedure;
  944. names.init;
  945. names.insert('INIT$$'+current_module^.modulename^);
  946. names.insert(target_os.cprefix+current_module^.modulename^+'_init');
  947. compile_proc_body(names,true,false);
  948. names.done;
  949. codegen_doneprocedure;
  950. { avoid self recursive destructor call !! PM }
  951. aktprocsym^.definition^.localst:=nil;
  952. { finalize? }
  953. if token=_FINALIZATION then
  954. begin
  955. { set module options }
  956. current_module^.flags:=current_module^.flags or uf_finalize;
  957. { Generate a procsym }
  958. gen_main_procsym(current_module^.modulename^+'_finalize',pounitfinalize,st);
  959. { Compile the finalize }
  960. codegen_newprocedure;
  961. names.init;
  962. names.insert('FINALIZE$$'+current_module^.modulename^);
  963. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  964. compile_proc_body(names,true,false);
  965. names.done;
  966. codegen_doneprocedure;
  967. end;
  968. { the last char should always be a point }
  969. consume(POINT);
  970. { avoid self recursive destructor call !! PM }
  971. aktprocsym^.definition^.localst:=nil;
  972. { absence does not matter here !! }
  973. aktprocsym^.definition^.forwarddef:=false;
  974. { test static symtable }
  975. if (Errorcount=0) then
  976. st^.allsymbolsused;
  977. { size of the static data }
  978. datasize:=st^.datasize;
  979. {$ifdef GDB}
  980. { add all used definitions even for implementation}
  981. if (cs_debuginfo in aktmoduleswitches) then
  982. begin
  983. {$IfnDef New_GDB}
  984. if assigned(current_module^.globalsymtable) then
  985. begin
  986. { all types }
  987. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  988. { and all local symbols}
  989. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  990. end;
  991. { all local types }
  992. punitsymtable(st)^.concattypestabto(debuglist);
  993. { and all local symbols}
  994. st^.concatstabto(debuglist);
  995. {$else New_GDB}
  996. write_gdb_info;
  997. {$endIf Def New_GDB}
  998. end;
  999. {$endif GDB}
  1000. reset_global_defs;
  1001. { tests, if all (interface) forwards are resolved }
  1002. if (Errorcount=0) then
  1003. symtablestack^.check_forwards;
  1004. { now we have a correct unit, change the symtable type }
  1005. current_module^.in_implementation:=false;
  1006. symtablestack^.symtabletype:=unitsymtable;
  1007. {$ifdef GDB}
  1008. punitsymtable(symtablestack)^.is_stab_written:=false;
  1009. {$endif GDB}
  1010. { leave when we got an error }
  1011. if (Errorcount>0) and not status.skip_error then
  1012. begin
  1013. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1014. status.skip_error:=true;
  1015. exit;
  1016. end;
  1017. { generate imports }
  1018. if current_module^.uses_imports then
  1019. importlib^.generatelib;
  1020. { insert own objectfile, or say that it's in a library
  1021. (no check for an .o when loading) }
  1022. if is_assembler_generated then
  1023. insertobjectfile;
  1024. if cs_local_browser in aktmoduleswitches then
  1025. current_module^.localsymtable:=refsymtable;
  1026. { Write out the ppufile }
  1027. {$ifdef Test_Double_checksum}
  1028. store_crc:=current_module^.interface_crc;
  1029. {$endif Test_Double_checksum}
  1030. if (Errorcount=0) then
  1031. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),false);
  1032. {$ifdef Test_Double_checksum}
  1033. if store_crc<>current_module^.interface_crc then
  1034. Def_comment(V_Warning,current_module^.ppufilename^+' CRC changed '+
  1035. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1036. {$endif def Test_Double_checksum}
  1037. { must be done only after local symtable ref stores !! }
  1038. closecurrentppu;
  1039. {$ifdef GDB}
  1040. pu:=pused_unit(usedunits.first);
  1041. while assigned(pu) do
  1042. begin
  1043. punitsymtable(pu^.u^.globalsymtable)^.is_stab_written:=false;
  1044. pu:=pused_unit(pu^.next);
  1045. end;
  1046. {$endif GDB}
  1047. { remove static symtable (=refsymtable) here to save some mem }
  1048. if not (cs_local_browser in aktmoduleswitches) then
  1049. begin
  1050. dispose(st,done);
  1051. current_module^.localsymtable:=nil;
  1052. end;
  1053. if is_assembler_generated then
  1054. begin
  1055. { finish asmlist by adding segment starts }
  1056. insertsegment;
  1057. { assemble }
  1058. create_objectfile;
  1059. end;
  1060. { leave when we got an error }
  1061. if (Errorcount>0) and not status.skip_error then
  1062. begin
  1063. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1064. status.skip_error:=true;
  1065. exit;
  1066. end;
  1067. end;
  1068. procedure proc_program(islibrary : boolean);
  1069. var
  1070. st : psymtable;
  1071. hp : pmodule;
  1072. names : Tstringcontainer;
  1073. begin
  1074. DLLsource:=islibrary;
  1075. parse_only:=false;
  1076. if islibrary then
  1077. begin
  1078. consume(_LIBRARY);
  1079. stringdispose(current_module^.modulename);
  1080. current_module^.modulename:=stringdup(pattern);
  1081. current_module^.islibrary:=true;
  1082. exportlib^.preparelib(pattern);
  1083. consume(ID);
  1084. consume(SEMICOLON);
  1085. end
  1086. else
  1087. { is there an program head ? }
  1088. if token=_PROGRAM then
  1089. begin
  1090. consume(_PROGRAM);
  1091. stringdispose(current_module^.modulename);
  1092. current_module^.modulename:=stringdup(pattern);
  1093. consume(ID);
  1094. if token=LKLAMMER then
  1095. begin
  1096. consume(LKLAMMER);
  1097. idlist;
  1098. consume(RKLAMMER);
  1099. end;
  1100. consume(SEMICOLON);
  1101. end;
  1102. { global switches are read, so further changes aren't allowed }
  1103. current_module^.in_global:=false;
  1104. { can't have local browser when no global browser }
  1105. if (cs_local_browser in aktmoduleswitches) and
  1106. not(cs_browser in aktmoduleswitches) then
  1107. aktmoduleswitches:=aktmoduleswitches-[cs_local_browser];
  1108. { set implementation flag }
  1109. current_module^.in_implementation:=true;
  1110. { insert after the unit symbol tables the static symbol table }
  1111. { of the program }
  1112. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1113. current_module^.localsymtable:=st;
  1114. symtablestack:=st;
  1115. refsymtable:=st;
  1116. { necessary for browser }
  1117. loaded_units.insert(current_module);
  1118. { load standard units (system,objpas,profile unit) }
  1119. loaddefaultunits;
  1120. { reset }
  1121. lexlevel:=0;
  1122. {Load the units used by the program we compile.}
  1123. if token=_USES then
  1124. loadunits;
  1125. reset_global_defs;
  1126. {Insert the name of the main program into the symbol table.}
  1127. if current_module^.modulename^<>'' then
  1128. {st^.insert(new(pprogramsym,init(current_module^.modulename^)));}
  1129. st^.insert(new(punitsym,init(current_module^.modulename^,punitsymtable(st))));
  1130. { ...is also constsymtable, this is the symtable where }
  1131. { the elements of enumeration types are inserted }
  1132. constsymtable:=st;
  1133. { Generate a procsym for main }
  1134. gen_main_procsym('main',poproginit,st);
  1135. { reset }
  1136. procprefix:='';
  1137. in_except_block:=false;
  1138. {The program intialization needs an alias, so it can be called
  1139. from the bootstrap code.}
  1140. codegen_newprocedure;
  1141. names.init;
  1142. names.insert('program_init');
  1143. names.insert('PASCALMAIN');
  1144. names.insert(target_os.cprefix+'main');
  1145. {$ifdef m68k}
  1146. if target_info.target=target_m68k_PalmOS then
  1147. names.insert('PilotMain');
  1148. {$endif}
  1149. compile_proc_body(names,true,false);
  1150. names.done;
  1151. { avoid self recursive destructor call !! PM }
  1152. aktprocsym^.definition^.localst:=nil;
  1153. { consider these symbols as global ones }
  1154. { for browser }
  1155. current_module^.globalsymtable:=current_module^.localsymtable;
  1156. current_module^.localsymtable:=nil;
  1157. codegen_doneprocedure;
  1158. { consume the last point }
  1159. consume(POINT);
  1160. {$ifdef New_GDB}
  1161. write_gdb_info;
  1162. {$endIf Def New_GDB}
  1163. { leave when we got an error }
  1164. if (Errorcount>0) and not status.skip_error then
  1165. begin
  1166. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1167. status.skip_error:=true;
  1168. exit;
  1169. end;
  1170. { generate imports }
  1171. if current_module^.uses_imports then
  1172. importlib^.generatelib;
  1173. if islibrary then
  1174. exportlib^.generatelib;
  1175. { insert heap }
  1176. insertinitfinaltable;
  1177. insertheap;
  1178. inserttargetspecific;
  1179. datasize:=symtablestack^.datasize;
  1180. { finish asmlist by adding segment starts }
  1181. insertsegment;
  1182. { insert own objectfile }
  1183. insertobjectfile;
  1184. { assemble and link }
  1185. create_objectfile;
  1186. { leave when we got an error }
  1187. if (Errorcount>0) and not status.skip_error then
  1188. begin
  1189. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1190. status.skip_error:=true;
  1191. exit;
  1192. end;
  1193. { create the executable when we are at level 1 }
  1194. if (compile_level=1) then
  1195. begin
  1196. { insert all .o files from all loaded units }
  1197. hp:=pmodule(loaded_units.first);
  1198. while assigned(hp) do
  1199. begin
  1200. Linker.AddModuleFiles(hp);
  1201. hp:=pmodule(hp^.next);
  1202. end;
  1203. { write .def file }
  1204. if (cs_link_deffile in aktglobalswitches) then
  1205. deffile.writefile;
  1206. { finally we can create a executable }
  1207. if (not current_module^.is_unit) then
  1208. Linker.MakeExecutable;
  1209. end;
  1210. end;
  1211. end.
  1212. {
  1213. $Log$
  1214. Revision 1.123 1999-05-21 13:55:06 peter
  1215. * NEWLAB for label as symbol
  1216. Revision 1.122 1999/05/18 22:36:29 pierre
  1217. * little redondant code removed
  1218. Revision 1.121 1999/05/17 14:43:54 pierre
  1219. * reset current_ppu to nil when appropriate
  1220. Revision 1.120 1999/05/13 21:59:35 peter
  1221. * removed oldppu code
  1222. * warning if objpas is loaded from uses
  1223. * first things for new deref writing
  1224. Revision 1.119 1999/05/09 11:38:08 peter
  1225. * don't write .o and link if errors occure during assembling
  1226. Revision 1.118 1999/05/03 18:03:28 peter
  1227. * no ar.exe running field in target_asm
  1228. Revision 1.117 1999/05/01 13:24:32 peter
  1229. * merged nasm compiler
  1230. * old asm moved to oldasm/
  1231. Revision 1.116 1999/04/26 23:22:42 peter
  1232. * fixed double .o file insertion
  1233. Revision 1.115 1999/04/26 18:29:58 peter
  1234. * farpointerdef moved into pointerdef.is_far
  1235. Revision 1.114 1999/04/26 13:31:39 peter
  1236. * release storenumber,double_checksum
  1237. Revision 1.113 1999/04/25 17:32:14 peter
  1238. * fixed double_checksum
  1239. Revision 1.112 1999/04/25 15:08:38 peter
  1240. * small fixes for double_checksum
  1241. Revision 1.111 1999/04/21 09:43:46 peter
  1242. * storenumber works
  1243. * fixed some typos in double_checksum
  1244. + incompatible types type1 and type2 message (with storenumber)
  1245. Revision 1.110 1999/04/17 13:14:52 peter
  1246. * concat_external added for new init/final
  1247. Revision 1.109 1999/04/15 12:19:59 peter
  1248. + finalization support
  1249. Revision 1.108 1999/04/14 09:14:52 peter
  1250. * first things to store the symbol/def number in the ppu
  1251. Revision 1.107 1999/04/08 10:53:54 michael
  1252. * Fixed forgotten ;
  1253. Revision 1.106 1999/04/07 15:39:30 pierre
  1254. + double_checksum code added
  1255. Revision 1.105 1999/03/26 00:05:38 peter
  1256. * released valintern
  1257. + deffile is now removed when compiling is finished
  1258. * ^( compiles now correct
  1259. + static directive
  1260. * shrd fixed
  1261. Revision 1.104 1999/03/24 23:17:17 peter
  1262. * fixed bugs 212,222,225,227,229,231,233
  1263. Revision 1.103 1999/03/18 20:30:46 peter
  1264. + .a writer
  1265. Revision 1.102 1999/03/16 21:07:25 peter
  1266. * check for dup uses
  1267. Revision 1.101 1999/02/25 21:02:43 peter
  1268. * ag386bin updates
  1269. + coff writer
  1270. Revision 1.100 1999/02/23 18:29:20 pierre
  1271. * win32 compilation error fix
  1272. + some work for local browser (not cl=omplete yet)
  1273. Revision 1.99 1999/02/22 13:06:58 pierre
  1274. + -b and -bl options work !
  1275. + cs_local_browser ($L+) is disabled if cs_browser ($Y+)
  1276. is not enabled when quitting global section
  1277. * local vars and procedures are not yet stored into PPU
  1278. Revision 1.98 1999/02/22 02:15:29 peter
  1279. * updates for ag386bin
  1280. Revision 1.97 1999/02/16 00:45:31 peter
  1281. * fixed crashes by forgotten strpnew() for init_symbol
  1282. Revision 1.96 1999/02/05 08:54:27 pierre
  1283. + linkofiles splitted inot linkofiles and linkunitfiles
  1284. because linkofiles must be stored with directory
  1285. to enabled linking of different objects with same name
  1286. in a different directory
  1287. Revision 1.95 1999/02/03 09:44:36 pierre
  1288. * symbol nubering begins with 1 in number_symbols
  1289. * program tmodule has globalsymtable for its staticsymtable
  1290. (to get it displayed in IDE globals list)
  1291. + list of symbol (browcol) greatly improved for IDE
  1292. Revision 1.94 1999/01/12 14:25:31 peter
  1293. + BrowserLog for browser.log generation
  1294. + BrowserCol for browser info in TCollections
  1295. * released all other UseBrowser
  1296. Revision 1.93 1999/01/06 12:39:46 peter
  1297. * renamed resource -> comprsrc (conflicted with FV)
  1298. Revision 1.92 1998/12/28 23:26:23 peter
  1299. + resource file handling ($R directive) for Win32
  1300. Revision 1.91 1998/12/15 17:14:17 peter
  1301. * fix for tp7
  1302. Revision 1.90 1998/12/15 10:23:26 peter
  1303. + -iSO, -iSP, -iTO, -iTP
  1304. Revision 1.89 1998/12/11 00:03:34 peter
  1305. + globtype,tokens,version unit splitted from globals
  1306. Revision 1.88 1998/12/08 10:18:11 peter
  1307. + -gh for heaptrc unit
  1308. Revision 1.87 1998/12/01 23:40:53 pierre
  1309. * new try for correct debug info generation
  1310. Revision 1.86 1998/11/30 09:43:22 pierre
  1311. * some range check bugs fixed (still not working !)
  1312. + added DLL writing support for win32 (also accepts variables)
  1313. + TempAnsi for code that could be used for Temporary ansi strings
  1314. handling
  1315. Revision 1.85 1998/11/28 16:20:54 peter
  1316. + support for dll variables
  1317. Revision 1.84 1998/11/18 09:18:03 pierre
  1318. + automatic loading of profile unit with -pg option
  1319. in go32v2 mode (also defines FPC_PROFILE)
  1320. * some memory leaks removed
  1321. * unreleased temp problem with sets solved
  1322. Revision 1.83 1998/11/16 11:29:00 pierre
  1323. * stackcheck removed for i386_win32
  1324. * exportlist does not crash at least !!
  1325. (was need for tests dir !)z
  1326. Revision 1.82 1998/11/12 12:55:16 pierre
  1327. * fix for bug0176 and bug0177
  1328. Revision 1.81 1998/11/12 11:34:58 peter
  1329. * fix for empty .o files and linking of libs
  1330. Revision 1.80 1998/11/06 09:48:14 pierre
  1331. * double initialization code calling bug fixed
  1332. Revision 1.79 1998/11/03 11:33:11 peter
  1333. + search_unit arg to only search for sources
  1334. Revision 1.78 1998/10/30 12:23:41 peter
  1335. * fix for lognunitname and longunit.pas
  1336. Revision 1.77 1998/10/29 11:35:52 florian
  1337. * some dll support for win32
  1338. * fixed assembler writing for PalmOS
  1339. Revision 1.76 1998/10/28 18:26:15 pierre
  1340. * removed some erros after other errors (introduced by useexcept)
  1341. * stabs works again correctly (for how long !)
  1342. Revision 1.75 1998/10/27 13:45:35 pierre
  1343. * classes get a vmt allways
  1344. * better error info (tried to remove
  1345. several error strings introduced by the tpexcept handling)
  1346. Revision 1.74 1998/10/26 09:34:50 peter
  1347. * unit check name works now for all units, not only systemunit
  1348. Revision 1.73 1998/10/22 23:53:27 peter
  1349. * leave when an error has been in the interface (else other units could
  1350. be compiled with the implementation uses!)
  1351. * don't always compile when not in implementation with a second_load
  1352. Revision 1.72 1998/10/22 11:36:34 peter
  1353. * fixed imports generation at the wrong place
  1354. Revision 1.71 1998/10/21 20:13:10 peter
  1355. * check for importsection for empty asm file
  1356. Revision 1.70 1998/10/20 09:30:05 peter
  1357. * set also in_library flag when no .o is generated
  1358. Revision 1.68 1998/10/19 08:54:59 pierre
  1359. * wrong stabs info corrected once again !!
  1360. + variable vmt offset with vmt field only if required
  1361. implemented now !!!
  1362. Revision 1.67 1998/10/13 13:10:25 peter
  1363. * new style for m68k/i386 infos and enums
  1364. Revision 1.66 1998/10/09 16:36:05 pierre
  1365. * some memory leaks specific to usebrowser define fixed
  1366. * removed tmodule.implsymtable (was like tmodule.localsymtable)
  1367. Revision 1.65 1998/10/09 14:38:55 pierre
  1368. * add a second load for PPU file
  1369. Revision 1.64 1998/10/09 08:56:28 pierre
  1370. * several memory leaks fixed
  1371. Revision 1.63 1998/10/08 23:29:01 peter
  1372. * -vu shows unit info, -vt shows tried/used files
  1373. Revision 1.62 1998/10/08 17:17:25 pierre
  1374. * current_module old scanner tagged as invalid if unit is recompiled
  1375. + added ppheap for better info on tracegetmem of heaptrc
  1376. (adds line column and file index)
  1377. * several memory leaks removed ith help of heaptrc !!
  1378. Revision 1.61 1998/10/08 13:48:47 peter
  1379. * fixed memory leaks for do nothing source
  1380. * fixed unit interdependency
  1381. Revision 1.60 1998/10/06 17:16:54 pierre
  1382. * some memory leaks fixed (thanks to Peter for heaptrc !)
  1383. Revision 1.59 1998/10/05 21:33:26 peter
  1384. * fixed 161,165,166,167,168
  1385. Revision 1.58 1998/09/30 16:43:37 peter
  1386. * fixed unit interdependency with circular uses
  1387. Revision 1.57 1998/09/30 12:11:52 peter
  1388. * fixed circular uses which looped forever
  1389. Revision 1.56 1998/09/28 11:22:15 pierre
  1390. * did not compile for browser
  1391. * merge from fixes
  1392. Revision 1.48.2.1 1998/09/28 10:55:16 pierre
  1393. fix for current_module dispose bug
  1394. Revision 1.55 1998/09/28 11:04:03 peter
  1395. * fixed loaddefaultunits which was at the wrong place for programs, so
  1396. the default defs were not loaded when main was initialized
  1397. Revision 1.54 1998/09/24 23:49:12 peter
  1398. + aktmodeswitches
  1399. Revision 1.53 1998/09/23 12:20:50 pierre
  1400. * main program tmodule had no symtable (crashed browser)
  1401. * unit symbols problem fixed !!
  1402. Revision 1.52 1998/09/22 17:13:49 pierre
  1403. + browsing updated and developed
  1404. records and objects fields are also stored
  1405. Revision 1.51 1998/09/22 15:40:55 peter
  1406. * some extra ifdef GDB
  1407. Revision 1.50 1998/09/21 08:45:17 pierre
  1408. + added vmt_offset in tobjectdef.write for fututre use
  1409. (first steps to have objects without vmt if no virtual !!)
  1410. + added fpu_used field for tabstractprocdef :
  1411. sets this level to 2 if the functions return with value in FPU
  1412. (is then set to correct value at parsing of implementation)
  1413. THIS MIGHT refuse some code with FPU expression too complex
  1414. that were accepted before and even in some cases
  1415. that don't overflow in fact
  1416. ( like if f : float; is a forward that finally in implementation
  1417. only uses one fpu register !!)
  1418. Nevertheless I think that it will improve security on
  1419. FPU operations !!
  1420. * most other changes only for UseBrowser code
  1421. (added symtable references for record and objects)
  1422. local switch for refs to args and local of each function
  1423. (static symtable still missing)
  1424. UseBrowser still not stable and probably broken by
  1425. the definition hash array !!
  1426. Revision 1.49 1998/09/18 08:01:36 pierre
  1427. + improvement on the usebrowser part
  1428. (does not work correctly for now)
  1429. Revision 1.48 1998/09/09 15:33:07 peter
  1430. * fixed in_global to allow directives also after interface token
  1431. Revision 1.47 1998/09/09 11:50:55 pierre
  1432. * forward def are not put in record or objects
  1433. + added check for forwards also in record and objects
  1434. * dummy parasymtable for unit initialization removed from
  1435. symtable stack
  1436. Revision 1.46 1998/09/03 11:24:01 peter
  1437. * moved more inputfile things from tscannerfile to tinputfile
  1438. * changed ifdef Sourceline to cs_asm_source
  1439. Revision 1.45 1998/08/31 12:26:28 peter
  1440. * m68k and palmos updates from surebugfixes
  1441. Revision 1.44 1998/08/26 15:35:33 peter
  1442. * fixed scannerfiles for macros
  1443. + $I %<environment>%
  1444. Revision 1.43 1998/08/26 10:08:47 peter
  1445. * fixed problem with libprefix at the wrong place
  1446. * fixed lib generation with smartlinking and no -CS used
  1447. Revision 1.42 1998/08/19 18:04:54 peter
  1448. * fixed current_module^.in_implementation flag
  1449. Revision 1.41 1998/08/17 10:10:08 peter
  1450. - removed OLDPPU
  1451. Revision 1.40 1998/08/17 09:17:50 peter
  1452. * static/shared linking updates
  1453. Revision 1.39 1998/08/14 21:56:37 peter
  1454. * setting the outputfile using -o works now to create static libs
  1455. Revision 1.38 1998/08/10 14:50:13 peter
  1456. + localswitches, moduleswitches, globalswitches splitting
  1457. Revision 1.37 1998/08/10 10:18:31 peter
  1458. + Compiler,Comphook unit which are the new interface units to the
  1459. compiler
  1460. Revision 1.36 1998/07/14 14:46:54 peter
  1461. * released NEWINPUT
  1462. Revision 1.35 1998/07/08 12:39:38 peter
  1463. * heap_size for m68k
  1464. Revision 1.34 1998/07/07 11:20:03 peter
  1465. + NEWINPUT for a better inputfile and scanner object
  1466. Revision 1.33 1998/06/25 11:15:34 pierre
  1467. * ppu files where not closed in newppu !!
  1468. second compilation was impossible due to too many opened files
  1469. (not visible in 'make cycle' as we remove all the ppu files)
  1470. Revision 1.32 1998/06/25 08:48:16 florian
  1471. * first version of rtti support
  1472. Revision 1.31 1998/06/24 14:48:35 peter
  1473. * ifdef newppu -> ifndef oldppu
  1474. Revision 1.30 1998/06/17 14:10:16 peter
  1475. * small os2 fixes
  1476. * fixed interdependent units with newppu (remake3 under linux works now)
  1477. Revision 1.29 1998/06/16 08:56:25 peter
  1478. + targetcpu
  1479. * cleaner pmodules for newppu
  1480. Revision 1.28 1998/06/13 00:10:10 peter
  1481. * working browser and newppu
  1482. * some small fixes against crashes which occured in bp7 (but not in
  1483. fpc?!)
  1484. Revision 1.27 1998/06/11 13:58:08 peter
  1485. * small fix to let newppu compile
  1486. Revision 1.26 1998/06/09 16:01:47 pierre
  1487. + added procedure directive parsing for procvars
  1488. (accepted are popstack cdecl and pascal)
  1489. + added C vars with the following syntax
  1490. var C calias 'true_c_name';(can be followed by external)
  1491. reason is that you must add the Cprefix
  1492. which is target dependent
  1493. Revision 1.25 1998/06/08 22:59:49 peter
  1494. * smartlinking works for win32
  1495. * some defines to exclude some compiler parts
  1496. Revision 1.24 1998/06/08 13:13:44 pierre
  1497. + temporary variables now in temp_gen.pas unit
  1498. because it is processor independent
  1499. * mppc68k.bat modified to undefine i386 and support_mmx
  1500. (which are defaults for i386)
  1501. Revision 1.23 1998/06/05 17:47:29 peter
  1502. * some better uses clauses
  1503. Revision 1.22 1998/06/05 14:37:34 pierre
  1504. * fixes for inline for operators
  1505. * inline procedure more correctly restricted
  1506. Revision 1.21 1998/06/04 23:51:53 peter
  1507. * m68k compiles
  1508. + .def file creation moved to gendef.pas so it could also be used
  1509. for win32
  1510. Revision 1.20 1998/06/04 09:55:42 pierre
  1511. * demangled name of procsym reworked to become independant of the mangling scheme
  1512. Revision 1.19 1998/06/03 23:40:38 peter
  1513. + unlimited file support, release tempclose
  1514. Revision 1.18 1998/06/03 22:49:00 peter
  1515. + wordbool,longbool
  1516. * rename bis,von -> high,low
  1517. * moved some systemunit loading/creating to psystem.pas
  1518. Revision 1.17 1998/05/28 14:40:25 peter
  1519. * fixes for newppu, remake3 works now with it
  1520. Revision 1.16 1998/05/27 19:45:06 peter
  1521. * symtable.pas splitted into includefiles
  1522. * symtable adapted for $ifdef NEWPPU
  1523. Revision 1.15 1998/05/23 01:21:22 peter
  1524. + aktasmmode, aktoptprocessor, aktoutputformat
  1525. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  1526. + $LIBNAME to set the library name where the unit will be put in
  1527. * splitted cgi386 a bit (codeseg to large for bp7)
  1528. * nasm, tasm works again. nasm moved to ag386nsm.pas
  1529. Revision 1.14 1998/05/20 09:42:35 pierre
  1530. + UseTokenInfo now default
  1531. * unit in interface uses and implementation uses gives error now
  1532. * only one error for unknown symbol (uses lastsymknown boolean)
  1533. the problem came from the label code !
  1534. + first inlined procedures and function work
  1535. (warning there might be allowed cases were the result is still wrong !!)
  1536. * UseBrower updated gives a global list of all position of all used symbols
  1537. with switch -gb
  1538. Revision 1.13 1998/05/12 10:47:00 peter
  1539. * moved printstatus to verb_def
  1540. + V_Normal which is between V_Error and V_Warning and doesn't have a
  1541. prefix like error: warning: and is included in V_Default
  1542. * fixed some messages
  1543. * first time parameter scan is only for -v and -T
  1544. - removed old style messages
  1545. Revision 1.12 1998/05/11 13:07:56 peter
  1546. + $ifdef NEWPPU for the new ppuformat
  1547. + $define GDB not longer required
  1548. * removed all warnings and stripped some log comments
  1549. * no findfirst/findnext anymore to remove smartlink *.o files
  1550. Revision 1.11 1998/05/06 18:36:53 peter
  1551. * tai_section extended with code,data,bss sections and enumerated type
  1552. * ident 'compiled by FPC' moved to pmodules
  1553. * small fix for smartlink
  1554. Revision 1.10 1998/05/04 17:54:28 peter
  1555. + smartlinking works (only case jumptable left todo)
  1556. * redesign of systems.pas to support assemblers and linkers
  1557. + Unitname is now also in the PPU-file, increased version to 14
  1558. Revision 1.9 1998/05/01 16:38:45 florian
  1559. * handling of private and protected fixed
  1560. + change_keywords_to_tp implemented to remove
  1561. keywords which aren't supported by tp
  1562. * break and continue are now symbols of the system unit
  1563. + widestring, longstring and ansistring type released
  1564. Revision 1.8 1998/04/30 15:59:41 pierre
  1565. * GDB works again better :
  1566. correct type info in one pass
  1567. + UseTokenInfo for better source position
  1568. * fixed one remaining bug in scanner for line counts
  1569. * several little fixes
  1570. Revision 1.7 1998/04/29 10:33:59 pierre
  1571. + added some code for ansistring (not complete nor working yet)
  1572. * corrected operator overloading
  1573. * corrected nasm output
  1574. + started inline procedures
  1575. + added starstarn : use ** for exponentiation (^ gave problems)
  1576. + started UseTokenInfo cond to get accurate positions
  1577. Revision 1.6 1998/04/27 23:10:28 peter
  1578. + new scanner
  1579. * $makelib -> if smartlink
  1580. * small filename fixes pmodule.setfilename
  1581. * moved import from files.pas -> import.pas
  1582. Revision 1.5 1998/04/14 23:27:03 florian
  1583. + exclude/include with constant second parameter added
  1584. Revision 1.4 1998/04/10 14:41:43 peter
  1585. * removed some Hints
  1586. * small speed optimization for AsmLn
  1587. Revision 1.3 1998/04/03 09:51:00 daniel
  1588. * Fixed heap allocation for OS/2.
  1589. }