pmodules.pas 63 KB

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