pmodules.pas 63 KB

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