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