pmodules.pas 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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)
  51. {$ifndef AG386BIN}
  52. or (cs_smartlink in aktmoduleswitches)
  53. {$endif}
  54. then
  55. Linker.MakeStaticLibrary(SmartLinkFilesCnt);
  56. end;
  57. procedure insertobjectfile;
  58. { Insert the used object file for this unit in the used list for this unit }
  59. begin
  60. if (cs_create_sharedlib in aktmoduleswitches) then
  61. begin
  62. current_module^.linksharedlibs.insert(current_module^.sharedlibfilename^);
  63. current_module^.flags:=current_module^.flags or uf_shared_linked;
  64. end
  65. else
  66. begin
  67. if (cs_create_staticlib in aktmoduleswitches) or
  68. (cs_smartlink in aktmoduleswitches) then
  69. begin
  70. current_module^.linkstaticlibs.insert(current_module^.staticlibfilename^);
  71. current_module^.flags:=current_module^.flags or uf_static_linked;
  72. end
  73. else
  74. begin
  75. current_module^.linkunitfiles.insert(current_module^.objfilename^);
  76. current_module^.flags:=current_module^.flags or uf_obj_linked;
  77. end;
  78. end;
  79. end;
  80. procedure insertsegment;
  81. procedure fixseg(p:paasmoutput;sec:tsection);
  82. begin
  83. p^.insert(new(pai_section,init(sec)));
  84. if (cs_smartlink in aktmoduleswitches) then
  85. p^.insert(new(pai_cut,init));
  86. p^.concat(new(pai_section,init(sec_none)));
  87. end;
  88. begin
  89. {Insert Ident of the compiler}
  90. if (not (cs_smartlink in aktmoduleswitches))
  91. {$ifndef EXTDEBUG}
  92. and (not current_module^.is_unit)
  93. {$endif}
  94. then
  95. begin
  96. datasegment^.insert(new(pai_align,init(4)));
  97. datasegment^.insert(new(pai_string,init('FPC '+version_string+
  98. ' for '+target_cpu_string+' - '+target_info.short_name)));
  99. end;
  100. { Insert start and end of sections }
  101. fixseg(codesegment,sec_code);
  102. fixseg(datasegment,sec_data);
  103. fixseg(bsssegment,sec_bss);
  104. { we should use .rdata section for these two no ? }
  105. { .rdata is a read only data section (PM) }
  106. fixseg(rttilist,sec_data);
  107. fixseg(consts,sec_data);
  108. {$ifdef GDB}
  109. if assigned(debuglist) then
  110. fixseg(debuglist,sec_code);
  111. {$endif GDB}
  112. end;
  113. procedure InsertInitFinalTable;
  114. var
  115. hp : pused_unit;
  116. unitinits : taasmoutput;
  117. count : longint;
  118. begin
  119. unitinits.init;
  120. count:=0;
  121. hp:=pused_unit(usedunits.first);
  122. while assigned(hp) do
  123. begin
  124. { call the unit init code and make it external }
  125. if (hp^.u^.flags and (uf_init or uf_finalize))<>0 then
  126. begin
  127. if (hp^.u^.flags and uf_init)<>0 then
  128. begin
  129. unitinits.concat(new(pai_const_symbol,init('INIT$$'+hp^.u^.modulename^)));
  130. concat_external('INIT$$'+hp^.u^.modulename^,EXT_NEAR);
  131. end
  132. else
  133. unitinits.concat(new(pai_const,init_32bit(0)));
  134. if (hp^.u^.flags and uf_finalize)<>0 then
  135. begin
  136. unitinits.concat(new(pai_const_symbol,init('FINALIZE$$'+hp^.u^.modulename^)));
  137. concat_external('FINALIZE$$'+hp^.u^.modulename^,EXT_NEAR);
  138. end
  139. else
  140. unitinits.concat(new(pai_const,init_32bit(0)));
  141. inc(count);
  142. end;
  143. hp:=Pused_unit(hp^.next);
  144. end;
  145. { TableCount,InitCount }
  146. unitinits.insert(new(pai_const,init_32bit(0)));
  147. unitinits.insert(new(pai_const,init_32bit(count)));
  148. unitinits.insert(new(pai_symbol,init_global('INITFINAL')));
  149. { insert in data segment }
  150. if (cs_smartlink in aktmoduleswitches) then
  151. datasegment^.concat(new(pai_cut,init));
  152. datasegment^.concatlist(@unitinits);
  153. unitinits.done;
  154. end;
  155. procedure insertheap;
  156. begin
  157. if (cs_smartlink in aktmoduleswitches) then
  158. begin
  159. bsssegment^.concat(new(pai_cut,init));
  160. datasegment^.concat(new(pai_cut,init));
  161. end;
  162. { On the Macintosh Classic M68k Architecture
  163. The Heap variable is simply a POINTER to the
  164. real HEAP. The HEAP must be set up by the RTL
  165. and must store the pointer in this value.
  166. On OS/2 the heap is also intialized by the RTL. We do
  167. not output a pointer }
  168. case target_info.target of
  169. {$ifdef i386}
  170. target_i386_OS2:
  171. ;
  172. {$endif i386}
  173. {$ifdef m68k}
  174. target_m68k_Mac:
  175. bsssegment^.concat(new(pai_datablock,init_global('HEAP',4)));
  176. target_m68k_PalmOS:
  177. ;
  178. {$endif m68k}
  179. else
  180. bsssegment^.concat(new(pai_datablock,init_global('HEAP',heapsize)));
  181. end;
  182. {$ifdef i386}
  183. datasegment^.concat(new(pai_symbol,init_global('HEAPSIZE')));
  184. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  185. {$endif i386}
  186. {$ifdef m68k}
  187. if target_info.target<>target_m68k_PalmOS then
  188. begin
  189. datasegment^.concat(new(pai_symbol,init_global('HEAP_SIZE')));
  190. datasegment^.concat(new(pai_const,init_32bit(heapsize)));
  191. end;
  192. {$endif m68k}
  193. end;
  194. procedure inserttargetspecific;
  195. begin
  196. case target_info.target of
  197. {$ifdef i386}
  198. target_i386_GO32V2 :
  199. begin
  200. { stacksize can be specified }
  201. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  202. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  203. end;
  204. target_i386_WIN32 :
  205. begin
  206. { Generate an external entry to be sure that _mainCRTStarup will be
  207. linked, can't use concat_external because those aren't written for
  208. asw (PFV) }
  209. target_link.bindcmd[1]:=target_link.bindcmd[1]+' -d '+deffile.fname;
  210. if DLLsource then
  211. target_link.binders:=2;
  212. if RelocSection then
  213. begin
  214. target_link.linkcmd:=target_link.linkcmd+' --base-file base.$$$';
  215. target_link.bindcmd[1]:=target_link.bindcmd[1]+' --base-file base.$$$';
  216. target_link.binders:=2;
  217. end;
  218. if apptype=at_gui then
  219. begin
  220. target_link.linkcmd:='--subsystem windows '+target_link.linkcmd;
  221. target_link.bindcmd[2]:='--subsystem windows '+target_link.bindcmd[2];
  222. end;
  223. if DLLsource then
  224. begin
  225. target_link.linkcmd:='--dll '+target_link.linkcmd;
  226. target_link.bindcmd[2]:='--dll '+target_link.bindcmd[2];
  227. end;
  228. end;
  229. {$endif i386}
  230. {$ifdef m68k}
  231. target_m68k_Atari :
  232. begin
  233. { stacksize can be specified }
  234. datasegment^.concat(new(pai_symbol,init_global('__stklen')));
  235. datasegment^.concat(new(pai_const,init_32bit(stacksize)));
  236. end;
  237. {$endif m68k}
  238. end;
  239. end;
  240. function loadunit(const s : string;compile_system:boolean) : pmodule;forward;
  241. procedure load_usedunits(compile_system:boolean);
  242. var
  243. pu : pused_unit;
  244. loaded_unit : pmodule;
  245. load_refs : boolean;
  246. nextmapentry : longint;
  247. begin
  248. load_refs:=true;
  249. { init the map }
  250. new(current_module^.map);
  251. fillchar(current_module^.map^,sizeof(tunitmap),#0);
  252. {$ifdef NEWMAP}
  253. current_module^.map^[0]:=current_module;
  254. {$endif NEWMAP}
  255. nextmapentry:=1;
  256. { load the used units from interface }
  257. current_module^.in_implementation:=false;
  258. pu:=pused_unit(current_module^.used_units.first);
  259. while assigned(pu) do
  260. begin
  261. if (not pu^.loaded) and (pu^.in_interface) then
  262. begin
  263. loaded_unit:=loadunit(pu^.name^,false);
  264. if current_module^.compiled then
  265. exit;
  266. { register unit in used units }
  267. pu^.u:=loaded_unit;
  268. pu^.loaded:=true;
  269. { doubles are not important for that list PM }
  270. pu^.u^.dependent_units.concat(new(pdependent_unit,init(current_module)));
  271. { need to recompile the current unit ? }
  272. if loaded_unit^.crc<>pu^.checksum then
  273. begin
  274. Message2(unit_u_recompile_crc_change,current_module^.modulename^,pu^.name^);
  275. current_module^.do_compile:=true;
  276. dispose(current_module^.map);
  277. current_module^.map:=nil;
  278. exit;
  279. end;
  280. { setup the map entry for deref }
  281. {$ifndef NEWMAP}
  282. current_module^.map^[nextmapentry]:=loaded_unit^.globalsymtable;
  283. {$else NEWMAP}
  284. current_module^.map^[nextmapentry]:=loaded_unit;
  285. {$endif NEWMAP}
  286. inc(nextmapentry);
  287. if nextmapentry>maxunits then
  288. Message(unit_f_too_much_units);
  289. end;
  290. pu:=pused_unit(pu^.next);
  291. end;
  292. { ok, now load the unit }
  293. current_module^.globalsymtable:=new(punitsymtable,loadasunit);
  294. { if this is the system unit insert the intern symbols }
  295. {$ifdef OLDPPU}
  296. if compile_system then
  297. begin
  298. make_ref:=false;
  299. insertinternsyms(psymtable(current_module^.globalsymtable));
  300. make_ref:=true;
  301. end;
  302. {$endif}
  303. { now only read the implementation part }
  304. current_module^.in_implementation:=true;
  305. { load the used units from implementation }
  306. pu:=pused_unit(current_module^.used_units.first);
  307. while assigned(pu) do
  308. begin
  309. if (not pu^.loaded) and (not pu^.in_interface) then
  310. begin
  311. loaded_unit:=loadunit(pu^.name^,false);
  312. if current_module^.compiled then
  313. exit;
  314. { register unit in used units }
  315. pu^.u:=loaded_unit;
  316. pu^.loaded:=true;
  317. {$ifndef OLDPPU}
  318. { need to recompile the current unit ? }
  319. if loaded_unit^.crc<>pu^.checksum then
  320. { if (loaded_unit^.interface_crc<>pu^.interface_checksum) then }
  321. begin
  322. Message2(unit_u_recompile_crc_change,current_module^.modulename^,pu^.name^);
  323. current_module^.do_compile:=true;
  324. dispose(current_module^.map);
  325. current_module^.map:=nil;
  326. exit;
  327. end;
  328. {$endif OLDPPU}
  329. { setup the map entry for deref }
  330. {$ifndef NEWMAP}
  331. current_module^.map^[nextmapentry]:=loaded_unit^.globalsymtable;
  332. {$else NEWMAP}
  333. current_module^.map^[nextmapentry]:=loaded_unit;
  334. {$endif NEWMAP}
  335. inc(nextmapentry);
  336. if nextmapentry>maxunits then
  337. Message(unit_f_too_much_units);
  338. end;
  339. pu:=pused_unit(pu^.next);
  340. end;
  341. { load browser info if stored }
  342. if ((current_module^.flags and uf_has_browser)<>0) and load_refs then
  343. punitsymtable(current_module^.globalsymtable)^.load_symtable_refs;
  344. { remove the map, it's not needed anymore }
  345. dispose(current_module^.map);
  346. current_module^.map:=nil;
  347. end;
  348. function loadunit(const s : string;compile_system:boolean) : pmodule;
  349. const
  350. ImplIntf : array[boolean] of string[15]=('interface','implementation');
  351. var
  352. st : punitsymtable;
  353. second_time : boolean;
  354. old_current_ppu : pppufile;
  355. old_current_module,hp,hp2,hp3 : pmodule;
  356. name : string;{ necessary because
  357. current_module^.mainsource^ is reset in compile !! }
  358. scanner : pscannerfile;
  359. procedure loadppufile;
  360. begin
  361. { load interface section }
  362. if not current_module^.do_compile then
  363. load_interface;
  364. { only load units when we don't recompile }
  365. if not current_module^.do_compile then
  366. load_usedunits(compile_system);
  367. { recompile if set }
  368. if current_module^.do_compile then
  369. begin
  370. { we don't need the ppufile anymore }
  371. if assigned(current_module^.ppufile) then
  372. begin
  373. dispose(current_module^.ppufile,done);
  374. current_module^.ppufile:=nil;
  375. end;
  376. { recompile the unit or give a fatal error if sources not available }
  377. if not(current_module^.sources_avail) then
  378. if (not current_module^.search_unit(current_module^.modulename^,true))
  379. and (length(current_module^.modulename^)>8) then
  380. current_module^.search_unit(copy(current_module^.modulename^,1,8),true);
  381. if not(current_module^.sources_avail) then
  382. Message1(unit_f_cant_compile_unit,current_module^.modulename^)
  383. else
  384. begin
  385. if current_module^.in_second_compile then
  386. Message1(parser_d_compiling_second_time,current_module^.modulename^);
  387. current_scanner^.tempcloseinputfile;
  388. name:=current_module^.mainsource^;
  389. if assigned(scanner) then
  390. scanner^.invalid:=true;
  391. compile(name,compile_system);
  392. if (not current_scanner^.invalid) then
  393. current_scanner^.tempopeninputfile;
  394. end;
  395. end
  396. else
  397. begin
  398. { only reassemble ? }
  399. if (current_module^.do_assemble) then
  400. OnlyAsm;
  401. end;
  402. if assigned(current_module^.ppufile) then
  403. begin
  404. dispose(current_module^.ppufile,done);
  405. current_module^.ppufile:=nil;
  406. end;
  407. end;
  408. begin
  409. old_current_module:=current_module;
  410. old_current_ppu:=current_ppu;
  411. { Info }
  412. Message3(unit_u_load_unit,current_module^.modulename^,ImplIntf[current_module^.in_implementation],s);
  413. { unit not found }
  414. st:=nil;
  415. { search all loaded units }
  416. hp:=pmodule(loaded_units.first);
  417. while assigned(hp) do
  418. begin
  419. if hp^.modulename^=s then
  420. begin
  421. {$ifndef OLDPPU}
  422. { forced to reload ? }
  423. if hp^.do_reload then
  424. break;
  425. {$endif}
  426. { the unit is already registered }
  427. { and this means that the unit }
  428. { is already compiled }
  429. { else there is a cyclic unit use }
  430. if assigned(hp^.globalsymtable) then
  431. st:=punitsymtable(hp^.globalsymtable)
  432. else
  433. begin
  434. { both units in interface ? }
  435. if (not current_module^.in_implementation) and (not hp^.in_implementation) then
  436. begin
  437. { check for a cycle }
  438. hp2:=current_module^.loaded_from;
  439. while assigned(hp2) and (hp2<>hp) do
  440. begin
  441. if hp2^.in_implementation then
  442. hp2:=nil
  443. else
  444. hp2:=hp2^.loaded_from;
  445. end;
  446. if assigned(hp2) then
  447. Message2(unit_f_circular_unit_reference,current_module^.modulename^,hp^.modulename^);
  448. end;
  449. end;
  450. break;
  451. end;
  452. { the next unit }
  453. hp:=pmodule(hp^.next);
  454. end;
  455. { the unit is not in the symtable stack }
  456. if (not assigned(st)) then
  457. begin
  458. if assigned(hp) then
  459. begin
  460. { remove the old unit }
  461. loaded_units.remove(hp);
  462. scanner:=hp^.scanner;
  463. hp^.reset;
  464. {$ifndef OLDPPU}
  465. hp2:=pmodule(loaded_units.first);
  466. while assigned(hp2) do
  467. begin
  468. if hp2^.do_reload then
  469. begin
  470. hp2^.do_reload:=false;
  471. hp3:=loadunit(hp^.modulename^,false);
  472. end;
  473. hp2:=pmodule(hp2^.next);
  474. end;
  475. {$endif}
  476. hp^.scanner:=scanner;
  477. { try to reopen ppu }
  478. hp^.search_unit(s,false);
  479. { try to load the unit a second time first }
  480. current_module:=hp;
  481. current_module^.in_second_compile:=true;
  482. Message1(unit_u_second_load_unit,current_module^.modulename^);
  483. second_time:=true;
  484. end
  485. else
  486. { generates a new unit info record }
  487. begin
  488. current_module:=new(pmodule,init(s,true));
  489. scanner:=nil;
  490. second_time:=false;
  491. end;
  492. current_ppu:=current_module^.ppufile;
  493. { now we can register the unit }
  494. current_module^.loaded_from:=old_current_module;
  495. loaded_units.insert(current_module);
  496. { now realy load the ppu }
  497. loadppufile;
  498. { set compiled flag }
  499. current_module^.compiled:=true;
  500. { register the unit _once_ }
  501. { this is buggy PM }
  502. if not second_time then
  503. usedunits.concat(new(pused_unit,init(current_module,true)));
  504. { load return pointer }
  505. hp:=current_module;
  506. end;
  507. { set the old module }
  508. current_ppu:=old_current_ppu;
  509. current_module:=old_current_module;
  510. loadunit:=hp;
  511. end;
  512. procedure loaddefaultunits;
  513. var
  514. hp : pmodule;
  515. begin
  516. { are we compiling the system unit? }
  517. if (cs_compilesystem in aktmoduleswitches) then
  518. begin
  519. { create system defines }
  520. createconstdefs;
  521. { we don't need to reset anything, it's already done in parser.pas }
  522. exit;
  523. end;
  524. { insert the system unit, it is allways the first }
  525. hp:=loadunit(upper(target_info.system_unit),true);
  526. systemunit:=hp^.globalsymtable;
  527. { it's always the first unit }
  528. systemunit^.next:=nil;
  529. symtablestack:=systemunit;
  530. { add to the used units }
  531. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  532. refsymtable^.insert(new(punitsym,init('SYSTEM',systemunit)));
  533. { read default constant definitions }
  534. make_ref:=false;
  535. readconstdefs;
  536. make_ref:=true;
  537. { if POWER is defined in the RTL then use it for starstar overloading }
  538. getsym('POWER',false);
  539. if assigned(srsym) and (srsym^.typ=procsym) and (overloaded_operators[STARSTAR]=nil) then
  540. overloaded_operators[STARSTAR]:=pprocsym(srsym);
  541. { Objpas unit? }
  542. if m_objpas in aktmodeswitches then
  543. begin
  544. hp:=loadunit('OBJPAS',false);
  545. objpasunit:=hp^.globalsymtable;
  546. { insert in stack }
  547. objpasunit^.next:=symtablestack;
  548. symtablestack:=objpasunit;
  549. { add to the used units }
  550. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  551. refsymtable^.insert(new(punitsym,init('OBJPAS',objpasunit)));
  552. end
  553. else
  554. objpasunit:=nil;
  555. { Profile unit? Needed for go32v2 only }
  556. if (cs_profile in aktmoduleswitches) and (target_info.target=target_i386_go32v2) then
  557. begin
  558. hp:=loadunit('PROFILE',false);
  559. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  560. symtablestack:=hp^.globalsymtable;
  561. { add to the used units }
  562. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  563. refsymtable^.insert(new(punitsym,init('PROFILE',hp^.globalsymtable)));
  564. end;
  565. { Heaptrc unit? (not needed for units), this is here to be sure that it is really
  566. loaded as first unit }
  567. if (cs_gdb_heaptrc in aktglobalswitches) and not(current_module^.is_unit)then
  568. begin
  569. hp:=loadunit('HEAPTRC',false);
  570. psymtable(hp^.globalsymtable)^.next:=symtablestack;
  571. symtablestack:=hp^.globalsymtable;
  572. { add to the used units }
  573. current_module^.used_units.concat(new(pused_unit,init(hp,true)));
  574. refsymtable^.insert(new(punitsym,init('HEAPTRC',hp^.globalsymtable)));
  575. end;
  576. { save default symtablestack }
  577. defaultsymtablestack:=symtablestack;
  578. end;
  579. procedure loadunits;
  580. var
  581. s : stringid;
  582. pu,
  583. hp : pused_unit;
  584. hp2 : pmodule;
  585. hp3 : psymtable;
  586. oldprocsym:Pprocsym;
  587. begin
  588. oldprocsym:=aktprocsym;
  589. consume(_USES);
  590. {$ifdef DEBUG}
  591. test_symtablestack;
  592. {$endif DEBUG}
  593. repeat
  594. s:=pattern;
  595. consume(ID);
  596. { check if the unit is already used }
  597. pu:=pused_unit(current_module^.used_units.first);
  598. while assigned(pu) do
  599. begin
  600. if (pu^.name^=s) then
  601. break;
  602. pu:=pused_unit(pu^.next);
  603. end;
  604. { avoid uses of itself }
  605. if not assigned(pu) and (s<>current_module^.modulename^) then
  606. begin
  607. { load the unit }
  608. hp2:=loadunit(s,false);
  609. { the current module uses the unit hp2 }
  610. current_module^.used_units.concat(new(pused_unit,init(hp2,not current_module^.in_implementation)));
  611. pused_unit(current_module^.used_units.last)^.in_uses:=true;
  612. if current_module^.compiled then
  613. exit;
  614. refsymtable^.insert(new(punitsym,init(s,hp2^.globalsymtable)));
  615. end
  616. else
  617. Message1(sym_e_duplicate_id,s);
  618. if token=COMMA then
  619. begin
  620. pattern:='';
  621. consume(COMMA);
  622. end
  623. else
  624. break;
  625. until false;
  626. consume(SEMICOLON);
  627. { set the symtable to systemunit so it gets reorderd correctly }
  628. symtablestack:=defaultsymtablestack;
  629. { now insert the units in the symtablestack }
  630. hp:=pused_unit(current_module^.used_units.first);
  631. while assigned(hp) do
  632. begin
  633. {$IfDef GDB}
  634. {$IfnDef New_GDB}
  635. if (cs_debuginfo in aktmoduleswitches) and
  636. not hp^.is_stab_written then
  637. begin
  638. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  639. hp^.is_stab_written:=true;
  640. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  641. end;
  642. {$endIf nDef New_GDB}
  643. {$EndIf GDB}
  644. if hp^.in_uses then
  645. begin
  646. hp3:=symtablestack;
  647. while assigned(hp3) do
  648. begin
  649. { insert units only once ! }
  650. if hp^.u^.globalsymtable=hp3 then
  651. break;
  652. hp3:=hp3^.next;
  653. { unit isn't inserted }
  654. if hp3=nil then
  655. begin
  656. psymtable(hp^.u^.globalsymtable)^.next:=symtablestack;
  657. symtablestack:=psymtable(hp^.u^.globalsymtable);
  658. {$ifdef CHAINPROCSYMS}
  659. symtablestack^.chainprocsyms;
  660. {$endif CHAINPROCSYMS}
  661. {$ifdef DEBUG}
  662. test_symtablestack;
  663. {$endif DEBUG}
  664. end;
  665. end;
  666. end;
  667. hp:=pused_unit(hp^.next);
  668. end;
  669. aktprocsym:=oldprocsym;
  670. end;
  671. procedure write_gdb_info;
  672. {$IfDef GDB}
  673. var
  674. hp : pused_unit;
  675. begin
  676. if not (cs_debuginfo in aktmoduleswitches) then
  677. exit;
  678. { now insert the units in the symtablestack }
  679. hp:=pused_unit(current_module^.used_units.first);
  680. while assigned(hp) do
  681. begin
  682. if (cs_debuginfo in aktmoduleswitches) and
  683. not hp^.is_stab_written then
  684. begin
  685. punitsymtable(hp^.u^.globalsymtable)^.concattypestabto(debuglist);
  686. hp^.is_stab_written:=true;
  687. hp^.unitid:=psymtable(hp^.u^.globalsymtable)^.unitid;
  688. end;
  689. hp:=pused_unit(hp^.next);
  690. end;
  691. if current_module^.in_implementation then
  692. begin
  693. if assigned(current_module^.localsymtable) then
  694. begin
  695. { all types }
  696. punitsymtable(current_module^.localsymtable)^.concattypestabto(debuglist);
  697. { and all local symbols}
  698. punitsymtable(current_module^.localsymtable)^.concatstabto(debuglist);
  699. end;
  700. end
  701. else
  702. begin
  703. if assigned(current_module^.globalsymtable) then
  704. begin
  705. { all types }
  706. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  707. { and all local symbols}
  708. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  709. end;
  710. end;
  711. end;
  712. {$Else GDB}
  713. begin
  714. end;
  715. {$EndIf GDB}
  716. procedure parse_implementation_uses(symt:Psymtable);
  717. begin
  718. if token=_USES then
  719. begin
  720. symt^.symtabletype:=unitsymtable;
  721. loadunits;
  722. symt^.symtabletype:=globalsymtable;
  723. {$ifdef DEBUG}
  724. test_symtablestack;
  725. {$endif DEBUG}
  726. end;
  727. end;
  728. procedure gen_main_procsym(const name:string;options:longint;st:psymtable);
  729. {$ifndef OLDPPU}
  730. var
  731. stt : psymtable;
  732. {$endif}
  733. begin
  734. {Generate a procsym for main}
  735. make_ref:=false;
  736. aktprocsym:=new(Pprocsym,init(name));
  737. {$ifndef OLDPPU}
  738. {Try to insert in in static symtable ! }
  739. stt:=symtablestack;
  740. symtablestack:=st;
  741. {$endif}
  742. aktprocsym^.definition:=new(Pprocdef,init);
  743. {$ifndef OLDPPU}
  744. symtablestack:=stt;
  745. {$endif}
  746. aktprocsym^.definition^.options:=aktprocsym^.definition^.options or options;
  747. aktprocsym^.definition^.setmangledname(target_os.cprefix+name);
  748. aktprocsym^.definition^.forwarddef:=false;
  749. make_ref:=true;
  750. { The localst is a local symtable. Change it into the static
  751. symtable }
  752. dispose(aktprocsym^.definition^.localst,done);
  753. aktprocsym^.definition^.localst:=st;
  754. { and insert the procsym in symtable }
  755. st^.insert(aktprocsym);
  756. { set some informations about the main program }
  757. with procinfo do
  758. begin
  759. retdef:=voiddef;
  760. _class:=nil;
  761. call_offset:=8;
  762. framepointer:=frame_pointer;
  763. flags:=0;
  764. end;
  765. end;
  766. procedure proc_unit;
  767. function is_assembler_generated:boolean;
  768. begin
  769. is_assembler_generated:=(Errorcount=0) and
  770. not(
  771. codesegment^.empty and
  772. datasegment^.empty and
  773. bsssegment^.empty and
  774. ((importssection=nil) or importssection^.empty) and
  775. ((resourcesection=nil) or resourcesection^.empty)
  776. );
  777. end;
  778. var
  779. names : Tstringcontainer;
  780. st : psymtable;
  781. unitst : punitsymtable;
  782. {$ifdef GDB}
  783. pu : pused_unit;
  784. {$endif GDB}
  785. {$ifdef Test_Double_checksum}
  786. store_crc : longint;
  787. {$endif}
  788. s1,s2 : ^string; {Saves stack space}
  789. begin
  790. consume(_UNIT);
  791. if token=ID then
  792. begin
  793. { create filenames and unit name }
  794. current_module^.SetFileName(current_scanner^.inputfile^.path^+current_scanner^.inputfile^.name^,true);
  795. stringdispose(current_module^.modulename);
  796. current_module^.modulename:=stringdup(upper(pattern));
  797. { check for system unit }
  798. new(s1);
  799. new(s2);
  800. s1^:=upper(target_info.system_unit);
  801. s2^:=upper(SplitName(current_scanner^.inputfile^.name^));
  802. if (cs_compilesystem in aktmoduleswitches) then
  803. begin
  804. if ((length(current_module^.modulename^)>8) or
  805. (current_module^.modulename^<>s1^) or
  806. (current_module^.modulename^<>s2^)) then
  807. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  808. end
  809. else
  810. begin
  811. if (cs_check_unit_name in aktglobalswitches) and
  812. not((current_module^.modulename^=s2^) or
  813. ((length(current_module^.modulename^)>8) and
  814. (copy(current_module^.modulename^,1,8)=s2^))) then
  815. Message1(unit_e_illegal_unit_name,current_module^.modulename^);
  816. if (current_module^.modulename^=s1^) then
  817. Message(unit_w_switch_us_missed);
  818. end;
  819. dispose(s2);
  820. dispose(s1);
  821. end;
  822. consume(ID);
  823. consume(SEMICOLON);
  824. consume(_INTERFACE);
  825. { global switches are read, so further changes aren't allowed }
  826. current_module^.in_global:=false;
  827. { can't have local browser when no global browser }
  828. if (cs_local_browser in aktmoduleswitches) and
  829. not(cs_browser in aktmoduleswitches) then
  830. aktmoduleswitches:=aktmoduleswitches-[cs_local_browser];
  831. Message1(unit_u_start_parse_interface,current_module^.modulename^);
  832. { update status }
  833. status.currentmodule:=current_module^.modulename^;
  834. { maybe turn off m_objpas if we are compiling objpas }
  835. if (current_module^.modulename^='OBJPAS') then
  836. aktmodeswitches:=aktmodeswitches-[m_objpas];
  837. { this should be placed after uses !!}
  838. {$ifndef UseNiceNames}
  839. procprefix:='_'+current_module^.modulename^+'$$';
  840. {$else UseNiceNames}
  841. procprefix:='_'+tostr(length(current_module^.modulename^))+lowercase(current_module^.modulename^)+'_';
  842. {$endif UseNiceNames}
  843. parse_only:=true;
  844. { generate now the global symboltable }
  845. st:=new(punitsymtable,init(globalsymtable,current_module^.modulename^));
  846. refsymtable:=st;
  847. unitst:=punitsymtable(st);
  848. { define first as local to overcome dependency conflicts }
  849. current_module^.localsymtable:=st;
  850. { the unit name must be usable as a unit specifier }
  851. { inside the unit itself (PM) }
  852. { this also forbids to have another symbol }
  853. { with the same name as the unit }
  854. refsymtable^.insert(new(punitsym,init(current_module^.modulename^,unitst)));
  855. { a unit compiled at command line must be inside the loaded_unit list }
  856. if (compile_level=1) then
  857. loaded_units.insert(current_module);
  858. { load default units, like the system unit }
  859. loaddefaultunits;
  860. { reset }
  861. make_ref:=true;
  862. lexlevel:=0;
  863. { insert qualifier for the system unit (allows system.writeln) }
  864. if not(cs_compilesystem in aktmoduleswitches) then
  865. begin
  866. if token=_USES then
  867. begin
  868. unitst^.symtabletype:=unitsymtable;
  869. loadunits;
  870. { has it been compiled at a higher level ?}
  871. if current_module^.compiled then
  872. begin
  873. { this unit symtable is obsolete }
  874. { dispose(unitst,done);
  875. disposed as localsymtable !! }
  876. exit;
  877. end;
  878. unitst^.symtabletype:=globalsymtable;
  879. end;
  880. { ... but insert the symbol table later }
  881. st^.next:=symtablestack;
  882. symtablestack:=st;
  883. end
  884. else
  885. { while compiling a system unit, some types are directly inserted }
  886. begin
  887. st^.next:=symtablestack;
  888. symtablestack:=st;
  889. insert_intern_types(st);
  890. end;
  891. { now we know the place to insert the constants }
  892. constsymtable:=symtablestack;
  893. { move the global symtab from the temporary local to global }
  894. current_module^.globalsymtable:=current_module^.localsymtable;
  895. current_module^.localsymtable:=nil;
  896. reset_global_defs;
  897. { ... parse the declarations }
  898. read_interface_declarations;
  899. { leave when we got an error }
  900. if (Errorcount>0) and not status.skip_error then
  901. begin
  902. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  903. status.skip_error:=true;
  904. exit;
  905. end;
  906. {$ifdef New_GDB}
  907. write_gdb_info;
  908. {$endIf Def New_GDB}
  909. {$ifndef OLDPPU}
  910. {$ifdef Test_Double_checksum}
  911. if (Errorcount=0) then
  912. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),true);
  913. {$endif Test_Double_checksum}
  914. {$endif OLDPPU}
  915. { Parse the implementation section }
  916. consume(_IMPLEMENTATION);
  917. current_module^.in_implementation:=true;
  918. Message1(unit_u_start_parse_implementation,current_module^.modulename^);
  919. parse_only:=false;
  920. { generates static symbol table }
  921. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  922. current_module^.localsymtable:=st;
  923. { remove the globalsymtable from the symtable stack }
  924. { to reinsert it after loading the implementation units }
  925. symtablestack:=unitst^.next;
  926. {$ifdef OLDPPU}
  927. { number the definitions, so a deref from other units works }
  928. refsymtable^.number_defs;
  929. refsymtable^.number_symbols;
  930. {$endif}
  931. { we don't want implementation units symbols in unitsymtable !! PM }
  932. refsymtable:=st;
  933. { Read the implementation units }
  934. parse_implementation_uses(unitst);
  935. if current_module^.compiled then
  936. begin
  937. exit;
  938. end;
  939. { reset ranges/stabs in exported definitions }
  940. reset_global_defs;
  941. { All units are read, now give them a number }
  942. numberunits;
  943. { now we can change refsymtable }
  944. refsymtable:=st;
  945. { but reinsert the global symtable as lasts }
  946. unitst^.next:=symtablestack;
  947. symtablestack:=unitst;
  948. {$ifdef DEBUG}
  949. test_symtablestack;
  950. {$endif DEBUG}
  951. constsymtable:=symtablestack;
  952. {$ifdef Splitheap}
  953. if testsplit then
  954. begin
  955. Split_Heap;
  956. allow_special:=true;
  957. Switch_to_temp_heap;
  958. end;
  959. { it will report all crossings }
  960. allow_special:=false;
  961. {$endif Splitheap}
  962. { Generate a procsym }
  963. gen_main_procsym(current_module^.modulename^+'_init',pounitinit,st);
  964. { Compile the unit }
  965. codegen_newprocedure;
  966. names.init;
  967. names.insert('INIT$$'+current_module^.modulename^);
  968. names.insert(target_os.cprefix+current_module^.modulename^+'_init');
  969. compile_proc_body(names,true,false);
  970. names.done;
  971. codegen_doneprocedure;
  972. { avoid self recursive destructor call !! PM }
  973. aktprocsym^.definition^.localst:=nil;
  974. { finalize? }
  975. if token=_FINALIZATION then
  976. begin
  977. { set module options }
  978. current_module^.flags:=current_module^.flags or uf_finalize;
  979. { Generate a procsym }
  980. gen_main_procsym(current_module^.modulename^+'_finalize',pounitfinalize,st);
  981. { Compile the finalize }
  982. codegen_newprocedure;
  983. names.init;
  984. names.insert('FINALIZE$$'+current_module^.modulename^);
  985. names.insert(target_os.cprefix+current_module^.modulename^+'_finalize');
  986. compile_proc_body(names,true,false);
  987. names.done;
  988. codegen_doneprocedure;
  989. { avoid self recursive destructor call !! PM }
  990. aktprocsym^.definition^.localst:=nil;
  991. end;
  992. { the last char should always be a point }
  993. consume(POINT);
  994. { avoid self recursive destructor call !! PM }
  995. aktprocsym^.definition^.localst:=nil;
  996. { absence does not matter here !! }
  997. aktprocsym^.definition^.forwarddef:=false;
  998. { test static symtable }
  999. if (Errorcount=0) then
  1000. st^.allsymbolsused;
  1001. { size of the static data }
  1002. datasize:=st^.datasize;
  1003. {$ifdef GDB}
  1004. { add all used definitions even for implementation}
  1005. if (cs_debuginfo in aktmoduleswitches) then
  1006. begin
  1007. {$IfnDef New_GDB}
  1008. if assigned(current_module^.globalsymtable) then
  1009. begin
  1010. { all types }
  1011. punitsymtable(current_module^.globalsymtable)^.concattypestabto(debuglist);
  1012. { and all local symbols}
  1013. punitsymtable(current_module^.globalsymtable)^.concatstabto(debuglist);
  1014. end;
  1015. { all local types }
  1016. punitsymtable(st)^.concattypestabto(debuglist);
  1017. { and all local symbols}
  1018. st^.concatstabto(debuglist);
  1019. {$else New_GDB}
  1020. write_gdb_info;
  1021. {$endIf Def New_GDB}
  1022. end;
  1023. {$endif GDB}
  1024. reset_global_defs;
  1025. { tests, if all (interface) forwards are resolved }
  1026. if (Errorcount=0) then
  1027. symtablestack^.check_forwards;
  1028. { now we have a correct unit, change the symtable type }
  1029. current_module^.in_implementation:=false;
  1030. symtablestack^.symtabletype:=unitsymtable;
  1031. {$ifdef GDB}
  1032. punitsymtable(symtablestack)^.is_stab_written:=false;
  1033. {$endif GDB}
  1034. { leave when we got an error }
  1035. if (Errorcount>0) and not status.skip_error then
  1036. begin
  1037. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1038. status.skip_error:=true;
  1039. exit;
  1040. end;
  1041. { generate imports }
  1042. if current_module^.uses_imports then
  1043. importlib^.generatelib;
  1044. { insert own objectfile, or say that it's in a library
  1045. (no check for an .o when loading) }
  1046. if is_assembler_generated then
  1047. insertobjectfile;
  1048. if cs_local_browser in aktmoduleswitches then
  1049. current_module^.localsymtable:=refsymtable;
  1050. { Write out the ppufile }
  1051. {$ifndef OLDPPU}
  1052. {$ifdef Test_Double_checksum}
  1053. store_crc:=current_module^.interface_crc;
  1054. {$endif Test_Double_checksum}
  1055. {$endif}
  1056. if (Errorcount=0) then
  1057. writeunitas(current_module^.ppufilename^,punitsymtable(symtablestack),false);
  1058. {$ifndef OLDPPU}
  1059. {$ifdef Test_Double_checksum}
  1060. if store_crc<>current_module^.interface_crc then
  1061. Def_comment(V_Warning,current_module^.ppufilename^+' CRC changed '+
  1062. tostr(store_crc)+'<>'+tostr(current_module^.interface_crc));
  1063. {$endif def Test_Double_checksum}
  1064. {$endif OLDPPU}
  1065. { must be done only after local symtable ref stores !! }
  1066. closecurrentppu;
  1067. {$ifdef GDB}
  1068. pu:=pused_unit(usedunits.first);
  1069. while assigned(pu) do
  1070. begin
  1071. punitsymtable(pu^.u^.globalsymtable)^.is_stab_written:=false;
  1072. pu:=pused_unit(pu^.next);
  1073. end;
  1074. {$endif GDB}
  1075. { remove static symtable (=refsymtable) here to save some mem }
  1076. if not (cs_local_browser in aktmoduleswitches) then
  1077. begin
  1078. dispose(st,done);
  1079. current_module^.localsymtable:=nil;
  1080. end;
  1081. if is_assembler_generated then
  1082. begin
  1083. { finish asmlist by adding segment starts }
  1084. insertsegment;
  1085. { assemble }
  1086. create_objectfile;
  1087. end;
  1088. end;
  1089. procedure proc_program(islibrary : boolean);
  1090. var
  1091. st : psymtable;
  1092. hp : pmodule;
  1093. names : Tstringcontainer;
  1094. begin
  1095. DLLsource:=islibrary;
  1096. parse_only:=false;
  1097. if islibrary then
  1098. begin
  1099. consume(_LIBRARY);
  1100. stringdispose(current_module^.modulename);
  1101. current_module^.modulename:=stringdup(pattern);
  1102. current_module^.islibrary:=true;
  1103. exportlib^.preparelib(pattern);
  1104. consume(ID);
  1105. consume(SEMICOLON);
  1106. end
  1107. else
  1108. { is there an program head ? }
  1109. if token=_PROGRAM then
  1110. begin
  1111. consume(_PROGRAM);
  1112. stringdispose(current_module^.modulename);
  1113. current_module^.modulename:=stringdup(pattern);
  1114. consume(ID);
  1115. if token=LKLAMMER then
  1116. begin
  1117. consume(LKLAMMER);
  1118. idlist;
  1119. consume(RKLAMMER);
  1120. end;
  1121. consume(SEMICOLON);
  1122. end;
  1123. { global switches are read, so further changes aren't allowed }
  1124. current_module^.in_global:=false;
  1125. { can't have local browser when no global browser }
  1126. if (cs_local_browser in aktmoduleswitches) and
  1127. not(cs_browser in aktmoduleswitches) then
  1128. aktmoduleswitches:=aktmoduleswitches-[cs_local_browser];
  1129. { set implementation flag }
  1130. current_module^.in_implementation:=true;
  1131. { insert after the unit symbol tables the static symbol table }
  1132. { of the program }
  1133. st:=new(punitsymtable,init(staticsymtable,current_module^.modulename^));
  1134. current_module^.localsymtable:=st;
  1135. symtablestack:=st;
  1136. refsymtable:=st;
  1137. { necessary for browser }
  1138. loaded_units.insert(current_module);
  1139. { load standard units (system,objpas,profile unit) }
  1140. loaddefaultunits;
  1141. { reset }
  1142. lexlevel:=0;
  1143. {Load the units used by the program we compile.}
  1144. if token=_USES then
  1145. loadunits;
  1146. reset_global_defs;
  1147. {Insert the name of the main program into the symbol table.}
  1148. if current_module^.modulename^<>'' then
  1149. {st^.insert(new(pprogramsym,init(current_module^.modulename^)));}
  1150. st^.insert(new(punitsym,init(current_module^.modulename^,punitsymtable(st))));
  1151. { ...is also constsymtable, this is the symtable where }
  1152. { the elements of enumeration types are inserted }
  1153. constsymtable:=st;
  1154. { Generate a procsym for main }
  1155. gen_main_procsym('main',poproginit,st);
  1156. { reset }
  1157. procprefix:='';
  1158. in_except_block:=false;
  1159. {The program intialization needs an alias, so it can be called
  1160. from the bootstrap code.}
  1161. codegen_newprocedure;
  1162. names.init;
  1163. names.insert('program_init');
  1164. names.insert('PASCALMAIN');
  1165. names.insert(target_os.cprefix+'main');
  1166. {$ifdef m68k}
  1167. if target_info.target=target_m68k_PalmOS then
  1168. names.insert('PilotMain');
  1169. {$endif}
  1170. compile_proc_body(names,true,false);
  1171. names.done;
  1172. { avoid self recursive destructor call !! PM }
  1173. aktprocsym^.definition^.localst:=nil;
  1174. { consider these symbols as global ones }
  1175. { for browser }
  1176. current_module^.globalsymtable:=current_module^.localsymtable;
  1177. current_module^.localsymtable:=nil;
  1178. codegen_doneprocedure;
  1179. { consume the last point }
  1180. consume(POINT);
  1181. {$ifdef New_GDB}
  1182. write_gdb_info;
  1183. {$endIf Def New_GDB}
  1184. { leave when we got an error }
  1185. if (Errorcount>0) and not status.skip_error then
  1186. begin
  1187. Message1(unit_f_errors_in_unit,tostr(Errorcount));
  1188. status.skip_error:=true;
  1189. exit;
  1190. end;
  1191. { generate imports }
  1192. if current_module^.uses_imports then
  1193. importlib^.generatelib;
  1194. if islibrary then
  1195. exportlib^.generatelib;
  1196. { insert heap }
  1197. insertinitfinaltable;
  1198. insertheap;
  1199. inserttargetspecific;
  1200. datasize:=symtablestack^.datasize;
  1201. { finish asmlist by adding segment starts }
  1202. insertsegment;
  1203. { insert own objectfile }
  1204. insertobjectfile;
  1205. { assemble and link }
  1206. create_objectfile;
  1207. { create the executable when we are at level 1 }
  1208. if (compile_level=1) then
  1209. begin
  1210. { insert all .o files from all loaded units }
  1211. hp:=pmodule(loaded_units.first);
  1212. while assigned(hp) do
  1213. begin
  1214. Linker.AddModuleFiles(hp);
  1215. hp:=pmodule(hp^.next);
  1216. end;
  1217. { write .def file }
  1218. if (cs_link_deffile in aktglobalswitches) then
  1219. deffile.writefile;
  1220. { finally we can create a executable }
  1221. if (not current_module^.is_unit) then
  1222. Linker.MakeExecutable;
  1223. end;
  1224. end;
  1225. end.
  1226. {
  1227. $Log$
  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. }