pmodules.pas 64 KB

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