pmodules.pas 64 KB

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