pmodules.pas 61 KB

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