pmodules.pas 63 KB

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