pmodules.pas 64 KB

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