pmodules.pas 62 KB

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